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 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 26 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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 | |
Jaegeuk Kim | a014e03 | 2014-06-20 21:44:02 -0700 | [diff] [blame] | 32 | inode = new_inode(dir->i_sb); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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 | |
Chao Yu | 70ff5df | 2014-05-08 17:09:30 +0800 | [diff] [blame] | 44 | inode_init_owner(inode, dir, mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 45 | |
| 46 | inode->i_ino = ino; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 47 | inode->i_blocks = 0; |
| 48 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 49 | inode->i_generation = sbi->s_next_generation++; |
| 50 | |
| 51 | err = insert_inode_locked(inode); |
| 52 | if (err) { |
| 53 | err = -EINVAL; |
| 54 | nid_free = true; |
| 55 | goto out; |
| 56 | } |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 57 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 58 | if (f2fs_may_inline(inode)) |
| 59 | set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 60 | if (test_opt(sbi, INLINE_DENTRY) && S_ISDIR(inode->i_mode)) |
| 61 | set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY); |
| 62 | |
Jaegeuk Kim | d70b4f5 | 2013-04-25 13:24:33 +0900 | [diff] [blame] | 63 | trace_f2fs_new_inode(inode, 0); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 64 | mark_inode_dirty(inode); |
| 65 | return inode; |
| 66 | |
| 67 | out: |
| 68 | clear_nlink(inode); |
| 69 | unlock_new_inode(inode); |
| 70 | fail: |
Jaegeuk Kim | d70b4f5 | 2013-04-25 13:24:33 +0900 | [diff] [blame] | 71 | trace_f2fs_new_inode(inode, err); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 72 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 73 | iput(inode); |
| 74 | if (nid_free) |
| 75 | alloc_nid_failed(sbi, ino); |
| 76 | return ERR_PTR(err); |
| 77 | } |
| 78 | |
| 79 | static int is_multimedia_file(const unsigned char *s, const char *sub) |
| 80 | { |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 81 | size_t slen = strlen(s); |
| 82 | size_t sublen = strlen(sub); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 83 | |
| 84 | if (sublen > slen) |
Namjae Jeon | e66509f0 | 2013-04-20 01:27:21 +0900 | [diff] [blame] | 85 | return 0; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 86 | |
Gu Zheng | 749ebfd | 2013-08-22 18:11:25 +0800 | [diff] [blame] | 87 | return !strncasecmp(s + slen - sublen, sub, sublen); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 88 | } |
| 89 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 90 | /* |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 91 | * Set multimedia files as cold files for hot/cold data separation |
| 92 | */ |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 93 | 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] | 94 | const unsigned char *name) |
| 95 | { |
| 96 | int i; |
| 97 | __u8 (*extlist)[8] = sbi->raw_super->extension_list; |
| 98 | |
| 99 | int count = le32_to_cpu(sbi->raw_super->extension_count); |
| 100 | for (i = 0; i < count; i++) { |
Namjae Jeon | e66509f0 | 2013-04-20 01:27:21 +0900 | [diff] [blame] | 101 | if (is_multimedia_file(name, extlist[i])) { |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 102 | file_set_cold(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
| 109 | bool excl) |
| 110 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 111 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 112 | struct inode *inode; |
| 113 | nid_t ino = 0; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 114 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 115 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 116 | f2fs_balance_fs(sbi); |
| 117 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 118 | inode = f2fs_new_inode(dir, mode); |
| 119 | if (IS_ERR(inode)) |
| 120 | return PTR_ERR(inode); |
| 121 | |
| 122 | if (!test_opt(sbi, DISABLE_EXT_IDENTIFY)) |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 123 | set_cold_files(sbi, inode, dentry->d_name.name); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 124 | |
| 125 | inode->i_op = &f2fs_file_inode_operations; |
| 126 | inode->i_fop = &f2fs_file_operations; |
| 127 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 128 | ino = inode->i_ino; |
| 129 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 130 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 131 | err = f2fs_add_link(dentry, inode); |
| 132 | if (err) |
| 133 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 134 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 135 | |
| 136 | alloc_nid_done(sbi, ino); |
| 137 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 138 | stat_inc_inline_inode(inode); |
Jaegeuk Kim | 0a364af | 2013-05-16 08:57:43 +0900 | [diff] [blame] | 139 | d_instantiate(dentry, inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 140 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 141 | |
| 142 | if (IS_DIRSYNC(dir)) |
| 143 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 144 | return 0; |
| 145 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 146 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 147 | return err; |
| 148 | } |
| 149 | |
| 150 | static int f2fs_link(struct dentry *old_dentry, struct inode *dir, |
| 151 | struct dentry *dentry) |
| 152 | { |
| 153 | struct inode *inode = old_dentry->d_inode; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 154 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 155 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 156 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 157 | f2fs_balance_fs(sbi); |
| 158 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 159 | inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 6f6fd83 | 2013-05-22 12:06:26 +0900 | [diff] [blame] | 160 | ihold(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 161 | |
| 162 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 163 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 164 | err = f2fs_add_link(dentry, inode); |
| 165 | if (err) |
| 166 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 167 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 168 | |
| 169 | d_instantiate(dentry, inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 170 | |
| 171 | if (IS_DIRSYNC(dir)) |
| 172 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 173 | return 0; |
| 174 | out: |
| 175 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
| 176 | iput(inode); |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 177 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 178 | return err; |
| 179 | } |
| 180 | |
| 181 | struct dentry *f2fs_get_parent(struct dentry *child) |
| 182 | { |
| 183 | struct qstr dotdot = QSTR_INIT("..", 2); |
| 184 | unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot); |
| 185 | if (!ino) |
| 186 | return ERR_PTR(-ENOENT); |
| 187 | return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino)); |
| 188 | } |
| 189 | |
| 190 | static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, |
| 191 | unsigned int flags) |
| 192 | { |
| 193 | struct inode *inode = NULL; |
| 194 | struct f2fs_dir_entry *de; |
| 195 | struct page *page; |
| 196 | |
Jaegeuk Kim | 5a20d33 | 2013-03-03 13:58:05 +0900 | [diff] [blame] | 197 | if (dentry->d_name.len > F2FS_NAME_LEN) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 198 | return ERR_PTR(-ENAMETOOLONG); |
| 199 | |
| 200 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 201 | if (de) { |
| 202 | nid_t ino = le32_to_cpu(de->ino); |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 203 | f2fs_dentry_kunmap(dir, page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 204 | f2fs_put_page(page, 0); |
| 205 | |
| 206 | inode = f2fs_iget(dir->i_sb, ino); |
| 207 | if (IS_ERR(inode)) |
| 208 | return ERR_CAST(inode); |
| 209 | } |
| 210 | |
| 211 | return d_splice_alias(inode, dentry); |
| 212 | } |
| 213 | |
| 214 | static int f2fs_unlink(struct inode *dir, struct dentry *dentry) |
| 215 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 216 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 217 | struct inode *inode = dentry->d_inode; |
| 218 | struct f2fs_dir_entry *de; |
| 219 | struct page *page; |
| 220 | int err = -ENOENT; |
| 221 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 222 | trace_f2fs_unlink_enter(dir, dentry); |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 223 | f2fs_balance_fs(sbi); |
| 224 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 225 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
| 226 | if (!de) |
| 227 | goto fail; |
| 228 | |
Jaegeuk Kim | ccaaca2 | 2013-10-08 10:19:28 +0900 | [diff] [blame] | 229 | f2fs_lock_op(sbi); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 230 | err = acquire_orphan_inode(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 231 | if (err) { |
Jaegeuk Kim | ccaaca2 | 2013-10-08 10:19:28 +0900 | [diff] [blame] | 232 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 233 | f2fs_dentry_kunmap(dir, page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 234 | f2fs_put_page(page, 0); |
| 235 | goto fail; |
| 236 | } |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 237 | f2fs_delete_entry(de, page, dir, inode); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 238 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 239 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 240 | /* In order to evict this inode, we set it dirty */ |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 241 | mark_inode_dirty(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 242 | |
| 243 | if (IS_DIRSYNC(dir)) |
| 244 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 245 | fail: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 246 | trace_f2fs_unlink_exit(inode, err); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 247 | return err; |
| 248 | } |
| 249 | |
| 250 | static int f2fs_symlink(struct inode *dir, struct dentry *dentry, |
| 251 | const char *symname) |
| 252 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 253 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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); |
| 269 | if (err) |
| 270 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 271 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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 | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 278 | |
| 279 | if (IS_DIRSYNC(dir)) |
| 280 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 281 | return err; |
| 282 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 283 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 284 | return err; |
| 285 | } |
| 286 | |
| 287 | static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 288 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 289 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 290 | struct inode *inode; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 291 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 292 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 293 | f2fs_balance_fs(sbi); |
| 294 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 295 | inode = f2fs_new_inode(dir, S_IFDIR | mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 296 | if (IS_ERR(inode)) |
Namjae Jeon | 61412b6 | 2012-12-01 10:56:25 +0900 | [diff] [blame] | 297 | return PTR_ERR(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 298 | |
| 299 | inode->i_op = &f2fs_dir_inode_operations; |
| 300 | inode->i_fop = &f2fs_dir_operations; |
| 301 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
Namjae Jeon | a0d4253 | 2012-12-08 14:54:18 +0900 | [diff] [blame] | 302 | mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 303 | |
| 304 | set_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 305 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 306 | err = f2fs_add_link(dentry, inode); |
| 307 | if (err) |
| 308 | goto out_fail; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 309 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 310 | |
Jaegeuk Kim | 3289c06 | 2014-10-13 20:00:16 -0700 | [diff] [blame] | 311 | stat_inc_inline_dir(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 312 | alloc_nid_done(sbi, inode->i_ino); |
| 313 | |
| 314 | d_instantiate(dentry, inode); |
| 315 | unlock_new_inode(inode); |
| 316 | |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 317 | if (IS_DIRSYNC(dir)) |
| 318 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 319 | return 0; |
| 320 | |
| 321 | out_fail: |
| 322 | clear_inode_flag(F2FS_I(inode), FI_INC_LINK); |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 323 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 324 | return err; |
| 325 | } |
| 326 | |
| 327 | static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) |
| 328 | { |
| 329 | struct inode *inode = dentry->d_inode; |
| 330 | if (f2fs_empty_dir(inode)) |
| 331 | return f2fs_unlink(dir, dentry); |
| 332 | return -ENOTEMPTY; |
| 333 | } |
| 334 | |
| 335 | static int f2fs_mknod(struct inode *dir, struct dentry *dentry, |
| 336 | umode_t mode, dev_t rdev) |
| 337 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 338 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 339 | struct inode *inode; |
| 340 | int err = 0; |
| 341 | |
| 342 | if (!new_valid_dev(rdev)) |
| 343 | return -EINVAL; |
| 344 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 345 | f2fs_balance_fs(sbi); |
| 346 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 347 | inode = f2fs_new_inode(dir, mode); |
| 348 | if (IS_ERR(inode)) |
| 349 | return PTR_ERR(inode); |
| 350 | |
| 351 | init_special_inode(inode, inode->i_mode, rdev); |
| 352 | inode->i_op = &f2fs_special_inode_operations; |
| 353 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 354 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 355 | err = f2fs_add_link(dentry, inode); |
| 356 | if (err) |
| 357 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 358 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 359 | |
| 360 | alloc_nid_done(sbi, inode->i_ino); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 361 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 362 | d_instantiate(dentry, inode); |
| 363 | unlock_new_inode(inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 364 | |
| 365 | if (IS_DIRSYNC(dir)) |
| 366 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 367 | return 0; |
| 368 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 369 | handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 370 | return err; |
| 371 | } |
| 372 | |
| 373 | static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 374 | struct inode *new_dir, struct dentry *new_dentry) |
| 375 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 376 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 377 | struct inode *old_inode = old_dentry->d_inode; |
| 378 | struct inode *new_inode = new_dentry->d_inode; |
| 379 | struct page *old_dir_page; |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 380 | struct page *old_page, *new_page; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 381 | struct f2fs_dir_entry *old_dir_entry = NULL; |
| 382 | struct f2fs_dir_entry *old_entry; |
| 383 | struct f2fs_dir_entry *new_entry; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 384 | int err = -ENOENT; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 385 | |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 386 | f2fs_balance_fs(sbi); |
| 387 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 388 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 389 | if (!old_entry) |
| 390 | goto out; |
| 391 | |
| 392 | if (S_ISDIR(old_inode->i_mode)) { |
| 393 | err = -EIO; |
| 394 | old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); |
| 395 | if (!old_dir_entry) |
| 396 | goto out_old; |
| 397 | } |
| 398 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 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 | |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 411 | f2fs_lock_op(sbi); |
| 412 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 413 | err = acquire_orphan_inode(sbi); |
| 414 | if (err) |
| 415 | goto put_out_dir; |
| 416 | |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 417 | if (update_dent_inode(old_inode, &new_dentry->d_name)) { |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 418 | release_orphan_inode(sbi); |
| 419 | goto put_out_dir; |
Jaegeuk Kim | 1cd14ca | 2013-07-18 18:02:31 +0900 | [diff] [blame] | 420 | } |
| 421 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 422 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 423 | |
| 424 | new_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 425 | down_write(&F2FS_I(new_inode)->i_sem); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 426 | if (old_dir_entry) |
| 427 | drop_nlink(new_inode); |
| 428 | drop_nlink(new_inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 429 | up_write(&F2FS_I(new_inode)->i_sem); |
| 430 | |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 431 | mark_inode_dirty(new_inode); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 432 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 433 | if (!new_inode->i_nlink) |
| 434 | add_orphan_inode(sbi, new_inode->i_ino); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 435 | else |
| 436 | release_orphan_inode(sbi); |
| 437 | |
Jaegeuk Kim | 8cb8268 | 2013-08-26 21:25:31 +0900 | [diff] [blame] | 438 | update_inode_page(old_inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 439 | update_inode_page(new_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 440 | } else { |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 441 | f2fs_lock_op(sbi); |
| 442 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 443 | err = f2fs_add_link(new_dentry, old_inode); |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 444 | if (err) { |
| 445 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 446 | goto out_dir; |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 447 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 448 | |
| 449 | if (old_dir_entry) { |
| 450 | inc_nlink(new_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 451 | update_inode_page(new_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Jaegeuk Kim | b2c0829 | 2014-06-30 18:09:55 +0900 | [diff] [blame] | 455 | down_write(&F2FS_I(old_inode)->i_sem); |
| 456 | file_lost_pino(old_inode); |
| 457 | up_write(&F2FS_I(old_inode)->i_sem); |
| 458 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 459 | old_inode->i_ctime = CURRENT_TIME; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 460 | mark_inode_dirty(old_inode); |
| 461 | |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 462 | f2fs_delete_entry(old_entry, old_page, old_dir, NULL); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 463 | |
| 464 | if (old_dir_entry) { |
| 465 | if (old_dir != new_dir) { |
| 466 | f2fs_set_link(old_inode, old_dir_entry, |
| 467 | old_dir_page, new_dir); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 468 | update_inode_page(old_inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 469 | } else { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 470 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 471 | f2fs_put_page(old_dir_page, 0); |
| 472 | } |
| 473 | drop_nlink(old_dir); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 474 | mark_inode_dirty(old_dir); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 475 | update_inode_page(old_dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 476 | } |
| 477 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 478 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 479 | |
| 480 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 481 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 482 | return 0; |
| 483 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 484 | put_out_dir: |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 485 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 486 | f2fs_dentry_kunmap(new_dir, new_page); |
Chao Yu | dd4d961 | 2014-06-24 14:13:13 +0800 | [diff] [blame] | 487 | f2fs_put_page(new_page, 0); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 488 | out_dir: |
| 489 | if (old_dir_entry) { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 490 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 491 | f2fs_put_page(old_dir_page, 0); |
| 492 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 493 | out_old: |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 494 | f2fs_dentry_kunmap(old_dir, old_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 495 | f2fs_put_page(old_page, 0); |
| 496 | out: |
| 497 | return err; |
| 498 | } |
| 499 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 500 | static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 501 | struct inode *new_dir, struct dentry *new_dentry) |
| 502 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 503 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 504 | struct inode *old_inode = old_dentry->d_inode; |
| 505 | struct inode *new_inode = new_dentry->d_inode; |
| 506 | struct page *old_dir_page, *new_dir_page; |
| 507 | struct page *old_page, *new_page; |
| 508 | struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; |
| 509 | struct f2fs_dir_entry *old_entry, *new_entry; |
| 510 | int old_nlink = 0, new_nlink = 0; |
| 511 | int err = -ENOENT; |
| 512 | |
| 513 | f2fs_balance_fs(sbi); |
| 514 | |
| 515 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
| 516 | if (!old_entry) |
| 517 | goto out; |
| 518 | |
| 519 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page); |
| 520 | if (!new_entry) |
| 521 | goto out_old; |
| 522 | |
| 523 | /* prepare for updating ".." directory entry info later */ |
| 524 | if (old_dir != new_dir) { |
| 525 | if (S_ISDIR(old_inode->i_mode)) { |
| 526 | err = -EIO; |
| 527 | old_dir_entry = f2fs_parent_dir(old_inode, |
| 528 | &old_dir_page); |
| 529 | if (!old_dir_entry) |
| 530 | goto out_new; |
| 531 | } |
| 532 | |
| 533 | if (S_ISDIR(new_inode->i_mode)) { |
| 534 | err = -EIO; |
| 535 | new_dir_entry = f2fs_parent_dir(new_inode, |
| 536 | &new_dir_page); |
| 537 | if (!new_dir_entry) |
| 538 | goto out_old_dir; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * If cross rename between file and directory those are not |
| 544 | * in the same directory, we will inc nlink of file's parent |
| 545 | * later, so we should check upper boundary of its nlink. |
| 546 | */ |
| 547 | if ((!old_dir_entry || !new_dir_entry) && |
| 548 | old_dir_entry != new_dir_entry) { |
| 549 | old_nlink = old_dir_entry ? -1 : 1; |
| 550 | new_nlink = -old_nlink; |
| 551 | err = -EMLINK; |
| 552 | if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) || |
| 553 | (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX)) |
| 554 | goto out_new_dir; |
| 555 | } |
| 556 | |
| 557 | f2fs_lock_op(sbi); |
| 558 | |
| 559 | err = update_dent_inode(old_inode, &new_dentry->d_name); |
| 560 | if (err) |
| 561 | goto out_unlock; |
| 562 | |
| 563 | err = update_dent_inode(new_inode, &old_dentry->d_name); |
| 564 | if (err) |
| 565 | goto out_undo; |
| 566 | |
| 567 | /* update ".." directory entry info of old dentry */ |
| 568 | if (old_dir_entry) |
| 569 | f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir); |
| 570 | |
| 571 | /* update ".." directory entry info of new dentry */ |
| 572 | if (new_dir_entry) |
| 573 | f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir); |
| 574 | |
| 575 | /* update directory entry info of old dir inode */ |
| 576 | f2fs_set_link(old_dir, old_entry, old_page, new_inode); |
| 577 | |
| 578 | down_write(&F2FS_I(old_inode)->i_sem); |
| 579 | file_lost_pino(old_inode); |
| 580 | up_write(&F2FS_I(old_inode)->i_sem); |
| 581 | |
| 582 | update_inode_page(old_inode); |
| 583 | |
| 584 | old_dir->i_ctime = CURRENT_TIME; |
| 585 | if (old_nlink) { |
| 586 | down_write(&F2FS_I(old_dir)->i_sem); |
| 587 | if (old_nlink < 0) |
| 588 | drop_nlink(old_dir); |
| 589 | else |
| 590 | inc_nlink(old_dir); |
| 591 | up_write(&F2FS_I(old_dir)->i_sem); |
| 592 | } |
| 593 | mark_inode_dirty(old_dir); |
| 594 | update_inode_page(old_dir); |
| 595 | |
| 596 | /* update directory entry info of new dir inode */ |
| 597 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 598 | |
| 599 | down_write(&F2FS_I(new_inode)->i_sem); |
| 600 | file_lost_pino(new_inode); |
| 601 | up_write(&F2FS_I(new_inode)->i_sem); |
| 602 | |
| 603 | update_inode_page(new_inode); |
| 604 | |
| 605 | new_dir->i_ctime = CURRENT_TIME; |
| 606 | if (new_nlink) { |
| 607 | down_write(&F2FS_I(new_dir)->i_sem); |
| 608 | if (new_nlink < 0) |
| 609 | drop_nlink(new_dir); |
| 610 | else |
| 611 | inc_nlink(new_dir); |
| 612 | up_write(&F2FS_I(new_dir)->i_sem); |
| 613 | } |
| 614 | mark_inode_dirty(new_dir); |
| 615 | update_inode_page(new_dir); |
| 616 | |
| 617 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 618 | |
| 619 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 620 | f2fs_sync_fs(sbi->sb, 1); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 621 | return 0; |
| 622 | out_undo: |
| 623 | /* Still we may fail to recover name info of f2fs_inode here */ |
| 624 | update_dent_inode(old_inode, &old_dentry->d_name); |
| 625 | out_unlock: |
| 626 | f2fs_unlock_op(sbi); |
| 627 | out_new_dir: |
| 628 | if (new_dir_entry) { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 629 | f2fs_dentry_kunmap(new_inode, new_dir_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 630 | f2fs_put_page(new_dir_page, 0); |
| 631 | } |
| 632 | out_old_dir: |
| 633 | if (old_dir_entry) { |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 634 | f2fs_dentry_kunmap(old_inode, old_dir_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 635 | f2fs_put_page(old_dir_page, 0); |
| 636 | } |
| 637 | out_new: |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 638 | f2fs_dentry_kunmap(new_dir, new_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 639 | f2fs_put_page(new_page, 0); |
| 640 | out_old: |
Jaegeuk Kim | 9486ba4 | 2014-11-21 16:36:28 -0800 | [diff] [blame] | 641 | f2fs_dentry_kunmap(old_dir, old_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 642 | f2fs_put_page(old_page, 0); |
| 643 | out: |
| 644 | return err; |
| 645 | } |
| 646 | |
| 647 | static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 648 | struct inode *new_dir, struct dentry *new_dentry, |
| 649 | unsigned int flags) |
| 650 | { |
| 651 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) |
| 652 | return -EINVAL; |
| 653 | |
| 654 | if (flags & RENAME_EXCHANGE) { |
| 655 | return f2fs_cross_rename(old_dir, old_dentry, |
| 656 | new_dir, new_dentry); |
| 657 | } |
| 658 | /* |
| 659 | * VFS has already handled the new dentry existence case, |
| 660 | * here, we just deal with "RENAME_NOREPLACE" as regular rename. |
| 661 | */ |
| 662 | return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 663 | } |
| 664 | |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 665 | static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 666 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 667 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 668 | struct inode *inode; |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 669 | int err; |
| 670 | |
| 671 | inode = f2fs_new_inode(dir, mode); |
| 672 | if (IS_ERR(inode)) |
| 673 | return PTR_ERR(inode); |
| 674 | |
| 675 | inode->i_op = &f2fs_file_inode_operations; |
| 676 | inode->i_fop = &f2fs_file_operations; |
| 677 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 678 | |
| 679 | f2fs_lock_op(sbi); |
| 680 | err = acquire_orphan_inode(sbi); |
| 681 | if (err) |
| 682 | goto out; |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 683 | |
| 684 | err = f2fs_do_tmpfile(inode, dir); |
| 685 | if (err) |
| 686 | goto release_out; |
| 687 | |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 688 | /* |
| 689 | * add this non-linked tmpfile to orphan list, in this way we could |
| 690 | * remove all unused data of tmpfile after abnormal power-off. |
| 691 | */ |
| 692 | add_orphan_inode(sbi, inode->i_ino); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 693 | f2fs_unlock_op(sbi); |
| 694 | |
| 695 | alloc_nid_done(sbi, inode->i_ino); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 696 | d_tmpfile(dentry, inode); |
| 697 | unlock_new_inode(inode); |
| 698 | return 0; |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 699 | |
| 700 | release_out: |
| 701 | release_orphan_inode(sbi); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 702 | out: |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 703 | handle_failed_inode(inode); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 704 | return err; |
| 705 | } |
| 706 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 707 | const struct inode_operations f2fs_dir_inode_operations = { |
| 708 | .create = f2fs_create, |
| 709 | .lookup = f2fs_lookup, |
| 710 | .link = f2fs_link, |
| 711 | .unlink = f2fs_unlink, |
| 712 | .symlink = f2fs_symlink, |
| 713 | .mkdir = f2fs_mkdir, |
| 714 | .rmdir = f2fs_rmdir, |
| 715 | .mknod = f2fs_mknod, |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 716 | .rename2 = f2fs_rename2, |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 717 | .tmpfile = f2fs_tmpfile, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 718 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 719 | .setattr = f2fs_setattr, |
| 720 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 721 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 722 | #ifdef CONFIG_F2FS_FS_XATTR |
| 723 | .setxattr = generic_setxattr, |
| 724 | .getxattr = generic_getxattr, |
| 725 | .listxattr = f2fs_listxattr, |
| 726 | .removexattr = generic_removexattr, |
| 727 | #endif |
| 728 | }; |
| 729 | |
| 730 | const struct inode_operations f2fs_symlink_inode_operations = { |
| 731 | .readlink = generic_readlink, |
| 732 | .follow_link = page_follow_link_light, |
| 733 | .put_link = page_put_link, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 734 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 735 | .setattr = f2fs_setattr, |
| 736 | #ifdef CONFIG_F2FS_FS_XATTR |
| 737 | .setxattr = generic_setxattr, |
| 738 | .getxattr = generic_getxattr, |
| 739 | .listxattr = f2fs_listxattr, |
| 740 | .removexattr = generic_removexattr, |
| 741 | #endif |
| 742 | }; |
| 743 | |
| 744 | const struct inode_operations f2fs_special_inode_operations = { |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 745 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 746 | .setattr = f2fs_setattr, |
| 747 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 748 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 749 | #ifdef CONFIG_F2FS_FS_XATTR |
| 750 | .setxattr = generic_setxattr, |
| 751 | .getxattr = generic_getxattr, |
| 752 | .listxattr = f2fs_listxattr, |
| 753 | .removexattr = generic_removexattr, |
| 754 | #endif |
| 755 | }; |