4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
13 #include <linux/pagemap.h>
14 #include <linux/sched.h>
15 #include <linux/ctype.h>
16 #include <linux/dcache.h>
17 #include <linux/namei.h>
18 #include <linux/quotaops.h>
24 #include <trace/events/f2fs.h>
26 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
28 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
31 bool nid_free = false;
35 inode = new_inode(dir->i_sb);
37 return ERR_PTR(-ENOMEM);
40 if (!alloc_nid(sbi, &ino)) {
49 inode_init_owner(inode, dir, mode);
53 inode->i_mtime = inode->i_atime = inode->i_ctime =
54 F2FS_I(inode)->i_crtime = current_time(inode);
55 inode->i_generation = sbi->s_next_generation++;
57 err = insert_inode_locked(inode);
63 if (f2fs_sb_has_project_quota(sbi->sb) &&
64 (F2FS_I(dir)->i_flags & FS_PROJINHERIT_FL))
65 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
67 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
70 err = dquot_initialize(inode);
74 err = dquot_alloc_inode(inode);
78 set_inode_flag(inode, FI_NEW_INODE);
80 /* If the directory encrypted, then we should encrypt the inode. */
81 if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
82 f2fs_set_encrypted_inode(inode);
84 if (f2fs_sb_has_extra_attr(sbi->sb)) {
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 (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
93 set_inode_flag(inode, FI_INLINE_DATA);
94 if (f2fs_may_inline_dentry(inode))
95 set_inode_flag(inode, FI_INLINE_DENTRY);
97 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
98 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
99 if (f2fs_has_inline_xattr(inode))
100 xattr_size = 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;
106 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
108 f2fs_init_extent_tree(inode, NULL);
110 stat_inc_inline_xattr(inode);
111 stat_inc_inline_inode(inode);
112 stat_inc_inline_dir(inode);
114 F2FS_I(inode)->i_flags =
115 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
117 if (S_ISDIR(inode->i_mode))
118 F2FS_I(inode)->i_flags |= FS_INDEX_FL;
120 if (F2FS_I(inode)->i_flags & FS_PROJINHERIT_FL)
121 set_inode_flag(inode, FI_PROJ_INHERIT);
123 trace_f2fs_new_inode(inode, 0);
127 trace_f2fs_new_inode(inode, err);
128 make_bad_inode(inode);
130 set_inode_flag(inode, FI_FREE_NID);
134 trace_f2fs_new_inode(inode, err);
136 inode->i_flags |= S_NOQUOTA;
138 set_inode_flag(inode, FI_FREE_NID);
140 unlock_new_inode(inode);
145 static int is_multimedia_file(const unsigned char *s, const char *sub)
147 size_t slen = strlen(s);
148 size_t sublen = strlen(sub);
152 * filename format of multimedia file should be defined as:
153 * "filename + '.' + extension + (optional: '.' + temp extension)".
155 if (slen < sublen + 2)
158 for (i = 1; i < slen - sublen; i++) {
161 if (!strncasecmp(s + i + 1, sub, sublen))
169 * Set multimedia files as cold files for hot/cold data separation
171 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
172 const unsigned char *name)
175 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
177 int count = le32_to_cpu(sbi->raw_super->extension_count);
178 for (i = 0; i < count; i++) {
179 if (is_multimedia_file(name, extlist[i])) {
180 file_set_cold(inode);
186 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
189 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
194 if (unlikely(f2fs_cp_error(sbi)))
197 err = dquot_initialize(dir);
201 inode = f2fs_new_inode(dir, mode);
203 return PTR_ERR(inode);
205 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
206 set_cold_files(sbi, inode, dentry->d_name.name);
208 inode->i_op = &f2fs_file_inode_operations;
209 inode->i_fop = &f2fs_file_operations;
210 inode->i_mapping->a_ops = &f2fs_dblock_aops;
214 err = f2fs_add_link(dentry, inode);
219 alloc_nid_done(sbi, ino);
221 d_instantiate(dentry, inode);
222 unlock_new_inode(inode);
225 f2fs_sync_fs(sbi->sb, 1);
227 f2fs_balance_fs(sbi, true);
230 handle_failed_inode(inode);
234 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
235 struct dentry *dentry)
237 struct inode *inode = d_inode(old_dentry);
238 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
241 if (unlikely(f2fs_cp_error(sbi)))
244 err = fscrypt_prepare_link(old_dentry, dir, dentry);
248 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
249 (!projid_eq(F2FS_I(dir)->i_projid,
250 F2FS_I(old_dentry->d_inode)->i_projid)))
253 err = dquot_initialize(dir);
257 f2fs_balance_fs(sbi, true);
259 inode->i_ctime = current_time(inode);
262 set_inode_flag(inode, FI_INC_LINK);
264 err = f2fs_add_link(dentry, inode);
269 d_instantiate(dentry, inode);
272 f2fs_sync_fs(sbi->sb, 1);
275 clear_inode_flag(inode, FI_INC_LINK);
281 struct dentry *f2fs_get_parent(struct dentry *child)
283 struct qstr dotdot = QSTR_INIT("..", 2);
285 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
288 return ERR_CAST(page);
289 return ERR_PTR(-ENOENT);
291 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
294 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
296 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
297 struct qstr dot = QSTR_INIT(".", 1);
298 struct qstr dotdot = QSTR_INIT("..", 2);
299 struct f2fs_dir_entry *de;
303 if (f2fs_readonly(sbi->sb)) {
304 f2fs_msg(sbi->sb, KERN_INFO,
305 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
306 "in readonly mountpoint", dir->i_ino, pino);
310 err = dquot_initialize(dir);
314 f2fs_balance_fs(sbi, true);
318 de = f2fs_find_entry(dir, &dot, &page);
320 f2fs_dentry_kunmap(dir, page);
321 f2fs_put_page(page, 0);
322 } else if (IS_ERR(page)) {
326 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
331 de = f2fs_find_entry(dir, &dotdot, &page);
333 f2fs_dentry_kunmap(dir, page);
334 f2fs_put_page(page, 0);
335 } else if (IS_ERR(page)) {
338 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
342 clear_inode_flag(dir, FI_INLINE_DOTS);
348 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
351 struct inode *inode = NULL;
352 struct f2fs_dir_entry *de;
357 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
359 trace_f2fs_lookup_start(dir, dentry, flags);
361 err = fscrypt_prepare_lookup(dir, dentry, flags);
365 if (dentry->d_name.len > F2FS_NAME_LEN) {
370 de = f2fs_find_entry(dir, &dentry->d_name, &page);
379 ino = le32_to_cpu(de->ino);
380 f2fs_dentry_kunmap(dir, page);
381 f2fs_put_page(page, 0);
383 inode = f2fs_iget(dir->i_sb, ino);
385 err = PTR_ERR(inode);
389 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
390 err = __recover_dot_dentries(dir, root_ino);
395 if (f2fs_has_inline_dots(inode)) {
396 err = __recover_dot_dentries(inode, dir->i_ino);
400 if (f2fs_encrypted_inode(dir) &&
401 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
402 !fscrypt_has_permitted_context(dir, inode)) {
403 f2fs_msg(inode->i_sb, KERN_WARNING,
404 "Inconsistent encryption contexts: %lu/%lu",
405 dir->i_ino, inode->i_ino);
410 new = d_splice_alias(inode, dentry);
413 trace_f2fs_lookup_end(dir, dentry, ino, err);
418 trace_f2fs_lookup_end(dir, dentry, ino, err);
422 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
424 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
425 struct inode *inode = d_inode(dentry);
426 struct f2fs_dir_entry *de;
430 trace_f2fs_unlink_enter(dir, dentry);
432 if (unlikely(f2fs_cp_error(sbi)))
435 err = dquot_initialize(dir);
438 err = dquot_initialize(inode);
442 de = f2fs_find_entry(dir, &dentry->d_name, &page);
449 f2fs_balance_fs(sbi, true);
452 err = acquire_orphan_inode(sbi);
455 f2fs_dentry_kunmap(dir, page);
456 f2fs_put_page(page, 0);
459 f2fs_delete_entry(de, page, dir, inode);
463 f2fs_sync_fs(sbi->sb, 1);
465 trace_f2fs_unlink_exit(inode, err);
469 static const char *f2fs_follow_link(struct dentry *dentry, void **cookie)
471 const char *link = page_follow_link_light(dentry, cookie);
472 if (!IS_ERR(link) && !*link) {
473 /* this is broken symlink case */
474 page_put_link(NULL, *cookie);
475 link = ERR_PTR(-ENOENT);
480 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
483 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
485 size_t len = strlen(symname);
486 struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
487 struct fscrypt_symlink_data *sd = NULL;
490 if (unlikely(f2fs_cp_error(sbi)))
493 if (f2fs_encrypted_inode(dir)) {
494 err = fscrypt_get_encryption_info(dir);
498 if (!fscrypt_has_encryption_key(dir))
501 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
502 sizeof(struct fscrypt_symlink_data));
505 if (disk_link.len > dir->i_sb->s_blocksize)
506 return -ENAMETOOLONG;
508 err = dquot_initialize(dir);
512 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
514 return PTR_ERR(inode);
516 if (f2fs_encrypted_inode(inode))
517 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
519 inode->i_op = &f2fs_symlink_inode_operations;
520 inode_nohighmem(inode);
521 inode->i_mapping->a_ops = &f2fs_dblock_aops;
524 err = f2fs_add_link(dentry, inode);
528 alloc_nid_done(sbi, inode->i_ino);
530 if (f2fs_encrypted_inode(inode)) {
531 struct qstr istr = QSTR_INIT(symname, len);
532 struct fscrypt_str ostr;
534 sd = f2fs_kzalloc(sbi, disk_link.len, GFP_NOFS);
540 err = fscrypt_get_encryption_info(inode);
544 if (!fscrypt_has_encryption_key(inode)) {
549 ostr.name = sd->encrypted_path;
550 ostr.len = disk_link.len;
551 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
555 sd->len = cpu_to_le16(ostr.len);
556 disk_link.name = (char *)sd;
559 err = page_symlink(inode, disk_link.name, disk_link.len);
562 d_instantiate(dentry, inode);
563 unlock_new_inode(inode);
566 * Let's flush symlink data in order to avoid broken symlink as much as
567 * possible. Nevertheless, fsyncing is the best way, but there is no
568 * way to get a file descriptor in order to flush that.
570 * Note that, it needs to do dir->fsync to make this recoverable.
571 * If the symlink path is stored into inline_data, there is no
572 * performance regression.
575 filemap_write_and_wait_range(inode->i_mapping, 0,
579 f2fs_sync_fs(sbi->sb, 1);
581 f2fs_unlink(dir, dentry);
586 f2fs_balance_fs(sbi, true);
589 handle_failed_inode(inode);
593 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
595 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
599 if (unlikely(f2fs_cp_error(sbi)))
602 err = dquot_initialize(dir);
606 inode = f2fs_new_inode(dir, S_IFDIR | mode);
608 return PTR_ERR(inode);
610 inode->i_op = &f2fs_dir_inode_operations;
611 inode->i_fop = &f2fs_dir_operations;
612 inode->i_mapping->a_ops = &f2fs_dblock_aops;
613 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
615 set_inode_flag(inode, FI_INC_LINK);
617 err = f2fs_add_link(dentry, inode);
622 alloc_nid_done(sbi, inode->i_ino);
624 d_instantiate(dentry, inode);
625 unlock_new_inode(inode);
628 f2fs_sync_fs(sbi->sb, 1);
630 f2fs_balance_fs(sbi, true);
634 clear_inode_flag(inode, FI_INC_LINK);
635 handle_failed_inode(inode);
639 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
641 struct inode *inode = d_inode(dentry);
642 if (f2fs_empty_dir(inode))
643 return f2fs_unlink(dir, dentry);
647 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
648 umode_t mode, dev_t rdev)
650 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
654 if (unlikely(f2fs_cp_error(sbi)))
657 err = dquot_initialize(dir);
661 inode = f2fs_new_inode(dir, mode);
663 return PTR_ERR(inode);
665 init_special_inode(inode, inode->i_mode, rdev);
666 inode->i_op = &f2fs_special_inode_operations;
669 err = f2fs_add_link(dentry, inode);
674 alloc_nid_done(sbi, inode->i_ino);
676 d_instantiate(dentry, inode);
677 unlock_new_inode(inode);
680 f2fs_sync_fs(sbi->sb, 1);
682 f2fs_balance_fs(sbi, true);
685 handle_failed_inode(inode);
689 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
690 umode_t mode, struct inode **whiteout)
692 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
696 err = dquot_initialize(dir);
700 inode = f2fs_new_inode(dir, mode);
702 return PTR_ERR(inode);
705 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
706 inode->i_op = &f2fs_special_inode_operations;
708 inode->i_op = &f2fs_file_inode_operations;
709 inode->i_fop = &f2fs_file_operations;
710 inode->i_mapping->a_ops = &f2fs_dblock_aops;
714 err = acquire_orphan_inode(sbi);
718 err = f2fs_do_tmpfile(inode, dir);
723 * add this non-linked tmpfile to orphan list, in this way we could
724 * remove all unused data of tmpfile after abnormal power-off.
726 add_orphan_inode(inode);
727 alloc_nid_done(sbi, inode->i_ino);
730 f2fs_i_links_write(inode, false);
733 d_tmpfile(dentry, inode);
735 /* link_count was changed by d_tmpfile as well. */
737 unlock_new_inode(inode);
739 f2fs_balance_fs(sbi, true);
743 release_orphan_inode(sbi);
745 handle_failed_inode(inode);
749 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
751 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
754 if (f2fs_encrypted_inode(dir)) {
755 int err = fscrypt_get_encryption_info(dir);
760 return __f2fs_tmpfile(dir, dentry, mode, NULL);
763 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
765 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
768 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
771 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
772 struct inode *new_dir, struct dentry *new_dentry,
775 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
776 struct inode *old_inode = d_inode(old_dentry);
777 struct inode *new_inode = d_inode(new_dentry);
778 struct inode *whiteout = NULL;
779 struct page *old_dir_page;
780 struct page *old_page, *new_page = NULL;
781 struct f2fs_dir_entry *old_dir_entry = NULL;
782 struct f2fs_dir_entry *old_entry;
783 struct f2fs_dir_entry *new_entry;
784 bool is_old_inline = f2fs_has_inline_dentry(old_dir);
787 if (unlikely(f2fs_cp_error(sbi)))
790 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
791 (!projid_eq(F2FS_I(new_dir)->i_projid,
792 F2FS_I(old_dentry->d_inode)->i_projid)))
795 err = dquot_initialize(old_dir);
799 err = dquot_initialize(new_dir);
804 err = dquot_initialize(new_inode);
809 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
811 if (IS_ERR(old_page))
812 err = PTR_ERR(old_page);
816 if (S_ISDIR(old_inode->i_mode)) {
817 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
818 if (!old_dir_entry) {
819 if (IS_ERR(old_dir_page))
820 err = PTR_ERR(old_dir_page);
825 if (flags & RENAME_WHITEOUT) {
826 err = f2fs_create_whiteout(old_dir, &whiteout);
834 if (old_dir_entry && !f2fs_empty_dir(new_inode))
838 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
841 if (IS_ERR(new_page))
842 err = PTR_ERR(new_page);
846 f2fs_balance_fs(sbi, true);
850 err = acquire_orphan_inode(sbi);
854 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
856 new_inode->i_ctime = current_time(new_inode);
857 down_write(&F2FS_I(new_inode)->i_sem);
859 f2fs_i_links_write(new_inode, false);
860 f2fs_i_links_write(new_inode, false);
861 up_write(&F2FS_I(new_inode)->i_sem);
863 if (!new_inode->i_nlink)
864 add_orphan_inode(new_inode);
866 release_orphan_inode(sbi);
868 f2fs_balance_fs(sbi, true);
872 err = f2fs_add_link(new_dentry, old_inode);
879 f2fs_i_links_write(new_dir, true);
882 * old entry and new entry can locate in the same inline
883 * dentry in inode, when attaching new entry in inline dentry,
884 * it could force inline dentry conversion, after that,
885 * old_entry and old_page will point to wrong address, in
886 * order to avoid this, let's do the check and update here.
888 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
889 f2fs_put_page(old_page, 0);
892 old_entry = f2fs_find_entry(old_dir,
893 &old_dentry->d_name, &old_page);
896 if (IS_ERR(old_page))
897 err = PTR_ERR(old_page);
904 down_write(&F2FS_I(old_inode)->i_sem);
905 if (!old_dir_entry || whiteout)
906 file_lost_pino(old_inode);
908 F2FS_I(old_inode)->i_pino = new_dir->i_ino;
909 up_write(&F2FS_I(old_inode)->i_sem);
911 old_inode->i_ctime = current_time(old_inode);
912 f2fs_mark_inode_dirty_sync(old_inode, false);
914 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
917 whiteout->i_state |= I_LINKABLE;
918 set_inode_flag(whiteout, FI_INC_LINK);
919 err = f2fs_add_link(old_dentry, whiteout);
922 whiteout->i_state &= ~I_LINKABLE;
927 if (old_dir != new_dir && !whiteout) {
928 f2fs_set_link(old_inode, old_dir_entry,
929 old_dir_page, new_dir);
931 f2fs_dentry_kunmap(old_inode, old_dir_page);
932 f2fs_put_page(old_dir_page, 0);
934 f2fs_i_links_write(old_dir, false);
936 add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
940 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
941 f2fs_sync_fs(sbi->sb, 1);
947 f2fs_dentry_kunmap(new_dir, new_page);
948 f2fs_put_page(new_page, 0);
955 f2fs_dentry_kunmap(old_inode, old_dir_page);
956 f2fs_put_page(old_dir_page, 0);
959 f2fs_dentry_kunmap(old_dir, old_page);
960 f2fs_put_page(old_page, 0);
965 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
966 struct inode *new_dir, struct dentry *new_dentry)
968 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
969 struct inode *old_inode = d_inode(old_dentry);
970 struct inode *new_inode = d_inode(new_dentry);
971 struct page *old_dir_page, *new_dir_page;
972 struct page *old_page, *new_page;
973 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
974 struct f2fs_dir_entry *old_entry, *new_entry;
975 int old_nlink = 0, new_nlink = 0;
978 if (unlikely(f2fs_cp_error(sbi)))
981 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
982 !projid_eq(F2FS_I(new_dir)->i_projid,
983 F2FS_I(old_dentry->d_inode)->i_projid)) ||
984 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
985 !projid_eq(F2FS_I(old_dir)->i_projid,
986 F2FS_I(new_dentry->d_inode)->i_projid)))
989 err = dquot_initialize(old_dir);
993 err = dquot_initialize(new_dir);
997 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
999 if (IS_ERR(old_page))
1000 err = PTR_ERR(old_page);
1004 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1006 if (IS_ERR(new_page))
1007 err = PTR_ERR(new_page);
1011 /* prepare for updating ".." directory entry info later */
1012 if (old_dir != new_dir) {
1013 if (S_ISDIR(old_inode->i_mode)) {
1014 old_dir_entry = f2fs_parent_dir(old_inode,
1016 if (!old_dir_entry) {
1017 if (IS_ERR(old_dir_page))
1018 err = PTR_ERR(old_dir_page);
1023 if (S_ISDIR(new_inode->i_mode)) {
1024 new_dir_entry = f2fs_parent_dir(new_inode,
1026 if (!new_dir_entry) {
1027 if (IS_ERR(new_dir_page))
1028 err = PTR_ERR(new_dir_page);
1035 * If cross rename between file and directory those are not
1036 * in the same directory, we will inc nlink of file's parent
1037 * later, so we should check upper boundary of its nlink.
1039 if ((!old_dir_entry || !new_dir_entry) &&
1040 old_dir_entry != new_dir_entry) {
1041 old_nlink = old_dir_entry ? -1 : 1;
1042 new_nlink = -old_nlink;
1044 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1045 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1049 f2fs_balance_fs(sbi, true);
1053 /* update ".." directory entry info of old dentry */
1055 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1057 /* update ".." directory entry info of new dentry */
1059 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1061 /* update directory entry info of old dir inode */
1062 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1064 down_write(&F2FS_I(old_inode)->i_sem);
1065 file_lost_pino(old_inode);
1066 up_write(&F2FS_I(old_inode)->i_sem);
1068 old_dir->i_ctime = current_time(old_dir);
1070 down_write(&F2FS_I(old_dir)->i_sem);
1071 f2fs_i_links_write(old_dir, old_nlink > 0);
1072 up_write(&F2FS_I(old_dir)->i_sem);
1074 f2fs_mark_inode_dirty_sync(old_dir, false);
1076 /* update directory entry info of new dir inode */
1077 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1079 down_write(&F2FS_I(new_inode)->i_sem);
1080 file_lost_pino(new_inode);
1081 up_write(&F2FS_I(new_inode)->i_sem);
1083 new_dir->i_ctime = current_time(new_dir);
1085 down_write(&F2FS_I(new_dir)->i_sem);
1086 f2fs_i_links_write(new_dir, new_nlink > 0);
1087 up_write(&F2FS_I(new_dir)->i_sem);
1089 f2fs_mark_inode_dirty_sync(new_dir, false);
1091 add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1092 add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1094 f2fs_unlock_op(sbi);
1096 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1097 f2fs_sync_fs(sbi->sb, 1);
1100 if (new_dir_entry) {
1101 f2fs_dentry_kunmap(new_inode, new_dir_page);
1102 f2fs_put_page(new_dir_page, 0);
1105 if (old_dir_entry) {
1106 f2fs_dentry_kunmap(old_inode, old_dir_page);
1107 f2fs_put_page(old_dir_page, 0);
1110 f2fs_dentry_kunmap(new_dir, new_page);
1111 f2fs_put_page(new_page, 0);
1113 f2fs_dentry_kunmap(old_dir, old_page);
1114 f2fs_put_page(old_page, 0);
1119 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1120 struct inode *new_dir, struct dentry *new_dentry,
1125 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1128 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1133 if (flags & RENAME_EXCHANGE) {
1134 return f2fs_cross_rename(old_dir, old_dentry,
1135 new_dir, new_dentry);
1138 * VFS has already handled the new dentry existence case,
1139 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1141 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1144 static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie)
1146 struct page *cpage = NULL;
1147 char *caddr, *paddr = NULL;
1148 struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
1149 struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
1150 struct fscrypt_symlink_data *sd;
1151 struct inode *inode = d_inode(dentry);
1152 u32 max_size = inode->i_sb->s_blocksize;
1155 res = fscrypt_get_encryption_info(inode);
1157 return ERR_PTR(res);
1159 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
1161 return ERR_CAST(cpage);
1162 caddr = page_address(cpage);
1164 /* Symlink is encrypted */
1165 sd = (struct fscrypt_symlink_data *)caddr;
1166 cstr.name = sd->encrypted_path;
1167 cstr.len = le16_to_cpu(sd->len);
1169 /* this is broken symlink case */
1170 if (unlikely(cstr.len == 0)) {
1175 if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
1176 /* Symlink data on the disk is corrupted */
1180 res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
1184 res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
1188 /* this is broken symlink case */
1189 if (unlikely(pstr.name[0] == 0)) {
1196 /* Null-terminate the name */
1197 paddr[pstr.len] = '\0';
1200 return *cookie = paddr;
1202 fscrypt_fname_free_buffer(&pstr);
1204 return ERR_PTR(res);
1207 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1208 .readlink = generic_readlink,
1209 .follow_link = f2fs_encrypted_follow_link,
1210 .put_link = kfree_put_link,
1211 .getattr = f2fs_getattr,
1212 .setattr = f2fs_setattr,
1213 #ifdef CONFIG_F2FS_FS_XATTR
1214 .setxattr = generic_setxattr,
1215 .getxattr = generic_getxattr,
1216 .listxattr = f2fs_listxattr,
1217 .removexattr = generic_removexattr,
1221 const struct inode_operations f2fs_dir_inode_operations = {
1222 .create = f2fs_create,
1223 .lookup = f2fs_lookup,
1225 .unlink = f2fs_unlink,
1226 .symlink = f2fs_symlink,
1227 .mkdir = f2fs_mkdir,
1228 .rmdir = f2fs_rmdir,
1229 .mknod = f2fs_mknod,
1230 .rename2 = f2fs_rename2,
1231 .tmpfile = f2fs_tmpfile,
1232 .getattr = f2fs_getattr,
1233 .setattr = f2fs_setattr,
1234 .get_acl = f2fs_get_acl,
1235 .set_acl = f2fs_set_acl,
1236 #ifdef CONFIG_F2FS_FS_XATTR
1237 .setxattr = generic_setxattr,
1238 .getxattr = generic_getxattr,
1239 .listxattr = f2fs_listxattr,
1240 .removexattr = generic_removexattr,
1244 const struct inode_operations f2fs_symlink_inode_operations = {
1245 .readlink = generic_readlink,
1246 .follow_link = f2fs_follow_link,
1247 .put_link = page_put_link,
1248 .getattr = f2fs_getattr,
1249 .setattr = f2fs_setattr,
1250 #ifdef CONFIG_F2FS_FS_XATTR
1251 .setxattr = generic_setxattr,
1252 .getxattr = generic_getxattr,
1253 .listxattr = f2fs_listxattr,
1254 .removexattr = generic_removexattr,
1258 const struct inode_operations f2fs_special_inode_operations = {
1259 .getattr = f2fs_getattr,
1260 .setattr = f2fs_setattr,
1261 .get_acl = f2fs_get_acl,
1262 .set_acl = f2fs_set_acl,
1263 #ifdef CONFIG_F2FS_FS_XATTR
1264 .setxattr = generic_setxattr,
1265 .getxattr = generic_getxattr,
1266 .listxattr = f2fs_listxattr,
1267 .removexattr = generic_removexattr,