irqchip: Fix dependencies for archs w/o HAS_IOMEM
[linux/fpc-iii.git] / fs / f2fs / namei.c
blobe7587fce1b8065bf80f35571d4a2c25b40b7c265
1 /*
2 * fs/f2fs/namei.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
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 #include <linux/namei.h>
19 #include "f2fs.h"
20 #include "node.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 err;
33 inode = new_inode(dir->i_sb);
34 if (!inode)
35 return ERR_PTR(-ENOMEM);
37 f2fs_lock_op(sbi);
38 if (!alloc_nid(sbi, &ino)) {
39 f2fs_unlock_op(sbi);
40 err = -ENOSPC;
41 goto fail;
43 f2fs_unlock_op(sbi);
45 inode_init_owner(inode, dir, mode);
47 inode->i_ino = ino;
48 inode->i_blocks = 0;
49 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
50 inode->i_generation = sbi->s_next_generation++;
52 err = insert_inode_locked(inode);
53 if (err) {
54 err = -EINVAL;
55 nid_free = true;
56 goto fail;
59 /* If the directory encrypted, then we should encrypt the inode. */
60 if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
61 f2fs_set_encrypted_inode(inode);
63 if (f2fs_may_inline_data(inode))
64 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
65 if (f2fs_may_inline_dentry(inode))
66 set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);
68 f2fs_init_extent_tree(inode, NULL);
70 stat_inc_inline_xattr(inode);
71 stat_inc_inline_inode(inode);
72 stat_inc_inline_dir(inode);
74 trace_f2fs_new_inode(inode, 0);
75 mark_inode_dirty(inode);
76 return inode;
78 fail:
79 trace_f2fs_new_inode(inode, err);
80 make_bad_inode(inode);
81 if (nid_free)
82 set_inode_flag(F2FS_I(inode), FI_FREE_NID);
83 iput(inode);
84 return ERR_PTR(err);
87 static int is_multimedia_file(const unsigned char *s, const char *sub)
89 size_t slen = strlen(s);
90 size_t sublen = strlen(sub);
93 * filename format of multimedia file should be defined as:
94 * "filename + '.' + extension".
96 if (slen < sublen + 2)
97 return 0;
99 if (s[slen - sublen - 1] != '.')
100 return 0;
102 return !strncasecmp(s + slen - sublen, sub, sublen);
106 * Set multimedia files as cold files for hot/cold data separation
108 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
109 const unsigned char *name)
111 int i;
112 __u8 (*extlist)[8] = sbi->raw_super->extension_list;
114 int count = le32_to_cpu(sbi->raw_super->extension_count);
115 for (i = 0; i < count; i++) {
116 if (is_multimedia_file(name, extlist[i])) {
117 file_set_cold(inode);
118 break;
123 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
124 bool excl)
126 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
127 struct inode *inode;
128 nid_t ino = 0;
129 int err;
131 f2fs_balance_fs(sbi);
133 inode = f2fs_new_inode(dir, mode);
134 if (IS_ERR(inode))
135 return PTR_ERR(inode);
137 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
138 set_cold_files(sbi, inode, dentry->d_name.name);
140 inode->i_op = &f2fs_file_inode_operations;
141 inode->i_fop = &f2fs_file_operations;
142 inode->i_mapping->a_ops = &f2fs_dblock_aops;
143 ino = inode->i_ino;
145 f2fs_lock_op(sbi);
146 err = f2fs_add_link(dentry, inode);
147 if (err)
148 goto out;
149 f2fs_unlock_op(sbi);
151 alloc_nid_done(sbi, ino);
153 d_instantiate(dentry, inode);
154 unlock_new_inode(inode);
156 if (IS_DIRSYNC(dir))
157 f2fs_sync_fs(sbi->sb, 1);
158 return 0;
159 out:
160 handle_failed_inode(inode);
161 return err;
164 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
165 struct dentry *dentry)
167 struct inode *inode = d_inode(old_dentry);
168 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
169 int err;
171 if (f2fs_encrypted_inode(dir) &&
172 !f2fs_is_child_context_consistent_with_parent(dir, inode))
173 return -EPERM;
175 f2fs_balance_fs(sbi);
177 inode->i_ctime = CURRENT_TIME;
178 ihold(inode);
180 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
181 f2fs_lock_op(sbi);
182 err = f2fs_add_link(dentry, inode);
183 if (err)
184 goto out;
185 f2fs_unlock_op(sbi);
187 d_instantiate(dentry, inode);
189 if (IS_DIRSYNC(dir))
190 f2fs_sync_fs(sbi->sb, 1);
191 return 0;
192 out:
193 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
194 iput(inode);
195 f2fs_unlock_op(sbi);
196 return err;
199 struct dentry *f2fs_get_parent(struct dentry *child)
201 struct qstr dotdot = QSTR_INIT("..", 2);
202 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
203 if (!ino)
204 return ERR_PTR(-ENOENT);
205 return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino));
208 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
210 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
211 struct qstr dot = QSTR_INIT(".", 1);
212 struct qstr dotdot = QSTR_INIT("..", 2);
213 struct f2fs_dir_entry *de;
214 struct page *page;
215 int err = 0;
217 f2fs_lock_op(sbi);
219 de = f2fs_find_entry(dir, &dot, &page);
220 if (de) {
221 f2fs_dentry_kunmap(dir, page);
222 f2fs_put_page(page, 0);
223 } else {
224 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
225 if (err)
226 goto out;
229 de = f2fs_find_entry(dir, &dotdot, &page);
230 if (de) {
231 f2fs_dentry_kunmap(dir, page);
232 f2fs_put_page(page, 0);
233 } else {
234 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
236 out:
237 if (!err) {
238 clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
239 mark_inode_dirty(dir);
242 f2fs_unlock_op(sbi);
243 return err;
246 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
247 unsigned int flags)
249 struct inode *inode = NULL;
250 struct f2fs_dir_entry *de;
251 struct page *page;
252 nid_t ino;
253 int err = 0;
255 if (dentry->d_name.len > F2FS_NAME_LEN)
256 return ERR_PTR(-ENAMETOOLONG);
258 de = f2fs_find_entry(dir, &dentry->d_name, &page);
259 if (!de)
260 return d_splice_alias(inode, dentry);
262 ino = le32_to_cpu(de->ino);
263 f2fs_dentry_kunmap(dir, page);
264 f2fs_put_page(page, 0);
266 inode = f2fs_iget(dir->i_sb, ino);
267 if (IS_ERR(inode))
268 return ERR_CAST(inode);
270 if (f2fs_has_inline_dots(inode)) {
271 err = __recover_dot_dentries(inode, dir->i_ino);
272 if (err)
273 goto err_out;
275 return d_splice_alias(inode, dentry);
277 err_out:
278 iget_failed(inode);
279 return ERR_PTR(err);
282 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
284 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
285 struct inode *inode = d_inode(dentry);
286 struct f2fs_dir_entry *de;
287 struct page *page;
288 int err = -ENOENT;
290 trace_f2fs_unlink_enter(dir, dentry);
291 f2fs_balance_fs(sbi);
293 de = f2fs_find_entry(dir, &dentry->d_name, &page);
294 if (!de)
295 goto fail;
297 f2fs_lock_op(sbi);
298 err = acquire_orphan_inode(sbi);
299 if (err) {
300 f2fs_unlock_op(sbi);
301 f2fs_dentry_kunmap(dir, page);
302 f2fs_put_page(page, 0);
303 goto fail;
305 f2fs_delete_entry(de, page, dir, inode);
306 f2fs_unlock_op(sbi);
308 /* In order to evict this inode, we set it dirty */
309 mark_inode_dirty(inode);
311 if (IS_DIRSYNC(dir))
312 f2fs_sync_fs(sbi->sb, 1);
313 fail:
314 trace_f2fs_unlink_exit(inode, err);
315 return err;
318 static const char *f2fs_get_link(struct dentry *dentry,
319 struct inode *inode,
320 struct delayed_call *done)
322 const char *link = page_get_link(dentry, inode, done);
323 if (!IS_ERR(link) && !*link) {
324 /* this is broken symlink case */
325 do_delayed_call(done);
326 clear_delayed_call(done);
327 link = ERR_PTR(-ENOENT);
329 return link;
332 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
333 const char *symname)
335 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
336 struct inode *inode;
337 size_t len = strlen(symname);
338 size_t p_len;
339 char *p_str;
340 struct f2fs_str disk_link = FSTR_INIT(NULL, 0);
341 struct f2fs_encrypted_symlink_data *sd = NULL;
342 int err;
344 if (len > dir->i_sb->s_blocksize)
345 return -ENAMETOOLONG;
347 f2fs_balance_fs(sbi);
349 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
350 if (IS_ERR(inode))
351 return PTR_ERR(inode);
353 if (f2fs_encrypted_inode(inode))
354 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
355 else
356 inode->i_op = &f2fs_symlink_inode_operations;
357 inode_nohighmem(inode);
358 inode->i_mapping->a_ops = &f2fs_dblock_aops;
360 f2fs_lock_op(sbi);
361 err = f2fs_add_link(dentry, inode);
362 if (err)
363 goto out;
364 f2fs_unlock_op(sbi);
365 alloc_nid_done(sbi, inode->i_ino);
367 if (f2fs_encrypted_inode(dir)) {
368 struct qstr istr = QSTR_INIT(symname, len);
370 err = f2fs_get_encryption_info(inode);
371 if (err)
372 goto err_out;
374 err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link);
375 if (err)
376 goto err_out;
378 err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link);
379 if (err < 0)
380 goto err_out;
382 p_len = encrypted_symlink_data_len(disk_link.len) + 1;
384 if (p_len > dir->i_sb->s_blocksize) {
385 err = -ENAMETOOLONG;
386 goto err_out;
389 sd = kzalloc(p_len, GFP_NOFS);
390 if (!sd) {
391 err = -ENOMEM;
392 goto err_out;
394 memcpy(sd->encrypted_path, disk_link.name, disk_link.len);
395 sd->len = cpu_to_le16(disk_link.len);
396 p_str = (char *)sd;
397 } else {
398 p_len = len + 1;
399 p_str = (char *)symname;
402 err = page_symlink(inode, p_str, p_len);
404 err_out:
405 d_instantiate(dentry, inode);
406 unlock_new_inode(inode);
409 * Let's flush symlink data in order to avoid broken symlink as much as
410 * possible. Nevertheless, fsyncing is the best way, but there is no
411 * way to get a file descriptor in order to flush that.
413 * Note that, it needs to do dir->fsync to make this recoverable.
414 * If the symlink path is stored into inline_data, there is no
415 * performance regression.
417 if (!err) {
418 filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1);
420 if (IS_DIRSYNC(dir))
421 f2fs_sync_fs(sbi->sb, 1);
422 } else {
423 f2fs_unlink(dir, dentry);
426 kfree(sd);
427 f2fs_fname_crypto_free_buffer(&disk_link);
428 return err;
429 out:
430 handle_failed_inode(inode);
431 return err;
434 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
436 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
437 struct inode *inode;
438 int err;
440 f2fs_balance_fs(sbi);
442 inode = f2fs_new_inode(dir, S_IFDIR | mode);
443 if (IS_ERR(inode))
444 return PTR_ERR(inode);
446 inode->i_op = &f2fs_dir_inode_operations;
447 inode->i_fop = &f2fs_dir_operations;
448 inode->i_mapping->a_ops = &f2fs_dblock_aops;
449 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
451 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
452 f2fs_lock_op(sbi);
453 err = f2fs_add_link(dentry, inode);
454 if (err)
455 goto out_fail;
456 f2fs_unlock_op(sbi);
458 alloc_nid_done(sbi, inode->i_ino);
460 d_instantiate(dentry, inode);
461 unlock_new_inode(inode);
463 if (IS_DIRSYNC(dir))
464 f2fs_sync_fs(sbi->sb, 1);
465 return 0;
467 out_fail:
468 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
469 handle_failed_inode(inode);
470 return err;
473 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
475 struct inode *inode = d_inode(dentry);
476 if (f2fs_empty_dir(inode))
477 return f2fs_unlink(dir, dentry);
478 return -ENOTEMPTY;
481 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
482 umode_t mode, dev_t rdev)
484 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
485 struct inode *inode;
486 int err = 0;
488 f2fs_balance_fs(sbi);
490 inode = f2fs_new_inode(dir, mode);
491 if (IS_ERR(inode))
492 return PTR_ERR(inode);
494 init_special_inode(inode, inode->i_mode, rdev);
495 inode->i_op = &f2fs_special_inode_operations;
497 f2fs_lock_op(sbi);
498 err = f2fs_add_link(dentry, inode);
499 if (err)
500 goto out;
501 f2fs_unlock_op(sbi);
503 alloc_nid_done(sbi, inode->i_ino);
505 d_instantiate(dentry, inode);
506 unlock_new_inode(inode);
508 if (IS_DIRSYNC(dir))
509 f2fs_sync_fs(sbi->sb, 1);
510 return 0;
511 out:
512 handle_failed_inode(inode);
513 return err;
516 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
517 umode_t mode, struct inode **whiteout)
519 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
520 struct inode *inode;
521 int err;
523 if (!whiteout)
524 f2fs_balance_fs(sbi);
526 inode = f2fs_new_inode(dir, mode);
527 if (IS_ERR(inode))
528 return PTR_ERR(inode);
530 if (whiteout) {
531 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
532 inode->i_op = &f2fs_special_inode_operations;
533 } else {
534 inode->i_op = &f2fs_file_inode_operations;
535 inode->i_fop = &f2fs_file_operations;
536 inode->i_mapping->a_ops = &f2fs_dblock_aops;
539 f2fs_lock_op(sbi);
540 err = acquire_orphan_inode(sbi);
541 if (err)
542 goto out;
544 err = f2fs_do_tmpfile(inode, dir);
545 if (err)
546 goto release_out;
549 * add this non-linked tmpfile to orphan list, in this way we could
550 * remove all unused data of tmpfile after abnormal power-off.
552 add_orphan_inode(sbi, inode->i_ino);
553 f2fs_unlock_op(sbi);
555 alloc_nid_done(sbi, inode->i_ino);
557 if (whiteout) {
558 inode_dec_link_count(inode);
559 *whiteout = inode;
560 } else {
561 d_tmpfile(dentry, inode);
563 unlock_new_inode(inode);
564 return 0;
566 release_out:
567 release_orphan_inode(sbi);
568 out:
569 handle_failed_inode(inode);
570 return err;
573 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
575 if (f2fs_encrypted_inode(dir)) {
576 int err = f2fs_get_encryption_info(dir);
577 if (err)
578 return err;
581 return __f2fs_tmpfile(dir, dentry, mode, NULL);
584 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
586 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
589 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
590 struct inode *new_dir, struct dentry *new_dentry,
591 unsigned int flags)
593 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
594 struct inode *old_inode = d_inode(old_dentry);
595 struct inode *new_inode = d_inode(new_dentry);
596 struct inode *whiteout = NULL;
597 struct page *old_dir_page;
598 struct page *old_page, *new_page = NULL;
599 struct f2fs_dir_entry *old_dir_entry = NULL;
600 struct f2fs_dir_entry *old_entry;
601 struct f2fs_dir_entry *new_entry;
602 int err = -ENOENT;
604 if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
605 !f2fs_is_child_context_consistent_with_parent(new_dir,
606 old_inode)) {
607 err = -EPERM;
608 goto out;
611 f2fs_balance_fs(sbi);
613 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
614 if (!old_entry)
615 goto out;
617 if (S_ISDIR(old_inode->i_mode)) {
618 err = -EIO;
619 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
620 if (!old_dir_entry)
621 goto out_old;
624 if (flags & RENAME_WHITEOUT) {
625 err = f2fs_create_whiteout(old_dir, &whiteout);
626 if (err)
627 goto out_dir;
630 if (new_inode) {
632 err = -ENOTEMPTY;
633 if (old_dir_entry && !f2fs_empty_dir(new_inode))
634 goto out_whiteout;
636 err = -ENOENT;
637 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
638 &new_page);
639 if (!new_entry)
640 goto out_whiteout;
642 f2fs_lock_op(sbi);
644 err = acquire_orphan_inode(sbi);
645 if (err)
646 goto put_out_dir;
648 if (update_dent_inode(old_inode, new_inode,
649 &new_dentry->d_name)) {
650 release_orphan_inode(sbi);
651 goto put_out_dir;
654 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
656 new_inode->i_ctime = CURRENT_TIME;
657 down_write(&F2FS_I(new_inode)->i_sem);
658 if (old_dir_entry)
659 drop_nlink(new_inode);
660 drop_nlink(new_inode);
661 up_write(&F2FS_I(new_inode)->i_sem);
663 mark_inode_dirty(new_inode);
665 if (!new_inode->i_nlink)
666 add_orphan_inode(sbi, new_inode->i_ino);
667 else
668 release_orphan_inode(sbi);
670 update_inode_page(old_inode);
671 update_inode_page(new_inode);
672 } else {
673 f2fs_lock_op(sbi);
675 err = f2fs_add_link(new_dentry, old_inode);
676 if (err) {
677 f2fs_unlock_op(sbi);
678 goto out_whiteout;
681 if (old_dir_entry) {
682 inc_nlink(new_dir);
683 update_inode_page(new_dir);
687 down_write(&F2FS_I(old_inode)->i_sem);
688 file_lost_pino(old_inode);
689 if (new_inode && file_enc_name(new_inode))
690 file_set_enc_name(old_inode);
691 up_write(&F2FS_I(old_inode)->i_sem);
693 old_inode->i_ctime = CURRENT_TIME;
694 mark_inode_dirty(old_inode);
696 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
698 if (whiteout) {
699 whiteout->i_state |= I_LINKABLE;
700 set_inode_flag(F2FS_I(whiteout), FI_INC_LINK);
701 err = f2fs_add_link(old_dentry, whiteout);
702 if (err)
703 goto put_out_dir;
704 whiteout->i_state &= ~I_LINKABLE;
705 iput(whiteout);
708 if (old_dir_entry) {
709 if (old_dir != new_dir && !whiteout) {
710 f2fs_set_link(old_inode, old_dir_entry,
711 old_dir_page, new_dir);
712 update_inode_page(old_inode);
713 } else {
714 f2fs_dentry_kunmap(old_inode, old_dir_page);
715 f2fs_put_page(old_dir_page, 0);
717 drop_nlink(old_dir);
718 mark_inode_dirty(old_dir);
719 update_inode_page(old_dir);
722 f2fs_unlock_op(sbi);
724 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
725 f2fs_sync_fs(sbi->sb, 1);
726 return 0;
728 put_out_dir:
729 f2fs_unlock_op(sbi);
730 if (new_page) {
731 f2fs_dentry_kunmap(new_dir, new_page);
732 f2fs_put_page(new_page, 0);
734 out_whiteout:
735 if (whiteout)
736 iput(whiteout);
737 out_dir:
738 if (old_dir_entry) {
739 f2fs_dentry_kunmap(old_inode, old_dir_page);
740 f2fs_put_page(old_dir_page, 0);
742 out_old:
743 f2fs_dentry_kunmap(old_dir, old_page);
744 f2fs_put_page(old_page, 0);
745 out:
746 return err;
749 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
750 struct inode *new_dir, struct dentry *new_dentry)
752 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
753 struct inode *old_inode = d_inode(old_dentry);
754 struct inode *new_inode = d_inode(new_dentry);
755 struct page *old_dir_page, *new_dir_page;
756 struct page *old_page, *new_page;
757 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
758 struct f2fs_dir_entry *old_entry, *new_entry;
759 int old_nlink = 0, new_nlink = 0;
760 int err = -ENOENT;
762 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
763 (old_dir != new_dir) &&
764 (!f2fs_is_child_context_consistent_with_parent(new_dir,
765 old_inode) ||
766 !f2fs_is_child_context_consistent_with_parent(old_dir,
767 new_inode)))
768 return -EPERM;
770 f2fs_balance_fs(sbi);
772 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
773 if (!old_entry)
774 goto out;
776 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
777 if (!new_entry)
778 goto out_old;
780 /* prepare for updating ".." directory entry info later */
781 if (old_dir != new_dir) {
782 if (S_ISDIR(old_inode->i_mode)) {
783 err = -EIO;
784 old_dir_entry = f2fs_parent_dir(old_inode,
785 &old_dir_page);
786 if (!old_dir_entry)
787 goto out_new;
790 if (S_ISDIR(new_inode->i_mode)) {
791 err = -EIO;
792 new_dir_entry = f2fs_parent_dir(new_inode,
793 &new_dir_page);
794 if (!new_dir_entry)
795 goto out_old_dir;
800 * If cross rename between file and directory those are not
801 * in the same directory, we will inc nlink of file's parent
802 * later, so we should check upper boundary of its nlink.
804 if ((!old_dir_entry || !new_dir_entry) &&
805 old_dir_entry != new_dir_entry) {
806 old_nlink = old_dir_entry ? -1 : 1;
807 new_nlink = -old_nlink;
808 err = -EMLINK;
809 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
810 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
811 goto out_new_dir;
814 f2fs_lock_op(sbi);
816 err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
817 if (err)
818 goto out_unlock;
819 if (file_enc_name(new_inode))
820 file_set_enc_name(old_inode);
822 err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
823 if (err)
824 goto out_undo;
825 if (file_enc_name(old_inode))
826 file_set_enc_name(new_inode);
828 /* update ".." directory entry info of old dentry */
829 if (old_dir_entry)
830 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
832 /* update ".." directory entry info of new dentry */
833 if (new_dir_entry)
834 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
836 /* update directory entry info of old dir inode */
837 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
839 down_write(&F2FS_I(old_inode)->i_sem);
840 file_lost_pino(old_inode);
841 up_write(&F2FS_I(old_inode)->i_sem);
843 update_inode_page(old_inode);
845 old_dir->i_ctime = CURRENT_TIME;
846 if (old_nlink) {
847 down_write(&F2FS_I(old_dir)->i_sem);
848 if (old_nlink < 0)
849 drop_nlink(old_dir);
850 else
851 inc_nlink(old_dir);
852 up_write(&F2FS_I(old_dir)->i_sem);
854 mark_inode_dirty(old_dir);
855 update_inode_page(old_dir);
857 /* update directory entry info of new dir inode */
858 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
860 down_write(&F2FS_I(new_inode)->i_sem);
861 file_lost_pino(new_inode);
862 up_write(&F2FS_I(new_inode)->i_sem);
864 update_inode_page(new_inode);
866 new_dir->i_ctime = CURRENT_TIME;
867 if (new_nlink) {
868 down_write(&F2FS_I(new_dir)->i_sem);
869 if (new_nlink < 0)
870 drop_nlink(new_dir);
871 else
872 inc_nlink(new_dir);
873 up_write(&F2FS_I(new_dir)->i_sem);
875 mark_inode_dirty(new_dir);
876 update_inode_page(new_dir);
878 f2fs_unlock_op(sbi);
880 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
881 f2fs_sync_fs(sbi->sb, 1);
882 return 0;
883 out_undo:
885 * Still we may fail to recover name info of f2fs_inode here
886 * Drop it, once its name is set as encrypted
888 update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
889 out_unlock:
890 f2fs_unlock_op(sbi);
891 out_new_dir:
892 if (new_dir_entry) {
893 f2fs_dentry_kunmap(new_inode, new_dir_page);
894 f2fs_put_page(new_dir_page, 0);
896 out_old_dir:
897 if (old_dir_entry) {
898 f2fs_dentry_kunmap(old_inode, old_dir_page);
899 f2fs_put_page(old_dir_page, 0);
901 out_new:
902 f2fs_dentry_kunmap(new_dir, new_page);
903 f2fs_put_page(new_page, 0);
904 out_old:
905 f2fs_dentry_kunmap(old_dir, old_page);
906 f2fs_put_page(old_page, 0);
907 out:
908 return err;
911 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
912 struct inode *new_dir, struct dentry *new_dentry,
913 unsigned int flags)
915 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
916 return -EINVAL;
918 if (flags & RENAME_EXCHANGE) {
919 return f2fs_cross_rename(old_dir, old_dentry,
920 new_dir, new_dentry);
923 * VFS has already handled the new dentry existence case,
924 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
926 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
929 #ifdef CONFIG_F2FS_FS_ENCRYPTION
930 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
931 struct inode *inode,
932 struct delayed_call *done)
934 struct page *cpage = NULL;
935 char *caddr, *paddr = NULL;
936 struct f2fs_str cstr;
937 struct f2fs_str pstr = FSTR_INIT(NULL, 0);
938 struct f2fs_encrypted_symlink_data *sd;
939 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
940 u32 max_size = inode->i_sb->s_blocksize;
941 int res;
943 if (!dentry)
944 return ERR_PTR(-ECHILD);
946 res = f2fs_get_encryption_info(inode);
947 if (res)
948 return ERR_PTR(res);
950 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
951 if (IS_ERR(cpage))
952 return ERR_CAST(cpage);
953 caddr = page_address(cpage);
954 caddr[size] = 0;
956 /* Symlink is encrypted */
957 sd = (struct f2fs_encrypted_symlink_data *)caddr;
958 cstr.len = le16_to_cpu(sd->len);
959 cstr.name = kmalloc(cstr.len, GFP_NOFS);
960 if (!cstr.name) {
961 res = -ENOMEM;
962 goto errout;
964 memcpy(cstr.name, sd->encrypted_path, cstr.len);
966 /* this is broken symlink case */
967 if (cstr.name[0] == 0 && cstr.len == 0) {
968 res = -ENOENT;
969 goto errout;
972 if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) >
973 max_size) {
974 /* Symlink data on the disk is corrupted */
975 res = -EIO;
976 goto errout;
978 res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr);
979 if (res)
980 goto errout;
982 res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
983 if (res < 0)
984 goto errout;
986 kfree(cstr.name);
988 paddr = pstr.name;
990 /* Null-terminate the name */
991 paddr[res] = '\0';
993 page_cache_release(cpage);
994 set_delayed_call(done, kfree_link, paddr);
995 return paddr;
996 errout:
997 kfree(cstr.name);
998 f2fs_fname_crypto_free_buffer(&pstr);
999 page_cache_release(cpage);
1000 return ERR_PTR(res);
1003 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1004 .readlink = generic_readlink,
1005 .get_link = f2fs_encrypted_get_link,
1006 .getattr = f2fs_getattr,
1007 .setattr = f2fs_setattr,
1008 .setxattr = generic_setxattr,
1009 .getxattr = generic_getxattr,
1010 .listxattr = f2fs_listxattr,
1011 .removexattr = generic_removexattr,
1013 #endif
1015 const struct inode_operations f2fs_dir_inode_operations = {
1016 .create = f2fs_create,
1017 .lookup = f2fs_lookup,
1018 .link = f2fs_link,
1019 .unlink = f2fs_unlink,
1020 .symlink = f2fs_symlink,
1021 .mkdir = f2fs_mkdir,
1022 .rmdir = f2fs_rmdir,
1023 .mknod = f2fs_mknod,
1024 .rename2 = f2fs_rename2,
1025 .tmpfile = f2fs_tmpfile,
1026 .getattr = f2fs_getattr,
1027 .setattr = f2fs_setattr,
1028 .get_acl = f2fs_get_acl,
1029 .set_acl = f2fs_set_acl,
1030 #ifdef CONFIG_F2FS_FS_XATTR
1031 .setxattr = generic_setxattr,
1032 .getxattr = generic_getxattr,
1033 .listxattr = f2fs_listxattr,
1034 .removexattr = generic_removexattr,
1035 #endif
1038 const struct inode_operations f2fs_symlink_inode_operations = {
1039 .readlink = generic_readlink,
1040 .get_link = f2fs_get_link,
1041 .getattr = f2fs_getattr,
1042 .setattr = f2fs_setattr,
1043 #ifdef CONFIG_F2FS_FS_XATTR
1044 .setxattr = generic_setxattr,
1045 .getxattr = generic_getxattr,
1046 .listxattr = f2fs_listxattr,
1047 .removexattr = generic_removexattr,
1048 #endif
1051 const struct inode_operations f2fs_special_inode_operations = {
1052 .getattr = f2fs_getattr,
1053 .setattr = f2fs_setattr,
1054 .get_acl = f2fs_get_acl,
1055 .set_acl = f2fs_set_acl,
1056 #ifdef CONFIG_F2FS_FS_XATTR
1057 .setxattr = generic_setxattr,
1058 .getxattr = generic_getxattr,
1059 .listxattr = f2fs_listxattr,
1060 .removexattr = generic_removexattr,
1061 #endif