1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/dir.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <asm/unaligned.h>
9 #include <linux/fs.h>
10 #include <linux/f2fs_fs.h>
11 #include <linux/sched/signal.h>
12 #include <linux/unicode.h>
13 #include "f2fs.h"
14 #include "node.h"
15 #include "acl.h"
16 #include "xattr.h"
17 #include <trace/events/f2fs.h>
18
19 #ifdef CONFIG_UNICODE
20 extern struct kmem_cache *f2fs_cf_name_slab;
21 #endif
22
dir_blocks(struct inode * inode)23 static unsigned long dir_blocks(struct inode *inode)
24 {
25 return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1))
26 >> PAGE_SHIFT;
27 }
28
dir_buckets(unsigned int level,int dir_level)29 static unsigned int dir_buckets(unsigned int level, int dir_level)
30 {
31 if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
32 return 1 << (level + dir_level);
33 else
34 return MAX_DIR_BUCKETS;
35 }
36
bucket_blocks(unsigned int level)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
45 static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
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
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
set_de_type(struct f2fs_dir_entry * de,umode_t mode)66 static void set_de_type(struct f2fs_dir_entry *de, umode_t mode)
67 {
68 de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
69 }
70
f2fs_get_de_type(struct f2fs_dir_entry * de)71 unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de)
72 {
73 if (de->file_type < F2FS_FT_MAX)
74 return f2fs_filetype_table[de->file_type];
75 return DT_UNKNOWN;
76 }
77
78 /* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
f2fs_init_casefolded_name(const struct inode * dir,struct f2fs_filename * fname)79 int f2fs_init_casefolded_name(const struct inode *dir,
80 struct f2fs_filename *fname)
81 {
82 #ifdef CONFIG_UNICODE
83 struct super_block *sb = dir->i_sb;
84
85 if (IS_CASEFOLDED(dir) &&
86 !is_dot_dotdot(fname->usr_fname->name, fname->usr_fname->len)) {
87 fname->cf_name.name = kmem_cache_alloc(f2fs_cf_name_slab,
88 GFP_NOFS);
89 if (!fname->cf_name.name)
90 return -ENOMEM;
91 fname->cf_name.len = utf8_casefold(sb->s_encoding,
92 fname->usr_fname,
93 fname->cf_name.name,
94 F2FS_NAME_LEN);
95 if ((int)fname->cf_name.len <= 0) {
96 kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
97 fname->cf_name.name = NULL;
98 if (sb_has_strict_encoding(sb))
99 return -EINVAL;
100 /* fall back to treating name as opaque byte sequence */
101 }
102 }
103 #endif
104 return 0;
105 }
106
__f2fs_setup_filename(const struct inode * dir,const struct fscrypt_name * crypt_name,struct f2fs_filename * fname)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
120 if (crypt_name->is_nokey_name) {
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 */
f2fs_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct f2fs_filename * fname)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 */
f2fs_prepare_lookup(struct inode * dir,struct dentry * dentry,struct f2fs_filename * fname)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
f2fs_free_filename(struct f2fs_filename * fname)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
178 #ifdef CONFIG_UNICODE
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 }
183 #endif
184 }
185
dir_block_index(unsigned int level,int dir_level,unsigned int idx)186 static unsigned long dir_block_index(unsigned int level,
187 int dir_level, unsigned int idx)
188 {
189 unsigned long i;
190 unsigned long bidx = 0;
191
192 for (i = 0; i < level; i++)
193 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
194 bidx += idx * bucket_blocks(level);
195 return bidx;
196 }
197
find_in_block(struct inode * dir,struct page * dentry_page,const struct f2fs_filename * fname,int * max_slots)198 static struct f2fs_dir_entry *find_in_block(struct inode *dir,
199 struct page *dentry_page,
200 const struct f2fs_filename *fname,
201 int *max_slots)
202 {
203 struct f2fs_dentry_block *dentry_blk;
204 struct f2fs_dentry_ptr d;
205
206 dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
207
208 make_dentry_ptr_block(dir, &d, dentry_blk);
209 return f2fs_find_target_dentry(&d, fname, max_slots);
210 }
211
212 #ifdef CONFIG_UNICODE
213 /*
214 * Test whether a case-insensitive directory entry matches the filename
215 * being searched for.
216 *
217 * Returns 1 for a match, 0 for no match, and -errno on an error.
218 */
f2fs_match_ci_name(const struct inode * dir,const struct qstr * name,const u8 * de_name,u32 de_name_len)219 static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
220 const u8 *de_name, u32 de_name_len)
221 {
222 const struct super_block *sb = dir->i_sb;
223 const struct unicode_map *um = sb->s_encoding;
224 struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
225 struct qstr entry = QSTR_INIT(de_name, de_name_len);
226 int res;
227
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
246 res = utf8_strncasecmp_folded(um, name, &entry);
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);
257 }
258 out:
259 kfree(decrypted_name.name);
260 return res;
261 }
262 #endif /* CONFIG_UNICODE */
263
f2fs_match_name(const struct inode * dir,const struct f2fs_filename * fname,const u8 * de_name,u32 de_name_len)264 static inline int f2fs_match_name(const struct inode *dir,
265 const struct f2fs_filename *fname,
266 const u8 *de_name, u32 de_name_len)
267 {
268 struct fscrypt_name f;
269
270 #ifdef CONFIG_UNICODE
271 if (fname->cf_name.name) {
272 struct qstr cf = FSTR_TO_QSTR(&fname->cf_name);
273
274 return f2fs_match_ci_name(dir, &cf, de_name, de_name_len);
275 }
276 #endif
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);
283 }
284
f2fs_find_target_dentry(const struct f2fs_dentry_ptr * d,const struct f2fs_filename * fname,int * max_slots)285 struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
286 const struct f2fs_filename *fname, int *max_slots)
287 {
288 struct f2fs_dir_entry *de;
289 unsigned long bit_pos = 0;
290 int max_len = 0;
291 int res = 0;
292
293 if (max_slots)
294 *max_slots = 0;
295 while (bit_pos < d->max) {
296 if (!test_bit_le(bit_pos, d->bitmap)) {
297 bit_pos++;
298 max_len++;
299 continue;
300 }
301
302 de = &d->dentry[bit_pos];
303
304 if (unlikely(!de->name_len)) {
305 bit_pos++;
306 continue;
307 }
308
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 }
318
319 if (max_slots && max_len > *max_slots)
320 *max_slots = max_len;
321 max_len = 0;
322
323 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
324 }
325
326 de = NULL;
327 found:
328 if (max_slots && max_len > *max_slots)
329 *max_slots = max_len;
330 return de;
331 }
332
find_in_level(struct inode * dir,unsigned int level,const struct f2fs_filename * fname,struct page ** res_page)333 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
334 unsigned int level,
335 const struct f2fs_filename *fname,
336 struct page **res_page)
337 {
338 int s = GET_DENTRY_SLOTS(fname->disk_name.len);
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;
344 int max_slots;
345
346 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
347 nblock = bucket_blocks(level);
348
349 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
350 le32_to_cpu(fname->hash) % nbucket);
351 end_block = bidx + nblock;
352
353 for (; bidx < end_block; bidx++) {
354 /* no need to allocate new dentry pages to all the indices */
355 dentry_page = f2fs_find_data_page(dir, bidx);
356 if (IS_ERR(dentry_page)) {
357 if (PTR_ERR(dentry_page) == -ENOENT) {
358 room = true;
359 continue;
360 } else {
361 *res_page = dentry_page;
362 break;
363 }
364 }
365
366 de = find_in_block(dir, dentry_page, fname, &max_slots);
367 if (IS_ERR(de)) {
368 *res_page = ERR_CAST(de);
369 de = NULL;
370 break;
371 } else if (de) {
372 *res_page = dentry_page;
373 break;
374 }
375
376 if (max_slots >= s)
377 room = true;
378 f2fs_put_page(dentry_page, 0);
379 }
380
381 if (!de && room && F2FS_I(dir)->chash != fname->hash) {
382 F2FS_I(dir)->chash = fname->hash;
383 F2FS_I(dir)->clevel = level;
384 }
385
386 return de;
387 }
388
__f2fs_find_entry(struct inode * dir,const struct f2fs_filename * fname,struct page ** res_page)389 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
390 const struct f2fs_filename *fname,
391 struct page **res_page)
392 {
393 unsigned long npages = dir_blocks(dir);
394 struct f2fs_dir_entry *de = NULL;
395 unsigned int max_depth;
396 unsigned int level;
397
398 *res_page = NULL;
399
400 if (f2fs_has_inline_dentry(dir)) {
401 de = f2fs_find_in_inline_dir(dir, fname, res_page);
402 goto out;
403 }
404
405 if (npages == 0)
406 goto out;
407
408 max_depth = F2FS_I(dir)->i_current_depth;
409 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
410 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
411 dir->i_ino, max_depth);
412 max_depth = MAX_DIR_HASH_DEPTH;
413 f2fs_i_depth_write(dir, max_depth);
414 }
415
416 for (level = 0; level < max_depth; level++) {
417 de = find_in_level(dir, level, fname, res_page);
418 if (de || IS_ERR(*res_page))
419 break;
420 }
421 out:
422 /* This is to increase the speed of f2fs_create */
423 if (!de)
424 F2FS_I(dir)->task = current;
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 */
f2fs_find_entry(struct inode * dir,const struct qstr * child,struct page ** res_page)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;
438 struct f2fs_filename fname;
439 int err;
440
441 err = f2fs_setup_filename(dir, child, 1, &fname);
442 if (err) {
443 if (err == -ENOENT)
444 *res_page = NULL;
445 else
446 *res_page = ERR_PTR(err);
447 return NULL;
448 }
449
450 de = __f2fs_find_entry(dir, &fname, res_page);
451
452 f2fs_free_filename(&fname);
453 return de;
454 }
455
f2fs_parent_dir(struct inode * dir,struct page ** p)456 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
457 {
458 struct qstr dotdot = QSTR_INIT("..", 2);
459
460 return f2fs_find_entry(dir, &dotdot, p);
461 }
462
f2fs_inode_by_name(struct inode * dir,const struct qstr * qstr,struct page ** page)463 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
464 struct page **page)
465 {
466 ino_t res = 0;
467 struct f2fs_dir_entry *de;
468
469 de = f2fs_find_entry(dir, qstr, page);
470 if (de) {
471 res = le32_to_cpu(de->ino);
472 f2fs_put_page(*page, 0);
473 }
474
475 return res;
476 }
477
f2fs_set_link(struct inode * dir,struct f2fs_dir_entry * de,struct page * page,struct inode * inode)478 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
479 struct page *page, struct inode *inode)
480 {
481 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
482
483 lock_page(page);
484 f2fs_wait_on_page_writeback(page, type, true, true);
485 de->ino = cpu_to_le32(inode->i_ino);
486 set_de_type(de, inode->i_mode);
487 set_page_dirty(page);
488
489 dir->i_mtime = dir->i_ctime = current_time(dir);
490 f2fs_mark_inode_dirty_sync(dir, false);
491 f2fs_put_page(page, 1);
492 }
493
init_dent_inode(struct inode * dir,struct inode * inode,const struct f2fs_filename * fname,struct page * ipage)494 static void init_dent_inode(struct inode *dir, struct inode *inode,
495 const struct f2fs_filename *fname,
496 struct page *ipage)
497 {
498 struct f2fs_inode *ri;
499
500 if (!fname) /* tmpfile case? */
501 return;
502
503 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
504
505 /* copy name info. to this inode page */
506 ri = F2FS_INODE(ipage);
507 ri->i_namelen = cpu_to_le32(fname->disk_name.len);
508 memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
509 if (IS_ENCRYPTED(dir)) {
510 file_set_enc_name(inode);
511 /*
512 * Roll-forward recovery doesn't have encryption keys available,
513 * so it can't compute the dirhash for encrypted+casefolded
514 * filenames. Append it to i_name if possible. Else, disable
515 * roll-forward recovery of the dentry (i.e., make fsync'ing the
516 * file force a checkpoint) by setting LOST_PINO.
517 */
518 if (IS_CASEFOLDED(dir)) {
519 if (fname->disk_name.len + sizeof(f2fs_hash_t) <=
520 F2FS_NAME_LEN)
521 put_unaligned(fname->hash, (f2fs_hash_t *)
522 &ri->i_name[fname->disk_name.len]);
523 else
524 file_lost_pino(inode);
525 }
526 }
527 set_page_dirty(ipage);
528 }
529
f2fs_do_make_empty_dir(struct inode * inode,struct inode * parent,struct f2fs_dentry_ptr * d)530 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
531 struct f2fs_dentry_ptr *d)
532 {
533 struct fscrypt_str dot = FSTR_INIT(".", 1);
534 struct fscrypt_str dotdot = FSTR_INIT("..", 2);
535
536 /* update dirent of "." */
537 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
538
539 /* update dirent of ".." */
540 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
541 }
542
make_empty_dir(struct inode * inode,struct inode * parent,struct page * page)543 static int make_empty_dir(struct inode *inode,
544 struct inode *parent, struct page *page)
545 {
546 struct page *dentry_page;
547 struct f2fs_dentry_block *dentry_blk;
548 struct f2fs_dentry_ptr d;
549
550 if (f2fs_has_inline_dentry(inode))
551 return f2fs_make_empty_inline_dir(inode, parent, page);
552
553 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
554 if (IS_ERR(dentry_page))
555 return PTR_ERR(dentry_page);
556
557 dentry_blk = page_address(dentry_page);
558
559 make_dentry_ptr_block(NULL, &d, dentry_blk);
560 f2fs_do_make_empty_dir(inode, parent, &d);
561
562 set_page_dirty(dentry_page);
563 f2fs_put_page(dentry_page, 1);
564 return 0;
565 }
566
f2fs_init_inode_metadata(struct inode * inode,struct inode * dir,const struct f2fs_filename * fname,struct page * dpage)567 struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
568 const struct f2fs_filename *fname, struct page *dpage)
569 {
570 struct page *page;
571 int err;
572
573 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
574 page = f2fs_new_inode_page(inode);
575 if (IS_ERR(page))
576 return page;
577
578 if (S_ISDIR(inode->i_mode)) {
579 /* in order to handle error case */
580 get_page(page);
581 err = make_empty_dir(inode, dir, page);
582 if (err) {
583 lock_page(page);
584 goto put_error;
585 }
586 put_page(page);
587 }
588
589 err = f2fs_init_acl(inode, dir, page, dpage);
590 if (err)
591 goto put_error;
592
593 err = f2fs_init_security(inode, dir,
594 fname ? fname->usr_fname : NULL, page);
595 if (err)
596 goto put_error;
597
598 if (IS_ENCRYPTED(inode)) {
599 err = fscrypt_set_context(inode, page);
600 if (err)
601 goto put_error;
602 }
603 } else {
604 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
605 if (IS_ERR(page))
606 return page;
607 }
608
609 init_dent_inode(dir, inode, fname, page);
610
611 /*
612 * This file should be checkpointed during fsync.
613 * We lost i_pino from now on.
614 */
615 if (is_inode_flag_set(inode, FI_INC_LINK)) {
616 if (!S_ISDIR(inode->i_mode))
617 file_lost_pino(inode);
618 /*
619 * If link the tmpfile to alias through linkat path,
620 * we should remove this inode from orphan list.
621 */
622 if (inode->i_nlink == 0)
623 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
624 f2fs_i_links_write(inode, true);
625 }
626 return page;
627
628 put_error:
629 clear_nlink(inode);
630 f2fs_update_inode(inode, page);
631 f2fs_put_page(page, 1);
632 return ERR_PTR(err);
633 }
634
f2fs_update_parent_metadata(struct inode * dir,struct inode * inode,unsigned int current_depth)635 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
636 unsigned int current_depth)
637 {
638 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
639 if (S_ISDIR(inode->i_mode))
640 f2fs_i_links_write(dir, true);
641 clear_inode_flag(inode, FI_NEW_INODE);
642 }
643 dir->i_mtime = dir->i_ctime = current_time(dir);
644 f2fs_mark_inode_dirty_sync(dir, false);
645
646 if (F2FS_I(dir)->i_current_depth != current_depth)
647 f2fs_i_depth_write(dir, current_depth);
648
649 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
650 clear_inode_flag(inode, FI_INC_LINK);
651 }
652
f2fs_room_for_filename(const void * bitmap,int slots,int max_slots)653 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
654 {
655 int bit_start = 0;
656 int zero_start, zero_end;
657 next:
658 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
659 if (zero_start >= max_slots)
660 return max_slots;
661
662 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
663 if (zero_end - zero_start >= slots)
664 return zero_start;
665
666 bit_start = zero_end + 1;
667
668 if (zero_end + 1 >= max_slots)
669 return max_slots;
670 goto next;
671 }
672
f2fs_has_enough_room(struct inode * dir,struct page * ipage,const struct f2fs_filename * fname)673 bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
674 const struct f2fs_filename *fname)
675 {
676 struct f2fs_dentry_ptr d;
677 unsigned int bit_pos;
678 int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
679
680 make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));
681
682 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
683
684 return bit_pos < d.max;
685 }
686
f2fs_update_dentry(nid_t ino,umode_t mode,struct f2fs_dentry_ptr * d,const struct fscrypt_str * name,f2fs_hash_t name_hash,unsigned int bit_pos)687 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
688 const struct fscrypt_str *name, f2fs_hash_t name_hash,
689 unsigned int bit_pos)
690 {
691 struct f2fs_dir_entry *de;
692 int slots = GET_DENTRY_SLOTS(name->len);
693 int i;
694
695 de = &d->dentry[bit_pos];
696 de->hash_code = name_hash;
697 de->name_len = cpu_to_le16(name->len);
698 memcpy(d->filename[bit_pos], name->name, name->len);
699 de->ino = cpu_to_le32(ino);
700 set_de_type(de, mode);
701 for (i = 0; i < slots; i++) {
702 __set_bit_le(bit_pos + i, (void *)d->bitmap);
703 /* avoid wrong garbage data for readdir */
704 if (i)
705 (de + i)->name_len = 0;
706 }
707 }
708
f2fs_add_regular_entry(struct inode * dir,const struct f2fs_filename * fname,struct inode * inode,nid_t ino,umode_t mode)709 int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
710 struct inode *inode, nid_t ino, umode_t mode)
711 {
712 unsigned int bit_pos;
713 unsigned int level;
714 unsigned int current_depth;
715 unsigned long bidx, block;
716 unsigned int nbucket, nblock;
717 struct page *dentry_page = NULL;
718 struct f2fs_dentry_block *dentry_blk = NULL;
719 struct f2fs_dentry_ptr d;
720 struct page *page = NULL;
721 int slots, err = 0;
722
723 level = 0;
724 slots = GET_DENTRY_SLOTS(fname->disk_name.len);
725
726 current_depth = F2FS_I(dir)->i_current_depth;
727 if (F2FS_I(dir)->chash == fname->hash) {
728 level = F2FS_I(dir)->clevel;
729 F2FS_I(dir)->chash = 0;
730 }
731
732 start:
733 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
734 f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH);
735 return -ENOSPC;
736 }
737
738 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
739 return -ENOSPC;
740
741 /* Increase the depth, if required */
742 if (level == current_depth)
743 ++current_depth;
744
745 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
746 nblock = bucket_blocks(level);
747
748 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
749 (le32_to_cpu(fname->hash) % nbucket));
750
751 for (block = bidx; block <= (bidx + nblock - 1); block++) {
752 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
753 if (IS_ERR(dentry_page))
754 return PTR_ERR(dentry_page);
755
756 dentry_blk = page_address(dentry_page);
757 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
758 slots, NR_DENTRY_IN_BLOCK);
759 if (bit_pos < NR_DENTRY_IN_BLOCK)
760 goto add_dentry;
761
762 f2fs_put_page(dentry_page, 1);
763 }
764
765 /* Move to next level to find the empty slot for new dentry */
766 ++level;
767 goto start;
768 add_dentry:
769 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
770
771 if (inode) {
772 f2fs_down_write(&F2FS_I(inode)->i_sem);
773 page = f2fs_init_inode_metadata(inode, dir, fname, NULL);
774 if (IS_ERR(page)) {
775 err = PTR_ERR(page);
776 goto fail;
777 }
778 }
779
780 make_dentry_ptr_block(NULL, &d, dentry_blk);
781 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
782 bit_pos);
783
784 set_page_dirty(dentry_page);
785
786 if (inode) {
787 f2fs_i_pino_write(inode, dir->i_ino);
788
789 /* synchronize inode page's data from inode cache */
790 if (is_inode_flag_set(inode, FI_NEW_INODE))
791 f2fs_update_inode(inode, page);
792
793 f2fs_put_page(page, 1);
794 }
795
796 f2fs_update_parent_metadata(dir, inode, current_depth);
797 fail:
798 if (inode)
799 f2fs_up_write(&F2FS_I(inode)->i_sem);
800
801 f2fs_put_page(dentry_page, 1);
802
803 return err;
804 }
805
f2fs_add_dentry(struct inode * dir,const struct f2fs_filename * fname,struct inode * inode,nid_t ino,umode_t mode)806 int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
807 struct inode *inode, nid_t ino, umode_t mode)
808 {
809 int err = -EAGAIN;
810
811 if (f2fs_has_inline_dentry(dir))
812 err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
813 if (err == -EAGAIN)
814 err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
815
816 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
817 return err;
818 }
819
820 /*
821 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
822 * f2fs_unlock_op().
823 */
f2fs_do_add_link(struct inode * dir,const struct qstr * name,struct inode * inode,nid_t ino,umode_t mode)824 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
825 struct inode *inode, nid_t ino, umode_t mode)
826 {
827 struct f2fs_filename fname;
828 struct page *page = NULL;
829 struct f2fs_dir_entry *de = NULL;
830 int err;
831
832 err = f2fs_setup_filename(dir, name, 0, &fname);
833 if (err)
834 return err;
835
836 /*
837 * An immature stackable filesystem shows a race condition between lookup
838 * and create. If we have same task when doing lookup and create, it's
839 * definitely fine as expected by VFS normally. Otherwise, let's just
840 * verify on-disk dentry one more time, which guarantees filesystem
841 * consistency more.
842 */
843 if (current != F2FS_I(dir)->task) {
844 de = __f2fs_find_entry(dir, &fname, &page);
845 F2FS_I(dir)->task = NULL;
846 }
847 if (de) {
848 f2fs_put_page(page, 0);
849 err = -EEXIST;
850 } else if (IS_ERR(page)) {
851 err = PTR_ERR(page);
852 } else {
853 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
854 }
855 f2fs_free_filename(&fname);
856 return err;
857 }
858
f2fs_do_tmpfile(struct inode * inode,struct inode * dir)859 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
860 {
861 struct page *page;
862 int err = 0;
863
864 f2fs_down_write(&F2FS_I(inode)->i_sem);
865 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL);
866 if (IS_ERR(page)) {
867 err = PTR_ERR(page);
868 goto fail;
869 }
870 f2fs_put_page(page, 1);
871
872 clear_inode_flag(inode, FI_NEW_INODE);
873 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
874 fail:
875 f2fs_up_write(&F2FS_I(inode)->i_sem);
876 return err;
877 }
878
f2fs_drop_nlink(struct inode * dir,struct inode * inode)879 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
880 {
881 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
882
883 f2fs_down_write(&F2FS_I(inode)->i_sem);
884
885 if (S_ISDIR(inode->i_mode))
886 f2fs_i_links_write(dir, false);
887 inode->i_ctime = current_time(inode);
888
889 f2fs_i_links_write(inode, false);
890 if (S_ISDIR(inode->i_mode)) {
891 f2fs_i_links_write(inode, false);
892 f2fs_i_size_write(inode, 0);
893 }
894 f2fs_up_write(&F2FS_I(inode)->i_sem);
895
896 if (inode->i_nlink == 0)
897 f2fs_add_orphan_inode(inode);
898 else
899 f2fs_release_orphan_inode(sbi);
900 }
901
902 /*
903 * It only removes the dentry from the dentry page, corresponding name
904 * entry in name page does not need to be touched during deletion.
905 */
f2fs_delete_entry(struct f2fs_dir_entry * dentry,struct page * page,struct inode * dir,struct inode * inode)906 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
907 struct inode *dir, struct inode *inode)
908 {
909 struct f2fs_dentry_block *dentry_blk;
910 unsigned int bit_pos;
911 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
912 int i;
913
914 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
915
916 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
917 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
918
919 if (f2fs_has_inline_dentry(dir))
920 return f2fs_delete_inline_entry(dentry, page, dir, inode);
921
922 lock_page(page);
923 f2fs_wait_on_page_writeback(page, DATA, true, true);
924
925 dentry_blk = page_address(page);
926 bit_pos = dentry - dentry_blk->dentry;
927 for (i = 0; i < slots; i++)
928 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
929
930 /* Let's check and deallocate this dentry page */
931 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
932 NR_DENTRY_IN_BLOCK,
933 0);
934 set_page_dirty(page);
935
936 if (bit_pos == NR_DENTRY_IN_BLOCK &&
937 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
938 f2fs_clear_page_cache_dirty_tag(page);
939 clear_page_dirty_for_io(page);
940 ClearPageUptodate(page);
941
942 clear_page_private_gcing(page);
943
944 inode_dec_dirty_pages(dir);
945 f2fs_remove_dirty_inode(dir);
946
947 detach_page_private(page);
948 set_page_private(page, 0);
949 }
950 f2fs_put_page(page, 1);
951
952 dir->i_ctime = dir->i_mtime = current_time(dir);
953 f2fs_mark_inode_dirty_sync(dir, false);
954
955 if (inode)
956 f2fs_drop_nlink(dir, inode);
957 }
958
f2fs_empty_dir(struct inode * dir)959 bool f2fs_empty_dir(struct inode *dir)
960 {
961 unsigned long bidx;
962 struct page *dentry_page;
963 unsigned int bit_pos;
964 struct f2fs_dentry_block *dentry_blk;
965 unsigned long nblock = dir_blocks(dir);
966
967 if (f2fs_has_inline_dentry(dir))
968 return f2fs_empty_inline_dir(dir);
969
970 for (bidx = 0; bidx < nblock; bidx++) {
971 dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
972 if (IS_ERR(dentry_page)) {
973 if (PTR_ERR(dentry_page) == -ENOENT)
974 continue;
975 else
976 return false;
977 }
978
979 dentry_blk = page_address(dentry_page);
980 if (bidx == 0)
981 bit_pos = 2;
982 else
983 bit_pos = 0;
984 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
985 NR_DENTRY_IN_BLOCK,
986 bit_pos);
987
988 f2fs_put_page(dentry_page, 1);
989
990 if (bit_pos < NR_DENTRY_IN_BLOCK)
991 return false;
992 }
993 return true;
994 }
995
f2fs_fill_dentries(struct dir_context * ctx,struct f2fs_dentry_ptr * d,unsigned int start_pos,struct fscrypt_str * fstr)996 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
997 unsigned int start_pos, struct fscrypt_str *fstr)
998 {
999 unsigned char d_type = DT_UNKNOWN;
1000 unsigned int bit_pos;
1001 struct f2fs_dir_entry *de = NULL;
1002 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
1003 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
1004 struct blk_plug plug;
1005 bool readdir_ra = sbi->readdir_ra == 1;
1006 bool found_valid_dirent = false;
1007 int err = 0;
1008
1009 bit_pos = ((unsigned long)ctx->pos % d->max);
1010
1011 if (readdir_ra)
1012 blk_start_plug(&plug);
1013
1014 while (bit_pos < d->max) {
1015 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
1016 if (bit_pos >= d->max)
1017 break;
1018
1019 de = &d->dentry[bit_pos];
1020 if (de->name_len == 0) {
1021 if (found_valid_dirent || !bit_pos) {
1022 printk_ratelimited(
1023 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
1024 KERN_WARNING, sbi->sb->s_id,
1025 le32_to_cpu(de->ino));
1026 set_sbi_flag(sbi, SBI_NEED_FSCK);
1027 }
1028 bit_pos++;
1029 ctx->pos = start_pos + bit_pos;
1030 continue;
1031 }
1032
1033 d_type = f2fs_get_de_type(de);
1034
1035 de_name.name = d->filename[bit_pos];
1036 de_name.len = le16_to_cpu(de->name_len);
1037
1038 /* check memory boundary before moving forward */
1039 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
1040 if (unlikely(bit_pos > d->max ||
1041 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
1042 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
1043 __func__, le16_to_cpu(de->name_len));
1044 set_sbi_flag(sbi, SBI_NEED_FSCK);
1045 err = -EFSCORRUPTED;
1046 goto out;
1047 }
1048
1049 if (IS_ENCRYPTED(d->inode)) {
1050 int save_len = fstr->len;
1051
1052 err = fscrypt_fname_disk_to_usr(d->inode,
1053 (u32)le32_to_cpu(de->hash_code),
1054 0, &de_name, fstr);
1055 if (err)
1056 goto out;
1057
1058 de_name = *fstr;
1059 fstr->len = save_len;
1060 }
1061
1062 if (!dir_emit(ctx, de_name.name, de_name.len,
1063 le32_to_cpu(de->ino), d_type)) {
1064 err = 1;
1065 goto out;
1066 }
1067
1068 if (readdir_ra)
1069 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
1070
1071 ctx->pos = start_pos + bit_pos;
1072 found_valid_dirent = true;
1073 }
1074 out:
1075 if (readdir_ra)
1076 blk_finish_plug(&plug);
1077 return err;
1078 }
1079
f2fs_readdir(struct file * file,struct dir_context * ctx)1080 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
1081 {
1082 struct inode *inode = file_inode(file);
1083 unsigned long npages = dir_blocks(inode);
1084 struct f2fs_dentry_block *dentry_blk = NULL;
1085 struct page *dentry_page = NULL;
1086 struct file_ra_state *ra = &file->f_ra;
1087 loff_t start_pos = ctx->pos;
1088 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
1089 struct f2fs_dentry_ptr d;
1090 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1091 int err = 0;
1092
1093 if (IS_ENCRYPTED(inode)) {
1094 err = fscrypt_prepare_readdir(inode);
1095 if (err)
1096 goto out;
1097
1098 err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
1099 if (err < 0)
1100 goto out;
1101 }
1102
1103 if (f2fs_has_inline_dentry(inode)) {
1104 err = f2fs_read_inline_dir(file, ctx, &fstr);
1105 goto out_free;
1106 }
1107
1108 for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
1109
1110 /* allow readdir() to be interrupted */
1111 if (fatal_signal_pending(current)) {
1112 err = -ERESTARTSYS;
1113 goto out_free;
1114 }
1115 cond_resched();
1116
1117 /* readahead for multi pages of dir */
1118 if (npages - n > 1 && !ra_has_index(ra, n))
1119 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1120 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1121
1122 dentry_page = f2fs_find_data_page(inode, n);
1123 if (IS_ERR(dentry_page)) {
1124 err = PTR_ERR(dentry_page);
1125 if (err == -ENOENT) {
1126 err = 0;
1127 continue;
1128 } else {
1129 goto out_free;
1130 }
1131 }
1132
1133 dentry_blk = page_address(dentry_page);
1134
1135 make_dentry_ptr_block(inode, &d, dentry_blk);
1136
1137 err = f2fs_fill_dentries(ctx, &d,
1138 n * NR_DENTRY_IN_BLOCK, &fstr);
1139 if (err) {
1140 f2fs_put_page(dentry_page, 0);
1141 break;
1142 }
1143
1144 f2fs_put_page(dentry_page, 0);
1145 }
1146 out_free:
1147 fscrypt_fname_free_buffer(&fstr);
1148 out:
1149 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1150 return err < 0 ? err : 0;
1151 }
1152
1153 const struct file_operations f2fs_dir_operations = {
1154 .llseek = generic_file_llseek,
1155 .read = generic_read_dir,
1156 .iterate_shared = f2fs_readdir,
1157 .fsync = f2fs_sync_file,
1158 .unlocked_ioctl = f2fs_ioctl,
1159 #ifdef CONFIG_COMPAT
1160 .compat_ioctl = f2fs_compat_ioctl,
1161 #endif
1162 };
1163