1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_cache.h"
6 #include "btree_update.h"
11 #include "fs-common.h"
20 #include <linux/bsearch.h>
21 #include <linux/dcache.h> /* struct qstr */
23 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
26 * XXX: this is handling transaction restarts without returning
27 * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
29 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
32 struct btree_iter iter;
37 for_each_btree_key_upto(trans, iter, BTREE_ID_extents,
38 SPOS(inum, 0, snapshot),
41 if (bkey_extent_is_allocation(k.k))
44 bch2_trans_iter_exit(trans, &iter);
46 return ret ?: sectors;
49 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
52 struct btree_iter iter;
54 struct bkey_s_c_dirent d;
58 for_each_btree_key_upto(trans, iter, BTREE_ID_dirents,
59 SPOS(inum, 0, snapshot),
62 if (k.k->type != KEY_TYPE_dirent)
65 d = bkey_s_c_to_dirent(k);
66 if (d.v->d_type == DT_DIR)
69 bch2_trans_iter_exit(trans, &iter);
71 return ret ?: subdirs;
74 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
77 struct bch_snapshot s;
78 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots,
82 *subvol = le32_to_cpu(s.subvol);
83 else if (bch2_err_matches(ret, ENOENT))
84 bch_err(trans->c, "snapshot %u not found", snapshot);
89 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
90 u32 *snapshot, u64 *inum)
92 struct bch_subvolume s;
95 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
97 *snapshot = le32_to_cpu(s.snapshot);
98 *inum = le64_to_cpu(s.inode);
102 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
103 u32 *snapshot, u64 *inum)
105 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
108 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
109 struct bch_inode_unpacked *inode)
111 struct btree_iter iter;
115 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
117 BTREE_ITER_ALL_SNAPSHOTS);
118 k = bch2_btree_iter_peek(&iter);
123 if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
124 ret = -BCH_ERR_ENOENT_inode;
128 ret = bch2_inode_unpack(k, inode);
130 bch_err_msg(trans->c, ret, "fetching inode %llu", inode_nr);
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 bch_err_msg(trans->c, ret, "fetching inode %llu:%u", inode_nr, *snapshot);
156 bch2_trans_iter_exit(trans, &iter);
160 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
161 struct bch_inode_unpacked *inode,
164 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
167 static int __lookup_dirent(struct btree_trans *trans,
168 struct bch_hash_info hash_info,
169 subvol_inum dir, struct qstr *name,
170 u64 *target, unsigned *type)
172 struct btree_iter iter;
173 struct bkey_s_c_dirent d;
176 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
177 &hash_info, dir, name, 0);
181 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
182 *target = le64_to_cpu(d.v->d_inum);
184 bch2_trans_iter_exit(trans, &iter);
188 static int __write_inode(struct btree_trans *trans,
189 struct bch_inode_unpacked *inode,
192 struct bkey_inode_buf *inode_p =
193 bch2_trans_kmalloc(trans, sizeof(*inode_p));
196 return PTR_ERR(inode_p);
198 bch2_inode_pack(inode_p, inode);
199 inode_p->inode.k.p.snapshot = snapshot;
201 return bch2_btree_insert_nonextent(trans, BTREE_ID_inodes,
203 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
206 static int fsck_write_inode(struct btree_trans *trans,
207 struct bch_inode_unpacked *inode,
210 int ret = commit_do(trans, NULL, NULL,
211 BCH_TRANS_COMMIT_no_enospc|
212 BCH_TRANS_COMMIT_lazy_rw,
213 __write_inode(trans, inode, snapshot));
215 bch_err_fn(trans->c, ret);
219 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
221 struct bch_fs *c = trans->c;
222 struct btree_iter iter;
223 struct bch_inode_unpacked dir_inode;
224 struct bch_hash_info dir_hash_info;
227 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
231 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
233 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
235 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
236 &dir_hash_info, &iter,
237 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
238 bch2_trans_iter_exit(trans, &iter);
244 /* Get lost+found, create if it doesn't exist: */
245 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
246 struct bch_inode_unpacked *lostfound)
248 struct bch_fs *c = trans->c;
249 struct bch_inode_unpacked root;
250 struct bch_hash_info root_hash_info;
251 struct qstr lostfound_str = QSTR("lost+found");
252 subvol_inum root_inum = { .subvol = subvol };
258 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
262 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
266 root_hash_info = bch2_hash_info_init(c, &root);
268 ret = __lookup_dirent(trans, root_hash_info, root_inum,
269 &lostfound_str, &inum, &d_type);
270 if (bch2_err_matches(ret, ENOENT)) {
271 bch_notice(c, "creating lost+found");
272 goto create_lostfound;
279 if (d_type != DT_DIR) {
280 bch_err(c, "error looking up lost+found: not a directory");
281 return -BCH_ERR_ENOENT_not_directory;
285 * The bch2_check_dirents pass has already run, dangling dirents
286 * shouldn't exist here:
288 return __lookup_inode(trans, inum, lostfound, &snapshot);
291 bch2_inode_init_early(c, lostfound);
293 ret = bch2_create_trans(trans, root_inum, &root,
294 lostfound, &lostfound_str,
295 0, 0, S_IFDIR|0700, 0, NULL, NULL,
296 (subvol_inum) { }, 0);
297 bch_err_msg(c, ret, "creating lost+found");
301 static int __reattach_inode(struct btree_trans *trans,
302 struct bch_inode_unpacked *inode,
305 struct bch_hash_info dir_hash;
306 struct bch_inode_unpacked lostfound;
313 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
317 ret = lookup_lostfound(trans, subvol, &lostfound);
321 if (S_ISDIR(inode->bi_mode)) {
322 lostfound.bi_nlink++;
324 ret = __write_inode(trans, &lostfound, U32_MAX);
329 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
331 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
332 name = (struct qstr) QSTR(name_buf);
334 ret = bch2_dirent_create(trans,
337 .inum = lostfound.bi_inum,
341 &name, inode->bi_inum, &dir_offset,
342 BCH_HASH_SET_MUST_CREATE);
346 inode->bi_dir = lostfound.bi_inum;
347 inode->bi_dir_offset = dir_offset;
349 return __write_inode(trans, inode, inode_snapshot);
352 static int reattach_inode(struct btree_trans *trans,
353 struct bch_inode_unpacked *inode,
356 int ret = commit_do(trans, NULL, NULL,
357 BCH_TRANS_COMMIT_lazy_rw|
358 BCH_TRANS_COMMIT_no_enospc,
359 __reattach_inode(trans, inode, inode_snapshot));
360 bch_err_msg(trans->c, ret, "reattaching inode %llu", inode->bi_inum);
364 static int remove_backpointer(struct btree_trans *trans,
365 struct bch_inode_unpacked *inode)
367 struct btree_iter iter;
368 struct bkey_s_c_dirent d;
371 d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
372 POS(inode->bi_dir, inode->bi_dir_offset), 0,
375 __remove_dirent(trans, d.k->p);
376 bch2_trans_iter_exit(trans, &iter);
380 struct snapshots_seen_entry {
385 struct snapshots_seen {
387 DARRAY(struct snapshots_seen_entry) ids;
390 static inline void snapshots_seen_exit(struct snapshots_seen *s)
392 darray_exit(&s->ids);
395 static inline void snapshots_seen_init(struct snapshots_seen *s)
397 memset(s, 0, sizeof(*s));
400 static int snapshots_seen_add_inorder(struct bch_fs *c, struct snapshots_seen *s, u32 id)
402 struct snapshots_seen_entry *i, n = {
404 .equiv = bch2_snapshot_equiv(c, id),
408 darray_for_each(s->ids, i) {
415 ret = darray_insert_item(&s->ids, i - s->ids.data, n);
417 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
422 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
423 enum btree_id btree_id, struct bpos pos)
425 struct snapshots_seen_entry *i, n = {
427 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
431 if (!bkey_eq(s->pos, pos))
435 s->pos.snapshot = n.equiv;
437 darray_for_each(s->ids, i) {
442 * We currently don't rigorously track for snapshot cleanup
443 * needing to be run, so it shouldn't be a fsck error yet:
445 if (i->equiv == n.equiv) {
446 bch_err(c, "snapshot deletion did not finish:\n"
447 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
448 bch2_btree_id_str(btree_id),
449 pos.inode, pos.offset,
450 i->id, n.id, n.equiv);
451 set_bit(BCH_FS_NEED_DELETE_DEAD_SNAPSHOTS, &c->flags);
452 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_delete_dead_snapshots);
456 ret = darray_push(&s->ids, n);
458 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
464 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
465 * and @ancestor hasn't been overwritten in @seen
467 * @c: filesystem handle
468 * @seen: list of snapshot ids already seen at current position
469 * @id: descendent snapshot id
470 * @ancestor: ancestor snapshot id
472 * Returns: whether key in @ancestor snapshot is visible in @id snapshot
474 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
475 u32 id, u32 ancestor)
479 EBUG_ON(id > ancestor);
480 EBUG_ON(!bch2_snapshot_is_equiv(c, id));
481 EBUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
483 /* @ancestor should be the snapshot most recently added to @seen */
484 EBUG_ON(ancestor != seen->pos.snapshot);
485 EBUG_ON(ancestor != seen->ids.data[seen->ids.nr - 1].equiv);
490 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
494 * We know that @id is a descendant of @ancestor, we're checking if
495 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
496 * @ascestor and with @id as a descendent.
498 * But we already know that we're scanning IDs between @id and @ancestor
499 * numerically, since snapshot ID lists are kept sorted, so if we find
500 * an id that's an ancestor of @id we're done:
503 for (i = seen->ids.nr - 2;
504 i >= 0 && seen->ids.data[i].equiv >= id;
506 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv))
513 * ref_visible - given a key with snapshot id @src that points to a key with
514 * snapshot id @dst, test whether there is some snapshot in which @dst is
517 * @c: filesystem handle
518 * @s: list of snapshot IDs already seen at @src
519 * @src: snapshot ID of src key
520 * @dst: snapshot ID of dst key
521 * Returns: true if there is some snapshot in which @dst is visible
523 * Assumes we're visiting @src keys in natural key order
525 static bool ref_visible(struct bch_fs *c, struct snapshots_seen *s,
529 ? key_visible_in_snapshot(c, s, dst, src)
530 : bch2_snapshot_is_ancestor(c, src, dst);
533 static int ref_visible2(struct bch_fs *c,
534 u32 src, struct snapshots_seen *src_seen,
535 u32 dst, struct snapshots_seen *dst_seen)
537 src = bch2_snapshot_equiv(c, src);
538 dst = bch2_snapshot_equiv(c, dst);
542 swap(dst_seen, src_seen);
544 return key_visible_in_snapshot(c, src_seen, dst, src);
547 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
548 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
549 (_i)->snapshot <= (_snapshot); _i++) \
550 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
552 struct inode_walker_entry {
553 struct bch_inode_unpacked inode;
559 struct inode_walker {
560 bool first_this_inode;
561 bool recalculate_sums;
562 struct bpos last_pos;
564 DARRAY(struct inode_walker_entry) inodes;
567 static void inode_walker_exit(struct inode_walker *w)
569 darray_exit(&w->inodes);
572 static struct inode_walker inode_walker_init(void)
574 return (struct inode_walker) { 0, };
577 static int add_inode(struct bch_fs *c, struct inode_walker *w,
578 struct bkey_s_c inode)
580 struct bch_inode_unpacked u;
582 BUG_ON(bch2_inode_unpack(inode, &u));
584 return darray_push(&w->inodes, ((struct inode_walker_entry) {
586 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
590 static int get_inodes_all_snapshots(struct btree_trans *trans,
591 struct inode_walker *w, u64 inum)
593 struct bch_fs *c = trans->c;
594 struct btree_iter iter;
596 u32 restart_count = trans->restart_count;
599 w->recalculate_sums = false;
602 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
603 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
604 if (k.k->p.offset != inum)
607 if (bkey_is_inode(k.k))
610 bch2_trans_iter_exit(trans, &iter);
615 w->first_this_inode = true;
617 return trans_was_restarted(trans, restart_count);
620 static struct inode_walker_entry *
621 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w,
622 u32 snapshot, bool is_whiteout)
624 struct inode_walker_entry *i;
626 snapshot = bch2_snapshot_equiv(c, snapshot);
628 darray_for_each(w->inodes, i)
629 if (bch2_snapshot_is_ancestor(c, snapshot, i->snapshot))
634 BUG_ON(snapshot > i->snapshot);
636 if (snapshot != i->snapshot && !is_whiteout) {
637 struct inode_walker_entry new = *i;
641 new.snapshot = snapshot;
644 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
645 w->last_pos.inode, snapshot, i->snapshot);
647 while (i > w->inodes.data && i[-1].snapshot > snapshot)
650 pos = i - w->inodes.data;
651 ret = darray_insert_item(&w->inodes, pos, new);
655 i = w->inodes.data + pos;
661 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
662 struct inode_walker *w, struct bpos pos,
665 if (w->last_pos.inode != pos.inode) {
666 int ret = get_inodes_all_snapshots(trans, w, pos.inode);
669 } else if (bkey_cmp(w->last_pos, pos)) {
670 struct inode_walker_entry *i;
672 darray_for_each(w->inodes, i)
673 i->seen_this_pos = false;
679 return lookup_inode_for_snapshot(trans->c, w, pos.snapshot, is_whiteout);
682 static int __get_visible_inodes(struct btree_trans *trans,
683 struct inode_walker *w,
684 struct snapshots_seen *s,
687 struct bch_fs *c = trans->c;
688 struct btree_iter iter;
694 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
695 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
696 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
698 if (k.k->p.offset != inum)
701 if (!ref_visible(c, s, s->pos.snapshot, equiv))
704 if (bkey_is_inode(k.k))
707 if (equiv >= s->pos.snapshot)
710 bch2_trans_iter_exit(trans, &iter);
715 static int check_key_has_snapshot(struct btree_trans *trans,
716 struct btree_iter *iter,
719 struct bch_fs *c = trans->c;
720 struct printbuf buf = PRINTBUF;
723 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
724 bkey_in_missing_snapshot,
725 "key in missing snapshot: %s",
726 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
727 ret = bch2_btree_delete_at(trans, iter,
728 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
734 static int hash_redo_key(struct btree_trans *trans,
735 const struct bch_hash_desc desc,
736 struct bch_hash_info *hash_info,
737 struct btree_iter *k_iter, struct bkey_s_c k)
739 struct bkey_i *delete;
742 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
744 return PTR_ERR(delete);
746 tmp = bch2_bkey_make_mut_noupdate(trans, k);
750 bkey_init(&delete->k);
751 delete->k.p = k_iter->pos;
752 return bch2_btree_iter_traverse(k_iter) ?:
753 bch2_trans_update(trans, k_iter, delete, 0) ?:
754 bch2_hash_set_snapshot(trans, desc, hash_info,
755 (subvol_inum) { 0, k.k->p.inode },
756 k.k->p.snapshot, tmp,
757 BCH_HASH_SET_MUST_CREATE,
758 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
759 bch2_trans_commit(trans, NULL, NULL,
760 BCH_TRANS_COMMIT_no_enospc|
761 BCH_TRANS_COMMIT_lazy_rw);
764 static int hash_check_key(struct btree_trans *trans,
765 const struct bch_hash_desc desc,
766 struct bch_hash_info *hash_info,
767 struct btree_iter *k_iter, struct bkey_s_c hash_k)
769 struct bch_fs *c = trans->c;
770 struct btree_iter iter = { NULL };
771 struct printbuf buf = PRINTBUF;
776 if (hash_k.k->type != desc.key_type)
779 hash = desc.hash_bkey(hash_info, hash_k);
781 if (likely(hash == hash_k.k->p.offset))
784 if (hash_k.k->p.offset < hash)
787 for_each_btree_key_norestart(trans, iter, desc.btree_id,
788 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
789 BTREE_ITER_SLOTS, k, ret) {
790 if (bkey_eq(k.k->p, hash_k.k->p))
793 if (fsck_err_on(k.k->type == desc.key_type &&
794 !desc.cmp_bkey(k, hash_k), c,
795 hash_table_key_duplicate,
796 "duplicate hash table keys:\n%s",
797 (printbuf_reset(&buf),
798 bch2_bkey_val_to_text(&buf, c, hash_k),
800 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
804 if (bkey_deleted(k.k)) {
805 bch2_trans_iter_exit(trans, &iter);
810 bch2_trans_iter_exit(trans, &iter);
814 if (fsck_err(c, hash_table_key_wrong_offset,
815 "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
816 bch2_btree_id_str(desc.btree_id), hash_k.k->p.inode, hash_k.k->p.offset, hash,
817 (printbuf_reset(&buf),
818 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
819 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
823 ret = -BCH_ERR_transaction_restart_nested;
829 static int check_inode(struct btree_trans *trans,
830 struct btree_iter *iter,
832 struct bch_inode_unpacked *prev,
833 struct snapshots_seen *s,
836 struct bch_fs *c = trans->c;
837 struct bch_inode_unpacked u;
838 bool do_update = false;
841 ret = check_key_has_snapshot(trans, iter, k);
847 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
851 if (!bkey_is_inode(k.k))
854 BUG_ON(bch2_inode_unpack(k, &u));
857 !(u.bi_flags & (BCH_INODE_i_size_dirty|
858 BCH_INODE_i_sectors_dirty|
859 BCH_INODE_unlinked)))
862 if (prev->bi_inum != u.bi_inum)
865 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
866 inode_d_type(prev) != inode_d_type(&u),
867 c, inode_snapshot_mismatch,
868 "inodes in different snapshots don't match")) {
869 bch_err(c, "repair not implemented yet");
873 if ((u.bi_flags & (BCH_INODE_i_size_dirty|BCH_INODE_unlinked)) &&
874 bch2_key_has_snapshot_overwrites(trans, BTREE_ID_inodes, k.k->p)) {
875 struct bpos new_min_pos;
877 ret = bch2_propagate_key_to_snapshot_leaves(trans, iter->btree_id, k, &new_min_pos);
881 u.bi_flags &= ~BCH_INODE_i_size_dirty|BCH_INODE_unlinked;
883 ret = __write_inode(trans, &u, iter->pos.snapshot);
884 bch_err_msg(c, ret, "in fsck updating inode");
888 if (!bpos_eq(new_min_pos, POS_MIN))
889 bch2_btree_iter_set_pos(iter, bpos_predecessor(new_min_pos));
893 if (u.bi_flags & BCH_INODE_unlinked &&
895 fsck_err(c, inode_unlinked_but_clean,
896 "filesystem marked clean, but inode %llu unlinked",
898 bch2_trans_unlock(trans);
901 ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
902 bch_err_msg(c, ret, "in fsck deleting inode");
906 if (u.bi_flags & BCH_INODE_i_size_dirty &&
908 fsck_err(c, inode_i_size_dirty_but_clean,
909 "filesystem marked clean, but inode %llu has i_size dirty",
911 bch_verbose(c, "truncating inode %llu", u.bi_inum);
913 bch2_trans_unlock(trans);
917 * XXX: need to truncate partial blocks too here - or ideally
918 * just switch units to bytes and that issue goes away
920 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
921 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
923 POS(u.bi_inum, U64_MAX),
925 bch_err_msg(c, ret, "in fsck truncating inode");
930 * We truncated without our normal sector accounting hook, just
931 * make sure we recalculate it:
933 u.bi_flags |= BCH_INODE_i_sectors_dirty;
935 u.bi_flags &= ~BCH_INODE_i_size_dirty;
939 if (u.bi_flags & BCH_INODE_i_sectors_dirty &&
941 fsck_err(c, inode_i_sectors_dirty_but_clean,
942 "filesystem marked clean, but inode %llu has i_sectors dirty",
946 bch_verbose(c, "recounting sectors for inode %llu",
949 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
951 bch_err_msg(c, sectors, "in fsck recounting inode sectors");
955 u.bi_sectors = sectors;
956 u.bi_flags &= ~BCH_INODE_i_sectors_dirty;
960 if (u.bi_flags & BCH_INODE_backptr_untrusted) {
963 u.bi_flags &= ~BCH_INODE_backptr_untrusted;
968 ret = __write_inode(trans, &u, iter->pos.snapshot);
969 bch_err_msg(c, ret, "in fsck updating inode");
980 int bch2_check_inodes(struct bch_fs *c)
982 bool full = c->opts.fsck;
983 struct btree_trans *trans = bch2_trans_get(c);
984 struct btree_iter iter;
985 struct bch_inode_unpacked prev = { 0 };
986 struct snapshots_seen s;
990 snapshots_seen_init(&s);
992 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
994 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
995 NULL, NULL, BCH_TRANS_COMMIT_lazy_rw|BCH_TRANS_COMMIT_no_enospc,
996 check_inode(trans, &iter, k, &prev, &s, full));
998 snapshots_seen_exit(&s);
999 bch2_trans_put(trans);
1004 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1005 struct btree_iter *iter,
1008 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
1011 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1012 struct bkey_s_c_dirent d)
1014 return inode->bi_dir == d.k->p.inode &&
1015 inode->bi_dir_offset == d.k->p.offset;
1018 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1019 struct bch_inode_unpacked *inode)
1021 return d.v->d_type == DT_SUBVOL
1022 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1023 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1026 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1028 struct bch_fs *c = trans->c;
1029 struct inode_walker_entry *i;
1030 u32 restart_count = trans->restart_count;
1034 darray_for_each(w->inodes, i) {
1035 if (i->inode.bi_sectors == i->count)
1038 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1040 if (w->recalculate_sums)
1043 if (i->count != count2) {
1044 bch_err(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1045 w->last_pos.inode, i->snapshot, i->count, count2);
1046 return -BCH_ERR_internal_fsck_err;
1049 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_sectors_dirty),
1050 c, inode_i_sectors_wrong,
1051 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1052 w->last_pos.inode, i->snapshot,
1053 i->inode.bi_sectors, i->count)) {
1054 i->inode.bi_sectors = i->count;
1055 ret = fsck_write_inode(trans, &i->inode, i->snapshot);
1062 return ret ?: trans_was_restarted(trans, restart_count);
1068 struct snapshots_seen seen;
1071 struct extent_ends {
1072 struct bpos last_pos;
1073 DARRAY(struct extent_end) e;
1076 static void extent_ends_reset(struct extent_ends *extent_ends)
1078 struct extent_end *i;
1080 darray_for_each(extent_ends->e, i)
1081 snapshots_seen_exit(&i->seen);
1083 extent_ends->e.nr = 0;
1086 static void extent_ends_exit(struct extent_ends *extent_ends)
1088 extent_ends_reset(extent_ends);
1089 darray_exit(&extent_ends->e);
1092 static void extent_ends_init(struct extent_ends *extent_ends)
1094 memset(extent_ends, 0, sizeof(*extent_ends));
1097 static int extent_ends_at(struct bch_fs *c,
1098 struct extent_ends *extent_ends,
1099 struct snapshots_seen *seen,
1102 struct extent_end *i, n = (struct extent_end) {
1103 .offset = k.k->p.offset,
1104 .snapshot = k.k->p.snapshot,
1108 n.seen.ids.data = kmemdup(seen->ids.data,
1109 sizeof(seen->ids.data[0]) * seen->ids.size,
1111 if (!n.seen.ids.data)
1112 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1114 darray_for_each(extent_ends->e, i) {
1115 if (i->snapshot == k.k->p.snapshot) {
1116 snapshots_seen_exit(&i->seen);
1121 if (i->snapshot >= k.k->p.snapshot)
1125 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1128 static int overlapping_extents_found(struct btree_trans *trans,
1129 enum btree_id btree,
1130 struct bpos pos1, struct snapshots_seen *pos1_seen,
1133 struct extent_end *extent_end)
1135 struct bch_fs *c = trans->c;
1136 struct printbuf buf = PRINTBUF;
1137 struct btree_iter iter1, iter2 = { NULL };
1138 struct bkey_s_c k1, k2;
1141 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1143 bch2_trans_iter_init(trans, &iter1, btree, pos1,
1144 BTREE_ITER_ALL_SNAPSHOTS|
1145 BTREE_ITER_NOT_EXTENTS);
1146 k1 = bch2_btree_iter_peek_upto(&iter1, POS(pos1.inode, U64_MAX));
1151 prt_str(&buf, "\n ");
1152 bch2_bkey_val_to_text(&buf, c, k1);
1154 if (!bpos_eq(pos1, k1.k->p)) {
1155 prt_str(&buf, "\n wanted\n ");
1156 bch2_bpos_to_text(&buf, pos1);
1157 prt_str(&buf, "\n ");
1158 bch2_bkey_to_text(&buf, &pos2);
1160 bch_err(c, "%s: error finding first overlapping extent when repairing, got%s",
1162 ret = -BCH_ERR_internal_fsck_err;
1166 bch2_trans_copy_iter(&iter2, &iter1);
1169 bch2_btree_iter_advance(&iter2);
1171 k2 = bch2_btree_iter_peek_upto(&iter2, POS(pos1.inode, U64_MAX));
1176 if (bpos_ge(k2.k->p, pos2.p))
1180 prt_str(&buf, "\n ");
1181 bch2_bkey_val_to_text(&buf, c, k2);
1183 if (bpos_gt(k2.k->p, pos2.p) ||
1184 pos2.size != k2.k->size) {
1185 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1187 ret = -BCH_ERR_internal_fsck_err;
1191 prt_printf(&buf, "\n overwriting %s extent",
1192 pos1.snapshot >= pos2.p.snapshot ? "first" : "second");
1194 if (fsck_err(c, extent_overlapping,
1195 "overlapping extents%s", buf.buf)) {
1196 struct btree_iter *old_iter = &iter1;
1197 struct disk_reservation res = { 0 };
1199 if (pos1.snapshot < pos2.p.snapshot) {
1204 trans->extra_journal_res += bch2_bkey_sectors_compressed(k2);
1206 ret = bch2_trans_update_extent_overwrite(trans, old_iter,
1207 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE,
1209 bch2_trans_commit(trans, &res, NULL,
1210 BCH_TRANS_COMMIT_lazy_rw|BCH_TRANS_COMMIT_no_enospc);
1211 bch2_disk_reservation_put(c, &res);
1218 if (pos1.snapshot == pos2.p.snapshot) {
1220 * We overwrote the first extent, and did the overwrite
1221 * in the same snapshot:
1223 extent_end->offset = bkey_start_offset(&pos2);
1224 } else if (pos1.snapshot > pos2.p.snapshot) {
1226 * We overwrote the first extent in pos2's snapshot:
1228 ret = snapshots_seen_add_inorder(c, pos1_seen, pos2.p.snapshot);
1231 * We overwrote the second extent - restart
1232 * check_extent() from the top:
1234 ret = -BCH_ERR_transaction_restart_nested;
1239 bch2_trans_iter_exit(trans, &iter2);
1240 bch2_trans_iter_exit(trans, &iter1);
1241 printbuf_exit(&buf);
1245 static int check_overlapping_extents(struct btree_trans *trans,
1246 struct snapshots_seen *seen,
1247 struct extent_ends *extent_ends,
1250 struct btree_iter *iter,
1253 struct bch_fs *c = trans->c;
1254 struct extent_end *i;
1257 /* transaction restart, running again */
1258 if (bpos_eq(extent_ends->last_pos, k.k->p))
1261 if (extent_ends->last_pos.inode != k.k->p.inode)
1262 extent_ends_reset(extent_ends);
1264 darray_for_each(extent_ends->e, i) {
1265 if (i->offset <= bkey_start_offset(k.k))
1268 if (!ref_visible2(c,
1269 k.k->p.snapshot, seen,
1270 i->snapshot, &i->seen))
1273 ret = overlapping_extents_found(trans, iter->btree_id,
1274 SPOS(iter->pos.inode,
1283 ret = extent_ends_at(c, extent_ends, seen, k);
1287 extent_ends->last_pos = k.k->p;
1292 static int check_extent_overbig(struct btree_trans *trans, struct btree_iter *iter,
1295 struct bch_fs *c = trans->c;
1296 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1297 struct bch_extent_crc_unpacked crc;
1298 const union bch_extent_entry *i;
1299 unsigned encoded_extent_max_sectors = c->opts.encoded_extent_max >> 9;
1301 bkey_for_each_crc(k.k, ptrs, crc, i)
1302 if (crc_is_encoded(crc) &&
1303 crc.uncompressed_size > encoded_extent_max_sectors) {
1304 struct printbuf buf = PRINTBUF;
1306 bch2_bkey_val_to_text(&buf, c, k);
1307 bch_err(c, "overbig encoded extent, please report this:\n %s", buf.buf);
1308 printbuf_exit(&buf);
1314 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1316 struct inode_walker *inode,
1317 struct snapshots_seen *s,
1318 struct extent_ends *extent_ends)
1320 struct bch_fs *c = trans->c;
1321 struct inode_walker_entry *i;
1322 struct printbuf buf = PRINTBUF;
1323 struct bpos equiv = k.k->p;
1326 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1328 ret = check_key_has_snapshot(trans, iter, k);
1330 ret = ret < 0 ? ret : 0;
1334 if (inode->last_pos.inode != k.k->p.inode) {
1335 ret = check_i_sectors(trans, inode);
1340 i = walk_inode(trans, inode, equiv, k.k->type == KEY_TYPE_whiteout);
1341 ret = PTR_ERR_OR_ZERO(i);
1345 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1349 if (k.k->type != KEY_TYPE_whiteout) {
1350 if (fsck_err_on(!i, c, extent_in_missing_inode,
1351 "extent in missing inode:\n %s",
1352 (printbuf_reset(&buf),
1353 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1356 if (fsck_err_on(i &&
1357 !S_ISREG(i->inode.bi_mode) &&
1358 !S_ISLNK(i->inode.bi_mode),
1359 c, extent_in_non_reg_inode,
1360 "extent in non regular inode mode %o:\n %s",
1362 (printbuf_reset(&buf),
1363 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1366 ret = check_overlapping_extents(trans, s, extent_ends, k,
1367 equiv.snapshot, iter,
1368 &inode->recalculate_sums);
1374 * Check inodes in reverse order, from oldest snapshots to newest,
1375 * starting from the inode that matches this extent's snapshot. If we
1376 * didn't have one, iterate over all inodes:
1379 i = inode->inodes.data + inode->inodes.nr - 1;
1382 inode->inodes.data && i >= inode->inodes.data;
1384 if (i->snapshot > equiv.snapshot ||
1385 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1388 if (k.k->type != KEY_TYPE_whiteout) {
1389 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_size_dirty) &&
1390 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1391 !bkey_extent_is_reservation(k),
1392 c, extent_past_end_of_inode,
1393 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1394 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1395 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1396 struct btree_iter iter2;
1398 bch2_trans_copy_iter(&iter2, iter);
1399 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1400 ret = bch2_btree_iter_traverse(&iter2) ?:
1401 bch2_btree_delete_at(trans, &iter2,
1402 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1403 bch2_trans_iter_exit(trans, &iter2);
1407 iter->k.type = KEY_TYPE_whiteout;
1410 if (bkey_extent_is_allocation(k.k))
1411 i->count += k.k->size;
1414 i->seen_this_pos = true;
1419 printbuf_exit(&buf);
1423 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1428 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1429 * that i_size an i_sectors are consistent
1431 int bch2_check_extents(struct bch_fs *c)
1433 struct inode_walker w = inode_walker_init();
1434 struct snapshots_seen s;
1435 struct btree_trans *trans = bch2_trans_get(c);
1436 struct btree_iter iter;
1438 struct extent_ends extent_ends;
1439 struct disk_reservation res = { 0 };
1442 snapshots_seen_init(&s);
1443 extent_ends_init(&extent_ends);
1445 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_extents,
1446 POS(BCACHEFS_ROOT_INO, 0),
1447 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1449 BCH_TRANS_COMMIT_lazy_rw|BCH_TRANS_COMMIT_no_enospc, ({
1450 bch2_disk_reservation_put(c, &res);
1451 check_extent(trans, &iter, k, &w, &s, &extent_ends) ?:
1452 check_extent_overbig(trans, &iter, k);
1454 check_i_sectors(trans, &w);
1456 bch2_disk_reservation_put(c, &res);
1457 extent_ends_exit(&extent_ends);
1458 inode_walker_exit(&w);
1459 snapshots_seen_exit(&s);
1460 bch2_trans_put(trans);
1466 int bch2_check_indirect_extents(struct bch_fs *c)
1468 struct btree_trans *trans = bch2_trans_get(c);
1469 struct btree_iter iter;
1471 struct disk_reservation res = { 0 };
1474 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_reflink,
1476 BTREE_ITER_PREFETCH, k,
1478 BCH_TRANS_COMMIT_lazy_rw|BCH_TRANS_COMMIT_no_enospc, ({
1479 bch2_disk_reservation_put(c, &res);
1480 check_extent_overbig(trans, &iter, k);
1483 bch2_disk_reservation_put(c, &res);
1484 bch2_trans_put(trans);
1490 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1492 struct bch_fs *c = trans->c;
1493 struct inode_walker_entry *i;
1494 u32 restart_count = trans->restart_count;
1498 darray_for_each(w->inodes, i) {
1499 if (i->inode.bi_nlink == i->count)
1502 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1506 if (i->count != count2) {
1507 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1510 if (i->inode.bi_nlink == i->count)
1514 if (fsck_err_on(i->inode.bi_nlink != i->count,
1515 c, inode_dir_wrong_nlink,
1516 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1517 w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1518 i->inode.bi_nlink = i->count;
1519 ret = fsck_write_inode(trans, &i->inode, i->snapshot);
1526 return ret ?: trans_was_restarted(trans, restart_count);
1529 static int check_dirent_target(struct btree_trans *trans,
1530 struct btree_iter *iter,
1531 struct bkey_s_c_dirent d,
1532 struct bch_inode_unpacked *target,
1533 u32 target_snapshot)
1535 struct bch_fs *c = trans->c;
1536 struct bkey_i_dirent *n;
1537 struct printbuf buf = PRINTBUF;
1538 struct btree_iter bp_iter = { NULL };
1541 if (!target->bi_dir &&
1542 !target->bi_dir_offset) {
1543 target->bi_dir = d.k->p.inode;
1544 target->bi_dir_offset = d.k->p.offset;
1546 ret = __write_inode(trans, target, target_snapshot);
1551 if (!inode_points_to_dirent(target, d)) {
1552 struct bkey_s_c_dirent bp_dirent = dirent_get_by_pos(trans, &bp_iter,
1553 SPOS(target->bi_dir, target->bi_dir_offset, target_snapshot));
1554 ret = bkey_err(bp_dirent);
1555 if (ret && !bch2_err_matches(ret, ENOENT))
1558 bool backpointer_exists = !ret;
1561 bch2_bkey_val_to_text(&buf, c, d.s_c);
1563 if (backpointer_exists)
1564 bch2_bkey_val_to_text(&buf, c, bp_dirent.s_c);
1566 if (fsck_err_on(S_ISDIR(target->bi_mode) && backpointer_exists,
1567 c, inode_dir_multiple_links,
1568 "directory %llu:%u with multiple links\n%s",
1569 target->bi_inum, target_snapshot, buf.buf)) {
1570 ret = __remove_dirent(trans, d.k->p);
1575 * hardlinked file with nlink 0:
1576 * We're just adjusting nlink here so check_nlinks() will pick
1577 * it up, it ignores inodes with nlink 0
1579 if (fsck_err_on(backpointer_exists && !target->bi_nlink,
1580 c, inode_multiple_links_but_nlink_0,
1581 "inode %llu:%u type %s has multiple links but i_nlink 0\n%s",
1582 target->bi_inum, target_snapshot, bch2_d_types[d.v->d_type], buf.buf)) {
1584 target->bi_flags &= ~BCH_INODE_unlinked;
1586 ret = __write_inode(trans, target, target_snapshot);
1591 if (fsck_err_on(!backpointer_exists,
1592 c, inode_wrong_backpointer,
1593 "inode %llu:%u has wrong backpointer:\n"
1595 "should be %llu:%llu",
1596 target->bi_inum, target_snapshot,
1598 target->bi_dir_offset,
1601 target->bi_dir = d.k->p.inode;
1602 target->bi_dir_offset = d.k->p.offset;
1604 ret = __write_inode(trans, target, target_snapshot);
1610 if (fsck_err_on(d.v->d_type != inode_d_type(target),
1611 c, dirent_d_type_wrong,
1612 "incorrect d_type: got %s, should be %s:\n%s",
1613 bch2_d_type_str(d.v->d_type),
1614 bch2_d_type_str(inode_d_type(target)),
1615 (printbuf_reset(&buf),
1616 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1617 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1618 ret = PTR_ERR_OR_ZERO(n);
1622 bkey_reassemble(&n->k_i, d.s_c);
1623 n->v.d_type = inode_d_type(target);
1625 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1629 d = dirent_i_to_s_c(n);
1632 if (d.v->d_type == DT_SUBVOL &&
1633 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1634 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1635 fsck_err(c, dirent_d_parent_subvol_wrong,
1636 "dirent has wrong d_parent_subvol field: got %u, should be %u",
1637 le32_to_cpu(d.v->d_parent_subvol),
1638 target->bi_parent_subvol))) {
1639 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1640 ret = PTR_ERR_OR_ZERO(n);
1644 bkey_reassemble(&n->k_i, d.s_c);
1645 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1647 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1651 d = dirent_i_to_s_c(n);
1656 bch2_trans_iter_exit(trans, &bp_iter);
1657 printbuf_exit(&buf);
1662 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1664 struct bch_hash_info *hash_info,
1665 struct inode_walker *dir,
1666 struct inode_walker *target,
1667 struct snapshots_seen *s)
1669 struct bch_fs *c = trans->c;
1670 struct bkey_s_c_dirent d;
1671 struct inode_walker_entry *i;
1672 struct printbuf buf = PRINTBUF;
1676 ret = check_key_has_snapshot(trans, iter, k);
1678 ret = ret < 0 ? ret : 0;
1683 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1685 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1689 if (k.k->type == KEY_TYPE_whiteout)
1692 if (dir->last_pos.inode != k.k->p.inode) {
1693 ret = check_subdir_count(trans, dir);
1698 BUG_ON(!iter->path->should_be_locked);
1700 i = walk_inode(trans, dir, equiv, k.k->type == KEY_TYPE_whiteout);
1701 ret = PTR_ERR_OR_ZERO(i);
1705 if (dir->first_this_inode && dir->inodes.nr)
1706 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1707 dir->first_this_inode = false;
1709 if (fsck_err_on(!i, c, dirent_in_missing_dir_inode,
1710 "dirent in nonexisting directory:\n%s",
1711 (printbuf_reset(&buf),
1712 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1713 ret = bch2_btree_delete_at(trans, iter,
1714 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1721 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode),
1722 c, dirent_in_non_dir_inode,
1723 "dirent in non directory inode type %s:\n%s",
1724 bch2_d_type_str(inode_d_type(&i->inode)),
1725 (printbuf_reset(&buf),
1726 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1727 ret = bch2_btree_delete_at(trans, iter, 0);
1731 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1735 /* dirent has been deleted */
1740 if (k.k->type != KEY_TYPE_dirent)
1743 d = bkey_s_c_to_dirent(k);
1745 if (d.v->d_type == DT_SUBVOL) {
1746 struct bch_inode_unpacked subvol_root;
1747 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1748 u32 target_snapshot;
1751 ret = __subvol_lookup(trans, target_subvol,
1752 &target_snapshot, &target_inum);
1753 if (ret && !bch2_err_matches(ret, ENOENT))
1756 if (fsck_err_on(ret, c, dirent_to_missing_subvol,
1757 "dirent points to missing subvolume %u",
1758 le32_to_cpu(d.v->d_child_subvol))) {
1759 ret = __remove_dirent(trans, d.k->p);
1763 ret = __lookup_inode(trans, target_inum,
1764 &subvol_root, &target_snapshot);
1765 if (ret && !bch2_err_matches(ret, ENOENT))
1768 if (fsck_err_on(ret, c, subvol_to_missing_root,
1769 "subvolume %u points to missing subvolume root %llu",
1772 bch_err(c, "repair not implemented yet");
1777 if (fsck_err_on(subvol_root.bi_subvol != target_subvol,
1778 c, subvol_root_wrong_bi_subvol,
1779 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1781 subvol_root.bi_subvol, target_subvol)) {
1782 subvol_root.bi_subvol = target_subvol;
1783 ret = __write_inode(trans, &subvol_root, target_snapshot);
1788 ret = check_dirent_target(trans, iter, d, &subvol_root,
1793 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1797 if (fsck_err_on(!target->inodes.nr,
1798 c, dirent_to_missing_inode,
1799 "dirent points to missing inode: (equiv %u)\n%s",
1801 (printbuf_reset(&buf),
1802 bch2_bkey_val_to_text(&buf, c, k),
1804 ret = __remove_dirent(trans, d.k->p);
1809 darray_for_each(target->inodes, i) {
1810 ret = check_dirent_target(trans, iter, d,
1811 &i->inode, i->snapshot);
1817 if (d.v->d_type == DT_DIR)
1818 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1824 printbuf_exit(&buf);
1830 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1833 int bch2_check_dirents(struct bch_fs *c)
1835 struct inode_walker dir = inode_walker_init();
1836 struct inode_walker target = inode_walker_init();
1837 struct snapshots_seen s;
1838 struct bch_hash_info hash_info;
1839 struct btree_trans *trans = bch2_trans_get(c);
1840 struct btree_iter iter;
1844 snapshots_seen_init(&s);
1846 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_dirents,
1847 POS(BCACHEFS_ROOT_INO, 0),
1848 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1851 BCH_TRANS_COMMIT_lazy_rw|BCH_TRANS_COMMIT_no_enospc,
1852 check_dirent(trans, &iter, k, &hash_info, &dir, &target, &s));
1854 bch2_trans_put(trans);
1855 snapshots_seen_exit(&s);
1856 inode_walker_exit(&dir);
1857 inode_walker_exit(&target);
1862 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1864 struct bch_hash_info *hash_info,
1865 struct inode_walker *inode)
1867 struct bch_fs *c = trans->c;
1868 struct inode_walker_entry *i;
1871 ret = check_key_has_snapshot(trans, iter, k);
1875 i = walk_inode(trans, inode, k.k->p, k.k->type == KEY_TYPE_whiteout);
1876 ret = PTR_ERR_OR_ZERO(i);
1880 if (inode->first_this_inode && inode->inodes.nr)
1881 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1882 inode->first_this_inode = false;
1884 if (fsck_err_on(!i, c, xattr_in_missing_inode,
1885 "xattr for missing inode %llu",
1887 return bch2_btree_delete_at(trans, iter, 0);
1892 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1899 * Walk xattrs: verify that they all have a corresponding inode
1901 int bch2_check_xattrs(struct bch_fs *c)
1903 struct inode_walker inode = inode_walker_init();
1904 struct bch_hash_info hash_info;
1905 struct btree_iter iter;
1909 ret = bch2_trans_run(c,
1910 for_each_btree_key_commit(trans, iter, BTREE_ID_xattrs,
1911 POS(BCACHEFS_ROOT_INO, 0),
1912 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1915 BCH_TRANS_COMMIT_lazy_rw|BCH_TRANS_COMMIT_no_enospc,
1916 check_xattr(trans, &iter, k, &hash_info, &inode)));
1921 static int check_root_trans(struct btree_trans *trans)
1923 struct bch_fs *c = trans->c;
1924 struct bch_inode_unpacked root_inode;
1929 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1930 if (ret && !bch2_err_matches(ret, ENOENT))
1933 if (mustfix_fsck_err_on(ret, c, root_subvol_missing,
1934 "root subvol missing")) {
1935 struct bkey_i_subvolume root_subvol;
1938 inum = BCACHEFS_ROOT_INO;
1940 bkey_subvolume_init(&root_subvol.k_i);
1941 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1942 root_subvol.v.flags = 0;
1943 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1944 root_subvol.v.inode = cpu_to_le64(inum);
1945 ret = commit_do(trans, NULL, NULL,
1946 BCH_TRANS_COMMIT_no_enospc|
1947 BCH_TRANS_COMMIT_lazy_rw,
1948 bch2_btree_insert_trans(trans, BTREE_ID_subvolumes,
1949 &root_subvol.k_i, 0));
1950 bch_err_msg(c, ret, "writing root subvol");
1956 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1957 if (ret && !bch2_err_matches(ret, ENOENT))
1960 if (mustfix_fsck_err_on(ret, c, root_dir_missing,
1961 "root directory missing") ||
1962 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode),
1963 c, root_inode_not_dir,
1964 "root inode not a directory")) {
1965 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1967 root_inode.bi_inum = inum;
1969 ret = __write_inode(trans, &root_inode, snapshot);
1970 bch_err_msg(c, ret, "writing root inode");
1977 /* Get root directory, create if it doesn't exist: */
1978 int bch2_check_root(struct bch_fs *c)
1982 ret = bch2_trans_do(c, NULL, NULL,
1983 BCH_TRANS_COMMIT_no_enospc|
1984 BCH_TRANS_COMMIT_lazy_rw,
1985 check_root_trans(trans));
1990 struct pathbuf_entry {
1995 typedef DARRAY(struct pathbuf_entry) pathbuf;
1997 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1999 struct pathbuf_entry *i;
2001 darray_for_each(*p, i)
2002 if (i->inum == inum &&
2003 i->snapshot == snapshot)
2009 static int path_down(struct bch_fs *c, pathbuf *p,
2010 u64 inum, u32 snapshot)
2012 int ret = darray_push(p, ((struct pathbuf_entry) {
2014 .snapshot = snapshot,
2018 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
2024 * Check that a given inode is reachable from the root:
2026 * XXX: we should also be verifying that inodes are in the right subvolumes
2028 static int check_path(struct btree_trans *trans,
2030 struct bch_inode_unpacked *inode,
2033 struct bch_fs *c = trans->c;
2036 snapshot = bch2_snapshot_equiv(c, snapshot);
2039 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
2040 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
2041 struct btree_iter dirent_iter;
2042 struct bkey_s_c_dirent d;
2043 u32 parent_snapshot = snapshot;
2045 if (inode->bi_subvol) {
2048 ret = subvol_lookup(trans, inode->bi_parent_subvol,
2049 &parent_snapshot, &inum);
2054 ret = lockrestart_do(trans,
2055 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
2056 SPOS(inode->bi_dir, inode->bi_dir_offset,
2057 parent_snapshot))).k));
2058 if (ret && !bch2_err_matches(ret, ENOENT))
2061 if (!ret && !dirent_points_to_inode(d, inode)) {
2062 bch2_trans_iter_exit(trans, &dirent_iter);
2063 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
2066 if (bch2_err_matches(ret, ENOENT)) {
2067 if (fsck_err(c, inode_unreachable,
2068 "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2069 inode->bi_inum, snapshot,
2070 bch2_d_type_str(inode_d_type(inode)),
2073 inode->bi_dir_offset))
2074 ret = reattach_inode(trans, inode, snapshot);
2078 bch2_trans_iter_exit(trans, &dirent_iter);
2080 if (!S_ISDIR(inode->bi_mode))
2083 ret = path_down(c, p, inode->bi_inum, snapshot);
2085 bch_err(c, "memory allocation failure");
2089 snapshot = parent_snapshot;
2091 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2093 /* Should have been caught in dirents pass */
2094 bch_err(c, "error looking up parent directory: %i", ret);
2098 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2099 struct pathbuf_entry *i;
2101 /* XXX print path */
2102 bch_err(c, "directory structure loop");
2104 darray_for_each(*p, i)
2105 pr_err("%llu:%u", i->inum, i->snapshot);
2106 pr_err("%llu:%u", inode->bi_inum, snapshot);
2108 if (!fsck_err(c, dir_loop,
2109 "directory structure loop"))
2112 ret = commit_do(trans, NULL, NULL,
2113 BCH_TRANS_COMMIT_no_enospc|
2114 BCH_TRANS_COMMIT_lazy_rw,
2115 remove_backpointer(trans, inode));
2117 bch_err(c, "error removing dirent: %i", ret);
2121 ret = reattach_inode(trans, inode, snapshot);
2130 * Check for unreachable inodes, as well as loops in the directory structure:
2131 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2134 int bch2_check_directory_structure(struct bch_fs *c)
2136 struct btree_trans *trans = bch2_trans_get(c);
2137 struct btree_iter iter;
2139 struct bch_inode_unpacked u;
2140 pathbuf path = { 0, };
2143 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS_MIN,
2145 BTREE_ITER_PREFETCH|
2146 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2147 if (!bkey_is_inode(k.k))
2150 ret = bch2_inode_unpack(k, &u);
2152 /* Should have been caught earlier in fsck: */
2153 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2157 if (u.bi_flags & BCH_INODE_unlinked)
2160 ret = check_path(trans, &path, &u, iter.pos.snapshot);
2164 bch2_trans_iter_exit(trans, &iter);
2165 bch2_trans_put(trans);
2171 struct nlink_table {
2182 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2183 u64 inum, u32 snapshot)
2185 if (t->nr == t->size) {
2186 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2187 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2190 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2192 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2196 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2204 t->d[t->nr++] = (struct nlink) {
2206 .snapshot = snapshot,
2212 static int nlink_cmp(const void *_l, const void *_r)
2214 const struct nlink *l = _l;
2215 const struct nlink *r = _r;
2217 return cmp_int(l->inum, r->inum);
2220 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2221 struct nlink_table *links,
2222 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2224 struct nlink *link, key = {
2225 .inum = inum, .snapshot = U32_MAX,
2228 if (inum < range_start || inum >= range_end)
2231 link = __inline_bsearch(&key, links->d, links->nr,
2232 sizeof(links->d[0]), nlink_cmp);
2236 while (link > links->d && link[0].inum == link[-1].inum)
2239 for (; link < links->d + links->nr && link->inum == inum; link++)
2240 if (ref_visible(c, s, snapshot, link->snapshot)) {
2242 if (link->snapshot >= snapshot)
2248 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2249 struct nlink_table *t,
2250 u64 start, u64 *end)
2252 struct btree_trans *trans = bch2_trans_get(c);
2253 struct btree_iter iter;
2255 struct bch_inode_unpacked u;
2258 for_each_btree_key(trans, iter, BTREE_ID_inodes,
2261 BTREE_ITER_PREFETCH|
2262 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2263 if (!bkey_is_inode(k.k))
2266 /* Should never fail, checked by bch2_inode_invalid: */
2267 BUG_ON(bch2_inode_unpack(k, &u));
2270 * Backpointer and directory structure checks are sufficient for
2271 * directories, since they can't have hardlinks:
2273 if (S_ISDIR(u.bi_mode))
2279 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2281 *end = k.k->p.offset;
2287 bch2_trans_iter_exit(trans, &iter);
2288 bch2_trans_put(trans);
2291 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2297 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2298 u64 range_start, u64 range_end)
2300 struct btree_trans *trans = bch2_trans_get(c);
2301 struct snapshots_seen s;
2302 struct btree_iter iter;
2304 struct bkey_s_c_dirent d;
2307 snapshots_seen_init(&s);
2309 for_each_btree_key(trans, iter, BTREE_ID_dirents, POS_MIN,
2311 BTREE_ITER_PREFETCH|
2312 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2313 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2317 switch (k.k->type) {
2318 case KEY_TYPE_dirent:
2319 d = bkey_s_c_to_dirent(k);
2321 if (d.v->d_type != DT_DIR &&
2322 d.v->d_type != DT_SUBVOL)
2323 inc_link(c, &s, links, range_start, range_end,
2324 le64_to_cpu(d.v->d_inum),
2325 bch2_snapshot_equiv(c, d.k->p.snapshot));
2329 bch2_trans_iter_exit(trans, &iter);
2332 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2334 bch2_trans_put(trans);
2335 snapshots_seen_exit(&s);
2339 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2341 struct nlink_table *links,
2342 size_t *idx, u64 range_end)
2344 struct bch_fs *c = trans->c;
2345 struct bch_inode_unpacked u;
2346 struct nlink *link = &links->d[*idx];
2349 if (k.k->p.offset >= range_end)
2352 if (!bkey_is_inode(k.k))
2355 BUG_ON(bch2_inode_unpack(k, &u));
2357 if (S_ISDIR(u.bi_mode))
2363 while ((cmp_int(link->inum, k.k->p.offset) ?:
2364 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2365 BUG_ON(*idx == links->nr);
2366 link = &links->d[++*idx];
2369 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count,
2370 c, inode_wrong_nlink,
2371 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2372 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2373 bch2_inode_nlink_get(&u), link->count)) {
2374 bch2_inode_nlink_set(&u, link->count);
2375 ret = __write_inode(trans, &u, k.k->p.snapshot);
2382 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2383 struct nlink_table *links,
2384 u64 range_start, u64 range_end)
2386 struct btree_iter iter;
2391 ret = bch2_trans_run(c,
2392 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
2393 POS(0, range_start),
2394 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2395 NULL, NULL, BCH_TRANS_COMMIT_lazy_rw|BCH_TRANS_COMMIT_no_enospc,
2396 check_nlinks_update_inode(trans, &iter, k, links, &idx, range_end)));
2398 bch_err(c, "error in fsck walking inodes: %s", bch2_err_str(ret));
2405 int bch2_check_nlinks(struct bch_fs *c)
2407 struct nlink_table links = { 0 };
2408 u64 this_iter_range_start, next_iter_range_start = 0;
2412 this_iter_range_start = next_iter_range_start;
2413 next_iter_range_start = U64_MAX;
2415 ret = check_nlinks_find_hardlinks(c, &links,
2416 this_iter_range_start,
2417 &next_iter_range_start);
2419 ret = check_nlinks_walk_dirents(c, &links,
2420 this_iter_range_start,
2421 next_iter_range_start);
2425 ret = check_nlinks_update_hardlinks(c, &links,
2426 this_iter_range_start,
2427 next_iter_range_start);
2432 } while (next_iter_range_start != U64_MAX);
2439 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2442 struct bkey_s_c_reflink_p p;
2443 struct bkey_i_reflink_p *u;
2446 if (k.k->type != KEY_TYPE_reflink_p)
2449 p = bkey_s_c_to_reflink_p(k);
2451 if (!p.v->front_pad && !p.v->back_pad)
2454 u = bch2_trans_kmalloc(trans, sizeof(*u));
2455 ret = PTR_ERR_OR_ZERO(u);
2459 bkey_reassemble(&u->k_i, k);
2463 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2466 int bch2_fix_reflink_p(struct bch_fs *c)
2468 struct btree_iter iter;
2472 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2475 ret = bch2_trans_run(c,
2476 for_each_btree_key_commit(trans, iter,
2477 BTREE_ID_extents, POS_MIN,
2478 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2479 BTREE_ITER_ALL_SNAPSHOTS, k,
2480 NULL, NULL, BCH_TRANS_COMMIT_no_enospc|BCH_TRANS_COMMIT_lazy_rw,
2481 fix_reflink_p_key(trans, &iter, k)));