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