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