xref: /linux/fs/f2fs/namei.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
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 	simple_inode_init_ts(inode);
247 	F2FS_I(inode)->i_crtime = inode_get_mtime(inode);
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 f2fs_dir_entry *de;
463 	struct page *page;
464 	int err = 0;
465 
466 	if (f2fs_readonly(sbi->sb)) {
467 		f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
468 			  dir->i_ino, pino);
469 		return 0;
470 	}
471 
472 	if (!S_ISDIR(dir->i_mode)) {
473 		f2fs_err(sbi, "inconsistent inode status, skip recovering inline_dots inode (ino:%lu, i_mode:%u, pino:%u)",
474 			  dir->i_ino, dir->i_mode, pino);
475 		set_sbi_flag(sbi, SBI_NEED_FSCK);
476 		return -ENOTDIR;
477 	}
478 
479 	err = f2fs_dquot_initialize(dir);
480 	if (err)
481 		return err;
482 
483 	f2fs_balance_fs(sbi, true);
484 
485 	f2fs_lock_op(sbi);
486 
487 	de = f2fs_find_entry(dir, &dot, &page);
488 	if (de) {
489 		f2fs_put_page(page, 0);
490 	} else if (IS_ERR(page)) {
491 		err = PTR_ERR(page);
492 		goto out;
493 	} else {
494 		err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
495 		if (err)
496 			goto out;
497 	}
498 
499 	de = f2fs_find_entry(dir, &dotdot_name, &page);
500 	if (de)
501 		f2fs_put_page(page, 0);
502 	else if (IS_ERR(page))
503 		err = PTR_ERR(page);
504 	else
505 		err = f2fs_do_add_link(dir, &dotdot_name, NULL, pino, S_IFDIR);
506 out:
507 	if (!err)
508 		clear_inode_flag(dir, FI_INLINE_DOTS);
509 
510 	f2fs_unlock_op(sbi);
511 	return err;
512 }
513 
514 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
515 		unsigned int flags)
516 {
517 	struct inode *inode = NULL;
518 	struct f2fs_dir_entry *de;
519 	struct page *page;
520 	struct dentry *new;
521 	nid_t ino = -1;
522 	int err = 0;
523 	unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
524 	struct f2fs_filename fname;
525 
526 	trace_f2fs_lookup_start(dir, dentry, flags);
527 
528 	if (dentry->d_name.len > F2FS_NAME_LEN) {
529 		err = -ENAMETOOLONG;
530 		goto out;
531 	}
532 
533 	err = f2fs_prepare_lookup(dir, dentry, &fname);
534 	generic_set_encrypted_ci_d_ops(dentry);
535 	if (err == -ENOENT)
536 		goto out_splice;
537 	if (err)
538 		goto out;
539 	de = __f2fs_find_entry(dir, &fname, &page);
540 	f2fs_free_filename(&fname);
541 
542 	if (!de) {
543 		if (IS_ERR(page)) {
544 			err = PTR_ERR(page);
545 			goto out;
546 		}
547 		err = -ENOENT;
548 		goto out_splice;
549 	}
550 
551 	ino = le32_to_cpu(de->ino);
552 	f2fs_put_page(page, 0);
553 
554 	inode = f2fs_iget(dir->i_sb, ino);
555 	if (IS_ERR(inode)) {
556 		err = PTR_ERR(inode);
557 		goto out;
558 	}
559 
560 	if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
561 		err = __recover_dot_dentries(dir, root_ino);
562 		if (err)
563 			goto out_iput;
564 	}
565 
566 	if (f2fs_has_inline_dots(inode)) {
567 		err = __recover_dot_dentries(inode, dir->i_ino);
568 		if (err)
569 			goto out_iput;
570 	}
571 	if (IS_ENCRYPTED(dir) &&
572 	    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
573 	    !fscrypt_has_permitted_context(dir, inode)) {
574 		f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
575 			  dir->i_ino, inode->i_ino);
576 		err = -EPERM;
577 		goto out_iput;
578 	}
579 out_splice:
580 #if IS_ENABLED(CONFIG_UNICODE)
581 	if (!inode && IS_CASEFOLDED(dir)) {
582 		/* Eventually we want to call d_add_ci(dentry, NULL)
583 		 * for negative dentries in the encoding case as
584 		 * well.  For now, prevent the negative dentry
585 		 * from being cached.
586 		 */
587 		trace_f2fs_lookup_end(dir, dentry, ino, err);
588 		return NULL;
589 	}
590 #endif
591 	new = d_splice_alias(inode, dentry);
592 	trace_f2fs_lookup_end(dir, !IS_ERR_OR_NULL(new) ? new : dentry,
593 				ino, IS_ERR(new) ? PTR_ERR(new) : err);
594 	return new;
595 out_iput:
596 	iput(inode);
597 out:
598 	trace_f2fs_lookup_end(dir, dentry, ino, err);
599 	return ERR_PTR(err);
600 }
601 
602 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
603 {
604 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
605 	struct inode *inode = d_inode(dentry);
606 	struct f2fs_dir_entry *de;
607 	struct page *page;
608 	int err;
609 
610 	trace_f2fs_unlink_enter(dir, dentry);
611 
612 	if (unlikely(f2fs_cp_error(sbi))) {
613 		err = -EIO;
614 		goto fail;
615 	}
616 
617 	err = f2fs_dquot_initialize(dir);
618 	if (err)
619 		goto fail;
620 	err = f2fs_dquot_initialize(inode);
621 	if (err)
622 		goto fail;
623 
624 	de = f2fs_find_entry(dir, &dentry->d_name, &page);
625 	if (!de) {
626 		if (IS_ERR(page))
627 			err = PTR_ERR(page);
628 		goto fail;
629 	}
630 
631 	f2fs_balance_fs(sbi, true);
632 
633 	f2fs_lock_op(sbi);
634 	err = f2fs_acquire_orphan_inode(sbi);
635 	if (err) {
636 		f2fs_unlock_op(sbi);
637 		f2fs_put_page(page, 0);
638 		goto fail;
639 	}
640 	f2fs_delete_entry(de, page, dir, inode);
641 	f2fs_unlock_op(sbi);
642 
643 #if IS_ENABLED(CONFIG_UNICODE)
644 	/* VFS negative dentries are incompatible with Encoding and
645 	 * Case-insensitiveness. Eventually we'll want avoid
646 	 * invalidating the dentries here, alongside with returning the
647 	 * negative dentries at f2fs_lookup(), when it is better
648 	 * supported by the VFS for the CI case.
649 	 */
650 	if (IS_CASEFOLDED(dir))
651 		d_invalidate(dentry);
652 #endif
653 	if (IS_DIRSYNC(dir))
654 		f2fs_sync_fs(sbi->sb, 1);
655 fail:
656 	trace_f2fs_unlink_exit(inode, err);
657 	return err;
658 }
659 
660 static const char *f2fs_get_link(struct dentry *dentry,
661 				 struct inode *inode,
662 				 struct delayed_call *done)
663 {
664 	const char *link = page_get_link(dentry, inode, done);
665 
666 	if (!IS_ERR(link) && !*link) {
667 		/* this is broken symlink case */
668 		do_delayed_call(done);
669 		clear_delayed_call(done);
670 		link = ERR_PTR(-ENOENT);
671 	}
672 	return link;
673 }
674 
675 static int f2fs_symlink(struct mnt_idmap *idmap, struct inode *dir,
676 			struct dentry *dentry, const char *symname)
677 {
678 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
679 	struct inode *inode;
680 	size_t len = strlen(symname);
681 	struct fscrypt_str disk_link;
682 	int err;
683 
684 	if (unlikely(f2fs_cp_error(sbi)))
685 		return -EIO;
686 	if (!f2fs_is_checkpoint_ready(sbi))
687 		return -ENOSPC;
688 
689 	err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
690 				      &disk_link);
691 	if (err)
692 		return err;
693 
694 	err = f2fs_dquot_initialize(dir);
695 	if (err)
696 		return err;
697 
698 	inode = f2fs_new_inode(idmap, dir, S_IFLNK | S_IRWXUGO, NULL);
699 	if (IS_ERR(inode))
700 		return PTR_ERR(inode);
701 
702 	if (IS_ENCRYPTED(inode))
703 		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
704 	else
705 		inode->i_op = &f2fs_symlink_inode_operations;
706 	inode_nohighmem(inode);
707 	inode->i_mapping->a_ops = &f2fs_dblock_aops;
708 
709 	f2fs_lock_op(sbi);
710 	err = f2fs_add_link(dentry, inode);
711 	if (err)
712 		goto out_f2fs_handle_failed_inode;
713 	f2fs_unlock_op(sbi);
714 	f2fs_alloc_nid_done(sbi, inode->i_ino);
715 
716 	err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
717 	if (err)
718 		goto err_out;
719 
720 	err = page_symlink(inode, disk_link.name, disk_link.len);
721 
722 err_out:
723 	d_instantiate_new(dentry, inode);
724 
725 	/*
726 	 * Let's flush symlink data in order to avoid broken symlink as much as
727 	 * possible. Nevertheless, fsyncing is the best way, but there is no
728 	 * way to get a file descriptor in order to flush that.
729 	 *
730 	 * Note that, it needs to do dir->fsync to make this recoverable.
731 	 * If the symlink path is stored into inline_data, there is no
732 	 * performance regression.
733 	 */
734 	if (!err) {
735 		filemap_write_and_wait_range(inode->i_mapping, 0,
736 							disk_link.len - 1);
737 
738 		if (IS_DIRSYNC(dir))
739 			f2fs_sync_fs(sbi->sb, 1);
740 	} else {
741 		f2fs_unlink(dir, dentry);
742 	}
743 
744 	f2fs_balance_fs(sbi, true);
745 	goto out_free_encrypted_link;
746 
747 out_f2fs_handle_failed_inode:
748 	f2fs_handle_failed_inode(inode);
749 out_free_encrypted_link:
750 	if (disk_link.name != (unsigned char *)symname)
751 		kfree(disk_link.name);
752 	return err;
753 }
754 
755 static int f2fs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
756 		      struct dentry *dentry, umode_t mode)
757 {
758 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
759 	struct inode *inode;
760 	int err;
761 
762 	if (unlikely(f2fs_cp_error(sbi)))
763 		return -EIO;
764 
765 	err = f2fs_dquot_initialize(dir);
766 	if (err)
767 		return err;
768 
769 	inode = f2fs_new_inode(idmap, dir, S_IFDIR | mode, NULL);
770 	if (IS_ERR(inode))
771 		return PTR_ERR(inode);
772 
773 	inode->i_op = &f2fs_dir_inode_operations;
774 	inode->i_fop = &f2fs_dir_operations;
775 	inode->i_mapping->a_ops = &f2fs_dblock_aops;
776 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
777 
778 	set_inode_flag(inode, FI_INC_LINK);
779 	f2fs_lock_op(sbi);
780 	err = f2fs_add_link(dentry, inode);
781 	if (err)
782 		goto out_fail;
783 	f2fs_unlock_op(sbi);
784 
785 	f2fs_alloc_nid_done(sbi, inode->i_ino);
786 
787 	d_instantiate_new(dentry, inode);
788 
789 	if (IS_DIRSYNC(dir))
790 		f2fs_sync_fs(sbi->sb, 1);
791 
792 	f2fs_balance_fs(sbi, true);
793 	return 0;
794 
795 out_fail:
796 	clear_inode_flag(inode, FI_INC_LINK);
797 	f2fs_handle_failed_inode(inode);
798 	return err;
799 }
800 
801 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
802 {
803 	struct inode *inode = d_inode(dentry);
804 
805 	if (f2fs_empty_dir(inode))
806 		return f2fs_unlink(dir, dentry);
807 	return -ENOTEMPTY;
808 }
809 
810 static int f2fs_mknod(struct mnt_idmap *idmap, struct inode *dir,
811 		      struct dentry *dentry, umode_t mode, dev_t rdev)
812 {
813 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
814 	struct inode *inode;
815 	int err = 0;
816 
817 	if (unlikely(f2fs_cp_error(sbi)))
818 		return -EIO;
819 	if (!f2fs_is_checkpoint_ready(sbi))
820 		return -ENOSPC;
821 
822 	err = f2fs_dquot_initialize(dir);
823 	if (err)
824 		return err;
825 
826 	inode = f2fs_new_inode(idmap, dir, mode, NULL);
827 	if (IS_ERR(inode))
828 		return PTR_ERR(inode);
829 
830 	init_special_inode(inode, inode->i_mode, rdev);
831 	inode->i_op = &f2fs_special_inode_operations;
832 
833 	f2fs_lock_op(sbi);
834 	err = f2fs_add_link(dentry, inode);
835 	if (err)
836 		goto out;
837 	f2fs_unlock_op(sbi);
838 
839 	f2fs_alloc_nid_done(sbi, inode->i_ino);
840 
841 	d_instantiate_new(dentry, inode);
842 
843 	if (IS_DIRSYNC(dir))
844 		f2fs_sync_fs(sbi->sb, 1);
845 
846 	f2fs_balance_fs(sbi, true);
847 	return 0;
848 out:
849 	f2fs_handle_failed_inode(inode);
850 	return err;
851 }
852 
853 static int __f2fs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
854 			  struct file *file, umode_t mode, bool is_whiteout,
855 			  struct inode **new_inode)
856 {
857 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
858 	struct inode *inode;
859 	int err;
860 
861 	err = f2fs_dquot_initialize(dir);
862 	if (err)
863 		return err;
864 
865 	inode = f2fs_new_inode(idmap, dir, mode, NULL);
866 	if (IS_ERR(inode))
867 		return PTR_ERR(inode);
868 
869 	if (is_whiteout) {
870 		init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
871 		inode->i_op = &f2fs_special_inode_operations;
872 	} else {
873 		inode->i_op = &f2fs_file_inode_operations;
874 		inode->i_fop = &f2fs_file_operations;
875 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
876 	}
877 
878 	f2fs_lock_op(sbi);
879 	err = f2fs_acquire_orphan_inode(sbi);
880 	if (err)
881 		goto out;
882 
883 	err = f2fs_do_tmpfile(inode, dir);
884 	if (err)
885 		goto release_out;
886 
887 	/*
888 	 * add this non-linked tmpfile to orphan list, in this way we could
889 	 * remove all unused data of tmpfile after abnormal power-off.
890 	 */
891 	f2fs_add_orphan_inode(inode);
892 	f2fs_alloc_nid_done(sbi, inode->i_ino);
893 
894 	if (is_whiteout) {
895 		f2fs_i_links_write(inode, false);
896 
897 		spin_lock(&inode->i_lock);
898 		inode->i_state |= I_LINKABLE;
899 		spin_unlock(&inode->i_lock);
900 	} else {
901 		if (file)
902 			d_tmpfile(file, inode);
903 		else
904 			f2fs_i_links_write(inode, false);
905 	}
906 	/* link_count was changed by d_tmpfile as well. */
907 	f2fs_unlock_op(sbi);
908 	unlock_new_inode(inode);
909 
910 	if (new_inode)
911 		*new_inode = inode;
912 
913 	f2fs_balance_fs(sbi, true);
914 	return 0;
915 
916 release_out:
917 	f2fs_release_orphan_inode(sbi);
918 out:
919 	f2fs_handle_failed_inode(inode);
920 	return err;
921 }
922 
923 static int f2fs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
924 			struct file *file, umode_t mode)
925 {
926 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
927 	int err;
928 
929 	if (unlikely(f2fs_cp_error(sbi)))
930 		return -EIO;
931 	if (!f2fs_is_checkpoint_ready(sbi))
932 		return -ENOSPC;
933 
934 	err = __f2fs_tmpfile(idmap, dir, file, mode, false, NULL);
935 
936 	return finish_open_simple(file, err);
937 }
938 
939 static int f2fs_create_whiteout(struct mnt_idmap *idmap,
940 				struct inode *dir, struct inode **whiteout)
941 {
942 	return __f2fs_tmpfile(idmap, dir, NULL,
943 				S_IFCHR | WHITEOUT_MODE, true, whiteout);
944 }
945 
946 int f2fs_get_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
947 		     struct inode **new_inode)
948 {
949 	return __f2fs_tmpfile(idmap, dir, NULL, S_IFREG, false, new_inode);
950 }
951 
952 static int f2fs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
953 			struct dentry *old_dentry, struct inode *new_dir,
954 			struct dentry *new_dentry, unsigned int flags)
955 {
956 	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
957 	struct inode *old_inode = d_inode(old_dentry);
958 	struct inode *new_inode = d_inode(new_dentry);
959 	struct inode *whiteout = NULL;
960 	struct page *old_dir_page = NULL;
961 	struct page *old_page, *new_page = NULL;
962 	struct f2fs_dir_entry *old_dir_entry = NULL;
963 	struct f2fs_dir_entry *old_entry;
964 	struct f2fs_dir_entry *new_entry;
965 	bool old_is_dir = S_ISDIR(old_inode->i_mode);
966 	int err;
967 
968 	if (unlikely(f2fs_cp_error(sbi)))
969 		return -EIO;
970 	if (!f2fs_is_checkpoint_ready(sbi))
971 		return -ENOSPC;
972 
973 	if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
974 			(!projid_eq(F2FS_I(new_dir)->i_projid,
975 			F2FS_I(old_dentry->d_inode)->i_projid)))
976 		return -EXDEV;
977 
978 	/*
979 	 * If new_inode is null, the below renaming flow will
980 	 * add a link in old_dir which can convert inline_dir.
981 	 * After then, if we failed to get the entry due to other
982 	 * reasons like ENOMEM, we had to remove the new entry.
983 	 * Instead of adding such the error handling routine, let's
984 	 * simply convert first here.
985 	 */
986 	if (old_dir == new_dir && !new_inode) {
987 		err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
988 		if (err)
989 			return err;
990 	}
991 
992 	if (flags & RENAME_WHITEOUT) {
993 		err = f2fs_create_whiteout(idmap, old_dir, &whiteout);
994 		if (err)
995 			return err;
996 	}
997 
998 	err = f2fs_dquot_initialize(old_dir);
999 	if (err)
1000 		goto out;
1001 
1002 	err = f2fs_dquot_initialize(new_dir);
1003 	if (err)
1004 		goto out;
1005 
1006 	if (new_inode) {
1007 		err = f2fs_dquot_initialize(new_inode);
1008 		if (err)
1009 			goto out;
1010 	}
1011 
1012 	err = -ENOENT;
1013 	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1014 	if (!old_entry) {
1015 		if (IS_ERR(old_page))
1016 			err = PTR_ERR(old_page);
1017 		goto out;
1018 	}
1019 
1020 	if (old_is_dir && old_dir != new_dir) {
1021 		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
1022 		if (!old_dir_entry) {
1023 			if (IS_ERR(old_dir_page))
1024 				err = PTR_ERR(old_dir_page);
1025 			goto out_old;
1026 		}
1027 	}
1028 
1029 	if (new_inode) {
1030 
1031 		err = -ENOTEMPTY;
1032 		if (old_is_dir && !f2fs_empty_dir(new_inode))
1033 			goto out_dir;
1034 
1035 		err = -ENOENT;
1036 		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
1037 						&new_page);
1038 		if (!new_entry) {
1039 			if (IS_ERR(new_page))
1040 				err = PTR_ERR(new_page);
1041 			goto out_dir;
1042 		}
1043 
1044 		f2fs_balance_fs(sbi, true);
1045 
1046 		f2fs_lock_op(sbi);
1047 
1048 		err = f2fs_acquire_orphan_inode(sbi);
1049 		if (err)
1050 			goto put_out_dir;
1051 
1052 		f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1053 		new_page = NULL;
1054 
1055 		inode_set_ctime_current(new_inode);
1056 		f2fs_down_write(&F2FS_I(new_inode)->i_sem);
1057 		if (old_is_dir)
1058 			f2fs_i_links_write(new_inode, false);
1059 		f2fs_i_links_write(new_inode, false);
1060 		f2fs_up_write(&F2FS_I(new_inode)->i_sem);
1061 
1062 		if (!new_inode->i_nlink)
1063 			f2fs_add_orphan_inode(new_inode);
1064 		else
1065 			f2fs_release_orphan_inode(sbi);
1066 	} else {
1067 		f2fs_balance_fs(sbi, true);
1068 
1069 		f2fs_lock_op(sbi);
1070 
1071 		err = f2fs_add_link(new_dentry, old_inode);
1072 		if (err) {
1073 			f2fs_unlock_op(sbi);
1074 			goto out_dir;
1075 		}
1076 
1077 		if (old_is_dir)
1078 			f2fs_i_links_write(new_dir, true);
1079 	}
1080 
1081 	f2fs_down_write(&F2FS_I(old_inode)->i_sem);
1082 	if (!old_is_dir || whiteout)
1083 		file_lost_pino(old_inode);
1084 	else
1085 		/* adjust dir's i_pino to pass fsck check */
1086 		f2fs_i_pino_write(old_inode, new_dir->i_ino);
1087 	f2fs_up_write(&F2FS_I(old_inode)->i_sem);
1088 
1089 	inode_set_ctime_current(old_inode);
1090 	f2fs_mark_inode_dirty_sync(old_inode, false);
1091 
1092 	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
1093 	old_page = NULL;
1094 
1095 	if (whiteout) {
1096 		set_inode_flag(whiteout, FI_INC_LINK);
1097 		err = f2fs_add_link(old_dentry, whiteout);
1098 		if (err)
1099 			goto put_out_dir;
1100 
1101 		spin_lock(&whiteout->i_lock);
1102 		whiteout->i_state &= ~I_LINKABLE;
1103 		spin_unlock(&whiteout->i_lock);
1104 
1105 		iput(whiteout);
1106 	}
1107 
1108 	if (old_is_dir) {
1109 		if (old_dir_entry)
1110 			f2fs_set_link(old_inode, old_dir_entry,
1111 						old_dir_page, new_dir);
1112 		else
1113 			f2fs_put_page(old_dir_page, 0);
1114 		f2fs_i_links_write(old_dir, false);
1115 	}
1116 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1117 		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1118 		if (S_ISDIR(old_inode->i_mode))
1119 			f2fs_add_ino_entry(sbi, old_inode->i_ino,
1120 							TRANS_DIR_INO);
1121 	}
1122 
1123 	f2fs_unlock_op(sbi);
1124 
1125 	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1126 		f2fs_sync_fs(sbi->sb, 1);
1127 
1128 	f2fs_update_time(sbi, REQ_TIME);
1129 	return 0;
1130 
1131 put_out_dir:
1132 	f2fs_unlock_op(sbi);
1133 	f2fs_put_page(new_page, 0);
1134 out_dir:
1135 	if (old_dir_entry)
1136 		f2fs_put_page(old_dir_page, 0);
1137 out_old:
1138 	f2fs_put_page(old_page, 0);
1139 out:
1140 	iput(whiteout);
1141 	return err;
1142 }
1143 
1144 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1145 			     struct inode *new_dir, struct dentry *new_dentry)
1146 {
1147 	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1148 	struct inode *old_inode = d_inode(old_dentry);
1149 	struct inode *new_inode = d_inode(new_dentry);
1150 	struct page *old_dir_page, *new_dir_page;
1151 	struct page *old_page, *new_page;
1152 	struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1153 	struct f2fs_dir_entry *old_entry, *new_entry;
1154 	int old_nlink = 0, new_nlink = 0;
1155 	int err;
1156 
1157 	if (unlikely(f2fs_cp_error(sbi)))
1158 		return -EIO;
1159 	if (!f2fs_is_checkpoint_ready(sbi))
1160 		return -ENOSPC;
1161 
1162 	if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1163 			!projid_eq(F2FS_I(new_dir)->i_projid,
1164 			F2FS_I(old_dentry->d_inode)->i_projid)) ||
1165 	    (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1166 			!projid_eq(F2FS_I(old_dir)->i_projid,
1167 			F2FS_I(new_dentry->d_inode)->i_projid)))
1168 		return -EXDEV;
1169 
1170 	err = f2fs_dquot_initialize(old_dir);
1171 	if (err)
1172 		goto out;
1173 
1174 	err = f2fs_dquot_initialize(new_dir);
1175 	if (err)
1176 		goto out;
1177 
1178 	err = -ENOENT;
1179 	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1180 	if (!old_entry) {
1181 		if (IS_ERR(old_page))
1182 			err = PTR_ERR(old_page);
1183 		goto out;
1184 	}
1185 
1186 	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1187 	if (!new_entry) {
1188 		if (IS_ERR(new_page))
1189 			err = PTR_ERR(new_page);
1190 		goto out_old;
1191 	}
1192 
1193 	/* prepare for updating ".." directory entry info later */
1194 	if (old_dir != new_dir) {
1195 		if (S_ISDIR(old_inode->i_mode)) {
1196 			old_dir_entry = f2fs_parent_dir(old_inode,
1197 							&old_dir_page);
1198 			if (!old_dir_entry) {
1199 				if (IS_ERR(old_dir_page))
1200 					err = PTR_ERR(old_dir_page);
1201 				goto out_new;
1202 			}
1203 		}
1204 
1205 		if (S_ISDIR(new_inode->i_mode)) {
1206 			new_dir_entry = f2fs_parent_dir(new_inode,
1207 							&new_dir_page);
1208 			if (!new_dir_entry) {
1209 				if (IS_ERR(new_dir_page))
1210 					err = PTR_ERR(new_dir_page);
1211 				goto out_old_dir;
1212 			}
1213 		}
1214 	}
1215 
1216 	/*
1217 	 * If cross rename between file and directory those are not
1218 	 * in the same directory, we will inc nlink of file's parent
1219 	 * later, so we should check upper boundary of its nlink.
1220 	 */
1221 	if ((!old_dir_entry || !new_dir_entry) &&
1222 				old_dir_entry != new_dir_entry) {
1223 		old_nlink = old_dir_entry ? -1 : 1;
1224 		new_nlink = -old_nlink;
1225 		err = -EMLINK;
1226 		if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1227 			(new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1228 			goto out_new_dir;
1229 	}
1230 
1231 	f2fs_balance_fs(sbi, true);
1232 
1233 	f2fs_lock_op(sbi);
1234 
1235 	/* update ".." directory entry info of old dentry */
1236 	if (old_dir_entry)
1237 		f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1238 
1239 	/* update ".." directory entry info of new dentry */
1240 	if (new_dir_entry)
1241 		f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1242 
1243 	/* update directory entry info of old dir inode */
1244 	f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1245 
1246 	f2fs_down_write(&F2FS_I(old_inode)->i_sem);
1247 	if (!old_dir_entry)
1248 		file_lost_pino(old_inode);
1249 	else
1250 		/* adjust dir's i_pino to pass fsck check */
1251 		f2fs_i_pino_write(old_inode, new_dir->i_ino);
1252 	f2fs_up_write(&F2FS_I(old_inode)->i_sem);
1253 
1254 	inode_set_ctime_current(old_dir);
1255 	if (old_nlink) {
1256 		f2fs_down_write(&F2FS_I(old_dir)->i_sem);
1257 		f2fs_i_links_write(old_dir, old_nlink > 0);
1258 		f2fs_up_write(&F2FS_I(old_dir)->i_sem);
1259 	}
1260 	f2fs_mark_inode_dirty_sync(old_dir, false);
1261 
1262 	/* update directory entry info of new dir inode */
1263 	f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1264 
1265 	f2fs_down_write(&F2FS_I(new_inode)->i_sem);
1266 	if (!new_dir_entry)
1267 		file_lost_pino(new_inode);
1268 	else
1269 		/* adjust dir's i_pino to pass fsck check */
1270 		f2fs_i_pino_write(new_inode, old_dir->i_ino);
1271 	f2fs_up_write(&F2FS_I(new_inode)->i_sem);
1272 
1273 	inode_set_ctime_current(new_dir);
1274 	if (new_nlink) {
1275 		f2fs_down_write(&F2FS_I(new_dir)->i_sem);
1276 		f2fs_i_links_write(new_dir, new_nlink > 0);
1277 		f2fs_up_write(&F2FS_I(new_dir)->i_sem);
1278 	}
1279 	f2fs_mark_inode_dirty_sync(new_dir, false);
1280 
1281 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1282 		f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1283 		f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1284 	}
1285 
1286 	f2fs_unlock_op(sbi);
1287 
1288 	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1289 		f2fs_sync_fs(sbi->sb, 1);
1290 
1291 	f2fs_update_time(sbi, REQ_TIME);
1292 	return 0;
1293 out_new_dir:
1294 	if (new_dir_entry) {
1295 		f2fs_put_page(new_dir_page, 0);
1296 	}
1297 out_old_dir:
1298 	if (old_dir_entry) {
1299 		f2fs_put_page(old_dir_page, 0);
1300 	}
1301 out_new:
1302 	f2fs_put_page(new_page, 0);
1303 out_old:
1304 	f2fs_put_page(old_page, 0);
1305 out:
1306 	return err;
1307 }
1308 
1309 static int f2fs_rename2(struct mnt_idmap *idmap,
1310 			struct inode *old_dir, struct dentry *old_dentry,
1311 			struct inode *new_dir, struct dentry *new_dentry,
1312 			unsigned int flags)
1313 {
1314 	int err;
1315 
1316 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1317 		return -EINVAL;
1318 
1319 	trace_f2fs_rename_start(old_dir, old_dentry, new_dir, new_dentry,
1320 								flags);
1321 
1322 	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1323 				     flags);
1324 	if (err)
1325 		return err;
1326 
1327 	if (flags & RENAME_EXCHANGE)
1328 		err = f2fs_cross_rename(old_dir, old_dentry,
1329 					new_dir, new_dentry);
1330 	else
1331 	/*
1332 	 * VFS has already handled the new dentry existence case,
1333 	 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1334 	 */
1335 		err = f2fs_rename(idmap, old_dir, old_dentry,
1336 					new_dir, new_dentry, flags);
1337 
1338 	trace_f2fs_rename_end(old_dentry, new_dentry, flags, err);
1339 	return err;
1340 }
1341 
1342 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1343 					   struct inode *inode,
1344 					   struct delayed_call *done)
1345 {
1346 	struct page *page;
1347 	const char *target;
1348 
1349 	if (!dentry)
1350 		return ERR_PTR(-ECHILD);
1351 
1352 	page = read_mapping_page(inode->i_mapping, 0, NULL);
1353 	if (IS_ERR(page))
1354 		return ERR_CAST(page);
1355 
1356 	target = fscrypt_get_symlink(inode, page_address(page),
1357 				     inode->i_sb->s_blocksize, done);
1358 	put_page(page);
1359 	return target;
1360 }
1361 
1362 static int f2fs_encrypted_symlink_getattr(struct mnt_idmap *idmap,
1363 					  const struct path *path,
1364 					  struct kstat *stat, u32 request_mask,
1365 					  unsigned int query_flags)
1366 {
1367 	f2fs_getattr(idmap, path, stat, request_mask, query_flags);
1368 
1369 	return fscrypt_symlink_getattr(path, stat);
1370 }
1371 
1372 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1373 	.get_link	= f2fs_encrypted_get_link,
1374 	.getattr	= f2fs_encrypted_symlink_getattr,
1375 	.setattr	= f2fs_setattr,
1376 	.listxattr	= f2fs_listxattr,
1377 };
1378 
1379 const struct inode_operations f2fs_dir_inode_operations = {
1380 	.create		= f2fs_create,
1381 	.lookup		= f2fs_lookup,
1382 	.link		= f2fs_link,
1383 	.unlink		= f2fs_unlink,
1384 	.symlink	= f2fs_symlink,
1385 	.mkdir		= f2fs_mkdir,
1386 	.rmdir		= f2fs_rmdir,
1387 	.mknod		= f2fs_mknod,
1388 	.rename		= f2fs_rename2,
1389 	.tmpfile	= f2fs_tmpfile,
1390 	.getattr	= f2fs_getattr,
1391 	.setattr	= f2fs_setattr,
1392 	.get_inode_acl	= f2fs_get_acl,
1393 	.set_acl	= f2fs_set_acl,
1394 	.listxattr	= f2fs_listxattr,
1395 	.fiemap		= f2fs_fiemap,
1396 	.fileattr_get	= f2fs_fileattr_get,
1397 	.fileattr_set	= f2fs_fileattr_set,
1398 };
1399 
1400 const struct inode_operations f2fs_symlink_inode_operations = {
1401 	.get_link	= f2fs_get_link,
1402 	.getattr	= f2fs_getattr,
1403 	.setattr	= f2fs_setattr,
1404 	.listxattr	= f2fs_listxattr,
1405 };
1406 
1407 const struct inode_operations f2fs_special_inode_operations = {
1408 	.getattr	= f2fs_getattr,
1409 	.setattr	= f2fs_setattr,
1410 	.get_inode_acl	= f2fs_get_acl,
1411 	.set_acl	= f2fs_set_acl,
1412 	.listxattr	= f2fs_listxattr,
1413 };
1414