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