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