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