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 | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 17 | #include <linux/namei.h> |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 18 | #include <linux/quotaops.h> |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 19 | |
| 20 | #include "f2fs.h" |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 21 | #include "node.h" |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 22 | #include "xattr.h" |
| 23 | #include "acl.h" |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 24 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 25 | |
| 26 | static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode) |
| 27 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 28 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 29 | nid_t ino; |
| 30 | struct inode *inode; |
| 31 | bool nid_free = false; |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 32 | int xattr_size = 0; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 33 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 34 | |
Jaegeuk Kim | a014e03 | 2014-06-20 21:44:02 -0700 | [diff] [blame] | 35 | inode = new_inode(dir->i_sb); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 36 | if (!inode) |
| 37 | return ERR_PTR(-ENOMEM); |
| 38 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 39 | f2fs_lock_op(sbi); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 40 | if (!f2fs_alloc_nid(sbi, &ino)) { |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 41 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 42 | err = -ENOSPC; |
| 43 | goto fail; |
| 44 | } |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 45 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 46 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 47 | nid_free = true; |
| 48 | |
Chao Yu | 70ff5df | 2014-05-08 17:09:30 +0800 | [diff] [blame] | 49 | inode_init_owner(inode, dir, mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 50 | |
| 51 | inode->i_ino = ino; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 52 | inode->i_blocks = 0; |
Jaegeuk Kim | b2d1e92 | 2018-10-01 10:45:32 -0700 | [diff] [blame] | 53 | inode->i_mtime = inode->i_atime = inode->i_ctime = |
| 54 | F2FS_I(inode)->i_crtime = current_time(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 55 | inode->i_generation = sbi->s_next_generation++; |
| 56 | |
Chao Yu | 2d96ad5 | 2018-05-07 20:28:52 +0800 | [diff] [blame] | 57 | if (S_ISDIR(inode->i_mode)) |
| 58 | F2FS_I(inode)->i_current_depth = 1; |
| 59 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 60 | err = insert_inode_locked(inode); |
| 61 | if (err) { |
| 62 | err = -EINVAL; |
Jaegeuk Kim | a21c20f | 2015-08-16 12:38:15 -0700 | [diff] [blame] | 63 | goto fail; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 64 | } |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 65 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 66 | if (f2fs_sb_has_project_quota(sbi->sb) && |
Chao Yu | 9bafde6 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 67 | (F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL)) |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 68 | F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid; |
| 69 | else |
| 70 | F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns, |
| 71 | F2FS_DEF_PROJID); |
| 72 | |
| 73 | err = dquot_initialize(inode); |
| 74 | if (err) |
| 75 | goto fail_drop; |
| 76 | |
| 77 | err = dquot_alloc_inode(inode); |
| 78 | if (err) |
| 79 | goto fail_drop; |
| 80 | |
Jaegeuk Kim | 2f17e34 | 2017-11-16 16:59:14 +0800 | [diff] [blame] | 81 | set_inode_flag(inode, FI_NEW_INODE); |
| 82 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 83 | /* If the directory encrypted, then we should encrypt the inode. */ |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 84 | if ((f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) && |
| 85 | f2fs_may_encrypt(inode)) |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 86 | f2fs_set_encrypted_inode(inode); |
| 87 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 88 | if (f2fs_sb_has_extra_attr(sbi->sb)) { |
| 89 | set_inode_flag(inode, FI_EXTRA_ATTR); |
| 90 | F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE; |
| 91 | } |
| 92 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 93 | if (test_opt(sbi, INLINE_XATTR)) |
| 94 | set_inode_flag(inode, FI_INLINE_XATTR); |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 95 | |
Chao Yu | fba48a8 | 2015-12-23 17:51:35 +0800 | [diff] [blame] | 96 | if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode)) |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 97 | set_inode_flag(inode, FI_INLINE_DATA); |
Jaegeuk Kim | 01b960e | 2015-04-23 10:27:21 -0700 | [diff] [blame] | 98 | if (f2fs_may_inline_dentry(inode)) |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 99 | set_inode_flag(inode, FI_INLINE_DENTRY); |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 100 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 101 | if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) { |
| 102 | f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode)); |
| 103 | if (f2fs_has_inline_xattr(inode)) |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 104 | xattr_size = F2FS_OPTION(sbi).inline_xattr_size; |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 105 | /* Otherwise, will be 0 */ |
| 106 | } else if (f2fs_has_inline_xattr(inode) || |
| 107 | f2fs_has_inline_dentry(inode)) { |
| 108 | xattr_size = DEFAULT_INLINE_XATTR_ADDRS; |
| 109 | } |
| 110 | F2FS_I(inode)->i_inline_xattr_size = xattr_size; |
| 111 | |
Jaegeuk Kim | 3e72f72 | 2015-06-19 17:53:26 -0700 | [diff] [blame] | 112 | f2fs_init_extent_tree(inode, NULL); |
| 113 | |
Chao Yu | d5e8f6c | 2015-07-15 17:28:53 +0800 | [diff] [blame] | 114 | stat_inc_inline_xattr(inode); |
Jaegeuk Kim | 2fb2c95 | 2015-04-30 18:58:22 -0700 | [diff] [blame] | 115 | stat_inc_inline_inode(inode); |
| 116 | stat_inc_inline_dir(inode); |
| 117 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 118 | F2FS_I(inode)->i_flags = |
| 119 | f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED); |
| 120 | |
| 121 | if (S_ISDIR(inode->i_mode)) |
Chao Yu | 9bafde6 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 122 | F2FS_I(inode)->i_flags |= F2FS_INDEX_FL; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 123 | |
Chao Yu | 9bafde6 | 2018-04-03 15:08:17 +0800 | [diff] [blame] | 124 | if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL) |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 125 | set_inode_flag(inode, FI_PROJ_INHERIT); |
| 126 | |
Jaegeuk Kim | d70b4f5 | 2013-04-25 13:24:33 +0900 | [diff] [blame] | 127 | trace_f2fs_new_inode(inode, 0); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 128 | return inode; |
| 129 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 130 | fail: |
Jaegeuk Kim | d70b4f5 | 2013-04-25 13:24:33 +0900 | [diff] [blame] | 131 | trace_f2fs_new_inode(inode, err); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 132 | make_bad_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 133 | if (nid_free) |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 134 | set_inode_flag(inode, FI_FREE_NID); |
Jaegeuk Kim | c9b63bd | 2015-06-23 10:36:08 -0700 | [diff] [blame] | 135 | iput(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 136 | return ERR_PTR(err); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 137 | fail_drop: |
| 138 | trace_f2fs_new_inode(inode, err); |
| 139 | dquot_drop(inode); |
| 140 | inode->i_flags |= S_NOQUOTA; |
| 141 | if (nid_free) |
| 142 | set_inode_flag(inode, FI_FREE_NID); |
| 143 | clear_nlink(inode); |
| 144 | unlock_new_inode(inode); |
| 145 | iput(inode); |
| 146 | return ERR_PTR(err); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 147 | } |
| 148 | |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 149 | static int is_extension_exist(const unsigned char *s, const char *sub) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 150 | { |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 151 | size_t slen = strlen(s); |
| 152 | size_t sublen = strlen(sub); |
Chao Yu | 7732c26 | 2016-09-05 12:28:27 +0800 | [diff] [blame] | 153 | int i; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 154 | |
Chao Yu | 741a7be | 2015-07-06 20:30:40 +0800 | [diff] [blame] | 155 | /* |
| 156 | * filename format of multimedia file should be defined as: |
Chao Yu | 7732c26 | 2016-09-05 12:28:27 +0800 | [diff] [blame] | 157 | * "filename + '.' + extension + (optional: '.' + temp extension)". |
Chao Yu | 741a7be | 2015-07-06 20:30:40 +0800 | [diff] [blame] | 158 | */ |
| 159 | if (slen < sublen + 2) |
| 160 | return 0; |
| 161 | |
Chao Yu | 7732c26 | 2016-09-05 12:28:27 +0800 | [diff] [blame] | 162 | for (i = 1; i < slen - sublen; i++) { |
| 163 | if (s[i] != '.') |
| 164 | continue; |
| 165 | if (!strncasecmp(s + i + 1, sub, sublen)) |
| 166 | return 1; |
| 167 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 168 | |
Chao Yu | 7732c26 | 2016-09-05 12:28:27 +0800 | [diff] [blame] | 169 | return 0; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 170 | } |
| 171 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 172 | /* |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 173 | * Set multimedia files as cold files for hot/cold data separation |
| 174 | */ |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 175 | static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 176 | const unsigned char *name) |
| 177 | { |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 178 | __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list; |
| 179 | int i, cold_count, hot_count; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 180 | |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 181 | down_read(&sbi->sb_lock); |
| 182 | |
| 183 | cold_count = le32_to_cpu(sbi->raw_super->extension_count); |
| 184 | hot_count = sbi->raw_super->hot_ext_count; |
| 185 | |
| 186 | for (i = 0; i < cold_count + hot_count; i++) { |
| 187 | if (!is_extension_exist(name, extlist[i])) |
| 188 | continue; |
| 189 | if (i < cold_count) |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 190 | file_set_cold(inode); |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 191 | else |
| 192 | file_set_hot(inode); |
| 193 | break; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 194 | } |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 195 | |
| 196 | up_read(&sbi->sb_lock); |
| 197 | } |
| 198 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 199 | int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 200 | bool hot, bool set) |
| 201 | { |
| 202 | __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list; |
| 203 | int cold_count = le32_to_cpu(sbi->raw_super->extension_count); |
| 204 | int hot_count = sbi->raw_super->hot_ext_count; |
| 205 | int total_count = cold_count + hot_count; |
| 206 | int start, count; |
| 207 | int i; |
| 208 | |
| 209 | if (set) { |
| 210 | if (total_count == F2FS_MAX_EXTENSION) |
| 211 | return -EINVAL; |
| 212 | } else { |
| 213 | if (!hot && !cold_count) |
| 214 | return -EINVAL; |
| 215 | if (hot && !hot_count) |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
| 219 | if (hot) { |
| 220 | start = cold_count; |
| 221 | count = total_count; |
| 222 | } else { |
| 223 | start = 0; |
| 224 | count = cold_count; |
| 225 | } |
| 226 | |
| 227 | for (i = start; i < count; i++) { |
| 228 | if (strcmp(name, extlist[i])) |
| 229 | continue; |
| 230 | |
| 231 | if (set) |
| 232 | return -EINVAL; |
| 233 | |
| 234 | memcpy(extlist[i], extlist[i + 1], |
| 235 | F2FS_EXTENSION_LEN * (total_count - i - 1)); |
| 236 | memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN); |
| 237 | if (hot) |
| 238 | sbi->raw_super->hot_ext_count = hot_count - 1; |
| 239 | else |
| 240 | sbi->raw_super->extension_count = |
| 241 | cpu_to_le32(cold_count - 1); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | if (!set) |
| 246 | return -EINVAL; |
| 247 | |
| 248 | if (hot) { |
Guenter Roeck | 5c8e7ab | 2018-07-01 13:57:06 -0700 | [diff] [blame] | 249 | memcpy(extlist[count], name, strlen(name)); |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 250 | sbi->raw_super->hot_ext_count = hot_count + 1; |
| 251 | } else { |
| 252 | char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN]; |
| 253 | |
| 254 | memcpy(buf, &extlist[cold_count], |
| 255 | F2FS_EXTENSION_LEN * hot_count); |
| 256 | memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN); |
Guenter Roeck | 5c8e7ab | 2018-07-01 13:57:06 -0700 | [diff] [blame] | 257 | memcpy(extlist[cold_count], name, strlen(name)); |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 258 | memcpy(&extlist[cold_count + 1], buf, |
| 259 | F2FS_EXTENSION_LEN * hot_count); |
| 260 | sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1); |
| 261 | } |
| 262 | return 0; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
| 266 | bool excl) |
| 267 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 268 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 269 | struct inode *inode; |
| 270 | nid_t ino = 0; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 271 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 272 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 273 | if (unlikely(f2fs_cp_error(sbi))) |
| 274 | return -EIO; |
| 275 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 276 | err = dquot_initialize(dir); |
| 277 | if (err) |
| 278 | return err; |
| 279 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 280 | inode = f2fs_new_inode(dir, mode); |
| 281 | if (IS_ERR(inode)) |
| 282 | return PTR_ERR(inode); |
| 283 | |
| 284 | if (!test_opt(sbi, DISABLE_EXT_IDENTIFY)) |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 285 | set_file_temperature(sbi, inode, dentry->d_name.name); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 286 | |
| 287 | inode->i_op = &f2fs_file_inode_operations; |
| 288 | inode->i_fop = &f2fs_file_operations; |
| 289 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 290 | ino = inode->i_ino; |
| 291 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 292 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 293 | err = f2fs_add_link(dentry, inode); |
| 294 | if (err) |
| 295 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 296 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 297 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 298 | f2fs_alloc_nid_done(sbi, ino); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 299 | |
Al Viro | 2d2d3f1 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 300 | d_instantiate_new(dentry, inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 301 | |
| 302 | if (IS_DIRSYNC(dir)) |
| 303 | f2fs_sync_fs(sbi->sb, 1); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 304 | |
| 305 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 306 | return 0; |
| 307 | out: |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 308 | f2fs_handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 309 | return err; |
| 310 | } |
| 311 | |
| 312 | static int f2fs_link(struct dentry *old_dentry, struct inode *dir, |
| 313 | struct dentry *dentry) |
| 314 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 315 | struct inode *inode = d_inode(old_dentry); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 316 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 317 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 318 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 319 | if (unlikely(f2fs_cp_error(sbi))) |
| 320 | return -EIO; |
| 321 | |
Jaegeuk Kim | 2f17e34 | 2017-11-16 16:59:14 +0800 | [diff] [blame] | 322 | err = fscrypt_prepare_link(old_dentry, dir, dentry); |
| 323 | if (err) |
| 324 | return err; |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 325 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 326 | if (is_inode_flag_set(dir, FI_PROJ_INHERIT) && |
| 327 | (!projid_eq(F2FS_I(dir)->i_projid, |
| 328 | F2FS_I(old_dentry->d_inode)->i_projid))) |
| 329 | return -EXDEV; |
| 330 | |
| 331 | err = dquot_initialize(dir); |
| 332 | if (err) |
| 333 | return err; |
| 334 | |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 335 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 336 | |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 337 | inode->i_ctime = current_time(inode); |
Jaegeuk Kim | 6f6fd83 | 2013-05-22 12:06:26 +0900 | [diff] [blame] | 338 | ihold(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 339 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 340 | set_inode_flag(inode, FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 341 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 342 | err = f2fs_add_link(dentry, inode); |
| 343 | if (err) |
| 344 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 345 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 346 | |
| 347 | d_instantiate(dentry, inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 348 | |
| 349 | if (IS_DIRSYNC(dir)) |
| 350 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 351 | return 0; |
| 352 | out: |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 353 | clear_inode_flag(inode, FI_INC_LINK); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 354 | iput(inode); |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 355 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 356 | return err; |
| 357 | } |
| 358 | |
| 359 | struct dentry *f2fs_get_parent(struct dentry *child) |
| 360 | { |
| 361 | struct qstr dotdot = QSTR_INIT("..", 2); |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 362 | struct page *page; |
| 363 | unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page); |
| 364 | if (!ino) { |
| 365 | if (IS_ERR(page)) |
| 366 | return ERR_CAST(page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 367 | return ERR_PTR(-ENOENT); |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 368 | } |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 369 | return d_obtain_alias(f2fs_iget(child->d_sb, ino)); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 370 | } |
| 371 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 372 | static int __recover_dot_dentries(struct inode *dir, nid_t pino) |
| 373 | { |
| 374 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 375 | struct qstr dot = QSTR_INIT(".", 1); |
| 376 | struct qstr dotdot = QSTR_INIT("..", 2); |
| 377 | struct f2fs_dir_entry *de; |
| 378 | struct page *page; |
| 379 | int err = 0; |
| 380 | |
Chao Yu | 4e0d836 | 2015-12-30 17:40:31 +0800 | [diff] [blame] | 381 | if (f2fs_readonly(sbi->sb)) { |
| 382 | f2fs_msg(sbi->sb, KERN_INFO, |
| 383 | "skip recovering inline_dots inode (ino:%lu, pino:%u) " |
| 384 | "in readonly mountpoint", dir->i_ino, pino); |
| 385 | return 0; |
| 386 | } |
| 387 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 388 | err = dquot_initialize(dir); |
| 389 | if (err) |
| 390 | return err; |
| 391 | |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 392 | f2fs_balance_fs(sbi, true); |
Chao Yu | d538417 | 2015-12-24 18:03:29 +0800 | [diff] [blame] | 393 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 394 | f2fs_lock_op(sbi); |
| 395 | |
| 396 | de = f2fs_find_entry(dir, &dot, &page); |
| 397 | if (de) { |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 398 | f2fs_put_page(page, 0); |
Jaegeuk Kim | 42d9640 | 2016-05-25 14:29:11 -0700 | [diff] [blame] | 399 | } else if (IS_ERR(page)) { |
| 400 | err = PTR_ERR(page); |
| 401 | goto out; |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 402 | } else { |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 403 | err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 404 | if (err) |
| 405 | goto out; |
| 406 | } |
| 407 | |
| 408 | de = f2fs_find_entry(dir, &dotdot, &page); |
Yunlong Song | 97d7f1c | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 409 | if (de) |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 410 | f2fs_put_page(page, 0); |
Yunlong Song | 97d7f1c | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 411 | else if (IS_ERR(page)) |
Jaegeuk Kim | 42d9640 | 2016-05-25 14:29:11 -0700 | [diff] [blame] | 412 | err = PTR_ERR(page); |
Yunlong Song | 97d7f1c | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 413 | else |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 414 | err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 415 | out: |
Jaegeuk Kim | 205b982 | 2016-05-20 09:52:20 -0700 | [diff] [blame] | 416 | if (!err) |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 417 | clear_inode_flag(dir, FI_INLINE_DOTS); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 418 | |
| 419 | f2fs_unlock_op(sbi); |
| 420 | return err; |
| 421 | } |
| 422 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 423 | static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, |
| 424 | unsigned int flags) |
| 425 | { |
| 426 | struct inode *inode = NULL; |
| 427 | struct f2fs_dir_entry *de; |
| 428 | struct page *page; |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 429 | struct dentry *new; |
| 430 | nid_t ino = -1; |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 431 | int err = 0; |
Liu Xue | 8c2b143 | 2016-02-26 06:39:23 +0000 | [diff] [blame] | 432 | unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir)); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 433 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 434 | trace_f2fs_lookup_start(dir, dentry, flags); |
| 435 | |
Jaegeuk Kim | 2f17e34 | 2017-11-16 16:59:14 +0800 | [diff] [blame] | 436 | err = fscrypt_prepare_lookup(dir, dentry, flags); |
| 437 | if (err) |
| 438 | goto out; |
Jaegeuk Kim | 8074bb5 | 2016-02-23 09:21:37 -0800 | [diff] [blame] | 439 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 440 | if (dentry->d_name.len > F2FS_NAME_LEN) { |
| 441 | err = -ENAMETOOLONG; |
| 442 | goto out; |
| 443 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 444 | |
| 445 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
Jaegeuk Kim | eb4246d | 2016-05-27 10:10:41 -0700 | [diff] [blame] | 446 | if (!de) { |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 447 | if (IS_ERR(page)) { |
| 448 | err = PTR_ERR(page); |
| 449 | goto out; |
| 450 | } |
| 451 | goto out_splice; |
Jaegeuk Kim | eb4246d | 2016-05-27 10:10:41 -0700 | [diff] [blame] | 452 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 453 | |
Jaegeuk Kim | 06957e8 | 2015-04-22 11:40:27 -0700 | [diff] [blame] | 454 | ino = le32_to_cpu(de->ino); |
Jaegeuk Kim | 06957e8 | 2015-04-22 11:40:27 -0700 | [diff] [blame] | 455 | f2fs_put_page(page, 0); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 456 | |
Jaegeuk Kim | 06957e8 | 2015-04-22 11:40:27 -0700 | [diff] [blame] | 457 | inode = f2fs_iget(dir->i_sb, ino); |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 458 | if (IS_ERR(inode)) { |
| 459 | err = PTR_ERR(inode); |
| 460 | goto out; |
| 461 | } |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 462 | |
Liu Xue | 8c2b143 | 2016-02-26 06:39:23 +0000 | [diff] [blame] | 463 | if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) { |
| 464 | err = __recover_dot_dentries(dir, root_ino); |
| 465 | if (err) |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 466 | goto out_iput; |
Liu Xue | 8c2b143 | 2016-02-26 06:39:23 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 469 | if (f2fs_has_inline_dots(inode)) { |
Jaegeuk Kim | 06957e8 | 2015-04-22 11:40:27 -0700 | [diff] [blame] | 470 | err = __recover_dot_dentries(inode, dir->i_ino); |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 471 | if (err) |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 472 | goto out_iput; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 473 | } |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 474 | if (f2fs_encrypted_inode(dir) && |
| 475 | (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) && |
| 476 | !fscrypt_has_permitted_context(dir, inode)) { |
| 477 | f2fs_msg(inode->i_sb, KERN_WARNING, |
| 478 | "Inconsistent encryption contexts: %lu/%lu", |
| 479 | dir->i_ino, inode->i_ino); |
| 480 | err = -EPERM; |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 481 | goto out_iput; |
Jaegeuk Kim | 8074bb5 | 2016-02-23 09:21:37 -0800 | [diff] [blame] | 482 | } |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 483 | out_splice: |
| 484 | new = d_splice_alias(inode, dentry); |
| 485 | if (IS_ERR(new)) |
| 486 | err = PTR_ERR(new); |
| 487 | trace_f2fs_lookup_end(dir, dentry, ino, err); |
| 488 | return new; |
| 489 | out_iput: |
Chao Yu | d726732 | 2016-03-10 22:24:23 +0800 | [diff] [blame] | 490 | iput(inode); |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 491 | out: |
| 492 | trace_f2fs_lookup_end(dir, dentry, ino, err); |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 493 | return ERR_PTR(err); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static int f2fs_unlink(struct inode *dir, struct dentry *dentry) |
| 497 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 498 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 499 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 500 | struct f2fs_dir_entry *de; |
| 501 | struct page *page; |
| 502 | int err = -ENOENT; |
| 503 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 504 | trace_f2fs_unlink_enter(dir, dentry); |
Jaegeuk Kim | 1efef83 | 2012-12-19 16:25:21 +0900 | [diff] [blame] | 505 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 506 | if (unlikely(f2fs_cp_error(sbi))) |
| 507 | return -EIO; |
| 508 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 509 | err = dquot_initialize(dir); |
| 510 | if (err) |
| 511 | return err; |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 512 | err = dquot_initialize(inode); |
| 513 | if (err) |
| 514 | return err; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 515 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 516 | de = f2fs_find_entry(dir, &dentry->d_name, &page); |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 517 | if (!de) { |
| 518 | if (IS_ERR(page)) |
| 519 | err = PTR_ERR(page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 520 | goto fail; |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 521 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 522 | |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 523 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 524 | |
Jaegeuk Kim | ccaaca2 | 2013-10-08 10:19:28 +0900 | [diff] [blame] | 525 | f2fs_lock_op(sbi); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 526 | err = f2fs_acquire_orphan_inode(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 527 | if (err) { |
Jaegeuk Kim | ccaaca2 | 2013-10-08 10:19:28 +0900 | [diff] [blame] | 528 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 529 | f2fs_put_page(page, 0); |
| 530 | goto fail; |
| 531 | } |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 532 | f2fs_delete_entry(de, page, dir, inode); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 533 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 534 | |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 535 | if (IS_DIRSYNC(dir)) |
| 536 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 537 | fail: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 538 | trace_f2fs_unlink_exit(inode, err); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 539 | return err; |
| 540 | } |
| 541 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 542 | static const char *f2fs_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 543 | struct inode *inode, |
| 544 | struct delayed_call *done) |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 545 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 546 | const char *link = page_get_link(dentry, inode, done); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 547 | if (!IS_ERR(link) && !*link) { |
| 548 | /* this is broken symlink case */ |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 549 | do_delayed_call(done); |
| 550 | clear_delayed_call(done); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 551 | link = ERR_PTR(-ENOENT); |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 552 | } |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 553 | return link; |
Jaegeuk Kim | feb7cbb | 2015-04-15 13:49:55 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 556 | static int f2fs_symlink(struct inode *dir, struct dentry *dentry, |
| 557 | const char *symname) |
| 558 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 559 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 560 | struct inode *inode; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 561 | size_t len = strlen(symname); |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 562 | struct fscrypt_str disk_link; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 563 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 564 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 565 | if (unlikely(f2fs_cp_error(sbi))) |
| 566 | return -EIO; |
| 567 | |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 568 | err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize, |
| 569 | &disk_link); |
| 570 | if (err) |
| 571 | return err; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 572 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 573 | err = dquot_initialize(dir); |
| 574 | if (err) |
| 575 | return err; |
| 576 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 577 | inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 578 | if (IS_ERR(inode)) |
| 579 | return PTR_ERR(inode); |
| 580 | |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 581 | if (IS_ENCRYPTED(inode)) |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 582 | inode->i_op = &f2fs_encrypted_symlink_inode_operations; |
| 583 | else |
| 584 | inode->i_op = &f2fs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 585 | inode_nohighmem(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 586 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 587 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 588 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 589 | err = f2fs_add_link(dentry, inode); |
| 590 | if (err) |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 591 | goto out_f2fs_handle_failed_inode; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 592 | f2fs_unlock_op(sbi); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 593 | f2fs_alloc_nid_done(sbi, inode->i_ino); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 594 | |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 595 | err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link); |
| 596 | if (err) |
| 597 | goto err_out; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 598 | |
Chao Yu | 922ec35 | 2016-02-15 17:54:26 +0800 | [diff] [blame] | 599 | err = page_symlink(inode, disk_link.name, disk_link.len); |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 600 | |
| 601 | err_out: |
Al Viro | 2d2d3f1 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 602 | d_instantiate_new(dentry, inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 603 | |
Jaegeuk Kim | d0cae97 | 2015-04-15 13:37:53 -0700 | [diff] [blame] | 604 | /* |
| 605 | * Let's flush symlink data in order to avoid broken symlink as much as |
| 606 | * possible. Nevertheless, fsyncing is the best way, but there is no |
| 607 | * way to get a file descriptor in order to flush that. |
| 608 | * |
| 609 | * Note that, it needs to do dir->fsync to make this recoverable. |
| 610 | * If the symlink path is stored into inline_data, there is no |
| 611 | * performance regression. |
| 612 | */ |
Chao Yu | a6be014 | 2015-10-22 18:23:08 +0800 | [diff] [blame] | 613 | if (!err) { |
Chao Yu | 922ec35 | 2016-02-15 17:54:26 +0800 | [diff] [blame] | 614 | filemap_write_and_wait_range(inode->i_mapping, 0, |
| 615 | disk_link.len - 1); |
Jaegeuk Kim | d0cae97 | 2015-04-15 13:37:53 -0700 | [diff] [blame] | 616 | |
Chao Yu | a6be014 | 2015-10-22 18:23:08 +0800 | [diff] [blame] | 617 | if (IS_DIRSYNC(dir)) |
| 618 | f2fs_sync_fs(sbi->sb, 1); |
| 619 | } else { |
| 620 | f2fs_unlink(dir, dentry); |
| 621 | } |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 622 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 623 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 624 | goto out_free_encrypted_link; |
| 625 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 626 | out_f2fs_handle_failed_inode: |
| 627 | f2fs_handle_failed_inode(inode); |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 628 | out_free_encrypted_link: |
| 629 | if (disk_link.name != (unsigned char *)symname) |
| 630 | kfree(disk_link.name); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 631 | return err; |
| 632 | } |
| 633 | |
| 634 | static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 635 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 636 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 637 | struct inode *inode; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 638 | int err; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 639 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 640 | if (unlikely(f2fs_cp_error(sbi))) |
| 641 | return -EIO; |
| 642 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 643 | err = dquot_initialize(dir); |
| 644 | if (err) |
| 645 | return err; |
| 646 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 647 | inode = f2fs_new_inode(dir, S_IFDIR | mode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 648 | if (IS_ERR(inode)) |
Namjae Jeon | 61412b6 | 2012-12-01 10:56:25 +0900 | [diff] [blame] | 649 | return PTR_ERR(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 650 | |
| 651 | inode->i_op = &f2fs_dir_inode_operations; |
| 652 | inode->i_fop = &f2fs_dir_operations; |
| 653 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
Yunlong Song | 97d7f1c | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 654 | inode_nohighmem(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 655 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 656 | set_inode_flag(inode, FI_INC_LINK); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 657 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 658 | err = f2fs_add_link(dentry, inode); |
| 659 | if (err) |
| 660 | goto out_fail; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 661 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 662 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 663 | f2fs_alloc_nid_done(sbi, inode->i_ino); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 664 | |
Al Viro | 2d2d3f1 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 665 | d_instantiate_new(dentry, inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 666 | |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 667 | if (IS_DIRSYNC(dir)) |
| 668 | f2fs_sync_fs(sbi->sb, 1); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 669 | |
| 670 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 671 | return 0; |
| 672 | |
| 673 | out_fail: |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 674 | clear_inode_flag(inode, FI_INC_LINK); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 675 | f2fs_handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 676 | return err; |
| 677 | } |
| 678 | |
| 679 | static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) |
| 680 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 681 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 682 | if (f2fs_empty_dir(inode)) |
| 683 | return f2fs_unlink(dir, dentry); |
| 684 | return -ENOTEMPTY; |
| 685 | } |
| 686 | |
| 687 | static int f2fs_mknod(struct inode *dir, struct dentry *dentry, |
| 688 | umode_t mode, dev_t rdev) |
| 689 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 690 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 691 | struct inode *inode; |
| 692 | int err = 0; |
| 693 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 694 | if (unlikely(f2fs_cp_error(sbi))) |
| 695 | return -EIO; |
| 696 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 697 | err = dquot_initialize(dir); |
| 698 | if (err) |
| 699 | return err; |
| 700 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 701 | inode = f2fs_new_inode(dir, mode); |
| 702 | if (IS_ERR(inode)) |
| 703 | return PTR_ERR(inode); |
| 704 | |
| 705 | init_special_inode(inode, inode->i_mode, rdev); |
| 706 | inode->i_op = &f2fs_special_inode_operations; |
| 707 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 708 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 709 | err = f2fs_add_link(dentry, inode); |
| 710 | if (err) |
| 711 | goto out; |
Jaegeuk Kim | 44c1615 | 2014-09-25 11:55:53 -0700 | [diff] [blame] | 712 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 713 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 714 | f2fs_alloc_nid_done(sbi, inode->i_ino); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 715 | |
Al Viro | 2d2d3f1 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 716 | d_instantiate_new(dentry, inode); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 717 | |
| 718 | if (IS_DIRSYNC(dir)) |
| 719 | f2fs_sync_fs(sbi->sb, 1); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 720 | |
| 721 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 722 | return 0; |
| 723 | out: |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 724 | f2fs_handle_failed_inode(inode); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 725 | return err; |
| 726 | } |
| 727 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 728 | static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry, |
| 729 | umode_t mode, struct inode **whiteout) |
| 730 | { |
| 731 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 732 | struct inode *inode; |
| 733 | int err; |
| 734 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 735 | err = dquot_initialize(dir); |
| 736 | if (err) |
| 737 | return err; |
| 738 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 739 | inode = f2fs_new_inode(dir, mode); |
| 740 | if (IS_ERR(inode)) |
| 741 | return PTR_ERR(inode); |
| 742 | |
| 743 | if (whiteout) { |
| 744 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); |
| 745 | inode->i_op = &f2fs_special_inode_operations; |
| 746 | } else { |
| 747 | inode->i_op = &f2fs_file_inode_operations; |
| 748 | inode->i_fop = &f2fs_file_operations; |
| 749 | inode->i_mapping->a_ops = &f2fs_dblock_aops; |
| 750 | } |
| 751 | |
| 752 | f2fs_lock_op(sbi); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 753 | err = f2fs_acquire_orphan_inode(sbi); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 754 | if (err) |
| 755 | goto out; |
| 756 | |
| 757 | err = f2fs_do_tmpfile(inode, dir); |
| 758 | if (err) |
| 759 | goto release_out; |
| 760 | |
| 761 | /* |
| 762 | * add this non-linked tmpfile to orphan list, in this way we could |
| 763 | * remove all unused data of tmpfile after abnormal power-off. |
| 764 | */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 765 | f2fs_add_orphan_inode(inode); |
| 766 | f2fs_alloc_nid_done(sbi, inode->i_ino); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 767 | |
| 768 | if (whiteout) { |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 769 | f2fs_i_links_write(inode, false); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 770 | *whiteout = inode; |
| 771 | } else { |
| 772 | d_tmpfile(dentry, inode); |
| 773 | } |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 774 | /* link_count was changed by d_tmpfile as well. */ |
| 775 | f2fs_unlock_op(sbi); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 776 | unlock_new_inode(inode); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 777 | |
| 778 | f2fs_balance_fs(sbi, true); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 779 | return 0; |
| 780 | |
| 781 | release_out: |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 782 | f2fs_release_orphan_inode(sbi); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 783 | out: |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 784 | f2fs_handle_failed_inode(inode); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 785 | return err; |
| 786 | } |
| 787 | |
| 788 | static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 789 | { |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 790 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 791 | |
| 792 | if (unlikely(f2fs_cp_error(sbi))) |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 793 | return -EIO; |
| 794 | |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 795 | if (f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 796 | int err = fscrypt_get_encryption_info(dir); |
Jaegeuk Kim | 304eecc | 2015-05-19 16:11:40 -0700 | [diff] [blame] | 797 | if (err) |
| 798 | return err; |
| 799 | } |
| 800 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 801 | return __f2fs_tmpfile(dir, dentry, mode, NULL); |
| 802 | } |
| 803 | |
| 804 | static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout) |
| 805 | { |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 806 | if (unlikely(f2fs_cp_error(F2FS_I_SB(dir)))) |
| 807 | return -EIO; |
| 808 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 809 | return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout); |
| 810 | } |
| 811 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 812 | static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 813 | struct inode *new_dir, struct dentry *new_dentry, |
| 814 | unsigned int flags) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 815 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 816 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 817 | struct inode *old_inode = d_inode(old_dentry); |
| 818 | struct inode *new_inode = d_inode(new_dentry); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 819 | struct inode *whiteout = NULL; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 820 | struct page *old_dir_page; |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 821 | struct page *old_page, *new_page = NULL; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 822 | struct f2fs_dir_entry *old_dir_entry = NULL; |
| 823 | struct f2fs_dir_entry *old_entry; |
| 824 | struct f2fs_dir_entry *new_entry; |
Chao Yu | 993a049 | 2016-02-17 16:47:05 +0800 | [diff] [blame] | 825 | bool is_old_inline = f2fs_has_inline_dentry(old_dir); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 826 | int err = -ENOENT; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 827 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 828 | if (unlikely(f2fs_cp_error(sbi))) |
| 829 | return -EIO; |
| 830 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 831 | if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && |
| 832 | (!projid_eq(F2FS_I(new_dir)->i_projid, |
| 833 | F2FS_I(old_dentry->d_inode)->i_projid))) |
| 834 | return -EXDEV; |
| 835 | |
| 836 | err = dquot_initialize(old_dir); |
| 837 | if (err) |
| 838 | goto out; |
| 839 | |
| 840 | err = dquot_initialize(new_dir); |
| 841 | if (err) |
| 842 | goto out; |
| 843 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 844 | if (new_inode) { |
| 845 | err = dquot_initialize(new_inode); |
| 846 | if (err) |
| 847 | goto out; |
| 848 | } |
| 849 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 850 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 851 | if (!old_entry) { |
| 852 | if (IS_ERR(old_page)) |
| 853 | err = PTR_ERR(old_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 854 | goto out; |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 855 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 856 | |
| 857 | if (S_ISDIR(old_inode->i_mode)) { |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 858 | old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); |
Jaegeuk Kim | 3e19886 | 2016-06-09 14:57:19 -0700 | [diff] [blame] | 859 | if (!old_dir_entry) { |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 860 | if (IS_ERR(old_dir_page)) |
| 861 | err = PTR_ERR(old_dir_page); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 862 | goto out_old; |
Jaegeuk Kim | 3e19886 | 2016-06-09 14:57:19 -0700 | [diff] [blame] | 863 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 864 | } |
| 865 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 866 | if (flags & RENAME_WHITEOUT) { |
| 867 | err = f2fs_create_whiteout(old_dir, &whiteout); |
| 868 | if (err) |
| 869 | goto out_dir; |
| 870 | } |
| 871 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 872 | if (new_inode) { |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 873 | |
| 874 | err = -ENOTEMPTY; |
| 875 | if (old_dir_entry && !f2fs_empty_dir(new_inode)) |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 876 | goto out_whiteout; |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 877 | |
| 878 | err = -ENOENT; |
| 879 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, |
| 880 | &new_page); |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 881 | if (!new_entry) { |
| 882 | if (IS_ERR(new_page)) |
| 883 | err = PTR_ERR(new_page); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 884 | goto out_whiteout; |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 885 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 886 | |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 887 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 888 | |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 889 | f2fs_lock_op(sbi); |
| 890 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 891 | err = f2fs_acquire_orphan_inode(sbi); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 892 | if (err) |
| 893 | goto put_out_dir; |
| 894 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 895 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 896 | |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 897 | new_inode->i_ctime = current_time(new_inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 898 | down_write(&F2FS_I(new_inode)->i_sem); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 899 | if (old_dir_entry) |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 900 | f2fs_i_links_write(new_inode, false); |
| 901 | f2fs_i_links_write(new_inode, false); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 902 | up_write(&F2FS_I(new_inode)->i_sem); |
| 903 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 904 | if (!new_inode->i_nlink) |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 905 | f2fs_add_orphan_inode(new_inode); |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 906 | else |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 907 | f2fs_release_orphan_inode(sbi); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 908 | } else { |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 909 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 910 | |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 911 | f2fs_lock_op(sbi); |
| 912 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 913 | err = f2fs_add_link(new_dentry, old_inode); |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 914 | if (err) { |
| 915 | f2fs_unlock_op(sbi); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 916 | goto out_whiteout; |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 917 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 918 | |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 919 | if (old_dir_entry) |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 920 | f2fs_i_links_write(new_dir, true); |
Chao Yu | 993a049 | 2016-02-17 16:47:05 +0800 | [diff] [blame] | 921 | |
| 922 | /* |
| 923 | * old entry and new entry can locate in the same inline |
| 924 | * dentry in inode, when attaching new entry in inline dentry, |
| 925 | * it could force inline dentry conversion, after that, |
| 926 | * old_entry and old_page will point to wrong address, in |
| 927 | * order to avoid this, let's do the check and update here. |
| 928 | */ |
| 929 | if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) { |
| 930 | f2fs_put_page(old_page, 0); |
| 931 | old_page = NULL; |
| 932 | |
| 933 | old_entry = f2fs_find_entry(old_dir, |
| 934 | &old_dentry->d_name, &old_page); |
| 935 | if (!old_entry) { |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 936 | err = -ENOENT; |
| 937 | if (IS_ERR(old_page)) |
| 938 | err = PTR_ERR(old_page); |
Chao Yu | 993a049 | 2016-02-17 16:47:05 +0800 | [diff] [blame] | 939 | f2fs_unlock_op(sbi); |
| 940 | goto out_whiteout; |
| 941 | } |
| 942 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 943 | } |
| 944 | |
Jaegeuk Kim | b2c0829 | 2014-06-30 18:09:55 +0900 | [diff] [blame] | 945 | down_write(&F2FS_I(old_inode)->i_sem); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 946 | if (!old_dir_entry || whiteout) |
| 947 | file_lost_pino(old_inode); |
| 948 | else |
| 949 | F2FS_I(old_inode)->i_pino = new_dir->i_ino; |
Jaegeuk Kim | b2c0829 | 2014-06-30 18:09:55 +0900 | [diff] [blame] | 950 | up_write(&F2FS_I(old_inode)->i_sem); |
| 951 | |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 952 | old_inode->i_ctime = current_time(old_inode); |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 953 | f2fs_mark_inode_dirty_sync(old_inode, false); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 954 | |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 955 | f2fs_delete_entry(old_entry, old_page, old_dir, NULL); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 956 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 957 | if (whiteout) { |
| 958 | whiteout->i_state |= I_LINKABLE; |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 959 | set_inode_flag(whiteout, FI_INC_LINK); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 960 | err = f2fs_add_link(old_dentry, whiteout); |
| 961 | if (err) |
| 962 | goto put_out_dir; |
| 963 | whiteout->i_state &= ~I_LINKABLE; |
| 964 | iput(whiteout); |
| 965 | } |
| 966 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 967 | if (old_dir_entry) { |
Yunlong Song | 97d7f1c | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 968 | if (old_dir != new_dir && !whiteout) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 969 | f2fs_set_link(old_inode, old_dir_entry, |
| 970 | old_dir_page, new_dir); |
Yunlong Song | 97d7f1c | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 971 | else |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 972 | f2fs_put_page(old_dir_page, 0); |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 973 | f2fs_i_links_write(old_dir, false); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 974 | } |
Jaegeuk Kim | d487b15 | 2018-04-24 22:43:01 -0600 | [diff] [blame] | 975 | if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) { |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 976 | f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO); |
Jaegeuk Kim | d487b15 | 2018-04-24 22:43:01 -0600 | [diff] [blame] | 977 | if (S_ISDIR(old_inode->i_mode)) |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 978 | f2fs_add_ino_entry(sbi, old_inode->i_ino, |
| 979 | TRANS_DIR_INO); |
Jaegeuk Kim | d487b15 | 2018-04-24 22:43:01 -0600 | [diff] [blame] | 980 | } |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 981 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 982 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 983 | |
| 984 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 985 | f2fs_sync_fs(sbi->sb, 1); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 986 | return 0; |
| 987 | |
Jaegeuk Kim | cbd56e7 | 2013-07-30 11:36:53 +0900 | [diff] [blame] | 988 | put_out_dir: |
Chao Yu | 1256010 | 2014-06-24 14:16:24 +0800 | [diff] [blame] | 989 | f2fs_unlock_op(sbi); |
Yunlong Song | 97d7f1c | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 990 | if (new_page) |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 991 | f2fs_put_page(new_page, 0); |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 992 | out_whiteout: |
| 993 | if (whiteout) |
| 994 | iput(whiteout); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 995 | out_dir: |
Yunlong Song | 97d7f1c | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 996 | if (old_dir_entry) |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 997 | f2fs_put_page(old_dir_page, 0); |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 998 | out_old: |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 999 | f2fs_put_page(old_page, 0); |
| 1000 | out: |
| 1001 | return err; |
| 1002 | } |
| 1003 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1004 | static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1005 | struct inode *new_dir, struct dentry *new_dentry) |
| 1006 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1007 | struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1008 | struct inode *old_inode = d_inode(old_dentry); |
| 1009 | struct inode *new_inode = d_inode(new_dentry); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1010 | struct page *old_dir_page, *new_dir_page; |
| 1011 | struct page *old_page, *new_page; |
| 1012 | struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; |
| 1013 | struct f2fs_dir_entry *old_entry, *new_entry; |
| 1014 | int old_nlink = 0, new_nlink = 0; |
| 1015 | int err = -ENOENT; |
| 1016 | |
Jaegeuk Kim | 8ae26d1 | 2017-09-23 17:02:18 +0800 | [diff] [blame] | 1017 | if (unlikely(f2fs_cp_error(sbi))) |
| 1018 | return -EIO; |
| 1019 | |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 1020 | if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && |
| 1021 | !projid_eq(F2FS_I(new_dir)->i_projid, |
| 1022 | F2FS_I(old_dentry->d_inode)->i_projid)) || |
| 1023 | (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && |
| 1024 | !projid_eq(F2FS_I(old_dir)->i_projid, |
| 1025 | F2FS_I(new_dentry->d_inode)->i_projid))) |
| 1026 | return -EXDEV; |
| 1027 | |
| 1028 | err = dquot_initialize(old_dir); |
| 1029 | if (err) |
| 1030 | goto out; |
| 1031 | |
| 1032 | err = dquot_initialize(new_dir); |
| 1033 | if (err) |
| 1034 | goto out; |
| 1035 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1036 | old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 1037 | if (!old_entry) { |
| 1038 | if (IS_ERR(old_page)) |
| 1039 | err = PTR_ERR(old_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1040 | goto out; |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 1041 | } |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1042 | |
| 1043 | new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page); |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 1044 | if (!new_entry) { |
| 1045 | if (IS_ERR(new_page)) |
| 1046 | err = PTR_ERR(new_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1047 | goto out_old; |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 1048 | } |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1049 | |
| 1050 | /* prepare for updating ".." directory entry info later */ |
| 1051 | if (old_dir != new_dir) { |
| 1052 | if (S_ISDIR(old_inode->i_mode)) { |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1053 | old_dir_entry = f2fs_parent_dir(old_inode, |
| 1054 | &old_dir_page); |
Jaegeuk Kim | 3e19886 | 2016-06-09 14:57:19 -0700 | [diff] [blame] | 1055 | if (!old_dir_entry) { |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 1056 | if (IS_ERR(old_dir_page)) |
| 1057 | err = PTR_ERR(old_dir_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1058 | goto out_new; |
Jaegeuk Kim | 3e19886 | 2016-06-09 14:57:19 -0700 | [diff] [blame] | 1059 | } |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | if (S_ISDIR(new_inode->i_mode)) { |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1063 | new_dir_entry = f2fs_parent_dir(new_inode, |
| 1064 | &new_dir_page); |
Jaegeuk Kim | 3e19886 | 2016-06-09 14:57:19 -0700 | [diff] [blame] | 1065 | if (!new_dir_entry) { |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 1066 | if (IS_ERR(new_dir_page)) |
| 1067 | err = PTR_ERR(new_dir_page); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1068 | goto out_old_dir; |
Jaegeuk Kim | 3e19886 | 2016-06-09 14:57:19 -0700 | [diff] [blame] | 1069 | } |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | * If cross rename between file and directory those are not |
| 1075 | * in the same directory, we will inc nlink of file's parent |
| 1076 | * later, so we should check upper boundary of its nlink. |
| 1077 | */ |
| 1078 | if ((!old_dir_entry || !new_dir_entry) && |
| 1079 | old_dir_entry != new_dir_entry) { |
| 1080 | old_nlink = old_dir_entry ? -1 : 1; |
| 1081 | new_nlink = -old_nlink; |
| 1082 | err = -EMLINK; |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 1083 | if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) || |
| 1084 | (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX)) |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1085 | goto out_new_dir; |
| 1086 | } |
| 1087 | |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 1088 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 00623e6 | 2015-12-22 11:56:08 -0800 | [diff] [blame] | 1089 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1090 | f2fs_lock_op(sbi); |
| 1091 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1092 | /* update ".." directory entry info of old dentry */ |
| 1093 | if (old_dir_entry) |
| 1094 | f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir); |
| 1095 | |
| 1096 | /* update ".." directory entry info of new dentry */ |
| 1097 | if (new_dir_entry) |
| 1098 | f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir); |
| 1099 | |
| 1100 | /* update directory entry info of old dir inode */ |
| 1101 | f2fs_set_link(old_dir, old_entry, old_page, new_inode); |
| 1102 | |
| 1103 | down_write(&F2FS_I(old_inode)->i_sem); |
| 1104 | file_lost_pino(old_inode); |
| 1105 | up_write(&F2FS_I(old_inode)->i_sem); |
| 1106 | |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 1107 | old_dir->i_ctime = current_time(old_dir); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1108 | if (old_nlink) { |
| 1109 | down_write(&F2FS_I(old_dir)->i_sem); |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 1110 | f2fs_i_links_write(old_dir, old_nlink > 0); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1111 | up_write(&F2FS_I(old_dir)->i_sem); |
| 1112 | } |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 1113 | f2fs_mark_inode_dirty_sync(old_dir, false); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1114 | |
| 1115 | /* update directory entry info of new dir inode */ |
| 1116 | f2fs_set_link(new_dir, new_entry, new_page, old_inode); |
| 1117 | |
| 1118 | down_write(&F2FS_I(new_inode)->i_sem); |
| 1119 | file_lost_pino(new_inode); |
| 1120 | up_write(&F2FS_I(new_inode)->i_sem); |
| 1121 | |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 1122 | new_dir->i_ctime = current_time(new_dir); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1123 | if (new_nlink) { |
| 1124 | down_write(&F2FS_I(new_dir)->i_sem); |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 1125 | f2fs_i_links_write(new_dir, new_nlink > 0); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1126 | up_write(&F2FS_I(new_dir)->i_sem); |
| 1127 | } |
Hyojun Kim | 63da420 | 2017-10-06 17:10:08 -0700 | [diff] [blame] | 1128 | f2fs_mark_inode_dirty_sync(new_dir, false); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1129 | |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 1130 | if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) { |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1131 | f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO); |
| 1132 | f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO); |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 1133 | } |
Jaegeuk Kim | 2f17e34 | 2017-11-16 16:59:14 +0800 | [diff] [blame] | 1134 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1135 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b7e1d80 | 2014-11-09 22:15:31 -0800 | [diff] [blame] | 1136 | |
| 1137 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 1138 | f2fs_sync_fs(sbi->sb, 1); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1139 | return 0; |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1140 | out_new_dir: |
| 1141 | if (new_dir_entry) { |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1142 | f2fs_put_page(new_dir_page, 0); |
| 1143 | } |
| 1144 | out_old_dir: |
| 1145 | if (old_dir_entry) { |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1146 | f2fs_put_page(old_dir_page, 0); |
| 1147 | } |
| 1148 | out_new: |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1149 | f2fs_put_page(new_page, 0); |
| 1150 | out_old: |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1151 | f2fs_put_page(old_page, 0); |
| 1152 | out: |
| 1153 | return err; |
| 1154 | } |
| 1155 | |
| 1156 | static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 1157 | struct inode *new_dir, struct dentry *new_dentry, |
| 1158 | unsigned int flags) |
| 1159 | { |
Jaegeuk Kim | 2f17e34 | 2017-11-16 16:59:14 +0800 | [diff] [blame] | 1160 | int err; |
| 1161 | |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 1162 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1163 | return -EINVAL; |
| 1164 | |
Jaegeuk Kim | 2f17e34 | 2017-11-16 16:59:14 +0800 | [diff] [blame] | 1165 | err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry, |
| 1166 | flags); |
| 1167 | if (err) |
| 1168 | return err; |
| 1169 | |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1170 | if (flags & RENAME_EXCHANGE) { |
| 1171 | return f2fs_cross_rename(old_dir, old_dentry, |
| 1172 | new_dir, new_dentry); |
| 1173 | } |
| 1174 | /* |
| 1175 | * VFS has already handled the new dentry existence case, |
| 1176 | * here, we just deal with "RENAME_NOREPLACE" as regular rename. |
| 1177 | */ |
Chao Yu | 7e01e7ad | 2015-05-19 17:37:26 +0800 | [diff] [blame] | 1178 | return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Chao Yu | 32f9bc2 | 2014-07-12 19:13:54 +0800 | [diff] [blame] | 1179 | } |
| 1180 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 1181 | static const char *f2fs_encrypted_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 1182 | struct inode *inode, |
| 1183 | struct delayed_call *done) |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 1184 | { |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 1185 | struct page *page; |
| 1186 | const char *target; |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 1187 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 1188 | if (!dentry) |
| 1189 | return ERR_PTR(-ECHILD); |
| 1190 | |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 1191 | page = read_mapping_page(inode->i_mapping, 0, NULL); |
| 1192 | if (IS_ERR(page)) |
| 1193 | return ERR_CAST(page); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 1194 | |
Jaegeuk Kim | d919765 | 2018-01-05 10:44:52 -0800 | [diff] [blame] | 1195 | target = fscrypt_get_symlink(inode, page_address(page), |
| 1196 | inode->i_sb->s_blocksize, done); |
| 1197 | put_page(page); |
| 1198 | return target; |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 1199 | } |
| 1200 | |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 1201 | const struct inode_operations f2fs_encrypted_symlink_inode_operations = { |
| 1202 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 1203 | .get_link = f2fs_encrypted_get_link, |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 1204 | .getattr = f2fs_getattr, |
| 1205 | .setattr = f2fs_setattr, |
Chao Yu | 3a9e643 | 2015-12-31 18:20:10 +0800 | [diff] [blame] | 1206 | #ifdef CONFIG_F2FS_FS_XATTR |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 1207 | .listxattr = f2fs_listxattr, |
Chao Yu | 3a9e643 | 2015-12-31 18:20:10 +0800 | [diff] [blame] | 1208 | #endif |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 1209 | }; |
Jaegeuk Kim | cbaf042 | 2015-04-29 15:10:53 -0700 | [diff] [blame] | 1210 | |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1211 | const struct inode_operations f2fs_dir_inode_operations = { |
| 1212 | .create = f2fs_create, |
| 1213 | .lookup = f2fs_lookup, |
| 1214 | .link = f2fs_link, |
| 1215 | .unlink = f2fs_unlink, |
| 1216 | .symlink = f2fs_symlink, |
| 1217 | .mkdir = f2fs_mkdir, |
| 1218 | .rmdir = f2fs_rmdir, |
| 1219 | .mknod = f2fs_mknod, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1220 | .rename = f2fs_rename2, |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 1221 | .tmpfile = f2fs_tmpfile, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1222 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1223 | .setattr = f2fs_setattr, |
| 1224 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 1225 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1226 | #ifdef CONFIG_F2FS_FS_XATTR |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1227 | .listxattr = f2fs_listxattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1228 | #endif |
| 1229 | }; |
| 1230 | |
| 1231 | const struct inode_operations f2fs_symlink_inode_operations = { |
| 1232 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 1233 | .get_link = f2fs_get_link, |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1234 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1235 | .setattr = f2fs_setattr, |
| 1236 | #ifdef CONFIG_F2FS_FS_XATTR |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1237 | .listxattr = f2fs_listxattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1238 | #endif |
| 1239 | }; |
| 1240 | |
| 1241 | const struct inode_operations f2fs_special_inode_operations = { |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 1242 | .getattr = f2fs_getattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1243 | .setattr = f2fs_setattr, |
| 1244 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 1245 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1246 | #ifdef CONFIG_F2FS_FS_XATTR |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1247 | .listxattr = f2fs_listxattr, |
Jaegeuk Kim | 57397d8 | 2012-11-02 17:11:10 +0900 | [diff] [blame] | 1248 | #endif |
| 1249 | }; |