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