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