1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
10 #include "fs-common.h"
14 #include "subvolume.h"
18 #include <linux/bsearch.h>
19 #include <linux/dcache.h> /* struct qstr */
21 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
24 * XXX: this is handling transaction restarts without returning
25 * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
27 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
30 struct btree_iter iter;
35 for_each_btree_key_upto(trans, iter, BTREE_ID_extents,
36 SPOS(inum, 0, snapshot),
39 if (bkey_extent_is_allocation(k.k))
42 bch2_trans_iter_exit(trans, &iter);
44 return ret ?: sectors;
47 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
50 struct btree_iter iter;
52 struct bkey_s_c_dirent d;
56 for_each_btree_key_upto(trans, iter, BTREE_ID_dirents,
57 SPOS(inum, 0, snapshot),
60 if (k.k->type != KEY_TYPE_dirent)
63 d = bkey_s_c_to_dirent(k);
64 if (d.v->d_type == DT_DIR)
67 bch2_trans_iter_exit(trans, &iter);
69 return ret ?: subdirs;
72 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
75 struct bch_snapshot s;
76 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots,
80 *subvol = le32_to_cpu(s.subvol);
81 else if (bch2_err_matches(ret, ENOENT))
82 bch_err(trans->c, "snapshot %u not fonud", snapshot);
87 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
88 u32 *snapshot, u64 *inum)
90 struct bch_subvolume s;
93 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
95 *snapshot = le32_to_cpu(s.snapshot);
96 *inum = le64_to_cpu(s.inode);
100 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
101 u32 *snapshot, u64 *inum)
103 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
106 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
107 struct bch_inode_unpacked *inode)
109 struct btree_iter iter;
113 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
115 BTREE_ITER_ALL_SNAPSHOTS);
116 k = bch2_btree_iter_peek(&iter);
121 if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
122 ret = -BCH_ERR_ENOENT_inode;
126 ret = bch2_inode_unpack(k, inode);
128 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
129 bch_err(trans->c, "error fetching inode %llu: %s",
130 inode_nr, bch2_err_str(ret));
131 bch2_trans_iter_exit(trans, &iter);
135 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
136 struct bch_inode_unpacked *inode,
139 struct btree_iter iter;
143 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
144 SPOS(0, inode_nr, *snapshot), 0);
149 ret = bkey_is_inode(k.k)
150 ? bch2_inode_unpack(k, inode)
151 : -BCH_ERR_ENOENT_inode;
153 *snapshot = iter.pos.snapshot;
155 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
156 bch_err(trans->c, "error fetching inode %llu:%u: %s",
157 inode_nr, *snapshot, bch2_err_str(ret));
158 bch2_trans_iter_exit(trans, &iter);
162 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
163 struct bch_inode_unpacked *inode,
166 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
169 static int __lookup_dirent(struct btree_trans *trans,
170 struct bch_hash_info hash_info,
171 subvol_inum dir, struct qstr *name,
172 u64 *target, unsigned *type)
174 struct btree_iter iter;
175 struct bkey_s_c_dirent d;
178 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
179 &hash_info, dir, name, 0);
183 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
184 *target = le64_to_cpu(d.v->d_inum);
186 bch2_trans_iter_exit(trans, &iter);
190 static int __write_inode(struct btree_trans *trans,
191 struct bch_inode_unpacked *inode,
194 struct bkey_inode_buf *inode_p =
195 bch2_trans_kmalloc(trans, sizeof(*inode_p));
198 return PTR_ERR(inode_p);
200 bch2_inode_pack(inode_p, inode);
201 inode_p->inode.k.p.snapshot = snapshot;
203 return bch2_btree_insert_nonextent(trans, BTREE_ID_inodes,
205 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
208 static int write_inode(struct btree_trans *trans,
209 struct bch_inode_unpacked *inode,
212 int ret = commit_do(trans, NULL, NULL,
214 BTREE_INSERT_LAZY_RW,
215 __write_inode(trans, inode, snapshot));
217 bch_err(trans->c, "error in fsck: error updating inode: %s",
222 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
224 struct bch_fs *c = trans->c;
225 struct btree_iter iter = { NULL };
226 struct bkey_i_inode_generation delete;
227 struct bch_inode_unpacked inode_u;
232 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
233 SPOS(inum, 0, snapshot),
234 SPOS(inum, U64_MAX, snapshot),
236 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
237 SPOS(inum, 0, snapshot),
238 SPOS(inum, U64_MAX, snapshot),
240 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
241 SPOS(inum, 0, snapshot),
242 SPOS(inum, U64_MAX, snapshot),
244 } while (ret == -BCH_ERR_transaction_restart_nested);
248 bch2_trans_begin(trans);
250 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
251 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
256 if (!bkey_is_inode(k.k)) {
257 bch2_fs_inconsistent(c,
258 "inode %llu:%u not found when deleting",
264 bch2_inode_unpack(k, &inode_u);
266 /* Subvolume root? */
267 if (inode_u.bi_subvol)
268 bch_warn(c, "deleting inode %llu marked as unlinked, but also a subvolume root!?", inode_u.bi_inum);
270 bkey_inode_generation_init(&delete.k_i);
271 delete.k.p = iter.pos;
272 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
274 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
275 bch2_trans_commit(trans, NULL, NULL,
276 BTREE_INSERT_NOFAIL);
278 bch2_trans_iter_exit(trans, &iter);
279 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
282 return ret ?: -BCH_ERR_transaction_restart_nested;
285 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
287 struct bch_fs *c = trans->c;
288 struct btree_iter iter;
289 struct bch_inode_unpacked dir_inode;
290 struct bch_hash_info dir_hash_info;
293 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
297 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
299 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
301 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
302 &dir_hash_info, &iter,
303 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
304 bch2_trans_iter_exit(trans, &iter);
306 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
311 /* Get lost+found, create if it doesn't exist: */
312 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
313 struct bch_inode_unpacked *lostfound)
315 struct bch_fs *c = trans->c;
316 struct bch_inode_unpacked root;
317 struct bch_hash_info root_hash_info;
318 struct qstr lostfound_str = QSTR("lost+found");
319 subvol_inum root_inum = { .subvol = subvol };
325 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
329 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
333 root_hash_info = bch2_hash_info_init(c, &root);
335 ret = __lookup_dirent(trans, root_hash_info, root_inum,
336 &lostfound_str, &inum, &d_type);
337 if (bch2_err_matches(ret, ENOENT)) {
338 bch_notice(c, "creating lost+found");
339 goto create_lostfound;
342 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
343 bch_err(c, "error looking up lost+found: %s", bch2_err_str(ret));
347 if (d_type != DT_DIR) {
348 bch_err(c, "error looking up lost+found: not a directory");
353 * The bch2_check_dirents pass has already run, dangling dirents
354 * shouldn't exist here:
356 return __lookup_inode(trans, inum, lostfound, &snapshot);
359 bch2_inode_init_early(c, lostfound);
361 ret = bch2_create_trans(trans, root_inum, &root,
362 lostfound, &lostfound_str,
363 0, 0, S_IFDIR|0700, 0, NULL, NULL,
364 (subvol_inum) { }, 0);
365 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
366 bch_err(c, "error creating lost+found: %s", bch2_err_str(ret));
370 static int __reattach_inode(struct btree_trans *trans,
371 struct bch_inode_unpacked *inode,
374 struct bch_hash_info dir_hash;
375 struct bch_inode_unpacked lostfound;
382 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
386 ret = lookup_lostfound(trans, subvol, &lostfound);
390 if (S_ISDIR(inode->bi_mode)) {
391 lostfound.bi_nlink++;
393 ret = __write_inode(trans, &lostfound, U32_MAX);
398 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
400 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
401 name = (struct qstr) QSTR(name_buf);
403 ret = bch2_dirent_create(trans,
406 .inum = lostfound.bi_inum,
410 &name, inode->bi_inum, &dir_offset,
411 BCH_HASH_SET_MUST_CREATE);
415 inode->bi_dir = lostfound.bi_inum;
416 inode->bi_dir_offset = dir_offset;
418 return __write_inode(trans, inode, inode_snapshot);
421 static int reattach_inode(struct btree_trans *trans,
422 struct bch_inode_unpacked *inode,
425 int ret = commit_do(trans, NULL, NULL,
426 BTREE_INSERT_LAZY_RW|
428 __reattach_inode(trans, inode, inode_snapshot));
430 bch_err(trans->c, "error reattaching inode %llu: %s",
431 inode->bi_inum, bch2_err_str(ret));
438 static int remove_backpointer(struct btree_trans *trans,
439 struct bch_inode_unpacked *inode)
441 struct btree_iter iter;
442 struct bkey_s_c_dirent d;
445 d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
446 POS(inode->bi_dir, inode->bi_dir_offset), 0,
449 __remove_dirent(trans, d.k->p);
450 bch2_trans_iter_exit(trans, &iter);
454 struct snapshots_seen_entry {
459 struct snapshots_seen {
461 DARRAY(struct snapshots_seen_entry) ids;
464 static inline void snapshots_seen_exit(struct snapshots_seen *s)
466 darray_exit(&s->ids);
469 static inline void snapshots_seen_init(struct snapshots_seen *s)
471 memset(s, 0, sizeof(*s));
474 static int snapshots_seen_add(struct bch_fs *c, struct snapshots_seen *s, u32 id)
476 struct snapshots_seen_entry *i, n = { id, id };
479 darray_for_each(s->ids, i) {
480 if (n.equiv < i->equiv)
483 if (i->equiv == n.equiv) {
484 bch_err(c, "%s(): adding duplicate snapshot", __func__);
489 ret = darray_insert_item(&s->ids, i - s->ids.data, n);
491 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
496 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
497 enum btree_id btree_id, struct bpos pos)
499 struct snapshots_seen_entry *i, n = {
501 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
505 if (!bkey_eq(s->pos, pos))
508 pos.snapshot = n.equiv;
511 darray_for_each(s->ids, i)
512 if (i->equiv == n.equiv) {
513 if (fsck_err_on(i->id != n.id, c,
514 "snapshot deletion did not run correctly:\n"
515 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
516 bch2_btree_ids[btree_id],
517 pos.inode, pos.offset,
518 i->id, n.id, n.equiv))
519 return -BCH_ERR_need_snapshot_cleanup;
524 ret = darray_push(&s->ids, n);
526 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
533 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
534 * and @ancestor hasn't been overwritten in @seen
536 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
538 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
539 u32 id, u32 ancestor)
542 u32 top = seen->ids.nr ? seen->ids.data[seen->ids.nr - 1].equiv : 0;
544 BUG_ON(id > ancestor);
545 BUG_ON(!bch2_snapshot_is_equiv(c, id));
546 BUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
548 /* @ancestor should be the snapshot most recently added to @seen */
549 BUG_ON(ancestor != seen->pos.snapshot);
550 BUG_ON(ancestor != top);
555 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
558 for (i = seen->ids.nr - 2;
559 i >= 0 && seen->ids.data[i].equiv >= id;
561 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv) &&
562 bch2_snapshot_is_ancestor(c, seen->ids.data[i].equiv, ancestor))
569 * ref_visible - given a key with snapshot id @src that points to a key with
570 * snapshot id @dst, test whether there is some snapshot in which @dst is
573 * This assumes we're visiting @src keys in natural key order.
575 * @s - list of snapshot IDs already seen at @src
576 * @src - snapshot ID of src key
577 * @dst - snapshot ID of dst key
579 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
583 ? key_visible_in_snapshot(c, s, dst, src)
584 : bch2_snapshot_is_ancestor(c, src, dst);
587 static int ref_visible2(struct bch_fs *c,
588 u32 src, struct snapshots_seen *src_seen,
589 u32 dst, struct snapshots_seen *dst_seen)
591 src = bch2_snapshot_equiv(c, src);
592 dst = bch2_snapshot_equiv(c, dst);
596 swap(dst_seen, src_seen);
598 return key_visible_in_snapshot(c, src_seen, dst, src);
601 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
602 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
603 (_i)->snapshot <= (_snapshot); _i++) \
604 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
606 struct inode_walker_entry {
607 struct bch_inode_unpacked inode;
612 struct inode_walker {
613 bool first_this_inode;
616 DARRAY(struct inode_walker_entry) inodes;
619 static void inode_walker_exit(struct inode_walker *w)
621 darray_exit(&w->inodes);
624 static struct inode_walker inode_walker_init(void)
626 return (struct inode_walker) { 0, };
629 static int add_inode(struct bch_fs *c, struct inode_walker *w,
630 struct bkey_s_c inode)
632 struct bch_inode_unpacked u;
634 BUG_ON(bch2_inode_unpack(inode, &u));
636 return darray_push(&w->inodes, ((struct inode_walker_entry) {
638 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
642 static int get_inodes_all_snapshots(struct btree_trans *trans,
643 struct inode_walker *w, u64 inum)
645 struct bch_fs *c = trans->c;
646 struct btree_iter iter;
648 u32 restart_count = trans->restart_count;
651 if (w->cur_inum == inum)
656 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
657 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
658 if (k.k->p.offset != inum)
661 if (bkey_is_inode(k.k))
664 bch2_trans_iter_exit(trans, &iter);
670 w->first_this_inode = true;
672 if (trans_was_restarted(trans, restart_count))
673 return -BCH_ERR_transaction_restart_nested;
678 static struct inode_walker_entry *
679 lookup_inode_for_snapshot(struct bch_fs *c,
680 struct inode_walker *w, u32 snapshot)
682 struct inode_walker_entry *i;
684 snapshot = bch2_snapshot_equiv(c, snapshot);
686 darray_for_each(w->inodes, i)
687 if (bch2_snapshot_is_ancestor(c, snapshot, i->snapshot))
692 BUG_ON(snapshot > i->snapshot);
694 if (snapshot != i->snapshot) {
695 struct inode_walker_entry new = *i;
698 new.snapshot = snapshot;
701 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
702 w->cur_inum, snapshot, i->snapshot);
704 while (i > w->inodes.data && i[-1].snapshot > snapshot)
707 ret = darray_insert_item(&w->inodes, i - w->inodes.data, new);
715 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
716 struct inode_walker *w, struct bpos pos)
718 int ret = get_inodes_all_snapshots(trans, w, pos.inode);
722 return lookup_inode_for_snapshot(trans->c, w, pos.snapshot);
725 static int __get_visible_inodes(struct btree_trans *trans,
726 struct inode_walker *w,
727 struct snapshots_seen *s,
730 struct bch_fs *c = trans->c;
731 struct btree_iter iter;
737 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
738 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
739 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
741 if (k.k->p.offset != inum)
744 if (!ref_visible(c, s, s->pos.snapshot, equiv))
747 if (bkey_is_inode(k.k))
750 if (equiv >= s->pos.snapshot)
753 bch2_trans_iter_exit(trans, &iter);
758 static int check_key_has_snapshot(struct btree_trans *trans,
759 struct btree_iter *iter,
762 struct bch_fs *c = trans->c;
763 struct printbuf buf = PRINTBUF;
766 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
767 "key in missing snapshot: %s",
768 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
769 ret = bch2_btree_delete_at(trans, iter,
770 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
776 static int hash_redo_key(struct btree_trans *trans,
777 const struct bch_hash_desc desc,
778 struct bch_hash_info *hash_info,
779 struct btree_iter *k_iter, struct bkey_s_c k)
781 struct bkey_i *delete;
784 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
786 return PTR_ERR(delete);
788 tmp = bch2_bkey_make_mut_noupdate(trans, k);
792 bkey_init(&delete->k);
793 delete->k.p = k_iter->pos;
794 return bch2_btree_iter_traverse(k_iter) ?:
795 bch2_trans_update(trans, k_iter, delete, 0) ?:
796 bch2_hash_set_snapshot(trans, desc, hash_info,
797 (subvol_inum) { 0, k.k->p.inode },
798 k.k->p.snapshot, tmp,
799 BCH_HASH_SET_MUST_CREATE,
800 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
801 bch2_trans_commit(trans, NULL, NULL,
803 BTREE_INSERT_LAZY_RW);
806 static int hash_check_key(struct btree_trans *trans,
807 const struct bch_hash_desc desc,
808 struct bch_hash_info *hash_info,
809 struct btree_iter *k_iter, struct bkey_s_c hash_k)
811 struct bch_fs *c = trans->c;
812 struct btree_iter iter = { NULL };
813 struct printbuf buf = PRINTBUF;
818 if (hash_k.k->type != desc.key_type)
821 hash = desc.hash_bkey(hash_info, hash_k);
823 if (likely(hash == hash_k.k->p.offset))
826 if (hash_k.k->p.offset < hash)
829 for_each_btree_key_norestart(trans, iter, desc.btree_id,
830 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
831 BTREE_ITER_SLOTS, k, ret) {
832 if (bkey_eq(k.k->p, hash_k.k->p))
835 if (fsck_err_on(k.k->type == desc.key_type &&
836 !desc.cmp_bkey(k, hash_k), c,
837 "duplicate hash table keys:\n%s",
838 (printbuf_reset(&buf),
839 bch2_bkey_val_to_text(&buf, c, hash_k),
841 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
845 if (bkey_deleted(k.k)) {
846 bch2_trans_iter_exit(trans, &iter);
851 bch2_trans_iter_exit(trans, &iter);
855 if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
856 bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
857 (printbuf_reset(&buf),
858 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
859 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
860 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
861 bch_err(c, "hash_redo_key err %s", bch2_err_str(ret));
864 ret = -BCH_ERR_transaction_restart_nested;
870 static int check_inode(struct btree_trans *trans,
871 struct btree_iter *iter,
873 struct bch_inode_unpacked *prev,
874 struct snapshots_seen *s,
877 struct bch_fs *c = trans->c;
878 struct bch_inode_unpacked u;
879 bool do_update = false;
882 ret = check_key_has_snapshot(trans, iter, k);
888 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
893 * if snapshot id isn't a leaf node, skip it - deletion in
894 * particular is not atomic, so on the internal snapshot nodes
895 * we can see inodes marked for deletion after a clean shutdown
897 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
900 if (!bkey_is_inode(k.k))
903 BUG_ON(bch2_inode_unpack(k, &u));
906 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
907 BCH_INODE_I_SECTORS_DIRTY|
908 BCH_INODE_UNLINKED)))
911 if (prev->bi_inum != u.bi_inum)
914 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
915 inode_d_type(prev) != inode_d_type(&u), c,
916 "inodes in different snapshots don't match")) {
917 bch_err(c, "repair not implemented yet");
921 if (u.bi_flags & BCH_INODE_UNLINKED &&
923 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
925 bch2_trans_unlock(trans);
928 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
929 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
930 bch_err(c, "error in fsck: error while deleting inode: %s",
935 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
937 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
939 bch_verbose(c, "truncating inode %llu", u.bi_inum);
941 bch2_trans_unlock(trans);
945 * XXX: need to truncate partial blocks too here - or ideally
946 * just switch units to bytes and that issue goes away
948 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
949 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
951 POS(u.bi_inum, U64_MAX),
953 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
954 bch_err(c, "error in fsck: error truncating inode: %s",
960 * We truncated without our normal sector accounting hook, just
961 * make sure we recalculate it:
963 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
965 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
969 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
971 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
975 bch_verbose(c, "recounting sectors for inode %llu",
978 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
980 bch_err(c, "error in fsck: error recounting inode sectors: %s",
981 bch2_err_str(sectors));
985 u.bi_sectors = sectors;
986 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
990 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
993 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
998 ret = __write_inode(trans, &u, iter->pos.snapshot);
1000 bch_err(c, "error in fsck: error updating inode: %s",
1011 int bch2_check_inodes(struct bch_fs *c)
1013 bool full = c->opts.fsck;
1014 struct btree_trans trans;
1015 struct btree_iter iter;
1016 struct bch_inode_unpacked prev = { 0 };
1017 struct snapshots_seen s;
1021 snapshots_seen_init(&s);
1022 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1024 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
1026 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1027 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1028 check_inode(&trans, &iter, k, &prev, &s, full));
1030 bch2_trans_exit(&trans);
1031 snapshots_seen_exit(&s);
1038 * Checking for overlapping extents needs to be reimplemented
1041 static int fix_overlapping_extent(struct btree_trans *trans,
1042 struct bkey_s_c k, struct bpos cut_at)
1044 struct btree_iter iter;
1048 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1049 ret = PTR_ERR_OR_ZERO(u);
1053 bkey_reassemble(u, k);
1054 bch2_cut_front(cut_at, u);
1058 * We don't want to go through the extent_handle_overwrites path:
1060 * XXX: this is going to screw up disk accounting, extent triggers
1061 * assume things about extent overwrites - we should be running the
1062 * triggers manually here
1064 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1065 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1067 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1068 ret = bch2_btree_iter_traverse(&iter) ?:
1069 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1070 bch2_trans_commit(trans, NULL, NULL,
1071 BTREE_INSERT_NOFAIL|
1072 BTREE_INSERT_LAZY_RW);
1073 bch2_trans_iter_exit(trans, &iter);
1078 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1079 struct btree_iter *iter,
1082 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
1085 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1086 struct bkey_s_c_dirent d)
1088 return inode->bi_dir == d.k->p.inode &&
1089 inode->bi_dir_offset == d.k->p.offset;
1092 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1093 struct bch_inode_unpacked *inode)
1095 return d.v->d_type == DT_SUBVOL
1096 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1097 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1100 static int inode_backpointer_exists(struct btree_trans *trans,
1101 struct bch_inode_unpacked *inode,
1104 struct btree_iter iter;
1105 struct bkey_s_c_dirent d;
1108 d = dirent_get_by_pos(trans, &iter,
1109 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1112 return bch2_err_matches(ret, ENOENT) ? 0 : ret;
1114 ret = dirent_points_to_inode(d, inode);
1115 bch2_trans_iter_exit(trans, &iter);
1119 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1121 struct bch_fs *c = trans->c;
1122 struct inode_walker_entry *i;
1123 u32 restart_count = trans->restart_count;
1127 darray_for_each(w->inodes, i) {
1128 if (i->inode.bi_sectors == i->count)
1131 count2 = bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot);
1133 if (i->count != count2) {
1134 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1137 if (i->inode.bi_sectors == i->count)
1141 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1142 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1143 w->cur_inum, i->snapshot,
1144 i->inode.bi_sectors, i->count)) {
1145 i->inode.bi_sectors = i->count;
1146 ret = write_inode(trans, &i->inode, i->snapshot);
1154 if (!ret && trans_was_restarted(trans, restart_count))
1155 ret = -BCH_ERR_transaction_restart_nested;
1162 struct snapshots_seen seen;
1165 typedef DARRAY(struct extent_end) extent_ends;
1167 static int get_print_extent(struct btree_trans *trans, struct bpos pos, struct printbuf *out)
1169 struct btree_iter iter;
1173 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_extents, pos,
1175 BTREE_ITER_ALL_SNAPSHOTS|
1176 BTREE_ITER_NOT_EXTENTS);
1181 bch2_bkey_val_to_text(out, trans->c, k);
1182 bch2_trans_iter_exit(trans, &iter);
1186 static int check_overlapping_extents(struct btree_trans *trans,
1187 struct snapshots_seen *seen,
1188 extent_ends *extent_ends,
1190 struct btree_iter *iter)
1192 struct bch_fs *c = trans->c;
1193 struct extent_end *i;
1194 struct printbuf buf = PRINTBUF;
1197 darray_for_each(*extent_ends, i) {
1198 /* duplicate, due to transaction restart: */
1199 if (i->offset == k.k->p.offset &&
1200 i->snapshot == k.k->p.snapshot)
1203 if (!ref_visible2(c,
1204 k.k->p.snapshot, seen,
1205 i->snapshot, &i->seen))
1208 if (i->offset <= bkey_start_offset(k.k))
1211 printbuf_reset(&buf);
1212 prt_str(&buf, "overlapping extents:\n ");
1213 bch2_bkey_val_to_text(&buf, c, k);
1214 prt_str(&buf, "\n ");
1216 ret = get_print_extent(trans, SPOS(k.k->p.inode, i->offset, i->snapshot), &buf);
1220 if (fsck_err(c, "%s", buf.buf)) {
1221 struct bkey_i *update = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1222 if ((ret = PTR_ERR_OR_ZERO(update)))
1224 bkey_reassemble(update, k);
1225 ret = bch2_trans_update_extent(trans, iter, update,
1226 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1233 printbuf_exit(&buf);
1237 static int extent_ends_at(extent_ends *extent_ends,
1238 struct snapshots_seen *seen,
1241 struct extent_end *i, n = (struct extent_end) {
1242 .snapshot = k.k->p.snapshot,
1243 .offset = k.k->p.offset,
1247 n.seen.ids.data = kmemdup(seen->ids.data,
1248 sizeof(seen->ids.data[0]) * seen->ids.size,
1250 if (!n.seen.ids.data)
1251 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1253 darray_for_each(*extent_ends, i) {
1254 if (i->snapshot == k.k->p.snapshot) {
1255 snapshots_seen_exit(&i->seen);
1260 if (i->snapshot >= k.k->p.snapshot)
1264 return darray_insert_item(extent_ends, i - extent_ends->data, n);
1267 static void extent_ends_reset(extent_ends *extent_ends)
1269 struct extent_end *i;
1271 darray_for_each(*extent_ends, i)
1272 snapshots_seen_exit(&i->seen);
1274 extent_ends->nr = 0;
1277 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1279 struct inode_walker *inode,
1280 struct snapshots_seen *s,
1281 extent_ends *extent_ends)
1283 struct bch_fs *c = trans->c;
1284 struct inode_walker_entry *i;
1285 struct printbuf buf = PRINTBUF;
1289 ret = check_key_has_snapshot(trans, iter, k);
1291 ret = ret < 0 ? ret : 0;
1296 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1298 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1302 if (k.k->type == KEY_TYPE_whiteout)
1305 if (inode->cur_inum != k.k->p.inode) {
1306 ret = check_i_sectors(trans, inode);
1310 extent_ends_reset(extent_ends);
1313 BUG_ON(!iter->path->should_be_locked);
1315 ret = check_overlapping_extents(trans, s, extent_ends, k, iter);
1319 ret = extent_ends_at(extent_ends, s, k);
1323 i = walk_inode(trans, inode, equiv);
1324 ret = PTR_ERR_OR_ZERO(i);
1328 if (fsck_err_on(!i, c,
1329 "extent in missing inode:\n %s",
1330 (printbuf_reset(&buf),
1331 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1332 ret = bch2_btree_delete_at(trans, iter,
1333 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1340 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1341 !S_ISLNK(i->inode.bi_mode), c,
1342 "extent in non regular inode mode %o:\n %s",
1344 (printbuf_reset(&buf),
1345 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1346 ret = bch2_btree_delete_at(trans, iter,
1347 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1352 * Check inodes in reverse order, from oldest snapshots to newest, so
1353 * that we emit the fewest number of whiteouts necessary:
1355 for (i = inode->inodes.data + inode->inodes.nr - 1;
1356 i >= inode->inodes.data;
1358 if (i->snapshot > equiv.snapshot ||
1359 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1362 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1363 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1364 !bkey_extent_is_reservation(k), c,
1365 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1366 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1367 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1368 struct btree_iter iter2;
1370 bch2_trans_copy_iter(&iter2, iter);
1371 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1372 ret = bch2_btree_iter_traverse(&iter2) ?:
1373 bch2_btree_delete_at(trans, &iter2,
1374 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1375 bch2_trans_iter_exit(trans, &iter2);
1379 if (i->snapshot != equiv.snapshot) {
1380 ret = snapshots_seen_add(c, s, i->snapshot);
1387 if (bkey_extent_is_allocation(k.k))
1388 for_each_visible_inode(c, s, inode, equiv.snapshot, i)
1389 i->count += k.k->size;
1391 bch2_bkey_buf_reassemble(&prev, c, k);
1397 printbuf_exit(&buf);
1399 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1405 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1406 * that i_size an i_sectors are consistent
1408 int bch2_check_extents(struct bch_fs *c)
1410 struct inode_walker w = inode_walker_init();
1411 struct snapshots_seen s;
1412 struct btree_trans trans;
1413 struct btree_iter iter;
1415 extent_ends extent_ends = { 0 };
1416 struct disk_reservation res = { 0 };
1419 snapshots_seen_init(&s);
1420 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1422 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1423 POS(BCACHEFS_ROOT_INO, 0),
1424 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1426 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
1427 bch2_disk_reservation_put(c, &res);
1428 check_extent(&trans, &iter, k, &w, &s, &extent_ends);
1431 bch2_disk_reservation_put(c, &res);
1432 extent_ends_reset(&extent_ends);
1433 darray_exit(&extent_ends);
1434 inode_walker_exit(&w);
1435 bch2_trans_exit(&trans);
1436 snapshots_seen_exit(&s);
1443 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1445 struct bch_fs *c = trans->c;
1446 struct inode_walker_entry *i;
1447 u32 restart_count = trans->restart_count;
1451 darray_for_each(w->inodes, i) {
1452 if (i->inode.bi_nlink == i->count)
1455 count2 = bch2_count_subdirs(trans, w->cur_inum, i->snapshot);
1459 if (i->count != count2) {
1460 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1463 if (i->inode.bi_nlink == i->count)
1467 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1468 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1469 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1470 i->inode.bi_nlink = i->count;
1471 ret = write_inode(trans, &i->inode, i->snapshot);
1479 if (!ret && trans_was_restarted(trans, restart_count))
1480 ret = -BCH_ERR_transaction_restart_nested;
1484 static int check_dirent_target(struct btree_trans *trans,
1485 struct btree_iter *iter,
1486 struct bkey_s_c_dirent d,
1487 struct bch_inode_unpacked *target,
1488 u32 target_snapshot)
1490 struct bch_fs *c = trans->c;
1491 struct bkey_i_dirent *n;
1492 bool backpointer_exists = true;
1493 struct printbuf buf = PRINTBUF;
1496 if (!target->bi_dir &&
1497 !target->bi_dir_offset) {
1498 target->bi_dir = d.k->p.inode;
1499 target->bi_dir_offset = d.k->p.offset;
1501 ret = __write_inode(trans, target, target_snapshot);
1506 if (!inode_points_to_dirent(target, d)) {
1507 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1511 backpointer_exists = ret;
1514 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1515 backpointer_exists, c,
1516 "directory %llu with multiple links",
1518 ret = __remove_dirent(trans, d.k->p);
1522 if (fsck_err_on(backpointer_exists &&
1523 !target->bi_nlink, c,
1524 "inode %llu type %s has multiple links but i_nlink 0",
1525 target->bi_inum, bch2_d_types[d.v->d_type])) {
1527 target->bi_flags &= ~BCH_INODE_UNLINKED;
1529 ret = __write_inode(trans, target, target_snapshot);
1534 if (fsck_err_on(!backpointer_exists, c,
1535 "inode %llu:%u has wrong backpointer:\n"
1537 "should be %llu:%llu",
1538 target->bi_inum, target_snapshot,
1540 target->bi_dir_offset,
1543 target->bi_dir = d.k->p.inode;
1544 target->bi_dir_offset = d.k->p.offset;
1546 ret = __write_inode(trans, target, target_snapshot);
1552 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1553 "incorrect d_type: got %s, should be %s:\n%s",
1554 bch2_d_type_str(d.v->d_type),
1555 bch2_d_type_str(inode_d_type(target)),
1556 (printbuf_reset(&buf),
1557 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1558 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1559 ret = PTR_ERR_OR_ZERO(n);
1563 bkey_reassemble(&n->k_i, d.s_c);
1564 n->v.d_type = inode_d_type(target);
1566 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1570 d = dirent_i_to_s_c(n);
1573 if (d.v->d_type == DT_SUBVOL &&
1574 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1575 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1576 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1577 le32_to_cpu(d.v->d_parent_subvol),
1578 target->bi_parent_subvol))) {
1579 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1580 ret = PTR_ERR_OR_ZERO(n);
1584 bkey_reassemble(&n->k_i, d.s_c);
1585 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1587 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1591 d = dirent_i_to_s_c(n);
1596 printbuf_exit(&buf);
1598 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1603 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1605 struct bch_hash_info *hash_info,
1606 struct inode_walker *dir,
1607 struct inode_walker *target,
1608 struct snapshots_seen *s)
1610 struct bch_fs *c = trans->c;
1611 struct bkey_s_c_dirent d;
1612 struct inode_walker_entry *i;
1613 struct printbuf buf = PRINTBUF;
1617 ret = check_key_has_snapshot(trans, iter, k);
1619 ret = ret < 0 ? ret : 0;
1624 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1626 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1630 if (k.k->type == KEY_TYPE_whiteout)
1633 if (dir->cur_inum != k.k->p.inode) {
1634 ret = check_subdir_count(trans, dir);
1639 BUG_ON(!iter->path->should_be_locked);
1641 i = walk_inode(trans, dir, equiv);
1642 ret = PTR_ERR_OR_ZERO(i);
1646 if (dir->first_this_inode)
1647 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1648 dir->first_this_inode = false;
1650 if (fsck_err_on(!i, c,
1651 "dirent in nonexisting directory:\n%s",
1652 (printbuf_reset(&buf),
1653 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1654 ret = bch2_btree_delete_at(trans, iter,
1655 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1662 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1663 "dirent in non directory inode type %s:\n%s",
1664 bch2_d_type_str(inode_d_type(&i->inode)),
1665 (printbuf_reset(&buf),
1666 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1667 ret = bch2_btree_delete_at(trans, iter, 0);
1671 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1675 /* dirent has been deleted */
1680 if (k.k->type != KEY_TYPE_dirent)
1683 d = bkey_s_c_to_dirent(k);
1685 if (d.v->d_type == DT_SUBVOL) {
1686 struct bch_inode_unpacked subvol_root;
1687 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1688 u32 target_snapshot;
1691 ret = __subvol_lookup(trans, target_subvol,
1692 &target_snapshot, &target_inum);
1693 if (ret && !bch2_err_matches(ret, ENOENT))
1696 if (fsck_err_on(ret, c,
1697 "dirent points to missing subvolume %u",
1698 le32_to_cpu(d.v->d_child_subvol))) {
1699 ret = __remove_dirent(trans, d.k->p);
1703 ret = __lookup_inode(trans, target_inum,
1704 &subvol_root, &target_snapshot);
1705 if (ret && !bch2_err_matches(ret, ENOENT))
1708 if (fsck_err_on(ret, c,
1709 "subvolume %u points to missing subvolume root %llu",
1712 bch_err(c, "repair not implemented yet");
1717 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1718 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1720 subvol_root.bi_subvol, target_subvol)) {
1721 subvol_root.bi_subvol = target_subvol;
1722 ret = __write_inode(trans, &subvol_root, target_snapshot);
1727 ret = check_dirent_target(trans, iter, d, &subvol_root,
1732 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1736 if (fsck_err_on(!target->inodes.nr, c,
1737 "dirent points to missing inode: (equiv %u)\n%s",
1739 (printbuf_reset(&buf),
1740 bch2_bkey_val_to_text(&buf, c, k),
1742 ret = __remove_dirent(trans, d.k->p);
1747 darray_for_each(target->inodes, i) {
1748 ret = check_dirent_target(trans, iter, d,
1749 &i->inode, i->snapshot);
1755 if (d.v->d_type == DT_DIR)
1756 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1762 printbuf_exit(&buf);
1764 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1770 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1773 int bch2_check_dirents(struct bch_fs *c)
1775 struct inode_walker dir = inode_walker_init();
1776 struct inode_walker target = inode_walker_init();
1777 struct snapshots_seen s;
1778 struct bch_hash_info hash_info;
1779 struct btree_trans trans;
1780 struct btree_iter iter;
1784 snapshots_seen_init(&s);
1785 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1787 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1788 POS(BCACHEFS_ROOT_INO, 0),
1789 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1792 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1793 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1795 bch2_trans_exit(&trans);
1796 snapshots_seen_exit(&s);
1797 inode_walker_exit(&dir);
1798 inode_walker_exit(&target);
1805 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1807 struct bch_hash_info *hash_info,
1808 struct inode_walker *inode)
1810 struct bch_fs *c = trans->c;
1811 struct inode_walker_entry *i;
1814 ret = check_key_has_snapshot(trans, iter, k);
1818 i = walk_inode(trans, inode, k.k->p);
1819 ret = PTR_ERR_OR_ZERO(i);
1823 if (inode->first_this_inode)
1824 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1825 inode->first_this_inode = false;
1827 if (fsck_err_on(!i, c,
1828 "xattr for missing inode %llu",
1830 return bch2_btree_delete_at(trans, iter, 0);
1835 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1837 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1843 * Walk xattrs: verify that they all have a corresponding inode
1845 int bch2_check_xattrs(struct bch_fs *c)
1847 struct inode_walker inode = inode_walker_init();
1848 struct bch_hash_info hash_info;
1849 struct btree_trans trans;
1850 struct btree_iter iter;
1854 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1856 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1857 POS(BCACHEFS_ROOT_INO, 0),
1858 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1861 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1862 check_xattr(&trans, &iter, k, &hash_info, &inode));
1864 bch2_trans_exit(&trans);
1871 static int check_root_trans(struct btree_trans *trans)
1873 struct bch_fs *c = trans->c;
1874 struct bch_inode_unpacked root_inode;
1879 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1880 if (ret && !bch2_err_matches(ret, ENOENT))
1883 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1884 struct bkey_i_subvolume root_subvol;
1887 inum = BCACHEFS_ROOT_INO;
1889 bkey_subvolume_init(&root_subvol.k_i);
1890 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1891 root_subvol.v.flags = 0;
1892 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1893 root_subvol.v.inode = cpu_to_le64(inum);
1894 ret = commit_do(trans, NULL, NULL,
1895 BTREE_INSERT_NOFAIL|
1896 BTREE_INSERT_LAZY_RW,
1897 __bch2_btree_insert(trans, BTREE_ID_subvolumes,
1898 &root_subvol.k_i, 0));
1900 bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1906 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1907 if (ret && !bch2_err_matches(ret, ENOENT))
1910 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1911 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1912 "root inode not a directory")) {
1913 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1915 root_inode.bi_inum = inum;
1917 ret = __write_inode(trans, &root_inode, snapshot);
1919 bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1926 /* Get root directory, create if it doesn't exist: */
1927 int bch2_check_root(struct bch_fs *c)
1931 ret = bch2_trans_do(c, NULL, NULL,
1932 BTREE_INSERT_NOFAIL|
1933 BTREE_INSERT_LAZY_RW,
1934 check_root_trans(&trans));
1941 struct pathbuf_entry {
1946 typedef DARRAY(struct pathbuf_entry) pathbuf;
1948 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1950 struct pathbuf_entry *i;
1952 darray_for_each(*p, i)
1953 if (i->inum == inum &&
1954 i->snapshot == snapshot)
1960 static int path_down(struct bch_fs *c, pathbuf *p,
1961 u64 inum, u32 snapshot)
1963 int ret = darray_push(p, ((struct pathbuf_entry) {
1965 .snapshot = snapshot,
1969 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1975 * Check that a given inode is reachable from the root:
1977 * XXX: we should also be verifying that inodes are in the right subvolumes
1979 static int check_path(struct btree_trans *trans,
1981 struct bch_inode_unpacked *inode,
1984 struct bch_fs *c = trans->c;
1987 snapshot = bch2_snapshot_equiv(c, snapshot);
1990 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1991 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1992 struct btree_iter dirent_iter;
1993 struct bkey_s_c_dirent d;
1994 u32 parent_snapshot = snapshot;
1996 if (inode->bi_subvol) {
1999 ret = subvol_lookup(trans, inode->bi_parent_subvol,
2000 &parent_snapshot, &inum);
2005 ret = lockrestart_do(trans,
2006 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
2007 SPOS(inode->bi_dir, inode->bi_dir_offset,
2008 parent_snapshot))).k));
2009 if (ret && !bch2_err_matches(ret, ENOENT))
2012 if (!ret && !dirent_points_to_inode(d, inode)) {
2013 bch2_trans_iter_exit(trans, &dirent_iter);
2014 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
2017 if (bch2_err_matches(ret, ENOENT)) {
2018 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2019 inode->bi_inum, snapshot,
2020 bch2_d_type_str(inode_d_type(inode)),
2023 inode->bi_dir_offset))
2024 ret = reattach_inode(trans, inode, snapshot);
2028 bch2_trans_iter_exit(trans, &dirent_iter);
2030 if (!S_ISDIR(inode->bi_mode))
2033 ret = path_down(c, p, inode->bi_inum, snapshot);
2035 bch_err(c, "memory allocation failure");
2039 snapshot = parent_snapshot;
2041 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2043 /* Should have been caught in dirents pass */
2044 bch_err(c, "error looking up parent directory: %i", ret);
2048 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2049 struct pathbuf_entry *i;
2051 /* XXX print path */
2052 bch_err(c, "directory structure loop");
2054 darray_for_each(*p, i)
2055 pr_err("%llu:%u", i->inum, i->snapshot);
2056 pr_err("%llu:%u", inode->bi_inum, snapshot);
2058 if (!fsck_err(c, "directory structure loop"))
2061 ret = commit_do(trans, NULL, NULL,
2062 BTREE_INSERT_NOFAIL|
2063 BTREE_INSERT_LAZY_RW,
2064 remove_backpointer(trans, inode));
2066 bch_err(c, "error removing dirent: %i", ret);
2070 ret = reattach_inode(trans, inode, snapshot);
2080 * Check for unreachable inodes, as well as loops in the directory structure:
2081 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2084 int bch2_check_directory_structure(struct bch_fs *c)
2086 struct btree_trans trans;
2087 struct btree_iter iter;
2089 struct bch_inode_unpacked u;
2090 pathbuf path = { 0, };
2093 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2095 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2097 BTREE_ITER_PREFETCH|
2098 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2099 if (!bkey_is_inode(k.k))
2102 ret = bch2_inode_unpack(k, &u);
2104 /* Should have been caught earlier in fsck: */
2105 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2109 if (u.bi_flags & BCH_INODE_UNLINKED)
2112 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2116 bch2_trans_iter_exit(&trans, &iter);
2117 bch2_trans_exit(&trans);
2125 struct nlink_table {
2136 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2137 u64 inum, u32 snapshot)
2139 if (t->nr == t->size) {
2140 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2141 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2144 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2146 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2150 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2158 t->d[t->nr++] = (struct nlink) {
2160 .snapshot = snapshot,
2166 static int nlink_cmp(const void *_l, const void *_r)
2168 const struct nlink *l = _l;
2169 const struct nlink *r = _r;
2171 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2174 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2175 struct nlink_table *links,
2176 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2178 struct nlink *link, key = {
2179 .inum = inum, .snapshot = U32_MAX,
2182 if (inum < range_start || inum >= range_end)
2185 link = __inline_bsearch(&key, links->d, links->nr,
2186 sizeof(links->d[0]), nlink_cmp);
2190 while (link > links->d && link[0].inum == link[-1].inum)
2193 for (; link < links->d + links->nr && link->inum == inum; link++)
2194 if (ref_visible(c, s, snapshot, link->snapshot)) {
2196 if (link->snapshot >= snapshot)
2202 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2203 struct nlink_table *t,
2204 u64 start, u64 *end)
2206 struct btree_trans trans;
2207 struct btree_iter iter;
2209 struct bch_inode_unpacked u;
2212 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2214 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2217 BTREE_ITER_PREFETCH|
2218 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2219 if (!bkey_is_inode(k.k))
2222 /* Should never fail, checked by bch2_inode_invalid: */
2223 BUG_ON(bch2_inode_unpack(k, &u));
2226 * Backpointer and directory structure checks are sufficient for
2227 * directories, since they can't have hardlinks:
2229 if (S_ISDIR(u.bi_mode))
2235 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2237 *end = k.k->p.offset;
2243 bch2_trans_iter_exit(&trans, &iter);
2244 bch2_trans_exit(&trans);
2247 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2253 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2254 u64 range_start, u64 range_end)
2256 struct btree_trans trans;
2257 struct snapshots_seen s;
2258 struct btree_iter iter;
2260 struct bkey_s_c_dirent d;
2263 snapshots_seen_init(&s);
2265 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2267 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2269 BTREE_ITER_PREFETCH|
2270 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2271 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2275 switch (k.k->type) {
2276 case KEY_TYPE_dirent:
2277 d = bkey_s_c_to_dirent(k);
2279 if (d.v->d_type != DT_DIR &&
2280 d.v->d_type != DT_SUBVOL)
2281 inc_link(c, &s, links, range_start, range_end,
2282 le64_to_cpu(d.v->d_inum),
2283 bch2_snapshot_equiv(c, d.k->p.snapshot));
2287 bch2_trans_iter_exit(&trans, &iter);
2290 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2292 bch2_trans_exit(&trans);
2293 snapshots_seen_exit(&s);
2297 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2299 struct nlink_table *links,
2300 size_t *idx, u64 range_end)
2302 struct bch_fs *c = trans->c;
2303 struct bch_inode_unpacked u;
2304 struct nlink *link = &links->d[*idx];
2307 if (k.k->p.offset >= range_end)
2310 if (!bkey_is_inode(k.k))
2313 BUG_ON(bch2_inode_unpack(k, &u));
2315 if (S_ISDIR(u.bi_mode))
2321 while ((cmp_int(link->inum, k.k->p.offset) ?:
2322 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2323 BUG_ON(*idx == links->nr);
2324 link = &links->d[++*idx];
2327 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2328 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2329 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2330 bch2_inode_nlink_get(&u), link->count)) {
2331 bch2_inode_nlink_set(&u, link->count);
2332 ret = __write_inode(trans, &u, k.k->p.snapshot);
2339 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2340 struct nlink_table *links,
2341 u64 range_start, u64 range_end)
2343 struct btree_trans trans;
2344 struct btree_iter iter;
2349 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2351 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2352 POS(0, range_start),
2353 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2354 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2355 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2357 bch2_trans_exit(&trans);
2360 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2367 int bch2_check_nlinks(struct bch_fs *c)
2369 struct nlink_table links = { 0 };
2370 u64 this_iter_range_start, next_iter_range_start = 0;
2374 this_iter_range_start = next_iter_range_start;
2375 next_iter_range_start = U64_MAX;
2377 ret = check_nlinks_find_hardlinks(c, &links,
2378 this_iter_range_start,
2379 &next_iter_range_start);
2381 ret = check_nlinks_walk_dirents(c, &links,
2382 this_iter_range_start,
2383 next_iter_range_start);
2387 ret = check_nlinks_update_hardlinks(c, &links,
2388 this_iter_range_start,
2389 next_iter_range_start);
2394 } while (next_iter_range_start != U64_MAX);
2403 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2406 struct bkey_s_c_reflink_p p;
2407 struct bkey_i_reflink_p *u;
2410 if (k.k->type != KEY_TYPE_reflink_p)
2413 p = bkey_s_c_to_reflink_p(k);
2415 if (!p.v->front_pad && !p.v->back_pad)
2418 u = bch2_trans_kmalloc(trans, sizeof(*u));
2419 ret = PTR_ERR_OR_ZERO(u);
2423 bkey_reassemble(&u->k_i, k);
2427 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2430 int bch2_fix_reflink_p(struct bch_fs *c)
2432 struct btree_iter iter;
2436 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2439 ret = bch2_trans_run(c,
2440 for_each_btree_key_commit(&trans, iter,
2441 BTREE_ID_extents, POS_MIN,
2442 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2443 BTREE_ITER_ALL_SNAPSHOTS, k,
2444 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2445 fix_reflink_p_key(&trans, &iter, k)));