1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
10 #include "fs-common.h"
19 #include <linux/bsearch.h>
20 #include <linux/dcache.h> /* struct qstr */
22 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
25 * XXX: this is handling transaction restarts without returning
26 * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
28 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
31 struct btree_iter iter;
36 for_each_btree_key_upto(trans, iter, BTREE_ID_extents,
37 SPOS(inum, 0, snapshot),
40 if (bkey_extent_is_allocation(k.k))
43 bch2_trans_iter_exit(trans, &iter);
45 return ret ?: sectors;
48 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
51 struct btree_iter iter;
53 struct bkey_s_c_dirent d;
57 for_each_btree_key_upto(trans, iter, BTREE_ID_dirents,
58 SPOS(inum, 0, snapshot),
61 if (k.k->type != KEY_TYPE_dirent)
64 d = bkey_s_c_to_dirent(k);
65 if (d.v->d_type == DT_DIR)
68 bch2_trans_iter_exit(trans, &iter);
70 return ret ?: subdirs;
73 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
76 struct bch_snapshot s;
77 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots,
81 *subvol = le32_to_cpu(s.subvol);
82 else if (bch2_err_matches(ret, ENOENT))
83 bch_err(trans->c, "snapshot %u not fonud", snapshot);
88 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
89 u32 *snapshot, u64 *inum)
91 struct bch_subvolume s;
94 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
96 *snapshot = le32_to_cpu(s.snapshot);
97 *inum = le64_to_cpu(s.inode);
101 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
102 u32 *snapshot, u64 *inum)
104 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
107 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
108 struct bch_inode_unpacked *inode)
110 struct btree_iter iter;
114 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
116 BTREE_ITER_ALL_SNAPSHOTS);
117 k = bch2_btree_iter_peek(&iter);
122 if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
123 ret = -BCH_ERR_ENOENT_inode;
127 ret = bch2_inode_unpack(k, inode);
129 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
130 bch_err(trans->c, "error fetching inode %llu: %s",
131 inode_nr, bch2_err_str(ret));
132 bch2_trans_iter_exit(trans, &iter);
136 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
137 struct bch_inode_unpacked *inode,
140 struct btree_iter iter;
144 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
145 SPOS(0, inode_nr, *snapshot), 0);
150 ret = bkey_is_inode(k.k)
151 ? bch2_inode_unpack(k, inode)
152 : -BCH_ERR_ENOENT_inode;
154 *snapshot = iter.pos.snapshot;
156 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
157 bch_err(trans->c, "error fetching inode %llu:%u: %s",
158 inode_nr, *snapshot, bch2_err_str(ret));
159 bch2_trans_iter_exit(trans, &iter);
163 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
164 struct bch_inode_unpacked *inode,
167 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
170 static int __lookup_dirent(struct btree_trans *trans,
171 struct bch_hash_info hash_info,
172 subvol_inum dir, struct qstr *name,
173 u64 *target, unsigned *type)
175 struct btree_iter iter;
176 struct bkey_s_c_dirent d;
179 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
180 &hash_info, dir, name, 0);
184 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
185 *target = le64_to_cpu(d.v->d_inum);
187 bch2_trans_iter_exit(trans, &iter);
191 static int __write_inode(struct btree_trans *trans,
192 struct bch_inode_unpacked *inode,
195 struct bkey_inode_buf *inode_p =
196 bch2_trans_kmalloc(trans, sizeof(*inode_p));
199 return PTR_ERR(inode_p);
201 bch2_inode_pack(inode_p, inode);
202 inode_p->inode.k.p.snapshot = snapshot;
204 return bch2_btree_insert_nonextent(trans, BTREE_ID_inodes,
206 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
209 static int write_inode(struct btree_trans *trans,
210 struct bch_inode_unpacked *inode,
213 int ret = commit_do(trans, NULL, NULL,
215 BTREE_INSERT_LAZY_RW,
216 __write_inode(trans, inode, snapshot));
218 bch_err(trans->c, "error in fsck: error updating inode: %s",
223 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
225 struct bch_fs *c = trans->c;
226 struct btree_iter iter;
227 struct bch_inode_unpacked dir_inode;
228 struct bch_hash_info dir_hash_info;
231 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
235 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
237 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
239 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
240 &dir_hash_info, &iter,
241 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
242 bch2_trans_iter_exit(trans, &iter);
244 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
249 /* Get lost+found, create if it doesn't exist: */
250 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
251 struct bch_inode_unpacked *lostfound)
253 struct bch_fs *c = trans->c;
254 struct bch_inode_unpacked root;
255 struct bch_hash_info root_hash_info;
256 struct qstr lostfound_str = QSTR("lost+found");
257 subvol_inum root_inum = { .subvol = subvol };
263 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
267 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
271 root_hash_info = bch2_hash_info_init(c, &root);
273 ret = __lookup_dirent(trans, root_hash_info, root_inum,
274 &lostfound_str, &inum, &d_type);
275 if (bch2_err_matches(ret, ENOENT)) {
276 bch_notice(c, "creating lost+found");
277 goto create_lostfound;
280 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
281 bch_err(c, "error looking up lost+found: %s", bch2_err_str(ret));
285 if (d_type != DT_DIR) {
286 bch_err(c, "error looking up lost+found: not a directory");
291 * The bch2_check_dirents pass has already run, dangling dirents
292 * shouldn't exist here:
294 return __lookup_inode(trans, inum, lostfound, &snapshot);
297 bch2_inode_init_early(c, lostfound);
299 ret = bch2_create_trans(trans, root_inum, &root,
300 lostfound, &lostfound_str,
301 0, 0, S_IFDIR|0700, 0, NULL, NULL,
302 (subvol_inum) { }, 0);
303 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
304 bch_err(c, "error creating lost+found: %s", bch2_err_str(ret));
308 static int __reattach_inode(struct btree_trans *trans,
309 struct bch_inode_unpacked *inode,
312 struct bch_hash_info dir_hash;
313 struct bch_inode_unpacked lostfound;
320 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
324 ret = lookup_lostfound(trans, subvol, &lostfound);
328 if (S_ISDIR(inode->bi_mode)) {
329 lostfound.bi_nlink++;
331 ret = __write_inode(trans, &lostfound, U32_MAX);
336 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
338 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
339 name = (struct qstr) QSTR(name_buf);
341 ret = bch2_dirent_create(trans,
344 .inum = lostfound.bi_inum,
348 &name, inode->bi_inum, &dir_offset,
349 BCH_HASH_SET_MUST_CREATE);
353 inode->bi_dir = lostfound.bi_inum;
354 inode->bi_dir_offset = dir_offset;
356 return __write_inode(trans, inode, inode_snapshot);
359 static int reattach_inode(struct btree_trans *trans,
360 struct bch_inode_unpacked *inode,
363 int ret = commit_do(trans, NULL, NULL,
364 BTREE_INSERT_LAZY_RW|
366 __reattach_inode(trans, inode, inode_snapshot));
368 bch_err(trans->c, "error reattaching inode %llu: %s",
369 inode->bi_inum, bch2_err_str(ret));
376 static int remove_backpointer(struct btree_trans *trans,
377 struct bch_inode_unpacked *inode)
379 struct btree_iter iter;
380 struct bkey_s_c_dirent d;
383 d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
384 POS(inode->bi_dir, inode->bi_dir_offset), 0,
387 __remove_dirent(trans, d.k->p);
388 bch2_trans_iter_exit(trans, &iter);
392 struct snapshots_seen_entry {
397 struct snapshots_seen {
399 DARRAY(struct snapshots_seen_entry) ids;
402 static inline void snapshots_seen_exit(struct snapshots_seen *s)
404 darray_exit(&s->ids);
407 static inline void snapshots_seen_init(struct snapshots_seen *s)
409 memset(s, 0, sizeof(*s));
412 static int snapshots_seen_add_inorder(struct bch_fs *c, struct snapshots_seen *s, u32 id)
414 struct snapshots_seen_entry *i, n = {
416 .equiv = bch2_snapshot_equiv(c, id),
420 darray_for_each(s->ids, i) {
427 ret = darray_insert_item(&s->ids, i - s->ids.data, n);
429 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
434 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
435 enum btree_id btree_id, struct bpos pos)
437 struct snapshots_seen_entry *i, n = {
439 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
443 if (!bkey_eq(s->pos, pos))
447 s->pos.snapshot = n.equiv;
449 darray_for_each(s->ids, i) {
454 * We currently don't rigorously track for snapshot cleanup
455 * needing to be run, so it shouldn't be a fsck error yet:
457 if (i->equiv == n.equiv) {
458 bch_err(c, "snapshot deletion did not finish:\n"
459 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
460 bch2_btree_ids[btree_id],
461 pos.inode, pos.offset,
462 i->id, n.id, n.equiv);
463 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_delete_dead_snapshots);
467 ret = darray_push(&s->ids, n);
469 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
475 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
476 * and @ancestor hasn't been overwritten in @seen
478 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
480 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
481 u32 id, u32 ancestor)
485 EBUG_ON(id > ancestor);
486 EBUG_ON(!bch2_snapshot_is_equiv(c, id));
487 EBUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
489 /* @ancestor should be the snapshot most recently added to @seen */
490 EBUG_ON(ancestor != seen->pos.snapshot);
491 EBUG_ON(ancestor != seen->ids.data[seen->ids.nr - 1].equiv);
496 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
500 * We know that @id is a descendant of @ancestor, we're checking if
501 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
502 * @ascestor and with @id as a descendent.
504 * But we already know that we're scanning IDs between @id and @ancestor
505 * numerically, since snapshot ID lists are kept sorted, so if we find
506 * an id that's an ancestor of @id we're done:
509 for (i = seen->ids.nr - 2;
510 i >= 0 && seen->ids.data[i].equiv >= id;
512 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv))
519 * ref_visible - given a key with snapshot id @src that points to a key with
520 * snapshot id @dst, test whether there is some snapshot in which @dst is
523 * This assumes we're visiting @src keys in natural key order.
525 * @s - list of snapshot IDs already seen at @src
526 * @src - snapshot ID of src key
527 * @dst - snapshot ID of dst key
529 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
533 ? key_visible_in_snapshot(c, s, dst, src)
534 : bch2_snapshot_is_ancestor(c, src, dst);
537 static int ref_visible2(struct bch_fs *c,
538 u32 src, struct snapshots_seen *src_seen,
539 u32 dst, struct snapshots_seen *dst_seen)
541 src = bch2_snapshot_equiv(c, src);
542 dst = bch2_snapshot_equiv(c, dst);
546 swap(dst_seen, src_seen);
548 return key_visible_in_snapshot(c, src_seen, dst, src);
551 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
552 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
553 (_i)->snapshot <= (_snapshot); _i++) \
554 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
556 struct inode_walker_entry {
557 struct bch_inode_unpacked inode;
563 struct inode_walker {
564 bool first_this_inode;
565 bool recalculate_sums;
566 struct bpos last_pos;
568 DARRAY(struct inode_walker_entry) inodes;
571 static void inode_walker_exit(struct inode_walker *w)
573 darray_exit(&w->inodes);
576 static struct inode_walker inode_walker_init(void)
578 return (struct inode_walker) { 0, };
581 static int add_inode(struct bch_fs *c, struct inode_walker *w,
582 struct bkey_s_c inode)
584 struct bch_inode_unpacked u;
586 BUG_ON(bch2_inode_unpack(inode, &u));
588 return darray_push(&w->inodes, ((struct inode_walker_entry) {
590 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
594 static int get_inodes_all_snapshots(struct btree_trans *trans,
595 struct inode_walker *w, u64 inum)
597 struct bch_fs *c = trans->c;
598 struct btree_iter iter;
600 u32 restart_count = trans->restart_count;
603 w->recalculate_sums = false;
606 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
607 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
608 if (k.k->p.offset != inum)
611 if (bkey_is_inode(k.k))
614 bch2_trans_iter_exit(trans, &iter);
619 w->first_this_inode = true;
621 if (trans_was_restarted(trans, restart_count))
622 return -BCH_ERR_transaction_restart_nested;
627 static struct inode_walker_entry *
628 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w,
629 u32 snapshot, bool is_whiteout)
631 struct inode_walker_entry *i;
633 snapshot = bch2_snapshot_equiv(c, snapshot);
635 darray_for_each(w->inodes, i)
636 if (bch2_snapshot_is_ancestor(c, snapshot, i->snapshot))
641 BUG_ON(snapshot > i->snapshot);
643 if (snapshot != i->snapshot && !is_whiteout) {
644 struct inode_walker_entry new = *i;
648 new.snapshot = snapshot;
651 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
652 w->last_pos.inode, snapshot, i->snapshot);
654 while (i > w->inodes.data && i[-1].snapshot > snapshot)
657 pos = i - w->inodes.data;
658 ret = darray_insert_item(&w->inodes, pos, new);
662 i = w->inodes.data + pos;
668 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
669 struct inode_walker *w, struct bpos pos,
672 if (w->last_pos.inode != pos.inode) {
673 int ret = get_inodes_all_snapshots(trans, w, pos.inode);
676 } else if (bkey_cmp(w->last_pos, pos)) {
677 struct inode_walker_entry *i;
679 darray_for_each(w->inodes, i)
680 i->seen_this_pos = false;
686 return lookup_inode_for_snapshot(trans->c, w, pos.snapshot, is_whiteout);
689 static int __get_visible_inodes(struct btree_trans *trans,
690 struct inode_walker *w,
691 struct snapshots_seen *s,
694 struct bch_fs *c = trans->c;
695 struct btree_iter iter;
701 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
702 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
703 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
705 if (k.k->p.offset != inum)
708 if (!ref_visible(c, s, s->pos.snapshot, equiv))
711 if (bkey_is_inode(k.k))
714 if (equiv >= s->pos.snapshot)
717 bch2_trans_iter_exit(trans, &iter);
722 static int check_key_has_snapshot(struct btree_trans *trans,
723 struct btree_iter *iter,
726 struct bch_fs *c = trans->c;
727 struct printbuf buf = PRINTBUF;
730 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
731 "key in missing snapshot: %s",
732 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
733 ret = bch2_btree_delete_at(trans, iter,
734 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
740 static int hash_redo_key(struct btree_trans *trans,
741 const struct bch_hash_desc desc,
742 struct bch_hash_info *hash_info,
743 struct btree_iter *k_iter, struct bkey_s_c k)
745 struct bkey_i *delete;
748 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
750 return PTR_ERR(delete);
752 tmp = bch2_bkey_make_mut_noupdate(trans, k);
756 bkey_init(&delete->k);
757 delete->k.p = k_iter->pos;
758 return bch2_btree_iter_traverse(k_iter) ?:
759 bch2_trans_update(trans, k_iter, delete, 0) ?:
760 bch2_hash_set_snapshot(trans, desc, hash_info,
761 (subvol_inum) { 0, k.k->p.inode },
762 k.k->p.snapshot, tmp,
763 BCH_HASH_SET_MUST_CREATE,
764 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
765 bch2_trans_commit(trans, NULL, NULL,
767 BTREE_INSERT_LAZY_RW);
770 static int hash_check_key(struct btree_trans *trans,
771 const struct bch_hash_desc desc,
772 struct bch_hash_info *hash_info,
773 struct btree_iter *k_iter, struct bkey_s_c hash_k)
775 struct bch_fs *c = trans->c;
776 struct btree_iter iter = { NULL };
777 struct printbuf buf = PRINTBUF;
782 if (hash_k.k->type != desc.key_type)
785 hash = desc.hash_bkey(hash_info, hash_k);
787 if (likely(hash == hash_k.k->p.offset))
790 if (hash_k.k->p.offset < hash)
793 for_each_btree_key_norestart(trans, iter, desc.btree_id,
794 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
795 BTREE_ITER_SLOTS, k, ret) {
796 if (bkey_eq(k.k->p, hash_k.k->p))
799 if (fsck_err_on(k.k->type == desc.key_type &&
800 !desc.cmp_bkey(k, hash_k), c,
801 "duplicate hash table keys:\n%s",
802 (printbuf_reset(&buf),
803 bch2_bkey_val_to_text(&buf, c, hash_k),
805 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
809 if (bkey_deleted(k.k)) {
810 bch2_trans_iter_exit(trans, &iter);
815 bch2_trans_iter_exit(trans, &iter);
819 if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
820 bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
821 (printbuf_reset(&buf),
822 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
823 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
824 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
825 bch_err(c, "hash_redo_key err %s", bch2_err_str(ret));
828 ret = -BCH_ERR_transaction_restart_nested;
834 static int check_inode(struct btree_trans *trans,
835 struct btree_iter *iter,
837 struct bch_inode_unpacked *prev,
838 struct snapshots_seen *s,
841 struct bch_fs *c = trans->c;
842 struct bch_inode_unpacked u;
843 bool do_update = false;
846 ret = check_key_has_snapshot(trans, iter, k);
852 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
857 * if snapshot id isn't a leaf node, skip it - deletion in
858 * particular is not atomic, so on the internal snapshot nodes
859 * we can see inodes marked for deletion after a clean shutdown
861 if (bch2_snapshot_is_internal_node(c, k.k->p.snapshot))
864 if (!bkey_is_inode(k.k))
867 BUG_ON(bch2_inode_unpack(k, &u));
870 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
871 BCH_INODE_I_SECTORS_DIRTY|
872 BCH_INODE_UNLINKED)))
875 if (prev->bi_inum != u.bi_inum)
878 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
879 inode_d_type(prev) != inode_d_type(&u), c,
880 "inodes in different snapshots don't match")) {
881 bch_err(c, "repair not implemented yet");
885 if (u.bi_flags & BCH_INODE_UNLINKED &&
887 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
889 bch2_trans_unlock(trans);
892 ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
893 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
894 bch_err(c, "error in fsck: error while deleting inode: %s",
899 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
901 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
903 bch_verbose(c, "truncating inode %llu", u.bi_inum);
905 bch2_trans_unlock(trans);
909 * XXX: need to truncate partial blocks too here - or ideally
910 * just switch units to bytes and that issue goes away
912 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
913 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
915 POS(u.bi_inum, U64_MAX),
917 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
918 bch_err(c, "error in fsck: error truncating inode: %s",
924 * We truncated without our normal sector accounting hook, just
925 * make sure we recalculate it:
927 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
929 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
933 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
935 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
939 bch_verbose(c, "recounting sectors for inode %llu",
942 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
944 bch_err(c, "error in fsck: error recounting inode sectors: %s",
945 bch2_err_str(sectors));
949 u.bi_sectors = sectors;
950 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
954 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
957 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
962 ret = __write_inode(trans, &u, iter->pos.snapshot);
964 bch_err(c, "error in fsck: error updating inode: %s",
975 int bch2_check_inodes(struct bch_fs *c)
977 bool full = c->opts.fsck;
978 struct btree_trans trans;
979 struct btree_iter iter;
980 struct bch_inode_unpacked prev = { 0 };
981 struct snapshots_seen s;
985 snapshots_seen_init(&s);
986 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
988 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
990 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
991 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
992 check_inode(&trans, &iter, k, &prev, &s, full));
994 bch2_trans_exit(&trans);
995 snapshots_seen_exit(&s);
1001 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1002 struct btree_iter *iter,
1005 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
1008 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1009 struct bkey_s_c_dirent d)
1011 return inode->bi_dir == d.k->p.inode &&
1012 inode->bi_dir_offset == d.k->p.offset;
1015 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1016 struct bch_inode_unpacked *inode)
1018 return d.v->d_type == DT_SUBVOL
1019 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1020 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1023 static int inode_backpointer_exists(struct btree_trans *trans,
1024 struct bch_inode_unpacked *inode,
1027 struct btree_iter iter;
1028 struct bkey_s_c_dirent d;
1031 d = dirent_get_by_pos(trans, &iter,
1032 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1035 return bch2_err_matches(ret, ENOENT) ? 0 : ret;
1037 ret = dirent_points_to_inode(d, inode);
1038 bch2_trans_iter_exit(trans, &iter);
1042 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1044 struct bch_fs *c = trans->c;
1045 struct inode_walker_entry *i;
1046 u32 restart_count = trans->restart_count;
1050 darray_for_each(w->inodes, i) {
1051 if (i->inode.bi_sectors == i->count)
1054 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1056 if (w->recalculate_sums)
1059 if (i->count != count2) {
1060 bch_err(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1061 w->last_pos.inode, i->snapshot, i->count, count2);
1062 return -BCH_ERR_internal_fsck_err;
1065 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1066 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1067 w->last_pos.inode, i->snapshot,
1068 i->inode.bi_sectors, i->count)) {
1069 i->inode.bi_sectors = i->count;
1070 ret = write_inode(trans, &i->inode, i->snapshot);
1078 if (!ret && trans_was_restarted(trans, restart_count))
1079 ret = -BCH_ERR_transaction_restart_nested;
1086 struct snapshots_seen seen;
1089 struct extent_ends {
1090 struct bpos last_pos;
1091 DARRAY(struct extent_end) e;
1094 static void extent_ends_reset(struct extent_ends *extent_ends)
1096 struct extent_end *i;
1098 darray_for_each(extent_ends->e, i)
1099 snapshots_seen_exit(&i->seen);
1101 extent_ends->e.nr = 0;
1104 static void extent_ends_exit(struct extent_ends *extent_ends)
1106 extent_ends_reset(extent_ends);
1107 darray_exit(&extent_ends->e);
1110 static void extent_ends_init(struct extent_ends *extent_ends)
1112 memset(extent_ends, 0, sizeof(*extent_ends));
1115 static int extent_ends_at(struct bch_fs *c,
1116 struct extent_ends *extent_ends,
1117 struct snapshots_seen *seen,
1120 struct extent_end *i, n = (struct extent_end) {
1121 .offset = k.k->p.offset,
1122 .snapshot = k.k->p.snapshot,
1126 n.seen.ids.data = kmemdup(seen->ids.data,
1127 sizeof(seen->ids.data[0]) * seen->ids.size,
1129 if (!n.seen.ids.data)
1130 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1132 darray_for_each(extent_ends->e, i) {
1133 if (i->snapshot == k.k->p.snapshot) {
1134 snapshots_seen_exit(&i->seen);
1139 if (i->snapshot >= k.k->p.snapshot)
1143 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1146 static int overlapping_extents_found(struct btree_trans *trans,
1147 enum btree_id btree,
1148 struct bpos pos1, struct snapshots_seen *pos1_seen,
1151 struct extent_end *extent_end)
1153 struct bch_fs *c = trans->c;
1154 struct printbuf buf = PRINTBUF;
1155 struct btree_iter iter1, iter2 = { NULL };
1156 struct bkey_s_c k1, k2;
1159 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1161 bch2_trans_iter_init(trans, &iter1, btree, pos1,
1162 BTREE_ITER_ALL_SNAPSHOTS|
1163 BTREE_ITER_NOT_EXTENTS);
1164 k1 = bch2_btree_iter_peek_upto(&iter1, POS(pos1.inode, U64_MAX));
1169 prt_str(&buf, "\n ");
1170 bch2_bkey_val_to_text(&buf, c, k1);
1172 if (!bpos_eq(pos1, k1.k->p)) {
1173 prt_str(&buf, "\n wanted\n ");
1174 bch2_bpos_to_text(&buf, pos1);
1175 prt_str(&buf, "\n ");
1176 bch2_bkey_to_text(&buf, &pos2);
1178 bch_err(c, "%s: error finding first overlapping extent when repairing, got%s",
1180 ret = -BCH_ERR_internal_fsck_err;
1184 bch2_trans_copy_iter(&iter2, &iter1);
1187 bch2_btree_iter_advance(&iter2);
1189 k2 = bch2_btree_iter_peek_upto(&iter2, POS(pos1.inode, U64_MAX));
1194 if (bpos_ge(k2.k->p, pos2.p))
1198 prt_str(&buf, "\n ");
1199 bch2_bkey_val_to_text(&buf, c, k2);
1201 if (bpos_gt(k2.k->p, pos2.p) ||
1202 pos2.size != k2.k->size) {
1203 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1205 ret = -BCH_ERR_internal_fsck_err;
1209 prt_printf(&buf, "\n overwriting %s extent",
1210 pos1.snapshot >= pos2.p.snapshot ? "first" : "second");
1212 if (fsck_err(c, "overlapping extents%s", buf.buf)) {
1213 struct btree_iter *old_iter = &iter1;
1214 struct disk_reservation res = { 0 };
1216 if (pos1.snapshot < pos2.p.snapshot) {
1221 trans->extra_journal_res += bch2_bkey_sectors_compressed(k2);
1223 ret = bch2_trans_update_extent_overwrite(trans, old_iter,
1224 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE,
1226 bch2_trans_commit(trans, &res, NULL,
1227 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL);
1228 bch2_disk_reservation_put(c, &res);
1235 if (pos1.snapshot == pos2.p.snapshot) {
1237 * We overwrote the first extent, and did the overwrite
1238 * in the same snapshot:
1240 extent_end->offset = bkey_start_offset(&pos2);
1241 } else if (pos1.snapshot > pos2.p.snapshot) {
1243 * We overwrote the first extent in pos2's snapshot:
1245 ret = snapshots_seen_add_inorder(c, pos1_seen, pos2.p.snapshot);
1248 * We overwrote the second extent - restart
1249 * check_extent() from the top:
1251 ret = -BCH_ERR_transaction_restart_nested;
1256 bch2_trans_iter_exit(trans, &iter2);
1257 bch2_trans_iter_exit(trans, &iter1);
1258 printbuf_exit(&buf);
1262 static int check_overlapping_extents(struct btree_trans *trans,
1263 struct snapshots_seen *seen,
1264 struct extent_ends *extent_ends,
1267 struct btree_iter *iter,
1270 struct bch_fs *c = trans->c;
1271 struct extent_end *i;
1274 /* transaction restart, running again */
1275 if (bpos_eq(extent_ends->last_pos, k.k->p))
1278 if (extent_ends->last_pos.inode != k.k->p.inode)
1279 extent_ends_reset(extent_ends);
1281 darray_for_each(extent_ends->e, i) {
1282 if (i->offset <= bkey_start_offset(k.k))
1285 if (!ref_visible2(c,
1286 k.k->p.snapshot, seen,
1287 i->snapshot, &i->seen))
1290 ret = overlapping_extents_found(trans, iter->btree_id,
1291 SPOS(iter->pos.inode,
1300 ret = extent_ends_at(c, extent_ends, seen, k);
1304 extent_ends->last_pos = k.k->p;
1309 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1311 struct inode_walker *inode,
1312 struct snapshots_seen *s,
1313 struct extent_ends *extent_ends)
1315 struct bch_fs *c = trans->c;
1316 struct inode_walker_entry *i;
1317 struct printbuf buf = PRINTBUF;
1318 struct bpos equiv = k.k->p;
1321 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1323 ret = check_key_has_snapshot(trans, iter, k);
1325 ret = ret < 0 ? ret : 0;
1329 if (inode->last_pos.inode != k.k->p.inode) {
1330 ret = check_i_sectors(trans, inode);
1335 i = walk_inode(trans, inode, equiv, k.k->type == KEY_TYPE_whiteout);
1336 ret = PTR_ERR_OR_ZERO(i);
1340 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1344 if (k.k->type != KEY_TYPE_whiteout) {
1345 if (fsck_err_on(!i, c,
1346 "extent in missing inode:\n %s",
1347 (printbuf_reset(&buf),
1348 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1351 if (fsck_err_on(i &&
1352 !S_ISREG(i->inode.bi_mode) &&
1353 !S_ISLNK(i->inode.bi_mode), c,
1354 "extent in non regular inode mode %o:\n %s",
1356 (printbuf_reset(&buf),
1357 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1360 ret = check_overlapping_extents(trans, s, extent_ends, k,
1361 equiv.snapshot, iter,
1362 &inode->recalculate_sums);
1368 * Check inodes in reverse order, from oldest snapshots to newest,
1369 * starting from the inode that matches this extent's snapshot. If we
1370 * didn't have one, iterate over all inodes:
1373 i = inode->inodes.data + inode->inodes.nr - 1;
1376 inode->inodes.data && i >= inode->inodes.data;
1378 if (i->snapshot > equiv.snapshot ||
1379 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1382 if (k.k->type != KEY_TYPE_whiteout) {
1383 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1384 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1385 !bkey_extent_is_reservation(k), c,
1386 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1387 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1388 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1389 struct btree_iter iter2;
1391 bch2_trans_copy_iter(&iter2, iter);
1392 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1393 ret = bch2_btree_iter_traverse(&iter2) ?:
1394 bch2_btree_delete_at(trans, &iter2,
1395 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1396 bch2_trans_iter_exit(trans, &iter2);
1400 iter->k.type = KEY_TYPE_whiteout;
1403 if (bkey_extent_is_allocation(k.k))
1404 i->count += k.k->size;
1407 i->seen_this_pos = true;
1412 printbuf_exit(&buf);
1414 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1418 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1423 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1424 * that i_size an i_sectors are consistent
1426 int bch2_check_extents(struct bch_fs *c)
1428 struct inode_walker w = inode_walker_init();
1429 struct snapshots_seen s;
1430 struct btree_trans trans;
1431 struct btree_iter iter;
1433 struct extent_ends extent_ends;
1434 struct disk_reservation res = { 0 };
1437 snapshots_seen_init(&s);
1438 extent_ends_init(&extent_ends);
1439 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 4096);
1441 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1442 POS(BCACHEFS_ROOT_INO, 0),
1443 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1445 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
1446 bch2_disk_reservation_put(c, &res);
1447 check_extent(&trans, &iter, k, &w, &s, &extent_ends);
1449 check_i_sectors(&trans, &w);
1451 bch2_disk_reservation_put(c, &res);
1452 extent_ends_exit(&extent_ends);
1453 inode_walker_exit(&w);
1454 bch2_trans_exit(&trans);
1455 snapshots_seen_exit(&s);
1462 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1464 struct bch_fs *c = trans->c;
1465 struct inode_walker_entry *i;
1466 u32 restart_count = trans->restart_count;
1470 darray_for_each(w->inodes, i) {
1471 if (i->inode.bi_nlink == i->count)
1474 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1478 if (i->count != count2) {
1479 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1482 if (i->inode.bi_nlink == i->count)
1486 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1487 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1488 w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1489 i->inode.bi_nlink = i->count;
1490 ret = write_inode(trans, &i->inode, i->snapshot);
1498 if (!ret && trans_was_restarted(trans, restart_count))
1499 ret = -BCH_ERR_transaction_restart_nested;
1503 static int check_dirent_target(struct btree_trans *trans,
1504 struct btree_iter *iter,
1505 struct bkey_s_c_dirent d,
1506 struct bch_inode_unpacked *target,
1507 u32 target_snapshot)
1509 struct bch_fs *c = trans->c;
1510 struct bkey_i_dirent *n;
1511 bool backpointer_exists = true;
1512 struct printbuf buf = PRINTBUF;
1515 if (!target->bi_dir &&
1516 !target->bi_dir_offset) {
1517 target->bi_dir = d.k->p.inode;
1518 target->bi_dir_offset = d.k->p.offset;
1520 ret = __write_inode(trans, target, target_snapshot);
1525 if (!inode_points_to_dirent(target, d)) {
1526 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1530 backpointer_exists = ret;
1533 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1534 backpointer_exists, c,
1535 "directory %llu with multiple links",
1537 ret = __remove_dirent(trans, d.k->p);
1541 if (fsck_err_on(backpointer_exists &&
1542 !target->bi_nlink, c,
1543 "inode %llu type %s has multiple links but i_nlink 0",
1544 target->bi_inum, bch2_d_types[d.v->d_type])) {
1546 target->bi_flags &= ~BCH_INODE_UNLINKED;
1548 ret = __write_inode(trans, target, target_snapshot);
1553 if (fsck_err_on(!backpointer_exists, c,
1554 "inode %llu:%u has wrong backpointer:\n"
1556 "should be %llu:%llu",
1557 target->bi_inum, target_snapshot,
1559 target->bi_dir_offset,
1562 target->bi_dir = d.k->p.inode;
1563 target->bi_dir_offset = d.k->p.offset;
1565 ret = __write_inode(trans, target, target_snapshot);
1571 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1572 "incorrect d_type: got %s, should be %s:\n%s",
1573 bch2_d_type_str(d.v->d_type),
1574 bch2_d_type_str(inode_d_type(target)),
1575 (printbuf_reset(&buf),
1576 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1577 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1578 ret = PTR_ERR_OR_ZERO(n);
1582 bkey_reassemble(&n->k_i, d.s_c);
1583 n->v.d_type = inode_d_type(target);
1585 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1589 d = dirent_i_to_s_c(n);
1592 if (d.v->d_type == DT_SUBVOL &&
1593 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1594 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1595 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1596 le32_to_cpu(d.v->d_parent_subvol),
1597 target->bi_parent_subvol))) {
1598 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1599 ret = PTR_ERR_OR_ZERO(n);
1603 bkey_reassemble(&n->k_i, d.s_c);
1604 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1606 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1610 d = dirent_i_to_s_c(n);
1615 printbuf_exit(&buf);
1617 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1622 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1624 struct bch_hash_info *hash_info,
1625 struct inode_walker *dir,
1626 struct inode_walker *target,
1627 struct snapshots_seen *s)
1629 struct bch_fs *c = trans->c;
1630 struct bkey_s_c_dirent d;
1631 struct inode_walker_entry *i;
1632 struct printbuf buf = PRINTBUF;
1636 ret = check_key_has_snapshot(trans, iter, k);
1638 ret = ret < 0 ? ret : 0;
1643 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1645 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1649 if (k.k->type == KEY_TYPE_whiteout)
1652 if (dir->last_pos.inode != k.k->p.inode) {
1653 ret = check_subdir_count(trans, dir);
1658 BUG_ON(!iter->path->should_be_locked);
1660 i = walk_inode(trans, dir, equiv, k.k->type == KEY_TYPE_whiteout);
1661 ret = PTR_ERR_OR_ZERO(i);
1665 if (dir->first_this_inode && dir->inodes.nr)
1666 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1667 dir->first_this_inode = false;
1669 if (fsck_err_on(!i, c,
1670 "dirent in nonexisting directory:\n%s",
1671 (printbuf_reset(&buf),
1672 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1673 ret = bch2_btree_delete_at(trans, iter,
1674 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1681 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1682 "dirent in non directory inode type %s:\n%s",
1683 bch2_d_type_str(inode_d_type(&i->inode)),
1684 (printbuf_reset(&buf),
1685 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1686 ret = bch2_btree_delete_at(trans, iter, 0);
1690 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1694 /* dirent has been deleted */
1699 if (k.k->type != KEY_TYPE_dirent)
1702 d = bkey_s_c_to_dirent(k);
1704 if (d.v->d_type == DT_SUBVOL) {
1705 struct bch_inode_unpacked subvol_root;
1706 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1707 u32 target_snapshot;
1710 ret = __subvol_lookup(trans, target_subvol,
1711 &target_snapshot, &target_inum);
1712 if (ret && !bch2_err_matches(ret, ENOENT))
1715 if (fsck_err_on(ret, c,
1716 "dirent points to missing subvolume %u",
1717 le32_to_cpu(d.v->d_child_subvol))) {
1718 ret = __remove_dirent(trans, d.k->p);
1722 ret = __lookup_inode(trans, target_inum,
1723 &subvol_root, &target_snapshot);
1724 if (ret && !bch2_err_matches(ret, ENOENT))
1727 if (fsck_err_on(ret, c,
1728 "subvolume %u points to missing subvolume root %llu",
1731 bch_err(c, "repair not implemented yet");
1736 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1737 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1739 subvol_root.bi_subvol, target_subvol)) {
1740 subvol_root.bi_subvol = target_subvol;
1741 ret = __write_inode(trans, &subvol_root, target_snapshot);
1746 ret = check_dirent_target(trans, iter, d, &subvol_root,
1751 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1755 if (fsck_err_on(!target->inodes.nr, c,
1756 "dirent points to missing inode: (equiv %u)\n%s",
1758 (printbuf_reset(&buf),
1759 bch2_bkey_val_to_text(&buf, c, k),
1761 ret = __remove_dirent(trans, d.k->p);
1766 darray_for_each(target->inodes, i) {
1767 ret = check_dirent_target(trans, iter, d,
1768 &i->inode, i->snapshot);
1774 if (d.v->d_type == DT_DIR)
1775 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1781 printbuf_exit(&buf);
1783 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1789 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1792 int bch2_check_dirents(struct bch_fs *c)
1794 struct inode_walker dir = inode_walker_init();
1795 struct inode_walker target = inode_walker_init();
1796 struct snapshots_seen s;
1797 struct bch_hash_info hash_info;
1798 struct btree_trans trans;
1799 struct btree_iter iter;
1803 snapshots_seen_init(&s);
1804 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1806 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1807 POS(BCACHEFS_ROOT_INO, 0),
1808 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1811 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1812 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1814 bch2_trans_exit(&trans);
1815 snapshots_seen_exit(&s);
1816 inode_walker_exit(&dir);
1817 inode_walker_exit(&target);
1824 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1826 struct bch_hash_info *hash_info,
1827 struct inode_walker *inode)
1829 struct bch_fs *c = trans->c;
1830 struct inode_walker_entry *i;
1833 ret = check_key_has_snapshot(trans, iter, k);
1837 i = walk_inode(trans, inode, k.k->p, k.k->type == KEY_TYPE_whiteout);
1838 ret = PTR_ERR_OR_ZERO(i);
1842 if (inode->first_this_inode && inode->inodes.nr)
1843 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1844 inode->first_this_inode = false;
1846 if (fsck_err_on(!i, c,
1847 "xattr for missing inode %llu",
1849 return bch2_btree_delete_at(trans, iter, 0);
1854 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1856 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1862 * Walk xattrs: verify that they all have a corresponding inode
1864 int bch2_check_xattrs(struct bch_fs *c)
1866 struct inode_walker inode = inode_walker_init();
1867 struct bch_hash_info hash_info;
1868 struct btree_trans trans;
1869 struct btree_iter iter;
1873 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1875 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1876 POS(BCACHEFS_ROOT_INO, 0),
1877 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1880 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1881 check_xattr(&trans, &iter, k, &hash_info, &inode));
1883 bch2_trans_exit(&trans);
1890 static int check_root_trans(struct btree_trans *trans)
1892 struct bch_fs *c = trans->c;
1893 struct bch_inode_unpacked root_inode;
1898 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1899 if (ret && !bch2_err_matches(ret, ENOENT))
1902 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1903 struct bkey_i_subvolume root_subvol;
1906 inum = BCACHEFS_ROOT_INO;
1908 bkey_subvolume_init(&root_subvol.k_i);
1909 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1910 root_subvol.v.flags = 0;
1911 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1912 root_subvol.v.inode = cpu_to_le64(inum);
1913 ret = commit_do(trans, NULL, NULL,
1914 BTREE_INSERT_NOFAIL|
1915 BTREE_INSERT_LAZY_RW,
1916 __bch2_btree_insert(trans, BTREE_ID_subvolumes,
1917 &root_subvol.k_i, 0));
1919 bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1925 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1926 if (ret && !bch2_err_matches(ret, ENOENT))
1929 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1930 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1931 "root inode not a directory")) {
1932 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1934 root_inode.bi_inum = inum;
1936 ret = __write_inode(trans, &root_inode, snapshot);
1938 bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1945 /* Get root directory, create if it doesn't exist: */
1946 int bch2_check_root(struct bch_fs *c)
1950 ret = bch2_trans_do(c, NULL, NULL,
1951 BTREE_INSERT_NOFAIL|
1952 BTREE_INSERT_LAZY_RW,
1953 check_root_trans(&trans));
1960 struct pathbuf_entry {
1965 typedef DARRAY(struct pathbuf_entry) pathbuf;
1967 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1969 struct pathbuf_entry *i;
1971 darray_for_each(*p, i)
1972 if (i->inum == inum &&
1973 i->snapshot == snapshot)
1979 static int path_down(struct bch_fs *c, pathbuf *p,
1980 u64 inum, u32 snapshot)
1982 int ret = darray_push(p, ((struct pathbuf_entry) {
1984 .snapshot = snapshot,
1988 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1994 * Check that a given inode is reachable from the root:
1996 * XXX: we should also be verifying that inodes are in the right subvolumes
1998 static int check_path(struct btree_trans *trans,
2000 struct bch_inode_unpacked *inode,
2003 struct bch_fs *c = trans->c;
2006 snapshot = bch2_snapshot_equiv(c, snapshot);
2009 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
2010 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
2011 struct btree_iter dirent_iter;
2012 struct bkey_s_c_dirent d;
2013 u32 parent_snapshot = snapshot;
2015 if (inode->bi_subvol) {
2018 ret = subvol_lookup(trans, inode->bi_parent_subvol,
2019 &parent_snapshot, &inum);
2024 ret = lockrestart_do(trans,
2025 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
2026 SPOS(inode->bi_dir, inode->bi_dir_offset,
2027 parent_snapshot))).k));
2028 if (ret && !bch2_err_matches(ret, ENOENT))
2031 if (!ret && !dirent_points_to_inode(d, inode)) {
2032 bch2_trans_iter_exit(trans, &dirent_iter);
2033 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
2036 if (bch2_err_matches(ret, ENOENT)) {
2037 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2038 inode->bi_inum, snapshot,
2039 bch2_d_type_str(inode_d_type(inode)),
2042 inode->bi_dir_offset))
2043 ret = reattach_inode(trans, inode, snapshot);
2047 bch2_trans_iter_exit(trans, &dirent_iter);
2049 if (!S_ISDIR(inode->bi_mode))
2052 ret = path_down(c, p, inode->bi_inum, snapshot);
2054 bch_err(c, "memory allocation failure");
2058 snapshot = parent_snapshot;
2060 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2062 /* Should have been caught in dirents pass */
2063 bch_err(c, "error looking up parent directory: %i", ret);
2067 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2068 struct pathbuf_entry *i;
2070 /* XXX print path */
2071 bch_err(c, "directory structure loop");
2073 darray_for_each(*p, i)
2074 pr_err("%llu:%u", i->inum, i->snapshot);
2075 pr_err("%llu:%u", inode->bi_inum, snapshot);
2077 if (!fsck_err(c, "directory structure loop"))
2080 ret = commit_do(trans, NULL, NULL,
2081 BTREE_INSERT_NOFAIL|
2082 BTREE_INSERT_LAZY_RW,
2083 remove_backpointer(trans, inode));
2085 bch_err(c, "error removing dirent: %i", ret);
2089 ret = reattach_inode(trans, inode, snapshot);
2099 * Check for unreachable inodes, as well as loops in the directory structure:
2100 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2103 int bch2_check_directory_structure(struct bch_fs *c)
2105 struct btree_trans trans;
2106 struct btree_iter iter;
2108 struct bch_inode_unpacked u;
2109 pathbuf path = { 0, };
2112 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2114 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2116 BTREE_ITER_PREFETCH|
2117 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2118 if (!bkey_is_inode(k.k))
2121 ret = bch2_inode_unpack(k, &u);
2123 /* Should have been caught earlier in fsck: */
2124 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2128 if (u.bi_flags & BCH_INODE_UNLINKED)
2131 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2135 bch2_trans_iter_exit(&trans, &iter);
2136 bch2_trans_exit(&trans);
2144 /* check_nlink pass: */
2146 struct nlink_table {
2157 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2158 u64 inum, u32 snapshot)
2160 if (t->nr == t->size) {
2161 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2162 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2165 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2167 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2171 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2179 t->d[t->nr++] = (struct nlink) {
2181 .snapshot = snapshot,
2187 static int nlink_cmp(const void *_l, const void *_r)
2189 const struct nlink *l = _l;
2190 const struct nlink *r = _r;
2192 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2195 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2196 struct nlink_table *links,
2197 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2199 struct nlink *link, key = {
2200 .inum = inum, .snapshot = U32_MAX,
2203 if (inum < range_start || inum >= range_end)
2206 link = __inline_bsearch(&key, links->d, links->nr,
2207 sizeof(links->d[0]), nlink_cmp);
2211 while (link > links->d && link[0].inum == link[-1].inum)
2214 for (; link < links->d + links->nr && link->inum == inum; link++)
2215 if (ref_visible(c, s, snapshot, link->snapshot)) {
2217 if (link->snapshot >= snapshot)
2223 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2224 struct nlink_table *t,
2225 u64 start, u64 *end)
2227 struct btree_trans trans;
2228 struct btree_iter iter;
2230 struct bch_inode_unpacked u;
2233 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2235 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2238 BTREE_ITER_PREFETCH|
2239 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2240 if (!bkey_is_inode(k.k))
2243 /* Should never fail, checked by bch2_inode_invalid: */
2244 BUG_ON(bch2_inode_unpack(k, &u));
2247 * Backpointer and directory structure checks are sufficient for
2248 * directories, since they can't have hardlinks:
2250 if (S_ISDIR(u.bi_mode))
2256 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2258 *end = k.k->p.offset;
2264 bch2_trans_iter_exit(&trans, &iter);
2265 bch2_trans_exit(&trans);
2268 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2274 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2275 u64 range_start, u64 range_end)
2277 struct btree_trans trans;
2278 struct snapshots_seen s;
2279 struct btree_iter iter;
2281 struct bkey_s_c_dirent d;
2284 snapshots_seen_init(&s);
2286 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2288 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2290 BTREE_ITER_PREFETCH|
2291 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2292 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2296 switch (k.k->type) {
2297 case KEY_TYPE_dirent:
2298 d = bkey_s_c_to_dirent(k);
2300 if (d.v->d_type != DT_DIR &&
2301 d.v->d_type != DT_SUBVOL)
2302 inc_link(c, &s, links, range_start, range_end,
2303 le64_to_cpu(d.v->d_inum),
2304 bch2_snapshot_equiv(c, d.k->p.snapshot));
2308 bch2_trans_iter_exit(&trans, &iter);
2311 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2313 bch2_trans_exit(&trans);
2314 snapshots_seen_exit(&s);
2318 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2320 struct nlink_table *links,
2321 size_t *idx, u64 range_end)
2323 struct bch_fs *c = trans->c;
2324 struct bch_inode_unpacked u;
2325 struct nlink *link = &links->d[*idx];
2328 if (k.k->p.offset >= range_end)
2331 if (!bkey_is_inode(k.k))
2334 BUG_ON(bch2_inode_unpack(k, &u));
2336 if (S_ISDIR(u.bi_mode))
2342 while ((cmp_int(link->inum, k.k->p.offset) ?:
2343 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2344 BUG_ON(*idx == links->nr);
2345 link = &links->d[++*idx];
2348 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2349 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2350 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2351 bch2_inode_nlink_get(&u), link->count)) {
2352 bch2_inode_nlink_set(&u, link->count);
2353 ret = __write_inode(trans, &u, k.k->p.snapshot);
2360 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2361 struct nlink_table *links,
2362 u64 range_start, u64 range_end)
2364 struct btree_trans trans;
2365 struct btree_iter iter;
2370 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2372 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2373 POS(0, range_start),
2374 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2375 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2376 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2378 bch2_trans_exit(&trans);
2381 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2388 int bch2_check_nlinks(struct bch_fs *c)
2390 struct nlink_table links = { 0 };
2391 u64 this_iter_range_start, next_iter_range_start = 0;
2395 this_iter_range_start = next_iter_range_start;
2396 next_iter_range_start = U64_MAX;
2398 ret = check_nlinks_find_hardlinks(c, &links,
2399 this_iter_range_start,
2400 &next_iter_range_start);
2402 ret = check_nlinks_walk_dirents(c, &links,
2403 this_iter_range_start,
2404 next_iter_range_start);
2408 ret = check_nlinks_update_hardlinks(c, &links,
2409 this_iter_range_start,
2410 next_iter_range_start);
2415 } while (next_iter_range_start != U64_MAX);
2424 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2427 struct bkey_s_c_reflink_p p;
2428 struct bkey_i_reflink_p *u;
2431 if (k.k->type != KEY_TYPE_reflink_p)
2434 p = bkey_s_c_to_reflink_p(k);
2436 if (!p.v->front_pad && !p.v->back_pad)
2439 u = bch2_trans_kmalloc(trans, sizeof(*u));
2440 ret = PTR_ERR_OR_ZERO(u);
2444 bkey_reassemble(&u->k_i, k);
2448 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2451 int bch2_fix_reflink_p(struct bch_fs *c)
2453 struct btree_iter iter;
2457 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2460 ret = bch2_trans_run(c,
2461 for_each_btree_key_commit(&trans, iter,
2462 BTREE_ID_extents, POS_MIN,
2463 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2464 BTREE_ITER_ALL_SNAPSHOTS, k,
2465 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2466 fix_reflink_p_key(&trans, &iter, k)));