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 | 9486ba442 | 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 | |
Al Viro | 6e77137 | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 311 | static const char *f2fs_follow_link(struct dentry *dentry, void **cookie) |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 312 | { |
Al Viro | 6e77137 | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 313 | const char *link = page_follow_link_light(dentry, cookie); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 314 | if (!IS_ERR(link) && !*link) { |
| 315 | /* this is broken symlink case */ |
Al Viro | 5f2c417 | 2015-05-07 11:14:26 -0400 | [diff] [blame] | 316 | page_put_link(NULL, *cookie); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 317 | link = ERR_PTR(-ENOENT); |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 318 | } |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 319 | return link; |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 322 | static int f2fs_symlink(struct inode *dir, struct dentry *dentry, |
| 323 | const char *symname) |
| 324 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 325 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 326 | struct inode *inode; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 327 | size_t len = strlen(symname); |
| 328 | size_t p_len; |
| 329 | char *p_str; |
| 330 | struct f2fs_str disk_link = FSTR_INIT(NULL, 0); |
| 331 | struct f2fs_encrypted_symlink_data *sd = NULL; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 332 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 333 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 334 | if (len > dir->i_sb->s_blocksize) |
| 335 | return -ENAMETOOLONG; |
| 336 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 337 | f2fs_balance_fs(sbi); |
| 338 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 339 | inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 340 | if (IS_ERR(inode)) |
| 341 | return PTR_ERR(inode); |
| 342 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 343 | if (f2fs_encrypted_inode(inode)) |
| 344 | inode->i_op = &f2fs_encrypted_symlink_inode_operations; |
| 345 | else |
| 346 | inode->i_op = &f2fs_symlink_inode_operations; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 347 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 348 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 349 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 350 | err = f2fs_add_link(dentry, inode); |
| 351 | if (err) |
| 352 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 353 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 354 | alloc_nid_done(sbi, inode->i_ino); |
| 355 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 356 | if (f2fs_encrypted_inode(dir)) { |
| 357 | struct qstr istr = QSTR_INIT(symname, len); |
| 358 | |
Jaegeuk Kim | 26bf3dc | 2015-05-19 22:26:54 -0700 | [diff] [blame] | 359 | err = f2fs_get_encryption_info(inode); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 360 | if (err) |
| 361 | goto err_out; |
| 362 | |
| 363 | err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link); |
| 364 | if (err) |
| 365 | goto err_out; |
| 366 | |
| 367 | err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link); |
| 368 | if (err < 0) |
| 369 | goto err_out; |
| 370 | |
| 371 | p_len = encrypted_symlink_data_len(disk_link.len) + 1; |
| 372 | |
| 373 | if (p_len > dir->i_sb->s_blocksize) { |
| 374 | err = -ENAMETOOLONG; |
| 375 | goto err_out; |
| 376 | } |
| 377 | |
| 378 | sd = kzalloc(p_len, GFP_NOFS); |
| 379 | if (!sd) { |
| 380 | err = -ENOMEM; |
| 381 | goto err_out; |
| 382 | } |
| 383 | memcpy(sd->encrypted_path, disk_link.name, disk_link.len); |
| 384 | sd->len = cpu_to_le16(disk_link.len); |
| 385 | p_str = (char *)sd; |
| 386 | } else { |
| 387 | p_len = len + 1; |
| 388 | p_str = (char *)symname; |
| 389 | } |
| 390 | |
| 391 | err = page_symlink(inode, p_str, p_len); |
| 392 | |
| 393 | err_out: |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 394 | d_instantiate(dentry, inode); |
| 395 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 396 | |
Jaegeuk Kim | d0cae97 | 2015-04-15 13:37:53 -0700 | [diff] [blame] | 397 | /* |
| 398 | * Let's flush symlink data in order to avoid broken symlink as much as |
| 399 | * possible. Nevertheless, fsyncing is the best way, but there is no |
| 400 | * way to get a file descriptor in order to flush that. |
| 401 | * |
| 402 | * Note that, it needs to do dir->fsync to make this recoverable. |
| 403 | * If the symlink path is stored into inline_data, there is no |
| 404 | * performance regression. |
| 405 | */ |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 406 | if (!err) |
| 407 | filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1); |
Jaegeuk Kim | d0cae97 | 2015-04-15 13:37:53 -0700 | [diff] [blame] | 408 | |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 409 | if (IS_DIRSYNC(dir)) |
| 410 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 411 | |
| 412 | kfree(sd); |
| 413 | f2fs_fname_crypto_free_buffer(&disk_link); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 414 | return err; |
| 415 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 416 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 417 | return err; |
| 418 | } |
| 419 | |
| 420 | static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 421 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 422 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 423 | struct inode *inode; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 424 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 425 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 426 | f2fs_balance_fs(sbi); |
| 427 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 428 | inode = f2fs_new_inode(dir, S_IFDIR | mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 429 | if (IS_ERR(inode)) |
Namjae Jeon | 61412b6 | 2012-12-01 10:56:25 +0900 | [diff] [blame] | 430 | return PTR_ERR(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 431 | |
| 432 | inode->i_op = &f2fs_dir_inode_operations; |
| 433 | inode->i_fop = &f2fs_dir_operations; |
| 434 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
Chao Yu | 560d467 | 2015-02-07 17:36:15 +0800 | [diff] [blame] | 435 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 436 | |
| 437 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 438 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 439 | err = f2fs_add_link(dentry, inode); |
| 440 | if (err) |
| 441 | goto out_fail; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 442 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 443 | |
| 444 | alloc_nid_done(sbi, inode->i_ino); |
| 445 | |
| 446 | d_instantiate(dentry, inode); |
| 447 | unlock_new_inode(inode); |
| 448 | |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 449 | if (IS_DIRSYNC(dir)) |
| 450 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 451 | return 0; |
| 452 | |
| 453 | out_fail: |
| 454 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 455 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 456 | return err; |
| 457 | } |
| 458 | |
| 459 | static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) |
| 460 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 461 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 462 | if (f2fs_empty_dir(inode)) |
| 463 | return f2fs_unlink(dir, dentry); |
| 464 | return -ENOTEMPTY; |
| 465 | } |
| 466 | |
| 467 | static int f2fs_mknod(struct inode *dir, struct dentry *dentry, |
| 468 | umode_t mode, dev_t rdev) |
| 469 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 470 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 471 | struct inode *inode; |
| 472 | int err = 0; |
| 473 | |
| 474 | if (!new_valid_dev(rdev)) |
| 475 | return -EINVAL; |
| 476 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 477 | f2fs_balance_fs(sbi); |
| 478 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 479 | inode = f2fs_new_inode(dir, mode); |
| 480 | if (IS_ERR(inode)) |
| 481 | return PTR_ERR(inode); |
| 482 | |
| 483 | init_special_inode(inode, inode->i_mode, rdev); |
| 484 | inode->i_op = &f2fs_special_inode_operations; |
| 485 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 486 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 487 | err = f2fs_add_link(dentry, inode); |
| 488 | if (err) |
| 489 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 490 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 491 | |
| 492 | alloc_nid_done(sbi, inode->i_ino); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 493 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 494 | d_instantiate(dentry, inode); |
| 495 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 496 | |
| 497 | if (IS_DIRSYNC(dir)) |
| 498 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 499 | return 0; |
| 500 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 501 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 502 | return err; |
| 503 | } |
| 504 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 505 | static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry, |
| 506 | umode_t mode, struct inode **whiteout) |
| 507 | { |
| 508 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 509 | struct inode *inode; |
| 510 | int err; |
| 511 | |
| 512 | if (!whiteout) |
| 513 | f2fs_balance_fs(sbi); |
| 514 | |
| 515 | inode = f2fs_new_inode(dir, mode); |
| 516 | if (IS_ERR(inode)) |
| 517 | return PTR_ERR(inode); |
| 518 | |
| 519 | if (whiteout) { |
| 520 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); |
| 521 | inode->i_op = &f2fs_special_inode_operations; |
| 522 | } else { |
| 523 | inode->i_op = &f2fs_file_inode_operations; |
| 524 | inode->i_fop = &f2fs_file_operations; |
| 525 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 526 | } |
| 527 | |
| 528 | f2fs_lock_op(sbi); |
| 529 | err = acquire_orphan_inode(sbi); |
| 530 | if (err) |
| 531 | goto out; |
| 532 | |
| 533 | err = f2fs_do_tmpfile(inode, dir); |
| 534 | if (err) |
| 535 | goto release_out; |
| 536 | |
| 537 | /* |
| 538 | * add this non-linked tmpfile to orphan list, in this way we could |
| 539 | * remove all unused data of tmpfile after abnormal power-off. |
| 540 | */ |
| 541 | add_orphan_inode(sbi, inode->i_ino); |
| 542 | f2fs_unlock_op(sbi); |
| 543 | |
| 544 | alloc_nid_done(sbi, inode->i_ino); |
| 545 | |
| 546 | if (whiteout) { |
| 547 | inode_dec_link_count(inode); |
| 548 | *whiteout = inode; |
| 549 | } else { |
| 550 | d_tmpfile(dentry, inode); |
| 551 | } |
| 552 | unlock_new_inode(inode); |
| 553 | return 0; |
| 554 | |
| 555 | release_out: |
| 556 | release_orphan_inode(sbi); |
| 557 | out: |
| 558 | handle_failed_inode(inode); |
| 559 | return err; |
| 560 | } |
| 561 | |
| 562 | static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 563 | { |
Jaegeuk Kim | 304eecc | 2015-05-19 16:11:40 -0700 | [diff] [blame] | 564 | if (f2fs_encrypted_inode(dir)) { |
| 565 | int err = f2fs_get_encryption_info(dir); |
| 566 | if (err) |
| 567 | return err; |
| 568 | } |
| 569 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 570 | return __f2fs_tmpfile(dir, dentry, mode, NULL); |
| 571 | } |
| 572 | |
| 573 | static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout) |
| 574 | { |
| 575 | return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout); |
| 576 | } |
| 577 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 578 | static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 579 | struct inode *new_dir, struct dentry *new_dentry, |
| 580 | unsigned int flags) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 581 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 582 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 583 | struct inode *old_inode = d_inode(old_dentry); |
| 584 | struct inode *new_inode = d_inode(new_dentry); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 585 | struct inode *whiteout = NULL; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 586 | struct page *old_dir_page; |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 587 | struct page *old_page, *new_page = NULL; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 588 | struct f2fs_dir_entry *old_dir_entry = NULL; |
| 589 | struct f2fs_dir_entry *old_entry; |
| 590 | struct f2fs_dir_entry *new_entry; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 591 | int err = -ENOENT; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 592 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 593 | if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) && |
| 594 | !f2fs_is_child_context_consistent_with_parent(new_dir, |
| 595 | old_inode)) { |
| 596 | err = -EPERM; |
| 597 | goto out; |
| 598 | } |
| 599 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 600 | f2fs_balance_fs(sbi); |
| 601 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 602 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 603 | if (!old_entry) |
| 604 | goto out; |
| 605 | |
| 606 | if (S_ISDIR(old_inode->i_mode)) { |
| 607 | err = -EIO; |
| 608 | old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); |
| 609 | if (!old_dir_entry) |
| 610 | goto out_old; |
| 611 | } |
| 612 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 613 | if (flags & RENAME_WHITEOUT) { |
| 614 | err = f2fs_create_whiteout(old_dir, &whiteout); |
| 615 | if (err) |
| 616 | goto out_dir; |
| 617 | } |
| 618 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 619 | if (new_inode) { |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 620 | |
| 621 | err = -ENOTEMPTY; |
| 622 | if (old_dir_entry && !f2fs_empty_dir(new_inode)) |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 623 | goto out_whiteout; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 624 | |
| 625 | err = -ENOENT; |
| 626 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, |
| 627 | &new_page); |
| 628 | if (!new_entry) |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 629 | goto out_whiteout; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 630 | |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 631 | f2fs_lock_op(sbi); |
| 632 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 633 | err = acquire_orphan_inode(sbi); |
| 634 | if (err) |
| 635 | goto put_out_dir; |
| 636 | |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 637 | if (update_dent_inode(old_inode, new_inode, |
| 638 | &new_dentry->d_name)) { |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 639 | release_orphan_inode(sbi); |
| 640 | goto put_out_dir; |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 641 | } |
| 642 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 643 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 644 | |
| 645 | new_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 646 | down_write(&F2FS_I(new_inode)->i_sem); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 647 | if (old_dir_entry) |
| 648 | drop_nlink(new_inode); |
| 649 | drop_nlink(new_inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 650 | up_write(&F2FS_I(new_inode)->i_sem); |
| 651 | |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 652 | mark_inode_dirty(new_inode); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 653 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 654 | if (!new_inode->i_nlink) |
| 655 | add_orphan_inode(sbi, new_inode->i_ino); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 656 | else |
| 657 | release_orphan_inode(sbi); |
| 658 | |
Jaegeuk Kim | 8cb8268 | 2013-08-26 21:25:31 +0900 | [diff] [blame] | 659 | update_inode_page(old_inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 660 | update_inode_page(new_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 661 | } else { |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 662 | f2fs_lock_op(sbi); |
| 663 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 664 | err = f2fs_add_link(new_dentry, old_inode); |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 665 | if (err) { |
| 666 | f2fs_unlock_op(sbi); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 667 | goto out_whiteout; |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 668 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 669 | |
| 670 | if (old_dir_entry) { |
| 671 | inc_nlink(new_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 672 | update_inode_page(new_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
Jaegeuk Kim | b2c0829 | 2014-06-30 18:09:55 +0900 | [diff] [blame] | 676 | down_write(&F2FS_I(old_inode)->i_sem); |
| 677 | file_lost_pino(old_inode); |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 678 | if (new_inode && file_enc_name(new_inode)) |
| 679 | file_set_enc_name(old_inode); |
Jaegeuk Kim | b2c0829 | 2014-06-30 18:09:55 +0900 | [diff] [blame] | 680 | up_write(&F2FS_I(old_inode)->i_sem); |
| 681 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 682 | old_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 683 | mark_inode_dirty(old_inode); |
| 684 | |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 685 | f2fs_delete_entry(old_entry, old_page, old_dir, NULL); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 686 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 687 | if (whiteout) { |
| 688 | whiteout->i_state |= I_LINKABLE; |
| 689 | set_inode_flag(F2FS_I(whiteout), FI_INC_LINK); |
| 690 | err = f2fs_add_link(old_dentry, whiteout); |
| 691 | if (err) |
| 692 | goto put_out_dir; |
| 693 | whiteout->i_state &= ~I_LINKABLE; |
| 694 | iput(whiteout); |
| 695 | } |
| 696 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 697 | if (old_dir_entry) { |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 698 | if (old_dir != new_dir && !whiteout) { |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 699 | f2fs_set_link(old_inode, old_dir_entry, |
| 700 | old_dir_page, new_dir); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 701 | update_inode_page(old_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 702 | } else { |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 703 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 704 | f2fs_put_page(old_dir_page, 0); |
| 705 | } |
| 706 | drop_nlink(old_dir); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 707 | mark_inode_dirty(old_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 708 | update_inode_page(old_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 709 | } |
| 710 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 711 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 712 | |
| 713 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 714 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 715 | return 0; |
| 716 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 717 | put_out_dir: |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 718 | f2fs_unlock_op(sbi); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 719 | if (new_page) { |
| 720 | f2fs_dentry_kunmap(new_dir, new_page); |
| 721 | f2fs_put_page(new_page, 0); |
| 722 | } |
| 723 | out_whiteout: |
| 724 | if (whiteout) |
| 725 | iput(whiteout); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 726 | out_dir: |
| 727 | if (old_dir_entry) { |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 728 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 729 | f2fs_put_page(old_dir_page, 0); |
| 730 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 731 | out_old: |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 732 | f2fs_dentry_kunmap(old_dir, old_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 733 | f2fs_put_page(old_page, 0); |
| 734 | out: |
| 735 | return err; |
| 736 | } |
| 737 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 738 | static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 739 | struct inode *new_dir, struct dentry *new_dentry) |
| 740 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 741 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 742 | struct inode *old_inode = d_inode(old_dentry); |
| 743 | struct inode *new_inode = d_inode(new_dentry); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 744 | struct page *old_dir_page, *new_dir_page; |
| 745 | struct page *old_page, *new_page; |
| 746 | struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; |
| 747 | struct f2fs_dir_entry *old_entry, *new_entry; |
| 748 | int old_nlink = 0, new_nlink = 0; |
| 749 | int err = -ENOENT; |
| 750 | |
Chao Yu | d3baf7c | 2015-05-25 18:07:02 +0800 | [diff] [blame] | 751 | if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) && |
| 752 | (old_dir != new_dir) && |
| 753 | (!f2fs_is_child_context_consistent_with_parent(new_dir, |
| 754 | old_inode) || |
| 755 | !f2fs_is_child_context_consistent_with_parent(old_dir, |
| 756 | new_inode))) |
| 757 | return -EPERM; |
| 758 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 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 | 9486ba442 | 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 | 9486ba442 | 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 | 9486ba442 | 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 | 9486ba442 | 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 | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 916 | } |
| 917 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 918 | #ifdef CONFIG_F2FS_FS_ENCRYPTION |
Linus Torvalds | cfcc0ad | 2015-06-24 20:38:29 -0700 | [diff] [blame] | 919 | static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie) |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 920 | { |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 921 | struct page *cpage = NULL; |
| 922 | char *caddr, *paddr = NULL; |
| 923 | struct f2fs_str cstr; |
| 924 | struct f2fs_str pstr = FSTR_INIT(NULL, 0); |
| 925 | struct inode *inode = d_inode(dentry); |
| 926 | struct f2fs_encrypted_symlink_data *sd; |
| 927 | loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1); |
| 928 | u32 max_size = inode->i_sb->s_blocksize; |
| 929 | int res; |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 930 | |
Jaegeuk Kim | 26bf3dc | 2015-05-19 22:26:54 -0700 | [diff] [blame] | 931 | res = f2fs_get_encryption_info(inode); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 932 | if (res) |
| 933 | return ERR_PTR(res); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 934 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 935 | cpage = read_mapping_page(inode->i_mapping, 0, NULL); |
| 936 | if (IS_ERR(cpage)) |
Linus Torvalds | cfcc0ad | 2015-06-24 20:38:29 -0700 | [diff] [blame] | 937 | return ERR_CAST(cpage); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 938 | caddr = kmap(cpage); |
| 939 | caddr[size] = 0; |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 940 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 941 | /* Symlink is encrypted */ |
| 942 | sd = (struct f2fs_encrypted_symlink_data *)caddr; |
| 943 | cstr.name = sd->encrypted_path; |
| 944 | cstr.len = le16_to_cpu(sd->len); |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 945 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 946 | /* this is broken symlink case */ |
| 947 | if (cstr.name[0] == 0 && cstr.len == 0) { |
| 948 | res = -ENOENT; |
| 949 | goto errout; |
| 950 | } |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 951 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 952 | if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) > |
| 953 | max_size) { |
| 954 | /* Symlink data on the disk is corrupted */ |
| 955 | res = -EIO; |
| 956 | goto errout; |
| 957 | } |
| 958 | res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr); |
| 959 | if (res) |
| 960 | goto errout; |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 961 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 962 | res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr); |
| 963 | if (res < 0) |
| 964 | goto errout; |
Chao Yu | 3c0d84d | 2015-02-16 16:20:27 +0800 | [diff] [blame] | 965 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 966 | paddr = pstr.name; |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 967 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 968 | /* Null-terminate the name */ |
| 969 | paddr[res] = '\0'; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 970 | |
| 971 | kunmap(cpage); |
| 972 | page_cache_release(cpage); |
Linus Torvalds | cfcc0ad | 2015-06-24 20:38:29 -0700 | [diff] [blame] | 973 | return *cookie = paddr; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 974 | errout: |
| 975 | f2fs_fname_crypto_free_buffer(&pstr); |
| 976 | kunmap(cpage); |
| 977 | page_cache_release(cpage); |
| 978 | return ERR_PTR(res); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 979 | } |
| 980 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 981 | const struct inode_operations f2fs_encrypted_symlink_inode_operations = { |
| 982 | .readlink = generic_readlink, |
| 983 | .follow_link = f2fs_encrypted_follow_link, |
| 984 | .put_link = kfree_put_link, |
| 985 | .getattr = f2fs_getattr, |
| 986 | .setattr = f2fs_setattr, |
| 987 | .setxattr = generic_setxattr, |
| 988 | .getxattr = generic_getxattr, |
| 989 | .listxattr = f2fs_listxattr, |
| 990 | .removexattr = generic_removexattr, |
| 991 | }; |
| 992 | #endif |
| 993 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 994 | const struct inode_operations f2fs_dir_inode_operations = { |
| 995 | .create = f2fs_create, |
| 996 | .lookup = f2fs_lookup, |
| 997 | .link = f2fs_link, |
| 998 | .unlink = f2fs_unlink, |
| 999 | .symlink = f2fs_symlink, |
| 1000 | .mkdir = f2fs_mkdir, |
| 1001 | .rmdir = f2fs_rmdir, |
| 1002 | .mknod = f2fs_mknod, |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1003 | .rename2 = f2fs_rename2, |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 1004 | .tmpfile = f2fs_tmpfile, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1005 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1006 | .setattr = f2fs_setattr, |
| 1007 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 1008 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1009 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1010 | .setxattr = generic_setxattr, |
| 1011 | .getxattr = generic_getxattr, |
| 1012 | .listxattr = f2fs_listxattr, |
| 1013 | .removexattr = generic_removexattr, |
| 1014 | #endif |
| 1015 | }; |
| 1016 | |
| 1017 | const struct inode_operations f2fs_symlink_inode_operations = { |
| 1018 | .readlink = generic_readlink, |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 1019 | .follow_link = f2fs_follow_link, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1020 | .put_link = page_put_link, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1021 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1022 | .setattr = f2fs_setattr, |
| 1023 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1024 | .setxattr = generic_setxattr, |
| 1025 | .getxattr = generic_getxattr, |
| 1026 | .listxattr = f2fs_listxattr, |
| 1027 | .removexattr = generic_removexattr, |
| 1028 | #endif |
| 1029 | }; |
| 1030 | |
| 1031 | const struct inode_operations f2fs_special_inode_operations = { |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1032 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1033 | .setattr = f2fs_setattr, |
| 1034 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 1035 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1036 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1037 | .setxattr = generic_setxattr, |
| 1038 | .getxattr = generic_getxattr, |
| 1039 | .listxattr = f2fs_listxattr, |
| 1040 | .removexattr = generic_removexattr, |
| 1041 | #endif |
| 1042 | }; |