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_put_page(page, 0);
321 } else if (IS_ERR(page)) {
325 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
330 de = f2fs_find_entry(dir, &dotdot, &page);
332 f2fs_put_page(page, 0);
333 else if (IS_ERR(page))
336 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
339 clear_inode_flag(dir, FI_INLINE_DOTS);
345 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
348 struct inode *inode = NULL;
349 struct f2fs_dir_entry *de;
354 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
356 trace_f2fs_lookup_start(dir, dentry, flags);
358 err = fscrypt_prepare_lookup(dir, dentry, flags);
362 if (dentry->d_name.len > F2FS_NAME_LEN) {
367 de = f2fs_find_entry(dir, &dentry->d_name, &page);
376 ino = le32_to_cpu(de->ino);
377 f2fs_put_page(page, 0);
379 inode = f2fs_iget(dir->i_sb, ino);
381 err = PTR_ERR(inode);
385 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
386 err = __recover_dot_dentries(dir, root_ino);
391 if (f2fs_has_inline_dots(inode)) {
392 err = __recover_dot_dentries(inode, dir->i_ino);
396 if (f2fs_encrypted_inode(dir) &&
397 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
398 !fscrypt_has_permitted_context(dir, inode)) {
399 f2fs_msg(inode->i_sb, KERN_WARNING,
400 "Inconsistent encryption contexts: %lu/%lu",
401 dir->i_ino, inode->i_ino);
406 new = d_splice_alias(inode, dentry);
409 trace_f2fs_lookup_end(dir, dentry, ino, err);
414 trace_f2fs_lookup_end(dir, dentry, ino, err);
418 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
420 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
421 struct inode *inode = d_inode(dentry);
422 struct f2fs_dir_entry *de;
426 trace_f2fs_unlink_enter(dir, dentry);
428 if (unlikely(f2fs_cp_error(sbi)))
431 err = dquot_initialize(dir);
434 err = dquot_initialize(inode);
438 de = f2fs_find_entry(dir, &dentry->d_name, &page);
445 f2fs_balance_fs(sbi, true);
448 err = acquire_orphan_inode(sbi);
451 f2fs_put_page(page, 0);
454 f2fs_delete_entry(de, page, dir, inode);
458 f2fs_sync_fs(sbi->sb, 1);
460 trace_f2fs_unlink_exit(inode, err);
464 static const char *f2fs_follow_link(struct dentry *dentry, void **cookie)
466 const char *link = page_follow_link_light(dentry, cookie);
467 if (!IS_ERR(link) && !*link) {
468 /* this is broken symlink case */
469 page_put_link(NULL, *cookie);
470 link = ERR_PTR(-ENOENT);
475 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
478 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
480 size_t len = strlen(symname);
481 struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
482 struct fscrypt_symlink_data *sd = NULL;
485 if (unlikely(f2fs_cp_error(sbi)))
488 if (f2fs_encrypted_inode(dir)) {
489 err = fscrypt_get_encryption_info(dir);
493 if (!fscrypt_has_encryption_key(dir))
496 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
497 sizeof(struct fscrypt_symlink_data));
500 if (disk_link.len > dir->i_sb->s_blocksize)
501 return -ENAMETOOLONG;
503 err = dquot_initialize(dir);
507 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
509 return PTR_ERR(inode);
511 if (f2fs_encrypted_inode(inode))
512 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
514 inode->i_op = &f2fs_symlink_inode_operations;
515 inode_nohighmem(inode);
516 inode->i_mapping->a_ops = &f2fs_dblock_aops;
519 err = f2fs_add_link(dentry, inode);
523 alloc_nid_done(sbi, inode->i_ino);
525 if (f2fs_encrypted_inode(inode)) {
526 struct qstr istr = QSTR_INIT(symname, len);
527 struct fscrypt_str ostr;
529 sd = f2fs_kzalloc(sbi, disk_link.len, GFP_NOFS);
535 err = fscrypt_get_encryption_info(inode);
539 if (!fscrypt_has_encryption_key(inode)) {
544 ostr.name = sd->encrypted_path;
545 ostr.len = disk_link.len;
546 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
550 sd->len = cpu_to_le16(ostr.len);
551 disk_link.name = (char *)sd;
554 err = page_symlink(inode, disk_link.name, disk_link.len);
557 d_instantiate(dentry, inode);
558 unlock_new_inode(inode);
561 * Let's flush symlink data in order to avoid broken symlink as much as
562 * possible. Nevertheless, fsyncing is the best way, but there is no
563 * way to get a file descriptor in order to flush that.
565 * Note that, it needs to do dir->fsync to make this recoverable.
566 * If the symlink path is stored into inline_data, there is no
567 * performance regression.
570 filemap_write_and_wait_range(inode->i_mapping, 0,
574 f2fs_sync_fs(sbi->sb, 1);
576 f2fs_unlink(dir, dentry);
581 f2fs_balance_fs(sbi, true);
584 handle_failed_inode(inode);
588 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
590 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
594 if (unlikely(f2fs_cp_error(sbi)))
597 err = dquot_initialize(dir);
601 inode = f2fs_new_inode(dir, S_IFDIR | mode);
603 return PTR_ERR(inode);
605 inode->i_op = &f2fs_dir_inode_operations;
606 inode->i_fop = &f2fs_dir_operations;
607 inode->i_mapping->a_ops = &f2fs_dblock_aops;
608 inode_nohighmem(inode);
610 set_inode_flag(inode, FI_INC_LINK);
612 err = f2fs_add_link(dentry, inode);
617 alloc_nid_done(sbi, inode->i_ino);
619 d_instantiate(dentry, inode);
620 unlock_new_inode(inode);
623 f2fs_sync_fs(sbi->sb, 1);
625 f2fs_balance_fs(sbi, true);
629 clear_inode_flag(inode, FI_INC_LINK);
630 handle_failed_inode(inode);
634 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
636 struct inode *inode = d_inode(dentry);
637 if (f2fs_empty_dir(inode))
638 return f2fs_unlink(dir, dentry);
642 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
643 umode_t mode, dev_t rdev)
645 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
649 if (unlikely(f2fs_cp_error(sbi)))
652 err = dquot_initialize(dir);
656 inode = f2fs_new_inode(dir, mode);
658 return PTR_ERR(inode);
660 init_special_inode(inode, inode->i_mode, rdev);
661 inode->i_op = &f2fs_special_inode_operations;
664 err = f2fs_add_link(dentry, inode);
669 alloc_nid_done(sbi, inode->i_ino);
671 d_instantiate(dentry, inode);
672 unlock_new_inode(inode);
675 f2fs_sync_fs(sbi->sb, 1);
677 f2fs_balance_fs(sbi, true);
680 handle_failed_inode(inode);
684 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
685 umode_t mode, struct inode **whiteout)
687 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
691 err = dquot_initialize(dir);
695 inode = f2fs_new_inode(dir, mode);
697 return PTR_ERR(inode);
700 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
701 inode->i_op = &f2fs_special_inode_operations;
703 inode->i_op = &f2fs_file_inode_operations;
704 inode->i_fop = &f2fs_file_operations;
705 inode->i_mapping->a_ops = &f2fs_dblock_aops;
709 err = acquire_orphan_inode(sbi);
713 err = f2fs_do_tmpfile(inode, dir);
718 * add this non-linked tmpfile to orphan list, in this way we could
719 * remove all unused data of tmpfile after abnormal power-off.
721 add_orphan_inode(inode);
722 alloc_nid_done(sbi, inode->i_ino);
725 f2fs_i_links_write(inode, false);
728 d_tmpfile(dentry, inode);
730 /* link_count was changed by d_tmpfile as well. */
732 unlock_new_inode(inode);
734 f2fs_balance_fs(sbi, true);
738 release_orphan_inode(sbi);
740 handle_failed_inode(inode);
744 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
746 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
749 if (f2fs_encrypted_inode(dir)) {
750 int err = fscrypt_get_encryption_info(dir);
755 return __f2fs_tmpfile(dir, dentry, mode, NULL);
758 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
760 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
763 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
766 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
767 struct inode *new_dir, struct dentry *new_dentry,
770 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
771 struct inode *old_inode = d_inode(old_dentry);
772 struct inode *new_inode = d_inode(new_dentry);
773 struct inode *whiteout = NULL;
774 struct page *old_dir_page;
775 struct page *old_page, *new_page = NULL;
776 struct f2fs_dir_entry *old_dir_entry = NULL;
777 struct f2fs_dir_entry *old_entry;
778 struct f2fs_dir_entry *new_entry;
779 bool is_old_inline = f2fs_has_inline_dentry(old_dir);
782 if (unlikely(f2fs_cp_error(sbi)))
785 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
786 (!projid_eq(F2FS_I(new_dir)->i_projid,
787 F2FS_I(old_dentry->d_inode)->i_projid)))
790 err = dquot_initialize(old_dir);
794 err = dquot_initialize(new_dir);
799 err = dquot_initialize(new_inode);
804 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
806 if (IS_ERR(old_page))
807 err = PTR_ERR(old_page);
811 if (S_ISDIR(old_inode->i_mode)) {
812 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
813 if (!old_dir_entry) {
814 if (IS_ERR(old_dir_page))
815 err = PTR_ERR(old_dir_page);
820 if (flags & RENAME_WHITEOUT) {
821 err = f2fs_create_whiteout(old_dir, &whiteout);
829 if (old_dir_entry && !f2fs_empty_dir(new_inode))
833 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
836 if (IS_ERR(new_page))
837 err = PTR_ERR(new_page);
841 f2fs_balance_fs(sbi, true);
845 err = acquire_orphan_inode(sbi);
849 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
851 new_inode->i_ctime = current_time(new_inode);
852 down_write(&F2FS_I(new_inode)->i_sem);
854 f2fs_i_links_write(new_inode, false);
855 f2fs_i_links_write(new_inode, false);
856 up_write(&F2FS_I(new_inode)->i_sem);
858 if (!new_inode->i_nlink)
859 add_orphan_inode(new_inode);
861 release_orphan_inode(sbi);
863 f2fs_balance_fs(sbi, true);
867 err = f2fs_add_link(new_dentry, old_inode);
874 f2fs_i_links_write(new_dir, true);
877 * old entry and new entry can locate in the same inline
878 * dentry in inode, when attaching new entry in inline dentry,
879 * it could force inline dentry conversion, after that,
880 * old_entry and old_page will point to wrong address, in
881 * order to avoid this, let's do the check and update here.
883 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
884 f2fs_put_page(old_page, 0);
887 old_entry = f2fs_find_entry(old_dir,
888 &old_dentry->d_name, &old_page);
891 if (IS_ERR(old_page))
892 err = PTR_ERR(old_page);
899 down_write(&F2FS_I(old_inode)->i_sem);
900 if (!old_dir_entry || whiteout)
901 file_lost_pino(old_inode);
903 F2FS_I(old_inode)->i_pino = new_dir->i_ino;
904 up_write(&F2FS_I(old_inode)->i_sem);
906 old_inode->i_ctime = current_time(old_inode);
907 f2fs_mark_inode_dirty_sync(old_inode, false);
909 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
912 whiteout->i_state |= I_LINKABLE;
913 set_inode_flag(whiteout, FI_INC_LINK);
914 err = f2fs_add_link(old_dentry, whiteout);
917 whiteout->i_state &= ~I_LINKABLE;
922 if (old_dir != new_dir && !whiteout)
923 f2fs_set_link(old_inode, old_dir_entry,
924 old_dir_page, new_dir);
926 f2fs_put_page(old_dir_page, 0);
927 f2fs_i_links_write(old_dir, false);
929 add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
933 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
934 f2fs_sync_fs(sbi->sb, 1);
940 f2fs_put_page(new_page, 0);
946 f2fs_put_page(old_dir_page, 0);
948 f2fs_put_page(old_page, 0);
953 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
954 struct inode *new_dir, struct dentry *new_dentry)
956 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
957 struct inode *old_inode = d_inode(old_dentry);
958 struct inode *new_inode = d_inode(new_dentry);
959 struct page *old_dir_page, *new_dir_page;
960 struct page *old_page, *new_page;
961 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
962 struct f2fs_dir_entry *old_entry, *new_entry;
963 int old_nlink = 0, new_nlink = 0;
966 if (unlikely(f2fs_cp_error(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)) ||
972 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
973 !projid_eq(F2FS_I(old_dir)->i_projid,
974 F2FS_I(new_dentry->d_inode)->i_projid)))
977 err = dquot_initialize(old_dir);
981 err = dquot_initialize(new_dir);
985 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
987 if (IS_ERR(old_page))
988 err = PTR_ERR(old_page);
992 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
994 if (IS_ERR(new_page))
995 err = PTR_ERR(new_page);
999 /* prepare for updating ".." directory entry info later */
1000 if (old_dir != new_dir) {
1001 if (S_ISDIR(old_inode->i_mode)) {
1002 old_dir_entry = f2fs_parent_dir(old_inode,
1004 if (!old_dir_entry) {
1005 if (IS_ERR(old_dir_page))
1006 err = PTR_ERR(old_dir_page);
1011 if (S_ISDIR(new_inode->i_mode)) {
1012 new_dir_entry = f2fs_parent_dir(new_inode,
1014 if (!new_dir_entry) {
1015 if (IS_ERR(new_dir_page))
1016 err = PTR_ERR(new_dir_page);
1023 * If cross rename between file and directory those are not
1024 * in the same directory, we will inc nlink of file's parent
1025 * later, so we should check upper boundary of its nlink.
1027 if ((!old_dir_entry || !new_dir_entry) &&
1028 old_dir_entry != new_dir_entry) {
1029 old_nlink = old_dir_entry ? -1 : 1;
1030 new_nlink = -old_nlink;
1032 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1033 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1037 f2fs_balance_fs(sbi, true);
1041 /* update ".." directory entry info of old dentry */
1043 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1045 /* update ".." directory entry info of new dentry */
1047 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1049 /* update directory entry info of old dir inode */
1050 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1052 down_write(&F2FS_I(old_inode)->i_sem);
1053 file_lost_pino(old_inode);
1054 up_write(&F2FS_I(old_inode)->i_sem);
1056 old_dir->i_ctime = current_time(old_dir);
1058 down_write(&F2FS_I(old_dir)->i_sem);
1059 f2fs_i_links_write(old_dir, old_nlink > 0);
1060 up_write(&F2FS_I(old_dir)->i_sem);
1062 f2fs_mark_inode_dirty_sync(old_dir, false);
1064 /* update directory entry info of new dir inode */
1065 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1067 down_write(&F2FS_I(new_inode)->i_sem);
1068 file_lost_pino(new_inode);
1069 up_write(&F2FS_I(new_inode)->i_sem);
1071 new_dir->i_ctime = current_time(new_dir);
1073 down_write(&F2FS_I(new_dir)->i_sem);
1074 f2fs_i_links_write(new_dir, new_nlink > 0);
1075 up_write(&F2FS_I(new_dir)->i_sem);
1077 f2fs_mark_inode_dirty_sync(new_dir, false);
1079 add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1080 add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1082 f2fs_unlock_op(sbi);
1084 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1085 f2fs_sync_fs(sbi->sb, 1);
1088 if (new_dir_entry) {
1089 f2fs_put_page(new_dir_page, 0);
1092 if (old_dir_entry) {
1093 f2fs_put_page(old_dir_page, 0);
1096 f2fs_put_page(new_page, 0);
1098 f2fs_put_page(old_page, 0);
1103 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1104 struct inode *new_dir, struct dentry *new_dentry,
1109 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1112 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1117 if (flags & RENAME_EXCHANGE) {
1118 return f2fs_cross_rename(old_dir, old_dentry,
1119 new_dir, new_dentry);
1122 * VFS has already handled the new dentry existence case,
1123 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1125 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1128 static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie)
1130 struct page *cpage = NULL;
1131 char *caddr, *paddr = NULL;
1132 struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
1133 struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
1134 struct fscrypt_symlink_data *sd;
1135 struct inode *inode = d_inode(dentry);
1136 u32 max_size = inode->i_sb->s_blocksize;
1139 res = fscrypt_get_encryption_info(inode);
1141 return ERR_PTR(res);
1143 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
1145 return ERR_CAST(cpage);
1146 caddr = page_address(cpage);
1148 /* Symlink is encrypted */
1149 sd = (struct fscrypt_symlink_data *)caddr;
1150 cstr.name = sd->encrypted_path;
1151 cstr.len = le16_to_cpu(sd->len);
1153 /* this is broken symlink case */
1154 if (unlikely(cstr.len == 0)) {
1159 if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
1160 /* Symlink data on the disk is corrupted */
1164 res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
1168 res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
1172 /* this is broken symlink case */
1173 if (unlikely(pstr.name[0] == 0)) {
1180 /* Null-terminate the name */
1181 paddr[pstr.len] = '\0';
1184 return *cookie = paddr;
1186 fscrypt_fname_free_buffer(&pstr);
1188 return ERR_PTR(res);
1191 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1192 .readlink = generic_readlink,
1193 .follow_link = f2fs_encrypted_follow_link,
1194 .put_link = kfree_put_link,
1195 .getattr = f2fs_getattr,
1196 .setattr = f2fs_setattr,
1197 #ifdef CONFIG_F2FS_FS_XATTR
1198 .setxattr = generic_setxattr,
1199 .getxattr = generic_getxattr,
1200 .listxattr = f2fs_listxattr,
1201 .removexattr = generic_removexattr,
1205 const struct inode_operations f2fs_dir_inode_operations = {
1206 .create = f2fs_create,
1207 .lookup = f2fs_lookup,
1209 .unlink = f2fs_unlink,
1210 .symlink = f2fs_symlink,
1211 .mkdir = f2fs_mkdir,
1212 .rmdir = f2fs_rmdir,
1213 .mknod = f2fs_mknod,
1214 .rename2 = f2fs_rename2,
1215 .tmpfile = f2fs_tmpfile,
1216 .getattr = f2fs_getattr,
1217 .setattr = f2fs_setattr,
1218 .get_acl = f2fs_get_acl,
1219 .set_acl = f2fs_set_acl,
1220 #ifdef CONFIG_F2FS_FS_XATTR
1221 .setxattr = generic_setxattr,
1222 .getxattr = generic_getxattr,
1223 .listxattr = f2fs_listxattr,
1224 .removexattr = generic_removexattr,
1228 const struct inode_operations f2fs_symlink_inode_operations = {
1229 .readlink = generic_readlink,
1230 .follow_link = f2fs_follow_link,
1231 .put_link = page_put_link,
1232 .getattr = f2fs_getattr,
1233 .setattr = f2fs_setattr,
1234 #ifdef CONFIG_F2FS_FS_XATTR
1235 .setxattr = generic_setxattr,
1236 .getxattr = generic_getxattr,
1237 .listxattr = f2fs_listxattr,
1238 .removexattr = generic_removexattr,
1242 const struct inode_operations f2fs_special_inode_operations = {
1243 .getattr = f2fs_getattr,
1244 .setattr = f2fs_setattr,
1245 .get_acl = f2fs_get_acl,
1246 .set_acl = f2fs_set_acl,
1247 #ifdef CONFIG_F2FS_FS_XATTR
1248 .setxattr = generic_setxattr,
1249 .getxattr = generic_getxattr,
1250 .listxattr = f2fs_listxattr,
1251 .removexattr = generic_removexattr,