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