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