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 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 static int check_inodes(struct bch_fs *c, bool full)
1013 struct btree_trans trans;
1014 struct btree_iter iter;
1015 struct bch_inode_unpacked prev = { 0 };
1016 struct snapshots_seen s;
1020 snapshots_seen_init(&s);
1021 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1023 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
1025 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1026 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1027 check_inode(&trans, &iter, k, &prev, &s, full));
1029 bch2_trans_exit(&trans);
1030 snapshots_seen_exit(&s);
1037 * Checking for overlapping extents needs to be reimplemented
1040 static int fix_overlapping_extent(struct btree_trans *trans,
1041 struct bkey_s_c k, struct bpos cut_at)
1043 struct btree_iter iter;
1047 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1048 ret = PTR_ERR_OR_ZERO(u);
1052 bkey_reassemble(u, k);
1053 bch2_cut_front(cut_at, u);
1057 * We don't want to go through the extent_handle_overwrites path:
1059 * XXX: this is going to screw up disk accounting, extent triggers
1060 * assume things about extent overwrites - we should be running the
1061 * triggers manually here
1063 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1064 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1066 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1067 ret = bch2_btree_iter_traverse(&iter) ?:
1068 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1069 bch2_trans_commit(trans, NULL, NULL,
1070 BTREE_INSERT_NOFAIL|
1071 BTREE_INSERT_LAZY_RW);
1072 bch2_trans_iter_exit(trans, &iter);
1077 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1078 struct btree_iter *iter,
1081 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
1084 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1085 struct bkey_s_c_dirent d)
1087 return inode->bi_dir == d.k->p.inode &&
1088 inode->bi_dir_offset == d.k->p.offset;
1091 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1092 struct bch_inode_unpacked *inode)
1094 return d.v->d_type == DT_SUBVOL
1095 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1096 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1099 static int inode_backpointer_exists(struct btree_trans *trans,
1100 struct bch_inode_unpacked *inode,
1103 struct btree_iter iter;
1104 struct bkey_s_c_dirent d;
1107 d = dirent_get_by_pos(trans, &iter,
1108 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1111 return bch2_err_matches(ret, ENOENT) ? 0 : ret;
1113 ret = dirent_points_to_inode(d, inode);
1114 bch2_trans_iter_exit(trans, &iter);
1118 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1120 struct bch_fs *c = trans->c;
1121 struct inode_walker_entry *i;
1122 u32 restart_count = trans->restart_count;
1126 darray_for_each(w->inodes, i) {
1127 if (i->inode.bi_sectors == i->count)
1130 count2 = bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot);
1132 if (i->count != count2) {
1133 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1136 if (i->inode.bi_sectors == i->count)
1140 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1141 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1142 w->cur_inum, i->snapshot,
1143 i->inode.bi_sectors, i->count)) {
1144 i->inode.bi_sectors = i->count;
1145 ret = write_inode(trans, &i->inode, i->snapshot);
1153 if (!ret && trans_was_restarted(trans, restart_count))
1154 ret = -BCH_ERR_transaction_restart_nested;
1161 struct snapshots_seen seen;
1164 typedef DARRAY(struct extent_end) extent_ends;
1166 static int get_print_extent(struct btree_trans *trans, struct bpos pos, struct printbuf *out)
1168 struct btree_iter iter;
1172 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_extents, pos,
1174 BTREE_ITER_ALL_SNAPSHOTS|
1175 BTREE_ITER_NOT_EXTENTS);
1180 bch2_bkey_val_to_text(out, trans->c, k);
1181 bch2_trans_iter_exit(trans, &iter);
1185 static int check_overlapping_extents(struct btree_trans *trans,
1186 struct snapshots_seen *seen,
1187 extent_ends *extent_ends,
1189 struct btree_iter *iter)
1191 struct bch_fs *c = trans->c;
1192 struct extent_end *i;
1193 struct printbuf buf = PRINTBUF;
1196 darray_for_each(*extent_ends, i) {
1197 /* duplicate, due to transaction restart: */
1198 if (i->offset == k.k->p.offset &&
1199 i->snapshot == k.k->p.snapshot)
1202 if (!ref_visible2(c,
1203 k.k->p.snapshot, seen,
1204 i->snapshot, &i->seen))
1207 if (i->offset <= bkey_start_offset(k.k))
1210 printbuf_reset(&buf);
1211 prt_str(&buf, "overlapping extents:\n ");
1212 bch2_bkey_val_to_text(&buf, c, k);
1213 prt_str(&buf, "\n ");
1215 ret = get_print_extent(trans, SPOS(k.k->p.inode, i->offset, i->snapshot), &buf);
1219 if (fsck_err(c, "%s", buf.buf)) {
1220 struct bkey_i *update = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1221 if ((ret = PTR_ERR_OR_ZERO(update)))
1223 bkey_reassemble(update, k);
1224 ret = bch2_trans_update_extent(trans, iter, update,
1225 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1232 printbuf_exit(&buf);
1236 static int extent_ends_at(extent_ends *extent_ends,
1237 struct snapshots_seen *seen,
1240 struct extent_end *i, n = (struct extent_end) {
1241 .snapshot = k.k->p.snapshot,
1242 .offset = k.k->p.offset,
1246 n.seen.ids.data = kmemdup(seen->ids.data,
1247 sizeof(seen->ids.data[0]) * seen->ids.size,
1249 if (!n.seen.ids.data)
1250 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1252 darray_for_each(*extent_ends, i) {
1253 if (i->snapshot == k.k->p.snapshot) {
1254 snapshots_seen_exit(&i->seen);
1259 if (i->snapshot >= k.k->p.snapshot)
1263 return darray_insert_item(extent_ends, i - extent_ends->data, n);
1266 static void extent_ends_reset(extent_ends *extent_ends)
1268 struct extent_end *i;
1270 darray_for_each(*extent_ends, i)
1271 snapshots_seen_exit(&i->seen);
1273 extent_ends->nr = 0;
1276 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1278 struct inode_walker *inode,
1279 struct snapshots_seen *s,
1280 extent_ends *extent_ends)
1282 struct bch_fs *c = trans->c;
1283 struct inode_walker_entry *i;
1284 struct printbuf buf = PRINTBUF;
1288 ret = check_key_has_snapshot(trans, iter, k);
1290 ret = ret < 0 ? ret : 0;
1295 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1297 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1301 if (k.k->type == KEY_TYPE_whiteout)
1304 if (inode->cur_inum != k.k->p.inode) {
1305 ret = check_i_sectors(trans, inode);
1309 extent_ends_reset(extent_ends);
1312 BUG_ON(!iter->path->should_be_locked);
1314 ret = check_overlapping_extents(trans, s, extent_ends, k, iter);
1318 ret = extent_ends_at(extent_ends, s, k);
1322 i = walk_inode(trans, inode, equiv);
1323 ret = PTR_ERR_OR_ZERO(i);
1327 if (fsck_err_on(!i, c,
1328 "extent in missing inode:\n %s",
1329 (printbuf_reset(&buf),
1330 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1331 ret = bch2_btree_delete_at(trans, iter,
1332 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1339 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1340 !S_ISLNK(i->inode.bi_mode), c,
1341 "extent in non regular inode mode %o:\n %s",
1343 (printbuf_reset(&buf),
1344 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1345 ret = bch2_btree_delete_at(trans, iter,
1346 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1351 * Check inodes in reverse order, from oldest snapshots to newest, so
1352 * that we emit the fewest number of whiteouts necessary:
1354 for (i = inode->inodes.data + inode->inodes.nr - 1;
1355 i >= inode->inodes.data;
1357 if (i->snapshot > equiv.snapshot ||
1358 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1361 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1362 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1363 !bkey_extent_is_reservation(k), c,
1364 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1365 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1366 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1367 struct btree_iter iter2;
1369 bch2_trans_copy_iter(&iter2, iter);
1370 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1371 ret = bch2_btree_iter_traverse(&iter2) ?:
1372 bch2_btree_delete_at(trans, &iter2,
1373 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1374 bch2_trans_iter_exit(trans, &iter2);
1378 if (i->snapshot != equiv.snapshot) {
1379 ret = snapshots_seen_add(c, s, i->snapshot);
1386 if (bkey_extent_is_allocation(k.k))
1387 for_each_visible_inode(c, s, inode, equiv.snapshot, i)
1388 i->count += k.k->size;
1390 bch2_bkey_buf_reassemble(&prev, c, k);
1396 printbuf_exit(&buf);
1398 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1404 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1405 * that i_size an i_sectors are consistent
1408 static int 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 bch_verbose(c, "checking extents");
1424 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1425 POS(BCACHEFS_ROOT_INO, 0),
1426 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1428 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
1429 bch2_disk_reservation_put(c, &res);
1430 check_extent(&trans, &iter, k, &w, &s, &extent_ends);
1433 bch2_disk_reservation_put(c, &res);
1434 extent_ends_reset(&extent_ends);
1435 darray_exit(&extent_ends);
1436 inode_walker_exit(&w);
1437 bch2_trans_exit(&trans);
1438 snapshots_seen_exit(&s);
1445 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1447 struct bch_fs *c = trans->c;
1448 struct inode_walker_entry *i;
1449 u32 restart_count = trans->restart_count;
1453 darray_for_each(w->inodes, i) {
1454 if (i->inode.bi_nlink == i->count)
1457 count2 = bch2_count_subdirs(trans, w->cur_inum, i->snapshot);
1461 if (i->count != count2) {
1462 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1465 if (i->inode.bi_nlink == i->count)
1469 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1470 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1471 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1472 i->inode.bi_nlink = i->count;
1473 ret = write_inode(trans, &i->inode, i->snapshot);
1481 if (!ret && trans_was_restarted(trans, restart_count))
1482 ret = -BCH_ERR_transaction_restart_nested;
1486 static int check_dirent_target(struct btree_trans *trans,
1487 struct btree_iter *iter,
1488 struct bkey_s_c_dirent d,
1489 struct bch_inode_unpacked *target,
1490 u32 target_snapshot)
1492 struct bch_fs *c = trans->c;
1493 struct bkey_i_dirent *n;
1494 bool backpointer_exists = true;
1495 struct printbuf buf = PRINTBUF;
1498 if (!target->bi_dir &&
1499 !target->bi_dir_offset) {
1500 target->bi_dir = d.k->p.inode;
1501 target->bi_dir_offset = d.k->p.offset;
1503 ret = __write_inode(trans, target, target_snapshot);
1508 if (!inode_points_to_dirent(target, d)) {
1509 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1513 backpointer_exists = ret;
1516 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1517 backpointer_exists, c,
1518 "directory %llu with multiple links",
1520 ret = __remove_dirent(trans, d.k->p);
1524 if (fsck_err_on(backpointer_exists &&
1525 !target->bi_nlink, c,
1526 "inode %llu type %s has multiple links but i_nlink 0",
1527 target->bi_inum, bch2_d_types[d.v->d_type])) {
1529 target->bi_flags &= ~BCH_INODE_UNLINKED;
1531 ret = __write_inode(trans, target, target_snapshot);
1536 if (fsck_err_on(!backpointer_exists, c,
1537 "inode %llu:%u has wrong backpointer:\n"
1539 "should be %llu:%llu",
1540 target->bi_inum, target_snapshot,
1542 target->bi_dir_offset,
1545 target->bi_dir = d.k->p.inode;
1546 target->bi_dir_offset = d.k->p.offset;
1548 ret = __write_inode(trans, target, target_snapshot);
1554 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1555 "incorrect d_type: got %s, should be %s:\n%s",
1556 bch2_d_type_str(d.v->d_type),
1557 bch2_d_type_str(inode_d_type(target)),
1558 (printbuf_reset(&buf),
1559 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1560 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1561 ret = PTR_ERR_OR_ZERO(n);
1565 bkey_reassemble(&n->k_i, d.s_c);
1566 n->v.d_type = inode_d_type(target);
1568 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1572 d = dirent_i_to_s_c(n);
1575 if (d.v->d_type == DT_SUBVOL &&
1576 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1577 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1578 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1579 le32_to_cpu(d.v->d_parent_subvol),
1580 target->bi_parent_subvol))) {
1581 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1582 ret = PTR_ERR_OR_ZERO(n);
1586 bkey_reassemble(&n->k_i, d.s_c);
1587 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1589 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1593 d = dirent_i_to_s_c(n);
1598 printbuf_exit(&buf);
1600 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1605 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1607 struct bch_hash_info *hash_info,
1608 struct inode_walker *dir,
1609 struct inode_walker *target,
1610 struct snapshots_seen *s)
1612 struct bch_fs *c = trans->c;
1613 struct bkey_s_c_dirent d;
1614 struct inode_walker_entry *i;
1615 struct printbuf buf = PRINTBUF;
1619 ret = check_key_has_snapshot(trans, iter, k);
1621 ret = ret < 0 ? ret : 0;
1626 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1628 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1632 if (k.k->type == KEY_TYPE_whiteout)
1635 if (dir->cur_inum != k.k->p.inode) {
1636 ret = check_subdir_count(trans, dir);
1641 BUG_ON(!iter->path->should_be_locked);
1643 i = walk_inode(trans, dir, equiv);
1644 ret = PTR_ERR_OR_ZERO(i);
1648 if (dir->first_this_inode)
1649 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1650 dir->first_this_inode = false;
1652 if (fsck_err_on(!i, c,
1653 "dirent in nonexisting directory:\n%s",
1654 (printbuf_reset(&buf),
1655 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1656 ret = bch2_btree_delete_at(trans, iter,
1657 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1664 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1665 "dirent in non directory inode type %s:\n%s",
1666 bch2_d_type_str(inode_d_type(&i->inode)),
1667 (printbuf_reset(&buf),
1668 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1669 ret = bch2_btree_delete_at(trans, iter, 0);
1673 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1677 /* dirent has been deleted */
1682 if (k.k->type != KEY_TYPE_dirent)
1685 d = bkey_s_c_to_dirent(k);
1687 if (d.v->d_type == DT_SUBVOL) {
1688 struct bch_inode_unpacked subvol_root;
1689 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1690 u32 target_snapshot;
1693 ret = __subvol_lookup(trans, target_subvol,
1694 &target_snapshot, &target_inum);
1695 if (ret && !bch2_err_matches(ret, ENOENT))
1698 if (fsck_err_on(ret, c,
1699 "dirent points to missing subvolume %u",
1700 le32_to_cpu(d.v->d_child_subvol))) {
1701 ret = __remove_dirent(trans, d.k->p);
1705 ret = __lookup_inode(trans, target_inum,
1706 &subvol_root, &target_snapshot);
1707 if (ret && !bch2_err_matches(ret, ENOENT))
1710 if (fsck_err_on(ret, c,
1711 "subvolume %u points to missing subvolume root %llu",
1714 bch_err(c, "repair not implemented yet");
1719 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1720 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1722 subvol_root.bi_subvol, target_subvol)) {
1723 subvol_root.bi_subvol = target_subvol;
1724 ret = __write_inode(trans, &subvol_root, target_snapshot);
1729 ret = check_dirent_target(trans, iter, d, &subvol_root,
1734 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1738 if (fsck_err_on(!target->inodes.nr, c,
1739 "dirent points to missing inode: (equiv %u)\n%s",
1741 (printbuf_reset(&buf),
1742 bch2_bkey_val_to_text(&buf, c, k),
1744 ret = __remove_dirent(trans, d.k->p);
1749 darray_for_each(target->inodes, i) {
1750 ret = check_dirent_target(trans, iter, d,
1751 &i->inode, i->snapshot);
1757 if (d.v->d_type == DT_DIR)
1758 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1764 printbuf_exit(&buf);
1766 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1772 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1776 static int check_dirents(struct bch_fs *c)
1778 struct inode_walker dir = inode_walker_init();
1779 struct inode_walker target = inode_walker_init();
1780 struct snapshots_seen s;
1781 struct bch_hash_info hash_info;
1782 struct btree_trans trans;
1783 struct btree_iter iter;
1787 bch_verbose(c, "checking dirents");
1789 snapshots_seen_init(&s);
1790 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1792 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1793 POS(BCACHEFS_ROOT_INO, 0),
1794 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1797 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1798 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1800 bch2_trans_exit(&trans);
1801 snapshots_seen_exit(&s);
1802 inode_walker_exit(&dir);
1803 inode_walker_exit(&target);
1810 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1812 struct bch_hash_info *hash_info,
1813 struct inode_walker *inode)
1815 struct bch_fs *c = trans->c;
1816 struct inode_walker_entry *i;
1819 ret = check_key_has_snapshot(trans, iter, k);
1823 i = walk_inode(trans, inode, k.k->p);
1824 ret = PTR_ERR_OR_ZERO(i);
1828 if (inode->first_this_inode)
1829 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1830 inode->first_this_inode = false;
1832 if (fsck_err_on(!i, c,
1833 "xattr for missing inode %llu",
1835 return bch2_btree_delete_at(trans, iter, 0);
1840 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1842 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1848 * Walk xattrs: verify that they all have a corresponding inode
1851 static int check_xattrs(struct bch_fs *c)
1853 struct inode_walker inode = inode_walker_init();
1854 struct bch_hash_info hash_info;
1855 struct btree_trans trans;
1856 struct btree_iter iter;
1860 bch_verbose(c, "checking xattrs");
1862 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1864 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1865 POS(BCACHEFS_ROOT_INO, 0),
1866 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1869 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1870 check_xattr(&trans, &iter, k, &hash_info, &inode));
1872 bch2_trans_exit(&trans);
1879 static int check_root_trans(struct btree_trans *trans)
1881 struct bch_fs *c = trans->c;
1882 struct bch_inode_unpacked root_inode;
1887 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1888 if (ret && !bch2_err_matches(ret, ENOENT))
1891 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1892 struct bkey_i_subvolume root_subvol;
1895 inum = BCACHEFS_ROOT_INO;
1897 bkey_subvolume_init(&root_subvol.k_i);
1898 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1899 root_subvol.v.flags = 0;
1900 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1901 root_subvol.v.inode = cpu_to_le64(inum);
1902 ret = commit_do(trans, NULL, NULL,
1903 BTREE_INSERT_NOFAIL|
1904 BTREE_INSERT_LAZY_RW,
1905 __bch2_btree_insert(trans, BTREE_ID_subvolumes,
1906 &root_subvol.k_i, 0));
1908 bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1914 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1915 if (ret && !bch2_err_matches(ret, ENOENT))
1918 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1919 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1920 "root inode not a directory")) {
1921 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1923 root_inode.bi_inum = inum;
1925 ret = __write_inode(trans, &root_inode, snapshot);
1927 bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1934 /* Get root directory, create if it doesn't exist: */
1936 static int check_root(struct bch_fs *c)
1940 bch_verbose(c, "checking root directory");
1942 ret = bch2_trans_do(c, NULL, NULL,
1943 BTREE_INSERT_NOFAIL|
1944 BTREE_INSERT_LAZY_RW,
1945 check_root_trans(&trans));
1952 struct pathbuf_entry {
1957 typedef DARRAY(struct pathbuf_entry) pathbuf;
1959 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1961 struct pathbuf_entry *i;
1963 darray_for_each(*p, i)
1964 if (i->inum == inum &&
1965 i->snapshot == snapshot)
1971 static int path_down(struct bch_fs *c, pathbuf *p,
1972 u64 inum, u32 snapshot)
1974 int ret = darray_push(p, ((struct pathbuf_entry) {
1976 .snapshot = snapshot,
1980 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1986 * Check that a given inode is reachable from the root:
1988 * XXX: we should also be verifying that inodes are in the right subvolumes
1990 static int check_path(struct btree_trans *trans,
1992 struct bch_inode_unpacked *inode,
1995 struct bch_fs *c = trans->c;
1998 snapshot = bch2_snapshot_equiv(c, snapshot);
2001 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
2002 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
2003 struct btree_iter dirent_iter;
2004 struct bkey_s_c_dirent d;
2005 u32 parent_snapshot = snapshot;
2007 if (inode->bi_subvol) {
2010 ret = subvol_lookup(trans, inode->bi_parent_subvol,
2011 &parent_snapshot, &inum);
2016 ret = lockrestart_do(trans,
2017 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
2018 SPOS(inode->bi_dir, inode->bi_dir_offset,
2019 parent_snapshot))).k));
2020 if (ret && !bch2_err_matches(ret, ENOENT))
2023 if (!ret && !dirent_points_to_inode(d, inode)) {
2024 bch2_trans_iter_exit(trans, &dirent_iter);
2025 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
2028 if (bch2_err_matches(ret, ENOENT)) {
2029 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2030 inode->bi_inum, snapshot,
2031 bch2_d_type_str(inode_d_type(inode)),
2034 inode->bi_dir_offset))
2035 ret = reattach_inode(trans, inode, snapshot);
2039 bch2_trans_iter_exit(trans, &dirent_iter);
2041 if (!S_ISDIR(inode->bi_mode))
2044 ret = path_down(c, p, inode->bi_inum, snapshot);
2046 bch_err(c, "memory allocation failure");
2050 snapshot = parent_snapshot;
2052 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2054 /* Should have been caught in dirents pass */
2055 bch_err(c, "error looking up parent directory: %i", ret);
2059 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2060 struct pathbuf_entry *i;
2062 /* XXX print path */
2063 bch_err(c, "directory structure loop");
2065 darray_for_each(*p, i)
2066 pr_err("%llu:%u", i->inum, i->snapshot);
2067 pr_err("%llu:%u", inode->bi_inum, snapshot);
2069 if (!fsck_err(c, "directory structure loop"))
2072 ret = commit_do(trans, NULL, NULL,
2073 BTREE_INSERT_NOFAIL|
2074 BTREE_INSERT_LAZY_RW,
2075 remove_backpointer(trans, inode));
2077 bch_err(c, "error removing dirent: %i", ret);
2081 ret = reattach_inode(trans, inode, snapshot);
2091 * Check for unreachable inodes, as well as loops in the directory structure:
2092 * After check_dirents(), if an inode backpointer doesn't exist that means it's
2096 static int check_directory_structure(struct bch_fs *c)
2098 struct btree_trans trans;
2099 struct btree_iter iter;
2101 struct bch_inode_unpacked u;
2102 pathbuf path = { 0, };
2105 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2107 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2109 BTREE_ITER_PREFETCH|
2110 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2111 if (!bkey_is_inode(k.k))
2114 ret = bch2_inode_unpack(k, &u);
2116 /* Should have been caught earlier in fsck: */
2117 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2121 if (u.bi_flags & BCH_INODE_UNLINKED)
2124 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2128 bch2_trans_iter_exit(&trans, &iter);
2129 bch2_trans_exit(&trans);
2137 struct nlink_table {
2148 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2149 u64 inum, u32 snapshot)
2151 if (t->nr == t->size) {
2152 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2153 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2156 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2158 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2162 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2170 t->d[t->nr++] = (struct nlink) {
2172 .snapshot = snapshot,
2178 static int nlink_cmp(const void *_l, const void *_r)
2180 const struct nlink *l = _l;
2181 const struct nlink *r = _r;
2183 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2186 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2187 struct nlink_table *links,
2188 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2190 struct nlink *link, key = {
2191 .inum = inum, .snapshot = U32_MAX,
2194 if (inum < range_start || inum >= range_end)
2197 link = __inline_bsearch(&key, links->d, links->nr,
2198 sizeof(links->d[0]), nlink_cmp);
2202 while (link > links->d && link[0].inum == link[-1].inum)
2205 for (; link < links->d + links->nr && link->inum == inum; link++)
2206 if (ref_visible(c, s, snapshot, link->snapshot)) {
2208 if (link->snapshot >= snapshot)
2214 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2215 struct nlink_table *t,
2216 u64 start, u64 *end)
2218 struct btree_trans trans;
2219 struct btree_iter iter;
2221 struct bch_inode_unpacked u;
2224 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2226 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2229 BTREE_ITER_PREFETCH|
2230 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2231 if (!bkey_is_inode(k.k))
2234 /* Should never fail, checked by bch2_inode_invalid: */
2235 BUG_ON(bch2_inode_unpack(k, &u));
2238 * Backpointer and directory structure checks are sufficient for
2239 * directories, since they can't have hardlinks:
2241 if (S_ISDIR(u.bi_mode))
2247 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2249 *end = k.k->p.offset;
2255 bch2_trans_iter_exit(&trans, &iter);
2256 bch2_trans_exit(&trans);
2259 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2265 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2266 u64 range_start, u64 range_end)
2268 struct btree_trans trans;
2269 struct snapshots_seen s;
2270 struct btree_iter iter;
2272 struct bkey_s_c_dirent d;
2275 snapshots_seen_init(&s);
2277 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2279 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2281 BTREE_ITER_PREFETCH|
2282 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2283 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2287 switch (k.k->type) {
2288 case KEY_TYPE_dirent:
2289 d = bkey_s_c_to_dirent(k);
2291 if (d.v->d_type != DT_DIR &&
2292 d.v->d_type != DT_SUBVOL)
2293 inc_link(c, &s, links, range_start, range_end,
2294 le64_to_cpu(d.v->d_inum),
2295 bch2_snapshot_equiv(c, d.k->p.snapshot));
2299 bch2_trans_iter_exit(&trans, &iter);
2302 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2304 bch2_trans_exit(&trans);
2305 snapshots_seen_exit(&s);
2309 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2311 struct nlink_table *links,
2312 size_t *idx, u64 range_end)
2314 struct bch_fs *c = trans->c;
2315 struct bch_inode_unpacked u;
2316 struct nlink *link = &links->d[*idx];
2319 if (k.k->p.offset >= range_end)
2322 if (!bkey_is_inode(k.k))
2325 BUG_ON(bch2_inode_unpack(k, &u));
2327 if (S_ISDIR(u.bi_mode))
2333 while ((cmp_int(link->inum, k.k->p.offset) ?:
2334 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2335 BUG_ON(*idx == links->nr);
2336 link = &links->d[++*idx];
2339 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2340 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2341 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2342 bch2_inode_nlink_get(&u), link->count)) {
2343 bch2_inode_nlink_set(&u, link->count);
2344 ret = __write_inode(trans, &u, k.k->p.snapshot);
2351 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2352 struct nlink_table *links,
2353 u64 range_start, u64 range_end)
2355 struct btree_trans trans;
2356 struct btree_iter iter;
2361 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2363 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2364 POS(0, range_start),
2365 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2366 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2367 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2369 bch2_trans_exit(&trans);
2372 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2380 static int check_nlinks(struct bch_fs *c)
2382 struct nlink_table links = { 0 };
2383 u64 this_iter_range_start, next_iter_range_start = 0;
2386 bch_verbose(c, "checking inode nlinks");
2389 this_iter_range_start = next_iter_range_start;
2390 next_iter_range_start = U64_MAX;
2392 ret = check_nlinks_find_hardlinks(c, &links,
2393 this_iter_range_start,
2394 &next_iter_range_start);
2396 ret = check_nlinks_walk_dirents(c, &links,
2397 this_iter_range_start,
2398 next_iter_range_start);
2402 ret = check_nlinks_update_hardlinks(c, &links,
2403 this_iter_range_start,
2404 next_iter_range_start);
2409 } while (next_iter_range_start != U64_MAX);
2418 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2421 struct bkey_s_c_reflink_p p;
2422 struct bkey_i_reflink_p *u;
2425 if (k.k->type != KEY_TYPE_reflink_p)
2428 p = bkey_s_c_to_reflink_p(k);
2430 if (!p.v->front_pad && !p.v->back_pad)
2433 u = bch2_trans_kmalloc(trans, sizeof(*u));
2434 ret = PTR_ERR_OR_ZERO(u);
2438 bkey_reassemble(&u->k_i, k);
2442 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2446 static int fix_reflink_p(struct bch_fs *c)
2448 struct btree_iter iter;
2452 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2455 bch_verbose(c, "fixing reflink_p keys");
2457 ret = bch2_trans_run(c,
2458 for_each_btree_key_commit(&trans, iter,
2459 BTREE_ID_extents, POS_MIN,
2460 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2461 BTREE_ITER_ALL_SNAPSHOTS, k,
2462 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2463 fix_reflink_p_key(&trans, &iter, k)));
2471 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2472 * Doesn't fix them yet, mainly because they haven't yet been observed:
2474 int bch2_fsck_full(struct bch_fs *c)
2478 ret = bch2_fs_check_snapshot_trees(c);
2479 bch2_fs_check_snapshots(c) ?:
2480 bch2_fs_check_subvols(c) ?:
2481 bch2_delete_dead_snapshots(c) ?:
2482 check_inodes(c, true) ?:
2487 check_directory_structure(c) ?:
2491 if (bch2_err_matches(ret, BCH_ERR_need_snapshot_cleanup)) {
2492 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
2499 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2501 return bch2_fs_check_snapshots(c) ?:
2502 bch2_fs_check_subvols(c) ?:
2503 bch2_delete_dead_snapshots(c) ?:
2504 check_inodes(c, false);