xref: /openbmc/linux/fs/f2fs/namei.c (revision 4cfb9080)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/f2fs/namei.c
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/pagemap.h>
11 #include <linux/sched.h>
12 #include <linux/ctype.h>
13 #include <linux/random.h>
14 #include <linux/dcache.h>
15 #include <linux/namei.h>
16 #include <linux/quotaops.h>
17 
18 #include "f2fs.h"
19 #include "node.h"
20 #include "segment.h"
21 #include "xattr.h"
22 #include "acl.h"
23 #include <trace/events/f2fs.h>
24 
25 static inline bool is_extension_exist(const unsigned char *s, const char *sub,
26 						bool tmp_ext, bool tmp_dot)
27 {
28 	size_t slen = strlen(s);
29 	size_t sublen = strlen(sub);
30 	int i;
31 
32 	if (sublen == 1 && *sub == '*')
33 		return true;
34 
35 	/*
36 	 * filename format of multimedia file should be defined as:
37 	 * "filename + '.' + extension + (optional: '.' + temp extension)".
38 	 */
39 	if (slen < sublen + 2)
40 		return false;
41 
42 	if (!tmp_ext) {
43 		/* file has no temp extension */
44 		if (s[slen - sublen - 1] != '.')
45 			return false;
46 		return !strncasecmp(s + slen - sublen, sub, sublen);
47 	}
48 
49 	for (i = 1; i < slen - sublen; i++) {
50 		if (s[i] != '.')
51 			continue;
52 		if (!strncasecmp(s + i + 1, sub, sublen)) {
53 			if (!tmp_dot)
54 				return true;
55 			if (i == slen - sublen - 1 || s[i + 1 + sublen] == '.')
56 				return true;
57 		}
58 	}
59 
60 	return false;
61 }
62 
63 static inline bool is_temperature_extension(const unsigned char *s, const char *sub)
64 {
65 	return is_extension_exist(s, sub, true, false);
66 }
67 
68 static inline bool is_compress_extension(const unsigned char *s, const char *sub)
69 {
70 	return is_extension_exist(s, sub, true, true);
71 }
72 
73 int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
74 							bool hot, bool set)
75 {
76 	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
77 	int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
78 	int hot_count = sbi->raw_super->hot_ext_count;
79 	int total_count = cold_count + hot_count;
80 	int start, count;
81 	int i;
82 
83 	if (set) {
84 		if (total_count == F2FS_MAX_EXTENSION)
85 			return -EINVAL;
86 	} else {
87 		if (!hot && !cold_count)
88 			return -EINVAL;
89 		if (hot && !hot_count)
90 			return -EINVAL;
91 	}
92 
93 	if (hot) {
94 		start = cold_count;
95 		count = total_count;
96 	} else {
97 		start = 0;
98 		count = cold_count;
99 	}
100 
101 	for (i = start; i < count; i++) {
102 		if (strcmp(name, extlist[i]))
103 			continue;
104 
105 		if (set)
106 			return -EINVAL;
107 
108 		memcpy(extlist[i], extlist[i + 1],
109 				F2FS_EXTENSION_LEN * (total_count - i - 1));
110 		memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
111 		if (hot)
112 			sbi->raw_super->hot_ext_count = hot_count - 1;
113 		else
114 			sbi->raw_super->extension_count =
115 						cpu_to_le32(cold_count - 1);
116 		return 0;
117 	}
118 
119 	if (!set)
120 		return -EINVAL;
121 
122 	if (hot) {
123 		memcpy(extlist[count], name, strlen(name));
124 		sbi->raw_super->hot_ext_count = hot_count + 1;
125 	} else {
126 		char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
127 
128 		memcpy(buf, &extlist[cold_count],
129 				F2FS_EXTENSION_LEN * hot_count);
130 		memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
131 		memcpy(extlist[cold_count], name, strlen(name));
132 		memcpy(&extlist[cold_count + 1], buf,
133 				F2FS_EXTENSION_LEN * hot_count);
134 		sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
135 	}
136 	return 0;
137 }
138 
139 static void set_compress_new_inode(struct f2fs_sb_info *sbi, struct inode *dir,
140 				struct inode *inode, const unsigned char *name)
141 {
142 	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
143 	unsigned char (*noext)[F2FS_EXTENSION_LEN] =
144 						F2FS_OPTION(sbi).noextensions;
145 	unsigned char (*ext)[F2FS_EXTENSION_LEN] = F2FS_OPTION(sbi).extensions;
146 	unsigned char ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
147 	unsigned char noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
148 	int i, cold_count, hot_count;
149 
150 	if (!f2fs_sb_has_compression(sbi))
151 		return;
152 
153 	if (S_ISDIR(inode->i_mode))
154 		goto inherit_comp;
155 
156 	/* This name comes only from normal files. */
157 	if (!name)
158 		return;
159 
160 	/* Don't compress hot files. */
161 	f2fs_down_read(&sbi->sb_lock);
162 	cold_count = le32_to_cpu(sbi->raw_super->extension_count);
163 	hot_count = sbi->raw_super->hot_ext_count;
164 	for (i = cold_count; i < cold_count + hot_count; i++)
165 		if (is_temperature_extension(name, extlist[i]))
166 			break;
167 	f2fs_up_read(&sbi->sb_lock);
168 	if (i < (cold_count + hot_count))
169 		return;
170 
171 	/* Don't compress unallowed extension. */
172 	for (i = 0; i < noext_cnt; i++)
173 		if (is_compress_extension(name, noext[i]))
174 			return;
175 
176 	/* Compress wanting extension. */
177 	for (i = 0; i < ext_cnt; i++) {
178 		if (is_compress_extension(name, ext[i])) {
179 			set_compress_context(inode);
180 			return;
181 		}
182 	}
183 inherit_comp:
184 	/* Inherit the {no-}compression flag in directory */
185 	if (F2FS_I(dir)->i_flags & F2FS_NOCOMP_FL) {
186 		F2FS_I(inode)->i_flags |= F2FS_NOCOMP_FL;
187 		f2fs_mark_inode_dirty_sync(inode, true);
188 	} else if (F2FS_I(dir)->i_flags & F2FS_COMPR_FL) {
189 		set_compress_context(inode);
190 	}
191 }
192 
193 /*
194  * Set file's temperature for hot/cold data separation
195  */
196 static void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
197 		const unsigned char *name)
198 {
199 	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
200 	int i, cold_count, hot_count;
201 
202 	f2fs_down_read(&sbi->sb_lock);
203 	cold_count = le32_to_cpu(sbi->raw_super->extension_count);
204 	hot_count = sbi->raw_super->hot_ext_count;
205 	for (i = 0; i < cold_count + hot_count; i++)
206 		if (is_temperature_extension(name, extlist[i]))
207 			break;
208 	f2fs_up_read(&sbi->sb_lock);
209 
210 	if (i == cold_count + hot_count)
211 		return;
212 
213 	if (i < cold_count)
214 		file_set_cold(inode);
215 	else
216 		file_set_hot(inode);
217 }
218 
219 static struct inode *f2fs_new_inode(struct mnt_idmap *idmap,
220 						struct inode *dir, umode_t mode,
221 						const char *name)
222 {
223 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
224 	nid_t ino;
225 	struct inode *inode;
226 	bool nid_free = false;
227 	bool encrypt = false;
228 	int xattr_size = 0;
229 	int err;
230 
231 	inode = new_inode(dir->i_sb);
232 	if (!inode)
233 		return ERR_PTR(-ENOMEM);
234 
235 	if (!f2fs_alloc_nid(sbi, &ino)) {
236 		err = -ENOSPC;
237 		goto fail;
238 	}
239 
240 	nid_free = true;
241 
242 	inode_init_owner(idmap, inode, dir, mode);
243 
244 	inode->i_ino = ino;
245 	inode->i_blocks = 0;
246 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
247 	F2FS_I(inode)->i_crtime = inode->i_mtime;
248 	inode->i_generation = get_random_u32();
249 
250 	if (S_ISDIR(inode->i_mode))
251 		F2FS_I(inode)->i_current_depth = 1;
252 
253 	err = insert_inode_locked(inode);
254 	if (err) {
255 		err = -EINVAL;
256 		goto fail;
257 	}
258 
259 	if (f2fs_sb_has_project_quota(sbi) &&
260 		(F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
261 		F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
262 	else
263 		F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
264 							F2FS_DEF_PROJID);
265 
266 	err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
267 	if (err)
268 		goto fail_drop;
269 
270 	err = f2fs_dquot_initialize(inode);
271 	if (err)
272 		goto fail_drop;
273 
274 	set_inode_flag(inode, FI_NEW_INODE);
275 
276 	if (encrypt)
277 		f2fs_set_encrypted_inode(inode);
278 
279 	if (f2fs_sb_has_extra_attr(sbi)) {
280 		set_inode_flag(inode, FI_EXTRA_ATTR);
281 		F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
282 	}
283 
284 	if (test_opt(sbi, INLINE_XATTR))
285 		set_inode_flag(inode, FI_INLINE_XATTR);
286 
287 	if (f2fs_may_inline_dentry(inode))
288 		set_inode_flag(inode, FI_INLINE_DENTRY);
289 
290 	if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
291 		f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
292 		if (f2fs_has_inline_xattr(inode))
293 			xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
294 		/* Otherwise, will be 0 */
295 	} else if (f2fs_has_inline_xattr(inode) ||
296 				f2fs_has_inline_dentry(inode)) {
297 		xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
298 	}
299 	F2FS_I(inode)->i_inline_xattr_size = xattr_size;
300 
301 	F2FS_I(inode)->i_flags =
302 		f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
303 
304 	if (S_ISDIR(inode->i_mode))
305 		F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
306 
307 	if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
308 		set_inode_flag(inode, FI_PROJ_INHERIT);
309 
310 	/* Check compression first. */
311 	set_compress_new_inode(sbi, dir, inode, name);
312 
313 	/* Should enable inline_data after compression set */
314 	if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
315 		set_inode_flag(inode, FI_INLINE_DATA);
316 
317 	if (name && !test_opt(sbi, DISABLE_EXT_IDENTIFY))
318 		set_file_temperature(sbi, inode, name);
319 
320 	stat_inc_inline_xattr(inode);
321 	stat_inc_inline_inode(inode);
322 	stat_inc_inline_dir(inode);
323 
324 	f2fs_set_inode_flags(inode);
325 
326 	f2fs_init_extent_tree(inode);
327 
328 	trace_f2fs_new_inode(inode, 0);
329 	return inode;
330 
331 fail:
332 	trace_f2fs_new_inode(inode, err);
333 	make_bad_inode(inode);
334 	if (nid_free)
335 		set_inode_flag(inode, FI_FREE_NID);
336 	iput(inode);
337 	return ERR_PTR(err);
338 fail_drop:
339 	trace_f2fs_new_inode(inode, err);
340 	dquot_drop(inode);
341 	inode->i_flags |= S_NOQUOTA;
342 	if (nid_free)
343 		set_inode_flag(inode, FI_FREE_NID);
344 	clear_nlink(inode);
345 	unlock_new_inode(inode);
346 	iput(inode);
347 	return ERR_PTR(err);
348 }
349 
350 static int f2fs_create(struct mnt_idmap *idmap, struct inode *dir,
351 		       struct dentry *dentry, umode_t mode, bool excl)
352 {
353 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
354 	struct inode *inode;
355 	nid_t ino = 0;
356 	int err;
357 
358 	if (unlikely(f2fs_cp_error(sbi)))
359 		return -EIO;
360 	if (!f2fs_is_checkpoint_ready(sbi))
361 		return -ENOSPC;
362 
363 	err = f2fs_dquot_initialize(dir);
364 	if (err)
365 		return err;
366 
367 	inode = f2fs_new_inode(idmap, dir, mode, dentry->d_name.name);
368 	if (IS_ERR(inode))
369 		return PTR_ERR(inode);
370 
371 	inode->i_op = &f2fs_file_inode_operations;
372 	inode->i_fop = &f2fs_file_operations;
373 	inode->i_mapping->a_ops = &f2fs_dblock_aops;
374 	ino = inode->i_ino;
375 
376 	f2fs_lock_op(sbi);
377 	err = f2fs_add_link(dentry, inode);
378 	if (err)
379 		goto out;
380 	f2fs_unlock_op(sbi);
381 
382 	f2fs_alloc_nid_done(sbi, ino);
383 
384 	d_instantiate_new(dentry, inode);
385 
386 	if (IS_DIRSYNC(dir))
387 		f2fs_sync_fs(sbi->sb, 1);
388 
389 	f2fs_balance_fs(sbi, true);
390 	return 0;
391 out:
392 	f2fs_handle_failed_inode(inode);
393 	return err;
394 }
395 
396 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
397 		struct dentry *dentry)
398 {
399 	struct inode *inode = d_inode(old_dentry);
400 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
401 	int err;
402 
403 	if (unlikely(f2fs_cp_error(sbi)))
404 		return -EIO;
405 	if (!f2fs_is_checkpoint_ready(sbi))
406 		return -ENOSPC;
407 
408 	err = fscrypt_prepare_link(old_dentry, dir, dentry);
409 	if (err)
410 		return err;
411 
412 	if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
413 			(!projid_eq(F2FS_I(dir)->i_projid,
414 			F2FS_I(old_dentry->d_inode)->i_projid)))
415 		return -EXDEV;
416 
417 	err = f2fs_dquot_initialize(dir);
418 	if (err)
419 		return err;
420 
421 	f2fs_balance_fs(sbi, true);
422 
423 	inode->i_ctime = current_time(inode);
424 	ihold(inode);
425 
426 	set_inode_flag(inode, FI_INC_LINK);
427 	f2fs_lock_op(sbi);
428 	err = f2fs_add_link(dentry, inode);
429 	if (err)
430 		goto out;
431 	f2fs_unlock_op(sbi);
432 
433 	d_instantiate(dentry, inode);
434 
435 	if (IS_DIRSYNC(dir))
436 		f2fs_sync_fs(sbi->sb, 1);
437 	return 0;
438 out:
439 	clear_inode_flag(inode, FI_INC_LINK);
440 	iput(inode);
441 	f2fs_unlock_op(sbi);
442 	return err;
443 }
444 
445 struct dentry *f2fs_get_parent(struct dentry *child)
446 {
447 	struct page *page;
448 	unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot_name, &page);
449 
450 	if (!ino) {
451 		if (IS_ERR(page))
452 			return ERR_CAST(page);
453 		return ERR_PTR(-ENOENT);
454 	}
455 	return d_obtain_alias(f2fs_iget(child->d_sb, ino));
456 }
457 
458 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
459 {
460 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
461 	struct qstr dot = QSTR_INIT(".", 1);
462 	struct qstr dotdot = QSTR_INIT("..", 2);
463 	struct f2fs_dir_entry *de;
464 	struct page *page;
465 	int err = 0;
466 
467 	if (f2fs_readonly(sbi->sb)) {
468 		f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
469 			  dir->i_ino, pino);
470 		return 0;
471 	}
472 
473 	if (!S_ISDIR(dir->i_mode)) {
474 		f2fs_err(sbi, "inconsistent inode status, skip recovering inline_dots inode (ino:%lu, i_mode:%u, pino:%u)",
475 			  dir->i_ino, dir->i_mode, pino);
476 		set_sbi_flag(sbi, SBI_NEED_FSCK);
477 		return -ENOTDIR;
478 	}
479 
480 	err = f2fs_dquot_initialize(dir);
481 	if (err)
482 		return err;
483 
484 	f2fs_balance_fs(sbi, true);
485 
486 	f2fs_lock_op(sbi);
487 
488 	de = f2fs_find_entry(dir, &dot, &page);
489 	if (de) {
490 		f2fs_put_page(page, 0);
491 	} else if (IS_ERR(page)) {
492 		err = PTR_ERR(page);
493 		goto out;
494 	} else {
495 		err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
496 		if (err)
497 			goto out;
498 	}
499 
500 	de = f2fs_find_entry(dir, &dotdot, &page);
501 	if (de)
502 		f2fs_put_page(page, 0);
503 	else if (IS_ERR(page))
504 		err = PTR_ERR(page);
505 	else
506 		err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
507 out:
508 	if (!err)
509 		clear_inode_flag(dir, FI_INLINE_DOTS);
510 
511 	f2fs_unlock_op(sbi);
512 	return err;
513 }
514 
515 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
516 		unsigned int flags)
517 {
518 	struct inode *inode = NULL;
519 	struct f2fs_dir_entry *de;
520 	struct page *page;
521 	struct dentry *new;
522 	nid_t ino = -1;
523 	int err = 0;
524 	unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
525 	struct f2fs_filename fname;
526 
527 	trace_f2fs_lookup_start(dir, dentry, flags);
528 
529 	if (dentry->d_name.len > F2FS_NAME_LEN) {
530 		err = -ENAMETOOLONG;
531 		goto out;
532 	}
533 
534 	err = f2fs_prepare_lookup(dir, dentry, &fname);
535 	generic_set_encrypted_ci_d_ops(dentry);
536 	if (err == -ENOENT)
537 		goto out_splice;
538 	if (err)
539 		goto out;
540 	de = __f2fs_find_entry(dir, &fname, &page);
541 	f2fs_free_filename(&fname);
542 
543 	if (!de) {
544 		if (IS_ERR(page)) {
545 			err = PTR_ERR(page);
546 			goto out;
547 		}
548 		err = -ENOENT;
549 		goto out_splice;
550 	}
551 
552 	ino = le32_to_cpu(de->ino);
553 	f2fs_put_page(page, 0);
554 
555 	inode = f2fs_iget(dir->i_sb, ino);
556 	if (IS_ERR(inode)) {
557 		err = PTR_ERR(inode);
558 		goto out;
559 	}
560 
561 	if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
562 		err = __recover_dot_dentries(dir, root_ino);
563 		if (err)
564 			goto out_iput;
565 	}
566 
567 	if (f2fs_has_inline_dots(inode)) {
568 		err = __recover_dot_dentries(inode, dir->i_ino);
569 		if (err)
570 			goto out_iput;
571 	}
572 	if (IS_ENCRYPTED(dir) &&
573 	    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
574 	    !fscrypt_has_permitted_context(dir, inode)) {
575 		f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
576 			  dir->i_ino, inode->i_ino);
577 		err = -EPERM;
578 		goto out_iput;
579 	}
580 out_splice:
581 #if IS_ENABLED(CONFIG_UNICODE)
582 	if (!inode && IS_CASEFOLDED(dir)) {
583 		/* Eventually we want to call d_add_ci(dentry, NULL)
584 		 * for negative dentries in the encoding case as
585 		 * well.  For now, prevent the negative dentry
586 		 * from being cached.
587 		 */
588 		trace_f2fs_lookup_end(dir, dentry, ino, err);
589 		return NULL;
590 	}
591 #endif
592 	new = d_splice_alias(inode, dentry);
593 	trace_f2fs_lookup_end(dir, !IS_ERR_OR_NULL(new) ? new : dentry,
594 				ino, IS_ERR(new) ? PTR_ERR(new) : err);
595 	return new;
596 out_iput:
597 	iput(inode);
598 out:
599 	trace_f2fs_lookup_end(dir, dentry, ino, err);
600 	return ERR_PTR(err);
601 }
602 
603 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
604 {
605 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
606 	struct inode *inode = d_inode(dentry);
607 	struct f2fs_dir_entry *de;
608 	struct page *page;
609 	int err;
610 
611 	trace_f2fs_unlink_enter(dir, dentry);
612 
613 	if (unlikely(f2fs_cp_error(sbi))) {
614 		err = -EIO;
615 		goto fail;
616 	}
617 
618 	err = f2fs_dquot_initialize(dir);
619 	if (err)
620 		goto fail;
621 	err = f2fs_dquot_initialize(inode);
622 	if (err)
623 		goto fail;
624 
625 	de = f2fs_find_entry(dir, &dentry->d_name, &page);
626 	if (!de) {
627 		if (IS_ERR(page))
628 			err = PTR_ERR(page);
629 		goto fail;
630 	}
631 
632 	f2fs_balance_fs(sbi, true);
633 
634 	f2fs_lock_op(sbi);
635 	err = f2fs_acquire_orphan_inode(sbi);
636 	if (err) {
637 		f2fs_unlock_op(sbi);
638 		f2fs_put_page(page, 0);
639 		goto fail;
640 	}
641 	f2fs_delete_entry(de, page, dir, inode);
642 	f2fs_unlock_op(sbi);
643 
644 #if IS_ENABLED(CONFIG_UNICODE)
645 	/* VFS negative dentries are incompatible with Encoding and
646 	 * Case-insensitiveness. Eventually we'll want avoid
647 	 * invalidating the dentries here, alongside with returning the
648 	 * negative dentries at f2fs_lookup(), when it is better
649 	 * supported by the VFS for the CI case.
650 	 */
651 	if (IS_CASEFOLDED(dir))
652 		d_invalidate(dentry);
653 #endif
654 	if (IS_DIRSYNC(dir))
655 		f2fs_sync_fs(sbi->sb, 1);
656 fail:
657 	trace_f2fs_unlink_exit(inode, err);
658 	return err;
659 }
660 
661 static const char *f2fs_get_link(struct dentry *dentry,
662 				 struct inode *inode,
663 				 struct delayed_call *done)
664 {
665 	const char *link = page_get_link(dentry, inode, done);
666 
667 	if (!IS_ERR(link) && !*link) {
668 		/* this is broken symlink case */
669 		do_delayed_call(done);
670 		clear_delayed_call(done);
671 		link = ERR_PTR(-ENOENT);
672 	}
673 	return link;
674 }
675 
676 static int f2fs_symlink(struct mnt_idmap *idmap, struct inode *dir,
677 			struct dentry *dentry, const char *symname)
678 {
679 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
680 	struct inode *inode;
681 	size_t len = strlen(symname);
682 	struct fscrypt_str disk_link;
683 	int err;
684 
685 	if (unlikely(f2fs_cp_error(sbi)))
686 		return -EIO;
687 	if (!f2fs_is_checkpoint_ready(sbi))
688 		return -ENOSPC;
689 
690 	err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
691 				      &disk_link);
692 	if (err)
693 		return err;
694 
695 	err = f2fs_dquot_initialize(dir);
696 	if (err)
697 		return err;
698 
699 	inode = f2fs_new_inode(idmap, dir, S_IFLNK | S_IRWXUGO, NULL);
700 	if (IS_ERR(inode))
701 		return PTR_ERR(inode);
702 
703 	if (IS_ENCRYPTED(inode))
704 		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
705 	else
706 		inode->i_op = &f2fs_symlink_inode_operations;
707 	inode_nohighmem(inode);
708 	inode->i_mapping->a_ops = &f2fs_dblock_aops;
709 
710 	f2fs_lock_op(sbi);
711 	err = f2fs_add_link(dentry, inode);
712 	if (err)
713 		goto out_f2fs_handle_failed_inode;
714 	f2fs_unlock_op(sbi);
715 	f2fs_alloc_nid_done(sbi, inode->i_ino);
716 
717 	err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
718 	if (err)
719 		goto err_out;
720 
721 	err = page_symlink(inode, disk_link.name, disk_link.len);
722 
723 err_out:
724 	d_instantiate_new(dentry, inode);
725 
726 	/*
727 	 * Let's flush symlink data in order to avoid broken symlink as much as
728 	 * possible. Nevertheless, fsyncing is the best way, but there is no
729 	 * way to get a file descriptor in order to flush that.
730 	 *
731 	 * Note that, it needs to do dir->fsync to make this recoverable.
732 	 * If the symlink path is stored into inline_data, there is no
733 	 * performance regression.
734 	 */
735 	if (!err) {
736 		filemap_write_and_wait_range(inode->i_mapping, 0,
737 							disk_link.len - 1);
738 
739 		if (IS_DIRSYNC(dir))
740 			f2fs_sync_fs(sbi->sb, 1);
741 	} else {
742 		f2fs_unlink(dir, dentry);
743 	}
744 
745 	f2fs_balance_fs(sbi, true);
746 	goto out_free_encrypted_link;
747 
748 out_f2fs_handle_failed_inode:
749 	f2fs_handle_failed_inode(inode);
750 out_free_encrypted_link:
751 	if (disk_link.name != (unsigned char *)symname)
752 		kfree(disk_link.name);
753 	return err;
754 }
755 
756 static int f2fs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
757 		      struct dentry *dentry, umode_t mode)
758 {
759 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
760 	struct inode *inode;
761 	int err;
762 
763 	if (unlikely(f2fs_cp_error(sbi)))
764 		return -EIO;
765 
766 	err = f2fs_dquot_initialize(dir);
767 	if (err)
768 		return err;
769 
770 	inode = f2fs_new_inode(idmap, dir, S_IFDIR | mode, NULL);
771 	if (IS_ERR(inode))
772 		return PTR_ERR(inode);
773 
774 	inode->i_op = &f2fs_dir_inode_operations;
775 	inode->i_fop = &f2fs_dir_operations;
776 	inode->i_mapping->a_ops = &f2fs_dblock_aops;
777 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
778 
779 	set_inode_flag(inode, FI_INC_LINK);
780 	f2fs_lock_op(sbi);
781 	err = f2fs_add_link(dentry, inode);
782 	if (err)
783 		goto out_fail;
784 	f2fs_unlock_op(sbi);
785 
786 	f2fs_alloc_nid_done(sbi, inode->i_ino);
787 
788 	d_instantiate_new(dentry, inode);
789 
790 	if (IS_DIRSYNC(dir))
791 		f2fs_sync_fs(sbi->sb, 1);
792 
793 	f2fs_balance_fs(sbi, true);
794 	return 0;
795 
796 out_fail:
797 	clear_inode_flag(inode, FI_INC_LINK);
798 	f2fs_handle_failed_inode(inode);
799 	return err;
800 }
801 
802 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
803 {
804 	struct inode *inode = d_inode(dentry);
805 
806 	if (f2fs_empty_dir(inode))
807 		return f2fs_unlink(dir, dentry);
808 	return -ENOTEMPTY;
809 }
810 
811 static int f2fs_mknod(struct mnt_idmap *idmap, struct inode *dir,
812 		      struct dentry *dentry, umode_t mode, dev_t rdev)
813 {
814 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
815 	struct inode *inode;
816 	int err = 0;
817 
818 	if (unlikely(f2fs_cp_error(sbi)))
819 		return -EIO;
820 	if (!f2fs_is_checkpoint_ready(sbi))
821 		return -ENOSPC;
822 
823 	err = f2fs_dquot_initialize(dir);
824 	if (err)
825 		return err;
826 
827 	inode = f2fs_new_inode(idmap, dir, mode, NULL);
828 	if (IS_ERR(inode))
829 		return PTR_ERR(inode);
830 
831 	init_special_inode(inode, inode->i_mode, rdev);
832 	inode->i_op = &f2fs_special_inode_operations;
833 
834 	f2fs_lock_op(sbi);
835 	err = f2fs_add_link(dentry, inode);
836 	if (err)
837 		goto out;
838 	f2fs_unlock_op(sbi);
839 
840 	f2fs_alloc_nid_done(sbi, inode->i_ino);
841 
842 	d_instantiate_new(dentry, inode);
843 
844 	if (IS_DIRSYNC(dir))
845 		f2fs_sync_fs(sbi->sb, 1);
846 
847 	f2fs_balance_fs(sbi, true);
848 	return 0;
849 out:
850 	f2fs_handle_failed_inode(inode);
851 	return err;
852 }
853 
854 static int __f2fs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
855 			  struct file *file, umode_t mode, bool is_whiteout,
856 			  struct inode **new_inode)
857 {
858 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
859 	struct inode *inode;
860 	int err;
861 
862 	err = f2fs_dquot_initialize(dir);
863 	if (err)
864 		return err;
865 
866 	inode = f2fs_new_inode(idmap, dir, mode, NULL);
867 	if (IS_ERR(inode))
868 		return PTR_ERR(inode);
869 
870 	if (is_whiteout) {
871 		init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
872 		inode->i_op = &f2fs_special_inode_operations;
873 	} else {
874 		inode->i_op = &f2fs_file_inode_operations;
875 		inode->i_fop = &f2fs_file_operations;
876 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
877 	}
878 
879 	f2fs_lock_op(sbi);
880 	err = f2fs_acquire_orphan_inode(sbi);
881 	if (err)
882 		goto out;
883 
884 	err = f2fs_do_tmpfile(inode, dir);
885 	if (err)
886 		goto release_out;
887 
888 	/*
889 	 * add this non-linked tmpfile to orphan list, in this way we could
890 	 * remove all unused data of tmpfile after abnormal power-off.
891 	 */
892 	f2fs_add_orphan_inode(inode);
893 	f2fs_alloc_nid_done(sbi, inode->i_ino);
894 
895 	if (is_whiteout) {
896 		f2fs_i_links_write(inode, false);
897 
898 		spin_lock(&inode->i_lock);
899 		inode->i_state |= I_LINKABLE;
900 		spin_unlock(&inode->i_lock);
901 	} else {
902 		if (file)
903 			d_tmpfile(file, inode);
904 		else
905 			f2fs_i_links_write(inode, false);
906 	}
907 	/* link_count was changed by d_tmpfile as well. */
908 	f2fs_unlock_op(sbi);
909 	unlock_new_inode(inode);
910 
911 	if (new_inode)
912 		*new_inode = inode;
913 
914 	f2fs_balance_fs(sbi, true);
915 	return 0;
916 
917 release_out:
918 	f2fs_release_orphan_inode(sbi);
919 out:
920 	f2fs_handle_failed_inode(inode);
921 	return err;
922 }
923 
924 static int f2fs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
925 			struct file *file, umode_t mode)
926 {
927 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
928 	int err;
929 
930 	if (unlikely(f2fs_cp_error(sbi)))
931 		return -EIO;
932 	if (!f2fs_is_checkpoint_ready(sbi))
933 		return -ENOSPC;
934 
935 	err = __f2fs_tmpfile(idmap, dir, file, mode, false, NULL);
936 
937 	return finish_open_simple(file, err);
938 }
939 
940 static int f2fs_create_whiteout(struct mnt_idmap *idmap,
941 				struct inode *dir, struct inode **whiteout)
942 {
943 	return __f2fs_tmpfile(idmap, dir, NULL,
944 				S_IFCHR | WHITEOUT_MODE, true, whiteout);
945 }
946 
947 int f2fs_get_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
948 		     struct inode **new_inode)
949 {
950 	return __f2fs_tmpfile(idmap, dir, NULL, S_IFREG, false, new_inode);
951 }
952 
953 static int f2fs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
954 			struct dentry *old_dentry, struct inode *new_dir,
955 			struct dentry *new_dentry, unsigned int flags)
956 {
957 	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
958 	struct inode *old_inode = d_inode(old_dentry);
959 	struct inode *new_inode = d_inode(new_dentry);
960 	struct inode *whiteout = NULL;
961 	struct page *old_dir_page = NULL;
962 	struct page *old_page, *new_page = NULL;
963 	struct f2fs_dir_entry *old_dir_entry = NULL;
964 	struct f2fs_dir_entry *old_entry;
965 	struct f2fs_dir_entry *new_entry;
966 	int err;
967 
968 	if (unlikely(f2fs_cp_error(sbi)))
969 		return -EIO;
970 	if (!f2fs_is_checkpoint_ready(sbi))
971 		return -ENOSPC;
972 
973 	if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
974 			(!projid_eq(F2FS_I(new_dir)->i_projid,
975 			F2FS_I(old_dentry->d_inode)->i_projid)))
976 		return -EXDEV;
977 
978 	/*
979 	 * If new_inode is null, the below renaming flow will
980 	 * add a link in old_dir which can convert inline_dir.
981 	 * After then, if we failed to get the entry due to other
982 	 * reasons like ENOMEM, we had to remove the new entry.
983 	 * Instead of adding such the error handling routine, let's
984 	 * simply convert first here.
985 	 */
986 	if (old_dir == new_dir && !new_inode) {
987 		err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
988 		if (err)
989 			return err;
990 	}
991 
992 	if (flags & RENAME_WHITEOUT) {
993 		err = f2fs_create_whiteout(idmap, old_dir, &whiteout);
994 		if (err)
995 			return err;
996 	}
997 
998 	err = f2fs_dquot_initialize(old_dir);
999 	if (err)
1000 		goto out;
1001 
1002 	err = f2fs_dquot_initialize(new_dir);
1003 	if (err)
1004 		goto out;
1005 
1006 	if (new_inode) {
1007 		err = f2fs_dquot_initialize(new_inode);
1008 		if (err)
1009 			goto out;
1010 	}
1011 
1012 	err = -ENOENT;
1013 	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1014 	if (!old_entry) {
1015 		if (IS_ERR(old_page))
1016 			err = PTR_ERR(old_page);
1017 		goto out;
1018 	}
1019 
1020 	if (S_ISDIR(old_inode->i_mode)) {
1021 		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
1022 		if (!old_dir_entry) {
1023 			if (IS_ERR(old_dir_page))
1024 				err = PTR_ERR(old_dir_page);
1025 			goto out_old;
1026 		}
1027 	}
1028 
1029 	if (new_inode) {
1030 
1031 		err = -ENOTEMPTY;
1032 		if (old_dir_entry && !f2fs_empty_dir(new_inode))
1033 			goto out_dir;
1034 
1035 		err = -ENOENT;
1036 		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
1037 						&new_page);
1038 		if (!new_entry) {
1039 			if (IS_ERR(new_page))
1040 				err = PTR_ERR(new_page);
1041 			goto out_dir;
1042 		}
1043 
1044 		f2fs_balance_fs(sbi, true);
1045 
1046 		f2fs_lock_op(sbi);
1047 
1048 		err = f2fs_acquire_orphan_inode(sbi);
1049 		if (err)
1050 			goto put_out_dir;
1051 
1052 		f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1053 		new_page = NULL;
1054 
1055 		new_inode->i_ctime = current_time(new_inode);
1056 		f2fs_down_write(&F2FS_I(new_inode)->i_sem);
1057 		if (old_dir_entry)
1058 			f2fs_i_links_write(new_inode, false);
1059 		f2fs_i_links_write(new_inode, false);
1060 		f2fs_up_write(&F2FS_I(new_inode)->i_sem);
1061 
1062 		if (!new_inode->i_nlink)
1063 			f2fs_add_orphan_inode(new_inode);
1064 		else
1065 			f2fs_release_orphan_inode(sbi);
1066 	} else {
1067 		f2fs_balance_fs(sbi, true);
1068 
1069 		f2fs_lock_op(sbi);
1070 
1071 		err = f2fs_add_link(new_dentry, old_inode);
1072 		if (err) {
1073 			f2fs_unlock_op(sbi);
1074 			goto out_dir;
1075 		}
1076 
1077 		if (old_dir_entry)
1078 			f2fs_i_links_write(new_dir, true);
1079 	}
1080 
1081 	f2fs_down_write(&F2FS_I(old_inode)->i_sem);
1082 	if (!old_dir_entry || whiteout)
1083 		file_lost_pino(old_inode);
1084 	else
1085 		/* adjust dir's i_pino to pass fsck check */
1086 		f2fs_i_pino_write(old_inode, new_dir->i_ino);
1087 	f2fs_up_write(&F2FS_I(old_inode)->i_sem);
1088 
1089 	old_inode->i_ctime = current_time(old_inode);
1090 	f2fs_mark_inode_dirty_sync(old_inode, false);
1091 
1092 	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
1093 	old_page = NULL;
1094 
1095 	if (whiteout) {
1096 		set_inode_flag(whiteout, FI_INC_LINK);
1097 		err = f2fs_add_link(old_dentry, whiteout);
1098 		if (err)
1099 			goto put_out_dir;
1100 
1101 		spin_lock(&whiteout->i_lock);
1102 		whiteout->i_state &= ~I_LINKABLE;
1103 		spin_unlock(&whiteout->i_lock);
1104 
1105 		iput(whiteout);
1106 	}
1107 
1108 	if (old_dir_entry) {
1109 		if (old_dir != new_dir && !whiteout)
1110 			f2fs_set_link(old_inode, old_dir_entry,
1111 						old_dir_page, new_dir);
1112 		else
1113 			f2fs_put_page(old_dir_page, 0);
1114 		f2fs_i_links_write(old_dir, false);
1115 	}
1116 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1117 		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1118 		if (S_ISDIR(old_inode->i_mode))
1119 			f2fs_add_ino_entry(sbi, old_inode->i_ino,
1120 							TRANS_DIR_INO);
1121 	}
1122 
1123 	f2fs_unlock_op(sbi);
1124 
1125 	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1126 		f2fs_sync_fs(sbi->sb, 1);
1127 
1128 	f2fs_update_time(sbi, REQ_TIME);
1129 	return 0;
1130 
1131 put_out_dir:
1132 	f2fs_unlock_op(sbi);
1133 	f2fs_put_page(new_page, 0);
1134 out_dir:
1135 	if (old_dir_entry)
1136 		f2fs_put_page(old_dir_page, 0);
1137 out_old:
1138 	f2fs_put_page(old_page, 0);
1139 out:
1140 	iput(whiteout);
1141 	return err;
1142 }
1143 
1144 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1145 			     struct inode *new_dir, struct dentry *new_dentry)
1146 {
1147 	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1148 	struct inode *old_inode = d_inode(old_dentry);
1149 	struct inode *new_inode = d_inode(new_dentry);
1150 	struct page *old_dir_page, *new_dir_page;
1151 	struct page *old_page, *new_page;
1152 	struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1153 	struct f2fs_dir_entry *old_entry, *new_entry;
1154 	int old_nlink = 0, new_nlink = 0;
1155 	int err;
1156 
1157 	if (unlikely(f2fs_cp_error(sbi)))
1158 		return -EIO;
1159 	if (!f2fs_is_checkpoint_ready(sbi))
1160 		return -ENOSPC;
1161 
1162 	if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1163 			!projid_eq(F2FS_I(new_dir)->i_projid,
1164 			F2FS_I(old_dentry->d_inode)->i_projid)) ||
1165 	    (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1166 			!projid_eq(F2FS_I(old_dir)->i_projid,
1167 			F2FS_I(new_dentry->d_inode)->i_projid)))
1168 		return -EXDEV;
1169 
1170 	err = f2fs_dquot_initialize(old_dir);
1171 	if (err)
1172 		goto out;
1173 
1174 	err = f2fs_dquot_initialize(new_dir);
1175 	if (err)
1176 		goto out;
1177 
1178 	err = -ENOENT;
1179 	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1180 	if (!old_entry) {
1181 		if (IS_ERR(old_page))
1182 			err = PTR_ERR(old_page);
1183 		goto out;
1184 	}
1185 
1186 	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1187 	if (!new_entry) {
1188 		if (IS_ERR(new_page))
1189 			err = PTR_ERR(new_page);
1190 		goto out_old;
1191 	}
1192 
1193 	/* prepare for updating ".." directory entry info later */
1194 	if (old_dir != new_dir) {
1195 		if (S_ISDIR(old_inode->i_mode)) {
1196 			old_dir_entry = f2fs_parent_dir(old_inode,
1197 							&old_dir_page);
1198 			if (!old_dir_entry) {
1199 				if (IS_ERR(old_dir_page))
1200 					err = PTR_ERR(old_dir_page);
1201 				goto out_new;
1202 			}
1203 		}
1204 
1205 		if (S_ISDIR(new_inode->i_mode)) {
1206 			new_dir_entry = f2fs_parent_dir(new_inode,
1207 							&new_dir_page);
1208 			if (!new_dir_entry) {
1209 				if (IS_ERR(new_dir_page))
1210 					err = PTR_ERR(new_dir_page);
1211 				goto out_old_dir;
1212 			}
1213 		}
1214 	}
1215 
1216 	/*
1217 	 * If cross rename between file and directory those are not
1218 	 * in the same directory, we will inc nlink of file's parent
1219 	 * later, so we should check upper boundary of its nlink.
1220 	 */
1221 	if ((!old_dir_entry || !new_dir_entry) &&
1222 				old_dir_entry != new_dir_entry) {
1223 		old_nlink = old_dir_entry ? -1 : 1;
1224 		new_nlink = -old_nlink;
1225 		err = -EMLINK;
1226 		if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1227 			(new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1228 			goto out_new_dir;
1229 	}
1230 
1231 	f2fs_balance_fs(sbi, true);
1232 
1233 	f2fs_lock_op(sbi);
1234 
1235 	/* update ".." directory entry info of old dentry */
1236 	if (old_dir_entry)
1237 		f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1238 
1239 	/* update ".." directory entry info of new dentry */
1240 	if (new_dir_entry)
1241 		f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1242 
1243 	/* update directory entry info of old dir inode */
1244 	f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1245 
1246 	f2fs_down_write(&F2FS_I(old_inode)->i_sem);
1247 	if (!old_dir_entry)
1248 		file_lost_pino(old_inode);
1249 	else
1250 		/* adjust dir's i_pino to pass fsck check */
1251 		f2fs_i_pino_write(old_inode, new_dir->i_ino);
1252 	f2fs_up_write(&F2FS_I(old_inode)->i_sem);
1253 
1254 	old_dir->i_ctime = current_time(old_dir);
1255 	if (old_nlink) {
1256 		f2fs_down_write(&F2FS_I(old_dir)->i_sem);
1257 		f2fs_i_links_write(old_dir, old_nlink > 0);
1258 		f2fs_up_write(&F2FS_I(old_dir)->i_sem);
1259 	}
1260 	f2fs_mark_inode_dirty_sync(old_dir, false);
1261 
1262 	/* update directory entry info of new dir inode */
1263 	f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1264 
1265 	f2fs_down_write(&F2FS_I(new_inode)->i_sem);
1266 	if (!new_dir_entry)
1267 		file_lost_pino(new_inode);
1268 	else
1269 		/* adjust dir's i_pino to pass fsck check */
1270 		f2fs_i_pino_write(new_inode, old_dir->i_ino);
1271 	f2fs_up_write(&F2FS_I(new_inode)->i_sem);
1272 
1273 	new_dir->i_ctime = current_time(new_dir);
1274 	if (new_nlink) {
1275 		f2fs_down_write(&F2FS_I(new_dir)->i_sem);
1276 		f2fs_i_links_write(new_dir, new_nlink > 0);
1277 		f2fs_up_write(&F2FS_I(new_dir)->i_sem);
1278 	}
1279 	f2fs_mark_inode_dirty_sync(new_dir, false);
1280 
1281 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1282 		f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1283 		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1284 	}
1285 
1286 	f2fs_unlock_op(sbi);
1287 
1288 	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1289 		f2fs_sync_fs(sbi->sb, 1);
1290 
1291 	f2fs_update_time(sbi, REQ_TIME);
1292 	return 0;
1293 out_new_dir:
1294 	if (new_dir_entry) {
1295 		f2fs_put_page(new_dir_page, 0);
1296 	}
1297 out_old_dir:
1298 	if (old_dir_entry) {
1299 		f2fs_put_page(old_dir_page, 0);
1300 	}
1301 out_new:
1302 	f2fs_put_page(new_page, 0);
1303 out_old:
1304 	f2fs_put_page(old_page, 0);
1305 out:
1306 	return err;
1307 }
1308 
1309 static int f2fs_rename2(struct mnt_idmap *idmap,
1310 			struct inode *old_dir, struct dentry *old_dentry,
1311 			struct inode *new_dir, struct dentry *new_dentry,
1312 			unsigned int flags)
1313 {
1314 	int err;
1315 
1316 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1317 		return -EINVAL;
1318 
1319 	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1320 				     flags);
1321 	if (err)
1322 		return err;
1323 
1324 	if (flags & RENAME_EXCHANGE) {
1325 		return f2fs_cross_rename(old_dir, old_dentry,
1326 					 new_dir, new_dentry);
1327 	}
1328 	/*
1329 	 * VFS has already handled the new dentry existence case,
1330 	 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1331 	 */
1332 	return f2fs_rename(idmap, old_dir, old_dentry,
1333 					new_dir, new_dentry, flags);
1334 }
1335 
1336 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1337 					   struct inode *inode,
1338 					   struct delayed_call *done)
1339 {
1340 	struct page *page;
1341 	const char *target;
1342 
1343 	if (!dentry)
1344 		return ERR_PTR(-ECHILD);
1345 
1346 	page = read_mapping_page(inode->i_mapping, 0, NULL);
1347 	if (IS_ERR(page))
1348 		return ERR_CAST(page);
1349 
1350 	target = fscrypt_get_symlink(inode, page_address(page),
1351 				     inode->i_sb->s_blocksize, done);
1352 	put_page(page);
1353 	return target;
1354 }
1355 
1356 static int f2fs_encrypted_symlink_getattr(struct mnt_idmap *idmap,
1357 					  const struct path *path,
1358 					  struct kstat *stat, u32 request_mask,
1359 					  unsigned int query_flags)
1360 {
1361 	f2fs_getattr(idmap, path, stat, request_mask, query_flags);
1362 
1363 	return fscrypt_symlink_getattr(path, stat);
1364 }
1365 
1366 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1367 	.get_link	= f2fs_encrypted_get_link,
1368 	.getattr	= f2fs_encrypted_symlink_getattr,
1369 	.setattr	= f2fs_setattr,
1370 	.listxattr	= f2fs_listxattr,
1371 };
1372 
1373 const struct inode_operations f2fs_dir_inode_operations = {
1374 	.create		= f2fs_create,
1375 	.lookup		= f2fs_lookup,
1376 	.link		= f2fs_link,
1377 	.unlink		= f2fs_unlink,
1378 	.symlink	= f2fs_symlink,
1379 	.mkdir		= f2fs_mkdir,
1380 	.rmdir		= f2fs_rmdir,
1381 	.mknod		= f2fs_mknod,
1382 	.rename		= f2fs_rename2,
1383 	.tmpfile	= f2fs_tmpfile,
1384 	.getattr	= f2fs_getattr,
1385 	.setattr	= f2fs_setattr,
1386 	.get_inode_acl	= f2fs_get_acl,
1387 	.set_acl	= f2fs_set_acl,
1388 	.listxattr	= f2fs_listxattr,
1389 	.fiemap		= f2fs_fiemap,
1390 	.fileattr_get	= f2fs_fileattr_get,
1391 	.fileattr_set	= f2fs_fileattr_set,
1392 };
1393 
1394 const struct inode_operations f2fs_symlink_inode_operations = {
1395 	.get_link	= f2fs_get_link,
1396 	.getattr	= f2fs_getattr,
1397 	.setattr	= f2fs_setattr,
1398 	.listxattr	= f2fs_listxattr,
1399 };
1400 
1401 const struct inode_operations f2fs_special_inode_operations = {
1402 	.getattr	= f2fs_getattr,
1403 	.setattr	= f2fs_setattr,
1404 	.get_inode_acl	= f2fs_get_acl,
1405 	.set_acl	= f2fs_set_acl,
1406 	.listxattr	= f2fs_listxattr,
1407 };
1408