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