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