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