1
2
3
4
5
6
7
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/pagemap.h>
11#include <linux/sched.h>
12#include <linux/ctype.h>
13#include <linux/random.h>
14#include <linux/dcache.h>
15#include <linux/namei.h>
16#include <linux/quotaops.h>
17
18#include "f2fs.h"
19#include "node.h"
20#include "segment.h"
21#include "xattr.h"
22#include "acl.h"
23#include <trace/events/f2fs.h>
24
25static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
26{
27 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
28 nid_t ino;
29 struct inode *inode;
30 bool nid_free = false;
31 int xattr_size = 0;
32 int err;
33
34 inode = new_inode(dir->i_sb);
35 if (!inode)
36 return ERR_PTR(-ENOMEM);
37
38 f2fs_lock_op(sbi);
39 if (!f2fs_alloc_nid(sbi, &ino)) {
40 f2fs_unlock_op(sbi);
41 err = -ENOSPC;
42 goto fail;
43 }
44 f2fs_unlock_op(sbi);
45
46 nid_free = true;
47
48 inode_init_owner(inode, dir, mode);
49
50 inode->i_ino = ino;
51 inode->i_blocks = 0;
52 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
53 F2FS_I(inode)->i_crtime = inode->i_mtime;
54 inode->i_generation = prandom_u32();
55
56 if (S_ISDIR(inode->i_mode))
57 F2FS_I(inode)->i_current_depth = 1;
58
59 err = insert_inode_locked(inode);
60 if (err) {
61 err = -EINVAL;
62 goto fail;
63 }
64
65 if (f2fs_sb_has_project_quota(sbi) &&
66 (F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
67 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
68 else
69 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
70 F2FS_DEF_PROJID);
71
72 err = dquot_initialize(inode);
73 if (err)
74 goto fail_drop;
75
76 set_inode_flag(inode, FI_NEW_INODE);
77
78
79 if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
80 f2fs_may_encrypt(inode))
81 f2fs_set_encrypted_inode(inode);
82
83 if (f2fs_sb_has_extra_attr(sbi)) {
84 set_inode_flag(inode, FI_EXTRA_ATTR);
85 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
86 }
87
88 if (test_opt(sbi, INLINE_XATTR))
89 set_inode_flag(inode, FI_INLINE_XATTR);
90
91 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
92 set_inode_flag(inode, FI_INLINE_DATA);
93 if (f2fs_may_inline_dentry(inode))
94 set_inode_flag(inode, FI_INLINE_DENTRY);
95
96 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
97 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
98 if (f2fs_has_inline_xattr(inode))
99 xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
100
101 } else if (f2fs_has_inline_xattr(inode) ||
102 f2fs_has_inline_dentry(inode)) {
103 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
104 }
105 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
106
107 f2fs_init_extent_tree(inode, NULL);
108
109 stat_inc_inline_xattr(inode);
110 stat_inc_inline_inode(inode);
111 stat_inc_inline_dir(inode);
112
113 F2FS_I(inode)->i_flags =
114 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
115
116 if (S_ISDIR(inode->i_mode))
117 F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
118
119 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
120 set_inode_flag(inode, FI_PROJ_INHERIT);
121
122 if (f2fs_sb_has_compression(sbi)) {
123
124 if ((F2FS_I(dir)->i_flags & F2FS_COMPR_FL) &&
125 f2fs_may_compress(inode))
126 set_compress_context(inode);
127 }
128
129 f2fs_set_inode_flags(inode);
130
131 trace_f2fs_new_inode(inode, 0);
132 return inode;
133
134fail:
135 trace_f2fs_new_inode(inode, err);
136 make_bad_inode(inode);
137 if (nid_free)
138 set_inode_flag(inode, FI_FREE_NID);
139 iput(inode);
140 return ERR_PTR(err);
141fail_drop:
142 trace_f2fs_new_inode(inode, err);
143 dquot_drop(inode);
144 inode->i_flags |= S_NOQUOTA;
145 if (nid_free)
146 set_inode_flag(inode, FI_FREE_NID);
147 clear_nlink(inode);
148 unlock_new_inode(inode);
149 iput(inode);
150 return ERR_PTR(err);
151}
152
153static inline int is_extension_exist(const unsigned char *s, const char *sub)
154{
155 size_t slen = strlen(s);
156 size_t sublen = strlen(sub);
157 int i;
158
159 if (sublen == 1 && *sub == '*')
160 return 1;
161
162
163
164
165
166 if (slen < sublen + 2)
167 return 0;
168
169 for (i = 1; i < slen - sublen; i++) {
170 if (s[i] != '.')
171 continue;
172 if (!strncasecmp(s + i + 1, sub, sublen))
173 return 1;
174 }
175
176 return 0;
177}
178
179
180
181
182static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
183 const unsigned char *name)
184{
185 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
186 int i, cold_count, hot_count;
187
188 down_read(&sbi->sb_lock);
189
190 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
191 hot_count = sbi->raw_super->hot_ext_count;
192
193 for (i = 0; i < cold_count + hot_count; i++) {
194 if (is_extension_exist(name, extlist[i]))
195 break;
196 }
197
198 up_read(&sbi->sb_lock);
199
200 if (i == cold_count + hot_count)
201 return;
202
203 if (i < cold_count)
204 file_set_cold(inode);
205 else
206 file_set_hot(inode);
207}
208
209int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
210 bool hot, bool set)
211{
212 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
213 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
214 int hot_count = sbi->raw_super->hot_ext_count;
215 int total_count = cold_count + hot_count;
216 int start, count;
217 int i;
218
219 if (set) {
220 if (total_count == F2FS_MAX_EXTENSION)
221 return -EINVAL;
222 } else {
223 if (!hot && !cold_count)
224 return -EINVAL;
225 if (hot && !hot_count)
226 return -EINVAL;
227 }
228
229 if (hot) {
230 start = cold_count;
231 count = total_count;
232 } else {
233 start = 0;
234 count = cold_count;
235 }
236
237 for (i = start; i < count; i++) {
238 if (strcmp(name, extlist[i]))
239 continue;
240
241 if (set)
242 return -EINVAL;
243
244 memcpy(extlist[i], extlist[i + 1],
245 F2FS_EXTENSION_LEN * (total_count - i - 1));
246 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
247 if (hot)
248 sbi->raw_super->hot_ext_count = hot_count - 1;
249 else
250 sbi->raw_super->extension_count =
251 cpu_to_le32(cold_count - 1);
252 return 0;
253 }
254
255 if (!set)
256 return -EINVAL;
257
258 if (hot) {
259 memcpy(extlist[count], name, strlen(name));
260 sbi->raw_super->hot_ext_count = hot_count + 1;
261 } else {
262 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
263
264 memcpy(buf, &extlist[cold_count],
265 F2FS_EXTENSION_LEN * hot_count);
266 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
267 memcpy(extlist[cold_count], name, strlen(name));
268 memcpy(&extlist[cold_count + 1], buf,
269 F2FS_EXTENSION_LEN * hot_count);
270 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
271 }
272 return 0;
273}
274
275static void set_compress_inode(struct f2fs_sb_info *sbi, struct inode *inode,
276 const unsigned char *name)
277{
278 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
279 unsigned char (*ext)[F2FS_EXTENSION_LEN];
280 unsigned int ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
281 int i, cold_count, hot_count;
282
283 if (!f2fs_sb_has_compression(sbi) ||
284 is_inode_flag_set(inode, FI_COMPRESSED_FILE) ||
285 F2FS_I(inode)->i_flags & F2FS_NOCOMP_FL ||
286 !f2fs_may_compress(inode))
287 return;
288
289 down_read(&sbi->sb_lock);
290
291 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
292 hot_count = sbi->raw_super->hot_ext_count;
293
294 for (i = cold_count; i < cold_count + hot_count; i++) {
295 if (is_extension_exist(name, extlist[i])) {
296 up_read(&sbi->sb_lock);
297 return;
298 }
299 }
300
301 up_read(&sbi->sb_lock);
302
303 ext = F2FS_OPTION(sbi).extensions;
304
305 for (i = 0; i < ext_cnt; i++) {
306 if (!is_extension_exist(name, ext[i]))
307 continue;
308
309 set_compress_context(inode);
310 return;
311 }
312}
313
314static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
315 bool excl)
316{
317 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
318 struct inode *inode;
319 nid_t ino = 0;
320 int err;
321
322 if (unlikely(f2fs_cp_error(sbi)))
323 return -EIO;
324 if (!f2fs_is_checkpoint_ready(sbi))
325 return -ENOSPC;
326
327 err = dquot_initialize(dir);
328 if (err)
329 return err;
330
331 inode = f2fs_new_inode(dir, mode);
332 if (IS_ERR(inode))
333 return PTR_ERR(inode);
334
335 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
336 set_file_temperature(sbi, inode, dentry->d_name.name);
337
338 set_compress_inode(sbi, inode, dentry->d_name.name);
339
340 inode->i_op = &f2fs_file_inode_operations;
341 inode->i_fop = &f2fs_file_operations;
342 inode->i_mapping->a_ops = &f2fs_dblock_aops;
343 ino = inode->i_ino;
344
345 f2fs_lock_op(sbi);
346 err = f2fs_add_link(dentry, inode);
347 if (err)
348 goto out;
349 f2fs_unlock_op(sbi);
350
351 f2fs_alloc_nid_done(sbi, ino);
352
353 d_instantiate_new(dentry, inode);
354
355 if (IS_DIRSYNC(dir))
356 f2fs_sync_fs(sbi->sb, 1);
357
358 f2fs_balance_fs(sbi, true);
359 return 0;
360out:
361 f2fs_handle_failed_inode(inode);
362 return err;
363}
364
365static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
366 struct dentry *dentry)
367{
368 struct inode *inode = d_inode(old_dentry);
369 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
370 int err;
371
372 if (unlikely(f2fs_cp_error(sbi)))
373 return -EIO;
374 if (!f2fs_is_checkpoint_ready(sbi))
375 return -ENOSPC;
376
377 err = fscrypt_prepare_link(old_dentry, dir, dentry);
378 if (err)
379 return err;
380
381 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
382 (!projid_eq(F2FS_I(dir)->i_projid,
383 F2FS_I(old_dentry->d_inode)->i_projid)))
384 return -EXDEV;
385
386 err = dquot_initialize(dir);
387 if (err)
388 return err;
389
390 f2fs_balance_fs(sbi, true);
391
392 inode->i_ctime = current_time(inode);
393 ihold(inode);
394
395 set_inode_flag(inode, FI_INC_LINK);
396 f2fs_lock_op(sbi);
397 err = f2fs_add_link(dentry, inode);
398 if (err)
399 goto out;
400 f2fs_unlock_op(sbi);
401
402 d_instantiate(dentry, inode);
403
404 if (IS_DIRSYNC(dir))
405 f2fs_sync_fs(sbi->sb, 1);
406 return 0;
407out:
408 clear_inode_flag(inode, FI_INC_LINK);
409 iput(inode);
410 f2fs_unlock_op(sbi);
411 return err;
412}
413
414struct dentry *f2fs_get_parent(struct dentry *child)
415{
416 struct qstr dotdot = QSTR_INIT("..", 2);
417 struct page *page;
418 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
419 if (!ino) {
420 if (IS_ERR(page))
421 return ERR_CAST(page);
422 return ERR_PTR(-ENOENT);
423 }
424 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
425}
426
427static int __recover_dot_dentries(struct inode *dir, nid_t pino)
428{
429 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
430 struct qstr dot = QSTR_INIT(".", 1);
431 struct qstr dotdot = QSTR_INIT("..", 2);
432 struct f2fs_dir_entry *de;
433 struct page *page;
434 int err = 0;
435
436 if (f2fs_readonly(sbi->sb)) {
437 f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
438 dir->i_ino, pino);
439 return 0;
440 }
441
442 err = dquot_initialize(dir);
443 if (err)
444 return err;
445
446 f2fs_balance_fs(sbi, true);
447
448 f2fs_lock_op(sbi);
449
450 de = f2fs_find_entry(dir, &dot, &page);
451 if (de) {
452 f2fs_put_page(page, 0);
453 } else if (IS_ERR(page)) {
454 err = PTR_ERR(page);
455 goto out;
456 } else {
457 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
458 if (err)
459 goto out;
460 }
461
462 de = f2fs_find_entry(dir, &dotdot, &page);
463 if (de)
464 f2fs_put_page(page, 0);
465 else if (IS_ERR(page))
466 err = PTR_ERR(page);
467 else
468 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
469out:
470 if (!err)
471 clear_inode_flag(dir, FI_INLINE_DOTS);
472
473 f2fs_unlock_op(sbi);
474 return err;
475}
476
477static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
478 unsigned int flags)
479{
480 struct inode *inode = NULL;
481 struct f2fs_dir_entry *de;
482 struct page *page;
483 struct dentry *new;
484 nid_t ino = -1;
485 int err = 0;
486 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
487 struct fscrypt_name fname;
488
489 trace_f2fs_lookup_start(dir, dentry, flags);
490
491 if (dentry->d_name.len > F2FS_NAME_LEN) {
492 err = -ENAMETOOLONG;
493 goto out;
494 }
495
496 err = fscrypt_prepare_lookup(dir, dentry, &fname);
497 if (err == -ENOENT)
498 goto out_splice;
499 if (err)
500 goto out;
501 de = __f2fs_find_entry(dir, &fname, &page);
502 fscrypt_free_filename(&fname);
503
504 if (!de) {
505 if (IS_ERR(page)) {
506 err = PTR_ERR(page);
507 goto out;
508 }
509 goto out_splice;
510 }
511
512 ino = le32_to_cpu(de->ino);
513 f2fs_put_page(page, 0);
514
515 inode = f2fs_iget(dir->i_sb, ino);
516 if (IS_ERR(inode)) {
517 err = PTR_ERR(inode);
518 goto out;
519 }
520
521 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
522 err = __recover_dot_dentries(dir, root_ino);
523 if (err)
524 goto out_iput;
525 }
526
527 if (f2fs_has_inline_dots(inode)) {
528 err = __recover_dot_dentries(inode, dir->i_ino);
529 if (err)
530 goto out_iput;
531 }
532 if (IS_ENCRYPTED(dir) &&
533 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
534 !fscrypt_has_permitted_context(dir, inode)) {
535 f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
536 dir->i_ino, inode->i_ino);
537 err = -EPERM;
538 goto out_iput;
539 }
540out_splice:
541#ifdef CONFIG_UNICODE
542 if (!inode && IS_CASEFOLDED(dir)) {
543
544
545
546
547
548 trace_f2fs_lookup_end(dir, dentry, ino, err);
549 return NULL;
550 }
551#endif
552 new = d_splice_alias(inode, dentry);
553 err = PTR_ERR_OR_ZERO(new);
554 trace_f2fs_lookup_end(dir, dentry, ino, err);
555 return new;
556out_iput:
557 iput(inode);
558out:
559 trace_f2fs_lookup_end(dir, dentry, ino, err);
560 return ERR_PTR(err);
561}
562
563static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
564{
565 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
566 struct inode *inode = d_inode(dentry);
567 struct f2fs_dir_entry *de;
568 struct page *page;
569 int err = -ENOENT;
570
571 trace_f2fs_unlink_enter(dir, dentry);
572
573 if (unlikely(f2fs_cp_error(sbi)))
574 return -EIO;
575
576 err = dquot_initialize(dir);
577 if (err)
578 return err;
579 err = dquot_initialize(inode);
580 if (err)
581 return err;
582
583 de = f2fs_find_entry(dir, &dentry->d_name, &page);
584 if (!de) {
585 if (IS_ERR(page))
586 err = PTR_ERR(page);
587 goto fail;
588 }
589
590 f2fs_balance_fs(sbi, true);
591
592 f2fs_lock_op(sbi);
593 err = f2fs_acquire_orphan_inode(sbi);
594 if (err) {
595 f2fs_unlock_op(sbi);
596 f2fs_put_page(page, 0);
597 goto fail;
598 }
599 f2fs_delete_entry(de, page, dir, inode);
600#ifdef CONFIG_UNICODE
601
602
603
604
605
606
607 if (IS_CASEFOLDED(dir))
608 d_invalidate(dentry);
609#endif
610 f2fs_unlock_op(sbi);
611
612 if (IS_DIRSYNC(dir))
613 f2fs_sync_fs(sbi->sb, 1);
614fail:
615 trace_f2fs_unlink_exit(inode, err);
616 return err;
617}
618
619static const char *f2fs_get_link(struct dentry *dentry,
620 struct inode *inode,
621 struct delayed_call *done)
622{
623 const char *link = page_get_link(dentry, inode, done);
624 if (!IS_ERR(link) && !*link) {
625
626 do_delayed_call(done);
627 clear_delayed_call(done);
628 link = ERR_PTR(-ENOENT);
629 }
630 return link;
631}
632
633static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
634 const char *symname)
635{
636 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
637 struct inode *inode;
638 size_t len = strlen(symname);
639 struct fscrypt_str disk_link;
640 int err;
641
642 if (unlikely(f2fs_cp_error(sbi)))
643 return -EIO;
644 if (!f2fs_is_checkpoint_ready(sbi))
645 return -ENOSPC;
646
647 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
648 &disk_link);
649 if (err)
650 return err;
651
652 err = dquot_initialize(dir);
653 if (err)
654 return err;
655
656 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
657 if (IS_ERR(inode))
658 return PTR_ERR(inode);
659
660 if (IS_ENCRYPTED(inode))
661 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
662 else
663 inode->i_op = &f2fs_symlink_inode_operations;
664 inode_nohighmem(inode);
665 inode->i_mapping->a_ops = &f2fs_dblock_aops;
666
667 f2fs_lock_op(sbi);
668 err = f2fs_add_link(dentry, inode);
669 if (err)
670 goto out_f2fs_handle_failed_inode;
671 f2fs_unlock_op(sbi);
672 f2fs_alloc_nid_done(sbi, inode->i_ino);
673
674 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
675 if (err)
676 goto err_out;
677
678 err = page_symlink(inode, disk_link.name, disk_link.len);
679
680err_out:
681 d_instantiate_new(dentry, inode);
682
683
684
685
686
687
688
689
690
691
692 if (!err) {
693 filemap_write_and_wait_range(inode->i_mapping, 0,
694 disk_link.len - 1);
695
696 if (IS_DIRSYNC(dir))
697 f2fs_sync_fs(sbi->sb, 1);
698 } else {
699 f2fs_unlink(dir, dentry);
700 }
701
702 f2fs_balance_fs(sbi, true);
703 goto out_free_encrypted_link;
704
705out_f2fs_handle_failed_inode:
706 f2fs_handle_failed_inode(inode);
707out_free_encrypted_link:
708 if (disk_link.name != (unsigned char *)symname)
709 kvfree(disk_link.name);
710 return err;
711}
712
713static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
714{
715 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
716 struct inode *inode;
717 int err;
718
719 if (unlikely(f2fs_cp_error(sbi)))
720 return -EIO;
721
722 err = dquot_initialize(dir);
723 if (err)
724 return err;
725
726 inode = f2fs_new_inode(dir, S_IFDIR | mode);
727 if (IS_ERR(inode))
728 return PTR_ERR(inode);
729
730 inode->i_op = &f2fs_dir_inode_operations;
731 inode->i_fop = &f2fs_dir_operations;
732 inode->i_mapping->a_ops = &f2fs_dblock_aops;
733 inode_nohighmem(inode);
734
735 set_inode_flag(inode, FI_INC_LINK);
736 f2fs_lock_op(sbi);
737 err = f2fs_add_link(dentry, inode);
738 if (err)
739 goto out_fail;
740 f2fs_unlock_op(sbi);
741
742 f2fs_alloc_nid_done(sbi, inode->i_ino);
743
744 d_instantiate_new(dentry, inode);
745
746 if (IS_DIRSYNC(dir))
747 f2fs_sync_fs(sbi->sb, 1);
748
749 f2fs_balance_fs(sbi, true);
750 return 0;
751
752out_fail:
753 clear_inode_flag(inode, FI_INC_LINK);
754 f2fs_handle_failed_inode(inode);
755 return err;
756}
757
758static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
759{
760 struct inode *inode = d_inode(dentry);
761 if (f2fs_empty_dir(inode))
762 return f2fs_unlink(dir, dentry);
763 return -ENOTEMPTY;
764}
765
766static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
767 umode_t mode, dev_t rdev)
768{
769 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
770 struct inode *inode;
771 int err = 0;
772
773 if (unlikely(f2fs_cp_error(sbi)))
774 return -EIO;
775 if (!f2fs_is_checkpoint_ready(sbi))
776 return -ENOSPC;
777
778 err = dquot_initialize(dir);
779 if (err)
780 return err;
781
782 inode = f2fs_new_inode(dir, mode);
783 if (IS_ERR(inode))
784 return PTR_ERR(inode);
785
786 init_special_inode(inode, inode->i_mode, rdev);
787 inode->i_op = &f2fs_special_inode_operations;
788
789 f2fs_lock_op(sbi);
790 err = f2fs_add_link(dentry, inode);
791 if (err)
792 goto out;
793 f2fs_unlock_op(sbi);
794
795 f2fs_alloc_nid_done(sbi, inode->i_ino);
796
797 d_instantiate_new(dentry, inode);
798
799 if (IS_DIRSYNC(dir))
800 f2fs_sync_fs(sbi->sb, 1);
801
802 f2fs_balance_fs(sbi, true);
803 return 0;
804out:
805 f2fs_handle_failed_inode(inode);
806 return err;
807}
808
809static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
810 umode_t mode, struct inode **whiteout)
811{
812 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
813 struct inode *inode;
814 int err;
815
816 err = dquot_initialize(dir);
817 if (err)
818 return err;
819
820 inode = f2fs_new_inode(dir, mode);
821 if (IS_ERR(inode))
822 return PTR_ERR(inode);
823
824 if (whiteout) {
825 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
826 inode->i_op = &f2fs_special_inode_operations;
827 } else {
828 inode->i_op = &f2fs_file_inode_operations;
829 inode->i_fop = &f2fs_file_operations;
830 inode->i_mapping->a_ops = &f2fs_dblock_aops;
831 }
832
833 f2fs_lock_op(sbi);
834 err = f2fs_acquire_orphan_inode(sbi);
835 if (err)
836 goto out;
837
838 err = f2fs_do_tmpfile(inode, dir);
839 if (err)
840 goto release_out;
841
842
843
844
845
846 f2fs_add_orphan_inode(inode);
847 f2fs_alloc_nid_done(sbi, inode->i_ino);
848
849 if (whiteout) {
850 f2fs_i_links_write(inode, false);
851 inode->i_state |= I_LINKABLE;
852 *whiteout = inode;
853 } else {
854 d_tmpfile(dentry, inode);
855 }
856
857 f2fs_unlock_op(sbi);
858 unlock_new_inode(inode);
859
860 f2fs_balance_fs(sbi, true);
861 return 0;
862
863release_out:
864 f2fs_release_orphan_inode(sbi);
865out:
866 f2fs_handle_failed_inode(inode);
867 return err;
868}
869
870static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
871{
872 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
873
874 if (unlikely(f2fs_cp_error(sbi)))
875 return -EIO;
876 if (!f2fs_is_checkpoint_ready(sbi))
877 return -ENOSPC;
878
879 if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
880 int err = fscrypt_get_encryption_info(dir);
881 if (err)
882 return err;
883 }
884
885 return __f2fs_tmpfile(dir, dentry, mode, NULL);
886}
887
888static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
889{
890 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
891 return -EIO;
892
893 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
894}
895
896static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
897 struct inode *new_dir, struct dentry *new_dentry,
898 unsigned int flags)
899{
900 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
901 struct inode *old_inode = d_inode(old_dentry);
902 struct inode *new_inode = d_inode(new_dentry);
903 struct inode *whiteout = NULL;
904 struct page *old_dir_page = NULL;
905 struct page *old_page, *new_page = NULL;
906 struct f2fs_dir_entry *old_dir_entry = NULL;
907 struct f2fs_dir_entry *old_entry;
908 struct f2fs_dir_entry *new_entry;
909 int err;
910
911 if (unlikely(f2fs_cp_error(sbi)))
912 return -EIO;
913 if (!f2fs_is_checkpoint_ready(sbi))
914 return -ENOSPC;
915
916 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
917 (!projid_eq(F2FS_I(new_dir)->i_projid,
918 F2FS_I(old_dentry->d_inode)->i_projid)))
919 return -EXDEV;
920
921
922
923
924
925
926
927
928
929 if (old_dir == new_dir && !new_inode) {
930 err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
931 if (err)
932 return err;
933 }
934
935 if (flags & RENAME_WHITEOUT) {
936 err = f2fs_create_whiteout(old_dir, &whiteout);
937 if (err)
938 return err;
939 }
940
941 err = dquot_initialize(old_dir);
942 if (err)
943 goto out;
944
945 err = dquot_initialize(new_dir);
946 if (err)
947 goto out;
948
949 if (new_inode) {
950 err = dquot_initialize(new_inode);
951 if (err)
952 goto out;
953 }
954
955 err = -ENOENT;
956 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
957 if (!old_entry) {
958 if (IS_ERR(old_page))
959 err = PTR_ERR(old_page);
960 goto out;
961 }
962
963 if (S_ISDIR(old_inode->i_mode)) {
964 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
965 if (!old_dir_entry) {
966 if (IS_ERR(old_dir_page))
967 err = PTR_ERR(old_dir_page);
968 goto out_old;
969 }
970 }
971
972 if (new_inode) {
973
974 err = -ENOTEMPTY;
975 if (old_dir_entry && !f2fs_empty_dir(new_inode))
976 goto out_dir;
977
978 err = -ENOENT;
979 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
980 &new_page);
981 if (!new_entry) {
982 if (IS_ERR(new_page))
983 err = PTR_ERR(new_page);
984 goto out_dir;
985 }
986
987 f2fs_balance_fs(sbi, true);
988
989 f2fs_lock_op(sbi);
990
991 err = f2fs_acquire_orphan_inode(sbi);
992 if (err)
993 goto put_out_dir;
994
995 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
996 new_page = NULL;
997
998 new_inode->i_ctime = current_time(new_inode);
999 down_write(&F2FS_I(new_inode)->i_sem);
1000 if (old_dir_entry)
1001 f2fs_i_links_write(new_inode, false);
1002 f2fs_i_links_write(new_inode, false);
1003 up_write(&F2FS_I(new_inode)->i_sem);
1004
1005 if (!new_inode->i_nlink)
1006 f2fs_add_orphan_inode(new_inode);
1007 else
1008 f2fs_release_orphan_inode(sbi);
1009 } else {
1010 f2fs_balance_fs(sbi, true);
1011
1012 f2fs_lock_op(sbi);
1013
1014 err = f2fs_add_link(new_dentry, old_inode);
1015 if (err) {
1016 f2fs_unlock_op(sbi);
1017 goto out_dir;
1018 }
1019
1020 if (old_dir_entry)
1021 f2fs_i_links_write(new_dir, true);
1022 }
1023
1024 down_write(&F2FS_I(old_inode)->i_sem);
1025 if (!old_dir_entry || whiteout)
1026 file_lost_pino(old_inode);
1027 else
1028
1029 f2fs_i_pino_write(old_inode, new_dir->i_ino);
1030 up_write(&F2FS_I(old_inode)->i_sem);
1031
1032 old_inode->i_ctime = current_time(old_inode);
1033 f2fs_mark_inode_dirty_sync(old_inode, false);
1034
1035 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
1036 old_page = NULL;
1037
1038 if (whiteout) {
1039 set_inode_flag(whiteout, FI_INC_LINK);
1040 err = f2fs_add_link(old_dentry, whiteout);
1041 if (err)
1042 goto put_out_dir;
1043 whiteout->i_state &= ~I_LINKABLE;
1044 iput(whiteout);
1045 }
1046
1047 if (old_dir_entry) {
1048 if (old_dir != new_dir && !whiteout)
1049 f2fs_set_link(old_inode, old_dir_entry,
1050 old_dir_page, new_dir);
1051 else
1052 f2fs_put_page(old_dir_page, 0);
1053 f2fs_i_links_write(old_dir, false);
1054 }
1055 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1056 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1057 if (S_ISDIR(old_inode->i_mode))
1058 f2fs_add_ino_entry(sbi, old_inode->i_ino,
1059 TRANS_DIR_INO);
1060 }
1061
1062 f2fs_unlock_op(sbi);
1063
1064 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1065 f2fs_sync_fs(sbi->sb, 1);
1066
1067 f2fs_update_time(sbi, REQ_TIME);
1068 return 0;
1069
1070put_out_dir:
1071 f2fs_unlock_op(sbi);
1072 f2fs_put_page(new_page, 0);
1073out_dir:
1074 if (old_dir_entry)
1075 f2fs_put_page(old_dir_page, 0);
1076out_old:
1077 f2fs_put_page(old_page, 0);
1078out:
1079 if (whiteout)
1080 iput(whiteout);
1081 return err;
1082}
1083
1084static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1085 struct inode *new_dir, struct dentry *new_dentry)
1086{
1087 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1088 struct inode *old_inode = d_inode(old_dentry);
1089 struct inode *new_inode = d_inode(new_dentry);
1090 struct page *old_dir_page, *new_dir_page;
1091 struct page *old_page, *new_page;
1092 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1093 struct f2fs_dir_entry *old_entry, *new_entry;
1094 int old_nlink = 0, new_nlink = 0;
1095 int err;
1096
1097 if (unlikely(f2fs_cp_error(sbi)))
1098 return -EIO;
1099 if (!f2fs_is_checkpoint_ready(sbi))
1100 return -ENOSPC;
1101
1102 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1103 !projid_eq(F2FS_I(new_dir)->i_projid,
1104 F2FS_I(old_dentry->d_inode)->i_projid)) ||
1105 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1106 !projid_eq(F2FS_I(old_dir)->i_projid,
1107 F2FS_I(new_dentry->d_inode)->i_projid)))
1108 return -EXDEV;
1109
1110 err = dquot_initialize(old_dir);
1111 if (err)
1112 goto out;
1113
1114 err = dquot_initialize(new_dir);
1115 if (err)
1116 goto out;
1117
1118 err = -ENOENT;
1119 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1120 if (!old_entry) {
1121 if (IS_ERR(old_page))
1122 err = PTR_ERR(old_page);
1123 goto out;
1124 }
1125
1126 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1127 if (!new_entry) {
1128 if (IS_ERR(new_page))
1129 err = PTR_ERR(new_page);
1130 goto out_old;
1131 }
1132
1133
1134 if (old_dir != new_dir) {
1135 if (S_ISDIR(old_inode->i_mode)) {
1136 old_dir_entry = f2fs_parent_dir(old_inode,
1137 &old_dir_page);
1138 if (!old_dir_entry) {
1139 if (IS_ERR(old_dir_page))
1140 err = PTR_ERR(old_dir_page);
1141 goto out_new;
1142 }
1143 }
1144
1145 if (S_ISDIR(new_inode->i_mode)) {
1146 new_dir_entry = f2fs_parent_dir(new_inode,
1147 &new_dir_page);
1148 if (!new_dir_entry) {
1149 if (IS_ERR(new_dir_page))
1150 err = PTR_ERR(new_dir_page);
1151 goto out_old_dir;
1152 }
1153 }
1154 }
1155
1156
1157
1158
1159
1160
1161 if ((!old_dir_entry || !new_dir_entry) &&
1162 old_dir_entry != new_dir_entry) {
1163 old_nlink = old_dir_entry ? -1 : 1;
1164 new_nlink = -old_nlink;
1165 err = -EMLINK;
1166 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1167 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1168 goto out_new_dir;
1169 }
1170
1171 f2fs_balance_fs(sbi, true);
1172
1173 f2fs_lock_op(sbi);
1174
1175
1176 if (old_dir_entry)
1177 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1178
1179
1180 if (new_dir_entry)
1181 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1182
1183
1184 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1185
1186 down_write(&F2FS_I(old_inode)->i_sem);
1187 if (!old_dir_entry)
1188 file_lost_pino(old_inode);
1189 else
1190
1191 f2fs_i_pino_write(old_inode, new_dir->i_ino);
1192 up_write(&F2FS_I(old_inode)->i_sem);
1193
1194 old_dir->i_ctime = current_time(old_dir);
1195 if (old_nlink) {
1196 down_write(&F2FS_I(old_dir)->i_sem);
1197 f2fs_i_links_write(old_dir, old_nlink > 0);
1198 up_write(&F2FS_I(old_dir)->i_sem);
1199 }
1200 f2fs_mark_inode_dirty_sync(old_dir, false);
1201
1202
1203 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1204
1205 down_write(&F2FS_I(new_inode)->i_sem);
1206 if (!new_dir_entry)
1207 file_lost_pino(new_inode);
1208 else
1209
1210 f2fs_i_pino_write(new_inode, old_dir->i_ino);
1211 up_write(&F2FS_I(new_inode)->i_sem);
1212
1213 new_dir->i_ctime = current_time(new_dir);
1214 if (new_nlink) {
1215 down_write(&F2FS_I(new_dir)->i_sem);
1216 f2fs_i_links_write(new_dir, new_nlink > 0);
1217 up_write(&F2FS_I(new_dir)->i_sem);
1218 }
1219 f2fs_mark_inode_dirty_sync(new_dir, false);
1220
1221 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1222 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1223 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1224 }
1225
1226 f2fs_unlock_op(sbi);
1227
1228 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1229 f2fs_sync_fs(sbi->sb, 1);
1230
1231 f2fs_update_time(sbi, REQ_TIME);
1232 return 0;
1233out_new_dir:
1234 if (new_dir_entry) {
1235 f2fs_put_page(new_dir_page, 0);
1236 }
1237out_old_dir:
1238 if (old_dir_entry) {
1239 f2fs_put_page(old_dir_page, 0);
1240 }
1241out_new:
1242 f2fs_put_page(new_page, 0);
1243out_old:
1244 f2fs_put_page(old_page, 0);
1245out:
1246 return err;
1247}
1248
1249static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1250 struct inode *new_dir, struct dentry *new_dentry,
1251 unsigned int flags)
1252{
1253 int err;
1254
1255 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1256 return -EINVAL;
1257
1258 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1259 flags);
1260 if (err)
1261 return err;
1262
1263 if (flags & RENAME_EXCHANGE) {
1264 return f2fs_cross_rename(old_dir, old_dentry,
1265 new_dir, new_dentry);
1266 }
1267
1268
1269
1270
1271 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1272}
1273
1274static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1275 struct inode *inode,
1276 struct delayed_call *done)
1277{
1278 struct page *page;
1279 const char *target;
1280
1281 if (!dentry)
1282 return ERR_PTR(-ECHILD);
1283
1284 page = read_mapping_page(inode->i_mapping, 0, NULL);
1285 if (IS_ERR(page))
1286 return ERR_CAST(page);
1287
1288 target = fscrypt_get_symlink(inode, page_address(page),
1289 inode->i_sb->s_blocksize, done);
1290 put_page(page);
1291 return target;
1292}
1293
1294const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1295 .get_link = f2fs_encrypted_get_link,
1296 .getattr = f2fs_getattr,
1297 .setattr = f2fs_setattr,
1298#ifdef CONFIG_F2FS_FS_XATTR
1299 .listxattr = f2fs_listxattr,
1300#endif
1301};
1302
1303const struct inode_operations f2fs_dir_inode_operations = {
1304 .create = f2fs_create,
1305 .lookup = f2fs_lookup,
1306 .link = f2fs_link,
1307 .unlink = f2fs_unlink,
1308 .symlink = f2fs_symlink,
1309 .mkdir = f2fs_mkdir,
1310 .rmdir = f2fs_rmdir,
1311 .mknod = f2fs_mknod,
1312 .rename = f2fs_rename2,
1313 .tmpfile = f2fs_tmpfile,
1314 .getattr = f2fs_getattr,
1315 .setattr = f2fs_setattr,
1316 .get_acl = f2fs_get_acl,
1317 .set_acl = f2fs_set_acl,
1318#ifdef CONFIG_F2FS_FS_XATTR
1319 .listxattr = f2fs_listxattr,
1320#endif
1321 .fiemap = f2fs_fiemap,
1322};
1323
1324const struct inode_operations f2fs_symlink_inode_operations = {
1325 .get_link = f2fs_get_link,
1326 .getattr = f2fs_getattr,
1327 .setattr = f2fs_setattr,
1328#ifdef CONFIG_F2FS_FS_XATTR
1329 .listxattr = f2fs_listxattr,
1330#endif
1331};
1332
1333const struct inode_operations f2fs_special_inode_operations = {
1334 .getattr = f2fs_getattr,
1335 .setattr = f2fs_setattr,
1336 .get_acl = f2fs_get_acl,
1337 .set_acl = f2fs_set_acl,
1338#ifdef CONFIG_F2FS_FS_XATTR
1339 .listxattr = f2fs_listxattr,
1340#endif
1341};
1342