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
);
420 down_write(&F2FS_I(old_inode
)->i_sem
);
421 F2FS_I(old_inode
)->i_pino
= new_dir
->i_ino
;
422 up_write(&F2FS_I(old_inode
)->i_sem
);
424 new_inode
->i_ctime
= CURRENT_TIME
;
425 down_write(&F2FS_I(new_inode
)->i_sem
);
427 drop_nlink(new_inode
);
428 drop_nlink(new_inode
);
429 up_write(&F2FS_I(new_inode
)->i_sem
);
431 mark_inode_dirty(new_inode
);
433 if (!new_inode
->i_nlink
)
434 add_orphan_inode(sbi
, new_inode
->i_ino
);
436 release_orphan_inode(sbi
);
438 update_inode_page(old_inode
);
439 update_inode_page(new_inode
);
441 err
= f2fs_add_link(new_dentry
, old_inode
);
447 update_inode_page(new_dir
);
451 old_inode
->i_ctime
= CURRENT_TIME
;
452 mark_inode_dirty(old_inode
);
454 f2fs_delete_entry(old_entry
, old_page
, NULL
);
457 if (old_dir
!= new_dir
) {
458 f2fs_set_link(old_inode
, old_dir_entry
,
459 old_dir_page
, new_dir
);
460 down_write(&F2FS_I(old_inode
)->i_sem
);
461 F2FS_I(old_inode
)->i_pino
= new_dir
->i_ino
;
462 up_write(&F2FS_I(old_inode
)->i_sem
);
463 update_inode_page(old_inode
);
465 kunmap(old_dir_page
);
466 f2fs_put_page(old_dir_page
, 0);
469 mark_inode_dirty(old_dir
);
470 update_inode_page(old_dir
);
477 f2fs_put_page(new_page
, 1);
480 kunmap(old_dir_page
);
481 f2fs_put_page(old_dir_page
, 0);
486 f2fs_put_page(old_page
, 0);
491 const struct inode_operations f2fs_dir_inode_operations
= {
492 .create
= f2fs_create
,
493 .lookup
= f2fs_lookup
,
495 .unlink
= f2fs_unlink
,
496 .symlink
= f2fs_symlink
,
500 .rename
= f2fs_rename
,
501 .getattr
= f2fs_getattr
,
502 .setattr
= f2fs_setattr
,
503 .get_acl
= f2fs_get_acl
,
504 .set_acl
= f2fs_set_acl
,
505 #ifdef CONFIG_F2FS_FS_XATTR
506 .setxattr
= generic_setxattr
,
507 .getxattr
= generic_getxattr
,
508 .listxattr
= f2fs_listxattr
,
509 .removexattr
= generic_removexattr
,
513 const struct inode_operations f2fs_symlink_inode_operations
= {
514 .readlink
= generic_readlink
,
515 .follow_link
= page_follow_link_light
,
516 .put_link
= page_put_link
,
517 .getattr
= f2fs_getattr
,
518 .setattr
= f2fs_setattr
,
519 #ifdef CONFIG_F2FS_FS_XATTR
520 .setxattr
= generic_setxattr
,
521 .getxattr
= generic_getxattr
,
522 .listxattr
= f2fs_listxattr
,
523 .removexattr
= generic_removexattr
,
527 const struct inode_operations f2fs_special_inode_operations
= {
528 .getattr
= f2fs_getattr
,
529 .setattr
= f2fs_setattr
,
530 .get_acl
= f2fs_get_acl
,
531 .set_acl
= f2fs_set_acl
,
532 #ifdef CONFIG_F2FS_FS_XATTR
533 .setxattr
= generic_setxattr
,
534 .getxattr
= generic_getxattr
,
535 .listxattr
= f2fs_listxattr
,
536 .removexattr
= generic_removexattr
,