1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
13 #include "subvolume.h"
17 #include <linux/bsearch.h>
18 #include <linux/dcache.h> /* struct qstr */
20 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
22 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
25 struct btree_iter iter;
30 for_each_btree_key(trans, iter, BTREE_ID_extents,
31 SPOS(inum, 0, snapshot), 0, k, ret) {
32 if (k.k->p.inode != inum)
35 if (bkey_extent_is_allocation(k.k))
39 bch2_trans_iter_exit(trans, &iter);
41 return ret ?: sectors;
44 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
47 struct btree_iter iter;
49 struct bkey_s_c_dirent d;
53 for_each_btree_key(trans, iter, BTREE_ID_dirents,
54 SPOS(inum, 0, snapshot), 0, k, ret) {
55 if (k.k->p.inode != inum)
58 if (k.k->type != KEY_TYPE_dirent)
61 d = bkey_s_c_to_dirent(k);
62 if (d.v->d_type == DT_DIR)
66 bch2_trans_iter_exit(trans, &iter);
68 return ret ?: subdirs;
71 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
74 struct btree_iter iter;
78 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
80 k = bch2_btree_iter_peek_slot(&iter);
85 if (k.k->type != KEY_TYPE_snapshot) {
86 bch_err(trans->c, "snapshot %u not fonud", snapshot);
91 *subvol = le32_to_cpu(bkey_s_c_to_snapshot(k).v->subvol);
93 bch2_trans_iter_exit(trans, &iter);
98 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
99 u32 *snapshot, u64 *inum)
101 struct bch_subvolume s;
104 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
106 *snapshot = le32_to_cpu(s.snapshot);
107 *inum = le64_to_cpu(s.inode);
111 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
112 u32 *snapshot, u64 *inum)
114 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
117 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
118 struct bch_inode_unpacked *inode)
120 struct btree_iter iter;
124 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
126 BTREE_ITER_ALL_SNAPSHOTS);
127 k = bch2_btree_iter_peek(&iter);
132 if (!k.k || bkey_cmp(k.k->p, POS(0, inode_nr))) {
137 ret = bch2_inode_unpack(k, inode);
139 if (ret && ret != -EINTR)
140 bch_err(trans->c, "error %i fetching inode %llu",
142 bch2_trans_iter_exit(trans, &iter);
146 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
147 struct bch_inode_unpacked *inode,
150 struct btree_iter iter;
154 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
155 SPOS(0, inode_nr, *snapshot), 0);
156 k = bch2_btree_iter_peek_slot(&iter);
161 ret = bkey_is_inode(k.k)
162 ? bch2_inode_unpack(k, inode)
165 *snapshot = iter.pos.snapshot;
167 if (ret && ret != -EINTR)
168 bch_err(trans->c, "error %i fetching inode %llu:%u",
169 ret, inode_nr, *snapshot);
170 bch2_trans_iter_exit(trans, &iter);
174 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
175 struct bch_inode_unpacked *inode,
178 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
181 static int __lookup_dirent(struct btree_trans *trans,
182 struct bch_hash_info hash_info,
183 subvol_inum dir, struct qstr *name,
184 u64 *target, unsigned *type)
186 struct btree_iter iter;
187 struct bkey_s_c_dirent d;
190 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
191 &hash_info, dir, name, 0);
195 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
196 *target = le64_to_cpu(d.v->d_inum);
198 bch2_trans_iter_exit(trans, &iter);
202 static int __write_inode(struct btree_trans *trans,
203 struct bch_inode_unpacked *inode,
206 struct btree_iter iter;
209 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
210 SPOS(0, inode->bi_inum, snapshot),
213 ret = bch2_btree_iter_traverse(&iter) ?:
214 bch2_inode_write(trans, &iter, inode);
215 bch2_trans_iter_exit(trans, &iter);
219 static int write_inode(struct btree_trans *trans,
220 struct bch_inode_unpacked *inode,
223 int ret = commit_do(trans, NULL, NULL,
225 BTREE_INSERT_LAZY_RW,
226 __write_inode(trans, inode, snapshot));
228 bch_err(trans->c, "error in fsck: error %i updating inode", ret);
232 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
234 struct bch_fs *c = trans->c;
235 struct btree_iter iter = { NULL };
236 struct bkey_i_inode_generation delete;
237 struct bch_inode_unpacked inode_u;
241 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
242 SPOS(inum, 0, snapshot),
243 SPOS(inum, U64_MAX, snapshot),
245 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
246 SPOS(inum, 0, snapshot),
247 SPOS(inum, U64_MAX, snapshot),
249 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
250 SPOS(inum, 0, snapshot),
251 SPOS(inum, U64_MAX, snapshot),
256 bch2_trans_begin(trans);
258 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
259 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
260 k = bch2_btree_iter_peek_slot(&iter);
266 if (!bkey_is_inode(k.k)) {
267 bch2_fs_inconsistent(c,
268 "inode %llu:%u not found when deleting",
274 bch2_inode_unpack(k, &inode_u);
276 /* Subvolume root? */
277 if (inode_u.bi_subvol)
278 bch_warn(c, "deleting inode %llu marked as unlinked, but also a subvolume root!?", inode_u.bi_inum);
280 bkey_inode_generation_init(&delete.k_i);
281 delete.k.p = iter.pos;
282 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
284 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
285 bch2_trans_commit(trans, NULL, NULL,
286 BTREE_INSERT_NOFAIL);
288 bch2_trans_iter_exit(trans, &iter);
295 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
297 struct bch_fs *c = trans->c;
298 struct btree_iter iter;
299 struct bch_inode_unpacked dir_inode;
300 struct bch_hash_info dir_hash_info;
303 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
307 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
309 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
311 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
312 &dir_hash_info, &iter,
313 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
314 bch2_trans_iter_exit(trans, &iter);
316 if (ret && ret != -EINTR)
317 bch_err(c, "error %i from __remove_dirent()", ret);
321 /* Get lost+found, create if it doesn't exist: */
322 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
323 struct bch_inode_unpacked *lostfound)
325 struct bch_fs *c = trans->c;
326 struct bch_inode_unpacked root;
327 struct bch_hash_info root_hash_info;
328 struct qstr lostfound_str = QSTR("lost+found");
329 subvol_inum root_inum = { .subvol = subvol };
335 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
339 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
343 root_hash_info = bch2_hash_info_init(c, &root);
345 ret = __lookup_dirent(trans, root_hash_info, root_inum,
346 &lostfound_str, &inum, &d_type);
347 if (ret == -ENOENT) {
348 bch_notice(c, "creating lost+found");
349 goto create_lostfound;
352 if (ret && ret != -EINTR)
353 bch_err(c, "error looking up lost+found: %i", ret);
357 if (d_type != DT_DIR) {
358 bch_err(c, "error looking up lost+found: not a directory");
363 * The check_dirents pass has already run, dangling dirents
364 * shouldn't exist here:
366 return __lookup_inode(trans, inum, lostfound, &snapshot);
369 bch2_inode_init_early(c, lostfound);
371 ret = bch2_create_trans(trans, root_inum, &root,
372 lostfound, &lostfound_str,
373 0, 0, S_IFDIR|0700, 0, NULL, NULL,
374 (subvol_inum) { }, 0);
375 if (ret && ret != -EINTR)
376 bch_err(c, "error creating lost+found: %i", ret);
380 static int __reattach_inode(struct btree_trans *trans,
381 struct bch_inode_unpacked *inode,
384 struct bch_hash_info dir_hash;
385 struct bch_inode_unpacked lostfound;
392 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
396 ret = lookup_lostfound(trans, subvol, &lostfound);
400 if (S_ISDIR(inode->bi_mode)) {
401 lostfound.bi_nlink++;
403 ret = __write_inode(trans, &lostfound, U32_MAX);
408 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
410 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
411 name = (struct qstr) QSTR(name_buf);
413 ret = bch2_dirent_create(trans,
416 .inum = lostfound.bi_inum,
420 &name, inode->bi_inum, &dir_offset,
421 BCH_HASH_SET_MUST_CREATE);
425 inode->bi_dir = lostfound.bi_inum;
426 inode->bi_dir_offset = dir_offset;
428 return __write_inode(trans, inode, inode_snapshot);
431 static int reattach_inode(struct btree_trans *trans,
432 struct bch_inode_unpacked *inode,
435 int ret = commit_do(trans, NULL, NULL,
436 BTREE_INSERT_LAZY_RW|
438 __reattach_inode(trans, inode, inode_snapshot));
440 bch_err(trans->c, "error %i reattaching inode %llu",
441 ret, inode->bi_inum);
448 static int remove_backpointer(struct btree_trans *trans,
449 struct bch_inode_unpacked *inode)
451 struct btree_iter iter;
455 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
456 POS(inode->bi_dir, inode->bi_dir_offset), 0);
457 k = bch2_btree_iter_peek_slot(&iter);
461 if (k.k->type != KEY_TYPE_dirent) {
466 ret = __remove_dirent(trans, k.k->p);
468 bch2_trans_iter_exit(trans, &iter);
472 struct snapshots_seen_entry {
477 struct snapshots_seen {
479 DARRAY(struct snapshots_seen_entry) ids;
482 static inline void snapshots_seen_exit(struct snapshots_seen *s)
484 darray_exit(&s->ids);
487 static inline void snapshots_seen_init(struct snapshots_seen *s)
489 memset(s, 0, sizeof(*s));
492 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
493 enum btree_id btree_id, struct bpos pos)
495 struct snapshots_seen_entry *i, n = {
497 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
501 if (bkey_cmp(s->pos, pos))
504 pos.snapshot = n.equiv;
507 darray_for_each(s->ids, i)
508 if (i->equiv == n.equiv) {
510 bch_err(c, "snapshot deletion did not run correctly:\n"
511 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
512 bch2_btree_ids[btree_id],
513 pos.inode, pos.offset,
514 i->id, n.id, n.equiv);
521 ret = darray_push(&s->ids, n);
523 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
529 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
530 * and @ancestor hasn't been overwritten in @seen
532 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
534 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
535 u32 id, u32 ancestor)
538 u32 top = seen->ids.nr ? seen->ids.data[seen->ids.nr - 1].equiv : 0;
540 BUG_ON(id > ancestor);
541 BUG_ON(!bch2_snapshot_is_equiv(c, id));
542 BUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
544 /* @ancestor should be the snapshot most recently added to @seen */
545 BUG_ON(ancestor != seen->pos.snapshot);
546 BUG_ON(ancestor != top);
551 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
554 for (i = seen->ids.nr - 2;
555 i >= 0 && seen->ids.data[i].equiv >= id;
557 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv) &&
558 bch2_snapshot_is_ancestor(c, seen->ids.data[i].equiv, ancestor))
565 * ref_visible - given a key with snapshot id @src that points to a key with
566 * snapshot id @dst, test whether there is some snapshot in which @dst is
569 * This assumes we're visiting @src keys in natural key order.
571 * @s - list of snapshot IDs already seen at @src
572 * @src - snapshot ID of src key
573 * @dst - snapshot ID of dst key
575 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
579 ? key_visible_in_snapshot(c, s, dst, src)
580 : bch2_snapshot_is_ancestor(c, src, dst);
583 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
584 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
585 (_i)->snapshot <= (_snapshot); _i++) \
586 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
588 struct inode_walker_entry {
589 struct bch_inode_unpacked inode;
594 struct inode_walker {
595 bool first_this_inode;
598 DARRAY(struct inode_walker_entry) inodes;
601 static void inode_walker_exit(struct inode_walker *w)
603 darray_exit(&w->inodes);
606 static struct inode_walker inode_walker_init(void)
608 return (struct inode_walker) { 0, };
611 static int add_inode(struct bch_fs *c, struct inode_walker *w,
612 struct bkey_s_c inode)
614 struct bch_inode_unpacked u;
616 BUG_ON(bch2_inode_unpack(inode, &u));
618 return darray_push(&w->inodes, ((struct inode_walker_entry) {
620 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
624 static int __walk_inode(struct btree_trans *trans,
625 struct inode_walker *w, struct bpos pos)
627 struct bch_fs *c = trans->c;
628 struct btree_iter iter;
633 pos.snapshot = bch2_snapshot_equiv(c, pos.snapshot);
635 if (pos.inode == w->cur_inum) {
636 w->first_this_inode = false;
637 goto lookup_snapshot;
642 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
643 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
644 if (k.k->p.offset != pos.inode)
647 if (bkey_is_inode(k.k))
650 bch2_trans_iter_exit(trans, &iter);
655 w->cur_inum = pos.inode;
656 w->first_this_inode = true;
658 for (i = 0; i < w->inodes.nr; i++)
659 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->inodes.data[i].snapshot))
663 BUG_ON(pos.snapshot > w->inodes.data[i].snapshot);
665 if (pos.snapshot != w->inodes.data[i].snapshot) {
666 struct inode_walker_entry e = w->inodes.data[i];
668 e.snapshot = pos.snapshot;
671 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
672 pos.inode, pos.snapshot, w->inodes.data[i].snapshot);
674 while (i && w->inodes.data[i - 1].snapshot > pos.snapshot)
677 ret = darray_insert_item(&w->inodes, i, e);
685 static int __get_visible_inodes(struct btree_trans *trans,
686 struct inode_walker *w,
687 struct snapshots_seen *s,
690 struct bch_fs *c = trans->c;
691 struct btree_iter iter;
697 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
698 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
699 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
701 if (k.k->p.offset != inum)
704 if (!ref_visible(c, s, s->pos.snapshot, equiv))
707 if (bkey_is_inode(k.k))
710 if (equiv >= s->pos.snapshot)
713 bch2_trans_iter_exit(trans, &iter);
718 static int check_key_has_snapshot(struct btree_trans *trans,
719 struct btree_iter *iter,
722 struct bch_fs *c = trans->c;
723 struct printbuf buf = PRINTBUF;
726 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
727 "key in missing snapshot: %s",
728 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
729 ret = bch2_btree_delete_at(trans, iter,
730 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
736 static int hash_redo_key(struct btree_trans *trans,
737 const struct bch_hash_desc desc,
738 struct bch_hash_info *hash_info,
739 struct btree_iter *k_iter, struct bkey_s_c k)
741 bch_err(trans->c, "hash_redo_key() not implemented yet");
744 struct bkey_i *delete;
747 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
749 return PTR_ERR(delete);
751 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
755 bkey_reassemble(tmp, k);
757 bkey_init(&delete->k);
758 delete->k.p = k_iter->pos;
759 return bch2_btree_iter_traverse(k_iter) ?:
760 bch2_trans_update(trans, k_iter, delete, 0) ?:
761 bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode, tmp, 0);
765 static int hash_check_key(struct btree_trans *trans,
766 const struct bch_hash_desc desc,
767 struct bch_hash_info *hash_info,
768 struct btree_iter *k_iter, struct bkey_s_c hash_k)
770 struct bch_fs *c = trans->c;
771 struct btree_iter iter = { NULL };
772 struct printbuf buf = PRINTBUF;
777 if (hash_k.k->type != desc.key_type)
780 hash = desc.hash_bkey(hash_info, hash_k);
782 if (likely(hash == hash_k.k->p.offset))
785 if (hash_k.k->p.offset < hash)
788 for_each_btree_key_norestart(trans, iter, desc.btree_id,
789 POS(hash_k.k->p.inode, hash),
790 BTREE_ITER_SLOTS, k, ret) {
791 if (!bkey_cmp(k.k->p, hash_k.k->p))
794 if (fsck_err_on(k.k->type == desc.key_type &&
795 !desc.cmp_bkey(k, hash_k), c,
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 at wrong offset: btree %s inode %llu offset %llu, "
815 "hashed to %llu\n%s",
816 bch2_btree_ids[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)) == FSCK_ERR_IGNORE)
821 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
823 bch_err(c, "hash_redo_key err %i", ret);
831 static int check_inode(struct btree_trans *trans,
832 struct btree_iter *iter,
834 struct bch_inode_unpacked *prev,
835 struct snapshots_seen *s,
838 struct bch_fs *c = trans->c;
839 struct bch_inode_unpacked u;
840 bool do_update = false;
843 ret = check_key_has_snapshot(trans, iter, k);
849 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
854 * if snapshot id isn't a leaf node, skip it - deletion in
855 * particular is not atomic, so on the internal snapshot nodes
856 * we can see inodes marked for deletion after a clean shutdown
858 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
861 if (!bkey_is_inode(k.k))
864 BUG_ON(bch2_inode_unpack(k, &u));
867 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
868 BCH_INODE_I_SECTORS_DIRTY|
869 BCH_INODE_UNLINKED)))
872 if (prev->bi_inum != u.bi_inum)
875 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
876 inode_d_type(prev) != inode_d_type(&u), c,
877 "inodes in different snapshots don't match")) {
878 bch_err(c, "repair not implemented yet");
882 if (u.bi_flags & BCH_INODE_UNLINKED &&
884 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
886 bch2_trans_unlock(trans);
889 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
891 bch_err(c, "error in fsck: error %i while deleting inode", ret);
895 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
897 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
899 bch_verbose(c, "truncating inode %llu", u.bi_inum);
901 bch2_trans_unlock(trans);
905 * XXX: need to truncate partial blocks too here - or ideally
906 * just switch units to bytes and that issue goes away
908 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
909 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
911 POS(u.bi_inum, U64_MAX),
914 bch_err(c, "error in fsck: error %i truncating inode", ret);
919 * We truncated without our normal sector accounting hook, just
920 * make sure we recalculate it:
922 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
924 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
928 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
930 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
934 bch_verbose(c, "recounting sectors for inode %llu",
937 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
939 bch_err(c, "error in fsck: error %i recounting inode sectors",
944 u.bi_sectors = sectors;
945 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
949 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
952 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
957 ret = write_inode(trans, &u, iter->pos.snapshot);
959 bch_err(c, "error in fsck: error %i "
960 "updating inode", ret);
965 bch_err(c, "error %i from check_inode()", ret);
970 static int check_inodes(struct bch_fs *c, bool full)
972 struct btree_trans trans;
973 struct btree_iter iter;
974 struct bch_inode_unpacked prev = { 0 };
975 struct snapshots_seen s;
979 snapshots_seen_init(&s);
980 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
982 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
984 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
987 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
988 check_inode(&trans, &iter, k, &prev, &s, full));
990 bch2_trans_exit(&trans);
991 snapshots_seen_exit(&s);
993 bch_err(c, "error %i from check_inodes()", ret);
998 * Checking for overlapping extents needs to be reimplemented
1001 static int fix_overlapping_extent(struct btree_trans *trans,
1002 struct bkey_s_c k, struct bpos cut_at)
1004 struct btree_iter iter;
1008 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1009 ret = PTR_ERR_OR_ZERO(u);
1013 bkey_reassemble(u, k);
1014 bch2_cut_front(cut_at, u);
1018 * We don't want to go through the extent_handle_overwrites path:
1020 * XXX: this is going to screw up disk accounting, extent triggers
1021 * assume things about extent overwrites - we should be running the
1022 * triggers manually here
1024 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1025 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1027 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1028 ret = bch2_btree_iter_traverse(&iter) ?:
1029 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1030 bch2_trans_commit(trans, NULL, NULL,
1031 BTREE_INSERT_NOFAIL|
1032 BTREE_INSERT_LAZY_RW);
1033 bch2_trans_iter_exit(trans, &iter);
1038 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1039 struct btree_iter *iter,
1045 bch2_trans_iter_init(trans, iter, BTREE_ID_dirents, pos, 0);
1046 k = bch2_btree_iter_peek_slot(iter);
1048 if (!ret && k.k->type != KEY_TYPE_dirent)
1051 bch2_trans_iter_exit(trans, iter);
1052 return (struct bkey_s_c_dirent) { .k = ERR_PTR(ret) };
1055 return bkey_s_c_to_dirent(k);
1058 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1059 struct bkey_s_c_dirent d)
1061 return inode->bi_dir == d.k->p.inode &&
1062 inode->bi_dir_offset == d.k->p.offset;
1065 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1066 struct bch_inode_unpacked *inode)
1068 return d.v->d_type == DT_SUBVOL
1069 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1070 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1073 static int inode_backpointer_exists(struct btree_trans *trans,
1074 struct bch_inode_unpacked *inode,
1077 struct btree_iter iter;
1078 struct bkey_s_c_dirent d;
1081 d = dirent_get_by_pos(trans, &iter,
1082 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1083 ret = bkey_err(d.s_c);
1085 return ret == -ENOENT ? 0 : ret;
1087 ret = dirent_points_to_inode(d, inode);
1088 bch2_trans_iter_exit(trans, &iter);
1092 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1094 struct bch_fs *c = trans->c;
1095 struct inode_walker_entry *i;
1096 int ret = 0, ret2 = 0;
1099 darray_for_each(w->inodes, i) {
1100 if (i->inode.bi_sectors == i->count)
1103 count2 = lockrestart_do(trans,
1104 bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot));
1106 if (i->count != count2) {
1107 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1110 if (i->inode.bi_sectors == i->count)
1114 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1115 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1116 w->cur_inum, i->snapshot,
1117 i->inode.bi_sectors, i->count) == FSCK_ERR_IGNORE)
1120 i->inode.bi_sectors = i->count;
1121 ret = write_inode(trans, &i->inode, i->snapshot);
1128 bch_err(c, "error %i from check_i_sectors()", ret);
1132 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1134 struct inode_walker *inode,
1135 struct snapshots_seen *s)
1137 struct bch_fs *c = trans->c;
1138 struct inode_walker_entry *i;
1139 struct printbuf buf = PRINTBUF;
1143 ret = check_key_has_snapshot(trans, iter, k);
1145 ret = ret < 0 ? ret : 0;
1150 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1152 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1156 if (k.k->type == KEY_TYPE_whiteout)
1159 if (inode->cur_inum != k.k->p.inode) {
1160 ret = check_i_sectors(trans, inode);
1165 if (!iter->path->should_be_locked) {
1167 * hack: check_i_sectors may have handled a transaction restart,
1168 * it shouldn't be but we need to fix the new i_sectors check
1169 * code and delete the old bch2_count_inode_sectors() first
1174 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
1178 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
1179 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
1181 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2)) {
1182 ret = fix_overlapping_extent(trans, k, prev.k->k.p) ?: -EINTR;
1187 ret = __walk_inode(trans, inode, equiv);
1191 if (fsck_err_on(ret == INT_MAX, c,
1192 "extent in missing inode:\n %s",
1193 (printbuf_reset(&buf),
1194 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1195 ret = bch2_btree_delete_at(trans, iter,
1196 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1200 if (ret == INT_MAX) {
1205 i = inode->inodes.data + ret;
1208 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1209 !S_ISLNK(i->inode.bi_mode), c,
1210 "extent in non regular inode mode %o:\n %s",
1212 (printbuf_reset(&buf),
1213 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1214 ret = bch2_btree_delete_at(trans, iter,
1215 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1219 if (!bch2_snapshot_internal_node(c, equiv.snapshot)) {
1220 for_each_visible_inode(c, s, inode, equiv.snapshot, i) {
1221 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1222 k.k->type != KEY_TYPE_reservation &&
1223 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9, c,
1224 "extent type %u offset %llu past end of inode %llu, i_size %llu",
1225 k.k->type, k.k->p.offset, k.k->p.inode, i->inode.bi_size)) {
1227 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1228 SPOS(k.k->p.inode, round_up(i->inode.bi_size, block_bytes(c)) >> 9,
1230 POS(k.k->p.inode, U64_MAX),
1237 if (bkey_extent_is_allocation(k.k))
1238 for_each_visible_inode(c, s, inode, equiv.snapshot, i)
1239 i->count += k.k->size;
1241 bch2_bkey_buf_reassemble(&prev, c, k);
1247 printbuf_exit(&buf);
1249 if (ret && ret != -EINTR)
1250 bch_err(c, "error %i from check_extent()", ret);
1255 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1256 * that i_size an i_sectors are consistent
1259 static int check_extents(struct bch_fs *c)
1261 struct inode_walker w = inode_walker_init();
1262 struct snapshots_seen s;
1263 struct btree_trans trans;
1264 struct btree_iter iter;
1269 struct bkey_buf prev;
1270 bch2_bkey_buf_init(&prev);
1271 prev.k->k = KEY(0, 0, 0);
1273 snapshots_seen_init(&s);
1274 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1276 bch_verbose(c, "checking extents");
1278 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1279 POS(BCACHEFS_ROOT_INO, 0),
1280 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1282 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1283 check_extent(&trans, &iter, k, &w, &s));
1285 bch2_bkey_buf_exit(&prev, c);
1287 inode_walker_exit(&w);
1288 bch2_trans_exit(&trans);
1289 snapshots_seen_exit(&s);
1292 bch_err(c, "error %i from check_extents()", ret);
1296 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1298 struct bch_fs *c = trans->c;
1299 struct inode_walker_entry *i;
1300 int ret = 0, ret2 = 0;
1303 darray_for_each(w->inodes, i) {
1304 if (i->inode.bi_nlink == i->count)
1307 count2 = bch2_count_subdirs(trans, w->cur_inum, i->snapshot);
1311 if (i->count != count2) {
1312 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1315 if (i->inode.bi_nlink == i->count)
1319 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1320 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1321 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1322 i->inode.bi_nlink = i->count;
1323 ret = write_inode(trans, &i->inode, i->snapshot);
1331 bch_err(c, "error %i from check_subdir_count()", ret);
1335 static int check_dirent_target(struct btree_trans *trans,
1336 struct btree_iter *iter,
1337 struct bkey_s_c_dirent d,
1338 struct bch_inode_unpacked *target,
1339 u32 target_snapshot)
1341 struct bch_fs *c = trans->c;
1342 struct bkey_i_dirent *n;
1343 bool backpointer_exists = true;
1344 struct printbuf buf = PRINTBUF;
1347 if (!target->bi_dir &&
1348 !target->bi_dir_offset) {
1349 target->bi_dir = d.k->p.inode;
1350 target->bi_dir_offset = d.k->p.offset;
1352 ret = __write_inode(trans, target, target_snapshot);
1357 if (!inode_points_to_dirent(target, d)) {
1358 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1362 backpointer_exists = ret;
1365 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1366 backpointer_exists, c,
1367 "directory %llu with multiple links",
1369 ret = __remove_dirent(trans, d.k->p);
1373 if (fsck_err_on(backpointer_exists &&
1374 !target->bi_nlink, c,
1375 "inode %llu type %s has multiple links but i_nlink 0",
1376 target->bi_inum, bch2_d_types[d.v->d_type])) {
1378 target->bi_flags &= ~BCH_INODE_UNLINKED;
1380 ret = __write_inode(trans, target, target_snapshot);
1385 if (fsck_err_on(!backpointer_exists, c,
1386 "inode %llu:%u has wrong backpointer:\n"
1388 "should be %llu:%llu",
1389 target->bi_inum, target_snapshot,
1391 target->bi_dir_offset,
1394 target->bi_dir = d.k->p.inode;
1395 target->bi_dir_offset = d.k->p.offset;
1397 ret = __write_inode(trans, target, target_snapshot);
1403 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1404 "incorrect d_type: got %s, should be %s:\n%s",
1405 bch2_d_type_str(d.v->d_type),
1406 bch2_d_type_str(inode_d_type(target)),
1407 (printbuf_reset(&buf),
1408 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1409 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1410 ret = PTR_ERR_OR_ZERO(n);
1414 bkey_reassemble(&n->k_i, d.s_c);
1415 n->v.d_type = inode_d_type(target);
1417 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1421 d = dirent_i_to_s_c(n);
1424 if (d.v->d_type == DT_SUBVOL &&
1425 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1426 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1427 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1428 le32_to_cpu(d.v->d_parent_subvol),
1429 target->bi_parent_subvol))) {
1430 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1431 ret = PTR_ERR_OR_ZERO(n);
1435 bkey_reassemble(&n->k_i, d.s_c);
1436 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1438 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1442 d = dirent_i_to_s_c(n);
1447 printbuf_exit(&buf);
1449 if (ret && ret != -EINTR)
1450 bch_err(c, "error %i from check_target()", ret);
1454 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1456 struct bch_hash_info *hash_info,
1457 struct inode_walker *dir,
1458 struct inode_walker *target,
1459 struct snapshots_seen *s)
1461 struct bch_fs *c = trans->c;
1462 struct bkey_s_c_dirent d;
1463 struct inode_walker_entry *i;
1464 struct printbuf buf = PRINTBUF;
1468 ret = check_key_has_snapshot(trans, iter, k);
1470 ret = ret < 0 ? ret : 0;
1475 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1477 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1481 if (k.k->type == KEY_TYPE_whiteout)
1484 if (dir->cur_inum != k.k->p.inode) {
1485 ret = check_subdir_count(trans, dir);
1490 if (!iter->path->should_be_locked) {
1491 /* hack: see check_extent() */
1495 ret = __walk_inode(trans, dir, equiv);
1499 if (fsck_err_on(ret == INT_MAX, c,
1500 "dirent in nonexisting directory:\n%s",
1501 (printbuf_reset(&buf),
1502 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1503 ret = bch2_btree_delete_at(trans, iter,
1504 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1508 if (ret == INT_MAX) {
1513 i = dir->inodes.data + ret;
1516 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1517 "dirent in non directory inode type %s:\n%s",
1518 bch2_d_type_str(inode_d_type(&i->inode)),
1519 (printbuf_reset(&buf),
1520 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1521 ret = bch2_btree_delete_at(trans, iter, 0);
1525 if (dir->first_this_inode)
1526 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1528 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1529 hash_info, iter, k);
1533 /* dirent has been deleted */
1538 if (k.k->type != KEY_TYPE_dirent)
1541 d = bkey_s_c_to_dirent(k);
1543 if (d.v->d_type == DT_SUBVOL) {
1544 struct bch_inode_unpacked subvol_root;
1545 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1546 u32 target_snapshot;
1549 ret = __subvol_lookup(trans, target_subvol,
1550 &target_snapshot, &target_inum);
1551 if (ret && ret != -ENOENT)
1554 if (fsck_err_on(ret, c,
1555 "dirent points to missing subvolume %llu",
1556 le64_to_cpu(d.v->d_child_subvol))) {
1557 ret = __remove_dirent(trans, d.k->p);
1561 ret = __lookup_inode(trans, target_inum,
1562 &subvol_root, &target_snapshot);
1563 if (ret && ret != -ENOENT)
1566 if (fsck_err_on(ret, c,
1567 "subvolume %u points to missing subvolume root %llu",
1570 bch_err(c, "repair not implemented yet");
1575 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1576 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1578 subvol_root.bi_subvol, target_subvol)) {
1579 subvol_root.bi_subvol = target_subvol;
1580 ret = __write_inode(trans, &subvol_root, target_snapshot);
1585 ret = check_dirent_target(trans, iter, d, &subvol_root,
1590 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1594 if (fsck_err_on(!target->inodes.nr, c,
1595 "dirent points to missing inode: (equiv %u)\n%s",
1597 (printbuf_reset(&buf),
1598 bch2_bkey_val_to_text(&buf, c, k),
1600 ret = __remove_dirent(trans, d.k->p);
1605 darray_for_each(target->inodes, i) {
1606 ret = check_dirent_target(trans, iter, d,
1607 &i->inode, i->snapshot);
1613 if (d.v->d_type == DT_DIR)
1614 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1620 printbuf_exit(&buf);
1622 if (ret && ret != -EINTR)
1623 bch_err(c, "error %i from check_dirent()", ret);
1628 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1632 static int check_dirents(struct bch_fs *c)
1634 struct inode_walker dir = inode_walker_init();
1635 struct inode_walker target = inode_walker_init();
1636 struct snapshots_seen s;
1637 struct bch_hash_info hash_info;
1638 struct btree_trans trans;
1639 struct btree_iter iter;
1643 bch_verbose(c, "checking dirents");
1645 snapshots_seen_init(&s);
1646 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1648 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1649 POS(BCACHEFS_ROOT_INO, 0),
1650 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1653 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1654 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1656 bch2_trans_exit(&trans);
1657 snapshots_seen_exit(&s);
1658 inode_walker_exit(&dir);
1659 inode_walker_exit(&target);
1662 bch_err(c, "error %i from check_dirents()", ret);
1666 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1668 struct bch_hash_info *hash_info,
1669 struct inode_walker *inode)
1671 struct bch_fs *c = trans->c;
1674 ret = check_key_has_snapshot(trans, iter, k);
1678 ret = __walk_inode(trans, inode, k.k->p);
1682 if (fsck_err_on(ret == INT_MAX, c,
1683 "xattr for missing inode %llu",
1685 return bch2_btree_delete_at(trans, iter, 0);
1692 if (inode->first_this_inode)
1693 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1695 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1697 if (ret && ret != -EINTR)
1698 bch_err(c, "error %i from check_xattr()", ret);
1703 * Walk xattrs: verify that they all have a corresponding inode
1706 static int check_xattrs(struct bch_fs *c)
1708 struct inode_walker inode = inode_walker_init();
1709 struct bch_hash_info hash_info;
1710 struct btree_trans trans;
1711 struct btree_iter iter;
1715 bch_verbose(c, "checking xattrs");
1717 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1719 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1720 POS(BCACHEFS_ROOT_INO, 0),
1721 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1724 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1725 check_xattr(&trans, &iter, k, &hash_info, &inode));
1727 bch2_trans_exit(&trans);
1730 bch_err(c, "error %i from check_xattrs()", ret);
1734 static int check_root_trans(struct btree_trans *trans)
1736 struct bch_fs *c = trans->c;
1737 struct bch_inode_unpacked root_inode;
1742 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1743 if (ret && ret != -ENOENT)
1746 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1747 struct bkey_i_subvolume root_subvol;
1750 inum = BCACHEFS_ROOT_INO;
1752 bkey_subvolume_init(&root_subvol.k_i);
1753 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1754 root_subvol.v.flags = 0;
1755 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1756 root_subvol.v.inode = cpu_to_le64(inum);
1757 ret = commit_do(trans, NULL, NULL,
1758 BTREE_INSERT_NOFAIL|
1759 BTREE_INSERT_LAZY_RW,
1760 __bch2_btree_insert(trans, BTREE_ID_subvolumes, &root_subvol.k_i));
1762 bch_err(c, "error writing root subvol: %i", ret);
1768 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1769 if (ret && ret != -ENOENT)
1772 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1773 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1774 "root inode not a directory")) {
1775 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1777 root_inode.bi_inum = inum;
1779 ret = __write_inode(trans, &root_inode, snapshot);
1781 bch_err(c, "error writing root inode: %i", ret);
1788 /* Get root directory, create if it doesn't exist: */
1790 static int check_root(struct bch_fs *c)
1792 bch_verbose(c, "checking root directory");
1794 return bch2_trans_do(c, NULL, NULL,
1795 BTREE_INSERT_NOFAIL|
1796 BTREE_INSERT_LAZY_RW,
1797 check_root_trans(&trans));
1800 struct pathbuf_entry {
1805 typedef DARRAY(struct pathbuf_entry) pathbuf;
1807 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1809 struct pathbuf_entry *i;
1811 darray_for_each(*p, i)
1812 if (i->inum == inum &&
1813 i->snapshot == snapshot)
1819 static int path_down(struct bch_fs *c, pathbuf *p,
1820 u64 inum, u32 snapshot)
1822 int ret = darray_push(p, ((struct pathbuf_entry) {
1824 .snapshot = snapshot,
1828 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1834 * Check that a given inode is reachable from the root:
1836 * XXX: we should also be verifying that inodes are in the right subvolumes
1838 static int check_path(struct btree_trans *trans,
1840 struct bch_inode_unpacked *inode,
1843 struct bch_fs *c = trans->c;
1846 snapshot = bch2_snapshot_equiv(c, snapshot);
1849 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1850 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1851 struct btree_iter dirent_iter;
1852 struct bkey_s_c_dirent d;
1853 u32 parent_snapshot = snapshot;
1855 if (inode->bi_subvol) {
1858 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1859 &parent_snapshot, &inum);
1864 ret = lockrestart_do(trans,
1865 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
1866 SPOS(inode->bi_dir, inode->bi_dir_offset,
1867 parent_snapshot))).k));
1868 if (ret && ret != -ENOENT)
1871 if (!ret && !dirent_points_to_inode(d, inode)) {
1872 bch2_trans_iter_exit(trans, &dirent_iter);
1876 if (ret == -ENOENT) {
1877 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
1878 inode->bi_inum, snapshot,
1879 bch2_d_type_str(inode_d_type(inode)),
1882 inode->bi_dir_offset))
1883 ret = reattach_inode(trans, inode, snapshot);
1887 bch2_trans_iter_exit(trans, &dirent_iter);
1889 if (!S_ISDIR(inode->bi_mode))
1892 ret = path_down(c, p, inode->bi_inum, snapshot);
1894 bch_err(c, "memory allocation failure");
1898 snapshot = parent_snapshot;
1900 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1902 /* Should have been caught in dirents pass */
1903 bch_err(c, "error looking up parent directory: %i", ret);
1907 if (path_is_dup(p, inode->bi_inum, snapshot)) {
1908 struct pathbuf_entry *i;
1910 /* XXX print path */
1911 bch_err(c, "directory structure loop");
1913 darray_for_each(*p, i)
1914 pr_err("%llu:%u", i->inum, i->snapshot);
1915 pr_err("%llu:%u", inode->bi_inum, snapshot);
1917 if (!fsck_err(c, "directory structure loop"))
1920 ret = commit_do(trans, NULL, NULL,
1921 BTREE_INSERT_NOFAIL|
1922 BTREE_INSERT_LAZY_RW,
1923 remove_backpointer(trans, inode));
1925 bch_err(c, "error removing dirent: %i", ret);
1929 ret = reattach_inode(trans, inode, snapshot);
1934 bch_err(c, "%s: err %i", __func__, ret);
1939 * Check for unreachable inodes, as well as loops in the directory structure:
1940 * After check_dirents(), if an inode backpointer doesn't exist that means it's
1944 static int check_directory_structure(struct bch_fs *c)
1946 struct btree_trans trans;
1947 struct btree_iter iter;
1949 struct bch_inode_unpacked u;
1950 pathbuf path = { 0, };
1953 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1955 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
1957 BTREE_ITER_PREFETCH|
1958 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
1959 if (!bkey_is_inode(k.k))
1962 ret = bch2_inode_unpack(k, &u);
1964 /* Should have been caught earlier in fsck: */
1965 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1969 if (u.bi_flags & BCH_INODE_UNLINKED)
1972 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
1976 bch2_trans_iter_exit(&trans, &iter);
1978 BUG_ON(ret == -EINTR);
1982 bch2_trans_exit(&trans);
1986 struct nlink_table {
1997 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
1998 u64 inum, u32 snapshot)
2000 if (t->nr == t->size) {
2001 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2002 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
2004 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2010 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2018 t->d[t->nr++] = (struct nlink) {
2020 .snapshot = snapshot,
2026 static int nlink_cmp(const void *_l, const void *_r)
2028 const struct nlink *l = _l;
2029 const struct nlink *r = _r;
2031 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2034 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2035 struct nlink_table *links,
2036 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2038 struct nlink *link, key = {
2039 .inum = inum, .snapshot = U32_MAX,
2042 if (inum < range_start || inum >= range_end)
2045 link = __inline_bsearch(&key, links->d, links->nr,
2046 sizeof(links->d[0]), nlink_cmp);
2050 while (link > links->d && link[0].inum == link[-1].inum)
2053 for (; link < links->d + links->nr && link->inum == inum; link++)
2054 if (ref_visible(c, s, snapshot, link->snapshot)) {
2056 if (link->snapshot >= snapshot)
2062 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2063 struct nlink_table *t,
2064 u64 start, u64 *end)
2066 struct btree_trans trans;
2067 struct btree_iter iter;
2069 struct bch_inode_unpacked u;
2072 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2074 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2077 BTREE_ITER_PREFETCH|
2078 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2079 if (!bkey_is_inode(k.k))
2082 /* Should never fail, checked by bch2_inode_invalid: */
2083 BUG_ON(bch2_inode_unpack(k, &u));
2086 * Backpointer and directory structure checks are sufficient for
2087 * directories, since they can't have hardlinks:
2089 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2095 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2097 *end = k.k->p.offset;
2103 bch2_trans_iter_exit(&trans, &iter);
2104 bch2_trans_exit(&trans);
2107 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2113 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2114 u64 range_start, u64 range_end)
2116 struct btree_trans trans;
2117 struct snapshots_seen s;
2118 struct btree_iter iter;
2120 struct bkey_s_c_dirent d;
2123 snapshots_seen_init(&s);
2125 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2127 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2129 BTREE_ITER_PREFETCH|
2130 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2131 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2135 switch (k.k->type) {
2136 case KEY_TYPE_dirent:
2137 d = bkey_s_c_to_dirent(k);
2139 if (d.v->d_type != DT_DIR &&
2140 d.v->d_type != DT_SUBVOL)
2141 inc_link(c, &s, links, range_start, range_end,
2142 le64_to_cpu(d.v->d_inum),
2147 bch2_trans_iter_exit(&trans, &iter);
2150 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2152 bch2_trans_exit(&trans);
2153 snapshots_seen_exit(&s);
2158 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2159 struct nlink_table *links,
2160 u64 range_start, u64 range_end)
2162 struct btree_trans trans;
2163 struct btree_iter iter;
2165 struct bch_inode_unpacked u;
2166 struct nlink *link = links->d;
2169 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2171 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2172 POS(0, range_start),
2174 BTREE_ITER_PREFETCH|
2175 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2176 if (k.k->p.offset >= range_end)
2179 if (!bkey_is_inode(k.k))
2182 BUG_ON(bch2_inode_unpack(k, &u));
2184 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2190 while ((cmp_int(link->inum, k.k->p.offset) ?:
2191 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2193 BUG_ON(link >= links->d + links->nr);
2196 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2197 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2198 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2199 bch2_inode_nlink_get(&u), link->count)) {
2200 bch2_inode_nlink_set(&u, link->count);
2202 ret = write_inode(&trans, &u, k.k->p.snapshot);
2204 bch_err(c, "error in fsck: error %i updating inode", ret);
2208 bch2_trans_iter_exit(&trans, &iter);
2209 bch2_trans_exit(&trans);
2212 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2218 static int check_nlinks(struct bch_fs *c)
2220 struct nlink_table links = { 0 };
2221 u64 this_iter_range_start, next_iter_range_start = 0;
2224 bch_verbose(c, "checking inode nlinks");
2227 this_iter_range_start = next_iter_range_start;
2228 next_iter_range_start = U64_MAX;
2230 ret = check_nlinks_find_hardlinks(c, &links,
2231 this_iter_range_start,
2232 &next_iter_range_start);
2234 ret = check_nlinks_walk_dirents(c, &links,
2235 this_iter_range_start,
2236 next_iter_range_start);
2240 ret = check_nlinks_update_hardlinks(c, &links,
2241 this_iter_range_start,
2242 next_iter_range_start);
2247 } while (next_iter_range_start != U64_MAX);
2254 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter)
2257 struct bkey_s_c_reflink_p p;
2258 struct bkey_i_reflink_p *u;
2261 k = bch2_btree_iter_peek(iter);
2269 if (k.k->type != KEY_TYPE_reflink_p)
2272 p = bkey_s_c_to_reflink_p(k);
2274 if (!p.v->front_pad && !p.v->back_pad)
2277 u = bch2_trans_kmalloc(trans, sizeof(*u));
2278 ret = PTR_ERR_OR_ZERO(u);
2282 bkey_reassemble(&u->k_i, k);
2286 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2290 static int fix_reflink_p(struct bch_fs *c)
2292 struct btree_trans trans;
2293 struct btree_iter iter;
2297 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2300 bch_verbose(c, "fixing reflink_p keys");
2302 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2304 for_each_btree_key(&trans, iter, BTREE_ID_extents, POS_MIN,
2306 BTREE_ITER_PREFETCH|
2307 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2308 if (k.k->type == KEY_TYPE_reflink_p) {
2309 ret = commit_do(&trans, NULL, NULL,
2310 BTREE_INSERT_NOFAIL|
2311 BTREE_INSERT_LAZY_RW,
2312 fix_reflink_p_key(&trans, &iter));
2317 bch2_trans_iter_exit(&trans, &iter);
2319 bch2_trans_exit(&trans);
2324 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2325 * Doesn't fix them yet, mainly because they haven't yet been observed:
2327 int bch2_fsck_full(struct bch_fs *c)
2329 return bch2_fs_check_snapshots(c) ?:
2330 bch2_fs_check_subvols(c) ?:
2331 bch2_delete_dead_snapshots(c) ?:
2332 check_inodes(c, true) ?:
2337 check_directory_structure(c) ?:
2342 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2344 return bch2_fs_check_snapshots(c) ?:
2345 bch2_fs_check_subvols(c) ?:
2346 bch2_delete_dead_snapshots(c) ?:
2347 check_inodes(c, false);