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