3 #include "btree_update.h"
13 #include <linux/dcache.h> /* struct qstr */
14 #include <linux/generic-radix-tree.h>
16 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
18 static int remove_dirent(struct bch_fs *c, struct btree_iter *iter,
19 struct bkey_s_c_dirent dirent)
22 struct bch_inode_unpacked dir_inode;
23 struct bch_hash_info dir_hash_info;
24 u64 dir_inum = dirent.k->p.inode;
28 name.len = bch2_dirent_name_bytes(dirent);
29 buf = kmalloc(name.len + 1, GFP_KERNEL);
33 memcpy(buf, dirent.v->d_name, name.len);
37 /* Unlock iter so we don't deadlock, after copying name: */
38 bch2_btree_iter_unlock(iter);
40 ret = bch2_inode_find_by_inum(c, dir_inum, &dir_inode);
42 bch_err(c, "remove_dirent: err %i looking up directory inode", ret);
46 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
48 ret = bch2_dirent_delete(c, dir_inum, &dir_hash_info, &name, NULL);
50 bch_err(c, "remove_dirent: err %i deleting dirent", ret);
56 static int reattach_inode(struct bch_fs *c,
57 struct bch_inode_unpacked *lostfound_inode,
60 struct bch_hash_info lostfound_hash_info =
61 bch2_hash_info_init(c, lostfound_inode);
62 struct bkey_inode_buf packed;
67 snprintf(name_buf, sizeof(name_buf), "%llu", inum);
68 name = (struct qstr) QSTR(name_buf);
70 lostfound_inode->bi_nlink++;
72 bch2_inode_pack(&packed, lostfound_inode);
74 ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
75 NULL, NULL, BTREE_INSERT_NOFAIL);
77 bch_err(c, "error %i reattaching inode %llu while updating lost+found",
82 ret = bch2_dirent_create(c, lostfound_inode->bi_inum,
84 DT_DIR, &name, inum, NULL,
87 bch_err(c, "error %i reattaching inode %llu while creating new dirent",
95 bool first_this_inode;
98 struct bch_inode_unpacked inode;
101 static struct inode_walker inode_walker_init(void)
103 return (struct inode_walker) {
109 static int walk_inode(struct bch_fs *c, struct inode_walker *w, u64 inum)
111 w->first_this_inode = inum != w->cur_inum;
114 if (w->first_this_inode) {
115 int ret = bch2_inode_find_by_inum(c, inum, &w->inode);
117 if (ret && ret != -ENOENT)
120 w->have_inode = !ret;
127 struct bch_hash_info info;
128 struct btree_trans *trans;
130 /* start of current chain of hash collisions: */
131 struct btree_iter *chain;
133 /* next offset in current chain of hash collisions: */
137 static void hash_check_init(const struct bch_hash_desc desc,
138 struct btree_trans *trans,
139 struct hash_check *h)
142 h->chain = bch2_trans_get_iter(trans, desc.btree_id, POS_MIN, 0);
146 static void hash_check_set_inode(struct hash_check *h, struct bch_fs *c,
147 const struct bch_inode_unpacked *bi)
149 h->info = bch2_hash_info_init(c, bi);
153 static int hash_redo_key(const struct bch_hash_desc desc,
154 struct hash_check *h, struct bch_fs *c,
155 struct btree_iter *k_iter, struct bkey_s_c k,
161 tmp = kmalloc(bkey_bytes(k.k), GFP_KERNEL);
165 bkey_reassemble(tmp, k);
167 ret = bch2_btree_delete_at(k_iter, 0);
171 bch2_btree_iter_unlock(k_iter);
173 bch2_hash_set(desc, &h->info, c, k_iter->pos.inode, NULL, tmp,
175 BCH_HASH_SET_MUST_CREATE);
181 /* fsck hasn't been converted to new transactions yet: */
182 static int fsck_hash_delete_at(const struct bch_hash_desc desc,
183 struct bch_hash_info *info,
184 struct btree_iter *orig_iter)
186 struct btree_trans trans;
187 struct btree_iter *iter;
190 bch2_btree_iter_unlock(orig_iter);
192 bch2_trans_init(&trans, orig_iter->c);
194 bch2_trans_begin(&trans);
196 iter = bch2_trans_copy_iter(&trans, orig_iter);
202 ret = bch2_hash_delete_at(&trans, desc, info, iter) ?:
203 bch2_trans_commit(&trans, NULL, NULL,
205 BTREE_INSERT_NOFAIL);
210 bch2_trans_exit(&trans);
214 static int hash_check_duplicates(const struct bch_hash_desc desc,
215 struct hash_check *h, struct bch_fs *c,
216 struct btree_iter *k_iter, struct bkey_s_c k)
218 struct btree_iter *iter;
223 if (!bkey_cmp(h->chain->pos, k_iter->pos))
226 iter = bch2_trans_copy_iter(h->trans, h->chain);
227 BUG_ON(IS_ERR(iter));
229 for_each_btree_key_continue(iter, 0, k2) {
230 if (bkey_cmp(k2.k->p, k.k->p) >= 0)
233 if (fsck_err_on(k2.k->type == desc.key_type &&
234 !desc.cmp_bkey(k, k2), c,
235 "duplicate hash table keys:\n%s",
236 (bch2_bkey_val_to_text(&PBUF(buf), c,
238 ret = fsck_hash_delete_at(desc, &h->info, k_iter);
246 bch2_trans_iter_free(h->trans, iter);
250 static bool key_has_correct_hash(const struct bch_hash_desc desc,
251 struct hash_check *h, struct bch_fs *c,
252 struct btree_iter *k_iter, struct bkey_s_c k)
256 if (k.k->type != KEY_TYPE_whiteout &&
257 k.k->type != desc.key_type)
260 if (k.k->p.offset != h->next)
261 bch2_btree_iter_copy(h->chain, k_iter);
262 h->next = k.k->p.offset + 1;
264 if (k.k->type != desc.key_type)
267 hash = desc.hash_bkey(&h->info, k);
269 return hash >= h->chain->pos.offset &&
270 hash <= k.k->p.offset;
273 static int hash_check_key(const struct bch_hash_desc desc,
274 struct hash_check *h, struct bch_fs *c,
275 struct btree_iter *k_iter, struct bkey_s_c k)
281 if (k.k->type != KEY_TYPE_whiteout &&
282 k.k->type != desc.key_type)
285 if (k.k->p.offset != h->next)
286 bch2_btree_iter_copy(h->chain, k_iter);
287 h->next = k.k->p.offset + 1;
289 if (k.k->type != desc.key_type)
292 hashed = desc.hash_bkey(&h->info, k);
294 if (fsck_err_on(hashed < h->chain->pos.offset ||
295 hashed > k.k->p.offset, c,
296 "hash table key at wrong offset: btree %u, %llu, "
297 "hashed to %llu chain starts at %llu\n%s",
298 desc.btree_id, k.k->p.offset,
299 hashed, h->chain->pos.offset,
300 (bch2_bkey_val_to_text(&PBUF(buf), c,
302 ret = hash_redo_key(desc, h, c, k_iter, k, hashed);
304 bch_err(c, "hash_redo_key err %i", ret);
310 ret = hash_check_duplicates(desc, h, c, k_iter, k);
315 static int check_dirent_hash(struct hash_check *h, struct bch_fs *c,
316 struct btree_iter *iter, struct bkey_s_c *k)
318 struct bkey_i_dirent *d = NULL;
324 if (key_has_correct_hash(bch2_dirent_hash_desc, h, c, iter, *k))
327 len = bch2_dirent_name_bytes(bkey_s_c_to_dirent(*k));
330 memcpy(buf, bkey_s_c_to_dirent(*k).v->d_name, len);
333 d = kmalloc(bkey_bytes(k->k), GFP_KERNEL);
335 bch_err(c, "memory allocation failure");
339 bkey_reassemble(&d->k_i, *k);
346 d->k.u64s = BKEY_U64s + dirent_val_u64s(len);
348 BUG_ON(bkey_val_bytes(&d->k) <
349 offsetof(struct bch_dirent, d_name) + len);
351 memset(d->v.d_name + len, 0,
352 bkey_val_bytes(&d->k) -
353 offsetof(struct bch_dirent, d_name) - len);
355 hash = bch2_dirent_hash_desc.hash_bkey(&h->info,
356 bkey_i_to_s_c(&d->k_i));
357 } while (hash < h->chain->pos.offset ||
358 hash > k->k->p.offset);
360 if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
361 buf, strlen(buf), d->v.d_name, len)) {
362 ret = bch2_btree_insert_at(c, NULL, NULL,
364 BTREE_INSERT_ENTRY(iter, &d->k_i));
368 *k = bch2_btree_iter_peek(iter);
370 BUG_ON(k->k->type != KEY_TYPE_dirent);
377 hash = bch2_dirent_hash_desc.hash_bkey(&h->info, *k);
379 if (fsck_err(c, "cannot fix dirent by removing trailing garbage %s (%zu)\n"
380 "hash table key at wrong offset: btree %u, offset %llu, "
381 "hashed to %llu chain starts at %llu\n%s",
382 buf, strlen(buf), BTREE_ID_DIRENTS,
383 k->k->p.offset, hash, h->chain->pos.offset,
384 (bch2_bkey_val_to_text(&PBUF(buf), c,
386 ret = hash_redo_key(bch2_dirent_hash_desc,
387 h, c, iter, *k, hash);
389 bch_err(c, "hash_redo_key err %i", ret);
397 static int bch2_inode_truncate(struct bch_fs *c, u64 inode_nr, u64 new_size)
399 return bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
400 POS(inode_nr, round_up(new_size, block_bytes(c)) >> 9),
401 POS(inode_nr + 1, 0), NULL);
405 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
406 * that i_size an i_sectors are consistent
409 static int check_extents(struct bch_fs *c)
411 struct inode_walker w = inode_walker_init();
412 struct btree_iter iter;
417 bch_verbose(c, "checking extents");
419 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS,
420 POS(BCACHEFS_ROOT_INO, 0), 0, k) {
421 ret = walk_inode(c, &w, k.k->p.inode);
425 if (fsck_err_on(!w.have_inode, c,
426 "extent type %u for missing inode %llu",
427 k.k->type, k.k->p.inode) ||
428 fsck_err_on(w.have_inode &&
429 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
430 "extent type %u for non regular file, inode %llu mode %o",
431 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
432 bch2_btree_iter_unlock(&iter);
434 ret = bch2_inode_truncate(c, k.k->p.inode, 0);
440 if (fsck_err_on(w.first_this_inode &&
442 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
443 w.inode.bi_sectors !=
444 (i_sectors = bch2_count_inode_sectors(c, w.cur_inum)),
445 c, "i_sectors wrong: got %llu, should be %llu",
446 w.inode.bi_sectors, i_sectors)) {
447 struct bkey_inode_buf p;
449 w.inode.bi_sectors = i_sectors;
451 bch2_btree_iter_unlock(&iter);
453 bch2_inode_pack(&p, &w.inode);
455 ret = bch2_btree_insert(c, BTREE_ID_INODES,
456 &p.inode.k_i, NULL, NULL,
457 BTREE_INSERT_NOFAIL);
459 bch_err(c, "error in fs gc: error %i "
460 "updating inode", ret);
464 /* revalidate iterator: */
465 k = bch2_btree_iter_peek(&iter);
468 if (fsck_err_on(w.have_inode &&
469 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
470 k.k->type != KEY_TYPE_reservation &&
471 k.k->p.offset > round_up(w.inode.bi_size, PAGE_SIZE) >> 9, c,
472 "extent type %u offset %llu past end of inode %llu, i_size %llu",
473 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
474 bch2_btree_iter_unlock(&iter);
476 ret = bch2_inode_truncate(c, k.k->p.inode,
485 return bch2_btree_iter_unlock(&iter) ?: ret;
489 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
493 static int check_dirents(struct bch_fs *c)
495 struct inode_walker w = inode_walker_init();
497 struct btree_trans trans;
498 struct btree_iter *iter;
504 bch_verbose(c, "checking dirents");
506 bch2_trans_init(&trans, c);
508 bch2_trans_preload_iters(&trans);
510 iter = bch2_trans_get_iter(&trans, BTREE_ID_DIRENTS,
511 POS(BCACHEFS_ROOT_INO, 0), 0);
513 hash_check_init(bch2_dirent_hash_desc, &trans, &h);
515 for_each_btree_key_continue(iter, 0, k) {
516 struct bkey_s_c_dirent d;
517 struct bch_inode_unpacked target;
521 ret = walk_inode(c, &w, k.k->p.inode);
525 if (fsck_err_on(!w.have_inode, c,
526 "dirent in nonexisting directory:\n%s",
527 (bch2_bkey_val_to_text(&PBUF(buf), c,
529 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
530 "dirent in non directory inode type %u:\n%s",
531 mode_to_type(w.inode.bi_mode),
532 (bch2_bkey_val_to_text(&PBUF(buf), c,
534 ret = bch2_btree_delete_at(iter, 0);
540 if (w.first_this_inode && w.have_inode)
541 hash_check_set_inode(&h, c, &w.inode);
543 ret = check_dirent_hash(&h, c, iter, &k);
554 if (k.k->type != KEY_TYPE_dirent)
557 d = bkey_s_c_to_dirent(k);
558 d_inum = le64_to_cpu(d.v->d_inum);
560 name_len = bch2_dirent_name_bytes(d);
562 if (fsck_err_on(!name_len, c, "empty dirent") ||
563 fsck_err_on(name_len == 1 &&
564 !memcmp(d.v->d_name, ".", 1), c,
566 fsck_err_on(name_len == 2 &&
567 !memcmp(d.v->d_name, "..", 2), c,
569 fsck_err_on(name_len == 2 &&
570 !memcmp(d.v->d_name, "..", 2), c,
572 fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
573 "dirent name has invalid chars")) {
574 ret = remove_dirent(c, iter, d);
580 if (fsck_err_on(d_inum == d.k->p.inode, c,
581 "dirent points to own directory:\n%s",
582 (bch2_bkey_val_to_text(&PBUF(buf), c,
584 ret = remove_dirent(c, iter, d);
590 ret = bch2_inode_find_by_inum(c, d_inum, &target);
591 if (ret && ret != -ENOENT)
597 if (fsck_err_on(!have_target, c,
598 "dirent points to missing inode:\n%s",
599 (bch2_bkey_val_to_text(&PBUF(buf), c,
601 ret = remove_dirent(c, iter, d);
607 if (fsck_err_on(have_target &&
609 mode_to_type(target.bi_mode), c,
610 "incorrect d_type: should be %u:\n%s",
611 mode_to_type(target.bi_mode),
612 (bch2_bkey_val_to_text(&PBUF(buf), c,
614 struct bkey_i_dirent *n;
616 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
622 bkey_reassemble(&n->k_i, d.s_c);
623 n->v.d_type = mode_to_type(target.bi_mode);
625 ret = bch2_btree_insert_at(c, NULL, NULL,
627 BTREE_INSERT_ENTRY(iter, &n->k_i));
636 return bch2_trans_exit(&trans) ?: ret;
640 * Walk xattrs: verify that they all have a corresponding inode
643 static int check_xattrs(struct bch_fs *c)
645 struct inode_walker w = inode_walker_init();
647 struct btree_trans trans;
648 struct btree_iter *iter;
652 bch_verbose(c, "checking xattrs");
654 bch2_trans_init(&trans, c);
656 bch2_trans_preload_iters(&trans);
658 iter = bch2_trans_get_iter(&trans, BTREE_ID_XATTRS,
659 POS(BCACHEFS_ROOT_INO, 0), 0);
661 hash_check_init(bch2_xattr_hash_desc, &trans, &h);
663 for_each_btree_key_continue(iter, 0, k) {
664 ret = walk_inode(c, &w, k.k->p.inode);
668 if (fsck_err_on(!w.have_inode, c,
669 "xattr for missing inode %llu",
671 ret = bch2_btree_delete_at(iter, 0);
677 if (w.first_this_inode && w.have_inode)
678 hash_check_set_inode(&h, c, &w.inode);
680 ret = hash_check_key(bch2_xattr_hash_desc, &h, c, iter, k);
686 return bch2_trans_exit(&trans) ?: ret;
689 /* Get root directory, create if it doesn't exist: */
690 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
692 struct bkey_inode_buf packed;
695 bch_verbose(c, "checking root directory");
697 ret = bch2_inode_find_by_inum(c, BCACHEFS_ROOT_INO, root_inode);
698 if (ret && ret != -ENOENT)
701 if (fsck_err_on(ret, c, "root directory missing"))
704 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
705 "root inode not a directory"))
712 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
714 root_inode->bi_inum = BCACHEFS_ROOT_INO;
716 bch2_inode_pack(&packed, root_inode);
718 return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
719 NULL, NULL, BTREE_INSERT_NOFAIL);
722 /* Get lost+found, create if it doesn't exist: */
723 static int check_lostfound(struct bch_fs *c,
724 struct bch_inode_unpacked *root_inode,
725 struct bch_inode_unpacked *lostfound_inode)
727 struct qstr lostfound = QSTR("lost+found");
728 struct bch_hash_info root_hash_info =
729 bch2_hash_info_init(c, root_inode);
730 struct bkey_inode_buf packed;
734 bch_verbose(c, "checking lost+found");
736 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
739 bch_notice(c, "creating lost+found");
740 goto create_lostfound;
743 ret = bch2_inode_find_by_inum(c, inum, lostfound_inode);
744 if (ret && ret != -ENOENT)
747 if (fsck_err_on(ret, c, "lost+found missing"))
748 goto create_lostfound;
750 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
751 "lost+found inode not a directory"))
752 goto create_lostfound;
758 root_inode->bi_nlink++;
760 bch2_inode_pack(&packed, root_inode);
762 ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
763 NULL, NULL, BTREE_INSERT_NOFAIL);
767 bch2_inode_init(c, lostfound_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
770 ret = bch2_inode_create(c, lostfound_inode, BLOCKDEV_INODE_MAX, 0,
771 &c->unused_inode_hint);
775 ret = bch2_dirent_create(c, BCACHEFS_ROOT_INO, &root_hash_info, DT_DIR,
776 &lostfound, lostfound_inode->bi_inum, NULL,
777 BTREE_INSERT_NOFAIL);
784 struct inode_bitmap {
789 static inline bool inode_bitmap_test(struct inode_bitmap *b, size_t nr)
791 return nr < b->size ? test_bit(nr, b->bits) : false;
794 static inline int inode_bitmap_set(struct inode_bitmap *b, size_t nr)
797 size_t new_size = max_t(size_t, max_t(size_t,
803 new_size = roundup_pow_of_two(new_size);
804 n = krealloc(b->bits, new_size / 8, GFP_KERNEL|__GFP_ZERO);
813 __set_bit(nr, b->bits);
821 struct pathbuf_entry {
827 static int path_down(struct pathbuf *p, u64 inum)
829 if (p->nr == p->size) {
830 size_t new_size = max_t(size_t, 256UL, p->size * 2);
831 void *n = krealloc(p->entries,
832 new_size * sizeof(p->entries[0]),
841 p->entries[p->nr++] = (struct pathbuf_entry) {
849 static int check_directory_structure(struct bch_fs *c,
850 struct bch_inode_unpacked *lostfound_inode)
852 struct inode_bitmap dirs_done = { NULL, 0 };
853 struct pathbuf path = { 0, 0, NULL };
854 struct pathbuf_entry *e;
855 struct btree_iter iter;
857 struct bkey_s_c_dirent dirent;
858 bool had_unreachable;
862 bch_verbose(c, "checking directory structure");
866 had_unreachable = false;
868 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
870 bch_err(c, "memory allocation failure in inode_bitmap_set()");
874 ret = path_down(&path, BCACHEFS_ROOT_INO);
881 e = &path.entries[path.nr - 1];
883 if (e->offset == U64_MAX)
886 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
887 POS(e->inum, e->offset + 1), 0, k) {
888 if (k.k->p.inode != e->inum)
891 e->offset = k.k->p.offset;
893 if (k.k->type != KEY_TYPE_dirent)
896 dirent = bkey_s_c_to_dirent(k);
898 if (dirent.v->d_type != DT_DIR)
901 d_inum = le64_to_cpu(dirent.v->d_inum);
903 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
904 "directory %llu has multiple hardlinks",
906 ret = remove_dirent(c, &iter, dirent);
912 ret = inode_bitmap_set(&dirs_done, d_inum);
914 bch_err(c, "memory allocation failure in inode_bitmap_set()");
918 ret = path_down(&path, d_inum);
923 bch2_btree_iter_unlock(&iter);
926 ret = bch2_btree_iter_unlock(&iter);
928 bch_err(c, "btree error %i in fsck", ret);
935 for_each_btree_key(&iter, c, BTREE_ID_INODES, POS_MIN, 0, k) {
936 if (k.k->type != KEY_TYPE_inode)
939 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
942 if (!bch2_empty_dir(c, k.k->p.inode))
945 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
946 "unreachable directory found (inum %llu)",
948 bch2_btree_iter_unlock(&iter);
950 ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
955 had_unreachable = true;
958 ret = bch2_btree_iter_unlock(&iter);
962 if (had_unreachable) {
963 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
964 kfree(dirs_done.bits);
966 memset(&dirs_done, 0, sizeof(dirs_done));
967 memset(&path, 0, sizeof(path));
972 kfree(dirs_done.bits);
977 ret = bch2_btree_iter_unlock(&iter) ?: ret;
986 typedef GENRADIX(struct nlink) nlink_table;
988 static void inc_link(struct bch_fs *c, nlink_table *links,
989 u64 range_start, u64 *range_end,
994 if (inum < range_start || inum >= *range_end)
997 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
999 bch_verbose(c, "allocation failed during fs gc - will need another pass");
1011 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1012 u64 range_start, u64 *range_end)
1014 struct btree_iter iter;
1016 struct bkey_s_c_dirent d;
1020 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
1022 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS, POS_MIN, 0, k) {
1023 switch (k.k->type) {
1024 case KEY_TYPE_dirent:
1025 d = bkey_s_c_to_dirent(k);
1026 d_inum = le64_to_cpu(d.v->d_inum);
1028 if (d.v->d_type == DT_DIR)
1029 inc_link(c, links, range_start, range_end,
1030 d.k->p.inode, true);
1032 inc_link(c, links, range_start, range_end,
1038 bch2_btree_iter_cond_resched(&iter);
1040 ret = bch2_btree_iter_unlock(&iter);
1042 bch_err(c, "error in fs gc: btree error %i while walking dirents", ret);
1047 s64 bch2_count_inode_sectors(struct bch_fs *c, u64 inum)
1049 struct btree_iter iter;
1053 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS(inum, 0), 0, k) {
1054 if (k.k->p.inode != inum)
1057 if (bkey_extent_is_allocation(k.k))
1058 sectors += k.k->size;
1061 return bch2_btree_iter_unlock(&iter) ?: sectors;
1064 static int check_inode_nlink(struct bch_fs *c,
1065 struct bch_inode_unpacked *lostfound_inode,
1066 struct bch_inode_unpacked *u,
1070 u32 i_nlink = u->bi_flags & BCH_INODE_UNLINKED
1072 : u->bi_nlink + nlink_bias(u->bi_mode);
1074 link->count * nlink_bias(u->bi_mode) +
1079 * These should have been caught/fixed by earlier passes, we don't
1082 if (S_ISDIR(u->bi_mode) && link->count > 1) {
1083 need_fsck_err(c, "directory %llu with multiple hardlinks: %u",
1084 u->bi_inum, link->count);
1088 if (S_ISDIR(u->bi_mode) && !link->count) {
1089 need_fsck_err(c, "unreachable directory found (inum %llu)",
1094 if (!S_ISDIR(u->bi_mode) && link->dir_count) {
1095 need_fsck_err(c, "non directory with subdirectories",
1101 !(u->bi_flags & BCH_INODE_UNLINKED) &&
1102 (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
1103 if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
1104 u->bi_inum, mode_to_type(u->bi_mode)) ==
1108 ret = reattach_inode(c, lostfound_inode, u->bi_inum);
1113 real_i_nlink = nlink_bias(u->bi_mode) + link->dir_count;
1117 if (i_nlink < link->count) {
1118 if (fsck_err(c, "inode %llu i_link too small (%u < %u, type %i)",
1119 u->bi_inum, i_nlink, link->count,
1120 mode_to_type(u->bi_mode)) == FSCK_ERR_IGNORE)
1125 if (i_nlink != real_i_nlink &&
1127 if (fsck_err(c, "filesystem marked clean, "
1128 "but inode %llu has wrong i_nlink "
1129 "(type %u i_nlink %u, should be %u)",
1130 u->bi_inum, mode_to_type(u->bi_mode),
1131 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1136 if (i_nlink != real_i_nlink &&
1137 (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
1138 if (fsck_err(c, "inode %llu has wrong i_nlink "
1139 "(type %u i_nlink %u, should be %u)",
1140 u->bi_inum, mode_to_type(u->bi_mode),
1141 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1146 if (real_i_nlink && i_nlink != real_i_nlink)
1147 bch_verbose(c, "setting inode %llu nlink from %u to %u",
1148 u->bi_inum, i_nlink, real_i_nlink);
1150 if (i_nlink != real_i_nlink) {
1152 u->bi_nlink = real_i_nlink - nlink_bias(u->bi_mode);
1153 u->bi_flags &= ~BCH_INODE_UNLINKED;
1156 u->bi_flags |= BCH_INODE_UNLINKED;
1165 static int check_inode(struct bch_fs *c,
1166 struct bch_inode_unpacked *lostfound_inode,
1167 struct btree_iter *iter,
1168 struct bkey_s_c_inode inode,
1171 struct bch_inode_unpacked u;
1172 bool do_update = false;
1175 ret = bch2_inode_unpack(inode, &u);
1176 if (bch2_fs_inconsistent_on(ret, c,
1177 "error unpacking inode %llu in fsck",
1182 ret = check_inode_nlink(c, lostfound_inode, &u, link,
1188 if (u.bi_flags & BCH_INODE_UNLINKED) {
1189 bch_verbose(c, "deleting inode %llu", u.bi_inum);
1191 ret = bch2_inode_rm(c, u.bi_inum);
1193 bch_err(c, "error in fs gc: error %i "
1194 "while deleting inode", ret);
1198 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY) {
1199 fsck_err_on(c->sb.clean, c,
1200 "filesystem marked clean, "
1201 "but inode %llu has i_size dirty",
1204 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1207 * XXX: need to truncate partial blocks too here - or ideally
1208 * just switch units to bytes and that issue goes away
1211 ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
1213 bch_err(c, "error in fs gc: error %i "
1214 "truncating inode", ret);
1219 * We truncated without our normal sector accounting hook, just
1220 * make sure we recalculate it:
1222 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1224 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1228 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY) {
1231 fsck_err_on(c->sb.clean, c,
1232 "filesystem marked clean, "
1233 "but inode %llu has i_sectors dirty",
1236 bch_verbose(c, "recounting sectors for inode %llu",
1239 sectors = bch2_count_inode_sectors(c, u.bi_inum);
1241 bch_err(c, "error in fs gc: error %i "
1242 "recounting inode sectors",
1247 u.bi_sectors = sectors;
1248 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1253 struct bkey_inode_buf p;
1255 bch2_inode_pack(&p, &u);
1257 ret = bch2_btree_insert_at(c, NULL, NULL,
1258 BTREE_INSERT_NOFAIL,
1259 BTREE_INSERT_ENTRY(iter, &p.inode.k_i));
1260 if (ret && ret != -EINTR)
1261 bch_err(c, "error in fs gc: error %i "
1262 "updating inode", ret);
1269 static int bch2_gc_walk_inodes(struct bch_fs *c,
1270 struct bch_inode_unpacked *lostfound_inode,
1272 u64 range_start, u64 range_end)
1274 struct btree_iter iter;
1276 struct nlink *link, zero_links = { 0, 0 };
1277 struct genradix_iter nlinks_iter;
1278 int ret = 0, ret2 = 0;
1281 bch2_btree_iter_init(&iter, c, BTREE_ID_INODES, POS(range_start, 0), 0);
1282 nlinks_iter = genradix_iter_init(links, 0);
1284 while ((k = bch2_btree_iter_peek(&iter)).k &&
1285 !btree_iter_err(k)) {
1286 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1288 if (!link && (!k.k || iter.pos.inode >= range_end))
1291 nlinks_pos = range_start + nlinks_iter.pos;
1292 if (iter.pos.inode > nlinks_pos) {
1293 /* Should have been caught by dirents pass: */
1294 need_fsck_err_on(link && link->count, c,
1295 "missing inode %llu (nlink %u)",
1296 nlinks_pos, link->count);
1297 genradix_iter_advance(&nlinks_iter, links);
1301 if (iter.pos.inode < nlinks_pos || !link)
1304 if (k.k && k.k->type == KEY_TYPE_inode) {
1306 * Avoid potential deadlocks with iter for
1309 bch2_btree_iter_unlock(&iter);
1311 ret = check_inode(c, lostfound_inode, &iter,
1312 bkey_s_c_to_inode(k), link);
1313 BUG_ON(ret == -EINTR);
1318 atomic_long_inc(&c->nr_inodes);
1320 /* Should have been caught by dirents pass: */
1321 need_fsck_err_on(link->count, c,
1322 "missing inode %llu (nlink %u)",
1323 nlinks_pos, link->count);
1326 if (nlinks_pos == iter.pos.inode)
1327 genradix_iter_advance(&nlinks_iter, links);
1329 bch2_btree_iter_next(&iter);
1330 bch2_btree_iter_cond_resched(&iter);
1333 ret2 = bch2_btree_iter_unlock(&iter);
1335 bch_err(c, "error in fs gc: btree error %i while walking inodes", ret2);
1341 static int check_inode_nlinks(struct bch_fs *c,
1342 struct bch_inode_unpacked *lostfound_inode)
1345 u64 this_iter_range_start, next_iter_range_start = 0;
1348 bch_verbose(c, "checking inode nlinks");
1350 genradix_init(&links);
1353 this_iter_range_start = next_iter_range_start;
1354 next_iter_range_start = U64_MAX;
1356 ret = bch2_gc_walk_dirents(c, &links,
1357 this_iter_range_start,
1358 &next_iter_range_start);
1362 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1363 this_iter_range_start,
1364 next_iter_range_start);
1368 genradix_free(&links);
1369 } while (next_iter_range_start != U64_MAX);
1371 genradix_free(&links);
1377 static int check_inodes_fast(struct bch_fs *c)
1379 struct btree_iter iter;
1381 struct bkey_s_c_inode inode;
1382 unsigned long nr_inodes = 0;
1385 for_each_btree_key(&iter, c, BTREE_ID_INODES, POS_MIN, 0, k) {
1386 if (k.k->type != KEY_TYPE_inode)
1389 inode = bkey_s_c_to_inode(k);
1391 if (!(inode.v->bi_flags & BCH_INODE_UNLINKED))
1394 if (inode.v->bi_flags &
1395 (BCH_INODE_I_SIZE_DIRTY|
1396 BCH_INODE_I_SECTORS_DIRTY|
1397 BCH_INODE_UNLINKED)) {
1398 fsck_err_on(c->sb.clean, c,
1399 "filesystem marked clean but found inode %llu with flags %x",
1400 inode.k->p.inode, inode.v->bi_flags);
1401 ret = check_inode(c, NULL, &iter, inode, NULL);
1402 BUG_ON(ret == -EINTR);
1407 atomic_long_set(&c->nr_inodes, nr_inodes);
1409 return bch2_btree_iter_unlock(&iter) ?: ret;
1413 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1414 * Doesn't fix them yet, mainly because they haven't yet been observed:
1416 static int bch2_fsck_full(struct bch_fs *c)
1418 struct bch_inode_unpacked root_inode, lostfound_inode;
1421 bch_verbose(c, "starting fsck:");
1422 ret = check_extents(c) ?:
1425 check_root(c, &root_inode) ?:
1426 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1427 check_directory_structure(c, &lostfound_inode) ?:
1428 check_inode_nlinks(c, &lostfound_inode);
1430 bch2_flush_fsck_errs(c);
1431 bch_verbose(c, "fsck done");
1436 static int bch2_fsck_inode_nlink(struct bch_fs *c)
1438 struct bch_inode_unpacked root_inode, lostfound_inode;
1441 bch_verbose(c, "checking inode link counts:");
1442 ret = check_root(c, &root_inode) ?:
1443 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1444 check_inode_nlinks(c, &lostfound_inode);
1446 bch2_flush_fsck_errs(c);
1447 bch_verbose(c, "done");
1452 static int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1456 bch_verbose(c, "walking inodes:");
1457 ret = check_inodes_fast(c);
1459 bch2_flush_fsck_errs(c);
1460 bch_verbose(c, "done");
1465 int bch2_fsck(struct bch_fs *c)
1468 return bch2_fsck_full(c);
1471 !(c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK)))
1472 return bch2_fsck_inode_nlink(c);
1474 return bch2_fsck_walk_inodes_only(c);