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 }
23 * XXX: this is handling transaction restarts without returning
24 * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
26 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
29 struct btree_iter iter;
34 for_each_btree_key(trans, iter, BTREE_ID_extents,
35 SPOS(inum, 0, snapshot), 0, k, ret) {
36 if (k.k->p.inode != inum)
39 if (bkey_extent_is_allocation(k.k))
43 bch2_trans_iter_exit(trans, &iter);
45 return ret ?: sectors;
48 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
51 struct btree_iter iter;
53 struct bkey_s_c_dirent d;
57 for_each_btree_key(trans, iter, BTREE_ID_dirents,
58 SPOS(inum, 0, snapshot), 0, k, ret) {
59 if (k.k->p.inode != inum)
62 if (k.k->type != KEY_TYPE_dirent)
65 d = bkey_s_c_to_dirent(k);
66 if (d.v->d_type == DT_DIR)
70 bch2_trans_iter_exit(trans, &iter);
72 return ret ?: subdirs;
75 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
78 struct btree_iter iter;
82 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
84 k = bch2_btree_iter_peek_slot(&iter);
89 if (k.k->type != KEY_TYPE_snapshot) {
90 bch_err(trans->c, "snapshot %u not fonud", snapshot);
95 *subvol = le32_to_cpu(bkey_s_c_to_snapshot(k).v->subvol);
97 bch2_trans_iter_exit(trans, &iter);
102 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
103 u32 *snapshot, u64 *inum)
105 struct bch_subvolume s;
108 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
110 *snapshot = le32_to_cpu(s.snapshot);
111 *inum = le64_to_cpu(s.inode);
115 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
116 u32 *snapshot, u64 *inum)
118 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
121 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
122 struct bch_inode_unpacked *inode)
124 struct btree_iter iter;
128 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
130 BTREE_ITER_ALL_SNAPSHOTS);
131 k = bch2_btree_iter_peek(&iter);
136 if (!k.k || bkey_cmp(k.k->p, POS(0, inode_nr))) {
141 ret = bch2_inode_unpack(k, inode);
143 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
144 bch_err(trans->c, "error fetching inode %llu: %s",
145 inode_nr, bch2_err_str(ret));
146 bch2_trans_iter_exit(trans, &iter);
150 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
151 struct bch_inode_unpacked *inode,
154 struct btree_iter iter;
158 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
159 SPOS(0, inode_nr, *snapshot), 0);
160 k = bch2_btree_iter_peek_slot(&iter);
165 ret = bkey_is_inode(k.k)
166 ? bch2_inode_unpack(k, inode)
169 *snapshot = iter.pos.snapshot;
171 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
172 bch_err(trans->c, "error fetching inode %llu:%u: %s",
173 inode_nr, *snapshot, bch2_err_str(ret));
174 bch2_trans_iter_exit(trans, &iter);
178 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
179 struct bch_inode_unpacked *inode,
182 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
185 static int __lookup_dirent(struct btree_trans *trans,
186 struct bch_hash_info hash_info,
187 subvol_inum dir, struct qstr *name,
188 u64 *target, unsigned *type)
190 struct btree_iter iter;
191 struct bkey_s_c_dirent d;
194 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
195 &hash_info, dir, name, 0);
199 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
200 *target = le64_to_cpu(d.v->d_inum);
202 bch2_trans_iter_exit(trans, &iter);
206 static int __write_inode(struct btree_trans *trans,
207 struct bch_inode_unpacked *inode,
210 struct btree_iter iter;
213 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
214 SPOS(0, inode->bi_inum, snapshot),
217 ret = bch2_btree_iter_traverse(&iter) ?:
218 bch2_inode_write(trans, &iter, inode);
219 bch2_trans_iter_exit(trans, &iter);
223 static int write_inode(struct btree_trans *trans,
224 struct bch_inode_unpacked *inode,
227 int ret = commit_do(trans, NULL, NULL,
229 BTREE_INSERT_LAZY_RW,
230 __write_inode(trans, inode, snapshot));
232 bch_err(trans->c, "error in fsck: error updating inode: %s",
237 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
239 struct bch_fs *c = trans->c;
240 struct btree_iter iter = { NULL };
241 struct bkey_i_inode_generation delete;
242 struct bch_inode_unpacked inode_u;
247 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
248 SPOS(inum, 0, snapshot),
249 SPOS(inum, U64_MAX, snapshot),
251 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
252 SPOS(inum, 0, snapshot),
253 SPOS(inum, U64_MAX, snapshot),
255 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
256 SPOS(inum, 0, snapshot),
257 SPOS(inum, U64_MAX, snapshot),
259 } while (ret == -BCH_ERR_transaction_restart_nested);
263 bch2_trans_begin(trans);
265 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
266 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
267 k = bch2_btree_iter_peek_slot(&iter);
273 if (!bkey_is_inode(k.k)) {
274 bch2_fs_inconsistent(c,
275 "inode %llu:%u not found when deleting",
281 bch2_inode_unpack(k, &inode_u);
283 /* Subvolume root? */
284 if (inode_u.bi_subvol)
285 bch_warn(c, "deleting inode %llu marked as unlinked, but also a subvolume root!?", inode_u.bi_inum);
287 bkey_inode_generation_init(&delete.k_i);
288 delete.k.p = iter.pos;
289 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
291 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
292 bch2_trans_commit(trans, NULL, NULL,
293 BTREE_INSERT_NOFAIL);
295 bch2_trans_iter_exit(trans, &iter);
296 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
299 return ret ?: -BCH_ERR_transaction_restart_nested;
302 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
304 struct bch_fs *c = trans->c;
305 struct btree_iter iter;
306 struct bch_inode_unpacked dir_inode;
307 struct bch_hash_info dir_hash_info;
310 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
314 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
316 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
318 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
319 &dir_hash_info, &iter,
320 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
321 bch2_trans_iter_exit(trans, &iter);
323 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
324 bch_err(c, "error from __remove_dirent(): %s", bch2_err_str(ret));
328 /* Get lost+found, create if it doesn't exist: */
329 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
330 struct bch_inode_unpacked *lostfound)
332 struct bch_fs *c = trans->c;
333 struct bch_inode_unpacked root;
334 struct bch_hash_info root_hash_info;
335 struct qstr lostfound_str = QSTR("lost+found");
336 subvol_inum root_inum = { .subvol = subvol };
342 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
346 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
350 root_hash_info = bch2_hash_info_init(c, &root);
352 ret = __lookup_dirent(trans, root_hash_info, root_inum,
353 &lostfound_str, &inum, &d_type);
354 if (ret == -ENOENT) {
355 bch_notice(c, "creating lost+found");
356 goto create_lostfound;
359 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
360 bch_err(c, "error looking up lost+found: %s", bch2_err_str(ret));
364 if (d_type != DT_DIR) {
365 bch_err(c, "error looking up lost+found: not a directory");
370 * The check_dirents pass has already run, dangling dirents
371 * shouldn't exist here:
373 return __lookup_inode(trans, inum, lostfound, &snapshot);
376 bch2_inode_init_early(c, lostfound);
378 ret = bch2_create_trans(trans, root_inum, &root,
379 lostfound, &lostfound_str,
380 0, 0, S_IFDIR|0700, 0, NULL, NULL,
381 (subvol_inum) { }, 0);
382 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
383 bch_err(c, "error creating lost+found: %s", bch2_err_str(ret));
387 static int __reattach_inode(struct btree_trans *trans,
388 struct bch_inode_unpacked *inode,
391 struct bch_hash_info dir_hash;
392 struct bch_inode_unpacked lostfound;
399 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
403 ret = lookup_lostfound(trans, subvol, &lostfound);
407 if (S_ISDIR(inode->bi_mode)) {
408 lostfound.bi_nlink++;
410 ret = __write_inode(trans, &lostfound, U32_MAX);
415 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
417 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
418 name = (struct qstr) QSTR(name_buf);
420 ret = bch2_dirent_create(trans,
423 .inum = lostfound.bi_inum,
427 &name, inode->bi_inum, &dir_offset,
428 BCH_HASH_SET_MUST_CREATE);
432 inode->bi_dir = lostfound.bi_inum;
433 inode->bi_dir_offset = dir_offset;
435 return __write_inode(trans, inode, inode_snapshot);
438 static int reattach_inode(struct btree_trans *trans,
439 struct bch_inode_unpacked *inode,
442 int ret = commit_do(trans, NULL, NULL,
443 BTREE_INSERT_LAZY_RW|
445 __reattach_inode(trans, inode, inode_snapshot));
447 bch_err(trans->c, "error reattaching inode %llu: %s",
448 inode->bi_inum, bch2_err_str(ret));
455 static int remove_backpointer(struct btree_trans *trans,
456 struct bch_inode_unpacked *inode)
458 struct btree_iter iter;
462 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
463 POS(inode->bi_dir, inode->bi_dir_offset), 0);
464 k = bch2_btree_iter_peek_slot(&iter);
468 if (k.k->type != KEY_TYPE_dirent) {
473 ret = __remove_dirent(trans, k.k->p);
475 bch2_trans_iter_exit(trans, &iter);
479 struct snapshots_seen_entry {
484 struct snapshots_seen {
486 DARRAY(struct snapshots_seen_entry) ids;
489 static inline void snapshots_seen_exit(struct snapshots_seen *s)
491 darray_exit(&s->ids);
494 static inline void snapshots_seen_init(struct snapshots_seen *s)
496 memset(s, 0, sizeof(*s));
499 static int snapshots_seen_add(struct bch_fs *c, struct snapshots_seen *s, u32 id)
501 struct snapshots_seen_entry *i, n = { id, id };
504 darray_for_each(s->ids, i) {
505 if (n.equiv < i->equiv)
508 if (i->equiv == n.equiv) {
509 bch_err(c, "adding duplicate snapshot in snapshots_seen_add()");
514 ret = darray_insert_item(&s->ids, i - s->ids.data, n);
516 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
521 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
522 enum btree_id btree_id, struct bpos pos)
524 struct snapshots_seen_entry *i, n = {
526 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
530 if (bkey_cmp(s->pos, pos))
533 pos.snapshot = n.equiv;
536 darray_for_each(s->ids, i)
537 if (i->equiv == n.equiv) {
538 if (fsck_err_on(i->id != n.id, c,
539 "snapshot deletion did not run correctly:\n"
540 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
541 bch2_btree_ids[btree_id],
542 pos.inode, pos.offset,
543 i->id, n.id, n.equiv))
544 return -BCH_ERR_need_snapshot_cleanup;
549 ret = darray_push(&s->ids, n);
551 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
558 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
559 * and @ancestor hasn't been overwritten in @seen
561 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
563 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
564 u32 id, u32 ancestor)
567 u32 top = seen->ids.nr ? seen->ids.data[seen->ids.nr - 1].equiv : 0;
569 BUG_ON(id > ancestor);
570 BUG_ON(!bch2_snapshot_is_equiv(c, id));
571 BUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
573 /* @ancestor should be the snapshot most recently added to @seen */
574 BUG_ON(ancestor != seen->pos.snapshot);
575 BUG_ON(ancestor != top);
580 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
583 for (i = seen->ids.nr - 2;
584 i >= 0 && seen->ids.data[i].equiv >= id;
586 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv) &&
587 bch2_snapshot_is_ancestor(c, seen->ids.data[i].equiv, ancestor))
594 * ref_visible - given a key with snapshot id @src that points to a key with
595 * snapshot id @dst, test whether there is some snapshot in which @dst is
598 * This assumes we're visiting @src keys in natural key order.
600 * @s - list of snapshot IDs already seen at @src
601 * @src - snapshot ID of src key
602 * @dst - snapshot ID of dst key
604 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
608 ? key_visible_in_snapshot(c, s, dst, src)
609 : bch2_snapshot_is_ancestor(c, src, dst);
612 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
613 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
614 (_i)->snapshot <= (_snapshot); _i++) \
615 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
617 struct inode_walker_entry {
618 struct bch_inode_unpacked inode;
623 struct inode_walker {
624 bool first_this_inode;
627 DARRAY(struct inode_walker_entry) inodes;
630 static void inode_walker_exit(struct inode_walker *w)
632 darray_exit(&w->inodes);
635 static struct inode_walker inode_walker_init(void)
637 return (struct inode_walker) { 0, };
640 static int add_inode(struct bch_fs *c, struct inode_walker *w,
641 struct bkey_s_c inode)
643 struct bch_inode_unpacked u;
645 BUG_ON(bch2_inode_unpack(inode, &u));
647 return darray_push(&w->inodes, ((struct inode_walker_entry) {
649 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
653 static int __walk_inode(struct btree_trans *trans,
654 struct inode_walker *w, struct bpos pos)
656 struct bch_fs *c = trans->c;
657 struct btree_iter iter;
659 u32 restart_count = trans->restart_count;
663 pos.snapshot = bch2_snapshot_equiv(c, pos.snapshot);
665 if (pos.inode == w->cur_inum) {
666 w->first_this_inode = false;
667 goto lookup_snapshot;
672 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
673 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
674 if (k.k->p.offset != pos.inode)
677 if (bkey_is_inode(k.k))
680 bch2_trans_iter_exit(trans, &iter);
685 w->cur_inum = pos.inode;
686 w->first_this_inode = true;
688 if (trans_was_restarted(trans, restart_count))
689 return -BCH_ERR_transaction_restart_nested;
692 for (i = 0; i < w->inodes.nr; i++)
693 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->inodes.data[i].snapshot))
697 BUG_ON(pos.snapshot > w->inodes.data[i].snapshot);
699 if (pos.snapshot != w->inodes.data[i].snapshot) {
700 struct inode_walker_entry e = w->inodes.data[i];
702 e.snapshot = pos.snapshot;
705 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
706 pos.inode, pos.snapshot, w->inodes.data[i].snapshot);
708 while (i && w->inodes.data[i - 1].snapshot > pos.snapshot)
711 ret = darray_insert_item(&w->inodes, i, e);
719 static int __get_visible_inodes(struct btree_trans *trans,
720 struct inode_walker *w,
721 struct snapshots_seen *s,
724 struct bch_fs *c = trans->c;
725 struct btree_iter iter;
731 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
732 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
733 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
735 if (k.k->p.offset != inum)
738 if (!ref_visible(c, s, s->pos.snapshot, equiv))
741 if (bkey_is_inode(k.k))
744 if (equiv >= s->pos.snapshot)
747 bch2_trans_iter_exit(trans, &iter);
752 static int check_key_has_snapshot(struct btree_trans *trans,
753 struct btree_iter *iter,
756 struct bch_fs *c = trans->c;
757 struct printbuf buf = PRINTBUF;
760 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
761 "key in missing snapshot: %s",
762 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
763 ret = bch2_btree_delete_at(trans, iter,
764 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
770 static int hash_redo_key(struct btree_trans *trans,
771 const struct bch_hash_desc desc,
772 struct bch_hash_info *hash_info,
773 struct btree_iter *k_iter, struct bkey_s_c k)
775 struct bkey_i *delete;
778 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
780 return PTR_ERR(delete);
782 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
786 bkey_reassemble(tmp, k);
788 bkey_init(&delete->k);
789 delete->k.p = k_iter->pos;
790 return bch2_btree_iter_traverse(k_iter) ?:
791 bch2_trans_update(trans, k_iter, delete, 0) ?:
792 bch2_hash_set_snapshot(trans, desc, hash_info,
793 (subvol_inum) { 0, k.k->p.inode },
794 k.k->p.snapshot, tmp,
795 BCH_HASH_SET_MUST_CREATE,
796 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
797 bch2_trans_commit(trans, NULL, NULL,
799 BTREE_INSERT_LAZY_RW);
802 static int hash_check_key(struct btree_trans *trans,
803 const struct bch_hash_desc desc,
804 struct bch_hash_info *hash_info,
805 struct btree_iter *k_iter, struct bkey_s_c hash_k)
807 struct bch_fs *c = trans->c;
808 struct btree_iter iter = { NULL };
809 struct printbuf buf = PRINTBUF;
814 if (hash_k.k->type != desc.key_type)
817 hash = desc.hash_bkey(hash_info, hash_k);
819 if (likely(hash == hash_k.k->p.offset))
822 if (hash_k.k->p.offset < hash)
825 for_each_btree_key_norestart(trans, iter, desc.btree_id,
826 POS(hash_k.k->p.inode, hash),
827 BTREE_ITER_SLOTS, k, ret) {
828 if (!bkey_cmp(k.k->p, hash_k.k->p))
831 if (fsck_err_on(k.k->type == desc.key_type &&
832 !desc.cmp_bkey(k, hash_k), c,
833 "duplicate hash table keys:\n%s",
834 (printbuf_reset(&buf),
835 bch2_bkey_val_to_text(&buf, c, hash_k),
837 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
841 if (bkey_deleted(k.k)) {
842 bch2_trans_iter_exit(trans, &iter);
847 bch2_trans_iter_exit(trans, &iter);
851 if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, "
852 "hashed to %llu\n%s",
853 bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
854 (printbuf_reset(&buf),
855 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
856 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
858 bch_err(c, "hash_redo_key err %s", bch2_err_str(ret));
861 ret = -BCH_ERR_transaction_restart_nested;
867 static int check_inode(struct btree_trans *trans,
868 struct btree_iter *iter,
870 struct bch_inode_unpacked *prev,
871 struct snapshots_seen *s,
874 struct bch_fs *c = trans->c;
875 struct bch_inode_unpacked u;
876 bool do_update = false;
879 ret = check_key_has_snapshot(trans, iter, k);
885 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
890 * if snapshot id isn't a leaf node, skip it - deletion in
891 * particular is not atomic, so on the internal snapshot nodes
892 * we can see inodes marked for deletion after a clean shutdown
894 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
897 if (!bkey_is_inode(k.k))
900 BUG_ON(bch2_inode_unpack(k, &u));
903 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
904 BCH_INODE_I_SECTORS_DIRTY|
905 BCH_INODE_UNLINKED)))
908 if (prev->bi_inum != u.bi_inum)
911 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
912 inode_d_type(prev) != inode_d_type(&u), c,
913 "inodes in different snapshots don't match")) {
914 bch_err(c, "repair not implemented yet");
918 if (u.bi_flags & BCH_INODE_UNLINKED &&
920 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
922 bch2_trans_unlock(trans);
925 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
926 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
927 bch_err(c, "error in fsck: error while deleting inode: %s",
932 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
934 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
936 bch_verbose(c, "truncating inode %llu", u.bi_inum);
938 bch2_trans_unlock(trans);
942 * XXX: need to truncate partial blocks too here - or ideally
943 * just switch units to bytes and that issue goes away
945 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
946 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
948 POS(u.bi_inum, U64_MAX),
951 bch_err(c, "error in fsck: error truncating inode: %s",
957 * We truncated without our normal sector accounting hook, just
958 * make sure we recalculate it:
960 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
962 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
966 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
968 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
972 bch_verbose(c, "recounting sectors for inode %llu",
975 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
977 bch_err(c, "error in fsck: error recounting inode sectors: %s",
978 bch2_err_str(sectors));
982 u.bi_sectors = sectors;
983 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
987 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
990 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
995 ret = __write_inode(trans, &u, iter->pos.snapshot);
997 bch_err(c, "error in fsck: error updating inode: %s",
1003 bch_err(c, "error from check_inode(): %s", bch2_err_str(ret));
1008 static int check_inodes(struct bch_fs *c, bool full)
1010 struct btree_trans trans;
1011 struct btree_iter iter;
1012 struct bch_inode_unpacked prev = { 0 };
1013 struct snapshots_seen s;
1017 snapshots_seen_init(&s);
1018 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1020 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
1022 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1023 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1024 check_inode(&trans, &iter, k, &prev, &s, full));
1026 bch2_trans_exit(&trans);
1027 snapshots_seen_exit(&s);
1029 bch_err(c, "error from check_inodes(): %s", bch2_err_str(ret));
1034 * Checking for overlapping extents needs to be reimplemented
1037 static int fix_overlapping_extent(struct btree_trans *trans,
1038 struct bkey_s_c k, struct bpos cut_at)
1040 struct btree_iter iter;
1044 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1045 ret = PTR_ERR_OR_ZERO(u);
1049 bkey_reassemble(u, k);
1050 bch2_cut_front(cut_at, u);
1054 * We don't want to go through the extent_handle_overwrites path:
1056 * XXX: this is going to screw up disk accounting, extent triggers
1057 * assume things about extent overwrites - we should be running the
1058 * triggers manually here
1060 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1061 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1063 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1064 ret = bch2_btree_iter_traverse(&iter) ?:
1065 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1066 bch2_trans_commit(trans, NULL, NULL,
1067 BTREE_INSERT_NOFAIL|
1068 BTREE_INSERT_LAZY_RW);
1069 bch2_trans_iter_exit(trans, &iter);
1074 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1075 struct btree_iter *iter,
1081 bch2_trans_iter_init(trans, iter, BTREE_ID_dirents, pos, 0);
1082 k = bch2_btree_iter_peek_slot(iter);
1084 if (!ret && k.k->type != KEY_TYPE_dirent)
1087 bch2_trans_iter_exit(trans, iter);
1088 return (struct bkey_s_c_dirent) { .k = ERR_PTR(ret) };
1091 return bkey_s_c_to_dirent(k);
1094 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1095 struct bkey_s_c_dirent d)
1097 return inode->bi_dir == d.k->p.inode &&
1098 inode->bi_dir_offset == d.k->p.offset;
1101 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1102 struct bch_inode_unpacked *inode)
1104 return d.v->d_type == DT_SUBVOL
1105 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1106 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1109 static int inode_backpointer_exists(struct btree_trans *trans,
1110 struct bch_inode_unpacked *inode,
1113 struct btree_iter iter;
1114 struct bkey_s_c_dirent d;
1117 d = dirent_get_by_pos(trans, &iter,
1118 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1119 ret = bkey_err(d.s_c);
1121 return ret == -ENOENT ? 0 : ret;
1123 ret = dirent_points_to_inode(d, inode);
1124 bch2_trans_iter_exit(trans, &iter);
1128 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1130 struct bch_fs *c = trans->c;
1131 struct inode_walker_entry *i;
1132 u32 restart_count = trans->restart_count;
1136 darray_for_each(w->inodes, i) {
1137 if (i->inode.bi_sectors == i->count)
1140 count2 = bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot);
1142 if (i->count != count2) {
1143 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1146 if (i->inode.bi_sectors == i->count)
1150 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1151 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1152 w->cur_inum, i->snapshot,
1153 i->inode.bi_sectors, i->count)) {
1154 i->inode.bi_sectors = i->count;
1155 ret = write_inode(trans, &i->inode, i->snapshot);
1162 bch_err(c, "error from check_i_sectors(): %s", bch2_err_str(ret));
1163 if (!ret && trans_was_restarted(trans, restart_count))
1164 ret = -BCH_ERR_transaction_restart_nested;
1168 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1170 struct inode_walker *inode,
1171 struct snapshots_seen *s)
1173 struct bch_fs *c = trans->c;
1174 struct inode_walker_entry *i;
1175 struct printbuf buf = PRINTBUF;
1179 ret = check_key_has_snapshot(trans, iter, k);
1181 ret = ret < 0 ? ret : 0;
1186 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1188 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1192 if (k.k->type == KEY_TYPE_whiteout)
1195 if (inode->cur_inum != k.k->p.inode) {
1196 ret = check_i_sectors(trans, inode);
1201 BUG_ON(!iter->path->should_be_locked);
1203 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
1207 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
1208 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
1210 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2)) {
1211 ret = fix_overlapping_extent(trans, k, prev.k->k.p)
1212 ?: -BCH_ERR_transaction_restart_nested;
1217 ret = __walk_inode(trans, inode, equiv);
1221 if (fsck_err_on(ret == INT_MAX, c,
1222 "extent in missing inode:\n %s",
1223 (printbuf_reset(&buf),
1224 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1225 ret = bch2_btree_delete_at(trans, iter,
1226 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1230 if (ret == INT_MAX) {
1235 i = inode->inodes.data + ret;
1238 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1239 !S_ISLNK(i->inode.bi_mode), c,
1240 "extent in non regular inode mode %o:\n %s",
1242 (printbuf_reset(&buf),
1243 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1244 ret = bch2_btree_delete_at(trans, iter,
1245 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1250 * Check inodes in reverse order, from oldest snapshots to newest, so
1251 * that we emit the fewest number of whiteouts necessary:
1253 for (i = inode->inodes.data + inode->inodes.nr - 1;
1254 i >= inode->inodes.data;
1256 if (i->snapshot > equiv.snapshot ||
1257 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1260 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1261 k.k->type != KEY_TYPE_reservation &&
1262 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9, c,
1263 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1264 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1265 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1266 struct btree_iter iter2;
1268 bch2_trans_copy_iter(&iter2, iter);
1269 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1270 ret = bch2_btree_iter_traverse(&iter2) ?:
1271 bch2_btree_delete_at(trans, &iter2,
1272 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1273 bch2_trans_iter_exit(trans, &iter2);
1277 if (i->snapshot != equiv.snapshot) {
1278 ret = snapshots_seen_add(c, s, i->snapshot);
1285 if (bkey_extent_is_allocation(k.k))
1286 for_each_visible_inode(c, s, inode, equiv.snapshot, i)
1287 i->count += k.k->size;
1289 bch2_bkey_buf_reassemble(&prev, c, k);
1295 printbuf_exit(&buf);
1297 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1298 bch_err(c, "error from check_extent(): %s", bch2_err_str(ret));
1303 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1304 * that i_size an i_sectors are consistent
1307 static int check_extents(struct bch_fs *c)
1309 struct inode_walker w = inode_walker_init();
1310 struct snapshots_seen s;
1311 struct btree_trans trans;
1312 struct btree_iter iter;
1317 struct bkey_buf prev;
1318 bch2_bkey_buf_init(&prev);
1319 prev.k->k = KEY(0, 0, 0);
1321 snapshots_seen_init(&s);
1322 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1324 bch_verbose(c, "checking extents");
1326 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1327 POS(BCACHEFS_ROOT_INO, 0),
1328 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1330 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1331 check_extent(&trans, &iter, k, &w, &s));
1333 bch2_bkey_buf_exit(&prev, c);
1335 inode_walker_exit(&w);
1336 bch2_trans_exit(&trans);
1337 snapshots_seen_exit(&s);
1340 bch_err(c, "error from check_extents(): %s", bch2_err_str(ret));
1344 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1346 struct bch_fs *c = trans->c;
1347 struct inode_walker_entry *i;
1348 u32 restart_count = trans->restart_count;
1352 darray_for_each(w->inodes, i) {
1353 if (i->inode.bi_nlink == i->count)
1356 count2 = bch2_count_subdirs(trans, w->cur_inum, i->snapshot);
1360 if (i->count != count2) {
1361 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1364 if (i->inode.bi_nlink == i->count)
1368 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1369 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1370 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1371 i->inode.bi_nlink = i->count;
1372 ret = write_inode(trans, &i->inode, i->snapshot);
1379 bch_err(c, "error from check_subdir_count(): %s", bch2_err_str(ret));
1380 if (!ret && trans_was_restarted(trans, restart_count))
1381 ret = -BCH_ERR_transaction_restart_nested;
1385 static int check_dirent_target(struct btree_trans *trans,
1386 struct btree_iter *iter,
1387 struct bkey_s_c_dirent d,
1388 struct bch_inode_unpacked *target,
1389 u32 target_snapshot)
1391 struct bch_fs *c = trans->c;
1392 struct bkey_i_dirent *n;
1393 bool backpointer_exists = true;
1394 struct printbuf buf = PRINTBUF;
1397 if (!target->bi_dir &&
1398 !target->bi_dir_offset) {
1399 target->bi_dir = d.k->p.inode;
1400 target->bi_dir_offset = d.k->p.offset;
1402 ret = __write_inode(trans, target, target_snapshot);
1407 if (!inode_points_to_dirent(target, d)) {
1408 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1412 backpointer_exists = ret;
1415 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1416 backpointer_exists, c,
1417 "directory %llu with multiple links",
1419 ret = __remove_dirent(trans, d.k->p);
1423 if (fsck_err_on(backpointer_exists &&
1424 !target->bi_nlink, c,
1425 "inode %llu type %s has multiple links but i_nlink 0",
1426 target->bi_inum, bch2_d_types[d.v->d_type])) {
1428 target->bi_flags &= ~BCH_INODE_UNLINKED;
1430 ret = __write_inode(trans, target, target_snapshot);
1435 if (fsck_err_on(!backpointer_exists, c,
1436 "inode %llu:%u has wrong backpointer:\n"
1438 "should be %llu:%llu",
1439 target->bi_inum, target_snapshot,
1441 target->bi_dir_offset,
1444 target->bi_dir = d.k->p.inode;
1445 target->bi_dir_offset = d.k->p.offset;
1447 ret = __write_inode(trans, target, target_snapshot);
1453 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1454 "incorrect d_type: got %s, should be %s:\n%s",
1455 bch2_d_type_str(d.v->d_type),
1456 bch2_d_type_str(inode_d_type(target)),
1457 (printbuf_reset(&buf),
1458 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1459 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1460 ret = PTR_ERR_OR_ZERO(n);
1464 bkey_reassemble(&n->k_i, d.s_c);
1465 n->v.d_type = inode_d_type(target);
1467 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1471 d = dirent_i_to_s_c(n);
1474 if (d.v->d_type == DT_SUBVOL &&
1475 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1476 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1477 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1478 le32_to_cpu(d.v->d_parent_subvol),
1479 target->bi_parent_subvol))) {
1480 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1481 ret = PTR_ERR_OR_ZERO(n);
1485 bkey_reassemble(&n->k_i, d.s_c);
1486 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1488 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1492 d = dirent_i_to_s_c(n);
1497 printbuf_exit(&buf);
1499 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1500 bch_err(c, "error from check_target(): %s", bch2_err_str(ret));
1504 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1506 struct bch_hash_info *hash_info,
1507 struct inode_walker *dir,
1508 struct inode_walker *target,
1509 struct snapshots_seen *s)
1511 struct bch_fs *c = trans->c;
1512 struct bkey_s_c_dirent d;
1513 struct inode_walker_entry *i;
1514 struct printbuf buf = PRINTBUF;
1518 ret = check_key_has_snapshot(trans, iter, k);
1520 ret = ret < 0 ? ret : 0;
1525 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1527 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1531 if (k.k->type == KEY_TYPE_whiteout)
1534 if (dir->cur_inum != k.k->p.inode) {
1535 ret = check_subdir_count(trans, dir);
1540 BUG_ON(!iter->path->should_be_locked);
1542 ret = __walk_inode(trans, dir, equiv);
1546 if (fsck_err_on(ret == INT_MAX, c,
1547 "dirent in nonexisting directory:\n%s",
1548 (printbuf_reset(&buf),
1549 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1550 ret = bch2_btree_delete_at(trans, iter,
1551 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1555 if (ret == INT_MAX) {
1560 i = dir->inodes.data + ret;
1563 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1564 "dirent in non directory inode type %s:\n%s",
1565 bch2_d_type_str(inode_d_type(&i->inode)),
1566 (printbuf_reset(&buf),
1567 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1568 ret = bch2_btree_delete_at(trans, iter, 0);
1572 if (dir->first_this_inode)
1573 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1575 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1576 hash_info, iter, k);
1580 /* dirent has been deleted */
1585 if (k.k->type != KEY_TYPE_dirent)
1588 d = bkey_s_c_to_dirent(k);
1590 if (d.v->d_type == DT_SUBVOL) {
1591 struct bch_inode_unpacked subvol_root;
1592 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1593 u32 target_snapshot;
1596 ret = __subvol_lookup(trans, target_subvol,
1597 &target_snapshot, &target_inum);
1598 if (ret && ret != -ENOENT)
1601 if (fsck_err_on(ret, c,
1602 "dirent points to missing subvolume %llu",
1603 le64_to_cpu(d.v->d_child_subvol))) {
1604 ret = __remove_dirent(trans, d.k->p);
1608 ret = __lookup_inode(trans, target_inum,
1609 &subvol_root, &target_snapshot);
1610 if (ret && ret != -ENOENT)
1613 if (fsck_err_on(ret, c,
1614 "subvolume %u points to missing subvolume root %llu",
1617 bch_err(c, "repair not implemented yet");
1622 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1623 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1625 subvol_root.bi_subvol, target_subvol)) {
1626 subvol_root.bi_subvol = target_subvol;
1627 ret = __write_inode(trans, &subvol_root, target_snapshot);
1632 ret = check_dirent_target(trans, iter, d, &subvol_root,
1637 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1641 if (fsck_err_on(!target->inodes.nr, c,
1642 "dirent points to missing inode: (equiv %u)\n%s",
1644 (printbuf_reset(&buf),
1645 bch2_bkey_val_to_text(&buf, c, k),
1647 ret = __remove_dirent(trans, d.k->p);
1652 darray_for_each(target->inodes, i) {
1653 ret = check_dirent_target(trans, iter, d,
1654 &i->inode, i->snapshot);
1660 if (d.v->d_type == DT_DIR)
1661 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1667 printbuf_exit(&buf);
1669 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1670 bch_err(c, "error from check_dirent(): %s", bch2_err_str(ret));
1675 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1679 static int check_dirents(struct bch_fs *c)
1681 struct inode_walker dir = inode_walker_init();
1682 struct inode_walker target = inode_walker_init();
1683 struct snapshots_seen s;
1684 struct bch_hash_info hash_info;
1685 struct btree_trans trans;
1686 struct btree_iter iter;
1690 bch_verbose(c, "checking dirents");
1692 snapshots_seen_init(&s);
1693 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1695 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1696 POS(BCACHEFS_ROOT_INO, 0),
1697 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1700 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1701 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1703 bch2_trans_exit(&trans);
1704 snapshots_seen_exit(&s);
1705 inode_walker_exit(&dir);
1706 inode_walker_exit(&target);
1709 bch_err(c, "error from check_dirents(): %s", bch2_err_str(ret));
1713 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1715 struct bch_hash_info *hash_info,
1716 struct inode_walker *inode)
1718 struct bch_fs *c = trans->c;
1721 ret = check_key_has_snapshot(trans, iter, k);
1725 ret = __walk_inode(trans, inode, k.k->p);
1729 if (fsck_err_on(ret == INT_MAX, c,
1730 "xattr for missing inode %llu",
1732 return bch2_btree_delete_at(trans, iter, 0);
1739 if (inode->first_this_inode)
1740 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1742 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1744 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1745 bch_err(c, "error from check_xattr(): %s", bch2_err_str(ret));
1750 * Walk xattrs: verify that they all have a corresponding inode
1753 static int check_xattrs(struct bch_fs *c)
1755 struct inode_walker inode = inode_walker_init();
1756 struct bch_hash_info hash_info;
1757 struct btree_trans trans;
1758 struct btree_iter iter;
1762 bch_verbose(c, "checking xattrs");
1764 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1766 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1767 POS(BCACHEFS_ROOT_INO, 0),
1768 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1771 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1772 check_xattr(&trans, &iter, k, &hash_info, &inode));
1774 bch2_trans_exit(&trans);
1777 bch_err(c, "error from check_xattrs(): %s", bch2_err_str(ret));
1781 static int check_root_trans(struct btree_trans *trans)
1783 struct bch_fs *c = trans->c;
1784 struct bch_inode_unpacked root_inode;
1789 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1790 if (ret && ret != -ENOENT)
1793 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1794 struct bkey_i_subvolume root_subvol;
1797 inum = BCACHEFS_ROOT_INO;
1799 bkey_subvolume_init(&root_subvol.k_i);
1800 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1801 root_subvol.v.flags = 0;
1802 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1803 root_subvol.v.inode = cpu_to_le64(inum);
1804 ret = commit_do(trans, NULL, NULL,
1805 BTREE_INSERT_NOFAIL|
1806 BTREE_INSERT_LAZY_RW,
1807 __bch2_btree_insert(trans, BTREE_ID_subvolumes, &root_subvol.k_i));
1809 bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1815 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1816 if (ret && ret != -ENOENT)
1819 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1820 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1821 "root inode not a directory")) {
1822 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1824 root_inode.bi_inum = inum;
1826 ret = __write_inode(trans, &root_inode, snapshot);
1828 bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1835 /* Get root directory, create if it doesn't exist: */
1837 static int check_root(struct bch_fs *c)
1839 bch_verbose(c, "checking root directory");
1841 return bch2_trans_do(c, NULL, NULL,
1842 BTREE_INSERT_NOFAIL|
1843 BTREE_INSERT_LAZY_RW,
1844 check_root_trans(&trans));
1847 struct pathbuf_entry {
1852 typedef DARRAY(struct pathbuf_entry) pathbuf;
1854 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1856 struct pathbuf_entry *i;
1858 darray_for_each(*p, i)
1859 if (i->inum == inum &&
1860 i->snapshot == snapshot)
1866 static int path_down(struct bch_fs *c, pathbuf *p,
1867 u64 inum, u32 snapshot)
1869 int ret = darray_push(p, ((struct pathbuf_entry) {
1871 .snapshot = snapshot,
1875 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1881 * Check that a given inode is reachable from the root:
1883 * XXX: we should also be verifying that inodes are in the right subvolumes
1885 static int check_path(struct btree_trans *trans,
1887 struct bch_inode_unpacked *inode,
1890 struct bch_fs *c = trans->c;
1893 snapshot = bch2_snapshot_equiv(c, snapshot);
1896 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1897 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1898 struct btree_iter dirent_iter;
1899 struct bkey_s_c_dirent d;
1900 u32 parent_snapshot = snapshot;
1902 if (inode->bi_subvol) {
1905 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1906 &parent_snapshot, &inum);
1911 ret = lockrestart_do(trans,
1912 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
1913 SPOS(inode->bi_dir, inode->bi_dir_offset,
1914 parent_snapshot))).k));
1915 if (ret && ret != -ENOENT)
1918 if (!ret && !dirent_points_to_inode(d, inode)) {
1919 bch2_trans_iter_exit(trans, &dirent_iter);
1923 if (ret == -ENOENT) {
1924 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
1925 inode->bi_inum, snapshot,
1926 bch2_d_type_str(inode_d_type(inode)),
1929 inode->bi_dir_offset))
1930 ret = reattach_inode(trans, inode, snapshot);
1934 bch2_trans_iter_exit(trans, &dirent_iter);
1936 if (!S_ISDIR(inode->bi_mode))
1939 ret = path_down(c, p, inode->bi_inum, snapshot);
1941 bch_err(c, "memory allocation failure");
1945 snapshot = parent_snapshot;
1947 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1949 /* Should have been caught in dirents pass */
1950 bch_err(c, "error looking up parent directory: %i", ret);
1954 if (path_is_dup(p, inode->bi_inum, snapshot)) {
1955 struct pathbuf_entry *i;
1957 /* XXX print path */
1958 bch_err(c, "directory structure loop");
1960 darray_for_each(*p, i)
1961 pr_err("%llu:%u", i->inum, i->snapshot);
1962 pr_err("%llu:%u", inode->bi_inum, snapshot);
1964 if (!fsck_err(c, "directory structure loop"))
1967 ret = commit_do(trans, NULL, NULL,
1968 BTREE_INSERT_NOFAIL|
1969 BTREE_INSERT_LAZY_RW,
1970 remove_backpointer(trans, inode));
1972 bch_err(c, "error removing dirent: %i", ret);
1976 ret = reattach_inode(trans, inode, snapshot);
1981 bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
1986 * Check for unreachable inodes, as well as loops in the directory structure:
1987 * After check_dirents(), if an inode backpointer doesn't exist that means it's
1991 static int check_directory_structure(struct bch_fs *c)
1993 struct btree_trans trans;
1994 struct btree_iter iter;
1996 struct bch_inode_unpacked u;
1997 pathbuf path = { 0, };
2000 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2002 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2004 BTREE_ITER_PREFETCH|
2005 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2006 if (!bkey_is_inode(k.k))
2009 ret = bch2_inode_unpack(k, &u);
2011 /* Should have been caught earlier in fsck: */
2012 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2016 if (u.bi_flags & BCH_INODE_UNLINKED)
2019 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2023 bch2_trans_iter_exit(&trans, &iter);
2027 bch2_trans_exit(&trans);
2031 struct nlink_table {
2042 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2043 u64 inum, u32 snapshot)
2045 if (t->nr == t->size) {
2046 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2047 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
2049 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2055 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2063 t->d[t->nr++] = (struct nlink) {
2065 .snapshot = snapshot,
2071 static int nlink_cmp(const void *_l, const void *_r)
2073 const struct nlink *l = _l;
2074 const struct nlink *r = _r;
2076 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2079 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2080 struct nlink_table *links,
2081 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2083 struct nlink *link, key = {
2084 .inum = inum, .snapshot = U32_MAX,
2087 if (inum < range_start || inum >= range_end)
2090 link = __inline_bsearch(&key, links->d, links->nr,
2091 sizeof(links->d[0]), nlink_cmp);
2095 while (link > links->d && link[0].inum == link[-1].inum)
2098 for (; link < links->d + links->nr && link->inum == inum; link++)
2099 if (ref_visible(c, s, snapshot, link->snapshot)) {
2101 if (link->snapshot >= snapshot)
2107 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2108 struct nlink_table *t,
2109 u64 start, u64 *end)
2111 struct btree_trans trans;
2112 struct btree_iter iter;
2114 struct bch_inode_unpacked u;
2117 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2119 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2122 BTREE_ITER_PREFETCH|
2123 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2124 if (!bkey_is_inode(k.k))
2127 /* Should never fail, checked by bch2_inode_invalid: */
2128 BUG_ON(bch2_inode_unpack(k, &u));
2131 * Backpointer and directory structure checks are sufficient for
2132 * directories, since they can't have hardlinks:
2134 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2140 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2142 *end = k.k->p.offset;
2148 bch2_trans_iter_exit(&trans, &iter);
2149 bch2_trans_exit(&trans);
2152 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2158 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2159 u64 range_start, u64 range_end)
2161 struct btree_trans trans;
2162 struct snapshots_seen s;
2163 struct btree_iter iter;
2165 struct bkey_s_c_dirent d;
2168 snapshots_seen_init(&s);
2170 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2172 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2174 BTREE_ITER_PREFETCH|
2175 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2176 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2180 switch (k.k->type) {
2181 case KEY_TYPE_dirent:
2182 d = bkey_s_c_to_dirent(k);
2184 if (d.v->d_type != DT_DIR &&
2185 d.v->d_type != DT_SUBVOL)
2186 inc_link(c, &s, links, range_start, range_end,
2187 le64_to_cpu(d.v->d_inum),
2188 bch2_snapshot_equiv(c, d.k->p.snapshot));
2192 bch2_trans_iter_exit(&trans, &iter);
2195 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2197 bch2_trans_exit(&trans);
2198 snapshots_seen_exit(&s);
2202 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2204 struct nlink_table *links,
2205 size_t *idx, u64 range_end)
2207 struct bch_fs *c = trans->c;
2208 struct bch_inode_unpacked u;
2209 struct nlink *link = &links->d[*idx];
2212 if (k.k->p.offset >= range_end)
2215 if (!bkey_is_inode(k.k))
2218 BUG_ON(bch2_inode_unpack(k, &u));
2220 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2226 while ((cmp_int(link->inum, k.k->p.offset) ?:
2227 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2228 BUG_ON(*idx == links->nr);
2229 link = &links->d[++*idx];
2232 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2233 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2234 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2235 bch2_inode_nlink_get(&u), link->count)) {
2236 bch2_inode_nlink_set(&u, link->count);
2237 ret = __write_inode(trans, &u, k.k->p.snapshot);
2244 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2245 struct nlink_table *links,
2246 u64 range_start, u64 range_end)
2248 struct btree_trans trans;
2249 struct btree_iter iter;
2254 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2256 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2257 POS(0, range_start),
2258 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2259 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2260 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2262 bch2_trans_exit(&trans);
2265 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2273 static int check_nlinks(struct bch_fs *c)
2275 struct nlink_table links = { 0 };
2276 u64 this_iter_range_start, next_iter_range_start = 0;
2279 bch_verbose(c, "checking inode nlinks");
2282 this_iter_range_start = next_iter_range_start;
2283 next_iter_range_start = U64_MAX;
2285 ret = check_nlinks_find_hardlinks(c, &links,
2286 this_iter_range_start,
2287 &next_iter_range_start);
2289 ret = check_nlinks_walk_dirents(c, &links,
2290 this_iter_range_start,
2291 next_iter_range_start);
2295 ret = check_nlinks_update_hardlinks(c, &links,
2296 this_iter_range_start,
2297 next_iter_range_start);
2302 } while (next_iter_range_start != U64_MAX);
2309 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2312 struct bkey_s_c_reflink_p p;
2313 struct bkey_i_reflink_p *u;
2316 if (k.k->type != KEY_TYPE_reflink_p)
2319 p = bkey_s_c_to_reflink_p(k);
2321 if (!p.v->front_pad && !p.v->back_pad)
2324 u = bch2_trans_kmalloc(trans, sizeof(*u));
2325 ret = PTR_ERR_OR_ZERO(u);
2329 bkey_reassemble(&u->k_i, k);
2333 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2337 static int fix_reflink_p(struct bch_fs *c)
2339 struct btree_trans trans;
2340 struct btree_iter iter;
2344 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2347 bch_verbose(c, "fixing reflink_p keys");
2349 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2351 ret = for_each_btree_key_commit(&trans, iter,
2352 BTREE_ID_extents, POS_MIN,
2353 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2354 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2355 fix_reflink_p_key(&trans, &iter, k));
2357 bch2_trans_exit(&trans);
2362 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2363 * Doesn't fix them yet, mainly because they haven't yet been observed:
2365 int bch2_fsck_full(struct bch_fs *c)
2369 ret = bch2_fs_check_snapshots(c) ?:
2370 bch2_fs_check_subvols(c) ?:
2371 bch2_delete_dead_snapshots(c) ?:
2372 check_inodes(c, true) ?:
2377 check_directory_structure(c) ?:
2381 if (bch2_err_matches(ret, BCH_ERR_need_snapshot_cleanup)) {
2382 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
2389 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2391 return bch2_fs_check_snapshots(c) ?:
2392 bch2_fs_check_subvols(c) ?:
2393 bch2_delete_dead_snapshots(c) ?:
2394 check_inodes(c, false);