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 | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 3 | * fs/f2fs/dir.c |
| 4 | * |
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 | * http://www.samsung.com/ |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 7 | */ |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 8 | #include <asm/unaligned.h> |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 9 | #include <linux/fs.h> |
| 10 | #include <linux/f2fs_fs.h> |
Chao Yu | 4414dea | 2017-10-13 18:01:34 +0800 | [diff] [blame] | 11 | #include <linux/sched/signal.h> |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 12 | #include <linux/unicode.h> |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 13 | #include "f2fs.h" |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 14 | #include "node.h" |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 15 | #include "acl.h" |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 16 | #include "xattr.h" |
Chao Yu | e97a3c4 | 2017-10-13 18:01:33 +0800 | [diff] [blame] | 17 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 18 | |
| 19 | static unsigned long dir_blocks(struct inode *inode) |
| 20 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 21 | return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1)) |
| 22 | >> PAGE_SHIFT; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 23 | } |
| 24 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 25 | static unsigned int dir_buckets(unsigned int level, int dir_level) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 26 | { |
Chao Yu | bfec07d | 2014-05-28 08:56:09 +0800 | [diff] [blame] | 27 | if (level + dir_level < MAX_DIR_HASH_DEPTH / 2) |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 28 | return 1 << (level + dir_level); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 29 | else |
Chao Yu | bfec07d | 2014-05-28 08:56:09 +0800 | [diff] [blame] | 30 | return MAX_DIR_BUCKETS; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static unsigned int bucket_blocks(unsigned int level) |
| 34 | { |
| 35 | if (level < MAX_DIR_HASH_DEPTH / 2) |
| 36 | return 2; |
| 37 | else |
| 38 | return 4; |
| 39 | } |
| 40 | |
Chao Yu | ebfa732 | 2016-09-18 23:30:03 +0800 | [diff] [blame] | 41 | static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 42 | [F2FS_FT_UNKNOWN] = DT_UNKNOWN, |
| 43 | [F2FS_FT_REG_FILE] = DT_REG, |
| 44 | [F2FS_FT_DIR] = DT_DIR, |
| 45 | [F2FS_FT_CHRDEV] = DT_CHR, |
| 46 | [F2FS_FT_BLKDEV] = DT_BLK, |
| 47 | [F2FS_FT_FIFO] = DT_FIFO, |
| 48 | [F2FS_FT_SOCK] = DT_SOCK, |
| 49 | [F2FS_FT_SYMLINK] = DT_LNK, |
| 50 | }; |
| 51 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 52 | static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 53 | [S_IFREG >> S_SHIFT] = F2FS_FT_REG_FILE, |
| 54 | [S_IFDIR >> S_SHIFT] = F2FS_FT_DIR, |
| 55 | [S_IFCHR >> S_SHIFT] = F2FS_FT_CHRDEV, |
| 56 | [S_IFBLK >> S_SHIFT] = F2FS_FT_BLKDEV, |
| 57 | [S_IFIFO >> S_SHIFT] = F2FS_FT_FIFO, |
| 58 | [S_IFSOCK >> S_SHIFT] = F2FS_FT_SOCK, |
| 59 | [S_IFLNK >> S_SHIFT] = F2FS_FT_SYMLINK, |
| 60 | }; |
| 61 | |
Chao Yu | 2e79d95 | 2018-05-30 00:20:40 +0800 | [diff] [blame] | 62 | static void set_de_type(struct f2fs_dir_entry *de, umode_t mode) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 63 | { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 64 | de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT]; |
| 65 | } |
| 66 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 67 | unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de) |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 68 | { |
| 69 | if (de->file_type < F2FS_FT_MAX) |
| 70 | return f2fs_filetype_table[de->file_type]; |
| 71 | return DT_UNKNOWN; |
| 72 | } |
| 73 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 74 | /* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */ |
| 75 | int f2fs_init_casefolded_name(const struct inode *dir, |
| 76 | struct f2fs_filename *fname) |
| 77 | { |
| 78 | #ifdef CONFIG_UNICODE |
Daniel Rosenberg | eca4873 | 2020-07-08 02:12:36 -0700 | [diff] [blame] | 79 | struct super_block *sb = dir->i_sb; |
| 80 | struct f2fs_sb_info *sbi = F2FS_SB(sb); |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 81 | |
| 82 | if (IS_CASEFOLDED(dir)) { |
| 83 | fname->cf_name.name = f2fs_kmalloc(sbi, F2FS_NAME_LEN, |
| 84 | GFP_NOFS); |
| 85 | if (!fname->cf_name.name) |
| 86 | return -ENOMEM; |
Daniel Rosenberg | eca4873 | 2020-07-08 02:12:36 -0700 | [diff] [blame] | 87 | fname->cf_name.len = utf8_casefold(sb->s_encoding, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 88 | fname->usr_fname, |
| 89 | fname->cf_name.name, |
| 90 | F2FS_NAME_LEN); |
| 91 | if ((int)fname->cf_name.len <= 0) { |
| 92 | kfree(fname->cf_name.name); |
| 93 | fname->cf_name.name = NULL; |
Daniel Rosenberg | eca4873 | 2020-07-08 02:12:36 -0700 | [diff] [blame] | 94 | if (sb_has_strict_encoding(sb)) |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 95 | return -EINVAL; |
| 96 | /* fall back to treating name as opaque byte sequence */ |
| 97 | } |
| 98 | } |
| 99 | #endif |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int __f2fs_setup_filename(const struct inode *dir, |
| 104 | const struct fscrypt_name *crypt_name, |
| 105 | struct f2fs_filename *fname) |
| 106 | { |
| 107 | int err; |
| 108 | |
| 109 | memset(fname, 0, sizeof(*fname)); |
| 110 | |
| 111 | fname->usr_fname = crypt_name->usr_fname; |
| 112 | fname->disk_name = crypt_name->disk_name; |
| 113 | #ifdef CONFIG_FS_ENCRYPTION |
| 114 | fname->crypto_buf = crypt_name->crypto_buf; |
| 115 | #endif |
Eric Biggers | 70fb261 | 2020-09-23 21:26:23 -0700 | [diff] [blame] | 116 | if (crypt_name->is_nokey_name) { |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 117 | /* hash was decoded from the no-key name */ |
| 118 | fname->hash = cpu_to_le32(crypt_name->hash); |
| 119 | } else { |
| 120 | err = f2fs_init_casefolded_name(dir, fname); |
| 121 | if (err) { |
| 122 | f2fs_free_filename(fname); |
| 123 | return err; |
| 124 | } |
| 125 | f2fs_hash_filename(dir, fname); |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Prepare to search for @iname in @dir. This is similar to |
| 132 | * fscrypt_setup_filename(), but this also handles computing the casefolded name |
| 133 | * and the f2fs dirhash if needed, then packing all the information about this |
| 134 | * filename up into a 'struct f2fs_filename'. |
| 135 | */ |
| 136 | int f2fs_setup_filename(struct inode *dir, const struct qstr *iname, |
| 137 | int lookup, struct f2fs_filename *fname) |
| 138 | { |
| 139 | struct fscrypt_name crypt_name; |
| 140 | int err; |
| 141 | |
| 142 | err = fscrypt_setup_filename(dir, iname, lookup, &crypt_name); |
| 143 | if (err) |
| 144 | return err; |
| 145 | |
| 146 | return __f2fs_setup_filename(dir, &crypt_name, fname); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Prepare to look up @dentry in @dir. This is similar to |
| 151 | * fscrypt_prepare_lookup(), but this also handles computing the casefolded name |
| 152 | * and the f2fs dirhash if needed, then packing all the information about this |
| 153 | * filename up into a 'struct f2fs_filename'. |
| 154 | */ |
| 155 | int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry, |
| 156 | struct f2fs_filename *fname) |
| 157 | { |
| 158 | struct fscrypt_name crypt_name; |
| 159 | int err; |
| 160 | |
| 161 | err = fscrypt_prepare_lookup(dir, dentry, &crypt_name); |
| 162 | if (err) |
| 163 | return err; |
| 164 | |
| 165 | return __f2fs_setup_filename(dir, &crypt_name, fname); |
| 166 | } |
| 167 | |
| 168 | void f2fs_free_filename(struct f2fs_filename *fname) |
| 169 | { |
| 170 | #ifdef CONFIG_FS_ENCRYPTION |
| 171 | kfree(fname->crypto_buf.name); |
| 172 | fname->crypto_buf.name = NULL; |
| 173 | #endif |
| 174 | #ifdef CONFIG_UNICODE |
| 175 | kfree(fname->cf_name.name); |
| 176 | fname->cf_name.name = NULL; |
| 177 | #endif |
| 178 | } |
| 179 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 180 | static unsigned long dir_block_index(unsigned int level, |
| 181 | int dir_level, unsigned int idx) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 182 | { |
| 183 | unsigned long i; |
| 184 | unsigned long bidx = 0; |
| 185 | |
| 186 | for (i = 0; i < level; i++) |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 187 | bidx += dir_buckets(i, dir_level) * bucket_blocks(i); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 188 | bidx += idx * bucket_blocks(level); |
| 189 | return bidx; |
| 190 | } |
| 191 | |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 192 | static struct f2fs_dir_entry *find_in_block(struct inode *dir, |
| 193 | struct page *dentry_page, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 194 | const struct f2fs_filename *fname, |
Chao Yu | 17f930e | 2020-09-26 08:20:46 +0800 | [diff] [blame] | 195 | int *max_slots) |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 196 | { |
| 197 | struct f2fs_dentry_block *dentry_blk; |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 198 | struct f2fs_dentry_ptr d; |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 199 | |
Yunlong Song | bdbc90f | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 200 | dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page); |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 201 | |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 202 | make_dentry_ptr_block(dir, &d, dentry_blk); |
Chao Yu | 17f930e | 2020-09-26 08:20:46 +0800 | [diff] [blame] | 203 | return f2fs_find_target_dentry(&d, fname, max_slots); |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 206 | #ifdef CONFIG_UNICODE |
| 207 | /* |
| 208 | * Test whether a case-insensitive directory entry matches the filename |
| 209 | * being searched for. |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 210 | * |
| 211 | * Returns 1 for a match, 0 for no match, and -errno on an error. |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 212 | */ |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 213 | static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 214 | const u8 *de_name, u32 de_name_len) |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 215 | { |
Daniel Rosenberg | eca4873 | 2020-07-08 02:12:36 -0700 | [diff] [blame] | 216 | const struct super_block *sb = dir->i_sb; |
| 217 | const struct unicode_map *um = sb->s_encoding; |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 218 | struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len); |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 219 | struct qstr entry = QSTR_INIT(de_name, de_name_len); |
Eric Biggers | f874fa1 | 2020-05-07 00:59:03 -0700 | [diff] [blame] | 220 | int res; |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 221 | |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 222 | if (IS_ENCRYPTED(dir)) { |
| 223 | const struct fscrypt_str encrypted_name = |
| 224 | FSTR_INIT((u8 *)de_name, de_name_len); |
| 225 | |
| 226 | if (WARN_ON_ONCE(!fscrypt_has_encryption_key(dir))) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL); |
| 230 | if (!decrypted_name.name) |
| 231 | return -ENOMEM; |
| 232 | res = fscrypt_fname_disk_to_usr(dir, 0, 0, &encrypted_name, |
| 233 | &decrypted_name); |
| 234 | if (res < 0) |
| 235 | goto out; |
| 236 | entry.name = decrypted_name.name; |
| 237 | entry.len = decrypted_name.len; |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 238 | } |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 239 | |
| 240 | res = utf8_strncasecmp_folded(um, name, &entry); |
| 241 | /* |
| 242 | * In strict mode, ignore invalid names. In non-strict mode, |
| 243 | * fall back to treating them as opaque byte sequences. |
| 244 | */ |
| 245 | if (res < 0 && !sb_has_strict_encoding(sb)) { |
| 246 | res = name->len == entry.len && |
| 247 | memcmp(name->name, entry.name, name->len) == 0; |
| 248 | } else { |
| 249 | /* utf8_strncasecmp_folded returns 0 on match */ |
| 250 | res = (res == 0); |
| 251 | } |
| 252 | out: |
| 253 | kfree(decrypted_name.name); |
| 254 | return res; |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 255 | } |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 256 | #endif /* CONFIG_UNICODE */ |
Chao Yu | 950d47f | 2019-08-21 23:13:35 +0800 | [diff] [blame] | 257 | |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 258 | static inline int f2fs_match_name(const struct inode *dir, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 259 | const struct f2fs_filename *fname, |
| 260 | const u8 *de_name, u32 de_name_len) |
Chao Yu | 950d47f | 2019-08-21 23:13:35 +0800 | [diff] [blame] | 261 | { |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 262 | struct fscrypt_name f; |
Chao Yu | fe76a16 | 2019-08-21 23:13:34 +0800 | [diff] [blame] | 263 | |
| 264 | #ifdef CONFIG_UNICODE |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 265 | if (fname->cf_name.name) { |
| 266 | struct qstr cf = FSTR_TO_QSTR(&fname->cf_name); |
Chao Yu | fe76a16 | 2019-08-21 23:13:34 +0800 | [diff] [blame] | 267 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 268 | return f2fs_match_ci_name(dir, &cf, de_name, de_name_len); |
Chao Yu | 950d47f | 2019-08-21 23:13:35 +0800 | [diff] [blame] | 269 | } |
Chao Yu | fe76a16 | 2019-08-21 23:13:34 +0800 | [diff] [blame] | 270 | #endif |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 271 | f.usr_fname = fname->usr_fname; |
| 272 | f.disk_name = fname->disk_name; |
| 273 | #ifdef CONFIG_FS_ENCRYPTION |
| 274 | f.crypto_buf = fname->crypto_buf; |
| 275 | #endif |
| 276 | return fscrypt_match_name(&f, de_name, de_name_len); |
Chao Yu | fe76a16 | 2019-08-21 23:13:34 +0800 | [diff] [blame] | 277 | } |
| 278 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 279 | struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d, |
| 280 | const struct f2fs_filename *fname, int *max_slots) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 281 | { |
| 282 | struct f2fs_dir_entry *de; |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 283 | unsigned long bit_pos = 0; |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 284 | int max_len = 0; |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 285 | int res = 0; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 286 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 287 | if (max_slots) |
| 288 | *max_slots = 0; |
| 289 | while (bit_pos < d->max) { |
| 290 | if (!test_bit_le(bit_pos, d->bitmap)) { |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 291 | bit_pos++; |
Chao Yu | bda1907 | 2015-03-09 17:33:16 +0800 | [diff] [blame] | 292 | max_len++; |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 293 | continue; |
| 294 | } |
Chao Yu | bda1907 | 2015-03-09 17:33:16 +0800 | [diff] [blame] | 295 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 296 | de = &d->dentry[bit_pos]; |
Jaegeuk Kim | 6e22c69 | 2015-04-27 17:12:39 -0700 | [diff] [blame] | 297 | |
Chao Yu | a4a13f5 | 2016-04-27 22:22:20 +0800 | [diff] [blame] | 298 | if (unlikely(!de->name_len)) { |
| 299 | bit_pos++; |
| 300 | continue; |
| 301 | } |
| 302 | |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 303 | if (de->hash_code == fname->hash) { |
| 304 | res = f2fs_match_name(d->inode, fname, |
| 305 | d->filename[bit_pos], |
| 306 | le16_to_cpu(de->name_len)); |
| 307 | if (res < 0) |
| 308 | return ERR_PTR(res); |
| 309 | if (res) |
| 310 | goto found; |
| 311 | } |
Eric Biggers | 1f73d49 | 2017-04-24 10:00:12 -0700 | [diff] [blame] | 312 | |
Chao Yu | bda1907 | 2015-03-09 17:33:16 +0800 | [diff] [blame] | 313 | if (max_slots && max_len > *max_slots) |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 314 | *max_slots = max_len; |
Chao Yu | bda1907 | 2015-03-09 17:33:16 +0800 | [diff] [blame] | 315 | max_len = 0; |
Chao Yu | 81e366f | 2014-07-10 12:37:46 +0800 | [diff] [blame] | 316 | |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 317 | bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | de = NULL; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 321 | found: |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 322 | if (max_slots && max_len > *max_slots) |
Jaegeuk Kim | 5d0c6671 | 2014-02-27 13:57:53 +0900 | [diff] [blame] | 323 | *max_slots = max_len; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 324 | return de; |
| 325 | } |
| 326 | |
| 327 | static struct f2fs_dir_entry *find_in_level(struct inode *dir, |
Jaegeuk Kim | 6e22c69 | 2015-04-27 17:12:39 -0700 | [diff] [blame] | 328 | unsigned int level, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 329 | const struct f2fs_filename *fname, |
Jaegeuk Kim | 6e22c69 | 2015-04-27 17:12:39 -0700 | [diff] [blame] | 330 | struct page **res_page) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 331 | { |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 332 | int s = GET_DENTRY_SLOTS(fname->disk_name.len); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 333 | unsigned int nbucket, nblock; |
| 334 | unsigned int bidx, end_block; |
| 335 | struct page *dentry_page; |
| 336 | struct f2fs_dir_entry *de = NULL; |
| 337 | bool room = false; |
Jaegeuk Kim | 4e6ebf6 | 2014-10-13 17:26:14 -0700 | [diff] [blame] | 338 | int max_slots; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 339 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 340 | nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 341 | nblock = bucket_blocks(level); |
| 342 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 343 | bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 344 | le32_to_cpu(fname->hash) % nbucket); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 345 | end_block = bidx + nblock; |
| 346 | |
| 347 | for (; bidx < end_block; bidx++) { |
| 348 | /* no need to allocate new dentry pages to all the indices */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 349 | dentry_page = f2fs_find_data_page(dir, bidx); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 350 | if (IS_ERR(dentry_page)) { |
Jaegeuk Kim | 42d9640 | 2016-05-25 14:29:11 -0700 | [diff] [blame] | 351 | if (PTR_ERR(dentry_page) == -ENOENT) { |
| 352 | room = true; |
| 353 | continue; |
| 354 | } else { |
| 355 | *res_page = dentry_page; |
| 356 | break; |
| 357 | } |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 358 | } |
| 359 | |
Chao Yu | 17f930e | 2020-09-26 08:20:46 +0800 | [diff] [blame] | 360 | de = find_in_block(dir, dentry_page, fname, &max_slots); |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 361 | if (IS_ERR(de)) { |
| 362 | *res_page = ERR_CAST(de); |
| 363 | de = NULL; |
| 364 | break; |
| 365 | } else if (de) { |
Chao Yu | 17f930e | 2020-09-26 08:20:46 +0800 | [diff] [blame] | 366 | *res_page = dentry_page; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 367 | break; |
Chao Yu | 17f930e | 2020-09-26 08:20:46 +0800 | [diff] [blame] | 368 | } |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 369 | |
| 370 | if (max_slots >= s) |
| 371 | room = true; |
| 372 | f2fs_put_page(dentry_page, 0); |
| 373 | } |
| 374 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 375 | if (!de && room && F2FS_I(dir)->chash != fname->hash) { |
| 376 | F2FS_I(dir)->chash = fname->hash; |
Sheng Yong | d3bb910 | 2017-04-22 10:39:20 +0800 | [diff] [blame] | 377 | F2FS_I(dir)->clevel = level; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | return de; |
| 381 | } |
| 382 | |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 383 | struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 384 | const struct f2fs_filename *fname, |
| 385 | struct page **res_page) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 386 | { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 387 | unsigned long npages = dir_blocks(dir); |
| 388 | struct f2fs_dir_entry *de = NULL; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 389 | unsigned int max_depth; |
| 390 | unsigned int level; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 391 | |
Chao Yu | 6d7ab88 | 2020-09-29 09:22:50 +0800 | [diff] [blame] | 392 | *res_page = NULL; |
| 393 | |
Jaegeuk Kim | 6e22c69 | 2015-04-27 17:12:39 -0700 | [diff] [blame] | 394 | if (f2fs_has_inline_dentry(dir)) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 395 | de = f2fs_find_in_inline_dir(dir, fname, res_page); |
Jaegeuk Kim | 6e22c69 | 2015-04-27 17:12:39 -0700 | [diff] [blame] | 396 | goto out; |
| 397 | } |
| 398 | |
Chao Yu | 6d7ab88 | 2020-09-29 09:22:50 +0800 | [diff] [blame] | 399 | if (npages == 0) |
Jaegeuk Kim | 6e22c69 | 2015-04-27 17:12:39 -0700 | [diff] [blame] | 400 | goto out; |
| 401 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 402 | max_depth = F2FS_I(dir)->i_current_depth; |
Jaegeuk Kim | 1f6fa26 | 2015-12-31 10:28:52 -0800 | [diff] [blame] | 403 | if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 404 | f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u", |
| 405 | dir->i_ino, max_depth); |
Jaegeuk Kim | 1f6fa26 | 2015-12-31 10:28:52 -0800 | [diff] [blame] | 406 | max_depth = MAX_DIR_HASH_DEPTH; |
Jaegeuk Kim | 205b982 | 2016-05-20 09:52:20 -0700 | [diff] [blame] | 407 | f2fs_i_depth_write(dir, max_depth); |
Jaegeuk Kim | 1f6fa26 | 2015-12-31 10:28:52 -0800 | [diff] [blame] | 408 | } |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 409 | |
| 410 | for (level = 0; level < max_depth; level++) { |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 411 | de = find_in_level(dir, level, fname, res_page); |
Jaegeuk Kim | 42d9640 | 2016-05-25 14:29:11 -0700 | [diff] [blame] | 412 | if (de || IS_ERR(*res_page)) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 413 | break; |
| 414 | } |
Jaegeuk Kim | 6e22c69 | 2015-04-27 17:12:39 -0700 | [diff] [blame] | 415 | out: |
Sheng Yong | d3bb910 | 2017-04-22 10:39:20 +0800 | [diff] [blame] | 416 | /* This is to increase the speed of f2fs_create */ |
| 417 | if (!de) |
| 418 | F2FS_I(dir)->task = current; |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 419 | return de; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Find an entry in the specified directory with the wanted name. |
| 424 | * It returns the page where the entry was found (as a parameter - res_page), |
| 425 | * and the entry itself. Page is returned mapped and unlocked. |
| 426 | * Entry is guaranteed to be valid. |
| 427 | */ |
| 428 | struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir, |
| 429 | const struct qstr *child, struct page **res_page) |
| 430 | { |
| 431 | struct f2fs_dir_entry *de = NULL; |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 432 | struct f2fs_filename fname; |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 433 | int err; |
| 434 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 435 | err = f2fs_setup_filename(dir, child, 1, &fname); |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 436 | if (err) { |
Eric Biggers | 54475f5 | 2016-12-05 11:12:44 -0800 | [diff] [blame] | 437 | if (err == -ENOENT) |
| 438 | *res_page = NULL; |
| 439 | else |
| 440 | *res_page = ERR_PTR(err); |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | de = __f2fs_find_entry(dir, &fname, res_page); |
| 445 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 446 | f2fs_free_filename(&fname); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 447 | return de; |
| 448 | } |
| 449 | |
| 450 | struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p) |
| 451 | { |
Al Viro | 80e5d1ff5 | 2021-04-15 19:46:50 -0400 | [diff] [blame] | 452 | return f2fs_find_entry(dir, &dotdot_name, p); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 453 | } |
| 454 | |
Linus Torvalds | 835c92d | 2016-08-06 09:49:02 -0400 | [diff] [blame] | 455 | ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr, |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 456 | struct page **page) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 457 | { |
| 458 | ino_t res = 0; |
| 459 | struct f2fs_dir_entry *de; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 460 | |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 461 | de = f2fs_find_entry(dir, qstr, page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 462 | if (de) { |
| 463 | res = le32_to_cpu(de->ino); |
Chao Yu | 91246c2 | 2016-07-19 08:27:47 +0800 | [diff] [blame] | 464 | f2fs_put_page(*page, 0); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | return res; |
| 468 | } |
| 469 | |
| 470 | void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de, |
| 471 | struct page *page, struct inode *inode) |
| 472 | { |
Jaegeuk Kim | 59a0615 | 2014-10-13 19:34:26 -0700 | [diff] [blame] | 473 | enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA; |
Yi Zhuang | 5f029c0 | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 474 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 475 | lock_page(page); |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 476 | f2fs_wait_on_page_writeback(page, type, true, true); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 477 | de->ino = cpu_to_le32(inode->i_ino); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 478 | set_de_type(de, inode->i_mode); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 479 | set_page_dirty(page); |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 480 | |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 481 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 482 | f2fs_mark_inode_dirty_sync(dir, false); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 483 | f2fs_put_page(page, 1); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 484 | } |
| 485 | |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 486 | static void init_dent_inode(struct inode *dir, struct inode *inode, |
| 487 | const struct f2fs_filename *fname, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 488 | struct page *ipage) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 489 | { |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 490 | struct f2fs_inode *ri; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 491 | |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 492 | if (!fname) /* tmpfile case? */ |
| 493 | return; |
| 494 | |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 495 | f2fs_wait_on_page_writeback(ipage, NODE, true, true); |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 496 | |
Al Viro | 53dc9a6 | 2013-01-25 16:01:21 -0500 | [diff] [blame] | 497 | /* copy name info. to this inode page */ |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 498 | ri = F2FS_INODE(ipage); |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 499 | ri->i_namelen = cpu_to_le32(fname->disk_name.len); |
| 500 | memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len); |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 501 | if (IS_ENCRYPTED(dir)) { |
| 502 | file_set_enc_name(inode); |
| 503 | /* |
| 504 | * Roll-forward recovery doesn't have encryption keys available, |
| 505 | * so it can't compute the dirhash for encrypted+casefolded |
| 506 | * filenames. Append it to i_name if possible. Else, disable |
| 507 | * roll-forward recovery of the dentry (i.e., make fsync'ing the |
| 508 | * file force a checkpoint) by setting LOST_PINO. |
| 509 | */ |
| 510 | if (IS_CASEFOLDED(dir)) { |
| 511 | if (fname->disk_name.len + sizeof(f2fs_hash_t) <= |
| 512 | F2FS_NAME_LEN) |
| 513 | put_unaligned(fname->hash, (f2fs_hash_t *) |
| 514 | &ri->i_name[fname->disk_name.len]); |
| 515 | else |
| 516 | file_lost_pino(inode); |
| 517 | } |
| 518 | } |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 519 | set_page_dirty(ipage); |
| 520 | } |
| 521 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 522 | void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent, |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 523 | struct f2fs_dentry_ptr *d) |
| 524 | { |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 525 | struct fscrypt_str dot = FSTR_INIT(".", 1); |
| 526 | struct fscrypt_str dotdot = FSTR_INIT("..", 2); |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 527 | |
Chao Yu | 291bf80 | 2016-03-09 22:07:28 +0800 | [diff] [blame] | 528 | /* update dirent of "." */ |
| 529 | f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0); |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 530 | |
Chao Yu | 291bf80 | 2016-03-09 22:07:28 +0800 | [diff] [blame] | 531 | /* update dirent of ".." */ |
| 532 | f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1); |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 535 | static int make_empty_dir(struct inode *inode, |
| 536 | struct inode *parent, struct page *page) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 537 | { |
| 538 | struct page *dentry_page; |
| 539 | struct f2fs_dentry_block *dentry_blk; |
Jaegeuk Kim | 062a3e7 | 2014-10-18 23:06:41 -0700 | [diff] [blame] | 540 | struct f2fs_dentry_ptr d; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 541 | |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 542 | if (f2fs_has_inline_dentry(inode)) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 543 | return f2fs_make_empty_inline_dir(inode, parent, page); |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 544 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 545 | dentry_page = f2fs_get_new_data_page(inode, page, 0, true); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 546 | if (IS_ERR(dentry_page)) |
| 547 | return PTR_ERR(dentry_page); |
| 548 | |
Yunlong Song | bdbc90f | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 549 | dentry_blk = page_address(dentry_page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 550 | |
Tomohiro Kusumi | 64c24ec | 2017-04-04 13:01:22 +0300 | [diff] [blame] | 551 | make_dentry_ptr_block(NULL, &d, dentry_blk); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 552 | f2fs_do_make_empty_dir(inode, parent, &d); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 553 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 554 | set_page_dirty(dentry_page); |
| 555 | f2fs_put_page(dentry_page, 1); |
| 556 | return 0; |
| 557 | } |
| 558 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 559 | struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 560 | const struct f2fs_filename *fname, struct page *dpage) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 561 | { |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 562 | struct page *page; |
| 563 | int err; |
| 564 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 565 | if (is_inode_flag_set(inode, FI_NEW_INODE)) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 566 | page = f2fs_new_inode_page(inode); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 567 | if (IS_ERR(page)) |
| 568 | return page; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 569 | |
| 570 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 571 | /* in order to handle error case */ |
| 572 | get_page(page); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 573 | err = make_empty_dir(inode, dir, page); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 574 | if (err) { |
| 575 | lock_page(page); |
| 576 | goto put_error; |
| 577 | } |
| 578 | put_page(page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 579 | } |
| 580 | |
Jaegeuk Kim | bce8d11 | 2014-10-13 19:42:53 -0700 | [diff] [blame] | 581 | err = f2fs_init_acl(inode, dir, page, dpage); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 582 | if (err) |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 583 | goto put_error; |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 584 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 585 | err = f2fs_init_security(inode, dir, |
| 586 | fname ? fname->usr_fname : NULL, page); |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 587 | if (err) |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 588 | goto put_error; |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 589 | |
Chao Yu | 8c7d4b5 | 2020-03-21 20:19:33 +0800 | [diff] [blame] | 590 | if (IS_ENCRYPTED(inode)) { |
Eric Biggers | e075b69 | 2020-09-16 21:11:27 -0700 | [diff] [blame] | 591 | err = fscrypt_set_context(inode, page); |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 592 | if (err) |
| 593 | goto put_error; |
| 594 | } |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 595 | } else { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 596 | page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 597 | if (IS_ERR(page)) |
| 598 | return page; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 599 | } |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 600 | |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 601 | init_dent_inode(dir, inode, fname, page); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 602 | |
Jaegeuk Kim | 83d5d6f | 2013-05-28 12:25:47 +0900 | [diff] [blame] | 603 | /* |
| 604 | * This file should be checkpointed during fsync. |
| 605 | * We lost i_pino from now on. |
| 606 | */ |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 607 | if (is_inode_flag_set(inode, FI_INC_LINK)) { |
Sheng Yong | d58dfb7 | 2017-06-26 10:41:35 +0800 | [diff] [blame] | 608 | if (!S_ISDIR(inode->i_mode)) |
| 609 | file_lost_pino(inode); |
Chao Yu | 50732df | 2014-06-19 16:23:19 +0800 | [diff] [blame] | 610 | /* |
| 611 | * If link the tmpfile to alias through linkat path, |
| 612 | * we should remove this inode from orphan list. |
| 613 | */ |
| 614 | if (inode->i_nlink == 0) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 615 | f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino); |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 616 | f2fs_i_links_write(inode, true); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 617 | } |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 618 | return page; |
| 619 | |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 620 | put_error: |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 621 | clear_nlink(inode); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 622 | f2fs_update_inode(inode, page); |
Jaegeuk Kim | 221149c | 2016-05-02 12:34:48 -0700 | [diff] [blame] | 623 | f2fs_put_page(page, 1); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 624 | return ERR_PTR(err); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 625 | } |
| 626 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 627 | void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode, |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 628 | unsigned int current_depth) |
| 629 | { |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 630 | if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) { |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 631 | if (S_ISDIR(inode->i_mode)) |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 632 | f2fs_i_links_write(dir, true); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 633 | clear_inode_flag(inode, FI_NEW_INODE); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 634 | } |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 635 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 636 | f2fs_mark_inode_dirty_sync(dir, false); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 637 | |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 638 | if (F2FS_I(dir)->i_current_depth != current_depth) |
Jaegeuk Kim | 205b982 | 2016-05-20 09:52:20 -0700 | [diff] [blame] | 639 | f2fs_i_depth_write(dir, current_depth); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 640 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 641 | if (inode && is_inode_flag_set(inode, FI_INC_LINK)) |
| 642 | clear_inode_flag(inode, FI_INC_LINK); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 643 | } |
| 644 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 645 | int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 646 | { |
| 647 | int bit_start = 0; |
| 648 | int zero_start, zero_end; |
| 649 | next: |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 650 | zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start); |
| 651 | if (zero_start >= max_slots) |
| 652 | return max_slots; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 653 | |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 654 | zero_end = find_next_bit_le(bitmap, max_slots, zero_start); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 655 | if (zero_end - zero_start >= slots) |
| 656 | return zero_start; |
| 657 | |
| 658 | bit_start = zero_end + 1; |
| 659 | |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 660 | if (zero_end + 1 >= max_slots) |
| 661 | return max_slots; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 662 | goto next; |
| 663 | } |
| 664 | |
Jaegeuk Kim | b06af2a | 2019-12-09 19:03:05 -0800 | [diff] [blame] | 665 | bool f2fs_has_enough_room(struct inode *dir, struct page *ipage, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 666 | const struct f2fs_filename *fname) |
Jaegeuk Kim | b06af2a | 2019-12-09 19:03:05 -0800 | [diff] [blame] | 667 | { |
| 668 | struct f2fs_dentry_ptr d; |
| 669 | unsigned int bit_pos; |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 670 | int slots = GET_DENTRY_SLOTS(fname->disk_name.len); |
Jaegeuk Kim | b06af2a | 2019-12-09 19:03:05 -0800 | [diff] [blame] | 671 | |
| 672 | make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage)); |
| 673 | |
| 674 | bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max); |
| 675 | |
| 676 | return bit_pos < d.max; |
| 677 | } |
| 678 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 679 | void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 680 | const struct fscrypt_str *name, f2fs_hash_t name_hash, |
| 681 | unsigned int bit_pos) |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 682 | { |
| 683 | struct f2fs_dir_entry *de; |
| 684 | int slots = GET_DENTRY_SLOTS(name->len); |
| 685 | int i; |
| 686 | |
| 687 | de = &d->dentry[bit_pos]; |
| 688 | de->hash_code = name_hash; |
| 689 | de->name_len = cpu_to_le16(name->len); |
| 690 | memcpy(d->filename[bit_pos], name->name, name->len); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 691 | de->ino = cpu_to_le32(ino); |
| 692 | set_de_type(de, mode); |
Jaegeuk Kim | 7d9dfa1 | 2016-02-12 14:29:28 -0800 | [diff] [blame] | 693 | for (i = 0; i < slots; i++) { |
Jaegeuk Kim | 6bf6b26 | 2016-08-31 16:20:37 -0700 | [diff] [blame] | 694 | __set_bit_le(bit_pos + i, (void *)d->bitmap); |
Jaegeuk Kim | 7d9dfa1 | 2016-02-12 14:29:28 -0800 | [diff] [blame] | 695 | /* avoid wrong garbage data for readdir */ |
| 696 | if (i) |
| 697 | (de + i)->name_len = 0; |
| 698 | } |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 699 | } |
| 700 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 701 | int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname, |
| 702 | struct inode *inode, nid_t ino, umode_t mode) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 703 | { |
| 704 | unsigned int bit_pos; |
| 705 | unsigned int level; |
| 706 | unsigned int current_depth; |
| 707 | unsigned long bidx, block; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 708 | unsigned int nbucket, nblock; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 709 | struct page *dentry_page = NULL; |
| 710 | struct f2fs_dentry_block *dentry_blk = NULL; |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 711 | struct f2fs_dentry_ptr d; |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 712 | struct page *page = NULL; |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 713 | int slots, err = 0; |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 714 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 715 | level = 0; |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 716 | slots = GET_DENTRY_SLOTS(fname->disk_name.len); |
Jaegeuk Kim | 9ea9716 | 2015-04-27 14:51:02 -0700 | [diff] [blame] | 717 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 718 | current_depth = F2FS_I(dir)->i_current_depth; |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 719 | if (F2FS_I(dir)->chash == fname->hash) { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 720 | level = F2FS_I(dir)->clevel; |
| 721 | F2FS_I(dir)->chash = 0; |
| 722 | } |
| 723 | |
| 724 | start: |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 725 | if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) { |
Chao Yu | c45d600 | 2019-11-01 17:53:23 +0800 | [diff] [blame] | 726 | f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH); |
Jaegeuk Kim | cb78942 | 2016-04-29 16:29:22 -0700 | [diff] [blame] | 727 | return -ENOSPC; |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 728 | } |
Arnd Bergmann | 7fa750a | 2018-08-13 23:38:06 +0200 | [diff] [blame] | 729 | |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 730 | if (unlikely(current_depth == MAX_DIR_HASH_DEPTH)) |
| 731 | return -ENOSPC; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 732 | |
| 733 | /* Increase the depth, if required */ |
| 734 | if (level == current_depth) |
| 735 | ++current_depth; |
| 736 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 737 | nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 738 | nblock = bucket_blocks(level); |
| 739 | |
Jaegeuk Kim | 3843154 | 2014-02-27 18:20:00 +0900 | [diff] [blame] | 740 | bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level, |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 741 | (le32_to_cpu(fname->hash) % nbucket)); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 742 | |
| 743 | for (block = bidx; block <= (bidx + nblock - 1); block++) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 744 | dentry_page = f2fs_get_new_data_page(dir, NULL, block, true); |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 745 | if (IS_ERR(dentry_page)) |
| 746 | return PTR_ERR(dentry_page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 747 | |
Yunlong Song | bdbc90f | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 748 | dentry_blk = page_address(dentry_page); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 749 | bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap, |
Jaegeuk Kim | a82afa2 | 2014-10-13 16:28:13 -0700 | [diff] [blame] | 750 | slots, NR_DENTRY_IN_BLOCK); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 751 | if (bit_pos < NR_DENTRY_IN_BLOCK) |
| 752 | goto add_dentry; |
| 753 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 754 | f2fs_put_page(dentry_page, 1); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /* Move to next level to find the empty slot for new dentry */ |
| 758 | ++level; |
| 759 | goto start; |
| 760 | add_dentry: |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 761 | f2fs_wait_on_page_writeback(dentry_page, DATA, true, true); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 762 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 763 | if (inode) { |
| 764 | down_write(&F2FS_I(inode)->i_sem); |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 765 | page = f2fs_init_inode_metadata(inode, dir, fname, NULL); |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 766 | if (IS_ERR(page)) { |
| 767 | err = PTR_ERR(page); |
| 768 | goto fail; |
| 769 | } |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 770 | } |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 771 | |
Tomohiro Kusumi | 64c24ec | 2017-04-04 13:01:22 +0300 | [diff] [blame] | 772 | make_dentry_ptr_block(NULL, &d, dentry_blk); |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 773 | f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash, |
| 774 | bit_pos); |
Chao Yu | 3b4d732 | 2015-02-16 16:17:20 +0800 | [diff] [blame] | 775 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 776 | set_page_dirty(dentry_page); |
Jaegeuk Kim | 6666e6a | 2012-12-10 17:52:48 +0900 | [diff] [blame] | 777 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 778 | if (inode) { |
Jaegeuk Kim | 205b982 | 2016-05-20 09:52:20 -0700 | [diff] [blame] | 779 | f2fs_i_pino_write(inode, dir->i_ino); |
Chao Yu | 9819403 | 2019-09-10 09:14:16 +0800 | [diff] [blame] | 780 | |
| 781 | /* synchronize inode page's data from inode cache */ |
| 782 | if (is_inode_flag_set(inode, FI_NEW_INODE)) |
| 783 | f2fs_update_inode(inode, page); |
| 784 | |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 785 | f2fs_put_page(page, 1); |
| 786 | } |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 787 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 788 | f2fs_update_parent_metadata(dir, inode, current_depth); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 789 | fail: |
Jaegeuk Kim | 510022a | 2015-03-30 15:07:16 -0700 | [diff] [blame] | 790 | if (inode) |
| 791 | up_write(&F2FS_I(inode)->i_sem); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 792 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 793 | f2fs_put_page(dentry_page, 1); |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 794 | |
| 795 | return err; |
| 796 | } |
| 797 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 798 | int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname, |
| 799 | struct inode *inode, nid_t ino, umode_t mode) |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 800 | { |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 801 | int err = -EAGAIN; |
| 802 | |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 803 | if (f2fs_has_inline_dentry(dir)) |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 804 | err = f2fs_add_inline_entry(dir, fname, inode, ino, mode); |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 805 | if (err == -EAGAIN) |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 806 | err = f2fs_add_regular_entry(dir, fname, inode, ino, mode); |
Shuoran Liu | e7ba108 | 2016-08-29 11:27:56 +0800 | [diff] [blame] | 807 | |
| 808 | f2fs_update_time(F2FS_I_SB(dir), REQ_TIME); |
| 809 | return err; |
| 810 | } |
| 811 | |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 812 | /* |
| 813 | * Caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 814 | * f2fs_unlock_op(). |
| 815 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 816 | int f2fs_do_add_link(struct inode *dir, const struct qstr *name, |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 817 | struct inode *inode, nid_t ino, umode_t mode) |
| 818 | { |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 819 | struct f2fs_filename fname; |
Jaegeuk Kim | 88c5c13 | 2017-02-14 09:54:37 -0800 | [diff] [blame] | 820 | struct page *page = NULL; |
| 821 | struct f2fs_dir_entry *de = NULL; |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 822 | int err; |
| 823 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 824 | err = f2fs_setup_filename(dir, name, 0, &fname); |
Chao Yu | 675f10b | 2016-02-22 18:29:18 +0800 | [diff] [blame] | 825 | if (err) |
| 826 | return err; |
| 827 | |
Jaegeuk Kim | 88c5c13 | 2017-02-14 09:54:37 -0800 | [diff] [blame] | 828 | /* |
Liu Song | e5cc2c5 | 2020-06-25 20:40:11 +0800 | [diff] [blame] | 829 | * An immature stackable filesystem shows a race condition between lookup |
Jaegeuk Kim | 88c5c13 | 2017-02-14 09:54:37 -0800 | [diff] [blame] | 830 | * and create. If we have same task when doing lookup and create, it's |
| 831 | * definitely fine as expected by VFS normally. Otherwise, let's just |
| 832 | * verify on-disk dentry one more time, which guarantees filesystem |
| 833 | * consistency more. |
| 834 | */ |
| 835 | if (current != F2FS_I(dir)->task) { |
| 836 | de = __f2fs_find_entry(dir, &fname, &page); |
| 837 | F2FS_I(dir)->task = NULL; |
| 838 | } |
| 839 | if (de) { |
Jaegeuk Kim | 88c5c13 | 2017-02-14 09:54:37 -0800 | [diff] [blame] | 840 | f2fs_put_page(page, 0); |
| 841 | err = -EEXIST; |
| 842 | } else if (IS_ERR(page)) { |
| 843 | err = PTR_ERR(page); |
| 844 | } else { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 845 | err = f2fs_add_dentry(dir, &fname, inode, ino, mode); |
Jaegeuk Kim | 88c5c13 | 2017-02-14 09:54:37 -0800 | [diff] [blame] | 846 | } |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 847 | f2fs_free_filename(&fname); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 848 | return err; |
| 849 | } |
| 850 | |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 851 | int f2fs_do_tmpfile(struct inode *inode, struct inode *dir) |
| 852 | { |
| 853 | struct page *page; |
| 854 | int err = 0; |
| 855 | |
| 856 | down_write(&F2FS_I(inode)->i_sem); |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 857 | page = f2fs_init_inode_metadata(inode, dir, NULL, NULL); |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 858 | if (IS_ERR(page)) { |
| 859 | err = PTR_ERR(page); |
| 860 | goto fail; |
| 861 | } |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 862 | f2fs_put_page(page, 1); |
| 863 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 864 | clear_inode_flag(inode, FI_NEW_INODE); |
Sahitya Tummala | c75f2fe | 2018-10-05 10:47:39 +0530 | [diff] [blame] | 865 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
Jaegeuk Kim | b97a9b5 | 2014-06-20 21:37:02 -0700 | [diff] [blame] | 866 | fail: |
| 867 | up_write(&F2FS_I(inode)->i_sem); |
| 868 | return err; |
| 869 | } |
| 870 | |
Jaegeuk Kim | 9f7c45c | 2016-06-01 21:18:25 -0700 | [diff] [blame] | 871 | void f2fs_drop_nlink(struct inode *dir, struct inode *inode) |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 872 | { |
| 873 | struct f2fs_sb_info *sbi = F2FS_I_SB(dir); |
| 874 | |
| 875 | down_write(&F2FS_I(inode)->i_sem); |
| 876 | |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 877 | if (S_ISDIR(inode->i_mode)) |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 878 | f2fs_i_links_write(dir, false); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 879 | inode->i_ctime = current_time(inode); |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 880 | |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 881 | f2fs_i_links_write(inode, false); |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 882 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | a196124 | 2016-05-20 09:43:20 -0700 | [diff] [blame] | 883 | f2fs_i_links_write(inode, false); |
Jaegeuk Kim | fc9581c | 2016-05-20 09:22:03 -0700 | [diff] [blame] | 884 | f2fs_i_size_write(inode, 0); |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 885 | } |
| 886 | up_write(&F2FS_I(inode)->i_sem); |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 887 | |
| 888 | if (inode->i_nlink == 0) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 889 | f2fs_add_orphan_inode(inode); |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 890 | else |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 891 | f2fs_release_orphan_inode(sbi); |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 892 | } |
| 893 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 894 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 895 | * It only removes the dentry from the dentry page, corresponding name |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 896 | * entry in name page does not need to be touched during deletion. |
| 897 | */ |
| 898 | void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page, |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 899 | struct inode *dir, struct inode *inode) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 900 | { |
| 901 | struct f2fs_dentry_block *dentry_blk; |
| 902 | unsigned int bit_pos; |
Namjae Jeon | 457d08e | 2012-12-08 14:54:50 +0900 | [diff] [blame] | 903 | int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 904 | int i; |
| 905 | |
Jaegeuk Kim | d0239e1 | 2016-01-08 16:57:48 -0800 | [diff] [blame] | 906 | f2fs_update_time(F2FS_I_SB(dir), REQ_TIME); |
| 907 | |
Chao Yu | 63189b7 | 2018-03-08 14:22:56 +0800 | [diff] [blame] | 908 | if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 909 | f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO); |
Jaegeuk Kim | 0a007b97 | 2017-12-28 08:09:44 -0800 | [diff] [blame] | 910 | |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 911 | if (f2fs_has_inline_dentry(dir)) |
| 912 | return f2fs_delete_inline_entry(dentry, page, dir, inode); |
| 913 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 914 | lock_page(page); |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 915 | f2fs_wait_on_page_writeback(page, DATA, true, true); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 916 | |
Gu Zheng | 1c3bb97 | 2014-06-27 17:57:04 +0800 | [diff] [blame] | 917 | dentry_blk = page_address(page); |
| 918 | bit_pos = dentry - dentry_blk->dentry; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 919 | for (i = 0; i < slots; i++) |
Jaegeuk Kim | 23380b8 | 2017-03-07 14:11:06 -0800 | [diff] [blame] | 920 | __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 921 | |
| 922 | /* Let's check and deallocate this dentry page */ |
| 923 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 924 | NR_DENTRY_IN_BLOCK, |
| 925 | 0); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 926 | set_page_dirty(page); |
| 927 | |
Chao Yu | 206e61b | 2015-08-12 17:48:21 +0800 | [diff] [blame] | 928 | if (bit_pos == NR_DENTRY_IN_BLOCK && |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 929 | !f2fs_truncate_hole(dir, page->index, page->index + 1)) { |
Matthew Wilcox | 5ec2d99 | 2017-12-04 20:25:25 -0500 | [diff] [blame] | 930 | f2fs_clear_page_cache_dirty_tag(page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 931 | clear_page_dirty_for_io(page); |
Chao Yu | 240a591 | 2019-03-06 17:30:59 +0800 | [diff] [blame] | 932 | f2fs_clear_page_private(page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 933 | ClearPageUptodate(page); |
Chao Yu | 2baf078 | 2018-07-27 18:15:16 +0800 | [diff] [blame] | 934 | clear_cold_data(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 935 | inode_dec_dirty_pages(dir); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 936 | f2fs_remove_dirty_inode(dir); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 937 | } |
Namjae Jeon | 508198b | 2012-12-08 14:54:35 +0900 | [diff] [blame] | 938 | f2fs_put_page(page, 1); |
Chao Yu | 9995e40 | 2020-03-20 18:17:54 +0800 | [diff] [blame] | 939 | |
| 940 | dir->i_ctime = dir->i_mtime = current_time(dir); |
| 941 | f2fs_mark_inode_dirty_sync(dir, false); |
| 942 | |
| 943 | if (inode) |
| 944 | f2fs_drop_nlink(dir, inode); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | bool f2fs_empty_dir(struct inode *dir) |
| 948 | { |
| 949 | unsigned long bidx; |
| 950 | struct page *dentry_page; |
| 951 | unsigned int bit_pos; |
Chao Yu | dbeacf0 | 2014-09-24 18:17:04 +0800 | [diff] [blame] | 952 | struct f2fs_dentry_block *dentry_blk; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 953 | unsigned long nblock = dir_blocks(dir); |
| 954 | |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 955 | if (f2fs_has_inline_dentry(dir)) |
| 956 | return f2fs_empty_inline_dir(dir); |
| 957 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 958 | for (bidx = 0; bidx < nblock; bidx++) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 959 | dentry_page = f2fs_get_lock_data_page(dir, bidx, false); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 960 | if (IS_ERR(dentry_page)) { |
| 961 | if (PTR_ERR(dentry_page) == -ENOENT) |
| 962 | continue; |
| 963 | else |
| 964 | return false; |
| 965 | } |
| 966 | |
Yunlong Song | bdbc90f | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 967 | dentry_blk = page_address(dentry_page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 968 | if (bidx == 0) |
| 969 | bit_pos = 2; |
| 970 | else |
| 971 | bit_pos = 0; |
| 972 | bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, |
| 973 | NR_DENTRY_IN_BLOCK, |
| 974 | bit_pos); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 975 | |
| 976 | f2fs_put_page(dentry_page, 1); |
| 977 | |
| 978 | if (bit_pos < NR_DENTRY_IN_BLOCK) |
| 979 | return false; |
| 980 | } |
| 981 | return true; |
| 982 | } |
| 983 | |
Chao Yu | ed6bd4b | 2016-10-29 18:46:34 +0800 | [diff] [blame] | 984 | int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 985 | unsigned int start_pos, struct fscrypt_str *fstr) |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 986 | { |
| 987 | unsigned char d_type = DT_UNKNOWN; |
| 988 | unsigned int bit_pos; |
| 989 | struct f2fs_dir_entry *de = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 990 | struct fscrypt_str de_name = FSTR_INIT(NULL, 0); |
Sheng Yong | f6df8f2 | 2017-11-22 18:23:38 +0800 | [diff] [blame] | 991 | struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode); |
Chao Yu | e1293bd | 2018-09-07 19:49:07 +0800 | [diff] [blame] | 992 | struct blk_plug plug; |
| 993 | bool readdir_ra = sbi->readdir_ra == 1; |
| 994 | int err = 0; |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 995 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 996 | bit_pos = ((unsigned long)ctx->pos % d->max); |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 997 | |
Chao Yu | e1293bd | 2018-09-07 19:49:07 +0800 | [diff] [blame] | 998 | if (readdir_ra) |
| 999 | blk_start_plug(&plug); |
| 1000 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 1001 | while (bit_pos < d->max) { |
| 1002 | bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos); |
| 1003 | if (bit_pos >= d->max) |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 1004 | break; |
| 1005 | |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 1006 | de = &d->dentry[bit_pos]; |
Jaegeuk Kim | 7d9dfa1 | 2016-02-12 14:29:28 -0800 | [diff] [blame] | 1007 | if (de->name_len == 0) { |
| 1008 | bit_pos++; |
| 1009 | ctx->pos = start_pos + bit_pos; |
Chao Yu | ddf06b7 | 2019-01-08 10:21:24 +0800 | [diff] [blame] | 1010 | printk_ratelimited( |
Chao Yu | c45d600 | 2019-11-01 17:53:23 +0800 | [diff] [blame] | 1011 | "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.", |
| 1012 | KERN_WARNING, sbi->sb->s_id, |
| 1013 | le32_to_cpu(de->ino)); |
Chao Yu | ddf06b7 | 2019-01-08 10:21:24 +0800 | [diff] [blame] | 1014 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Jaegeuk Kim | 7d9dfa1 | 2016-02-12 14:29:28 -0800 | [diff] [blame] | 1015 | continue; |
| 1016 | } |
| 1017 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1018 | d_type = f2fs_get_de_type(de); |
Jaegeuk Kim | d8c6822 | 2015-04-27 16:26:24 -0700 | [diff] [blame] | 1019 | |
Jaegeuk Kim | d8c6822 | 2015-04-27 16:26:24 -0700 | [diff] [blame] | 1020 | de_name.name = d->filename[bit_pos]; |
| 1021 | de_name.len = le16_to_cpu(de->name_len); |
| 1022 | |
Jaegeuk Kim | 4e240d1 | 2018-11-14 12:40:30 -0800 | [diff] [blame] | 1023 | /* check memory boundary before moving forward */ |
| 1024 | bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); |
Sheng Yong | 720db06 | 2019-01-07 15:02:34 +0800 | [diff] [blame] | 1025 | if (unlikely(bit_pos > d->max || |
| 1026 | le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 1027 | f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.", |
| 1028 | __func__, le16_to_cpu(de->name_len)); |
Jaegeuk Kim | 4e240d1 | 2018-11-14 12:40:30 -0800 | [diff] [blame] | 1029 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 1030 | err = -EFSCORRUPTED; |
Jaegeuk Kim | 4e240d1 | 2018-11-14 12:40:30 -0800 | [diff] [blame] | 1031 | goto out; |
| 1032 | } |
| 1033 | |
Chandan Rajendra | 62230e0d | 2018-12-12 15:20:11 +0530 | [diff] [blame] | 1034 | if (IS_ENCRYPTED(d->inode)) { |
Jaegeuk Kim | d8c6822 | 2015-04-27 16:26:24 -0700 | [diff] [blame] | 1035 | int save_len = fstr->len; |
Jaegeuk Kim | d8c6822 | 2015-04-27 16:26:24 -0700 | [diff] [blame] | 1036 | |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 1037 | err = fscrypt_fname_disk_to_usr(d->inode, |
Chao Yu | 36af5f4 | 2019-05-28 17:23:33 +0800 | [diff] [blame] | 1038 | (u32)le32_to_cpu(de->hash_code), |
| 1039 | 0, &de_name, fstr); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 1040 | if (err) |
Chao Yu | e1293bd | 2018-09-07 19:49:07 +0800 | [diff] [blame] | 1041 | goto out; |
Jaegeuk Kim | 569cf18 | 2015-09-03 13:38:23 -0700 | [diff] [blame] | 1042 | |
| 1043 | de_name = *fstr; |
| 1044 | fstr->len = save_len; |
Jaegeuk Kim | d8c6822 | 2015-04-27 16:26:24 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | if (!dir_emit(ctx, de_name.name, de_name.len, |
Chao Yu | e1293bd | 2018-09-07 19:49:07 +0800 | [diff] [blame] | 1048 | le32_to_cpu(de->ino), d_type)) { |
| 1049 | err = 1; |
| 1050 | goto out; |
| 1051 | } |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 1052 | |
Chao Yu | e1293bd | 2018-09-07 19:49:07 +0800 | [diff] [blame] | 1053 | if (readdir_ra) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1054 | f2fs_ra_node_page(sbi, le32_to_cpu(de->ino)); |
Sheng Yong | f6df8f2 | 2017-11-22 18:23:38 +0800 | [diff] [blame] | 1055 | |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 1056 | ctx->pos = start_pos + bit_pos; |
| 1057 | } |
Chao Yu | e1293bd | 2018-09-07 19:49:07 +0800 | [diff] [blame] | 1058 | out: |
| 1059 | if (readdir_ra) |
| 1060 | blk_finish_plug(&plug); |
| 1061 | return err; |
Jaegeuk Kim | 38594de | 2014-10-15 21:29:51 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 1064 | static int f2fs_readdir(struct file *file, struct dir_context *ctx) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1065 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1066 | struct inode *inode = file_inode(file); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1067 | unsigned long npages = dir_blocks(inode); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1068 | struct f2fs_dentry_block *dentry_blk = NULL; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1069 | struct page *dentry_page = NULL; |
Chao Yu | 817202d9 | 2014-04-28 17:59:43 +0800 | [diff] [blame] | 1070 | struct file_ra_state *ra = &file->f_ra; |
Chao Yu | e97a3c4 | 2017-10-13 18:01:33 +0800 | [diff] [blame] | 1071 | loff_t start_pos = ctx->pos; |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 1072 | unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK); |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 1073 | struct f2fs_dentry_ptr d; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1074 | struct fscrypt_str fstr = FSTR_INIT(NULL, 0); |
Jaegeuk Kim | d8c6822 | 2015-04-27 16:26:24 -0700 | [diff] [blame] | 1075 | int err = 0; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1076 | |
Chandan Rajendra | 62230e0d | 2018-12-12 15:20:11 +0530 | [diff] [blame] | 1077 | if (IS_ENCRYPTED(inode)) { |
Eric Biggers | ec0caa97 | 2020-12-02 18:20:37 -0800 | [diff] [blame] | 1078 | err = fscrypt_prepare_readdir(inode); |
Eric Biggers | 3b1ada55 | 2019-12-09 13:23:48 -0800 | [diff] [blame] | 1079 | if (err) |
Chao Yu | e97a3c4 | 2017-10-13 18:01:33 +0800 | [diff] [blame] | 1080 | goto out; |
Jaegeuk Kim | 26bf3dc | 2015-05-19 22:26:54 -0700 | [diff] [blame] | 1081 | |
Jeff Layton | 8b10fe6 | 2020-08-10 10:21:39 -0400 | [diff] [blame] | 1082 | err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr); |
Jaegeuk Kim | d8c6822 | 2015-04-27 16:26:24 -0700 | [diff] [blame] | 1083 | if (err < 0) |
Chao Yu | e97a3c4 | 2017-10-13 18:01:33 +0800 | [diff] [blame] | 1084 | goto out; |
Jaegeuk Kim | d8c6822 | 2015-04-27 16:26:24 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | if (f2fs_has_inline_dentry(inode)) { |
| 1088 | err = f2fs_read_inline_dir(file, ctx, &fstr); |
Chao Yu | e97a3c4 | 2017-10-13 18:01:33 +0800 | [diff] [blame] | 1089 | goto out_free; |
Jaegeuk Kim | d8c6822 | 2015-04-27 16:26:24 -0700 | [diff] [blame] | 1090 | } |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 1091 | |
Chao Yu | ab383be | 2017-10-13 18:01:36 +0800 | [diff] [blame] | 1092 | for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) { |
Chao Yu | 4414dea | 2017-10-13 18:01:34 +0800 | [diff] [blame] | 1093 | |
| 1094 | /* allow readdir() to be interrupted */ |
| 1095 | if (fatal_signal_pending(current)) { |
| 1096 | err = -ERESTARTSYS; |
| 1097 | goto out_free; |
| 1098 | } |
| 1099 | cond_resched(); |
| 1100 | |
Chao Yu | cb7a844 | 2017-10-13 18:01:35 +0800 | [diff] [blame] | 1101 | /* readahead for multi pages of dir */ |
| 1102 | if (npages - n > 1 && !ra_has_index(ra, n)) |
| 1103 | page_cache_sync_readahead(inode->i_mapping, ra, file, n, |
| 1104 | min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES)); |
| 1105 | |
Gao Xiang | 613f3dc | 2019-02-21 12:57:35 +0800 | [diff] [blame] | 1106 | dentry_page = f2fs_find_data_page(inode, n); |
Chao Yu | 57b62d2 | 2015-11-19 16:09:07 +0800 | [diff] [blame] | 1107 | if (IS_ERR(dentry_page)) { |
| 1108 | err = PTR_ERR(dentry_page); |
Chao Yu | ed6bd4b | 2016-10-29 18:46:34 +0800 | [diff] [blame] | 1109 | if (err == -ENOENT) { |
| 1110 | err = 0; |
Chao Yu | 57b62d2 | 2015-11-19 16:09:07 +0800 | [diff] [blame] | 1111 | continue; |
Chao Yu | ed6bd4b | 2016-10-29 18:46:34 +0800 | [diff] [blame] | 1112 | } else { |
Chao Yu | e97a3c4 | 2017-10-13 18:01:33 +0800 | [diff] [blame] | 1113 | goto out_free; |
Chao Yu | ed6bd4b | 2016-10-29 18:46:34 +0800 | [diff] [blame] | 1114 | } |
Chao Yu | 57b62d2 | 2015-11-19 16:09:07 +0800 | [diff] [blame] | 1115 | } |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1116 | |
Yunlong Song | bdbc90f | 2018-02-28 20:31:52 +0800 | [diff] [blame] | 1117 | dentry_blk = page_address(dentry_page); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1118 | |
Tomohiro Kusumi | 64c24ec | 2017-04-04 13:01:22 +0300 | [diff] [blame] | 1119 | make_dentry_ptr_block(inode, &d, dentry_blk); |
Jaegeuk Kim | 7b3cd7d | 2014-10-18 22:52:52 -0700 | [diff] [blame] | 1120 | |
Chao Yu | ed6bd4b | 2016-10-29 18:46:34 +0800 | [diff] [blame] | 1121 | err = f2fs_fill_dentries(ctx, &d, |
| 1122 | n * NR_DENTRY_IN_BLOCK, &fstr); |
| 1123 | if (err) { |
Gao Xiang | 613f3dc | 2019-02-21 12:57:35 +0800 | [diff] [blame] | 1124 | f2fs_put_page(dentry_page, 0); |
Chao Yu | e9837bc | 2015-12-01 11:41:50 +0800 | [diff] [blame] | 1125 | break; |
| 1126 | } |
Jaegeuk Kim | 99b072bb | 2013-07-05 17:28:12 +0900 | [diff] [blame] | 1127 | |
Gao Xiang | 613f3dc | 2019-02-21 12:57:35 +0800 | [diff] [blame] | 1128 | f2fs_put_page(dentry_page, 0); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1129 | } |
Chao Yu | e97a3c4 | 2017-10-13 18:01:33 +0800 | [diff] [blame] | 1130 | out_free: |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1131 | fscrypt_fname_free_buffer(&fstr); |
Chao Yu | e97a3c4 | 2017-10-13 18:01:33 +0800 | [diff] [blame] | 1132 | out: |
| 1133 | trace_f2fs_readdir(inode, start_pos, ctx->pos, err); |
Chao Yu | ed6bd4b | 2016-10-29 18:46:34 +0800 | [diff] [blame] | 1134 | return err < 0 ? err : 0; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | const struct file_operations f2fs_dir_operations = { |
| 1138 | .llseek = generic_file_llseek, |
| 1139 | .read = generic_read_dir, |
Al Viro | e77d0c6 | 2016-05-10 16:41:13 -0400 | [diff] [blame] | 1140 | .iterate_shared = f2fs_readdir, |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1141 | .fsync = f2fs_sync_file, |
| 1142 | .unlocked_ioctl = f2fs_ioctl, |
hujianyang | 08b9512 | 2015-05-12 16:05:57 +0800 | [diff] [blame] | 1143 | #ifdef CONFIG_COMPAT |
| 1144 | .compat_ioctl = f2fs_compat_ioctl, |
| 1145 | #endif |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 1146 | }; |