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>
23 #include <trace/events/f2fs.h>
25 static struct inode
*f2fs_new_inode(struct inode
*dir
, umode_t mode
)
27 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
30 bool nid_free
= false;
33 inode
= new_inode(dir
->i_sb
);
35 return ERR_PTR(-ENOMEM
);
38 if (!alloc_nid(sbi
, &ino
)) {
45 inode_init_owner(inode
, dir
, mode
);
49 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
50 inode
->i_generation
= sbi
->s_next_generation
++;
52 err
= insert_inode_locked(inode
);
59 /* If the directory encrypted, then we should encrypt the inode. */
60 if (f2fs_encrypted_inode(dir
) && f2fs_may_encrypt(inode
))
61 f2fs_set_encrypted_inode(inode
);
63 set_inode_flag(inode
, FI_NEW_INODE
);
65 if (test_opt(sbi
, INLINE_XATTR
))
66 set_inode_flag(inode
, FI_INLINE_XATTR
);
67 if (test_opt(sbi
, INLINE_DATA
) && f2fs_may_inline_data(inode
))
68 set_inode_flag(inode
, FI_INLINE_DATA
);
69 if (f2fs_may_inline_dentry(inode
))
70 set_inode_flag(inode
, FI_INLINE_DENTRY
);
72 f2fs_init_extent_tree(inode
, NULL
);
74 stat_inc_inline_xattr(inode
);
75 stat_inc_inline_inode(inode
);
76 stat_inc_inline_dir(inode
);
78 trace_f2fs_new_inode(inode
, 0);
82 trace_f2fs_new_inode(inode
, err
);
83 make_bad_inode(inode
);
85 set_inode_flag(inode
, FI_FREE_NID
);
90 static int is_multimedia_file(const unsigned char *s
, const char *sub
)
92 size_t slen
= strlen(s
);
93 size_t sublen
= strlen(sub
);
96 * filename format of multimedia file should be defined as:
97 * "filename + '.' + extension".
99 if (slen
< sublen
+ 2)
102 if (s
[slen
- sublen
- 1] != '.')
105 return !strncasecmp(s
+ slen
- sublen
, sub
, sublen
);
109 * Set multimedia files as cold files for hot/cold data separation
111 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
112 const unsigned char *name
)
115 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
117 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
118 for (i
= 0; i
< count
; i
++) {
119 if (is_multimedia_file(name
, extlist
[i
])) {
120 file_set_cold(inode
);
126 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
129 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
134 inode
= f2fs_new_inode(dir
, mode
);
136 return PTR_ERR(inode
);
138 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
139 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
141 inode
->i_op
= &f2fs_file_inode_operations
;
142 inode
->i_fop
= &f2fs_file_operations
;
143 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
146 f2fs_balance_fs(sbi
, true);
149 err
= f2fs_add_link(dentry
, inode
);
154 alloc_nid_done(sbi
, ino
);
156 d_instantiate(dentry
, inode
);
157 unlock_new_inode(inode
);
160 f2fs_sync_fs(sbi
->sb
, 1);
163 handle_failed_inode(inode
);
167 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
168 struct dentry
*dentry
)
170 struct inode
*inode
= d_inode(old_dentry
);
171 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
174 if (f2fs_encrypted_inode(dir
) &&
175 !fscrypt_has_permitted_context(dir
, inode
))
178 f2fs_balance_fs(sbi
, true);
180 inode
->i_ctime
= CURRENT_TIME
;
183 set_inode_flag(inode
, FI_INC_LINK
);
185 err
= f2fs_add_link(dentry
, inode
);
190 d_instantiate(dentry
, inode
);
193 f2fs_sync_fs(sbi
->sb
, 1);
196 clear_inode_flag(inode
, FI_INC_LINK
);
202 struct dentry
*f2fs_get_parent(struct dentry
*child
)
204 struct qstr dotdot
= QSTR_INIT("..", 2);
206 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
, &page
);
209 return ERR_CAST(page
);
210 return ERR_PTR(-ENOENT
);
212 return d_obtain_alias(f2fs_iget(child
->d_sb
, ino
));
215 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
217 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
218 struct qstr dot
= QSTR_INIT(".", 1);
219 struct qstr dotdot
= QSTR_INIT("..", 2);
220 struct f2fs_dir_entry
*de
;
224 if (f2fs_readonly(sbi
->sb
)) {
225 f2fs_msg(sbi
->sb
, KERN_INFO
,
226 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
227 "in readonly mountpoint", dir
->i_ino
, pino
);
231 f2fs_balance_fs(sbi
, true);
235 de
= f2fs_find_entry(dir
, &dot
, &page
);
237 f2fs_dentry_kunmap(dir
, page
);
238 f2fs_put_page(page
, 0);
239 } else if (IS_ERR(page
)) {
243 err
= __f2fs_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
248 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
250 f2fs_dentry_kunmap(dir
, page
);
251 f2fs_put_page(page
, 0);
252 } else if (IS_ERR(page
)) {
255 err
= __f2fs_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
259 clear_inode_flag(dir
, FI_INLINE_DOTS
);
265 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
268 struct inode
*inode
= NULL
;
269 struct f2fs_dir_entry
*de
;
273 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
275 if (f2fs_encrypted_inode(dir
)) {
276 int res
= fscrypt_get_encryption_info(dir
);
279 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
280 * created while the directory was encrypted and we
281 * don't have access to the key.
283 if (fscrypt_has_encryption_key(dir
))
284 fscrypt_set_encrypted_dentry(dentry
);
285 fscrypt_set_d_op(dentry
);
286 if (res
&& res
!= -ENOKEY
)
290 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
291 return ERR_PTR(-ENAMETOOLONG
);
293 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
296 return (struct dentry
*)page
;
297 return d_splice_alias(inode
, dentry
);
300 ino
= le32_to_cpu(de
->ino
);
301 f2fs_dentry_kunmap(dir
, page
);
302 f2fs_put_page(page
, 0);
304 inode
= f2fs_iget(dir
->i_sb
, ino
);
306 return ERR_CAST(inode
);
308 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
309 err
= __recover_dot_dentries(dir
, root_ino
);
314 if (f2fs_has_inline_dots(inode
)) {
315 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
319 if (!IS_ERR(inode
) && f2fs_encrypted_inode(dir
) &&
320 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
321 !fscrypt_has_permitted_context(dir
, inode
)) {
322 bool nokey
= f2fs_encrypted_inode(inode
) &&
323 !fscrypt_has_encryption_key(inode
);
324 err
= nokey
? -ENOKEY
: -EPERM
;
327 return d_splice_alias(inode
, dentry
);
334 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
336 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
337 struct inode
*inode
= d_inode(dentry
);
338 struct f2fs_dir_entry
*de
;
342 trace_f2fs_unlink_enter(dir
, dentry
);
344 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
351 f2fs_balance_fs(sbi
, true);
354 err
= acquire_orphan_inode(sbi
);
357 f2fs_dentry_kunmap(dir
, page
);
358 f2fs_put_page(page
, 0);
361 f2fs_delete_entry(de
, page
, dir
, inode
);
365 f2fs_sync_fs(sbi
->sb
, 1);
367 trace_f2fs_unlink_exit(inode
, err
);
371 static const char *f2fs_get_link(struct dentry
*dentry
,
373 struct delayed_call
*done
)
375 const char *link
= page_get_link(dentry
, inode
, done
);
376 if (!IS_ERR(link
) && !*link
) {
377 /* this is broken symlink case */
378 do_delayed_call(done
);
379 clear_delayed_call(done
);
380 link
= ERR_PTR(-ENOENT
);
385 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
388 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
390 size_t len
= strlen(symname
);
391 struct fscrypt_str disk_link
= FSTR_INIT((char *)symname
, len
+ 1);
392 struct fscrypt_symlink_data
*sd
= NULL
;
395 if (f2fs_encrypted_inode(dir
)) {
396 err
= fscrypt_get_encryption_info(dir
);
400 if (!fscrypt_has_encryption_key(dir
))
403 disk_link
.len
= (fscrypt_fname_encrypted_size(dir
, len
) +
404 sizeof(struct fscrypt_symlink_data
));
407 if (disk_link
.len
> dir
->i_sb
->s_blocksize
)
408 return -ENAMETOOLONG
;
410 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
412 return PTR_ERR(inode
);
414 if (f2fs_encrypted_inode(inode
))
415 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
417 inode
->i_op
= &f2fs_symlink_inode_operations
;
418 inode_nohighmem(inode
);
419 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
421 f2fs_balance_fs(sbi
, true);
424 err
= f2fs_add_link(dentry
, inode
);
428 alloc_nid_done(sbi
, inode
->i_ino
);
430 if (f2fs_encrypted_inode(inode
)) {
431 struct qstr istr
= QSTR_INIT(symname
, len
);
432 struct fscrypt_str ostr
;
434 sd
= kzalloc(disk_link
.len
, GFP_NOFS
);
440 err
= fscrypt_get_encryption_info(inode
);
444 if (!fscrypt_has_encryption_key(inode
)) {
449 ostr
.name
= sd
->encrypted_path
;
450 ostr
.len
= disk_link
.len
;
451 err
= fscrypt_fname_usr_to_disk(inode
, &istr
, &ostr
);
455 sd
->len
= cpu_to_le16(ostr
.len
);
456 disk_link
.name
= (char *)sd
;
459 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
462 d_instantiate(dentry
, inode
);
463 unlock_new_inode(inode
);
466 * Let's flush symlink data in order to avoid broken symlink as much as
467 * possible. Nevertheless, fsyncing is the best way, but there is no
468 * way to get a file descriptor in order to flush that.
470 * Note that, it needs to do dir->fsync to make this recoverable.
471 * If the symlink path is stored into inline_data, there is no
472 * performance regression.
475 filemap_write_and_wait_range(inode
->i_mapping
, 0,
479 f2fs_sync_fs(sbi
->sb
, 1);
481 f2fs_unlink(dir
, dentry
);
487 handle_failed_inode(inode
);
491 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
493 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
497 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
499 return PTR_ERR(inode
);
501 inode
->i_op
= &f2fs_dir_inode_operations
;
502 inode
->i_fop
= &f2fs_dir_operations
;
503 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
504 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_HIGH_ZERO
);
506 f2fs_balance_fs(sbi
, true);
508 set_inode_flag(inode
, FI_INC_LINK
);
510 err
= f2fs_add_link(dentry
, inode
);
515 alloc_nid_done(sbi
, inode
->i_ino
);
517 d_instantiate(dentry
, inode
);
518 unlock_new_inode(inode
);
521 f2fs_sync_fs(sbi
->sb
, 1);
525 clear_inode_flag(inode
, FI_INC_LINK
);
526 handle_failed_inode(inode
);
530 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
532 struct inode
*inode
= d_inode(dentry
);
533 if (f2fs_empty_dir(inode
))
534 return f2fs_unlink(dir
, dentry
);
538 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
539 umode_t mode
, dev_t rdev
)
541 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
545 inode
= f2fs_new_inode(dir
, mode
);
547 return PTR_ERR(inode
);
549 init_special_inode(inode
, inode
->i_mode
, rdev
);
550 inode
->i_op
= &f2fs_special_inode_operations
;
552 f2fs_balance_fs(sbi
, true);
555 err
= f2fs_add_link(dentry
, inode
);
560 alloc_nid_done(sbi
, inode
->i_ino
);
562 d_instantiate(dentry
, inode
);
563 unlock_new_inode(inode
);
566 f2fs_sync_fs(sbi
->sb
, 1);
569 handle_failed_inode(inode
);
573 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
574 umode_t mode
, struct inode
**whiteout
)
576 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
580 inode
= f2fs_new_inode(dir
, mode
);
582 return PTR_ERR(inode
);
585 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
586 inode
->i_op
= &f2fs_special_inode_operations
;
588 inode
->i_op
= &f2fs_file_inode_operations
;
589 inode
->i_fop
= &f2fs_file_operations
;
590 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
593 f2fs_balance_fs(sbi
, true);
596 err
= acquire_orphan_inode(sbi
);
600 err
= f2fs_do_tmpfile(inode
, dir
);
605 * add this non-linked tmpfile to orphan list, in this way we could
606 * remove all unused data of tmpfile after abnormal power-off.
608 add_orphan_inode(inode
);
609 alloc_nid_done(sbi
, inode
->i_ino
);
612 f2fs_i_links_write(inode
, false);
615 d_tmpfile(dentry
, inode
);
617 /* link_count was changed by d_tmpfile as well. */
619 unlock_new_inode(inode
);
623 release_orphan_inode(sbi
);
625 handle_failed_inode(inode
);
629 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
631 if (f2fs_encrypted_inode(dir
)) {
632 int err
= fscrypt_get_encryption_info(dir
);
637 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
640 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
642 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
645 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
646 struct inode
*new_dir
, struct dentry
*new_dentry
,
649 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
650 struct inode
*old_inode
= d_inode(old_dentry
);
651 struct inode
*new_inode
= d_inode(new_dentry
);
652 struct inode
*whiteout
= NULL
;
653 struct page
*old_dir_page
;
654 struct page
*old_page
, *new_page
= NULL
;
655 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
656 struct f2fs_dir_entry
*old_entry
;
657 struct f2fs_dir_entry
*new_entry
;
658 bool is_old_inline
= f2fs_has_inline_dentry(old_dir
);
661 if ((old_dir
!= new_dir
) && f2fs_encrypted_inode(new_dir
) &&
662 !fscrypt_has_permitted_context(new_dir
, old_inode
)) {
667 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
669 if (IS_ERR(old_page
))
670 err
= PTR_ERR(old_page
);
674 if (S_ISDIR(old_inode
->i_mode
)) {
675 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
676 if (!old_dir_entry
) {
677 if (IS_ERR(old_dir_page
))
678 err
= PTR_ERR(old_dir_page
);
683 if (flags
& RENAME_WHITEOUT
) {
684 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
692 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
696 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
699 if (IS_ERR(new_page
))
700 err
= PTR_ERR(new_page
);
704 f2fs_balance_fs(sbi
, true);
708 err
= acquire_orphan_inode(sbi
);
712 err
= update_dent_inode(old_inode
, new_inode
,
713 &new_dentry
->d_name
);
715 release_orphan_inode(sbi
);
719 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
721 new_inode
->i_ctime
= CURRENT_TIME
;
722 down_write(&F2FS_I(new_inode
)->i_sem
);
724 f2fs_i_links_write(new_inode
, false);
725 f2fs_i_links_write(new_inode
, false);
726 up_write(&F2FS_I(new_inode
)->i_sem
);
728 if (!new_inode
->i_nlink
)
729 add_orphan_inode(new_inode
);
731 release_orphan_inode(sbi
);
733 f2fs_balance_fs(sbi
, true);
737 err
= f2fs_add_link(new_dentry
, old_inode
);
744 f2fs_i_links_write(new_dir
, true);
747 * old entry and new entry can locate in the same inline
748 * dentry in inode, when attaching new entry in inline dentry,
749 * it could force inline dentry conversion, after that,
750 * old_entry and old_page will point to wrong address, in
751 * order to avoid this, let's do the check and update here.
753 if (is_old_inline
&& !f2fs_has_inline_dentry(old_dir
)) {
754 f2fs_put_page(old_page
, 0);
757 old_entry
= f2fs_find_entry(old_dir
,
758 &old_dentry
->d_name
, &old_page
);
761 if (IS_ERR(old_page
))
762 err
= PTR_ERR(old_page
);
769 down_write(&F2FS_I(old_inode
)->i_sem
);
770 file_lost_pino(old_inode
);
771 if (new_inode
&& file_enc_name(new_inode
))
772 file_set_enc_name(old_inode
);
773 up_write(&F2FS_I(old_inode
)->i_sem
);
775 old_inode
->i_ctime
= CURRENT_TIME
;
776 f2fs_mark_inode_dirty_sync(old_inode
);
778 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
781 whiteout
->i_state
|= I_LINKABLE
;
782 set_inode_flag(whiteout
, FI_INC_LINK
);
783 err
= f2fs_add_link(old_dentry
, whiteout
);
786 whiteout
->i_state
&= ~I_LINKABLE
;
791 if (old_dir
!= new_dir
&& !whiteout
) {
792 f2fs_set_link(old_inode
, old_dir_entry
,
793 old_dir_page
, new_dir
);
795 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
796 f2fs_put_page(old_dir_page
, 0);
798 f2fs_i_links_write(old_dir
, false);
803 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
804 f2fs_sync_fs(sbi
->sb
, 1);
810 f2fs_dentry_kunmap(new_dir
, new_page
);
811 f2fs_put_page(new_page
, 0);
818 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
819 f2fs_put_page(old_dir_page
, 0);
822 f2fs_dentry_kunmap(old_dir
, old_page
);
823 f2fs_put_page(old_page
, 0);
828 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
829 struct inode
*new_dir
, struct dentry
*new_dentry
)
831 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
832 struct inode
*old_inode
= d_inode(old_dentry
);
833 struct inode
*new_inode
= d_inode(new_dentry
);
834 struct page
*old_dir_page
, *new_dir_page
;
835 struct page
*old_page
, *new_page
;
836 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
837 struct f2fs_dir_entry
*old_entry
, *new_entry
;
838 int old_nlink
= 0, new_nlink
= 0;
841 if ((f2fs_encrypted_inode(old_dir
) || f2fs_encrypted_inode(new_dir
)) &&
842 (old_dir
!= new_dir
) &&
843 (!fscrypt_has_permitted_context(new_dir
, old_inode
) ||
844 !fscrypt_has_permitted_context(old_dir
, new_inode
)))
847 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
849 if (IS_ERR(old_page
))
850 err
= PTR_ERR(old_page
);
854 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
856 if (IS_ERR(new_page
))
857 err
= PTR_ERR(new_page
);
861 /* prepare for updating ".." directory entry info later */
862 if (old_dir
!= new_dir
) {
863 if (S_ISDIR(old_inode
->i_mode
)) {
864 old_dir_entry
= f2fs_parent_dir(old_inode
,
866 if (!old_dir_entry
) {
867 if (IS_ERR(old_dir_page
))
868 err
= PTR_ERR(old_dir_page
);
873 if (S_ISDIR(new_inode
->i_mode
)) {
874 new_dir_entry
= f2fs_parent_dir(new_inode
,
876 if (!new_dir_entry
) {
877 if (IS_ERR(new_dir_page
))
878 err
= PTR_ERR(new_dir_page
);
885 * If cross rename between file and directory those are not
886 * in the same directory, we will inc nlink of file's parent
887 * later, so we should check upper boundary of its nlink.
889 if ((!old_dir_entry
|| !new_dir_entry
) &&
890 old_dir_entry
!= new_dir_entry
) {
891 old_nlink
= old_dir_entry
? -1 : 1;
892 new_nlink
= -old_nlink
;
894 if ((old_nlink
> 0 && old_inode
->i_nlink
>= F2FS_LINK_MAX
) ||
895 (new_nlink
> 0 && new_inode
->i_nlink
>= F2FS_LINK_MAX
))
899 f2fs_balance_fs(sbi
, true);
903 err
= update_dent_inode(old_inode
, new_inode
, &new_dentry
->d_name
);
906 if (file_enc_name(new_inode
))
907 file_set_enc_name(old_inode
);
909 err
= update_dent_inode(new_inode
, old_inode
, &old_dentry
->d_name
);
912 if (file_enc_name(old_inode
))
913 file_set_enc_name(new_inode
);
915 /* update ".." directory entry info of old dentry */
917 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
919 /* update ".." directory entry info of new dentry */
921 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
923 /* update directory entry info of old dir inode */
924 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
926 down_write(&F2FS_I(old_inode
)->i_sem
);
927 file_lost_pino(old_inode
);
928 up_write(&F2FS_I(old_inode
)->i_sem
);
930 old_dir
->i_ctime
= CURRENT_TIME
;
932 down_write(&F2FS_I(old_dir
)->i_sem
);
933 f2fs_i_links_write(old_dir
, old_nlink
> 0);
934 up_write(&F2FS_I(old_dir
)->i_sem
);
936 f2fs_mark_inode_dirty_sync(old_dir
);
938 /* update directory entry info of new dir inode */
939 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
941 down_write(&F2FS_I(new_inode
)->i_sem
);
942 file_lost_pino(new_inode
);
943 up_write(&F2FS_I(new_inode
)->i_sem
);
945 new_dir
->i_ctime
= CURRENT_TIME
;
947 down_write(&F2FS_I(new_dir
)->i_sem
);
948 f2fs_i_links_write(new_dir
, new_nlink
> 0);
949 up_write(&F2FS_I(new_dir
)->i_sem
);
951 f2fs_mark_inode_dirty_sync(new_dir
);
955 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
956 f2fs_sync_fs(sbi
->sb
, 1);
960 * Still we may fail to recover name info of f2fs_inode here
961 * Drop it, once its name is set as encrypted
963 update_dent_inode(old_inode
, old_inode
, &old_dentry
->d_name
);
968 f2fs_dentry_kunmap(new_inode
, new_dir_page
);
969 f2fs_put_page(new_dir_page
, 0);
973 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
974 f2fs_put_page(old_dir_page
, 0);
977 f2fs_dentry_kunmap(new_dir
, new_page
);
978 f2fs_put_page(new_page
, 0);
980 f2fs_dentry_kunmap(old_dir
, old_page
);
981 f2fs_put_page(old_page
, 0);
986 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
987 struct inode
*new_dir
, struct dentry
*new_dentry
,
990 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
993 if (flags
& RENAME_EXCHANGE
) {
994 return f2fs_cross_rename(old_dir
, old_dentry
,
995 new_dir
, new_dentry
);
998 * VFS has already handled the new dentry existence case,
999 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1001 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1004 static const char *f2fs_encrypted_get_link(struct dentry
*dentry
,
1005 struct inode
*inode
,
1006 struct delayed_call
*done
)
1008 struct page
*cpage
= NULL
;
1009 char *caddr
, *paddr
= NULL
;
1010 struct fscrypt_str cstr
= FSTR_INIT(NULL
, 0);
1011 struct fscrypt_str pstr
= FSTR_INIT(NULL
, 0);
1012 struct fscrypt_symlink_data
*sd
;
1013 loff_t size
= min_t(loff_t
, i_size_read(inode
), PAGE_SIZE
- 1);
1014 u32 max_size
= inode
->i_sb
->s_blocksize
;
1018 return ERR_PTR(-ECHILD
);
1020 res
= fscrypt_get_encryption_info(inode
);
1022 return ERR_PTR(res
);
1024 cpage
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1026 return ERR_CAST(cpage
);
1027 caddr
= page_address(cpage
);
1030 /* Symlink is encrypted */
1031 sd
= (struct fscrypt_symlink_data
*)caddr
;
1032 cstr
.name
= sd
->encrypted_path
;
1033 cstr
.len
= le16_to_cpu(sd
->len
);
1035 /* this is broken symlink case */
1036 if (unlikely(cstr
.len
== 0)) {
1041 if ((cstr
.len
+ sizeof(struct fscrypt_symlink_data
) - 1) > max_size
) {
1042 /* Symlink data on the disk is corrupted */
1046 res
= fscrypt_fname_alloc_buffer(inode
, cstr
.len
, &pstr
);
1050 res
= fscrypt_fname_disk_to_usr(inode
, 0, 0, &cstr
, &pstr
);
1054 /* this is broken symlink case */
1055 if (unlikely(pstr
.name
[0] == 0)) {
1062 /* Null-terminate the name */
1066 set_delayed_call(done
, kfree_link
, paddr
);
1069 fscrypt_fname_free_buffer(&pstr
);
1071 return ERR_PTR(res
);
1074 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1075 .readlink
= generic_readlink
,
1076 .get_link
= f2fs_encrypted_get_link
,
1077 .getattr
= f2fs_getattr
,
1078 .setattr
= f2fs_setattr
,
1079 #ifdef CONFIG_F2FS_FS_XATTR
1080 .setxattr
= generic_setxattr
,
1081 .getxattr
= generic_getxattr
,
1082 .listxattr
= f2fs_listxattr
,
1083 .removexattr
= generic_removexattr
,
1087 const struct inode_operations f2fs_dir_inode_operations
= {
1088 .create
= f2fs_create
,
1089 .lookup
= f2fs_lookup
,
1091 .unlink
= f2fs_unlink
,
1092 .symlink
= f2fs_symlink
,
1093 .mkdir
= f2fs_mkdir
,
1094 .rmdir
= f2fs_rmdir
,
1095 .mknod
= f2fs_mknod
,
1096 .rename2
= f2fs_rename2
,
1097 .tmpfile
= f2fs_tmpfile
,
1098 .getattr
= f2fs_getattr
,
1099 .setattr
= f2fs_setattr
,
1100 .get_acl
= f2fs_get_acl
,
1101 .set_acl
= f2fs_set_acl
,
1102 #ifdef CONFIG_F2FS_FS_XATTR
1103 .setxattr
= generic_setxattr
,
1104 .getxattr
= generic_getxattr
,
1105 .listxattr
= f2fs_listxattr
,
1106 .removexattr
= generic_removexattr
,
1110 const struct inode_operations f2fs_symlink_inode_operations
= {
1111 .readlink
= generic_readlink
,
1112 .get_link
= f2fs_get_link
,
1113 .getattr
= f2fs_getattr
,
1114 .setattr
= f2fs_setattr
,
1115 #ifdef CONFIG_F2FS_FS_XATTR
1116 .setxattr
= generic_setxattr
,
1117 .getxattr
= generic_getxattr
,
1118 .listxattr
= f2fs_listxattr
,
1119 .removexattr
= generic_removexattr
,
1123 const struct inode_operations f2fs_special_inode_operations
= {
1124 .getattr
= f2fs_getattr
,
1125 .setattr
= f2fs_setattr
,
1126 .get_acl
= f2fs_get_acl
,
1127 .set_acl
= f2fs_set_acl
,
1128 #ifdef CONFIG_F2FS_FS_XATTR
1129 .setxattr
= generic_setxattr
,
1130 .getxattr
= generic_getxattr
,
1131 .listxattr
= f2fs_listxattr
,
1132 .removexattr
= generic_removexattr
,