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