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