1
2
3
4
5
6
7
8
9
10
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/pagemap.h>
14#include <linux/sched.h>
15#include <linux/ctype.h>
16
17#include "f2fs.h"
18#include "node.h"
19#include "xattr.h"
20#include "acl.h"
21#include <trace/events/f2fs.h>
22
23static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
24{
25 struct super_block *sb = dir->i_sb;
26 struct f2fs_sb_info *sbi = F2FS_SB(sb);
27 nid_t ino;
28 struct inode *inode;
29 bool nid_free = false;
30 int err;
31
32 inode = new_inode(sb);
33 if (!inode)
34 return ERR_PTR(-ENOMEM);
35
36 f2fs_lock_op(sbi);
37 if (!alloc_nid(sbi, &ino)) {
38 f2fs_unlock_op(sbi);
39 err = -ENOSPC;
40 goto fail;
41 }
42 f2fs_unlock_op(sbi);
43
44 inode_init_owner(inode, dir, mode);
45
46 inode->i_ino = ino;
47 inode->i_blocks = 0;
48 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
49 inode->i_generation = sbi->s_next_generation++;
50
51 err = insert_inode_locked(inode);
52 if (err) {
53 err = -EINVAL;
54 nid_free = true;
55 goto out;
56 }
57 trace_f2fs_new_inode(inode, 0);
58 mark_inode_dirty(inode);
59 return inode;
60
61out:
62 clear_nlink(inode);
63 unlock_new_inode(inode);
64fail:
65 trace_f2fs_new_inode(inode, err);
66 make_bad_inode(inode);
67 iput(inode);
68 if (nid_free)
69 alloc_nid_failed(sbi, ino);
70 return ERR_PTR(err);
71}
72
73static int is_multimedia_file(const unsigned char *s, const char *sub)
74{
75 size_t slen = strlen(s);
76 size_t sublen = strlen(sub);
77
78 if (sublen > slen)
79 return 0;
80
81 return !strncasecmp(s + slen - sublen, sub, sublen);
82}
83
84
85
86
87static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
88 const unsigned char *name)
89{
90 int i;
91 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
92
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])) {
96 file_set_cold(inode);
97 break;
98 }
99 }
100}
101
102static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
103 bool excl)
104{
105 struct super_block *sb = dir->i_sb;
106 struct f2fs_sb_info *sbi = F2FS_SB(sb);
107 struct inode *inode;
108 nid_t ino = 0;
109 int err;
110
111 f2fs_balance_fs(sbi);
112
113 inode = f2fs_new_inode(dir, mode);
114 if (IS_ERR(inode))
115 return PTR_ERR(inode);
116
117 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
118 set_cold_files(sbi, inode, dentry->d_name.name);
119
120 inode->i_op = &f2fs_file_inode_operations;
121 inode->i_fop = &f2fs_file_operations;
122 inode->i_mapping->a_ops = &f2fs_dblock_aops;
123 ino = inode->i_ino;
124
125 f2fs_lock_op(sbi);
126 err = f2fs_add_link(dentry, inode);
127 f2fs_unlock_op(sbi);
128 if (err)
129 goto out;
130
131 alloc_nid_done(sbi, ino);
132
133 d_instantiate(dentry, inode);
134 unlock_new_inode(inode);
135 return 0;
136out:
137 clear_nlink(inode);
138 unlock_new_inode(inode);
139 make_bad_inode(inode);
140 iput(inode);
141 alloc_nid_failed(sbi, ino);
142 return err;
143}
144
145static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
146 struct dentry *dentry)
147{
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);
151 int err;
152
153 f2fs_balance_fs(sbi);
154
155 inode->i_ctime = CURRENT_TIME;
156 ihold(inode);
157
158 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
159 f2fs_lock_op(sbi);
160 err = f2fs_add_link(dentry, inode);
161 f2fs_unlock_op(sbi);
162 if (err)
163 goto out;
164
165 d_instantiate(dentry, inode);
166 return 0;
167out:
168 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
169 iput(inode);
170 return err;
171}
172
173struct dentry *f2fs_get_parent(struct dentry *child)
174{
175 struct qstr dotdot = QSTR_INIT("..", 2);
176 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
177 if (!ino)
178 return ERR_PTR(-ENOENT);
179 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
180}
181
182static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
183 unsigned int flags)
184{
185 struct inode *inode = NULL;
186 struct f2fs_dir_entry *de;
187 struct page *page;
188
189 if (dentry->d_name.len > F2FS_NAME_LEN)
190 return ERR_PTR(-ENAMETOOLONG);
191
192 de = f2fs_find_entry(dir, &dentry->d_name, &page);
193 if (de) {
194 nid_t ino = le32_to_cpu(de->ino);
195 kunmap(page);
196 f2fs_put_page(page, 0);
197
198 inode = f2fs_iget(dir->i_sb, ino);
199 if (IS_ERR(inode))
200 return ERR_CAST(inode);
201
202 stat_inc_inline_inode(inode);
203 }
204
205 return d_splice_alias(inode, dentry);
206}
207
208static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
209{
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;
214 struct page *page;
215 int err = -ENOENT;
216
217 trace_f2fs_unlink_enter(dir, dentry);
218 f2fs_balance_fs(sbi);
219
220 de = f2fs_find_entry(dir, &dentry->d_name, &page);
221 if (!de)
222 goto fail;
223
224 f2fs_lock_op(sbi);
225 err = acquire_orphan_inode(sbi);
226 if (err) {
227 f2fs_unlock_op(sbi);
228 kunmap(page);
229 f2fs_put_page(page, 0);
230 goto fail;
231 }
232 f2fs_delete_entry(de, page, inode);
233 f2fs_unlock_op(sbi);
234
235
236 mark_inode_dirty(inode);
237fail:
238 trace_f2fs_unlink_exit(inode, err);
239 return err;
240}
241
242static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
243 const char *symname)
244{
245 struct super_block *sb = dir->i_sb;
246 struct f2fs_sb_info *sbi = F2FS_SB(sb);
247 struct inode *inode;
248 size_t symlen = strlen(symname) + 1;
249 int err;
250
251 f2fs_balance_fs(sbi);
252
253 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
254 if (IS_ERR(inode))
255 return PTR_ERR(inode);
256
257 inode->i_op = &f2fs_symlink_inode_operations;
258 inode->i_mapping->a_ops = &f2fs_dblock_aops;
259
260 f2fs_lock_op(sbi);
261 err = f2fs_add_link(dentry, inode);
262 f2fs_unlock_op(sbi);
263 if (err)
264 goto out;
265
266 err = page_symlink(inode, symname, symlen);
267 alloc_nid_done(sbi, inode->i_ino);
268
269 d_instantiate(dentry, inode);
270 unlock_new_inode(inode);
271 return err;
272out:
273 clear_nlink(inode);
274 unlock_new_inode(inode);
275 make_bad_inode(inode);
276 iput(inode);
277 alloc_nid_failed(sbi, inode->i_ino);
278 return err;
279}
280
281static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
282{
283 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
284 struct inode *inode;
285 int err;
286
287 f2fs_balance_fs(sbi);
288
289 inode = f2fs_new_inode(dir, S_IFDIR | mode);
290 if (IS_ERR(inode))
291 return PTR_ERR(inode);
292
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);
297
298 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
299 f2fs_lock_op(sbi);
300 err = f2fs_add_link(dentry, inode);
301 f2fs_unlock_op(sbi);
302 if (err)
303 goto out_fail;
304
305 alloc_nid_done(sbi, inode->i_ino);
306
307 d_instantiate(dentry, inode);
308 unlock_new_inode(inode);
309
310 return 0;
311
312out_fail:
313 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
314 clear_nlink(inode);
315 unlock_new_inode(inode);
316 make_bad_inode(inode);
317 iput(inode);
318 alloc_nid_failed(sbi, inode->i_ino);
319 return err;
320}
321
322static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
323{
324 struct inode *inode = dentry->d_inode;
325 if (f2fs_empty_dir(inode))
326 return f2fs_unlink(dir, dentry);
327 return -ENOTEMPTY;
328}
329
330static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
331 umode_t mode, dev_t rdev)
332{
333 struct super_block *sb = dir->i_sb;
334 struct f2fs_sb_info *sbi = F2FS_SB(sb);
335 struct inode *inode;
336 int err = 0;
337
338 if (!new_valid_dev(rdev))
339 return -EINVAL;
340
341 f2fs_balance_fs(sbi);
342
343 inode = f2fs_new_inode(dir, mode);
344 if (IS_ERR(inode))
345 return PTR_ERR(inode);
346
347 init_special_inode(inode, inode->i_mode, rdev);
348 inode->i_op = &f2fs_special_inode_operations;
349
350 f2fs_lock_op(sbi);
351 err = f2fs_add_link(dentry, inode);
352 f2fs_unlock_op(sbi);
353 if (err)
354 goto out;
355
356 alloc_nid_done(sbi, inode->i_ino);
357 d_instantiate(dentry, inode);
358 unlock_new_inode(inode);
359 return 0;
360out:
361 clear_nlink(inode);
362 unlock_new_inode(inode);
363 make_bad_inode(inode);
364 iput(inode);
365 alloc_nid_failed(sbi, inode->i_ino);
366 return err;
367}
368
369static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
370 struct inode *new_dir, struct dentry *new_dentry)
371{
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;
381 int err = -ENOENT;
382
383 f2fs_balance_fs(sbi);
384
385 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
386 if (!old_entry)
387 goto out;
388
389 if (S_ISDIR(old_inode->i_mode)) {
390 err = -EIO;
391 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
392 if (!old_dir_entry)
393 goto out_old;
394 }
395
396 f2fs_lock_op(sbi);
397
398 if (new_inode) {
399
400 err = -ENOTEMPTY;
401 if (old_dir_entry && !f2fs_empty_dir(new_inode))
402 goto out_dir;
403
404 err = -ENOENT;
405 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
406 &new_page);
407 if (!new_entry)
408 goto out_dir;
409
410 err = acquire_orphan_inode(sbi);
411 if (err)
412 goto put_out_dir;
413
414 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
415 release_orphan_inode(sbi);
416 goto put_out_dir;
417 }
418
419 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
420
421 new_inode->i_ctime = CURRENT_TIME;
422 down_write(&F2FS_I(new_inode)->i_sem);
423 if (old_dir_entry)
424 drop_nlink(new_inode);
425 drop_nlink(new_inode);
426 up_write(&F2FS_I(new_inode)->i_sem);
427
428 mark_inode_dirty(new_inode);
429
430 if (!new_inode->i_nlink)
431 add_orphan_inode(sbi, new_inode->i_ino);
432 else
433 release_orphan_inode(sbi);
434
435 update_inode_page(old_inode);
436 update_inode_page(new_inode);
437 } else {
438 err = f2fs_add_link(new_dentry, old_inode);
439 if (err)
440 goto out_dir;
441
442 if (old_dir_entry) {
443 inc_nlink(new_dir);
444 update_inode_page(new_dir);
445 }
446 }
447
448 down_write(&F2FS_I(old_inode)->i_sem);
449 file_lost_pino(old_inode);
450 up_write(&F2FS_I(old_inode)->i_sem);
451
452 old_inode->i_ctime = CURRENT_TIME;
453 mark_inode_dirty(old_inode);
454
455 f2fs_delete_entry(old_entry, old_page, NULL);
456
457 if (old_dir_entry) {
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);
462 } else {
463 kunmap(old_dir_page);
464 f2fs_put_page(old_dir_page, 0);
465 }
466 drop_nlink(old_dir);
467 mark_inode_dirty(old_dir);
468 update_inode_page(old_dir);
469 }
470
471 f2fs_unlock_op(sbi);
472 return 0;
473
474put_out_dir:
475 kunmap(new_page);
476 f2fs_put_page(new_page, 0);
477out_dir:
478 if (old_dir_entry) {
479 kunmap(old_dir_page);
480 f2fs_put_page(old_dir_page, 0);
481 }
482 f2fs_unlock_op(sbi);
483out_old:
484 kunmap(old_page);
485 f2fs_put_page(old_page, 0);
486out:
487 return err;
488}
489
490const struct inode_operations f2fs_dir_inode_operations = {
491 .create = f2fs_create,
492 .lookup = f2fs_lookup,
493 .link = f2fs_link,
494 .unlink = f2fs_unlink,
495 .symlink = f2fs_symlink,
496 .mkdir = f2fs_mkdir,
497 .rmdir = f2fs_rmdir,
498 .mknod = f2fs_mknod,
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,
509#endif
510};
511
512const 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,
523#endif
524};
525
526const 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,
536#endif
537};
538