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
);
90 return !strncasecmp(s
+ slen
- sublen
, sub
, sublen
);
94 * Set multimedia files as cold files for hot/cold data separation
96 static inline void set_cold_files(struct f2fs_sb_info
*sbi
, struct inode
*inode
,
97 const unsigned char *name
)
100 __u8 (*extlist
)[8] = sbi
->raw_super
->extension_list
;
102 int count
= le32_to_cpu(sbi
->raw_super
->extension_count
);
103 for (i
= 0; i
< count
; i
++) {
104 if (is_multimedia_file(name
, extlist
[i
])) {
105 file_set_cold(inode
);
111 static int f2fs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
114 struct super_block
*sb
= dir
->i_sb
;
115 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
120 f2fs_balance_fs(sbi
);
122 inode
= f2fs_new_inode(dir
, mode
);
124 return PTR_ERR(inode
);
126 if (!test_opt(sbi
, DISABLE_EXT_IDENTIFY
))
127 set_cold_files(sbi
, inode
, dentry
->d_name
.name
);
129 inode
->i_op
= &f2fs_file_inode_operations
;
130 inode
->i_fop
= &f2fs_file_operations
;
131 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
134 ilock
= mutex_lock_op(sbi
);
135 err
= f2fs_add_link(dentry
, inode
);
136 mutex_unlock_op(sbi
, ilock
);
140 alloc_nid_done(sbi
, ino
);
142 d_instantiate(dentry
, inode
);
143 unlock_new_inode(inode
);
147 unlock_new_inode(inode
);
148 make_bad_inode(inode
);
150 alloc_nid_failed(sbi
, ino
);
154 static int f2fs_link(struct dentry
*old_dentry
, struct inode
*dir
,
155 struct dentry
*dentry
)
157 struct inode
*inode
= old_dentry
->d_inode
;
158 struct super_block
*sb
= dir
->i_sb
;
159 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
162 f2fs_balance_fs(sbi
);
164 inode
->i_ctime
= CURRENT_TIME
;
167 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
168 ilock
= mutex_lock_op(sbi
);
169 err
= f2fs_add_link(dentry
, inode
);
170 mutex_unlock_op(sbi
, ilock
);
174 d_instantiate(dentry
, inode
);
177 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
182 struct dentry
*f2fs_get_parent(struct dentry
*child
)
184 struct qstr dotdot
= QSTR_INIT("..", 2);
185 unsigned long ino
= f2fs_inode_by_name(child
->d_inode
, &dotdot
);
187 return ERR_PTR(-ENOENT
);
188 return d_obtain_alias(f2fs_iget(child
->d_inode
->i_sb
, ino
));
191 static struct dentry
*f2fs_lookup(struct inode
*dir
, struct dentry
*dentry
,
194 struct inode
*inode
= NULL
;
195 struct f2fs_dir_entry
*de
;
198 if (dentry
->d_name
.len
> F2FS_NAME_LEN
)
199 return ERR_PTR(-ENAMETOOLONG
);
201 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
203 nid_t ino
= le32_to_cpu(de
->ino
);
205 f2fs_put_page(page
, 0);
207 inode
= f2fs_iget(dir
->i_sb
, ino
);
209 return ERR_CAST(inode
);
212 return d_splice_alias(inode
, dentry
);
215 static int f2fs_unlink(struct inode
*dir
, struct dentry
*dentry
)
217 struct super_block
*sb
= dir
->i_sb
;
218 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
219 struct inode
*inode
= dentry
->d_inode
;
220 struct f2fs_dir_entry
*de
;
225 trace_f2fs_unlink_enter(dir
, dentry
);
226 f2fs_balance_fs(sbi
);
228 de
= f2fs_find_entry(dir
, &dentry
->d_name
, &page
);
232 err
= acquire_orphan_inode(sbi
);
235 f2fs_put_page(page
, 0);
239 ilock
= mutex_lock_op(sbi
);
240 f2fs_delete_entry(de
, page
, inode
);
241 mutex_unlock_op(sbi
, ilock
);
243 /* In order to evict this inode, we set it dirty */
244 mark_inode_dirty(inode
);
246 trace_f2fs_unlink_exit(inode
, err
);
250 static int f2fs_symlink(struct inode
*dir
, struct dentry
*dentry
,
253 struct super_block
*sb
= dir
->i_sb
;
254 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
256 size_t symlen
= strlen(symname
) + 1;
259 f2fs_balance_fs(sbi
);
261 inode
= f2fs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
263 return PTR_ERR(inode
);
265 inode
->i_op
= &f2fs_symlink_inode_operations
;
266 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
268 ilock
= mutex_lock_op(sbi
);
269 err
= f2fs_add_link(dentry
, inode
);
270 mutex_unlock_op(sbi
, ilock
);
274 err
= page_symlink(inode
, symname
, symlen
);
275 alloc_nid_done(sbi
, inode
->i_ino
);
277 d_instantiate(dentry
, inode
);
278 unlock_new_inode(inode
);
282 unlock_new_inode(inode
);
283 make_bad_inode(inode
);
285 alloc_nid_failed(sbi
, inode
->i_ino
);
289 static int f2fs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
291 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
295 f2fs_balance_fs(sbi
);
297 inode
= f2fs_new_inode(dir
, S_IFDIR
| mode
);
299 return PTR_ERR(inode
);
301 inode
->i_op
= &f2fs_dir_inode_operations
;
302 inode
->i_fop
= &f2fs_dir_operations
;
303 inode
->i_mapping
->a_ops
= &f2fs_dblock_aops
;
304 mapping_set_gfp_mask(inode
->i_mapping
, GFP_F2FS_ZERO
);
306 set_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
307 ilock
= mutex_lock_op(sbi
);
308 err
= f2fs_add_link(dentry
, inode
);
309 mutex_unlock_op(sbi
, ilock
);
313 alloc_nid_done(sbi
, inode
->i_ino
);
315 d_instantiate(dentry
, inode
);
316 unlock_new_inode(inode
);
321 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
323 unlock_new_inode(inode
);
324 make_bad_inode(inode
);
326 alloc_nid_failed(sbi
, inode
->i_ino
);
330 static int f2fs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
332 struct inode
*inode
= dentry
->d_inode
;
333 if (f2fs_empty_dir(inode
))
334 return f2fs_unlink(dir
, dentry
);
338 static int f2fs_mknod(struct inode
*dir
, struct dentry
*dentry
,
339 umode_t mode
, dev_t rdev
)
341 struct super_block
*sb
= dir
->i_sb
;
342 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
347 if (!new_valid_dev(rdev
))
350 f2fs_balance_fs(sbi
);
352 inode
= f2fs_new_inode(dir
, mode
);
354 return PTR_ERR(inode
);
356 init_special_inode(inode
, inode
->i_mode
, rdev
);
357 inode
->i_op
= &f2fs_special_inode_operations
;
359 ilock
= mutex_lock_op(sbi
);
360 err
= f2fs_add_link(dentry
, inode
);
361 mutex_unlock_op(sbi
, ilock
);
365 alloc_nid_done(sbi
, inode
->i_ino
);
366 d_instantiate(dentry
, inode
);
367 unlock_new_inode(inode
);
371 unlock_new_inode(inode
);
372 make_bad_inode(inode
);
374 alloc_nid_failed(sbi
, inode
->i_ino
);
378 static int f2fs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
379 struct inode
*new_dir
, struct dentry
*new_dentry
)
381 struct super_block
*sb
= old_dir
->i_sb
;
382 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
383 struct inode
*old_inode
= old_dentry
->d_inode
;
384 struct inode
*new_inode
= new_dentry
->d_inode
;
385 struct page
*old_dir_page
;
386 struct page
*old_page
, *new_page
;
387 struct f2fs_dir_entry
*old_dir_entry
= NULL
;
388 struct f2fs_dir_entry
*old_entry
;
389 struct f2fs_dir_entry
*new_entry
;
390 int err
= -ENOENT
, ilock
= -1;
392 f2fs_balance_fs(sbi
);
394 old_entry
= f2fs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
398 if (S_ISDIR(old_inode
->i_mode
)) {
400 old_dir_entry
= f2fs_parent_dir(old_inode
, &old_dir_page
);
405 ilock
= mutex_lock_op(sbi
);
410 if (old_dir_entry
&& !f2fs_empty_dir(new_inode
))
414 new_entry
= f2fs_find_entry(new_dir
, &new_dentry
->d_name
,
419 err
= acquire_orphan_inode(sbi
);
423 if (update_dent_inode(old_inode
, &new_dentry
->d_name
)) {
424 release_orphan_inode(sbi
);
428 f2fs_set_link(new_dir
, new_entry
, new_page
, old_inode
);
430 new_inode
->i_ctime
= CURRENT_TIME
;
432 drop_nlink(new_inode
);
433 drop_nlink(new_inode
);
435 if (!new_inode
->i_nlink
)
436 add_orphan_inode(sbi
, new_inode
->i_ino
);
438 release_orphan_inode(sbi
);
440 update_inode_page(old_inode
);
441 update_inode_page(new_inode
);
443 err
= f2fs_add_link(new_dentry
, old_inode
);
449 update_inode_page(new_dir
);
453 old_inode
->i_ctime
= CURRENT_TIME
;
454 mark_inode_dirty(old_inode
);
456 f2fs_delete_entry(old_entry
, old_page
, NULL
);
459 if (old_dir
!= new_dir
) {
460 f2fs_set_link(old_inode
, old_dir_entry
,
461 old_dir_page
, new_dir
);
463 kunmap(old_dir_page
);
464 f2fs_put_page(old_dir_page
, 0);
467 update_inode_page(old_dir
);
470 mutex_unlock_op(sbi
, ilock
);
474 f2fs_put_page(new_page
, 1);
477 kunmap(old_dir_page
);
478 f2fs_put_page(old_dir_page
, 0);
480 mutex_unlock_op(sbi
, ilock
);
483 f2fs_put_page(old_page
, 0);
488 const struct inode_operations f2fs_dir_inode_operations
= {
489 .create
= f2fs_create
,
490 .lookup
= f2fs_lookup
,
492 .unlink
= f2fs_unlink
,
493 .symlink
= f2fs_symlink
,
497 .rename
= f2fs_rename
,
498 .getattr
= f2fs_getattr
,
499 .setattr
= f2fs_setattr
,
500 .get_acl
= f2fs_get_acl
,
501 #ifdef CONFIG_F2FS_FS_XATTR
502 .setxattr
= generic_setxattr
,
503 .getxattr
= generic_getxattr
,
504 .listxattr
= f2fs_listxattr
,
505 .removexattr
= generic_removexattr
,
509 const struct inode_operations f2fs_symlink_inode_operations
= {
510 .readlink
= generic_readlink
,
511 .follow_link
= page_follow_link_light
,
512 .put_link
= page_put_link
,
513 .getattr
= f2fs_getattr
,
514 .setattr
= f2fs_setattr
,
515 #ifdef CONFIG_F2FS_FS_XATTR
516 .setxattr
= generic_setxattr
,
517 .getxattr
= generic_getxattr
,
518 .listxattr
= f2fs_listxattr
,
519 .removexattr
= generic_removexattr
,
523 const struct inode_operations f2fs_special_inode_operations
= {
524 .getattr
= f2fs_getattr
,
525 .setattr
= f2fs_setattr
,
526 .get_acl
= f2fs_get_acl
,
527 #ifdef CONFIG_F2FS_FS_XATTR
528 .setxattr
= generic_setxattr
,
529 .getxattr
= generic_getxattr
,
530 .listxattr
= f2fs_listxattr
,
531 .removexattr
= generic_removexattr
,