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,
78 bch_err(c, "error %i reattaching inode %llu while updating lost+found",
83 ret = bch2_dirent_create(c, lostfound_inode->bi_inum,
85 DT_DIR, &name, inum, NULL,
88 bch_err(c, "error %i reattaching inode %llu while creating new dirent",
96 bool first_this_inode;
99 struct bch_inode_unpacked inode;
102 static struct inode_walker inode_walker_init(void)
104 return (struct inode_walker) {
110 static int walk_inode(struct bch_fs *c, struct inode_walker *w, u64 inum)
112 w->first_this_inode = inum != w->cur_inum;
115 if (w->first_this_inode) {
116 int ret = bch2_inode_find_by_inum(c, inum, &w->inode);
118 if (ret && ret != -ENOENT)
121 w->have_inode = !ret;
128 struct bch_hash_info info;
129 struct btree_iter chain;
130 struct btree_iter iter;
134 static void hash_check_init(const struct bch_hash_desc desc,
135 struct hash_check *h, struct bch_fs *c)
137 bch2_btree_iter_init(&h->chain, c, desc.btree_id, POS_MIN, 0);
138 bch2_btree_iter_init(&h->iter, c, desc.btree_id, POS_MIN, 0);
141 static void hash_check_set_inode(struct hash_check *h, struct bch_fs *c,
142 const struct bch_inode_unpacked *bi)
144 h->info = bch2_hash_info_init(c, bi);
148 static int hash_redo_key(const struct bch_hash_desc desc,
149 struct hash_check *h, struct bch_fs *c,
150 struct btree_iter *k_iter, struct bkey_s_c k,
156 tmp = kmalloc(bkey_bytes(k.k), GFP_KERNEL);
160 bkey_reassemble(tmp, k);
162 ret = bch2_btree_delete_at(k_iter, 0);
166 bch2_btree_iter_unlock(k_iter);
168 bch2_hash_set(desc, &h->info, c, k_iter->pos.inode, NULL, tmp,
170 BCH_HASH_SET_MUST_CREATE);
176 static int hash_check_key(const struct bch_hash_desc desc,
177 struct hash_check *h, struct bch_fs *c,
178 struct btree_iter *k_iter, struct bkey_s_c k)
184 if (k.k->type != desc.whiteout_type &&
185 k.k->type != desc.key_type)
188 if (k.k->p.offset != h->next) {
189 if (!btree_iter_linked(&h->chain)) {
190 bch2_btree_iter_link(k_iter, &h->chain);
191 bch2_btree_iter_link(k_iter, &h->iter);
193 bch2_btree_iter_copy(&h->chain, k_iter);
195 h->next = k.k->p.offset + 1;
197 if (k.k->type != desc.key_type)
200 hashed = desc.hash_bkey(&h->info, k);
202 if (fsck_err_on(hashed < h->chain.pos.offset ||
203 hashed > k.k->p.offset, c,
204 "hash table key at wrong offset: %llu, "
205 "hashed to %llu chain starts at %llu\n%s",
206 k.k->p.offset, hashed, h->chain.pos.offset,
207 (bch2_bkey_val_to_text(c, bkey_type(0, desc.btree_id),
208 buf, sizeof(buf), k), buf))) {
209 ret = hash_redo_key(desc, h, c, k_iter, k, hashed);
211 bch_err(c, "hash_redo_key err %i", ret);
217 if (!bkey_cmp(h->chain.pos, k_iter->pos))
220 bch2_btree_iter_copy(&h->iter, &h->chain);
221 while (bkey_cmp(h->iter.pos, k_iter->pos) < 0) {
222 struct bkey_s_c k2 = bch2_btree_iter_peek(&h->iter);
224 if (fsck_err_on(k2.k->type == desc.key_type &&
225 !desc.cmp_bkey(k, k2), c,
226 "duplicate hash table keys:\n%s",
227 (bch2_bkey_val_to_text(c, bkey_type(0, desc.btree_id),
228 buf, sizeof(buf), k), buf))) {
229 ret = bch2_hash_delete_at(desc, &h->info, &h->iter, NULL);
234 bch2_btree_iter_next(&h->iter);
241 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
242 * that i_size an i_sectors are consistent
245 static int check_extents(struct bch_fs *c)
247 struct inode_walker w = inode_walker_init();
248 struct btree_iter iter;
253 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS,
254 POS(BCACHEFS_ROOT_INO, 0), 0, k) {
255 if (k.k->type == KEY_TYPE_DISCARD)
258 ret = walk_inode(c, &w, k.k->p.inode);
262 if (fsck_err_on(!w.have_inode, c,
263 "extent type %u for missing inode %llu",
264 k.k->type, k.k->p.inode) ||
265 fsck_err_on(w.have_inode &&
266 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
267 "extent type %u for non regular file, inode %llu mode %o",
268 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
269 bch2_btree_iter_unlock(&iter);
271 ret = bch2_inode_truncate(c, k.k->p.inode, 0, NULL, NULL);
277 if (fsck_err_on(w.first_this_inode &&
279 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
280 w.inode.bi_sectors !=
281 (i_sectors = bch2_count_inode_sectors(c, w.cur_inum)),
282 c, "i_sectors wrong: got %llu, should be %llu",
283 w.inode.bi_sectors, i_sectors)) {
284 struct bkey_inode_buf p;
286 w.inode.bi_sectors = i_sectors;
288 bch2_btree_iter_unlock(&iter);
290 bch2_inode_pack(&p, &w.inode);
292 ret = bch2_btree_insert(c, BTREE_ID_INODES,
297 BTREE_INSERT_NOFAIL);
299 bch_err(c, "error in fs gc: error %i "
300 "updating inode", ret);
304 /* revalidate iterator: */
305 k = bch2_btree_iter_peek(&iter);
308 if (fsck_err_on(w.have_inode &&
309 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
310 k.k->type != BCH_RESERVATION &&
311 k.k->p.offset > round_up(w.inode.bi_size, PAGE_SIZE) >> 9, c,
312 "extent type %u offset %llu past end of inode %llu, i_size %llu",
313 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
314 bch2_btree_iter_unlock(&iter);
316 ret = bch2_inode_truncate(c, k.k->p.inode,
317 round_up(w.inode.bi_size, PAGE_SIZE) >> 9,
326 return bch2_btree_iter_unlock(&iter) ?: ret;
330 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
334 static int check_dirents(struct bch_fs *c)
336 struct inode_walker w = inode_walker_init();
338 struct btree_iter iter;
344 hash_check_init(bch2_dirent_hash_desc, &h, c);
346 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
347 POS(BCACHEFS_ROOT_INO, 0), 0, k) {
348 struct bkey_s_c_dirent d;
349 struct bch_inode_unpacked target;
353 ret = walk_inode(c, &w, k.k->p.inode);
357 if (fsck_err_on(!w.have_inode, c,
358 "dirent in nonexisting directory:\n%s",
359 (bch2_bkey_val_to_text(c, BTREE_ID_DIRENTS,
360 buf, sizeof(buf), k), buf)) ||
361 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
362 "dirent in non directory inode type %u:\n%s",
363 mode_to_type(w.inode.bi_mode),
364 (bch2_bkey_val_to_text(c, BTREE_ID_DIRENTS,
365 buf, sizeof(buf), k), buf))) {
366 ret = bch2_btree_delete_at(&iter, 0);
372 if (w.first_this_inode && w.have_inode)
373 hash_check_set_inode(&h, c, &w.inode);
375 ret = hash_check_key(bch2_dirent_hash_desc, &h, c, &iter, k);
384 if (k.k->type != BCH_DIRENT)
387 d = bkey_s_c_to_dirent(k);
388 d_inum = le64_to_cpu(d.v->d_inum);
390 name_len = bch2_dirent_name_bytes(d);
392 if (fsck_err_on(!name_len, c, "empty dirent") ||
393 fsck_err_on(name_len == 1 &&
394 !memcmp(d.v->d_name, ".", 1), c,
396 fsck_err_on(name_len == 2 &&
397 !memcmp(d.v->d_name, "..", 2), c,
399 ret = remove_dirent(c, &iter, d);
405 if (fsck_err_on(d_inum == d.k->p.inode, c,
406 "dirent points to own directory:\n%s",
407 (bch2_bkey_val_to_text(c, BTREE_ID_DIRENTS,
408 buf, sizeof(buf), k), buf))) {
409 ret = remove_dirent(c, &iter, d);
415 ret = bch2_inode_find_by_inum(c, d_inum, &target);
416 if (ret && ret != -ENOENT)
422 if (fsck_err_on(!have_target, c,
423 "dirent points to missing inode:\n%s",
424 (bch2_bkey_val_to_text(c, BTREE_ID_DIRENTS,
425 buf, sizeof(buf), k), buf))) {
426 ret = remove_dirent(c, &iter, d);
432 if (fsck_err_on(have_target &&
434 mode_to_type(target.bi_mode), c,
435 "incorrect d_type: should be %u:\n%s",
436 mode_to_type(target.bi_mode),
437 (bch2_bkey_val_to_text(c, BTREE_ID_DIRENTS,
438 buf, sizeof(buf), k), buf))) {
439 struct bkey_i_dirent *n;
441 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
447 bkey_reassemble(&n->k_i, d.s_c);
448 n->v.d_type = mode_to_type(target.bi_mode);
450 ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
452 BTREE_INSERT_ENTRY(&iter, &n->k_i));
461 bch2_btree_iter_unlock(&h.chain);
462 bch2_btree_iter_unlock(&h.iter);
463 return bch2_btree_iter_unlock(&iter) ?: ret;
467 * Walk xattrs: verify that they all have a corresponding inode
470 static int check_xattrs(struct bch_fs *c)
472 struct inode_walker w = inode_walker_init();
474 struct btree_iter iter;
478 hash_check_init(bch2_xattr_hash_desc, &h, c);
480 for_each_btree_key(&iter, c, BTREE_ID_XATTRS,
481 POS(BCACHEFS_ROOT_INO, 0), 0, k) {
482 ret = walk_inode(c, &w, k.k->p.inode);
486 if (fsck_err_on(!w.have_inode, c,
487 "xattr for missing inode %llu",
489 ret = bch2_btree_delete_at(&iter, 0);
495 if (w.first_this_inode && w.have_inode)
496 hash_check_set_inode(&h, c, &w.inode);
498 ret = hash_check_key(bch2_xattr_hash_desc, &h, c, &iter, k);
504 bch2_btree_iter_unlock(&h.chain);
505 bch2_btree_iter_unlock(&h.iter);
506 return bch2_btree_iter_unlock(&iter) ?: ret;
509 /* Get root directory, create if it doesn't exist: */
510 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
512 struct bkey_inode_buf packed;
515 ret = bch2_inode_find_by_inum(c, BCACHEFS_ROOT_INO, root_inode);
516 if (ret && ret != -ENOENT)
519 if (fsck_err_on(ret, c, "root directory missing"))
522 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
523 "root inode not a directory"))
530 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
532 root_inode->bi_inum = BCACHEFS_ROOT_INO;
534 bch2_inode_pack(&packed, root_inode);
536 return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
537 NULL, NULL, NULL, BTREE_INSERT_NOFAIL);
540 /* Get lost+found, create if it doesn't exist: */
541 static int check_lostfound(struct bch_fs *c,
542 struct bch_inode_unpacked *root_inode,
543 struct bch_inode_unpacked *lostfound_inode)
545 struct qstr lostfound = QSTR("lost+found");
546 struct bch_hash_info root_hash_info =
547 bch2_hash_info_init(c, root_inode);
548 struct bkey_inode_buf packed;
552 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
555 bch_notice(c, "creating lost+found");
556 goto create_lostfound;
559 ret = bch2_inode_find_by_inum(c, inum, lostfound_inode);
560 if (ret && ret != -ENOENT)
563 if (fsck_err_on(ret, c, "lost+found missing"))
564 goto create_lostfound;
566 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
567 "lost+found inode not a directory"))
568 goto create_lostfound;
574 root_inode->bi_nlink++;
576 bch2_inode_pack(&packed, root_inode);
578 ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
579 NULL, NULL, NULL, BTREE_INSERT_NOFAIL);
583 bch2_inode_init(c, lostfound_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
586 ret = bch2_inode_create(c, lostfound_inode, BLOCKDEV_INODE_MAX, 0,
587 &c->unused_inode_hint);
591 ret = bch2_dirent_create(c, BCACHEFS_ROOT_INO, &root_hash_info, DT_DIR,
592 &lostfound, lostfound_inode->bi_inum, NULL,
593 BTREE_INSERT_NOFAIL);
600 struct inode_bitmap {
605 static inline bool inode_bitmap_test(struct inode_bitmap *b, size_t nr)
607 return nr < b->size ? test_bit(nr, b->bits) : false;
610 static inline int inode_bitmap_set(struct inode_bitmap *b, size_t nr)
613 size_t new_size = max(max(PAGE_SIZE * 8,
618 new_size = roundup_pow_of_two(new_size);
619 n = krealloc(b->bits, new_size / 8, GFP_KERNEL|__GFP_ZERO);
628 __set_bit(nr, b->bits);
636 struct pathbuf_entry {
642 static int path_down(struct pathbuf *p, u64 inum)
644 if (p->nr == p->size) {
645 size_t new_size = max(256UL, p->size * 2);
646 void *n = krealloc(p->entries,
647 new_size * sizeof(p->entries[0]),
656 p->entries[p->nr++] = (struct pathbuf_entry) {
664 static int check_directory_structure(struct bch_fs *c,
665 struct bch_inode_unpacked *lostfound_inode)
667 struct inode_bitmap dirs_done = { NULL, 0 };
668 struct pathbuf path = { 0, 0, NULL };
669 struct pathbuf_entry *e;
670 struct btree_iter iter;
672 struct bkey_s_c_dirent dirent;
673 bool had_unreachable;
679 had_unreachable = false;
681 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
683 bch_err(c, "memory allocation failure in inode_bitmap_set()");
687 ret = path_down(&path, BCACHEFS_ROOT_INO);
694 e = &path.entries[path.nr - 1];
696 if (e->offset == U64_MAX)
699 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
700 POS(e->inum, e->offset + 1), 0, k) {
701 if (k.k->p.inode != e->inum)
704 e->offset = k.k->p.offset;
706 if (k.k->type != BCH_DIRENT)
709 dirent = bkey_s_c_to_dirent(k);
711 if (dirent.v->d_type != DT_DIR)
714 d_inum = le64_to_cpu(dirent.v->d_inum);
716 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
717 "directory %llu has multiple hardlinks",
719 ret = remove_dirent(c, &iter, dirent);
725 ret = inode_bitmap_set(&dirs_done, d_inum);
727 bch_err(c, "memory allocation failure in inode_bitmap_set()");
731 ret = path_down(&path, d_inum);
736 bch2_btree_iter_unlock(&iter);
739 ret = bch2_btree_iter_unlock(&iter);
741 bch_err(c, "btree error %i in fsck", ret);
748 for_each_btree_key(&iter, c, BTREE_ID_INODES, POS_MIN, 0, k) {
749 if (k.k->type != BCH_INODE_FS ||
750 !S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
753 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
754 "unreachable directory found (inum %llu)",
756 bch2_btree_iter_unlock(&iter);
758 ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
763 had_unreachable = true;
766 ret = bch2_btree_iter_unlock(&iter);
770 if (had_unreachable) {
771 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
772 kfree(dirs_done.bits);
774 memset(&dirs_done, 0, sizeof(dirs_done));
775 memset(&path, 0, sizeof(path));
780 kfree(dirs_done.bits);
785 ret = bch2_btree_iter_unlock(&iter) ?: ret;
794 typedef GENRADIX(struct nlink) nlink_table;
796 static void inc_link(struct bch_fs *c, nlink_table *links,
797 u64 range_start, u64 *range_end,
802 if (inum < range_start || inum >= *range_end)
805 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
807 bch_verbose(c, "allocation failed during fs gc - will need another pass");
819 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
820 u64 range_start, u64 *range_end)
822 struct btree_iter iter;
824 struct bkey_s_c_dirent d;
828 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
830 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS, POS_MIN, 0, k) {
833 d = bkey_s_c_to_dirent(k);
834 d_inum = le64_to_cpu(d.v->d_inum);
836 if (d.v->d_type == DT_DIR)
837 inc_link(c, links, range_start, range_end,
840 inc_link(c, links, range_start, range_end,
846 bch2_btree_iter_cond_resched(&iter);
848 ret = bch2_btree_iter_unlock(&iter);
850 bch_err(c, "error in fs gc: btree error %i while walking dirents", ret);
855 s64 bch2_count_inode_sectors(struct bch_fs *c, u64 inum)
857 struct btree_iter iter;
861 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS(inum, 0), 0, k) {
862 if (k.k->p.inode != inum)
865 if (bkey_extent_is_allocation(k.k))
866 sectors += k.k->size;
869 return bch2_btree_iter_unlock(&iter) ?: sectors;
872 static int bch2_gc_do_inode(struct bch_fs *c,
873 struct bch_inode_unpacked *lostfound_inode,
874 struct btree_iter *iter,
875 struct bkey_s_c_inode inode, struct nlink link)
877 struct bch_inode_unpacked u;
879 u32 i_nlink, real_i_nlink;
880 bool do_update = false;
882 ret = bch2_inode_unpack(inode, &u);
883 if (bch2_fs_inconsistent_on(ret, c,
884 "error unpacking inode %llu in fsck",
888 i_nlink = u.bi_nlink + nlink_bias(u.bi_mode);
890 fsck_err_on(i_nlink < link.count, c,
891 "inode %llu i_link too small (%u < %u, type %i)",
892 inode.k->p.inode, i_nlink,
893 link.count, mode_to_type(u.bi_mode));
895 /* These should have been caught/fixed by earlier passes: */
896 if (S_ISDIR(u.bi_mode)) {
897 need_fsck_err_on(link.count > 1, c,
898 "directory %llu with multiple hardlinks: %u",
899 inode.k->p.inode, link.count);
901 real_i_nlink = link.count * 2 + link.dir_count;
903 need_fsck_err_on(link.dir_count, c,
904 "found dirents for non directory %llu",
907 real_i_nlink = link.count + link.dir_count;
911 fsck_err_on(c->sb.clean, c,
912 "filesystem marked clean, "
913 "but found orphaned inode %llu",
916 if (fsck_err_on(S_ISDIR(u.bi_mode) &&
917 bch2_empty_dir(c, inode.k->p.inode), c,
918 "non empty directory with link count 0, "
919 "inode nlink %u, dir links found %u",
920 i_nlink, link.dir_count)) {
921 ret = reattach_inode(c, lostfound_inode,
927 bch_verbose(c, "deleting inode %llu", inode.k->p.inode);
929 ret = bch2_inode_rm(c, inode.k->p.inode);
931 bch_err(c, "error in fs gc: error %i "
932 "while deleting inode", ret);
936 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY) {
937 fsck_err_on(c->sb.clean, c,
938 "filesystem marked clean, "
939 "but inode %llu has i_size dirty",
942 bch_verbose(c, "truncating inode %llu", inode.k->p.inode);
945 * XXX: need to truncate partial blocks too here - or ideally
946 * just switch units to bytes and that issue goes away
949 ret = bch2_inode_truncate(c, inode.k->p.inode,
950 round_up(u.bi_size, PAGE_SIZE) >> 9,
953 bch_err(c, "error in fs gc: error %i "
954 "truncating inode", ret);
959 * We truncated without our normal sector accounting hook, just
960 * make sure we recalculate it:
962 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
964 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
968 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY) {
971 fsck_err_on(c->sb.clean, c,
972 "filesystem marked clean, "
973 "but inode %llu has i_sectors dirty",
976 bch_verbose(c, "recounting sectors for inode %llu",
979 sectors = bch2_count_inode_sectors(c, inode.k->p.inode);
981 bch_err(c, "error in fs gc: error %i "
982 "recounting inode sectors",
987 u.bi_sectors = sectors;
988 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
992 if (i_nlink != real_i_nlink) {
993 fsck_err_on(c->sb.clean, c,
994 "filesystem marked clean, "
995 "but inode %llu has wrong i_nlink "
996 "(type %u i_nlink %u, should be %u)",
997 inode.k->p.inode, mode_to_type(u.bi_mode),
998 i_nlink, real_i_nlink);
1000 bch_verbose(c, "setting inode %llu nlinks from %u to %u",
1001 inode.k->p.inode, i_nlink, real_i_nlink);
1002 u.bi_nlink = real_i_nlink - nlink_bias(u.bi_mode);
1007 struct bkey_inode_buf p;
1009 bch2_inode_pack(&p, &u);
1011 ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
1012 BTREE_INSERT_NOFAIL,
1013 BTREE_INSERT_ENTRY(iter, &p.inode.k_i));
1014 if (ret && ret != -EINTR)
1015 bch_err(c, "error in fs gc: error %i "
1016 "updating inode", ret);
1023 static int bch2_gc_walk_inodes(struct bch_fs *c,
1024 struct bch_inode_unpacked *lostfound_inode,
1026 u64 range_start, u64 range_end)
1028 struct btree_iter iter;
1030 struct nlink *link, zero_links = { 0, 0 };
1031 struct genradix_iter nlinks_iter;
1032 int ret = 0, ret2 = 0;
1035 bch2_btree_iter_init(&iter, c, BTREE_ID_INODES, POS(range_start, 0), 0);
1036 nlinks_iter = genradix_iter_init(links, 0);
1038 while ((k = bch2_btree_iter_peek(&iter)).k &&
1039 !btree_iter_err(k)) {
1040 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1042 if (!link && (!k.k || iter.pos.inode >= range_end))
1045 nlinks_pos = range_start + nlinks_iter.pos;
1046 if (iter.pos.inode > nlinks_pos) {
1047 /* Should have been caught by dirents pass: */
1048 need_fsck_err_on(link && link->count, c,
1049 "missing inode %llu (nlink %u)",
1050 nlinks_pos, link->count);
1051 genradix_iter_advance(&nlinks_iter, links);
1055 if (iter.pos.inode < nlinks_pos || !link)
1058 if (k.k && k.k->type == BCH_INODE_FS) {
1060 * Avoid potential deadlocks with iter for
1063 bch2_btree_iter_unlock(&iter);
1065 ret = bch2_gc_do_inode(c, lostfound_inode, &iter,
1066 bkey_s_c_to_inode(k), *link);
1073 atomic_long_inc(&c->nr_inodes);
1075 /* Should have been caught by dirents pass: */
1076 need_fsck_err_on(link->count, c,
1077 "missing inode %llu (nlink %u)",
1078 nlinks_pos, link->count);
1081 if (nlinks_pos == iter.pos.inode)
1082 genradix_iter_advance(&nlinks_iter, links);
1084 bch2_btree_iter_next(&iter);
1085 bch2_btree_iter_cond_resched(&iter);
1088 ret2 = bch2_btree_iter_unlock(&iter);
1090 bch_err(c, "error in fs gc: btree error %i while walking inodes", ret2);
1096 static int check_inode_nlinks(struct bch_fs *c,
1097 struct bch_inode_unpacked *lostfound_inode)
1100 u64 this_iter_range_start, next_iter_range_start = 0;
1103 genradix_init(&links);
1106 this_iter_range_start = next_iter_range_start;
1107 next_iter_range_start = U64_MAX;
1109 ret = bch2_gc_walk_dirents(c, &links,
1110 this_iter_range_start,
1111 &next_iter_range_start);
1115 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1116 this_iter_range_start,
1117 next_iter_range_start);
1121 genradix_free(&links);
1122 } while (next_iter_range_start != U64_MAX);
1124 genradix_free(&links);
1130 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1131 * Doesn't fix them yet, mainly because they haven't yet been observed:
1133 int bch2_fsck(struct bch_fs *c, bool full_fsck)
1135 struct bch_inode_unpacked root_inode, lostfound_inode;
1139 bch_verbose(c, "checking extents");
1140 ret = check_extents(c);
1144 bch_verbose(c, "checking dirents");
1145 ret = check_dirents(c);
1149 bch_verbose(c, "checking xattrs");
1150 ret = check_xattrs(c);
1154 bch_verbose(c, "checking root directory");
1155 ret = check_root(c, &root_inode);
1159 bch_verbose(c, "checking lost+found");
1160 ret = check_lostfound(c, &root_inode, &lostfound_inode);
1164 bch_verbose(c, "checking directory structure");
1165 ret = check_directory_structure(c, &lostfound_inode);
1169 bch_verbose(c, "checking inode nlinks");
1170 ret = check_inode_nlinks(c, &lostfound_inode);
1174 bch_verbose(c, "checking root directory");
1175 ret = check_root(c, &root_inode);
1179 bch_verbose(c, "checking lost+found");
1180 ret = check_lostfound(c, &root_inode, &lostfound_inode);
1184 bch_verbose(c, "checking inode nlinks");
1185 ret = check_inode_nlinks(c, &lostfound_inode);
1190 bch2_flush_fsck_errs(c);