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_I_SB(dir);
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);
58 if (f2fs_may_inline(inode))
59 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
60 if (test_opt(sbi, INLINE_DENTRY) && S_ISDIR(inode->i_mode))
61 set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);
63 trace_f2fs_new_inode(inode, 0);
64 mark_inode_dirty(inode);
69 unlock_new_inode(inode);
71 trace_f2fs_new_inode(inode, err);
72 make_bad_inode(inode);
75 alloc_nid_failed(sbi, ino);
79 static int is_multimedia_file(const unsigned char *s, const char *sub)
81 size_t slen = strlen(s);
82 size_t sublen = strlen(sub);
87 return !strncasecmp(s + slen - sublen, sub, sublen);
91 * Set multimedia files as cold files for hot/cold data separation
93 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
94 const unsigned char *name)
97 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
99 int count = le32_to_cpu(sbi->raw_super->extension_count);
100 for (i = 0; i < count; i++) {
101 if (is_multimedia_file(name, extlist[i])) {
102 file_set_cold(inode);
108 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
111 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
116 f2fs_balance_fs(sbi);
118 inode = f2fs_new_inode(dir, mode);
120 return PTR_ERR(inode);
122 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
123 set_cold_files(sbi, inode, dentry->d_name.name);
125 inode->i_op = &f2fs_file_inode_operations;
126 inode->i_fop = &f2fs_file_operations;
127 inode->i_mapping->a_ops = &f2fs_dblock_aops;
131 err = f2fs_add_link(dentry, inode);
136 alloc_nid_done(sbi, ino);
138 stat_inc_inline_inode(inode);
139 d_instantiate(dentry, inode);
140 unlock_new_inode(inode);
143 f2fs_sync_fs(sbi->sb, 1);
146 handle_failed_inode(inode);
150 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
151 struct dentry *dentry)
153 struct inode *inode = old_dentry->d_inode;
154 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
157 f2fs_balance_fs(sbi);
159 inode->i_ctime = CURRENT_TIME;
162 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
164 err = f2fs_add_link(dentry, inode);
169 d_instantiate(dentry, inode);
172 f2fs_sync_fs(sbi->sb, 1);
175 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
181 struct dentry *f2fs_get_parent(struct dentry *child)
183 struct qstr dotdot = QSTR_INIT("..", 2);
184 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
186 return ERR_PTR(-ENOENT);
187 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
190 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
192 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
193 struct qstr dot = QSTR_INIT(".", 1);
194 struct qstr dotdot = QSTR_INIT("..", 2);
195 struct f2fs_dir_entry *de;
201 de = f2fs_find_entry(dir, &dot, &page);
203 f2fs_dentry_kunmap(dir, page);
204 f2fs_put_page(page, 0);
206 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
211 de = f2fs_find_entry(dir, &dotdot, &page);
213 f2fs_dentry_kunmap(dir, page);
214 f2fs_put_page(page, 0);
216 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
220 clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
221 mark_inode_dirty(dir);
228 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
231 struct inode *inode = NULL;
232 struct f2fs_dir_entry *de;
235 if (dentry->d_name.len > F2FS_NAME_LEN)
236 return ERR_PTR(-ENAMETOOLONG);
238 de = f2fs_find_entry(dir, &dentry->d_name, &page);
240 nid_t ino = le32_to_cpu(de->ino);
241 f2fs_dentry_kunmap(dir, page);
242 f2fs_put_page(page, 0);
244 inode = f2fs_iget(dir->i_sb, ino);
246 return ERR_CAST(inode);
248 if (f2fs_has_inline_dots(inode)) {
251 err = __recover_dot_dentries(inode, dir->i_ino);
259 return d_splice_alias(inode, dentry);
262 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
264 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
265 struct inode *inode = dentry->d_inode;
266 struct f2fs_dir_entry *de;
270 trace_f2fs_unlink_enter(dir, dentry);
271 f2fs_balance_fs(sbi);
273 de = f2fs_find_entry(dir, &dentry->d_name, &page);
278 err = acquire_orphan_inode(sbi);
281 f2fs_dentry_kunmap(dir, page);
282 f2fs_put_page(page, 0);
285 f2fs_delete_entry(de, page, dir, inode);
288 /* In order to evict this inode, we set it dirty */
289 mark_inode_dirty(inode);
292 f2fs_sync_fs(sbi->sb, 1);
294 trace_f2fs_unlink_exit(inode, err);
298 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
301 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
303 size_t symlen = strlen(symname) + 1;
306 f2fs_balance_fs(sbi);
308 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
310 return PTR_ERR(inode);
312 inode->i_op = &f2fs_symlink_inode_operations;
313 inode->i_mapping->a_ops = &f2fs_dblock_aops;
316 err = f2fs_add_link(dentry, inode);
321 err = page_symlink(inode, symname, symlen);
322 alloc_nid_done(sbi, inode->i_ino);
324 d_instantiate(dentry, inode);
325 unlock_new_inode(inode);
328 f2fs_sync_fs(sbi->sb, 1);
331 handle_failed_inode(inode);
335 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
337 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
341 f2fs_balance_fs(sbi);
343 inode = f2fs_new_inode(dir, S_IFDIR | mode);
345 return PTR_ERR(inode);
347 inode->i_op = &f2fs_dir_inode_operations;
348 inode->i_fop = &f2fs_dir_operations;
349 inode->i_mapping->a_ops = &f2fs_dblock_aops;
350 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
352 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
354 err = f2fs_add_link(dentry, inode);
359 stat_inc_inline_dir(inode);
360 alloc_nid_done(sbi, inode->i_ino);
362 d_instantiate(dentry, inode);
363 unlock_new_inode(inode);
366 f2fs_sync_fs(sbi->sb, 1);
370 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
371 handle_failed_inode(inode);
375 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
377 struct inode *inode = dentry->d_inode;
378 if (f2fs_empty_dir(inode))
379 return f2fs_unlink(dir, dentry);
383 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
384 umode_t mode, dev_t rdev)
386 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
390 if (!new_valid_dev(rdev))
393 f2fs_balance_fs(sbi);
395 inode = f2fs_new_inode(dir, mode);
397 return PTR_ERR(inode);
399 init_special_inode(inode, inode->i_mode, rdev);
400 inode->i_op = &f2fs_special_inode_operations;
403 err = f2fs_add_link(dentry, inode);
408 alloc_nid_done(sbi, inode->i_ino);
410 d_instantiate(dentry, inode);
411 unlock_new_inode(inode);
414 f2fs_sync_fs(sbi->sb, 1);
417 handle_failed_inode(inode);
421 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
422 struct inode *new_dir, struct dentry *new_dentry)
424 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
425 struct inode *old_inode = old_dentry->d_inode;
426 struct inode *new_inode = new_dentry->d_inode;
427 struct page *old_dir_page;
428 struct page *old_page, *new_page;
429 struct f2fs_dir_entry *old_dir_entry = NULL;
430 struct f2fs_dir_entry *old_entry;
431 struct f2fs_dir_entry *new_entry;
434 f2fs_balance_fs(sbi);
436 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
440 if (S_ISDIR(old_inode->i_mode)) {
442 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
450 if (old_dir_entry && !f2fs_empty_dir(new_inode))
454 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
461 err = acquire_orphan_inode(sbi);
465 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
466 release_orphan_inode(sbi);
470 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
472 new_inode->i_ctime = CURRENT_TIME;
473 down_write(&F2FS_I(new_inode)->i_sem);
475 drop_nlink(new_inode);
476 drop_nlink(new_inode);
477 up_write(&F2FS_I(new_inode)->i_sem);
479 mark_inode_dirty(new_inode);
481 if (!new_inode->i_nlink)
482 add_orphan_inode(sbi, new_inode->i_ino);
484 release_orphan_inode(sbi);
486 update_inode_page(old_inode);
487 update_inode_page(new_inode);
491 err = f2fs_add_link(new_dentry, old_inode);
499 update_inode_page(new_dir);
503 down_write(&F2FS_I(old_inode)->i_sem);
504 file_lost_pino(old_inode);
505 up_write(&F2FS_I(old_inode)->i_sem);
507 old_inode->i_ctime = CURRENT_TIME;
508 mark_inode_dirty(old_inode);
510 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
513 if (old_dir != new_dir) {
514 f2fs_set_link(old_inode, old_dir_entry,
515 old_dir_page, new_dir);
516 update_inode_page(old_inode);
518 f2fs_dentry_kunmap(old_inode, old_dir_page);
519 f2fs_put_page(old_dir_page, 0);
522 mark_inode_dirty(old_dir);
523 update_inode_page(old_dir);
528 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
529 f2fs_sync_fs(sbi->sb, 1);
534 f2fs_dentry_kunmap(new_dir, new_page);
535 f2fs_put_page(new_page, 0);
538 f2fs_dentry_kunmap(old_inode, old_dir_page);
539 f2fs_put_page(old_dir_page, 0);
542 f2fs_dentry_kunmap(old_dir, old_page);
543 f2fs_put_page(old_page, 0);
548 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
549 struct inode *new_dir, struct dentry *new_dentry)
551 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
552 struct inode *old_inode = old_dentry->d_inode;
553 struct inode *new_inode = new_dentry->d_inode;
554 struct page *old_dir_page, *new_dir_page;
555 struct page *old_page, *new_page;
556 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
557 struct f2fs_dir_entry *old_entry, *new_entry;
558 int old_nlink = 0, new_nlink = 0;
561 f2fs_balance_fs(sbi);
563 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
567 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
571 /* prepare for updating ".." directory entry info later */
572 if (old_dir != new_dir) {
573 if (S_ISDIR(old_inode->i_mode)) {
575 old_dir_entry = f2fs_parent_dir(old_inode,
581 if (S_ISDIR(new_inode->i_mode)) {
583 new_dir_entry = f2fs_parent_dir(new_inode,
591 * If cross rename between file and directory those are not
592 * in the same directory, we will inc nlink of file's parent
593 * later, so we should check upper boundary of its nlink.
595 if ((!old_dir_entry || !new_dir_entry) &&
596 old_dir_entry != new_dir_entry) {
597 old_nlink = old_dir_entry ? -1 : 1;
598 new_nlink = -old_nlink;
600 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
601 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
607 err = update_dent_inode(old_inode, &new_dentry->d_name);
611 err = update_dent_inode(new_inode, &old_dentry->d_name);
615 /* update ".." directory entry info of old dentry */
617 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
619 /* update ".." directory entry info of new dentry */
621 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
623 /* update directory entry info of old dir inode */
624 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
626 down_write(&F2FS_I(old_inode)->i_sem);
627 file_lost_pino(old_inode);
628 up_write(&F2FS_I(old_inode)->i_sem);
630 update_inode_page(old_inode);
632 old_dir->i_ctime = CURRENT_TIME;
634 down_write(&F2FS_I(old_dir)->i_sem);
639 up_write(&F2FS_I(old_dir)->i_sem);
641 mark_inode_dirty(old_dir);
642 update_inode_page(old_dir);
644 /* update directory entry info of new dir inode */
645 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
647 down_write(&F2FS_I(new_inode)->i_sem);
648 file_lost_pino(new_inode);
649 up_write(&F2FS_I(new_inode)->i_sem);
651 update_inode_page(new_inode);
653 new_dir->i_ctime = CURRENT_TIME;
655 down_write(&F2FS_I(new_dir)->i_sem);
660 up_write(&F2FS_I(new_dir)->i_sem);
662 mark_inode_dirty(new_dir);
663 update_inode_page(new_dir);
667 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
668 f2fs_sync_fs(sbi->sb, 1);
671 /* Still we may fail to recover name info of f2fs_inode here */
672 update_dent_inode(old_inode, &old_dentry->d_name);
677 f2fs_dentry_kunmap(new_inode, new_dir_page);
678 f2fs_put_page(new_dir_page, 0);
682 f2fs_dentry_kunmap(old_inode, old_dir_page);
683 f2fs_put_page(old_dir_page, 0);
686 f2fs_dentry_kunmap(new_dir, new_page);
687 f2fs_put_page(new_page, 0);
689 f2fs_dentry_kunmap(old_dir, old_page);
690 f2fs_put_page(old_page, 0);
695 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
696 struct inode *new_dir, struct dentry *new_dentry,
699 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
702 if (flags & RENAME_EXCHANGE) {
703 return f2fs_cross_rename(old_dir, old_dentry,
704 new_dir, new_dentry);
707 * VFS has already handled the new dentry existence case,
708 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
710 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry);
713 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
715 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
719 inode = f2fs_new_inode(dir, mode);
721 return PTR_ERR(inode);
723 inode->i_op = &f2fs_file_inode_operations;
724 inode->i_fop = &f2fs_file_operations;
725 inode->i_mapping->a_ops = &f2fs_dblock_aops;
728 err = acquire_orphan_inode(sbi);
732 err = f2fs_do_tmpfile(inode, dir);
737 * add this non-linked tmpfile to orphan list, in this way we could
738 * remove all unused data of tmpfile after abnormal power-off.
740 add_orphan_inode(sbi, inode->i_ino);
743 alloc_nid_done(sbi, inode->i_ino);
745 stat_inc_inline_inode(inode);
746 d_tmpfile(dentry, inode);
747 unlock_new_inode(inode);
751 release_orphan_inode(sbi);
753 handle_failed_inode(inode);
757 const struct inode_operations f2fs_dir_inode_operations = {
758 .create = f2fs_create,
759 .lookup = f2fs_lookup,
761 .unlink = f2fs_unlink,
762 .symlink = f2fs_symlink,
766 .rename2 = f2fs_rename2,
767 .tmpfile = f2fs_tmpfile,
768 .getattr = f2fs_getattr,
769 .setattr = f2fs_setattr,
770 .get_acl = f2fs_get_acl,
771 .set_acl = f2fs_set_acl,
772 #ifdef CONFIG_F2FS_FS_XATTR
773 .setxattr = generic_setxattr,
774 .getxattr = generic_getxattr,
775 .listxattr = f2fs_listxattr,
776 .removexattr = generic_removexattr,
780 const struct inode_operations f2fs_symlink_inode_operations = {
781 .readlink = generic_readlink,
782 .follow_link = page_follow_link_light,
783 .put_link = page_put_link,
784 .getattr = f2fs_getattr,
785 .setattr = f2fs_setattr,
786 #ifdef CONFIG_F2FS_FS_XATTR
787 .setxattr = generic_setxattr,
788 .getxattr = generic_getxattr,
789 .listxattr = f2fs_listxattr,
790 .removexattr = generic_removexattr,
794 const struct inode_operations f2fs_special_inode_operations = {
795 .getattr = f2fs_getattr,
796 .setattr = f2fs_setattr,
797 .get_acl = f2fs_get_acl,
798 .set_acl = f2fs_set_acl,
799 #ifdef CONFIG_F2FS_FS_XATTR
800 .setxattr = generic_setxattr,
801 .getxattr = generic_getxattr,
802 .listxattr = f2fs_listxattr,
803 .removexattr = generic_removexattr,