Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 2 | * fs/f2fs/namei.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 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. |
| 10 | */ |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/ctype.h> |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 16 | #include <linux/dcache.h> |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 17 | #include <linux/namei.h> |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 18 | |
| 19 | #include "f2fs.h" |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 20 | #include "node.h" |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 21 | #include "xattr.h" |
| 22 | #include "acl.h" |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 23 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 24 | |
| 25 | static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode) |
| 26 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 27 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 28 | nid_t ino; |
| 29 | struct inode *inode; |
| 30 | bool nid_free = false; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 31 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 32 | |
Jaegeuk Kim | a014e03 | 2014-06-20 21:44:02 -0700 | [diff] [blame] | 33 | inode = new_inode(dir->i_sb); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 34 | if (!inode) |
| 35 | return ERR_PTR(-ENOMEM); |
| 36 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 37 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 38 | if (!alloc_nid(sbi, &ino)) { |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 39 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 40 | err = -ENOSPC; |
| 41 | goto fail; |
| 42 | } |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 43 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 44 | |
Chao Yu | 70ff5df | 2014-05-08 17:09:30 +0800 | [diff] [blame] | 45 | inode_init_owner(inode, dir, mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 46 | |
| 47 | inode->i_ino = ino; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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++; |
| 51 | |
| 52 | err = insert_inode_locked(inode); |
| 53 | if (err) { |
| 54 | err = -EINVAL; |
| 55 | nid_free = true; |
| 56 | goto out; |
| 57 | } |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 58 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 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); |
| 62 | |
Jaegeuk Kim | 01b960e | 2015-04-23 10:27:21 -0700 | [diff] [blame] | 63 | if (f2fs_may_inline_data(inode)) |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 64 | set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); |
Jaegeuk Kim | 01b960e | 2015-04-23 10:27:21 -0700 | [diff] [blame] | 65 | if (f2fs_may_inline_dentry(inode)) |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 66 | set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY); |
| 67 | |
Jaegeuk Kim | 2fb2c95 | 2015-04-30 18:58:22 -0700 | [diff] [blame] | 68 | stat_inc_inline_inode(inode); |
| 69 | stat_inc_inline_dir(inode); |
| 70 | |
Jaegeuk Kim | d70b4f5 | 2013-04-25 13:24:33 +0900 | [diff] [blame] | 71 | trace_f2fs_new_inode(inode, 0); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 72 | mark_inode_dirty(inode); |
| 73 | return inode; |
| 74 | |
| 75 | out: |
| 76 | clear_nlink(inode); |
| 77 | unlock_new_inode(inode); |
| 78 | fail: |
Jaegeuk Kim | d70b4f5 | 2013-04-25 13:24:33 +0900 | [diff] [blame] | 79 | trace_f2fs_new_inode(inode, err); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 80 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 81 | iput(inode); |
| 82 | if (nid_free) |
| 83 | alloc_nid_failed(sbi, ino); |
| 84 | return ERR_PTR(err); |
| 85 | } |
| 86 | |
| 87 | static int is_multimedia_file(const unsigned char *s, const char *sub) |
| 88 | { |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 89 | size_t slen = strlen(s); |
| 90 | size_t sublen = strlen(sub); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 91 | |
| 92 | if (sublen > slen) |
Namjae Jeon | e66509f0 | 2013-04-20 01:27:21 +0900 | [diff] [blame] | 93 | return 0; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 94 | |
Gu Zheng | 749ebfd | 2013-08-22 18:11:25 +0800 | [diff] [blame] | 95 | return !strncasecmp(s + slen - sublen, sub, sublen); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 98 | /* |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 99 | * Set multimedia files as cold files for hot/cold data separation |
| 100 | */ |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 101 | static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 102 | const unsigned char *name) |
| 103 | { |
| 104 | int i; |
| 105 | __u8 (*extlist)[8] = sbi->raw_super->extension_list; |
| 106 | |
| 107 | int count = le32_to_cpu(sbi->raw_super->extension_count); |
| 108 | for (i = 0; i < count; i++) { |
Namjae Jeon | e66509f0 | 2013-04-20 01:27:21 +0900 | [diff] [blame] | 109 | if (is_multimedia_file(name, extlist[i])) { |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 110 | file_set_cold(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
| 117 | bool excl) |
| 118 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 119 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 120 | struct inode *inode; |
| 121 | nid_t ino = 0; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 122 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 123 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 124 | f2fs_balance_fs(sbi); |
| 125 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 126 | inode = f2fs_new_inode(dir, mode); |
| 127 | if (IS_ERR(inode)) |
| 128 | return PTR_ERR(inode); |
| 129 | |
| 130 | if (!test_opt(sbi, DISABLE_EXT_IDENTIFY)) |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 131 | set_cold_files(sbi, inode, dentry->d_name.name); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 132 | |
| 133 | inode->i_op = &f2fs_file_inode_operations; |
| 134 | inode->i_fop = &f2fs_file_operations; |
| 135 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 136 | ino = inode->i_ino; |
| 137 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 138 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 139 | err = f2fs_add_link(dentry, inode); |
| 140 | if (err) |
| 141 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 142 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 143 | |
| 144 | alloc_nid_done(sbi, ino); |
| 145 | |
Jaegeuk Kim | 0a364af | 2013-05-16 08:57:43 +0900 | [diff] [blame] | 146 | d_instantiate(dentry, inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 147 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 148 | |
| 149 | if (IS_DIRSYNC(dir)) |
| 150 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 151 | return 0; |
| 152 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 153 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 154 | return err; |
| 155 | } |
| 156 | |
| 157 | static int f2fs_link(struct dentry *old_dentry, struct inode *dir, |
| 158 | struct dentry *dentry) |
| 159 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 160 | struct inode *inode = d_inode(old_dentry); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 161 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 162 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 163 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 164 | if (f2fs_encrypted_inode(dir) && |
| 165 | !f2fs_is_child_context_consistent_with_parent(dir, inode)) |
| 166 | return -EPERM; |
| 167 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 168 | f2fs_balance_fs(sbi); |
| 169 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 170 | inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 6f6fd83 | 2013-05-22 12:06:26 +0900 | [diff] [blame] | 171 | ihold(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 172 | |
| 173 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 174 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 175 | err = f2fs_add_link(dentry, inode); |
| 176 | if (err) |
| 177 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 178 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 179 | |
| 180 | d_instantiate(dentry, inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 181 | |
| 182 | if (IS_DIRSYNC(dir)) |
| 183 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 184 | return 0; |
| 185 | out: |
| 186 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
| 187 | iput(inode); |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 188 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 189 | return err; |
| 190 | } |
| 191 | |
| 192 | struct dentry *f2fs_get_parent(struct dentry *child) |
| 193 | { |
| 194 | struct qstr dotdot = QSTR_INIT("..", 2); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 195 | unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 196 | if (!ino) |
| 197 | return ERR_PTR(-ENOENT); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 198 | return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino)); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 199 | } |
| 200 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 201 | static int __recover_dot_dentries(struct inode *dir, nid_t pino) |
| 202 | { |
| 203 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 204 | struct qstr dot = QSTR_INIT(".", 1); |
| 205 | struct qstr dotdot = QSTR_INIT("..", 2); |
| 206 | struct f2fs_dir_entry *de; |
| 207 | struct page *page; |
| 208 | int err = 0; |
| 209 | |
| 210 | f2fs_lock_op(sbi); |
| 211 | |
| 212 | de = f2fs_find_entry(dir, &dot, &page); |
| 213 | if (de) { |
| 214 | f2fs_dentry_kunmap(dir, page); |
| 215 | f2fs_put_page(page, 0); |
| 216 | } else { |
| 217 | err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR); |
| 218 | if (err) |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | de = f2fs_find_entry(dir, &dotdot, &page); |
| 223 | if (de) { |
| 224 | f2fs_dentry_kunmap(dir, page); |
| 225 | f2fs_put_page(page, 0); |
| 226 | } else { |
| 227 | err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR); |
| 228 | } |
| 229 | out: |
| 230 | if (!err) { |
| 231 | clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS); |
| 232 | mark_inode_dirty(dir); |
| 233 | } |
| 234 | |
| 235 | f2fs_unlock_op(sbi); |
| 236 | return err; |
| 237 | } |
| 238 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 239 | static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, |
| 240 | unsigned int flags) |
| 241 | { |
| 242 | struct inode *inode = NULL; |
| 243 | struct f2fs_dir_entry *de; |
| 244 | struct page *page; |
Jaegeuk Kim | 06957e8 | 2015-04-22 11:40:27 -0700 | [diff] [blame] | 245 | nid_t ino; |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 246 | int err = 0; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 247 | |
Jaegeuk Kim | 5a20d33 | 2013-03-03 13:58:05 +0900 | [diff] [blame] | 248 | if (dentry->d_name.len > F2FS_NAME_LEN) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 249 | return ERR_PTR(-ENAMETOOLONG); |
| 250 | |
| 251 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
Jaegeuk Kim | 06957e8 | 2015-04-22 11:40:27 -0700 | [diff] [blame] | 252 | if (!de) |
| 253 | return d_splice_alias(inode, dentry); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 254 | |
Jaegeuk Kim | 06957e8 | 2015-04-22 11:40:27 -0700 | [diff] [blame] | 255 | ino = le32_to_cpu(de->ino); |
| 256 | f2fs_dentry_kunmap(dir, page); |
| 257 | f2fs_put_page(page, 0); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 258 | |
Jaegeuk Kim | 06957e8 | 2015-04-22 11:40:27 -0700 | [diff] [blame] | 259 | inode = f2fs_iget(dir->i_sb, ino); |
| 260 | if (IS_ERR(inode)) |
| 261 | return ERR_CAST(inode); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 262 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 263 | if (f2fs_has_inline_dots(inode)) { |
Jaegeuk Kim | 06957e8 | 2015-04-22 11:40:27 -0700 | [diff] [blame] | 264 | err = __recover_dot_dentries(inode, dir->i_ino); |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 265 | if (err) |
| 266 | goto err_out; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 267 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 268 | return d_splice_alias(inode, dentry); |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 269 | |
| 270 | err_out: |
| 271 | iget_failed(inode); |
| 272 | return ERR_PTR(err); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int f2fs_unlink(struct inode *dir, struct dentry *dentry) |
| 276 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 277 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 278 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 279 | struct f2fs_dir_entry *de; |
| 280 | struct page *page; |
| 281 | int err = -ENOENT; |
| 282 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 283 | trace_f2fs_unlink_enter(dir, dentry); |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 284 | f2fs_balance_fs(sbi); |
| 285 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 286 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 287 | if (!de) |
| 288 | goto fail; |
| 289 | |
Jaegeuk Kim | ccaaca2 | 2013-10-08 10:19:28 +0900 | [diff] [blame] | 290 | f2fs_lock_op(sbi); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 291 | err = acquire_orphan_inode(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 292 | if (err) { |
Jaegeuk Kim | ccaaca2 | 2013-10-08 10:19:28 +0900 | [diff] [blame] | 293 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 294 | f2fs_dentry_kunmap(dir, page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 295 | f2fs_put_page(page, 0); |
| 296 | goto fail; |
| 297 | } |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 298 | f2fs_delete_entry(de, page, dir, inode); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 299 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 300 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 301 | /* In order to evict this inode, we set it dirty */ |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 302 | mark_inode_dirty(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 303 | |
| 304 | if (IS_DIRSYNC(dir)) |
| 305 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 306 | fail: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 307 | trace_f2fs_unlink_exit(inode, err); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 308 | return err; |
| 309 | } |
| 310 | |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 311 | static void *f2fs_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 312 | { |
Jaegeuk Kim | 7263b1b | 2015-04-22 11:03:48 -0700 | [diff] [blame] | 313 | struct page *page = page_follow_link_light(dentry, nd); |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 314 | |
Jaegeuk Kim | 7263b1b | 2015-04-22 11:03:48 -0700 | [diff] [blame] | 315 | if (IS_ERR_OR_NULL(page)) |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 316 | return page; |
| 317 | |
| 318 | /* this is broken symlink case */ |
| 319 | if (*nd_get_link(nd) == 0) { |
Jaegeuk Kim | 7263b1b | 2015-04-22 11:03:48 -0700 | [diff] [blame] | 320 | page_put_link(dentry, nd, page); |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 321 | return ERR_PTR(-ENOENT); |
| 322 | } |
| 323 | return page; |
| 324 | } |
| 325 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 326 | static int f2fs_symlink(struct inode *dir, struct dentry *dentry, |
| 327 | const char *symname) |
| 328 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 329 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 330 | struct inode *inode; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 331 | size_t len = strlen(symname); |
| 332 | size_t p_len; |
| 333 | char *p_str; |
| 334 | struct f2fs_str disk_link = FSTR_INIT(NULL, 0); |
| 335 | struct f2fs_encrypted_symlink_data *sd = NULL; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 336 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 337 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 338 | if (len > dir->i_sb->s_blocksize) |
| 339 | return -ENAMETOOLONG; |
| 340 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 341 | f2fs_balance_fs(sbi); |
| 342 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 343 | inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 344 | if (IS_ERR(inode)) |
| 345 | return PTR_ERR(inode); |
| 346 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 347 | if (f2fs_encrypted_inode(inode)) |
| 348 | inode->i_op = &f2fs_encrypted_symlink_inode_operations; |
| 349 | else |
| 350 | inode->i_op = &f2fs_symlink_inode_operations; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 351 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 352 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 353 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 354 | err = f2fs_add_link(dentry, inode); |
| 355 | if (err) |
| 356 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 357 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 358 | alloc_nid_done(sbi, inode->i_ino); |
| 359 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 360 | if (f2fs_encrypted_inode(dir)) { |
| 361 | struct qstr istr = QSTR_INIT(symname, len); |
| 362 | |
| 363 | err = f2fs_inherit_context(dir, inode, NULL); |
| 364 | if (err) |
| 365 | goto err_out; |
| 366 | |
Jaegeuk Kim | 26bf3dc | 2015-05-19 22:26:54 -0700 | [diff] [blame^] | 367 | err = f2fs_get_encryption_info(inode); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 368 | if (err) |
| 369 | goto err_out; |
| 370 | |
| 371 | err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link); |
| 372 | if (err) |
| 373 | goto err_out; |
| 374 | |
| 375 | err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link); |
| 376 | if (err < 0) |
| 377 | goto err_out; |
| 378 | |
| 379 | p_len = encrypted_symlink_data_len(disk_link.len) + 1; |
| 380 | |
| 381 | if (p_len > dir->i_sb->s_blocksize) { |
| 382 | err = -ENAMETOOLONG; |
| 383 | goto err_out; |
| 384 | } |
| 385 | |
| 386 | sd = kzalloc(p_len, GFP_NOFS); |
| 387 | if (!sd) { |
| 388 | err = -ENOMEM; |
| 389 | goto err_out; |
| 390 | } |
| 391 | memcpy(sd->encrypted_path, disk_link.name, disk_link.len); |
| 392 | sd->len = cpu_to_le16(disk_link.len); |
| 393 | p_str = (char *)sd; |
| 394 | } else { |
| 395 | p_len = len + 1; |
| 396 | p_str = (char *)symname; |
| 397 | } |
| 398 | |
| 399 | err = page_symlink(inode, p_str, p_len); |
| 400 | |
| 401 | err_out: |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 402 | d_instantiate(dentry, inode); |
| 403 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 404 | |
Jaegeuk Kim | d0cae97 | 2015-04-15 13:37:53 -0700 | [diff] [blame] | 405 | /* |
| 406 | * Let's flush symlink data in order to avoid broken symlink as much as |
| 407 | * possible. Nevertheless, fsyncing is the best way, but there is no |
| 408 | * way to get a file descriptor in order to flush that. |
| 409 | * |
| 410 | * Note that, it needs to do dir->fsync to make this recoverable. |
| 411 | * If the symlink path is stored into inline_data, there is no |
| 412 | * performance regression. |
| 413 | */ |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 414 | if (!err) |
| 415 | filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1); |
Jaegeuk Kim | d0cae97 | 2015-04-15 13:37:53 -0700 | [diff] [blame] | 416 | |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 417 | if (IS_DIRSYNC(dir)) |
| 418 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 419 | |
| 420 | kfree(sd); |
| 421 | f2fs_fname_crypto_free_buffer(&disk_link); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 422 | return err; |
| 423 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 424 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 425 | return err; |
| 426 | } |
| 427 | |
| 428 | static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 429 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 430 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 431 | struct inode *inode; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 432 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 433 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 434 | f2fs_balance_fs(sbi); |
| 435 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 436 | inode = f2fs_new_inode(dir, S_IFDIR | mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 437 | if (IS_ERR(inode)) |
Namjae Jeon | 61412b6 | 2012-12-01 10:56:25 +0900 | [diff] [blame] | 438 | return PTR_ERR(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 439 | |
| 440 | inode->i_op = &f2fs_dir_inode_operations; |
| 441 | inode->i_fop = &f2fs_dir_operations; |
| 442 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
Chao Yu | 560d467 | 2015-02-07 17:36:15 +0800 | [diff] [blame] | 443 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 444 | |
| 445 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 446 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 447 | err = f2fs_add_link(dentry, inode); |
| 448 | if (err) |
| 449 | goto out_fail; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 450 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 451 | |
| 452 | alloc_nid_done(sbi, inode->i_ino); |
| 453 | |
| 454 | d_instantiate(dentry, inode); |
| 455 | unlock_new_inode(inode); |
| 456 | |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 457 | if (IS_DIRSYNC(dir)) |
| 458 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 459 | return 0; |
| 460 | |
| 461 | out_fail: |
| 462 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 463 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 464 | return err; |
| 465 | } |
| 466 | |
| 467 | static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) |
| 468 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 469 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 470 | if (f2fs_empty_dir(inode)) |
| 471 | return f2fs_unlink(dir, dentry); |
| 472 | return -ENOTEMPTY; |
| 473 | } |
| 474 | |
| 475 | static int f2fs_mknod(struct inode *dir, struct dentry *dentry, |
| 476 | umode_t mode, dev_t rdev) |
| 477 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 478 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 479 | struct inode *inode; |
| 480 | int err = 0; |
| 481 | |
| 482 | if (!new_valid_dev(rdev)) |
| 483 | return -EINVAL; |
| 484 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 485 | f2fs_balance_fs(sbi); |
| 486 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 487 | inode = f2fs_new_inode(dir, mode); |
| 488 | if (IS_ERR(inode)) |
| 489 | return PTR_ERR(inode); |
| 490 | |
| 491 | init_special_inode(inode, inode->i_mode, rdev); |
| 492 | inode->i_op = &f2fs_special_inode_operations; |
| 493 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 494 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 495 | err = f2fs_add_link(dentry, inode); |
| 496 | if (err) |
| 497 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 498 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 499 | |
| 500 | alloc_nid_done(sbi, inode->i_ino); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 501 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 502 | d_instantiate(dentry, inode); |
| 503 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 504 | |
| 505 | if (IS_DIRSYNC(dir)) |
| 506 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 507 | return 0; |
| 508 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 509 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 510 | return err; |
| 511 | } |
| 512 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 513 | static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry, |
| 514 | umode_t mode, struct inode **whiteout) |
| 515 | { |
| 516 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 517 | struct inode *inode; |
| 518 | int err; |
| 519 | |
| 520 | if (!whiteout) |
| 521 | f2fs_balance_fs(sbi); |
| 522 | |
| 523 | inode = f2fs_new_inode(dir, mode); |
| 524 | if (IS_ERR(inode)) |
| 525 | return PTR_ERR(inode); |
| 526 | |
| 527 | if (whiteout) { |
| 528 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); |
| 529 | inode->i_op = &f2fs_special_inode_operations; |
| 530 | } else { |
| 531 | inode->i_op = &f2fs_file_inode_operations; |
| 532 | inode->i_fop = &f2fs_file_operations; |
| 533 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 534 | } |
| 535 | |
| 536 | f2fs_lock_op(sbi); |
| 537 | err = acquire_orphan_inode(sbi); |
| 538 | if (err) |
| 539 | goto out; |
| 540 | |
| 541 | err = f2fs_do_tmpfile(inode, dir); |
| 542 | if (err) |
| 543 | goto release_out; |
| 544 | |
| 545 | /* |
| 546 | * add this non-linked tmpfile to orphan list, in this way we could |
| 547 | * remove all unused data of tmpfile after abnormal power-off. |
| 548 | */ |
| 549 | add_orphan_inode(sbi, inode->i_ino); |
| 550 | f2fs_unlock_op(sbi); |
| 551 | |
| 552 | alloc_nid_done(sbi, inode->i_ino); |
| 553 | |
| 554 | if (whiteout) { |
| 555 | inode_dec_link_count(inode); |
| 556 | *whiteout = inode; |
| 557 | } else { |
| 558 | d_tmpfile(dentry, inode); |
| 559 | } |
| 560 | unlock_new_inode(inode); |
| 561 | return 0; |
| 562 | |
| 563 | release_out: |
| 564 | release_orphan_inode(sbi); |
| 565 | out: |
| 566 | handle_failed_inode(inode); |
| 567 | return err; |
| 568 | } |
| 569 | |
| 570 | static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 571 | { |
Jaegeuk Kim | 304eecc | 2015-05-19 16:11:40 -0700 | [diff] [blame] | 572 | if (f2fs_encrypted_inode(dir)) { |
| 573 | int err = f2fs_get_encryption_info(dir); |
| 574 | if (err) |
| 575 | return err; |
| 576 | } |
| 577 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 578 | return __f2fs_tmpfile(dir, dentry, mode, NULL); |
| 579 | } |
| 580 | |
| 581 | static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout) |
| 582 | { |
| 583 | return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout); |
| 584 | } |
| 585 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 586 | static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 587 | struct inode *new_dir, struct dentry *new_dentry, |
| 588 | unsigned int flags) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 589 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 590 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 591 | struct inode *old_inode = d_inode(old_dentry); |
| 592 | struct inode *new_inode = d_inode(new_dentry); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 593 | struct inode *whiteout = NULL; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 594 | struct page *old_dir_page; |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 595 | struct page *old_page, *new_page = NULL; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 596 | struct f2fs_dir_entry *old_dir_entry = NULL; |
| 597 | struct f2fs_dir_entry *old_entry; |
| 598 | struct f2fs_dir_entry *new_entry; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 599 | int err = -ENOENT; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 600 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 601 | if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) && |
| 602 | !f2fs_is_child_context_consistent_with_parent(new_dir, |
| 603 | old_inode)) { |
| 604 | err = -EPERM; |
| 605 | goto out; |
| 606 | } |
| 607 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 608 | f2fs_balance_fs(sbi); |
| 609 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 610 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 611 | if (!old_entry) |
| 612 | goto out; |
| 613 | |
| 614 | if (S_ISDIR(old_inode->i_mode)) { |
| 615 | err = -EIO; |
| 616 | old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); |
| 617 | if (!old_dir_entry) |
| 618 | goto out_old; |
| 619 | } |
| 620 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 621 | if (flags & RENAME_WHITEOUT) { |
| 622 | err = f2fs_create_whiteout(old_dir, &whiteout); |
| 623 | if (err) |
| 624 | goto out_dir; |
| 625 | } |
| 626 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 627 | if (new_inode) { |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 628 | |
| 629 | err = -ENOTEMPTY; |
| 630 | if (old_dir_entry && !f2fs_empty_dir(new_inode)) |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 631 | goto out_whiteout; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 632 | |
| 633 | err = -ENOENT; |
| 634 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, |
| 635 | &new_page); |
| 636 | if (!new_entry) |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 637 | goto out_whiteout; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 638 | |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 639 | f2fs_lock_op(sbi); |
| 640 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 641 | err = acquire_orphan_inode(sbi); |
| 642 | if (err) |
| 643 | goto put_out_dir; |
| 644 | |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 645 | if (update_dent_inode(old_inode, new_inode, |
| 646 | &new_dentry->d_name)) { |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 647 | release_orphan_inode(sbi); |
| 648 | goto put_out_dir; |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 649 | } |
| 650 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 651 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 652 | |
| 653 | new_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 654 | down_write(&F2FS_I(new_inode)->i_sem); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 655 | if (old_dir_entry) |
| 656 | drop_nlink(new_inode); |
| 657 | drop_nlink(new_inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 658 | up_write(&F2FS_I(new_inode)->i_sem); |
| 659 | |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 660 | mark_inode_dirty(new_inode); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 661 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 662 | if (!new_inode->i_nlink) |
| 663 | add_orphan_inode(sbi, new_inode->i_ino); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 664 | else |
| 665 | release_orphan_inode(sbi); |
| 666 | |
Jaegeuk Kim | 8cb8268 | 2013-08-26 21:25:31 +0900 | [diff] [blame] | 667 | update_inode_page(old_inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 668 | update_inode_page(new_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 669 | } else { |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 670 | f2fs_lock_op(sbi); |
| 671 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 672 | err = f2fs_add_link(new_dentry, old_inode); |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 673 | if (err) { |
| 674 | f2fs_unlock_op(sbi); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 675 | goto out_whiteout; |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 676 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 677 | |
| 678 | if (old_dir_entry) { |
| 679 | inc_nlink(new_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 680 | update_inode_page(new_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
Jaegeuk Kim | b2c0829 | 2014-06-30 18:09:55 +0900 | [diff] [blame] | 684 | down_write(&F2FS_I(old_inode)->i_sem); |
| 685 | file_lost_pino(old_inode); |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 686 | if (new_inode && file_enc_name(new_inode)) |
| 687 | file_set_enc_name(old_inode); |
Jaegeuk Kim | b2c0829 | 2014-06-30 18:09:55 +0900 | [diff] [blame] | 688 | up_write(&F2FS_I(old_inode)->i_sem); |
| 689 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 690 | old_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 691 | mark_inode_dirty(old_inode); |
| 692 | |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 693 | f2fs_delete_entry(old_entry, old_page, old_dir, NULL); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 694 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 695 | if (whiteout) { |
| 696 | whiteout->i_state |= I_LINKABLE; |
| 697 | set_inode_flag(F2FS_I(whiteout), FI_INC_LINK); |
| 698 | err = f2fs_add_link(old_dentry, whiteout); |
| 699 | if (err) |
| 700 | goto put_out_dir; |
| 701 | whiteout->i_state &= ~I_LINKABLE; |
| 702 | iput(whiteout); |
| 703 | } |
| 704 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 705 | if (old_dir_entry) { |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 706 | if (old_dir != new_dir && !whiteout) { |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 707 | f2fs_set_link(old_inode, old_dir_entry, |
| 708 | old_dir_page, new_dir); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 709 | update_inode_page(old_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 710 | } else { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 711 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 712 | f2fs_put_page(old_dir_page, 0); |
| 713 | } |
| 714 | drop_nlink(old_dir); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 715 | mark_inode_dirty(old_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 716 | update_inode_page(old_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 717 | } |
| 718 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 719 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 720 | |
| 721 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 722 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 723 | return 0; |
| 724 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 725 | put_out_dir: |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 726 | f2fs_unlock_op(sbi); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 727 | if (new_page) { |
| 728 | f2fs_dentry_kunmap(new_dir, new_page); |
| 729 | f2fs_put_page(new_page, 0); |
| 730 | } |
| 731 | out_whiteout: |
| 732 | if (whiteout) |
| 733 | iput(whiteout); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 734 | out_dir: |
| 735 | if (old_dir_entry) { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 736 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 737 | f2fs_put_page(old_dir_page, 0); |
| 738 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 739 | out_old: |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 740 | f2fs_dentry_kunmap(old_dir, old_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 741 | f2fs_put_page(old_page, 0); |
| 742 | out: |
| 743 | return err; |
| 744 | } |
| 745 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 746 | static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 747 | struct inode *new_dir, struct dentry *new_dentry) |
| 748 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 749 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 750 | struct inode *old_inode = d_inode(old_dentry); |
| 751 | struct inode *new_inode = d_inode(new_dentry); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 752 | struct page *old_dir_page, *new_dir_page; |
| 753 | struct page *old_page, *new_page; |
| 754 | struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; |
| 755 | struct f2fs_dir_entry *old_entry, *new_entry; |
| 756 | int old_nlink = 0, new_nlink = 0; |
| 757 | int err = -ENOENT; |
| 758 | |
| 759 | f2fs_balance_fs(sbi); |
| 760 | |
| 761 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 762 | if (!old_entry) |
| 763 | goto out; |
| 764 | |
| 765 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page); |
| 766 | if (!new_entry) |
| 767 | goto out_old; |
| 768 | |
| 769 | /* prepare for updating ".." directory entry info later */ |
| 770 | if (old_dir != new_dir) { |
| 771 | if (S_ISDIR(old_inode->i_mode)) { |
| 772 | err = -EIO; |
| 773 | old_dir_entry = f2fs_parent_dir(old_inode, |
| 774 | &old_dir_page); |
| 775 | if (!old_dir_entry) |
| 776 | goto out_new; |
| 777 | } |
| 778 | |
| 779 | if (S_ISDIR(new_inode->i_mode)) { |
| 780 | err = -EIO; |
| 781 | new_dir_entry = f2fs_parent_dir(new_inode, |
| 782 | &new_dir_page); |
| 783 | if (!new_dir_entry) |
| 784 | goto out_old_dir; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * If cross rename between file and directory those are not |
| 790 | * in the same directory, we will inc nlink of file's parent |
| 791 | * later, so we should check upper boundary of its nlink. |
| 792 | */ |
| 793 | if ((!old_dir_entry || !new_dir_entry) && |
| 794 | old_dir_entry != new_dir_entry) { |
| 795 | old_nlink = old_dir_entry ? -1 : 1; |
| 796 | new_nlink = -old_nlink; |
| 797 | err = -EMLINK; |
| 798 | if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) || |
| 799 | (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX)) |
| 800 | goto out_new_dir; |
| 801 | } |
| 802 | |
| 803 | f2fs_lock_op(sbi); |
| 804 | |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 805 | err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 806 | if (err) |
| 807 | goto out_unlock; |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 808 | if (file_enc_name(new_inode)) |
| 809 | file_set_enc_name(old_inode); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 810 | |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 811 | err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 812 | if (err) |
| 813 | goto out_undo; |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 814 | if (file_enc_name(old_inode)) |
| 815 | file_set_enc_name(new_inode); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 816 | |
| 817 | /* update ".." directory entry info of old dentry */ |
| 818 | if (old_dir_entry) |
| 819 | f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir); |
| 820 | |
| 821 | /* update ".." directory entry info of new dentry */ |
| 822 | if (new_dir_entry) |
| 823 | f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir); |
| 824 | |
| 825 | /* update directory entry info of old dir inode */ |
| 826 | f2fs_set_link(old_dir, old_entry, old_page, new_inode); |
| 827 | |
| 828 | down_write(&F2FS_I(old_inode)->i_sem); |
| 829 | file_lost_pino(old_inode); |
| 830 | up_write(&F2FS_I(old_inode)->i_sem); |
| 831 | |
| 832 | update_inode_page(old_inode); |
| 833 | |
| 834 | old_dir->i_ctime = CURRENT_TIME; |
| 835 | if (old_nlink) { |
| 836 | down_write(&F2FS_I(old_dir)->i_sem); |
| 837 | if (old_nlink < 0) |
| 838 | drop_nlink(old_dir); |
| 839 | else |
| 840 | inc_nlink(old_dir); |
| 841 | up_write(&F2FS_I(old_dir)->i_sem); |
| 842 | } |
| 843 | mark_inode_dirty(old_dir); |
| 844 | update_inode_page(old_dir); |
| 845 | |
| 846 | /* update directory entry info of new dir inode */ |
| 847 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 848 | |
| 849 | down_write(&F2FS_I(new_inode)->i_sem); |
| 850 | file_lost_pino(new_inode); |
| 851 | up_write(&F2FS_I(new_inode)->i_sem); |
| 852 | |
| 853 | update_inode_page(new_inode); |
| 854 | |
| 855 | new_dir->i_ctime = CURRENT_TIME; |
| 856 | if (new_nlink) { |
| 857 | down_write(&F2FS_I(new_dir)->i_sem); |
| 858 | if (new_nlink < 0) |
| 859 | drop_nlink(new_dir); |
| 860 | else |
| 861 | inc_nlink(new_dir); |
| 862 | up_write(&F2FS_I(new_dir)->i_sem); |
| 863 | } |
| 864 | mark_inode_dirty(new_dir); |
| 865 | update_inode_page(new_dir); |
| 866 | |
| 867 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 868 | |
| 869 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 870 | f2fs_sync_fs(sbi->sb, 1); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 871 | return 0; |
| 872 | out_undo: |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 873 | /* |
| 874 | * Still we may fail to recover name info of f2fs_inode here |
| 875 | * Drop it, once its name is set as encrypted |
| 876 | */ |
| 877 | update_dent_inode(old_inode, old_inode, &old_dentry->d_name); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 878 | out_unlock: |
| 879 | f2fs_unlock_op(sbi); |
| 880 | out_new_dir: |
| 881 | if (new_dir_entry) { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 882 | f2fs_dentry_kunmap(new_inode, new_dir_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 883 | f2fs_put_page(new_dir_page, 0); |
| 884 | } |
| 885 | out_old_dir: |
| 886 | if (old_dir_entry) { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 887 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 888 | f2fs_put_page(old_dir_page, 0); |
| 889 | } |
| 890 | out_new: |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 891 | f2fs_dentry_kunmap(new_dir, new_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 892 | f2fs_put_page(new_page, 0); |
| 893 | out_old: |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 894 | f2fs_dentry_kunmap(old_dir, old_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 895 | f2fs_put_page(old_page, 0); |
| 896 | out: |
| 897 | return err; |
| 898 | } |
| 899 | |
| 900 | static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 901 | struct inode *new_dir, struct dentry *new_dentry, |
| 902 | unsigned int flags) |
| 903 | { |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 904 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 905 | return -EINVAL; |
| 906 | |
| 907 | if (flags & RENAME_EXCHANGE) { |
| 908 | return f2fs_cross_rename(old_dir, old_dentry, |
| 909 | new_dir, new_dentry); |
| 910 | } |
| 911 | /* |
| 912 | * VFS has already handled the new dentry existence case, |
| 913 | * here, we just deal with "RENAME_NOREPLACE" as regular rename. |
| 914 | */ |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 915 | return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 916 | } |
| 917 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 918 | #ifdef CONFIG_F2FS_FS_ENCRYPTION |
| 919 | static void *f2fs_encrypted_follow_link(struct dentry *dentry, |
| 920 | struct nameidata *nd) |
| 921 | { |
| 922 | struct page *cpage = NULL; |
| 923 | char *caddr, *paddr = NULL; |
| 924 | struct f2fs_str cstr; |
| 925 | struct f2fs_str pstr = FSTR_INIT(NULL, 0); |
| 926 | struct inode *inode = d_inode(dentry); |
| 927 | struct f2fs_encrypted_symlink_data *sd; |
| 928 | loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1); |
| 929 | u32 max_size = inode->i_sb->s_blocksize; |
| 930 | int res; |
| 931 | |
Jaegeuk Kim | 26bf3dc | 2015-05-19 22:26:54 -0700 | [diff] [blame^] | 932 | res = f2fs_get_encryption_info(inode); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 933 | if (res) |
| 934 | return ERR_PTR(res); |
| 935 | |
| 936 | cpage = read_mapping_page(inode->i_mapping, 0, NULL); |
| 937 | if (IS_ERR(cpage)) |
| 938 | return cpage; |
| 939 | caddr = kmap(cpage); |
| 940 | caddr[size] = 0; |
| 941 | |
| 942 | /* Symlink is encrypted */ |
| 943 | sd = (struct f2fs_encrypted_symlink_data *)caddr; |
| 944 | cstr.name = sd->encrypted_path; |
| 945 | cstr.len = le16_to_cpu(sd->len); |
| 946 | |
| 947 | /* this is broken symlink case */ |
| 948 | if (cstr.name[0] == 0 && cstr.len == 0) { |
| 949 | res = -ENOENT; |
| 950 | goto errout; |
| 951 | } |
| 952 | |
| 953 | if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) > |
| 954 | max_size) { |
| 955 | /* Symlink data on the disk is corrupted */ |
| 956 | res = -EIO; |
| 957 | goto errout; |
| 958 | } |
| 959 | res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr); |
| 960 | if (res) |
| 961 | goto errout; |
| 962 | |
| 963 | res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr); |
| 964 | if (res < 0) |
| 965 | goto errout; |
| 966 | |
| 967 | paddr = pstr.name; |
| 968 | |
| 969 | /* Null-terminate the name */ |
| 970 | paddr[res] = '\0'; |
| 971 | nd_set_link(nd, paddr); |
| 972 | |
| 973 | kunmap(cpage); |
| 974 | page_cache_release(cpage); |
| 975 | return NULL; |
| 976 | errout: |
| 977 | f2fs_fname_crypto_free_buffer(&pstr); |
| 978 | kunmap(cpage); |
| 979 | page_cache_release(cpage); |
| 980 | return ERR_PTR(res); |
| 981 | } |
| 982 | |
| 983 | const struct inode_operations f2fs_encrypted_symlink_inode_operations = { |
| 984 | .readlink = generic_readlink, |
| 985 | .follow_link = f2fs_encrypted_follow_link, |
| 986 | .put_link = kfree_put_link, |
| 987 | .getattr = f2fs_getattr, |
| 988 | .setattr = f2fs_setattr, |
| 989 | .setxattr = generic_setxattr, |
| 990 | .getxattr = generic_getxattr, |
| 991 | .listxattr = f2fs_listxattr, |
| 992 | .removexattr = generic_removexattr, |
| 993 | }; |
| 994 | #endif |
| 995 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 996 | const struct inode_operations f2fs_dir_inode_operations = { |
| 997 | .create = f2fs_create, |
| 998 | .lookup = f2fs_lookup, |
| 999 | .link = f2fs_link, |
| 1000 | .unlink = f2fs_unlink, |
| 1001 | .symlink = f2fs_symlink, |
| 1002 | .mkdir = f2fs_mkdir, |
| 1003 | .rmdir = f2fs_rmdir, |
| 1004 | .mknod = f2fs_mknod, |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1005 | .rename2 = f2fs_rename2, |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 1006 | .tmpfile = f2fs_tmpfile, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1007 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1008 | .setattr = f2fs_setattr, |
| 1009 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 1010 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1011 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1012 | .setxattr = generic_setxattr, |
| 1013 | .getxattr = generic_getxattr, |
| 1014 | .listxattr = f2fs_listxattr, |
| 1015 | .removexattr = generic_removexattr, |
| 1016 | #endif |
| 1017 | }; |
| 1018 | |
| 1019 | const struct inode_operations f2fs_symlink_inode_operations = { |
| 1020 | .readlink = generic_readlink, |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 1021 | .follow_link = f2fs_follow_link, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1022 | .put_link = page_put_link, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1023 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1024 | .setattr = f2fs_setattr, |
| 1025 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1026 | .setxattr = generic_setxattr, |
| 1027 | .getxattr = generic_getxattr, |
| 1028 | .listxattr = f2fs_listxattr, |
| 1029 | .removexattr = generic_removexattr, |
| 1030 | #endif |
| 1031 | }; |
| 1032 | |
| 1033 | const struct inode_operations f2fs_special_inode_operations = { |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1034 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1035 | .setattr = f2fs_setattr, |
| 1036 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 1037 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1038 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1039 | .setxattr = generic_setxattr, |
| 1040 | .getxattr = generic_getxattr, |
| 1041 | .listxattr = f2fs_listxattr, |
| 1042 | .removexattr = generic_removexattr, |
| 1043 | #endif |
| 1044 | }; |