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>
22 #include <trace/events/f2fs.h>
24 static struct inode
*f2fs_new_inode(struct inode
*dir
, umode_t mode
)
26 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
29 bool nid_free
= false;
32 inode
= new_inode(dir
->i_sb
);
34 return ERR_PTR(-ENOMEM
);
37 if (!alloc_nid(sbi
, &ino
)) {
44 inode_init_owner(inode
, dir
, mode
);
48 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
49 inode
->i_generation
= sbi
->s_next_generation
++;
51 err
= insert_inode_locked(inode
);
57 trace_f2fs_new_inode(inode
, 0);
58 mark_inode_dirty(inode
);
63 unlock_new_inode(inode
);
65 trace_f2fs_new_inode(inode
, err
);
66 make_bad_inode(inode
);
69 alloc_nid_failed(sbi
, ino
);
73 static int is_multimedia_file(const unsigned char *s
, const char *sub
)
75 size_t slen
= strlen(s
);
76 size_t sublen
= strlen(sub
);
81 return !strncasecmp(s
+ slen
- sublen
, sub
, sublen
);
85 * Set multimedia files as cold files for hot/cold data separation
87 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
88 const unsigned char *name
)
91 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
93 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
94 for (i
= 0; i
< count
; i
++) {
95 if (is_multimedia_file(name
, extlist
[i
])) {
102 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
105 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
110 f2fs_balance_fs(sbi
);
112 inode
= f2fs_new_inode(dir
, mode
);
114 return PTR_ERR(inode
);
116 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
117 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
119 inode
->i_op
= &f2fs_file_inode_operations
;
120 inode
->i_fop
= &f2fs_file_operations
;
121 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
125 err
= f2fs_add_link(dentry
, inode
);
130 alloc_nid_done(sbi
, ino
);
132 d_instantiate(dentry
, inode
);
133 unlock_new_inode(inode
);
137 unlock_new_inode(inode
);
138 make_bad_inode(inode
);
140 alloc_nid_failed(sbi
, ino
);
144 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
145 struct dentry
*dentry
)
147 struct inode
*inode
= old_dentry
->d_inode
;
148 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
151 f2fs_balance_fs(sbi
);
153 inode
->i_ctime
= CURRENT_TIME
;
156 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
158 err
= f2fs_add_link(dentry
, inode
);
163 d_instantiate(dentry
, inode
);
166 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
171 struct dentry
*f2fs_get_parent(struct dentry
*child
)
173 struct qstr dotdot
= QSTR_INIT("..", 2);
174 unsigned long ino
= f2fs_inode_by_name(child
->d_inode
, &dotdot
);
176 return ERR_PTR(-ENOENT
);
177 return d_obtain_alias(f2fs_iget(child
->d_inode
->i_sb
, ino
));
180 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
183 struct inode
*inode
= NULL
;
184 struct f2fs_dir_entry
*de
;
187 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
188 return ERR_PTR(-ENAMETOOLONG
);
190 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
192 nid_t ino
= le32_to_cpu(de
->ino
);
194 f2fs_put_page(page
, 0);
196 inode
= f2fs_iget(dir
->i_sb
, ino
);
198 return ERR_CAST(inode
);
200 stat_inc_inline_inode(inode
);
203 return d_splice_alias(inode
, dentry
);
206 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
208 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
209 struct inode
*inode
= dentry
->d_inode
;
210 struct f2fs_dir_entry
*de
;
214 trace_f2fs_unlink_enter(dir
, dentry
);
215 f2fs_balance_fs(sbi
);
217 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
222 err
= acquire_orphan_inode(sbi
);
226 f2fs_put_page(page
, 0);
229 f2fs_delete_entry(de
, page
, inode
);
232 /* In order to evict this inode, we set it dirty */
233 mark_inode_dirty(inode
);
235 trace_f2fs_unlink_exit(inode
, err
);
239 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
242 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
244 size_t symlen
= strlen(symname
) + 1;
247 f2fs_balance_fs(sbi
);
249 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
251 return PTR_ERR(inode
);
253 inode
->i_op
= &f2fs_symlink_inode_operations
;
254 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
257 err
= f2fs_add_link(dentry
, inode
);
262 err
= page_symlink(inode
, symname
, symlen
);
263 alloc_nid_done(sbi
, inode
->i_ino
);
265 d_instantiate(dentry
, inode
);
266 unlock_new_inode(inode
);
270 unlock_new_inode(inode
);
271 make_bad_inode(inode
);
273 alloc_nid_failed(sbi
, inode
->i_ino
);
277 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
279 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
283 f2fs_balance_fs(sbi
);
285 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
287 return PTR_ERR(inode
);
289 inode
->i_op
= &f2fs_dir_inode_operations
;
290 inode
->i_fop
= &f2fs_dir_operations
;
291 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
292 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_ZERO
);
294 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
296 err
= f2fs_add_link(dentry
, inode
);
301 alloc_nid_done(sbi
, inode
->i_ino
);
303 d_instantiate(dentry
, inode
);
304 unlock_new_inode(inode
);
309 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
311 unlock_new_inode(inode
);
312 make_bad_inode(inode
);
314 alloc_nid_failed(sbi
, inode
->i_ino
);
318 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
320 struct inode
*inode
= dentry
->d_inode
;
321 if (f2fs_empty_dir(inode
))
322 return f2fs_unlink(dir
, dentry
);
326 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
327 umode_t mode
, dev_t rdev
)
329 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
333 if (!new_valid_dev(rdev
))
336 f2fs_balance_fs(sbi
);
338 inode
= f2fs_new_inode(dir
, mode
);
340 return PTR_ERR(inode
);
342 init_special_inode(inode
, inode
->i_mode
, rdev
);
343 inode
->i_op
= &f2fs_special_inode_operations
;
346 err
= f2fs_add_link(dentry
, inode
);
351 alloc_nid_done(sbi
, inode
->i_ino
);
352 d_instantiate(dentry
, inode
);
353 unlock_new_inode(inode
);
357 unlock_new_inode(inode
);
358 make_bad_inode(inode
);
360 alloc_nid_failed(sbi
, inode
->i_ino
);
364 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
365 struct inode
*new_dir
, struct dentry
*new_dentry
)
367 struct f2fs_sb_info
*sbi
= F2FS_SB(old_dir
->i_sb
);
368 struct inode
*old_inode
= old_dentry
->d_inode
;
369 struct inode
*new_inode
= new_dentry
->d_inode
;
370 struct page
*old_dir_page
;
371 struct page
*old_page
, *new_page
;
372 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
373 struct f2fs_dir_entry
*old_entry
;
374 struct f2fs_dir_entry
*new_entry
;
377 f2fs_balance_fs(sbi
);
379 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
383 if (S_ISDIR(old_inode
->i_mode
)) {
385 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
393 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
397 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
404 err
= acquire_orphan_inode(sbi
);
408 if (update_dent_inode(old_inode
, &new_dentry
->d_name
)) {
409 release_orphan_inode(sbi
);
413 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
415 new_inode
->i_ctime
= CURRENT_TIME
;
416 down_write(&F2FS_I(new_inode
)->i_sem
);
418 drop_nlink(new_inode
);
419 drop_nlink(new_inode
);
420 up_write(&F2FS_I(new_inode
)->i_sem
);
422 mark_inode_dirty(new_inode
);
424 if (!new_inode
->i_nlink
)
425 add_orphan_inode(sbi
, new_inode
->i_ino
);
427 release_orphan_inode(sbi
);
429 update_inode_page(old_inode
);
430 update_inode_page(new_inode
);
434 err
= f2fs_add_link(new_dentry
, old_inode
);
442 update_inode_page(new_dir
);
446 down_write(&F2FS_I(old_inode
)->i_sem
);
447 file_lost_pino(old_inode
);
448 up_write(&F2FS_I(old_inode
)->i_sem
);
450 old_inode
->i_ctime
= CURRENT_TIME
;
451 mark_inode_dirty(old_inode
);
453 f2fs_delete_entry(old_entry
, old_page
, NULL
);
456 if (old_dir
!= new_dir
) {
457 f2fs_set_link(old_inode
, old_dir_entry
,
458 old_dir_page
, new_dir
);
459 update_inode_page(old_inode
);
461 kunmap(old_dir_page
);
462 f2fs_put_page(old_dir_page
, 0);
465 mark_inode_dirty(old_dir
);
466 update_inode_page(old_dir
);
475 f2fs_put_page(new_page
, 0);
478 kunmap(old_dir_page
);
479 f2fs_put_page(old_dir_page
, 0);
483 f2fs_put_page(old_page
, 0);
488 static int f2fs_cross_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
489 struct inode
*new_dir
, struct dentry
*new_dentry
)
491 struct super_block
*sb
= old_dir
->i_sb
;
492 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
493 struct inode
*old_inode
= old_dentry
->d_inode
;
494 struct inode
*new_inode
= new_dentry
->d_inode
;
495 struct page
*old_dir_page
, *new_dir_page
;
496 struct page
*old_page
, *new_page
;
497 struct f2fs_dir_entry
*old_dir_entry
= NULL
, *new_dir_entry
= NULL
;
498 struct f2fs_dir_entry
*old_entry
, *new_entry
;
499 int old_nlink
= 0, new_nlink
= 0;
502 f2fs_balance_fs(sbi
);
504 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
508 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
512 /* prepare for updating ".." directory entry info later */
513 if (old_dir
!= new_dir
) {
514 if (S_ISDIR(old_inode
->i_mode
)) {
516 old_dir_entry
= f2fs_parent_dir(old_inode
,
522 if (S_ISDIR(new_inode
->i_mode
)) {
524 new_dir_entry
= f2fs_parent_dir(new_inode
,
532 * If cross rename between file and directory those are not
533 * in the same directory, we will inc nlink of file's parent
534 * later, so we should check upper boundary of its nlink.
536 if ((!old_dir_entry
|| !new_dir_entry
) &&
537 old_dir_entry
!= new_dir_entry
) {
538 old_nlink
= old_dir_entry
? -1 : 1;
539 new_nlink
= -old_nlink
;
541 if ((old_nlink
> 0 && old_inode
->i_nlink
>= F2FS_LINK_MAX
) ||
542 (new_nlink
> 0 && new_inode
->i_nlink
>= F2FS_LINK_MAX
))
548 err
= update_dent_inode(old_inode
, &new_dentry
->d_name
);
552 err
= update_dent_inode(new_inode
, &old_dentry
->d_name
);
556 /* update ".." directory entry info of old dentry */
558 f2fs_set_link(old_inode
, old_dir_entry
, old_dir_page
, new_dir
);
560 /* update ".." directory entry info of new dentry */
562 f2fs_set_link(new_inode
, new_dir_entry
, new_dir_page
, old_dir
);
564 /* update directory entry info of old dir inode */
565 f2fs_set_link(old_dir
, old_entry
, old_page
, new_inode
);
567 down_write(&F2FS_I(old_inode
)->i_sem
);
568 file_lost_pino(old_inode
);
569 up_write(&F2FS_I(old_inode
)->i_sem
);
571 update_inode_page(old_inode
);
573 old_dir
->i_ctime
= CURRENT_TIME
;
575 down_write(&F2FS_I(old_dir
)->i_sem
);
580 up_write(&F2FS_I(old_dir
)->i_sem
);
582 mark_inode_dirty(old_dir
);
583 update_inode_page(old_dir
);
585 /* update directory entry info of new dir inode */
586 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
588 down_write(&F2FS_I(new_inode
)->i_sem
);
589 file_lost_pino(new_inode
);
590 up_write(&F2FS_I(new_inode
)->i_sem
);
592 update_inode_page(new_inode
);
594 new_dir
->i_ctime
= CURRENT_TIME
;
596 down_write(&F2FS_I(new_dir
)->i_sem
);
601 up_write(&F2FS_I(new_dir
)->i_sem
);
603 mark_inode_dirty(new_dir
);
604 update_inode_page(new_dir
);
609 /* Still we may fail to recover name info of f2fs_inode here */
610 update_dent_inode(old_inode
, &old_dentry
->d_name
);
615 kunmap(new_dir_page
);
616 f2fs_put_page(new_dir_page
, 0);
620 kunmap(old_dir_page
);
621 f2fs_put_page(old_dir_page
, 0);
625 f2fs_put_page(new_page
, 0);
628 f2fs_put_page(old_page
, 0);
633 static int f2fs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
634 struct inode
*new_dir
, struct dentry
*new_dentry
,
637 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
))
640 if (flags
& RENAME_EXCHANGE
) {
641 return f2fs_cross_rename(old_dir
, old_dentry
,
642 new_dir
, new_dentry
);
645 * VFS has already handled the new dentry existence case,
646 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
648 return f2fs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
651 static int f2fs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
653 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
657 inode
= f2fs_new_inode(dir
, mode
);
659 return PTR_ERR(inode
);
661 inode
->i_op
= &f2fs_file_inode_operations
;
662 inode
->i_fop
= &f2fs_file_operations
;
663 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
666 err
= acquire_orphan_inode(sbi
);
670 err
= f2fs_do_tmpfile(inode
, dir
);
675 * add this non-linked tmpfile to orphan list, in this way we could
676 * remove all unused data of tmpfile after abnormal power-off.
678 add_orphan_inode(sbi
, inode
->i_ino
);
681 alloc_nid_done(sbi
, inode
->i_ino
);
682 d_tmpfile(dentry
, inode
);
683 unlock_new_inode(inode
);
687 release_orphan_inode(sbi
);
691 unlock_new_inode(inode
);
692 make_bad_inode(inode
);
694 alloc_nid_failed(sbi
, inode
->i_ino
);
698 const struct inode_operations f2fs_dir_inode_operations
= {
699 .create
= f2fs_create
,
700 .lookup
= f2fs_lookup
,
702 .unlink
= f2fs_unlink
,
703 .symlink
= f2fs_symlink
,
707 .rename
= f2fs_rename
,
708 .rename2
= f2fs_rename2
,
709 .tmpfile
= f2fs_tmpfile
,
710 .getattr
= f2fs_getattr
,
711 .setattr
= f2fs_setattr
,
712 .get_acl
= f2fs_get_acl
,
713 .set_acl
= f2fs_set_acl
,
714 #ifdef CONFIG_F2FS_FS_XATTR
715 .setxattr
= generic_setxattr
,
716 .getxattr
= generic_getxattr
,
717 .listxattr
= f2fs_listxattr
,
718 .removexattr
= generic_removexattr
,
722 const struct inode_operations f2fs_symlink_inode_operations
= {
723 .readlink
= generic_readlink
,
724 .follow_link
= page_follow_link_light
,
725 .put_link
= page_put_link
,
726 .getattr
= f2fs_getattr
,
727 .setattr
= f2fs_setattr
,
728 #ifdef CONFIG_F2FS_FS_XATTR
729 .setxattr
= generic_setxattr
,
730 .getxattr
= generic_getxattr
,
731 .listxattr
= f2fs_listxattr
,
732 .removexattr
= generic_removexattr
,
736 const struct inode_operations f2fs_special_inode_operations
= {
737 .getattr
= f2fs_getattr
,
738 .setattr
= f2fs_setattr
,
739 .get_acl
= f2fs_get_acl
,
740 .set_acl
= f2fs_set_acl
,
741 #ifdef CONFIG_F2FS_FS_XATTR
742 .setxattr
= generic_setxattr
,
743 .getxattr
= generic_getxattr
,
744 .listxattr
= f2fs_listxattr
,
745 .removexattr
= generic_removexattr
,