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 = __bch2_trans_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 btree_iter iter = { NULL };
235 struct bkey_i_inode_generation delete;
236 struct bch_inode_unpacked inode_u;
240 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
241 SPOS(inum, 0, snapshot),
242 SPOS(inum, U64_MAX, snapshot),
244 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
245 SPOS(inum, 0, snapshot),
246 SPOS(inum, U64_MAX, snapshot),
248 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
249 SPOS(inum, 0, snapshot),
250 SPOS(inum, U64_MAX, snapshot),
255 bch2_trans_begin(trans);
257 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
258 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
259 k = bch2_btree_iter_peek_slot(&iter);
265 if (!bkey_is_inode(k.k)) {
266 bch2_fs_inconsistent(trans->c,
267 "inode %llu:%u not found when deleting",
273 bch2_inode_unpack(k, &inode_u);
275 /* Subvolume root? */
276 if (inode_u.bi_subvol) {
277 ret = bch2_subvolume_delete(trans, inode_u.bi_subvol);
282 bkey_inode_generation_init(&delete.k_i);
283 delete.k.p = iter.pos;
284 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
286 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
287 bch2_trans_commit(trans, NULL, NULL,
288 BTREE_INSERT_NOFAIL);
290 bch2_trans_iter_exit(trans, &iter);
297 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
299 struct bch_fs *c = trans->c;
300 struct btree_iter iter;
301 struct bch_inode_unpacked dir_inode;
302 struct bch_hash_info dir_hash_info;
305 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
309 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
311 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
313 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
314 &dir_hash_info, &iter, 0);
315 bch2_trans_iter_exit(trans, &iter);
317 if (ret && ret != -EINTR)
318 bch_err(c, "error %i from __remove_dirent()", ret);
322 /* Get lost+found, create if it doesn't exist: */
323 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
324 struct bch_inode_unpacked *lostfound)
326 struct bch_fs *c = trans->c;
327 struct bch_inode_unpacked root;
328 struct bch_hash_info root_hash_info;
329 struct qstr lostfound_str = QSTR("lost+found");
330 subvol_inum root_inum = { .subvol = subvol };
336 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
340 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
344 root_hash_info = bch2_hash_info_init(c, &root);
346 ret = __lookup_dirent(trans, root_hash_info, root_inum,
347 &lostfound_str, &inum, &d_type);
348 if (ret == -ENOENT) {
349 bch_notice(c, "creating lost+found");
350 goto create_lostfound;
353 if (ret && ret != -EINTR)
354 bch_err(c, "error looking up lost+found: %i", ret);
358 if (d_type != DT_DIR) {
359 bch_err(c, "error looking up lost+found: not a directory");
364 * The check_dirents pass has already run, dangling dirents
365 * shouldn't exist here:
367 return __lookup_inode(trans, inum, lostfound, &snapshot);
370 bch2_inode_init_early(c, lostfound);
372 ret = bch2_create_trans(trans, root_inum, &root,
373 lostfound, &lostfound_str,
374 0, 0, S_IFDIR|0700, 0, NULL, NULL,
375 (subvol_inum) { }, 0);
376 if (ret && ret != -EINTR)
377 bch_err(c, "error creating lost+found: %i", ret);
381 static int __reattach_inode(struct btree_trans *trans,
382 struct bch_inode_unpacked *inode,
385 struct bch_hash_info dir_hash;
386 struct bch_inode_unpacked lostfound;
393 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
397 ret = lookup_lostfound(trans, subvol, &lostfound);
401 if (S_ISDIR(inode->bi_mode)) {
402 lostfound.bi_nlink++;
404 ret = __write_inode(trans, &lostfound, U32_MAX);
409 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
411 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
412 name = (struct qstr) QSTR(name_buf);
414 ret = bch2_dirent_create(trans,
417 .inum = lostfound.bi_inum,
421 &name, inode->bi_inum, &dir_offset,
422 BCH_HASH_SET_MUST_CREATE);
426 inode->bi_dir = lostfound.bi_inum;
427 inode->bi_dir_offset = dir_offset;
429 return __write_inode(trans, inode, inode_snapshot);
432 static int reattach_inode(struct btree_trans *trans,
433 struct bch_inode_unpacked *inode,
436 int ret = __bch2_trans_do(trans, NULL, NULL,
437 BTREE_INSERT_LAZY_RW|
439 __reattach_inode(trans, inode, inode_snapshot));
441 bch_err(trans->c, "error %i reattaching inode %llu",
442 ret, inode->bi_inum);
449 static int remove_backpointer(struct btree_trans *trans,
450 struct bch_inode_unpacked *inode)
452 struct btree_iter iter;
456 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
457 POS(inode->bi_dir, inode->bi_dir_offset), 0);
458 k = bch2_btree_iter_peek_slot(&iter);
462 if (k.k->type != KEY_TYPE_dirent) {
467 ret = __remove_dirent(trans, k.k->p);
469 bch2_trans_iter_exit(trans, &iter);
473 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s, struct bpos pos)
475 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
477 if (bkey_cmp(s->pos, pos))
481 /* Might get called multiple times due to lock restarts */
482 if (s->ids.nr && s->ids.data[s->ids.nr - 1] == pos.snapshot)
485 return snapshots_seen_add(c, s, pos.snapshot);
489 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
490 * and @ancestor hasn't been overwritten in @seen
492 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
494 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
495 u32 id, u32 ancestor)
499 BUG_ON(id > ancestor);
501 id = snapshot_t(c, id)->equiv;
502 ancestor = snapshot_t(c, ancestor)->equiv;
504 /* @ancestor should be the snapshot most recently added to @seen */
505 BUG_ON(!seen->ids.nr || seen->ids.data[seen->ids.nr - 1] != ancestor);
506 BUG_ON(seen->pos.snapshot != ancestor);
511 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
514 for (i = seen->ids.nr - 2;
515 i >= 0 && seen->ids.data[i] >= id;
517 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i]) &&
518 bch2_snapshot_is_ancestor(c, seen->ids.data[i], ancestor))
525 * ref_visible - given a key with snapshot id @src that points to a key with
526 * snapshot id @dst, test whether there is some snapshot in which @dst is
529 * This assumes we're visiting @src keys in natural key order.
531 * @s - list of snapshot IDs already seen at @src
532 * @src - snapshot ID of src key
533 * @dst - snapshot ID of dst key
535 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
539 ? key_visible_in_snapshot(c, s, dst, src)
540 : bch2_snapshot_is_ancestor(c, src, dst);
543 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
544 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && (_i)->snapshot <= (_snapshot); _i++)\
545 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
547 struct inode_walker_entry {
548 struct bch_inode_unpacked inode;
553 struct inode_walker {
554 bool first_this_inode;
557 DARRAY(struct inode_walker_entry) inodes;
560 static void inode_walker_exit(struct inode_walker *w)
562 darray_exit(w->inodes);
565 static struct inode_walker inode_walker_init(void)
567 return (struct inode_walker) { 0, };
570 static int add_inode(struct bch_fs *c, struct inode_walker *w,
571 struct bkey_s_c inode)
573 struct bch_inode_unpacked u;
575 BUG_ON(bch2_inode_unpack(inode, &u));
577 return darray_push(w->inodes, ((struct inode_walker_entry) {
579 .snapshot = snapshot_t(c, inode.k->p.snapshot)->equiv,
583 static int __walk_inode(struct btree_trans *trans,
584 struct inode_walker *w, struct bpos pos)
586 struct bch_fs *c = trans->c;
587 struct btree_iter iter;
589 unsigned i, ancestor_pos;
592 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
594 if (pos.inode == w->cur_inum) {
595 w->first_this_inode = false;
596 goto lookup_snapshot;
601 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
602 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
603 if (k.k->p.offset != pos.inode)
606 if (bkey_is_inode(k.k))
609 bch2_trans_iter_exit(trans, &iter);
614 w->cur_inum = pos.inode;
615 w->first_this_inode = true;
617 for (i = 0; i < w->inodes.nr; i++)
618 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->inodes.data[i].snapshot))
622 BUG_ON(pos.snapshot > w->inodes.data[i].snapshot);
624 if (pos.snapshot != w->inodes.data[i].snapshot) {
627 while (i && w->inodes.data[i - 1].snapshot > pos.snapshot)
630 ret = darray_insert_item(w->inodes, i, w->inodes.data[ancestor_pos]);
634 w->inodes.data[i].snapshot = pos.snapshot;
635 w->inodes.data[i].count = 0;
641 static int __get_visible_inodes(struct btree_trans *trans,
642 struct inode_walker *w,
643 struct snapshots_seen *s,
646 struct bch_fs *c = trans->c;
647 struct btree_iter iter;
653 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
654 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
655 if (k.k->p.offset != inum)
658 if (!bkey_is_inode(k.k))
661 if (ref_visible(c, s, s->pos.snapshot, k.k->p.snapshot)) {
663 if (k.k->p.snapshot >= s->pos.snapshot)
667 bch2_trans_iter_exit(trans, &iter);
672 static int check_key_has_snapshot(struct btree_trans *trans,
673 struct btree_iter *iter,
676 struct bch_fs *c = trans->c;
677 struct printbuf buf = PRINTBUF;
680 if (mustfix_fsck_err_on(!snapshot_t(c, k.k->p.snapshot)->equiv, c,
681 "key in missing snapshot: %s",
682 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
683 ret = bch2_btree_delete_at(trans, iter,
684 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
690 static int hash_redo_key(struct btree_trans *trans,
691 const struct bch_hash_desc desc,
692 struct bch_hash_info *hash_info,
693 struct btree_iter *k_iter, struct bkey_s_c k)
695 bch_err(trans->c, "hash_redo_key() not implemented yet");
698 struct bkey_i *delete;
701 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
703 return PTR_ERR(delete);
705 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
709 bkey_reassemble(tmp, k);
711 bkey_init(&delete->k);
712 delete->k.p = k_iter->pos;
713 return bch2_btree_iter_traverse(k_iter) ?:
714 bch2_trans_update(trans, k_iter, delete, 0) ?:
715 bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode, tmp, 0);
719 static int hash_check_key(struct btree_trans *trans,
720 const struct bch_hash_desc desc,
721 struct bch_hash_info *hash_info,
722 struct btree_iter *k_iter, struct bkey_s_c hash_k)
724 struct bch_fs *c = trans->c;
725 struct btree_iter iter = { NULL };
726 struct printbuf buf = PRINTBUF;
731 if (hash_k.k->type != desc.key_type)
734 hash = desc.hash_bkey(hash_info, hash_k);
736 if (likely(hash == hash_k.k->p.offset))
739 if (hash_k.k->p.offset < hash)
742 for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
743 BTREE_ITER_SLOTS, k, ret) {
744 if (!bkey_cmp(k.k->p, hash_k.k->p))
747 if (fsck_err_on(k.k->type == desc.key_type &&
748 !desc.cmp_bkey(k, hash_k), c,
749 "duplicate hash table keys:\n%s",
750 (printbuf_reset(&buf),
751 bch2_bkey_val_to_text(&buf, c, hash_k),
753 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
757 if (bkey_deleted(k.k)) {
758 bch2_trans_iter_exit(trans, &iter);
764 bch2_trans_iter_exit(trans, &iter);
768 if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
769 "hashed to %llu\n%s",
770 desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset, hash,
771 (printbuf_reset(&buf),
772 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf)) == FSCK_ERR_IGNORE)
775 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
777 bch_err(c, "hash_redo_key err %i", ret);
785 static int check_inode(struct btree_trans *trans,
786 struct btree_iter *iter,
787 struct bch_inode_unpacked *prev,
790 struct bch_fs *c = trans->c;
792 struct bch_inode_unpacked u;
793 bool do_update = false;
796 k = bch2_btree_iter_peek(iter);
804 ret = check_key_has_snapshot(trans, iter, k);
811 * if snapshot id isn't a leaf node, skip it - deletion in
812 * particular is not atomic, so on the internal snapshot nodes
813 * we can see inodes marked for deletion after a clean shutdown
815 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
818 if (!bkey_is_inode(k.k))
821 BUG_ON(bch2_inode_unpack(k, &u));
824 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
825 BCH_INODE_I_SECTORS_DIRTY|
826 BCH_INODE_UNLINKED)))
829 if (prev->bi_inum != u.bi_inum)
832 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
833 inode_d_type(prev) != inode_d_type(&u), c,
834 "inodes in different snapshots don't match")) {
835 bch_err(c, "repair not implemented yet");
839 if (u.bi_flags & BCH_INODE_UNLINKED &&
841 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
843 bch2_trans_unlock(trans);
846 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
848 bch_err(c, "error in fsck: error %i while deleting inode", ret);
852 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
854 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
856 bch_verbose(c, "truncating inode %llu", u.bi_inum);
858 bch2_trans_unlock(trans);
862 * XXX: need to truncate partial blocks too here - or ideally
863 * just switch units to bytes and that issue goes away
865 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
866 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
868 POS(u.bi_inum, U64_MAX),
871 bch_err(c, "error in fsck: error %i truncating inode", ret);
876 * We truncated without our normal sector accounting hook, just
877 * make sure we recalculate it:
879 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
881 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
885 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
887 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
891 bch_verbose(c, "recounting sectors for inode %llu",
894 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
896 bch_err(c, "error in fsck: error %i recounting inode sectors",
901 u.bi_sectors = sectors;
902 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
906 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
909 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
914 ret = write_inode(trans, &u, iter->pos.snapshot);
916 bch_err(c, "error in fsck: error %i "
917 "updating inode", ret);
922 bch_err(c, "error %i from check_inode()", ret);
927 static int check_inodes(struct bch_fs *c, bool full)
929 struct btree_trans trans;
930 struct btree_iter iter;
931 struct bch_inode_unpacked prev = { 0 };
934 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
936 bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes, POS_MIN,
939 BTREE_ITER_ALL_SNAPSHOTS);
942 ret = __bch2_trans_do(&trans, NULL, NULL,
943 BTREE_INSERT_LAZY_RW|
945 check_inode(&trans, &iter, &prev, full));
948 } while (bch2_btree_iter_advance(&iter));
949 bch2_trans_iter_exit(&trans, &iter);
951 bch2_trans_exit(&trans);
953 bch_err(c, "error %i from check_inodes()", ret);
957 static int check_subvol(struct btree_trans *trans,
958 struct btree_iter *iter)
961 struct bkey_s_c_subvolume subvol;
964 k = bch2_btree_iter_peek(iter);
972 if (k.k->type != KEY_TYPE_subvolume)
975 subvol = bkey_s_c_to_subvolume(k);
977 if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
978 ret = bch2_subvolume_delete(trans, iter->pos.offset);
979 if (ret && ret != -EINTR)
980 bch_err(trans->c, "error deleting subvolume %llu: %i",
981 iter->pos.offset, ret);
990 static int check_subvols(struct bch_fs *c)
992 struct btree_trans trans;
993 struct btree_iter iter;
996 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
998 bch2_trans_iter_init(&trans, &iter, BTREE_ID_subvolumes,
1001 BTREE_ITER_PREFETCH);
1004 ret = __bch2_trans_do(&trans, NULL, NULL,
1005 BTREE_INSERT_LAZY_RW|
1006 BTREE_INSERT_NOFAIL,
1007 check_subvol(&trans, &iter));
1010 } while (bch2_btree_iter_advance(&iter));
1011 bch2_trans_iter_exit(&trans, &iter);
1013 bch2_trans_exit(&trans);
1018 * Checking for overlapping extents needs to be reimplemented
1021 static int fix_overlapping_extent(struct btree_trans *trans,
1022 struct bkey_s_c k, struct bpos cut_at)
1024 struct btree_iter iter;
1028 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1029 ret = PTR_ERR_OR_ZERO(u);
1033 bkey_reassemble(u, k);
1034 bch2_cut_front(cut_at, u);
1038 * We don't want to go through the extent_handle_overwrites path:
1040 * XXX: this is going to screw up disk accounting, extent triggers
1041 * assume things about extent overwrites - we should be running the
1042 * triggers manually here
1044 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1045 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1047 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1048 ret = bch2_btree_iter_traverse(&iter) ?:
1049 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1050 bch2_trans_commit(trans, NULL, NULL,
1051 BTREE_INSERT_NOFAIL|
1052 BTREE_INSERT_LAZY_RW);
1053 bch2_trans_iter_exit(trans, &iter);
1058 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1059 struct btree_iter *iter,
1065 bch2_trans_iter_init(trans, iter, BTREE_ID_dirents, pos, 0);
1066 k = bch2_btree_iter_peek_slot(iter);
1068 if (!ret && k.k->type != KEY_TYPE_dirent)
1071 bch2_trans_iter_exit(trans, iter);
1072 return (struct bkey_s_c_dirent) { .k = ERR_PTR(ret) };
1075 return bkey_s_c_to_dirent(k);
1078 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1079 struct bkey_s_c_dirent d)
1081 return inode->bi_dir == d.k->p.inode &&
1082 inode->bi_dir_offset == d.k->p.offset;
1085 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1086 struct bch_inode_unpacked *inode)
1088 return d.v->d_type == DT_SUBVOL
1089 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1090 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1093 static int inode_backpointer_exists(struct btree_trans *trans,
1094 struct bch_inode_unpacked *inode,
1097 struct btree_iter iter;
1098 struct bkey_s_c_dirent d;
1101 d = dirent_get_by_pos(trans, &iter,
1102 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1103 ret = bkey_err(d.s_c);
1105 return ret == -ENOENT ? 0 : ret;
1107 ret = dirent_points_to_inode(d, inode);
1108 bch2_trans_iter_exit(trans, &iter);
1112 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1114 struct bch_fs *c = trans->c;
1115 struct inode_walker_entry *i;
1116 int ret = 0, ret2 = 0;
1119 darray_for_each(w->inodes, i) {
1120 if (i->inode.bi_sectors == i->count)
1123 count2 = lockrestart_do(trans,
1124 bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot));
1126 if (i->count != count2) {
1127 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1130 if (i->inode.bi_sectors == i->count)
1134 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1135 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1136 w->cur_inum, i->snapshot,
1137 i->inode.bi_sectors, i->count) == FSCK_ERR_IGNORE)
1140 i->inode.bi_sectors = i->count;
1141 ret = write_inode(trans, &i->inode, i->snapshot);
1148 bch_err(c, "error %i from check_i_sectors()", ret);
1152 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1153 struct inode_walker *inode,
1154 struct snapshots_seen *s)
1156 struct bch_fs *c = trans->c;
1158 struct inode_walker_entry *i;
1159 struct printbuf buf = PRINTBUF;
1162 k = bch2_btree_iter_peek(iter);
1170 ret = check_key_has_snapshot(trans, iter, k);
1172 ret = ret < 0 ? ret : 0;
1176 ret = snapshots_seen_update(c, s, k.k->p);
1180 if (k.k->type == KEY_TYPE_whiteout)
1183 if (inode->cur_inum != k.k->p.inode) {
1184 ret = check_i_sectors(trans, inode);
1189 if (!iter->path->should_be_locked) {
1191 * hack: check_i_sectors may have handled a transaction restart,
1192 * it shouldn't be but we need to fix the new i_sectors check
1193 * code and delete the old bch2_count_inode_sectors() first
1198 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
1202 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
1203 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
1205 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2)) {
1206 ret = fix_overlapping_extent(trans, k, prev.k->k.p) ?: -EINTR;
1211 ret = __walk_inode(trans, inode, k.k->p);
1215 if (fsck_err_on(ret == INT_MAX, c,
1216 "extent in missing inode:\n %s",
1217 (printbuf_reset(&buf),
1218 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1219 ret = bch2_btree_delete_at(trans, iter,
1220 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1224 if (ret == INT_MAX) {
1229 i = inode->inodes.data + ret;
1232 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1233 !S_ISLNK(i->inode.bi_mode), c,
1234 "extent in non regular inode mode %o:\n %s",
1236 (printbuf_reset(&buf),
1237 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1238 ret = bch2_btree_delete_at(trans, iter,
1239 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1243 if (!bch2_snapshot_internal_node(c, k.k->p.snapshot)) {
1244 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i) {
1245 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1246 k.k->type != KEY_TYPE_reservation &&
1247 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9, c,
1248 "extent type %u offset %llu past end of inode %llu, i_size %llu",
1249 k.k->type, k.k->p.offset, k.k->p.inode, i->inode.bi_size)) {
1251 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1252 SPOS(k.k->p.inode, round_up(i->inode.bi_size, block_bytes(c)) >> 9,
1254 POS(k.k->p.inode, U64_MAX),
1261 if (bkey_extent_is_allocation(k.k))
1262 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i)
1263 i->count += k.k->size;
1265 bch2_bkey_buf_reassemble(&prev, c, k);
1271 printbuf_exit(&buf);
1273 if (ret && ret != -EINTR)
1274 bch_err(c, "error %i from check_extent()", ret);
1279 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1280 * that i_size an i_sectors are consistent
1283 static int check_extents(struct bch_fs *c)
1285 struct inode_walker w = inode_walker_init();
1286 struct snapshots_seen s;
1287 struct btree_trans trans;
1288 struct btree_iter iter;
1292 struct bkey_buf prev;
1293 bch2_bkey_buf_init(&prev);
1294 prev.k->k = KEY(0, 0, 0);
1296 snapshots_seen_init(&s);
1297 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1299 bch_verbose(c, "checking extents");
1301 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
1302 POS(BCACHEFS_ROOT_INO, 0),
1304 BTREE_ITER_PREFETCH|
1305 BTREE_ITER_ALL_SNAPSHOTS);
1308 ret = __bch2_trans_do(&trans, NULL, NULL,
1309 BTREE_INSERT_LAZY_RW|
1310 BTREE_INSERT_NOFAIL,
1311 check_extent(&trans, &iter, &w, &s));
1314 } while (bch2_btree_iter_advance(&iter));
1315 bch2_trans_iter_exit(&trans, &iter);
1317 bch2_bkey_buf_exit(&prev, c);
1319 inode_walker_exit(&w);
1320 bch2_trans_exit(&trans);
1321 snapshots_seen_exit(&s);
1324 bch_err(c, "error %i from check_extents()", ret);
1328 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1330 struct bch_fs *c = trans->c;
1331 struct inode_walker_entry *i;
1332 int ret = 0, ret2 = 0;
1335 darray_for_each(w->inodes, i) {
1336 if (i->inode.bi_nlink == i->count)
1339 count2 = bch2_count_subdirs(trans, w->cur_inum, i->snapshot);
1343 if (i->count != count2) {
1344 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1347 if (i->inode.bi_nlink == i->count)
1351 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1352 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1353 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1354 i->inode.bi_nlink = i->count;
1355 ret = write_inode(trans, &i->inode, i->snapshot);
1363 bch_err(c, "error %i from check_subdir_count()", ret);
1367 static int check_dirent_target(struct btree_trans *trans,
1368 struct btree_iter *iter,
1369 struct bkey_s_c_dirent d,
1370 struct bch_inode_unpacked *target,
1371 u32 target_snapshot)
1373 struct bch_fs *c = trans->c;
1374 struct bkey_i_dirent *n;
1375 bool backpointer_exists = true;
1376 struct printbuf buf = PRINTBUF;
1379 if (!target->bi_dir &&
1380 !target->bi_dir_offset) {
1381 target->bi_dir = d.k->p.inode;
1382 target->bi_dir_offset = d.k->p.offset;
1384 ret = __write_inode(trans, target, target_snapshot);
1389 if (!inode_points_to_dirent(target, d)) {
1390 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1394 backpointer_exists = ret;
1397 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1398 backpointer_exists, c,
1399 "directory %llu with multiple links",
1401 ret = __remove_dirent(trans, d.k->p);
1405 if (fsck_err_on(backpointer_exists &&
1406 !target->bi_nlink, c,
1407 "inode %llu has multiple links but i_nlink 0",
1410 target->bi_flags &= ~BCH_INODE_UNLINKED;
1412 ret = __write_inode(trans, target, target_snapshot);
1417 if (fsck_err_on(!backpointer_exists, c,
1418 "inode %llu:%u has wrong backpointer:\n"
1420 "should be %llu:%llu",
1421 target->bi_inum, target_snapshot,
1423 target->bi_dir_offset,
1426 target->bi_dir = d.k->p.inode;
1427 target->bi_dir_offset = d.k->p.offset;
1429 ret = __write_inode(trans, target, target_snapshot);
1435 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1436 "incorrect d_type: got %s, should be %s:\n%s",
1437 bch2_d_type_str(d.v->d_type),
1438 bch2_d_type_str(inode_d_type(target)),
1439 (printbuf_reset(&buf),
1440 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1441 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1442 ret = PTR_ERR_OR_ZERO(n);
1446 bkey_reassemble(&n->k_i, d.s_c);
1447 n->v.d_type = inode_d_type(target);
1449 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1453 d = dirent_i_to_s_c(n);
1456 if (d.v->d_type == DT_SUBVOL &&
1457 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1458 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1459 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1460 le32_to_cpu(d.v->d_parent_subvol),
1461 target->bi_parent_subvol))) {
1462 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1463 ret = PTR_ERR_OR_ZERO(n);
1467 bkey_reassemble(&n->k_i, d.s_c);
1468 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1470 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1474 d = dirent_i_to_s_c(n);
1479 printbuf_exit(&buf);
1481 if (ret && ret != -EINTR)
1482 bch_err(c, "error %i from check_target()", ret);
1486 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1487 struct bch_hash_info *hash_info,
1488 struct inode_walker *dir,
1489 struct inode_walker *target,
1490 struct snapshots_seen *s)
1492 struct bch_fs *c = trans->c;
1494 struct bkey_s_c_dirent d;
1495 struct inode_walker_entry *i;
1496 struct printbuf buf = PRINTBUF;
1499 k = bch2_btree_iter_peek(iter);
1507 ret = check_key_has_snapshot(trans, iter, k);
1509 ret = ret < 0 ? ret : 0;
1513 ret = snapshots_seen_update(c, s, k.k->p);
1517 if (k.k->type == KEY_TYPE_whiteout)
1520 if (dir->cur_inum != k.k->p.inode) {
1521 ret = check_subdir_count(trans, dir);
1526 if (!iter->path->should_be_locked) {
1527 /* hack: see check_extent() */
1531 ret = __walk_inode(trans, dir, k.k->p);
1535 if (fsck_err_on(ret == INT_MAX, c,
1536 "dirent in nonexisting directory:\n%s",
1537 (printbuf_reset(&buf),
1538 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1539 ret = bch2_btree_delete_at(trans, iter,
1540 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1544 if (ret == INT_MAX) {
1549 i = dir->inodes.data + ret;
1552 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1553 "dirent in non directory inode type %s:\n%s",
1554 bch2_d_type_str(inode_d_type(&i->inode)),
1555 (printbuf_reset(&buf),
1556 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1557 ret = bch2_btree_delete_at(trans, iter, 0);
1561 if (dir->first_this_inode)
1562 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1564 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1565 hash_info, iter, k);
1569 /* dirent has been deleted */
1574 if (k.k->type != KEY_TYPE_dirent)
1577 d = bkey_s_c_to_dirent(k);
1579 if (d.v->d_type == DT_SUBVOL) {
1580 struct bch_inode_unpacked subvol_root;
1581 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1582 u32 target_snapshot;
1585 ret = __subvol_lookup(trans, target_subvol,
1586 &target_snapshot, &target_inum);
1587 if (ret && ret != -ENOENT)
1590 if (fsck_err_on(ret, c,
1591 "dirent points to missing subvolume %llu",
1592 le64_to_cpu(d.v->d_child_subvol))) {
1593 ret = __remove_dirent(trans, d.k->p);
1597 ret = __lookup_inode(trans, target_inum,
1598 &subvol_root, &target_snapshot);
1599 if (ret && ret != -ENOENT)
1602 if (fsck_err_on(ret, c,
1603 "subvolume %u points to missing subvolume root %llu",
1606 bch_err(c, "repair not implemented yet");
1611 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1612 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1614 subvol_root.bi_subvol, target_subvol)) {
1615 subvol_root.bi_subvol = target_subvol;
1616 ret = __write_inode(trans, &subvol_root, target_snapshot);
1621 ret = check_dirent_target(trans, iter, d, &subvol_root,
1626 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1630 if (fsck_err_on(!target->inodes.nr, c,
1631 "dirent points to missing inode:\n%s",
1632 (printbuf_reset(&buf),
1633 bch2_bkey_val_to_text(&buf, c, k),
1635 ret = __remove_dirent(trans, d.k->p);
1640 darray_for_each(target->inodes, i) {
1641 ret = check_dirent_target(trans, iter, d,
1642 &i->inode, i->snapshot);
1648 if (d.v->d_type == DT_DIR)
1649 for_each_visible_inode(c, s, dir, d.k->p.snapshot, i)
1655 printbuf_exit(&buf);
1657 if (ret && ret != -EINTR)
1658 bch_err(c, "error %i from check_dirent()", ret);
1663 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1667 static int check_dirents(struct bch_fs *c)
1669 struct inode_walker dir = inode_walker_init();
1670 struct inode_walker target = inode_walker_init();
1671 struct snapshots_seen s;
1672 struct bch_hash_info hash_info;
1673 struct btree_trans trans;
1674 struct btree_iter iter;
1677 bch_verbose(c, "checking dirents");
1679 snapshots_seen_init(&s);
1680 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1682 bch2_trans_iter_init(&trans, &iter, BTREE_ID_dirents,
1683 POS(BCACHEFS_ROOT_INO, 0),
1685 BTREE_ITER_PREFETCH|
1686 BTREE_ITER_ALL_SNAPSHOTS);
1689 ret = __bch2_trans_do(&trans, NULL, NULL,
1690 BTREE_INSERT_LAZY_RW|
1691 BTREE_INSERT_NOFAIL,
1692 check_dirent(&trans, &iter, &hash_info,
1693 &dir, &target, &s));
1696 } while (bch2_btree_iter_advance(&iter));
1697 bch2_trans_iter_exit(&trans, &iter);
1699 bch2_trans_exit(&trans);
1700 snapshots_seen_exit(&s);
1701 inode_walker_exit(&dir);
1702 inode_walker_exit(&target);
1705 bch_err(c, "error %i from check_dirents()", ret);
1709 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1710 struct bch_hash_info *hash_info,
1711 struct inode_walker *inode)
1713 struct bch_fs *c = trans->c;
1717 k = bch2_btree_iter_peek(iter);
1725 ret = check_key_has_snapshot(trans, iter, k);
1729 ret = __walk_inode(trans, inode, k.k->p);
1733 if (fsck_err_on(ret == INT_MAX, c,
1734 "xattr for missing inode %llu",
1736 return bch2_btree_delete_at(trans, iter, 0);
1743 if (inode->first_this_inode)
1744 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1746 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1748 if (ret && ret != -EINTR)
1749 bch_err(c, "error %i from check_xattr()", ret);
1754 * Walk xattrs: verify that they all have a corresponding inode
1757 static int check_xattrs(struct bch_fs *c)
1759 struct inode_walker inode = inode_walker_init();
1760 struct bch_hash_info hash_info;
1761 struct btree_trans trans;
1762 struct btree_iter iter;
1765 bch_verbose(c, "checking xattrs");
1767 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1769 bch2_trans_iter_init(&trans, &iter, BTREE_ID_xattrs,
1770 POS(BCACHEFS_ROOT_INO, 0),
1772 BTREE_ITER_PREFETCH|
1773 BTREE_ITER_ALL_SNAPSHOTS);
1776 ret = __bch2_trans_do(&trans, NULL, NULL,
1777 BTREE_INSERT_LAZY_RW|
1778 BTREE_INSERT_NOFAIL,
1779 check_xattr(&trans, &iter, &hash_info,
1783 } while (bch2_btree_iter_advance(&iter));
1784 bch2_trans_iter_exit(&trans, &iter);
1786 bch2_trans_exit(&trans);
1789 bch_err(c, "error %i from check_xattrs()", ret);
1793 static int check_root_trans(struct btree_trans *trans)
1795 struct bch_fs *c = trans->c;
1796 struct bch_inode_unpacked root_inode;
1801 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1802 if (ret && ret != -ENOENT)
1805 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1806 struct bkey_i_subvolume root_subvol;
1809 inum = BCACHEFS_ROOT_INO;
1811 bkey_subvolume_init(&root_subvol.k_i);
1812 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1813 root_subvol.v.flags = 0;
1814 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1815 root_subvol.v.inode = cpu_to_le64(inum);
1816 ret = __bch2_trans_do(trans, NULL, NULL,
1817 BTREE_INSERT_NOFAIL|
1818 BTREE_INSERT_LAZY_RW,
1819 __bch2_btree_insert(trans, BTREE_ID_subvolumes, &root_subvol.k_i));
1821 bch_err(c, "error writing root subvol: %i", ret);
1827 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1828 if (ret && ret != -ENOENT)
1831 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1832 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1833 "root inode not a directory")) {
1834 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1836 root_inode.bi_inum = inum;
1838 ret = __write_inode(trans, &root_inode, snapshot);
1840 bch_err(c, "error writing root inode: %i", ret);
1847 /* Get root directory, create if it doesn't exist: */
1849 static int check_root(struct bch_fs *c)
1851 bch_verbose(c, "checking root directory");
1853 return bch2_trans_do(c, NULL, NULL,
1854 BTREE_INSERT_NOFAIL|
1855 BTREE_INSERT_LAZY_RW,
1856 check_root_trans(&trans));
1859 struct pathbuf_entry {
1864 typedef DARRAY(struct pathbuf_entry) pathbuf;
1866 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1868 struct pathbuf_entry *i;
1870 darray_for_each(*p, i)
1871 if (i->inum == inum &&
1872 i->snapshot == snapshot)
1878 static int path_down(struct bch_fs *c, pathbuf *p,
1879 u64 inum, u32 snapshot)
1881 int ret = darray_push(*p, ((struct pathbuf_entry) {
1883 .snapshot = snapshot,
1887 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1893 * Check that a given inode is reachable from the root:
1895 * XXX: we should also be verifying that inodes are in the right subvolumes
1897 static int check_path(struct btree_trans *trans,
1899 struct bch_inode_unpacked *inode,
1902 struct bch_fs *c = trans->c;
1905 snapshot = snapshot_t(c, snapshot)->equiv;
1908 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1909 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1910 struct btree_iter dirent_iter;
1911 struct bkey_s_c_dirent d;
1912 u32 parent_snapshot = snapshot;
1914 if (inode->bi_subvol) {
1917 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1918 &parent_snapshot, &inum);
1923 ret = lockrestart_do(trans,
1924 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
1925 SPOS(inode->bi_dir, inode->bi_dir_offset,
1926 parent_snapshot))).k));
1927 if (ret && ret != -ENOENT)
1930 if (!ret && !dirent_points_to_inode(d, inode)) {
1931 bch2_trans_iter_exit(trans, &dirent_iter);
1935 if (ret == -ENOENT) {
1936 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
1937 inode->bi_inum, snapshot,
1938 bch2_d_type_str(inode_d_type(inode)),
1941 inode->bi_dir_offset))
1942 ret = reattach_inode(trans, inode, snapshot);
1946 bch2_trans_iter_exit(trans, &dirent_iter);
1948 if (!S_ISDIR(inode->bi_mode))
1951 ret = path_down(c, p, inode->bi_inum, snapshot);
1953 bch_err(c, "memory allocation failure");
1957 snapshot = parent_snapshot;
1959 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1961 /* Should have been caught in dirents pass */
1962 bch_err(c, "error looking up parent directory: %i", ret);
1966 if (path_is_dup(p, inode->bi_inum, snapshot)) {
1967 struct pathbuf_entry *i;
1969 /* XXX print path */
1970 bch_err(c, "directory structure loop");
1972 darray_for_each(*p, i)
1973 pr_err("%llu:%u", i->inum, i->snapshot);
1974 pr_err("%llu:%u", inode->bi_inum, snapshot);
1976 if (!fsck_err(c, "directory structure loop"))
1979 ret = __bch2_trans_do(trans, NULL, NULL,
1980 BTREE_INSERT_NOFAIL|
1981 BTREE_INSERT_LAZY_RW,
1982 remove_backpointer(trans, inode));
1984 bch_err(c, "error removing dirent: %i", ret);
1988 ret = reattach_inode(trans, inode, snapshot);
1993 bch_err(c, "%s: err %i", __func__, ret);
1998 * Check for unreachable inodes, as well as loops in the directory structure:
1999 * After check_dirents(), if an inode backpointer doesn't exist that means it's
2003 static int check_directory_structure(struct bch_fs *c)
2005 struct btree_trans trans;
2006 struct btree_iter iter;
2008 struct bch_inode_unpacked u;
2009 pathbuf path = { 0, };
2012 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2014 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2016 BTREE_ITER_PREFETCH|
2017 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2018 if (!bkey_is_inode(k.k))
2021 ret = bch2_inode_unpack(k, &u);
2023 /* Should have been caught earlier in fsck: */
2024 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2028 if (u.bi_flags & BCH_INODE_UNLINKED)
2031 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2035 bch2_trans_iter_exit(&trans, &iter);
2037 BUG_ON(ret == -EINTR);
2041 bch2_trans_exit(&trans);
2045 struct nlink_table {
2056 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2057 u64 inum, u32 snapshot)
2059 if (t->nr == t->size) {
2060 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2061 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
2063 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2069 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2077 t->d[t->nr++] = (struct nlink) {
2079 .snapshot = snapshot,
2085 static int nlink_cmp(const void *_l, const void *_r)
2087 const struct nlink *l = _l;
2088 const struct nlink *r = _r;
2090 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2093 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2094 struct nlink_table *links,
2095 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2097 struct nlink *link, key = {
2098 .inum = inum, .snapshot = U32_MAX,
2101 if (inum < range_start || inum >= range_end)
2104 link = __inline_bsearch(&key, links->d, links->nr,
2105 sizeof(links->d[0]), nlink_cmp);
2109 while (link > links->d && link[0].inum == link[-1].inum)
2112 for (; link < links->d + links->nr && link->inum == inum; link++)
2113 if (ref_visible(c, s, snapshot, link->snapshot)) {
2115 if (link->snapshot >= snapshot)
2121 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2122 struct nlink_table *t,
2123 u64 start, u64 *end)
2125 struct btree_trans trans;
2126 struct btree_iter iter;
2128 struct bch_inode_unpacked u;
2131 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2133 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2136 BTREE_ITER_PREFETCH|
2137 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2138 if (!bkey_is_inode(k.k))
2141 /* Should never fail, checked by bch2_inode_invalid: */
2142 BUG_ON(bch2_inode_unpack(k, &u));
2145 * Backpointer and directory structure checks are sufficient for
2146 * directories, since they can't have hardlinks:
2148 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2154 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2156 *end = k.k->p.offset;
2162 bch2_trans_iter_exit(&trans, &iter);
2163 bch2_trans_exit(&trans);
2166 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2172 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2173 u64 range_start, u64 range_end)
2175 struct btree_trans trans;
2176 struct snapshots_seen s;
2177 struct btree_iter iter;
2179 struct bkey_s_c_dirent d;
2182 snapshots_seen_init(&s);
2184 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2186 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2188 BTREE_ITER_PREFETCH|
2189 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2190 ret = snapshots_seen_update(c, &s, k.k->p);
2194 switch (k.k->type) {
2195 case KEY_TYPE_dirent:
2196 d = bkey_s_c_to_dirent(k);
2198 if (d.v->d_type != DT_DIR &&
2199 d.v->d_type != DT_SUBVOL)
2200 inc_link(c, &s, links, range_start, range_end,
2201 le64_to_cpu(d.v->d_inum),
2206 bch2_trans_iter_exit(&trans, &iter);
2209 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2211 bch2_trans_exit(&trans);
2212 snapshots_seen_exit(&s);
2217 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2218 struct nlink_table *links,
2219 u64 range_start, u64 range_end)
2221 struct btree_trans trans;
2222 struct btree_iter iter;
2224 struct bch_inode_unpacked u;
2225 struct nlink *link = links->d;
2228 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2230 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2231 POS(0, range_start),
2233 BTREE_ITER_PREFETCH|
2234 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2235 if (k.k->p.offset >= range_end)
2238 if (!bkey_is_inode(k.k))
2241 BUG_ON(bch2_inode_unpack(k, &u));
2243 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2249 while ((cmp_int(link->inum, k.k->p.offset) ?:
2250 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2252 BUG_ON(link >= links->d + links->nr);
2255 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2256 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
2257 u.bi_inum, mode_to_type(u.bi_mode),
2258 bch2_inode_nlink_get(&u), link->count)) {
2259 bch2_inode_nlink_set(&u, link->count);
2261 ret = write_inode(&trans, &u, k.k->p.snapshot);
2263 bch_err(c, "error in fsck: error %i updating inode", ret);
2267 bch2_trans_iter_exit(&trans, &iter);
2268 bch2_trans_exit(&trans);
2271 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2277 static int check_nlinks(struct bch_fs *c)
2279 struct nlink_table links = { 0 };
2280 u64 this_iter_range_start, next_iter_range_start = 0;
2283 bch_verbose(c, "checking inode nlinks");
2286 this_iter_range_start = next_iter_range_start;
2287 next_iter_range_start = U64_MAX;
2289 ret = check_nlinks_find_hardlinks(c, &links,
2290 this_iter_range_start,
2291 &next_iter_range_start);
2293 ret = check_nlinks_walk_dirents(c, &links,
2294 this_iter_range_start,
2295 next_iter_range_start);
2299 ret = check_nlinks_update_hardlinks(c, &links,
2300 this_iter_range_start,
2301 next_iter_range_start);
2306 } while (next_iter_range_start != U64_MAX);
2313 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter)
2316 struct bkey_s_c_reflink_p p;
2317 struct bkey_i_reflink_p *u;
2320 k = bch2_btree_iter_peek(iter);
2328 if (k.k->type != KEY_TYPE_reflink_p)
2331 p = bkey_s_c_to_reflink_p(k);
2333 if (!p.v->front_pad && !p.v->back_pad)
2336 u = bch2_trans_kmalloc(trans, sizeof(*u));
2337 ret = PTR_ERR_OR_ZERO(u);
2341 bkey_reassemble(&u->k_i, k);
2345 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2349 static int fix_reflink_p(struct bch_fs *c)
2351 struct btree_trans trans;
2352 struct btree_iter iter;
2356 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2359 bch_verbose(c, "fixing reflink_p keys");
2361 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2363 for_each_btree_key(&trans, iter, BTREE_ID_extents, POS_MIN,
2365 BTREE_ITER_PREFETCH|
2366 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2367 if (k.k->type == KEY_TYPE_reflink_p) {
2368 ret = __bch2_trans_do(&trans, NULL, NULL,
2369 BTREE_INSERT_NOFAIL|
2370 BTREE_INSERT_LAZY_RW,
2371 fix_reflink_p_key(&trans, &iter));
2376 bch2_trans_iter_exit(&trans, &iter);
2378 bch2_trans_exit(&trans);
2383 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2384 * Doesn't fix them yet, mainly because they haven't yet been observed:
2386 int bch2_fsck_full(struct bch_fs *c)
2388 return bch2_fs_snapshots_check(c) ?:
2389 check_inodes(c, true) ?:
2395 check_directory_structure(c) ?:
2400 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2402 return check_inodes(c, false);