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