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
);
36 ilock
= mutex_lock_op(sbi
);
37 if (!alloc_nid(sbi
, &ino
)) {
38 mutex_unlock_op(sbi
, ilock
);
42 mutex_unlock_op(sbi
, ilock
);
44 inode
->i_uid
= current_fsuid();
46 if (dir
->i_mode
& S_ISGID
) {
47 inode
->i_gid
= dir
->i_gid
;
51 inode
->i_gid
= current_fsgid();
57 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
58 inode
->i_generation
= sbi
->s_next_generation
++;
60 err
= insert_inode_locked(inode
);
66 trace_f2fs_new_inode(inode
, 0);
67 mark_inode_dirty(inode
);
72 unlock_new_inode(inode
);
74 trace_f2fs_new_inode(inode
, err
);
75 make_bad_inode(inode
);
78 alloc_nid_failed(sbi
, ino
);
82 static int is_multimedia_file(const unsigned char *s
, const char *sub
)
84 size_t slen
= strlen(s
);
85 size_t sublen
= strlen(sub
);
91 ret
= memcmp(s
+ slen
- sublen
, sub
, sublen
);
92 if (ret
) { /* compare upper case */
95 for (i
= 0; i
< sublen
&& i
< sizeof(upper_sub
); i
++)
96 upper_sub
[i
] = toupper(sub
[i
]);
97 return !memcmp(s
+ slen
- sublen
, upper_sub
, sublen
);
104 * Set multimedia files as cold files for hot/cold data separation
106 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
107 const unsigned char *name
)
110 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
112 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
113 for (i
= 0; i
< count
; i
++) {
114 if (is_multimedia_file(name
, extlist
[i
])) {
115 file_set_cold(inode
);
121 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
124 struct super_block
*sb
= dir
->i_sb
;
125 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
130 f2fs_balance_fs(sbi
);
132 inode
= f2fs_new_inode(dir
, mode
);
134 return PTR_ERR(inode
);
136 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
137 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
139 inode
->i_op
= &f2fs_file_inode_operations
;
140 inode
->i_fop
= &f2fs_file_operations
;
141 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
144 ilock
= mutex_lock_op(sbi
);
145 err
= f2fs_add_link(dentry
, inode
);
146 mutex_unlock_op(sbi
, ilock
);
150 alloc_nid_done(sbi
, ino
);
152 d_instantiate(dentry
, inode
);
153 unlock_new_inode(inode
);
157 unlock_new_inode(inode
);
158 make_bad_inode(inode
);
160 alloc_nid_failed(sbi
, ino
);
164 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
165 struct dentry
*dentry
)
167 struct inode
*inode
= old_dentry
->d_inode
;
168 struct super_block
*sb
= dir
->i_sb
;
169 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
172 f2fs_balance_fs(sbi
);
174 inode
->i_ctime
= CURRENT_TIME
;
177 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
178 ilock
= mutex_lock_op(sbi
);
179 err
= f2fs_add_link(dentry
, inode
);
180 mutex_unlock_op(sbi
, ilock
);
184 d_instantiate(dentry
, inode
);
187 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
192 struct dentry
*f2fs_get_parent(struct dentry
*child
)
194 struct qstr dotdot
= QSTR_INIT("..", 2);
195 unsigned long ino
= f2fs_inode_by_name(child
->d_inode
, &dotdot
);
197 return ERR_PTR(-ENOENT
);
198 return d_obtain_alias(f2fs_iget(child
->d_inode
->i_sb
, ino
));
201 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
204 struct inode
*inode
= NULL
;
205 struct f2fs_dir_entry
*de
;
208 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
209 return ERR_PTR(-ENAMETOOLONG
);
211 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
213 nid_t ino
= le32_to_cpu(de
->ino
);
215 f2fs_put_page(page
, 0);
217 inode
= f2fs_iget(dir
->i_sb
, ino
);
219 return ERR_CAST(inode
);
222 return d_splice_alias(inode
, dentry
);
225 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
227 struct super_block
*sb
= dir
->i_sb
;
228 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
229 struct inode
*inode
= dentry
->d_inode
;
230 struct f2fs_dir_entry
*de
;
235 trace_f2fs_unlink_enter(dir
, dentry
);
236 f2fs_balance_fs(sbi
);
238 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
242 err
= check_orphan_space(sbi
);
245 f2fs_put_page(page
, 0);
249 ilock
= mutex_lock_op(sbi
);
250 f2fs_delete_entry(de
, page
, inode
);
251 mutex_unlock_op(sbi
, ilock
);
253 /* In order to evict this inode, we set it dirty */
254 mark_inode_dirty(inode
);
256 trace_f2fs_unlink_exit(inode
, err
);
260 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
263 struct super_block
*sb
= dir
->i_sb
;
264 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
266 size_t symlen
= strlen(symname
) + 1;
269 f2fs_balance_fs(sbi
);
271 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
273 return PTR_ERR(inode
);
275 inode
->i_op
= &f2fs_symlink_inode_operations
;
276 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
278 ilock
= mutex_lock_op(sbi
);
279 err
= f2fs_add_link(dentry
, inode
);
280 mutex_unlock_op(sbi
, ilock
);
284 err
= page_symlink(inode
, symname
, symlen
);
285 alloc_nid_done(sbi
, inode
->i_ino
);
287 d_instantiate(dentry
, inode
);
288 unlock_new_inode(inode
);
292 unlock_new_inode(inode
);
293 make_bad_inode(inode
);
295 alloc_nid_failed(sbi
, inode
->i_ino
);
299 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
301 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
305 f2fs_balance_fs(sbi
);
307 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
309 return PTR_ERR(inode
);
311 inode
->i_op
= &f2fs_dir_inode_operations
;
312 inode
->i_fop
= &f2fs_dir_operations
;
313 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
314 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_ZERO
);
316 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
317 ilock
= mutex_lock_op(sbi
);
318 err
= f2fs_add_link(dentry
, inode
);
319 mutex_unlock_op(sbi
, ilock
);
323 alloc_nid_done(sbi
, inode
->i_ino
);
325 d_instantiate(dentry
, inode
);
326 unlock_new_inode(inode
);
331 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
333 unlock_new_inode(inode
);
334 make_bad_inode(inode
);
336 alloc_nid_failed(sbi
, inode
->i_ino
);
340 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
342 struct inode
*inode
= dentry
->d_inode
;
343 if (f2fs_empty_dir(inode
))
344 return f2fs_unlink(dir
, dentry
);
348 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
349 umode_t mode
, dev_t rdev
)
351 struct super_block
*sb
= dir
->i_sb
;
352 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
357 if (!new_valid_dev(rdev
))
360 f2fs_balance_fs(sbi
);
362 inode
= f2fs_new_inode(dir
, mode
);
364 return PTR_ERR(inode
);
366 init_special_inode(inode
, inode
->i_mode
, rdev
);
367 inode
->i_op
= &f2fs_special_inode_operations
;
369 ilock
= mutex_lock_op(sbi
);
370 err
= f2fs_add_link(dentry
, inode
);
371 mutex_unlock_op(sbi
, ilock
);
375 alloc_nid_done(sbi
, inode
->i_ino
);
376 d_instantiate(dentry
, inode
);
377 unlock_new_inode(inode
);
381 unlock_new_inode(inode
);
382 make_bad_inode(inode
);
384 alloc_nid_failed(sbi
, inode
->i_ino
);
388 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
389 struct inode
*new_dir
, struct dentry
*new_dentry
)
391 struct super_block
*sb
= old_dir
->i_sb
;
392 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
393 struct inode
*old_inode
= old_dentry
->d_inode
;
394 struct inode
*new_inode
= new_dentry
->d_inode
;
395 struct page
*old_dir_page
;
396 struct page
*old_page
;
397 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
398 struct f2fs_dir_entry
*old_entry
;
399 struct f2fs_dir_entry
*new_entry
;
400 int err
= -ENOENT
, ilock
= -1;
402 f2fs_balance_fs(sbi
);
404 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
408 if (S_ISDIR(old_inode
->i_mode
)) {
410 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
415 ilock
= mutex_lock_op(sbi
);
418 struct page
*new_page
;
421 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
425 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
430 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
432 new_inode
->i_ctime
= CURRENT_TIME
;
434 drop_nlink(new_inode
);
435 drop_nlink(new_inode
);
436 if (!new_inode
->i_nlink
)
437 add_orphan_inode(sbi
, new_inode
->i_ino
);
438 update_inode_page(new_inode
);
440 err
= f2fs_add_link(new_dentry
, old_inode
);
446 update_inode_page(new_dir
);
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
);
460 kunmap(old_dir_page
);
461 f2fs_put_page(old_dir_page
, 0);
464 update_inode_page(old_dir
);
467 mutex_unlock_op(sbi
, ilock
);
472 kunmap(old_dir_page
);
473 f2fs_put_page(old_dir_page
, 0);
475 mutex_unlock_op(sbi
, ilock
);
478 f2fs_put_page(old_page
, 0);
483 const struct inode_operations f2fs_dir_inode_operations
= {
484 .create
= f2fs_create
,
485 .lookup
= f2fs_lookup
,
487 .unlink
= f2fs_unlink
,
488 .symlink
= f2fs_symlink
,
492 .rename
= f2fs_rename
,
493 .getattr
= f2fs_getattr
,
494 .setattr
= f2fs_setattr
,
495 .get_acl
= f2fs_get_acl
,
496 #ifdef CONFIG_F2FS_FS_XATTR
497 .setxattr
= generic_setxattr
,
498 .getxattr
= generic_getxattr
,
499 .listxattr
= f2fs_listxattr
,
500 .removexattr
= generic_removexattr
,
504 const struct inode_operations f2fs_symlink_inode_operations
= {
505 .readlink
= generic_readlink
,
506 .follow_link
= page_follow_link_light
,
507 .put_link
= page_put_link
,
508 .getattr
= f2fs_getattr
,
509 .setattr
= f2fs_setattr
,
510 #ifdef CONFIG_F2FS_FS_XATTR
511 .setxattr
= generic_setxattr
,
512 .getxattr
= generic_getxattr
,
513 .listxattr
= f2fs_listxattr
,
514 .removexattr
= generic_removexattr
,
518 const struct inode_operations f2fs_special_inode_operations
= {
519 .getattr
= f2fs_getattr
,
520 .setattr
= f2fs_setattr
,
521 .get_acl
= f2fs_get_acl
,
522 #ifdef CONFIG_F2FS_FS_XATTR
523 .setxattr
= generic_setxattr
,
524 .getxattr
= generic_getxattr
,
525 .listxattr
= f2fs_listxattr
,
526 .removexattr
= generic_removexattr
,