1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
10 #include "fs-common.h"
19 #include <linux/bsearch.h>
20 #include <linux/dcache.h> /* struct qstr */
22 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
25 * XXX: this is handling transaction restarts without returning
26 * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
28 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
31 struct btree_iter iter;
36 for_each_btree_key_upto(trans, iter, BTREE_ID_extents,
37 SPOS(inum, 0, snapshot),
40 if (bkey_extent_is_allocation(k.k))
43 bch2_trans_iter_exit(trans, &iter);
45 return ret ?: sectors;
48 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
51 struct btree_iter iter;
53 struct bkey_s_c_dirent d;
57 for_each_btree_key_upto(trans, iter, BTREE_ID_dirents,
58 SPOS(inum, 0, snapshot),
61 if (k.k->type != KEY_TYPE_dirent)
64 d = bkey_s_c_to_dirent(k);
65 if (d.v->d_type == DT_DIR)
68 bch2_trans_iter_exit(trans, &iter);
70 return ret ?: subdirs;
73 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
76 struct bch_snapshot s;
77 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots,
81 *subvol = le32_to_cpu(s.subvol);
82 else if (bch2_err_matches(ret, ENOENT))
83 bch_err(trans->c, "snapshot %u not found", snapshot);
88 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
89 u32 *snapshot, u64 *inum)
91 struct bch_subvolume s;
94 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
96 *snapshot = le32_to_cpu(s.snapshot);
97 *inum = le64_to_cpu(s.inode);
101 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
102 u32 *snapshot, u64 *inum)
104 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
107 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
108 struct bch_inode_unpacked *inode)
110 struct btree_iter iter;
114 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
116 BTREE_ITER_ALL_SNAPSHOTS);
117 k = bch2_btree_iter_peek(&iter);
122 if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
123 ret = -BCH_ERR_ENOENT_inode;
127 ret = bch2_inode_unpack(k, inode);
129 bch_err_msg(trans->c, ret, "fetching inode %llu", inode_nr);
130 bch2_trans_iter_exit(trans, &iter);
134 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
135 struct bch_inode_unpacked *inode,
138 struct btree_iter iter;
142 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
143 SPOS(0, inode_nr, *snapshot), 0);
148 ret = bkey_is_inode(k.k)
149 ? bch2_inode_unpack(k, inode)
150 : -BCH_ERR_ENOENT_inode;
152 *snapshot = iter.pos.snapshot;
154 bch_err_msg(trans->c, ret, "fetching inode %llu:%u", inode_nr, *snapshot);
155 bch2_trans_iter_exit(trans, &iter);
159 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
160 struct bch_inode_unpacked *inode,
163 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
166 static int __lookup_dirent(struct btree_trans *trans,
167 struct bch_hash_info hash_info,
168 subvol_inum dir, struct qstr *name,
169 u64 *target, unsigned *type)
171 struct btree_iter iter;
172 struct bkey_s_c_dirent d;
175 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
176 &hash_info, dir, name, 0);
180 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
181 *target = le64_to_cpu(d.v->d_inum);
183 bch2_trans_iter_exit(trans, &iter);
187 static int __write_inode(struct btree_trans *trans,
188 struct bch_inode_unpacked *inode,
191 struct bkey_inode_buf *inode_p =
192 bch2_trans_kmalloc(trans, sizeof(*inode_p));
195 return PTR_ERR(inode_p);
197 bch2_inode_pack(inode_p, inode);
198 inode_p->inode.k.p.snapshot = snapshot;
200 return bch2_btree_insert_nonextent(trans, BTREE_ID_inodes,
202 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
205 static int fsck_write_inode(struct btree_trans *trans,
206 struct bch_inode_unpacked *inode,
209 int ret = commit_do(trans, NULL, NULL,
211 BTREE_INSERT_LAZY_RW,
212 __write_inode(trans, inode, snapshot));
214 bch_err_fn(trans->c, ret);
218 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
220 struct bch_fs *c = trans->c;
221 struct btree_iter iter;
222 struct bch_inode_unpacked dir_inode;
223 struct bch_hash_info dir_hash_info;
226 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
230 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
232 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
234 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
235 &dir_hash_info, &iter,
236 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
237 bch2_trans_iter_exit(trans, &iter);
243 /* Get lost+found, create if it doesn't exist: */
244 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
245 struct bch_inode_unpacked *lostfound)
247 struct bch_fs *c = trans->c;
248 struct bch_inode_unpacked root;
249 struct bch_hash_info root_hash_info;
250 struct qstr lostfound_str = QSTR("lost+found");
251 subvol_inum root_inum = { .subvol = subvol };
257 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
261 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
265 root_hash_info = bch2_hash_info_init(c, &root);
267 ret = __lookup_dirent(trans, root_hash_info, root_inum,
268 &lostfound_str, &inum, &d_type);
269 if (bch2_err_matches(ret, ENOENT)) {
270 bch_notice(c, "creating lost+found");
271 goto create_lostfound;
278 if (d_type != DT_DIR) {
279 bch_err(c, "error looking up lost+found: not a directory");
280 return -BCH_ERR_ENOENT_not_directory;
284 * The bch2_check_dirents pass has already run, dangling dirents
285 * shouldn't exist here:
287 return __lookup_inode(trans, inum, lostfound, &snapshot);
290 bch2_inode_init_early(c, lostfound);
292 ret = bch2_create_trans(trans, root_inum, &root,
293 lostfound, &lostfound_str,
294 0, 0, S_IFDIR|0700, 0, NULL, NULL,
295 (subvol_inum) { }, 0);
296 bch_err_msg(c, ret, "creating lost+found");
300 static int __reattach_inode(struct btree_trans *trans,
301 struct bch_inode_unpacked *inode,
304 struct bch_hash_info dir_hash;
305 struct bch_inode_unpacked lostfound;
312 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
316 ret = lookup_lostfound(trans, subvol, &lostfound);
320 if (S_ISDIR(inode->bi_mode)) {
321 lostfound.bi_nlink++;
323 ret = __write_inode(trans, &lostfound, U32_MAX);
328 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
330 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
331 name = (struct qstr) QSTR(name_buf);
333 ret = bch2_dirent_create(trans,
336 .inum = lostfound.bi_inum,
340 &name, inode->bi_inum, &dir_offset,
341 BCH_HASH_SET_MUST_CREATE);
345 inode->bi_dir = lostfound.bi_inum;
346 inode->bi_dir_offset = dir_offset;
348 return __write_inode(trans, inode, inode_snapshot);
351 static int reattach_inode(struct btree_trans *trans,
352 struct bch_inode_unpacked *inode,
355 int ret = commit_do(trans, NULL, NULL,
356 BTREE_INSERT_LAZY_RW|
358 __reattach_inode(trans, inode, inode_snapshot));
359 bch_err_msg(trans->c, ret, "reattaching inode %llu", inode->bi_inum);
363 static int remove_backpointer(struct btree_trans *trans,
364 struct bch_inode_unpacked *inode)
366 struct btree_iter iter;
367 struct bkey_s_c_dirent d;
370 d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
371 POS(inode->bi_dir, inode->bi_dir_offset), 0,
374 __remove_dirent(trans, d.k->p);
375 bch2_trans_iter_exit(trans, &iter);
379 struct snapshots_seen_entry {
384 struct snapshots_seen {
386 DARRAY(struct snapshots_seen_entry) ids;
389 static inline void snapshots_seen_exit(struct snapshots_seen *s)
391 darray_exit(&s->ids);
394 static inline void snapshots_seen_init(struct snapshots_seen *s)
396 memset(s, 0, sizeof(*s));
399 static int snapshots_seen_add_inorder(struct bch_fs *c, struct snapshots_seen *s, u32 id)
401 struct snapshots_seen_entry *i, n = {
403 .equiv = bch2_snapshot_equiv(c, id),
407 darray_for_each(s->ids, i) {
414 ret = darray_insert_item(&s->ids, i - s->ids.data, n);
416 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
421 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
422 enum btree_id btree_id, struct bpos pos)
424 struct snapshots_seen_entry *i, n = {
426 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
430 if (!bkey_eq(s->pos, pos))
434 s->pos.snapshot = n.equiv;
436 darray_for_each(s->ids, i) {
441 * We currently don't rigorously track for snapshot cleanup
442 * needing to be run, so it shouldn't be a fsck error yet:
444 if (i->equiv == n.equiv) {
445 bch_err(c, "snapshot deletion did not finish:\n"
446 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
447 bch2_btree_ids[btree_id],
448 pos.inode, pos.offset,
449 i->id, n.id, n.equiv);
450 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_delete_dead_snapshots);
454 ret = darray_push(&s->ids, n);
456 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
462 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
463 * and @ancestor hasn't been overwritten in @seen
465 * @c: filesystem handle
466 * @seen: list of snapshot ids already seen at current position
467 * @id: descendent snapshot id
468 * @ancestor: ancestor snapshot id
470 * Returns: whether key in @ancestor snapshot is visible in @id snapshot
472 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
473 u32 id, u32 ancestor)
477 EBUG_ON(id > ancestor);
478 EBUG_ON(!bch2_snapshot_is_equiv(c, id));
479 EBUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
481 /* @ancestor should be the snapshot most recently added to @seen */
482 EBUG_ON(ancestor != seen->pos.snapshot);
483 EBUG_ON(ancestor != seen->ids.data[seen->ids.nr - 1].equiv);
488 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
492 * We know that @id is a descendant of @ancestor, we're checking if
493 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
494 * @ascestor and with @id as a descendent.
496 * But we already know that we're scanning IDs between @id and @ancestor
497 * numerically, since snapshot ID lists are kept sorted, so if we find
498 * an id that's an ancestor of @id we're done:
501 for (i = seen->ids.nr - 2;
502 i >= 0 && seen->ids.data[i].equiv >= id;
504 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv))
511 * ref_visible - given a key with snapshot id @src that points to a key with
512 * snapshot id @dst, test whether there is some snapshot in which @dst is
515 * @c: filesystem handle
516 * @s: list of snapshot IDs already seen at @src
517 * @src: snapshot ID of src key
518 * @dst: snapshot ID of dst key
519 * Returns: true if there is some snapshot in which @dst is visible
521 * Assumes we're visiting @src keys in natural key order
523 static bool ref_visible(struct bch_fs *c, struct snapshots_seen *s,
527 ? key_visible_in_snapshot(c, s, dst, src)
528 : bch2_snapshot_is_ancestor(c, src, dst);
531 static int ref_visible2(struct bch_fs *c,
532 u32 src, struct snapshots_seen *src_seen,
533 u32 dst, struct snapshots_seen *dst_seen)
535 src = bch2_snapshot_equiv(c, src);
536 dst = bch2_snapshot_equiv(c, dst);
540 swap(dst_seen, src_seen);
542 return key_visible_in_snapshot(c, src_seen, dst, src);
545 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
546 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
547 (_i)->snapshot <= (_snapshot); _i++) \
548 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
550 struct inode_walker_entry {
551 struct bch_inode_unpacked inode;
557 struct inode_walker {
558 bool first_this_inode;
559 bool recalculate_sums;
560 struct bpos last_pos;
562 DARRAY(struct inode_walker_entry) inodes;
565 static void inode_walker_exit(struct inode_walker *w)
567 darray_exit(&w->inodes);
570 static struct inode_walker inode_walker_init(void)
572 return (struct inode_walker) { 0, };
575 static int add_inode(struct bch_fs *c, struct inode_walker *w,
576 struct bkey_s_c inode)
578 struct bch_inode_unpacked u;
580 BUG_ON(bch2_inode_unpack(inode, &u));
582 return darray_push(&w->inodes, ((struct inode_walker_entry) {
584 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
588 static int get_inodes_all_snapshots(struct btree_trans *trans,
589 struct inode_walker *w, u64 inum)
591 struct bch_fs *c = trans->c;
592 struct btree_iter iter;
594 u32 restart_count = trans->restart_count;
597 w->recalculate_sums = false;
600 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
601 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
602 if (k.k->p.offset != inum)
605 if (bkey_is_inode(k.k))
608 bch2_trans_iter_exit(trans, &iter);
613 w->first_this_inode = true;
615 return trans_was_restarted(trans, restart_count);
618 static struct inode_walker_entry *
619 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w,
620 u32 snapshot, bool is_whiteout)
622 struct inode_walker_entry *i;
624 snapshot = bch2_snapshot_equiv(c, snapshot);
626 darray_for_each(w->inodes, i)
627 if (bch2_snapshot_is_ancestor(c, snapshot, i->snapshot))
632 BUG_ON(snapshot > i->snapshot);
634 if (snapshot != i->snapshot && !is_whiteout) {
635 struct inode_walker_entry new = *i;
639 new.snapshot = snapshot;
642 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
643 w->last_pos.inode, snapshot, i->snapshot);
645 while (i > w->inodes.data && i[-1].snapshot > snapshot)
648 pos = i - w->inodes.data;
649 ret = darray_insert_item(&w->inodes, pos, new);
653 i = w->inodes.data + pos;
659 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
660 struct inode_walker *w, struct bpos pos,
663 if (w->last_pos.inode != pos.inode) {
664 int ret = get_inodes_all_snapshots(trans, w, pos.inode);
667 } else if (bkey_cmp(w->last_pos, pos)) {
668 struct inode_walker_entry *i;
670 darray_for_each(w->inodes, i)
671 i->seen_this_pos = false;
677 return lookup_inode_for_snapshot(trans->c, w, pos.snapshot, is_whiteout);
680 static int __get_visible_inodes(struct btree_trans *trans,
681 struct inode_walker *w,
682 struct snapshots_seen *s,
685 struct bch_fs *c = trans->c;
686 struct btree_iter iter;
692 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
693 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
694 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
696 if (k.k->p.offset != inum)
699 if (!ref_visible(c, s, s->pos.snapshot, equiv))
702 if (bkey_is_inode(k.k))
705 if (equiv >= s->pos.snapshot)
708 bch2_trans_iter_exit(trans, &iter);
713 static int check_key_has_snapshot(struct btree_trans *trans,
714 struct btree_iter *iter,
717 struct bch_fs *c = trans->c;
718 struct printbuf buf = PRINTBUF;
721 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
722 "key in missing snapshot: %s",
723 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
724 ret = bch2_btree_delete_at(trans, iter,
725 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
731 static int hash_redo_key(struct btree_trans *trans,
732 const struct bch_hash_desc desc,
733 struct bch_hash_info *hash_info,
734 struct btree_iter *k_iter, struct bkey_s_c k)
736 struct bkey_i *delete;
739 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
741 return PTR_ERR(delete);
743 tmp = bch2_bkey_make_mut_noupdate(trans, k);
747 bkey_init(&delete->k);
748 delete->k.p = k_iter->pos;
749 return bch2_btree_iter_traverse(k_iter) ?:
750 bch2_trans_update(trans, k_iter, delete, 0) ?:
751 bch2_hash_set_snapshot(trans, desc, hash_info,
752 (subvol_inum) { 0, k.k->p.inode },
753 k.k->p.snapshot, tmp,
754 BCH_HASH_SET_MUST_CREATE,
755 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
756 bch2_trans_commit(trans, NULL, NULL,
758 BTREE_INSERT_LAZY_RW);
761 static int hash_check_key(struct btree_trans *trans,
762 const struct bch_hash_desc desc,
763 struct bch_hash_info *hash_info,
764 struct btree_iter *k_iter, struct bkey_s_c hash_k)
766 struct bch_fs *c = trans->c;
767 struct btree_iter iter = { NULL };
768 struct printbuf buf = PRINTBUF;
773 if (hash_k.k->type != desc.key_type)
776 hash = desc.hash_bkey(hash_info, hash_k);
778 if (likely(hash == hash_k.k->p.offset))
781 if (hash_k.k->p.offset < hash)
784 for_each_btree_key_norestart(trans, iter, desc.btree_id,
785 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
786 BTREE_ITER_SLOTS, k, ret) {
787 if (bkey_eq(k.k->p, hash_k.k->p))
790 if (fsck_err_on(k.k->type == desc.key_type &&
791 !desc.cmp_bkey(k, hash_k), c,
792 "duplicate hash table keys:\n%s",
793 (printbuf_reset(&buf),
794 bch2_bkey_val_to_text(&buf, c, hash_k),
796 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
800 if (bkey_deleted(k.k)) {
801 bch2_trans_iter_exit(trans, &iter);
806 bch2_trans_iter_exit(trans, &iter);
810 if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
811 bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
812 (printbuf_reset(&buf),
813 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
814 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
818 ret = -BCH_ERR_transaction_restart_nested;
824 static int check_inode(struct btree_trans *trans,
825 struct btree_iter *iter,
827 struct bch_inode_unpacked *prev,
828 struct snapshots_seen *s,
831 struct bch_fs *c = trans->c;
832 struct bch_inode_unpacked u;
833 bool do_update = false;
836 ret = check_key_has_snapshot(trans, iter, k);
842 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
846 if (!bkey_is_inode(k.k))
849 BUG_ON(bch2_inode_unpack(k, &u));
852 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
853 BCH_INODE_I_SECTORS_DIRTY|
854 BCH_INODE_UNLINKED)))
857 if (prev->bi_inum != u.bi_inum)
860 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
861 inode_d_type(prev) != inode_d_type(&u), c,
862 "inodes in different snapshots don't match")) {
863 bch_err(c, "repair not implemented yet");
867 if ((u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|BCH_INODE_UNLINKED)) &&
868 bch2_key_has_snapshot_overwrites(trans, BTREE_ID_inodes, k.k->p)) {
869 struct bpos new_min_pos;
871 ret = bch2_propagate_key_to_snapshot_leaves(trans, iter->btree_id, k, &new_min_pos);
875 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY|BCH_INODE_UNLINKED;
877 ret = __write_inode(trans, &u, iter->pos.snapshot);
878 bch_err_msg(c, ret, "in fsck updating inode");
882 if (!bpos_eq(new_min_pos, POS_MIN))
883 bch2_btree_iter_set_pos(iter, bpos_predecessor(new_min_pos));
887 if (u.bi_flags & BCH_INODE_UNLINKED &&
889 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
891 bch2_trans_unlock(trans);
894 ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
895 bch_err_msg(c, ret, "in fsck deleting inode");
899 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
901 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
903 bch_verbose(c, "truncating inode %llu", u.bi_inum);
905 bch2_trans_unlock(trans);
909 * XXX: need to truncate partial blocks too here - or ideally
910 * just switch units to bytes and that issue goes away
912 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
913 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
915 POS(u.bi_inum, U64_MAX),
917 bch_err_msg(c, ret, "in fsck truncating inode");
922 * We truncated without our normal sector accounting hook, just
923 * make sure we recalculate it:
925 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
927 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
931 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
933 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
937 bch_verbose(c, "recounting sectors for inode %llu",
940 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
942 bch_err_msg(c, sectors, "in fsck recounting inode sectors");
946 u.bi_sectors = sectors;
947 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
951 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
954 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
959 ret = __write_inode(trans, &u, iter->pos.snapshot);
960 bch_err_msg(c, ret, "in fsck updating inode");
971 int bch2_check_inodes(struct bch_fs *c)
973 bool full = c->opts.fsck;
974 struct btree_trans *trans = bch2_trans_get(c);
975 struct btree_iter iter;
976 struct bch_inode_unpacked prev = { 0 };
977 struct snapshots_seen s;
981 snapshots_seen_init(&s);
983 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
985 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
986 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
987 check_inode(trans, &iter, k, &prev, &s, full));
989 snapshots_seen_exit(&s);
990 bch2_trans_put(trans);
995 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
996 struct btree_iter *iter,
999 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
1002 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1003 struct bkey_s_c_dirent d)
1005 return inode->bi_dir == d.k->p.inode &&
1006 inode->bi_dir_offset == d.k->p.offset;
1009 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1010 struct bch_inode_unpacked *inode)
1012 return d.v->d_type == DT_SUBVOL
1013 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1014 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1017 static int inode_backpointer_exists(struct btree_trans *trans,
1018 struct bch_inode_unpacked *inode,
1021 struct btree_iter iter;
1022 struct bkey_s_c_dirent d;
1025 d = dirent_get_by_pos(trans, &iter,
1026 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1029 return bch2_err_matches(ret, ENOENT) ? 0 : ret;
1031 ret = dirent_points_to_inode(d, inode);
1032 bch2_trans_iter_exit(trans, &iter);
1036 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1038 struct bch_fs *c = trans->c;
1039 struct inode_walker_entry *i;
1040 u32 restart_count = trans->restart_count;
1044 darray_for_each(w->inodes, i) {
1045 if (i->inode.bi_sectors == i->count)
1048 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1050 if (w->recalculate_sums)
1053 if (i->count != count2) {
1054 bch_err(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1055 w->last_pos.inode, i->snapshot, i->count, count2);
1056 return -BCH_ERR_internal_fsck_err;
1059 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1060 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1061 w->last_pos.inode, i->snapshot,
1062 i->inode.bi_sectors, i->count)) {
1063 i->inode.bi_sectors = i->count;
1064 ret = fsck_write_inode(trans, &i->inode, i->snapshot);
1071 return ret ?: trans_was_restarted(trans, restart_count);
1077 struct snapshots_seen seen;
1080 struct extent_ends {
1081 struct bpos last_pos;
1082 DARRAY(struct extent_end) e;
1085 static void extent_ends_reset(struct extent_ends *extent_ends)
1087 struct extent_end *i;
1089 darray_for_each(extent_ends->e, i)
1090 snapshots_seen_exit(&i->seen);
1092 extent_ends->e.nr = 0;
1095 static void extent_ends_exit(struct extent_ends *extent_ends)
1097 extent_ends_reset(extent_ends);
1098 darray_exit(&extent_ends->e);
1101 static void extent_ends_init(struct extent_ends *extent_ends)
1103 memset(extent_ends, 0, sizeof(*extent_ends));
1106 static int extent_ends_at(struct bch_fs *c,
1107 struct extent_ends *extent_ends,
1108 struct snapshots_seen *seen,
1111 struct extent_end *i, n = (struct extent_end) {
1112 .offset = k.k->p.offset,
1113 .snapshot = k.k->p.snapshot,
1117 n.seen.ids.data = kmemdup(seen->ids.data,
1118 sizeof(seen->ids.data[0]) * seen->ids.size,
1120 if (!n.seen.ids.data)
1121 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1123 darray_for_each(extent_ends->e, i) {
1124 if (i->snapshot == k.k->p.snapshot) {
1125 snapshots_seen_exit(&i->seen);
1130 if (i->snapshot >= k.k->p.snapshot)
1134 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1137 static int overlapping_extents_found(struct btree_trans *trans,
1138 enum btree_id btree,
1139 struct bpos pos1, struct snapshots_seen *pos1_seen,
1142 struct extent_end *extent_end)
1144 struct bch_fs *c = trans->c;
1145 struct printbuf buf = PRINTBUF;
1146 struct btree_iter iter1, iter2 = { NULL };
1147 struct bkey_s_c k1, k2;
1150 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1152 bch2_trans_iter_init(trans, &iter1, btree, pos1,
1153 BTREE_ITER_ALL_SNAPSHOTS|
1154 BTREE_ITER_NOT_EXTENTS);
1155 k1 = bch2_btree_iter_peek_upto(&iter1, POS(pos1.inode, U64_MAX));
1160 prt_str(&buf, "\n ");
1161 bch2_bkey_val_to_text(&buf, c, k1);
1163 if (!bpos_eq(pos1, k1.k->p)) {
1164 prt_str(&buf, "\n wanted\n ");
1165 bch2_bpos_to_text(&buf, pos1);
1166 prt_str(&buf, "\n ");
1167 bch2_bkey_to_text(&buf, &pos2);
1169 bch_err(c, "%s: error finding first overlapping extent when repairing, got%s",
1171 ret = -BCH_ERR_internal_fsck_err;
1175 bch2_trans_copy_iter(&iter2, &iter1);
1178 bch2_btree_iter_advance(&iter2);
1180 k2 = bch2_btree_iter_peek_upto(&iter2, POS(pos1.inode, U64_MAX));
1185 if (bpos_ge(k2.k->p, pos2.p))
1189 prt_str(&buf, "\n ");
1190 bch2_bkey_val_to_text(&buf, c, k2);
1192 if (bpos_gt(k2.k->p, pos2.p) ||
1193 pos2.size != k2.k->size) {
1194 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1196 ret = -BCH_ERR_internal_fsck_err;
1200 prt_printf(&buf, "\n overwriting %s extent",
1201 pos1.snapshot >= pos2.p.snapshot ? "first" : "second");
1203 if (fsck_err(c, "overlapping extents%s", buf.buf)) {
1204 struct btree_iter *old_iter = &iter1;
1205 struct disk_reservation res = { 0 };
1207 if (pos1.snapshot < pos2.p.snapshot) {
1212 trans->extra_journal_res += bch2_bkey_sectors_compressed(k2);
1214 ret = bch2_trans_update_extent_overwrite(trans, old_iter,
1215 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE,
1217 bch2_trans_commit(trans, &res, NULL,
1218 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL);
1219 bch2_disk_reservation_put(c, &res);
1226 if (pos1.snapshot == pos2.p.snapshot) {
1228 * We overwrote the first extent, and did the overwrite
1229 * in the same snapshot:
1231 extent_end->offset = bkey_start_offset(&pos2);
1232 } else if (pos1.snapshot > pos2.p.snapshot) {
1234 * We overwrote the first extent in pos2's snapshot:
1236 ret = snapshots_seen_add_inorder(c, pos1_seen, pos2.p.snapshot);
1239 * We overwrote the second extent - restart
1240 * check_extent() from the top:
1242 ret = -BCH_ERR_transaction_restart_nested;
1247 bch2_trans_iter_exit(trans, &iter2);
1248 bch2_trans_iter_exit(trans, &iter1);
1249 printbuf_exit(&buf);
1253 static int check_overlapping_extents(struct btree_trans *trans,
1254 struct snapshots_seen *seen,
1255 struct extent_ends *extent_ends,
1258 struct btree_iter *iter,
1261 struct bch_fs *c = trans->c;
1262 struct extent_end *i;
1265 /* transaction restart, running again */
1266 if (bpos_eq(extent_ends->last_pos, k.k->p))
1269 if (extent_ends->last_pos.inode != k.k->p.inode)
1270 extent_ends_reset(extent_ends);
1272 darray_for_each(extent_ends->e, i) {
1273 if (i->offset <= bkey_start_offset(k.k))
1276 if (!ref_visible2(c,
1277 k.k->p.snapshot, seen,
1278 i->snapshot, &i->seen))
1281 ret = overlapping_extents_found(trans, iter->btree_id,
1282 SPOS(iter->pos.inode,
1291 ret = extent_ends_at(c, extent_ends, seen, k);
1295 extent_ends->last_pos = k.k->p;
1300 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1302 struct inode_walker *inode,
1303 struct snapshots_seen *s,
1304 struct extent_ends *extent_ends)
1306 struct bch_fs *c = trans->c;
1307 struct inode_walker_entry *i;
1308 struct printbuf buf = PRINTBUF;
1309 struct bpos equiv = k.k->p;
1312 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1314 ret = check_key_has_snapshot(trans, iter, k);
1316 ret = ret < 0 ? ret : 0;
1320 if (inode->last_pos.inode != k.k->p.inode) {
1321 ret = check_i_sectors(trans, inode);
1326 i = walk_inode(trans, inode, equiv, k.k->type == KEY_TYPE_whiteout);
1327 ret = PTR_ERR_OR_ZERO(i);
1331 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1335 if (k.k->type != KEY_TYPE_whiteout) {
1336 if (fsck_err_on(!i, c,
1337 "extent in missing inode:\n %s",
1338 (printbuf_reset(&buf),
1339 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1342 if (fsck_err_on(i &&
1343 !S_ISREG(i->inode.bi_mode) &&
1344 !S_ISLNK(i->inode.bi_mode), c,
1345 "extent in non regular inode mode %o:\n %s",
1347 (printbuf_reset(&buf),
1348 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1351 ret = check_overlapping_extents(trans, s, extent_ends, k,
1352 equiv.snapshot, iter,
1353 &inode->recalculate_sums);
1359 * Check inodes in reverse order, from oldest snapshots to newest,
1360 * starting from the inode that matches this extent's snapshot. If we
1361 * didn't have one, iterate over all inodes:
1364 i = inode->inodes.data + inode->inodes.nr - 1;
1367 inode->inodes.data && i >= inode->inodes.data;
1369 if (i->snapshot > equiv.snapshot ||
1370 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1373 if (k.k->type != KEY_TYPE_whiteout) {
1374 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1375 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1376 !bkey_extent_is_reservation(k), c,
1377 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1378 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1379 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1380 struct btree_iter iter2;
1382 bch2_trans_copy_iter(&iter2, iter);
1383 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1384 ret = bch2_btree_iter_traverse(&iter2) ?:
1385 bch2_btree_delete_at(trans, &iter2,
1386 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1387 bch2_trans_iter_exit(trans, &iter2);
1391 iter->k.type = KEY_TYPE_whiteout;
1394 if (bkey_extent_is_allocation(k.k))
1395 i->count += k.k->size;
1398 i->seen_this_pos = true;
1403 printbuf_exit(&buf);
1407 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1412 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1413 * that i_size an i_sectors are consistent
1415 int bch2_check_extents(struct bch_fs *c)
1417 struct inode_walker w = inode_walker_init();
1418 struct snapshots_seen s;
1419 struct btree_trans *trans = bch2_trans_get(c);
1420 struct btree_iter iter;
1422 struct extent_ends extent_ends;
1423 struct disk_reservation res = { 0 };
1426 snapshots_seen_init(&s);
1427 extent_ends_init(&extent_ends);
1429 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_extents,
1430 POS(BCACHEFS_ROOT_INO, 0),
1431 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1433 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
1434 bch2_disk_reservation_put(c, &res);
1435 check_extent(trans, &iter, k, &w, &s, &extent_ends);
1437 check_i_sectors(trans, &w);
1439 bch2_disk_reservation_put(c, &res);
1440 extent_ends_exit(&extent_ends);
1441 inode_walker_exit(&w);
1442 snapshots_seen_exit(&s);
1443 bch2_trans_put(trans);
1449 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1451 struct bch_fs *c = trans->c;
1452 struct inode_walker_entry *i;
1453 u32 restart_count = trans->restart_count;
1457 darray_for_each(w->inodes, i) {
1458 if (i->inode.bi_nlink == i->count)
1461 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1465 if (i->count != count2) {
1466 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1469 if (i->inode.bi_nlink == i->count)
1473 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1474 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1475 w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1476 i->inode.bi_nlink = i->count;
1477 ret = fsck_write_inode(trans, &i->inode, i->snapshot);
1484 return ret ?: trans_was_restarted(trans, restart_count);
1487 static int check_dirent_target(struct btree_trans *trans,
1488 struct btree_iter *iter,
1489 struct bkey_s_c_dirent d,
1490 struct bch_inode_unpacked *target,
1491 u32 target_snapshot)
1493 struct bch_fs *c = trans->c;
1494 struct bkey_i_dirent *n;
1495 bool backpointer_exists = true;
1496 struct printbuf buf = PRINTBUF;
1499 if (!target->bi_dir &&
1500 !target->bi_dir_offset) {
1501 target->bi_dir = d.k->p.inode;
1502 target->bi_dir_offset = d.k->p.offset;
1504 ret = __write_inode(trans, target, target_snapshot);
1509 if (!inode_points_to_dirent(target, d)) {
1510 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1514 backpointer_exists = ret;
1517 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1518 backpointer_exists, c,
1519 "directory %llu with multiple links",
1521 ret = __remove_dirent(trans, d.k->p);
1525 if (fsck_err_on(backpointer_exists &&
1526 !target->bi_nlink, c,
1527 "inode %llu type %s has multiple links but i_nlink 0",
1528 target->bi_inum, bch2_d_types[d.v->d_type])) {
1530 target->bi_flags &= ~BCH_INODE_UNLINKED;
1532 ret = __write_inode(trans, target, target_snapshot);
1537 if (fsck_err_on(!backpointer_exists, c,
1538 "inode %llu:%u has wrong backpointer:\n"
1540 "should be %llu:%llu",
1541 target->bi_inum, target_snapshot,
1543 target->bi_dir_offset,
1546 target->bi_dir = d.k->p.inode;
1547 target->bi_dir_offset = d.k->p.offset;
1549 ret = __write_inode(trans, target, target_snapshot);
1555 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1556 "incorrect d_type: got %s, should be %s:\n%s",
1557 bch2_d_type_str(d.v->d_type),
1558 bch2_d_type_str(inode_d_type(target)),
1559 (printbuf_reset(&buf),
1560 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1561 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1562 ret = PTR_ERR_OR_ZERO(n);
1566 bkey_reassemble(&n->k_i, d.s_c);
1567 n->v.d_type = inode_d_type(target);
1569 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1573 d = dirent_i_to_s_c(n);
1576 if (d.v->d_type == DT_SUBVOL &&
1577 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1578 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1579 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1580 le32_to_cpu(d.v->d_parent_subvol),
1581 target->bi_parent_subvol))) {
1582 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1583 ret = PTR_ERR_OR_ZERO(n);
1587 bkey_reassemble(&n->k_i, d.s_c);
1588 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1590 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1594 d = dirent_i_to_s_c(n);
1599 printbuf_exit(&buf);
1604 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1606 struct bch_hash_info *hash_info,
1607 struct inode_walker *dir,
1608 struct inode_walker *target,
1609 struct snapshots_seen *s)
1611 struct bch_fs *c = trans->c;
1612 struct bkey_s_c_dirent d;
1613 struct inode_walker_entry *i;
1614 struct printbuf buf = PRINTBUF;
1618 ret = check_key_has_snapshot(trans, iter, k);
1620 ret = ret < 0 ? ret : 0;
1625 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1627 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1631 if (k.k->type == KEY_TYPE_whiteout)
1634 if (dir->last_pos.inode != k.k->p.inode) {
1635 ret = check_subdir_count(trans, dir);
1640 BUG_ON(!iter->path->should_be_locked);
1642 i = walk_inode(trans, dir, equiv, k.k->type == KEY_TYPE_whiteout);
1643 ret = PTR_ERR_OR_ZERO(i);
1647 if (dir->first_this_inode && dir->inodes.nr)
1648 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1649 dir->first_this_inode = false;
1651 if (fsck_err_on(!i, c,
1652 "dirent in nonexisting directory:\n%s",
1653 (printbuf_reset(&buf),
1654 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1655 ret = bch2_btree_delete_at(trans, iter,
1656 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1663 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1664 "dirent in non directory inode type %s:\n%s",
1665 bch2_d_type_str(inode_d_type(&i->inode)),
1666 (printbuf_reset(&buf),
1667 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1668 ret = bch2_btree_delete_at(trans, iter, 0);
1672 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1676 /* dirent has been deleted */
1681 if (k.k->type != KEY_TYPE_dirent)
1684 d = bkey_s_c_to_dirent(k);
1686 if (d.v->d_type == DT_SUBVOL) {
1687 struct bch_inode_unpacked subvol_root;
1688 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1689 u32 target_snapshot;
1692 ret = __subvol_lookup(trans, target_subvol,
1693 &target_snapshot, &target_inum);
1694 if (ret && !bch2_err_matches(ret, ENOENT))
1697 if (fsck_err_on(ret, c,
1698 "dirent points to missing subvolume %u",
1699 le32_to_cpu(d.v->d_child_subvol))) {
1700 ret = __remove_dirent(trans, d.k->p);
1704 ret = __lookup_inode(trans, target_inum,
1705 &subvol_root, &target_snapshot);
1706 if (ret && !bch2_err_matches(ret, ENOENT))
1709 if (fsck_err_on(ret, c,
1710 "subvolume %u points to missing subvolume root %llu",
1713 bch_err(c, "repair not implemented yet");
1718 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1719 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1721 subvol_root.bi_subvol, target_subvol)) {
1722 subvol_root.bi_subvol = target_subvol;
1723 ret = __write_inode(trans, &subvol_root, target_snapshot);
1728 ret = check_dirent_target(trans, iter, d, &subvol_root,
1733 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1737 if (fsck_err_on(!target->inodes.nr, c,
1738 "dirent points to missing inode: (equiv %u)\n%s",
1740 (printbuf_reset(&buf),
1741 bch2_bkey_val_to_text(&buf, c, k),
1743 ret = __remove_dirent(trans, d.k->p);
1748 darray_for_each(target->inodes, i) {
1749 ret = check_dirent_target(trans, iter, d,
1750 &i->inode, i->snapshot);
1756 if (d.v->d_type == DT_DIR)
1757 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1763 printbuf_exit(&buf);
1769 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1772 int bch2_check_dirents(struct bch_fs *c)
1774 struct inode_walker dir = inode_walker_init();
1775 struct inode_walker target = inode_walker_init();
1776 struct snapshots_seen s;
1777 struct bch_hash_info hash_info;
1778 struct btree_trans *trans = bch2_trans_get(c);
1779 struct btree_iter iter;
1783 snapshots_seen_init(&s);
1785 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_dirents,
1786 POS(BCACHEFS_ROOT_INO, 0),
1787 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1790 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1791 check_dirent(trans, &iter, k, &hash_info, &dir, &target, &s));
1793 bch2_trans_put(trans);
1794 snapshots_seen_exit(&s);
1795 inode_walker_exit(&dir);
1796 inode_walker_exit(&target);
1801 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1803 struct bch_hash_info *hash_info,
1804 struct inode_walker *inode)
1806 struct bch_fs *c = trans->c;
1807 struct inode_walker_entry *i;
1810 ret = check_key_has_snapshot(trans, iter, k);
1814 i = walk_inode(trans, inode, k.k->p, k.k->type == KEY_TYPE_whiteout);
1815 ret = PTR_ERR_OR_ZERO(i);
1819 if (inode->first_this_inode && inode->inodes.nr)
1820 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1821 inode->first_this_inode = false;
1823 if (fsck_err_on(!i, c,
1824 "xattr for missing inode %llu",
1826 return bch2_btree_delete_at(trans, iter, 0);
1831 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1838 * Walk xattrs: verify that they all have a corresponding inode
1840 int bch2_check_xattrs(struct bch_fs *c)
1842 struct inode_walker inode = inode_walker_init();
1843 struct bch_hash_info hash_info;
1844 struct btree_iter iter;
1848 ret = bch2_trans_run(c,
1849 for_each_btree_key_commit(trans, iter, BTREE_ID_xattrs,
1850 POS(BCACHEFS_ROOT_INO, 0),
1851 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1854 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1855 check_xattr(trans, &iter, k, &hash_info, &inode)));
1860 static int check_root_trans(struct btree_trans *trans)
1862 struct bch_fs *c = trans->c;
1863 struct bch_inode_unpacked root_inode;
1868 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1869 if (ret && !bch2_err_matches(ret, ENOENT))
1872 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1873 struct bkey_i_subvolume root_subvol;
1876 inum = BCACHEFS_ROOT_INO;
1878 bkey_subvolume_init(&root_subvol.k_i);
1879 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1880 root_subvol.v.flags = 0;
1881 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1882 root_subvol.v.inode = cpu_to_le64(inum);
1883 ret = commit_do(trans, NULL, NULL,
1884 BTREE_INSERT_NOFAIL|
1885 BTREE_INSERT_LAZY_RW,
1886 bch2_btree_insert_trans(trans, BTREE_ID_subvolumes,
1887 &root_subvol.k_i, 0));
1888 bch_err_msg(c, ret, "writing root subvol");
1894 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1895 if (ret && !bch2_err_matches(ret, ENOENT))
1898 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1899 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1900 "root inode not a directory")) {
1901 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1903 root_inode.bi_inum = inum;
1905 ret = __write_inode(trans, &root_inode, snapshot);
1906 bch_err_msg(c, ret, "writing root inode");
1913 /* Get root directory, create if it doesn't exist: */
1914 int bch2_check_root(struct bch_fs *c)
1918 ret = bch2_trans_do(c, NULL, NULL,
1919 BTREE_INSERT_NOFAIL|
1920 BTREE_INSERT_LAZY_RW,
1921 check_root_trans(trans));
1926 struct pathbuf_entry {
1931 typedef DARRAY(struct pathbuf_entry) pathbuf;
1933 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1935 struct pathbuf_entry *i;
1937 darray_for_each(*p, i)
1938 if (i->inum == inum &&
1939 i->snapshot == snapshot)
1945 static int path_down(struct bch_fs *c, pathbuf *p,
1946 u64 inum, u32 snapshot)
1948 int ret = darray_push(p, ((struct pathbuf_entry) {
1950 .snapshot = snapshot,
1954 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1960 * Check that a given inode is reachable from the root:
1962 * XXX: we should also be verifying that inodes are in the right subvolumes
1964 static int check_path(struct btree_trans *trans,
1966 struct bch_inode_unpacked *inode,
1969 struct bch_fs *c = trans->c;
1972 snapshot = bch2_snapshot_equiv(c, snapshot);
1975 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1976 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1977 struct btree_iter dirent_iter;
1978 struct bkey_s_c_dirent d;
1979 u32 parent_snapshot = snapshot;
1981 if (inode->bi_subvol) {
1984 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1985 &parent_snapshot, &inum);
1990 ret = lockrestart_do(trans,
1991 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
1992 SPOS(inode->bi_dir, inode->bi_dir_offset,
1993 parent_snapshot))).k));
1994 if (ret && !bch2_err_matches(ret, ENOENT))
1997 if (!ret && !dirent_points_to_inode(d, inode)) {
1998 bch2_trans_iter_exit(trans, &dirent_iter);
1999 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
2002 if (bch2_err_matches(ret, ENOENT)) {
2003 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2004 inode->bi_inum, snapshot,
2005 bch2_d_type_str(inode_d_type(inode)),
2008 inode->bi_dir_offset))
2009 ret = reattach_inode(trans, inode, snapshot);
2013 bch2_trans_iter_exit(trans, &dirent_iter);
2015 if (!S_ISDIR(inode->bi_mode))
2018 ret = path_down(c, p, inode->bi_inum, snapshot);
2020 bch_err(c, "memory allocation failure");
2024 snapshot = parent_snapshot;
2026 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2028 /* Should have been caught in dirents pass */
2029 bch_err(c, "error looking up parent directory: %i", ret);
2033 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2034 struct pathbuf_entry *i;
2036 /* XXX print path */
2037 bch_err(c, "directory structure loop");
2039 darray_for_each(*p, i)
2040 pr_err("%llu:%u", i->inum, i->snapshot);
2041 pr_err("%llu:%u", inode->bi_inum, snapshot);
2043 if (!fsck_err(c, "directory structure loop"))
2046 ret = commit_do(trans, NULL, NULL,
2047 BTREE_INSERT_NOFAIL|
2048 BTREE_INSERT_LAZY_RW,
2049 remove_backpointer(trans, inode));
2051 bch_err(c, "error removing dirent: %i", ret);
2055 ret = reattach_inode(trans, inode, snapshot);
2064 * Check for unreachable inodes, as well as loops in the directory structure:
2065 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2068 int bch2_check_directory_structure(struct bch_fs *c)
2070 struct btree_trans *trans = bch2_trans_get(c);
2071 struct btree_iter iter;
2073 struct bch_inode_unpacked u;
2074 pathbuf path = { 0, };
2077 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS_MIN,
2079 BTREE_ITER_PREFETCH|
2080 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2081 if (!bkey_is_inode(k.k))
2084 ret = bch2_inode_unpack(k, &u);
2086 /* Should have been caught earlier in fsck: */
2087 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2091 if (u.bi_flags & BCH_INODE_UNLINKED)
2094 ret = check_path(trans, &path, &u, iter.pos.snapshot);
2098 bch2_trans_iter_exit(trans, &iter);
2099 bch2_trans_put(trans);
2105 struct nlink_table {
2116 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2117 u64 inum, u32 snapshot)
2119 if (t->nr == t->size) {
2120 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2121 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2124 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2126 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2130 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2138 t->d[t->nr++] = (struct nlink) {
2140 .snapshot = snapshot,
2146 static int nlink_cmp(const void *_l, const void *_r)
2148 const struct nlink *l = _l;
2149 const struct nlink *r = _r;
2151 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2154 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2155 struct nlink_table *links,
2156 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2158 struct nlink *link, key = {
2159 .inum = inum, .snapshot = U32_MAX,
2162 if (inum < range_start || inum >= range_end)
2165 link = __inline_bsearch(&key, links->d, links->nr,
2166 sizeof(links->d[0]), nlink_cmp);
2170 while (link > links->d && link[0].inum == link[-1].inum)
2173 for (; link < links->d + links->nr && link->inum == inum; link++)
2174 if (ref_visible(c, s, snapshot, link->snapshot)) {
2176 if (link->snapshot >= snapshot)
2182 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2183 struct nlink_table *t,
2184 u64 start, u64 *end)
2186 struct btree_trans *trans = bch2_trans_get(c);
2187 struct btree_iter iter;
2189 struct bch_inode_unpacked u;
2192 for_each_btree_key(trans, iter, BTREE_ID_inodes,
2195 BTREE_ITER_PREFETCH|
2196 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2197 if (!bkey_is_inode(k.k))
2200 /* Should never fail, checked by bch2_inode_invalid: */
2201 BUG_ON(bch2_inode_unpack(k, &u));
2204 * Backpointer and directory structure checks are sufficient for
2205 * directories, since they can't have hardlinks:
2207 if (S_ISDIR(u.bi_mode))
2213 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2215 *end = k.k->p.offset;
2221 bch2_trans_iter_exit(trans, &iter);
2222 bch2_trans_put(trans);
2225 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2231 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2232 u64 range_start, u64 range_end)
2234 struct btree_trans *trans = bch2_trans_get(c);
2235 struct snapshots_seen s;
2236 struct btree_iter iter;
2238 struct bkey_s_c_dirent d;
2241 snapshots_seen_init(&s);
2243 for_each_btree_key(trans, iter, BTREE_ID_dirents, POS_MIN,
2245 BTREE_ITER_PREFETCH|
2246 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2247 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2251 switch (k.k->type) {
2252 case KEY_TYPE_dirent:
2253 d = bkey_s_c_to_dirent(k);
2255 if (d.v->d_type != DT_DIR &&
2256 d.v->d_type != DT_SUBVOL)
2257 inc_link(c, &s, links, range_start, range_end,
2258 le64_to_cpu(d.v->d_inum),
2259 bch2_snapshot_equiv(c, d.k->p.snapshot));
2263 bch2_trans_iter_exit(trans, &iter);
2266 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2268 bch2_trans_put(trans);
2269 snapshots_seen_exit(&s);
2273 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2275 struct nlink_table *links,
2276 size_t *idx, u64 range_end)
2278 struct bch_fs *c = trans->c;
2279 struct bch_inode_unpacked u;
2280 struct nlink *link = &links->d[*idx];
2283 if (k.k->p.offset >= range_end)
2286 if (!bkey_is_inode(k.k))
2289 BUG_ON(bch2_inode_unpack(k, &u));
2291 if (S_ISDIR(u.bi_mode))
2297 while ((cmp_int(link->inum, k.k->p.offset) ?:
2298 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2299 BUG_ON(*idx == links->nr);
2300 link = &links->d[++*idx];
2303 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2304 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2305 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2306 bch2_inode_nlink_get(&u), link->count)) {
2307 bch2_inode_nlink_set(&u, link->count);
2308 ret = __write_inode(trans, &u, k.k->p.snapshot);
2315 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2316 struct nlink_table *links,
2317 u64 range_start, u64 range_end)
2319 struct btree_iter iter;
2324 ret = bch2_trans_run(c,
2325 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
2326 POS(0, range_start),
2327 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2328 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2329 check_nlinks_update_inode(trans, &iter, k, links, &idx, range_end)));
2331 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2338 int bch2_check_nlinks(struct bch_fs *c)
2340 struct nlink_table links = { 0 };
2341 u64 this_iter_range_start, next_iter_range_start = 0;
2345 this_iter_range_start = next_iter_range_start;
2346 next_iter_range_start = U64_MAX;
2348 ret = check_nlinks_find_hardlinks(c, &links,
2349 this_iter_range_start,
2350 &next_iter_range_start);
2352 ret = check_nlinks_walk_dirents(c, &links,
2353 this_iter_range_start,
2354 next_iter_range_start);
2358 ret = check_nlinks_update_hardlinks(c, &links,
2359 this_iter_range_start,
2360 next_iter_range_start);
2365 } while (next_iter_range_start != U64_MAX);
2372 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2375 struct bkey_s_c_reflink_p p;
2376 struct bkey_i_reflink_p *u;
2379 if (k.k->type != KEY_TYPE_reflink_p)
2382 p = bkey_s_c_to_reflink_p(k);
2384 if (!p.v->front_pad && !p.v->back_pad)
2387 u = bch2_trans_kmalloc(trans, sizeof(*u));
2388 ret = PTR_ERR_OR_ZERO(u);
2392 bkey_reassemble(&u->k_i, k);
2396 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2399 int bch2_fix_reflink_p(struct bch_fs *c)
2401 struct btree_iter iter;
2405 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2408 ret = bch2_trans_run(c,
2409 for_each_btree_key_commit(trans, iter,
2410 BTREE_ID_extents, POS_MIN,
2411 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2412 BTREE_ITER_ALL_SNAPSHOTS, k,
2413 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2414 fix_reflink_p_key(trans, &iter, k)));