HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / f2fs / namei.c
blobe5553cd8fe4edd7f40a2d622b7cb6b891d926ae3
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_new(dentry, inode);
155 if (IS_DIRSYNC(dir))
156 f2fs_sync_fs(sbi->sb, 1);
157 return 0;
158 out:
159 handle_failed_inode(inode);
160 return err;
163 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
164 struct dentry *dentry)
166 struct inode *inode = d_inode(old_dentry);
167 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
168 int err;
170 if (f2fs_encrypted_inode(dir) &&
171 !f2fs_is_child_context_consistent_with_parent(dir, inode))
172 return -EPERM;
174 f2fs_balance_fs(sbi);
176 inode->i_ctime = CURRENT_TIME;
177 ihold(inode);
179 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
180 f2fs_lock_op(sbi);
181 err = f2fs_add_link(dentry, inode);
182 if (err)
183 goto out;
184 f2fs_unlock_op(sbi);
186 d_instantiate(dentry, inode);
188 if (IS_DIRSYNC(dir))
189 f2fs_sync_fs(sbi->sb, 1);
190 return 0;
191 out:
192 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
193 iput(inode);
194 f2fs_unlock_op(sbi);
195 return err;
198 struct dentry *f2fs_get_parent(struct dentry *child)
200 struct qstr dotdot = QSTR_INIT("..", 2);
201 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
202 if (!ino)
203 return ERR_PTR(-ENOENT);
204 return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino));
207 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
209 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
210 struct qstr dot = QSTR_INIT(".", 1);
211 struct qstr dotdot = QSTR_INIT("..", 2);
212 struct f2fs_dir_entry *de;
213 struct page *page;
214 int err = 0;
216 f2fs_lock_op(sbi);
218 de = f2fs_find_entry(dir, &dot, &page);
219 if (de) {
220 f2fs_dentry_kunmap(dir, page);
221 f2fs_put_page(page, 0);
222 } else {
223 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
224 if (err)
225 goto out;
228 de = f2fs_find_entry(dir, &dotdot, &page);
229 if (de) {
230 f2fs_dentry_kunmap(dir, page);
231 f2fs_put_page(page, 0);
232 } else {
233 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
235 out:
236 if (!err) {
237 clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
238 mark_inode_dirty(dir);
241 f2fs_unlock_op(sbi);
242 return err;
245 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
246 unsigned int flags)
248 struct inode *inode = NULL;
249 struct f2fs_dir_entry *de;
250 struct page *page;
251 nid_t ino;
252 int err = 0;
254 if (dentry->d_name.len > F2FS_NAME_LEN)
255 return ERR_PTR(-ENAMETOOLONG);
257 de = f2fs_find_entry(dir, &dentry->d_name, &page);
258 if (!de)
259 return d_splice_alias(inode, dentry);
261 ino = le32_to_cpu(de->ino);
262 f2fs_dentry_kunmap(dir, page);
263 f2fs_put_page(page, 0);
265 inode = f2fs_iget(dir->i_sb, ino);
266 if (IS_ERR(inode))
267 return ERR_CAST(inode);
269 if (f2fs_has_inline_dots(inode)) {
270 err = __recover_dot_dentries(inode, dir->i_ino);
271 if (err)
272 goto err_out;
274 return d_splice_alias(inode, dentry);
276 err_out:
277 iget_failed(inode);
278 return ERR_PTR(err);
281 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
283 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
284 struct inode *inode = d_inode(dentry);
285 struct f2fs_dir_entry *de;
286 struct page *page;
287 int err = -ENOENT;
289 trace_f2fs_unlink_enter(dir, dentry);
290 f2fs_balance_fs(sbi);
292 de = f2fs_find_entry(dir, &dentry->d_name, &page);
293 if (!de)
294 goto fail;
296 f2fs_lock_op(sbi);
297 err = acquire_orphan_inode(sbi);
298 if (err) {
299 f2fs_unlock_op(sbi);
300 f2fs_dentry_kunmap(dir, page);
301 f2fs_put_page(page, 0);
302 goto fail;
304 f2fs_delete_entry(de, page, dir, inode);
305 f2fs_unlock_op(sbi);
307 /* In order to evict this inode, we set it dirty */
308 mark_inode_dirty(inode);
310 if (IS_DIRSYNC(dir))
311 f2fs_sync_fs(sbi->sb, 1);
312 fail:
313 trace_f2fs_unlink_exit(inode, err);
314 return err;
317 static const char *f2fs_follow_link(struct dentry *dentry, void **cookie)
319 const char *link = page_follow_link_light(dentry, cookie);
320 if (!IS_ERR(link) && !*link) {
321 /* this is broken symlink case */
322 page_put_link(NULL, *cookie);
323 link = ERR_PTR(-ENOENT);
325 return link;
328 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
329 const char *symname)
331 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
332 struct inode *inode;
333 size_t len = strlen(symname);
334 size_t p_len;
335 char *p_str;
336 struct f2fs_str disk_link = FSTR_INIT(NULL, 0);
337 struct f2fs_encrypted_symlink_data *sd = NULL;
338 int err;
340 if (len > dir->i_sb->s_blocksize)
341 return -ENAMETOOLONG;
343 f2fs_balance_fs(sbi);
345 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
346 if (IS_ERR(inode))
347 return PTR_ERR(inode);
349 if (f2fs_encrypted_inode(inode))
350 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
351 else
352 inode->i_op = &f2fs_symlink_inode_operations;
353 inode_nohighmem(inode);
354 inode->i_mapping->a_ops = &f2fs_dblock_aops;
356 f2fs_lock_op(sbi);
357 err = f2fs_add_link(dentry, inode);
358 if (err)
359 goto out;
360 f2fs_unlock_op(sbi);
361 alloc_nid_done(sbi, inode->i_ino);
363 if (f2fs_encrypted_inode(dir)) {
364 struct qstr istr = QSTR_INIT(symname, len);
366 err = f2fs_get_encryption_info(inode);
367 if (err)
368 goto err_out;
370 err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link);
371 if (err)
372 goto err_out;
374 err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link);
375 if (err < 0)
376 goto err_out;
378 p_len = encrypted_symlink_data_len(disk_link.len) + 1;
380 if (p_len > dir->i_sb->s_blocksize) {
381 err = -ENAMETOOLONG;
382 goto err_out;
385 sd = kzalloc(p_len, GFP_NOFS);
386 if (!sd) {
387 err = -ENOMEM;
388 goto err_out;
390 memcpy(sd->encrypted_path, disk_link.name, disk_link.len);
391 sd->len = cpu_to_le16(disk_link.len);
392 p_str = (char *)sd;
393 } else {
394 p_len = len + 1;
395 p_str = (char *)symname;
398 err = page_symlink(inode, p_str, p_len);
400 err_out:
401 d_instantiate_new(dentry, inode);
404 * Let's flush symlink data in order to avoid broken symlink as much as
405 * possible. Nevertheless, fsyncing is the best way, but there is no
406 * way to get a file descriptor in order to flush that.
408 * Note that, it needs to do dir->fsync to make this recoverable.
409 * If the symlink path is stored into inline_data, there is no
410 * performance regression.
412 if (!err) {
413 filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1);
415 if (IS_DIRSYNC(dir))
416 f2fs_sync_fs(sbi->sb, 1);
417 } else {
418 f2fs_unlink(dir, dentry);
421 kfree(sd);
422 f2fs_fname_crypto_free_buffer(&disk_link);
423 return err;
424 out:
425 handle_failed_inode(inode);
426 return err;
429 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
431 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
432 struct inode *inode;
433 int err;
435 f2fs_balance_fs(sbi);
437 inode = f2fs_new_inode(dir, S_IFDIR | mode);
438 if (IS_ERR(inode))
439 return PTR_ERR(inode);
441 inode->i_op = &f2fs_dir_inode_operations;
442 inode->i_fop = &f2fs_dir_operations;
443 inode->i_mapping->a_ops = &f2fs_dblock_aops;
444 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
446 set_inode_flag(F2FS_I(inode), FI_INC_LINK);
447 f2fs_lock_op(sbi);
448 err = f2fs_add_link(dentry, inode);
449 if (err)
450 goto out_fail;
451 f2fs_unlock_op(sbi);
453 alloc_nid_done(sbi, inode->i_ino);
455 d_instantiate_new(dentry, inode);
457 if (IS_DIRSYNC(dir))
458 f2fs_sync_fs(sbi->sb, 1);
459 return 0;
461 out_fail:
462 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
463 handle_failed_inode(inode);
464 return err;
467 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
469 struct inode *inode = d_inode(dentry);
470 if (f2fs_empty_dir(inode))
471 return f2fs_unlink(dir, dentry);
472 return -ENOTEMPTY;
475 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
476 umode_t mode, dev_t rdev)
478 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
479 struct inode *inode;
480 int err = 0;
482 f2fs_balance_fs(sbi);
484 inode = f2fs_new_inode(dir, mode);
485 if (IS_ERR(inode))
486 return PTR_ERR(inode);
488 init_special_inode(inode, inode->i_mode, rdev);
489 inode->i_op = &f2fs_special_inode_operations;
491 f2fs_lock_op(sbi);
492 err = f2fs_add_link(dentry, inode);
493 if (err)
494 goto out;
495 f2fs_unlock_op(sbi);
497 alloc_nid_done(sbi, inode->i_ino);
499 d_instantiate_new(dentry, inode);
501 if (IS_DIRSYNC(dir))
502 f2fs_sync_fs(sbi->sb, 1);
503 return 0;
504 out:
505 handle_failed_inode(inode);
506 return err;
509 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
510 umode_t mode, struct inode **whiteout)
512 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
513 struct inode *inode;
514 int err;
516 if (!whiteout)
517 f2fs_balance_fs(sbi);
519 inode = f2fs_new_inode(dir, mode);
520 if (IS_ERR(inode))
521 return PTR_ERR(inode);
523 if (whiteout) {
524 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
525 inode->i_op = &f2fs_special_inode_operations;
526 } else {
527 inode->i_op = &f2fs_file_inode_operations;
528 inode->i_fop = &f2fs_file_operations;
529 inode->i_mapping->a_ops = &f2fs_dblock_aops;
532 f2fs_lock_op(sbi);
533 err = acquire_orphan_inode(sbi);
534 if (err)
535 goto out;
537 err = f2fs_do_tmpfile(inode, dir);
538 if (err)
539 goto release_out;
542 * add this non-linked tmpfile to orphan list, in this way we could
543 * remove all unused data of tmpfile after abnormal power-off.
545 add_orphan_inode(sbi, inode->i_ino);
546 f2fs_unlock_op(sbi);
548 alloc_nid_done(sbi, inode->i_ino);
550 if (whiteout) {
551 inode_dec_link_count(inode);
552 *whiteout = inode;
553 } else {
554 d_tmpfile(dentry, inode);
556 unlock_new_inode(inode);
557 return 0;
559 release_out:
560 release_orphan_inode(sbi);
561 out:
562 handle_failed_inode(inode);
563 return err;
566 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
568 if (f2fs_encrypted_inode(dir)) {
569 int err = f2fs_get_encryption_info(dir);
570 if (err)
571 return err;
574 return __f2fs_tmpfile(dir, dentry, mode, NULL);
577 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
579 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
582 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
583 struct inode *new_dir, struct dentry *new_dentry,
584 unsigned int flags)
586 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
587 struct inode *old_inode = d_inode(old_dentry);
588 struct inode *new_inode = d_inode(new_dentry);
589 struct inode *whiteout = NULL;
590 struct page *old_dir_page;
591 struct page *old_page, *new_page = NULL;
592 struct f2fs_dir_entry *old_dir_entry = NULL;
593 struct f2fs_dir_entry *old_entry;
594 struct f2fs_dir_entry *new_entry;
595 int err = -ENOENT;
597 if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
598 !f2fs_is_child_context_consistent_with_parent(new_dir,
599 old_inode)) {
600 err = -EPERM;
601 goto out;
604 f2fs_balance_fs(sbi);
606 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
607 if (!old_entry)
608 goto out;
610 if (S_ISDIR(old_inode->i_mode)) {
611 err = -EIO;
612 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
613 if (!old_dir_entry)
614 goto out_old;
617 if (flags & RENAME_WHITEOUT) {
618 err = f2fs_create_whiteout(old_dir, &whiteout);
619 if (err)
620 goto out_dir;
623 if (new_inode) {
625 err = -ENOTEMPTY;
626 if (old_dir_entry && !f2fs_empty_dir(new_inode))
627 goto out_whiteout;
629 err = -ENOENT;
630 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
631 &new_page);
632 if (!new_entry)
633 goto out_whiteout;
635 f2fs_lock_op(sbi);
637 err = acquire_orphan_inode(sbi);
638 if (err)
639 goto put_out_dir;
641 if (update_dent_inode(old_inode, new_inode,
642 &new_dentry->d_name)) {
643 release_orphan_inode(sbi);
644 goto put_out_dir;
647 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
649 new_inode->i_ctime = CURRENT_TIME;
650 down_write(&F2FS_I(new_inode)->i_sem);
651 if (old_dir_entry)
652 drop_nlink(new_inode);
653 drop_nlink(new_inode);
654 up_write(&F2FS_I(new_inode)->i_sem);
656 mark_inode_dirty(new_inode);
658 if (!new_inode->i_nlink)
659 add_orphan_inode(sbi, new_inode->i_ino);
660 else
661 release_orphan_inode(sbi);
663 update_inode_page(old_inode);
664 update_inode_page(new_inode);
665 } else {
666 f2fs_lock_op(sbi);
668 err = f2fs_add_link(new_dentry, old_inode);
669 if (err) {
670 f2fs_unlock_op(sbi);
671 goto out_whiteout;
674 if (old_dir_entry) {
675 inc_nlink(new_dir);
676 update_inode_page(new_dir);
680 down_write(&F2FS_I(old_inode)->i_sem);
681 file_lost_pino(old_inode);
682 if (new_inode && file_enc_name(new_inode))
683 file_set_enc_name(old_inode);
684 up_write(&F2FS_I(old_inode)->i_sem);
686 old_inode->i_ctime = CURRENT_TIME;
687 mark_inode_dirty(old_inode);
689 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
691 if (whiteout) {
692 whiteout->i_state |= I_LINKABLE;
693 set_inode_flag(F2FS_I(whiteout), FI_INC_LINK);
694 err = f2fs_add_link(old_dentry, whiteout);
695 if (err)
696 goto put_out_dir;
697 whiteout->i_state &= ~I_LINKABLE;
698 iput(whiteout);
701 if (old_dir_entry) {
702 if (old_dir != new_dir && !whiteout) {
703 f2fs_set_link(old_inode, old_dir_entry,
704 old_dir_page, new_dir);
705 update_inode_page(old_inode);
706 } else {
707 f2fs_dentry_kunmap(old_inode, old_dir_page);
708 f2fs_put_page(old_dir_page, 0);
710 drop_nlink(old_dir);
711 mark_inode_dirty(old_dir);
712 update_inode_page(old_dir);
715 f2fs_unlock_op(sbi);
717 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
718 f2fs_sync_fs(sbi->sb, 1);
719 return 0;
721 put_out_dir:
722 f2fs_unlock_op(sbi);
723 if (new_page) {
724 f2fs_dentry_kunmap(new_dir, new_page);
725 f2fs_put_page(new_page, 0);
727 out_whiteout:
728 if (whiteout)
729 iput(whiteout);
730 out_dir:
731 if (old_dir_entry) {
732 f2fs_dentry_kunmap(old_inode, old_dir_page);
733 f2fs_put_page(old_dir_page, 0);
735 out_old:
736 f2fs_dentry_kunmap(old_dir, old_page);
737 f2fs_put_page(old_page, 0);
738 out:
739 return err;
742 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
743 struct inode *new_dir, struct dentry *new_dentry)
745 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
746 struct inode *old_inode = d_inode(old_dentry);
747 struct inode *new_inode = d_inode(new_dentry);
748 struct page *old_dir_page, *new_dir_page;
749 struct page *old_page, *new_page;
750 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
751 struct f2fs_dir_entry *old_entry, *new_entry;
752 int old_nlink = 0, new_nlink = 0;
753 int err = -ENOENT;
755 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
756 (old_dir != new_dir) &&
757 (!f2fs_is_child_context_consistent_with_parent(new_dir,
758 old_inode) ||
759 !f2fs_is_child_context_consistent_with_parent(old_dir,
760 new_inode)))
761 return -EPERM;
763 f2fs_balance_fs(sbi);
765 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
766 if (!old_entry)
767 goto out;
769 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
770 if (!new_entry)
771 goto out_old;
773 /* prepare for updating ".." directory entry info later */
774 if (old_dir != new_dir) {
775 if (S_ISDIR(old_inode->i_mode)) {
776 err = -EIO;
777 old_dir_entry = f2fs_parent_dir(old_inode,
778 &old_dir_page);
779 if (!old_dir_entry)
780 goto out_new;
783 if (S_ISDIR(new_inode->i_mode)) {
784 err = -EIO;
785 new_dir_entry = f2fs_parent_dir(new_inode,
786 &new_dir_page);
787 if (!new_dir_entry)
788 goto out_old_dir;
793 * If cross rename between file and directory those are not
794 * in the same directory, we will inc nlink of file's parent
795 * later, so we should check upper boundary of its nlink.
797 if ((!old_dir_entry || !new_dir_entry) &&
798 old_dir_entry != new_dir_entry) {
799 old_nlink = old_dir_entry ? -1 : 1;
800 new_nlink = -old_nlink;
801 err = -EMLINK;
802 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
803 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
804 goto out_new_dir;
807 f2fs_lock_op(sbi);
809 err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
810 if (err)
811 goto out_unlock;
812 if (file_enc_name(new_inode))
813 file_set_enc_name(old_inode);
815 err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
816 if (err)
817 goto out_undo;
818 if (file_enc_name(old_inode))
819 file_set_enc_name(new_inode);
821 /* update ".." directory entry info of old dentry */
822 if (old_dir_entry)
823 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
825 /* update ".." directory entry info of new dentry */
826 if (new_dir_entry)
827 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
829 /* update directory entry info of old dir inode */
830 f2fs_set_link(old_dir, old_entry, old_page, new_inode);
832 down_write(&F2FS_I(old_inode)->i_sem);
833 file_lost_pino(old_inode);
834 up_write(&F2FS_I(old_inode)->i_sem);
836 update_inode_page(old_inode);
838 old_dir->i_ctime = CURRENT_TIME;
839 if (old_nlink) {
840 down_write(&F2FS_I(old_dir)->i_sem);
841 if (old_nlink < 0)
842 drop_nlink(old_dir);
843 else
844 inc_nlink(old_dir);
845 up_write(&F2FS_I(old_dir)->i_sem);
847 mark_inode_dirty(old_dir);
848 update_inode_page(old_dir);
850 /* update directory entry info of new dir inode */
851 f2fs_set_link(new_dir, new_entry, new_page, old_inode);
853 down_write(&F2FS_I(new_inode)->i_sem);
854 file_lost_pino(new_inode);
855 up_write(&F2FS_I(new_inode)->i_sem);
857 update_inode_page(new_inode);
859 new_dir->i_ctime = CURRENT_TIME;
860 if (new_nlink) {
861 down_write(&F2FS_I(new_dir)->i_sem);
862 if (new_nlink < 0)
863 drop_nlink(new_dir);
864 else
865 inc_nlink(new_dir);
866 up_write(&F2FS_I(new_dir)->i_sem);
868 mark_inode_dirty(new_dir);
869 update_inode_page(new_dir);
871 f2fs_unlock_op(sbi);
873 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
874 f2fs_sync_fs(sbi->sb, 1);
875 return 0;
876 out_undo:
878 * Still we may fail to recover name info of f2fs_inode here
879 * Drop it, once its name is set as encrypted
881 update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
882 out_unlock:
883 f2fs_unlock_op(sbi);
884 out_new_dir:
885 if (new_dir_entry) {
886 f2fs_dentry_kunmap(new_inode, new_dir_page);
887 f2fs_put_page(new_dir_page, 0);
889 out_old_dir:
890 if (old_dir_entry) {
891 f2fs_dentry_kunmap(old_inode, old_dir_page);
892 f2fs_put_page(old_dir_page, 0);
894 out_new:
895 f2fs_dentry_kunmap(new_dir, new_page);
896 f2fs_put_page(new_page, 0);
897 out_old:
898 f2fs_dentry_kunmap(old_dir, old_page);
899 f2fs_put_page(old_page, 0);
900 out:
901 return err;
904 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
905 struct inode *new_dir, struct dentry *new_dentry,
906 unsigned int flags)
908 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
909 return -EINVAL;
911 if (flags & RENAME_EXCHANGE) {
912 return f2fs_cross_rename(old_dir, old_dentry,
913 new_dir, new_dentry);
916 * VFS has already handled the new dentry existence case,
917 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
919 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
922 #ifdef CONFIG_F2FS_FS_ENCRYPTION
923 static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie)
925 struct page *cpage = NULL;
926 char *caddr, *paddr = NULL;
927 struct f2fs_str cstr;
928 struct f2fs_str pstr = FSTR_INIT(NULL, 0);
929 struct inode *inode = d_inode(dentry);
930 struct f2fs_encrypted_symlink_data *sd;
931 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
932 u32 max_size = inode->i_sb->s_blocksize;
933 int res;
935 res = f2fs_get_encryption_info(inode);
936 if (res)
937 return ERR_PTR(res);
939 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
940 if (IS_ERR(cpage))
941 return ERR_CAST(cpage);
942 caddr = page_address(cpage);
943 caddr[size] = 0;
945 /* Symlink is encrypted */
946 sd = (struct f2fs_encrypted_symlink_data *)caddr;
947 cstr.len = le16_to_cpu(sd->len);
948 cstr.name = kmalloc(cstr.len, GFP_NOFS);
949 if (!cstr.name) {
950 res = -ENOMEM;
951 goto errout;
953 memcpy(cstr.name, sd->encrypted_path, cstr.len);
955 /* this is broken symlink case */
956 if (cstr.name[0] == 0 && cstr.len == 0) {
957 res = -ENOENT;
958 goto errout;
961 if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) >
962 max_size) {
963 /* Symlink data on the disk is corrupted */
964 res = -EIO;
965 goto errout;
967 res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr);
968 if (res)
969 goto errout;
971 res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
972 if (res < 0)
973 goto errout;
975 kfree(cstr.name);
977 paddr = pstr.name;
979 /* Null-terminate the name */
980 paddr[res] = '\0';
982 page_cache_release(cpage);
983 return *cookie = paddr;
984 errout:
985 kfree(cstr.name);
986 f2fs_fname_crypto_free_buffer(&pstr);
987 page_cache_release(cpage);
988 return ERR_PTR(res);
991 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
992 .readlink = generic_readlink,
993 .follow_link = f2fs_encrypted_follow_link,
994 .put_link = kfree_put_link,
995 .getattr = f2fs_getattr,
996 .setattr = f2fs_setattr,
997 .setxattr = generic_setxattr,
998 .getxattr = generic_getxattr,
999 .listxattr = f2fs_listxattr,
1000 .removexattr = generic_removexattr,
1002 #endif
1004 const struct inode_operations f2fs_dir_inode_operations = {
1005 .create = f2fs_create,
1006 .lookup = f2fs_lookup,
1007 .link = f2fs_link,
1008 .unlink = f2fs_unlink,
1009 .symlink = f2fs_symlink,
1010 .mkdir = f2fs_mkdir,
1011 .rmdir = f2fs_rmdir,
1012 .mknod = f2fs_mknod,
1013 .rename2 = f2fs_rename2,
1014 .tmpfile = f2fs_tmpfile,
1015 .getattr = f2fs_getattr,
1016 .setattr = f2fs_setattr,
1017 .get_acl = f2fs_get_acl,
1018 .set_acl = f2fs_set_acl,
1019 #ifdef CONFIG_F2FS_FS_XATTR
1020 .setxattr = generic_setxattr,
1021 .getxattr = generic_getxattr,
1022 .listxattr = f2fs_listxattr,
1023 .removexattr = generic_removexattr,
1024 #endif
1027 const struct inode_operations f2fs_symlink_inode_operations = {
1028 .readlink = generic_readlink,
1029 .follow_link = f2fs_follow_link,
1030 .put_link = page_put_link,
1031 .getattr = f2fs_getattr,
1032 .setattr = f2fs_setattr,
1033 #ifdef CONFIG_F2FS_FS_XATTR
1034 .setxattr = generic_setxattr,
1035 .getxattr = generic_getxattr,
1036 .listxattr = f2fs_listxattr,
1037 .removexattr = generic_removexattr,
1038 #endif
1041 const struct inode_operations f2fs_special_inode_operations = {
1042 .getattr = f2fs_getattr,
1043 .setattr = f2fs_setattr,
1044 .get_acl = f2fs_get_acl,
1045 .set_acl = f2fs_set_acl,
1046 #ifdef CONFIG_F2FS_FS_XATTR
1047 .setxattr = generic_setxattr,
1048 .getxattr = generic_getxattr,
1049 .listxattr = f2fs_listxattr,
1050 .removexattr = generic_removexattr,
1051 #endif