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 __remove_dirent(struct btree_trans *trans, struct bpos pos)
224 struct bch_fs *c = trans->c;
225 struct btree_iter iter;
226 struct bch_inode_unpacked dir_inode;
227 struct bch_hash_info dir_hash_info;
230 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
234 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
236 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
238 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
239 &dir_hash_info, &iter,
240 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
241 bch2_trans_iter_exit(trans, &iter);
243 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
248 /* Get lost+found, create if it doesn't exist: */
249 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
250 struct bch_inode_unpacked *lostfound)
252 struct bch_fs *c = trans->c;
253 struct bch_inode_unpacked root;
254 struct bch_hash_info root_hash_info;
255 struct qstr lostfound_str = QSTR("lost+found");
256 subvol_inum root_inum = { .subvol = subvol };
262 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
266 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
270 root_hash_info = bch2_hash_info_init(c, &root);
272 ret = __lookup_dirent(trans, root_hash_info, root_inum,
273 &lostfound_str, &inum, &d_type);
274 if (bch2_err_matches(ret, ENOENT)) {
275 bch_notice(c, "creating lost+found");
276 goto create_lostfound;
279 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
280 bch_err(c, "error looking up lost+found: %s", bch2_err_str(ret));
284 if (d_type != DT_DIR) {
285 bch_err(c, "error looking up lost+found: not a directory");
290 * The bch2_check_dirents pass has already run, dangling dirents
291 * shouldn't exist here:
293 return __lookup_inode(trans, inum, lostfound, &snapshot);
296 bch2_inode_init_early(c, lostfound);
298 ret = bch2_create_trans(trans, root_inum, &root,
299 lostfound, &lostfound_str,
300 0, 0, S_IFDIR|0700, 0, NULL, NULL,
301 (subvol_inum) { }, 0);
302 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
303 bch_err(c, "error creating lost+found: %s", bch2_err_str(ret));
307 static int __reattach_inode(struct btree_trans *trans,
308 struct bch_inode_unpacked *inode,
311 struct bch_hash_info dir_hash;
312 struct bch_inode_unpacked lostfound;
319 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
323 ret = lookup_lostfound(trans, subvol, &lostfound);
327 if (S_ISDIR(inode->bi_mode)) {
328 lostfound.bi_nlink++;
330 ret = __write_inode(trans, &lostfound, U32_MAX);
335 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
337 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
338 name = (struct qstr) QSTR(name_buf);
340 ret = bch2_dirent_create(trans,
343 .inum = lostfound.bi_inum,
347 &name, inode->bi_inum, &dir_offset,
348 BCH_HASH_SET_MUST_CREATE);
352 inode->bi_dir = lostfound.bi_inum;
353 inode->bi_dir_offset = dir_offset;
355 return __write_inode(trans, inode, inode_snapshot);
358 static int reattach_inode(struct btree_trans *trans,
359 struct bch_inode_unpacked *inode,
362 int ret = commit_do(trans, NULL, NULL,
363 BTREE_INSERT_LAZY_RW|
365 __reattach_inode(trans, inode, inode_snapshot));
367 bch_err(trans->c, "error reattaching inode %llu: %s",
368 inode->bi_inum, bch2_err_str(ret));
375 static int remove_backpointer(struct btree_trans *trans,
376 struct bch_inode_unpacked *inode)
378 struct btree_iter iter;
379 struct bkey_s_c_dirent d;
382 d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
383 POS(inode->bi_dir, inode->bi_dir_offset), 0,
386 __remove_dirent(trans, d.k->p);
387 bch2_trans_iter_exit(trans, &iter);
391 struct snapshots_seen_entry {
396 struct snapshots_seen {
398 DARRAY(struct snapshots_seen_entry) ids;
401 static inline void snapshots_seen_exit(struct snapshots_seen *s)
403 darray_exit(&s->ids);
406 static inline void snapshots_seen_init(struct snapshots_seen *s)
408 memset(s, 0, sizeof(*s));
411 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
412 enum btree_id btree_id, struct bpos pos)
414 struct snapshots_seen_entry *i, n = {
416 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
420 if (!bkey_eq(s->pos, pos))
424 s->pos.snapshot = n.equiv;
426 darray_for_each(s->ids, i) {
431 * We currently don't rigorously track for snapshot cleanup
432 * needing to be run, so it shouldn't be a fsck error yet:
434 if (i->equiv == n.equiv) {
435 bch_err(c, "snapshot deletion did not finish:\n"
436 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
437 bch2_btree_ids[btree_id],
438 pos.inode, pos.offset,
439 i->id, n.id, n.equiv);
440 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_delete_dead_snapshots);
444 ret = darray_push(&s->ids, n);
446 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
452 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
453 * and @ancestor hasn't been overwritten in @seen
455 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
457 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
458 u32 id, u32 ancestor)
462 EBUG_ON(id > ancestor);
463 EBUG_ON(!bch2_snapshot_is_equiv(c, id));
464 EBUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
466 /* @ancestor should be the snapshot most recently added to @seen */
467 EBUG_ON(ancestor != seen->pos.snapshot);
468 EBUG_ON(ancestor != seen->ids.data[seen->ids.nr - 1].equiv);
473 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
477 * We know that @id is a descendant of @ancestor, we're checking if
478 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
479 * @ascestor and with @id as a descendent.
481 * But we already know that we're scanning IDs between @id and @ancestor
482 * numerically, since snapshot ID lists are kept sorted, so if we find
483 * an id that's an ancestor of @id we're done:
486 for (i = seen->ids.nr - 2;
487 i >= 0 && seen->ids.data[i].equiv >= id;
489 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv))
496 * ref_visible - given a key with snapshot id @src that points to a key with
497 * snapshot id @dst, test whether there is some snapshot in which @dst is
500 * This assumes we're visiting @src keys in natural key order.
502 * @s - list of snapshot IDs already seen at @src
503 * @src - snapshot ID of src key
504 * @dst - snapshot ID of dst key
506 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
510 ? key_visible_in_snapshot(c, s, dst, src)
511 : bch2_snapshot_is_ancestor(c, src, dst);
514 static int ref_visible2(struct bch_fs *c,
515 u32 src, struct snapshots_seen *src_seen,
516 u32 dst, struct snapshots_seen *dst_seen)
518 src = bch2_snapshot_equiv(c, src);
519 dst = bch2_snapshot_equiv(c, dst);
523 swap(dst_seen, src_seen);
525 return key_visible_in_snapshot(c, src_seen, dst, src);
528 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
529 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
530 (_i)->snapshot <= (_snapshot); _i++) \
531 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
533 struct inode_walker_entry {
534 struct bch_inode_unpacked inode;
540 struct inode_walker {
541 bool first_this_inode;
542 bool recalculate_sums;
543 struct bpos last_pos;
545 DARRAY(struct inode_walker_entry) inodes;
548 static void inode_walker_exit(struct inode_walker *w)
550 darray_exit(&w->inodes);
553 static struct inode_walker inode_walker_init(void)
555 return (struct inode_walker) { 0, };
558 static int add_inode(struct bch_fs *c, struct inode_walker *w,
559 struct bkey_s_c inode)
561 struct bch_inode_unpacked u;
563 BUG_ON(bch2_inode_unpack(inode, &u));
565 return darray_push(&w->inodes, ((struct inode_walker_entry) {
567 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
571 static int get_inodes_all_snapshots(struct btree_trans *trans,
572 struct inode_walker *w, u64 inum)
574 struct bch_fs *c = trans->c;
575 struct btree_iter iter;
577 u32 restart_count = trans->restart_count;
580 w->recalculate_sums = false;
583 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
584 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
585 if (k.k->p.offset != inum)
588 if (bkey_is_inode(k.k))
591 bch2_trans_iter_exit(trans, &iter);
596 w->first_this_inode = true;
598 if (trans_was_restarted(trans, restart_count))
599 return -BCH_ERR_transaction_restart_nested;
604 static struct inode_walker_entry *
605 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w,
606 u32 snapshot, bool is_whiteout)
608 struct inode_walker_entry *i;
610 snapshot = bch2_snapshot_equiv(c, snapshot);
612 darray_for_each(w->inodes, i)
613 if (bch2_snapshot_is_ancestor(c, snapshot, i->snapshot))
618 BUG_ON(snapshot > i->snapshot);
620 if (snapshot != i->snapshot && !is_whiteout) {
621 struct inode_walker_entry new = *i;
625 new.snapshot = snapshot;
628 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
629 w->last_pos.inode, snapshot, i->snapshot);
631 while (i > w->inodes.data && i[-1].snapshot > snapshot)
634 pos = i - w->inodes.data;
635 ret = darray_insert_item(&w->inodes, pos, new);
639 i = w->inodes.data + pos;
645 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
646 struct inode_walker *w, struct bpos pos,
649 if (w->last_pos.inode != pos.inode) {
650 int ret = get_inodes_all_snapshots(trans, w, pos.inode);
653 } else if (bkey_cmp(w->last_pos, pos)) {
654 struct inode_walker_entry *i;
656 darray_for_each(w->inodes, i)
657 i->seen_this_pos = false;
663 return lookup_inode_for_snapshot(trans->c, w, pos.snapshot, is_whiteout);
666 static int __get_visible_inodes(struct btree_trans *trans,
667 struct inode_walker *w,
668 struct snapshots_seen *s,
671 struct bch_fs *c = trans->c;
672 struct btree_iter iter;
678 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
679 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
680 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
682 if (k.k->p.offset != inum)
685 if (!ref_visible(c, s, s->pos.snapshot, equiv))
688 if (bkey_is_inode(k.k))
691 if (equiv >= s->pos.snapshot)
694 bch2_trans_iter_exit(trans, &iter);
699 static int check_key_has_snapshot(struct btree_trans *trans,
700 struct btree_iter *iter,
703 struct bch_fs *c = trans->c;
704 struct printbuf buf = PRINTBUF;
707 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
708 "key in missing snapshot: %s",
709 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
710 ret = bch2_btree_delete_at(trans, iter,
711 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
717 static int hash_redo_key(struct btree_trans *trans,
718 const struct bch_hash_desc desc,
719 struct bch_hash_info *hash_info,
720 struct btree_iter *k_iter, struct bkey_s_c k)
722 struct bkey_i *delete;
725 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
727 return PTR_ERR(delete);
729 tmp = bch2_bkey_make_mut_noupdate(trans, k);
733 bkey_init(&delete->k);
734 delete->k.p = k_iter->pos;
735 return bch2_btree_iter_traverse(k_iter) ?:
736 bch2_trans_update(trans, k_iter, delete, 0) ?:
737 bch2_hash_set_snapshot(trans, desc, hash_info,
738 (subvol_inum) { 0, k.k->p.inode },
739 k.k->p.snapshot, tmp,
740 BCH_HASH_SET_MUST_CREATE,
741 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
742 bch2_trans_commit(trans, NULL, NULL,
744 BTREE_INSERT_LAZY_RW);
747 static int hash_check_key(struct btree_trans *trans,
748 const struct bch_hash_desc desc,
749 struct bch_hash_info *hash_info,
750 struct btree_iter *k_iter, struct bkey_s_c hash_k)
752 struct bch_fs *c = trans->c;
753 struct btree_iter iter = { NULL };
754 struct printbuf buf = PRINTBUF;
759 if (hash_k.k->type != desc.key_type)
762 hash = desc.hash_bkey(hash_info, hash_k);
764 if (likely(hash == hash_k.k->p.offset))
767 if (hash_k.k->p.offset < hash)
770 for_each_btree_key_norestart(trans, iter, desc.btree_id,
771 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
772 BTREE_ITER_SLOTS, k, ret) {
773 if (bkey_eq(k.k->p, hash_k.k->p))
776 if (fsck_err_on(k.k->type == desc.key_type &&
777 !desc.cmp_bkey(k, hash_k), c,
778 "duplicate hash table keys:\n%s",
779 (printbuf_reset(&buf),
780 bch2_bkey_val_to_text(&buf, c, hash_k),
782 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
786 if (bkey_deleted(k.k)) {
787 bch2_trans_iter_exit(trans, &iter);
792 bch2_trans_iter_exit(trans, &iter);
796 if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
797 bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
798 (printbuf_reset(&buf),
799 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
800 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
801 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
802 bch_err(c, "hash_redo_key err %s", bch2_err_str(ret));
805 ret = -BCH_ERR_transaction_restart_nested;
811 static int check_inode(struct btree_trans *trans,
812 struct btree_iter *iter,
814 struct bch_inode_unpacked *prev,
815 struct snapshots_seen *s,
818 struct bch_fs *c = trans->c;
819 struct bch_inode_unpacked u;
820 bool do_update = false;
823 ret = check_key_has_snapshot(trans, iter, k);
829 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
834 * if snapshot id isn't a leaf node, skip it - deletion in
835 * particular is not atomic, so on the internal snapshot nodes
836 * we can see inodes marked for deletion after a clean shutdown
838 if (bch2_snapshot_is_internal_node(c, k.k->p.snapshot))
841 if (!bkey_is_inode(k.k))
844 BUG_ON(bch2_inode_unpack(k, &u));
847 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
848 BCH_INODE_I_SECTORS_DIRTY|
849 BCH_INODE_UNLINKED)))
852 if (prev->bi_inum != u.bi_inum)
855 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
856 inode_d_type(prev) != inode_d_type(&u), c,
857 "inodes in different snapshots don't match")) {
858 bch_err(c, "repair not implemented yet");
862 if (u.bi_flags & BCH_INODE_UNLINKED &&
864 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
866 bch2_trans_unlock(trans);
869 ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
870 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
871 bch_err(c, "error in fsck: error while deleting inode: %s",
876 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
878 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
880 bch_verbose(c, "truncating inode %llu", u.bi_inum);
882 bch2_trans_unlock(trans);
886 * XXX: need to truncate partial blocks too here - or ideally
887 * just switch units to bytes and that issue goes away
889 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
890 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
892 POS(u.bi_inum, U64_MAX),
894 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
895 bch_err(c, "error in fsck: error truncating inode: %s",
901 * We truncated without our normal sector accounting hook, just
902 * make sure we recalculate it:
904 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
906 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
910 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
912 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
916 bch_verbose(c, "recounting sectors for inode %llu",
919 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
921 bch_err(c, "error in fsck: error recounting inode sectors: %s",
922 bch2_err_str(sectors));
926 u.bi_sectors = sectors;
927 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
931 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
934 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
939 ret = __write_inode(trans, &u, iter->pos.snapshot);
941 bch_err(c, "error in fsck: error updating inode: %s",
952 int bch2_check_inodes(struct bch_fs *c)
954 bool full = c->opts.fsck;
955 struct btree_trans trans;
956 struct btree_iter iter;
957 struct bch_inode_unpacked prev = { 0 };
958 struct snapshots_seen s;
962 snapshots_seen_init(&s);
963 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
965 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
967 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
968 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
969 check_inode(&trans, &iter, k, &prev, &s, full));
971 bch2_trans_exit(&trans);
972 snapshots_seen_exit(&s);
978 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
979 struct btree_iter *iter,
982 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
985 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
986 struct bkey_s_c_dirent d)
988 return inode->bi_dir == d.k->p.inode &&
989 inode->bi_dir_offset == d.k->p.offset;
992 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
993 struct bch_inode_unpacked *inode)
995 return d.v->d_type == DT_SUBVOL
996 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
997 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1000 static int inode_backpointer_exists(struct btree_trans *trans,
1001 struct bch_inode_unpacked *inode,
1004 struct btree_iter iter;
1005 struct bkey_s_c_dirent d;
1008 d = dirent_get_by_pos(trans, &iter,
1009 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1012 return bch2_err_matches(ret, ENOENT) ? 0 : ret;
1014 ret = dirent_points_to_inode(d, inode);
1015 bch2_trans_iter_exit(trans, &iter);
1019 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1021 struct bch_fs *c = trans->c;
1022 struct inode_walker_entry *i;
1023 u32 restart_count = trans->restart_count;
1027 darray_for_each(w->inodes, i) {
1028 if (i->inode.bi_sectors == i->count)
1031 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1033 if (w->recalculate_sums)
1036 if (i->count != count2) {
1037 bch_err(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1038 w->last_pos.inode, i->snapshot, i->count, count2);
1039 return -BCH_ERR_internal_fsck_err;
1042 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1043 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1044 w->last_pos.inode, i->snapshot,
1045 i->inode.bi_sectors, i->count)) {
1046 i->inode.bi_sectors = i->count;
1047 ret = write_inode(trans, &i->inode, i->snapshot);
1055 if (!ret && trans_was_restarted(trans, restart_count))
1056 ret = -BCH_ERR_transaction_restart_nested;
1063 struct snapshots_seen seen;
1066 struct extent_ends {
1067 struct bpos last_pos;
1068 DARRAY(struct extent_end) e;
1071 static void extent_ends_reset(struct extent_ends *extent_ends)
1073 struct extent_end *i;
1075 darray_for_each(extent_ends->e, i)
1076 snapshots_seen_exit(&i->seen);
1078 extent_ends->e.nr = 0;
1081 static void extent_ends_exit(struct extent_ends *extent_ends)
1083 extent_ends_reset(extent_ends);
1084 darray_exit(&extent_ends->e);
1087 static void extent_ends_init(struct extent_ends *extent_ends)
1089 memset(extent_ends, 0, sizeof(*extent_ends));
1092 static int extent_ends_at(struct bch_fs *c,
1093 struct extent_ends *extent_ends,
1094 struct snapshots_seen *seen,
1097 struct extent_end *i, n = (struct extent_end) {
1098 .offset = k.k->p.offset,
1099 .snapshot = k.k->p.snapshot,
1103 n.seen.ids.data = kmemdup(seen->ids.data,
1104 sizeof(seen->ids.data[0]) * seen->ids.size,
1106 if (!n.seen.ids.data)
1107 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1109 darray_for_each(extent_ends->e, i) {
1110 if (i->snapshot == k.k->p.snapshot) {
1111 snapshots_seen_exit(&i->seen);
1116 if (i->snapshot >= k.k->p.snapshot)
1120 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1123 static int overlapping_extents_found(struct btree_trans *trans,
1124 enum btree_id btree,
1125 struct bpos pos1, struct bkey pos2,
1128 struct bch_fs *c = trans->c;
1129 struct printbuf buf = PRINTBUF;
1130 struct btree_iter iter;
1132 u32 snapshot = min(pos1.snapshot, pos2.p.snapshot);
1135 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1137 bch2_trans_iter_init(trans, &iter, btree, SPOS(pos1.inode, pos1.offset - 1, snapshot), 0);
1138 k = bch2_btree_iter_peek_upto(&iter, POS(pos1.inode, U64_MAX));
1143 prt_str(&buf, "\n ");
1144 bch2_bkey_val_to_text(&buf, c, k);
1146 if (!bpos_eq(pos1, k.k->p)) {
1147 bch_err(c, "%s: error finding first overlapping extent when repairing%s",
1149 ret = -BCH_ERR_internal_fsck_err;
1154 bch2_btree_iter_advance(&iter);
1156 k = bch2_btree_iter_peek_upto(&iter, POS(pos1.inode, U64_MAX));
1161 if (bkey_ge(k.k->p, pos2.p))
1166 prt_str(&buf, "\n ");
1167 bch2_bkey_val_to_text(&buf, c, k);
1169 if (bkey_gt(k.k->p, pos2.p) ||
1170 pos2.size != k.k->size) {
1171 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1173 ret = -BCH_ERR_internal_fsck_err;
1177 if (fsck_err(c, "overlapping extents%s", buf.buf)) {
1178 struct bpos update_pos = pos1.snapshot < pos2.p.snapshot ? pos1 : pos2.p;
1179 struct btree_iter update_iter;
1181 struct bkey_i *update = bch2_bkey_get_mut(trans, &update_iter,
1183 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1184 bch2_trans_iter_exit(trans, &update_iter);
1185 if ((ret = PTR_ERR_OR_ZERO(update)))
1192 bch2_trans_iter_exit(trans, &iter);
1193 printbuf_exit(&buf);
1197 static int check_overlapping_extents(struct btree_trans *trans,
1198 struct snapshots_seen *seen,
1199 struct extent_ends *extent_ends,
1202 struct btree_iter *iter)
1204 struct bch_fs *c = trans->c;
1205 struct extent_end *i;
1209 /* transaction restart, running again */
1210 if (bpos_eq(extent_ends->last_pos, k.k->p))
1213 if (extent_ends->last_pos.inode != k.k->p.inode)
1214 extent_ends_reset(extent_ends);
1216 darray_for_each(extent_ends->e, i) {
1217 if (i->offset <= bkey_start_offset(k.k))
1220 if (!ref_visible2(c,
1221 k.k->p.snapshot, seen,
1222 i->snapshot, &i->seen))
1225 ret = overlapping_extents_found(trans, iter->btree_id,
1226 SPOS(iter->pos.inode,
1234 ret = extent_ends_at(c, extent_ends, seen, k);
1238 extent_ends->last_pos = k.k->p;
1240 return ret ?: fixed;
1243 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1245 struct inode_walker *inode,
1246 struct snapshots_seen *s,
1247 struct extent_ends *extent_ends)
1249 struct bch_fs *c = trans->c;
1250 struct inode_walker_entry *i;
1251 struct printbuf buf = PRINTBUF;
1252 struct bpos equiv = k.k->p;
1255 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1257 ret = check_key_has_snapshot(trans, iter, k);
1259 ret = ret < 0 ? ret : 0;
1263 if (inode->last_pos.inode != k.k->p.inode) {
1264 ret = check_i_sectors(trans, inode);
1269 i = walk_inode(trans, inode, equiv, k.k->type == KEY_TYPE_whiteout);
1270 ret = PTR_ERR_OR_ZERO(i);
1274 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1278 if (k.k->type != KEY_TYPE_whiteout) {
1279 if (fsck_err_on(!i, c,
1280 "extent in missing inode:\n %s",
1281 (printbuf_reset(&buf),
1282 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1285 if (fsck_err_on(i &&
1286 !S_ISREG(i->inode.bi_mode) &&
1287 !S_ISLNK(i->inode.bi_mode), c,
1288 "extent in non regular inode mode %o:\n %s",
1290 (printbuf_reset(&buf),
1291 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1294 ret = check_overlapping_extents(trans, s, extent_ends, k,
1295 equiv.snapshot, iter);
1300 inode->recalculate_sums = true;
1305 * Check inodes in reverse order, from oldest snapshots to newest,
1306 * starting from the inode that matches this extent's snapshot. If we
1307 * didn't have one, iterate over all inodes:
1310 i = inode->inodes.data + inode->inodes.nr - 1;
1313 inode->inodes.data && i >= inode->inodes.data;
1315 if (i->snapshot > equiv.snapshot ||
1316 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1319 if (k.k->type != KEY_TYPE_whiteout) {
1320 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1321 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1322 !bkey_extent_is_reservation(k), c,
1323 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1324 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1325 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1326 struct btree_iter iter2;
1328 bch2_trans_copy_iter(&iter2, iter);
1329 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1330 ret = bch2_btree_iter_traverse(&iter2) ?:
1331 bch2_btree_delete_at(trans, &iter2,
1332 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1333 bch2_trans_iter_exit(trans, &iter2);
1337 iter->k.type = KEY_TYPE_whiteout;
1340 if (bkey_extent_is_allocation(k.k))
1341 i->count += k.k->size;
1344 i->seen_this_pos = true;
1349 printbuf_exit(&buf);
1351 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1355 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1360 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1361 * that i_size an i_sectors are consistent
1363 int bch2_check_extents(struct bch_fs *c)
1365 struct inode_walker w = inode_walker_init();
1366 struct snapshots_seen s;
1367 struct btree_trans trans;
1368 struct btree_iter iter;
1370 struct extent_ends extent_ends;
1371 struct disk_reservation res = { 0 };
1374 snapshots_seen_init(&s);
1375 extent_ends_init(&extent_ends);
1376 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1378 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1379 POS(BCACHEFS_ROOT_INO, 0),
1380 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1382 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
1383 bch2_disk_reservation_put(c, &res);
1384 check_extent(&trans, &iter, k, &w, &s, &extent_ends);
1386 check_i_sectors(&trans, &w);
1388 bch2_disk_reservation_put(c, &res);
1389 extent_ends_exit(&extent_ends);
1390 inode_walker_exit(&w);
1391 bch2_trans_exit(&trans);
1392 snapshots_seen_exit(&s);
1399 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1401 struct bch_fs *c = trans->c;
1402 struct inode_walker_entry *i;
1403 u32 restart_count = trans->restart_count;
1407 darray_for_each(w->inodes, i) {
1408 if (i->inode.bi_nlink == i->count)
1411 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1415 if (i->count != count2) {
1416 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1419 if (i->inode.bi_nlink == i->count)
1423 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1424 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1425 w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1426 i->inode.bi_nlink = i->count;
1427 ret = write_inode(trans, &i->inode, i->snapshot);
1435 if (!ret && trans_was_restarted(trans, restart_count))
1436 ret = -BCH_ERR_transaction_restart_nested;
1440 static int check_dirent_target(struct btree_trans *trans,
1441 struct btree_iter *iter,
1442 struct bkey_s_c_dirent d,
1443 struct bch_inode_unpacked *target,
1444 u32 target_snapshot)
1446 struct bch_fs *c = trans->c;
1447 struct bkey_i_dirent *n;
1448 bool backpointer_exists = true;
1449 struct printbuf buf = PRINTBUF;
1452 if (!target->bi_dir &&
1453 !target->bi_dir_offset) {
1454 target->bi_dir = d.k->p.inode;
1455 target->bi_dir_offset = d.k->p.offset;
1457 ret = __write_inode(trans, target, target_snapshot);
1462 if (!inode_points_to_dirent(target, d)) {
1463 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1467 backpointer_exists = ret;
1470 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1471 backpointer_exists, c,
1472 "directory %llu with multiple links",
1474 ret = __remove_dirent(trans, d.k->p);
1478 if (fsck_err_on(backpointer_exists &&
1479 !target->bi_nlink, c,
1480 "inode %llu type %s has multiple links but i_nlink 0",
1481 target->bi_inum, bch2_d_types[d.v->d_type])) {
1483 target->bi_flags &= ~BCH_INODE_UNLINKED;
1485 ret = __write_inode(trans, target, target_snapshot);
1490 if (fsck_err_on(!backpointer_exists, c,
1491 "inode %llu:%u has wrong backpointer:\n"
1493 "should be %llu:%llu",
1494 target->bi_inum, target_snapshot,
1496 target->bi_dir_offset,
1499 target->bi_dir = d.k->p.inode;
1500 target->bi_dir_offset = d.k->p.offset;
1502 ret = __write_inode(trans, target, target_snapshot);
1508 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1509 "incorrect d_type: got %s, should be %s:\n%s",
1510 bch2_d_type_str(d.v->d_type),
1511 bch2_d_type_str(inode_d_type(target)),
1512 (printbuf_reset(&buf),
1513 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1514 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1515 ret = PTR_ERR_OR_ZERO(n);
1519 bkey_reassemble(&n->k_i, d.s_c);
1520 n->v.d_type = inode_d_type(target);
1522 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1526 d = dirent_i_to_s_c(n);
1529 if (d.v->d_type == DT_SUBVOL &&
1530 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1531 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1532 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1533 le32_to_cpu(d.v->d_parent_subvol),
1534 target->bi_parent_subvol))) {
1535 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1536 ret = PTR_ERR_OR_ZERO(n);
1540 bkey_reassemble(&n->k_i, d.s_c);
1541 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1543 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1547 d = dirent_i_to_s_c(n);
1552 printbuf_exit(&buf);
1554 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1559 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1561 struct bch_hash_info *hash_info,
1562 struct inode_walker *dir,
1563 struct inode_walker *target,
1564 struct snapshots_seen *s)
1566 struct bch_fs *c = trans->c;
1567 struct bkey_s_c_dirent d;
1568 struct inode_walker_entry *i;
1569 struct printbuf buf = PRINTBUF;
1573 ret = check_key_has_snapshot(trans, iter, k);
1575 ret = ret < 0 ? ret : 0;
1580 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1582 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1586 if (k.k->type == KEY_TYPE_whiteout)
1589 if (dir->last_pos.inode != k.k->p.inode) {
1590 ret = check_subdir_count(trans, dir);
1595 BUG_ON(!iter->path->should_be_locked);
1597 i = walk_inode(trans, dir, equiv, k.k->type == KEY_TYPE_whiteout);
1598 ret = PTR_ERR_OR_ZERO(i);
1602 if (dir->first_this_inode && dir->inodes.nr)
1603 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1604 dir->first_this_inode = false;
1606 if (fsck_err_on(!i, c,
1607 "dirent in nonexisting directory:\n%s",
1608 (printbuf_reset(&buf),
1609 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1610 ret = bch2_btree_delete_at(trans, iter,
1611 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1618 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1619 "dirent in non directory inode type %s:\n%s",
1620 bch2_d_type_str(inode_d_type(&i->inode)),
1621 (printbuf_reset(&buf),
1622 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1623 ret = bch2_btree_delete_at(trans, iter, 0);
1627 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1631 /* dirent has been deleted */
1636 if (k.k->type != KEY_TYPE_dirent)
1639 d = bkey_s_c_to_dirent(k);
1641 if (d.v->d_type == DT_SUBVOL) {
1642 struct bch_inode_unpacked subvol_root;
1643 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1644 u32 target_snapshot;
1647 ret = __subvol_lookup(trans, target_subvol,
1648 &target_snapshot, &target_inum);
1649 if (ret && !bch2_err_matches(ret, ENOENT))
1652 if (fsck_err_on(ret, c,
1653 "dirent points to missing subvolume %u",
1654 le32_to_cpu(d.v->d_child_subvol))) {
1655 ret = __remove_dirent(trans, d.k->p);
1659 ret = __lookup_inode(trans, target_inum,
1660 &subvol_root, &target_snapshot);
1661 if (ret && !bch2_err_matches(ret, ENOENT))
1664 if (fsck_err_on(ret, c,
1665 "subvolume %u points to missing subvolume root %llu",
1668 bch_err(c, "repair not implemented yet");
1673 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1674 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1676 subvol_root.bi_subvol, target_subvol)) {
1677 subvol_root.bi_subvol = target_subvol;
1678 ret = __write_inode(trans, &subvol_root, target_snapshot);
1683 ret = check_dirent_target(trans, iter, d, &subvol_root,
1688 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1692 if (fsck_err_on(!target->inodes.nr, c,
1693 "dirent points to missing inode: (equiv %u)\n%s",
1695 (printbuf_reset(&buf),
1696 bch2_bkey_val_to_text(&buf, c, k),
1698 ret = __remove_dirent(trans, d.k->p);
1703 darray_for_each(target->inodes, i) {
1704 ret = check_dirent_target(trans, iter, d,
1705 &i->inode, i->snapshot);
1711 if (d.v->d_type == DT_DIR)
1712 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1718 printbuf_exit(&buf);
1720 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1726 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1729 int bch2_check_dirents(struct bch_fs *c)
1731 struct inode_walker dir = inode_walker_init();
1732 struct inode_walker target = inode_walker_init();
1733 struct snapshots_seen s;
1734 struct bch_hash_info hash_info;
1735 struct btree_trans trans;
1736 struct btree_iter iter;
1740 snapshots_seen_init(&s);
1741 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1743 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1744 POS(BCACHEFS_ROOT_INO, 0),
1745 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1748 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1749 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1751 bch2_trans_exit(&trans);
1752 snapshots_seen_exit(&s);
1753 inode_walker_exit(&dir);
1754 inode_walker_exit(&target);
1761 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1763 struct bch_hash_info *hash_info,
1764 struct inode_walker *inode)
1766 struct bch_fs *c = trans->c;
1767 struct inode_walker_entry *i;
1770 ret = check_key_has_snapshot(trans, iter, k);
1774 i = walk_inode(trans, inode, k.k->p, k.k->type == KEY_TYPE_whiteout);
1775 ret = PTR_ERR_OR_ZERO(i);
1779 if (inode->first_this_inode && inode->inodes.nr)
1780 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1781 inode->first_this_inode = false;
1783 if (fsck_err_on(!i, c,
1784 "xattr for missing inode %llu",
1786 return bch2_btree_delete_at(trans, iter, 0);
1791 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1793 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1799 * Walk xattrs: verify that they all have a corresponding inode
1801 int bch2_check_xattrs(struct bch_fs *c)
1803 struct inode_walker inode = inode_walker_init();
1804 struct bch_hash_info hash_info;
1805 struct btree_trans trans;
1806 struct btree_iter iter;
1810 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1812 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1813 POS(BCACHEFS_ROOT_INO, 0),
1814 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1817 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1818 check_xattr(&trans, &iter, k, &hash_info, &inode));
1820 bch2_trans_exit(&trans);
1827 static int check_root_trans(struct btree_trans *trans)
1829 struct bch_fs *c = trans->c;
1830 struct bch_inode_unpacked root_inode;
1835 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1836 if (ret && !bch2_err_matches(ret, ENOENT))
1839 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1840 struct bkey_i_subvolume root_subvol;
1843 inum = BCACHEFS_ROOT_INO;
1845 bkey_subvolume_init(&root_subvol.k_i);
1846 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1847 root_subvol.v.flags = 0;
1848 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1849 root_subvol.v.inode = cpu_to_le64(inum);
1850 ret = commit_do(trans, NULL, NULL,
1851 BTREE_INSERT_NOFAIL|
1852 BTREE_INSERT_LAZY_RW,
1853 __bch2_btree_insert(trans, BTREE_ID_subvolumes,
1854 &root_subvol.k_i, 0));
1856 bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1862 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1863 if (ret && !bch2_err_matches(ret, ENOENT))
1866 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1867 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1868 "root inode not a directory")) {
1869 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1871 root_inode.bi_inum = inum;
1873 ret = __write_inode(trans, &root_inode, snapshot);
1875 bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1882 /* Get root directory, create if it doesn't exist: */
1883 int bch2_check_root(struct bch_fs *c)
1887 ret = bch2_trans_do(c, NULL, NULL,
1888 BTREE_INSERT_NOFAIL|
1889 BTREE_INSERT_LAZY_RW,
1890 check_root_trans(&trans));
1897 struct pathbuf_entry {
1902 typedef DARRAY(struct pathbuf_entry) pathbuf;
1904 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1906 struct pathbuf_entry *i;
1908 darray_for_each(*p, i)
1909 if (i->inum == inum &&
1910 i->snapshot == snapshot)
1916 static int path_down(struct bch_fs *c, pathbuf *p,
1917 u64 inum, u32 snapshot)
1919 int ret = darray_push(p, ((struct pathbuf_entry) {
1921 .snapshot = snapshot,
1925 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1931 * Check that a given inode is reachable from the root:
1933 * XXX: we should also be verifying that inodes are in the right subvolumes
1935 static int check_path(struct btree_trans *trans,
1937 struct bch_inode_unpacked *inode,
1940 struct bch_fs *c = trans->c;
1943 snapshot = bch2_snapshot_equiv(c, snapshot);
1946 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1947 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1948 struct btree_iter dirent_iter;
1949 struct bkey_s_c_dirent d;
1950 u32 parent_snapshot = snapshot;
1952 if (inode->bi_subvol) {
1955 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1956 &parent_snapshot, &inum);
1961 ret = lockrestart_do(trans,
1962 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
1963 SPOS(inode->bi_dir, inode->bi_dir_offset,
1964 parent_snapshot))).k));
1965 if (ret && !bch2_err_matches(ret, ENOENT))
1968 if (!ret && !dirent_points_to_inode(d, inode)) {
1969 bch2_trans_iter_exit(trans, &dirent_iter);
1970 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
1973 if (bch2_err_matches(ret, ENOENT)) {
1974 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
1975 inode->bi_inum, snapshot,
1976 bch2_d_type_str(inode_d_type(inode)),
1979 inode->bi_dir_offset))
1980 ret = reattach_inode(trans, inode, snapshot);
1984 bch2_trans_iter_exit(trans, &dirent_iter);
1986 if (!S_ISDIR(inode->bi_mode))
1989 ret = path_down(c, p, inode->bi_inum, snapshot);
1991 bch_err(c, "memory allocation failure");
1995 snapshot = parent_snapshot;
1997 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1999 /* Should have been caught in dirents pass */
2000 bch_err(c, "error looking up parent directory: %i", ret);
2004 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2005 struct pathbuf_entry *i;
2007 /* XXX print path */
2008 bch_err(c, "directory structure loop");
2010 darray_for_each(*p, i)
2011 pr_err("%llu:%u", i->inum, i->snapshot);
2012 pr_err("%llu:%u", inode->bi_inum, snapshot);
2014 if (!fsck_err(c, "directory structure loop"))
2017 ret = commit_do(trans, NULL, NULL,
2018 BTREE_INSERT_NOFAIL|
2019 BTREE_INSERT_LAZY_RW,
2020 remove_backpointer(trans, inode));
2022 bch_err(c, "error removing dirent: %i", ret);
2026 ret = reattach_inode(trans, inode, snapshot);
2036 * Check for unreachable inodes, as well as loops in the directory structure:
2037 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2040 int bch2_check_directory_structure(struct bch_fs *c)
2042 struct btree_trans trans;
2043 struct btree_iter iter;
2045 struct bch_inode_unpacked u;
2046 pathbuf path = { 0, };
2049 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2051 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2053 BTREE_ITER_PREFETCH|
2054 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2055 if (!bkey_is_inode(k.k))
2058 ret = bch2_inode_unpack(k, &u);
2060 /* Should have been caught earlier in fsck: */
2061 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2065 if (u.bi_flags & BCH_INODE_UNLINKED)
2068 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2072 bch2_trans_iter_exit(&trans, &iter);
2073 bch2_trans_exit(&trans);
2081 /* check_nlink pass: */
2083 struct nlink_table {
2094 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2095 u64 inum, u32 snapshot)
2097 if (t->nr == t->size) {
2098 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2099 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2102 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2104 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2108 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2116 t->d[t->nr++] = (struct nlink) {
2118 .snapshot = snapshot,
2124 static int nlink_cmp(const void *_l, const void *_r)
2126 const struct nlink *l = _l;
2127 const struct nlink *r = _r;
2129 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2132 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2133 struct nlink_table *links,
2134 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2136 struct nlink *link, key = {
2137 .inum = inum, .snapshot = U32_MAX,
2140 if (inum < range_start || inum >= range_end)
2143 link = __inline_bsearch(&key, links->d, links->nr,
2144 sizeof(links->d[0]), nlink_cmp);
2148 while (link > links->d && link[0].inum == link[-1].inum)
2151 for (; link < links->d + links->nr && link->inum == inum; link++)
2152 if (ref_visible(c, s, snapshot, link->snapshot)) {
2154 if (link->snapshot >= snapshot)
2160 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2161 struct nlink_table *t,
2162 u64 start, u64 *end)
2164 struct btree_trans trans;
2165 struct btree_iter iter;
2167 struct bch_inode_unpacked u;
2170 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2172 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2175 BTREE_ITER_PREFETCH|
2176 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2177 if (!bkey_is_inode(k.k))
2180 /* Should never fail, checked by bch2_inode_invalid: */
2181 BUG_ON(bch2_inode_unpack(k, &u));
2184 * Backpointer and directory structure checks are sufficient for
2185 * directories, since they can't have hardlinks:
2187 if (S_ISDIR(u.bi_mode))
2193 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2195 *end = k.k->p.offset;
2201 bch2_trans_iter_exit(&trans, &iter);
2202 bch2_trans_exit(&trans);
2205 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2211 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2212 u64 range_start, u64 range_end)
2214 struct btree_trans trans;
2215 struct snapshots_seen s;
2216 struct btree_iter iter;
2218 struct bkey_s_c_dirent d;
2221 snapshots_seen_init(&s);
2223 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2225 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2227 BTREE_ITER_PREFETCH|
2228 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2229 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2233 switch (k.k->type) {
2234 case KEY_TYPE_dirent:
2235 d = bkey_s_c_to_dirent(k);
2237 if (d.v->d_type != DT_DIR &&
2238 d.v->d_type != DT_SUBVOL)
2239 inc_link(c, &s, links, range_start, range_end,
2240 le64_to_cpu(d.v->d_inum),
2241 bch2_snapshot_equiv(c, d.k->p.snapshot));
2245 bch2_trans_iter_exit(&trans, &iter);
2248 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2250 bch2_trans_exit(&trans);
2251 snapshots_seen_exit(&s);
2255 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2257 struct nlink_table *links,
2258 size_t *idx, u64 range_end)
2260 struct bch_fs *c = trans->c;
2261 struct bch_inode_unpacked u;
2262 struct nlink *link = &links->d[*idx];
2265 if (k.k->p.offset >= range_end)
2268 if (!bkey_is_inode(k.k))
2271 BUG_ON(bch2_inode_unpack(k, &u));
2273 if (S_ISDIR(u.bi_mode))
2279 while ((cmp_int(link->inum, k.k->p.offset) ?:
2280 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2281 BUG_ON(*idx == links->nr);
2282 link = &links->d[++*idx];
2285 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2286 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2287 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2288 bch2_inode_nlink_get(&u), link->count)) {
2289 bch2_inode_nlink_set(&u, link->count);
2290 ret = __write_inode(trans, &u, k.k->p.snapshot);
2297 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2298 struct nlink_table *links,
2299 u64 range_start, u64 range_end)
2301 struct btree_trans trans;
2302 struct btree_iter iter;
2307 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2309 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2310 POS(0, range_start),
2311 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2312 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2313 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2315 bch2_trans_exit(&trans);
2318 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2325 int bch2_check_nlinks(struct bch_fs *c)
2327 struct nlink_table links = { 0 };
2328 u64 this_iter_range_start, next_iter_range_start = 0;
2332 this_iter_range_start = next_iter_range_start;
2333 next_iter_range_start = U64_MAX;
2335 ret = check_nlinks_find_hardlinks(c, &links,
2336 this_iter_range_start,
2337 &next_iter_range_start);
2339 ret = check_nlinks_walk_dirents(c, &links,
2340 this_iter_range_start,
2341 next_iter_range_start);
2345 ret = check_nlinks_update_hardlinks(c, &links,
2346 this_iter_range_start,
2347 next_iter_range_start);
2352 } while (next_iter_range_start != U64_MAX);
2361 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2364 struct bkey_s_c_reflink_p p;
2365 struct bkey_i_reflink_p *u;
2368 if (k.k->type != KEY_TYPE_reflink_p)
2371 p = bkey_s_c_to_reflink_p(k);
2373 if (!p.v->front_pad && !p.v->back_pad)
2376 u = bch2_trans_kmalloc(trans, sizeof(*u));
2377 ret = PTR_ERR_OR_ZERO(u);
2381 bkey_reassemble(&u->k_i, k);
2385 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2388 int bch2_fix_reflink_p(struct bch_fs *c)
2390 struct btree_iter iter;
2394 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2397 ret = bch2_trans_run(c,
2398 for_each_btree_key_commit(&trans, iter,
2399 BTREE_ID_extents, POS_MIN,
2400 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2401 BTREE_ITER_ALL_SNAPSHOTS, k,
2402 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2403 fix_reflink_p_key(&trans, &iter, k)));