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 | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 115 | file_set_cold(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; |
Jaegeuk Kim | 6f6fd83 | 2013-05-22 12:06:26 +0900 | [diff] [blame] | 175 | ihold(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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 | |
| 184 | d_instantiate(dentry, inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 185 | return 0; |
| 186 | out: |
| 187 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
| 188 | iput(inode); |
| 189 | return err; |
| 190 | } |
| 191 | |
| 192 | struct dentry *f2fs_get_parent(struct dentry *child) |
| 193 | { |
| 194 | struct qstr dotdot = QSTR_INIT("..", 2); |
| 195 | unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot); |
| 196 | if (!ino) |
| 197 | return ERR_PTR(-ENOENT); |
| 198 | return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino)); |
| 199 | } |
| 200 | |
| 201 | static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, |
| 202 | unsigned int flags) |
| 203 | { |
| 204 | struct inode *inode = NULL; |
| 205 | struct f2fs_dir_entry *de; |
| 206 | struct page *page; |
| 207 | |
Jaegeuk Kim | 5a20d33 | 2013-03-03 13:58:05 +0900 | [diff] [blame] | 208 | if (dentry->d_name.len > F2FS_NAME_LEN) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 209 | return ERR_PTR(-ENAMETOOLONG); |
| 210 | |
| 211 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 212 | if (de) { |
| 213 | nid_t ino = le32_to_cpu(de->ino); |
| 214 | kunmap(page); |
| 215 | f2fs_put_page(page, 0); |
| 216 | |
| 217 | inode = f2fs_iget(dir->i_sb, ino); |
| 218 | if (IS_ERR(inode)) |
| 219 | return ERR_CAST(inode); |
| 220 | } |
| 221 | |
| 222 | return d_splice_alias(inode, dentry); |
| 223 | } |
| 224 | |
| 225 | static int f2fs_unlink(struct inode *dir, struct dentry *dentry) |
| 226 | { |
| 227 | struct super_block *sb = dir->i_sb; |
| 228 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 229 | struct inode *inode = dentry->d_inode; |
| 230 | struct f2fs_dir_entry *de; |
| 231 | struct page *page; |
| 232 | int err = -ENOENT; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 233 | int ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 234 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 235 | trace_f2fs_unlink_enter(dir, dentry); |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 236 | f2fs_balance_fs(sbi); |
| 237 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 238 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 239 | if (!de) |
| 240 | goto fail; |
| 241 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 242 | err = acquire_orphan_inode(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 243 | if (err) { |
| 244 | kunmap(page); |
| 245 | f2fs_put_page(page, 0); |
| 246 | goto fail; |
| 247 | } |
| 248 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 249 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 250 | f2fs_delete_entry(de, page, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 251 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 252 | |
| 253 | /* In order to evict this inode, we set it dirty */ |
| 254 | mark_inode_dirty(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 255 | fail: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 256 | trace_f2fs_unlink_exit(inode, err); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 257 | return err; |
| 258 | } |
| 259 | |
| 260 | static int f2fs_symlink(struct inode *dir, struct dentry *dentry, |
| 261 | const char *symname) |
| 262 | { |
| 263 | struct super_block *sb = dir->i_sb; |
| 264 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 265 | struct inode *inode; |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 266 | size_t symlen = strlen(symname) + 1; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 267 | int err, ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 268 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 269 | f2fs_balance_fs(sbi); |
| 270 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 271 | inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 272 | if (IS_ERR(inode)) |
| 273 | return PTR_ERR(inode); |
| 274 | |
| 275 | inode->i_op = &f2fs_symlink_inode_operations; |
| 276 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 277 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 278 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 279 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 280 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 281 | if (err) |
| 282 | goto out; |
| 283 | |
| 284 | err = page_symlink(inode, symname, symlen); |
| 285 | alloc_nid_done(sbi, inode->i_ino); |
| 286 | |
| 287 | d_instantiate(dentry, inode); |
| 288 | unlock_new_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 289 | return err; |
| 290 | out: |
| 291 | clear_nlink(inode); |
| 292 | unlock_new_inode(inode); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 293 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 334 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 335 | iput(inode); |
| 336 | alloc_nid_failed(sbi, inode->i_ino); |
| 337 | return err; |
| 338 | } |
| 339 | |
| 340 | static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) |
| 341 | { |
| 342 | struct inode *inode = dentry->d_inode; |
| 343 | if (f2fs_empty_dir(inode)) |
| 344 | return f2fs_unlink(dir, dentry); |
| 345 | return -ENOTEMPTY; |
| 346 | } |
| 347 | |
| 348 | static int f2fs_mknod(struct inode *dir, struct dentry *dentry, |
| 349 | umode_t mode, dev_t rdev) |
| 350 | { |
| 351 | struct super_block *sb = dir->i_sb; |
| 352 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 353 | struct inode *inode; |
| 354 | int err = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 355 | int ilock; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 356 | |
| 357 | if (!new_valid_dev(rdev)) |
| 358 | return -EINVAL; |
| 359 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 360 | f2fs_balance_fs(sbi); |
| 361 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 362 | inode = f2fs_new_inode(dir, mode); |
| 363 | if (IS_ERR(inode)) |
| 364 | return PTR_ERR(inode); |
| 365 | |
| 366 | init_special_inode(inode, inode->i_mode, rdev); |
| 367 | inode->i_op = &f2fs_special_inode_operations; |
| 368 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 369 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 370 | err = f2fs_add_link(dentry, inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 371 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 372 | if (err) |
| 373 | goto out; |
| 374 | |
| 375 | alloc_nid_done(sbi, inode->i_ino); |
| 376 | d_instantiate(dentry, inode); |
| 377 | unlock_new_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 378 | return 0; |
| 379 | out: |
| 380 | clear_nlink(inode); |
| 381 | unlock_new_inode(inode); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 382 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 383 | iput(inode); |
| 384 | alloc_nid_failed(sbi, inode->i_ino); |
| 385 | return err; |
| 386 | } |
| 387 | |
| 388 | static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 389 | struct inode *new_dir, struct dentry *new_dentry) |
| 390 | { |
| 391 | struct super_block *sb = old_dir->i_sb; |
| 392 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
| 393 | struct inode *old_inode = old_dentry->d_inode; |
| 394 | struct inode *new_inode = new_dentry->d_inode; |
| 395 | struct page *old_dir_page; |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 396 | struct page *old_page, *new_page; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 397 | struct f2fs_dir_entry *old_dir_entry = NULL; |
| 398 | struct f2fs_dir_entry *old_entry; |
| 399 | struct f2fs_dir_entry *new_entry; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 400 | int err = -ENOENT, ilock = -1; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 401 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 402 | f2fs_balance_fs(sbi); |
| 403 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 404 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 405 | if (!old_entry) |
| 406 | goto out; |
| 407 | |
| 408 | if (S_ISDIR(old_inode->i_mode)) { |
| 409 | err = -EIO; |
| 410 | old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); |
| 411 | if (!old_dir_entry) |
| 412 | goto out_old; |
| 413 | } |
| 414 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 415 | ilock = mutex_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 416 | |
| 417 | if (new_inode) { |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 418 | |
| 419 | err = -ENOTEMPTY; |
| 420 | if (old_dir_entry && !f2fs_empty_dir(new_inode)) |
| 421 | goto out_dir; |
| 422 | |
| 423 | err = -ENOENT; |
| 424 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, |
| 425 | &new_page); |
| 426 | if (!new_entry) |
| 427 | goto out_dir; |
| 428 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 429 | err = acquire_orphan_inode(sbi); |
| 430 | if (err) |
| 431 | goto put_out_dir; |
| 432 | |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 433 | if (update_dent_inode(old_inode, &new_dentry->d_name)) { |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 434 | release_orphan_inode(sbi); |
| 435 | goto put_out_dir; |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 436 | } |
| 437 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 444 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 445 | if (!new_inode->i_nlink) |
| 446 | add_orphan_inode(sbi, new_inode->i_ino); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 447 | else |
| 448 | release_orphan_inode(sbi); |
| 449 | |
Jaegeuk Kim | 8cb8268 | 2013-08-26 21:25:31 +0900 | [diff] [blame^] | 450 | update_inode_page(old_inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 451 | update_inode_page(new_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 452 | } else { |
| 453 | err = f2fs_add_link(new_dentry, old_inode); |
| 454 | if (err) |
| 455 | goto out_dir; |
| 456 | |
| 457 | if (old_dir_entry) { |
| 458 | inc_nlink(new_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 459 | update_inode_page(new_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | old_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 464 | mark_inode_dirty(old_inode); |
| 465 | |
| 466 | f2fs_delete_entry(old_entry, old_page, NULL); |
| 467 | |
| 468 | if (old_dir_entry) { |
| 469 | if (old_dir != new_dir) { |
| 470 | f2fs_set_link(old_inode, old_dir_entry, |
| 471 | old_dir_page, new_dir); |
| 472 | } else { |
| 473 | kunmap(old_dir_page); |
| 474 | f2fs_put_page(old_dir_page, 0); |
| 475 | } |
| 476 | drop_nlink(old_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 477 | update_inode_page(old_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 478 | } |
| 479 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 480 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 481 | return 0; |
| 482 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 483 | put_out_dir: |
| 484 | f2fs_put_page(new_page, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 485 | out_dir: |
| 486 | if (old_dir_entry) { |
| 487 | kunmap(old_dir_page); |
| 488 | f2fs_put_page(old_dir_page, 0); |
| 489 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 490 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 491 | out_old: |
| 492 | kunmap(old_page); |
| 493 | f2fs_put_page(old_page, 0); |
| 494 | out: |
| 495 | return err; |
| 496 | } |
| 497 | |
| 498 | const struct inode_operations f2fs_dir_inode_operations = { |
| 499 | .create = f2fs_create, |
| 500 | .lookup = f2fs_lookup, |
| 501 | .link = f2fs_link, |
| 502 | .unlink = f2fs_unlink, |
| 503 | .symlink = f2fs_symlink, |
| 504 | .mkdir = f2fs_mkdir, |
| 505 | .rmdir = f2fs_rmdir, |
| 506 | .mknod = f2fs_mknod, |
| 507 | .rename = f2fs_rename, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 508 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 509 | .setattr = f2fs_setattr, |
| 510 | .get_acl = f2fs_get_acl, |
| 511 | #ifdef CONFIG_F2FS_FS_XATTR |
| 512 | .setxattr = generic_setxattr, |
| 513 | .getxattr = generic_getxattr, |
| 514 | .listxattr = f2fs_listxattr, |
| 515 | .removexattr = generic_removexattr, |
| 516 | #endif |
| 517 | }; |
| 518 | |
| 519 | const struct inode_operations f2fs_symlink_inode_operations = { |
| 520 | .readlink = generic_readlink, |
| 521 | .follow_link = page_follow_link_light, |
| 522 | .put_link = page_put_link, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 523 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 524 | .setattr = f2fs_setattr, |
| 525 | #ifdef CONFIG_F2FS_FS_XATTR |
| 526 | .setxattr = generic_setxattr, |
| 527 | .getxattr = generic_getxattr, |
| 528 | .listxattr = f2fs_listxattr, |
| 529 | .removexattr = generic_removexattr, |
| 530 | #endif |
| 531 | }; |
| 532 | |
| 533 | const struct inode_operations f2fs_special_inode_operations = { |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 534 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 535 | .setattr = f2fs_setattr, |
| 536 | .get_acl = f2fs_get_acl, |
| 537 | #ifdef CONFIG_F2FS_FS_XATTR |
| 538 | .setxattr = generic_setxattr, |
| 539 | .getxattr = generic_getxattr, |
| 540 | .listxattr = f2fs_listxattr, |
| 541 | .removexattr = generic_removexattr, |
| 542 | #endif |
| 543 | }; |