1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
10 #include "fs-common.h"
14 #include "subvolume.h"
18 #include <linux/bsearch.h>
19 #include <linux/dcache.h> /* struct qstr */
21 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
24 * XXX: this is handling transaction restarts without returning
25 * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
27 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
30 struct btree_iter iter;
35 for_each_btree_key_upto(trans, iter, BTREE_ID_extents,
36 SPOS(inum, 0, snapshot),
39 if (bkey_extent_is_allocation(k.k))
42 bch2_trans_iter_exit(trans, &iter);
44 return ret ?: sectors;
47 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
50 struct btree_iter iter;
52 struct bkey_s_c_dirent d;
56 for_each_btree_key_upto(trans, iter, BTREE_ID_dirents,
57 SPOS(inum, 0, snapshot),
60 if (k.k->type != KEY_TYPE_dirent)
63 d = bkey_s_c_to_dirent(k);
64 if (d.v->d_type == DT_DIR)
67 bch2_trans_iter_exit(trans, &iter);
69 return ret ?: subdirs;
72 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
75 struct bch_snapshot s;
76 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots,
80 *subvol = le32_to_cpu(s.subvol);
81 else if (bch2_err_matches(ret, ENOENT))
82 bch_err(trans->c, "snapshot %u not fonud", snapshot);
87 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
88 u32 *snapshot, u64 *inum)
90 struct bch_subvolume s;
93 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
95 *snapshot = le32_to_cpu(s.snapshot);
96 *inum = le64_to_cpu(s.inode);
100 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
101 u32 *snapshot, u64 *inum)
103 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
106 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
107 struct bch_inode_unpacked *inode)
109 struct btree_iter iter;
113 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
115 BTREE_ITER_ALL_SNAPSHOTS);
116 k = bch2_btree_iter_peek(&iter);
121 if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
122 ret = -BCH_ERR_ENOENT_inode;
126 ret = bch2_inode_unpack(k, inode);
128 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
129 bch_err(trans->c, "error fetching inode %llu: %s",
130 inode_nr, bch2_err_str(ret));
131 bch2_trans_iter_exit(trans, &iter);
135 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
136 struct bch_inode_unpacked *inode,
139 struct btree_iter iter;
143 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
144 SPOS(0, inode_nr, *snapshot), 0);
149 ret = bkey_is_inode(k.k)
150 ? bch2_inode_unpack(k, inode)
151 : -BCH_ERR_ENOENT_inode;
153 *snapshot = iter.pos.snapshot;
155 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
156 bch_err(trans->c, "error fetching inode %llu:%u: %s",
157 inode_nr, *snapshot, bch2_err_str(ret));
158 bch2_trans_iter_exit(trans, &iter);
162 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
163 struct bch_inode_unpacked *inode,
166 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
169 static int __lookup_dirent(struct btree_trans *trans,
170 struct bch_hash_info hash_info,
171 subvol_inum dir, struct qstr *name,
172 u64 *target, unsigned *type)
174 struct btree_iter iter;
175 struct bkey_s_c_dirent d;
178 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
179 &hash_info, dir, name, 0);
183 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
184 *target = le64_to_cpu(d.v->d_inum);
186 bch2_trans_iter_exit(trans, &iter);
190 static int __write_inode(struct btree_trans *trans,
191 struct bch_inode_unpacked *inode,
194 struct bkey_inode_buf *inode_p =
195 bch2_trans_kmalloc(trans, sizeof(*inode_p));
198 return PTR_ERR(inode_p);
200 bch2_inode_pack(inode_p, inode);
201 inode_p->inode.k.p.snapshot = snapshot;
203 return bch2_btree_insert_nonextent(trans, BTREE_ID_inodes,
205 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
208 static int write_inode(struct btree_trans *trans,
209 struct bch_inode_unpacked *inode,
212 int ret = commit_do(trans, NULL, NULL,
214 BTREE_INSERT_LAZY_RW,
215 __write_inode(trans, inode, snapshot));
217 bch_err(trans->c, "error in fsck: error updating inode: %s",
222 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
224 struct bch_fs *c = trans->c;
225 struct btree_iter iter = { NULL };
226 struct bkey_i_inode_generation delete;
227 struct bch_inode_unpacked inode_u;
232 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
233 SPOS(inum, 0, snapshot),
234 SPOS(inum, U64_MAX, snapshot),
236 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
237 SPOS(inum, 0, snapshot),
238 SPOS(inum, U64_MAX, snapshot),
240 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
241 SPOS(inum, 0, snapshot),
242 SPOS(inum, U64_MAX, snapshot),
244 } while (ret == -BCH_ERR_transaction_restart_nested);
248 bch2_trans_begin(trans);
250 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
251 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
256 if (!bkey_is_inode(k.k)) {
257 bch2_fs_inconsistent(c,
258 "inode %llu:%u not found when deleting",
264 bch2_inode_unpack(k, &inode_u);
266 /* Subvolume root? */
267 if (inode_u.bi_subvol)
268 bch_warn(c, "deleting inode %llu marked as unlinked, but also a subvolume root!?", inode_u.bi_inum);
270 bkey_inode_generation_init(&delete.k_i);
271 delete.k.p = iter.pos;
272 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
274 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
275 bch2_trans_commit(trans, NULL, NULL,
276 BTREE_INSERT_NOFAIL);
278 bch2_trans_iter_exit(trans, &iter);
279 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
282 return ret ?: -BCH_ERR_transaction_restart_nested;
285 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
287 struct bch_fs *c = trans->c;
288 struct btree_iter iter;
289 struct bch_inode_unpacked dir_inode;
290 struct bch_hash_info dir_hash_info;
293 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
297 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
299 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
301 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
302 &dir_hash_info, &iter,
303 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
304 bch2_trans_iter_exit(trans, &iter);
306 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
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);
333 root_hash_info = bch2_hash_info_init(c, &root);
335 ret = __lookup_dirent(trans, root_hash_info, root_inum,
336 &lostfound_str, &inum, &d_type);
337 if (bch2_err_matches(ret, ENOENT)) {
338 bch_notice(c, "creating lost+found");
339 goto create_lostfound;
342 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
343 bch_err(c, "error looking up lost+found: %s", bch2_err_str(ret));
347 if (d_type != DT_DIR) {
348 bch_err(c, "error looking up lost+found: not a directory");
353 * The bch2_check_dirents pass has already run, dangling dirents
354 * shouldn't exist here:
356 return __lookup_inode(trans, inum, lostfound, &snapshot);
359 bch2_inode_init_early(c, lostfound);
361 ret = bch2_create_trans(trans, root_inum, &root,
362 lostfound, &lostfound_str,
363 0, 0, S_IFDIR|0700, 0, NULL, NULL,
364 (subvol_inum) { }, 0);
365 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
366 bch_err(c, "error creating lost+found: %s", bch2_err_str(ret));
370 static int __reattach_inode(struct btree_trans *trans,
371 struct bch_inode_unpacked *inode,
374 struct bch_hash_info dir_hash;
375 struct bch_inode_unpacked lostfound;
382 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
386 ret = lookup_lostfound(trans, subvol, &lostfound);
390 if (S_ISDIR(inode->bi_mode)) {
391 lostfound.bi_nlink++;
393 ret = __write_inode(trans, &lostfound, U32_MAX);
398 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
400 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
401 name = (struct qstr) QSTR(name_buf);
403 ret = bch2_dirent_create(trans,
406 .inum = lostfound.bi_inum,
410 &name, inode->bi_inum, &dir_offset,
411 BCH_HASH_SET_MUST_CREATE);
415 inode->bi_dir = lostfound.bi_inum;
416 inode->bi_dir_offset = dir_offset;
418 return __write_inode(trans, inode, inode_snapshot);
421 static int reattach_inode(struct btree_trans *trans,
422 struct bch_inode_unpacked *inode,
425 int ret = commit_do(trans, NULL, NULL,
426 BTREE_INSERT_LAZY_RW|
428 __reattach_inode(trans, inode, inode_snapshot));
430 bch_err(trans->c, "error reattaching inode %llu: %s",
431 inode->bi_inum, bch2_err_str(ret));
438 static int remove_backpointer(struct btree_trans *trans,
439 struct bch_inode_unpacked *inode)
441 struct btree_iter iter;
442 struct bkey_s_c_dirent d;
445 d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
446 POS(inode->bi_dir, inode->bi_dir_offset), 0,
449 __remove_dirent(trans, d.k->p);
450 bch2_trans_iter_exit(trans, &iter);
454 struct snapshots_seen_entry {
459 struct snapshots_seen {
461 DARRAY(struct snapshots_seen_entry) ids;
464 static inline void snapshots_seen_exit(struct snapshots_seen *s)
466 darray_exit(&s->ids);
469 static inline void snapshots_seen_init(struct snapshots_seen *s)
471 memset(s, 0, sizeof(*s));
474 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
475 enum btree_id btree_id, struct bpos pos)
477 struct snapshots_seen_entry *i, n = {
479 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
483 if (!bkey_eq(s->pos, pos))
487 s->pos.snapshot = n.equiv;
489 darray_for_each(s->ids, i) {
494 * We currently don't rigorously track for snapshot cleanup
495 * needing to be run, so it shouldn't be a fsck error yet:
497 if (i->equiv == n.equiv) {
498 bch_err(c, "snapshot deletion did not finish:\n"
499 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
500 bch2_btree_ids[btree_id],
501 pos.inode, pos.offset,
502 i->id, n.id, n.equiv);
503 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_delete_dead_snapshots);
507 ret = darray_push(&s->ids, n);
509 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
515 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
516 * and @ancestor hasn't been overwritten in @seen
518 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
520 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
521 u32 id, u32 ancestor)
524 u32 top = seen->ids.nr ? seen->ids.data[seen->ids.nr - 1].equiv : 0;
526 EBUG_ON(id > ancestor);
527 EBUG_ON(!bch2_snapshot_is_equiv(c, id));
528 EBUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
530 /* @ancestor should be the snapshot most recently added to @seen */
531 EBUG_ON(ancestor != seen->pos.snapshot);
532 EBUG_ON(ancestor != top);
537 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
541 * We know that @id is a descendant of @ancestor, we're checking if
542 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
543 * @ascestor and with @id as a descendent.
545 * But we already know that we're scanning IDs between @id and @ancestor
546 * numerically, since snapshot ID lists are kept sorted, so if we find
547 * an id that's an ancestor of @id we're done:
550 for (i = seen->ids.nr - 2;
551 i >= 0 && seen->ids.data[i].equiv >= id;
553 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv))
560 * ref_visible - given a key with snapshot id @src that points to a key with
561 * snapshot id @dst, test whether there is some snapshot in which @dst is
564 * This assumes we're visiting @src keys in natural key order.
566 * @s - list of snapshot IDs already seen at @src
567 * @src - snapshot ID of src key
568 * @dst - snapshot ID of dst key
570 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
574 ? key_visible_in_snapshot(c, s, dst, src)
575 : bch2_snapshot_is_ancestor(c, src, dst);
578 static int ref_visible2(struct bch_fs *c,
579 u32 src, struct snapshots_seen *src_seen,
580 u32 dst, struct snapshots_seen *dst_seen)
582 src = bch2_snapshot_equiv(c, src);
583 dst = bch2_snapshot_equiv(c, dst);
587 swap(dst_seen, src_seen);
589 return key_visible_in_snapshot(c, src_seen, dst, src);
592 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
593 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
594 (_i)->snapshot <= (_snapshot); _i++) \
595 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
597 struct inode_walker_entry {
598 struct bch_inode_unpacked inode;
604 struct inode_walker {
605 bool first_this_inode;
606 bool recalculate_sums;
607 struct bpos last_pos;
609 DARRAY(struct inode_walker_entry) inodes;
612 static void inode_walker_exit(struct inode_walker *w)
614 darray_exit(&w->inodes);
617 static struct inode_walker inode_walker_init(void)
619 return (struct inode_walker) { 0, };
622 static int add_inode(struct bch_fs *c, struct inode_walker *w,
623 struct bkey_s_c inode)
625 struct bch_inode_unpacked u;
627 BUG_ON(bch2_inode_unpack(inode, &u));
629 return darray_push(&w->inodes, ((struct inode_walker_entry) {
631 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
635 static int get_inodes_all_snapshots(struct btree_trans *trans,
636 struct inode_walker *w, u64 inum)
638 struct bch_fs *c = trans->c;
639 struct btree_iter iter;
641 u32 restart_count = trans->restart_count;
644 w->recalculate_sums = false;
647 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
648 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
649 if (k.k->p.offset != inum)
652 if (bkey_is_inode(k.k))
655 bch2_trans_iter_exit(trans, &iter);
660 w->first_this_inode = true;
662 if (trans_was_restarted(trans, restart_count))
663 return -BCH_ERR_transaction_restart_nested;
668 static struct inode_walker_entry *
669 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w,
670 u32 snapshot, bool is_whiteout)
672 struct inode_walker_entry *i;
674 snapshot = bch2_snapshot_equiv(c, snapshot);
676 darray_for_each(w->inodes, i)
677 if (bch2_snapshot_is_ancestor(c, snapshot, i->snapshot))
682 BUG_ON(snapshot > i->snapshot);
684 if (snapshot != i->snapshot && !is_whiteout) {
685 struct inode_walker_entry new = *i;
689 new.snapshot = snapshot;
692 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
693 w->last_pos.inode, snapshot, i->snapshot);
695 while (i > w->inodes.data && i[-1].snapshot > snapshot)
698 pos = i - w->inodes.data;
699 ret = darray_insert_item(&w->inodes, pos, new);
703 i = w->inodes.data + pos;
709 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
710 struct inode_walker *w, struct bpos pos,
713 if (w->last_pos.inode != pos.inode) {
714 int ret = get_inodes_all_snapshots(trans, w, pos.inode);
717 } else if (bkey_cmp(w->last_pos, pos)) {
718 struct inode_walker_entry *i;
720 darray_for_each(w->inodes, i)
721 i->seen_this_pos = false;
727 return lookup_inode_for_snapshot(trans->c, w, pos.snapshot, is_whiteout);
730 static int __get_visible_inodes(struct btree_trans *trans,
731 struct inode_walker *w,
732 struct snapshots_seen *s,
735 struct bch_fs *c = trans->c;
736 struct btree_iter iter;
742 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
743 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
744 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
746 if (k.k->p.offset != inum)
749 if (!ref_visible(c, s, s->pos.snapshot, equiv))
752 if (bkey_is_inode(k.k))
755 if (equiv >= s->pos.snapshot)
758 bch2_trans_iter_exit(trans, &iter);
763 static int check_key_has_snapshot(struct btree_trans *trans,
764 struct btree_iter *iter,
767 struct bch_fs *c = trans->c;
768 struct printbuf buf = PRINTBUF;
771 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
772 "key in missing snapshot: %s",
773 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
774 ret = bch2_btree_delete_at(trans, iter,
775 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
781 static int hash_redo_key(struct btree_trans *trans,
782 const struct bch_hash_desc desc,
783 struct bch_hash_info *hash_info,
784 struct btree_iter *k_iter, struct bkey_s_c k)
786 struct bkey_i *delete;
789 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
791 return PTR_ERR(delete);
793 tmp = bch2_bkey_make_mut_noupdate(trans, k);
797 bkey_init(&delete->k);
798 delete->k.p = k_iter->pos;
799 return bch2_btree_iter_traverse(k_iter) ?:
800 bch2_trans_update(trans, k_iter, delete, 0) ?:
801 bch2_hash_set_snapshot(trans, desc, hash_info,
802 (subvol_inum) { 0, k.k->p.inode },
803 k.k->p.snapshot, tmp,
804 BCH_HASH_SET_MUST_CREATE,
805 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
806 bch2_trans_commit(trans, NULL, NULL,
808 BTREE_INSERT_LAZY_RW);
811 static int hash_check_key(struct btree_trans *trans,
812 const struct bch_hash_desc desc,
813 struct bch_hash_info *hash_info,
814 struct btree_iter *k_iter, struct bkey_s_c hash_k)
816 struct bch_fs *c = trans->c;
817 struct btree_iter iter = { NULL };
818 struct printbuf buf = PRINTBUF;
823 if (hash_k.k->type != desc.key_type)
826 hash = desc.hash_bkey(hash_info, hash_k);
828 if (likely(hash == hash_k.k->p.offset))
831 if (hash_k.k->p.offset < hash)
834 for_each_btree_key_norestart(trans, iter, desc.btree_id,
835 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
836 BTREE_ITER_SLOTS, k, ret) {
837 if (bkey_eq(k.k->p, hash_k.k->p))
840 if (fsck_err_on(k.k->type == desc.key_type &&
841 !desc.cmp_bkey(k, hash_k), c,
842 "duplicate hash table keys:\n%s",
843 (printbuf_reset(&buf),
844 bch2_bkey_val_to_text(&buf, c, hash_k),
846 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
850 if (bkey_deleted(k.k)) {
851 bch2_trans_iter_exit(trans, &iter);
856 bch2_trans_iter_exit(trans, &iter);
860 if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
861 bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
862 (printbuf_reset(&buf),
863 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
864 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
865 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
866 bch_err(c, "hash_redo_key err %s", bch2_err_str(ret));
869 ret = -BCH_ERR_transaction_restart_nested;
875 static int check_inode(struct btree_trans *trans,
876 struct btree_iter *iter,
878 struct bch_inode_unpacked *prev,
879 struct snapshots_seen *s,
882 struct bch_fs *c = trans->c;
883 struct bch_inode_unpacked u;
884 bool do_update = false;
887 ret = check_key_has_snapshot(trans, iter, k);
893 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
898 * if snapshot id isn't a leaf node, skip it - deletion in
899 * particular is not atomic, so on the internal snapshot nodes
900 * we can see inodes marked for deletion after a clean shutdown
902 if (bch2_snapshot_is_internal_node(c, k.k->p.snapshot))
905 if (!bkey_is_inode(k.k))
908 BUG_ON(bch2_inode_unpack(k, &u));
911 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
912 BCH_INODE_I_SECTORS_DIRTY|
913 BCH_INODE_UNLINKED)))
916 if (prev->bi_inum != u.bi_inum)
919 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
920 inode_d_type(prev) != inode_d_type(&u), c,
921 "inodes in different snapshots don't match")) {
922 bch_err(c, "repair not implemented yet");
926 if (u.bi_flags & BCH_INODE_UNLINKED &&
928 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
930 bch2_trans_unlock(trans);
933 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
934 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
935 bch_err(c, "error in fsck: error while deleting inode: %s",
940 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
942 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
944 bch_verbose(c, "truncating inode %llu", u.bi_inum);
946 bch2_trans_unlock(trans);
950 * XXX: need to truncate partial blocks too here - or ideally
951 * just switch units to bytes and that issue goes away
953 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
954 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
956 POS(u.bi_inum, U64_MAX),
958 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
959 bch_err(c, "error in fsck: error truncating inode: %s",
965 * We truncated without our normal sector accounting hook, just
966 * make sure we recalculate it:
968 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
970 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
974 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
976 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
980 bch_verbose(c, "recounting sectors for inode %llu",
983 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
985 bch_err(c, "error in fsck: error recounting inode sectors: %s",
986 bch2_err_str(sectors));
990 u.bi_sectors = sectors;
991 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
995 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
998 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
1003 ret = __write_inode(trans, &u, iter->pos.snapshot);
1005 bch_err(c, "error in fsck: error updating inode: %s",
1016 int bch2_check_inodes(struct bch_fs *c)
1018 bool full = c->opts.fsck;
1019 struct btree_trans trans;
1020 struct btree_iter iter;
1021 struct bch_inode_unpacked prev = { 0 };
1022 struct snapshots_seen s;
1026 snapshots_seen_init(&s);
1027 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1029 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
1031 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1032 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1033 check_inode(&trans, &iter, k, &prev, &s, full));
1035 bch2_trans_exit(&trans);
1036 snapshots_seen_exit(&s);
1042 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1043 struct btree_iter *iter,
1046 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
1049 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1050 struct bkey_s_c_dirent d)
1052 return inode->bi_dir == d.k->p.inode &&
1053 inode->bi_dir_offset == d.k->p.offset;
1056 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1057 struct bch_inode_unpacked *inode)
1059 return d.v->d_type == DT_SUBVOL
1060 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1061 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1064 static int inode_backpointer_exists(struct btree_trans *trans,
1065 struct bch_inode_unpacked *inode,
1068 struct btree_iter iter;
1069 struct bkey_s_c_dirent d;
1072 d = dirent_get_by_pos(trans, &iter,
1073 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1076 return bch2_err_matches(ret, ENOENT) ? 0 : ret;
1078 ret = dirent_points_to_inode(d, inode);
1079 bch2_trans_iter_exit(trans, &iter);
1083 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1085 struct bch_fs *c = trans->c;
1086 struct inode_walker_entry *i;
1087 u32 restart_count = trans->restart_count;
1091 darray_for_each(w->inodes, i) {
1092 if (i->inode.bi_sectors == i->count)
1095 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1097 if (w->recalculate_sums)
1100 if (i->count != count2) {
1101 bch_err(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1102 w->last_pos.inode, i->snapshot, i->count, count2);
1103 return -BCH_ERR_internal_fsck_err;
1106 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1107 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1108 w->last_pos.inode, i->snapshot,
1109 i->inode.bi_sectors, i->count)) {
1110 i->inode.bi_sectors = i->count;
1111 ret = write_inode(trans, &i->inode, i->snapshot);
1119 if (!ret && trans_was_restarted(trans, restart_count))
1120 ret = -BCH_ERR_transaction_restart_nested;
1127 struct snapshots_seen seen;
1130 struct extent_ends {
1131 struct bpos last_pos;
1132 DARRAY(struct extent_end) e;
1135 static void extent_ends_reset(struct extent_ends *extent_ends)
1137 struct extent_end *i;
1139 darray_for_each(extent_ends->e, i)
1140 snapshots_seen_exit(&i->seen);
1142 extent_ends->e.nr = 0;
1145 static void extent_ends_exit(struct extent_ends *extent_ends)
1147 extent_ends_reset(extent_ends);
1148 darray_exit(&extent_ends->e);
1151 static void extent_ends_init(struct extent_ends *extent_ends)
1153 memset(extent_ends, 0, sizeof(*extent_ends));
1156 static int extent_ends_at(struct bch_fs *c,
1157 struct extent_ends *extent_ends,
1158 struct snapshots_seen *seen,
1161 struct extent_end *i, n = (struct extent_end) {
1162 .offset = k.k->p.offset,
1163 .snapshot = k.k->p.snapshot,
1167 n.seen.ids.data = kmemdup(seen->ids.data,
1168 sizeof(seen->ids.data[0]) * seen->ids.size,
1170 if (!n.seen.ids.data)
1171 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1173 darray_for_each(extent_ends->e, i) {
1174 if (i->snapshot == k.k->p.snapshot) {
1175 snapshots_seen_exit(&i->seen);
1180 if (i->snapshot >= k.k->p.snapshot)
1184 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1187 static int overlapping_extents_found(struct btree_trans *trans,
1188 enum btree_id btree,
1189 struct bpos pos1, struct bkey pos2,
1192 struct bch_fs *c = trans->c;
1193 struct printbuf buf = PRINTBUF;
1194 struct btree_iter iter;
1196 u32 snapshot = min(pos1.snapshot, pos2.p.snapshot);
1199 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1201 prt_str(&buf, "\n ");
1202 bch2_bpos_to_text(&buf, pos1);
1203 prt_str(&buf, "\n ");
1205 bch2_bkey_to_text(&buf, &pos2);
1206 prt_str(&buf, "\n ");
1208 bch2_trans_iter_init(trans, &iter, btree, SPOS(pos1.inode, pos1.offset - 1, snapshot), 0);
1209 k = bch2_btree_iter_peek_upto(&iter, POS(pos1.inode, U64_MAX));
1214 bch2_bkey_val_to_text(&buf, c, k);
1216 if (!bpos_eq(pos1, k.k->p)) {
1217 bch_err(c, "%s: error finding first overlapping extent when repairing%s",
1219 ret = -BCH_ERR_internal_fsck_err;
1224 bch2_btree_iter_advance(&iter);
1226 k = bch2_btree_iter_peek_upto(&iter, POS(pos1.inode, U64_MAX));
1231 if (bkey_ge(k.k->p, pos2.p))
1236 prt_str(&buf, "\n ");
1237 bch2_bkey_val_to_text(&buf, c, k);
1239 if (bkey_gt(k.k->p, pos2.p) ||
1240 pos2.size != k.k->size) {
1241 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1243 ret = -BCH_ERR_internal_fsck_err;
1247 if (fsck_err(c, "overlapping extents%s", buf.buf)) {
1248 struct bpos update_pos = pos1.snapshot < pos2.p.snapshot ? pos1 : pos2.p;
1249 struct btree_iter update_iter;
1251 struct bkey_i *update = bch2_bkey_get_mut(trans, &update_iter,
1253 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1254 bch2_trans_iter_exit(trans, &update_iter);
1255 if ((ret = PTR_ERR_OR_ZERO(update)))
1262 bch2_trans_iter_exit(trans, &iter);
1263 printbuf_exit(&buf);
1267 static int check_overlapping_extents(struct btree_trans *trans,
1268 struct snapshots_seen *seen,
1269 struct extent_ends *extent_ends,
1272 struct btree_iter *iter)
1274 struct bch_fs *c = trans->c;
1275 struct extent_end *i;
1279 /* transaction restart, running again */
1280 if (bpos_eq(extent_ends->last_pos, k.k->p))
1283 if (extent_ends->last_pos.inode != k.k->p.inode)
1284 extent_ends_reset(extent_ends);
1286 darray_for_each(extent_ends->e, i) {
1287 if (i->offset <= bkey_start_offset(k.k))
1290 if (!ref_visible2(c,
1291 k.k->p.snapshot, seen,
1292 i->snapshot, &i->seen))
1295 ret = overlapping_extents_found(trans, iter->btree_id,
1296 SPOS(iter->pos.inode,
1304 ret = extent_ends_at(c, extent_ends, seen, k);
1308 extent_ends->last_pos = k.k->p;
1310 return ret ?: fixed;
1313 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1315 struct inode_walker *inode,
1316 struct snapshots_seen *s,
1317 struct extent_ends *extent_ends)
1319 struct bch_fs *c = trans->c;
1320 struct inode_walker_entry *i;
1321 struct printbuf buf = PRINTBUF;
1322 struct bpos equiv = k.k->p;
1325 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1327 ret = check_key_has_snapshot(trans, iter, k);
1329 ret = ret < 0 ? ret : 0;
1333 if (inode->last_pos.inode != k.k->p.inode) {
1334 ret = check_i_sectors(trans, inode);
1339 i = walk_inode(trans, inode, equiv, k.k->type == KEY_TYPE_whiteout);
1340 ret = PTR_ERR_OR_ZERO(i);
1344 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1348 if (k.k->type != KEY_TYPE_whiteout) {
1349 if (fsck_err_on(!i, c,
1350 "extent in missing inode:\n %s",
1351 (printbuf_reset(&buf),
1352 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1355 if (fsck_err_on(i &&
1356 !S_ISREG(i->inode.bi_mode) &&
1357 !S_ISLNK(i->inode.bi_mode), c,
1358 "extent in non regular inode mode %o:\n %s",
1360 (printbuf_reset(&buf),
1361 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1364 ret = check_overlapping_extents(trans, s, extent_ends, k,
1365 equiv.snapshot, iter);
1370 inode->recalculate_sums = true;
1375 * Check inodes in reverse order, from oldest snapshots to newest,
1376 * starting from the inode that matches this extent's snapshot. If we
1377 * didn't have one, iterate over all inodes:
1380 i = inode->inodes.data + inode->inodes.nr - 1;
1383 inode->inodes.data && i >= inode->inodes.data;
1385 if (i->snapshot > equiv.snapshot ||
1386 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1389 if (k.k->type != KEY_TYPE_whiteout) {
1390 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1391 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1392 !bkey_extent_is_reservation(k), c,
1393 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1394 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1395 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1396 struct btree_iter iter2;
1398 bch2_trans_copy_iter(&iter2, iter);
1399 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1400 ret = bch2_btree_iter_traverse(&iter2) ?:
1401 bch2_btree_delete_at(trans, &iter2,
1402 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1403 bch2_trans_iter_exit(trans, &iter2);
1407 iter->k.type = KEY_TYPE_whiteout;
1410 if (bkey_extent_is_allocation(k.k))
1411 i->count += k.k->size;
1414 i->seen_this_pos = true;
1419 printbuf_exit(&buf);
1421 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1425 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1430 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1431 * that i_size an i_sectors are consistent
1433 int bch2_check_extents(struct bch_fs *c)
1435 struct inode_walker w = inode_walker_init();
1436 struct snapshots_seen s;
1437 struct btree_trans trans;
1438 struct btree_iter iter;
1440 struct extent_ends extent_ends;
1441 struct disk_reservation res = { 0 };
1444 snapshots_seen_init(&s);
1445 extent_ends_init(&extent_ends);
1446 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1448 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1449 POS(BCACHEFS_ROOT_INO, 0),
1450 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1452 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
1453 bch2_disk_reservation_put(c, &res);
1454 check_extent(&trans, &iter, k, &w, &s, &extent_ends);
1456 check_i_sectors(&trans, &w);
1458 bch2_disk_reservation_put(c, &res);
1459 extent_ends_exit(&extent_ends);
1460 inode_walker_exit(&w);
1461 bch2_trans_exit(&trans);
1462 snapshots_seen_exit(&s);
1469 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1471 struct bch_fs *c = trans->c;
1472 struct inode_walker_entry *i;
1473 u32 restart_count = trans->restart_count;
1477 darray_for_each(w->inodes, i) {
1478 if (i->inode.bi_nlink == i->count)
1481 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1485 if (i->count != count2) {
1486 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1489 if (i->inode.bi_nlink == i->count)
1493 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1494 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1495 w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1496 i->inode.bi_nlink = i->count;
1497 ret = write_inode(trans, &i->inode, i->snapshot);
1505 if (!ret && trans_was_restarted(trans, restart_count))
1506 ret = -BCH_ERR_transaction_restart_nested;
1510 static int check_dirent_target(struct btree_trans *trans,
1511 struct btree_iter *iter,
1512 struct bkey_s_c_dirent d,
1513 struct bch_inode_unpacked *target,
1514 u32 target_snapshot)
1516 struct bch_fs *c = trans->c;
1517 struct bkey_i_dirent *n;
1518 bool backpointer_exists = true;
1519 struct printbuf buf = PRINTBUF;
1522 if (!target->bi_dir &&
1523 !target->bi_dir_offset) {
1524 target->bi_dir = d.k->p.inode;
1525 target->bi_dir_offset = d.k->p.offset;
1527 ret = __write_inode(trans, target, target_snapshot);
1532 if (!inode_points_to_dirent(target, d)) {
1533 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1537 backpointer_exists = ret;
1540 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1541 backpointer_exists, c,
1542 "directory %llu with multiple links",
1544 ret = __remove_dirent(trans, d.k->p);
1548 if (fsck_err_on(backpointer_exists &&
1549 !target->bi_nlink, c,
1550 "inode %llu type %s has multiple links but i_nlink 0",
1551 target->bi_inum, bch2_d_types[d.v->d_type])) {
1553 target->bi_flags &= ~BCH_INODE_UNLINKED;
1555 ret = __write_inode(trans, target, target_snapshot);
1560 if (fsck_err_on(!backpointer_exists, c,
1561 "inode %llu:%u has wrong backpointer:\n"
1563 "should be %llu:%llu",
1564 target->bi_inum, target_snapshot,
1566 target->bi_dir_offset,
1569 target->bi_dir = d.k->p.inode;
1570 target->bi_dir_offset = d.k->p.offset;
1572 ret = __write_inode(trans, target, target_snapshot);
1578 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1579 "incorrect d_type: got %s, should be %s:\n%s",
1580 bch2_d_type_str(d.v->d_type),
1581 bch2_d_type_str(inode_d_type(target)),
1582 (printbuf_reset(&buf),
1583 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1584 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1585 ret = PTR_ERR_OR_ZERO(n);
1589 bkey_reassemble(&n->k_i, d.s_c);
1590 n->v.d_type = inode_d_type(target);
1592 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1596 d = dirent_i_to_s_c(n);
1599 if (d.v->d_type == DT_SUBVOL &&
1600 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1601 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1602 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1603 le32_to_cpu(d.v->d_parent_subvol),
1604 target->bi_parent_subvol))) {
1605 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1606 ret = PTR_ERR_OR_ZERO(n);
1610 bkey_reassemble(&n->k_i, d.s_c);
1611 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1613 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1617 d = dirent_i_to_s_c(n);
1622 printbuf_exit(&buf);
1624 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1629 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1631 struct bch_hash_info *hash_info,
1632 struct inode_walker *dir,
1633 struct inode_walker *target,
1634 struct snapshots_seen *s)
1636 struct bch_fs *c = trans->c;
1637 struct bkey_s_c_dirent d;
1638 struct inode_walker_entry *i;
1639 struct printbuf buf = PRINTBUF;
1643 ret = check_key_has_snapshot(trans, iter, k);
1645 ret = ret < 0 ? ret : 0;
1650 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1652 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1656 if (k.k->type == KEY_TYPE_whiteout)
1659 if (dir->last_pos.inode != k.k->p.inode) {
1660 ret = check_subdir_count(trans, dir);
1665 BUG_ON(!iter->path->should_be_locked);
1667 i = walk_inode(trans, dir, equiv, k.k->type == KEY_TYPE_whiteout);
1668 ret = PTR_ERR_OR_ZERO(i);
1672 if (dir->first_this_inode)
1673 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1674 dir->first_this_inode = false;
1676 if (fsck_err_on(!i, c,
1677 "dirent in nonexisting directory:\n%s",
1678 (printbuf_reset(&buf),
1679 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1680 ret = bch2_btree_delete_at(trans, iter,
1681 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1688 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1689 "dirent in non directory inode type %s:\n%s",
1690 bch2_d_type_str(inode_d_type(&i->inode)),
1691 (printbuf_reset(&buf),
1692 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1693 ret = bch2_btree_delete_at(trans, iter, 0);
1697 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1701 /* dirent has been deleted */
1706 if (k.k->type != KEY_TYPE_dirent)
1709 d = bkey_s_c_to_dirent(k);
1711 if (d.v->d_type == DT_SUBVOL) {
1712 struct bch_inode_unpacked subvol_root;
1713 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1714 u32 target_snapshot;
1717 ret = __subvol_lookup(trans, target_subvol,
1718 &target_snapshot, &target_inum);
1719 if (ret && !bch2_err_matches(ret, ENOENT))
1722 if (fsck_err_on(ret, c,
1723 "dirent points to missing subvolume %u",
1724 le32_to_cpu(d.v->d_child_subvol))) {
1725 ret = __remove_dirent(trans, d.k->p);
1729 ret = __lookup_inode(trans, target_inum,
1730 &subvol_root, &target_snapshot);
1731 if (ret && !bch2_err_matches(ret, ENOENT))
1734 if (fsck_err_on(ret, c,
1735 "subvolume %u points to missing subvolume root %llu",
1738 bch_err(c, "repair not implemented yet");
1743 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1744 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1746 subvol_root.bi_subvol, target_subvol)) {
1747 subvol_root.bi_subvol = target_subvol;
1748 ret = __write_inode(trans, &subvol_root, target_snapshot);
1753 ret = check_dirent_target(trans, iter, d, &subvol_root,
1758 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1762 if (fsck_err_on(!target->inodes.nr, c,
1763 "dirent points to missing inode: (equiv %u)\n%s",
1765 (printbuf_reset(&buf),
1766 bch2_bkey_val_to_text(&buf, c, k),
1768 ret = __remove_dirent(trans, d.k->p);
1773 darray_for_each(target->inodes, i) {
1774 ret = check_dirent_target(trans, iter, d,
1775 &i->inode, i->snapshot);
1781 if (d.v->d_type == DT_DIR)
1782 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1788 printbuf_exit(&buf);
1790 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1796 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1799 int bch2_check_dirents(struct bch_fs *c)
1801 struct inode_walker dir = inode_walker_init();
1802 struct inode_walker target = inode_walker_init();
1803 struct snapshots_seen s;
1804 struct bch_hash_info hash_info;
1805 struct btree_trans trans;
1806 struct btree_iter iter;
1810 snapshots_seen_init(&s);
1811 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1813 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1814 POS(BCACHEFS_ROOT_INO, 0),
1815 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1818 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1819 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1821 bch2_trans_exit(&trans);
1822 snapshots_seen_exit(&s);
1823 inode_walker_exit(&dir);
1824 inode_walker_exit(&target);
1831 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1833 struct bch_hash_info *hash_info,
1834 struct inode_walker *inode)
1836 struct bch_fs *c = trans->c;
1837 struct inode_walker_entry *i;
1840 ret = check_key_has_snapshot(trans, iter, k);
1844 i = walk_inode(trans, inode, k.k->p, k.k->type == KEY_TYPE_whiteout);
1845 ret = PTR_ERR_OR_ZERO(i);
1849 if (inode->first_this_inode)
1850 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1851 inode->first_this_inode = false;
1853 if (fsck_err_on(!i, c,
1854 "xattr for missing inode %llu",
1856 return bch2_btree_delete_at(trans, iter, 0);
1861 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1863 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1869 * Walk xattrs: verify that they all have a corresponding inode
1871 int bch2_check_xattrs(struct bch_fs *c)
1873 struct inode_walker inode = inode_walker_init();
1874 struct bch_hash_info hash_info;
1875 struct btree_trans trans;
1876 struct btree_iter iter;
1880 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1882 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1883 POS(BCACHEFS_ROOT_INO, 0),
1884 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1887 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1888 check_xattr(&trans, &iter, k, &hash_info, &inode));
1890 bch2_trans_exit(&trans);
1897 static int check_root_trans(struct btree_trans *trans)
1899 struct bch_fs *c = trans->c;
1900 struct bch_inode_unpacked root_inode;
1905 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1906 if (ret && !bch2_err_matches(ret, ENOENT))
1909 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1910 struct bkey_i_subvolume root_subvol;
1913 inum = BCACHEFS_ROOT_INO;
1915 bkey_subvolume_init(&root_subvol.k_i);
1916 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1917 root_subvol.v.flags = 0;
1918 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1919 root_subvol.v.inode = cpu_to_le64(inum);
1920 ret = commit_do(trans, NULL, NULL,
1921 BTREE_INSERT_NOFAIL|
1922 BTREE_INSERT_LAZY_RW,
1923 __bch2_btree_insert(trans, BTREE_ID_subvolumes,
1924 &root_subvol.k_i, 0));
1926 bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1932 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1933 if (ret && !bch2_err_matches(ret, ENOENT))
1936 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1937 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1938 "root inode not a directory")) {
1939 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1941 root_inode.bi_inum = inum;
1943 ret = __write_inode(trans, &root_inode, snapshot);
1945 bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1952 /* Get root directory, create if it doesn't exist: */
1953 int bch2_check_root(struct bch_fs *c)
1957 ret = bch2_trans_do(c, NULL, NULL,
1958 BTREE_INSERT_NOFAIL|
1959 BTREE_INSERT_LAZY_RW,
1960 check_root_trans(&trans));
1967 struct pathbuf_entry {
1972 typedef DARRAY(struct pathbuf_entry) pathbuf;
1974 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1976 struct pathbuf_entry *i;
1978 darray_for_each(*p, i)
1979 if (i->inum == inum &&
1980 i->snapshot == snapshot)
1986 static int path_down(struct bch_fs *c, pathbuf *p,
1987 u64 inum, u32 snapshot)
1989 int ret = darray_push(p, ((struct pathbuf_entry) {
1991 .snapshot = snapshot,
1995 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
2001 * Check that a given inode is reachable from the root:
2003 * XXX: we should also be verifying that inodes are in the right subvolumes
2005 static int check_path(struct btree_trans *trans,
2007 struct bch_inode_unpacked *inode,
2010 struct bch_fs *c = trans->c;
2013 snapshot = bch2_snapshot_equiv(c, snapshot);
2016 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
2017 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
2018 struct btree_iter dirent_iter;
2019 struct bkey_s_c_dirent d;
2020 u32 parent_snapshot = snapshot;
2022 if (inode->bi_subvol) {
2025 ret = subvol_lookup(trans, inode->bi_parent_subvol,
2026 &parent_snapshot, &inum);
2031 ret = lockrestart_do(trans,
2032 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
2033 SPOS(inode->bi_dir, inode->bi_dir_offset,
2034 parent_snapshot))).k));
2035 if (ret && !bch2_err_matches(ret, ENOENT))
2038 if (!ret && !dirent_points_to_inode(d, inode)) {
2039 bch2_trans_iter_exit(trans, &dirent_iter);
2040 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
2043 if (bch2_err_matches(ret, ENOENT)) {
2044 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2045 inode->bi_inum, snapshot,
2046 bch2_d_type_str(inode_d_type(inode)),
2049 inode->bi_dir_offset))
2050 ret = reattach_inode(trans, inode, snapshot);
2054 bch2_trans_iter_exit(trans, &dirent_iter);
2056 if (!S_ISDIR(inode->bi_mode))
2059 ret = path_down(c, p, inode->bi_inum, snapshot);
2061 bch_err(c, "memory allocation failure");
2065 snapshot = parent_snapshot;
2067 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2069 /* Should have been caught in dirents pass */
2070 bch_err(c, "error looking up parent directory: %i", ret);
2074 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2075 struct pathbuf_entry *i;
2077 /* XXX print path */
2078 bch_err(c, "directory structure loop");
2080 darray_for_each(*p, i)
2081 pr_err("%llu:%u", i->inum, i->snapshot);
2082 pr_err("%llu:%u", inode->bi_inum, snapshot);
2084 if (!fsck_err(c, "directory structure loop"))
2087 ret = commit_do(trans, NULL, NULL,
2088 BTREE_INSERT_NOFAIL|
2089 BTREE_INSERT_LAZY_RW,
2090 remove_backpointer(trans, inode));
2092 bch_err(c, "error removing dirent: %i", ret);
2096 ret = reattach_inode(trans, inode, snapshot);
2106 * Check for unreachable inodes, as well as loops in the directory structure:
2107 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2110 int bch2_check_directory_structure(struct bch_fs *c)
2112 struct btree_trans trans;
2113 struct btree_iter iter;
2115 struct bch_inode_unpacked u;
2116 pathbuf path = { 0, };
2119 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2121 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2123 BTREE_ITER_PREFETCH|
2124 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2125 if (!bkey_is_inode(k.k))
2128 ret = bch2_inode_unpack(k, &u);
2130 /* Should have been caught earlier in fsck: */
2131 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2135 if (u.bi_flags & BCH_INODE_UNLINKED)
2138 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2142 bch2_trans_iter_exit(&trans, &iter);
2143 bch2_trans_exit(&trans);
2151 /* check_nlink pass: */
2153 struct nlink_table {
2164 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2165 u64 inum, u32 snapshot)
2167 if (t->nr == t->size) {
2168 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2169 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2172 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2174 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2178 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2186 t->d[t->nr++] = (struct nlink) {
2188 .snapshot = snapshot,
2194 static int nlink_cmp(const void *_l, const void *_r)
2196 const struct nlink *l = _l;
2197 const struct nlink *r = _r;
2199 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2202 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2203 struct nlink_table *links,
2204 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2206 struct nlink *link, key = {
2207 .inum = inum, .snapshot = U32_MAX,
2210 if (inum < range_start || inum >= range_end)
2213 link = __inline_bsearch(&key, links->d, links->nr,
2214 sizeof(links->d[0]), nlink_cmp);
2218 while (link > links->d && link[0].inum == link[-1].inum)
2221 for (; link < links->d + links->nr && link->inum == inum; link++)
2222 if (ref_visible(c, s, snapshot, link->snapshot)) {
2224 if (link->snapshot >= snapshot)
2230 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2231 struct nlink_table *t,
2232 u64 start, u64 *end)
2234 struct btree_trans trans;
2235 struct btree_iter iter;
2237 struct bch_inode_unpacked u;
2240 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2242 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2245 BTREE_ITER_PREFETCH|
2246 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2247 if (!bkey_is_inode(k.k))
2250 /* Should never fail, checked by bch2_inode_invalid: */
2251 BUG_ON(bch2_inode_unpack(k, &u));
2254 * Backpointer and directory structure checks are sufficient for
2255 * directories, since they can't have hardlinks:
2257 if (S_ISDIR(u.bi_mode))
2263 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2265 *end = k.k->p.offset;
2271 bch2_trans_iter_exit(&trans, &iter);
2272 bch2_trans_exit(&trans);
2275 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2281 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2282 u64 range_start, u64 range_end)
2284 struct btree_trans trans;
2285 struct snapshots_seen s;
2286 struct btree_iter iter;
2288 struct bkey_s_c_dirent d;
2291 snapshots_seen_init(&s);
2293 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2295 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2297 BTREE_ITER_PREFETCH|
2298 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2299 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2303 switch (k.k->type) {
2304 case KEY_TYPE_dirent:
2305 d = bkey_s_c_to_dirent(k);
2307 if (d.v->d_type != DT_DIR &&
2308 d.v->d_type != DT_SUBVOL)
2309 inc_link(c, &s, links, range_start, range_end,
2310 le64_to_cpu(d.v->d_inum),
2311 bch2_snapshot_equiv(c, d.k->p.snapshot));
2315 bch2_trans_iter_exit(&trans, &iter);
2318 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2320 bch2_trans_exit(&trans);
2321 snapshots_seen_exit(&s);
2325 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2327 struct nlink_table *links,
2328 size_t *idx, u64 range_end)
2330 struct bch_fs *c = trans->c;
2331 struct bch_inode_unpacked u;
2332 struct nlink *link = &links->d[*idx];
2335 if (k.k->p.offset >= range_end)
2338 if (!bkey_is_inode(k.k))
2341 BUG_ON(bch2_inode_unpack(k, &u));
2343 if (S_ISDIR(u.bi_mode))
2349 while ((cmp_int(link->inum, k.k->p.offset) ?:
2350 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2351 BUG_ON(*idx == links->nr);
2352 link = &links->d[++*idx];
2355 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2356 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2357 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2358 bch2_inode_nlink_get(&u), link->count)) {
2359 bch2_inode_nlink_set(&u, link->count);
2360 ret = __write_inode(trans, &u, k.k->p.snapshot);
2367 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2368 struct nlink_table *links,
2369 u64 range_start, u64 range_end)
2371 struct btree_trans trans;
2372 struct btree_iter iter;
2377 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2379 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2380 POS(0, range_start),
2381 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2382 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2383 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2385 bch2_trans_exit(&trans);
2388 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2395 int bch2_check_nlinks(struct bch_fs *c)
2397 struct nlink_table links = { 0 };
2398 u64 this_iter_range_start, next_iter_range_start = 0;
2402 this_iter_range_start = next_iter_range_start;
2403 next_iter_range_start = U64_MAX;
2405 ret = check_nlinks_find_hardlinks(c, &links,
2406 this_iter_range_start,
2407 &next_iter_range_start);
2409 ret = check_nlinks_walk_dirents(c, &links,
2410 this_iter_range_start,
2411 next_iter_range_start);
2415 ret = check_nlinks_update_hardlinks(c, &links,
2416 this_iter_range_start,
2417 next_iter_range_start);
2422 } while (next_iter_range_start != U64_MAX);
2431 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2434 struct bkey_s_c_reflink_p p;
2435 struct bkey_i_reflink_p *u;
2438 if (k.k->type != KEY_TYPE_reflink_p)
2441 p = bkey_s_c_to_reflink_p(k);
2443 if (!p.v->front_pad && !p.v->back_pad)
2446 u = bch2_trans_kmalloc(trans, sizeof(*u));
2447 ret = PTR_ERR_OR_ZERO(u);
2451 bkey_reassemble(&u->k_i, k);
2455 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2458 int bch2_fix_reflink_p(struct bch_fs *c)
2460 struct btree_iter iter;
2464 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2467 ret = bch2_trans_run(c,
2468 for_each_btree_key_commit(&trans, iter,
2469 BTREE_ID_extents, POS_MIN,
2470 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2471 BTREE_ITER_ALL_SNAPSHOTS, k,
2472 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2473 fix_reflink_p_key(&trans, &iter, k)));