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 btree_iter iter;
79 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
81 k = bch2_btree_iter_peek_slot(&iter);
86 if (k.k->type != KEY_TYPE_snapshot) {
87 bch_err(trans->c, "snapshot %u not fonud", snapshot);
92 *subvol = le32_to_cpu(bkey_s_c_to_snapshot(k).v->subvol);
94 bch2_trans_iter_exit(trans, &iter);
99 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
100 u32 *snapshot, u64 *inum)
102 struct bch_subvolume s;
105 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
107 *snapshot = le32_to_cpu(s.snapshot);
108 *inum = le64_to_cpu(s.inode);
112 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
113 u32 *snapshot, u64 *inum)
115 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
118 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
119 struct bch_inode_unpacked *inode)
121 struct btree_iter iter;
125 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
127 BTREE_ITER_ALL_SNAPSHOTS);
128 k = bch2_btree_iter_peek(&iter);
133 if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
138 ret = bch2_inode_unpack(k, inode);
140 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
141 bch_err(trans->c, "error fetching inode %llu: %s",
142 inode_nr, bch2_err_str(ret));
143 bch2_trans_iter_exit(trans, &iter);
147 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
148 struct bch_inode_unpacked *inode,
151 struct btree_iter iter;
155 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
156 SPOS(0, inode_nr, *snapshot), 0);
157 k = bch2_btree_iter_peek_slot(&iter);
162 ret = bkey_is_inode(k.k)
163 ? bch2_inode_unpack(k, inode)
166 *snapshot = iter.pos.snapshot;
168 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
169 bch_err(trans->c, "error fetching inode %llu:%u: %s",
170 inode_nr, *snapshot, bch2_err_str(ret));
171 bch2_trans_iter_exit(trans, &iter);
175 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
176 struct bch_inode_unpacked *inode,
179 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
182 static int __lookup_dirent(struct btree_trans *trans,
183 struct bch_hash_info hash_info,
184 subvol_inum dir, struct qstr *name,
185 u64 *target, unsigned *type)
187 struct btree_iter iter;
188 struct bkey_s_c_dirent d;
191 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
192 &hash_info, dir, name, 0);
196 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
197 *target = le64_to_cpu(d.v->d_inum);
199 bch2_trans_iter_exit(trans, &iter);
203 static int __write_inode(struct btree_trans *trans,
204 struct bch_inode_unpacked *inode,
207 struct btree_iter iter;
210 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
211 SPOS(0, inode->bi_inum, snapshot),
214 ret = bch2_btree_iter_traverse(&iter) ?:
215 bch2_inode_write(trans, &iter, inode);
216 bch2_trans_iter_exit(trans, &iter);
220 static int write_inode(struct btree_trans *trans,
221 struct bch_inode_unpacked *inode,
224 int ret = commit_do(trans, NULL, NULL,
226 BTREE_INSERT_LAZY_RW,
227 __write_inode(trans, inode, snapshot));
229 bch_err(trans->c, "error in fsck: error updating inode: %s",
234 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
236 struct bch_fs *c = trans->c;
237 struct btree_iter iter = { NULL };
238 struct bkey_i_inode_generation delete;
239 struct bch_inode_unpacked inode_u;
244 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
245 SPOS(inum, 0, snapshot),
246 SPOS(inum, U64_MAX, snapshot),
248 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
249 SPOS(inum, 0, snapshot),
250 SPOS(inum, U64_MAX, snapshot),
252 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
253 SPOS(inum, 0, snapshot),
254 SPOS(inum, U64_MAX, snapshot),
256 } while (ret == -BCH_ERR_transaction_restart_nested);
260 bch2_trans_begin(trans);
262 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
263 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
264 k = bch2_btree_iter_peek_slot(&iter);
270 if (!bkey_is_inode(k.k)) {
271 bch2_fs_inconsistent(c,
272 "inode %llu:%u not found when deleting",
278 bch2_inode_unpack(k, &inode_u);
280 /* Subvolume root? */
281 if (inode_u.bi_subvol)
282 bch_warn(c, "deleting inode %llu marked as unlinked, but also a subvolume root!?", inode_u.bi_inum);
284 bkey_inode_generation_init(&delete.k_i);
285 delete.k.p = iter.pos;
286 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
288 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
289 bch2_trans_commit(trans, NULL, NULL,
290 BTREE_INSERT_NOFAIL);
292 bch2_trans_iter_exit(trans, &iter);
293 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
296 return ret ?: -BCH_ERR_transaction_restart_nested;
299 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
301 struct bch_fs *c = trans->c;
302 struct btree_iter iter;
303 struct bch_inode_unpacked dir_inode;
304 struct bch_hash_info dir_hash_info;
307 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
311 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
313 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
315 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
316 &dir_hash_info, &iter,
317 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
318 bch2_trans_iter_exit(trans, &iter);
320 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
321 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
325 /* Get lost+found, create if it doesn't exist: */
326 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
327 struct bch_inode_unpacked *lostfound)
329 struct bch_fs *c = trans->c;
330 struct bch_inode_unpacked root;
331 struct bch_hash_info root_hash_info;
332 struct qstr lostfound_str = QSTR("lost+found");
333 subvol_inum root_inum = { .subvol = subvol };
339 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
343 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
347 root_hash_info = bch2_hash_info_init(c, &root);
349 ret = __lookup_dirent(trans, root_hash_info, root_inum,
350 &lostfound_str, &inum, &d_type);
351 if (ret == -ENOENT) {
352 bch_notice(c, "creating lost+found");
353 goto create_lostfound;
356 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
357 bch_err(c, "error looking up lost+found: %s", bch2_err_str(ret));
361 if (d_type != DT_DIR) {
362 bch_err(c, "error looking up lost+found: not a directory");
367 * The check_dirents pass has already run, dangling dirents
368 * shouldn't exist here:
370 return __lookup_inode(trans, inum, lostfound, &snapshot);
373 bch2_inode_init_early(c, lostfound);
375 ret = bch2_create_trans(trans, root_inum, &root,
376 lostfound, &lostfound_str,
377 0, 0, S_IFDIR|0700, 0, NULL, NULL,
378 (subvol_inum) { }, 0);
379 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
380 bch_err(c, "error creating lost+found: %s", bch2_err_str(ret));
384 static int __reattach_inode(struct btree_trans *trans,
385 struct bch_inode_unpacked *inode,
388 struct bch_hash_info dir_hash;
389 struct bch_inode_unpacked lostfound;
396 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
400 ret = lookup_lostfound(trans, subvol, &lostfound);
404 if (S_ISDIR(inode->bi_mode)) {
405 lostfound.bi_nlink++;
407 ret = __write_inode(trans, &lostfound, U32_MAX);
412 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
414 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
415 name = (struct qstr) QSTR(name_buf);
417 ret = bch2_dirent_create(trans,
420 .inum = lostfound.bi_inum,
424 &name, inode->bi_inum, &dir_offset,
425 BCH_HASH_SET_MUST_CREATE);
429 inode->bi_dir = lostfound.bi_inum;
430 inode->bi_dir_offset = dir_offset;
432 return __write_inode(trans, inode, inode_snapshot);
435 static int reattach_inode(struct btree_trans *trans,
436 struct bch_inode_unpacked *inode,
439 int ret = commit_do(trans, NULL, NULL,
440 BTREE_INSERT_LAZY_RW|
442 __reattach_inode(trans, inode, inode_snapshot));
444 bch_err(trans->c, "error reattaching inode %llu: %s",
445 inode->bi_inum, bch2_err_str(ret));
452 static int remove_backpointer(struct btree_trans *trans,
453 struct bch_inode_unpacked *inode)
455 struct btree_iter iter;
459 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
460 POS(inode->bi_dir, inode->bi_dir_offset), 0);
461 k = bch2_btree_iter_peek_slot(&iter);
465 if (k.k->type != KEY_TYPE_dirent) {
470 ret = __remove_dirent(trans, k.k->p);
472 bch2_trans_iter_exit(trans, &iter);
476 struct snapshots_seen_entry {
481 struct snapshots_seen {
483 DARRAY(struct snapshots_seen_entry) ids;
486 static inline void snapshots_seen_exit(struct snapshots_seen *s)
488 darray_exit(&s->ids);
491 static inline void snapshots_seen_init(struct snapshots_seen *s)
493 memset(s, 0, sizeof(*s));
496 static int snapshots_seen_add(struct bch_fs *c, struct snapshots_seen *s, u32 id)
498 struct snapshots_seen_entry *i, n = { id, id };
501 darray_for_each(s->ids, i) {
502 if (n.equiv < i->equiv)
505 if (i->equiv == n.equiv) {
506 bch_err(c, "%s(): adding duplicate snapshot", __func__);
511 ret = darray_insert_item(&s->ids, i - s->ids.data, n);
513 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
518 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
519 enum btree_id btree_id, struct bpos pos)
521 struct snapshots_seen_entry *i, n = {
523 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
527 if (!bkey_eq(s->pos, pos))
530 pos.snapshot = n.equiv;
533 darray_for_each(s->ids, i)
534 if (i->equiv == n.equiv) {
535 if (fsck_err_on(i->id != n.id, c,
536 "snapshot deletion did not run correctly:\n"
537 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
538 bch2_btree_ids[btree_id],
539 pos.inode, pos.offset,
540 i->id, n.id, n.equiv))
541 return -BCH_ERR_need_snapshot_cleanup;
546 ret = darray_push(&s->ids, n);
548 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
555 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
556 * and @ancestor hasn't been overwritten in @seen
558 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
560 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
561 u32 id, u32 ancestor)
564 u32 top = seen->ids.nr ? seen->ids.data[seen->ids.nr - 1].equiv : 0;
566 BUG_ON(id > ancestor);
567 BUG_ON(!bch2_snapshot_is_equiv(c, id));
568 BUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
570 /* @ancestor should be the snapshot most recently added to @seen */
571 BUG_ON(ancestor != seen->pos.snapshot);
572 BUG_ON(ancestor != top);
577 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
580 for (i = seen->ids.nr - 2;
581 i >= 0 && seen->ids.data[i].equiv >= id;
583 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv) &&
584 bch2_snapshot_is_ancestor(c, seen->ids.data[i].equiv, ancestor))
591 * ref_visible - given a key with snapshot id @src that points to a key with
592 * snapshot id @dst, test whether there is some snapshot in which @dst is
595 * This assumes we're visiting @src keys in natural key order.
597 * @s - list of snapshot IDs already seen at @src
598 * @src - snapshot ID of src key
599 * @dst - snapshot ID of dst key
601 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
605 ? key_visible_in_snapshot(c, s, dst, src)
606 : bch2_snapshot_is_ancestor(c, src, dst);
609 static int ref_visible2(struct bch_fs *c,
610 u32 src, struct snapshots_seen *src_seen,
611 u32 dst, struct snapshots_seen *dst_seen)
613 src = bch2_snapshot_equiv(c, src);
614 dst = bch2_snapshot_equiv(c, dst);
618 swap(dst_seen, src_seen);
620 return key_visible_in_snapshot(c, src_seen, dst, src);
623 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
624 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
625 (_i)->snapshot <= (_snapshot); _i++) \
626 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
628 struct inode_walker_entry {
629 struct bch_inode_unpacked inode;
634 struct inode_walker {
635 bool first_this_inode;
638 DARRAY(struct inode_walker_entry) inodes;
641 static void inode_walker_exit(struct inode_walker *w)
643 darray_exit(&w->inodes);
646 static struct inode_walker inode_walker_init(void)
648 return (struct inode_walker) { 0, };
651 static int add_inode(struct bch_fs *c, struct inode_walker *w,
652 struct bkey_s_c inode)
654 struct bch_inode_unpacked u;
656 BUG_ON(bch2_inode_unpack(inode, &u));
658 return darray_push(&w->inodes, ((struct inode_walker_entry) {
660 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
664 static int __walk_inode(struct btree_trans *trans,
665 struct inode_walker *w, struct bpos pos)
667 struct bch_fs *c = trans->c;
668 struct btree_iter iter;
670 u32 restart_count = trans->restart_count;
674 pos.snapshot = bch2_snapshot_equiv(c, pos.snapshot);
676 if (pos.inode == w->cur_inum) {
677 w->first_this_inode = false;
678 goto lookup_snapshot;
683 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
684 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
685 if (k.k->p.offset != pos.inode)
688 if (bkey_is_inode(k.k))
691 bch2_trans_iter_exit(trans, &iter);
696 w->cur_inum = pos.inode;
697 w->first_this_inode = true;
699 if (trans_was_restarted(trans, restart_count))
700 return -BCH_ERR_transaction_restart_nested;
703 for (i = 0; i < w->inodes.nr; i++)
704 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->inodes.data[i].snapshot))
708 BUG_ON(pos.snapshot > w->inodes.data[i].snapshot);
710 if (pos.snapshot != w->inodes.data[i].snapshot) {
711 struct inode_walker_entry e = w->inodes.data[i];
713 e.snapshot = pos.snapshot;
716 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
717 pos.inode, pos.snapshot, w->inodes.data[i].snapshot);
719 while (i && w->inodes.data[i - 1].snapshot > pos.snapshot)
722 ret = darray_insert_item(&w->inodes, i, e);
730 static int __get_visible_inodes(struct btree_trans *trans,
731 struct inode_walker *w,
732 struct snapshots_seen *s,
735 struct bch_fs *c = trans->c;
736 struct btree_iter iter;
742 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
743 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
744 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
746 if (k.k->p.offset != inum)
749 if (!ref_visible(c, s, s->pos.snapshot, equiv))
752 if (bkey_is_inode(k.k))
755 if (equiv >= s->pos.snapshot)
758 bch2_trans_iter_exit(trans, &iter);
763 static int check_key_has_snapshot(struct btree_trans *trans,
764 struct btree_iter *iter,
767 struct bch_fs *c = trans->c;
768 struct printbuf buf = PRINTBUF;
771 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
772 "key in missing snapshot: %s",
773 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
774 ret = bch2_btree_delete_at(trans, iter,
775 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
781 static int hash_redo_key(struct btree_trans *trans,
782 const struct bch_hash_desc desc,
783 struct bch_hash_info *hash_info,
784 struct btree_iter *k_iter, struct bkey_s_c k)
786 struct bkey_i *delete;
789 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
791 return PTR_ERR(delete);
793 tmp = bch2_bkey_make_mut(trans, k);
797 bkey_init(&delete->k);
798 delete->k.p = k_iter->pos;
799 return bch2_btree_iter_traverse(k_iter) ?:
800 bch2_trans_update(trans, k_iter, delete, 0) ?:
801 bch2_hash_set_snapshot(trans, desc, hash_info,
802 (subvol_inum) { 0, k.k->p.inode },
803 k.k->p.snapshot, tmp,
804 BCH_HASH_SET_MUST_CREATE,
805 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
806 bch2_trans_commit(trans, NULL, NULL,
808 BTREE_INSERT_LAZY_RW);
811 static int hash_check_key(struct btree_trans *trans,
812 const struct bch_hash_desc desc,
813 struct bch_hash_info *hash_info,
814 struct btree_iter *k_iter, struct bkey_s_c hash_k)
816 struct bch_fs *c = trans->c;
817 struct btree_iter iter = { NULL };
818 struct printbuf buf = PRINTBUF;
823 if (hash_k.k->type != desc.key_type)
826 hash = desc.hash_bkey(hash_info, hash_k);
828 if (likely(hash == hash_k.k->p.offset))
831 if (hash_k.k->p.offset < hash)
834 for_each_btree_key_norestart(trans, iter, desc.btree_id,
835 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
836 BTREE_ITER_SLOTS, k, ret) {
837 if (bkey_eq(k.k->p, hash_k.k->p))
840 if (fsck_err_on(k.k->type == desc.key_type &&
841 !desc.cmp_bkey(k, hash_k), c,
842 "duplicate hash table keys:\n%s",
843 (printbuf_reset(&buf),
844 bch2_bkey_val_to_text(&buf, c, hash_k),
846 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
850 if (bkey_deleted(k.k)) {
851 bch2_trans_iter_exit(trans, &iter);
856 bch2_trans_iter_exit(trans, &iter);
860 if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
861 bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
862 (printbuf_reset(&buf),
863 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
864 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
866 bch_err(c, "hash_redo_key err %s", bch2_err_str(ret));
869 ret = -BCH_ERR_transaction_restart_nested;
875 static int check_inode(struct btree_trans *trans,
876 struct btree_iter *iter,
878 struct bch_inode_unpacked *prev,
879 struct snapshots_seen *s,
882 struct bch_fs *c = trans->c;
883 struct bch_inode_unpacked u;
884 bool do_update = false;
887 ret = check_key_has_snapshot(trans, iter, k);
893 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
898 * if snapshot id isn't a leaf node, skip it - deletion in
899 * particular is not atomic, so on the internal snapshot nodes
900 * we can see inodes marked for deletion after a clean shutdown
902 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
905 if (!bkey_is_inode(k.k))
908 BUG_ON(bch2_inode_unpack(k, &u));
911 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
912 BCH_INODE_I_SECTORS_DIRTY|
913 BCH_INODE_UNLINKED)))
916 if (prev->bi_inum != u.bi_inum)
919 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
920 inode_d_type(prev) != inode_d_type(&u), c,
921 "inodes in different snapshots don't match")) {
922 bch_err(c, "repair not implemented yet");
926 if (u.bi_flags & BCH_INODE_UNLINKED &&
928 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
930 bch2_trans_unlock(trans);
933 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
934 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
935 bch_err(c, "error in fsck: error while deleting inode: %s",
940 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
942 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
944 bch_verbose(c, "truncating inode %llu", u.bi_inum);
946 bch2_trans_unlock(trans);
950 * XXX: need to truncate partial blocks too here - or ideally
951 * just switch units to bytes and that issue goes away
953 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
954 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
956 POS(u.bi_inum, U64_MAX),
958 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
959 bch_err(c, "error in fsck: error truncating inode: %s",
965 * We truncated without our normal sector accounting hook, just
966 * make sure we recalculate it:
968 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
970 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
974 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
976 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
980 bch_verbose(c, "recounting sectors for inode %llu",
983 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
985 bch_err(c, "error in fsck: error recounting inode sectors: %s",
986 bch2_err_str(sectors));
990 u.bi_sectors = sectors;
991 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
995 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
998 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
1003 ret = __write_inode(trans, &u, iter->pos.snapshot);
1005 bch_err(c, "error in fsck: error updating inode: %s",
1011 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1016 static int check_inodes(struct bch_fs *c, bool full)
1018 struct btree_trans trans;
1019 struct btree_iter iter;
1020 struct bch_inode_unpacked prev = { 0 };
1021 struct snapshots_seen s;
1025 snapshots_seen_init(&s);
1026 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1028 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
1030 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1031 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1032 check_inode(&trans, &iter, k, &prev, &s, full));
1034 bch2_trans_exit(&trans);
1035 snapshots_seen_exit(&s);
1037 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1042 * Checking for overlapping extents needs to be reimplemented
1045 static int fix_overlapping_extent(struct btree_trans *trans,
1046 struct bkey_s_c k, struct bpos cut_at)
1048 struct btree_iter iter;
1052 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1053 ret = PTR_ERR_OR_ZERO(u);
1057 bkey_reassemble(u, k);
1058 bch2_cut_front(cut_at, u);
1062 * We don't want to go through the extent_handle_overwrites path:
1064 * XXX: this is going to screw up disk accounting, extent triggers
1065 * assume things about extent overwrites - we should be running the
1066 * triggers manually here
1068 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1069 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1071 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1072 ret = bch2_btree_iter_traverse(&iter) ?:
1073 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1074 bch2_trans_commit(trans, NULL, NULL,
1075 BTREE_INSERT_NOFAIL|
1076 BTREE_INSERT_LAZY_RW);
1077 bch2_trans_iter_exit(trans, &iter);
1082 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1083 struct btree_iter *iter,
1089 bch2_trans_iter_init(trans, iter, BTREE_ID_dirents, pos, 0);
1090 k = bch2_btree_iter_peek_slot(iter);
1092 if (!ret && k.k->type != KEY_TYPE_dirent)
1095 bch2_trans_iter_exit(trans, iter);
1096 return (struct bkey_s_c_dirent) { .k = ERR_PTR(ret) };
1099 return bkey_s_c_to_dirent(k);
1102 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1103 struct bkey_s_c_dirent d)
1105 return inode->bi_dir == d.k->p.inode &&
1106 inode->bi_dir_offset == d.k->p.offset;
1109 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1110 struct bch_inode_unpacked *inode)
1112 return d.v->d_type == DT_SUBVOL
1113 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1114 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1117 static int inode_backpointer_exists(struct btree_trans *trans,
1118 struct bch_inode_unpacked *inode,
1121 struct btree_iter iter;
1122 struct bkey_s_c_dirent d;
1125 d = dirent_get_by_pos(trans, &iter,
1126 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1127 ret = bkey_err(d.s_c);
1129 return ret == -ENOENT ? 0 : ret;
1131 ret = dirent_points_to_inode(d, inode);
1132 bch2_trans_iter_exit(trans, &iter);
1136 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1138 struct bch_fs *c = trans->c;
1139 struct inode_walker_entry *i;
1140 u32 restart_count = trans->restart_count;
1144 darray_for_each(w->inodes, i) {
1145 if (i->inode.bi_sectors == i->count)
1148 count2 = bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot);
1150 if (i->count != count2) {
1151 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1154 if (i->inode.bi_sectors == i->count)
1158 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1159 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1160 w->cur_inum, i->snapshot,
1161 i->inode.bi_sectors, i->count)) {
1162 i->inode.bi_sectors = i->count;
1163 ret = write_inode(trans, &i->inode, i->snapshot);
1170 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1171 if (!ret && trans_was_restarted(trans, restart_count))
1172 ret = -BCH_ERR_transaction_restart_nested;
1179 struct snapshots_seen seen;
1182 typedef DARRAY(struct extent_end) extent_ends;
1184 static int check_overlapping_extents(struct btree_trans *trans,
1185 struct snapshots_seen *seen,
1186 extent_ends *extent_ends,
1188 struct btree_iter *iter)
1190 struct bch_fs *c = trans->c;
1191 struct extent_end *i;
1192 struct printbuf buf = PRINTBUF;
1195 darray_for_each(*extent_ends, i) {
1196 /* duplicate, due to transaction restart: */
1197 if (i->offset == k.k->p.offset &&
1198 i->snapshot == k.k->p.snapshot)
1201 if (!ref_visible2(c,
1202 k.k->p.snapshot, seen,
1203 i->snapshot, &i->seen))
1206 if (fsck_err_on(i->offset > bkey_start_offset(k.k), c,
1207 "overlapping extents: extent in snapshot %u ends at %llu overlaps with\n%s",
1210 (printbuf_reset(&buf),
1211 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1212 struct bkey_i *update = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1213 if ((ret = PTR_ERR_OR_ZERO(update)))
1215 bkey_reassemble(update, k);
1216 ret = bch2_trans_update_extent(trans, iter, update, 0);
1223 printbuf_exit(&buf);
1227 static int extent_ends_at(extent_ends *extent_ends,
1228 struct snapshots_seen *seen,
1231 struct extent_end *i, n = (struct extent_end) {
1232 .snapshot = k.k->p.snapshot,
1233 .offset = k.k->p.offset,
1237 n.seen.ids.data = kmemdup(seen->ids.data,
1238 sizeof(seen->ids.data[0]) * seen->ids.size,
1240 if (!n.seen.ids.data)
1241 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1243 darray_for_each(*extent_ends, i) {
1244 if (i->snapshot == k.k->p.snapshot) {
1245 snapshots_seen_exit(&i->seen);
1250 if (i->snapshot >= k.k->p.snapshot)
1254 return darray_insert_item(extent_ends, i - extent_ends->data, n);
1257 static void extent_ends_reset(extent_ends *extent_ends)
1259 struct extent_end *i;
1261 darray_for_each(*extent_ends, i)
1262 snapshots_seen_exit(&i->seen);
1264 extent_ends->nr = 0;
1267 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1269 struct inode_walker *inode,
1270 struct snapshots_seen *s,
1271 extent_ends *extent_ends)
1273 struct bch_fs *c = trans->c;
1274 struct inode_walker_entry *i;
1275 struct printbuf buf = PRINTBUF;
1279 ret = check_key_has_snapshot(trans, iter, k);
1281 ret = ret < 0 ? ret : 0;
1286 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1288 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1292 if (k.k->type == KEY_TYPE_whiteout)
1295 if (inode->cur_inum != k.k->p.inode) {
1296 ret = check_i_sectors(trans, inode);
1300 extent_ends_reset(extent_ends);
1303 BUG_ON(!iter->path->should_be_locked);
1305 ret = check_overlapping_extents(trans, s, extent_ends, k, iter);
1309 ret = extent_ends_at(extent_ends, s, k);
1313 ret = __walk_inode(trans, inode, equiv);
1317 if (fsck_err_on(ret == INT_MAX, c,
1318 "extent in missing inode:\n %s",
1319 (printbuf_reset(&buf),
1320 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1321 ret = bch2_btree_delete_at(trans, iter,
1322 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1326 if (ret == INT_MAX) {
1331 i = inode->inodes.data + ret;
1334 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1335 !S_ISLNK(i->inode.bi_mode), c,
1336 "extent in non regular inode mode %o:\n %s",
1338 (printbuf_reset(&buf),
1339 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1340 ret = bch2_btree_delete_at(trans, iter,
1341 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1346 * Check inodes in reverse order, from oldest snapshots to newest, so
1347 * that we emit the fewest number of whiteouts necessary:
1349 for (i = inode->inodes.data + inode->inodes.nr - 1;
1350 i >= inode->inodes.data;
1352 if (i->snapshot > equiv.snapshot ||
1353 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1356 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1357 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1358 !bkey_extent_is_reservation(k), c,
1359 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1360 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1361 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1362 struct btree_iter iter2;
1364 bch2_trans_copy_iter(&iter2, iter);
1365 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1366 ret = bch2_btree_iter_traverse(&iter2) ?:
1367 bch2_btree_delete_at(trans, &iter2,
1368 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1369 bch2_trans_iter_exit(trans, &iter2);
1373 if (i->snapshot != equiv.snapshot) {
1374 ret = snapshots_seen_add(c, s, i->snapshot);
1381 if (bkey_extent_is_allocation(k.k))
1382 for_each_visible_inode(c, s, inode, equiv.snapshot, i)
1383 i->count += k.k->size;
1385 bch2_bkey_buf_reassemble(&prev, c, k);
1391 printbuf_exit(&buf);
1393 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1394 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1399 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1400 * that i_size an i_sectors are consistent
1403 static int check_extents(struct bch_fs *c)
1405 struct inode_walker w = inode_walker_init();
1406 struct snapshots_seen s;
1407 struct btree_trans trans;
1408 struct btree_iter iter;
1410 extent_ends extent_ends = { 0 };
1411 struct disk_reservation res = { 0 };
1414 snapshots_seen_init(&s);
1415 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1417 bch_verbose(c, "checking extents");
1419 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1420 POS(BCACHEFS_ROOT_INO, 0),
1421 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1423 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
1424 bch2_disk_reservation_put(c, &res);
1425 check_extent(&trans, &iter, k, &w, &s, &extent_ends);
1428 bch2_disk_reservation_put(c, &res);
1429 extent_ends_reset(&extent_ends);
1430 darray_exit(&extent_ends);
1431 inode_walker_exit(&w);
1432 bch2_trans_exit(&trans);
1433 snapshots_seen_exit(&s);
1436 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1440 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1442 struct bch_fs *c = trans->c;
1443 struct inode_walker_entry *i;
1444 u32 restart_count = trans->restart_count;
1448 darray_for_each(w->inodes, i) {
1449 if (i->inode.bi_nlink == i->count)
1452 count2 = bch2_count_subdirs(trans, w->cur_inum, i->snapshot);
1456 if (i->count != count2) {
1457 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1460 if (i->inode.bi_nlink == i->count)
1464 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1465 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1466 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1467 i->inode.bi_nlink = i->count;
1468 ret = write_inode(trans, &i->inode, i->snapshot);
1475 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1476 if (!ret && trans_was_restarted(trans, restart_count))
1477 ret = -BCH_ERR_transaction_restart_nested;
1481 static int check_dirent_target(struct btree_trans *trans,
1482 struct btree_iter *iter,
1483 struct bkey_s_c_dirent d,
1484 struct bch_inode_unpacked *target,
1485 u32 target_snapshot)
1487 struct bch_fs *c = trans->c;
1488 struct bkey_i_dirent *n;
1489 bool backpointer_exists = true;
1490 struct printbuf buf = PRINTBUF;
1493 if (!target->bi_dir &&
1494 !target->bi_dir_offset) {
1495 target->bi_dir = d.k->p.inode;
1496 target->bi_dir_offset = d.k->p.offset;
1498 ret = __write_inode(trans, target, target_snapshot);
1503 if (!inode_points_to_dirent(target, d)) {
1504 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1508 backpointer_exists = ret;
1511 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1512 backpointer_exists, c,
1513 "directory %llu with multiple links",
1515 ret = __remove_dirent(trans, d.k->p);
1519 if (fsck_err_on(backpointer_exists &&
1520 !target->bi_nlink, c,
1521 "inode %llu type %s has multiple links but i_nlink 0",
1522 target->bi_inum, bch2_d_types[d.v->d_type])) {
1524 target->bi_flags &= ~BCH_INODE_UNLINKED;
1526 ret = __write_inode(trans, target, target_snapshot);
1531 if (fsck_err_on(!backpointer_exists, c,
1532 "inode %llu:%u has wrong backpointer:\n"
1534 "should be %llu:%llu",
1535 target->bi_inum, target_snapshot,
1537 target->bi_dir_offset,
1540 target->bi_dir = d.k->p.inode;
1541 target->bi_dir_offset = d.k->p.offset;
1543 ret = __write_inode(trans, target, target_snapshot);
1549 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1550 "incorrect d_type: got %s, should be %s:\n%s",
1551 bch2_d_type_str(d.v->d_type),
1552 bch2_d_type_str(inode_d_type(target)),
1553 (printbuf_reset(&buf),
1554 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1555 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1556 ret = PTR_ERR_OR_ZERO(n);
1560 bkey_reassemble(&n->k_i, d.s_c);
1561 n->v.d_type = inode_d_type(target);
1563 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1567 d = dirent_i_to_s_c(n);
1570 if (d.v->d_type == DT_SUBVOL &&
1571 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1572 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1573 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1574 le32_to_cpu(d.v->d_parent_subvol),
1575 target->bi_parent_subvol))) {
1576 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1577 ret = PTR_ERR_OR_ZERO(n);
1581 bkey_reassemble(&n->k_i, d.s_c);
1582 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1584 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1588 d = dirent_i_to_s_c(n);
1593 printbuf_exit(&buf);
1595 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1596 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1600 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1602 struct bch_hash_info *hash_info,
1603 struct inode_walker *dir,
1604 struct inode_walker *target,
1605 struct snapshots_seen *s)
1607 struct bch_fs *c = trans->c;
1608 struct bkey_s_c_dirent d;
1609 struct inode_walker_entry *i;
1610 struct printbuf buf = PRINTBUF;
1614 ret = check_key_has_snapshot(trans, iter, k);
1616 ret = ret < 0 ? ret : 0;
1621 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1623 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1627 if (k.k->type == KEY_TYPE_whiteout)
1630 if (dir->cur_inum != k.k->p.inode) {
1631 ret = check_subdir_count(trans, dir);
1636 BUG_ON(!iter->path->should_be_locked);
1638 ret = __walk_inode(trans, dir, equiv);
1642 if (fsck_err_on(ret == INT_MAX, c,
1643 "dirent in nonexisting directory:\n%s",
1644 (printbuf_reset(&buf),
1645 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1646 ret = bch2_btree_delete_at(trans, iter,
1647 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1651 if (ret == INT_MAX) {
1656 i = dir->inodes.data + ret;
1659 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1660 "dirent in non directory inode type %s:\n%s",
1661 bch2_d_type_str(inode_d_type(&i->inode)),
1662 (printbuf_reset(&buf),
1663 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1664 ret = bch2_btree_delete_at(trans, iter, 0);
1668 if (dir->first_this_inode)
1669 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1671 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1672 hash_info, iter, k);
1676 /* dirent has been deleted */
1681 if (k.k->type != KEY_TYPE_dirent)
1684 d = bkey_s_c_to_dirent(k);
1686 if (d.v->d_type == DT_SUBVOL) {
1687 struct bch_inode_unpacked subvol_root;
1688 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1689 u32 target_snapshot;
1692 ret = __subvol_lookup(trans, target_subvol,
1693 &target_snapshot, &target_inum);
1694 if (ret && ret != -ENOENT)
1697 if (fsck_err_on(ret, c,
1698 "dirent points to missing subvolume %llu",
1699 le64_to_cpu(d.v->d_child_subvol))) {
1700 ret = __remove_dirent(trans, d.k->p);
1704 ret = __lookup_inode(trans, target_inum,
1705 &subvol_root, &target_snapshot);
1706 if (ret && ret != -ENOENT)
1709 if (fsck_err_on(ret, c,
1710 "subvolume %u points to missing subvolume root %llu",
1713 bch_err(c, "repair not implemented yet");
1718 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1719 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1721 subvol_root.bi_subvol, target_subvol)) {
1722 subvol_root.bi_subvol = target_subvol;
1723 ret = __write_inode(trans, &subvol_root, target_snapshot);
1728 ret = check_dirent_target(trans, iter, d, &subvol_root,
1733 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1737 if (fsck_err_on(!target->inodes.nr, c,
1738 "dirent points to missing inode: (equiv %u)\n%s",
1740 (printbuf_reset(&buf),
1741 bch2_bkey_val_to_text(&buf, c, k),
1743 ret = __remove_dirent(trans, d.k->p);
1748 darray_for_each(target->inodes, i) {
1749 ret = check_dirent_target(trans, iter, d,
1750 &i->inode, i->snapshot);
1756 if (d.v->d_type == DT_DIR)
1757 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1763 printbuf_exit(&buf);
1765 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1766 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1771 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1775 static int check_dirents(struct bch_fs *c)
1777 struct inode_walker dir = inode_walker_init();
1778 struct inode_walker target = inode_walker_init();
1779 struct snapshots_seen s;
1780 struct bch_hash_info hash_info;
1781 struct btree_trans trans;
1782 struct btree_iter iter;
1786 bch_verbose(c, "checking dirents");
1788 snapshots_seen_init(&s);
1789 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1791 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1792 POS(BCACHEFS_ROOT_INO, 0),
1793 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1796 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1797 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1799 bch2_trans_exit(&trans);
1800 snapshots_seen_exit(&s);
1801 inode_walker_exit(&dir);
1802 inode_walker_exit(&target);
1805 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1809 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1811 struct bch_hash_info *hash_info,
1812 struct inode_walker *inode)
1814 struct bch_fs *c = trans->c;
1817 ret = check_key_has_snapshot(trans, iter, k);
1821 ret = __walk_inode(trans, inode, k.k->p);
1825 if (fsck_err_on(ret == INT_MAX, c,
1826 "xattr for missing inode %llu",
1828 return bch2_btree_delete_at(trans, iter, 0);
1835 if (inode->first_this_inode)
1836 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1838 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1840 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1841 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1846 * Walk xattrs: verify that they all have a corresponding inode
1849 static int check_xattrs(struct bch_fs *c)
1851 struct inode_walker inode = inode_walker_init();
1852 struct bch_hash_info hash_info;
1853 struct btree_trans trans;
1854 struct btree_iter iter;
1858 bch_verbose(c, "checking xattrs");
1860 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1862 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1863 POS(BCACHEFS_ROOT_INO, 0),
1864 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1867 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1868 check_xattr(&trans, &iter, k, &hash_info, &inode));
1870 bch2_trans_exit(&trans);
1873 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1877 static int check_root_trans(struct btree_trans *trans)
1879 struct bch_fs *c = trans->c;
1880 struct bch_inode_unpacked root_inode;
1885 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1886 if (ret && ret != -ENOENT)
1889 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1890 struct bkey_i_subvolume root_subvol;
1893 inum = BCACHEFS_ROOT_INO;
1895 bkey_subvolume_init(&root_subvol.k_i);
1896 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1897 root_subvol.v.flags = 0;
1898 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1899 root_subvol.v.inode = cpu_to_le64(inum);
1900 ret = commit_do(trans, NULL, NULL,
1901 BTREE_INSERT_NOFAIL|
1902 BTREE_INSERT_LAZY_RW,
1903 __bch2_btree_insert(trans, BTREE_ID_subvolumes,
1904 &root_subvol.k_i, 0));
1906 bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1912 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1913 if (ret && ret != -ENOENT)
1916 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1917 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1918 "root inode not a directory")) {
1919 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1921 root_inode.bi_inum = inum;
1923 ret = __write_inode(trans, &root_inode, snapshot);
1925 bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1932 /* Get root directory, create if it doesn't exist: */
1934 static int check_root(struct bch_fs *c)
1936 bch_verbose(c, "checking root directory");
1938 return bch2_trans_do(c, NULL, NULL,
1939 BTREE_INSERT_NOFAIL|
1940 BTREE_INSERT_LAZY_RW,
1941 check_root_trans(&trans));
1944 struct pathbuf_entry {
1949 typedef DARRAY(struct pathbuf_entry) pathbuf;
1951 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1953 struct pathbuf_entry *i;
1955 darray_for_each(*p, i)
1956 if (i->inum == inum &&
1957 i->snapshot == snapshot)
1963 static int path_down(struct bch_fs *c, pathbuf *p,
1964 u64 inum, u32 snapshot)
1966 int ret = darray_push(p, ((struct pathbuf_entry) {
1968 .snapshot = snapshot,
1972 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1978 * Check that a given inode is reachable from the root:
1980 * XXX: we should also be verifying that inodes are in the right subvolumes
1982 static int check_path(struct btree_trans *trans,
1984 struct bch_inode_unpacked *inode,
1987 struct bch_fs *c = trans->c;
1990 snapshot = bch2_snapshot_equiv(c, snapshot);
1993 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1994 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1995 struct btree_iter dirent_iter;
1996 struct bkey_s_c_dirent d;
1997 u32 parent_snapshot = snapshot;
1999 if (inode->bi_subvol) {
2002 ret = subvol_lookup(trans, inode->bi_parent_subvol,
2003 &parent_snapshot, &inum);
2008 ret = lockrestart_do(trans,
2009 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
2010 SPOS(inode->bi_dir, inode->bi_dir_offset,
2011 parent_snapshot))).k));
2012 if (ret && ret != -ENOENT)
2015 if (!ret && !dirent_points_to_inode(d, inode)) {
2016 bch2_trans_iter_exit(trans, &dirent_iter);
2020 if (ret == -ENOENT) {
2021 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2022 inode->bi_inum, snapshot,
2023 bch2_d_type_str(inode_d_type(inode)),
2026 inode->bi_dir_offset))
2027 ret = reattach_inode(trans, inode, snapshot);
2031 bch2_trans_iter_exit(trans, &dirent_iter);
2033 if (!S_ISDIR(inode->bi_mode))
2036 ret = path_down(c, p, inode->bi_inum, snapshot);
2038 bch_err(c, "memory allocation failure");
2042 snapshot = parent_snapshot;
2044 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2046 /* Should have been caught in dirents pass */
2047 bch_err(c, "error looking up parent directory: %i", ret);
2051 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2052 struct pathbuf_entry *i;
2054 /* XXX print path */
2055 bch_err(c, "directory structure loop");
2057 darray_for_each(*p, i)
2058 pr_err("%llu:%u", i->inum, i->snapshot);
2059 pr_err("%llu:%u", inode->bi_inum, snapshot);
2061 if (!fsck_err(c, "directory structure loop"))
2064 ret = commit_do(trans, NULL, NULL,
2065 BTREE_INSERT_NOFAIL|
2066 BTREE_INSERT_LAZY_RW,
2067 remove_backpointer(trans, inode));
2069 bch_err(c, "error removing dirent: %i", ret);
2073 ret = reattach_inode(trans, inode, snapshot);
2078 bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
2083 * Check for unreachable inodes, as well as loops in the directory structure:
2084 * After check_dirents(), if an inode backpointer doesn't exist that means it's
2088 static int check_directory_structure(struct bch_fs *c)
2090 struct btree_trans trans;
2091 struct btree_iter iter;
2093 struct bch_inode_unpacked u;
2094 pathbuf path = { 0, };
2097 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2099 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2101 BTREE_ITER_PREFETCH|
2102 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2103 if (!bkey_is_inode(k.k))
2106 ret = bch2_inode_unpack(k, &u);
2108 /* Should have been caught earlier in fsck: */
2109 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2113 if (u.bi_flags & BCH_INODE_UNLINKED)
2116 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2120 bch2_trans_iter_exit(&trans, &iter);
2124 bch2_trans_exit(&trans);
2128 struct nlink_table {
2139 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2140 u64 inum, u32 snapshot)
2142 if (t->nr == t->size) {
2143 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2144 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2147 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2149 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2153 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2161 t->d[t->nr++] = (struct nlink) {
2163 .snapshot = snapshot,
2169 static int nlink_cmp(const void *_l, const void *_r)
2171 const struct nlink *l = _l;
2172 const struct nlink *r = _r;
2174 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2177 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2178 struct nlink_table *links,
2179 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2181 struct nlink *link, key = {
2182 .inum = inum, .snapshot = U32_MAX,
2185 if (inum < range_start || inum >= range_end)
2188 link = __inline_bsearch(&key, links->d, links->nr,
2189 sizeof(links->d[0]), nlink_cmp);
2193 while (link > links->d && link[0].inum == link[-1].inum)
2196 for (; link < links->d + links->nr && link->inum == inum; link++)
2197 if (ref_visible(c, s, snapshot, link->snapshot)) {
2199 if (link->snapshot >= snapshot)
2205 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2206 struct nlink_table *t,
2207 u64 start, u64 *end)
2209 struct btree_trans trans;
2210 struct btree_iter iter;
2212 struct bch_inode_unpacked u;
2215 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2217 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2220 BTREE_ITER_PREFETCH|
2221 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2222 if (!bkey_is_inode(k.k))
2225 /* Should never fail, checked by bch2_inode_invalid: */
2226 BUG_ON(bch2_inode_unpack(k, &u));
2229 * Backpointer and directory structure checks are sufficient for
2230 * directories, since they can't have hardlinks:
2232 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2238 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2240 *end = k.k->p.offset;
2246 bch2_trans_iter_exit(&trans, &iter);
2247 bch2_trans_exit(&trans);
2250 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2256 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2257 u64 range_start, u64 range_end)
2259 struct btree_trans trans;
2260 struct snapshots_seen s;
2261 struct btree_iter iter;
2263 struct bkey_s_c_dirent d;
2266 snapshots_seen_init(&s);
2268 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2270 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2272 BTREE_ITER_PREFETCH|
2273 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2274 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2278 switch (k.k->type) {
2279 case KEY_TYPE_dirent:
2280 d = bkey_s_c_to_dirent(k);
2282 if (d.v->d_type != DT_DIR &&
2283 d.v->d_type != DT_SUBVOL)
2284 inc_link(c, &s, links, range_start, range_end,
2285 le64_to_cpu(d.v->d_inum),
2286 bch2_snapshot_equiv(c, d.k->p.snapshot));
2290 bch2_trans_iter_exit(&trans, &iter);
2293 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2295 bch2_trans_exit(&trans);
2296 snapshots_seen_exit(&s);
2300 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2302 struct nlink_table *links,
2303 size_t *idx, u64 range_end)
2305 struct bch_fs *c = trans->c;
2306 struct bch_inode_unpacked u;
2307 struct nlink *link = &links->d[*idx];
2310 if (k.k->p.offset >= range_end)
2313 if (!bkey_is_inode(k.k))
2316 BUG_ON(bch2_inode_unpack(k, &u));
2318 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2324 while ((cmp_int(link->inum, k.k->p.offset) ?:
2325 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2326 BUG_ON(*idx == links->nr);
2327 link = &links->d[++*idx];
2330 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2331 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2332 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2333 bch2_inode_nlink_get(&u), link->count)) {
2334 bch2_inode_nlink_set(&u, link->count);
2335 ret = __write_inode(trans, &u, k.k->p.snapshot);
2342 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2343 struct nlink_table *links,
2344 u64 range_start, u64 range_end)
2346 struct btree_trans trans;
2347 struct btree_iter iter;
2352 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2354 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2355 POS(0, range_start),
2356 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2357 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2358 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2360 bch2_trans_exit(&trans);
2363 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2371 static int check_nlinks(struct bch_fs *c)
2373 struct nlink_table links = { 0 };
2374 u64 this_iter_range_start, next_iter_range_start = 0;
2377 bch_verbose(c, "checking inode nlinks");
2380 this_iter_range_start = next_iter_range_start;
2381 next_iter_range_start = U64_MAX;
2383 ret = check_nlinks_find_hardlinks(c, &links,
2384 this_iter_range_start,
2385 &next_iter_range_start);
2387 ret = check_nlinks_walk_dirents(c, &links,
2388 this_iter_range_start,
2389 next_iter_range_start);
2393 ret = check_nlinks_update_hardlinks(c, &links,
2394 this_iter_range_start,
2395 next_iter_range_start);
2400 } while (next_iter_range_start != U64_MAX);
2407 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2410 struct bkey_s_c_reflink_p p;
2411 struct bkey_i_reflink_p *u;
2414 if (k.k->type != KEY_TYPE_reflink_p)
2417 p = bkey_s_c_to_reflink_p(k);
2419 if (!p.v->front_pad && !p.v->back_pad)
2422 u = bch2_trans_kmalloc(trans, sizeof(*u));
2423 ret = PTR_ERR_OR_ZERO(u);
2427 bkey_reassemble(&u->k_i, k);
2431 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2435 static int fix_reflink_p(struct bch_fs *c)
2437 struct btree_trans trans;
2438 struct btree_iter iter;
2442 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2445 bch_verbose(c, "fixing reflink_p keys");
2447 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2449 ret = for_each_btree_key_commit(&trans, iter,
2450 BTREE_ID_extents, POS_MIN,
2451 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2452 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2453 fix_reflink_p_key(&trans, &iter, k));
2455 bch2_trans_exit(&trans);
2460 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2461 * Doesn't fix them yet, mainly because they haven't yet been observed:
2463 int bch2_fsck_full(struct bch_fs *c)
2467 ret = bch2_fs_check_snapshots(c) ?:
2468 bch2_fs_check_subvols(c) ?:
2469 bch2_delete_dead_snapshots(c) ?:
2470 check_inodes(c, true) ?:
2475 check_directory_structure(c) ?:
2479 if (bch2_err_matches(ret, BCH_ERR_need_snapshot_cleanup)) {
2480 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
2487 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2489 return bch2_fs_check_snapshots(c) ?:
2490 bch2_fs_check_subvols(c) ?:
2491 bch2_delete_dead_snapshots(c) ?:
2492 check_inodes(c, false);