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_t(size_t, max_t(size_t,
619 new_size = roundup_pow_of_two(new_size);
620 n = krealloc(b->bits, new_size / 8, GFP_KERNEL|__GFP_ZERO);
629 __set_bit(nr, b->bits);
637 struct pathbuf_entry {
643 static int path_down(struct pathbuf *p, u64 inum)
645 if (p->nr == p->size) {
646 size_t new_size = max_t(size_t, 256UL, p->size * 2);
647 void *n = krealloc(p->entries,
648 new_size * sizeof(p->entries[0]),
657 p->entries[p->nr++] = (struct pathbuf_entry) {
665 static int check_directory_structure(struct bch_fs *c,
666 struct bch_inode_unpacked *lostfound_inode)
668 struct inode_bitmap dirs_done = { NULL, 0 };
669 struct pathbuf path = { 0, 0, NULL };
670 struct pathbuf_entry *e;
671 struct btree_iter iter;
673 struct bkey_s_c_dirent dirent;
674 bool had_unreachable;
680 had_unreachable = false;
682 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
684 bch_err(c, "memory allocation failure in inode_bitmap_set()");
688 ret = path_down(&path, BCACHEFS_ROOT_INO);
695 e = &path.entries[path.nr - 1];
697 if (e->offset == U64_MAX)
700 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
701 POS(e->inum, e->offset + 1), 0, k) {
702 if (k.k->p.inode != e->inum)
705 e->offset = k.k->p.offset;
707 if (k.k->type != BCH_DIRENT)
710 dirent = bkey_s_c_to_dirent(k);
712 if (dirent.v->d_type != DT_DIR)
715 d_inum = le64_to_cpu(dirent.v->d_inum);
717 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
718 "directory %llu has multiple hardlinks",
720 ret = remove_dirent(c, &iter, dirent);
726 ret = inode_bitmap_set(&dirs_done, d_inum);
728 bch_err(c, "memory allocation failure in inode_bitmap_set()");
732 ret = path_down(&path, d_inum);
737 bch2_btree_iter_unlock(&iter);
740 ret = bch2_btree_iter_unlock(&iter);
742 bch_err(c, "btree error %i in fsck", ret);
749 for_each_btree_key(&iter, c, BTREE_ID_INODES, POS_MIN, 0, k) {
750 if (k.k->type != BCH_INODE_FS)
753 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
756 if (!bch2_empty_dir(c, k.k->p.inode))
759 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
760 "unreachable directory found (inum %llu)",
762 bch2_btree_iter_unlock(&iter);
764 ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
769 had_unreachable = true;
772 ret = bch2_btree_iter_unlock(&iter);
776 if (had_unreachable) {
777 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
778 kfree(dirs_done.bits);
780 memset(&dirs_done, 0, sizeof(dirs_done));
781 memset(&path, 0, sizeof(path));
786 kfree(dirs_done.bits);
791 ret = bch2_btree_iter_unlock(&iter) ?: ret;
800 typedef GENRADIX(struct nlink) nlink_table;
802 static void inc_link(struct bch_fs *c, nlink_table *links,
803 u64 range_start, u64 *range_end,
808 if (inum < range_start || inum >= *range_end)
811 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
813 bch_verbose(c, "allocation failed during fs gc - will need another pass");
825 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
826 u64 range_start, u64 *range_end)
828 struct btree_iter iter;
830 struct bkey_s_c_dirent d;
834 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
836 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS, POS_MIN, 0, k) {
839 d = bkey_s_c_to_dirent(k);
840 d_inum = le64_to_cpu(d.v->d_inum);
842 if (d.v->d_type == DT_DIR)
843 inc_link(c, links, range_start, range_end,
846 inc_link(c, links, range_start, range_end,
852 bch2_btree_iter_cond_resched(&iter);
854 ret = bch2_btree_iter_unlock(&iter);
856 bch_err(c, "error in fs gc: btree error %i while walking dirents", ret);
861 s64 bch2_count_inode_sectors(struct bch_fs *c, u64 inum)
863 struct btree_iter iter;
867 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS(inum, 0), 0, k) {
868 if (k.k->p.inode != inum)
871 if (bkey_extent_is_allocation(k.k))
872 sectors += k.k->size;
875 return bch2_btree_iter_unlock(&iter) ?: sectors;
878 static int bch2_gc_do_inode(struct bch_fs *c,
879 struct bch_inode_unpacked *lostfound_inode,
880 struct btree_iter *iter,
881 struct bkey_s_c_inode inode, struct nlink link)
883 struct bch_inode_unpacked u;
885 u32 i_nlink, real_i_nlink;
886 bool do_update = false;
888 ret = bch2_inode_unpack(inode, &u);
889 if (bch2_fs_inconsistent_on(ret, c,
890 "error unpacking inode %llu in fsck",
894 i_nlink = u.bi_nlink + nlink_bias(u.bi_mode);
896 fsck_err_on(i_nlink < link.count, c,
897 "inode %llu i_link too small (%u < %u, type %i)",
898 inode.k->p.inode, i_nlink,
899 link.count, mode_to_type(u.bi_mode));
901 /* These should have been caught/fixed by earlier passes: */
902 if (S_ISDIR(u.bi_mode)) {
903 need_fsck_err_on(link.count > 1, c,
904 "directory %llu with multiple hardlinks: %u",
905 inode.k->p.inode, link.count);
907 real_i_nlink = link.count * 2 + link.dir_count;
909 need_fsck_err_on(link.dir_count, c,
910 "found dirents for non directory %llu",
913 real_i_nlink = link.count + link.dir_count;
917 fsck_err_on(c->sb.clean, c,
918 "filesystem marked clean, "
919 "but found orphaned inode %llu",
922 if (fsck_err_on(S_ISDIR(u.bi_mode) &&
923 bch2_empty_dir(c, inode.k->p.inode), c,
924 "non empty directory with link count 0, "
925 "inode nlink %u, dir links found %u",
926 i_nlink, link.dir_count)) {
927 ret = reattach_inode(c, lostfound_inode,
933 bch_verbose(c, "deleting inode %llu", inode.k->p.inode);
935 ret = bch2_inode_rm(c, inode.k->p.inode);
937 bch_err(c, "error in fs gc: error %i "
938 "while deleting inode", ret);
942 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY) {
943 fsck_err_on(c->sb.clean, c,
944 "filesystem marked clean, "
945 "but inode %llu has i_size dirty",
948 bch_verbose(c, "truncating inode %llu", inode.k->p.inode);
951 * XXX: need to truncate partial blocks too here - or ideally
952 * just switch units to bytes and that issue goes away
955 ret = bch2_inode_truncate(c, inode.k->p.inode,
956 round_up(u.bi_size, PAGE_SIZE) >> 9,
959 bch_err(c, "error in fs gc: error %i "
960 "truncating inode", ret);
965 * We truncated without our normal sector accounting hook, just
966 * make sure we recalculate it:
968 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
970 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
974 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY) {
977 fsck_err_on(c->sb.clean, c,
978 "filesystem marked clean, "
979 "but inode %llu has i_sectors dirty",
982 bch_verbose(c, "recounting sectors for inode %llu",
985 sectors = bch2_count_inode_sectors(c, inode.k->p.inode);
987 bch_err(c, "error in fs gc: error %i "
988 "recounting inode sectors",
993 u.bi_sectors = sectors;
994 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
998 if (i_nlink != real_i_nlink) {
999 fsck_err_on(c->sb.clean, c,
1000 "filesystem marked clean, "
1001 "but inode %llu has wrong i_nlink "
1002 "(type %u i_nlink %u, should be %u)",
1003 inode.k->p.inode, mode_to_type(u.bi_mode),
1004 i_nlink, real_i_nlink);
1006 bch_verbose(c, "setting inode %llu nlinks from %u to %u",
1007 inode.k->p.inode, i_nlink, real_i_nlink);
1008 u.bi_nlink = real_i_nlink - nlink_bias(u.bi_mode);
1013 struct bkey_inode_buf p;
1015 bch2_inode_pack(&p, &u);
1017 ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
1018 BTREE_INSERT_NOFAIL,
1019 BTREE_INSERT_ENTRY(iter, &p.inode.k_i));
1020 if (ret && ret != -EINTR)
1021 bch_err(c, "error in fs gc: error %i "
1022 "updating inode", ret);
1029 static int bch2_gc_walk_inodes(struct bch_fs *c,
1030 struct bch_inode_unpacked *lostfound_inode,
1032 u64 range_start, u64 range_end)
1034 struct btree_iter iter;
1036 struct nlink *link, zero_links = { 0, 0 };
1037 struct genradix_iter nlinks_iter;
1038 int ret = 0, ret2 = 0;
1041 bch2_btree_iter_init(&iter, c, BTREE_ID_INODES, POS(range_start, 0), 0);
1042 nlinks_iter = genradix_iter_init(links, 0);
1044 while ((k = bch2_btree_iter_peek(&iter)).k &&
1045 !btree_iter_err(k)) {
1046 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1048 if (!link && (!k.k || iter.pos.inode >= range_end))
1051 nlinks_pos = range_start + nlinks_iter.pos;
1052 if (iter.pos.inode > nlinks_pos) {
1053 /* Should have been caught by dirents pass: */
1054 need_fsck_err_on(link && link->count, c,
1055 "missing inode %llu (nlink %u)",
1056 nlinks_pos, link->count);
1057 genradix_iter_advance(&nlinks_iter, links);
1061 if (iter.pos.inode < nlinks_pos || !link)
1064 if (k.k && k.k->type == BCH_INODE_FS) {
1066 * Avoid potential deadlocks with iter for
1069 bch2_btree_iter_unlock(&iter);
1071 ret = bch2_gc_do_inode(c, lostfound_inode, &iter,
1072 bkey_s_c_to_inode(k), *link);
1079 atomic_long_inc(&c->nr_inodes);
1081 /* Should have been caught by dirents pass: */
1082 need_fsck_err_on(link->count, c,
1083 "missing inode %llu (nlink %u)",
1084 nlinks_pos, link->count);
1087 if (nlinks_pos == iter.pos.inode)
1088 genradix_iter_advance(&nlinks_iter, links);
1090 bch2_btree_iter_next(&iter);
1091 bch2_btree_iter_cond_resched(&iter);
1094 ret2 = bch2_btree_iter_unlock(&iter);
1096 bch_err(c, "error in fs gc: btree error %i while walking inodes", ret2);
1102 static int check_inode_nlinks(struct bch_fs *c,
1103 struct bch_inode_unpacked *lostfound_inode)
1106 u64 this_iter_range_start, next_iter_range_start = 0;
1109 genradix_init(&links);
1112 this_iter_range_start = next_iter_range_start;
1113 next_iter_range_start = U64_MAX;
1115 ret = bch2_gc_walk_dirents(c, &links,
1116 this_iter_range_start,
1117 &next_iter_range_start);
1121 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1122 this_iter_range_start,
1123 next_iter_range_start);
1127 genradix_free(&links);
1128 } while (next_iter_range_start != U64_MAX);
1130 genradix_free(&links);
1136 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1137 * Doesn't fix them yet, mainly because they haven't yet been observed:
1139 int bch2_fsck(struct bch_fs *c, bool full_fsck)
1141 struct bch_inode_unpacked root_inode, lostfound_inode;
1145 bch_verbose(c, "checking extents");
1146 ret = check_extents(c);
1150 bch_verbose(c, "checking dirents");
1151 ret = check_dirents(c);
1155 bch_verbose(c, "checking xattrs");
1156 ret = check_xattrs(c);
1160 bch_verbose(c, "checking root directory");
1161 ret = check_root(c, &root_inode);
1165 bch_verbose(c, "checking lost+found");
1166 ret = check_lostfound(c, &root_inode, &lostfound_inode);
1170 bch_verbose(c, "checking directory structure");
1171 ret = check_directory_structure(c, &lostfound_inode);
1175 bch_verbose(c, "checking inode nlinks");
1176 ret = check_inode_nlinks(c, &lostfound_inode);
1180 bch_verbose(c, "checking root directory");
1181 ret = check_root(c, &root_inode);
1185 bch_verbose(c, "checking lost+found");
1186 ret = check_lostfound(c, &root_inode, &lostfound_inode);
1190 bch_verbose(c, "checking inode nlinks");
1191 ret = check_inode_nlinks(c, &lostfound_inode);
1196 bch2_flush_fsck_errs(c);