xref: /linux/fs/f2fs/namei.c (revision 249ebf3f65f8530beb2cbfb91bff1d83ba88d23c)
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 	simple_inode_init_ts(inode);
247 	F2FS_I(inode)->i_crtime = inode_get_mtime(inode);
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_set_ctime_current(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 f2fs_dir_entry *de;
463 	struct page *page;
464 	int err = 0;
465 
466 	if (f2fs_readonly(sbi->sb)) {
467 		f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
468 			  dir->i_ino, pino);
469 		return 0;
470 	}
471 
472 	if (!S_ISDIR(dir->i_mode)) {
473 		f2fs_err(sbi, "inconsistent inode status, skip recovering inline_dots inode (ino:%lu, i_mode:%u, pino:%u)",
474 			  dir->i_ino, dir->i_mode, pino);
475 		set_sbi_flag(sbi, SBI_NEED_FSCK);
476 		return -ENOTDIR;
477 	}
478 
479 	err = f2fs_dquot_initialize(dir);
480 	if (err)
481 		return err;
482 
483 	f2fs_balance_fs(sbi, true);
484 
485 	f2fs_lock_op(sbi);
486 
487 	de = f2fs_find_entry(dir, &dot, &page);
488 	if (de) {
489 		f2fs_put_page(page, 0);
490 	} else if (IS_ERR(page)) {
491 		err = PTR_ERR(page);
492 		goto out;
493 	} else {
494 		err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
495 		if (err)
496 			goto out;
497 	}
498 
499 	de = f2fs_find_entry(dir, &dotdot_name, &page);
500 	if (de)
501 		f2fs_put_page(page, 0);
502 	else if (IS_ERR(page))
503 		err = PTR_ERR(page);
504 	else
505 		err = f2fs_do_add_link(dir, &dotdot_name, NULL, pino, S_IFDIR);
506 out:
507 	if (!err)
508 		clear_inode_flag(dir, FI_INLINE_DOTS);
509 
510 	f2fs_unlock_op(sbi);
511 	return err;
512 }
513 
514 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
515 		unsigned int flags)
516 {
517 	struct inode *inode = NULL;
518 	struct f2fs_dir_entry *de;
519 	struct page *page;
520 	struct dentry *new;
521 	nid_t ino = -1;
522 	int err = 0;
523 	unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
524 	struct f2fs_filename fname;
525 
526 	trace_f2fs_lookup_start(dir, dentry, flags);
527 
528 	if (dentry->d_name.len > F2FS_NAME_LEN) {
529 		err = -ENAMETOOLONG;
530 		goto out;
531 	}
532 
533 	err = f2fs_prepare_lookup(dir, dentry, &fname);
534 	if (err == -ENOENT)
535 		goto out_splice;
536 	if (err)
537 		goto out;
538 	de = __f2fs_find_entry(dir, &fname, &page);
539 	f2fs_free_filename(&fname);
540 
541 	if (!de) {
542 		if (IS_ERR(page)) {
543 			err = PTR_ERR(page);
544 			goto out;
545 		}
546 		err = -ENOENT;
547 		goto out_splice;
548 	}
549 
550 	ino = le32_to_cpu(de->ino);
551 	f2fs_put_page(page, 0);
552 
553 	inode = f2fs_iget(dir->i_sb, ino);
554 	if (IS_ERR(inode)) {
555 		err = PTR_ERR(inode);
556 		goto out;
557 	}
558 
559 	if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
560 		err = __recover_dot_dentries(dir, root_ino);
561 		if (err)
562 			goto out_iput;
563 	}
564 
565 	if (f2fs_has_inline_dots(inode)) {
566 		err = __recover_dot_dentries(inode, dir->i_ino);
567 		if (err)
568 			goto out_iput;
569 	}
570 	if (IS_ENCRYPTED(dir) &&
571 	    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
572 	    !fscrypt_has_permitted_context(dir, inode)) {
573 		f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
574 			  dir->i_ino, inode->i_ino);
575 		err = -EPERM;
576 		goto out_iput;
577 	}
578 out_splice:
579 #if IS_ENABLED(CONFIG_UNICODE)
580 	if (!inode && IS_CASEFOLDED(dir)) {
581 		/* Eventually we want to call d_add_ci(dentry, NULL)
582 		 * for negative dentries in the encoding case as
583 		 * well.  For now, prevent the negative dentry
584 		 * from being cached.
585 		 */
586 		trace_f2fs_lookup_end(dir, dentry, ino, err);
587 		return NULL;
588 	}
589 #endif
590 	new = d_splice_alias(inode, dentry);
591 	trace_f2fs_lookup_end(dir, !IS_ERR_OR_NULL(new) ? new : dentry,
592 				ino, IS_ERR(new) ? PTR_ERR(new) : err);
593 	return new;
594 out_iput:
595 	iput(inode);
596 out:
597 	trace_f2fs_lookup_end(dir, dentry, ino, err);
598 	return ERR_PTR(err);
599 }
600 
601 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
602 {
603 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
604 	struct inode *inode = d_inode(dentry);
605 	struct f2fs_dir_entry *de;
606 	struct page *page;
607 	int err;
608 
609 	trace_f2fs_unlink_enter(dir, dentry);
610 
611 	if (unlikely(f2fs_cp_error(sbi))) {
612 		err = -EIO;
613 		goto fail;
614 	}
615 
616 	err = f2fs_dquot_initialize(dir);
617 	if (err)
618 		goto fail;
619 	err = f2fs_dquot_initialize(inode);
620 	if (err)
621 		goto fail;
622 
623 	de = f2fs_find_entry(dir, &dentry->d_name, &page);
624 	if (!de) {
625 		if (IS_ERR(page))
626 			err = PTR_ERR(page);
627 		goto fail;
628 	}
629 
630 	f2fs_balance_fs(sbi, true);
631 
632 	f2fs_lock_op(sbi);
633 	err = f2fs_acquire_orphan_inode(sbi);
634 	if (err) {
635 		f2fs_unlock_op(sbi);
636 		f2fs_put_page(page, 0);
637 		goto fail;
638 	}
639 	f2fs_delete_entry(de, page, dir, inode);
640 	f2fs_unlock_op(sbi);
641 
642 #if IS_ENABLED(CONFIG_UNICODE)
643 	/* VFS negative dentries are incompatible with Encoding and
644 	 * Case-insensitiveness. Eventually we'll want avoid
645 	 * invalidating the dentries here, alongside with returning the
646 	 * negative dentries at f2fs_lookup(), when it is better
647 	 * supported by the VFS for the CI case.
648 	 */
649 	if (IS_CASEFOLDED(dir))
650 		d_invalidate(dentry);
651 #endif
652 	if (IS_DIRSYNC(dir))
653 		f2fs_sync_fs(sbi->sb, 1);
654 fail:
655 	trace_f2fs_unlink_exit(inode, err);
656 	return err;
657 }
658 
659 static const char *f2fs_get_link(struct dentry *dentry,
660 				 struct inode *inode,
661 				 struct delayed_call *done)
662 {
663 	const char *link = page_get_link(dentry, inode, done);
664 
665 	if (!IS_ERR(link) && !*link) {
666 		/* this is broken symlink case */
667 		do_delayed_call(done);
668 		clear_delayed_call(done);
669 		link = ERR_PTR(-ENOENT);
670 	}
671 	return link;
672 }
673 
674 static int f2fs_symlink(struct mnt_idmap *idmap, struct inode *dir,
675 			struct dentry *dentry, const char *symname)
676 {
677 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
678 	struct inode *inode;
679 	size_t len = strlen(symname);
680 	struct fscrypt_str disk_link;
681 	int err;
682 
683 	if (unlikely(f2fs_cp_error(sbi)))
684 		return -EIO;
685 	if (!f2fs_is_checkpoint_ready(sbi))
686 		return -ENOSPC;
687 
688 	err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
689 				      &disk_link);
690 	if (err)
691 		return err;
692 
693 	err = f2fs_dquot_initialize(dir);
694 	if (err)
695 		return err;
696 
697 	inode = f2fs_new_inode(idmap, dir, S_IFLNK | S_IRWXUGO, NULL);
698 	if (IS_ERR(inode))
699 		return PTR_ERR(inode);
700 
701 	if (IS_ENCRYPTED(inode))
702 		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
703 	else
704 		inode->i_op = &f2fs_symlink_inode_operations;
705 	inode_nohighmem(inode);
706 	inode->i_mapping->a_ops = &f2fs_dblock_aops;
707 
708 	f2fs_lock_op(sbi);
709 	err = f2fs_add_link(dentry, inode);
710 	if (err)
711 		goto out_f2fs_handle_failed_inode;
712 	f2fs_unlock_op(sbi);
713 	f2fs_alloc_nid_done(sbi, inode->i_ino);
714 
715 	err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
716 	if (err)
717 		goto err_out;
718 
719 	err = page_symlink(inode, disk_link.name, disk_link.len);
720 
721 err_out:
722 	d_instantiate_new(dentry, inode);
723 
724 	/*
725 	 * Let's flush symlink data in order to avoid broken symlink as much as
726 	 * possible. Nevertheless, fsyncing is the best way, but there is no
727 	 * way to get a file descriptor in order to flush that.
728 	 *
729 	 * Note that, it needs to do dir->fsync to make this recoverable.
730 	 * If the symlink path is stored into inline_data, there is no
731 	 * performance regression.
732 	 */
733 	if (!err) {
734 		filemap_write_and_wait_range(inode->i_mapping, 0,
735 							disk_link.len - 1);
736 
737 		if (IS_DIRSYNC(dir))
738 			f2fs_sync_fs(sbi->sb, 1);
739 	} else {
740 		f2fs_unlink(dir, dentry);
741 	}
742 
743 	f2fs_balance_fs(sbi, true);
744 	goto out_free_encrypted_link;
745 
746 out_f2fs_handle_failed_inode:
747 	f2fs_handle_failed_inode(inode);
748 out_free_encrypted_link:
749 	if (disk_link.name != (unsigned char *)symname)
750 		kfree(disk_link.name);
751 	return err;
752 }
753 
754 static int f2fs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
755 		      struct dentry *dentry, umode_t mode)
756 {
757 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
758 	struct inode *inode;
759 	int err;
760 
761 	if (unlikely(f2fs_cp_error(sbi)))
762 		return -EIO;
763 
764 	err = f2fs_dquot_initialize(dir);
765 	if (err)
766 		return err;
767 
768 	inode = f2fs_new_inode(idmap, dir, S_IFDIR | mode, NULL);
769 	if (IS_ERR(inode))
770 		return PTR_ERR(inode);
771 
772 	inode->i_op = &f2fs_dir_inode_operations;
773 	inode->i_fop = &f2fs_dir_operations;
774 	inode->i_mapping->a_ops = &f2fs_dblock_aops;
775 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
776 
777 	set_inode_flag(inode, FI_INC_LINK);
778 	f2fs_lock_op(sbi);
779 	err = f2fs_add_link(dentry, inode);
780 	if (err)
781 		goto out_fail;
782 	f2fs_unlock_op(sbi);
783 
784 	f2fs_alloc_nid_done(sbi, inode->i_ino);
785 
786 	d_instantiate_new(dentry, inode);
787 
788 	if (IS_DIRSYNC(dir))
789 		f2fs_sync_fs(sbi->sb, 1);
790 
791 	f2fs_balance_fs(sbi, true);
792 	return 0;
793 
794 out_fail:
795 	clear_inode_flag(inode, FI_INC_LINK);
796 	f2fs_handle_failed_inode(inode);
797 	return err;
798 }
799 
800 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
801 {
802 	struct inode *inode = d_inode(dentry);
803 
804 	if (f2fs_empty_dir(inode))
805 		return f2fs_unlink(dir, dentry);
806 	return -ENOTEMPTY;
807 }
808 
809 static int f2fs_mknod(struct mnt_idmap *idmap, struct inode *dir,
810 		      struct dentry *dentry, umode_t mode, dev_t rdev)
811 {
812 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
813 	struct inode *inode;
814 	int err = 0;
815 
816 	if (unlikely(f2fs_cp_error(sbi)))
817 		return -EIO;
818 	if (!f2fs_is_checkpoint_ready(sbi))
819 		return -ENOSPC;
820 
821 	err = f2fs_dquot_initialize(dir);
822 	if (err)
823 		return err;
824 
825 	inode = f2fs_new_inode(idmap, dir, mode, NULL);
826 	if (IS_ERR(inode))
827 		return PTR_ERR(inode);
828 
829 	init_special_inode(inode, inode->i_mode, rdev);
830 	inode->i_op = &f2fs_special_inode_operations;
831 
832 	f2fs_lock_op(sbi);
833 	err = f2fs_add_link(dentry, inode);
834 	if (err)
835 		goto out;
836 	f2fs_unlock_op(sbi);
837 
838 	f2fs_alloc_nid_done(sbi, inode->i_ino);
839 
840 	d_instantiate_new(dentry, inode);
841 
842 	if (IS_DIRSYNC(dir))
843 		f2fs_sync_fs(sbi->sb, 1);
844 
845 	f2fs_balance_fs(sbi, true);
846 	return 0;
847 out:
848 	f2fs_handle_failed_inode(inode);
849 	return err;
850 }
851 
852 static int __f2fs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
853 			  struct file *file, umode_t mode, bool is_whiteout,
854 			  struct inode **new_inode, struct f2fs_filename *fname)
855 {
856 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
857 	struct inode *inode;
858 	int err;
859 
860 	err = f2fs_dquot_initialize(dir);
861 	if (err)
862 		return err;
863 
864 	inode = f2fs_new_inode(idmap, dir, mode, NULL);
865 	if (IS_ERR(inode))
866 		return PTR_ERR(inode);
867 
868 	if (is_whiteout) {
869 		init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
870 		inode->i_op = &f2fs_special_inode_operations;
871 	} else {
872 		inode->i_op = &f2fs_file_inode_operations;
873 		inode->i_fop = &f2fs_file_operations;
874 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
875 	}
876 
877 	f2fs_lock_op(sbi);
878 	err = f2fs_acquire_orphan_inode(sbi);
879 	if (err)
880 		goto out;
881 
882 	err = f2fs_do_tmpfile(inode, dir, fname);
883 	if (err)
884 		goto release_out;
885 
886 	/*
887 	 * add this non-linked tmpfile to orphan list, in this way we could
888 	 * remove all unused data of tmpfile after abnormal power-off.
889 	 */
890 	f2fs_add_orphan_inode(inode);
891 	f2fs_alloc_nid_done(sbi, inode->i_ino);
892 
893 	if (is_whiteout) {
894 		f2fs_i_links_write(inode, false);
895 
896 		spin_lock(&inode->i_lock);
897 		inode->i_state |= I_LINKABLE;
898 		spin_unlock(&inode->i_lock);
899 	} else {
900 		if (file)
901 			d_tmpfile(file, inode);
902 		else
903 			f2fs_i_links_write(inode, false);
904 	}
905 	/* link_count was changed by d_tmpfile as well. */
906 	f2fs_unlock_op(sbi);
907 	unlock_new_inode(inode);
908 
909 	if (new_inode)
910 		*new_inode = inode;
911 
912 	f2fs_balance_fs(sbi, true);
913 	return 0;
914 
915 release_out:
916 	f2fs_release_orphan_inode(sbi);
917 out:
918 	f2fs_handle_failed_inode(inode);
919 	return err;
920 }
921 
922 static int f2fs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
923 			struct file *file, umode_t mode)
924 {
925 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
926 	int err;
927 
928 	if (unlikely(f2fs_cp_error(sbi)))
929 		return -EIO;
930 	if (!f2fs_is_checkpoint_ready(sbi))
931 		return -ENOSPC;
932 
933 	err = __f2fs_tmpfile(idmap, dir, file, mode, false, NULL, NULL);
934 
935 	return finish_open_simple(file, err);
936 }
937 
938 static int f2fs_create_whiteout(struct mnt_idmap *idmap,
939 				struct inode *dir, struct inode **whiteout,
940 				struct f2fs_filename *fname)
941 {
942 	return __f2fs_tmpfile(idmap, dir, NULL, S_IFCHR | WHITEOUT_MODE,
943 						true, whiteout, fname);
944 }
945 
946 int f2fs_get_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
947 		     struct inode **new_inode)
948 {
949 	return __f2fs_tmpfile(idmap, dir, NULL, S_IFREG,
950 				false, new_inode, NULL);
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 	bool old_is_dir = S_ISDIR(old_inode->i_mode);
967 	int err;
968 
969 	if (unlikely(f2fs_cp_error(sbi)))
970 		return -EIO;
971 	if (!f2fs_is_checkpoint_ready(sbi))
972 		return -ENOSPC;
973 
974 	if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
975 			(!projid_eq(F2FS_I(new_dir)->i_projid,
976 			F2FS_I(old_dentry->d_inode)->i_projid)))
977 		return -EXDEV;
978 
979 	/*
980 	 * If new_inode is null, the below renaming flow will
981 	 * add a link in old_dir which can convert inline_dir.
982 	 * After then, if we failed to get the entry due to other
983 	 * reasons like ENOMEM, we had to remove the new entry.
984 	 * Instead of adding such the error handling routine, let's
985 	 * simply convert first here.
986 	 */
987 	if (old_dir == new_dir && !new_inode) {
988 		err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
989 		if (err)
990 			return err;
991 	}
992 
993 	if (flags & RENAME_WHITEOUT) {
994 		struct f2fs_filename fname;
995 
996 		err = f2fs_setup_filename(old_dir, &old_dentry->d_name,
997 							0, &fname);
998 		if (err)
999 			return err;
1000 
1001 		err = f2fs_create_whiteout(idmap, old_dir, &whiteout, &fname);
1002 		if (err)
1003 			return err;
1004 	}
1005 
1006 	err = f2fs_dquot_initialize(old_dir);
1007 	if (err)
1008 		goto out;
1009 
1010 	err = f2fs_dquot_initialize(new_dir);
1011 	if (err)
1012 		goto out;
1013 
1014 	if (new_inode) {
1015 		err = f2fs_dquot_initialize(new_inode);
1016 		if (err)
1017 			goto out;
1018 	}
1019 
1020 	err = -ENOENT;
1021 	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1022 	if (!old_entry) {
1023 		if (IS_ERR(old_page))
1024 			err = PTR_ERR(old_page);
1025 		goto out;
1026 	}
1027 
1028 	if (old_is_dir && old_dir != new_dir) {
1029 		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
1030 		if (!old_dir_entry) {
1031 			if (IS_ERR(old_dir_page))
1032 				err = PTR_ERR(old_dir_page);
1033 			goto out_old;
1034 		}
1035 	}
1036 
1037 	if (new_inode) {
1038 
1039 		err = -ENOTEMPTY;
1040 		if (old_is_dir && !f2fs_empty_dir(new_inode))
1041 			goto out_dir;
1042 
1043 		err = -ENOENT;
1044 		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
1045 						&new_page);
1046 		if (!new_entry) {
1047 			if (IS_ERR(new_page))
1048 				err = PTR_ERR(new_page);
1049 			goto out_dir;
1050 		}
1051 
1052 		f2fs_balance_fs(sbi, true);
1053 
1054 		f2fs_lock_op(sbi);
1055 
1056 		err = f2fs_acquire_orphan_inode(sbi);
1057 		if (err)
1058 			goto put_out_dir;
1059 
1060 		f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1061 		new_page = NULL;
1062 
1063 		inode_set_ctime_current(new_inode);
1064 		f2fs_down_write(&F2FS_I(new_inode)->i_sem);
1065 		if (old_is_dir)
1066 			f2fs_i_links_write(new_inode, false);
1067 		f2fs_i_links_write(new_inode, false);
1068 		f2fs_up_write(&F2FS_I(new_inode)->i_sem);
1069 
1070 		if (!new_inode->i_nlink)
1071 			f2fs_add_orphan_inode(new_inode);
1072 		else
1073 			f2fs_release_orphan_inode(sbi);
1074 	} else {
1075 		f2fs_balance_fs(sbi, true);
1076 
1077 		f2fs_lock_op(sbi);
1078 
1079 		err = f2fs_add_link(new_dentry, old_inode);
1080 		if (err) {
1081 			f2fs_unlock_op(sbi);
1082 			goto out_dir;
1083 		}
1084 
1085 		if (old_is_dir)
1086 			f2fs_i_links_write(new_dir, true);
1087 	}
1088 
1089 	f2fs_down_write(&F2FS_I(old_inode)->i_sem);
1090 	if (!old_is_dir || whiteout)
1091 		file_lost_pino(old_inode);
1092 	else
1093 		/* adjust dir's i_pino to pass fsck check */
1094 		f2fs_i_pino_write(old_inode, new_dir->i_ino);
1095 	f2fs_up_write(&F2FS_I(old_inode)->i_sem);
1096 
1097 	inode_set_ctime_current(old_inode);
1098 	f2fs_mark_inode_dirty_sync(old_inode, false);
1099 
1100 	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
1101 	old_page = NULL;
1102 
1103 	if (whiteout) {
1104 		set_inode_flag(whiteout, FI_INC_LINK);
1105 		err = f2fs_add_link(old_dentry, whiteout);
1106 		if (err)
1107 			goto put_out_dir;
1108 
1109 		spin_lock(&whiteout->i_lock);
1110 		whiteout->i_state &= ~I_LINKABLE;
1111 		spin_unlock(&whiteout->i_lock);
1112 
1113 		iput(whiteout);
1114 	}
1115 
1116 	if (old_dir_entry)
1117 		f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1118 	if (old_is_dir)
1119 		f2fs_i_links_write(old_dir, false);
1120 
1121 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1122 		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1123 		if (S_ISDIR(old_inode->i_mode))
1124 			f2fs_add_ino_entry(sbi, old_inode->i_ino,
1125 							TRANS_DIR_INO);
1126 	}
1127 
1128 	f2fs_unlock_op(sbi);
1129 
1130 	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1131 		f2fs_sync_fs(sbi->sb, 1);
1132 
1133 	f2fs_update_time(sbi, REQ_TIME);
1134 	return 0;
1135 
1136 put_out_dir:
1137 	f2fs_unlock_op(sbi);
1138 	f2fs_put_page(new_page, 0);
1139 out_dir:
1140 	if (old_dir_entry)
1141 		f2fs_put_page(old_dir_page, 0);
1142 out_old:
1143 	f2fs_put_page(old_page, 0);
1144 out:
1145 	iput(whiteout);
1146 	return err;
1147 }
1148 
1149 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1150 			     struct inode *new_dir, struct dentry *new_dentry)
1151 {
1152 	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1153 	struct inode *old_inode = d_inode(old_dentry);
1154 	struct inode *new_inode = d_inode(new_dentry);
1155 	struct page *old_dir_page, *new_dir_page;
1156 	struct page *old_page, *new_page;
1157 	struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1158 	struct f2fs_dir_entry *old_entry, *new_entry;
1159 	int old_nlink = 0, new_nlink = 0;
1160 	int err;
1161 
1162 	if (unlikely(f2fs_cp_error(sbi)))
1163 		return -EIO;
1164 	if (!f2fs_is_checkpoint_ready(sbi))
1165 		return -ENOSPC;
1166 
1167 	if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1168 			!projid_eq(F2FS_I(new_dir)->i_projid,
1169 			F2FS_I(old_dentry->d_inode)->i_projid)) ||
1170 	    (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1171 			!projid_eq(F2FS_I(old_dir)->i_projid,
1172 			F2FS_I(new_dentry->d_inode)->i_projid)))
1173 		return -EXDEV;
1174 
1175 	err = f2fs_dquot_initialize(old_dir);
1176 	if (err)
1177 		goto out;
1178 
1179 	err = f2fs_dquot_initialize(new_dir);
1180 	if (err)
1181 		goto out;
1182 
1183 	err = -ENOENT;
1184 	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1185 	if (!old_entry) {
1186 		if (IS_ERR(old_page))
1187 			err = PTR_ERR(old_page);
1188 		goto out;
1189 	}
1190 
1191 	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1192 	if (!new_entry) {
1193 		if (IS_ERR(new_page))
1194 			err = PTR_ERR(new_page);
1195 		goto out_old;
1196 	}
1197 
1198 	/* prepare for updating ".." directory entry info later */
1199 	if (old_dir != new_dir) {
1200 		if (S_ISDIR(old_inode->i_mode)) {
1201 			old_dir_entry = f2fs_parent_dir(old_inode,
1202 							&old_dir_page);
1203 			if (!old_dir_entry) {
1204 				if (IS_ERR(old_dir_page))
1205 					err = PTR_ERR(old_dir_page);
1206 				goto out_new;
1207 			}
1208 		}
1209 
1210 		if (S_ISDIR(new_inode->i_mode)) {
1211 			new_dir_entry = f2fs_parent_dir(new_inode,
1212 							&new_dir_page);
1213 			if (!new_dir_entry) {
1214 				if (IS_ERR(new_dir_page))
1215 					err = PTR_ERR(new_dir_page);
1216 				goto out_old_dir;
1217 			}
1218 		}
1219 	}
1220 
1221 	/*
1222 	 * If cross rename between file and directory those are not
1223 	 * in the same directory, we will inc nlink of file's parent
1224 	 * later, so we should check upper boundary of its nlink.
1225 	 */
1226 	if ((!old_dir_entry || !new_dir_entry) &&
1227 				old_dir_entry != new_dir_entry) {
1228 		old_nlink = old_dir_entry ? -1 : 1;
1229 		new_nlink = -old_nlink;
1230 		err = -EMLINK;
1231 		if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1232 			(new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1233 			goto out_new_dir;
1234 	}
1235 
1236 	f2fs_balance_fs(sbi, true);
1237 
1238 	f2fs_lock_op(sbi);
1239 
1240 	/* update ".." directory entry info of old dentry */
1241 	if (old_dir_entry)
1242 		f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1243 
1244 	/* update ".." directory entry info of new dentry */
1245 	if (new_dir_entry)
1246 		f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1247 
1248 	/* update directory entry info of old dir inode */
1249 	f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1250 
1251 	f2fs_down_write(&F2FS_I(old_inode)->i_sem);
1252 	if (!old_dir_entry)
1253 		file_lost_pino(old_inode);
1254 	else
1255 		/* adjust dir's i_pino to pass fsck check */
1256 		f2fs_i_pino_write(old_inode, new_dir->i_ino);
1257 	f2fs_up_write(&F2FS_I(old_inode)->i_sem);
1258 
1259 	inode_set_ctime_current(old_dir);
1260 	if (old_nlink) {
1261 		f2fs_down_write(&F2FS_I(old_dir)->i_sem);
1262 		f2fs_i_links_write(old_dir, old_nlink > 0);
1263 		f2fs_up_write(&F2FS_I(old_dir)->i_sem);
1264 	}
1265 	f2fs_mark_inode_dirty_sync(old_dir, false);
1266 
1267 	/* update directory entry info of new dir inode */
1268 	f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1269 
1270 	f2fs_down_write(&F2FS_I(new_inode)->i_sem);
1271 	if (!new_dir_entry)
1272 		file_lost_pino(new_inode);
1273 	else
1274 		/* adjust dir's i_pino to pass fsck check */
1275 		f2fs_i_pino_write(new_inode, old_dir->i_ino);
1276 	f2fs_up_write(&F2FS_I(new_inode)->i_sem);
1277 
1278 	inode_set_ctime_current(new_dir);
1279 	if (new_nlink) {
1280 		f2fs_down_write(&F2FS_I(new_dir)->i_sem);
1281 		f2fs_i_links_write(new_dir, new_nlink > 0);
1282 		f2fs_up_write(&F2FS_I(new_dir)->i_sem);
1283 	}
1284 	f2fs_mark_inode_dirty_sync(new_dir, false);
1285 
1286 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1287 		f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1288 		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1289 	}
1290 
1291 	f2fs_unlock_op(sbi);
1292 
1293 	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1294 		f2fs_sync_fs(sbi->sb, 1);
1295 
1296 	f2fs_update_time(sbi, REQ_TIME);
1297 	return 0;
1298 out_new_dir:
1299 	if (new_dir_entry) {
1300 		f2fs_put_page(new_dir_page, 0);
1301 	}
1302 out_old_dir:
1303 	if (old_dir_entry) {
1304 		f2fs_put_page(old_dir_page, 0);
1305 	}
1306 out_new:
1307 	f2fs_put_page(new_page, 0);
1308 out_old:
1309 	f2fs_put_page(old_page, 0);
1310 out:
1311 	return err;
1312 }
1313 
1314 static int f2fs_rename2(struct mnt_idmap *idmap,
1315 			struct inode *old_dir, struct dentry *old_dentry,
1316 			struct inode *new_dir, struct dentry *new_dentry,
1317 			unsigned int flags)
1318 {
1319 	int err;
1320 
1321 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1322 		return -EINVAL;
1323 
1324 	trace_f2fs_rename_start(old_dir, old_dentry, new_dir, new_dentry,
1325 								flags);
1326 
1327 	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1328 				     flags);
1329 	if (err)
1330 		return err;
1331 
1332 	if (flags & RENAME_EXCHANGE)
1333 		err = f2fs_cross_rename(old_dir, old_dentry,
1334 					new_dir, new_dentry);
1335 	else
1336 	/*
1337 	 * VFS has already handled the new dentry existence case,
1338 	 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1339 	 */
1340 		err = f2fs_rename(idmap, old_dir, old_dentry,
1341 					new_dir, new_dentry, flags);
1342 
1343 	trace_f2fs_rename_end(old_dentry, new_dentry, flags, err);
1344 	return err;
1345 }
1346 
1347 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1348 					   struct inode *inode,
1349 					   struct delayed_call *done)
1350 {
1351 	struct page *page;
1352 	const char *target;
1353 
1354 	if (!dentry)
1355 		return ERR_PTR(-ECHILD);
1356 
1357 	page = read_mapping_page(inode->i_mapping, 0, NULL);
1358 	if (IS_ERR(page))
1359 		return ERR_CAST(page);
1360 
1361 	target = fscrypt_get_symlink(inode, page_address(page),
1362 				     inode->i_sb->s_blocksize, done);
1363 	put_page(page);
1364 	return target;
1365 }
1366 
1367 static int f2fs_encrypted_symlink_getattr(struct mnt_idmap *idmap,
1368 					  const struct path *path,
1369 					  struct kstat *stat, u32 request_mask,
1370 					  unsigned int query_flags)
1371 {
1372 	f2fs_getattr(idmap, path, stat, request_mask, query_flags);
1373 
1374 	return fscrypt_symlink_getattr(path, stat);
1375 }
1376 
1377 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1378 	.get_link	= f2fs_encrypted_get_link,
1379 	.getattr	= f2fs_encrypted_symlink_getattr,
1380 	.setattr	= f2fs_setattr,
1381 	.listxattr	= f2fs_listxattr,
1382 };
1383 
1384 const struct inode_operations f2fs_dir_inode_operations = {
1385 	.create		= f2fs_create,
1386 	.lookup		= f2fs_lookup,
1387 	.link		= f2fs_link,
1388 	.unlink		= f2fs_unlink,
1389 	.symlink	= f2fs_symlink,
1390 	.mkdir		= f2fs_mkdir,
1391 	.rmdir		= f2fs_rmdir,
1392 	.mknod		= f2fs_mknod,
1393 	.rename		= f2fs_rename2,
1394 	.tmpfile	= f2fs_tmpfile,
1395 	.getattr	= f2fs_getattr,
1396 	.setattr	= f2fs_setattr,
1397 	.get_inode_acl	= f2fs_get_acl,
1398 	.set_acl	= f2fs_set_acl,
1399 	.listxattr	= f2fs_listxattr,
1400 	.fiemap		= f2fs_fiemap,
1401 	.fileattr_get	= f2fs_fileattr_get,
1402 	.fileattr_set	= f2fs_fileattr_set,
1403 };
1404 
1405 const struct inode_operations f2fs_symlink_inode_operations = {
1406 	.get_link	= f2fs_get_link,
1407 	.getattr	= f2fs_getattr,
1408 	.setattr	= f2fs_setattr,
1409 	.listxattr	= f2fs_listxattr,
1410 };
1411 
1412 const struct inode_operations f2fs_special_inode_operations = {
1413 	.getattr	= f2fs_getattr,
1414 	.setattr	= f2fs_setattr,
1415 	.get_inode_acl	= f2fs_get_acl,
1416 	.set_acl	= f2fs_set_acl,
1417 	.listxattr	= f2fs_listxattr,
1418 };
1419