1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
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>
23 #include <trace/events/f2fs.h>
25 static struct inode *f2fs_new_inode(struct user_namespace *mnt_userns,
26 struct inode *dir, umode_t mode)
28 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
31 bool nid_free = false;
36 inode = new_inode(dir->i_sb);
38 return ERR_PTR(-ENOMEM);
40 if (!f2fs_alloc_nid(sbi, &ino)) {
47 inode_init_owner(mnt_userns, inode, dir, mode);
51 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
52 F2FS_I(inode)->i_crtime = inode->i_mtime;
53 inode->i_generation = prandom_u32();
55 if (S_ISDIR(inode->i_mode))
56 F2FS_I(inode)->i_current_depth = 1;
58 err = insert_inode_locked(inode);
64 if (f2fs_sb_has_project_quota(sbi) &&
65 (F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
66 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
68 F2FS_I(inode)->i_projid = make_kprojid(mnt_userns,
71 err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
75 err = f2fs_dquot_initialize(inode);
79 set_inode_flag(inode, FI_NEW_INODE);
82 f2fs_set_encrypted_inode(inode);
84 if (f2fs_sb_has_extra_attr(sbi)) {
85 set_inode_flag(inode, FI_EXTRA_ATTR);
86 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
89 if (test_opt(sbi, INLINE_XATTR))
90 set_inode_flag(inode, FI_INLINE_XATTR);
92 if (f2fs_may_inline_dentry(inode))
93 set_inode_flag(inode, FI_INLINE_DENTRY);
95 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
96 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
97 if (f2fs_has_inline_xattr(inode))
98 xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
99 /* Otherwise, will be 0 */
100 } else if (f2fs_has_inline_xattr(inode) ||
101 f2fs_has_inline_dentry(inode)) {
102 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
104 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
106 f2fs_init_extent_tree(inode, NULL);
108 F2FS_I(inode)->i_flags =
109 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
111 if (S_ISDIR(inode->i_mode))
112 F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
114 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
115 set_inode_flag(inode, FI_PROJ_INHERIT);
117 if (f2fs_sb_has_compression(sbi)) {
118 /* Inherit the compression flag in directory */
119 if ((F2FS_I(dir)->i_flags & F2FS_COMPR_FL) &&
120 f2fs_may_compress(inode))
121 set_compress_context(inode);
124 /* Should enable inline_data after compression set */
125 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
126 set_inode_flag(inode, FI_INLINE_DATA);
128 stat_inc_inline_xattr(inode);
129 stat_inc_inline_inode(inode);
130 stat_inc_inline_dir(inode);
132 f2fs_set_inode_flags(inode);
134 trace_f2fs_new_inode(inode, 0);
138 trace_f2fs_new_inode(inode, err);
139 make_bad_inode(inode);
141 set_inode_flag(inode, FI_FREE_NID);
145 trace_f2fs_new_inode(inode, err);
147 inode->i_flags |= S_NOQUOTA;
149 set_inode_flag(inode, FI_FREE_NID);
151 unlock_new_inode(inode);
156 static inline int is_extension_exist(const unsigned char *s, const char *sub,
159 size_t slen = strlen(s);
160 size_t sublen = strlen(sub);
163 if (sublen == 1 && *sub == '*')
167 * filename format of multimedia file should be defined as:
168 * "filename + '.' + extension + (optional: '.' + temp extension)".
170 if (slen < sublen + 2)
174 /* file has no temp extension */
175 if (s[slen - sublen - 1] != '.')
177 return !strncasecmp(s + slen - sublen, sub, sublen);
180 for (i = 1; i < slen - sublen; i++) {
183 if (!strncasecmp(s + i + 1, sub, sublen))
191 * Set file's temperature for hot/cold data separation
193 static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
194 const unsigned char *name)
196 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
197 int i, cold_count, hot_count;
199 f2fs_down_read(&sbi->sb_lock);
201 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
202 hot_count = sbi->raw_super->hot_ext_count;
204 for (i = 0; i < cold_count + hot_count; i++) {
205 if (is_extension_exist(name, extlist[i], true))
209 f2fs_up_read(&sbi->sb_lock);
211 if (i == cold_count + hot_count)
215 file_set_cold(inode);
220 int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
223 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
224 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
225 int hot_count = sbi->raw_super->hot_ext_count;
226 int total_count = cold_count + hot_count;
231 if (total_count == F2FS_MAX_EXTENSION)
234 if (!hot && !cold_count)
236 if (hot && !hot_count)
248 for (i = start; i < count; i++) {
249 if (strcmp(name, extlist[i]))
255 memcpy(extlist[i], extlist[i + 1],
256 F2FS_EXTENSION_LEN * (total_count - i - 1));
257 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
259 sbi->raw_super->hot_ext_count = hot_count - 1;
261 sbi->raw_super->extension_count =
262 cpu_to_le32(cold_count - 1);
270 memcpy(extlist[count], name, strlen(name));
271 sbi->raw_super->hot_ext_count = hot_count + 1;
273 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
275 memcpy(buf, &extlist[cold_count],
276 F2FS_EXTENSION_LEN * hot_count);
277 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
278 memcpy(extlist[cold_count], name, strlen(name));
279 memcpy(&extlist[cold_count + 1], buf,
280 F2FS_EXTENSION_LEN * hot_count);
281 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
286 static void set_compress_inode(struct f2fs_sb_info *sbi, struct inode *inode,
287 const unsigned char *name)
289 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
290 unsigned char (*noext)[F2FS_EXTENSION_LEN] = F2FS_OPTION(sbi).noextensions;
291 unsigned char (*ext)[F2FS_EXTENSION_LEN] = F2FS_OPTION(sbi).extensions;
292 unsigned char ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
293 unsigned char noext_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
294 int i, cold_count, hot_count;
296 if (!f2fs_sb_has_compression(sbi) ||
297 F2FS_I(inode)->i_flags & F2FS_NOCOMP_FL ||
298 !f2fs_may_compress(inode) ||
299 (!ext_cnt && !noext_cnt))
302 f2fs_down_read(&sbi->sb_lock);
304 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
305 hot_count = sbi->raw_super->hot_ext_count;
307 for (i = cold_count; i < cold_count + hot_count; i++) {
308 if (is_extension_exist(name, extlist[i], false)) {
309 f2fs_up_read(&sbi->sb_lock);
314 f2fs_up_read(&sbi->sb_lock);
316 for (i = 0; i < noext_cnt; i++) {
317 if (is_extension_exist(name, noext[i], false)) {
318 f2fs_disable_compressed_file(inode);
323 if (is_inode_flag_set(inode, FI_COMPRESSED_FILE))
326 for (i = 0; i < ext_cnt; i++) {
327 if (!is_extension_exist(name, ext[i], false))
330 /* Do not use inline_data with compression */
331 stat_dec_inline_inode(inode);
332 clear_inode_flag(inode, FI_INLINE_DATA);
333 set_compress_context(inode);
338 static int f2fs_create(struct user_namespace *mnt_userns, struct inode *dir,
339 struct dentry *dentry, umode_t mode, bool excl)
341 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
346 if (unlikely(f2fs_cp_error(sbi)))
348 if (!f2fs_is_checkpoint_ready(sbi))
351 err = f2fs_dquot_initialize(dir);
355 inode = f2fs_new_inode(mnt_userns, dir, mode);
357 return PTR_ERR(inode);
359 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
360 set_file_temperature(sbi, inode, dentry->d_name.name);
362 set_compress_inode(sbi, inode, dentry->d_name.name);
364 inode->i_op = &f2fs_file_inode_operations;
365 inode->i_fop = &f2fs_file_operations;
366 inode->i_mapping->a_ops = &f2fs_dblock_aops;
370 err = f2fs_add_link(dentry, inode);
375 f2fs_alloc_nid_done(sbi, ino);
377 d_instantiate_new(dentry, inode);
380 f2fs_sync_fs(sbi->sb, 1);
382 f2fs_balance_fs(sbi, true);
385 f2fs_handle_failed_inode(inode);
389 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
390 struct dentry *dentry)
392 struct inode *inode = d_inode(old_dentry);
393 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
396 if (unlikely(f2fs_cp_error(sbi)))
398 if (!f2fs_is_checkpoint_ready(sbi))
401 err = fscrypt_prepare_link(old_dentry, dir, dentry);
405 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
406 (!projid_eq(F2FS_I(dir)->i_projid,
407 F2FS_I(old_dentry->d_inode)->i_projid)))
410 err = f2fs_dquot_initialize(dir);
414 f2fs_balance_fs(sbi, true);
416 inode->i_ctime = current_time(inode);
419 set_inode_flag(inode, FI_INC_LINK);
421 err = f2fs_add_link(dentry, inode);
426 d_instantiate(dentry, inode);
429 f2fs_sync_fs(sbi->sb, 1);
432 clear_inode_flag(inode, FI_INC_LINK);
438 struct dentry *f2fs_get_parent(struct dentry *child)
441 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot_name, &page);
445 return ERR_CAST(page);
446 return ERR_PTR(-ENOENT);
448 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
451 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
453 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
454 struct qstr dot = QSTR_INIT(".", 1);
455 struct qstr dotdot = QSTR_INIT("..", 2);
456 struct f2fs_dir_entry *de;
460 if (f2fs_readonly(sbi->sb)) {
461 f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
466 if (!S_ISDIR(dir->i_mode)) {
467 f2fs_err(sbi, "inconsistent inode status, skip recovering inline_dots inode (ino:%lu, i_mode:%u, pino:%u)",
468 dir->i_ino, dir->i_mode, pino);
469 set_sbi_flag(sbi, SBI_NEED_FSCK);
473 err = f2fs_dquot_initialize(dir);
477 f2fs_balance_fs(sbi, true);
481 de = f2fs_find_entry(dir, &dot, &page);
483 f2fs_put_page(page, 0);
484 } else if (IS_ERR(page)) {
488 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
493 de = f2fs_find_entry(dir, &dotdot, &page);
495 f2fs_put_page(page, 0);
496 else if (IS_ERR(page))
499 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
502 clear_inode_flag(dir, FI_INLINE_DOTS);
508 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
511 struct inode *inode = NULL;
512 struct f2fs_dir_entry *de;
517 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
518 struct f2fs_filename fname;
520 trace_f2fs_lookup_start(dir, dentry, flags);
522 if (dentry->d_name.len > F2FS_NAME_LEN) {
527 err = f2fs_prepare_lookup(dir, dentry, &fname);
528 generic_set_encrypted_ci_d_ops(dentry);
533 de = __f2fs_find_entry(dir, &fname, &page);
534 f2fs_free_filename(&fname);
545 ino = le32_to_cpu(de->ino);
546 f2fs_put_page(page, 0);
548 inode = f2fs_iget(dir->i_sb, ino);
550 err = PTR_ERR(inode);
554 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
555 err = __recover_dot_dentries(dir, root_ino);
560 if (f2fs_has_inline_dots(inode)) {
561 err = __recover_dot_dentries(inode, dir->i_ino);
565 if (IS_ENCRYPTED(dir) &&
566 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
567 !fscrypt_has_permitted_context(dir, inode)) {
568 f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
569 dir->i_ino, inode->i_ino);
574 #if IS_ENABLED(CONFIG_UNICODE)
575 if (!inode && IS_CASEFOLDED(dir)) {
576 /* Eventually we want to call d_add_ci(dentry, NULL)
577 * for negative dentries in the encoding case as
578 * well. For now, prevent the negative dentry
581 trace_f2fs_lookup_end(dir, dentry, ino, err);
585 new = d_splice_alias(inode, dentry);
586 err = PTR_ERR_OR_ZERO(new);
587 trace_f2fs_lookup_end(dir, dentry, ino, !new ? -ENOENT : err);
592 trace_f2fs_lookup_end(dir, dentry, ino, err);
596 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
598 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
599 struct inode *inode = d_inode(dentry);
600 struct f2fs_dir_entry *de;
604 trace_f2fs_unlink_enter(dir, dentry);
606 if (unlikely(f2fs_cp_error(sbi))) {
611 err = f2fs_dquot_initialize(dir);
614 err = f2fs_dquot_initialize(inode);
618 de = f2fs_find_entry(dir, &dentry->d_name, &page);
625 f2fs_balance_fs(sbi, true);
628 err = f2fs_acquire_orphan_inode(sbi);
631 f2fs_put_page(page, 0);
634 f2fs_delete_entry(de, page, dir, inode);
635 #if IS_ENABLED(CONFIG_UNICODE)
636 /* VFS negative dentries are incompatible with Encoding and
637 * Case-insensitiveness. Eventually we'll want avoid
638 * invalidating the dentries here, alongside with returning the
639 * negative dentries at f2fs_lookup(), when it is better
640 * supported by the VFS for the CI case.
642 if (IS_CASEFOLDED(dir))
643 d_invalidate(dentry);
648 f2fs_sync_fs(sbi->sb, 1);
650 trace_f2fs_unlink_exit(inode, err);
654 static const char *f2fs_get_link(struct dentry *dentry,
656 struct delayed_call *done)
658 const char *link = page_get_link(dentry, inode, done);
660 if (!IS_ERR(link) && !*link) {
661 /* this is broken symlink case */
662 do_delayed_call(done);
663 clear_delayed_call(done);
664 link = ERR_PTR(-ENOENT);
669 static int f2fs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
670 struct dentry *dentry, const char *symname)
672 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
674 size_t len = strlen(symname);
675 struct fscrypt_str disk_link;
678 if (unlikely(f2fs_cp_error(sbi)))
680 if (!f2fs_is_checkpoint_ready(sbi))
683 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
688 err = f2fs_dquot_initialize(dir);
692 inode = f2fs_new_inode(mnt_userns, dir, S_IFLNK | S_IRWXUGO);
694 return PTR_ERR(inode);
696 if (IS_ENCRYPTED(inode))
697 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
699 inode->i_op = &f2fs_symlink_inode_operations;
700 inode_nohighmem(inode);
701 inode->i_mapping->a_ops = &f2fs_dblock_aops;
704 err = f2fs_add_link(dentry, inode);
706 goto out_f2fs_handle_failed_inode;
708 f2fs_alloc_nid_done(sbi, inode->i_ino);
710 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
714 err = page_symlink(inode, disk_link.name, disk_link.len);
717 d_instantiate_new(dentry, inode);
720 * Let's flush symlink data in order to avoid broken symlink as much as
721 * possible. Nevertheless, fsyncing is the best way, but there is no
722 * way to get a file descriptor in order to flush that.
724 * Note that, it needs to do dir->fsync to make this recoverable.
725 * If the symlink path is stored into inline_data, there is no
726 * performance regression.
729 filemap_write_and_wait_range(inode->i_mapping, 0,
733 f2fs_sync_fs(sbi->sb, 1);
735 f2fs_unlink(dir, dentry);
738 f2fs_balance_fs(sbi, true);
739 goto out_free_encrypted_link;
741 out_f2fs_handle_failed_inode:
742 f2fs_handle_failed_inode(inode);
743 out_free_encrypted_link:
744 if (disk_link.name != (unsigned char *)symname)
745 kfree(disk_link.name);
749 static int f2fs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
750 struct dentry *dentry, umode_t mode)
752 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
756 if (unlikely(f2fs_cp_error(sbi)))
759 err = f2fs_dquot_initialize(dir);
763 inode = f2fs_new_inode(mnt_userns, dir, S_IFDIR | mode);
765 return PTR_ERR(inode);
767 inode->i_op = &f2fs_dir_inode_operations;
768 inode->i_fop = &f2fs_dir_operations;
769 inode->i_mapping->a_ops = &f2fs_dblock_aops;
770 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
772 set_inode_flag(inode, FI_INC_LINK);
774 err = f2fs_add_link(dentry, inode);
779 f2fs_alloc_nid_done(sbi, inode->i_ino);
781 d_instantiate_new(dentry, inode);
784 f2fs_sync_fs(sbi->sb, 1);
786 f2fs_balance_fs(sbi, true);
790 clear_inode_flag(inode, FI_INC_LINK);
791 f2fs_handle_failed_inode(inode);
795 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
797 struct inode *inode = d_inode(dentry);
799 if (f2fs_empty_dir(inode))
800 return f2fs_unlink(dir, dentry);
804 static int f2fs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
805 struct dentry *dentry, umode_t mode, dev_t rdev)
807 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
811 if (unlikely(f2fs_cp_error(sbi)))
813 if (!f2fs_is_checkpoint_ready(sbi))
816 err = f2fs_dquot_initialize(dir);
820 inode = f2fs_new_inode(mnt_userns, dir, mode);
822 return PTR_ERR(inode);
824 init_special_inode(inode, inode->i_mode, rdev);
825 inode->i_op = &f2fs_special_inode_operations;
828 err = f2fs_add_link(dentry, inode);
833 f2fs_alloc_nid_done(sbi, inode->i_ino);
835 d_instantiate_new(dentry, inode);
838 f2fs_sync_fs(sbi->sb, 1);
840 f2fs_balance_fs(sbi, true);
843 f2fs_handle_failed_inode(inode);
847 static int __f2fs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
848 struct file *file, umode_t mode, bool is_whiteout,
849 struct inode **new_inode)
851 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
855 err = f2fs_dquot_initialize(dir);
859 inode = f2fs_new_inode(mnt_userns, dir, mode);
861 return PTR_ERR(inode);
864 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
865 inode->i_op = &f2fs_special_inode_operations;
867 inode->i_op = &f2fs_file_inode_operations;
868 inode->i_fop = &f2fs_file_operations;
869 inode->i_mapping->a_ops = &f2fs_dblock_aops;
873 err = f2fs_acquire_orphan_inode(sbi);
877 err = f2fs_do_tmpfile(inode, dir);
882 * add this non-linked tmpfile to orphan list, in this way we could
883 * remove all unused data of tmpfile after abnormal power-off.
885 f2fs_add_orphan_inode(inode);
886 f2fs_alloc_nid_done(sbi, inode->i_ino);
889 f2fs_i_links_write(inode, false);
891 spin_lock(&inode->i_lock);
892 inode->i_state |= I_LINKABLE;
893 spin_unlock(&inode->i_lock);
896 d_tmpfile(file, inode);
898 f2fs_i_links_write(inode, false);
900 /* link_count was changed by d_tmpfile as well. */
902 unlock_new_inode(inode);
907 f2fs_balance_fs(sbi, true);
911 f2fs_release_orphan_inode(sbi);
913 f2fs_handle_failed_inode(inode);
917 static int f2fs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
918 struct file *file, umode_t mode)
920 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
923 if (unlikely(f2fs_cp_error(sbi)))
925 if (!f2fs_is_checkpoint_ready(sbi))
928 err = __f2fs_tmpfile(mnt_userns, dir, file, mode, false, NULL);
930 return finish_open_simple(file, err);
933 static int f2fs_create_whiteout(struct user_namespace *mnt_userns,
934 struct inode *dir, struct inode **whiteout)
936 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
939 return __f2fs_tmpfile(mnt_userns, dir, NULL,
940 S_IFCHR | WHITEOUT_MODE, true, whiteout);
943 int f2fs_get_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
944 struct inode **new_inode)
946 return __f2fs_tmpfile(mnt_userns, dir, NULL, S_IFREG, false, new_inode);
949 static int f2fs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
950 struct dentry *old_dentry, struct inode *new_dir,
951 struct dentry *new_dentry, unsigned int flags)
953 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
954 struct inode *old_inode = d_inode(old_dentry);
955 struct inode *new_inode = d_inode(new_dentry);
956 struct inode *whiteout = NULL;
957 struct page *old_dir_page = NULL;
958 struct page *old_page, *new_page = NULL;
959 struct f2fs_dir_entry *old_dir_entry = NULL;
960 struct f2fs_dir_entry *old_entry;
961 struct f2fs_dir_entry *new_entry;
964 if (unlikely(f2fs_cp_error(sbi)))
966 if (!f2fs_is_checkpoint_ready(sbi))
969 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
970 (!projid_eq(F2FS_I(new_dir)->i_projid,
971 F2FS_I(old_dentry->d_inode)->i_projid)))
975 * If new_inode is null, the below renaming flow will
976 * add a link in old_dir which can conver inline_dir.
977 * After then, if we failed to get the entry due to other
978 * reasons like ENOMEM, we had to remove the new entry.
979 * Instead of adding such the error handling routine, let's
980 * simply convert first here.
982 if (old_dir == new_dir && !new_inode) {
983 err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
988 if (flags & RENAME_WHITEOUT) {
989 err = f2fs_create_whiteout(mnt_userns, old_dir, &whiteout);
994 err = f2fs_dquot_initialize(old_dir);
998 err = f2fs_dquot_initialize(new_dir);
1003 err = f2fs_dquot_initialize(new_inode);
1009 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1011 if (IS_ERR(old_page))
1012 err = PTR_ERR(old_page);
1016 if (S_ISDIR(old_inode->i_mode)) {
1017 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
1018 if (!old_dir_entry) {
1019 if (IS_ERR(old_dir_page))
1020 err = PTR_ERR(old_dir_page);
1028 if (old_dir_entry && !f2fs_empty_dir(new_inode))
1032 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
1035 if (IS_ERR(new_page))
1036 err = PTR_ERR(new_page);
1040 f2fs_balance_fs(sbi, true);
1044 err = f2fs_acquire_orphan_inode(sbi);
1048 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1051 new_inode->i_ctime = current_time(new_inode);
1052 f2fs_down_write(&F2FS_I(new_inode)->i_sem);
1054 f2fs_i_links_write(new_inode, false);
1055 f2fs_i_links_write(new_inode, false);
1056 f2fs_up_write(&F2FS_I(new_inode)->i_sem);
1058 if (!new_inode->i_nlink)
1059 f2fs_add_orphan_inode(new_inode);
1061 f2fs_release_orphan_inode(sbi);
1063 f2fs_balance_fs(sbi, true);
1067 err = f2fs_add_link(new_dentry, old_inode);
1069 f2fs_unlock_op(sbi);
1074 f2fs_i_links_write(new_dir, true);
1077 f2fs_down_write(&F2FS_I(old_inode)->i_sem);
1078 if (!old_dir_entry || whiteout)
1079 file_lost_pino(old_inode);
1081 /* adjust dir's i_pino to pass fsck check */
1082 f2fs_i_pino_write(old_inode, new_dir->i_ino);
1083 f2fs_up_write(&F2FS_I(old_inode)->i_sem);
1085 old_inode->i_ctime = current_time(old_inode);
1086 f2fs_mark_inode_dirty_sync(old_inode, false);
1088 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
1092 set_inode_flag(whiteout, FI_INC_LINK);
1093 err = f2fs_add_link(old_dentry, whiteout);
1097 spin_lock(&whiteout->i_lock);
1098 whiteout->i_state &= ~I_LINKABLE;
1099 spin_unlock(&whiteout->i_lock);
1104 if (old_dir_entry) {
1105 if (old_dir != new_dir && !whiteout)
1106 f2fs_set_link(old_inode, old_dir_entry,
1107 old_dir_page, new_dir);
1109 f2fs_put_page(old_dir_page, 0);
1110 f2fs_i_links_write(old_dir, false);
1112 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1113 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1114 if (S_ISDIR(old_inode->i_mode))
1115 f2fs_add_ino_entry(sbi, old_inode->i_ino,
1119 f2fs_unlock_op(sbi);
1121 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1122 f2fs_sync_fs(sbi->sb, 1);
1124 f2fs_update_time(sbi, REQ_TIME);
1128 f2fs_unlock_op(sbi);
1129 f2fs_put_page(new_page, 0);
1132 f2fs_put_page(old_dir_page, 0);
1134 f2fs_put_page(old_page, 0);
1140 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1141 struct inode *new_dir, struct dentry *new_dentry)
1143 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1144 struct inode *old_inode = d_inode(old_dentry);
1145 struct inode *new_inode = d_inode(new_dentry);
1146 struct page *old_dir_page, *new_dir_page;
1147 struct page *old_page, *new_page;
1148 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1149 struct f2fs_dir_entry *old_entry, *new_entry;
1150 int old_nlink = 0, new_nlink = 0;
1153 if (unlikely(f2fs_cp_error(sbi)))
1155 if (!f2fs_is_checkpoint_ready(sbi))
1158 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1159 !projid_eq(F2FS_I(new_dir)->i_projid,
1160 F2FS_I(old_dentry->d_inode)->i_projid)) ||
1161 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1162 !projid_eq(F2FS_I(old_dir)->i_projid,
1163 F2FS_I(new_dentry->d_inode)->i_projid)))
1166 err = f2fs_dquot_initialize(old_dir);
1170 err = f2fs_dquot_initialize(new_dir);
1175 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1177 if (IS_ERR(old_page))
1178 err = PTR_ERR(old_page);
1182 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1184 if (IS_ERR(new_page))
1185 err = PTR_ERR(new_page);
1189 /* prepare for updating ".." directory entry info later */
1190 if (old_dir != new_dir) {
1191 if (S_ISDIR(old_inode->i_mode)) {
1192 old_dir_entry = f2fs_parent_dir(old_inode,
1194 if (!old_dir_entry) {
1195 if (IS_ERR(old_dir_page))
1196 err = PTR_ERR(old_dir_page);
1201 if (S_ISDIR(new_inode->i_mode)) {
1202 new_dir_entry = f2fs_parent_dir(new_inode,
1204 if (!new_dir_entry) {
1205 if (IS_ERR(new_dir_page))
1206 err = PTR_ERR(new_dir_page);
1213 * If cross rename between file and directory those are not
1214 * in the same directory, we will inc nlink of file's parent
1215 * later, so we should check upper boundary of its nlink.
1217 if ((!old_dir_entry || !new_dir_entry) &&
1218 old_dir_entry != new_dir_entry) {
1219 old_nlink = old_dir_entry ? -1 : 1;
1220 new_nlink = -old_nlink;
1222 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1223 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1227 f2fs_balance_fs(sbi, true);
1231 /* update ".." directory entry info of old dentry */
1233 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1235 /* update ".." directory entry info of new dentry */
1237 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1239 /* update directory entry info of old dir inode */
1240 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1242 f2fs_down_write(&F2FS_I(old_inode)->i_sem);
1244 file_lost_pino(old_inode);
1246 /* adjust dir's i_pino to pass fsck check */
1247 f2fs_i_pino_write(old_inode, new_dir->i_ino);
1248 f2fs_up_write(&F2FS_I(old_inode)->i_sem);
1250 old_dir->i_ctime = current_time(old_dir);
1252 f2fs_down_write(&F2FS_I(old_dir)->i_sem);
1253 f2fs_i_links_write(old_dir, old_nlink > 0);
1254 f2fs_up_write(&F2FS_I(old_dir)->i_sem);
1256 f2fs_mark_inode_dirty_sync(old_dir, false);
1258 /* update directory entry info of new dir inode */
1259 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1261 f2fs_down_write(&F2FS_I(new_inode)->i_sem);
1263 file_lost_pino(new_inode);
1265 /* adjust dir's i_pino to pass fsck check */
1266 f2fs_i_pino_write(new_inode, old_dir->i_ino);
1267 f2fs_up_write(&F2FS_I(new_inode)->i_sem);
1269 new_dir->i_ctime = current_time(new_dir);
1271 f2fs_down_write(&F2FS_I(new_dir)->i_sem);
1272 f2fs_i_links_write(new_dir, new_nlink > 0);
1273 f2fs_up_write(&F2FS_I(new_dir)->i_sem);
1275 f2fs_mark_inode_dirty_sync(new_dir, false);
1277 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1278 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1279 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1282 f2fs_unlock_op(sbi);
1284 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1285 f2fs_sync_fs(sbi->sb, 1);
1287 f2fs_update_time(sbi, REQ_TIME);
1290 if (new_dir_entry) {
1291 f2fs_put_page(new_dir_page, 0);
1294 if (old_dir_entry) {
1295 f2fs_put_page(old_dir_page, 0);
1298 f2fs_put_page(new_page, 0);
1300 f2fs_put_page(old_page, 0);
1305 static int f2fs_rename2(struct user_namespace *mnt_userns,
1306 struct inode *old_dir, struct dentry *old_dentry,
1307 struct inode *new_dir, struct dentry *new_dentry,
1312 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1315 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1320 if (flags & RENAME_EXCHANGE) {
1321 return f2fs_cross_rename(old_dir, old_dentry,
1322 new_dir, new_dentry);
1325 * VFS has already handled the new dentry existence case,
1326 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1328 return f2fs_rename(mnt_userns, old_dir, old_dentry,
1329 new_dir, new_dentry, flags);
1332 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1333 struct inode *inode,
1334 struct delayed_call *done)
1340 return ERR_PTR(-ECHILD);
1342 page = read_mapping_page(inode->i_mapping, 0, NULL);
1344 return ERR_CAST(page);
1346 target = fscrypt_get_symlink(inode, page_address(page),
1347 inode->i_sb->s_blocksize, done);
1352 static int f2fs_encrypted_symlink_getattr(struct user_namespace *mnt_userns,
1353 const struct path *path,
1354 struct kstat *stat, u32 request_mask,
1355 unsigned int query_flags)
1357 f2fs_getattr(mnt_userns, path, stat, request_mask, query_flags);
1359 return fscrypt_symlink_getattr(path, stat);
1362 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1363 .get_link = f2fs_encrypted_get_link,
1364 .getattr = f2fs_encrypted_symlink_getattr,
1365 .setattr = f2fs_setattr,
1366 .listxattr = f2fs_listxattr,
1369 const struct inode_operations f2fs_dir_inode_operations = {
1370 .create = f2fs_create,
1371 .lookup = f2fs_lookup,
1373 .unlink = f2fs_unlink,
1374 .symlink = f2fs_symlink,
1375 .mkdir = f2fs_mkdir,
1376 .rmdir = f2fs_rmdir,
1377 .mknod = f2fs_mknod,
1378 .rename = f2fs_rename2,
1379 .tmpfile = f2fs_tmpfile,
1380 .getattr = f2fs_getattr,
1381 .setattr = f2fs_setattr,
1382 .get_acl = f2fs_get_acl,
1383 .set_acl = f2fs_set_acl,
1384 .listxattr = f2fs_listxattr,
1385 .fiemap = f2fs_fiemap,
1386 .fileattr_get = f2fs_fileattr_get,
1387 .fileattr_set = f2fs_fileattr_set,
1390 const struct inode_operations f2fs_symlink_inode_operations = {
1391 .get_link = f2fs_get_link,
1392 .getattr = f2fs_getattr,
1393 .setattr = f2fs_setattr,
1394 .listxattr = f2fs_listxattr,
1397 const struct inode_operations f2fs_special_inode_operations = {
1398 .getattr = f2fs_getattr,
1399 .setattr = f2fs_setattr,
1400 .get_acl = f2fs_get_acl,
1401 .set_acl = f2fs_set_acl,
1402 .listxattr = f2fs_listxattr,