1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
13 #include "subvolume.h"
17 #include <linux/bsearch.h>
18 #include <linux/dcache.h> /* struct qstr */
20 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
23 * XXX: this is handling transaction restarts without returning
24 * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
26 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
29 struct btree_iter iter;
34 for_each_btree_key_upto(trans, iter, BTREE_ID_extents,
35 SPOS(inum, 0, snapshot),
38 if (bkey_extent_is_allocation(k.k))
41 bch2_trans_iter_exit(trans, &iter);
43 return ret ?: sectors;
46 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
49 struct btree_iter iter;
51 struct bkey_s_c_dirent d;
55 for_each_btree_key_upto(trans, iter, BTREE_ID_dirents,
56 SPOS(inum, 0, snapshot),
59 if (k.k->type != KEY_TYPE_dirent)
62 d = bkey_s_c_to_dirent(k);
63 if (d.v->d_type == DT_DIR)
66 bch2_trans_iter_exit(trans, &iter);
68 return ret ?: subdirs;
71 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
74 struct btree_iter iter;
78 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
80 k = bch2_btree_iter_peek_slot(&iter);
85 if (k.k->type != KEY_TYPE_snapshot) {
86 bch_err(trans->c, "snapshot %u not fonud", snapshot);
91 *subvol = le32_to_cpu(bkey_s_c_to_snapshot(k).v->subvol);
93 bch2_trans_iter_exit(trans, &iter);
98 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
99 u32 *snapshot, u64 *inum)
101 struct bch_subvolume s;
104 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
106 *snapshot = le32_to_cpu(s.snapshot);
107 *inum = le64_to_cpu(s.inode);
111 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
112 u32 *snapshot, u64 *inum)
114 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
117 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
118 struct bch_inode_unpacked *inode)
120 struct btree_iter iter;
124 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
126 BTREE_ITER_ALL_SNAPSHOTS);
127 k = bch2_btree_iter_peek(&iter);
132 if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
137 ret = bch2_inode_unpack(k, inode);
139 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
140 bch_err(trans->c, "error fetching inode %llu: %s",
141 inode_nr, bch2_err_str(ret));
142 bch2_trans_iter_exit(trans, &iter);
146 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
147 struct bch_inode_unpacked *inode,
150 struct btree_iter iter;
154 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
155 SPOS(0, inode_nr, *snapshot), 0);
156 k = bch2_btree_iter_peek_slot(&iter);
161 ret = bkey_is_inode(k.k)
162 ? bch2_inode_unpack(k, inode)
165 *snapshot = iter.pos.snapshot;
167 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
168 bch_err(trans->c, "error fetching inode %llu:%u: %s",
169 inode_nr, *snapshot, bch2_err_str(ret));
170 bch2_trans_iter_exit(trans, &iter);
174 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
175 struct bch_inode_unpacked *inode,
178 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
181 static int __lookup_dirent(struct btree_trans *trans,
182 struct bch_hash_info hash_info,
183 subvol_inum dir, struct qstr *name,
184 u64 *target, unsigned *type)
186 struct btree_iter iter;
187 struct bkey_s_c_dirent d;
190 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
191 &hash_info, dir, name, 0);
195 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
196 *target = le64_to_cpu(d.v->d_inum);
198 bch2_trans_iter_exit(trans, &iter);
202 static int __write_inode(struct btree_trans *trans,
203 struct bch_inode_unpacked *inode,
206 struct btree_iter iter;
209 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
210 SPOS(0, inode->bi_inum, snapshot),
213 ret = bch2_btree_iter_traverse(&iter) ?:
214 bch2_inode_write(trans, &iter, inode);
215 bch2_trans_iter_exit(trans, &iter);
219 static int write_inode(struct btree_trans *trans,
220 struct bch_inode_unpacked *inode,
223 int ret = commit_do(trans, NULL, NULL,
225 BTREE_INSERT_LAZY_RW,
226 __write_inode(trans, inode, snapshot));
228 bch_err(trans->c, "error in fsck: error updating inode: %s",
233 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
235 struct bch_fs *c = trans->c;
236 struct btree_iter iter = { NULL };
237 struct bkey_i_inode_generation delete;
238 struct bch_inode_unpacked inode_u;
243 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
244 SPOS(inum, 0, snapshot),
245 SPOS(inum, U64_MAX, snapshot),
247 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
248 SPOS(inum, 0, snapshot),
249 SPOS(inum, U64_MAX, snapshot),
251 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
252 SPOS(inum, 0, snapshot),
253 SPOS(inum, U64_MAX, snapshot),
255 } while (ret == -BCH_ERR_transaction_restart_nested);
259 bch2_trans_begin(trans);
261 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
262 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
263 k = bch2_btree_iter_peek_slot(&iter);
269 if (!bkey_is_inode(k.k)) {
270 bch2_fs_inconsistent(c,
271 "inode %llu:%u not found when deleting",
277 bch2_inode_unpack(k, &inode_u);
279 /* Subvolume root? */
280 if (inode_u.bi_subvol)
281 bch_warn(c, "deleting inode %llu marked as unlinked, but also a subvolume root!?", inode_u.bi_inum);
283 bkey_inode_generation_init(&delete.k_i);
284 delete.k.p = iter.pos;
285 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
287 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
288 bch2_trans_commit(trans, NULL, NULL,
289 BTREE_INSERT_NOFAIL);
291 bch2_trans_iter_exit(trans, &iter);
292 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
295 return ret ?: -BCH_ERR_transaction_restart_nested;
298 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
300 struct bch_fs *c = trans->c;
301 struct btree_iter iter;
302 struct bch_inode_unpacked dir_inode;
303 struct bch_hash_info dir_hash_info;
306 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
310 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
312 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
314 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
315 &dir_hash_info, &iter,
316 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
317 bch2_trans_iter_exit(trans, &iter);
319 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
320 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
324 /* Get lost+found, create if it doesn't exist: */
325 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
326 struct bch_inode_unpacked *lostfound)
328 struct bch_fs *c = trans->c;
329 struct bch_inode_unpacked root;
330 struct bch_hash_info root_hash_info;
331 struct qstr lostfound_str = QSTR("lost+found");
332 subvol_inum root_inum = { .subvol = subvol };
338 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
342 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
346 root_hash_info = bch2_hash_info_init(c, &root);
348 ret = __lookup_dirent(trans, root_hash_info, root_inum,
349 &lostfound_str, &inum, &d_type);
350 if (ret == -ENOENT) {
351 bch_notice(c, "creating lost+found");
352 goto create_lostfound;
355 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
356 bch_err(c, "error looking up lost+found: %s", bch2_err_str(ret));
360 if (d_type != DT_DIR) {
361 bch_err(c, "error looking up lost+found: not a directory");
366 * The check_dirents pass has already run, dangling dirents
367 * shouldn't exist here:
369 return __lookup_inode(trans, inum, lostfound, &snapshot);
372 bch2_inode_init_early(c, lostfound);
374 ret = bch2_create_trans(trans, root_inum, &root,
375 lostfound, &lostfound_str,
376 0, 0, S_IFDIR|0700, 0, NULL, NULL,
377 (subvol_inum) { }, 0);
378 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
379 bch_err(c, "error creating lost+found: %s", bch2_err_str(ret));
383 static int __reattach_inode(struct btree_trans *trans,
384 struct bch_inode_unpacked *inode,
387 struct bch_hash_info dir_hash;
388 struct bch_inode_unpacked lostfound;
395 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
399 ret = lookup_lostfound(trans, subvol, &lostfound);
403 if (S_ISDIR(inode->bi_mode)) {
404 lostfound.bi_nlink++;
406 ret = __write_inode(trans, &lostfound, U32_MAX);
411 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
413 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
414 name = (struct qstr) QSTR(name_buf);
416 ret = bch2_dirent_create(trans,
419 .inum = lostfound.bi_inum,
423 &name, inode->bi_inum, &dir_offset,
424 BCH_HASH_SET_MUST_CREATE);
428 inode->bi_dir = lostfound.bi_inum;
429 inode->bi_dir_offset = dir_offset;
431 return __write_inode(trans, inode, inode_snapshot);
434 static int reattach_inode(struct btree_trans *trans,
435 struct bch_inode_unpacked *inode,
438 int ret = commit_do(trans, NULL, NULL,
439 BTREE_INSERT_LAZY_RW|
441 __reattach_inode(trans, inode, inode_snapshot));
443 bch_err(trans->c, "error reattaching inode %llu: %s",
444 inode->bi_inum, bch2_err_str(ret));
451 static int remove_backpointer(struct btree_trans *trans,
452 struct bch_inode_unpacked *inode)
454 struct btree_iter iter;
458 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
459 POS(inode->bi_dir, inode->bi_dir_offset), 0);
460 k = bch2_btree_iter_peek_slot(&iter);
464 if (k.k->type != KEY_TYPE_dirent) {
469 ret = __remove_dirent(trans, k.k->p);
471 bch2_trans_iter_exit(trans, &iter);
475 struct snapshots_seen_entry {
480 struct snapshots_seen {
482 DARRAY(struct snapshots_seen_entry) ids;
485 static inline void snapshots_seen_exit(struct snapshots_seen *s)
487 darray_exit(&s->ids);
490 static inline void snapshots_seen_init(struct snapshots_seen *s)
492 memset(s, 0, sizeof(*s));
495 static int snapshots_seen_add(struct bch_fs *c, struct snapshots_seen *s, u32 id)
497 struct snapshots_seen_entry *i, n = { id, id };
500 darray_for_each(s->ids, i) {
501 if (n.equiv < i->equiv)
504 if (i->equiv == n.equiv) {
505 bch_err(c, "%s(): adding duplicate snapshot", __func__);
510 ret = darray_insert_item(&s->ids, i - s->ids.data, n);
512 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
517 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
518 enum btree_id btree_id, struct bpos pos)
520 struct snapshots_seen_entry *i, n = {
522 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
526 if (!bkey_eq(s->pos, pos))
529 pos.snapshot = n.equiv;
532 darray_for_each(s->ids, i)
533 if (i->equiv == n.equiv) {
534 if (fsck_err_on(i->id != n.id, c,
535 "snapshot deletion did not run correctly:\n"
536 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
537 bch2_btree_ids[btree_id],
538 pos.inode, pos.offset,
539 i->id, n.id, n.equiv))
540 return -BCH_ERR_need_snapshot_cleanup;
545 ret = darray_push(&s->ids, n);
547 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
554 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
555 * and @ancestor hasn't been overwritten in @seen
557 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
559 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
560 u32 id, u32 ancestor)
563 u32 top = seen->ids.nr ? seen->ids.data[seen->ids.nr - 1].equiv : 0;
565 BUG_ON(id > ancestor);
566 BUG_ON(!bch2_snapshot_is_equiv(c, id));
567 BUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
569 /* @ancestor should be the snapshot most recently added to @seen */
570 BUG_ON(ancestor != seen->pos.snapshot);
571 BUG_ON(ancestor != top);
576 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
579 for (i = seen->ids.nr - 2;
580 i >= 0 && seen->ids.data[i].equiv >= id;
582 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv) &&
583 bch2_snapshot_is_ancestor(c, seen->ids.data[i].equiv, ancestor))
590 * ref_visible - given a key with snapshot id @src that points to a key with
591 * snapshot id @dst, test whether there is some snapshot in which @dst is
594 * This assumes we're visiting @src keys in natural key order.
596 * @s - list of snapshot IDs already seen at @src
597 * @src - snapshot ID of src key
598 * @dst - snapshot ID of dst key
600 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
604 ? key_visible_in_snapshot(c, s, dst, src)
605 : bch2_snapshot_is_ancestor(c, src, dst);
608 static int ref_visible2(struct bch_fs *c,
609 u32 src, struct snapshots_seen *src_seen,
610 u32 dst, struct snapshots_seen *dst_seen)
614 swap(dst_seen, src_seen);
616 return key_visible_in_snapshot(c, src_seen, dst, src);
619 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
620 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
621 (_i)->snapshot <= (_snapshot); _i++) \
622 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
624 struct inode_walker_entry {
625 struct bch_inode_unpacked inode;
630 struct inode_walker {
631 bool first_this_inode;
634 DARRAY(struct inode_walker_entry) inodes;
637 static void inode_walker_exit(struct inode_walker *w)
639 darray_exit(&w->inodes);
642 static struct inode_walker inode_walker_init(void)
644 return (struct inode_walker) { 0, };
647 static int add_inode(struct bch_fs *c, struct inode_walker *w,
648 struct bkey_s_c inode)
650 struct bch_inode_unpacked u;
652 BUG_ON(bch2_inode_unpack(inode, &u));
654 return darray_push(&w->inodes, ((struct inode_walker_entry) {
656 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
660 static int __walk_inode(struct btree_trans *trans,
661 struct inode_walker *w, struct bpos pos)
663 struct bch_fs *c = trans->c;
664 struct btree_iter iter;
666 u32 restart_count = trans->restart_count;
670 pos.snapshot = bch2_snapshot_equiv(c, pos.snapshot);
672 if (pos.inode == w->cur_inum) {
673 w->first_this_inode = false;
674 goto lookup_snapshot;
679 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
680 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
681 if (k.k->p.offset != pos.inode)
684 if (bkey_is_inode(k.k))
687 bch2_trans_iter_exit(trans, &iter);
692 w->cur_inum = pos.inode;
693 w->first_this_inode = true;
695 if (trans_was_restarted(trans, restart_count))
696 return -BCH_ERR_transaction_restart_nested;
699 for (i = 0; i < w->inodes.nr; i++)
700 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->inodes.data[i].snapshot))
704 BUG_ON(pos.snapshot > w->inodes.data[i].snapshot);
706 if (pos.snapshot != w->inodes.data[i].snapshot) {
707 struct inode_walker_entry e = w->inodes.data[i];
709 e.snapshot = pos.snapshot;
712 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
713 pos.inode, pos.snapshot, w->inodes.data[i].snapshot);
715 while (i && w->inodes.data[i - 1].snapshot > pos.snapshot)
718 ret = darray_insert_item(&w->inodes, i, e);
726 static int __get_visible_inodes(struct btree_trans *trans,
727 struct inode_walker *w,
728 struct snapshots_seen *s,
731 struct bch_fs *c = trans->c;
732 struct btree_iter iter;
738 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
739 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
740 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
742 if (k.k->p.offset != inum)
745 if (!ref_visible(c, s, s->pos.snapshot, equiv))
748 if (bkey_is_inode(k.k))
751 if (equiv >= s->pos.snapshot)
754 bch2_trans_iter_exit(trans, &iter);
759 static int check_key_has_snapshot(struct btree_trans *trans,
760 struct btree_iter *iter,
763 struct bch_fs *c = trans->c;
764 struct printbuf buf = PRINTBUF;
767 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
768 "key in missing snapshot: %s",
769 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
770 ret = bch2_btree_delete_at(trans, iter,
771 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
777 static int hash_redo_key(struct btree_trans *trans,
778 const struct bch_hash_desc desc,
779 struct bch_hash_info *hash_info,
780 struct btree_iter *k_iter, struct bkey_s_c k)
782 struct bkey_i *delete;
785 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
787 return PTR_ERR(delete);
789 tmp = bch2_bkey_make_mut(trans, k);
793 bkey_init(&delete->k);
794 delete->k.p = k_iter->pos;
795 return bch2_btree_iter_traverse(k_iter) ?:
796 bch2_trans_update(trans, k_iter, delete, 0) ?:
797 bch2_hash_set_snapshot(trans, desc, hash_info,
798 (subvol_inum) { 0, k.k->p.inode },
799 k.k->p.snapshot, tmp,
800 BCH_HASH_SET_MUST_CREATE,
801 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
802 bch2_trans_commit(trans, NULL, NULL,
804 BTREE_INSERT_LAZY_RW);
807 static int hash_check_key(struct btree_trans *trans,
808 const struct bch_hash_desc desc,
809 struct bch_hash_info *hash_info,
810 struct btree_iter *k_iter, struct bkey_s_c hash_k)
812 struct bch_fs *c = trans->c;
813 struct btree_iter iter = { NULL };
814 struct printbuf buf = PRINTBUF;
819 if (hash_k.k->type != desc.key_type)
822 hash = desc.hash_bkey(hash_info, hash_k);
824 if (likely(hash == hash_k.k->p.offset))
827 if (hash_k.k->p.offset < hash)
830 for_each_btree_key_norestart(trans, iter, desc.btree_id,
831 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
832 BTREE_ITER_SLOTS, k, ret) {
833 if (bkey_eq(k.k->p, hash_k.k->p))
836 if (fsck_err_on(k.k->type == desc.key_type &&
837 !desc.cmp_bkey(k, hash_k), c,
838 "duplicate hash table keys:\n%s",
839 (printbuf_reset(&buf),
840 bch2_bkey_val_to_text(&buf, c, hash_k),
842 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
846 if (bkey_deleted(k.k)) {
847 bch2_trans_iter_exit(trans, &iter);
852 bch2_trans_iter_exit(trans, &iter);
856 if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
857 bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
858 (printbuf_reset(&buf),
859 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
860 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
862 bch_err(c, "hash_redo_key err %s", bch2_err_str(ret));
865 ret = -BCH_ERR_transaction_restart_nested;
871 static int check_inode(struct btree_trans *trans,
872 struct btree_iter *iter,
874 struct bch_inode_unpacked *prev,
875 struct snapshots_seen *s,
878 struct bch_fs *c = trans->c;
879 struct bch_inode_unpacked u;
880 bool do_update = false;
883 ret = check_key_has_snapshot(trans, iter, k);
889 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
894 * if snapshot id isn't a leaf node, skip it - deletion in
895 * particular is not atomic, so on the internal snapshot nodes
896 * we can see inodes marked for deletion after a clean shutdown
898 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
901 if (!bkey_is_inode(k.k))
904 BUG_ON(bch2_inode_unpack(k, &u));
907 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
908 BCH_INODE_I_SECTORS_DIRTY|
909 BCH_INODE_UNLINKED)))
912 if (prev->bi_inum != u.bi_inum)
915 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
916 inode_d_type(prev) != inode_d_type(&u), c,
917 "inodes in different snapshots don't match")) {
918 bch_err(c, "repair not implemented yet");
922 if (u.bi_flags & BCH_INODE_UNLINKED &&
924 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
926 bch2_trans_unlock(trans);
929 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
930 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
931 bch_err(c, "error in fsck: error while deleting inode: %s",
936 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
938 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
940 bch_verbose(c, "truncating inode %llu", u.bi_inum);
942 bch2_trans_unlock(trans);
946 * XXX: need to truncate partial blocks too here - or ideally
947 * just switch units to bytes and that issue goes away
949 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
950 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
952 POS(u.bi_inum, U64_MAX),
955 bch_err(c, "error in fsck: error truncating inode: %s",
961 * We truncated without our normal sector accounting hook, just
962 * make sure we recalculate it:
964 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
966 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
970 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
972 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
976 bch_verbose(c, "recounting sectors for inode %llu",
979 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
981 bch_err(c, "error in fsck: error recounting inode sectors: %s",
982 bch2_err_str(sectors));
986 u.bi_sectors = sectors;
987 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
991 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
994 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
999 ret = __write_inode(trans, &u, iter->pos.snapshot);
1001 bch_err(c, "error in fsck: error updating inode: %s",
1007 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1012 static int check_inodes(struct bch_fs *c, bool full)
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);
1033 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
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,
1085 bch2_trans_iter_init(trans, iter, BTREE_ID_dirents, pos, 0);
1086 k = bch2_btree_iter_peek_slot(iter);
1088 if (!ret && k.k->type != KEY_TYPE_dirent)
1091 bch2_trans_iter_exit(trans, iter);
1092 return (struct bkey_s_c_dirent) { .k = ERR_PTR(ret) };
1095 return bkey_s_c_to_dirent(k);
1098 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1099 struct bkey_s_c_dirent d)
1101 return inode->bi_dir == d.k->p.inode &&
1102 inode->bi_dir_offset == d.k->p.offset;
1105 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1106 struct bch_inode_unpacked *inode)
1108 return d.v->d_type == DT_SUBVOL
1109 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1110 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1113 static int inode_backpointer_exists(struct btree_trans *trans,
1114 struct bch_inode_unpacked *inode,
1117 struct btree_iter iter;
1118 struct bkey_s_c_dirent d;
1121 d = dirent_get_by_pos(trans, &iter,
1122 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1123 ret = bkey_err(d.s_c);
1125 return ret == -ENOENT ? 0 : ret;
1127 ret = dirent_points_to_inode(d, inode);
1128 bch2_trans_iter_exit(trans, &iter);
1132 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1134 struct bch_fs *c = trans->c;
1135 struct inode_walker_entry *i;
1136 u32 restart_count = trans->restart_count;
1140 darray_for_each(w->inodes, i) {
1141 if (i->inode.bi_sectors == i->count)
1144 count2 = bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot);
1146 if (i->count != count2) {
1147 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1150 if (i->inode.bi_sectors == i->count)
1154 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1155 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1156 w->cur_inum, i->snapshot,
1157 i->inode.bi_sectors, i->count)) {
1158 i->inode.bi_sectors = i->count;
1159 ret = write_inode(trans, &i->inode, i->snapshot);
1166 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1167 if (!ret && trans_was_restarted(trans, restart_count))
1168 ret = -BCH_ERR_transaction_restart_nested;
1175 struct snapshots_seen seen;
1178 typedef DARRAY(struct extent_end) extent_ends;
1180 static int check_overlapping_extents(struct btree_trans *trans,
1181 struct snapshots_seen *seen,
1182 extent_ends *extent_ends,
1184 struct btree_iter *iter)
1186 struct bch_fs *c = trans->c;
1187 struct extent_end *i;
1188 struct printbuf buf = PRINTBUF;
1191 darray_for_each(*extent_ends, i) {
1192 /* duplicate, due to transaction restart: */
1193 if (i->offset == k.k->p.offset &&
1194 i->snapshot == k.k->p.snapshot)
1197 if (!ref_visible2(c,
1198 k.k->p.snapshot, seen,
1199 i->snapshot, &i->seen))
1202 if (fsck_err_on(i->offset > bkey_start_offset(k.k), c,
1203 "overlapping extents: extent in snapshot %u ends at %llu overlaps with\n%s",
1206 (printbuf_reset(&buf),
1207 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1208 struct bkey_i *update = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1209 if ((ret = PTR_ERR_OR_ZERO(update)))
1211 bkey_reassemble(update, k);
1212 ret = bch2_trans_update_extent(trans, iter, update, 0);
1219 printbuf_exit(&buf);
1223 static int extent_ends_at(extent_ends *extent_ends,
1224 struct snapshots_seen *seen,
1227 struct extent_end *i, n = (struct extent_end) {
1228 .snapshot = k.k->p.snapshot,
1229 .offset = k.k->p.offset,
1233 n.seen.ids.data = kmemdup(seen->ids.data,
1234 sizeof(seen->ids.data[0]) * seen->ids.size,
1236 if (!n.seen.ids.data)
1239 darray_for_each(*extent_ends, i) {
1240 if (i->snapshot == k.k->p.snapshot) {
1241 snapshots_seen_exit(&i->seen);
1246 if (i->snapshot >= k.k->p.snapshot)
1250 return darray_insert_item(extent_ends, i - extent_ends->data, n);
1253 static void extent_ends_reset(extent_ends *extent_ends)
1255 struct extent_end *i;
1257 darray_for_each(*extent_ends, i)
1258 snapshots_seen_exit(&i->seen);
1260 extent_ends->nr = 0;
1263 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1265 struct inode_walker *inode,
1266 struct snapshots_seen *s,
1267 extent_ends *extent_ends)
1269 struct bch_fs *c = trans->c;
1270 struct inode_walker_entry *i;
1271 struct printbuf buf = PRINTBUF;
1275 ret = check_key_has_snapshot(trans, iter, k);
1277 ret = ret < 0 ? ret : 0;
1282 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1284 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1288 if (k.k->type == KEY_TYPE_whiteout)
1291 if (inode->cur_inum != k.k->p.inode) {
1292 ret = check_i_sectors(trans, inode);
1296 extent_ends_reset(extent_ends);
1299 BUG_ON(!iter->path->should_be_locked);
1301 ret = check_overlapping_extents(trans, s, extent_ends, k, iter);
1305 ret = extent_ends_at(extent_ends, s, k);
1309 ret = __walk_inode(trans, inode, equiv);
1313 if (fsck_err_on(ret == INT_MAX, c,
1314 "extent in missing inode:\n %s",
1315 (printbuf_reset(&buf),
1316 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1317 ret = bch2_btree_delete_at(trans, iter,
1318 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1322 if (ret == INT_MAX) {
1327 i = inode->inodes.data + ret;
1330 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1331 !S_ISLNK(i->inode.bi_mode), c,
1332 "extent in non regular inode mode %o:\n %s",
1334 (printbuf_reset(&buf),
1335 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1336 ret = bch2_btree_delete_at(trans, iter,
1337 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1342 * Check inodes in reverse order, from oldest snapshots to newest, so
1343 * that we emit the fewest number of whiteouts necessary:
1345 for (i = inode->inodes.data + inode->inodes.nr - 1;
1346 i >= inode->inodes.data;
1348 if (i->snapshot > equiv.snapshot ||
1349 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1352 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1353 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1354 !bkey_extent_is_reservation(k), c,
1355 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1356 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1357 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1358 struct btree_iter iter2;
1360 bch2_trans_copy_iter(&iter2, iter);
1361 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1362 ret = bch2_btree_iter_traverse(&iter2) ?:
1363 bch2_btree_delete_at(trans, &iter2,
1364 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1365 bch2_trans_iter_exit(trans, &iter2);
1369 if (i->snapshot != equiv.snapshot) {
1370 ret = snapshots_seen_add(c, s, i->snapshot);
1377 if (bkey_extent_is_allocation(k.k))
1378 for_each_visible_inode(c, s, inode, equiv.snapshot, i)
1379 i->count += k.k->size;
1381 bch2_bkey_buf_reassemble(&prev, c, k);
1387 printbuf_exit(&buf);
1389 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1390 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1395 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1396 * that i_size an i_sectors are consistent
1399 static int check_extents(struct bch_fs *c)
1401 struct inode_walker w = inode_walker_init();
1402 struct snapshots_seen s;
1403 struct btree_trans trans;
1404 struct btree_iter iter;
1406 extent_ends extent_ends = { 0 };
1409 snapshots_seen_init(&s);
1410 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1412 bch_verbose(c, "checking extents");
1414 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1415 POS(BCACHEFS_ROOT_INO, 0),
1416 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1418 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1419 check_extent(&trans, &iter, k, &w, &s, &extent_ends));
1421 extent_ends_reset(&extent_ends);
1422 darray_exit(&extent_ends);
1423 inode_walker_exit(&w);
1424 bch2_trans_exit(&trans);
1425 snapshots_seen_exit(&s);
1428 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1432 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1434 struct bch_fs *c = trans->c;
1435 struct inode_walker_entry *i;
1436 u32 restart_count = trans->restart_count;
1440 darray_for_each(w->inodes, i) {
1441 if (i->inode.bi_nlink == i->count)
1444 count2 = bch2_count_subdirs(trans, w->cur_inum, i->snapshot);
1448 if (i->count != count2) {
1449 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1452 if (i->inode.bi_nlink == i->count)
1456 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1457 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1458 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1459 i->inode.bi_nlink = i->count;
1460 ret = write_inode(trans, &i->inode, i->snapshot);
1467 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1468 if (!ret && trans_was_restarted(trans, restart_count))
1469 ret = -BCH_ERR_transaction_restart_nested;
1473 static int check_dirent_target(struct btree_trans *trans,
1474 struct btree_iter *iter,
1475 struct bkey_s_c_dirent d,
1476 struct bch_inode_unpacked *target,
1477 u32 target_snapshot)
1479 struct bch_fs *c = trans->c;
1480 struct bkey_i_dirent *n;
1481 bool backpointer_exists = true;
1482 struct printbuf buf = PRINTBUF;
1485 if (!target->bi_dir &&
1486 !target->bi_dir_offset) {
1487 target->bi_dir = d.k->p.inode;
1488 target->bi_dir_offset = d.k->p.offset;
1490 ret = __write_inode(trans, target, target_snapshot);
1495 if (!inode_points_to_dirent(target, d)) {
1496 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1500 backpointer_exists = ret;
1503 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1504 backpointer_exists, c,
1505 "directory %llu with multiple links",
1507 ret = __remove_dirent(trans, d.k->p);
1511 if (fsck_err_on(backpointer_exists &&
1512 !target->bi_nlink, c,
1513 "inode %llu type %s has multiple links but i_nlink 0",
1514 target->bi_inum, bch2_d_types[d.v->d_type])) {
1516 target->bi_flags &= ~BCH_INODE_UNLINKED;
1518 ret = __write_inode(trans, target, target_snapshot);
1523 if (fsck_err_on(!backpointer_exists, c,
1524 "inode %llu:%u has wrong backpointer:\n"
1526 "should be %llu:%llu",
1527 target->bi_inum, target_snapshot,
1529 target->bi_dir_offset,
1532 target->bi_dir = d.k->p.inode;
1533 target->bi_dir_offset = d.k->p.offset;
1535 ret = __write_inode(trans, target, target_snapshot);
1541 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1542 "incorrect d_type: got %s, should be %s:\n%s",
1543 bch2_d_type_str(d.v->d_type),
1544 bch2_d_type_str(inode_d_type(target)),
1545 (printbuf_reset(&buf),
1546 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1547 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1548 ret = PTR_ERR_OR_ZERO(n);
1552 bkey_reassemble(&n->k_i, d.s_c);
1553 n->v.d_type = inode_d_type(target);
1555 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1559 d = dirent_i_to_s_c(n);
1562 if (d.v->d_type == DT_SUBVOL &&
1563 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1564 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1565 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1566 le32_to_cpu(d.v->d_parent_subvol),
1567 target->bi_parent_subvol))) {
1568 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1569 ret = PTR_ERR_OR_ZERO(n);
1573 bkey_reassemble(&n->k_i, d.s_c);
1574 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1576 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1580 d = dirent_i_to_s_c(n);
1585 printbuf_exit(&buf);
1587 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1588 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1592 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1594 struct bch_hash_info *hash_info,
1595 struct inode_walker *dir,
1596 struct inode_walker *target,
1597 struct snapshots_seen *s)
1599 struct bch_fs *c = trans->c;
1600 struct bkey_s_c_dirent d;
1601 struct inode_walker_entry *i;
1602 struct printbuf buf = PRINTBUF;
1606 ret = check_key_has_snapshot(trans, iter, k);
1608 ret = ret < 0 ? ret : 0;
1613 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1615 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1619 if (k.k->type == KEY_TYPE_whiteout)
1622 if (dir->cur_inum != k.k->p.inode) {
1623 ret = check_subdir_count(trans, dir);
1628 BUG_ON(!iter->path->should_be_locked);
1630 ret = __walk_inode(trans, dir, equiv);
1634 if (fsck_err_on(ret == INT_MAX, c,
1635 "dirent in nonexisting directory:\n%s",
1636 (printbuf_reset(&buf),
1637 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1638 ret = bch2_btree_delete_at(trans, iter,
1639 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1643 if (ret == INT_MAX) {
1648 i = dir->inodes.data + ret;
1651 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1652 "dirent in non directory inode type %s:\n%s",
1653 bch2_d_type_str(inode_d_type(&i->inode)),
1654 (printbuf_reset(&buf),
1655 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1656 ret = bch2_btree_delete_at(trans, iter, 0);
1660 if (dir->first_this_inode)
1661 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1663 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1664 hash_info, iter, k);
1668 /* dirent has been deleted */
1673 if (k.k->type != KEY_TYPE_dirent)
1676 d = bkey_s_c_to_dirent(k);
1678 if (d.v->d_type == DT_SUBVOL) {
1679 struct bch_inode_unpacked subvol_root;
1680 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1681 u32 target_snapshot;
1684 ret = __subvol_lookup(trans, target_subvol,
1685 &target_snapshot, &target_inum);
1686 if (ret && ret != -ENOENT)
1689 if (fsck_err_on(ret, c,
1690 "dirent points to missing subvolume %llu",
1691 le64_to_cpu(d.v->d_child_subvol))) {
1692 ret = __remove_dirent(trans, d.k->p);
1696 ret = __lookup_inode(trans, target_inum,
1697 &subvol_root, &target_snapshot);
1698 if (ret && ret != -ENOENT)
1701 if (fsck_err_on(ret, c,
1702 "subvolume %u points to missing subvolume root %llu",
1705 bch_err(c, "repair not implemented yet");
1710 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1711 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1713 subvol_root.bi_subvol, target_subvol)) {
1714 subvol_root.bi_subvol = target_subvol;
1715 ret = __write_inode(trans, &subvol_root, target_snapshot);
1720 ret = check_dirent_target(trans, iter, d, &subvol_root,
1725 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1729 if (fsck_err_on(!target->inodes.nr, c,
1730 "dirent points to missing inode: (equiv %u)\n%s",
1732 (printbuf_reset(&buf),
1733 bch2_bkey_val_to_text(&buf, c, k),
1735 ret = __remove_dirent(trans, d.k->p);
1740 darray_for_each(target->inodes, i) {
1741 ret = check_dirent_target(trans, iter, d,
1742 &i->inode, i->snapshot);
1748 if (d.v->d_type == DT_DIR)
1749 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1755 printbuf_exit(&buf);
1757 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1758 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1763 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1767 static int check_dirents(struct bch_fs *c)
1769 struct inode_walker dir = inode_walker_init();
1770 struct inode_walker target = inode_walker_init();
1771 struct snapshots_seen s;
1772 struct bch_hash_info hash_info;
1773 struct btree_trans trans;
1774 struct btree_iter iter;
1778 bch_verbose(c, "checking dirents");
1780 snapshots_seen_init(&s);
1781 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1783 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1784 POS(BCACHEFS_ROOT_INO, 0),
1785 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1788 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1789 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1791 bch2_trans_exit(&trans);
1792 snapshots_seen_exit(&s);
1793 inode_walker_exit(&dir);
1794 inode_walker_exit(&target);
1797 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1801 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1803 struct bch_hash_info *hash_info,
1804 struct inode_walker *inode)
1806 struct bch_fs *c = trans->c;
1809 ret = check_key_has_snapshot(trans, iter, k);
1813 ret = __walk_inode(trans, inode, k.k->p);
1817 if (fsck_err_on(ret == INT_MAX, c,
1818 "xattr for missing inode %llu",
1820 return bch2_btree_delete_at(trans, iter, 0);
1827 if (inode->first_this_inode)
1828 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1830 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1832 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1833 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1838 * Walk xattrs: verify that they all have a corresponding inode
1841 static int check_xattrs(struct bch_fs *c)
1843 struct inode_walker inode = inode_walker_init();
1844 struct bch_hash_info hash_info;
1845 struct btree_trans trans;
1846 struct btree_iter iter;
1850 bch_verbose(c, "checking xattrs");
1852 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1854 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1855 POS(BCACHEFS_ROOT_INO, 0),
1856 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1859 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1860 check_xattr(&trans, &iter, k, &hash_info, &inode));
1862 bch2_trans_exit(&trans);
1865 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1869 static int check_root_trans(struct btree_trans *trans)
1871 struct bch_fs *c = trans->c;
1872 struct bch_inode_unpacked root_inode;
1877 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1878 if (ret && ret != -ENOENT)
1881 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1882 struct bkey_i_subvolume root_subvol;
1885 inum = BCACHEFS_ROOT_INO;
1887 bkey_subvolume_init(&root_subvol.k_i);
1888 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1889 root_subvol.v.flags = 0;
1890 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1891 root_subvol.v.inode = cpu_to_le64(inum);
1892 ret = commit_do(trans, NULL, NULL,
1893 BTREE_INSERT_NOFAIL|
1894 BTREE_INSERT_LAZY_RW,
1895 __bch2_btree_insert(trans, BTREE_ID_subvolumes,
1896 &root_subvol.k_i, 0));
1898 bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1904 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1905 if (ret && ret != -ENOENT)
1908 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1909 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1910 "root inode not a directory")) {
1911 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1913 root_inode.bi_inum = inum;
1915 ret = __write_inode(trans, &root_inode, snapshot);
1917 bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1924 /* Get root directory, create if it doesn't exist: */
1926 static int check_root(struct bch_fs *c)
1928 bch_verbose(c, "checking root directory");
1930 return bch2_trans_do(c, NULL, NULL,
1931 BTREE_INSERT_NOFAIL|
1932 BTREE_INSERT_LAZY_RW,
1933 check_root_trans(&trans));
1936 struct pathbuf_entry {
1941 typedef DARRAY(struct pathbuf_entry) pathbuf;
1943 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1945 struct pathbuf_entry *i;
1947 darray_for_each(*p, i)
1948 if (i->inum == inum &&
1949 i->snapshot == snapshot)
1955 static int path_down(struct bch_fs *c, pathbuf *p,
1956 u64 inum, u32 snapshot)
1958 int ret = darray_push(p, ((struct pathbuf_entry) {
1960 .snapshot = snapshot,
1964 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1970 * Check that a given inode is reachable from the root:
1972 * XXX: we should also be verifying that inodes are in the right subvolumes
1974 static int check_path(struct btree_trans *trans,
1976 struct bch_inode_unpacked *inode,
1979 struct bch_fs *c = trans->c;
1982 snapshot = bch2_snapshot_equiv(c, snapshot);
1985 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1986 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1987 struct btree_iter dirent_iter;
1988 struct bkey_s_c_dirent d;
1989 u32 parent_snapshot = snapshot;
1991 if (inode->bi_subvol) {
1994 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1995 &parent_snapshot, &inum);
2000 ret = lockrestart_do(trans,
2001 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
2002 SPOS(inode->bi_dir, inode->bi_dir_offset,
2003 parent_snapshot))).k));
2004 if (ret && ret != -ENOENT)
2007 if (!ret && !dirent_points_to_inode(d, inode)) {
2008 bch2_trans_iter_exit(trans, &dirent_iter);
2012 if (ret == -ENOENT) {
2013 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2014 inode->bi_inum, snapshot,
2015 bch2_d_type_str(inode_d_type(inode)),
2018 inode->bi_dir_offset))
2019 ret = reattach_inode(trans, inode, snapshot);
2023 bch2_trans_iter_exit(trans, &dirent_iter);
2025 if (!S_ISDIR(inode->bi_mode))
2028 ret = path_down(c, p, inode->bi_inum, snapshot);
2030 bch_err(c, "memory allocation failure");
2034 snapshot = parent_snapshot;
2036 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2038 /* Should have been caught in dirents pass */
2039 bch_err(c, "error looking up parent directory: %i", ret);
2043 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2044 struct pathbuf_entry *i;
2046 /* XXX print path */
2047 bch_err(c, "directory structure loop");
2049 darray_for_each(*p, i)
2050 pr_err("%llu:%u", i->inum, i->snapshot);
2051 pr_err("%llu:%u", inode->bi_inum, snapshot);
2053 if (!fsck_err(c, "directory structure loop"))
2056 ret = commit_do(trans, NULL, NULL,
2057 BTREE_INSERT_NOFAIL|
2058 BTREE_INSERT_LAZY_RW,
2059 remove_backpointer(trans, inode));
2061 bch_err(c, "error removing dirent: %i", ret);
2065 ret = reattach_inode(trans, inode, snapshot);
2070 bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
2075 * Check for unreachable inodes, as well as loops in the directory structure:
2076 * After check_dirents(), if an inode backpointer doesn't exist that means it's
2080 static int check_directory_structure(struct bch_fs *c)
2082 struct btree_trans trans;
2083 struct btree_iter iter;
2085 struct bch_inode_unpacked u;
2086 pathbuf path = { 0, };
2089 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2091 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2093 BTREE_ITER_PREFETCH|
2094 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2095 if (!bkey_is_inode(k.k))
2098 ret = bch2_inode_unpack(k, &u);
2100 /* Should have been caught earlier in fsck: */
2101 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2105 if (u.bi_flags & BCH_INODE_UNLINKED)
2108 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2112 bch2_trans_iter_exit(&trans, &iter);
2116 bch2_trans_exit(&trans);
2120 struct nlink_table {
2131 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2132 u64 inum, u32 snapshot)
2134 if (t->nr == t->size) {
2135 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2136 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2139 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2145 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2153 t->d[t->nr++] = (struct nlink) {
2155 .snapshot = snapshot,
2161 static int nlink_cmp(const void *_l, const void *_r)
2163 const struct nlink *l = _l;
2164 const struct nlink *r = _r;
2166 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2169 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2170 struct nlink_table *links,
2171 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2173 struct nlink *link, key = {
2174 .inum = inum, .snapshot = U32_MAX,
2177 if (inum < range_start || inum >= range_end)
2180 link = __inline_bsearch(&key, links->d, links->nr,
2181 sizeof(links->d[0]), nlink_cmp);
2185 while (link > links->d && link[0].inum == link[-1].inum)
2188 for (; link < links->d + links->nr && link->inum == inum; link++)
2189 if (ref_visible(c, s, snapshot, link->snapshot)) {
2191 if (link->snapshot >= snapshot)
2197 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2198 struct nlink_table *t,
2199 u64 start, u64 *end)
2201 struct btree_trans trans;
2202 struct btree_iter iter;
2204 struct bch_inode_unpacked u;
2207 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2209 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2212 BTREE_ITER_PREFETCH|
2213 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2214 if (!bkey_is_inode(k.k))
2217 /* Should never fail, checked by bch2_inode_invalid: */
2218 BUG_ON(bch2_inode_unpack(k, &u));
2221 * Backpointer and directory structure checks are sufficient for
2222 * directories, since they can't have hardlinks:
2224 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2230 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2232 *end = k.k->p.offset;
2238 bch2_trans_iter_exit(&trans, &iter);
2239 bch2_trans_exit(&trans);
2242 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2248 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2249 u64 range_start, u64 range_end)
2251 struct btree_trans trans;
2252 struct snapshots_seen s;
2253 struct btree_iter iter;
2255 struct bkey_s_c_dirent d;
2258 snapshots_seen_init(&s);
2260 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2262 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2264 BTREE_ITER_PREFETCH|
2265 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2266 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2270 switch (k.k->type) {
2271 case KEY_TYPE_dirent:
2272 d = bkey_s_c_to_dirent(k);
2274 if (d.v->d_type != DT_DIR &&
2275 d.v->d_type != DT_SUBVOL)
2276 inc_link(c, &s, links, range_start, range_end,
2277 le64_to_cpu(d.v->d_inum),
2278 bch2_snapshot_equiv(c, d.k->p.snapshot));
2282 bch2_trans_iter_exit(&trans, &iter);
2285 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2287 bch2_trans_exit(&trans);
2288 snapshots_seen_exit(&s);
2292 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2294 struct nlink_table *links,
2295 size_t *idx, u64 range_end)
2297 struct bch_fs *c = trans->c;
2298 struct bch_inode_unpacked u;
2299 struct nlink *link = &links->d[*idx];
2302 if (k.k->p.offset >= range_end)
2305 if (!bkey_is_inode(k.k))
2308 BUG_ON(bch2_inode_unpack(k, &u));
2310 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2316 while ((cmp_int(link->inum, k.k->p.offset) ?:
2317 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2318 BUG_ON(*idx == links->nr);
2319 link = &links->d[++*idx];
2322 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2323 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2324 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2325 bch2_inode_nlink_get(&u), link->count)) {
2326 bch2_inode_nlink_set(&u, link->count);
2327 ret = __write_inode(trans, &u, k.k->p.snapshot);
2334 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2335 struct nlink_table *links,
2336 u64 range_start, u64 range_end)
2338 struct btree_trans trans;
2339 struct btree_iter iter;
2344 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2346 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2347 POS(0, range_start),
2348 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2349 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2350 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2352 bch2_trans_exit(&trans);
2355 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2363 static int check_nlinks(struct bch_fs *c)
2365 struct nlink_table links = { 0 };
2366 u64 this_iter_range_start, next_iter_range_start = 0;
2369 bch_verbose(c, "checking inode nlinks");
2372 this_iter_range_start = next_iter_range_start;
2373 next_iter_range_start = U64_MAX;
2375 ret = check_nlinks_find_hardlinks(c, &links,
2376 this_iter_range_start,
2377 &next_iter_range_start);
2379 ret = check_nlinks_walk_dirents(c, &links,
2380 this_iter_range_start,
2381 next_iter_range_start);
2385 ret = check_nlinks_update_hardlinks(c, &links,
2386 this_iter_range_start,
2387 next_iter_range_start);
2392 } while (next_iter_range_start != U64_MAX);
2399 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2402 struct bkey_s_c_reflink_p p;
2403 struct bkey_i_reflink_p *u;
2406 if (k.k->type != KEY_TYPE_reflink_p)
2409 p = bkey_s_c_to_reflink_p(k);
2411 if (!p.v->front_pad && !p.v->back_pad)
2414 u = bch2_trans_kmalloc(trans, sizeof(*u));
2415 ret = PTR_ERR_OR_ZERO(u);
2419 bkey_reassemble(&u->k_i, k);
2423 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2427 static int fix_reflink_p(struct bch_fs *c)
2429 struct btree_trans trans;
2430 struct btree_iter iter;
2434 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2437 bch_verbose(c, "fixing reflink_p keys");
2439 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2441 ret = for_each_btree_key_commit(&trans, iter,
2442 BTREE_ID_extents, POS_MIN,
2443 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2444 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2445 fix_reflink_p_key(&trans, &iter, k));
2447 bch2_trans_exit(&trans);
2452 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2453 * Doesn't fix them yet, mainly because they haven't yet been observed:
2455 int bch2_fsck_full(struct bch_fs *c)
2459 ret = bch2_fs_check_snapshots(c) ?:
2460 bch2_fs_check_subvols(c) ?:
2461 bch2_delete_dead_snapshots(c) ?:
2462 check_inodes(c, true) ?:
2467 check_directory_structure(c) ?:
2471 if (bch2_err_matches(ret, BCH_ERR_need_snapshot_cleanup)) {
2472 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
2479 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2481 return bch2_fs_check_snapshots(c) ?:
2482 bch2_fs_check_subvols(c) ?:
2483 bch2_delete_dead_snapshots(c) ?:
2484 check_inodes(c, false);