gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / fs / f2fs / namei.c
blobf54119da2217ffbc82670d2e9b5f5f44efa6f940
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/namei.c
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
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>
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>
25 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
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;
34 inode = new_inode(dir->i_sb);
35 if (!inode)
36 return ERR_PTR(-ENOMEM);
38 f2fs_lock_op(sbi);
39 if (!f2fs_alloc_nid(sbi, &ino)) {
40 f2fs_unlock_op(sbi);
41 err = -ENOSPC;
42 goto fail;
44 f2fs_unlock_op(sbi);
46 nid_free = true;
48 inode_init_owner(inode, dir, mode);
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();
56 if (S_ISDIR(inode->i_mode))
57 F2FS_I(inode)->i_current_depth = 1;
59 err = insert_inode_locked(inode);
60 if (err) {
61 err = -EINVAL;
62 goto fail;
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);
72 err = dquot_initialize(inode);
73 if (err)
74 goto fail_drop;
76 set_inode_flag(inode, FI_NEW_INODE);
78 if (f2fs_may_encrypt(dir, inode))
79 f2fs_set_encrypted_inode(inode);
81 if (f2fs_sb_has_extra_attr(sbi)) {
82 set_inode_flag(inode, FI_EXTRA_ATTR);
83 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
86 if (test_opt(sbi, INLINE_XATTR))
87 set_inode_flag(inode, FI_INLINE_XATTR);
89 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
90 set_inode_flag(inode, FI_INLINE_DATA);
91 if (f2fs_may_inline_dentry(inode))
92 set_inode_flag(inode, FI_INLINE_DENTRY);
94 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
95 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
96 if (f2fs_has_inline_xattr(inode))
97 xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
98 /* Otherwise, will be 0 */
99 } else if (f2fs_has_inline_xattr(inode) ||
100 f2fs_has_inline_dentry(inode)) {
101 xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
103 F2FS_I(inode)->i_inline_xattr_size = xattr_size;
105 f2fs_init_extent_tree(inode, NULL);
107 stat_inc_inline_xattr(inode);
108 stat_inc_inline_inode(inode);
109 stat_inc_inline_dir(inode);
111 F2FS_I(inode)->i_flags =
112 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
114 if (S_ISDIR(inode->i_mode))
115 F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
117 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
118 set_inode_flag(inode, FI_PROJ_INHERIT);
120 if (f2fs_sb_has_compression(sbi)) {
121 /* Inherit the compression flag in directory */
122 if ((F2FS_I(dir)->i_flags & F2FS_COMPR_FL) &&
123 f2fs_may_compress(inode))
124 set_compress_context(inode);
127 f2fs_set_inode_flags(inode);
129 trace_f2fs_new_inode(inode, 0);
130 return inode;
132 fail:
133 trace_f2fs_new_inode(inode, err);
134 make_bad_inode(inode);
135 if (nid_free)
136 set_inode_flag(inode, FI_FREE_NID);
137 iput(inode);
138 return ERR_PTR(err);
139 fail_drop:
140 trace_f2fs_new_inode(inode, err);
141 dquot_drop(inode);
142 inode->i_flags |= S_NOQUOTA;
143 if (nid_free)
144 set_inode_flag(inode, FI_FREE_NID);
145 clear_nlink(inode);
146 unlock_new_inode(inode);
147 iput(inode);
148 return ERR_PTR(err);
151 static inline int is_extension_exist(const unsigned char *s, const char *sub)
153 size_t slen = strlen(s);
154 size_t sublen = strlen(sub);
155 int i;
157 if (sublen == 1 && *sub == '*')
158 return 1;
161 * filename format of multimedia file should be defined as:
162 * "filename + '.' + extension + (optional: '.' + temp extension)".
164 if (slen < sublen + 2)
165 return 0;
167 for (i = 1; i < slen - sublen; i++) {
168 if (s[i] != '.')
169 continue;
170 if (!strncasecmp(s + i + 1, sub, sublen))
171 return 1;
174 return 0;
178 * Set file's temperature for hot/cold data separation
180 static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
181 const unsigned char *name)
183 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
184 int i, cold_count, hot_count;
186 down_read(&sbi->sb_lock);
188 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
189 hot_count = sbi->raw_super->hot_ext_count;
191 for (i = 0; i < cold_count + hot_count; i++) {
192 if (is_extension_exist(name, extlist[i]))
193 break;
196 up_read(&sbi->sb_lock);
198 if (i == cold_count + hot_count)
199 return;
201 if (i < cold_count)
202 file_set_cold(inode);
203 else
204 file_set_hot(inode);
207 int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
208 bool hot, bool set)
210 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
211 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
212 int hot_count = sbi->raw_super->hot_ext_count;
213 int total_count = cold_count + hot_count;
214 int start, count;
215 int i;
217 if (set) {
218 if (total_count == F2FS_MAX_EXTENSION)
219 return -EINVAL;
220 } else {
221 if (!hot && !cold_count)
222 return -EINVAL;
223 if (hot && !hot_count)
224 return -EINVAL;
227 if (hot) {
228 start = cold_count;
229 count = total_count;
230 } else {
231 start = 0;
232 count = cold_count;
235 for (i = start; i < count; i++) {
236 if (strcmp(name, extlist[i]))
237 continue;
239 if (set)
240 return -EINVAL;
242 memcpy(extlist[i], extlist[i + 1],
243 F2FS_EXTENSION_LEN * (total_count - i - 1));
244 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
245 if (hot)
246 sbi->raw_super->hot_ext_count = hot_count - 1;
247 else
248 sbi->raw_super->extension_count =
249 cpu_to_le32(cold_count - 1);
250 return 0;
253 if (!set)
254 return -EINVAL;
256 if (hot) {
257 memcpy(extlist[count], name, strlen(name));
258 sbi->raw_super->hot_ext_count = hot_count + 1;
259 } else {
260 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
262 memcpy(buf, &extlist[cold_count],
263 F2FS_EXTENSION_LEN * hot_count);
264 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
265 memcpy(extlist[cold_count], name, strlen(name));
266 memcpy(&extlist[cold_count + 1], buf,
267 F2FS_EXTENSION_LEN * hot_count);
268 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
270 return 0;
273 static void set_compress_inode(struct f2fs_sb_info *sbi, struct inode *inode,
274 const unsigned char *name)
276 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
277 unsigned char (*ext)[F2FS_EXTENSION_LEN];
278 unsigned int ext_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
279 int i, cold_count, hot_count;
281 if (!f2fs_sb_has_compression(sbi) ||
282 is_inode_flag_set(inode, FI_COMPRESSED_FILE) ||
283 F2FS_I(inode)->i_flags & F2FS_NOCOMP_FL ||
284 !f2fs_may_compress(inode))
285 return;
287 down_read(&sbi->sb_lock);
289 cold_count = le32_to_cpu(sbi->raw_super->extension_count);
290 hot_count = sbi->raw_super->hot_ext_count;
292 for (i = cold_count; i < cold_count + hot_count; i++) {
293 if (is_extension_exist(name, extlist[i])) {
294 up_read(&sbi->sb_lock);
295 return;
299 up_read(&sbi->sb_lock);
301 ext = F2FS_OPTION(sbi).extensions;
303 for (i = 0; i < ext_cnt; i++) {
304 if (!is_extension_exist(name, ext[i]))
305 continue;
307 set_compress_context(inode);
308 return;
312 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
313 bool excl)
315 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
316 struct inode *inode;
317 nid_t ino = 0;
318 int err;
320 if (unlikely(f2fs_cp_error(sbi)))
321 return -EIO;
322 if (!f2fs_is_checkpoint_ready(sbi))
323 return -ENOSPC;
325 err = dquot_initialize(dir);
326 if (err)
327 return err;
329 inode = f2fs_new_inode(dir, mode);
330 if (IS_ERR(inode))
331 return PTR_ERR(inode);
333 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
334 set_file_temperature(sbi, inode, dentry->d_name.name);
336 set_compress_inode(sbi, inode, dentry->d_name.name);
338 inode->i_op = &f2fs_file_inode_operations;
339 inode->i_fop = &f2fs_file_operations;
340 inode->i_mapping->a_ops = &f2fs_dblock_aops;
341 ino = inode->i_ino;
343 f2fs_lock_op(sbi);
344 err = f2fs_add_link(dentry, inode);
345 if (err)
346 goto out;
347 f2fs_unlock_op(sbi);
349 f2fs_alloc_nid_done(sbi, ino);
351 d_instantiate_new(dentry, inode);
353 if (IS_DIRSYNC(dir))
354 f2fs_sync_fs(sbi->sb, 1);
356 f2fs_balance_fs(sbi, true);
357 return 0;
358 out:
359 f2fs_handle_failed_inode(inode);
360 return err;
363 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
364 struct dentry *dentry)
366 struct inode *inode = d_inode(old_dentry);
367 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
368 int err;
370 if (unlikely(f2fs_cp_error(sbi)))
371 return -EIO;
372 if (!f2fs_is_checkpoint_ready(sbi))
373 return -ENOSPC;
375 err = fscrypt_prepare_link(old_dentry, dir, dentry);
376 if (err)
377 return err;
379 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
380 (!projid_eq(F2FS_I(dir)->i_projid,
381 F2FS_I(old_dentry->d_inode)->i_projid)))
382 return -EXDEV;
384 err = dquot_initialize(dir);
385 if (err)
386 return err;
388 f2fs_balance_fs(sbi, true);
390 inode->i_ctime = current_time(inode);
391 ihold(inode);
393 set_inode_flag(inode, FI_INC_LINK);
394 f2fs_lock_op(sbi);
395 err = f2fs_add_link(dentry, inode);
396 if (err)
397 goto out;
398 f2fs_unlock_op(sbi);
400 d_instantiate(dentry, inode);
402 if (IS_DIRSYNC(dir))
403 f2fs_sync_fs(sbi->sb, 1);
404 return 0;
405 out:
406 clear_inode_flag(inode, FI_INC_LINK);
407 iput(inode);
408 f2fs_unlock_op(sbi);
409 return err;
412 struct dentry *f2fs_get_parent(struct dentry *child)
414 struct qstr dotdot = QSTR_INIT("..", 2);
415 struct page *page;
416 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
417 if (!ino) {
418 if (IS_ERR(page))
419 return ERR_CAST(page);
420 return ERR_PTR(-ENOENT);
422 return d_obtain_alias(f2fs_iget(child->d_sb, ino));
425 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
427 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
428 struct qstr dot = QSTR_INIT(".", 1);
429 struct qstr dotdot = QSTR_INIT("..", 2);
430 struct f2fs_dir_entry *de;
431 struct page *page;
432 int err = 0;
434 if (f2fs_readonly(sbi->sb)) {
435 f2fs_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint",
436 dir->i_ino, pino);
437 return 0;
440 err = dquot_initialize(dir);
441 if (err)
442 return err;
444 f2fs_balance_fs(sbi, true);
446 f2fs_lock_op(sbi);
448 de = f2fs_find_entry(dir, &dot, &page);
449 if (de) {
450 f2fs_put_page(page, 0);
451 } else if (IS_ERR(page)) {
452 err = PTR_ERR(page);
453 goto out;
454 } else {
455 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
456 if (err)
457 goto out;
460 de = f2fs_find_entry(dir, &dotdot, &page);
461 if (de)
462 f2fs_put_page(page, 0);
463 else if (IS_ERR(page))
464 err = PTR_ERR(page);
465 else
466 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
467 out:
468 if (!err)
469 clear_inode_flag(dir, FI_INLINE_DOTS);
471 f2fs_unlock_op(sbi);
472 return err;
475 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
476 unsigned int flags)
478 struct inode *inode = NULL;
479 struct f2fs_dir_entry *de;
480 struct page *page;
481 struct dentry *new;
482 nid_t ino = -1;
483 int err = 0;
484 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
485 struct fscrypt_name fname;
487 trace_f2fs_lookup_start(dir, dentry, flags);
489 if (dentry->d_name.len > F2FS_NAME_LEN) {
490 err = -ENAMETOOLONG;
491 goto out;
494 err = fscrypt_prepare_lookup(dir, dentry, &fname);
495 if (err == -ENOENT)
496 goto out_splice;
497 if (err)
498 goto out;
499 de = __f2fs_find_entry(dir, &fname, &page);
500 fscrypt_free_filename(&fname);
502 if (!de) {
503 if (IS_ERR(page)) {
504 err = PTR_ERR(page);
505 goto out;
507 goto out_splice;
510 ino = le32_to_cpu(de->ino);
511 f2fs_put_page(page, 0);
513 inode = f2fs_iget(dir->i_sb, ino);
514 if (IS_ERR(inode)) {
515 err = PTR_ERR(inode);
516 goto out;
519 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
520 err = __recover_dot_dentries(dir, root_ino);
521 if (err)
522 goto out_iput;
525 if (f2fs_has_inline_dots(inode)) {
526 err = __recover_dot_dentries(inode, dir->i_ino);
527 if (err)
528 goto out_iput;
530 if (IS_ENCRYPTED(dir) &&
531 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
532 !fscrypt_has_permitted_context(dir, inode)) {
533 f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",
534 dir->i_ino, inode->i_ino);
535 err = -EPERM;
536 goto out_iput;
538 out_splice:
539 #ifdef CONFIG_UNICODE
540 if (!inode && IS_CASEFOLDED(dir)) {
541 /* Eventually we want to call d_add_ci(dentry, NULL)
542 * for negative dentries in the encoding case as
543 * well. For now, prevent the negative dentry
544 * from being cached.
546 trace_f2fs_lookup_end(dir, dentry, ino, err);
547 return NULL;
549 #endif
550 new = d_splice_alias(inode, dentry);
551 err = PTR_ERR_OR_ZERO(new);
552 trace_f2fs_lookup_end(dir, dentry, ino, err);
553 return new;
554 out_iput:
555 iput(inode);
556 out:
557 trace_f2fs_lookup_end(dir, dentry, ino, err);
558 return ERR_PTR(err);
561 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
563 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
564 struct inode *inode = d_inode(dentry);
565 struct f2fs_dir_entry *de;
566 struct page *page;
567 int err = -ENOENT;
569 trace_f2fs_unlink_enter(dir, dentry);
571 if (unlikely(f2fs_cp_error(sbi)))
572 return -EIO;
574 err = dquot_initialize(dir);
575 if (err)
576 return err;
577 err = dquot_initialize(inode);
578 if (err)
579 return err;
581 de = f2fs_find_entry(dir, &dentry->d_name, &page);
582 if (!de) {
583 if (IS_ERR(page))
584 err = PTR_ERR(page);
585 goto fail;
588 f2fs_balance_fs(sbi, true);
590 f2fs_lock_op(sbi);
591 err = f2fs_acquire_orphan_inode(sbi);
592 if (err) {
593 f2fs_unlock_op(sbi);
594 f2fs_put_page(page, 0);
595 goto fail;
597 f2fs_delete_entry(de, page, dir, inode);
598 #ifdef CONFIG_UNICODE
599 /* VFS negative dentries are incompatible with Encoding and
600 * Case-insensitiveness. Eventually we'll want avoid
601 * invalidating the dentries here, alongside with returning the
602 * negative dentries at f2fs_lookup(), when it is better
603 * supported by the VFS for the CI case.
605 if (IS_CASEFOLDED(dir))
606 d_invalidate(dentry);
607 #endif
608 f2fs_unlock_op(sbi);
610 if (IS_DIRSYNC(dir))
611 f2fs_sync_fs(sbi->sb, 1);
612 fail:
613 trace_f2fs_unlink_exit(inode, err);
614 return err;
617 static const char *f2fs_get_link(struct dentry *dentry,
618 struct inode *inode,
619 struct delayed_call *done)
621 const char *link = page_get_link(dentry, inode, done);
622 if (!IS_ERR(link) && !*link) {
623 /* this is broken symlink case */
624 do_delayed_call(done);
625 clear_delayed_call(done);
626 link = ERR_PTR(-ENOENT);
628 return link;
631 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
632 const char *symname)
634 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
635 struct inode *inode;
636 size_t len = strlen(symname);
637 struct fscrypt_str disk_link;
638 int err;
640 if (unlikely(f2fs_cp_error(sbi)))
641 return -EIO;
642 if (!f2fs_is_checkpoint_ready(sbi))
643 return -ENOSPC;
645 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
646 &disk_link);
647 if (err)
648 return err;
650 err = dquot_initialize(dir);
651 if (err)
652 return err;
654 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
655 if (IS_ERR(inode))
656 return PTR_ERR(inode);
658 if (IS_ENCRYPTED(inode))
659 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
660 else
661 inode->i_op = &f2fs_symlink_inode_operations;
662 inode_nohighmem(inode);
663 inode->i_mapping->a_ops = &f2fs_dblock_aops;
665 f2fs_lock_op(sbi);
666 err = f2fs_add_link(dentry, inode);
667 if (err)
668 goto out_f2fs_handle_failed_inode;
669 f2fs_unlock_op(sbi);
670 f2fs_alloc_nid_done(sbi, inode->i_ino);
672 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
673 if (err)
674 goto err_out;
676 err = page_symlink(inode, disk_link.name, disk_link.len);
678 err_out:
679 d_instantiate_new(dentry, inode);
682 * Let's flush symlink data in order to avoid broken symlink as much as
683 * possible. Nevertheless, fsyncing is the best way, but there is no
684 * way to get a file descriptor in order to flush that.
686 * Note that, it needs to do dir->fsync to make this recoverable.
687 * If the symlink path is stored into inline_data, there is no
688 * performance regression.
690 if (!err) {
691 filemap_write_and_wait_range(inode->i_mapping, 0,
692 disk_link.len - 1);
694 if (IS_DIRSYNC(dir))
695 f2fs_sync_fs(sbi->sb, 1);
696 } else {
697 f2fs_unlink(dir, dentry);
700 f2fs_balance_fs(sbi, true);
701 goto out_free_encrypted_link;
703 out_f2fs_handle_failed_inode:
704 f2fs_handle_failed_inode(inode);
705 out_free_encrypted_link:
706 if (disk_link.name != (unsigned char *)symname)
707 kvfree(disk_link.name);
708 return err;
711 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
713 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
714 struct inode *inode;
715 int err;
717 if (unlikely(f2fs_cp_error(sbi)))
718 return -EIO;
720 err = dquot_initialize(dir);
721 if (err)
722 return err;
724 inode = f2fs_new_inode(dir, S_IFDIR | mode);
725 if (IS_ERR(inode))
726 return PTR_ERR(inode);
728 inode->i_op = &f2fs_dir_inode_operations;
729 inode->i_fop = &f2fs_dir_operations;
730 inode->i_mapping->a_ops = &f2fs_dblock_aops;
731 inode_nohighmem(inode);
733 set_inode_flag(inode, FI_INC_LINK);
734 f2fs_lock_op(sbi);
735 err = f2fs_add_link(dentry, inode);
736 if (err)
737 goto out_fail;
738 f2fs_unlock_op(sbi);
740 f2fs_alloc_nid_done(sbi, inode->i_ino);
742 d_instantiate_new(dentry, inode);
744 if (IS_DIRSYNC(dir))
745 f2fs_sync_fs(sbi->sb, 1);
747 f2fs_balance_fs(sbi, true);
748 return 0;
750 out_fail:
751 clear_inode_flag(inode, FI_INC_LINK);
752 f2fs_handle_failed_inode(inode);
753 return err;
756 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
758 struct inode *inode = d_inode(dentry);
759 if (f2fs_empty_dir(inode))
760 return f2fs_unlink(dir, dentry);
761 return -ENOTEMPTY;
764 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
765 umode_t mode, dev_t rdev)
767 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
768 struct inode *inode;
769 int err = 0;
771 if (unlikely(f2fs_cp_error(sbi)))
772 return -EIO;
773 if (!f2fs_is_checkpoint_ready(sbi))
774 return -ENOSPC;
776 err = dquot_initialize(dir);
777 if (err)
778 return err;
780 inode = f2fs_new_inode(dir, mode);
781 if (IS_ERR(inode))
782 return PTR_ERR(inode);
784 init_special_inode(inode, inode->i_mode, rdev);
785 inode->i_op = &f2fs_special_inode_operations;
787 f2fs_lock_op(sbi);
788 err = f2fs_add_link(dentry, inode);
789 if (err)
790 goto out;
791 f2fs_unlock_op(sbi);
793 f2fs_alloc_nid_done(sbi, inode->i_ino);
795 d_instantiate_new(dentry, inode);
797 if (IS_DIRSYNC(dir))
798 f2fs_sync_fs(sbi->sb, 1);
800 f2fs_balance_fs(sbi, true);
801 return 0;
802 out:
803 f2fs_handle_failed_inode(inode);
804 return err;
807 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
808 umode_t mode, struct inode **whiteout)
810 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
811 struct inode *inode;
812 int err;
814 err = dquot_initialize(dir);
815 if (err)
816 return err;
818 inode = f2fs_new_inode(dir, mode);
819 if (IS_ERR(inode))
820 return PTR_ERR(inode);
822 if (whiteout) {
823 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
824 inode->i_op = &f2fs_special_inode_operations;
825 } else {
826 inode->i_op = &f2fs_file_inode_operations;
827 inode->i_fop = &f2fs_file_operations;
828 inode->i_mapping->a_ops = &f2fs_dblock_aops;
831 f2fs_lock_op(sbi);
832 err = f2fs_acquire_orphan_inode(sbi);
833 if (err)
834 goto out;
836 err = f2fs_do_tmpfile(inode, dir);
837 if (err)
838 goto release_out;
841 * add this non-linked tmpfile to orphan list, in this way we could
842 * remove all unused data of tmpfile after abnormal power-off.
844 f2fs_add_orphan_inode(inode);
845 f2fs_alloc_nid_done(sbi, inode->i_ino);
847 if (whiteout) {
848 f2fs_i_links_write(inode, false);
849 inode->i_state |= I_LINKABLE;
850 *whiteout = inode;
851 } else {
852 d_tmpfile(dentry, inode);
854 /* link_count was changed by d_tmpfile as well. */
855 f2fs_unlock_op(sbi);
856 unlock_new_inode(inode);
858 f2fs_balance_fs(sbi, true);
859 return 0;
861 release_out:
862 f2fs_release_orphan_inode(sbi);
863 out:
864 f2fs_handle_failed_inode(inode);
865 return err;
868 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
870 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
872 if (unlikely(f2fs_cp_error(sbi)))
873 return -EIO;
874 if (!f2fs_is_checkpoint_ready(sbi))
875 return -ENOSPC;
877 return __f2fs_tmpfile(dir, dentry, mode, NULL);
880 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
882 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
883 return -EIO;
885 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
888 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
889 struct inode *new_dir, struct dentry *new_dentry,
890 unsigned int flags)
892 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
893 struct inode *old_inode = d_inode(old_dentry);
894 struct inode *new_inode = d_inode(new_dentry);
895 struct inode *whiteout = NULL;
896 struct page *old_dir_page = NULL;
897 struct page *old_page, *new_page = NULL;
898 struct f2fs_dir_entry *old_dir_entry = NULL;
899 struct f2fs_dir_entry *old_entry;
900 struct f2fs_dir_entry *new_entry;
901 int err;
903 if (unlikely(f2fs_cp_error(sbi)))
904 return -EIO;
905 if (!f2fs_is_checkpoint_ready(sbi))
906 return -ENOSPC;
908 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
909 (!projid_eq(F2FS_I(new_dir)->i_projid,
910 F2FS_I(old_dentry->d_inode)->i_projid)))
911 return -EXDEV;
914 * If new_inode is null, the below renaming flow will
915 * add a link in old_dir which can conver inline_dir.
916 * After then, if we failed to get the entry due to other
917 * reasons like ENOMEM, we had to remove the new entry.
918 * Instead of adding such the error handling routine, let's
919 * simply convert first here.
921 if (old_dir == new_dir && !new_inode) {
922 err = f2fs_try_convert_inline_dir(old_dir, new_dentry);
923 if (err)
924 return err;
927 if (flags & RENAME_WHITEOUT) {
928 err = f2fs_create_whiteout(old_dir, &whiteout);
929 if (err)
930 return err;
933 err = dquot_initialize(old_dir);
934 if (err)
935 goto out;
937 err = dquot_initialize(new_dir);
938 if (err)
939 goto out;
941 if (new_inode) {
942 err = dquot_initialize(new_inode);
943 if (err)
944 goto out;
947 err = -ENOENT;
948 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
949 if (!old_entry) {
950 if (IS_ERR(old_page))
951 err = PTR_ERR(old_page);
952 goto out;
955 if (S_ISDIR(old_inode->i_mode)) {
956 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
957 if (!old_dir_entry) {
958 if (IS_ERR(old_dir_page))
959 err = PTR_ERR(old_dir_page);
960 goto out_old;
964 if (new_inode) {
966 err = -ENOTEMPTY;
967 if (old_dir_entry && !f2fs_empty_dir(new_inode))
968 goto out_dir;
970 err = -ENOENT;
971 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
972 &new_page);
973 if (!new_entry) {
974 if (IS_ERR(new_page))
975 err = PTR_ERR(new_page);
976 goto out_dir;
979 f2fs_balance_fs(sbi, true);
981 f2fs_lock_op(sbi);
983 err = f2fs_acquire_orphan_inode(sbi);
984 if (err)
985 goto put_out_dir;
987 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
988 new_page = NULL;
990 new_inode->i_ctime = current_time(new_inode);
991 down_write(&F2FS_I(new_inode)->i_sem);
992 if (old_dir_entry)
993 f2fs_i_links_write(new_inode, false);
994 f2fs_i_links_write(new_inode, false);
995 up_write(&F2FS_I(new_inode)->i_sem);
997 if (!new_inode->i_nlink)
998 f2fs_add_orphan_inode(new_inode);
999 else
1000 f2fs_release_orphan_inode(sbi);
1001 } else {
1002 f2fs_balance_fs(sbi, true);
1004 f2fs_lock_op(sbi);
1006 err = f2fs_add_link(new_dentry, old_inode);
1007 if (err) {
1008 f2fs_unlock_op(sbi);
1009 goto out_dir;
1012 if (old_dir_entry)
1013 f2fs_i_links_write(new_dir, true);
1016 down_write(&F2FS_I(old_inode)->i_sem);
1017 if (!old_dir_entry || whiteout)
1018 file_lost_pino(old_inode);
1019 else
1020 /* adjust dir's i_pino to pass fsck check */
1021 f2fs_i_pino_write(old_inode, new_dir->i_ino);
1022 up_write(&F2FS_I(old_inode)->i_sem);
1024 old_inode->i_ctime = current_time(old_inode);
1025 f2fs_mark_inode_dirty_sync(old_inode, false);
1027 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
1028 old_page = NULL;
1030 if (whiteout) {
1031 set_inode_flag(whiteout, FI_INC_LINK);
1032 err = f2fs_add_link(old_dentry, whiteout);
1033 if (err)
1034 goto put_out_dir;
1035 whiteout->i_state &= ~I_LINKABLE;
1036 iput(whiteout);
1039 if (old_dir_entry) {
1040 if (old_dir != new_dir && !whiteout)
1041 f2fs_set_link(old_inode, old_dir_entry,
1042 old_dir_page, new_dir);
1043 else
1044 f2fs_put_page(old_dir_page, 0);
1045 f2fs_i_links_write(old_dir, false);
1047 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1048 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1049 if (S_ISDIR(old_inode->i_mode))
1050 f2fs_add_ino_entry(sbi, old_inode->i_ino,
1051 TRANS_DIR_INO);
1054 f2fs_unlock_op(sbi);
1056 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1057 f2fs_sync_fs(sbi->sb, 1);
1059 f2fs_update_time(sbi, REQ_TIME);
1060 return 0;
1062 put_out_dir:
1063 f2fs_unlock_op(sbi);
1064 f2fs_put_page(new_page, 0);
1065 out_dir:
1066 if (old_dir_entry)
1067 f2fs_put_page(old_dir_page, 0);
1068 out_old:
1069 f2fs_put_page(old_page, 0);
1070 out:
1071 if (whiteout)
1072 iput(whiteout);
1073 return err;
1076 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1077 struct inode *new_dir, struct dentry *new_dentry)
1079 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1080 struct inode *old_inode = d_inode(old_dentry);
1081 struct inode *new_inode = d_inode(new_dentry);
1082 struct page *old_dir_page, *new_dir_page;
1083 struct page *old_page, *new_page;
1084 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1085 struct f2fs_dir_entry *old_entry, *new_entry;
1086 int old_nlink = 0, new_nlink = 0;
1087 int err;
1089 if (unlikely(f2fs_cp_error(sbi)))
1090 return -EIO;
1091 if (!f2fs_is_checkpoint_ready(sbi))
1092 return -ENOSPC;
1094 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1095 !projid_eq(F2FS_I(new_dir)->i_projid,
1096 F2FS_I(old_dentry->d_inode)->i_projid)) ||
1097 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1098 !projid_eq(F2FS_I(old_dir)->i_projid,
1099 F2FS_I(new_dentry->d_inode)->i_projid)))
1100 return -EXDEV;
1102 err = dquot_initialize(old_dir);
1103 if (err)
1104 goto out;
1106 err = dquot_initialize(new_dir);
1107 if (err)
1108 goto out;
1110 err = -ENOENT;
1111 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1112 if (!old_entry) {
1113 if (IS_ERR(old_page))
1114 err = PTR_ERR(old_page);
1115 goto out;
1118 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1119 if (!new_entry) {
1120 if (IS_ERR(new_page))
1121 err = PTR_ERR(new_page);
1122 goto out_old;
1125 /* prepare for updating ".." directory entry info later */
1126 if (old_dir != new_dir) {
1127 if (S_ISDIR(old_inode->i_mode)) {
1128 old_dir_entry = f2fs_parent_dir(old_inode,
1129 &old_dir_page);
1130 if (!old_dir_entry) {
1131 if (IS_ERR(old_dir_page))
1132 err = PTR_ERR(old_dir_page);
1133 goto out_new;
1137 if (S_ISDIR(new_inode->i_mode)) {
1138 new_dir_entry = f2fs_parent_dir(new_inode,
1139 &new_dir_page);
1140 if (!new_dir_entry) {
1141 if (IS_ERR(new_dir_page))
1142 err = PTR_ERR(new_dir_page);
1143 goto out_old_dir;
1149 * If cross rename between file and directory those are not
1150 * in the same directory, we will inc nlink of file's parent
1151 * later, so we should check upper boundary of its nlink.
1153 if ((!old_dir_entry || !new_dir_entry) &&
1154 old_dir_entry != new_dir_entry) {
1155 old_nlink = old_dir_entry ? -1 : 1;
1156 new_nlink = -old_nlink;
1157 err = -EMLINK;
1158 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1159 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1160 goto out_new_dir;
1163 f2fs_balance_fs(sbi, true);
1165 f2fs_lock_op(sbi);
1167 /* update ".." directory entry info of old dentry */
1168 if (old_dir_entry)
1169 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1171 /* update ".." directory entry info of new dentry */
1172 if (new_dir_entry)
1173 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1175 /* update directory entry info of old dir inode */
1176 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1178 down_write(&F2FS_I(old_inode)->i_sem);
1179 if (!old_dir_entry)
1180 file_lost_pino(old_inode);
1181 else
1182 /* adjust dir's i_pino to pass fsck check */
1183 f2fs_i_pino_write(old_inode, new_dir->i_ino);
1184 up_write(&F2FS_I(old_inode)->i_sem);
1186 old_dir->i_ctime = current_time(old_dir);
1187 if (old_nlink) {
1188 down_write(&F2FS_I(old_dir)->i_sem);
1189 f2fs_i_links_write(old_dir, old_nlink > 0);
1190 up_write(&F2FS_I(old_dir)->i_sem);
1192 f2fs_mark_inode_dirty_sync(old_dir, false);
1194 /* update directory entry info of new dir inode */
1195 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1197 down_write(&F2FS_I(new_inode)->i_sem);
1198 if (!new_dir_entry)
1199 file_lost_pino(new_inode);
1200 else
1201 /* adjust dir's i_pino to pass fsck check */
1202 f2fs_i_pino_write(new_inode, old_dir->i_ino);
1203 up_write(&F2FS_I(new_inode)->i_sem);
1205 new_dir->i_ctime = current_time(new_dir);
1206 if (new_nlink) {
1207 down_write(&F2FS_I(new_dir)->i_sem);
1208 f2fs_i_links_write(new_dir, new_nlink > 0);
1209 up_write(&F2FS_I(new_dir)->i_sem);
1211 f2fs_mark_inode_dirty_sync(new_dir, false);
1213 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1214 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1215 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1218 f2fs_unlock_op(sbi);
1220 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1221 f2fs_sync_fs(sbi->sb, 1);
1223 f2fs_update_time(sbi, REQ_TIME);
1224 return 0;
1225 out_new_dir:
1226 if (new_dir_entry) {
1227 f2fs_put_page(new_dir_page, 0);
1229 out_old_dir:
1230 if (old_dir_entry) {
1231 f2fs_put_page(old_dir_page, 0);
1233 out_new:
1234 f2fs_put_page(new_page, 0);
1235 out_old:
1236 f2fs_put_page(old_page, 0);
1237 out:
1238 return err;
1241 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1242 struct inode *new_dir, struct dentry *new_dentry,
1243 unsigned int flags)
1245 int err;
1247 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1248 return -EINVAL;
1250 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1251 flags);
1252 if (err)
1253 return err;
1255 if (flags & RENAME_EXCHANGE) {
1256 return f2fs_cross_rename(old_dir, old_dentry,
1257 new_dir, new_dentry);
1260 * VFS has already handled the new dentry existence case,
1261 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1263 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1266 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1267 struct inode *inode,
1268 struct delayed_call *done)
1270 struct page *page;
1271 const char *target;
1273 if (!dentry)
1274 return ERR_PTR(-ECHILD);
1276 page = read_mapping_page(inode->i_mapping, 0, NULL);
1277 if (IS_ERR(page))
1278 return ERR_CAST(page);
1280 target = fscrypt_get_symlink(inode, page_address(page),
1281 inode->i_sb->s_blocksize, done);
1282 put_page(page);
1283 return target;
1286 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1287 .get_link = f2fs_encrypted_get_link,
1288 .getattr = f2fs_getattr,
1289 .setattr = f2fs_setattr,
1290 #ifdef CONFIG_F2FS_FS_XATTR
1291 .listxattr = f2fs_listxattr,
1292 #endif
1295 const struct inode_operations f2fs_dir_inode_operations = {
1296 .create = f2fs_create,
1297 .lookup = f2fs_lookup,
1298 .link = f2fs_link,
1299 .unlink = f2fs_unlink,
1300 .symlink = f2fs_symlink,
1301 .mkdir = f2fs_mkdir,
1302 .rmdir = f2fs_rmdir,
1303 .mknod = f2fs_mknod,
1304 .rename = f2fs_rename2,
1305 .tmpfile = f2fs_tmpfile,
1306 .getattr = f2fs_getattr,
1307 .setattr = f2fs_setattr,
1308 .get_acl = f2fs_get_acl,
1309 .set_acl = f2fs_set_acl,
1310 #ifdef CONFIG_F2FS_FS_XATTR
1311 .listxattr = f2fs_listxattr,
1312 #endif
1313 .fiemap = f2fs_fiemap,
1316 const struct inode_operations f2fs_symlink_inode_operations = {
1317 .get_link = f2fs_get_link,
1318 .getattr = f2fs_getattr,
1319 .setattr = f2fs_setattr,
1320 #ifdef CONFIG_F2FS_FS_XATTR
1321 .listxattr = f2fs_listxattr,
1322 #endif
1325 const struct inode_operations f2fs_special_inode_operations = {
1326 .getattr = f2fs_getattr,
1327 .setattr = f2fs_setattr,
1328 .get_acl = f2fs_get_acl,
1329 .set_acl = f2fs_set_acl,
1330 #ifdef CONFIG_F2FS_FS_XATTR
1331 .listxattr = f2fs_listxattr,
1332 #endif