xref: /openbmc/linux/fs/f2fs/namei.c (revision b04b076f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/f2fs/namei.c
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/pagemap.h>
11 #include <linux/sched.h>
12 #include <linux/ctype.h>
13 #include <linux/random.h>
14 #include <linux/dcache.h>
15 #include <linux/namei.h>
16 #include <linux/quotaops.h>
17 
18 #include "f2fs.h"
19 #include "node.h"
20 #include "segment.h"
21 #include "xattr.h"
22 #include "acl.h"
23 #include <trace/events/f2fs.h>
24 
25 static inline bool is_extension_exist(const unsigned char *s, const char *sub,
26 						bool tmp_ext)
27 {
28 	size_t slen = strlen(s);
29 	size_t sublen = strlen(sub);
30 	int i;
31 
32 	if (sublen == 1 && *sub == '*')
33 		return true;
34 
35 	/*
36 	 * filename format of multimedia file should be defined as:
37 	 * "filename + '.' + extension + (optional: '.' + temp extension)".
38 	 */
39 	if (slen < sublen + 2)
40 		return false;
41 
42 	if (!tmp_ext) {
43 		/* file has no temp extension */
44 		if (s[slen - sublen - 1] != '.')
45 			return false;
46 		return !strncasecmp(s + slen - sublen, sub, sublen);
47 	}
48 
49 	for (i = 1; i < slen - sublen; i++) {
50 		if (s[i] != '.')
51 			continue;
52 		if (!strncasecmp(s + i + 1, sub, sublen))
53 			return true;
54 	}
55 
56 	return false;
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 	/*
999 	 * Copied from ext4_rename: we need to protect against old.inode
1000 	 * directory getting converted from inline directory format into
1001 	 * a normal one.
1002 	 */
1003 	if (S_ISDIR(old_inode->i_mode))
1004 		inode_lock_nested(old_inode, I_MUTEX_NONDIR2);
1005 
1006 	err = -ENOENT;
1007 	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1008 	if (!old_entry) {
1009 		if (IS_ERR(old_page))
1010 			err = PTR_ERR(old_page);
1011 		goto out_unlock_old;
1012 	}
1013 
1014 	if (S_ISDIR(old_inode->i_mode)) {
1015 		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
1016 		if (!old_dir_entry) {
1017 			if (IS_ERR(old_dir_page))
1018 				err = PTR_ERR(old_dir_page);
1019 			goto out_old;
1020 		}
1021 	}
1022 
1023 	if (new_inode) {
1024 
1025 		err = -ENOTEMPTY;
1026 		if (old_dir_entry && !f2fs_empty_dir(new_inode))
1027 			goto out_dir;
1028 
1029 		err = -ENOENT;
1030 		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
1031 						&new_page);
1032 		if (!new_entry) {
1033 			if (IS_ERR(new_page))
1034 				err = PTR_ERR(new_page);
1035 			goto out_dir;
1036 		}
1037 
1038 		f2fs_balance_fs(sbi, true);
1039 
1040 		f2fs_lock_op(sbi);
1041 
1042 		err = f2fs_acquire_orphan_inode(sbi);
1043 		if (err)
1044 			goto put_out_dir;
1045 
1046 		f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1047 		new_page = NULL;
1048 
1049 		new_inode->i_ctime = current_time(new_inode);
1050 		f2fs_down_write(&F2FS_I(new_inode)->i_sem);
1051 		if (old_dir_entry)
1052 			f2fs_i_links_write(new_inode, false);
1053 		f2fs_i_links_write(new_inode, false);
1054 		f2fs_up_write(&F2FS_I(new_inode)->i_sem);
1055 
1056 		if (!new_inode->i_nlink)
1057 			f2fs_add_orphan_inode(new_inode);
1058 		else
1059 			f2fs_release_orphan_inode(sbi);
1060 	} else {
1061 		f2fs_balance_fs(sbi, true);
1062 
1063 		f2fs_lock_op(sbi);
1064 
1065 		err = f2fs_add_link(new_dentry, old_inode);
1066 		if (err) {
1067 			f2fs_unlock_op(sbi);
1068 			goto out_dir;
1069 		}
1070 
1071 		if (old_dir_entry)
1072 			f2fs_i_links_write(new_dir, true);
1073 	}
1074 
1075 	f2fs_down_write(&F2FS_I(old_inode)->i_sem);
1076 	if (!old_dir_entry || whiteout)
1077 		file_lost_pino(old_inode);
1078 	else
1079 		/* adjust dir's i_pino to pass fsck check */
1080 		f2fs_i_pino_write(old_inode, new_dir->i_ino);
1081 	f2fs_up_write(&F2FS_I(old_inode)->i_sem);
1082 
1083 	old_inode->i_ctime = current_time(old_inode);
1084 	f2fs_mark_inode_dirty_sync(old_inode, false);
1085 
1086 	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
1087 	old_page = NULL;
1088 
1089 	if (whiteout) {
1090 		set_inode_flag(whiteout, FI_INC_LINK);
1091 		err = f2fs_add_link(old_dentry, whiteout);
1092 		if (err)
1093 			goto put_out_dir;
1094 
1095 		spin_lock(&whiteout->i_lock);
1096 		whiteout->i_state &= ~I_LINKABLE;
1097 		spin_unlock(&whiteout->i_lock);
1098 
1099 		iput(whiteout);
1100 	}
1101 
1102 	if (old_dir_entry) {
1103 		if (old_dir != new_dir && !whiteout)
1104 			f2fs_set_link(old_inode, old_dir_entry,
1105 						old_dir_page, new_dir);
1106 		else
1107 			f2fs_put_page(old_dir_page, 0);
1108 		f2fs_i_links_write(old_dir, false);
1109 	}
1110 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1111 		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1112 		if (S_ISDIR(old_inode->i_mode))
1113 			f2fs_add_ino_entry(sbi, old_inode->i_ino,
1114 							TRANS_DIR_INO);
1115 	}
1116 
1117 	f2fs_unlock_op(sbi);
1118 
1119 	if (S_ISDIR(old_inode->i_mode))
1120 		inode_unlock(old_inode);
1121 
1122 	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1123 		f2fs_sync_fs(sbi->sb, 1);
1124 
1125 	f2fs_update_time(sbi, REQ_TIME);
1126 	return 0;
1127 
1128 put_out_dir:
1129 	f2fs_unlock_op(sbi);
1130 	f2fs_put_page(new_page, 0);
1131 out_dir:
1132 	if (old_dir_entry)
1133 		f2fs_put_page(old_dir_page, 0);
1134 out_old:
1135 	f2fs_put_page(old_page, 0);
1136 out_unlock_old:
1137 	if (S_ISDIR(old_inode->i_mode))
1138 		inode_unlock(old_inode);
1139 out:
1140 	iput(whiteout);
1141 	return err;
1142 }
1143 
1144 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1145 			     struct inode *new_dir, struct dentry *new_dentry)
1146 {
1147 	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1148 	struct inode *old_inode = d_inode(old_dentry);
1149 	struct inode *new_inode = d_inode(new_dentry);
1150 	struct page *old_dir_page, *new_dir_page;
1151 	struct page *old_page, *new_page;
1152 	struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1153 	struct f2fs_dir_entry *old_entry, *new_entry;
1154 	int old_nlink = 0, new_nlink = 0;
1155 	int err;
1156 
1157 	if (unlikely(f2fs_cp_error(sbi)))
1158 		return -EIO;
1159 	if (!f2fs_is_checkpoint_ready(sbi))
1160 		return -ENOSPC;
1161 
1162 	if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1163 			!projid_eq(F2FS_I(new_dir)->i_projid,
1164 			F2FS_I(old_dentry->d_inode)->i_projid)) ||
1165 	    (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1166 			!projid_eq(F2FS_I(old_dir)->i_projid,
1167 			F2FS_I(new_dentry->d_inode)->i_projid)))
1168 		return -EXDEV;
1169 
1170 	err = f2fs_dquot_initialize(old_dir);
1171 	if (err)
1172 		goto out;
1173 
1174 	err = f2fs_dquot_initialize(new_dir);
1175 	if (err)
1176 		goto out;
1177 
1178 	err = -ENOENT;
1179 	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1180 	if (!old_entry) {
1181 		if (IS_ERR(old_page))
1182 			err = PTR_ERR(old_page);
1183 		goto out;
1184 	}
1185 
1186 	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1187 	if (!new_entry) {
1188 		if (IS_ERR(new_page))
1189 			err = PTR_ERR(new_page);
1190 		goto out_old;
1191 	}
1192 
1193 	/* prepare for updating ".." directory entry info later */
1194 	if (old_dir != new_dir) {
1195 		if (S_ISDIR(old_inode->i_mode)) {
1196 			old_dir_entry = f2fs_parent_dir(old_inode,
1197 							&old_dir_page);
1198 			if (!old_dir_entry) {
1199 				if (IS_ERR(old_dir_page))
1200 					err = PTR_ERR(old_dir_page);
1201 				goto out_new;
1202 			}
1203 		}
1204 
1205 		if (S_ISDIR(new_inode->i_mode)) {
1206 			new_dir_entry = f2fs_parent_dir(new_inode,
1207 							&new_dir_page);
1208 			if (!new_dir_entry) {
1209 				if (IS_ERR(new_dir_page))
1210 					err = PTR_ERR(new_dir_page);
1211 				goto out_old_dir;
1212 			}
1213 		}
1214 	}
1215 
1216 	/*
1217 	 * If cross rename between file and directory those are not
1218 	 * in the same directory, we will inc nlink of file's parent
1219 	 * later, so we should check upper boundary of its nlink.
1220 	 */
1221 	if ((!old_dir_entry || !new_dir_entry) &&
1222 				old_dir_entry != new_dir_entry) {
1223 		old_nlink = old_dir_entry ? -1 : 1;
1224 		new_nlink = -old_nlink;
1225 		err = -EMLINK;
1226 		if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1227 			(new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1228 			goto out_new_dir;
1229 	}
1230 
1231 	f2fs_balance_fs(sbi, true);
1232 
1233 	f2fs_lock_op(sbi);
1234 
1235 	/* update ".." directory entry info of old dentry */
1236 	if (old_dir_entry)
1237 		f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1238 
1239 	/* update ".." directory entry info of new dentry */
1240 	if (new_dir_entry)
1241 		f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1242 
1243 	/* update directory entry info of old dir inode */
1244 	f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1245 
1246 	f2fs_down_write(&F2FS_I(old_inode)->i_sem);
1247 	if (!old_dir_entry)
1248 		file_lost_pino(old_inode);
1249 	else
1250 		/* adjust dir's i_pino to pass fsck check */
1251 		f2fs_i_pino_write(old_inode, new_dir->i_ino);
1252 	f2fs_up_write(&F2FS_I(old_inode)->i_sem);
1253 
1254 	old_dir->i_ctime = current_time(old_dir);
1255 	if (old_nlink) {
1256 		f2fs_down_write(&F2FS_I(old_dir)->i_sem);
1257 		f2fs_i_links_write(old_dir, old_nlink > 0);
1258 		f2fs_up_write(&F2FS_I(old_dir)->i_sem);
1259 	}
1260 	f2fs_mark_inode_dirty_sync(old_dir, false);
1261 
1262 	/* update directory entry info of new dir inode */
1263 	f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1264 
1265 	f2fs_down_write(&F2FS_I(new_inode)->i_sem);
1266 	if (!new_dir_entry)
1267 		file_lost_pino(new_inode);
1268 	else
1269 		/* adjust dir's i_pino to pass fsck check */
1270 		f2fs_i_pino_write(new_inode, old_dir->i_ino);
1271 	f2fs_up_write(&F2FS_I(new_inode)->i_sem);
1272 
1273 	new_dir->i_ctime = current_time(new_dir);
1274 	if (new_nlink) {
1275 		f2fs_down_write(&F2FS_I(new_dir)->i_sem);
1276 		f2fs_i_links_write(new_dir, new_nlink > 0);
1277 		f2fs_up_write(&F2FS_I(new_dir)->i_sem);
1278 	}
1279 	f2fs_mark_inode_dirty_sync(new_dir, false);
1280 
1281 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1282 		f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1283 		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1284 	}
1285 
1286 	f2fs_unlock_op(sbi);
1287 
1288 	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1289 		f2fs_sync_fs(sbi->sb, 1);
1290 
1291 	f2fs_update_time(sbi, REQ_TIME);
1292 	return 0;
1293 out_new_dir:
1294 	if (new_dir_entry) {
1295 		f2fs_put_page(new_dir_page, 0);
1296 	}
1297 out_old_dir:
1298 	if (old_dir_entry) {
1299 		f2fs_put_page(old_dir_page, 0);
1300 	}
1301 out_new:
1302 	f2fs_put_page(new_page, 0);
1303 out_old:
1304 	f2fs_put_page(old_page, 0);
1305 out:
1306 	return err;
1307 }
1308 
1309 static int f2fs_rename2(struct mnt_idmap *idmap,
1310 			struct inode *old_dir, struct dentry *old_dentry,
1311 			struct inode *new_dir, struct dentry *new_dentry,
1312 			unsigned int flags)
1313 {
1314 	int err;
1315 
1316 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1317 		return -EINVAL;
1318 
1319 	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1320 				     flags);
1321 	if (err)
1322 		return err;
1323 
1324 	if (flags & RENAME_EXCHANGE) {
1325 		return f2fs_cross_rename(old_dir, old_dentry,
1326 					 new_dir, new_dentry);
1327 	}
1328 	/*
1329 	 * VFS has already handled the new dentry existence case,
1330 	 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1331 	 */
1332 	return f2fs_rename(idmap, old_dir, old_dentry,
1333 					new_dir, new_dentry, flags);
1334 }
1335 
1336 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1337 					   struct inode *inode,
1338 					   struct delayed_call *done)
1339 {
1340 	struct page *page;
1341 	const char *target;
1342 
1343 	if (!dentry)
1344 		return ERR_PTR(-ECHILD);
1345 
1346 	page = read_mapping_page(inode->i_mapping, 0, NULL);
1347 	if (IS_ERR(page))
1348 		return ERR_CAST(page);
1349 
1350 	target = fscrypt_get_symlink(inode, page_address(page),
1351 				     inode->i_sb->s_blocksize, done);
1352 	put_page(page);
1353 	return target;
1354 }
1355 
1356 static int f2fs_encrypted_symlink_getattr(struct mnt_idmap *idmap,
1357 					  const struct path *path,
1358 					  struct kstat *stat, u32 request_mask,
1359 					  unsigned int query_flags)
1360 {
1361 	f2fs_getattr(idmap, path, stat, request_mask, query_flags);
1362 
1363 	return fscrypt_symlink_getattr(path, stat);
1364 }
1365 
1366 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1367 	.get_link	= f2fs_encrypted_get_link,
1368 	.getattr	= f2fs_encrypted_symlink_getattr,
1369 	.setattr	= f2fs_setattr,
1370 	.listxattr	= f2fs_listxattr,
1371 };
1372 
1373 const struct inode_operations f2fs_dir_inode_operations = {
1374 	.create		= f2fs_create,
1375 	.lookup		= f2fs_lookup,
1376 	.link		= f2fs_link,
1377 	.unlink		= f2fs_unlink,
1378 	.symlink	= f2fs_symlink,
1379 	.mkdir		= f2fs_mkdir,
1380 	.rmdir		= f2fs_rmdir,
1381 	.mknod		= f2fs_mknod,
1382 	.rename		= f2fs_rename2,
1383 	.tmpfile	= f2fs_tmpfile,
1384 	.getattr	= f2fs_getattr,
1385 	.setattr	= f2fs_setattr,
1386 	.get_inode_acl	= f2fs_get_acl,
1387 	.set_acl	= f2fs_set_acl,
1388 	.listxattr	= f2fs_listxattr,
1389 	.fiemap		= f2fs_fiemap,
1390 	.fileattr_get	= f2fs_fileattr_get,
1391 	.fileattr_set	= f2fs_fileattr_set,
1392 };
1393 
1394 const struct inode_operations f2fs_symlink_inode_operations = {
1395 	.get_link	= f2fs_get_link,
1396 	.getattr	= f2fs_getattr,
1397 	.setattr	= f2fs_setattr,
1398 	.listxattr	= f2fs_listxattr,
1399 };
1400 
1401 const struct inode_operations f2fs_special_inode_operations = {
1402 	.getattr	= f2fs_getattr,
1403 	.setattr	= f2fs_setattr,
1404 	.get_inode_acl	= f2fs_get_acl,
1405 	.set_acl	= f2fs_set_acl,
1406 	.listxattr	= f2fs_listxattr,
1407 };
1408