io_uring: don't use 'fd' for openat/openat2/statx
[linux/fpc-iii.git] / fs / f2fs / namei.c
blob2aa035422c0fac63a51e60d1a5dce93a37482dc6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/namei.c
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>
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>
25 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
27 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
28 nid_t ino;
29 struct inode *inode;
30 bool nid_free = false;
31 int xattr_size = 0;
32 int err;
34 inode = new_inode(dir->i_sb);
35 if (!inode)
36 return ERR_PTR(-ENOMEM);
38 f2fs_lock_op(sbi);
39 if (!f2fs_alloc_nid(sbi, &ino)) {
40 f2fs_unlock_op(sbi);
41 err = -ENOSPC;
42 goto fail;
44 f2fs_unlock_op(sbi);
46 nid_free = true;
48 inode_init_owner(inode, dir, mode);
50 inode->i_ino = ino;
51 inode->i_blocks = 0;
52 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
53 F2FS_I(inode)->i_crtime = inode->i_mtime;
54 inode->i_generation = prandom_u32();
56 if (S_ISDIR(inode->i_mode))
57 F2FS_I(inode)->i_current_depth = 1;
59 err = insert_inode_locked(inode);
60 if (err) {
61 err = -EINVAL;
62 goto fail;
65 if (f2fs_sb_has_project_quota(sbi) &&
66 (F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
67 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
68 else
69 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
70 F2FS_DEF_PROJID);
72 err = dquot_initialize(inode);
73 if (err)
74 goto fail_drop;
76 set_inode_flag(inode, FI_NEW_INODE);
78 /* If the directory encrypted, then we should encrypt the inode. */
79 if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
80 f2fs_may_encrypt(inode))
81 f2fs_set_encrypted_inode(inode);
83 if (f2fs_sb_has_extra_attr(sbi)) {
84 set_inode_flag(inode, FI_EXTRA_ATTR);
85 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
88 if (test_opt(sbi, INLINE_XATTR))
89 set_inode_flag(inode, FI_INLINE_XATTR);
91 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
92 set_inode_flag(inode, FI_INLINE_DATA);
93 if (f2fs_may_inline_dentry(inode))
94 set_inode_flag(inode, FI_INLINE_DENTRY);
96 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
97 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
98 if (f2fs_has_inline_xattr(inode))
99 xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
100 /* Otherwise, will be 0 */
101 } else if (f2fs_has_inline_xattr(inode) ||
102 f2fs_has_inline_dentry(inode)) {
103 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
105 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
107 f2fs_init_extent_tree(inode, NULL);
109 stat_inc_inline_xattr(inode);
110 stat_inc_inline_inode(inode);
111 stat_inc_inline_dir(inode);
113 F2FS_I(inode)->i_flags =
114 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
116 if (S_ISDIR(inode->i_mode))
117 F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
119 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
120 set_inode_flag(inode, FI_PROJ_INHERIT);
122 if (f2fs_sb_has_compression(sbi)) {
123 /* Inherit the compression flag in directory */
124 if ((F2FS_I(dir)->i_flags & F2FS_COMPR_FL) &&
125 f2fs_may_compress(inode))
126 set_compress_context(inode);
129 f2fs_set_inode_flags(inode);
131 trace_f2fs_new_inode(inode, 0);
132 return inode;
134 fail:
135 trace_f2fs_new_inode(inode, err);
136 make_bad_inode(inode);
137 if (nid_free)
138 set_inode_flag(inode, FI_FREE_NID);
139 iput(inode);
140 return ERR_PTR(err);
141 fail_drop:
142 trace_f2fs_new_inode(inode, err);
143 dquot_drop(inode);
144 inode->i_flags |= S_NOQUOTA;
145 if (nid_free)
146 set_inode_flag(inode, FI_FREE_NID);
147 clear_nlink(inode);
148 unlock_new_inode(inode);
149 iput(inode);
150 return ERR_PTR(err);
153 static inline int is_extension_exist(const unsigned char *s, const char *sub)
155 size_t slen = strlen(s);
156 size_t sublen = strlen(sub);
157 int i;
159 if (sublen == 1 && *sub == '*')
160 return 1;
163 * filename format of multimedia file should be defined as:
164 * "filename + '.' + extension + (optional: '.' + temp extension)".
166 if (slen < sublen + 2)
167 return 0;
169 for (i = 1; i < slen - sublen; i++) {
170 if (s[i] != '.')
171 continue;
172 if (!strncasecmp(s + i + 1, sub, sublen))
173 return 1;
176 return 0;
180 * Set multimedia files as cold files for hot/cold data separation
182 static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
183 const unsigned char *name)
185 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
186 int i, cold_count, hot_count;
188 down_read(&sbi->sb_lock);
190 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
191 hot_count = sbi->raw_super->hot_ext_count;
193 for (i = 0; i < cold_count + hot_count; i++) {
194 if (is_extension_exist(name, extlist[i]))
195 break;
198 up_read(&sbi->sb_lock);
200 if (i == cold_count + hot_count)
201 return;
203 if (i < cold_count)
204 file_set_cold(inode);
205 else
206 file_set_hot(inode);
209 int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
210 bool hot, bool set)
212 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
213 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
214 int hot_count = sbi->raw_super->hot_ext_count;
215 int total_count = cold_count + hot_count;
216 int start, count;
217 int i;
219 if (set) {
220 if (total_count == F2FS_MAX_EXTENSION)
221 return -EINVAL;
222 } else {
223 if (!hot && !cold_count)
224 return -EINVAL;
225 if (hot && !hot_count)
226 return -EINVAL;
229 if (hot) {
230 start = cold_count;
231 count = total_count;
232 } else {
233 start = 0;
234 count = cold_count;
237 for (i = start; i < count; i++) {
238 if (strcmp(name, extlist[i]))
239 continue;
241 if (set)
242 return -EINVAL;
244 memcpy(extlist[i], extlist[i + 1],
245 F2FS_EXTENSION_LEN * (total_count - i - 1));
246 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
247 if (hot)
248 sbi->raw_super->hot_ext_count = hot_count - 1;
249 else
250 sbi->raw_super->extension_count =
251 cpu_to_le32(cold_count - 1);
252 return 0;
255 if (!set)
256 return -EINVAL;
258 if (hot) {
259 memcpy(extlist[count], name, strlen(name));
260 sbi->raw_super->hot_ext_count = hot_count + 1;
261 } else {
262 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
264 memcpy(buf, &extlist[cold_count],
265 F2FS_EXTENSION_LEN * hot_count);
266 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
267 memcpy(extlist[cold_count], name, strlen(name));
268 memcpy(&extlist[cold_count + 1], buf,
269 F2FS_EXTENSION_LEN * hot_count);
270 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
272 return 0;
275 static void set_compress_inode(struct f2fs_sb_info *sbi, struct inode *inode,
276 const unsigned char *name)
278 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
279 unsigned char (*ext)[F2FS_EXTENSION_LEN];
280 unsigned int ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
281 int i, cold_count, hot_count;
283 if (!f2fs_sb_has_compression(sbi) ||
284 is_inode_flag_set(inode, FI_COMPRESSED_FILE) ||
285 F2FS_I(inode)->i_flags & F2FS_NOCOMP_FL ||
286 !f2fs_may_compress(inode))
287 return;
289 down_read(&sbi->sb_lock);
291 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
292 hot_count = sbi->raw_super->hot_ext_count;
294 for (i = cold_count; i < cold_count + hot_count; i++) {
295 if (is_extension_exist(name, extlist[i])) {
296 up_read(&sbi->sb_lock);
297 return;
301 up_read(&sbi->sb_lock);
303 ext = F2FS_OPTION(sbi).extensions;
305 for (i = 0; i < ext_cnt; i++) {
306 if (!is_extension_exist(name, ext[i]))
307 continue;
309 set_compress_context(inode);
310 return;
314 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
315 bool excl)
317 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
318 struct inode *inode;
319 nid_t ino = 0;
320 int err;
322 if (unlikely(f2fs_cp_error(sbi)))
323 return -EIO;
324 if (!f2fs_is_checkpoint_ready(sbi))
325 return -ENOSPC;
327 err = dquot_initialize(dir);
328 if (err)
329 return err;
331 inode = f2fs_new_inode(dir, mode);
332 if (IS_ERR(inode))
333 return PTR_ERR(inode);
335 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
336 set_file_temperature(sbi, inode, dentry->d_name.name);
338 set_compress_inode(sbi, inode, dentry->d_name.name);
340 inode->i_op = &f2fs_file_inode_operations;
341 inode->i_fop = &f2fs_file_operations;
342 inode->i_mapping->a_ops = &f2fs_dblock_aops;
343 ino = inode->i_ino;
345 f2fs_lock_op(sbi);
346 err = f2fs_add_link(dentry, inode);
347 if (err)
348 goto out;
349 f2fs_unlock_op(sbi);
351 f2fs_alloc_nid_done(sbi, ino);
353 d_instantiate_new(dentry, inode);
355 if (IS_DIRSYNC(dir))
356 f2fs_sync_fs(sbi->sb, 1);
358 f2fs_balance_fs(sbi, true);
359 return 0;
360 out:
361 f2fs_handle_failed_inode(inode);
362 return err;
365 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
366 struct dentry *dentry)
368 struct inode *inode = d_inode(old_dentry);
369 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
370 int err;
372 if (unlikely(f2fs_cp_error(sbi)))
373 return -EIO;
374 if (!f2fs_is_checkpoint_ready(sbi))
375 return -ENOSPC;
377 err = fscrypt_prepare_link(old_dentry, dir, dentry);
378 if (err)
379 return err;
381 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
382 (!projid_eq(F2FS_I(dir)->i_projid,
383 F2FS_I(old_dentry->d_inode)->i_projid)))
384 return -EXDEV;
386 err = dquot_initialize(dir);
387 if (err)
388 return err;
390 f2fs_balance_fs(sbi, true);
392 inode->i_ctime = current_time(inode);
393 ihold(inode);
395 set_inode_flag(inode, FI_INC_LINK);
396 f2fs_lock_op(sbi);
397 err = f2fs_add_link(dentry, inode);
398 if (err)
399 goto out;
400 f2fs_unlock_op(sbi);
402 d_instantiate(dentry, inode);
404 if (IS_DIRSYNC(dir))
405 f2fs_sync_fs(sbi->sb, 1);
406 return 0;
407 out:
408 clear_inode_flag(inode, FI_INC_LINK);
409 iput(inode);
410 f2fs_unlock_op(sbi);
411 return err;
414 struct dentry *f2fs_get_parent(struct dentry *child)
416 struct qstr dotdot = QSTR_INIT("..", 2);
417 struct page *page;
418 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
419 if (!ino) {
420 if (IS_ERR(page))
421 return ERR_CAST(page);
422 return ERR_PTR(-ENOENT);
424 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
427 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
429 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
430 struct qstr dot = QSTR_INIT(".", 1);
431 struct qstr dotdot = QSTR_INIT("..", 2);
432 struct f2fs_dir_entry *de;
433 struct page *page;
434 int err = 0;
436 if (f2fs_readonly(sbi->sb)) {
437 f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
438 dir->i_ino, pino);
439 return 0;
442 err = dquot_initialize(dir);
443 if (err)
444 return err;
446 f2fs_balance_fs(sbi, true);
448 f2fs_lock_op(sbi);
450 de = f2fs_find_entry(dir, &dot, &page);
451 if (de) {
452 f2fs_put_page(page, 0);
453 } else if (IS_ERR(page)) {
454 err = PTR_ERR(page);
455 goto out;
456 } else {
457 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
458 if (err)
459 goto out;
462 de = f2fs_find_entry(dir, &dotdot, &page);
463 if (de)
464 f2fs_put_page(page, 0);
465 else if (IS_ERR(page))
466 err = PTR_ERR(page);
467 else
468 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
469 out:
470 if (!err)
471 clear_inode_flag(dir, FI_INLINE_DOTS);
473 f2fs_unlock_op(sbi);
474 return err;
477 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
478 unsigned int flags)
480 struct inode *inode = NULL;
481 struct f2fs_dir_entry *de;
482 struct page *page;
483 struct dentry *new;
484 nid_t ino = -1;
485 int err = 0;
486 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
487 struct fscrypt_name fname;
489 trace_f2fs_lookup_start(dir, dentry, flags);
491 if (dentry->d_name.len > F2FS_NAME_LEN) {
492 err = -ENAMETOOLONG;
493 goto out;
496 err = fscrypt_prepare_lookup(dir, dentry, &fname);
497 if (err == -ENOENT)
498 goto out_splice;
499 if (err)
500 goto out;
501 de = __f2fs_find_entry(dir, &fname, &page);
502 fscrypt_free_filename(&fname);
504 if (!de) {
505 if (IS_ERR(page)) {
506 err = PTR_ERR(page);
507 goto out;
509 goto out_splice;
512 ino = le32_to_cpu(de->ino);
513 f2fs_put_page(page, 0);
515 inode = f2fs_iget(dir->i_sb, ino);
516 if (IS_ERR(inode)) {
517 err = PTR_ERR(inode);
518 goto out;
521 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
522 err = __recover_dot_dentries(dir, root_ino);
523 if (err)
524 goto out_iput;
527 if (f2fs_has_inline_dots(inode)) {
528 err = __recover_dot_dentries(inode, dir->i_ino);
529 if (err)
530 goto out_iput;
532 if (IS_ENCRYPTED(dir) &&
533 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
534 !fscrypt_has_permitted_context(dir, inode)) {
535 f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
536 dir->i_ino, inode->i_ino);
537 err = -EPERM;
538 goto out_iput;
540 out_splice:
541 #ifdef CONFIG_UNICODE
542 if (!inode && IS_CASEFOLDED(dir)) {
543 /* Eventually we want to call d_add_ci(dentry, NULL)
544 * for negative dentries in the encoding case as
545 * well. For now, prevent the negative dentry
546 * from being cached.
548 trace_f2fs_lookup_end(dir, dentry, ino, err);
549 return NULL;
551 #endif
552 new = d_splice_alias(inode, dentry);
553 err = PTR_ERR_OR_ZERO(new);
554 trace_f2fs_lookup_end(dir, dentry, ino, err);
555 return new;
556 out_iput:
557 iput(inode);
558 out:
559 trace_f2fs_lookup_end(dir, dentry, ino, err);
560 return ERR_PTR(err);
563 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
565 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
566 struct inode *inode = d_inode(dentry);
567 struct f2fs_dir_entry *de;
568 struct page *page;
569 int err = -ENOENT;
571 trace_f2fs_unlink_enter(dir, dentry);
573 if (unlikely(f2fs_cp_error(sbi)))
574 return -EIO;
576 err = dquot_initialize(dir);
577 if (err)
578 return err;
579 err = dquot_initialize(inode);
580 if (err)
581 return err;
583 de = f2fs_find_entry(dir, &dentry->d_name, &page);
584 if (!de) {
585 if (IS_ERR(page))
586 err = PTR_ERR(page);
587 goto fail;
590 f2fs_balance_fs(sbi, true);
592 f2fs_lock_op(sbi);
593 err = f2fs_acquire_orphan_inode(sbi);
594 if (err) {
595 f2fs_unlock_op(sbi);
596 f2fs_put_page(page, 0);
597 goto fail;
599 f2fs_delete_entry(de, page, dir, inode);
600 #ifdef CONFIG_UNICODE
601 /* VFS negative dentries are incompatible with Encoding and
602 * Case-insensitiveness. Eventually we'll want avoid
603 * invalidating the dentries here, alongside with returning the
604 * negative dentries at f2fs_lookup(), when it is better
605 * supported by the VFS for the CI case.
607 if (IS_CASEFOLDED(dir))
608 d_invalidate(dentry);
609 #endif
610 f2fs_unlock_op(sbi);
612 if (IS_DIRSYNC(dir))
613 f2fs_sync_fs(sbi->sb, 1);
614 fail:
615 trace_f2fs_unlink_exit(inode, err);
616 return err;
619 static const char *f2fs_get_link(struct dentry *dentry,
620 struct inode *inode,
621 struct delayed_call *done)
623 const char *link = page_get_link(dentry, inode, done);
624 if (!IS_ERR(link) && !*link) {
625 /* this is broken symlink case */
626 do_delayed_call(done);
627 clear_delayed_call(done);
628 link = ERR_PTR(-ENOENT);
630 return link;
633 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
634 const char *symname)
636 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
637 struct inode *inode;
638 size_t len = strlen(symname);
639 struct fscrypt_str disk_link;
640 int err;
642 if (unlikely(f2fs_cp_error(sbi)))
643 return -EIO;
644 if (!f2fs_is_checkpoint_ready(sbi))
645 return -ENOSPC;
647 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
648 &disk_link);
649 if (err)
650 return err;
652 err = dquot_initialize(dir);
653 if (err)
654 return err;
656 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
657 if (IS_ERR(inode))
658 return PTR_ERR(inode);
660 if (IS_ENCRYPTED(inode))
661 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
662 else
663 inode->i_op = &f2fs_symlink_inode_operations;
664 inode_nohighmem(inode);
665 inode->i_mapping->a_ops = &f2fs_dblock_aops;
667 f2fs_lock_op(sbi);
668 err = f2fs_add_link(dentry, inode);
669 if (err)
670 goto out_f2fs_handle_failed_inode;
671 f2fs_unlock_op(sbi);
672 f2fs_alloc_nid_done(sbi, inode->i_ino);
674 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
675 if (err)
676 goto err_out;
678 err = page_symlink(inode, disk_link.name, disk_link.len);
680 err_out:
681 d_instantiate_new(dentry, inode);
684 * Let's flush symlink data in order to avoid broken symlink as much as
685 * possible. Nevertheless, fsyncing is the best way, but there is no
686 * way to get a file descriptor in order to flush that.
688 * Note that, it needs to do dir->fsync to make this recoverable.
689 * If the symlink path is stored into inline_data, there is no
690 * performance regression.
692 if (!err) {
693 filemap_write_and_wait_range(inode->i_mapping, 0,
694 disk_link.len - 1);
696 if (IS_DIRSYNC(dir))
697 f2fs_sync_fs(sbi->sb, 1);
698 } else {
699 f2fs_unlink(dir, dentry);
702 f2fs_balance_fs(sbi, true);
703 goto out_free_encrypted_link;
705 out_f2fs_handle_failed_inode:
706 f2fs_handle_failed_inode(inode);
707 out_free_encrypted_link:
708 if (disk_link.name != (unsigned char *)symname)
709 kvfree(disk_link.name);
710 return err;
713 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
715 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
716 struct inode *inode;
717 int err;
719 if (unlikely(f2fs_cp_error(sbi)))
720 return -EIO;
722 err = dquot_initialize(dir);
723 if (err)
724 return err;
726 inode = f2fs_new_inode(dir, S_IFDIR | mode);
727 if (IS_ERR(inode))
728 return PTR_ERR(inode);
730 inode->i_op = &f2fs_dir_inode_operations;
731 inode->i_fop = &f2fs_dir_operations;
732 inode->i_mapping->a_ops = &f2fs_dblock_aops;
733 inode_nohighmem(inode);
735 set_inode_flag(inode, FI_INC_LINK);
736 f2fs_lock_op(sbi);
737 err = f2fs_add_link(dentry, inode);
738 if (err)
739 goto out_fail;
740 f2fs_unlock_op(sbi);
742 f2fs_alloc_nid_done(sbi, inode->i_ino);
744 d_instantiate_new(dentry, inode);
746 if (IS_DIRSYNC(dir))
747 f2fs_sync_fs(sbi->sb, 1);
749 f2fs_balance_fs(sbi, true);
750 return 0;
752 out_fail:
753 clear_inode_flag(inode, FI_INC_LINK);
754 f2fs_handle_failed_inode(inode);
755 return err;
758 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
760 struct inode *inode = d_inode(dentry);
761 if (f2fs_empty_dir(inode))
762 return f2fs_unlink(dir, dentry);
763 return -ENOTEMPTY;
766 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
767 umode_t mode, dev_t rdev)
769 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
770 struct inode *inode;
771 int err = 0;
773 if (unlikely(f2fs_cp_error(sbi)))
774 return -EIO;
775 if (!f2fs_is_checkpoint_ready(sbi))
776 return -ENOSPC;
778 err = dquot_initialize(dir);
779 if (err)
780 return err;
782 inode = f2fs_new_inode(dir, mode);
783 if (IS_ERR(inode))
784 return PTR_ERR(inode);
786 init_special_inode(inode, inode->i_mode, rdev);
787 inode->i_op = &f2fs_special_inode_operations;
789 f2fs_lock_op(sbi);
790 err = f2fs_add_link(dentry, inode);
791 if (err)
792 goto out;
793 f2fs_unlock_op(sbi);
795 f2fs_alloc_nid_done(sbi, inode->i_ino);
797 d_instantiate_new(dentry, inode);
799 if (IS_DIRSYNC(dir))
800 f2fs_sync_fs(sbi->sb, 1);
802 f2fs_balance_fs(sbi, true);
803 return 0;
804 out:
805 f2fs_handle_failed_inode(inode);
806 return err;
809 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
810 umode_t mode, struct inode **whiteout)
812 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
813 struct inode *inode;
814 int err;
816 err = dquot_initialize(dir);
817 if (err)
818 return err;
820 inode = f2fs_new_inode(dir, mode);
821 if (IS_ERR(inode))
822 return PTR_ERR(inode);
824 if (whiteout) {
825 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
826 inode->i_op = &f2fs_special_inode_operations;
827 } else {
828 inode->i_op = &f2fs_file_inode_operations;
829 inode->i_fop = &f2fs_file_operations;
830 inode->i_mapping->a_ops = &f2fs_dblock_aops;
833 f2fs_lock_op(sbi);
834 err = f2fs_acquire_orphan_inode(sbi);
835 if (err)
836 goto out;
838 err = f2fs_do_tmpfile(inode, dir);
839 if (err)
840 goto release_out;
843 * add this non-linked tmpfile to orphan list, in this way we could
844 * remove all unused data of tmpfile after abnormal power-off.
846 f2fs_add_orphan_inode(inode);
847 f2fs_alloc_nid_done(sbi, inode->i_ino);
849 if (whiteout) {
850 f2fs_i_links_write(inode, false);
851 inode->i_state |= I_LINKABLE;
852 *whiteout = inode;
853 } else {
854 d_tmpfile(dentry, inode);
856 /* link_count was changed by d_tmpfile as well. */
857 f2fs_unlock_op(sbi);
858 unlock_new_inode(inode);
860 f2fs_balance_fs(sbi, true);
861 return 0;
863 release_out:
864 f2fs_release_orphan_inode(sbi);
865 out:
866 f2fs_handle_failed_inode(inode);
867 return err;
870 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
872 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
874 if (unlikely(f2fs_cp_error(sbi)))
875 return -EIO;
876 if (!f2fs_is_checkpoint_ready(sbi))
877 return -ENOSPC;
879 if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
880 int err = fscrypt_get_encryption_info(dir);
881 if (err)
882 return err;
885 return __f2fs_tmpfile(dir, dentry, mode, NULL);
888 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
890 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
891 return -EIO;
893 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
896 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
897 struct inode *new_dir, struct dentry *new_dentry,
898 unsigned int flags)
900 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
901 struct inode *old_inode = d_inode(old_dentry);
902 struct inode *new_inode = d_inode(new_dentry);
903 struct inode *whiteout = NULL;
904 struct page *old_dir_page = NULL;
905 struct page *old_page, *new_page = NULL;
906 struct f2fs_dir_entry *old_dir_entry = NULL;
907 struct f2fs_dir_entry *old_entry;
908 struct f2fs_dir_entry *new_entry;
909 int err;
911 if (unlikely(f2fs_cp_error(sbi)))
912 return -EIO;
913 if (!f2fs_is_checkpoint_ready(sbi))
914 return -ENOSPC;
916 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
917 (!projid_eq(F2FS_I(new_dir)->i_projid,
918 F2FS_I(old_dentry->d_inode)->i_projid)))
919 return -EXDEV;
922 * If new_inode is null, the below renaming flow will
923 * add a link in old_dir which can conver inline_dir.
924 * After then, if we failed to get the entry due to other
925 * reasons like ENOMEM, we had to remove the new entry.
926 * Instead of adding such the error handling routine, let's
927 * simply convert first here.
929 if (old_dir == new_dir && !new_inode) {
930 err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
931 if (err)
932 return err;
935 if (flags & RENAME_WHITEOUT) {
936 err = f2fs_create_whiteout(old_dir, &whiteout);
937 if (err)
938 return err;
941 err = dquot_initialize(old_dir);
942 if (err)
943 goto out;
945 err = dquot_initialize(new_dir);
946 if (err)
947 goto out;
949 if (new_inode) {
950 err = dquot_initialize(new_inode);
951 if (err)
952 goto out;
955 err = -ENOENT;
956 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
957 if (!old_entry) {
958 if (IS_ERR(old_page))
959 err = PTR_ERR(old_page);
960 goto out;
963 if (S_ISDIR(old_inode->i_mode)) {
964 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
965 if (!old_dir_entry) {
966 if (IS_ERR(old_dir_page))
967 err = PTR_ERR(old_dir_page);
968 goto out_old;
972 if (new_inode) {
974 err = -ENOTEMPTY;
975 if (old_dir_entry && !f2fs_empty_dir(new_inode))
976 goto out_dir;
978 err = -ENOENT;
979 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
980 &new_page);
981 if (!new_entry) {
982 if (IS_ERR(new_page))
983 err = PTR_ERR(new_page);
984 goto out_dir;
987 f2fs_balance_fs(sbi, true);
989 f2fs_lock_op(sbi);
991 err = f2fs_acquire_orphan_inode(sbi);
992 if (err)
993 goto put_out_dir;
995 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
996 new_page = NULL;
998 new_inode->i_ctime = current_time(new_inode);
999 down_write(&F2FS_I(new_inode)->i_sem);
1000 if (old_dir_entry)
1001 f2fs_i_links_write(new_inode, false);
1002 f2fs_i_links_write(new_inode, false);
1003 up_write(&F2FS_I(new_inode)->i_sem);
1005 if (!new_inode->i_nlink)
1006 f2fs_add_orphan_inode(new_inode);
1007 else
1008 f2fs_release_orphan_inode(sbi);
1009 } else {
1010 f2fs_balance_fs(sbi, true);
1012 f2fs_lock_op(sbi);
1014 err = f2fs_add_link(new_dentry, old_inode);
1015 if (err) {
1016 f2fs_unlock_op(sbi);
1017 goto out_dir;
1020 if (old_dir_entry)
1021 f2fs_i_links_write(new_dir, true);
1024 down_write(&F2FS_I(old_inode)->i_sem);
1025 if (!old_dir_entry || whiteout)
1026 file_lost_pino(old_inode);
1027 else
1028 /* adjust dir's i_pino to pass fsck check */
1029 f2fs_i_pino_write(old_inode, new_dir->i_ino);
1030 up_write(&F2FS_I(old_inode)->i_sem);
1032 old_inode->i_ctime = current_time(old_inode);
1033 f2fs_mark_inode_dirty_sync(old_inode, false);
1035 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
1036 old_page = NULL;
1038 if (whiteout) {
1039 set_inode_flag(whiteout, FI_INC_LINK);
1040 err = f2fs_add_link(old_dentry, whiteout);
1041 if (err)
1042 goto put_out_dir;
1043 whiteout->i_state &= ~I_LINKABLE;
1044 iput(whiteout);
1047 if (old_dir_entry) {
1048 if (old_dir != new_dir && !whiteout)
1049 f2fs_set_link(old_inode, old_dir_entry,
1050 old_dir_page, new_dir);
1051 else
1052 f2fs_put_page(old_dir_page, 0);
1053 f2fs_i_links_write(old_dir, false);
1055 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1056 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1057 if (S_ISDIR(old_inode->i_mode))
1058 f2fs_add_ino_entry(sbi, old_inode->i_ino,
1059 TRANS_DIR_INO);
1062 f2fs_unlock_op(sbi);
1064 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1065 f2fs_sync_fs(sbi->sb, 1);
1067 f2fs_update_time(sbi, REQ_TIME);
1068 return 0;
1070 put_out_dir:
1071 f2fs_unlock_op(sbi);
1072 f2fs_put_page(new_page, 0);
1073 out_dir:
1074 if (old_dir_entry)
1075 f2fs_put_page(old_dir_page, 0);
1076 out_old:
1077 f2fs_put_page(old_page, 0);
1078 out:
1079 if (whiteout)
1080 iput(whiteout);
1081 return err;
1084 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1085 struct inode *new_dir, struct dentry *new_dentry)
1087 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1088 struct inode *old_inode = d_inode(old_dentry);
1089 struct inode *new_inode = d_inode(new_dentry);
1090 struct page *old_dir_page, *new_dir_page;
1091 struct page *old_page, *new_page;
1092 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1093 struct f2fs_dir_entry *old_entry, *new_entry;
1094 int old_nlink = 0, new_nlink = 0;
1095 int err;
1097 if (unlikely(f2fs_cp_error(sbi)))
1098 return -EIO;
1099 if (!f2fs_is_checkpoint_ready(sbi))
1100 return -ENOSPC;
1102 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1103 !projid_eq(F2FS_I(new_dir)->i_projid,
1104 F2FS_I(old_dentry->d_inode)->i_projid)) ||
1105 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1106 !projid_eq(F2FS_I(old_dir)->i_projid,
1107 F2FS_I(new_dentry->d_inode)->i_projid)))
1108 return -EXDEV;
1110 err = dquot_initialize(old_dir);
1111 if (err)
1112 goto out;
1114 err = dquot_initialize(new_dir);
1115 if (err)
1116 goto out;
1118 err = -ENOENT;
1119 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1120 if (!old_entry) {
1121 if (IS_ERR(old_page))
1122 err = PTR_ERR(old_page);
1123 goto out;
1126 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1127 if (!new_entry) {
1128 if (IS_ERR(new_page))
1129 err = PTR_ERR(new_page);
1130 goto out_old;
1133 /* prepare for updating ".." directory entry info later */
1134 if (old_dir != new_dir) {
1135 if (S_ISDIR(old_inode->i_mode)) {
1136 old_dir_entry = f2fs_parent_dir(old_inode,
1137 &old_dir_page);
1138 if (!old_dir_entry) {
1139 if (IS_ERR(old_dir_page))
1140 err = PTR_ERR(old_dir_page);
1141 goto out_new;
1145 if (S_ISDIR(new_inode->i_mode)) {
1146 new_dir_entry = f2fs_parent_dir(new_inode,
1147 &new_dir_page);
1148 if (!new_dir_entry) {
1149 if (IS_ERR(new_dir_page))
1150 err = PTR_ERR(new_dir_page);
1151 goto out_old_dir;
1157 * If cross rename between file and directory those are not
1158 * in the same directory, we will inc nlink of file's parent
1159 * later, so we should check upper boundary of its nlink.
1161 if ((!old_dir_entry || !new_dir_entry) &&
1162 old_dir_entry != new_dir_entry) {
1163 old_nlink = old_dir_entry ? -1 : 1;
1164 new_nlink = -old_nlink;
1165 err = -EMLINK;
1166 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1167 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1168 goto out_new_dir;
1171 f2fs_balance_fs(sbi, true);
1173 f2fs_lock_op(sbi);
1175 /* update ".." directory entry info of old dentry */
1176 if (old_dir_entry)
1177 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1179 /* update ".." directory entry info of new dentry */
1180 if (new_dir_entry)
1181 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1183 /* update directory entry info of old dir inode */
1184 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1186 down_write(&F2FS_I(old_inode)->i_sem);
1187 if (!old_dir_entry)
1188 file_lost_pino(old_inode);
1189 else
1190 /* adjust dir's i_pino to pass fsck check */
1191 f2fs_i_pino_write(old_inode, new_dir->i_ino);
1192 up_write(&F2FS_I(old_inode)->i_sem);
1194 old_dir->i_ctime = current_time(old_dir);
1195 if (old_nlink) {
1196 down_write(&F2FS_I(old_dir)->i_sem);
1197 f2fs_i_links_write(old_dir, old_nlink > 0);
1198 up_write(&F2FS_I(old_dir)->i_sem);
1200 f2fs_mark_inode_dirty_sync(old_dir, false);
1202 /* update directory entry info of new dir inode */
1203 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1205 down_write(&F2FS_I(new_inode)->i_sem);
1206 if (!new_dir_entry)
1207 file_lost_pino(new_inode);
1208 else
1209 /* adjust dir's i_pino to pass fsck check */
1210 f2fs_i_pino_write(new_inode, old_dir->i_ino);
1211 up_write(&F2FS_I(new_inode)->i_sem);
1213 new_dir->i_ctime = current_time(new_dir);
1214 if (new_nlink) {
1215 down_write(&F2FS_I(new_dir)->i_sem);
1216 f2fs_i_links_write(new_dir, new_nlink > 0);
1217 up_write(&F2FS_I(new_dir)->i_sem);
1219 f2fs_mark_inode_dirty_sync(new_dir, false);
1221 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1222 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1223 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1226 f2fs_unlock_op(sbi);
1228 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1229 f2fs_sync_fs(sbi->sb, 1);
1231 f2fs_update_time(sbi, REQ_TIME);
1232 return 0;
1233 out_new_dir:
1234 if (new_dir_entry) {
1235 f2fs_put_page(new_dir_page, 0);
1237 out_old_dir:
1238 if (old_dir_entry) {
1239 f2fs_put_page(old_dir_page, 0);
1241 out_new:
1242 f2fs_put_page(new_page, 0);
1243 out_old:
1244 f2fs_put_page(old_page, 0);
1245 out:
1246 return err;
1249 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1250 struct inode *new_dir, struct dentry *new_dentry,
1251 unsigned int flags)
1253 int err;
1255 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1256 return -EINVAL;
1258 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1259 flags);
1260 if (err)
1261 return err;
1263 if (flags & RENAME_EXCHANGE) {
1264 return f2fs_cross_rename(old_dir, old_dentry,
1265 new_dir, new_dentry);
1268 * VFS has already handled the new dentry existence case,
1269 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1271 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1274 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1275 struct inode *inode,
1276 struct delayed_call *done)
1278 struct page *page;
1279 const char *target;
1281 if (!dentry)
1282 return ERR_PTR(-ECHILD);
1284 page = read_mapping_page(inode->i_mapping, 0, NULL);
1285 if (IS_ERR(page))
1286 return ERR_CAST(page);
1288 target = fscrypt_get_symlink(inode, page_address(page),
1289 inode->i_sb->s_blocksize, done);
1290 put_page(page);
1291 return target;
1294 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1295 .get_link = f2fs_encrypted_get_link,
1296 .getattr = f2fs_getattr,
1297 .setattr = f2fs_setattr,
1298 #ifdef CONFIG_F2FS_FS_XATTR
1299 .listxattr = f2fs_listxattr,
1300 #endif
1303 const struct inode_operations f2fs_dir_inode_operations = {
1304 .create = f2fs_create,
1305 .lookup = f2fs_lookup,
1306 .link = f2fs_link,
1307 .unlink = f2fs_unlink,
1308 .symlink = f2fs_symlink,
1309 .mkdir = f2fs_mkdir,
1310 .rmdir = f2fs_rmdir,
1311 .mknod = f2fs_mknod,
1312 .rename = f2fs_rename2,
1313 .tmpfile = f2fs_tmpfile,
1314 .getattr = f2fs_getattr,
1315 .setattr = f2fs_setattr,
1316 .get_acl = f2fs_get_acl,
1317 .set_acl = f2fs_set_acl,
1318 #ifdef CONFIG_F2FS_FS_XATTR
1319 .listxattr = f2fs_listxattr,
1320 #endif
1321 .fiemap = f2fs_fiemap,
1324 const struct inode_operations f2fs_symlink_inode_operations = {
1325 .get_link = f2fs_get_link,
1326 .getattr = f2fs_getattr,
1327 .setattr = f2fs_setattr,
1328 #ifdef CONFIG_F2FS_FS_XATTR
1329 .listxattr = f2fs_listxattr,
1330 #endif
1333 const struct inode_operations f2fs_special_inode_operations = {
1334 .getattr = f2fs_getattr,
1335 .setattr = f2fs_setattr,
1336 .get_acl = f2fs_get_acl,
1337 .set_acl = f2fs_set_acl,
1338 #ifdef CONFIG_F2FS_FS_XATTR
1339 .listxattr = f2fs_listxattr,
1340 #endif