Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 2 | * fs/f2fs/namei.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/ctype.h> |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 16 | #include <linux/dcache.h> |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 17 | #include <linux/namei.h> |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 18 | |
| 19 | #include "f2fs.h" |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 20 | #include "node.h" |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 21 | #include "xattr.h" |
| 22 | #include "acl.h" |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 23 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 24 | |
| 25 | static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode) |
| 26 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 27 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 28 | nid_t ino; |
| 29 | struct inode *inode; |
| 30 | bool nid_free = false; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 31 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 32 | |
Jaegeuk Kim | a014e03 | 2014-06-20 21:44:02 -0700 | [diff] [blame] | 33 | inode = new_inode(dir->i_sb); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 34 | if (!inode) |
| 35 | return ERR_PTR(-ENOMEM); |
| 36 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 37 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 38 | if (!alloc_nid(sbi, &ino)) { |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 39 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 40 | err = -ENOSPC; |
| 41 | goto fail; |
| 42 | } |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 43 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 44 | |
Chao Yu | 70ff5df | 2014-05-08 17:09:30 +0800 | [diff] [blame] | 45 | inode_init_owner(inode, dir, mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 46 | |
| 47 | inode->i_ino = ino; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 48 | inode->i_blocks = 0; |
| 49 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 50 | inode->i_generation = sbi->s_next_generation++; |
| 51 | |
| 52 | err = insert_inode_locked(inode); |
| 53 | if (err) { |
| 54 | err = -EINVAL; |
| 55 | nid_free = true; |
| 56 | goto out; |
| 57 | } |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 58 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 59 | if (f2fs_may_inline(inode)) |
| 60 | set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 61 | if (test_opt(sbi, INLINE_DENTRY) && S_ISDIR(inode->i_mode)) |
| 62 | set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY); |
| 63 | |
Jaegeuk Kim | d70b4f5 | 2013-04-25 13:24:33 +0900 | [diff] [blame] | 64 | trace_f2fs_new_inode(inode, 0); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 65 | mark_inode_dirty(inode); |
| 66 | return inode; |
| 67 | |
| 68 | out: |
| 69 | clear_nlink(inode); |
| 70 | unlock_new_inode(inode); |
| 71 | fail: |
Jaegeuk Kim | d70b4f5 | 2013-04-25 13:24:33 +0900 | [diff] [blame] | 72 | trace_f2fs_new_inode(inode, err); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 73 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 74 | iput(inode); |
| 75 | if (nid_free) |
| 76 | alloc_nid_failed(sbi, ino); |
| 77 | return ERR_PTR(err); |
| 78 | } |
| 79 | |
| 80 | static int is_multimedia_file(const unsigned char *s, const char *sub) |
| 81 | { |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 82 | size_t slen = strlen(s); |
| 83 | size_t sublen = strlen(sub); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 84 | |
| 85 | if (sublen > slen) |
Namjae Jeon | e66509f0 | 2013-04-20 01:27:21 +0900 | [diff] [blame] | 86 | return 0; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 87 | |
Gu Zheng | 749ebfd | 2013-08-22 18:11:25 +0800 | [diff] [blame] | 88 | return !strncasecmp(s + slen - sublen, sub, sublen); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 91 | /* |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 92 | * Set multimedia files as cold files for hot/cold data separation |
| 93 | */ |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 94 | 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] | 95 | const unsigned char *name) |
| 96 | { |
| 97 | int i; |
| 98 | __u8 (*extlist)[8] = sbi->raw_super->extension_list; |
| 99 | |
| 100 | int count = le32_to_cpu(sbi->raw_super->extension_count); |
| 101 | for (i = 0; i < count; i++) { |
Namjae Jeon | e66509f0 | 2013-04-20 01:27:21 +0900 | [diff] [blame] | 102 | if (is_multimedia_file(name, extlist[i])) { |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 103 | file_set_cold(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
| 110 | bool excl) |
| 111 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 112 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 113 | struct inode *inode; |
| 114 | nid_t ino = 0; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 115 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 116 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 117 | f2fs_balance_fs(sbi); |
| 118 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 119 | inode = f2fs_new_inode(dir, mode); |
| 120 | if (IS_ERR(inode)) |
| 121 | return PTR_ERR(inode); |
| 122 | |
| 123 | if (!test_opt(sbi, DISABLE_EXT_IDENTIFY)) |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 124 | set_cold_files(sbi, inode, dentry->d_name.name); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 125 | |
| 126 | inode->i_op = &f2fs_file_inode_operations; |
| 127 | inode->i_fop = &f2fs_file_operations; |
| 128 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 129 | ino = inode->i_ino; |
| 130 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 131 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 132 | err = f2fs_add_link(dentry, inode); |
| 133 | if (err) |
| 134 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 135 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 136 | |
| 137 | alloc_nid_done(sbi, ino); |
| 138 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 139 | stat_inc_inline_inode(inode); |
Jaegeuk Kim | 0a364af | 2013-05-16 08:57:43 +0900 | [diff] [blame] | 140 | d_instantiate(dentry, inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 141 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 142 | |
| 143 | if (IS_DIRSYNC(dir)) |
| 144 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 145 | return 0; |
| 146 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 147 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 148 | return err; |
| 149 | } |
| 150 | |
| 151 | static int f2fs_link(struct dentry *old_dentry, struct inode *dir, |
| 152 | struct dentry *dentry) |
| 153 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 154 | struct inode *inode = d_inode(old_dentry); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 155 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 156 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 157 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 158 | f2fs_balance_fs(sbi); |
| 159 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 160 | inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 6f6fd83 | 2013-05-22 12:06:26 +0900 | [diff] [blame] | 161 | ihold(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 162 | |
| 163 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 164 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 165 | err = f2fs_add_link(dentry, inode); |
| 166 | if (err) |
| 167 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 168 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 169 | |
| 170 | d_instantiate(dentry, inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 171 | |
| 172 | if (IS_DIRSYNC(dir)) |
| 173 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 174 | return 0; |
| 175 | out: |
| 176 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
| 177 | iput(inode); |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 178 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 179 | return err; |
| 180 | } |
| 181 | |
| 182 | struct dentry *f2fs_get_parent(struct dentry *child) |
| 183 | { |
| 184 | struct qstr dotdot = QSTR_INIT("..", 2); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 185 | unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 186 | if (!ino) |
| 187 | return ERR_PTR(-ENOENT); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 188 | return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino)); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 189 | } |
| 190 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 191 | static int __recover_dot_dentries(struct inode *dir, nid_t pino) |
| 192 | { |
| 193 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 194 | struct qstr dot = QSTR_INIT(".", 1); |
| 195 | struct qstr dotdot = QSTR_INIT("..", 2); |
| 196 | struct f2fs_dir_entry *de; |
| 197 | struct page *page; |
| 198 | int err = 0; |
| 199 | |
| 200 | f2fs_lock_op(sbi); |
| 201 | |
| 202 | de = f2fs_find_entry(dir, &dot, &page); |
| 203 | if (de) { |
| 204 | f2fs_dentry_kunmap(dir, page); |
| 205 | f2fs_put_page(page, 0); |
| 206 | } else { |
| 207 | err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR); |
| 208 | if (err) |
| 209 | goto out; |
| 210 | } |
| 211 | |
| 212 | de = f2fs_find_entry(dir, &dotdot, &page); |
| 213 | if (de) { |
| 214 | f2fs_dentry_kunmap(dir, page); |
| 215 | f2fs_put_page(page, 0); |
| 216 | } else { |
| 217 | err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR); |
| 218 | } |
| 219 | out: |
| 220 | if (!err) { |
| 221 | clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS); |
| 222 | mark_inode_dirty(dir); |
| 223 | } |
| 224 | |
| 225 | f2fs_unlock_op(sbi); |
| 226 | return err; |
| 227 | } |
| 228 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 229 | static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, |
| 230 | unsigned int flags) |
| 231 | { |
| 232 | struct inode *inode = NULL; |
| 233 | struct f2fs_dir_entry *de; |
| 234 | struct page *page; |
| 235 | |
Jaegeuk Kim | 5a20d33 | 2013-03-03 13:58:05 +0900 | [diff] [blame] | 236 | if (dentry->d_name.len > F2FS_NAME_LEN) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 237 | return ERR_PTR(-ENAMETOOLONG); |
| 238 | |
| 239 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 240 | if (de) { |
| 241 | nid_t ino = le32_to_cpu(de->ino); |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 242 | f2fs_dentry_kunmap(dir, page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 243 | f2fs_put_page(page, 0); |
| 244 | |
| 245 | inode = f2fs_iget(dir->i_sb, ino); |
| 246 | if (IS_ERR(inode)) |
| 247 | return ERR_CAST(inode); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 248 | |
| 249 | if (f2fs_has_inline_dots(inode)) { |
| 250 | int err; |
| 251 | |
| 252 | err = __recover_dot_dentries(inode, dir->i_ino); |
| 253 | if (err) { |
| 254 | iget_failed(inode); |
| 255 | return ERR_PTR(err); |
| 256 | } |
| 257 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return d_splice_alias(inode, dentry); |
| 261 | } |
| 262 | |
| 263 | static int f2fs_unlink(struct inode *dir, struct dentry *dentry) |
| 264 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 265 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 266 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 267 | struct f2fs_dir_entry *de; |
| 268 | struct page *page; |
| 269 | int err = -ENOENT; |
| 270 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 271 | trace_f2fs_unlink_enter(dir, dentry); |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 272 | f2fs_balance_fs(sbi); |
| 273 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 274 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 275 | if (!de) |
| 276 | goto fail; |
| 277 | |
Jaegeuk Kim | ccaaca2 | 2013-10-08 10:19:28 +0900 | [diff] [blame] | 278 | f2fs_lock_op(sbi); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 279 | err = acquire_orphan_inode(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 280 | if (err) { |
Jaegeuk Kim | ccaaca2 | 2013-10-08 10:19:28 +0900 | [diff] [blame] | 281 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 282 | f2fs_dentry_kunmap(dir, page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 283 | f2fs_put_page(page, 0); |
| 284 | goto fail; |
| 285 | } |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 286 | f2fs_delete_entry(de, page, dir, inode); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 287 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 288 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 289 | /* In order to evict this inode, we set it dirty */ |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 290 | mark_inode_dirty(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 291 | |
| 292 | if (IS_DIRSYNC(dir)) |
| 293 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 294 | fail: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 295 | trace_f2fs_unlink_exit(inode, err); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 296 | return err; |
| 297 | } |
| 298 | |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 299 | static void *f2fs_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 300 | { |
Jaegeuk Kim | 7263b1b | 2015-04-22 11:03:48 -0700 | [diff] [blame] | 301 | struct page *page = page_follow_link_light(dentry, nd); |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 302 | |
Jaegeuk Kim | 7263b1b | 2015-04-22 11:03:48 -0700 | [diff] [blame] | 303 | if (IS_ERR_OR_NULL(page)) |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 304 | return page; |
| 305 | |
| 306 | /* this is broken symlink case */ |
| 307 | if (*nd_get_link(nd) == 0) { |
Jaegeuk Kim | 7263b1b | 2015-04-22 11:03:48 -0700 | [diff] [blame] | 308 | page_put_link(dentry, nd, page); |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 309 | return ERR_PTR(-ENOENT); |
| 310 | } |
| 311 | return page; |
| 312 | } |
| 313 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 314 | static int f2fs_symlink(struct inode *dir, struct dentry *dentry, |
| 315 | const char *symname) |
| 316 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 317 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 318 | struct inode *inode; |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 319 | size_t symlen = strlen(symname) + 1; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 320 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 321 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 322 | f2fs_balance_fs(sbi); |
| 323 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 324 | inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 325 | if (IS_ERR(inode)) |
| 326 | return PTR_ERR(inode); |
| 327 | |
| 328 | inode->i_op = &f2fs_symlink_inode_operations; |
| 329 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 330 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 331 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 332 | err = f2fs_add_link(dentry, inode); |
| 333 | if (err) |
| 334 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 335 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 336 | |
| 337 | err = page_symlink(inode, symname, symlen); |
| 338 | alloc_nid_done(sbi, inode->i_ino); |
| 339 | |
| 340 | d_instantiate(dentry, inode); |
| 341 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 342 | |
Jaegeuk Kim | d0cae97 | 2015-04-15 13:37:53 -0700 | [diff] [blame] | 343 | /* |
| 344 | * Let's flush symlink data in order to avoid broken symlink as much as |
| 345 | * possible. Nevertheless, fsyncing is the best way, but there is no |
| 346 | * way to get a file descriptor in order to flush that. |
| 347 | * |
| 348 | * Note that, it needs to do dir->fsync to make this recoverable. |
| 349 | * If the symlink path is stored into inline_data, there is no |
| 350 | * performance regression. |
| 351 | */ |
| 352 | filemap_write_and_wait_range(inode->i_mapping, 0, symlen - 1); |
| 353 | |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 354 | if (IS_DIRSYNC(dir)) |
| 355 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 356 | return err; |
| 357 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 358 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 359 | return err; |
| 360 | } |
| 361 | |
| 362 | static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 363 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 364 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 365 | struct inode *inode; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 366 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 367 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 368 | f2fs_balance_fs(sbi); |
| 369 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 370 | inode = f2fs_new_inode(dir, S_IFDIR | mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 371 | if (IS_ERR(inode)) |
Namjae Jeon | 61412b6 | 2012-12-01 10:56:25 +0900 | [diff] [blame] | 372 | return PTR_ERR(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 373 | |
| 374 | inode->i_op = &f2fs_dir_inode_operations; |
| 375 | inode->i_fop = &f2fs_dir_operations; |
| 376 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
Chao Yu | 560d467 | 2015-02-07 17:36:15 +0800 | [diff] [blame] | 377 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 378 | |
| 379 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 380 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 381 | err = f2fs_add_link(dentry, inode); |
| 382 | if (err) |
| 383 | goto out_fail; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 384 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 385 | |
Jaegeuk Kim | 3289c06 | 2014-10-13 20:00:16 -0700 | [diff] [blame] | 386 | stat_inc_inline_dir(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 387 | alloc_nid_done(sbi, inode->i_ino); |
| 388 | |
| 389 | d_instantiate(dentry, inode); |
| 390 | unlock_new_inode(inode); |
| 391 | |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 392 | if (IS_DIRSYNC(dir)) |
| 393 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 394 | return 0; |
| 395 | |
| 396 | out_fail: |
| 397 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 398 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 399 | return err; |
| 400 | } |
| 401 | |
| 402 | static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) |
| 403 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 404 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 405 | if (f2fs_empty_dir(inode)) |
| 406 | return f2fs_unlink(dir, dentry); |
| 407 | return -ENOTEMPTY; |
| 408 | } |
| 409 | |
| 410 | static int f2fs_mknod(struct inode *dir, struct dentry *dentry, |
| 411 | umode_t mode, dev_t rdev) |
| 412 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 413 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 414 | struct inode *inode; |
| 415 | int err = 0; |
| 416 | |
| 417 | if (!new_valid_dev(rdev)) |
| 418 | return -EINVAL; |
| 419 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 420 | f2fs_balance_fs(sbi); |
| 421 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 422 | inode = f2fs_new_inode(dir, mode); |
| 423 | if (IS_ERR(inode)) |
| 424 | return PTR_ERR(inode); |
| 425 | |
| 426 | init_special_inode(inode, inode->i_mode, rdev); |
| 427 | inode->i_op = &f2fs_special_inode_operations; |
| 428 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 429 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 430 | err = f2fs_add_link(dentry, inode); |
| 431 | if (err) |
| 432 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 433 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 434 | |
| 435 | alloc_nid_done(sbi, inode->i_ino); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 436 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 437 | d_instantiate(dentry, inode); |
| 438 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 439 | |
| 440 | if (IS_DIRSYNC(dir)) |
| 441 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 442 | return 0; |
| 443 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 444 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 445 | return err; |
| 446 | } |
| 447 | |
| 448 | static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 449 | struct inode *new_dir, struct dentry *new_dentry) |
| 450 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 451 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 452 | struct inode *old_inode = d_inode(old_dentry); |
| 453 | struct inode *new_inode = d_inode(new_dentry); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 454 | struct page *old_dir_page; |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 455 | struct page *old_page, *new_page; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 456 | struct f2fs_dir_entry *old_dir_entry = NULL; |
| 457 | struct f2fs_dir_entry *old_entry; |
| 458 | struct f2fs_dir_entry *new_entry; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 459 | int err = -ENOENT; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 460 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 461 | f2fs_balance_fs(sbi); |
| 462 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 463 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 464 | if (!old_entry) |
| 465 | goto out; |
| 466 | |
| 467 | if (S_ISDIR(old_inode->i_mode)) { |
| 468 | err = -EIO; |
| 469 | old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); |
| 470 | if (!old_dir_entry) |
| 471 | goto out_old; |
| 472 | } |
| 473 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 474 | if (new_inode) { |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 475 | |
| 476 | err = -ENOTEMPTY; |
| 477 | if (old_dir_entry && !f2fs_empty_dir(new_inode)) |
| 478 | goto out_dir; |
| 479 | |
| 480 | err = -ENOENT; |
| 481 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, |
| 482 | &new_page); |
| 483 | if (!new_entry) |
| 484 | goto out_dir; |
| 485 | |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 486 | f2fs_lock_op(sbi); |
| 487 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 488 | err = acquire_orphan_inode(sbi); |
| 489 | if (err) |
| 490 | goto put_out_dir; |
| 491 | |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 492 | if (update_dent_inode(old_inode, &new_dentry->d_name)) { |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 493 | release_orphan_inode(sbi); |
| 494 | goto put_out_dir; |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 495 | } |
| 496 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 497 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 498 | |
| 499 | new_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 500 | down_write(&F2FS_I(new_inode)->i_sem); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 501 | if (old_dir_entry) |
| 502 | drop_nlink(new_inode); |
| 503 | drop_nlink(new_inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 504 | up_write(&F2FS_I(new_inode)->i_sem); |
| 505 | |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 506 | mark_inode_dirty(new_inode); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 507 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 508 | if (!new_inode->i_nlink) |
| 509 | add_orphan_inode(sbi, new_inode->i_ino); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 510 | else |
| 511 | release_orphan_inode(sbi); |
| 512 | |
Jaegeuk Kim | 8cb8268 | 2013-08-26 21:25:31 +0900 | [diff] [blame] | 513 | update_inode_page(old_inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 514 | update_inode_page(new_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 515 | } else { |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 516 | f2fs_lock_op(sbi); |
| 517 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 518 | err = f2fs_add_link(new_dentry, old_inode); |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 519 | if (err) { |
| 520 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 521 | goto out_dir; |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 522 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 523 | |
| 524 | if (old_dir_entry) { |
| 525 | inc_nlink(new_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 526 | update_inode_page(new_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Jaegeuk Kim | b2c0829 | 2014-06-30 18:09:55 +0900 | [diff] [blame] | 530 | down_write(&F2FS_I(old_inode)->i_sem); |
| 531 | file_lost_pino(old_inode); |
| 532 | up_write(&F2FS_I(old_inode)->i_sem); |
| 533 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 534 | old_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 535 | mark_inode_dirty(old_inode); |
| 536 | |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 537 | f2fs_delete_entry(old_entry, old_page, old_dir, NULL); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 538 | |
| 539 | if (old_dir_entry) { |
| 540 | if (old_dir != new_dir) { |
| 541 | f2fs_set_link(old_inode, old_dir_entry, |
| 542 | old_dir_page, new_dir); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 543 | update_inode_page(old_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 544 | } else { |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 545 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 546 | f2fs_put_page(old_dir_page, 0); |
| 547 | } |
| 548 | drop_nlink(old_dir); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 549 | mark_inode_dirty(old_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 550 | update_inode_page(old_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 551 | } |
| 552 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 553 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 554 | |
| 555 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 556 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 557 | return 0; |
| 558 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 559 | put_out_dir: |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 560 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 561 | f2fs_dentry_kunmap(new_dir, new_page); |
Chao Yu | dd4d961 | 2014-06-24 14:13:13 +0800 | [diff] [blame] | 562 | f2fs_put_page(new_page, 0); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 563 | out_dir: |
| 564 | if (old_dir_entry) { |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 565 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 566 | f2fs_put_page(old_dir_page, 0); |
| 567 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 568 | out_old: |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 569 | f2fs_dentry_kunmap(old_dir, old_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 570 | f2fs_put_page(old_page, 0); |
| 571 | out: |
| 572 | return err; |
| 573 | } |
| 574 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 575 | static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 576 | struct inode *new_dir, struct dentry *new_dentry) |
| 577 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 578 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 579 | struct inode *old_inode = d_inode(old_dentry); |
| 580 | struct inode *new_inode = d_inode(new_dentry); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 581 | struct page *old_dir_page, *new_dir_page; |
| 582 | struct page *old_page, *new_page; |
| 583 | struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; |
| 584 | struct f2fs_dir_entry *old_entry, *new_entry; |
| 585 | int old_nlink = 0, new_nlink = 0; |
| 586 | int err = -ENOENT; |
| 587 | |
| 588 | f2fs_balance_fs(sbi); |
| 589 | |
| 590 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 591 | if (!old_entry) |
| 592 | goto out; |
| 593 | |
| 594 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page); |
| 595 | if (!new_entry) |
| 596 | goto out_old; |
| 597 | |
| 598 | /* prepare for updating ".." directory entry info later */ |
| 599 | if (old_dir != new_dir) { |
| 600 | if (S_ISDIR(old_inode->i_mode)) { |
| 601 | err = -EIO; |
| 602 | old_dir_entry = f2fs_parent_dir(old_inode, |
| 603 | &old_dir_page); |
| 604 | if (!old_dir_entry) |
| 605 | goto out_new; |
| 606 | } |
| 607 | |
| 608 | if (S_ISDIR(new_inode->i_mode)) { |
| 609 | err = -EIO; |
| 610 | new_dir_entry = f2fs_parent_dir(new_inode, |
| 611 | &new_dir_page); |
| 612 | if (!new_dir_entry) |
| 613 | goto out_old_dir; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * If cross rename between file and directory those are not |
| 619 | * in the same directory, we will inc nlink of file's parent |
| 620 | * later, so we should check upper boundary of its nlink. |
| 621 | */ |
| 622 | if ((!old_dir_entry || !new_dir_entry) && |
| 623 | old_dir_entry != new_dir_entry) { |
| 624 | old_nlink = old_dir_entry ? -1 : 1; |
| 625 | new_nlink = -old_nlink; |
| 626 | err = -EMLINK; |
| 627 | if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) || |
| 628 | (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX)) |
| 629 | goto out_new_dir; |
| 630 | } |
| 631 | |
| 632 | f2fs_lock_op(sbi); |
| 633 | |
| 634 | err = update_dent_inode(old_inode, &new_dentry->d_name); |
| 635 | if (err) |
| 636 | goto out_unlock; |
| 637 | |
| 638 | err = update_dent_inode(new_inode, &old_dentry->d_name); |
| 639 | if (err) |
| 640 | goto out_undo; |
| 641 | |
| 642 | /* update ".." directory entry info of old dentry */ |
| 643 | if (old_dir_entry) |
| 644 | f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir); |
| 645 | |
| 646 | /* update ".." directory entry info of new dentry */ |
| 647 | if (new_dir_entry) |
| 648 | f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir); |
| 649 | |
| 650 | /* update directory entry info of old dir inode */ |
| 651 | f2fs_set_link(old_dir, old_entry, old_page, new_inode); |
| 652 | |
| 653 | down_write(&F2FS_I(old_inode)->i_sem); |
| 654 | file_lost_pino(old_inode); |
| 655 | up_write(&F2FS_I(old_inode)->i_sem); |
| 656 | |
| 657 | update_inode_page(old_inode); |
| 658 | |
| 659 | old_dir->i_ctime = CURRENT_TIME; |
| 660 | if (old_nlink) { |
| 661 | down_write(&F2FS_I(old_dir)->i_sem); |
| 662 | if (old_nlink < 0) |
| 663 | drop_nlink(old_dir); |
| 664 | else |
| 665 | inc_nlink(old_dir); |
| 666 | up_write(&F2FS_I(old_dir)->i_sem); |
| 667 | } |
| 668 | mark_inode_dirty(old_dir); |
| 669 | update_inode_page(old_dir); |
| 670 | |
| 671 | /* update directory entry info of new dir inode */ |
| 672 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 673 | |
| 674 | down_write(&F2FS_I(new_inode)->i_sem); |
| 675 | file_lost_pino(new_inode); |
| 676 | up_write(&F2FS_I(new_inode)->i_sem); |
| 677 | |
| 678 | update_inode_page(new_inode); |
| 679 | |
| 680 | new_dir->i_ctime = CURRENT_TIME; |
| 681 | if (new_nlink) { |
| 682 | down_write(&F2FS_I(new_dir)->i_sem); |
| 683 | if (new_nlink < 0) |
| 684 | drop_nlink(new_dir); |
| 685 | else |
| 686 | inc_nlink(new_dir); |
| 687 | up_write(&F2FS_I(new_dir)->i_sem); |
| 688 | } |
| 689 | mark_inode_dirty(new_dir); |
| 690 | update_inode_page(new_dir); |
| 691 | |
| 692 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 693 | |
| 694 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 695 | f2fs_sync_fs(sbi->sb, 1); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 696 | return 0; |
| 697 | out_undo: |
| 698 | /* Still we may fail to recover name info of f2fs_inode here */ |
| 699 | update_dent_inode(old_inode, &old_dentry->d_name); |
| 700 | out_unlock: |
| 701 | f2fs_unlock_op(sbi); |
| 702 | out_new_dir: |
| 703 | if (new_dir_entry) { |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 704 | f2fs_dentry_kunmap(new_inode, new_dir_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 705 | f2fs_put_page(new_dir_page, 0); |
| 706 | } |
| 707 | out_old_dir: |
| 708 | if (old_dir_entry) { |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 709 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 710 | f2fs_put_page(old_dir_page, 0); |
| 711 | } |
| 712 | out_new: |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 713 | f2fs_dentry_kunmap(new_dir, new_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 714 | f2fs_put_page(new_page, 0); |
| 715 | out_old: |
Jaegeuk Kim | 9486ba442 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 716 | f2fs_dentry_kunmap(old_dir, old_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 717 | f2fs_put_page(old_page, 0); |
| 718 | out: |
| 719 | return err; |
| 720 | } |
| 721 | |
| 722 | static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 723 | struct inode *new_dir, struct dentry *new_dentry, |
| 724 | unsigned int flags) |
| 725 | { |
| 726 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) |
| 727 | return -EINVAL; |
| 728 | |
| 729 | if (flags & RENAME_EXCHANGE) { |
| 730 | return f2fs_cross_rename(old_dir, old_dentry, |
| 731 | new_dir, new_dentry); |
| 732 | } |
| 733 | /* |
| 734 | * VFS has already handled the new dentry existence case, |
| 735 | * here, we just deal with "RENAME_NOREPLACE" as regular rename. |
| 736 | */ |
| 737 | return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 738 | } |
| 739 | |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 740 | static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 741 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 742 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 743 | struct inode *inode; |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 744 | int err; |
| 745 | |
| 746 | inode = f2fs_new_inode(dir, mode); |
| 747 | if (IS_ERR(inode)) |
| 748 | return PTR_ERR(inode); |
| 749 | |
| 750 | inode->i_op = &f2fs_file_inode_operations; |
| 751 | inode->i_fop = &f2fs_file_operations; |
| 752 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 753 | |
| 754 | f2fs_lock_op(sbi); |
| 755 | err = acquire_orphan_inode(sbi); |
| 756 | if (err) |
| 757 | goto out; |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 758 | |
| 759 | err = f2fs_do_tmpfile(inode, dir); |
| 760 | if (err) |
| 761 | goto release_out; |
| 762 | |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 763 | /* |
| 764 | * add this non-linked tmpfile to orphan list, in this way we could |
| 765 | * remove all unused data of tmpfile after abnormal power-off. |
| 766 | */ |
| 767 | add_orphan_inode(sbi, inode->i_ino); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 768 | f2fs_unlock_op(sbi); |
| 769 | |
| 770 | alloc_nid_done(sbi, inode->i_ino); |
Chao Yu | 3c0d84d | 2015-02-16 16:20:27 +0800 | [diff] [blame] | 771 | |
| 772 | stat_inc_inline_inode(inode); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 773 | d_tmpfile(dentry, inode); |
| 774 | unlock_new_inode(inode); |
| 775 | return 0; |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 776 | |
| 777 | release_out: |
| 778 | release_orphan_inode(sbi); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 779 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 780 | handle_failed_inode(inode); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 781 | return err; |
| 782 | } |
| 783 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 784 | const struct inode_operations f2fs_dir_inode_operations = { |
| 785 | .create = f2fs_create, |
| 786 | .lookup = f2fs_lookup, |
| 787 | .link = f2fs_link, |
| 788 | .unlink = f2fs_unlink, |
| 789 | .symlink = f2fs_symlink, |
| 790 | .mkdir = f2fs_mkdir, |
| 791 | .rmdir = f2fs_rmdir, |
| 792 | .mknod = f2fs_mknod, |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 793 | .rename2 = f2fs_rename2, |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 794 | .tmpfile = f2fs_tmpfile, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 795 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 796 | .setattr = f2fs_setattr, |
| 797 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 798 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 799 | #ifdef CONFIG_F2FS_FS_XATTR |
| 800 | .setxattr = generic_setxattr, |
| 801 | .getxattr = generic_getxattr, |
| 802 | .listxattr = f2fs_listxattr, |
| 803 | .removexattr = generic_removexattr, |
| 804 | #endif |
| 805 | }; |
| 806 | |
| 807 | const struct inode_operations f2fs_symlink_inode_operations = { |
| 808 | .readlink = generic_readlink, |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 809 | .follow_link = f2fs_follow_link, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 810 | .put_link = page_put_link, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 811 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 812 | .setattr = f2fs_setattr, |
| 813 | #ifdef CONFIG_F2FS_FS_XATTR |
| 814 | .setxattr = generic_setxattr, |
| 815 | .getxattr = generic_getxattr, |
| 816 | .listxattr = f2fs_listxattr, |
| 817 | .removexattr = generic_removexattr, |
| 818 | #endif |
| 819 | }; |
| 820 | |
| 821 | const struct inode_operations f2fs_special_inode_operations = { |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 822 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 823 | .setattr = f2fs_setattr, |
| 824 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 825 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 826 | #ifdef CONFIG_F2FS_FS_XATTR |
| 827 | .setxattr = generic_setxattr, |
| 828 | .getxattr = generic_getxattr, |
| 829 | .listxattr = f2fs_listxattr, |
| 830 | .removexattr = generic_removexattr, |
| 831 | #endif |
| 832 | }; |