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>
21 #include <trace/events/f2fs.h>
23 static struct inode
*f2fs_new_inode(struct inode
*dir
, umode_t mode
)
25 struct super_block
*sb
= dir
->i_sb
;
26 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
29 bool nid_free
= false;
32 inode
= new_inode(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 super_block
*sb
= dir
->i_sb
;
106 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
111 f2fs_balance_fs(sbi
);
113 inode
= f2fs_new_inode(dir
, mode
);
115 return PTR_ERR(inode
);
117 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
118 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
120 inode
->i_op
= &f2fs_file_inode_operations
;
121 inode
->i_fop
= &f2fs_file_operations
;
122 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
126 err
= f2fs_add_link(dentry
, inode
);
131 alloc_nid_done(sbi
, ino
);
133 d_instantiate(dentry
, inode
);
134 unlock_new_inode(inode
);
138 unlock_new_inode(inode
);
139 make_bad_inode(inode
);
141 alloc_nid_failed(sbi
, ino
);
145 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
146 struct dentry
*dentry
)
148 struct inode
*inode
= old_dentry
->d_inode
;
149 struct super_block
*sb
= dir
->i_sb
;
150 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
153 f2fs_balance_fs(sbi
);
155 inode
->i_ctime
= CURRENT_TIME
;
158 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
160 err
= f2fs_add_link(dentry
, inode
);
165 d_instantiate(dentry
, inode
);
168 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
173 struct dentry
*f2fs_get_parent(struct dentry
*child
)
175 struct qstr dotdot
= QSTR_INIT("..", 2);
176 unsigned long ino
= f2fs_inode_by_name(child
->d_inode
, &dotdot
);
178 return ERR_PTR(-ENOENT
);
179 return d_obtain_alias(f2fs_iget(child
->d_inode
->i_sb
, ino
));
182 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
185 struct inode
*inode
= NULL
;
186 struct f2fs_dir_entry
*de
;
189 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
190 return ERR_PTR(-ENAMETOOLONG
);
192 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
194 nid_t ino
= le32_to_cpu(de
->ino
);
196 f2fs_put_page(page
, 0);
198 inode
= f2fs_iget(dir
->i_sb
, ino
);
200 return ERR_CAST(inode
);
202 stat_inc_inline_inode(inode
);
205 return d_splice_alias(inode
, dentry
);
208 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
210 struct super_block
*sb
= dir
->i_sb
;
211 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
212 struct inode
*inode
= dentry
->d_inode
;
213 struct f2fs_dir_entry
*de
;
217 trace_f2fs_unlink_enter(dir
, dentry
);
218 f2fs_balance_fs(sbi
);
220 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
225 err
= acquire_orphan_inode(sbi
);
229 f2fs_put_page(page
, 0);
232 f2fs_delete_entry(de
, page
, inode
);
235 /* In order to evict this inode, we set it dirty */
236 mark_inode_dirty(inode
);
238 trace_f2fs_unlink_exit(inode
, err
);
242 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
245 struct super_block
*sb
= dir
->i_sb
;
246 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
248 size_t symlen
= strlen(symname
) + 1;
251 f2fs_balance_fs(sbi
);
253 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
255 return PTR_ERR(inode
);
257 inode
->i_op
= &f2fs_symlink_inode_operations
;
258 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
261 err
= f2fs_add_link(dentry
, inode
);
266 err
= page_symlink(inode
, symname
, symlen
);
267 alloc_nid_done(sbi
, inode
->i_ino
);
269 d_instantiate(dentry
, inode
);
270 unlock_new_inode(inode
);
274 unlock_new_inode(inode
);
275 make_bad_inode(inode
);
277 alloc_nid_failed(sbi
, inode
->i_ino
);
281 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
283 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
287 f2fs_balance_fs(sbi
);
289 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
291 return PTR_ERR(inode
);
293 inode
->i_op
= &f2fs_dir_inode_operations
;
294 inode
->i_fop
= &f2fs_dir_operations
;
295 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
296 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_ZERO
);
298 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
300 err
= f2fs_add_link(dentry
, inode
);
305 alloc_nid_done(sbi
, inode
->i_ino
);
307 d_instantiate(dentry
, inode
);
308 unlock_new_inode(inode
);
313 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
315 unlock_new_inode(inode
);
316 make_bad_inode(inode
);
318 alloc_nid_failed(sbi
, inode
->i_ino
);
322 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
324 struct inode
*inode
= dentry
->d_inode
;
325 if (f2fs_empty_dir(inode
))
326 return f2fs_unlink(dir
, dentry
);
330 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
331 umode_t mode
, dev_t rdev
)
333 struct super_block
*sb
= dir
->i_sb
;
334 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
338 if (!new_valid_dev(rdev
))
341 f2fs_balance_fs(sbi
);
343 inode
= f2fs_new_inode(dir
, mode
);
345 return PTR_ERR(inode
);
347 init_special_inode(inode
, inode
->i_mode
, rdev
);
348 inode
->i_op
= &f2fs_special_inode_operations
;
351 err
= f2fs_add_link(dentry
, inode
);
356 alloc_nid_done(sbi
, inode
->i_ino
);
357 d_instantiate(dentry
, inode
);
358 unlock_new_inode(inode
);
362 unlock_new_inode(inode
);
363 make_bad_inode(inode
);
365 alloc_nid_failed(sbi
, inode
->i_ino
);
369 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
370 struct inode
*new_dir
, struct dentry
*new_dentry
)
372 struct super_block
*sb
= old_dir
->i_sb
;
373 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
374 struct inode
*old_inode
= old_dentry
->d_inode
;
375 struct inode
*new_inode
= new_dentry
->d_inode
;
376 struct page
*old_dir_page
;
377 struct page
*old_page
, *new_page
;
378 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
379 struct f2fs_dir_entry
*old_entry
;
380 struct f2fs_dir_entry
*new_entry
;
383 f2fs_balance_fs(sbi
);
385 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
389 if (S_ISDIR(old_inode
->i_mode
)) {
391 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
401 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
405 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
410 err
= acquire_orphan_inode(sbi
);
414 if (update_dent_inode(old_inode
, &new_dentry
->d_name
)) {
415 release_orphan_inode(sbi
);
419 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
421 new_inode
->i_ctime
= CURRENT_TIME
;
422 down_write(&F2FS_I(new_inode
)->i_sem
);
424 drop_nlink(new_inode
);
425 drop_nlink(new_inode
);
426 up_write(&F2FS_I(new_inode
)->i_sem
);
428 mark_inode_dirty(new_inode
);
430 if (!new_inode
->i_nlink
)
431 add_orphan_inode(sbi
, new_inode
->i_ino
);
433 release_orphan_inode(sbi
);
435 update_inode_page(old_inode
);
436 update_inode_page(new_inode
);
438 err
= f2fs_add_link(new_dentry
, old_inode
);
444 update_inode_page(new_dir
);
448 down_write(&F2FS_I(old_inode
)->i_sem
);
449 file_lost_pino(old_inode
);
450 up_write(&F2FS_I(old_inode
)->i_sem
);
452 old_inode
->i_ctime
= CURRENT_TIME
;
453 mark_inode_dirty(old_inode
);
455 f2fs_delete_entry(old_entry
, old_page
, NULL
);
458 if (old_dir
!= new_dir
) {
459 f2fs_set_link(old_inode
, old_dir_entry
,
460 old_dir_page
, new_dir
);
461 update_inode_page(old_inode
);
463 kunmap(old_dir_page
);
464 f2fs_put_page(old_dir_page
, 0);
467 mark_inode_dirty(old_dir
);
468 update_inode_page(old_dir
);
476 f2fs_put_page(new_page
, 0);
479 kunmap(old_dir_page
);
480 f2fs_put_page(old_dir_page
, 0);
485 f2fs_put_page(old_page
, 0);
490 const struct inode_operations f2fs_dir_inode_operations
= {
491 .create
= f2fs_create
,
492 .lookup
= f2fs_lookup
,
494 .unlink
= f2fs_unlink
,
495 .symlink
= f2fs_symlink
,
499 .rename
= f2fs_rename
,
500 .getattr
= f2fs_getattr
,
501 .setattr
= f2fs_setattr
,
502 .get_acl
= f2fs_get_acl
,
503 .set_acl
= f2fs_set_acl
,
504 #ifdef CONFIG_F2FS_FS_XATTR
505 .setxattr
= generic_setxattr
,
506 .getxattr
= generic_getxattr
,
507 .listxattr
= f2fs_listxattr
,
508 .removexattr
= generic_removexattr
,
512 const struct inode_operations f2fs_symlink_inode_operations
= {
513 .readlink
= generic_readlink
,
514 .follow_link
= page_follow_link_light
,
515 .put_link
= page_put_link
,
516 .getattr
= f2fs_getattr
,
517 .setattr
= f2fs_setattr
,
518 #ifdef CONFIG_F2FS_FS_XATTR
519 .setxattr
= generic_setxattr
,
520 .getxattr
= generic_getxattr
,
521 .listxattr
= f2fs_listxattr
,
522 .removexattr
= generic_removexattr
,
526 const struct inode_operations f2fs_special_inode_operations
= {
527 .getattr
= f2fs_getattr
,
528 .setattr
= f2fs_setattr
,
529 .get_acl
= f2fs_get_acl
,
530 .set_acl
= f2fs_set_acl
,
531 #ifdef CONFIG_F2FS_FS_XATTR
532 .setxattr
= generic_setxattr
,
533 .getxattr
= generic_getxattr
,
534 .listxattr
= f2fs_listxattr
,
535 .removexattr
= generic_removexattr
,