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(inode
);
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
);
97 * filename format of multimedia file should be defined as:
98 * "filename + '.' + extension + (optional: '.' + temp extension)".
100 if (slen
< sublen
+ 2)
103 for (i
= 1; i
< slen
- sublen
; i
++) {
106 if (!strncasecmp(s
+ i
+ 1, sub
, sublen
))
114 * Set multimedia files as cold files for hot/cold data separation
116 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
117 const unsigned char *name
)
120 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
122 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
123 for (i
= 0; i
< count
; i
++) {
124 if (is_multimedia_file(name
, extlist
[i
])) {
125 file_set_cold(inode
);
131 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
134 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
139 inode
= f2fs_new_inode(dir
, mode
);
141 return PTR_ERR(inode
);
143 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
144 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
146 inode
->i_op
= &f2fs_file_inode_operations
;
147 inode
->i_fop
= &f2fs_file_operations
;
148 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
151 f2fs_balance_fs(sbi
, true);
154 err
= f2fs_add_link(dentry
, inode
);
159 alloc_nid_done(sbi
, ino
);
161 d_instantiate_new(dentry
, inode
);
164 f2fs_sync_fs(sbi
->sb
, 1);
167 handle_failed_inode(inode
);
171 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
172 struct dentry
*dentry
)
174 struct inode
*inode
= d_inode(old_dentry
);
175 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
178 if (f2fs_encrypted_inode(dir
) &&
179 !fscrypt_has_permitted_context(dir
, inode
))
182 f2fs_balance_fs(sbi
, true);
184 inode
->i_ctime
= current_time(inode
);
187 set_inode_flag(inode
, FI_INC_LINK
);
189 err
= f2fs_add_link(dentry
, inode
);
194 d_instantiate(dentry
, inode
);
197 f2fs_sync_fs(sbi
->sb
, 1);
200 clear_inode_flag(inode
, FI_INC_LINK
);
206 struct dentry
*f2fs_get_parent(struct dentry
*child
)
208 struct qstr dotdot
= QSTR_INIT("..", 2);
210 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
, &page
);
213 return ERR_CAST(page
);
214 return ERR_PTR(-ENOENT
);
216 return d_obtain_alias(f2fs_iget(child
->d_sb
, ino
));
219 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
221 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
222 struct qstr dot
= QSTR_INIT(".", 1);
223 struct qstr dotdot
= QSTR_INIT("..", 2);
224 struct f2fs_dir_entry
*de
;
228 if (f2fs_readonly(sbi
->sb
)) {
229 f2fs_msg(sbi
->sb
, KERN_INFO
,
230 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
231 "in readonly mountpoint", dir
->i_ino
, pino
);
235 f2fs_balance_fs(sbi
, true);
239 de
= f2fs_find_entry(dir
, &dot
, &page
);
241 f2fs_dentry_kunmap(dir
, page
);
242 f2fs_put_page(page
, 0);
243 } else if (IS_ERR(page
)) {
247 err
= __f2fs_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
252 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
254 f2fs_dentry_kunmap(dir
, page
);
255 f2fs_put_page(page
, 0);
256 } else if (IS_ERR(page
)) {
259 err
= __f2fs_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
263 clear_inode_flag(dir
, FI_INLINE_DOTS
);
269 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
272 struct inode
*inode
= NULL
;
273 struct f2fs_dir_entry
*de
;
277 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
279 if (f2fs_encrypted_inode(dir
)) {
280 int res
= fscrypt_get_encryption_info(dir
);
283 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
284 * created while the directory was encrypted and we
285 * don't have access to the key.
287 if (fscrypt_has_encryption_key(dir
))
288 fscrypt_set_encrypted_dentry(dentry
);
289 fscrypt_set_d_op(dentry
);
290 if (res
&& res
!= -ENOKEY
)
294 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
295 return ERR_PTR(-ENAMETOOLONG
);
297 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
300 return (struct dentry
*)page
;
301 return d_splice_alias(inode
, dentry
);
304 ino
= le32_to_cpu(de
->ino
);
305 f2fs_dentry_kunmap(dir
, page
);
306 f2fs_put_page(page
, 0);
308 inode
= f2fs_iget(dir
->i_sb
, ino
);
310 return ERR_CAST(inode
);
312 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
313 err
= __recover_dot_dentries(dir
, root_ino
);
318 if (f2fs_has_inline_dots(inode
)) {
319 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
323 if (!IS_ERR(inode
) && f2fs_encrypted_inode(dir
) &&
324 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
325 !fscrypt_has_permitted_context(dir
, inode
)) {
326 bool nokey
= f2fs_encrypted_inode(inode
) &&
327 !fscrypt_has_encryption_key(inode
);
328 err
= nokey
? -ENOKEY
: -EPERM
;
331 return d_splice_alias(inode
, dentry
);
338 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
340 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
341 struct inode
*inode
= d_inode(dentry
);
342 struct f2fs_dir_entry
*de
;
346 trace_f2fs_unlink_enter(dir
, dentry
);
348 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
355 f2fs_balance_fs(sbi
, true);
358 err
= acquire_orphan_inode(sbi
);
361 f2fs_dentry_kunmap(dir
, page
);
362 f2fs_put_page(page
, 0);
365 f2fs_delete_entry(de
, page
, dir
, inode
);
369 f2fs_sync_fs(sbi
->sb
, 1);
371 trace_f2fs_unlink_exit(inode
, err
);
375 static const char *f2fs_get_link(struct dentry
*dentry
,
377 struct delayed_call
*done
)
379 const char *link
= page_get_link(dentry
, inode
, done
);
380 if (!IS_ERR(link
) && !*link
) {
381 /* this is broken symlink case */
382 do_delayed_call(done
);
383 clear_delayed_call(done
);
384 link
= ERR_PTR(-ENOENT
);
389 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
392 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
394 size_t len
= strlen(symname
);
395 struct fscrypt_str disk_link
= FSTR_INIT((char *)symname
, len
+ 1);
396 struct fscrypt_symlink_data
*sd
= NULL
;
399 if (f2fs_encrypted_inode(dir
)) {
400 err
= fscrypt_get_encryption_info(dir
);
404 if (!fscrypt_has_encryption_key(dir
))
407 disk_link
.len
= (fscrypt_fname_encrypted_size(dir
, len
) +
408 sizeof(struct fscrypt_symlink_data
));
411 if (disk_link
.len
> dir
->i_sb
->s_blocksize
)
412 return -ENAMETOOLONG
;
414 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
416 return PTR_ERR(inode
);
418 if (f2fs_encrypted_inode(inode
))
419 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
421 inode
->i_op
= &f2fs_symlink_inode_operations
;
422 inode_nohighmem(inode
);
423 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
425 f2fs_balance_fs(sbi
, true);
428 err
= f2fs_add_link(dentry
, inode
);
432 alloc_nid_done(sbi
, inode
->i_ino
);
434 if (f2fs_encrypted_inode(inode
)) {
435 struct qstr istr
= QSTR_INIT(symname
, len
);
436 struct fscrypt_str ostr
;
438 sd
= kzalloc(disk_link
.len
, GFP_NOFS
);
444 err
= fscrypt_get_encryption_info(inode
);
448 if (!fscrypt_has_encryption_key(inode
)) {
453 ostr
.name
= sd
->encrypted_path
;
454 ostr
.len
= disk_link
.len
;
455 err
= fscrypt_fname_usr_to_disk(inode
, &istr
, &ostr
);
459 sd
->len
= cpu_to_le16(ostr
.len
);
460 disk_link
.name
= (char *)sd
;
463 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
466 d_instantiate_new(dentry
, inode
);
469 * Let's flush symlink data in order to avoid broken symlink as much as
470 * possible. Nevertheless, fsyncing is the best way, but there is no
471 * way to get a file descriptor in order to flush that.
473 * Note that, it needs to do dir->fsync to make this recoverable.
474 * If the symlink path is stored into inline_data, there is no
475 * performance regression.
478 filemap_write_and_wait_range(inode
->i_mapping
, 0,
482 f2fs_sync_fs(sbi
->sb
, 1);
484 f2fs_unlink(dir
, dentry
);
490 handle_failed_inode(inode
);
494 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
496 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
500 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
502 return PTR_ERR(inode
);
504 inode
->i_op
= &f2fs_dir_inode_operations
;
505 inode
->i_fop
= &f2fs_dir_operations
;
506 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
507 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_HIGH_ZERO
);
509 f2fs_balance_fs(sbi
, true);
511 set_inode_flag(inode
, FI_INC_LINK
);
513 err
= f2fs_add_link(dentry
, inode
);
518 alloc_nid_done(sbi
, inode
->i_ino
);
520 d_instantiate_new(dentry
, inode
);
523 f2fs_sync_fs(sbi
->sb
, 1);
527 clear_inode_flag(inode
, FI_INC_LINK
);
528 handle_failed_inode(inode
);
532 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
534 struct inode
*inode
= d_inode(dentry
);
535 if (f2fs_empty_dir(inode
))
536 return f2fs_unlink(dir
, dentry
);
540 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
541 umode_t mode
, dev_t rdev
)
543 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
547 inode
= f2fs_new_inode(dir
, mode
);
549 return PTR_ERR(inode
);
551 init_special_inode(inode
, inode
->i_mode
, rdev
);
552 inode
->i_op
= &f2fs_special_inode_operations
;
554 f2fs_balance_fs(sbi
, true);
557 err
= f2fs_add_link(dentry
, inode
);
562 alloc_nid_done(sbi
, inode
->i_ino
);
564 d_instantiate_new(dentry
, inode
);
567 f2fs_sync_fs(sbi
->sb
, 1);
570 handle_failed_inode(inode
);
574 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
575 umode_t mode
, struct inode
**whiteout
)
577 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
581 inode
= f2fs_new_inode(dir
, mode
);
583 return PTR_ERR(inode
);
586 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
587 inode
->i_op
= &f2fs_special_inode_operations
;
589 inode
->i_op
= &f2fs_file_inode_operations
;
590 inode
->i_fop
= &f2fs_file_operations
;
591 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
594 f2fs_balance_fs(sbi
, true);
597 err
= acquire_orphan_inode(sbi
);
601 err
= f2fs_do_tmpfile(inode
, dir
);
606 * add this non-linked tmpfile to orphan list, in this way we could
607 * remove all unused data of tmpfile after abnormal power-off.
609 add_orphan_inode(inode
);
610 alloc_nid_done(sbi
, inode
->i_ino
);
613 f2fs_i_links_write(inode
, false);
616 d_tmpfile(dentry
, inode
);
618 /* link_count was changed by d_tmpfile as well. */
620 unlock_new_inode(inode
);
624 release_orphan_inode(sbi
);
626 handle_failed_inode(inode
);
630 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
632 if (f2fs_encrypted_inode(dir
)) {
633 int err
= fscrypt_get_encryption_info(dir
);
638 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
641 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
643 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
646 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
647 struct inode
*new_dir
, struct dentry
*new_dentry
,
650 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
651 struct inode
*old_inode
= d_inode(old_dentry
);
652 struct inode
*new_inode
= d_inode(new_dentry
);
653 struct inode
*whiteout
= NULL
;
654 struct page
*old_dir_page
;
655 struct page
*old_page
, *new_page
= NULL
;
656 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
657 struct f2fs_dir_entry
*old_entry
;
658 struct f2fs_dir_entry
*new_entry
;
659 bool is_old_inline
= f2fs_has_inline_dentry(old_dir
);
662 if ((f2fs_encrypted_inode(old_dir
) &&
663 !fscrypt_has_encryption_key(old_dir
)) ||
664 (f2fs_encrypted_inode(new_dir
) &&
665 !fscrypt_has_encryption_key(new_dir
)))
668 if ((old_dir
!= new_dir
) && f2fs_encrypted_inode(new_dir
) &&
669 !fscrypt_has_permitted_context(new_dir
, old_inode
)) {
674 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
676 if (IS_ERR(old_page
))
677 err
= PTR_ERR(old_page
);
681 if (S_ISDIR(old_inode
->i_mode
)) {
682 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
683 if (!old_dir_entry
) {
684 if (IS_ERR(old_dir_page
))
685 err
= PTR_ERR(old_dir_page
);
690 if (flags
& RENAME_WHITEOUT
) {
691 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
699 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
703 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
706 if (IS_ERR(new_page
))
707 err
= PTR_ERR(new_page
);
711 f2fs_balance_fs(sbi
, true);
715 err
= acquire_orphan_inode(sbi
);
719 err
= update_dent_inode(old_inode
, new_inode
,
720 &new_dentry
->d_name
);
722 release_orphan_inode(sbi
);
726 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
728 new_inode
->i_ctime
= current_time(new_inode
);
729 down_write(&F2FS_I(new_inode
)->i_sem
);
731 f2fs_i_links_write(new_inode
, false);
732 f2fs_i_links_write(new_inode
, false);
733 up_write(&F2FS_I(new_inode
)->i_sem
);
735 if (!new_inode
->i_nlink
)
736 add_orphan_inode(new_inode
);
738 release_orphan_inode(sbi
);
740 f2fs_balance_fs(sbi
, true);
744 err
= f2fs_add_link(new_dentry
, old_inode
);
751 f2fs_i_links_write(new_dir
, true);
754 * old entry and new entry can locate in the same inline
755 * dentry in inode, when attaching new entry in inline dentry,
756 * it could force inline dentry conversion, after that,
757 * old_entry and old_page will point to wrong address, in
758 * order to avoid this, let's do the check and update here.
760 if (is_old_inline
&& !f2fs_has_inline_dentry(old_dir
)) {
761 f2fs_put_page(old_page
, 0);
764 old_entry
= f2fs_find_entry(old_dir
,
765 &old_dentry
->d_name
, &old_page
);
768 if (IS_ERR(old_page
))
769 err
= PTR_ERR(old_page
);
776 down_write(&F2FS_I(old_inode
)->i_sem
);
777 file_lost_pino(old_inode
);
778 if (new_inode
&& file_enc_name(new_inode
))
779 file_set_enc_name(old_inode
);
780 up_write(&F2FS_I(old_inode
)->i_sem
);
782 old_inode
->i_ctime
= current_time(old_inode
);
783 f2fs_mark_inode_dirty_sync(old_inode
);
785 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
788 whiteout
->i_state
|= I_LINKABLE
;
789 set_inode_flag(whiteout
, FI_INC_LINK
);
790 err
= f2fs_add_link(old_dentry
, whiteout
);
793 whiteout
->i_state
&= ~I_LINKABLE
;
798 if (old_dir
!= new_dir
&& !whiteout
) {
799 f2fs_set_link(old_inode
, old_dir_entry
,
800 old_dir_page
, new_dir
);
802 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
803 f2fs_put_page(old_dir_page
, 0);
805 f2fs_i_links_write(old_dir
, false);
810 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
811 f2fs_sync_fs(sbi
->sb
, 1);
817 f2fs_dentry_kunmap(new_dir
, new_page
);
818 f2fs_put_page(new_page
, 0);
825 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
826 f2fs_put_page(old_dir_page
, 0);
829 f2fs_dentry_kunmap(old_dir
, old_page
);
830 f2fs_put_page(old_page
, 0);
835 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
836 struct inode
*new_dir
, struct dentry
*new_dentry
)
838 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
839 struct inode
*old_inode
= d_inode(old_dentry
);
840 struct inode
*new_inode
= d_inode(new_dentry
);
841 struct page
*old_dir_page
, *new_dir_page
;
842 struct page
*old_page
, *new_page
;
843 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
844 struct f2fs_dir_entry
*old_entry
, *new_entry
;
845 int old_nlink
= 0, new_nlink
= 0;
848 if ((f2fs_encrypted_inode(old_dir
) &&
849 !fscrypt_has_encryption_key(old_dir
)) ||
850 (f2fs_encrypted_inode(new_dir
) &&
851 !fscrypt_has_encryption_key(new_dir
)))
854 if ((f2fs_encrypted_inode(old_dir
) || f2fs_encrypted_inode(new_dir
)) &&
855 (old_dir
!= new_dir
) &&
856 (!fscrypt_has_permitted_context(new_dir
, old_inode
) ||
857 !fscrypt_has_permitted_context(old_dir
, new_inode
)))
860 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
862 if (IS_ERR(old_page
))
863 err
= PTR_ERR(old_page
);
867 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
869 if (IS_ERR(new_page
))
870 err
= PTR_ERR(new_page
);
874 /* prepare for updating ".." directory entry info later */
875 if (old_dir
!= new_dir
) {
876 if (S_ISDIR(old_inode
->i_mode
)) {
877 old_dir_entry
= f2fs_parent_dir(old_inode
,
879 if (!old_dir_entry
) {
880 if (IS_ERR(old_dir_page
))
881 err
= PTR_ERR(old_dir_page
);
886 if (S_ISDIR(new_inode
->i_mode
)) {
887 new_dir_entry
= f2fs_parent_dir(new_inode
,
889 if (!new_dir_entry
) {
890 if (IS_ERR(new_dir_page
))
891 err
= PTR_ERR(new_dir_page
);
898 * If cross rename between file and directory those are not
899 * in the same directory, we will inc nlink of file's parent
900 * later, so we should check upper boundary of its nlink.
902 if ((!old_dir_entry
|| !new_dir_entry
) &&
903 old_dir_entry
!= new_dir_entry
) {
904 old_nlink
= old_dir_entry
? -1 : 1;
905 new_nlink
= -old_nlink
;
907 if ((old_nlink
> 0 && old_inode
->i_nlink
>= F2FS_LINK_MAX
) ||
908 (new_nlink
> 0 && new_inode
->i_nlink
>= F2FS_LINK_MAX
))
912 f2fs_balance_fs(sbi
, true);
916 err
= update_dent_inode(old_inode
, new_inode
, &new_dentry
->d_name
);
919 if (file_enc_name(new_inode
))
920 file_set_enc_name(old_inode
);
922 err
= update_dent_inode(new_inode
, old_inode
, &old_dentry
->d_name
);
925 if (file_enc_name(old_inode
))
926 file_set_enc_name(new_inode
);
928 /* update ".." directory entry info of old dentry */
930 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
932 /* update ".." directory entry info of new dentry */
934 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
936 /* update directory entry info of old dir inode */
937 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
939 down_write(&F2FS_I(old_inode
)->i_sem
);
940 file_lost_pino(old_inode
);
941 up_write(&F2FS_I(old_inode
)->i_sem
);
943 old_dir
->i_ctime
= current_time(old_dir
);
945 down_write(&F2FS_I(old_dir
)->i_sem
);
946 f2fs_i_links_write(old_dir
, old_nlink
> 0);
947 up_write(&F2FS_I(old_dir
)->i_sem
);
949 f2fs_mark_inode_dirty_sync(old_dir
);
951 /* update directory entry info of new dir inode */
952 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
954 down_write(&F2FS_I(new_inode
)->i_sem
);
955 file_lost_pino(new_inode
);
956 up_write(&F2FS_I(new_inode
)->i_sem
);
958 new_dir
->i_ctime
= current_time(new_dir
);
960 down_write(&F2FS_I(new_dir
)->i_sem
);
961 f2fs_i_links_write(new_dir
, new_nlink
> 0);
962 up_write(&F2FS_I(new_dir
)->i_sem
);
964 f2fs_mark_inode_dirty_sync(new_dir
);
968 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
969 f2fs_sync_fs(sbi
->sb
, 1);
973 * Still we may fail to recover name info of f2fs_inode here
974 * Drop it, once its name is set as encrypted
976 update_dent_inode(old_inode
, old_inode
, &old_dentry
->d_name
);
981 f2fs_dentry_kunmap(new_inode
, new_dir_page
);
982 f2fs_put_page(new_dir_page
, 0);
986 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
987 f2fs_put_page(old_dir_page
, 0);
990 f2fs_dentry_kunmap(new_dir
, new_page
);
991 f2fs_put_page(new_page
, 0);
993 f2fs_dentry_kunmap(old_dir
, old_page
);
994 f2fs_put_page(old_page
, 0);
999 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
1000 struct inode
*new_dir
, struct dentry
*new_dentry
,
1003 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
1006 if (flags
& RENAME_EXCHANGE
) {
1007 return f2fs_cross_rename(old_dir
, old_dentry
,
1008 new_dir
, new_dentry
);
1011 * VFS has already handled the new dentry existence case,
1012 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1014 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
1017 static const char *f2fs_encrypted_get_link(struct dentry
*dentry
,
1018 struct inode
*inode
,
1019 struct delayed_call
*done
)
1021 struct page
*cpage
= NULL
;
1022 char *caddr
, *paddr
= NULL
;
1023 struct fscrypt_str cstr
= FSTR_INIT(NULL
, 0);
1024 struct fscrypt_str pstr
= FSTR_INIT(NULL
, 0);
1025 struct fscrypt_symlink_data
*sd
;
1026 u32 max_size
= inode
->i_sb
->s_blocksize
;
1030 return ERR_PTR(-ECHILD
);
1032 res
= fscrypt_get_encryption_info(inode
);
1034 return ERR_PTR(res
);
1036 cpage
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1038 return ERR_CAST(cpage
);
1039 caddr
= page_address(cpage
);
1041 /* Symlink is encrypted */
1042 sd
= (struct fscrypt_symlink_data
*)caddr
;
1043 cstr
.name
= sd
->encrypted_path
;
1044 cstr
.len
= le16_to_cpu(sd
->len
);
1046 /* this is broken symlink case */
1047 if (unlikely(cstr
.len
== 0)) {
1052 if ((cstr
.len
+ sizeof(struct fscrypt_symlink_data
) - 1) > max_size
) {
1053 /* Symlink data on the disk is corrupted */
1057 res
= fscrypt_fname_alloc_buffer(inode
, cstr
.len
, &pstr
);
1061 res
= fscrypt_fname_disk_to_usr(inode
, 0, 0, &cstr
, &pstr
);
1065 /* this is broken symlink case */
1066 if (unlikely(pstr
.name
[0] == 0)) {
1073 /* Null-terminate the name */
1074 paddr
[pstr
.len
] = '\0';
1077 set_delayed_call(done
, kfree_link
, paddr
);
1080 fscrypt_fname_free_buffer(&pstr
);
1082 return ERR_PTR(res
);
1085 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1086 .readlink
= generic_readlink
,
1087 .get_link
= f2fs_encrypted_get_link
,
1088 .getattr
= f2fs_getattr
,
1089 .setattr
= f2fs_setattr
,
1090 #ifdef CONFIG_F2FS_FS_XATTR
1091 .listxattr
= f2fs_listxattr
,
1095 const struct inode_operations f2fs_dir_inode_operations
= {
1096 .create
= f2fs_create
,
1097 .lookup
= f2fs_lookup
,
1099 .unlink
= f2fs_unlink
,
1100 .symlink
= f2fs_symlink
,
1101 .mkdir
= f2fs_mkdir
,
1102 .rmdir
= f2fs_rmdir
,
1103 .mknod
= f2fs_mknod
,
1104 .rename
= f2fs_rename2
,
1105 .tmpfile
= f2fs_tmpfile
,
1106 .getattr
= f2fs_getattr
,
1107 .setattr
= f2fs_setattr
,
1108 .get_acl
= f2fs_get_acl
,
1109 .set_acl
= f2fs_set_acl
,
1110 #ifdef CONFIG_F2FS_FS_XATTR
1111 .listxattr
= f2fs_listxattr
,
1115 const struct inode_operations f2fs_symlink_inode_operations
= {
1116 .readlink
= generic_readlink
,
1117 .get_link
= f2fs_get_link
,
1118 .getattr
= f2fs_getattr
,
1119 .setattr
= f2fs_setattr
,
1120 #ifdef CONFIG_F2FS_FS_XATTR
1121 .listxattr
= f2fs_listxattr
,
1125 const struct inode_operations f2fs_special_inode_operations
= {
1126 .getattr
= f2fs_getattr
,
1127 .setattr
= f2fs_setattr
,
1128 .get_acl
= f2fs_get_acl
,
1129 .set_acl
= f2fs_set_acl
,
1130 #ifdef CONFIG_F2FS_FS_XATTR
1131 .listxattr
= f2fs_listxattr
,