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