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 if (test_opt(sbi
, INLINE_DATA
) && f2fs_may_inline_data(inode
))
64 set_inode_flag(F2FS_I(inode
), FI_INLINE_DATA
);
65 if (f2fs_may_inline_dentry(inode
))
66 set_inode_flag(F2FS_I(inode
), FI_INLINE_DENTRY
);
68 f2fs_init_extent_tree(inode
, NULL
);
70 stat_inc_inline_xattr(inode
);
71 stat_inc_inline_inode(inode
);
72 stat_inc_inline_dir(inode
);
74 trace_f2fs_new_inode(inode
, 0);
75 mark_inode_dirty(inode
);
79 trace_f2fs_new_inode(inode
, err
);
80 make_bad_inode(inode
);
82 set_inode_flag(F2FS_I(inode
), FI_FREE_NID
);
87 static int is_multimedia_file(const unsigned char *s
, const char *sub
)
89 size_t slen
= strlen(s
);
90 size_t sublen
= strlen(sub
);
93 * filename format of multimedia file should be defined as:
94 * "filename + '.' + extension".
96 if (slen
< sublen
+ 2)
99 if (s
[slen
- sublen
- 1] != '.')
102 return !strncasecmp(s
+ slen
- sublen
, sub
, sublen
);
106 * Set multimedia files as cold files for hot/cold data separation
108 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
109 const unsigned char *name
)
112 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
114 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
115 for (i
= 0; i
< count
; i
++) {
116 if (is_multimedia_file(name
, extlist
[i
])) {
117 file_set_cold(inode
);
123 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
126 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
131 inode
= f2fs_new_inode(dir
, mode
);
133 return PTR_ERR(inode
);
135 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
136 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
138 inode
->i_op
= &f2fs_file_inode_operations
;
139 inode
->i_fop
= &f2fs_file_operations
;
140 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
143 f2fs_balance_fs(sbi
, true);
146 err
= f2fs_add_link(dentry
, inode
);
151 alloc_nid_done(sbi
, ino
);
153 d_instantiate(dentry
, inode
);
154 unlock_new_inode(inode
);
157 f2fs_sync_fs(sbi
->sb
, 1);
160 handle_failed_inode(inode
);
164 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
165 struct dentry
*dentry
)
167 struct inode
*inode
= d_inode(old_dentry
);
168 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
171 if (f2fs_encrypted_inode(dir
) &&
172 !fscrypt_has_permitted_context(dir
, inode
))
175 f2fs_balance_fs(sbi
, true);
177 inode
->i_ctime
= CURRENT_TIME
;
180 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
182 err
= f2fs_add_link(dentry
, inode
);
187 d_instantiate(dentry
, inode
);
190 f2fs_sync_fs(sbi
->sb
, 1);
193 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
199 struct dentry
*f2fs_get_parent(struct dentry
*child
)
201 struct qstr dotdot
= QSTR_INIT("..", 2);
202 unsigned long ino
= f2fs_inode_by_name(d_inode(child
), &dotdot
);
204 return ERR_PTR(-ENOENT
);
205 return d_obtain_alias(f2fs_iget(d_inode(child
)->i_sb
, ino
));
208 static int __recover_dot_dentries(struct inode
*dir
, nid_t pino
)
210 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
211 struct qstr dot
= QSTR_INIT(".", 1);
212 struct qstr dotdot
= QSTR_INIT("..", 2);
213 struct f2fs_dir_entry
*de
;
217 if (f2fs_readonly(sbi
->sb
)) {
218 f2fs_msg(sbi
->sb
, KERN_INFO
,
219 "skip recovering inline_dots inode (ino:%lu, pino:%u) "
220 "in readonly mountpoint", dir
->i_ino
, pino
);
224 f2fs_balance_fs(sbi
, true);
228 de
= f2fs_find_entry(dir
, &dot
, &page
);
230 f2fs_dentry_kunmap(dir
, page
);
231 f2fs_put_page(page
, 0);
233 err
= __f2fs_add_link(dir
, &dot
, NULL
, dir
->i_ino
, S_IFDIR
);
238 de
= f2fs_find_entry(dir
, &dotdot
, &page
);
240 f2fs_dentry_kunmap(dir
, page
);
241 f2fs_put_page(page
, 0);
243 err
= __f2fs_add_link(dir
, &dotdot
, NULL
, pino
, S_IFDIR
);
247 clear_inode_flag(F2FS_I(dir
), FI_INLINE_DOTS
);
248 mark_inode_dirty(dir
);
255 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
258 struct inode
*inode
= NULL
;
259 struct f2fs_dir_entry
*de
;
263 unsigned int root_ino
= F2FS_ROOT_INO(F2FS_I_SB(dir
));
265 if (f2fs_encrypted_inode(dir
)) {
266 int res
= fscrypt_get_encryption_info(dir
);
269 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
270 * created while the directory was encrypted and we
271 * don't have access to the key.
273 if (fscrypt_has_encryption_key(dir
))
274 fscrypt_set_encrypted_dentry(dentry
);
275 fscrypt_set_d_op(dentry
);
276 if (res
&& res
!= -ENOKEY
)
280 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
281 return ERR_PTR(-ENAMETOOLONG
);
283 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
285 return d_splice_alias(inode
, dentry
);
287 ino
= le32_to_cpu(de
->ino
);
288 f2fs_dentry_kunmap(dir
, page
);
289 f2fs_put_page(page
, 0);
291 inode
= f2fs_iget(dir
->i_sb
, ino
);
293 return ERR_CAST(inode
);
295 if ((dir
->i_ino
== root_ino
) && f2fs_has_inline_dots(dir
)) {
296 err
= __recover_dot_dentries(dir
, root_ino
);
301 if (f2fs_has_inline_dots(inode
)) {
302 err
= __recover_dot_dentries(inode
, dir
->i_ino
);
306 if (!IS_ERR(inode
) && f2fs_encrypted_inode(dir
) &&
307 (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
308 !fscrypt_has_permitted_context(dir
, inode
)) {
309 bool nokey
= f2fs_encrypted_inode(inode
) &&
310 !fscrypt_has_encryption_key(inode
);
311 err
= nokey
? -ENOKEY
: -EPERM
;
314 return d_splice_alias(inode
, dentry
);
321 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
323 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
324 struct inode
*inode
= d_inode(dentry
);
325 struct f2fs_dir_entry
*de
;
329 trace_f2fs_unlink_enter(dir
, dentry
);
331 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
335 f2fs_balance_fs(sbi
, true);
338 err
= acquire_orphan_inode(sbi
);
341 f2fs_dentry_kunmap(dir
, page
);
342 f2fs_put_page(page
, 0);
345 f2fs_delete_entry(de
, page
, dir
, inode
);
348 /* In order to evict this inode, we set it dirty */
349 mark_inode_dirty(inode
);
352 f2fs_sync_fs(sbi
->sb
, 1);
354 trace_f2fs_unlink_exit(inode
, err
);
358 static const char *f2fs_get_link(struct dentry
*dentry
,
360 struct delayed_call
*done
)
362 const char *link
= page_get_link(dentry
, inode
, done
);
363 if (!IS_ERR(link
) && !*link
) {
364 /* this is broken symlink case */
365 do_delayed_call(done
);
366 clear_delayed_call(done
);
367 link
= ERR_PTR(-ENOENT
);
372 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
375 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
377 size_t len
= strlen(symname
);
378 struct fscrypt_str disk_link
= FSTR_INIT((char *)symname
, len
+ 1);
379 struct fscrypt_symlink_data
*sd
= NULL
;
382 if (f2fs_encrypted_inode(dir
)) {
383 err
= fscrypt_get_encryption_info(dir
);
387 if (!fscrypt_has_encryption_key(dir
))
390 disk_link
.len
= (fscrypt_fname_encrypted_size(dir
, len
) +
391 sizeof(struct fscrypt_symlink_data
));
394 if (disk_link
.len
> dir
->i_sb
->s_blocksize
)
395 return -ENAMETOOLONG
;
397 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
399 return PTR_ERR(inode
);
401 if (f2fs_encrypted_inode(inode
))
402 inode
->i_op
= &f2fs_encrypted_symlink_inode_operations
;
404 inode
->i_op
= &f2fs_symlink_inode_operations
;
405 inode_nohighmem(inode
);
406 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
408 f2fs_balance_fs(sbi
, true);
411 err
= f2fs_add_link(dentry
, inode
);
415 alloc_nid_done(sbi
, inode
->i_ino
);
417 if (f2fs_encrypted_inode(inode
)) {
418 struct qstr istr
= QSTR_INIT(symname
, len
);
419 struct fscrypt_str ostr
;
421 sd
= kzalloc(disk_link
.len
, GFP_NOFS
);
427 err
= fscrypt_get_encryption_info(inode
);
431 if (!fscrypt_has_encryption_key(inode
)) {
436 ostr
.name
= sd
->encrypted_path
;
437 ostr
.len
= disk_link
.len
;
438 err
= fscrypt_fname_usr_to_disk(inode
, &istr
, &ostr
);
442 sd
->len
= cpu_to_le16(ostr
.len
);
443 disk_link
.name
= (char *)sd
;
446 err
= page_symlink(inode
, disk_link
.name
, disk_link
.len
);
449 d_instantiate(dentry
, inode
);
450 unlock_new_inode(inode
);
453 * Let's flush symlink data in order to avoid broken symlink as much as
454 * possible. Nevertheless, fsyncing is the best way, but there is no
455 * way to get a file descriptor in order to flush that.
457 * Note that, it needs to do dir->fsync to make this recoverable.
458 * If the symlink path is stored into inline_data, there is no
459 * performance regression.
462 filemap_write_and_wait_range(inode
->i_mapping
, 0,
466 f2fs_sync_fs(sbi
->sb
, 1);
468 f2fs_unlink(dir
, dentry
);
474 handle_failed_inode(inode
);
478 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
480 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
484 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
486 return PTR_ERR(inode
);
488 inode
->i_op
= &f2fs_dir_inode_operations
;
489 inode
->i_fop
= &f2fs_dir_operations
;
490 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
491 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_HIGH_ZERO
);
493 f2fs_balance_fs(sbi
, true);
495 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
497 err
= f2fs_add_link(dentry
, inode
);
502 alloc_nid_done(sbi
, inode
->i_ino
);
504 d_instantiate(dentry
, inode
);
505 unlock_new_inode(inode
);
508 f2fs_sync_fs(sbi
->sb
, 1);
512 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
513 handle_failed_inode(inode
);
517 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
519 struct inode
*inode
= d_inode(dentry
);
520 if (f2fs_empty_dir(inode
))
521 return f2fs_unlink(dir
, dentry
);
525 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
526 umode_t mode
, dev_t rdev
)
528 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
532 inode
= f2fs_new_inode(dir
, mode
);
534 return PTR_ERR(inode
);
536 init_special_inode(inode
, inode
->i_mode
, rdev
);
537 inode
->i_op
= &f2fs_special_inode_operations
;
539 f2fs_balance_fs(sbi
, true);
542 err
= f2fs_add_link(dentry
, inode
);
547 alloc_nid_done(sbi
, inode
->i_ino
);
549 d_instantiate(dentry
, inode
);
550 unlock_new_inode(inode
);
553 f2fs_sync_fs(sbi
->sb
, 1);
556 handle_failed_inode(inode
);
560 static int __f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
561 umode_t mode
, struct inode
**whiteout
)
563 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
567 inode
= f2fs_new_inode(dir
, mode
);
569 return PTR_ERR(inode
);
572 init_special_inode(inode
, inode
->i_mode
, WHITEOUT_DEV
);
573 inode
->i_op
= &f2fs_special_inode_operations
;
575 inode
->i_op
= &f2fs_file_inode_operations
;
576 inode
->i_fop
= &f2fs_file_operations
;
577 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
580 f2fs_balance_fs(sbi
, true);
583 err
= acquire_orphan_inode(sbi
);
587 err
= f2fs_do_tmpfile(inode
, dir
);
592 * add this non-linked tmpfile to orphan list, in this way we could
593 * remove all unused data of tmpfile after abnormal power-off.
595 add_orphan_inode(sbi
, inode
->i_ino
);
598 alloc_nid_done(sbi
, inode
->i_ino
);
601 inode_dec_link_count(inode
);
604 d_tmpfile(dentry
, inode
);
606 unlock_new_inode(inode
);
610 release_orphan_inode(sbi
);
612 handle_failed_inode(inode
);
616 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
618 if (f2fs_encrypted_inode(dir
)) {
619 int err
= fscrypt_get_encryption_info(dir
);
624 return __f2fs_tmpfile(dir
, dentry
, mode
, NULL
);
627 static int f2fs_create_whiteout(struct inode
*dir
, struct inode
**whiteout
)
629 return __f2fs_tmpfile(dir
, NULL
, S_IFCHR
| WHITEOUT_MODE
, whiteout
);
632 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
633 struct inode
*new_dir
, struct dentry
*new_dentry
,
636 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
637 struct inode
*old_inode
= d_inode(old_dentry
);
638 struct inode
*new_inode
= d_inode(new_dentry
);
639 struct inode
*whiteout
= NULL
;
640 struct page
*old_dir_page
;
641 struct page
*old_page
, *new_page
= NULL
;
642 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
643 struct f2fs_dir_entry
*old_entry
;
644 struct f2fs_dir_entry
*new_entry
;
645 bool is_old_inline
= f2fs_has_inline_dentry(old_dir
);
648 if ((old_dir
!= new_dir
) && f2fs_encrypted_inode(new_dir
) &&
649 !fscrypt_has_permitted_context(new_dir
, old_inode
)) {
654 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
658 if (S_ISDIR(old_inode
->i_mode
)) {
660 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
665 if (flags
& RENAME_WHITEOUT
) {
666 err
= f2fs_create_whiteout(old_dir
, &whiteout
);
674 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
678 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
683 f2fs_balance_fs(sbi
, true);
687 err
= acquire_orphan_inode(sbi
);
691 err
= update_dent_inode(old_inode
, new_inode
,
692 &new_dentry
->d_name
);
694 release_orphan_inode(sbi
);
698 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
700 new_inode
->i_ctime
= CURRENT_TIME
;
701 down_write(&F2FS_I(new_inode
)->i_sem
);
703 drop_nlink(new_inode
);
704 drop_nlink(new_inode
);
705 up_write(&F2FS_I(new_inode
)->i_sem
);
707 mark_inode_dirty(new_inode
);
709 if (!new_inode
->i_nlink
)
710 add_orphan_inode(sbi
, new_inode
->i_ino
);
712 release_orphan_inode(sbi
);
714 update_inode_page(old_inode
);
715 update_inode_page(new_inode
);
717 f2fs_balance_fs(sbi
, true);
721 err
= f2fs_add_link(new_dentry
, old_inode
);
729 update_inode_page(new_dir
);
733 * old entry and new entry can locate in the same inline
734 * dentry in inode, when attaching new entry in inline dentry,
735 * it could force inline dentry conversion, after that,
736 * old_entry and old_page will point to wrong address, in
737 * order to avoid this, let's do the check and update here.
739 if (is_old_inline
&& !f2fs_has_inline_dentry(old_dir
)) {
740 f2fs_put_page(old_page
, 0);
743 old_entry
= f2fs_find_entry(old_dir
,
744 &old_dentry
->d_name
, &old_page
);
753 down_write(&F2FS_I(old_inode
)->i_sem
);
754 file_lost_pino(old_inode
);
755 if (new_inode
&& file_enc_name(new_inode
))
756 file_set_enc_name(old_inode
);
757 up_write(&F2FS_I(old_inode
)->i_sem
);
759 old_inode
->i_ctime
= CURRENT_TIME
;
760 mark_inode_dirty(old_inode
);
762 f2fs_delete_entry(old_entry
, old_page
, old_dir
, NULL
);
765 whiteout
->i_state
|= I_LINKABLE
;
766 set_inode_flag(F2FS_I(whiteout
), FI_INC_LINK
);
767 err
= f2fs_add_link(old_dentry
, whiteout
);
770 whiteout
->i_state
&= ~I_LINKABLE
;
775 if (old_dir
!= new_dir
&& !whiteout
) {
776 f2fs_set_link(old_inode
, old_dir_entry
,
777 old_dir_page
, new_dir
);
778 update_inode_page(old_inode
);
780 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
781 f2fs_put_page(old_dir_page
, 0);
784 mark_inode_dirty(old_dir
);
785 update_inode_page(old_dir
);
790 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
791 f2fs_sync_fs(sbi
->sb
, 1);
797 f2fs_dentry_kunmap(new_dir
, new_page
);
798 f2fs_put_page(new_page
, 0);
805 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
806 f2fs_put_page(old_dir_page
, 0);
809 f2fs_dentry_kunmap(old_dir
, old_page
);
810 f2fs_put_page(old_page
, 0);
815 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
816 struct inode
*new_dir
, struct dentry
*new_dentry
)
818 struct f2fs_sb_info
*sbi
= F2FS_I_SB(old_dir
);
819 struct inode
*old_inode
= d_inode(old_dentry
);
820 struct inode
*new_inode
= d_inode(new_dentry
);
821 struct page
*old_dir_page
, *new_dir_page
;
822 struct page
*old_page
, *new_page
;
823 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
824 struct f2fs_dir_entry
*old_entry
, *new_entry
;
825 int old_nlink
= 0, new_nlink
= 0;
828 if ((f2fs_encrypted_inode(old_dir
) || f2fs_encrypted_inode(new_dir
)) &&
829 (old_dir
!= new_dir
) &&
830 (!fscrypt_has_permitted_context(new_dir
, old_inode
) ||
831 !fscrypt_has_permitted_context(old_dir
, new_inode
)))
834 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
838 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
842 /* prepare for updating ".." directory entry info later */
843 if (old_dir
!= new_dir
) {
844 if (S_ISDIR(old_inode
->i_mode
)) {
846 old_dir_entry
= f2fs_parent_dir(old_inode
,
852 if (S_ISDIR(new_inode
->i_mode
)) {
854 new_dir_entry
= f2fs_parent_dir(new_inode
,
862 * If cross rename between file and directory those are not
863 * in the same directory, we will inc nlink of file's parent
864 * later, so we should check upper boundary of its nlink.
866 if ((!old_dir_entry
|| !new_dir_entry
) &&
867 old_dir_entry
!= new_dir_entry
) {
868 old_nlink
= old_dir_entry
? -1 : 1;
869 new_nlink
= -old_nlink
;
871 if ((old_nlink
> 0 && old_inode
->i_nlink
>= F2FS_LINK_MAX
) ||
872 (new_nlink
> 0 && new_inode
->i_nlink
>= F2FS_LINK_MAX
))
876 f2fs_balance_fs(sbi
, true);
880 err
= update_dent_inode(old_inode
, new_inode
, &new_dentry
->d_name
);
883 if (file_enc_name(new_inode
))
884 file_set_enc_name(old_inode
);
886 err
= update_dent_inode(new_inode
, old_inode
, &old_dentry
->d_name
);
889 if (file_enc_name(old_inode
))
890 file_set_enc_name(new_inode
);
892 /* update ".." directory entry info of old dentry */
894 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
896 /* update ".." directory entry info of new dentry */
898 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
900 /* update directory entry info of old dir inode */
901 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
903 down_write(&F2FS_I(old_inode
)->i_sem
);
904 file_lost_pino(old_inode
);
905 up_write(&F2FS_I(old_inode
)->i_sem
);
907 update_inode_page(old_inode
);
909 old_dir
->i_ctime
= CURRENT_TIME
;
911 down_write(&F2FS_I(old_dir
)->i_sem
);
916 up_write(&F2FS_I(old_dir
)->i_sem
);
918 mark_inode_dirty(old_dir
);
919 update_inode_page(old_dir
);
921 /* update directory entry info of new dir inode */
922 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
924 down_write(&F2FS_I(new_inode
)->i_sem
);
925 file_lost_pino(new_inode
);
926 up_write(&F2FS_I(new_inode
)->i_sem
);
928 update_inode_page(new_inode
);
930 new_dir
->i_ctime
= CURRENT_TIME
;
932 down_write(&F2FS_I(new_dir
)->i_sem
);
937 up_write(&F2FS_I(new_dir
)->i_sem
);
939 mark_inode_dirty(new_dir
);
940 update_inode_page(new_dir
);
944 if (IS_DIRSYNC(old_dir
) || IS_DIRSYNC(new_dir
))
945 f2fs_sync_fs(sbi
->sb
, 1);
949 * Still we may fail to recover name info of f2fs_inode here
950 * Drop it, once its name is set as encrypted
952 update_dent_inode(old_inode
, old_inode
, &old_dentry
->d_name
);
957 f2fs_dentry_kunmap(new_inode
, new_dir_page
);
958 f2fs_put_page(new_dir_page
, 0);
962 f2fs_dentry_kunmap(old_inode
, old_dir_page
);
963 f2fs_put_page(old_dir_page
, 0);
966 f2fs_dentry_kunmap(new_dir
, new_page
);
967 f2fs_put_page(new_page
, 0);
969 f2fs_dentry_kunmap(old_dir
, old_page
);
970 f2fs_put_page(old_page
, 0);
975 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
976 struct inode
*new_dir
, struct dentry
*new_dentry
,
979 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
982 if (flags
& RENAME_EXCHANGE
) {
983 return f2fs_cross_rename(old_dir
, old_dentry
,
984 new_dir
, new_dentry
);
987 * VFS has already handled the new dentry existence case,
988 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
990 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
993 static const char *f2fs_encrypted_get_link(struct dentry
*dentry
,
995 struct delayed_call
*done
)
997 struct page
*cpage
= NULL
;
998 char *caddr
, *paddr
= NULL
;
999 struct fscrypt_str cstr
= FSTR_INIT(NULL
, 0);
1000 struct fscrypt_str pstr
= FSTR_INIT(NULL
, 0);
1001 struct fscrypt_symlink_data
*sd
;
1002 loff_t size
= min_t(loff_t
, i_size_read(inode
), PAGE_SIZE
- 1);
1003 u32 max_size
= inode
->i_sb
->s_blocksize
;
1007 return ERR_PTR(-ECHILD
);
1009 res
= fscrypt_get_encryption_info(inode
);
1011 return ERR_PTR(res
);
1013 cpage
= read_mapping_page(inode
->i_mapping
, 0, NULL
);
1015 return ERR_CAST(cpage
);
1016 caddr
= page_address(cpage
);
1019 /* Symlink is encrypted */
1020 sd
= (struct fscrypt_symlink_data
*)caddr
;
1021 cstr
.name
= sd
->encrypted_path
;
1022 cstr
.len
= le16_to_cpu(sd
->len
);
1024 /* this is broken symlink case */
1025 if (unlikely(cstr
.len
== 0)) {
1030 if ((cstr
.len
+ sizeof(struct fscrypt_symlink_data
) - 1) > max_size
) {
1031 /* Symlink data on the disk is corrupted */
1035 res
= fscrypt_fname_alloc_buffer(inode
, cstr
.len
, &pstr
);
1039 res
= fscrypt_fname_disk_to_usr(inode
, 0, 0, &cstr
, &pstr
);
1043 /* this is broken symlink case */
1044 if (unlikely(pstr
.name
[0] == 0)) {
1051 /* Null-terminate the name */
1055 set_delayed_call(done
, kfree_link
, paddr
);
1058 fscrypt_fname_free_buffer(&pstr
);
1060 return ERR_PTR(res
);
1063 const struct inode_operations f2fs_encrypted_symlink_inode_operations
= {
1064 .readlink
= generic_readlink
,
1065 .get_link
= f2fs_encrypted_get_link
,
1066 .getattr
= f2fs_getattr
,
1067 .setattr
= f2fs_setattr
,
1068 #ifdef CONFIG_F2FS_FS_XATTR
1069 .setxattr
= generic_setxattr
,
1070 .getxattr
= generic_getxattr
,
1071 .listxattr
= f2fs_listxattr
,
1072 .removexattr
= generic_removexattr
,
1076 const struct inode_operations f2fs_dir_inode_operations
= {
1077 .create
= f2fs_create
,
1078 .lookup
= f2fs_lookup
,
1080 .unlink
= f2fs_unlink
,
1081 .symlink
= f2fs_symlink
,
1082 .mkdir
= f2fs_mkdir
,
1083 .rmdir
= f2fs_rmdir
,
1084 .mknod
= f2fs_mknod
,
1085 .rename2
= f2fs_rename2
,
1086 .tmpfile
= f2fs_tmpfile
,
1087 .getattr
= f2fs_getattr
,
1088 .setattr
= f2fs_setattr
,
1089 .get_acl
= f2fs_get_acl
,
1090 .set_acl
= f2fs_set_acl
,
1091 #ifdef CONFIG_F2FS_FS_XATTR
1092 .setxattr
= generic_setxattr
,
1093 .getxattr
= generic_getxattr
,
1094 .listxattr
= f2fs_listxattr
,
1095 .removexattr
= generic_removexattr
,
1099 const struct inode_operations f2fs_symlink_inode_operations
= {
1100 .readlink
= generic_readlink
,
1101 .get_link
= f2fs_get_link
,
1102 .getattr
= f2fs_getattr
,
1103 .setattr
= f2fs_setattr
,
1104 #ifdef CONFIG_F2FS_FS_XATTR
1105 .setxattr
= generic_setxattr
,
1106 .getxattr
= generic_getxattr
,
1107 .listxattr
= f2fs_listxattr
,
1108 .removexattr
= generic_removexattr
,
1112 const struct inode_operations f2fs_special_inode_operations
= {
1113 .getattr
= f2fs_getattr
,
1114 .setattr
= f2fs_setattr
,
1115 .get_acl
= f2fs_get_acl
,
1116 .set_acl
= f2fs_set_acl
,
1117 #ifdef CONFIG_F2FS_FS_XATTR
1118 .setxattr
= generic_setxattr
,
1119 .getxattr
= generic_getxattr
,
1120 .listxattr
= f2fs_listxattr
,
1121 .removexattr
= generic_removexattr
,