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> |
| 16 | |
| 17 | #include "f2fs.h" |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 18 | #include "node.h" |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 19 | #include "xattr.h" |
| 20 | #include "acl.h" |
| 21 | |
| 22 | static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode) |
| 23 | { |
| 24 | struct super_block *sb = dir->i_sb; |
| 25 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 26 | nid_t ino; |
| 27 | struct inode *inode; |
| 28 | bool nid_free = false; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 29 | int err, ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 30 | |
| 31 | inode = new_inode(sb); |
| 32 | if (!inode) |
| 33 | return ERR_PTR(-ENOMEM); |
| 34 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 35 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 36 | if (!alloc_nid(sbi, &ino)) { |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 37 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 38 | err = -ENOSPC; |
| 39 | goto fail; |
| 40 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 41 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 42 | |
| 43 | inode->i_uid = current_fsuid(); |
| 44 | |
| 45 | if (dir->i_mode & S_ISGID) { |
| 46 | inode->i_gid = dir->i_gid; |
| 47 | if (S_ISDIR(mode)) |
| 48 | mode |= S_ISGID; |
| 49 | } else { |
| 50 | inode->i_gid = current_fsgid(); |
| 51 | } |
| 52 | |
| 53 | inode->i_ino = ino; |
| 54 | inode->i_mode = mode; |
| 55 | inode->i_blocks = 0; |
| 56 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 57 | inode->i_generation = sbi->s_next_generation++; |
| 58 | |
| 59 | err = insert_inode_locked(inode); |
| 60 | if (err) { |
| 61 | err = -EINVAL; |
| 62 | nid_free = true; |
| 63 | goto out; |
| 64 | } |
| 65 | |
| 66 | mark_inode_dirty(inode); |
| 67 | return inode; |
| 68 | |
| 69 | out: |
| 70 | clear_nlink(inode); |
| 71 | unlock_new_inode(inode); |
| 72 | fail: |
| 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 | int ret; |
| 84 | |
| 85 | if (sublen > slen) |
| 86 | return 1; |
| 87 | |
| 88 | ret = memcmp(s + slen - sublen, sub, sublen); |
| 89 | if (ret) { /* compare upper case */ |
| 90 | int i; |
| 91 | char upper_sub[8]; |
| 92 | for (i = 0; i < sublen && i < sizeof(upper_sub); i++) |
| 93 | upper_sub[i] = toupper(sub[i]); |
| 94 | return memcmp(s + slen - sublen, upper_sub, sublen); |
| 95 | } |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 100 | /* |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 101 | * Set multimedia files as cold files for hot/cold data separation |
| 102 | */ |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 103 | 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] | 104 | const unsigned char *name) |
| 105 | { |
| 106 | int i; |
| 107 | __u8 (*extlist)[8] = sbi->raw_super->extension_list; |
| 108 | |
| 109 | int count = le32_to_cpu(sbi->raw_super->extension_count); |
| 110 | for (i = 0; i < count; i++) { |
| 111 | if (!is_multimedia_file(name, extlist[i])) { |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 112 | set_cold_file(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 113 | break; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
| 119 | bool excl) |
| 120 | { |
| 121 | struct super_block *sb = dir->i_sb; |
| 122 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 123 | struct inode *inode; |
| 124 | nid_t ino = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 125 | int err, ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 126 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 127 | f2fs_balance_fs(sbi); |
| 128 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 129 | inode = f2fs_new_inode(dir, mode); |
| 130 | if (IS_ERR(inode)) |
| 131 | return PTR_ERR(inode); |
| 132 | |
| 133 | if (!test_opt(sbi, DISABLE_EXT_IDENTIFY)) |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 134 | set_cold_files(sbi, inode, dentry->d_name.name); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 135 | |
| 136 | inode->i_op = &f2fs_file_inode_operations; |
| 137 | inode->i_fop = &f2fs_file_operations; |
| 138 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 139 | ino = inode->i_ino; |
| 140 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 141 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 142 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 143 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 144 | if (err) |
| 145 | goto out; |
| 146 | |
| 147 | alloc_nid_done(sbi, ino); |
| 148 | |
| 149 | if (!sbi->por_doing) |
| 150 | d_instantiate(dentry, inode); |
| 151 | unlock_new_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 152 | return 0; |
| 153 | out: |
| 154 | clear_nlink(inode); |
| 155 | unlock_new_inode(inode); |
| 156 | iput(inode); |
| 157 | alloc_nid_failed(sbi, ino); |
| 158 | return err; |
| 159 | } |
| 160 | |
| 161 | static int f2fs_link(struct dentry *old_dentry, struct inode *dir, |
| 162 | struct dentry *dentry) |
| 163 | { |
| 164 | struct inode *inode = old_dentry->d_inode; |
| 165 | struct super_block *sb = dir->i_sb; |
| 166 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 167 | int err, ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 168 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 169 | f2fs_balance_fs(sbi); |
| 170 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 171 | inode->i_ctime = CURRENT_TIME; |
| 172 | atomic_inc(&inode->i_count); |
| 173 | |
| 174 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 175 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 176 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 177 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 178 | if (err) |
| 179 | goto out; |
| 180 | |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 181 | /* |
| 182 | * This file should be checkpointed during fsync. |
| 183 | * We lost i_pino from now on. |
| 184 | */ |
| 185 | set_cp_file(inode); |
| 186 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 187 | d_instantiate(dentry, inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 188 | return 0; |
| 189 | out: |
| 190 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
| 191 | iput(inode); |
| 192 | return err; |
| 193 | } |
| 194 | |
| 195 | struct dentry *f2fs_get_parent(struct dentry *child) |
| 196 | { |
| 197 | struct qstr dotdot = QSTR_INIT("..", 2); |
| 198 | unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot); |
| 199 | if (!ino) |
| 200 | return ERR_PTR(-ENOENT); |
| 201 | return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino)); |
| 202 | } |
| 203 | |
| 204 | static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, |
| 205 | unsigned int flags) |
| 206 | { |
| 207 | struct inode *inode = NULL; |
| 208 | struct f2fs_dir_entry *de; |
| 209 | struct page *page; |
| 210 | |
Jaegeuk Kim | 5a20d33 | 2013-03-03 13:58:05 +0900 | [diff] [blame] | 211 | if (dentry->d_name.len > F2FS_NAME_LEN) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 212 | return ERR_PTR(-ENAMETOOLONG); |
| 213 | |
| 214 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 215 | if (de) { |
| 216 | nid_t ino = le32_to_cpu(de->ino); |
| 217 | kunmap(page); |
| 218 | f2fs_put_page(page, 0); |
| 219 | |
| 220 | inode = f2fs_iget(dir->i_sb, ino); |
| 221 | if (IS_ERR(inode)) |
| 222 | return ERR_CAST(inode); |
| 223 | } |
| 224 | |
| 225 | return d_splice_alias(inode, dentry); |
| 226 | } |
| 227 | |
| 228 | static int f2fs_unlink(struct inode *dir, struct dentry *dentry) |
| 229 | { |
| 230 | struct super_block *sb = dir->i_sb; |
| 231 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 232 | struct inode *inode = dentry->d_inode; |
| 233 | struct f2fs_dir_entry *de; |
| 234 | struct page *page; |
| 235 | int err = -ENOENT; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 236 | int ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 237 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 238 | f2fs_balance_fs(sbi); |
| 239 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 240 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 241 | if (!de) |
| 242 | goto fail; |
| 243 | |
| 244 | err = check_orphan_space(sbi); |
| 245 | if (err) { |
| 246 | kunmap(page); |
| 247 | f2fs_put_page(page, 0); |
| 248 | goto fail; |
| 249 | } |
| 250 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 251 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 252 | f2fs_delete_entry(de, page, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 253 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 254 | |
| 255 | /* In order to evict this inode, we set it dirty */ |
| 256 | mark_inode_dirty(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 257 | fail: |
| 258 | return err; |
| 259 | } |
| 260 | |
| 261 | static int f2fs_symlink(struct inode *dir, struct dentry *dentry, |
| 262 | const char *symname) |
| 263 | { |
| 264 | struct super_block *sb = dir->i_sb; |
| 265 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 266 | struct inode *inode; |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 267 | size_t symlen = strlen(symname) + 1; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 268 | int err, ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 269 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 270 | f2fs_balance_fs(sbi); |
| 271 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 272 | inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 273 | if (IS_ERR(inode)) |
| 274 | return PTR_ERR(inode); |
| 275 | |
| 276 | inode->i_op = &f2fs_symlink_inode_operations; |
| 277 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 278 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 279 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 280 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 281 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 282 | if (err) |
| 283 | goto out; |
| 284 | |
| 285 | err = page_symlink(inode, symname, symlen); |
| 286 | alloc_nid_done(sbi, inode->i_ino); |
| 287 | |
| 288 | d_instantiate(dentry, inode); |
| 289 | unlock_new_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 290 | return err; |
| 291 | out: |
| 292 | clear_nlink(inode); |
| 293 | unlock_new_inode(inode); |
| 294 | iput(inode); |
| 295 | alloc_nid_failed(sbi, inode->i_ino); |
| 296 | return err; |
| 297 | } |
| 298 | |
| 299 | static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 300 | { |
| 301 | struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); |
| 302 | struct inode *inode; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 303 | int err, ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 304 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 305 | f2fs_balance_fs(sbi); |
| 306 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 307 | inode = f2fs_new_inode(dir, S_IFDIR | mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 308 | if (IS_ERR(inode)) |
Namjae Jeon | 61412b6 | 2012-12-01 10:56:25 +0900 | [diff] [blame] | 309 | return PTR_ERR(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 310 | |
| 311 | inode->i_op = &f2fs_dir_inode_operations; |
| 312 | inode->i_fop = &f2fs_dir_operations; |
| 313 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
Namjae Jeon | a0d4253 | 2012-12-08 14:54:18 +0900 | [diff] [blame] | 314 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 315 | |
| 316 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 317 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 318 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 319 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 320 | if (err) |
| 321 | goto out_fail; |
| 322 | |
| 323 | alloc_nid_done(sbi, inode->i_ino); |
| 324 | |
| 325 | d_instantiate(dentry, inode); |
| 326 | unlock_new_inode(inode); |
| 327 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 328 | return 0; |
| 329 | |
| 330 | out_fail: |
| 331 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
| 332 | clear_nlink(inode); |
| 333 | unlock_new_inode(inode); |
| 334 | iput(inode); |
| 335 | alloc_nid_failed(sbi, inode->i_ino); |
| 336 | return err; |
| 337 | } |
| 338 | |
| 339 | static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) |
| 340 | { |
| 341 | struct inode *inode = dentry->d_inode; |
| 342 | if (f2fs_empty_dir(inode)) |
| 343 | return f2fs_unlink(dir, dentry); |
| 344 | return -ENOTEMPTY; |
| 345 | } |
| 346 | |
| 347 | static int f2fs_mknod(struct inode *dir, struct dentry *dentry, |
| 348 | umode_t mode, dev_t rdev) |
| 349 | { |
| 350 | struct super_block *sb = dir->i_sb; |
| 351 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 352 | struct inode *inode; |
| 353 | int err = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 354 | int ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 355 | |
| 356 | if (!new_valid_dev(rdev)) |
| 357 | return -EINVAL; |
| 358 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 359 | f2fs_balance_fs(sbi); |
| 360 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 361 | inode = f2fs_new_inode(dir, mode); |
| 362 | if (IS_ERR(inode)) |
| 363 | return PTR_ERR(inode); |
| 364 | |
| 365 | init_special_inode(inode, inode->i_mode, rdev); |
| 366 | inode->i_op = &f2fs_special_inode_operations; |
| 367 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 368 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 369 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 370 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 371 | if (err) |
| 372 | goto out; |
| 373 | |
| 374 | alloc_nid_done(sbi, inode->i_ino); |
| 375 | d_instantiate(dentry, inode); |
| 376 | unlock_new_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 377 | return 0; |
| 378 | out: |
| 379 | clear_nlink(inode); |
| 380 | unlock_new_inode(inode); |
| 381 | iput(inode); |
| 382 | alloc_nid_failed(sbi, inode->i_ino); |
| 383 | return err; |
| 384 | } |
| 385 | |
| 386 | static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 387 | struct inode *new_dir, struct dentry *new_dentry) |
| 388 | { |
| 389 | struct super_block *sb = old_dir->i_sb; |
| 390 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 391 | struct inode *old_inode = old_dentry->d_inode; |
| 392 | struct inode *new_inode = new_dentry->d_inode; |
| 393 | struct page *old_dir_page; |
| 394 | struct page *old_page; |
| 395 | struct f2fs_dir_entry *old_dir_entry = NULL; |
| 396 | struct f2fs_dir_entry *old_entry; |
| 397 | struct f2fs_dir_entry *new_entry; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 398 | int err = -ENOENT, ilock = -1; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 399 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 400 | f2fs_balance_fs(sbi); |
| 401 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 402 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 403 | if (!old_entry) |
| 404 | goto out; |
| 405 | |
| 406 | if (S_ISDIR(old_inode->i_mode)) { |
| 407 | err = -EIO; |
| 408 | old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); |
| 409 | if (!old_dir_entry) |
| 410 | goto out_old; |
| 411 | } |
| 412 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 413 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 414 | |
| 415 | if (new_inode) { |
| 416 | struct page *new_page; |
| 417 | |
| 418 | err = -ENOTEMPTY; |
| 419 | if (old_dir_entry && !f2fs_empty_dir(new_inode)) |
| 420 | goto out_dir; |
| 421 | |
| 422 | err = -ENOENT; |
| 423 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, |
| 424 | &new_page); |
| 425 | if (!new_entry) |
| 426 | goto out_dir; |
| 427 | |
| 428 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 429 | |
| 430 | new_inode->i_ctime = CURRENT_TIME; |
| 431 | if (old_dir_entry) |
| 432 | drop_nlink(new_inode); |
| 433 | drop_nlink(new_inode); |
| 434 | if (!new_inode->i_nlink) |
| 435 | add_orphan_inode(sbi, new_inode->i_ino); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 436 | update_inode_page(new_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 437 | } else { |
| 438 | err = f2fs_add_link(new_dentry, old_inode); |
| 439 | if (err) |
| 440 | goto out_dir; |
| 441 | |
| 442 | if (old_dir_entry) { |
| 443 | inc_nlink(new_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 444 | update_inode_page(new_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | old_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 449 | mark_inode_dirty(old_inode); |
| 450 | |
| 451 | f2fs_delete_entry(old_entry, old_page, NULL); |
| 452 | |
| 453 | if (old_dir_entry) { |
| 454 | if (old_dir != new_dir) { |
| 455 | f2fs_set_link(old_inode, old_dir_entry, |
| 456 | old_dir_page, new_dir); |
| 457 | } else { |
| 458 | kunmap(old_dir_page); |
| 459 | f2fs_put_page(old_dir_page, 0); |
| 460 | } |
| 461 | drop_nlink(old_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 462 | update_inode_page(old_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 463 | } |
| 464 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 465 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 466 | return 0; |
| 467 | |
| 468 | out_dir: |
| 469 | if (old_dir_entry) { |
| 470 | kunmap(old_dir_page); |
| 471 | f2fs_put_page(old_dir_page, 0); |
| 472 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame^] | 473 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 474 | out_old: |
| 475 | kunmap(old_page); |
| 476 | f2fs_put_page(old_page, 0); |
| 477 | out: |
| 478 | return err; |
| 479 | } |
| 480 | |
| 481 | const struct inode_operations f2fs_dir_inode_operations = { |
| 482 | .create = f2fs_create, |
| 483 | .lookup = f2fs_lookup, |
| 484 | .link = f2fs_link, |
| 485 | .unlink = f2fs_unlink, |
| 486 | .symlink = f2fs_symlink, |
| 487 | .mkdir = f2fs_mkdir, |
| 488 | .rmdir = f2fs_rmdir, |
| 489 | .mknod = f2fs_mknod, |
| 490 | .rename = f2fs_rename, |
| 491 | .setattr = f2fs_setattr, |
| 492 | .get_acl = f2fs_get_acl, |
| 493 | #ifdef CONFIG_F2FS_FS_XATTR |
| 494 | .setxattr = generic_setxattr, |
| 495 | .getxattr = generic_getxattr, |
| 496 | .listxattr = f2fs_listxattr, |
| 497 | .removexattr = generic_removexattr, |
| 498 | #endif |
| 499 | }; |
| 500 | |
| 501 | const struct inode_operations f2fs_symlink_inode_operations = { |
| 502 | .readlink = generic_readlink, |
| 503 | .follow_link = page_follow_link_light, |
| 504 | .put_link = page_put_link, |
| 505 | .setattr = f2fs_setattr, |
| 506 | #ifdef CONFIG_F2FS_FS_XATTR |
| 507 | .setxattr = generic_setxattr, |
| 508 | .getxattr = generic_getxattr, |
| 509 | .listxattr = f2fs_listxattr, |
| 510 | .removexattr = generic_removexattr, |
| 511 | #endif |
| 512 | }; |
| 513 | |
| 514 | const struct inode_operations f2fs_special_inode_operations = { |
| 515 | .setattr = f2fs_setattr, |
| 516 | .get_acl = f2fs_get_acl, |
| 517 | #ifdef CONFIG_F2FS_FS_XATTR |
| 518 | .setxattr = generic_setxattr, |
| 519 | .getxattr = generic_getxattr, |
| 520 | .listxattr = f2fs_listxattr, |
| 521 | .removexattr = generic_removexattr, |
| 522 | #endif |
| 523 | }; |