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