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 | |
Jaegeuk Kim | 0a364af | 2013-05-16 08:57:43 +0900 | [diff] [blame^] | 152 | d_instantiate(dentry, inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 153 | unlock_new_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 154 | return 0; |
| 155 | out: |
| 156 | clear_nlink(inode); |
| 157 | unlock_new_inode(inode); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 158 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 159 | iput(inode); |
| 160 | alloc_nid_failed(sbi, ino); |
| 161 | return err; |
| 162 | } |
| 163 | |
| 164 | static int f2fs_link(struct dentry *old_dentry, struct inode *dir, |
| 165 | struct dentry *dentry) |
| 166 | { |
| 167 | struct inode *inode = old_dentry->d_inode; |
| 168 | struct super_block *sb = dir->i_sb; |
| 169 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 170 | int err, ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 171 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 172 | f2fs_balance_fs(sbi); |
| 173 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 174 | inode->i_ctime = CURRENT_TIME; |
| 175 | atomic_inc(&inode->i_count); |
| 176 | |
| 177 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 178 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 179 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 180 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 181 | if (err) |
| 182 | goto out; |
| 183 | |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 184 | /* |
| 185 | * This file should be checkpointed during fsync. |
| 186 | * We lost i_pino from now on. |
| 187 | */ |
| 188 | set_cp_file(inode); |
| 189 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 190 | d_instantiate(dentry, inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 191 | return 0; |
| 192 | out: |
| 193 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 194 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 195 | iput(inode); |
| 196 | return err; |
| 197 | } |
| 198 | |
| 199 | struct dentry *f2fs_get_parent(struct dentry *child) |
| 200 | { |
| 201 | struct qstr dotdot = QSTR_INIT("..", 2); |
| 202 | unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot); |
| 203 | if (!ino) |
| 204 | return ERR_PTR(-ENOENT); |
| 205 | return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino)); |
| 206 | } |
| 207 | |
| 208 | static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, |
| 209 | unsigned int flags) |
| 210 | { |
| 211 | struct inode *inode = NULL; |
| 212 | struct f2fs_dir_entry *de; |
| 213 | struct page *page; |
| 214 | |
Jaegeuk Kim | 5a20d33 | 2013-03-03 13:58:05 +0900 | [diff] [blame] | 215 | if (dentry->d_name.len > F2FS_NAME_LEN) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 216 | return ERR_PTR(-ENAMETOOLONG); |
| 217 | |
| 218 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 219 | if (de) { |
| 220 | nid_t ino = le32_to_cpu(de->ino); |
| 221 | kunmap(page); |
| 222 | f2fs_put_page(page, 0); |
| 223 | |
| 224 | inode = f2fs_iget(dir->i_sb, ino); |
| 225 | if (IS_ERR(inode)) |
| 226 | return ERR_CAST(inode); |
| 227 | } |
| 228 | |
| 229 | return d_splice_alias(inode, dentry); |
| 230 | } |
| 231 | |
| 232 | static int f2fs_unlink(struct inode *dir, struct dentry *dentry) |
| 233 | { |
| 234 | struct super_block *sb = dir->i_sb; |
| 235 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 236 | struct inode *inode = dentry->d_inode; |
| 237 | struct f2fs_dir_entry *de; |
| 238 | struct page *page; |
| 239 | int err = -ENOENT; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 240 | int ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 241 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 242 | trace_f2fs_unlink_enter(dir, dentry); |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 243 | f2fs_balance_fs(sbi); |
| 244 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 245 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 246 | if (!de) |
| 247 | goto fail; |
| 248 | |
| 249 | err = check_orphan_space(sbi); |
| 250 | if (err) { |
| 251 | kunmap(page); |
| 252 | f2fs_put_page(page, 0); |
| 253 | goto fail; |
| 254 | } |
| 255 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 256 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 257 | f2fs_delete_entry(de, page, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 258 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 259 | |
| 260 | /* In order to evict this inode, we set it dirty */ |
| 261 | mark_inode_dirty(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 262 | fail: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 263 | trace_f2fs_unlink_exit(inode, err); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 264 | return err; |
| 265 | } |
| 266 | |
| 267 | static int f2fs_symlink(struct inode *dir, struct dentry *dentry, |
| 268 | const char *symname) |
| 269 | { |
| 270 | struct super_block *sb = dir->i_sb; |
| 271 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 272 | struct inode *inode; |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 273 | size_t symlen = strlen(symname) + 1; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 274 | int err, ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 275 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 276 | f2fs_balance_fs(sbi); |
| 277 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 278 | inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 279 | if (IS_ERR(inode)) |
| 280 | return PTR_ERR(inode); |
| 281 | |
| 282 | inode->i_op = &f2fs_symlink_inode_operations; |
| 283 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 284 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 285 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 286 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 287 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 288 | if (err) |
| 289 | goto out; |
| 290 | |
| 291 | err = page_symlink(inode, symname, symlen); |
| 292 | alloc_nid_done(sbi, inode->i_ino); |
| 293 | |
| 294 | d_instantiate(dentry, inode); |
| 295 | unlock_new_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 296 | return err; |
| 297 | out: |
| 298 | clear_nlink(inode); |
| 299 | unlock_new_inode(inode); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 300 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 301 | iput(inode); |
| 302 | alloc_nid_failed(sbi, inode->i_ino); |
| 303 | return err; |
| 304 | } |
| 305 | |
| 306 | static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 307 | { |
| 308 | struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); |
| 309 | struct inode *inode; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 310 | int err, ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 311 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 312 | f2fs_balance_fs(sbi); |
| 313 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 314 | inode = f2fs_new_inode(dir, S_IFDIR | mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 315 | if (IS_ERR(inode)) |
Namjae Jeon | 61412b6 | 2012-12-01 10:56:25 +0900 | [diff] [blame] | 316 | return PTR_ERR(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 317 | |
| 318 | inode->i_op = &f2fs_dir_inode_operations; |
| 319 | inode->i_fop = &f2fs_dir_operations; |
| 320 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
Namjae Jeon | a0d4253 | 2012-12-08 14:54:18 +0900 | [diff] [blame] | 321 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 322 | |
| 323 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 324 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 325 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 326 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 327 | if (err) |
| 328 | goto out_fail; |
| 329 | |
| 330 | alloc_nid_done(sbi, inode->i_ino); |
| 331 | |
| 332 | d_instantiate(dentry, inode); |
| 333 | unlock_new_inode(inode); |
| 334 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 335 | return 0; |
| 336 | |
| 337 | out_fail: |
| 338 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
| 339 | clear_nlink(inode); |
| 340 | unlock_new_inode(inode); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 341 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 342 | iput(inode); |
| 343 | alloc_nid_failed(sbi, inode->i_ino); |
| 344 | return err; |
| 345 | } |
| 346 | |
| 347 | static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) |
| 348 | { |
| 349 | struct inode *inode = dentry->d_inode; |
| 350 | if (f2fs_empty_dir(inode)) |
| 351 | return f2fs_unlink(dir, dentry); |
| 352 | return -ENOTEMPTY; |
| 353 | } |
| 354 | |
| 355 | static int f2fs_mknod(struct inode *dir, struct dentry *dentry, |
| 356 | umode_t mode, dev_t rdev) |
| 357 | { |
| 358 | struct super_block *sb = dir->i_sb; |
| 359 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 360 | struct inode *inode; |
| 361 | int err = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 362 | int ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 363 | |
| 364 | if (!new_valid_dev(rdev)) |
| 365 | return -EINVAL; |
| 366 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 367 | f2fs_balance_fs(sbi); |
| 368 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 369 | inode = f2fs_new_inode(dir, mode); |
| 370 | if (IS_ERR(inode)) |
| 371 | return PTR_ERR(inode); |
| 372 | |
| 373 | init_special_inode(inode, inode->i_mode, rdev); |
| 374 | inode->i_op = &f2fs_special_inode_operations; |
| 375 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 376 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 377 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 378 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 379 | if (err) |
| 380 | goto out; |
| 381 | |
| 382 | alloc_nid_done(sbi, inode->i_ino); |
| 383 | d_instantiate(dentry, inode); |
| 384 | unlock_new_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 385 | return 0; |
| 386 | out: |
| 387 | clear_nlink(inode); |
| 388 | unlock_new_inode(inode); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 389 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 390 | iput(inode); |
| 391 | alloc_nid_failed(sbi, inode->i_ino); |
| 392 | return err; |
| 393 | } |
| 394 | |
| 395 | static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 396 | struct inode *new_dir, struct dentry *new_dentry) |
| 397 | { |
| 398 | struct super_block *sb = old_dir->i_sb; |
| 399 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 400 | struct inode *old_inode = old_dentry->d_inode; |
| 401 | struct inode *new_inode = new_dentry->d_inode; |
| 402 | struct page *old_dir_page; |
| 403 | struct page *old_page; |
| 404 | struct f2fs_dir_entry *old_dir_entry = NULL; |
| 405 | struct f2fs_dir_entry *old_entry; |
| 406 | struct f2fs_dir_entry *new_entry; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 407 | int err = -ENOENT, ilock = -1; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 408 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 409 | f2fs_balance_fs(sbi); |
| 410 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 411 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 412 | if (!old_entry) |
| 413 | goto out; |
| 414 | |
| 415 | if (S_ISDIR(old_inode->i_mode)) { |
| 416 | err = -EIO; |
| 417 | old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); |
| 418 | if (!old_dir_entry) |
| 419 | goto out_old; |
| 420 | } |
| 421 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 422 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 423 | |
| 424 | if (new_inode) { |
| 425 | struct page *new_page; |
| 426 | |
| 427 | err = -ENOTEMPTY; |
| 428 | if (old_dir_entry && !f2fs_empty_dir(new_inode)) |
| 429 | goto out_dir; |
| 430 | |
| 431 | err = -ENOENT; |
| 432 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, |
| 433 | &new_page); |
| 434 | if (!new_entry) |
| 435 | goto out_dir; |
| 436 | |
| 437 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 438 | |
| 439 | new_inode->i_ctime = CURRENT_TIME; |
| 440 | if (old_dir_entry) |
| 441 | drop_nlink(new_inode); |
| 442 | drop_nlink(new_inode); |
| 443 | if (!new_inode->i_nlink) |
| 444 | add_orphan_inode(sbi, new_inode->i_ino); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 445 | update_inode_page(new_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 446 | } else { |
| 447 | err = f2fs_add_link(new_dentry, old_inode); |
| 448 | if (err) |
| 449 | goto out_dir; |
| 450 | |
| 451 | if (old_dir_entry) { |
| 452 | inc_nlink(new_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 453 | update_inode_page(new_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | old_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 458 | mark_inode_dirty(old_inode); |
| 459 | |
| 460 | f2fs_delete_entry(old_entry, old_page, NULL); |
| 461 | |
| 462 | if (old_dir_entry) { |
| 463 | if (old_dir != new_dir) { |
| 464 | f2fs_set_link(old_inode, old_dir_entry, |
| 465 | old_dir_page, new_dir); |
| 466 | } else { |
| 467 | kunmap(old_dir_page); |
| 468 | f2fs_put_page(old_dir_page, 0); |
| 469 | } |
| 470 | drop_nlink(old_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 471 | update_inode_page(old_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 472 | } |
| 473 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 474 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 475 | return 0; |
| 476 | |
| 477 | out_dir: |
| 478 | if (old_dir_entry) { |
| 479 | kunmap(old_dir_page); |
| 480 | f2fs_put_page(old_dir_page, 0); |
| 481 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 482 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 483 | out_old: |
| 484 | kunmap(old_page); |
| 485 | f2fs_put_page(old_page, 0); |
| 486 | out: |
| 487 | return err; |
| 488 | } |
| 489 | |
| 490 | const struct inode_operations f2fs_dir_inode_operations = { |
| 491 | .create = f2fs_create, |
| 492 | .lookup = f2fs_lookup, |
| 493 | .link = f2fs_link, |
| 494 | .unlink = f2fs_unlink, |
| 495 | .symlink = f2fs_symlink, |
| 496 | .mkdir = f2fs_mkdir, |
| 497 | .rmdir = f2fs_rmdir, |
| 498 | .mknod = f2fs_mknod, |
| 499 | .rename = f2fs_rename, |
| 500 | .setattr = f2fs_setattr, |
| 501 | .get_acl = f2fs_get_acl, |
| 502 | #ifdef CONFIG_F2FS_FS_XATTR |
| 503 | .setxattr = generic_setxattr, |
| 504 | .getxattr = generic_getxattr, |
| 505 | .listxattr = f2fs_listxattr, |
| 506 | .removexattr = generic_removexattr, |
| 507 | #endif |
| 508 | }; |
| 509 | |
| 510 | const struct inode_operations f2fs_symlink_inode_operations = { |
| 511 | .readlink = generic_readlink, |
| 512 | .follow_link = page_follow_link_light, |
| 513 | .put_link = page_put_link, |
| 514 | .setattr = f2fs_setattr, |
| 515 | #ifdef CONFIG_F2FS_FS_XATTR |
| 516 | .setxattr = generic_setxattr, |
| 517 | .getxattr = generic_getxattr, |
| 518 | .listxattr = f2fs_listxattr, |
| 519 | .removexattr = generic_removexattr, |
| 520 | #endif |
| 521 | }; |
| 522 | |
| 523 | const struct inode_operations f2fs_special_inode_operations = { |
| 524 | .setattr = f2fs_setattr, |
| 525 | .get_acl = f2fs_get_acl, |
| 526 | #ifdef CONFIG_F2FS_FS_XATTR |
| 527 | .setxattr = generic_setxattr, |
| 528 | .getxattr = generic_getxattr, |
| 529 | .listxattr = f2fs_listxattr, |
| 530 | .removexattr = generic_removexattr, |
| 531 | #endif |
| 532 | }; |