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