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