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