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; |
Jaegeuk Kim | a21c20f | 2015-08-16 12:38:15 -0700 | [diff] [blame] | 56 | goto fail; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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 | |
Chao Yu | fba48a8 | 2015-12-23 17:51:35 +0800 | [diff] [blame] | 63 | if (test_opt(sbi, INLINE_DATA) && 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 | |
Chao Yu | d5e8f6c | 2015-07-15 17:28:53 +0800 | [diff] [blame] | 70 | stat_inc_inline_xattr(inode); |
Jaegeuk Kim | 2fb2c95 | 2015-04-30 18:58:22 -0700 | [diff] [blame] | 71 | stat_inc_inline_inode(inode); |
| 72 | stat_inc_inline_dir(inode); |
| 73 | |
Jaegeuk Kim | d70b4f5 | 2013-04-25 13:24:33 +0900 | [diff] [blame] | 74 | trace_f2fs_new_inode(inode, 0); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 75 | mark_inode_dirty(inode); |
| 76 | return inode; |
| 77 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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 | if (nid_free) |
Jaegeuk Kim | c9b63bd | 2015-06-23 10:36:08 -0700 | [diff] [blame] | 82 | set_inode_flag(F2FS_I(inode), FI_FREE_NID); |
| 83 | iput(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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 | |
Chao Yu | 741a7be | 2015-07-06 20:30:40 +0800 | [diff] [blame] | 92 | /* |
| 93 | * filename format of multimedia file should be defined as: |
| 94 | * "filename + '.' + extension". |
| 95 | */ |
| 96 | if (slen < sublen + 2) |
| 97 | return 0; |
| 98 | |
| 99 | if (s[slen - sublen - 1] != '.') |
Namjae Jeon | e66509f0 | 2013-04-20 01:27:21 +0900 | [diff] [blame] | 100 | return 0; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 101 | |
Gu Zheng | 749ebfd | 2013-08-22 18:11:25 +0800 | [diff] [blame] | 102 | return !strncasecmp(s + slen - sublen, sub, sublen); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 103 | } |
| 104 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 105 | /* |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 106 | * Set multimedia files as cold files for hot/cold data separation |
| 107 | */ |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 108 | 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] | 109 | const unsigned char *name) |
| 110 | { |
| 111 | int i; |
| 112 | __u8 (*extlist)[8] = sbi->raw_super->extension_list; |
| 113 | |
| 114 | int count = le32_to_cpu(sbi->raw_super->extension_count); |
| 115 | for (i = 0; i < count; i++) { |
Namjae Jeon | e66509f0 | 2013-04-20 01:27:21 +0900 | [diff] [blame] | 116 | if (is_multimedia_file(name, extlist[i])) { |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 117 | file_set_cold(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 118 | break; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
| 124 | bool excl) |
| 125 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 126 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 127 | struct inode *inode; |
| 128 | nid_t ino = 0; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 129 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 130 | |
| 131 | inode = f2fs_new_inode(dir, mode); |
| 132 | if (IS_ERR(inode)) |
| 133 | return PTR_ERR(inode); |
| 134 | |
| 135 | if (!test_opt(sbi, DISABLE_EXT_IDENTIFY)) |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 136 | set_cold_files(sbi, inode, dentry->d_name.name); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 137 | |
| 138 | inode->i_op = &f2fs_file_inode_operations; |
| 139 | inode->i_fop = &f2fs_file_operations; |
| 140 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 141 | ino = inode->i_ino; |
| 142 | |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 143 | f2fs_balance_fs(sbi); |
| 144 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 145 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 146 | err = f2fs_add_link(dentry, inode); |
| 147 | if (err) |
| 148 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 149 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 150 | |
| 151 | alloc_nid_done(sbi, ino); |
| 152 | |
Jaegeuk Kim | 0a364af | 2013-05-16 08:57:43 +0900 | [diff] [blame] | 153 | d_instantiate(dentry, inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 154 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 155 | |
| 156 | if (IS_DIRSYNC(dir)) |
| 157 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 158 | return 0; |
| 159 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 160 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 161 | return err; |
| 162 | } |
| 163 | |
| 164 | static int f2fs_link(struct dentry *old_dentry, struct inode *dir, |
| 165 | struct dentry *dentry) |
| 166 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 167 | struct inode *inode = d_inode(old_dentry); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 168 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 169 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 170 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 171 | if (f2fs_encrypted_inode(dir) && |
| 172 | !f2fs_is_child_context_consistent_with_parent(dir, inode)) |
| 173 | return -EPERM; |
| 174 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 175 | f2fs_balance_fs(sbi); |
| 176 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 177 | inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 6f6fd83 | 2013-05-22 12:06:26 +0900 | [diff] [blame] | 178 | ihold(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 179 | |
| 180 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 181 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 182 | err = f2fs_add_link(dentry, inode); |
| 183 | if (err) |
| 184 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 185 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 186 | |
| 187 | d_instantiate(dentry, inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 188 | |
| 189 | if (IS_DIRSYNC(dir)) |
| 190 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 191 | return 0; |
| 192 | out: |
| 193 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
| 194 | iput(inode); |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 195 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 196 | return err; |
| 197 | } |
| 198 | |
| 199 | struct dentry *f2fs_get_parent(struct dentry *child) |
| 200 | { |
| 201 | struct qstr dotdot = QSTR_INIT("..", 2); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 202 | unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 203 | if (!ino) |
| 204 | return ERR_PTR(-ENOENT); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 205 | return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino)); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 206 | } |
| 207 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 208 | static int __recover_dot_dentries(struct inode *dir, nid_t pino) |
| 209 | { |
| 210 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 211 | struct qstr dot = QSTR_INIT(".", 1); |
| 212 | struct qstr dotdot = QSTR_INIT("..", 2); |
| 213 | struct f2fs_dir_entry *de; |
| 214 | struct page *page; |
| 215 | int err = 0; |
| 216 | |
Chao Yu | d538417 | 2015-12-24 18:03:29 +0800 | [diff] [blame] | 217 | f2fs_balance_fs(sbi); |
| 218 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 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 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 294 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 295 | if (!de) |
| 296 | goto fail; |
| 297 | |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 298 | f2fs_balance_fs(sbi); |
| 299 | |
Jaegeuk Kim | ccaaca2 | 2013-10-08 10:19:28 +0900 | [diff] [blame] | 300 | f2fs_lock_op(sbi); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 301 | err = acquire_orphan_inode(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 302 | if (err) { |
Jaegeuk Kim | ccaaca2 | 2013-10-08 10:19:28 +0900 | [diff] [blame] | 303 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 304 | f2fs_dentry_kunmap(dir, page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 305 | f2fs_put_page(page, 0); |
| 306 | goto fail; |
| 307 | } |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 308 | f2fs_delete_entry(de, page, dir, inode); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 309 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 310 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 311 | /* In order to evict this inode, we set it dirty */ |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 312 | mark_inode_dirty(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 313 | |
| 314 | if (IS_DIRSYNC(dir)) |
| 315 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 316 | fail: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 317 | trace_f2fs_unlink_exit(inode, err); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 318 | return err; |
| 319 | } |
| 320 | |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 321 | static const char *f2fs_follow_link(struct dentry *dentry, void **cookie) |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 322 | { |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 323 | const char *link = page_follow_link_light(dentry, cookie); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 324 | if (!IS_ERR(link) && !*link) { |
| 325 | /* this is broken symlink case */ |
Al Viro | 5f2c417 | 2015-05-07 11:14:26 -0400 | [diff] [blame] | 326 | page_put_link(NULL, *cookie); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 327 | link = ERR_PTR(-ENOENT); |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 328 | } |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 329 | return link; |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 332 | static int f2fs_symlink(struct inode *dir, struct dentry *dentry, |
| 333 | const char *symname) |
| 334 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 335 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 336 | struct inode *inode; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 337 | size_t len = strlen(symname); |
| 338 | size_t p_len; |
| 339 | char *p_str; |
| 340 | struct f2fs_str disk_link = FSTR_INIT(NULL, 0); |
| 341 | struct f2fs_encrypted_symlink_data *sd = NULL; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 342 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 343 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 344 | if (len > dir->i_sb->s_blocksize) |
| 345 | return -ENAMETOOLONG; |
| 346 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 347 | inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 348 | if (IS_ERR(inode)) |
| 349 | return PTR_ERR(inode); |
| 350 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 351 | if (f2fs_encrypted_inode(inode)) |
| 352 | inode->i_op = &f2fs_encrypted_symlink_inode_operations; |
| 353 | else |
| 354 | inode->i_op = &f2fs_symlink_inode_operations; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 355 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 356 | |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 357 | f2fs_balance_fs(sbi); |
| 358 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 359 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 360 | err = f2fs_add_link(dentry, inode); |
| 361 | if (err) |
| 362 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 363 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 364 | alloc_nid_done(sbi, inode->i_ino); |
| 365 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 366 | if (f2fs_encrypted_inode(dir)) { |
| 367 | struct qstr istr = QSTR_INIT(symname, len); |
| 368 | |
Jaegeuk Kim | 26bf3dc | 2015-05-19 22:26:54 -0700 | [diff] [blame] | 369 | err = f2fs_get_encryption_info(inode); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 370 | if (err) |
| 371 | goto err_out; |
| 372 | |
| 373 | err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link); |
| 374 | if (err) |
| 375 | goto err_out; |
| 376 | |
| 377 | err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link); |
| 378 | if (err < 0) |
| 379 | goto err_out; |
| 380 | |
| 381 | p_len = encrypted_symlink_data_len(disk_link.len) + 1; |
| 382 | |
| 383 | if (p_len > dir->i_sb->s_blocksize) { |
| 384 | err = -ENAMETOOLONG; |
| 385 | goto err_out; |
| 386 | } |
| 387 | |
| 388 | sd = kzalloc(p_len, GFP_NOFS); |
| 389 | if (!sd) { |
| 390 | err = -ENOMEM; |
| 391 | goto err_out; |
| 392 | } |
| 393 | memcpy(sd->encrypted_path, disk_link.name, disk_link.len); |
| 394 | sd->len = cpu_to_le16(disk_link.len); |
| 395 | p_str = (char *)sd; |
| 396 | } else { |
| 397 | p_len = len + 1; |
| 398 | p_str = (char *)symname; |
| 399 | } |
| 400 | |
| 401 | err = page_symlink(inode, p_str, p_len); |
| 402 | |
| 403 | err_out: |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 404 | d_instantiate(dentry, inode); |
| 405 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 406 | |
Jaegeuk Kim | d0cae97 | 2015-04-15 13:37:53 -0700 | [diff] [blame] | 407 | /* |
| 408 | * Let's flush symlink data in order to avoid broken symlink as much as |
| 409 | * possible. Nevertheless, fsyncing is the best way, but there is no |
| 410 | * way to get a file descriptor in order to flush that. |
| 411 | * |
| 412 | * Note that, it needs to do dir->fsync to make this recoverable. |
| 413 | * If the symlink path is stored into inline_data, there is no |
| 414 | * performance regression. |
| 415 | */ |
Chao Yu | a6be014 | 2015-10-22 18:23:08 +0800 | [diff] [blame] | 416 | if (!err) { |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 417 | filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1); |
Jaegeuk Kim | d0cae97 | 2015-04-15 13:37:53 -0700 | [diff] [blame] | 418 | |
Chao Yu | a6be014 | 2015-10-22 18:23:08 +0800 | [diff] [blame] | 419 | if (IS_DIRSYNC(dir)) |
| 420 | f2fs_sync_fs(sbi->sb, 1); |
| 421 | } else { |
| 422 | f2fs_unlink(dir, dentry); |
| 423 | } |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 424 | |
| 425 | kfree(sd); |
| 426 | f2fs_fname_crypto_free_buffer(&disk_link); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 427 | return err; |
| 428 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 429 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 430 | return err; |
| 431 | } |
| 432 | |
| 433 | static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 434 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 435 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 436 | struct inode *inode; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 437 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 438 | |
| 439 | inode = f2fs_new_inode(dir, S_IFDIR | mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 440 | if (IS_ERR(inode)) |
Namjae Jeon | 61412b6 | 2012-12-01 10:56:25 +0900 | [diff] [blame] | 441 | return PTR_ERR(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 442 | |
| 443 | inode->i_op = &f2fs_dir_inode_operations; |
| 444 | inode->i_fop = &f2fs_dir_operations; |
| 445 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
Chao Yu | 560d467 | 2015-02-07 17:36:15 +0800 | [diff] [blame] | 446 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 447 | |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 448 | f2fs_balance_fs(sbi); |
| 449 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 450 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 451 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 452 | err = f2fs_add_link(dentry, inode); |
| 453 | if (err) |
| 454 | goto out_fail; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 455 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 456 | |
| 457 | alloc_nid_done(sbi, inode->i_ino); |
| 458 | |
| 459 | d_instantiate(dentry, inode); |
| 460 | unlock_new_inode(inode); |
| 461 | |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 462 | if (IS_DIRSYNC(dir)) |
| 463 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 464 | return 0; |
| 465 | |
| 466 | out_fail: |
| 467 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 468 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 469 | return err; |
| 470 | } |
| 471 | |
| 472 | static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) |
| 473 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 474 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 475 | if (f2fs_empty_dir(inode)) |
| 476 | return f2fs_unlink(dir, dentry); |
| 477 | return -ENOTEMPTY; |
| 478 | } |
| 479 | |
| 480 | static int f2fs_mknod(struct inode *dir, struct dentry *dentry, |
| 481 | umode_t mode, dev_t rdev) |
| 482 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 483 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 484 | struct inode *inode; |
| 485 | int err = 0; |
| 486 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 487 | inode = f2fs_new_inode(dir, mode); |
| 488 | if (IS_ERR(inode)) |
| 489 | return PTR_ERR(inode); |
| 490 | |
| 491 | init_special_inode(inode, inode->i_mode, rdev); |
| 492 | inode->i_op = &f2fs_special_inode_operations; |
| 493 | |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 494 | f2fs_balance_fs(sbi); |
| 495 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 496 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 497 | err = f2fs_add_link(dentry, inode); |
| 498 | if (err) |
| 499 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 500 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 501 | |
| 502 | alloc_nid_done(sbi, inode->i_ino); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 503 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 504 | d_instantiate(dentry, inode); |
| 505 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 506 | |
| 507 | if (IS_DIRSYNC(dir)) |
| 508 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 509 | return 0; |
| 510 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 511 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 512 | return err; |
| 513 | } |
| 514 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 515 | static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry, |
| 516 | umode_t mode, struct inode **whiteout) |
| 517 | { |
| 518 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 519 | struct inode *inode; |
| 520 | int err; |
| 521 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 522 | inode = f2fs_new_inode(dir, mode); |
| 523 | if (IS_ERR(inode)) |
| 524 | return PTR_ERR(inode); |
| 525 | |
| 526 | if (whiteout) { |
| 527 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); |
| 528 | inode->i_op = &f2fs_special_inode_operations; |
| 529 | } else { |
| 530 | inode->i_op = &f2fs_file_inode_operations; |
| 531 | inode->i_fop = &f2fs_file_operations; |
| 532 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 533 | } |
| 534 | |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 535 | f2fs_balance_fs(sbi); |
| 536 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 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 | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 609 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 610 | if (!old_entry) |
| 611 | goto out; |
| 612 | |
| 613 | if (S_ISDIR(old_inode->i_mode)) { |
| 614 | err = -EIO; |
| 615 | old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); |
| 616 | if (!old_dir_entry) |
| 617 | goto out_old; |
| 618 | } |
| 619 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 620 | if (flags & RENAME_WHITEOUT) { |
| 621 | err = f2fs_create_whiteout(old_dir, &whiteout); |
| 622 | if (err) |
| 623 | goto out_dir; |
| 624 | } |
| 625 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 626 | if (new_inode) { |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 627 | |
| 628 | err = -ENOTEMPTY; |
| 629 | if (old_dir_entry && !f2fs_empty_dir(new_inode)) |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 630 | goto out_whiteout; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 631 | |
| 632 | err = -ENOENT; |
| 633 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, |
| 634 | &new_page); |
| 635 | if (!new_entry) |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 636 | goto out_whiteout; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 637 | |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 638 | f2fs_balance_fs(sbi); |
| 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 { |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 671 | f2fs_balance_fs(sbi); |
| 672 | |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 673 | f2fs_lock_op(sbi); |
| 674 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 675 | err = f2fs_add_link(new_dentry, old_inode); |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 676 | if (err) { |
| 677 | f2fs_unlock_op(sbi); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 678 | goto out_whiteout; |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 679 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 680 | |
| 681 | if (old_dir_entry) { |
| 682 | inc_nlink(new_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 683 | update_inode_page(new_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Jaegeuk Kim | b2c0829 | 2014-06-30 18:09:55 +0900 | [diff] [blame] | 687 | down_write(&F2FS_I(old_inode)->i_sem); |
| 688 | file_lost_pino(old_inode); |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 689 | if (new_inode && file_enc_name(new_inode)) |
| 690 | file_set_enc_name(old_inode); |
Jaegeuk Kim | b2c0829 | 2014-06-30 18:09:55 +0900 | [diff] [blame] | 691 | up_write(&F2FS_I(old_inode)->i_sem); |
| 692 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 693 | old_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 694 | mark_inode_dirty(old_inode); |
| 695 | |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 696 | f2fs_delete_entry(old_entry, old_page, old_dir, NULL); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 697 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 698 | if (whiteout) { |
| 699 | whiteout->i_state |= I_LINKABLE; |
| 700 | set_inode_flag(F2FS_I(whiteout), FI_INC_LINK); |
| 701 | err = f2fs_add_link(old_dentry, whiteout); |
| 702 | if (err) |
| 703 | goto put_out_dir; |
| 704 | whiteout->i_state &= ~I_LINKABLE; |
| 705 | iput(whiteout); |
| 706 | } |
| 707 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 708 | if (old_dir_entry) { |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 709 | if (old_dir != new_dir && !whiteout) { |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 710 | f2fs_set_link(old_inode, old_dir_entry, |
| 711 | old_dir_page, new_dir); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 712 | update_inode_page(old_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 713 | } else { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 714 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 715 | f2fs_put_page(old_dir_page, 0); |
| 716 | } |
| 717 | drop_nlink(old_dir); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 718 | mark_inode_dirty(old_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 719 | update_inode_page(old_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 720 | } |
| 721 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 722 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 723 | |
| 724 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 725 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 726 | return 0; |
| 727 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 728 | put_out_dir: |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 729 | f2fs_unlock_op(sbi); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 730 | if (new_page) { |
| 731 | f2fs_dentry_kunmap(new_dir, new_page); |
| 732 | f2fs_put_page(new_page, 0); |
| 733 | } |
| 734 | out_whiteout: |
| 735 | if (whiteout) |
| 736 | iput(whiteout); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 737 | out_dir: |
| 738 | if (old_dir_entry) { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 739 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 740 | f2fs_put_page(old_dir_page, 0); |
| 741 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 742 | out_old: |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 743 | f2fs_dentry_kunmap(old_dir, old_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 744 | f2fs_put_page(old_page, 0); |
| 745 | out: |
| 746 | return err; |
| 747 | } |
| 748 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 749 | static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 750 | struct inode *new_dir, struct dentry *new_dentry) |
| 751 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 752 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 753 | struct inode *old_inode = d_inode(old_dentry); |
| 754 | struct inode *new_inode = d_inode(new_dentry); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 755 | struct page *old_dir_page, *new_dir_page; |
| 756 | struct page *old_page, *new_page; |
| 757 | struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; |
| 758 | struct f2fs_dir_entry *old_entry, *new_entry; |
| 759 | int old_nlink = 0, new_nlink = 0; |
| 760 | int err = -ENOENT; |
| 761 | |
Chao Yu | d3baf7c | 2015-05-25 18:07:02 +0800 | [diff] [blame] | 762 | if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) && |
| 763 | (old_dir != new_dir) && |
| 764 | (!f2fs_is_child_context_consistent_with_parent(new_dir, |
| 765 | old_inode) || |
| 766 | !f2fs_is_child_context_consistent_with_parent(old_dir, |
| 767 | new_inode))) |
| 768 | return -EPERM; |
| 769 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 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 | |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 812 | f2fs_balance_fs(sbi); |
| 813 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 814 | f2fs_lock_op(sbi); |
| 815 | |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 816 | err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 817 | if (err) |
| 818 | goto out_unlock; |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 819 | if (file_enc_name(new_inode)) |
| 820 | file_set_enc_name(old_inode); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 821 | |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 822 | err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 823 | if (err) |
| 824 | goto out_undo; |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 825 | if (file_enc_name(old_inode)) |
| 826 | file_set_enc_name(new_inode); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 827 | |
| 828 | /* update ".." directory entry info of old dentry */ |
| 829 | if (old_dir_entry) |
| 830 | f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir); |
| 831 | |
| 832 | /* update ".." directory entry info of new dentry */ |
| 833 | if (new_dir_entry) |
| 834 | f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir); |
| 835 | |
| 836 | /* update directory entry info of old dir inode */ |
| 837 | f2fs_set_link(old_dir, old_entry, old_page, new_inode); |
| 838 | |
| 839 | down_write(&F2FS_I(old_inode)->i_sem); |
| 840 | file_lost_pino(old_inode); |
| 841 | up_write(&F2FS_I(old_inode)->i_sem); |
| 842 | |
| 843 | update_inode_page(old_inode); |
| 844 | |
| 845 | old_dir->i_ctime = CURRENT_TIME; |
| 846 | if (old_nlink) { |
| 847 | down_write(&F2FS_I(old_dir)->i_sem); |
| 848 | if (old_nlink < 0) |
| 849 | drop_nlink(old_dir); |
| 850 | else |
| 851 | inc_nlink(old_dir); |
| 852 | up_write(&F2FS_I(old_dir)->i_sem); |
| 853 | } |
| 854 | mark_inode_dirty(old_dir); |
| 855 | update_inode_page(old_dir); |
| 856 | |
| 857 | /* update directory entry info of new dir inode */ |
| 858 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 859 | |
| 860 | down_write(&F2FS_I(new_inode)->i_sem); |
| 861 | file_lost_pino(new_inode); |
| 862 | up_write(&F2FS_I(new_inode)->i_sem); |
| 863 | |
| 864 | update_inode_page(new_inode); |
| 865 | |
| 866 | new_dir->i_ctime = CURRENT_TIME; |
| 867 | if (new_nlink) { |
| 868 | down_write(&F2FS_I(new_dir)->i_sem); |
| 869 | if (new_nlink < 0) |
| 870 | drop_nlink(new_dir); |
| 871 | else |
| 872 | inc_nlink(new_dir); |
| 873 | up_write(&F2FS_I(new_dir)->i_sem); |
| 874 | } |
| 875 | mark_inode_dirty(new_dir); |
| 876 | update_inode_page(new_dir); |
| 877 | |
| 878 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 879 | |
| 880 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 881 | f2fs_sync_fs(sbi->sb, 1); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 882 | return 0; |
| 883 | out_undo: |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 884 | /* |
| 885 | * Still we may fail to recover name info of f2fs_inode here |
| 886 | * Drop it, once its name is set as encrypted |
| 887 | */ |
| 888 | update_dent_inode(old_inode, old_inode, &old_dentry->d_name); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 889 | out_unlock: |
| 890 | f2fs_unlock_op(sbi); |
| 891 | out_new_dir: |
| 892 | if (new_dir_entry) { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 893 | f2fs_dentry_kunmap(new_inode, new_dir_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 894 | f2fs_put_page(new_dir_page, 0); |
| 895 | } |
| 896 | out_old_dir: |
| 897 | if (old_dir_entry) { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 898 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 899 | f2fs_put_page(old_dir_page, 0); |
| 900 | } |
| 901 | out_new: |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 902 | f2fs_dentry_kunmap(new_dir, new_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 903 | f2fs_put_page(new_page, 0); |
| 904 | out_old: |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 905 | f2fs_dentry_kunmap(old_dir, old_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 906 | f2fs_put_page(old_page, 0); |
| 907 | out: |
| 908 | return err; |
| 909 | } |
| 910 | |
| 911 | static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 912 | struct inode *new_dir, struct dentry *new_dentry, |
| 913 | unsigned int flags) |
| 914 | { |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 915 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 916 | return -EINVAL; |
| 917 | |
| 918 | if (flags & RENAME_EXCHANGE) { |
| 919 | return f2fs_cross_rename(old_dir, old_dentry, |
| 920 | new_dir, new_dentry); |
| 921 | } |
| 922 | /* |
| 923 | * VFS has already handled the new dentry existence case, |
| 924 | * here, we just deal with "RENAME_NOREPLACE" as regular rename. |
| 925 | */ |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 926 | return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 927 | } |
| 928 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 929 | #ifdef CONFIG_F2FS_FS_ENCRYPTION |
Linus Torvalds | cfcc0ad | 2015-06-24 20:38:29 -0700 | [diff] [blame] | 930 | static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie) |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 931 | { |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 932 | struct page *cpage = NULL; |
| 933 | char *caddr, *paddr = NULL; |
Jaegeuk Kim | 8dc0d6a | 2015-12-24 16:13:09 -0800 | [diff] [blame^] | 934 | struct f2fs_str cstr = FSTR_INIT(NULL, 0); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 935 | struct f2fs_str pstr = FSTR_INIT(NULL, 0); |
| 936 | struct inode *inode = d_inode(dentry); |
| 937 | struct f2fs_encrypted_symlink_data *sd; |
| 938 | loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1); |
| 939 | u32 max_size = inode->i_sb->s_blocksize; |
| 940 | int res; |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 941 | |
Jaegeuk Kim | 26bf3dc | 2015-05-19 22:26:54 -0700 | [diff] [blame] | 942 | res = f2fs_get_encryption_info(inode); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 943 | if (res) |
| 944 | return ERR_PTR(res); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 945 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 946 | cpage = read_mapping_page(inode->i_mapping, 0, NULL); |
| 947 | if (IS_ERR(cpage)) |
Linus Torvalds | cfcc0ad | 2015-06-24 20:38:29 -0700 | [diff] [blame] | 948 | return ERR_CAST(cpage); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 949 | caddr = kmap(cpage); |
| 950 | caddr[size] = 0; |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 951 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 952 | /* Symlink is encrypted */ |
| 953 | sd = (struct f2fs_encrypted_symlink_data *)caddr; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 954 | cstr.len = le16_to_cpu(sd->len); |
Jaegeuk Kim | 8dc0d6a | 2015-12-24 16:13:09 -0800 | [diff] [blame^] | 955 | |
| 956 | /* this is broken symlink case */ |
| 957 | if (unlikely(cstr.len == 0)) { |
| 958 | res = -ENOENT; |
| 959 | goto errout; |
| 960 | } |
Jaegeuk Kim | 569cf18 | 2015-09-03 13:38:23 -0700 | [diff] [blame] | 961 | cstr.name = kmalloc(cstr.len, GFP_NOFS); |
| 962 | if (!cstr.name) { |
| 963 | res = -ENOMEM; |
| 964 | goto errout; |
| 965 | } |
| 966 | memcpy(cstr.name, sd->encrypted_path, cstr.len); |
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 | /* this is broken symlink case */ |
Jaegeuk Kim | 8dc0d6a | 2015-12-24 16:13:09 -0800 | [diff] [blame^] | 969 | if (unlikely(cstr.name[0] == 0)) { |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 970 | res = -ENOENT; |
| 971 | goto errout; |
| 972 | } |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 973 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 974 | if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) > |
| 975 | max_size) { |
| 976 | /* Symlink data on the disk is corrupted */ |
| 977 | res = -EIO; |
| 978 | goto errout; |
| 979 | } |
| 980 | res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr); |
| 981 | if (res) |
| 982 | goto errout; |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 983 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 984 | res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr); |
| 985 | if (res < 0) |
| 986 | goto errout; |
Chao Yu | 3c0d84d | 2015-02-16 16:20:27 +0800 | [diff] [blame] | 987 | |
Jaegeuk Kim | 569cf18 | 2015-09-03 13:38:23 -0700 | [diff] [blame] | 988 | kfree(cstr.name); |
| 989 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 990 | paddr = pstr.name; |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 991 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 992 | /* Null-terminate the name */ |
| 993 | paddr[res] = '\0'; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 994 | |
| 995 | kunmap(cpage); |
| 996 | page_cache_release(cpage); |
Linus Torvalds | cfcc0ad | 2015-06-24 20:38:29 -0700 | [diff] [blame] | 997 | return *cookie = paddr; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 998 | errout: |
Jaegeuk Kim | 569cf18 | 2015-09-03 13:38:23 -0700 | [diff] [blame] | 999 | kfree(cstr.name); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 1000 | f2fs_fname_crypto_free_buffer(&pstr); |
| 1001 | kunmap(cpage); |
| 1002 | page_cache_release(cpage); |
| 1003 | return ERR_PTR(res); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 1006 | const struct inode_operations f2fs_encrypted_symlink_inode_operations = { |
| 1007 | .readlink = generic_readlink, |
| 1008 | .follow_link = f2fs_encrypted_follow_link, |
| 1009 | .put_link = kfree_put_link, |
| 1010 | .getattr = f2fs_getattr, |
| 1011 | .setattr = f2fs_setattr, |
| 1012 | .setxattr = generic_setxattr, |
| 1013 | .getxattr = generic_getxattr, |
| 1014 | .listxattr = f2fs_listxattr, |
| 1015 | .removexattr = generic_removexattr, |
| 1016 | }; |
| 1017 | #endif |
| 1018 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1019 | const struct inode_operations f2fs_dir_inode_operations = { |
| 1020 | .create = f2fs_create, |
| 1021 | .lookup = f2fs_lookup, |
| 1022 | .link = f2fs_link, |
| 1023 | .unlink = f2fs_unlink, |
| 1024 | .symlink = f2fs_symlink, |
| 1025 | .mkdir = f2fs_mkdir, |
| 1026 | .rmdir = f2fs_rmdir, |
| 1027 | .mknod = f2fs_mknod, |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1028 | .rename2 = f2fs_rename2, |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 1029 | .tmpfile = f2fs_tmpfile, |
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 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 1033 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1034 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1035 | .setxattr = generic_setxattr, |
| 1036 | .getxattr = generic_getxattr, |
| 1037 | .listxattr = f2fs_listxattr, |
| 1038 | .removexattr = generic_removexattr, |
| 1039 | #endif |
| 1040 | }; |
| 1041 | |
| 1042 | const struct inode_operations f2fs_symlink_inode_operations = { |
| 1043 | .readlink = generic_readlink, |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 1044 | .follow_link = f2fs_follow_link, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1045 | .put_link = page_put_link, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1046 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1047 | .setattr = f2fs_setattr, |
| 1048 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1049 | .setxattr = generic_setxattr, |
| 1050 | .getxattr = generic_getxattr, |
| 1051 | .listxattr = f2fs_listxattr, |
| 1052 | .removexattr = generic_removexattr, |
| 1053 | #endif |
| 1054 | }; |
| 1055 | |
| 1056 | const struct inode_operations f2fs_special_inode_operations = { |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1057 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1058 | .setattr = f2fs_setattr, |
| 1059 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 1060 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1061 | #ifdef CONFIG_F2FS_FS_XATTR |
| 1062 | .setxattr = generic_setxattr, |
| 1063 | .getxattr = generic_getxattr, |
| 1064 | .listxattr = f2fs_listxattr, |
| 1065 | .removexattr = generic_removexattr, |
| 1066 | #endif |
| 1067 | }; |