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;
34 inode
= new_inode(dir
->i_sb
);
36 return ERR_PTR(-ENOMEM
);
39 if (!alloc_nid(sbi
, &ino
)) {
48 inode_init_owner(inode
, dir
, mode
);
52 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
53 inode
->i_generation
= sbi
->s_next_generation
++;
55 err
= insert_inode_locked(inode
);
61 if (f2fs_sb_has_project_quota(sbi
->sb
) &&
62 (F2FS_I(dir
)->i_flags
& FS_PROJINHERIT_FL
))
63 F2FS_I(inode
)->i_projid
= F2FS_I(dir
)->i_projid
;
65 F2FS_I(inode
)->i_projid
= make_kprojid(&init_user_ns
,
68 err
= dquot_initialize(inode
);
72 err
= dquot_alloc_inode(inode
);
76 /* If the directory encrypted, then we should encrypt the inode. */
77 if (f2fs_encrypted_inode(dir
) && f2fs_may_encrypt(inode
))
78 f2fs_set_encrypted_inode(inode
);
80 set_inode_flag(inode
, FI_NEW_INODE
);
82 if (f2fs_sb_has_extra_attr(sbi
->sb
)) {
83 set_inode_flag(inode
, FI_EXTRA_ATTR
);
84 F2FS_I(inode
)->i_extra_isize
= F2FS_TOTAL_EXTRA_ATTR_SIZE
;
87 if (test_opt(sbi
, INLINE_XATTR
))
88 set_inode_flag(inode
, FI_INLINE_XATTR
);
89 if (test_opt(sbi
, INLINE_DATA
) && f2fs_may_inline_data(inode
))
90 set_inode_flag(inode
, FI_INLINE_DATA
);
91 if (f2fs_may_inline_dentry(inode
))
92 set_inode_flag(inode
, FI_INLINE_DENTRY
);
94 f2fs_init_extent_tree(inode
, NULL
);
96 stat_inc_inline_xattr(inode
);
97 stat_inc_inline_inode(inode
);
98 stat_inc_inline_dir(inode
);
100 F2FS_I(inode
)->i_flags
=
101 f2fs_mask_flags(mode
, F2FS_I(dir
)->i_flags
& F2FS_FL_INHERITED
);
103 if (S_ISDIR(inode
->i_mode
))
104 F2FS_I(inode
)->i_flags
|= FS_INDEX_FL
;
106 if (F2FS_I(inode
)->i_flags
& FS_PROJINHERIT_FL
)
107 set_inode_flag(inode
, FI_PROJ_INHERIT
);
109 trace_f2fs_new_inode(inode
, 0);
113 trace_f2fs_new_inode(inode
, err
);
114 make_bad_inode(inode
);
116 set_inode_flag(inode
, FI_FREE_NID
);
120 trace_f2fs_new_inode(inode
, err
);
122 inode
->i_flags
|= S_NOQUOTA
;
124 set_inode_flag(inode
, FI_FREE_NID
);
126 unlock_new_inode(inode
);
131 static int is_multimedia_file(const unsigned char *s
, const char *sub
)
133 size_t slen
= strlen(s
);
134 size_t sublen
= strlen(sub
);
138 * filename format of multimedia file should be defined as:
139 * "filename + '.' + extension + (optional: '.' + temp extension)".
141 if (slen
< sublen
+ 2)
144 for (i
= 1; i
< slen
- sublen
; i
++) {
147 if (!strncasecmp(s
+ i
+ 1, sub
, sublen
))
155 * Set multimedia files as cold files for hot/cold data separation
157 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
158 const unsigned char *name
)
161 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
163 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
164 for (i
= 0; i
< count
; i
++) {
165 if (is_multimedia_file(name
, extlist
[i
])) {
166 file_set_cold(inode
);
172 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
175 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
180 err
= dquot_initialize(dir
);
184 inode
= f2fs_new_inode(dir
, mode
);
186 return PTR_ERR(inode
);
188 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
189 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
191 inode
->i_op
= &f2fs_file_inode_operations
;
192 inode
->i_fop
= &f2fs_file_operations
;
193 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
197 err
= f2fs_add_link(dentry
, inode
);
202 alloc_nid_done(sbi
, ino
);
204 d_instantiate(dentry
, inode
);
205 unlock_new_inode(inode
);
208 f2fs_sync_fs(sbi
->sb
, 1);
210 f2fs_balance_fs(sbi
, true);
213 handle_failed_inode(inode
);
217 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
218 struct dentry
*dentry
)
220 struct inode
*inode
= d_inode(old_dentry
);
221 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
224 if (f2fs_encrypted_inode(dir
) &&
225 !fscrypt_has_permitted_context(dir
, inode
))
228 if (is_inode_flag_set(dir
, FI_PROJ_INHERIT
) &&
229 (!projid_eq(F2FS_I(dir
)->i_projid
,
230 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
233 err
= dquot_initialize(dir
);
237 f2fs_balance_fs(sbi
, true);
239 inode
->i_ctime
= current_time(inode
);
242 set_inode_flag(inode
, FI_INC_LINK
);
244 err
= f2fs_add_link(dentry
, inode
);
249 d_instantiate(dentry
, inode
);
252 f2fs_sync_fs(sbi
->sb
, 1);
255 clear_inode_flag(inode
, FI_INC_LINK
);
261 struct dentry
*f2fs_get_parent(struct dentry
*child
)
263 struct qstr dotdot
= QSTR_INIT("..", 2);
265 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
, &page
);
268 return ERR_CAST(page
);
269 return ERR_PTR(-ENOENT
);
271 return d_obtain_alias(f2fs_iget(child
->d_sb
, ino
));
274 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
276 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
277 struct qstr dot
= QSTR_INIT(".", 1);
278 struct qstr dotdot
= QSTR_INIT("..", 2);
279 struct f2fs_dir_entry
*de
;
283 if (f2fs_readonly(sbi
->sb
)) {
284 f2fs_msg(sbi
->sb
, KERN_INFO
,
285 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
286 "in readonly mountpoint", dir
->i_ino
, pino
);
290 err
= dquot_initialize(dir
);
294 f2fs_balance_fs(sbi
, true);
298 de
= f2fs_find_entry(dir
, &dot
, &page
);
300 f2fs_dentry_kunmap(dir
, page
);
301 f2fs_put_page(page
, 0);
302 } else if (IS_ERR(page
)) {
306 err
= __f2fs_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
311 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
313 f2fs_dentry_kunmap(dir
, page
);
314 f2fs_put_page(page
, 0);
315 } else if (IS_ERR(page
)) {
318 err
= __f2fs_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
322 clear_inode_flag(dir
, FI_INLINE_DOTS
);
328 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
331 struct inode
*inode
= NULL
;
332 struct f2fs_dir_entry
*de
;
336 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
338 if (f2fs_encrypted_inode(dir
)) {
339 int res
= fscrypt_get_encryption_info(dir
);
342 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
343 * created while the directory was encrypted and we
344 * don't have access to the key.
346 if (fscrypt_has_encryption_key(dir
))
347 fscrypt_set_encrypted_dentry(dentry
);
348 fscrypt_set_d_op(dentry
);
349 if (res
&& res
!= -ENOKEY
)
353 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
354 return ERR_PTR(-ENAMETOOLONG
);
356 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
359 return (struct dentry
*)page
;
360 return d_splice_alias(inode
, dentry
);
363 ino
= le32_to_cpu(de
->ino
);
364 f2fs_dentry_kunmap(dir
, page
);
365 f2fs_put_page(page
, 0);
367 inode
= f2fs_iget(dir
->i_sb
, ino
);
369 return ERR_CAST(inode
);
371 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
372 err
= __recover_dot_dentries(dir
, root_ino
);
377 if (f2fs_has_inline_dots(inode
)) {
378 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
382 if (f2fs_encrypted_inode(dir
) &&
383 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
384 !fscrypt_has_permitted_context(dir
, inode
)) {
385 f2fs_msg(inode
->i_sb
, KERN_WARNING
,
386 "Inconsistent encryption contexts: %lu/%lu",
387 dir
->i_ino
, inode
->i_ino
);
391 return d_splice_alias(inode
, dentry
);
398 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
400 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
401 struct inode
*inode
= d_inode(dentry
);
402 struct f2fs_dir_entry
*de
;
406 trace_f2fs_unlink_enter(dir
, dentry
);
408 err
= dquot_initialize(dir
);
412 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
419 f2fs_balance_fs(sbi
, true);
422 err
= acquire_orphan_inode(sbi
);
425 f2fs_dentry_kunmap(dir
, page
);
426 f2fs_put_page(page
, 0);
429 f2fs_delete_entry(de
, page
, dir
, inode
);
433 f2fs_sync_fs(sbi
->sb
, 1);
435 trace_f2fs_unlink_exit(inode
, err
);
439 static const char *f2fs_get_link(struct dentry
*dentry
,
441 struct delayed_call
*done
)
443 const char *link
= page_get_link(dentry
, inode
, done
);
444 if (!IS_ERR(link
) && !*link
) {
445 /* this is broken symlink case */
446 do_delayed_call(done
);
447 clear_delayed_call(done
);
448 link
= ERR_PTR(-ENOENT
);
453 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
456 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
458 size_t len
= strlen(symname
);
459 struct fscrypt_str disk_link
= FSTR_INIT((char *)symname
, len
+ 1);
460 struct fscrypt_symlink_data
*sd
= NULL
;
463 if (f2fs_encrypted_inode(dir
)) {
464 err
= fscrypt_get_encryption_info(dir
);
468 if (!fscrypt_has_encryption_key(dir
))
471 disk_link
.len
= (fscrypt_fname_encrypted_size(dir
, len
) +
472 sizeof(struct fscrypt_symlink_data
));
475 if (disk_link
.len
> dir
->i_sb
->s_blocksize
)
476 return -ENAMETOOLONG
;
478 err
= dquot_initialize(dir
);
482 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
484 return PTR_ERR(inode
);
486 if (f2fs_encrypted_inode(inode
))
487 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
489 inode
->i_op
= &f2fs_symlink_inode_operations
;
490 inode_nohighmem(inode
);
491 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
494 err
= f2fs_add_link(dentry
, inode
);
498 alloc_nid_done(sbi
, inode
->i_ino
);
500 if (f2fs_encrypted_inode(inode
)) {
501 struct qstr istr
= QSTR_INIT(symname
, len
);
502 struct fscrypt_str ostr
;
504 sd
= kzalloc(disk_link
.len
, GFP_NOFS
);
510 err
= fscrypt_get_encryption_info(inode
);
514 if (!fscrypt_has_encryption_key(inode
)) {
519 ostr
.name
= sd
->encrypted_path
;
520 ostr
.len
= disk_link
.len
;
521 err
= fscrypt_fname_usr_to_disk(inode
, &istr
, &ostr
);
525 sd
->len
= cpu_to_le16(ostr
.len
);
526 disk_link
.name
= (char *)sd
;
529 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
532 d_instantiate(dentry
, inode
);
533 unlock_new_inode(inode
);
536 * Let's flush symlink data in order to avoid broken symlink as much as
537 * possible. Nevertheless, fsyncing is the best way, but there is no
538 * way to get a file descriptor in order to flush that.
540 * Note that, it needs to do dir->fsync to make this recoverable.
541 * If the symlink path is stored into inline_data, there is no
542 * performance regression.
545 filemap_write_and_wait_range(inode
->i_mapping
, 0,
549 f2fs_sync_fs(sbi
->sb
, 1);
551 f2fs_unlink(dir
, dentry
);
556 f2fs_balance_fs(sbi
, true);
559 handle_failed_inode(inode
);
563 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
565 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
569 err
= dquot_initialize(dir
);
573 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
575 return PTR_ERR(inode
);
577 inode
->i_op
= &f2fs_dir_inode_operations
;
578 inode
->i_fop
= &f2fs_dir_operations
;
579 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
580 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_HIGH_ZERO
);
582 set_inode_flag(inode
, FI_INC_LINK
);
584 err
= f2fs_add_link(dentry
, inode
);
589 alloc_nid_done(sbi
, inode
->i_ino
);
591 d_instantiate(dentry
, inode
);
592 unlock_new_inode(inode
);
595 f2fs_sync_fs(sbi
->sb
, 1);
597 f2fs_balance_fs(sbi
, true);
601 clear_inode_flag(inode
, FI_INC_LINK
);
602 handle_failed_inode(inode
);
606 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
608 struct inode
*inode
= d_inode(dentry
);
609 if (f2fs_empty_dir(inode
))
610 return f2fs_unlink(dir
, dentry
);
614 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
615 umode_t mode
, dev_t rdev
)
617 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
621 err
= dquot_initialize(dir
);
625 inode
= f2fs_new_inode(dir
, mode
);
627 return PTR_ERR(inode
);
629 init_special_inode(inode
, inode
->i_mode
, rdev
);
630 inode
->i_op
= &f2fs_special_inode_operations
;
633 err
= f2fs_add_link(dentry
, inode
);
638 alloc_nid_done(sbi
, inode
->i_ino
);
640 d_instantiate(dentry
, inode
);
641 unlock_new_inode(inode
);
644 f2fs_sync_fs(sbi
->sb
, 1);
646 f2fs_balance_fs(sbi
, true);
649 handle_failed_inode(inode
);
653 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
654 umode_t mode
, struct inode
**whiteout
)
656 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
660 err
= dquot_initialize(dir
);
664 inode
= f2fs_new_inode(dir
, mode
);
666 return PTR_ERR(inode
);
669 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
670 inode
->i_op
= &f2fs_special_inode_operations
;
672 inode
->i_op
= &f2fs_file_inode_operations
;
673 inode
->i_fop
= &f2fs_file_operations
;
674 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
678 err
= acquire_orphan_inode(sbi
);
682 err
= f2fs_do_tmpfile(inode
, dir
);
687 * add this non-linked tmpfile to orphan list, in this way we could
688 * remove all unused data of tmpfile after abnormal power-off.
690 add_orphan_inode(inode
);
691 alloc_nid_done(sbi
, inode
->i_ino
);
694 f2fs_i_links_write(inode
, false);
697 d_tmpfile(dentry
, inode
);
699 /* link_count was changed by d_tmpfile as well. */
701 unlock_new_inode(inode
);
703 f2fs_balance_fs(sbi
, true);
707 release_orphan_inode(sbi
);
709 handle_failed_inode(inode
);
713 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
715 if (f2fs_encrypted_inode(dir
)) {
716 int err
= fscrypt_get_encryption_info(dir
);
721 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
724 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
726 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
729 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
730 struct inode
*new_dir
, struct dentry
*new_dentry
,
733 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
734 struct inode
*old_inode
= d_inode(old_dentry
);
735 struct inode
*new_inode
= d_inode(new_dentry
);
736 struct inode
*whiteout
= NULL
;
737 struct page
*old_dir_page
;
738 struct page
*old_page
, *new_page
= NULL
;
739 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
740 struct f2fs_dir_entry
*old_entry
;
741 struct f2fs_dir_entry
*new_entry
;
742 bool is_old_inline
= f2fs_has_inline_dentry(old_dir
);
745 if ((f2fs_encrypted_inode(old_dir
) &&
746 !fscrypt_has_encryption_key(old_dir
)) ||
747 (f2fs_encrypted_inode(new_dir
) &&
748 !fscrypt_has_encryption_key(new_dir
)))
751 if ((old_dir
!= new_dir
) && f2fs_encrypted_inode(new_dir
) &&
752 !fscrypt_has_permitted_context(new_dir
, old_inode
)) {
757 if (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
758 (!projid_eq(F2FS_I(new_dir
)->i_projid
,
759 F2FS_I(old_dentry
->d_inode
)->i_projid
)))
762 err
= dquot_initialize(old_dir
);
766 err
= dquot_initialize(new_dir
);
770 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
772 if (IS_ERR(old_page
))
773 err
= PTR_ERR(old_page
);
777 if (S_ISDIR(old_inode
->i_mode
)) {
778 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
779 if (!old_dir_entry
) {
780 if (IS_ERR(old_dir_page
))
781 err
= PTR_ERR(old_dir_page
);
786 if (flags
& RENAME_WHITEOUT
) {
787 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
795 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
799 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
802 if (IS_ERR(new_page
))
803 err
= PTR_ERR(new_page
);
807 f2fs_balance_fs(sbi
, true);
811 err
= acquire_orphan_inode(sbi
);
815 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
817 new_inode
->i_ctime
= current_time(new_inode
);
818 down_write(&F2FS_I(new_inode
)->i_sem
);
820 f2fs_i_links_write(new_inode
, false);
821 f2fs_i_links_write(new_inode
, false);
822 up_write(&F2FS_I(new_inode
)->i_sem
);
824 if (!new_inode
->i_nlink
)
825 add_orphan_inode(new_inode
);
827 release_orphan_inode(sbi
);
829 f2fs_balance_fs(sbi
, true);
833 err
= f2fs_add_link(new_dentry
, old_inode
);
840 f2fs_i_links_write(new_dir
, true);
843 * old entry and new entry can locate in the same inline
844 * dentry in inode, when attaching new entry in inline dentry,
845 * it could force inline dentry conversion, after that,
846 * old_entry and old_page will point to wrong address, in
847 * order to avoid this, let's do the check and update here.
849 if (is_old_inline
&& !f2fs_has_inline_dentry(old_dir
)) {
850 f2fs_put_page(old_page
, 0);
853 old_entry
= f2fs_find_entry(old_dir
,
854 &old_dentry
->d_name
, &old_page
);
857 if (IS_ERR(old_page
))
858 err
= PTR_ERR(old_page
);
865 down_write(&F2FS_I(old_inode
)->i_sem
);
866 if (!old_dir_entry
|| whiteout
)
867 file_lost_pino(old_inode
);
869 F2FS_I(old_inode
)->i_pino
= new_dir
->i_ino
;
870 up_write(&F2FS_I(old_inode
)->i_sem
);
872 old_inode
->i_ctime
= current_time(old_inode
);
873 f2fs_mark_inode_dirty_sync(old_inode
, false);
875 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
878 whiteout
->i_state
|= I_LINKABLE
;
879 set_inode_flag(whiteout
, FI_INC_LINK
);
880 err
= f2fs_add_link(old_dentry
, whiteout
);
883 whiteout
->i_state
&= ~I_LINKABLE
;
888 if (old_dir
!= new_dir
&& !whiteout
) {
889 f2fs_set_link(old_inode
, old_dir_entry
,
890 old_dir_page
, new_dir
);
892 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
893 f2fs_put_page(old_dir_page
, 0);
895 f2fs_i_links_write(old_dir
, false);
900 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
901 f2fs_sync_fs(sbi
->sb
, 1);
907 f2fs_dentry_kunmap(new_dir
, new_page
);
908 f2fs_put_page(new_page
, 0);
915 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
916 f2fs_put_page(old_dir_page
, 0);
919 f2fs_dentry_kunmap(old_dir
, old_page
);
920 f2fs_put_page(old_page
, 0);
925 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
926 struct inode
*new_dir
, struct dentry
*new_dentry
)
928 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
929 struct inode
*old_inode
= d_inode(old_dentry
);
930 struct inode
*new_inode
= d_inode(new_dentry
);
931 struct page
*old_dir_page
, *new_dir_page
;
932 struct page
*old_page
, *new_page
;
933 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
934 struct f2fs_dir_entry
*old_entry
, *new_entry
;
935 int old_nlink
= 0, new_nlink
= 0;
938 if ((f2fs_encrypted_inode(old_dir
) &&
939 !fscrypt_has_encryption_key(old_dir
)) ||
940 (f2fs_encrypted_inode(new_dir
) &&
941 !fscrypt_has_encryption_key(new_dir
)))
944 if ((f2fs_encrypted_inode(old_dir
) || f2fs_encrypted_inode(new_dir
)) &&
945 (old_dir
!= new_dir
) &&
946 (!fscrypt_has_permitted_context(new_dir
, old_inode
) ||
947 !fscrypt_has_permitted_context(old_dir
, new_inode
)))
950 if ((is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
951 !projid_eq(F2FS_I(new_dir
)->i_projid
,
952 F2FS_I(old_dentry
->d_inode
)->i_projid
)) ||
953 (is_inode_flag_set(new_dir
, FI_PROJ_INHERIT
) &&
954 !projid_eq(F2FS_I(old_dir
)->i_projid
,
955 F2FS_I(new_dentry
->d_inode
)->i_projid
)))
958 err
= dquot_initialize(old_dir
);
962 err
= dquot_initialize(new_dir
);
966 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
968 if (IS_ERR(old_page
))
969 err
= PTR_ERR(old_page
);
973 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
975 if (IS_ERR(new_page
))
976 err
= PTR_ERR(new_page
);
980 /* prepare for updating ".." directory entry info later */
981 if (old_dir
!= new_dir
) {
982 if (S_ISDIR(old_inode
->i_mode
)) {
983 old_dir_entry
= f2fs_parent_dir(old_inode
,
985 if (!old_dir_entry
) {
986 if (IS_ERR(old_dir_page
))
987 err
= PTR_ERR(old_dir_page
);
992 if (S_ISDIR(new_inode
->i_mode
)) {
993 new_dir_entry
= f2fs_parent_dir(new_inode
,
995 if (!new_dir_entry
) {
996 if (IS_ERR(new_dir_page
))
997 err
= PTR_ERR(new_dir_page
);
1004 * If cross rename between file and directory those are not
1005 * in the same directory, we will inc nlink of file's parent
1006 * later, so we should check upper boundary of its nlink.
1008 if ((!old_dir_entry
|| !new_dir_entry
) &&
1009 old_dir_entry
!= new_dir_entry
) {
1010 old_nlink
= old_dir_entry
? -1 : 1;
1011 new_nlink
= -old_nlink
;
1013 if ((old_nlink
> 0 && old_dir
->i_nlink
>= F2FS_LINK_MAX
) ||
1014 (new_nlink
> 0 && new_dir
->i_nlink
>= F2FS_LINK_MAX
))
1018 f2fs_balance_fs(sbi
, true);
1022 /* update ".." directory entry info of old dentry */
1024 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
1026 /* update ".." directory entry info of new dentry */
1028 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
1030 /* update directory entry info of old dir inode */
1031 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
1033 down_write(&F2FS_I(old_inode
)->i_sem
);
1034 file_lost_pino(old_inode
);
1035 up_write(&F2FS_I(old_inode
)->i_sem
);
1037 old_dir
->i_ctime
= current_time(old_dir
);
1039 down_write(&F2FS_I(old_dir
)->i_sem
);
1040 f2fs_i_links_write(old_dir
, old_nlink
> 0);
1041 up_write(&F2FS_I(old_dir
)->i_sem
);
1043 f2fs_mark_inode_dirty_sync(old_dir
, false);
1045 /* update directory entry info of new dir inode */
1046 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
1048 down_write(&F2FS_I(new_inode
)->i_sem
);
1049 file_lost_pino(new_inode
);
1050 up_write(&F2FS_I(new_inode
)->i_sem
);
1052 new_dir
->i_ctime
= current_time(new_dir
);
1054 down_write(&F2FS_I(new_dir
)->i_sem
);
1055 f2fs_i_links_write(new_dir
, new_nlink
> 0);
1056 up_write(&F2FS_I(new_dir
)->i_sem
);
1058 f2fs_mark_inode_dirty_sync(new_dir
, false);
1060 f2fs_unlock_op(sbi
);
1062 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
1063 f2fs_sync_fs(sbi
->sb
, 1);
1066 if (new_dir_entry
) {
1067 f2fs_dentry_kunmap(new_inode
, new_dir_page
);
1068 f2fs_put_page(new_dir_page
, 0);
1071 if (old_dir_entry
) {
1072 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
1073 f2fs_put_page(old_dir_page
, 0);
1076 f2fs_dentry_kunmap(new_dir
, new_page
);
1077 f2fs_put_page(new_page
, 0);
1079 f2fs_dentry_kunmap(old_dir
, old_page
);
1080 f2fs_put_page(old_page
, 0);
1085 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
1086 struct inode
*new_dir
, struct dentry
*new_dentry
,
1089 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
1092 if (flags
& RENAME_EXCHANGE
) {
1093 return f2fs_cross_rename(old_dir
, old_dentry
,
1094 new_dir
, new_dentry
);
1097 * VFS has already handled the new dentry existence case,
1098 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1100 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1103 static const char *f2fs_encrypted_get_link(struct dentry
*dentry
,
1104 struct inode
*inode
,
1105 struct delayed_call
*done
)
1107 struct page
*cpage
= NULL
;
1108 char *caddr
, *paddr
= NULL
;
1109 struct fscrypt_str cstr
= FSTR_INIT(NULL
, 0);
1110 struct fscrypt_str pstr
= FSTR_INIT(NULL
, 0);
1111 struct fscrypt_symlink_data
*sd
;
1112 u32 max_size
= inode
->i_sb
->s_blocksize
;
1116 return ERR_PTR(-ECHILD
);
1118 res
= fscrypt_get_encryption_info(inode
);
1120 return ERR_PTR(res
);
1122 cpage
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1124 return ERR_CAST(cpage
);
1125 caddr
= page_address(cpage
);
1127 /* Symlink is encrypted */
1128 sd
= (struct fscrypt_symlink_data
*)caddr
;
1129 cstr
.name
= sd
->encrypted_path
;
1130 cstr
.len
= le16_to_cpu(sd
->len
);
1132 /* this is broken symlink case */
1133 if (unlikely(cstr
.len
== 0)) {
1138 if ((cstr
.len
+ sizeof(struct fscrypt_symlink_data
) - 1) > max_size
) {
1139 /* Symlink data on the disk is corrupted */
1143 res
= fscrypt_fname_alloc_buffer(inode
, cstr
.len
, &pstr
);
1147 res
= fscrypt_fname_disk_to_usr(inode
, 0, 0, &cstr
, &pstr
);
1151 /* this is broken symlink case */
1152 if (unlikely(pstr
.name
[0] == 0)) {
1159 /* Null-terminate the name */
1160 paddr
[pstr
.len
] = '\0';
1163 set_delayed_call(done
, kfree_link
, paddr
);
1166 fscrypt_fname_free_buffer(&pstr
);
1168 return ERR_PTR(res
);
1171 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1172 .get_link
= f2fs_encrypted_get_link
,
1173 .getattr
= f2fs_getattr
,
1174 .setattr
= f2fs_setattr
,
1175 #ifdef CONFIG_F2FS_FS_XATTR
1176 .listxattr
= f2fs_listxattr
,
1180 const struct inode_operations f2fs_dir_inode_operations
= {
1181 .create
= f2fs_create
,
1182 .lookup
= f2fs_lookup
,
1184 .unlink
= f2fs_unlink
,
1185 .symlink
= f2fs_symlink
,
1186 .mkdir
= f2fs_mkdir
,
1187 .rmdir
= f2fs_rmdir
,
1188 .mknod
= f2fs_mknod
,
1189 .rename
= f2fs_rename2
,
1190 .tmpfile
= f2fs_tmpfile
,
1191 .getattr
= f2fs_getattr
,
1192 .setattr
= f2fs_setattr
,
1193 .get_acl
= f2fs_get_acl
,
1194 .set_acl
= f2fs_set_acl
,
1195 #ifdef CONFIG_F2FS_FS_XATTR
1196 .listxattr
= f2fs_listxattr
,
1200 const struct inode_operations f2fs_symlink_inode_operations
= {
1201 .get_link
= f2fs_get_link
,
1202 .getattr
= f2fs_getattr
,
1203 .setattr
= f2fs_setattr
,
1204 #ifdef CONFIG_F2FS_FS_XATTR
1205 .listxattr
= f2fs_listxattr
,
1209 const struct inode_operations f2fs_special_inode_operations
= {
1210 .getattr
= f2fs_getattr
,
1211 .setattr
= f2fs_setattr
,
1212 .get_acl
= f2fs_get_acl
,
1213 .set_acl
= f2fs_set_acl
,
1214 #ifdef CONFIG_F2FS_FS_XATTR
1215 .listxattr
= f2fs_listxattr
,