1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
12 #include "subvolume.h"
16 #include <linux/bsearch.h>
17 #include <linux/dcache.h> /* struct qstr */
19 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
21 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
24 struct btree_iter iter;
29 for_each_btree_key(trans, iter, BTREE_ID_extents,
30 SPOS(inum, 0, snapshot), 0, k, ret) {
31 if (k.k->p.inode != inum)
34 if (bkey_extent_is_allocation(k.k))
38 bch2_trans_iter_exit(trans, &iter);
40 return ret ?: sectors;
43 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
46 struct btree_iter iter;
48 struct bkey_s_c_dirent d;
52 for_each_btree_key(trans, iter, BTREE_ID_dirents,
53 SPOS(inum, 0, snapshot), 0, k, ret) {
54 if (k.k->p.inode != inum)
57 if (k.k->type != KEY_TYPE_dirent)
60 d = bkey_s_c_to_dirent(k);
61 if (d.v->d_type == DT_DIR)
65 bch2_trans_iter_exit(trans, &iter);
67 return ret ?: subdirs;
70 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
73 struct btree_iter iter;
77 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
79 k = bch2_btree_iter_peek_slot(&iter);
84 if (k.k->type != KEY_TYPE_snapshot) {
85 bch_err(trans->c, "snapshot %u not fonud", snapshot);
90 *subvol = le32_to_cpu(bkey_s_c_to_snapshot(k).v->subvol);
92 bch2_trans_iter_exit(trans, &iter);
97 static int snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
100 return lockrestart_do(trans, __snapshot_lookup_subvol(trans, snapshot, subvol));
103 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
104 u32 *snapshot, u64 *inum)
106 struct bch_subvolume s;
109 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
111 *snapshot = le32_to_cpu(s.snapshot);
112 *inum = le64_to_cpu(s.inode);
116 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
117 u32 *snapshot, u64 *inum)
119 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
122 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
123 struct bch_inode_unpacked *inode,
126 struct btree_iter iter;
130 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
131 SPOS(0, inode_nr, *snapshot), 0);
132 k = bch2_btree_iter_peek_slot(&iter);
137 *snapshot = iter.pos.snapshot;
138 ret = k.k->type == KEY_TYPE_inode
139 ? bch2_inode_unpack(bkey_s_c_to_inode(k), inode)
142 bch2_trans_iter_exit(trans, &iter);
146 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
147 struct bch_inode_unpacked *inode,
150 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
153 static int __lookup_dirent(struct btree_trans *trans,
154 struct bch_hash_info hash_info,
155 subvol_inum dir, struct qstr *name,
156 u64 *target, unsigned *type)
158 struct btree_iter iter;
159 struct bkey_s_c_dirent d;
162 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
163 &hash_info, dir, name, 0);
167 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
168 *target = le64_to_cpu(d.v->d_inum);
170 bch2_trans_iter_exit(trans, &iter);
174 static int lookup_dirent(struct btree_trans *trans,
175 struct bch_hash_info hash_info,
176 subvol_inum dir, struct qstr *name,
177 u64 *target, unsigned *type)
179 return lockrestart_do(trans,
180 __lookup_dirent(trans, hash_info, dir, name, target, type));
183 static int __write_inode(struct btree_trans *trans,
184 struct bch_inode_unpacked *inode,
187 struct btree_iter iter;
190 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
191 SPOS(0, inode->bi_inum, snapshot),
194 ret = bch2_btree_iter_traverse(&iter) ?:
195 bch2_inode_write(trans, &iter, inode);
196 bch2_trans_iter_exit(trans, &iter);
200 static int write_inode(struct btree_trans *trans,
201 struct bch_inode_unpacked *inode,
204 int ret = __bch2_trans_do(trans, NULL, NULL,
206 BTREE_INSERT_LAZY_RW,
207 __write_inode(trans, inode, snapshot));
209 bch_err(trans->c, "error in fsck: error %i updating inode", ret);
213 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
215 struct btree_iter iter = { NULL };
216 struct bkey_i_inode_generation delete;
217 struct bch_inode_unpacked inode_u;
221 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
222 SPOS(inum, 0, snapshot),
223 SPOS(inum, U64_MAX, snapshot),
225 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
226 SPOS(inum, 0, snapshot),
227 SPOS(inum, U64_MAX, snapshot),
229 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
230 SPOS(inum, 0, snapshot),
231 SPOS(inum, U64_MAX, snapshot),
236 bch2_trans_begin(trans);
238 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
239 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
240 k = bch2_btree_iter_peek_slot(&iter);
246 if (k.k->type != KEY_TYPE_inode) {
247 bch2_fs_inconsistent(trans->c,
248 "inode %llu:%u not found when deleting",
254 bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u);
256 /* Subvolume root? */
257 if (inode_u.bi_subvol) {
258 ret = bch2_subvolume_delete(trans, inode_u.bi_subvol, -1);
263 bkey_inode_generation_init(&delete.k_i);
264 delete.k.p = iter.pos;
265 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
267 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
268 bch2_trans_commit(trans, NULL, NULL,
269 BTREE_INSERT_NOFAIL);
271 bch2_trans_iter_exit(trans, &iter);
278 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
280 struct bch_fs *c = trans->c;
281 struct btree_iter iter;
282 struct bch_inode_unpacked dir_inode;
283 struct bch_hash_info dir_hash_info;
286 ret = lookup_inode(trans, pos.inode, &dir_inode, NULL);
290 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
292 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
294 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
295 &dir_hash_info, &iter, 0);
296 bch2_trans_iter_exit(trans, &iter);
300 static int remove_dirent(struct btree_trans *trans, struct bpos pos)
302 int ret = __bch2_trans_do(trans, NULL, NULL,
304 BTREE_INSERT_LAZY_RW,
305 __remove_dirent(trans, pos));
307 bch_err(trans->c, "remove_dirent: err %i deleting dirent", ret);
311 /* Get lost+found, create if it doesn't exist: */
312 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
313 struct bch_inode_unpacked *lostfound)
315 struct bch_fs *c = trans->c;
316 struct bch_inode_unpacked root;
317 struct bch_hash_info root_hash_info;
318 struct qstr lostfound_str = QSTR("lost+found");
319 subvol_inum root_inum = { .subvol = subvol };
325 ret = subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
329 ret = lookup_inode(trans, root_inum.inum, &root, &snapshot);
331 bch_err(c, "error fetching subvol root: %i", ret);
335 root_hash_info = bch2_hash_info_init(c, &root);
337 ret = lookup_dirent(trans, root_hash_info, root_inum,
338 &lostfound_str, &inum, &d_type);
339 if (ret == -ENOENT) {
340 bch_notice(c, "creating lost+found");
341 goto create_lostfound;
345 bch_err(c, "error looking up lost+found: %i", ret);
349 if (d_type != DT_DIR) {
350 bch_err(c, "error looking up lost+found: not a directory");
355 ret = lookup_inode(trans, inum, lostfound, &snapshot);
356 if (ret && ret != -ENOENT) {
358 * The check_dirents pass has already run, dangling dirents
359 * shouldn't exist here:
361 bch_err(c, "error looking up lost+found: %i", ret);
365 if (ret == -ENOENT) {
367 bch2_inode_init_early(c, lostfound);
369 ret = __bch2_trans_do(trans, NULL, NULL,
371 BTREE_INSERT_LAZY_RW,
372 bch2_create_trans(trans, root_inum, &root,
373 lostfound, &lostfound_str,
374 0, 0, S_IFDIR|0700, 0, NULL, NULL,
375 (subvol_inum) { }, 0));
377 bch_err(c, "error creating lost+found: %i", ret);
383 static int reattach_inode(struct btree_trans *trans,
384 struct bch_inode_unpacked *inode,
387 struct bch_hash_info dir_hash;
388 struct bch_inode_unpacked lostfound;
395 ret = snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
399 ret = lookup_lostfound(trans, subvol, &lostfound);
403 if (S_ISDIR(inode->bi_mode)) {
404 lostfound.bi_nlink++;
406 ret = write_inode(trans, &lostfound, U32_MAX);
411 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
413 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
414 name = (struct qstr) QSTR(name_buf);
416 ret = __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
417 bch2_dirent_create(trans,
420 .inum = lostfound.bi_inum,
423 mode_to_type(inode->bi_mode),
424 &name, inode->bi_inum, &dir_offset,
425 BCH_HASH_SET_MUST_CREATE));
427 bch_err(trans->c, "error %i reattaching inode %llu",
428 ret, inode->bi_inum);
432 inode->bi_dir = lostfound.bi_inum;
433 inode->bi_dir_offset = dir_offset;
435 return write_inode(trans, inode, inode_snapshot);
438 static int remove_backpointer(struct btree_trans *trans,
439 struct bch_inode_unpacked *inode)
441 struct btree_iter iter;
445 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
446 POS(inode->bi_dir, inode->bi_dir_offset), 0);
447 k = bch2_btree_iter_peek_slot(&iter);
451 if (k.k->type != KEY_TYPE_dirent) {
456 ret = remove_dirent(trans, k.k->p);
458 bch2_trans_iter_exit(trans, &iter);
462 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s, struct bpos pos)
464 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
466 if (bkey_cmp(s->pos, pos))
470 /* Might get called multiple times due to lock restarts */
471 if (s->nr && s->d[s->nr - 1] == pos.snapshot)
474 return snapshots_seen_add(c, s, pos.snapshot);
478 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
479 * and @ancestor hasn't been overwritten in @seen
481 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
483 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
484 u32 id, u32 ancestor)
488 BUG_ON(id > ancestor);
490 id = snapshot_t(c, id)->equiv;
491 ancestor = snapshot_t(c, ancestor)->equiv;
493 /* @ancestor should be the snapshot most recently added to @seen */
494 BUG_ON(!seen->nr || seen->d[seen->nr - 1] != ancestor);
495 BUG_ON(seen->pos.snapshot != ancestor);
500 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
503 for (i = seen->nr - 2;
504 i >= 0 && seen->d[i] >= id;
506 if (bch2_snapshot_is_ancestor(c, id, seen->d[i]) &&
507 bch2_snapshot_is_ancestor(c, seen->d[i], ancestor))
514 * ref_visible - given a key with snapshot id @src that points to a key with
515 * snapshot id @dst, test whether there is some snapshot in which @dst is
518 * This assumes we're visiting @src keys in natural key order.
520 * @s - list of snapshot IDs already seen at @src
521 * @src - snapshot ID of src key
522 * @dst - snapshot ID of dst key
524 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
528 ? key_visible_in_snapshot(c, s, dst, src)
529 : bch2_snapshot_is_ancestor(c, src, dst);
532 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
533 for (_i = (_w)->d; _i < (_w)->d + (_w)->nr && (_i)->snapshot <= (_snapshot); _i++)\
534 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
536 struct inode_walker {
537 bool first_this_inode;
542 struct inode_walker_entry {
543 struct bch_inode_unpacked inode;
549 static void inode_walker_exit(struct inode_walker *w)
555 static struct inode_walker inode_walker_init(void)
557 return (struct inode_walker) { 0, };
560 static int inode_walker_realloc(struct inode_walker *w)
562 if (w->nr == w->size) {
563 size_t new_size = max_t(size_t, 8UL, w->size * 2);
564 void *d = krealloc(w->d, new_size * sizeof(w->d[0]),
576 static int add_inode(struct bch_fs *c, struct inode_walker *w,
577 struct bkey_s_c_inode inode)
579 struct bch_inode_unpacked u;
582 ret = inode_walker_realloc(w);
586 BUG_ON(bch2_inode_unpack(inode, &u));
588 w->d[w->nr++] = (struct inode_walker_entry) {
590 .snapshot = snapshot_t(c, inode.k->p.snapshot)->equiv,
596 static int __walk_inode(struct btree_trans *trans,
597 struct inode_walker *w, struct bpos pos)
599 struct bch_fs *c = trans->c;
600 struct btree_iter iter;
602 unsigned i, ancestor_pos;
605 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
607 if (pos.inode == w->cur_inum) {
608 w->first_this_inode = false;
609 goto lookup_snapshot;
614 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
615 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
616 if (k.k->p.offset != pos.inode)
619 if (k.k->type == KEY_TYPE_inode)
620 add_inode(c, w, bkey_s_c_to_inode(k));
622 bch2_trans_iter_exit(trans, &iter);
627 w->cur_inum = pos.inode;
628 w->first_this_inode = true;
630 for (i = 0; i < w->nr; i++)
631 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->d[i].snapshot))
635 BUG_ON(pos.snapshot > w->d[i].snapshot);
637 if (pos.snapshot != w->d[i].snapshot) {
640 while (i && w->d[i - 1].snapshot > pos.snapshot)
643 ret = inode_walker_realloc(w);
647 array_insert_item(w->d, w->nr, i, w->d[ancestor_pos]);
648 w->d[i].snapshot = pos.snapshot;
655 static int walk_inode(struct btree_trans *trans,
656 struct inode_walker *w, struct bpos pos)
658 return lockrestart_do(trans, __walk_inode(trans, w, pos));
661 static int __get_visible_inodes(struct btree_trans *trans,
662 struct inode_walker *w,
663 struct snapshots_seen *s,
666 struct bch_fs *c = trans->c;
667 struct btree_iter iter;
673 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
674 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
675 if (k.k->p.offset != inum)
678 if (k.k->type != KEY_TYPE_inode)
681 if (ref_visible(c, s, s->pos.snapshot, k.k->p.snapshot)) {
682 add_inode(c, w, bkey_s_c_to_inode(k));
683 if (k.k->p.snapshot >= s->pos.snapshot)
687 bch2_trans_iter_exit(trans, &iter);
692 static int check_key_has_snapshot(struct btree_trans *trans,
693 struct btree_iter *iter,
696 struct bch_fs *c = trans->c;
700 if (fsck_err_on(!snapshot_t(c, k.k->p.snapshot)->equiv, c,
701 "key in missing snapshot: %s",
702 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf))) {
703 ret = __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
704 bch2_btree_delete_at(trans, iter,
705 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
706 return ret ?: -EINTR;
712 static int hash_redo_key(struct btree_trans *trans,
713 const struct bch_hash_desc desc,
714 struct bch_hash_info *hash_info,
715 struct btree_iter *k_iter, struct bkey_s_c k)
717 bch_err(trans->c, "hash_redo_key() not implemented yet");
720 struct bkey_i *delete;
723 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
725 return PTR_ERR(delete);
727 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
731 bkey_reassemble(tmp, k);
733 bkey_init(&delete->k);
734 delete->k.p = k_iter->pos;
735 return bch2_btree_iter_traverse(k_iter) ?:
736 bch2_trans_update(trans, k_iter, delete, 0) ?:
737 bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode, tmp, 0);
741 static int fsck_hash_delete_at(struct btree_trans *trans,
742 const struct bch_hash_desc desc,
743 struct bch_hash_info *info,
744 struct btree_iter *iter)
748 ret = bch2_hash_delete_at(trans, desc, info, iter, 0) ?:
749 bch2_trans_commit(trans, NULL, NULL,
751 BTREE_INSERT_LAZY_RW);
753 ret = bch2_btree_iter_traverse(iter);
761 static int hash_check_key(struct btree_trans *trans,
762 const struct bch_hash_desc desc,
763 struct bch_hash_info *hash_info,
764 struct btree_iter *k_iter, struct bkey_s_c hash_k)
766 struct bch_fs *c = trans->c;
767 struct btree_iter iter = { NULL };
773 if (hash_k.k->type != desc.key_type)
776 hash = desc.hash_bkey(hash_info, hash_k);
778 if (likely(hash == hash_k.k->p.offset))
781 if (hash_k.k->p.offset < hash)
784 for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
785 BTREE_ITER_SLOTS, k, ret) {
786 if (!bkey_cmp(k.k->p, hash_k.k->p))
789 if (fsck_err_on(k.k->type == desc.key_type &&
790 !desc.cmp_bkey(k, hash_k), c,
791 "duplicate hash table keys:\n%s",
792 (bch2_bkey_val_to_text(&PBUF(buf), c,
794 ret = fsck_hash_delete_at(trans, desc, hash_info, k_iter);
801 if (bkey_deleted(k.k)) {
802 bch2_trans_iter_exit(trans, &iter);
807 bch2_trans_iter_exit(trans, &iter);
810 if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
811 "hashed to %llu\n%s",
812 desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset, hash,
813 (bch2_bkey_val_to_text(&PBUF(buf), c, hash_k), buf)) == FSCK_ERR_IGNORE)
816 ret = __bch2_trans_do(trans, NULL, NULL,
817 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
818 hash_redo_key(trans, desc, hash_info, k_iter, hash_k));
820 bch_err(c, "hash_redo_key err %i", ret);
828 static int check_inode(struct btree_trans *trans,
829 struct btree_iter *iter,
830 struct bch_inode_unpacked *prev,
831 struct bch_inode_unpacked u)
833 struct bch_fs *c = trans->c;
834 bool do_update = false;
837 if (fsck_err_on(prev &&
838 (prev->bi_hash_seed != u.bi_hash_seed ||
839 mode_to_type(prev->bi_mode) != mode_to_type(u.bi_mode)), c,
840 "inodes in different snapshots don't match")) {
841 bch_err(c, "repair not implemented yet");
845 if (u.bi_flags & BCH_INODE_UNLINKED &&
847 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
849 bch2_trans_unlock(trans);
852 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
854 bch_err(c, "error in fsck: error %i while deleting inode", ret);
858 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
860 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
862 bch_verbose(c, "truncating inode %llu", u.bi_inum);
864 bch2_trans_unlock(trans);
868 * XXX: need to truncate partial blocks too here - or ideally
869 * just switch units to bytes and that issue goes away
871 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
872 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
874 POS(u.bi_inum, U64_MAX),
877 bch_err(c, "error in fsck: error %i truncating inode", ret);
882 * We truncated without our normal sector accounting hook, just
883 * make sure we recalculate it:
885 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
887 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
891 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
893 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
897 bch_verbose(c, "recounting sectors for inode %llu",
900 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
902 bch_err(c, "error in fsck: error %i recounting inode sectors",
907 u.bi_sectors = sectors;
908 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
912 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
915 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
920 ret = write_inode(trans, &u, iter->pos.snapshot);
922 bch_err(c, "error in fsck: error %i "
923 "updating inode", ret);
930 static int check_inodes(struct bch_fs *c, bool full)
932 struct btree_trans trans;
933 struct btree_iter iter;
935 struct bkey_s_c_inode inode;
936 struct bch_inode_unpacked prev, u;
939 memset(&prev, 0, sizeof(prev));
941 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
943 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
946 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
947 ret = check_key_has_snapshot(&trans, &iter, k);
952 * if snapshot id isn't a leaf node, skip it - deletion in
953 * particular is not atomic, so on the internal snapshot nodes
954 * we can see inodes marked for deletion after a clean shutdown
956 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
959 if (k.k->type != KEY_TYPE_inode)
962 inode = bkey_s_c_to_inode(k);
965 !(inode.v->bi_flags & (BCH_INODE_I_SIZE_DIRTY|
966 BCH_INODE_I_SECTORS_DIRTY|
967 BCH_INODE_UNLINKED)))
970 BUG_ON(bch2_inode_unpack(inode, &u));
972 ret = check_inode(&trans, &iter,
973 full && prev.bi_inum == u.bi_inum
980 bch2_trans_iter_exit(&trans, &iter);
982 BUG_ON(ret == -EINTR);
984 return bch2_trans_exit(&trans) ?: ret;
988 static int check_subvols(struct bch_fs *c)
990 struct btree_trans trans;
991 struct btree_iter iter;
995 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
997 for_each_btree_key(&trans, iter, BTREE_ID_subvolumes, POS_MIN,
1000 bch2_trans_iter_exit(&trans, &iter);
1002 bch2_trans_exit(&trans);
1007 * Checking for overlapping extents needs to be reimplemented
1010 static int fix_overlapping_extent(struct btree_trans *trans,
1011 struct bkey_s_c k, struct bpos cut_at)
1013 struct btree_iter iter;
1017 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1018 ret = PTR_ERR_OR_ZERO(u);
1022 bkey_reassemble(u, k);
1023 bch2_cut_front(cut_at, u);
1027 * We don't want to go through the extent_handle_overwrites path:
1029 * XXX: this is going to screw up disk accounting, extent triggers
1030 * assume things about extent overwrites - we should be running the
1031 * triggers manually here
1033 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1034 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1036 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1037 ret = bch2_btree_iter_traverse(&iter) ?:
1038 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1039 bch2_trans_commit(trans, NULL, NULL,
1040 BTREE_INSERT_NOFAIL|
1041 BTREE_INSERT_LAZY_RW);
1042 bch2_trans_iter_exit(trans, &iter);
1047 static int inode_backpointer_exists(struct btree_trans *trans,
1048 struct bch_inode_unpacked *inode,
1051 struct btree_iter iter;
1053 u32 target_subvol, target_snapshot;
1057 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
1058 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot), 0);
1059 k = bch2_btree_iter_peek_slot(&iter);
1063 if (k.k->type != KEY_TYPE_dirent)
1066 ret = __bch2_dirent_read_target(trans, bkey_s_c_to_dirent(k),
1074 ret = target_inum == inode->bi_inum;
1076 bch2_trans_iter_exit(trans, &iter);
1080 static bool inode_backpointer_matches(struct bkey_s_c_dirent d,
1081 struct bch_inode_unpacked *inode)
1083 return d.k->p.inode == inode->bi_dir &&
1084 d.k->p.offset == inode->bi_dir_offset;
1087 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1089 struct bch_fs *c = trans->c;
1090 struct inode_walker_entry *i;
1091 int ret = 0, ret2 = 0;
1094 for (i = w->d; i < w->d + w->nr; i++) {
1095 if (i->inode.bi_sectors == i->count)
1098 count2 = lockrestart_do(trans,
1099 bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot));
1101 if (i->count != count2) {
1102 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1105 if (i->inode.bi_sectors == i->count)
1109 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1110 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1111 w->cur_inum, i->snapshot,
1112 i->inode.bi_sectors, i->count) == FSCK_ERR_IGNORE)
1115 i->inode.bi_sectors = i->count;
1116 ret = write_inode(trans, &i->inode, i->snapshot);
1125 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1126 struct inode_walker *inode,
1127 struct snapshots_seen *s)
1129 struct bch_fs *c = trans->c;
1131 struct inode_walker_entry *i;
1135 k = bch2_btree_iter_peek(iter);
1143 ret = check_key_has_snapshot(trans, iter, k);
1147 ret = snapshots_seen_update(c, s, k.k->p);
1151 if (k.k->type == KEY_TYPE_whiteout)
1154 if (inode->cur_inum != k.k->p.inode) {
1155 ret = check_i_sectors(trans, inode);
1160 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
1164 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
1165 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
1167 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
1168 return fix_overlapping_extent(trans, k, prev.k->k.p) ?: -EINTR;
1171 ret = __walk_inode(trans, inode, k.k->p);
1175 if (fsck_err_on(ret == INT_MAX, c,
1176 "extent in missing inode:\n %s",
1177 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1178 return __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
1179 bch2_btree_delete_at(trans, iter,
1180 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
1188 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1189 !S_ISLNK(i->inode.bi_mode), c,
1190 "extent in non regular inode mode %o:\n %s",
1192 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1193 return __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
1194 bch2_btree_delete_at(trans, iter,
1195 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
1197 if (!bch2_snapshot_internal_node(c, k.k->p.snapshot)) {
1198 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i) {
1199 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1200 k.k->type != KEY_TYPE_reservation &&
1201 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9, c,
1202 "extent type %u offset %llu past end of inode %llu, i_size %llu",
1203 k.k->type, k.k->p.offset, k.k->p.inode, i->inode.bi_size)) {
1205 return bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1206 SPOS(k.k->p.inode, round_up(i->inode.bi_size, block_bytes(c)) >> 9,
1208 POS(k.k->p.inode, U64_MAX),
1214 if (bkey_extent_is_allocation(k.k))
1215 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i)
1216 i->count += k.k->size;
1218 bch2_bkey_buf_reassemble(&prev, c, k);
1226 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1227 * that i_size an i_sectors are consistent
1230 static int check_extents(struct bch_fs *c)
1232 struct inode_walker w = inode_walker_init();
1233 struct snapshots_seen s;
1234 struct btree_trans trans;
1235 struct btree_iter iter;
1239 struct bkey_buf prev;
1240 bch2_bkey_buf_init(&prev);
1241 prev.k->k = KEY(0, 0, 0);
1243 snapshots_seen_init(&s);
1244 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1246 bch_verbose(c, "checking extents");
1248 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
1249 POS(BCACHEFS_ROOT_INO, 0),
1251 BTREE_ITER_PREFETCH|
1252 BTREE_ITER_ALL_SNAPSHOTS);
1255 ret = lockrestart_do(&trans,
1256 check_extent(&trans, &iter, &w, &s));
1259 } while (bch2_btree_iter_advance(&iter));
1260 bch2_trans_iter_exit(&trans, &iter);
1262 bch2_bkey_buf_exit(&prev, c);
1264 inode_walker_exit(&w);
1265 bch2_trans_exit(&trans);
1266 snapshots_seen_exit(&s);
1271 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1273 struct bch_fs *c = trans->c;
1274 struct inode_walker_entry *i;
1275 int ret = 0, ret2 = 0;
1278 for (i = w->d; i < w->d + w->nr; i++) {
1279 if (i->inode.bi_nlink == i->count)
1282 count2 = lockrestart_do(trans,
1283 bch2_count_subdirs(trans, w->cur_inum, i->snapshot));
1285 if (i->count != count2) {
1286 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1289 if (i->inode.bi_nlink == i->count)
1293 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1294 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1295 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1296 i->inode.bi_nlink = i->count;
1297 ret = write_inode(trans, &i->inode, i->snapshot);
1307 static int check_dirent_target(struct btree_trans *trans,
1308 struct btree_iter *iter,
1309 struct bkey_s_c_dirent d,
1310 struct bch_inode_unpacked *target,
1311 u32 target_snapshot)
1313 struct bch_fs *c = trans->c;
1314 bool backpointer_exists = true;
1318 if (!target->bi_dir &&
1319 !target->bi_dir_offset) {
1320 target->bi_dir = d.k->p.inode;
1321 target->bi_dir_offset = d.k->p.offset;
1323 ret = write_inode(trans, target, target_snapshot);
1328 if (!inode_backpointer_matches(d, target)) {
1329 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1333 backpointer_exists = ret;
1336 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1337 backpointer_exists, c,
1338 "directory %llu with multiple links",
1340 ret = remove_dirent(trans, d.k->p);
1346 if (fsck_err_on(backpointer_exists &&
1347 !target->bi_nlink, c,
1348 "inode %llu has multiple links but i_nlink 0",
1351 target->bi_flags &= ~BCH_INODE_UNLINKED;
1353 ret = write_inode(trans, target, target_snapshot);
1358 if (fsck_err_on(!backpointer_exists, c,
1359 "inode %llu has wrong backpointer:\n"
1361 "should be %llu:%llu",
1364 target->bi_dir_offset,
1367 target->bi_dir = d.k->p.inode;
1368 target->bi_dir_offset = d.k->p.offset;
1370 ret = write_inode(trans, target, target_snapshot);
1376 if (fsck_err_on(vfs_d_type(d.v->d_type) != mode_to_type(target->bi_mode), c,
1377 "incorrect d_type: should be %u:\n%s",
1378 mode_to_type(target->bi_mode),
1379 (bch2_bkey_val_to_text(&PBUF(buf), c, d.s_c), buf))) {
1380 struct bkey_i_dirent *n;
1382 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
1388 bkey_reassemble(&n->k_i, d.s_c);
1389 n->v.d_type = mode_to_type(target->bi_mode);
1391 ret = __bch2_trans_do(trans, NULL, NULL,
1392 BTREE_INSERT_NOFAIL|
1393 BTREE_INSERT_LAZY_RW,
1394 bch2_trans_update(trans, iter, &n->k_i, 0));
1404 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1405 struct bch_hash_info *hash_info,
1406 struct inode_walker *dir,
1407 struct inode_walker *target,
1408 struct snapshots_seen *s)
1410 struct bch_fs *c = trans->c;
1412 struct bkey_s_c_dirent d;
1413 struct inode_walker_entry *i;
1414 u32 target_snapshot;
1420 k = bch2_btree_iter_peek(iter);
1428 ret = check_key_has_snapshot(trans, iter, k);
1432 ret = snapshots_seen_update(c, s, k.k->p);
1436 if (k.k->type == KEY_TYPE_whiteout)
1439 if (dir->cur_inum != k.k->p.inode) {
1440 ret = check_subdir_count(trans, dir);
1445 ret = __walk_inode(trans, dir, k.k->p);
1449 if (fsck_err_on(ret == INT_MAX, c,
1450 "dirent in nonexisting directory:\n%s",
1451 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1452 return __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
1453 bch2_btree_delete_at(trans, iter,
1454 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
1462 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1463 "dirent in non directory inode type %u:\n%s",
1464 mode_to_type(i->inode.bi_mode),
1465 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1466 return __bch2_trans_do(trans, NULL, NULL, 0,
1467 bch2_btree_delete_at(trans, iter, 0));
1469 if (dir->first_this_inode)
1470 *hash_info = bch2_hash_info_init(c, &dir->d[0].inode);
1472 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1473 hash_info, iter, k);
1476 if (ret) /* dirent has been deleted */
1479 if (k.k->type != KEY_TYPE_dirent)
1482 d = bkey_s_c_to_dirent(k);
1484 ret = __bch2_dirent_read_target(trans, d,
1489 if (ret && ret != -ENOENT)
1492 if (fsck_err_on(ret, c,
1493 "dirent points to missing subvolume %llu",
1494 le64_to_cpu(d.v->d_inum)))
1495 return remove_dirent(trans, d.k->p);
1497 if (target_subvol) {
1498 struct bch_inode_unpacked subvol_root;
1500 ret = __lookup_inode(trans, target_inum,
1501 &subvol_root, &target_snapshot);
1502 if (ret && ret != -ENOENT)
1505 if (fsck_err_on(ret, c,
1506 "subvolume %u points to missing subvolume root %llu",
1509 bch_err(c, "repair not implemented yet");
1513 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1514 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1516 subvol_root.bi_subvol, target_subvol)) {
1517 subvol_root.bi_subvol = target_subvol;
1518 ret = write_inode(trans, &subvol_root, target_snapshot);
1523 ret = check_dirent_target(trans, iter, d, &subvol_root,
1528 ret = __get_visible_inodes(trans, target, s, target_inum);
1532 if (fsck_err_on(!target->nr, c,
1533 "dirent points to missing inode:\n%s",
1534 (bch2_bkey_val_to_text(&PBUF(buf), c,
1536 ret = remove_dirent(trans, d.k->p);
1541 for (i = target->d; i < target->d + target->nr; i++) {
1542 ret = check_dirent_target(trans, iter, d,
1543 &i->inode, i->snapshot);
1549 if (d.v->d_type == DT_DIR)
1550 for_each_visible_inode(c, s, dir, d.k->p.snapshot, i)
1558 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1562 static int check_dirents(struct bch_fs *c)
1564 struct inode_walker dir = inode_walker_init();
1565 struct inode_walker target = inode_walker_init();
1566 struct snapshots_seen s;
1567 struct bch_hash_info hash_info;
1568 struct btree_trans trans;
1569 struct btree_iter iter;
1572 bch_verbose(c, "checking dirents");
1574 snapshots_seen_init(&s);
1575 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1577 bch2_trans_iter_init(&trans, &iter, BTREE_ID_dirents,
1578 POS(BCACHEFS_ROOT_INO, 0),
1580 BTREE_ITER_PREFETCH|
1581 BTREE_ITER_ALL_SNAPSHOTS);
1584 ret = lockrestart_do(&trans,
1585 check_dirent(&trans, &iter, &hash_info,
1586 &dir, &target, &s));
1589 } while (bch2_btree_iter_advance(&iter));
1590 bch2_trans_iter_exit(&trans, &iter);
1592 bch2_trans_exit(&trans);
1593 snapshots_seen_exit(&s);
1594 inode_walker_exit(&dir);
1595 inode_walker_exit(&target);
1600 * Walk xattrs: verify that they all have a corresponding inode
1603 static int check_xattrs(struct bch_fs *c)
1605 struct inode_walker w = inode_walker_init();
1606 struct bch_hash_info hash_info;
1607 struct btree_trans trans;
1608 struct btree_iter iter;
1612 bch_verbose(c, "checking xattrs");
1614 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1616 bch2_trans_iter_init(&trans, &iter, BTREE_ID_xattrs,
1617 POS(BCACHEFS_ROOT_INO, 0),
1619 BTREE_ITER_PREFETCH|
1620 BTREE_ITER_ALL_SNAPSHOTS);
1622 bch2_trans_begin(&trans);
1624 while ((k = bch2_btree_iter_peek(&iter)).k &&
1625 !(ret = bkey_err(k))) {
1626 ret = check_key_has_snapshot(&trans, &iter, k);
1630 ret = walk_inode(&trans, &w, k.k->p);
1634 if (fsck_err_on(ret == INT_MAX, c,
1635 "xattr for missing inode %llu",
1637 ret = bch2_btree_delete_at(&trans, &iter, 0);
1647 if (w.first_this_inode)
1648 hash_info = bch2_hash_info_init(c, &w.d[0].inode);
1650 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
1651 &hash_info, &iter, k);
1655 bch2_btree_iter_advance(&iter);
1661 bch2_trans_iter_exit(&trans, &iter);
1662 return bch2_trans_exit(&trans) ?: ret;
1665 /* Get root directory, create if it doesn't exist: */
1666 static int check_root(struct bch_fs *c)
1668 struct btree_trans trans;
1669 struct bch_inode_unpacked root_inode;
1674 bch2_trans_init(&trans, c, 0, 0);
1676 bch_verbose(c, "checking root directory");
1678 ret = subvol_lookup(&trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1679 if (ret && ret != -ENOENT)
1682 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1683 struct bkey_i_subvolume root_subvol;
1686 inum = BCACHEFS_ROOT_INO;
1688 bkey_subvolume_init(&root_subvol.k_i);
1689 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1690 root_subvol.v.flags = 0;
1691 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1692 root_subvol.v.inode = cpu_to_le64(inum);
1693 ret = __bch2_trans_do(&trans, NULL, NULL,
1694 BTREE_INSERT_NOFAIL|
1695 BTREE_INSERT_LAZY_RW,
1696 __bch2_btree_insert(&trans, BTREE_ID_subvolumes, &root_subvol.k_i));
1698 bch_err(c, "error writing root subvol: %i", ret);
1704 ret = lookup_inode(&trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1705 if (ret && ret != -ENOENT)
1708 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1709 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1710 "root inode not a directory")) {
1711 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1713 root_inode.bi_inum = inum;
1715 ret = write_inode(&trans, &root_inode, snapshot);
1717 bch_err(c, "error writing root inode: %i", ret);
1721 bch2_trans_exit(&trans);
1729 struct pathbuf_entry {
1734 static int path_down(struct pathbuf *p, u64 inum)
1736 if (p->nr == p->size) {
1737 size_t new_size = max_t(size_t, 256UL, p->size * 2);
1738 void *n = krealloc(p->entries,
1739 new_size * sizeof(p->entries[0]),
1749 p->entries[p->nr++] = (struct pathbuf_entry) {
1755 static int check_path(struct btree_trans *trans,
1757 struct bch_inode_unpacked *inode,
1760 struct bch_fs *c = trans->c;
1764 snapshot = snapshot_t(c, snapshot)->equiv;
1767 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1768 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1769 if (inode->bi_parent_subvol) {
1772 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1778 ret = lockrestart_do(trans,
1779 inode_backpointer_exists(trans, inode, snapshot));
1784 if (fsck_err(c, "unreachable inode %llu:%u, type %u nlink %u backptr %llu:%llu",
1785 inode->bi_inum, snapshot,
1786 mode_to_type(inode->bi_mode),
1789 inode->bi_dir_offset))
1790 ret = reattach_inode(trans, inode, snapshot);
1795 if (!S_ISDIR(inode->bi_mode))
1798 ret = path_down(p, inode->bi_inum);
1800 bch_err(c, "memory allocation failure");
1804 for (i = 0; i < p->nr; i++) {
1805 if (inode->bi_dir != p->entries[i].inum)
1808 /* XXX print path */
1809 if (!fsck_err(c, "directory structure loop"))
1812 ret = lockrestart_do(trans,
1813 remove_backpointer(trans, inode));
1815 bch_err(c, "error removing dirent: %i", ret);
1819 ret = reattach_inode(trans, inode, snapshot);
1823 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1825 /* Should have been caught in dirents pass */
1826 bch_err(c, "error looking up parent directory: %i", ret);
1832 bch_err(c, "%s: err %i", __func__, ret);
1837 * Check for unreachable inodes, as well as loops in the directory structure:
1838 * After check_dirents(), if an inode backpointer doesn't exist that means it's
1841 static int check_directory_structure(struct bch_fs *c)
1843 struct btree_trans trans;
1844 struct btree_iter iter;
1846 struct bch_inode_unpacked u;
1847 struct pathbuf path = { 0, 0, NULL };
1850 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1852 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
1854 BTREE_ITER_PREFETCH|
1855 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
1856 if (k.k->type != KEY_TYPE_inode)
1859 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), &u);
1861 /* Should have been caught earlier in fsck: */
1862 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1866 if (u.bi_flags & BCH_INODE_UNLINKED)
1869 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
1873 bch2_trans_iter_exit(&trans, &iter);
1875 BUG_ON(ret == -EINTR);
1877 kfree(path.entries);
1879 return bch2_trans_exit(&trans) ?: ret;
1882 struct nlink_table {
1893 static int add_nlink(struct nlink_table *t, u64 inum, u32 snapshot)
1895 if (t->nr == t->size) {
1896 size_t new_size = max_t(size_t, 128UL, t->size * 2);
1897 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
1903 memcpy(d, t->d, t->size * sizeof(t->d[0]));
1911 t->d[t->nr++] = (struct nlink) {
1913 .snapshot = snapshot,
1919 static int nlink_cmp(const void *_l, const void *_r)
1921 const struct nlink *l = _l;
1922 const struct nlink *r = _r;
1924 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
1927 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
1928 struct nlink_table *links,
1929 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
1931 struct nlink *link, key = {
1932 .inum = inum, .snapshot = U32_MAX,
1935 if (inum < range_start || inum >= range_end)
1938 link = __inline_bsearch(&key, links->d, links->nr,
1939 sizeof(links->d[0]), nlink_cmp);
1943 while (link > links->d && link[0].inum == link[-1].inum)
1946 for (; link < links->d + links->nr && link->inum == inum; link++)
1947 if (ref_visible(c, s, snapshot, link->snapshot)) {
1949 if (link->snapshot >= snapshot)
1955 static int check_nlinks_find_hardlinks(struct bch_fs *c,
1956 struct nlink_table *t,
1957 u64 start, u64 *end)
1959 struct btree_trans trans;
1960 struct btree_iter iter;
1962 struct bkey_s_c_inode inode;
1963 struct bch_inode_unpacked u;
1966 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1968 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
1971 BTREE_ITER_PREFETCH|
1972 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
1973 if (k.k->type != KEY_TYPE_inode)
1976 inode = bkey_s_c_to_inode(k);
1979 * Backpointer and directory structure checks are sufficient for
1980 * directories, since they can't have hardlinks:
1982 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
1985 /* Should never fail, checked by bch2_inode_invalid: */
1986 BUG_ON(bch2_inode_unpack(inode, &u));
1991 ret = add_nlink(t, k.k->p.offset, k.k->p.snapshot);
1993 *end = k.k->p.offset;
1999 bch2_trans_iter_exit(&trans, &iter);
2000 bch2_trans_exit(&trans);
2003 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2009 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2010 u64 range_start, u64 range_end)
2012 struct btree_trans trans;
2013 struct snapshots_seen s;
2014 struct btree_iter iter;
2016 struct bkey_s_c_dirent d;
2019 snapshots_seen_init(&s);
2021 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2023 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2025 BTREE_ITER_PREFETCH|
2026 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2027 ret = snapshots_seen_update(c, &s, k.k->p);
2031 switch (k.k->type) {
2032 case KEY_TYPE_dirent:
2033 d = bkey_s_c_to_dirent(k);
2035 if (d.v->d_type != DT_DIR &&
2036 d.v->d_type != DT_SUBVOL)
2037 inc_link(c, &s, links, range_start, range_end,
2038 le64_to_cpu(d.v->d_inum),
2043 bch2_trans_cond_resched(&trans);
2045 bch2_trans_iter_exit(&trans, &iter);
2048 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2050 bch2_trans_exit(&trans);
2051 snapshots_seen_exit(&s);
2056 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2057 struct nlink_table *links,
2058 u64 range_start, u64 range_end)
2060 struct btree_trans trans;
2061 struct btree_iter iter;
2063 struct bkey_s_c_inode inode;
2064 struct bch_inode_unpacked u;
2065 struct nlink *link = links->d;
2068 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2070 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2071 POS(0, range_start),
2073 BTREE_ITER_PREFETCH|
2074 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2075 if (k.k->p.offset >= range_end)
2078 if (k.k->type != KEY_TYPE_inode)
2081 inode = bkey_s_c_to_inode(k);
2082 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
2085 BUG_ON(bch2_inode_unpack(inode, &u));
2090 while ((cmp_int(link->inum, k.k->p.offset) ?:
2091 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2093 BUG_ON(link >= links->d + links->nr);
2096 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2097 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
2098 u.bi_inum, mode_to_type(u.bi_mode),
2099 bch2_inode_nlink_get(&u), link->count)) {
2100 bch2_inode_nlink_set(&u, link->count);
2102 ret = write_inode(&trans, &u, k.k->p.snapshot);
2104 bch_err(c, "error in fsck: error %i updating inode", ret);
2108 bch2_trans_iter_exit(&trans, &iter);
2109 bch2_trans_exit(&trans);
2112 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2118 static int check_nlinks(struct bch_fs *c)
2120 struct nlink_table links = { 0 };
2121 u64 this_iter_range_start, next_iter_range_start = 0;
2124 bch_verbose(c, "checking inode nlinks");
2127 this_iter_range_start = next_iter_range_start;
2128 next_iter_range_start = U64_MAX;
2130 ret = check_nlinks_find_hardlinks(c, &links,
2131 this_iter_range_start,
2132 &next_iter_range_start);
2134 ret = check_nlinks_walk_dirents(c, &links,
2135 this_iter_range_start,
2136 next_iter_range_start);
2140 ret = check_nlinks_update_hardlinks(c, &links,
2141 this_iter_range_start,
2142 next_iter_range_start);
2147 } while (next_iter_range_start != U64_MAX);
2155 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2156 * Doesn't fix them yet, mainly because they haven't yet been observed:
2158 int bch2_fsck_full(struct bch_fs *c)
2160 return bch2_fs_snapshots_check(c) ?:
2161 check_inodes(c, true) ?:
2167 check_directory_structure(c) ?:
2171 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2173 return check_inodes(c, false);