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#include <linux/dcache.h>
17
18#include "f2fs.h"
19#include "node.h"
20#include "xattr.h"
21#include "acl.h"
22#include <trace/events/f2fs.h>
23
24static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
25{
26 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
27 nid_t ino;
28 struct inode *inode;
29 bool nid_free = false;
30 int err;
31
32 inode = new_inode(dir->i_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 f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
106 struct inode *inode;
107 nid_t ino = 0;
108 int err;
109
110 f2fs_balance_fs(sbi);
111
112 inode = f2fs_new_inode(dir, mode);
113 if (IS_ERR(inode))
114 return PTR_ERR(inode);
115
116 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
117 set_cold_files(sbi, inode, dentry->d_name.name);
118
119 inode->i_op = &f2fs_file_inode_operations;
120 inode->i_fop = &f2fs_file_operations;
121 inode->i_mapping->a_ops = &f2fs_dblock_aops;
122 ino = inode->i_ino;
123
124 f2fs_lock_op(sbi);
125 err = f2fs_add_link(dentry, inode);
126 f2fs_unlock_op(sbi);
127 if (err)
128 goto out;
129
130 alloc_nid_done(sbi, ino);
131
132 d_instantiate(dentry, inode);
133 unlock_new_inode(inode);
134 return 0;
135out:
136 clear_nlink(inode);
137 iget_failed(inode);
138 alloc_nid_failed(sbi, ino);
139 return err;
140}
141
142static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
143 struct dentry *dentry)
144{
145 struct inode *inode = old_dentry->d_inode;
146 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
147 int err;
148
149 f2fs_balance_fs(sbi);
150
151 inode->i_ctime = CURRENT_TIME;
152 ihold(inode);
153
154 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
155 f2fs_lock_op(sbi);
156 err = f2fs_add_link(dentry, inode);
157 f2fs_unlock_op(sbi);
158 if (err)
159 goto out;
160
161 d_instantiate(dentry, inode);
162 return 0;
163out:
164 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
165 iput(inode);
166 return err;
167}
168
169struct dentry *f2fs_get_parent(struct dentry *child)
170{
171 struct qstr dotdot = QSTR_INIT("..", 2);
172 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot);
173 if (!ino)
174 return ERR_PTR(-ENOENT);
175 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino));
176}
177
178static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
179 unsigned int flags)
180{
181 struct inode *inode = NULL;
182 struct f2fs_dir_entry *de;
183 struct page *page;
184
185 if (dentry->d_name.len > F2FS_NAME_LEN)
186 return ERR_PTR(-ENAMETOOLONG);
187
188 de = f2fs_find_entry(dir, &dentry->d_name, &page);
189 if (de) {
190 nid_t ino = le32_to_cpu(de->ino);
191 kunmap(page);
192 f2fs_put_page(page, 0);
193
194 inode = f2fs_iget(dir->i_sb, ino);
195 if (IS_ERR(inode))
196 return ERR_CAST(inode);
197
198 stat_inc_inline_inode(inode);
199 }
200
201 return d_splice_alias(inode, dentry);
202}
203
204static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
205{
206 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
207 struct inode *inode = dentry->d_inode;
208 struct f2fs_dir_entry *de;
209 struct page *page;
210 int err = -ENOENT;
211
212 trace_f2fs_unlink_enter(dir, dentry);
213 f2fs_balance_fs(sbi);
214
215 de = f2fs_find_entry(dir, &dentry->d_name, &page);
216 if (!de)
217 goto fail;
218
219 f2fs_lock_op(sbi);
220 err = acquire_orphan_inode(sbi);
221 if (err) {
222 f2fs_unlock_op(sbi);
223 kunmap(page);
224 f2fs_put_page(page, 0);
225 goto fail;
226 }
227 f2fs_delete_entry(de, page, inode);
228 f2fs_unlock_op(sbi);
229
230
231 mark_inode_dirty(inode);
232fail:
233 trace_f2fs_unlink_exit(inode, err);
234 return err;
235}
236
237static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
238 const char *symname)
239{
240 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
241 struct inode *inode;
242 size_t symlen = strlen(symname) + 1;
243 int err;
244
245 f2fs_balance_fs(sbi);
246
247 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
248 if (IS_ERR(inode))
249 return PTR_ERR(inode);
250
251 inode->i_op = &f2fs_symlink_inode_operations;
252 inode->i_mapping->a_ops = &f2fs_dblock_aops;
253
254 f2fs_lock_op(sbi);
255 err = f2fs_add_link(dentry, inode);
256 f2fs_unlock_op(sbi);
257 if (err)
258 goto out;
259
260 err = page_symlink(inode, symname, symlen);
261 alloc_nid_done(sbi, inode->i_ino);
262
263 d_instantiate(dentry, inode);
264 unlock_new_inode(inode);
265 return err;
266out:
267 clear_nlink(inode);
268 iget_failed(inode);
269 alloc_nid_failed(sbi, inode->i_ino);
270 return err;
271}
272
273static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
274{
275 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
276 struct inode *inode;
277 int err;
278
279 f2fs_balance_fs(sbi);
280
281 inode = f2fs_new_inode(dir, S_IFDIR | mode);
282 if (IS_ERR(inode))
283 return PTR_ERR(inode);
284
285 inode->i_op = &f2fs_dir_inode_operations;
286 inode->i_fop = &f2fs_dir_operations;
287 inode->i_mapping->a_ops = &f2fs_dblock_aops;
288 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
289
290 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
291 f2fs_lock_op(sbi);
292 err = f2fs_add_link(dentry, inode);
293 f2fs_unlock_op(sbi);
294 if (err)
295 goto out_fail;
296
297 alloc_nid_done(sbi, inode->i_ino);
298
299 d_instantiate(dentry, inode);
300 unlock_new_inode(inode);
301
302 return 0;
303
304out_fail:
305 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
306 clear_nlink(inode);
307 iget_failed(inode);
308 alloc_nid_failed(sbi, inode->i_ino);
309 return err;
310}
311
312static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
313{
314 struct inode *inode = dentry->d_inode;
315 if (f2fs_empty_dir(inode))
316 return f2fs_unlink(dir, dentry);
317 return -ENOTEMPTY;
318}
319
320static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
321 umode_t mode, dev_t rdev)
322{
323 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
324 struct inode *inode;
325 int err = 0;
326
327 if (!new_valid_dev(rdev))
328 return -EINVAL;
329
330 f2fs_balance_fs(sbi);
331
332 inode = f2fs_new_inode(dir, mode);
333 if (IS_ERR(inode))
334 return PTR_ERR(inode);
335
336 init_special_inode(inode, inode->i_mode, rdev);
337 inode->i_op = &f2fs_special_inode_operations;
338
339 f2fs_lock_op(sbi);
340 err = f2fs_add_link(dentry, inode);
341 f2fs_unlock_op(sbi);
342 if (err)
343 goto out;
344
345 alloc_nid_done(sbi, inode->i_ino);
346 d_instantiate(dentry, inode);
347 unlock_new_inode(inode);
348 return 0;
349out:
350 clear_nlink(inode);
351 iget_failed(inode);
352 alloc_nid_failed(sbi, inode->i_ino);
353 return err;
354}
355
356static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
357 struct inode *new_dir, struct dentry *new_dentry)
358{
359 struct f2fs_sb_info *sbi = F2FS_SB(old_dir->i_sb);
360 struct inode *old_inode = old_dentry->d_inode;
361 struct inode *new_inode = new_dentry->d_inode;
362 struct page *old_dir_page;
363 struct page *old_page, *new_page;
364 struct f2fs_dir_entry *old_dir_entry = NULL;
365 struct f2fs_dir_entry *old_entry;
366 struct f2fs_dir_entry *new_entry;
367 int err = -ENOENT;
368
369 f2fs_balance_fs(sbi);
370
371 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
372 if (!old_entry)
373 goto out;
374
375 if (S_ISDIR(old_inode->i_mode)) {
376 err = -EIO;
377 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
378 if (!old_dir_entry)
379 goto out_old;
380 }
381
382 if (new_inode) {
383
384 err = -ENOTEMPTY;
385 if (old_dir_entry && !f2fs_empty_dir(new_inode))
386 goto out_dir;
387
388 err = -ENOENT;
389 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
390 &new_page);
391 if (!new_entry)
392 goto out_dir;
393
394 f2fs_lock_op(sbi);
395
396 err = acquire_orphan_inode(sbi);
397 if (err)
398 goto put_out_dir;
399
400 if (update_dent_inode(old_inode, &new_dentry->d_name)) {
401 release_orphan_inode(sbi);
402 goto put_out_dir;
403 }
404
405 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
406
407 new_inode->i_ctime = CURRENT_TIME;
408 down_write(&F2FS_I(new_inode)->i_sem);
409 if (old_dir_entry)
410 drop_nlink(new_inode);
411 drop_nlink(new_inode);
412 up_write(&F2FS_I(new_inode)->i_sem);
413
414 mark_inode_dirty(new_inode);
415
416 if (!new_inode->i_nlink)
417 add_orphan_inode(sbi, new_inode->i_ino);
418 else
419 release_orphan_inode(sbi);
420
421 update_inode_page(old_inode);
422 update_inode_page(new_inode);
423 } else {
424 f2fs_lock_op(sbi);
425
426 err = f2fs_add_link(new_dentry, old_inode);
427 if (err) {
428 f2fs_unlock_op(sbi);
429 goto out_dir;
430 }
431
432 if (old_dir_entry) {
433 inc_nlink(new_dir);
434 update_inode_page(new_dir);
435 }
436 }
437
438 down_write(&F2FS_I(old_inode)->i_sem);
439 file_lost_pino(old_inode);
440 up_write(&F2FS_I(old_inode)->i_sem);
441
442 old_inode->i_ctime = CURRENT_TIME;
443 mark_inode_dirty(old_inode);
444
445 f2fs_delete_entry(old_entry, old_page, NULL);
446
447 if (old_dir_entry) {
448 if (old_dir != new_dir) {
449 f2fs_set_link(old_inode, old_dir_entry,
450 old_dir_page, new_dir);
451 update_inode_page(old_inode);
452 } else {
453 kunmap(old_dir_page);
454 f2fs_put_page(old_dir_page, 0);
455 }
456 drop_nlink(old_dir);
457 mark_inode_dirty(old_dir);
458 update_inode_page(old_dir);
459 }
460
461 f2fs_unlock_op(sbi);
462 return 0;
463
464put_out_dir:
465 f2fs_unlock_op(sbi);
466 kunmap(new_page);
467 f2fs_put_page(new_page, 0);
468out_dir:
469 if (old_dir_entry) {
470 kunmap(old_dir_page);
471 f2fs_put_page(old_dir_page, 0);
472 }
473out_old:
474 kunmap(old_page);
475 f2fs_put_page(old_page, 0);
476out:
477 return err;
478}
479
480static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
481 struct inode *new_dir, struct dentry *new_dentry)
482{
483 struct super_block *sb = old_dir->i_sb;
484 struct f2fs_sb_info *sbi = F2FS_SB(sb);
485 struct inode *old_inode = old_dentry->d_inode;
486 struct inode *new_inode = new_dentry->d_inode;
487 struct page *old_dir_page, *new_dir_page;
488 struct page *old_page, *new_page;
489 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
490 struct f2fs_dir_entry *old_entry, *new_entry;
491 int old_nlink = 0, new_nlink = 0;
492 int err = -ENOENT;
493
494 f2fs_balance_fs(sbi);
495
496 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
497 if (!old_entry)
498 goto out;
499
500 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
501 if (!new_entry)
502 goto out_old;
503
504
505 if (old_dir != new_dir) {
506 if (S_ISDIR(old_inode->i_mode)) {
507 err = -EIO;
508 old_dir_entry = f2fs_parent_dir(old_inode,
509 &old_dir_page);
510 if (!old_dir_entry)
511 goto out_new;
512 }
513
514 if (S_ISDIR(new_inode->i_mode)) {
515 err = -EIO;
516 new_dir_entry = f2fs_parent_dir(new_inode,
517 &new_dir_page);
518 if (!new_dir_entry)
519 goto out_old_dir;
520 }
521 }
522
523
524
525
526
527
528 if ((!old_dir_entry || !new_dir_entry) &&
529 old_dir_entry != new_dir_entry) {
530 old_nlink = old_dir_entry ? -1 : 1;
531 new_nlink = -old_nlink;
532 err = -EMLINK;
533 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
534 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
535 goto out_new_dir;
536 }
537
538 f2fs_lock_op(sbi);
539
540 err = update_dent_inode(old_inode, &new_dentry->d_name);
541 if (err)
542 goto out_unlock;
543
544 err = update_dent_inode(new_inode, &old_dentry->d_name);
545 if (err)
546 goto out_undo;
547
548
549 if (old_dir_entry)
550 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
551
552
553 if (new_dir_entry)
554 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
555
556
557 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
558
559 down_write(&F2FS_I(old_inode)->i_sem);
560 file_lost_pino(old_inode);
561 up_write(&F2FS_I(old_inode)->i_sem);
562
563 update_inode_page(old_inode);
564
565 old_dir->i_ctime = CURRENT_TIME;
566 if (old_nlink) {
567 down_write(&F2FS_I(old_dir)->i_sem);
568 if (old_nlink < 0)
569 drop_nlink(old_dir);
570 else
571 inc_nlink(old_dir);
572 up_write(&F2FS_I(old_dir)->i_sem);
573 }
574 mark_inode_dirty(old_dir);
575 update_inode_page(old_dir);
576
577
578 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
579
580 down_write(&F2FS_I(new_inode)->i_sem);
581 file_lost_pino(new_inode);
582 up_write(&F2FS_I(new_inode)->i_sem);
583
584 update_inode_page(new_inode);
585
586 new_dir->i_ctime = CURRENT_TIME;
587 if (new_nlink) {
588 down_write(&F2FS_I(new_dir)->i_sem);
589 if (new_nlink < 0)
590 drop_nlink(new_dir);
591 else
592 inc_nlink(new_dir);
593 up_write(&F2FS_I(new_dir)->i_sem);
594 }
595 mark_inode_dirty(new_dir);
596 update_inode_page(new_dir);
597
598 f2fs_unlock_op(sbi);
599 return 0;
600out_undo:
601
602 update_dent_inode(old_inode, &old_dentry->d_name);
603out_unlock:
604 f2fs_unlock_op(sbi);
605out_new_dir:
606 if (new_dir_entry) {
607 kunmap(new_dir_page);
608 f2fs_put_page(new_dir_page, 0);
609 }
610out_old_dir:
611 if (old_dir_entry) {
612 kunmap(old_dir_page);
613 f2fs_put_page(old_dir_page, 0);
614 }
615out_new:
616 kunmap(new_page);
617 f2fs_put_page(new_page, 0);
618out_old:
619 kunmap(old_page);
620 f2fs_put_page(old_page, 0);
621out:
622 return err;
623}
624
625static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
626 struct inode *new_dir, struct dentry *new_dentry,
627 unsigned int flags)
628{
629 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
630 return -EINVAL;
631
632 if (flags & RENAME_EXCHANGE) {
633 return f2fs_cross_rename(old_dir, old_dentry,
634 new_dir, new_dentry);
635 }
636
637
638
639
640 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry);
641}
642
643static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
644{
645 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
646 struct inode *inode;
647 int err;
648
649 inode = f2fs_new_inode(dir, mode);
650 if (IS_ERR(inode))
651 return PTR_ERR(inode);
652
653 inode->i_op = &f2fs_file_inode_operations;
654 inode->i_fop = &f2fs_file_operations;
655 inode->i_mapping->a_ops = &f2fs_dblock_aops;
656
657 f2fs_lock_op(sbi);
658 err = acquire_orphan_inode(sbi);
659 if (err)
660 goto out;
661
662 err = f2fs_do_tmpfile(inode, dir);
663 if (err)
664 goto release_out;
665
666
667
668
669
670 add_orphan_inode(sbi, inode->i_ino);
671 f2fs_unlock_op(sbi);
672
673 alloc_nid_done(sbi, inode->i_ino);
674 d_tmpfile(dentry, inode);
675 unlock_new_inode(inode);
676 return 0;
677
678release_out:
679 release_orphan_inode(sbi);
680out:
681 f2fs_unlock_op(sbi);
682 clear_nlink(inode);
683 iget_failed(inode);
684 alloc_nid_failed(sbi, inode->i_ino);
685 return err;
686}
687
688const struct inode_operations f2fs_dir_inode_operations = {
689 .create = f2fs_create,
690 .lookup = f2fs_lookup,
691 .link = f2fs_link,
692 .unlink = f2fs_unlink,
693 .symlink = f2fs_symlink,
694 .mkdir = f2fs_mkdir,
695 .rmdir = f2fs_rmdir,
696 .mknod = f2fs_mknod,
697 .rename2 = f2fs_rename2,
698 .tmpfile = f2fs_tmpfile,
699 .getattr = f2fs_getattr,
700 .setattr = f2fs_setattr,
701 .get_acl = f2fs_get_acl,
702 .set_acl = f2fs_set_acl,
703#ifdef CONFIG_F2FS_FS_XATTR
704 .setxattr = generic_setxattr,
705 .getxattr = generic_getxattr,
706 .listxattr = f2fs_listxattr,
707 .removexattr = generic_removexattr,
708#endif
709};
710
711const struct inode_operations f2fs_symlink_inode_operations = {
712 .readlink = generic_readlink,
713 .follow_link = page_follow_link_light,
714 .put_link = page_put_link,
715 .getattr = f2fs_getattr,
716 .setattr = f2fs_setattr,
717#ifdef CONFIG_F2FS_FS_XATTR
718 .setxattr = generic_setxattr,
719 .getxattr = generic_getxattr,
720 .listxattr = f2fs_listxattr,
721 .removexattr = generic_removexattr,
722#endif
723};
724
725const struct inode_operations f2fs_special_inode_operations = {
726 .getattr = f2fs_getattr,
727 .setattr = f2fs_setattr,
728 .get_acl = f2fs_get_acl,
729 .set_acl = f2fs_set_acl,
730#ifdef CONFIG_F2FS_FS_XATTR
731 .setxattr = generic_setxattr,
732 .getxattr = generic_getxattr,
733 .listxattr = f2fs_listxattr,
734 .removexattr = generic_removexattr,
735#endif
736};
737