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