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