1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_cache.h"
6 #include "btree_update.h"
11 #include "fs-common.h"
20 #include <linux/bsearch.h>
21 #include <linux/dcache.h> /* struct qstr */
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,
32 int ret = for_each_btree_key_upto(trans, iter, BTREE_ID_extents,
33 SPOS(inum, 0, snapshot),
36 if (bkey_extent_is_allocation(k.k))
41 return ret ?: sectors;
44 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
49 int ret = for_each_btree_key_upto(trans, iter, BTREE_ID_dirents,
50 SPOS(inum, 0, snapshot),
53 if (k.k->type == KEY_TYPE_dirent &&
54 bkey_s_c_to_dirent(k).v->d_type == DT_DIR)
59 return ret ?: subdirs;
62 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
63 u32 *snapshot, u64 *inum)
65 struct bch_subvolume s;
68 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
70 *snapshot = le32_to_cpu(s.snapshot);
71 *inum = le64_to_cpu(s.inode);
75 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
76 struct bch_inode_unpacked *inode)
78 struct btree_iter iter;
82 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
84 BTREE_ITER_ALL_SNAPSHOTS);
85 k = bch2_btree_iter_peek(&iter);
90 if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
91 ret = -BCH_ERR_ENOENT_inode;
95 ret = bch2_inode_unpack(k, inode);
97 bch_err_msg(trans->c, ret, "fetching inode %llu", inode_nr);
98 bch2_trans_iter_exit(trans, &iter);
102 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
103 struct bch_inode_unpacked *inode,
106 struct btree_iter iter;
110 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
111 SPOS(0, inode_nr, *snapshot), 0);
116 ret = bkey_is_inode(k.k)
117 ? bch2_inode_unpack(k, inode)
118 : -BCH_ERR_ENOENT_inode;
120 *snapshot = iter.pos.snapshot;
122 bch_err_msg(trans->c, ret, "fetching inode %llu:%u", inode_nr, *snapshot);
123 bch2_trans_iter_exit(trans, &iter);
127 static int __lookup_dirent(struct btree_trans *trans,
128 struct bch_hash_info hash_info,
129 subvol_inum dir, struct qstr *name,
130 u64 *target, unsigned *type)
132 struct btree_iter iter;
133 struct bkey_s_c_dirent d;
136 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
137 &hash_info, dir, name, 0);
141 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
142 *target = le64_to_cpu(d.v->d_inum);
144 bch2_trans_iter_exit(trans, &iter);
148 static int __write_inode(struct btree_trans *trans,
149 struct bch_inode_unpacked *inode,
152 struct bkey_inode_buf *inode_p =
153 bch2_trans_kmalloc(trans, sizeof(*inode_p));
156 return PTR_ERR(inode_p);
158 bch2_inode_pack(inode_p, inode);
159 inode_p->inode.k.p.snapshot = snapshot;
161 return bch2_btree_insert_nonextent(trans, BTREE_ID_inodes,
163 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
166 static int fsck_write_inode(struct btree_trans *trans,
167 struct bch_inode_unpacked *inode,
170 int ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
171 __write_inode(trans, inode, snapshot));
172 bch_err_fn(trans->c, ret);
176 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
178 struct bch_fs *c = trans->c;
179 struct btree_iter iter;
180 struct bch_inode_unpacked dir_inode;
181 struct bch_hash_info dir_hash_info;
184 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
188 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
190 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
192 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
193 &dir_hash_info, &iter,
194 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
195 bch2_trans_iter_exit(trans, &iter);
201 /* Get lost+found, create if it doesn't exist: */
202 static int lookup_lostfound(struct btree_trans *trans, u32 snapshot,
203 struct bch_inode_unpacked *lostfound)
205 struct bch_fs *c = trans->c;
206 struct qstr lostfound_str = QSTR("lost+found");
211 struct bch_snapshot_tree st;
212 ret = bch2_snapshot_tree_lookup(trans,
213 bch2_snapshot_tree(c, snapshot), &st);
217 subvol_inum root_inum = { .subvol = le32_to_cpu(st.master_subvol) };
220 ret = subvol_lookup(trans, le32_to_cpu(st.master_subvol),
221 &subvol_snapshot, &root_inum.inum);
222 bch_err_msg(c, ret, "looking up root subvol");
226 struct bch_inode_unpacked root_inode;
227 struct bch_hash_info root_hash_info;
228 ret = lookup_inode(trans, root_inum.inum, &root_inode, &snapshot);
229 bch_err_msg(c, ret, "looking up root inode");
233 root_hash_info = bch2_hash_info_init(c, &root_inode);
235 ret = __lookup_dirent(trans, root_hash_info, root_inum,
236 &lostfound_str, &inum, &d_type);
237 if (bch2_err_matches(ret, ENOENT))
238 goto create_lostfound;
244 if (d_type != DT_DIR) {
245 bch_err(c, "error looking up lost+found: not a directory");
246 return -BCH_ERR_ENOENT_not_directory;
250 * The bch2_check_dirents pass has already run, dangling dirents
251 * shouldn't exist here:
253 return lookup_inode(trans, inum, lostfound, &snapshot);
257 * XXX: we could have a nicer log message here if we had a nice way to
258 * walk backpointers to print a path
260 bch_notice(c, "creating lost+found in snapshot %u", le32_to_cpu(st.root_snapshot));
262 u64 now = bch2_current_time(c);
263 struct btree_iter lostfound_iter = { NULL };
264 u64 cpu = raw_smp_processor_id();
266 bch2_inode_init_early(c, lostfound);
267 bch2_inode_init_late(lostfound, now, 0, 0, S_IFDIR|0700, 0, &root_inode);
268 lostfound->bi_dir = root_inode.bi_inum;
270 root_inode.bi_nlink++;
272 ret = bch2_inode_create(trans, &lostfound_iter, lostfound, snapshot, cpu);
276 bch2_btree_iter_set_snapshot(&lostfound_iter, snapshot);
277 ret = bch2_btree_iter_traverse(&lostfound_iter);
281 ret = bch2_dirent_create_snapshot(trans,
282 root_inode.bi_inum, snapshot, &root_hash_info,
283 mode_to_type(lostfound->bi_mode),
286 &lostfound->bi_dir_offset,
287 BCH_HASH_SET_MUST_CREATE) ?:
288 bch2_inode_write_flags(trans, &lostfound_iter, lostfound,
289 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
291 bch_err_msg(c, ret, "creating lost+found");
292 bch2_trans_iter_exit(trans, &lostfound_iter);
296 static int reattach_inode(struct btree_trans *trans,
297 struct bch_inode_unpacked *inode,
300 struct bch_hash_info dir_hash;
301 struct bch_inode_unpacked lostfound;
307 ret = lookup_lostfound(trans, inode_snapshot, &lostfound);
311 if (S_ISDIR(inode->bi_mode)) {
312 lostfound.bi_nlink++;
314 ret = __write_inode(trans, &lostfound, U32_MAX);
319 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
321 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
322 name = (struct qstr) QSTR(name_buf);
324 ret = bch2_dirent_create_snapshot(trans,
325 lostfound.bi_inum, inode_snapshot,
328 &name, inode->bi_inum, &dir_offset,
329 BCH_HASH_SET_MUST_CREATE);
333 inode->bi_dir = lostfound.bi_inum;
334 inode->bi_dir_offset = dir_offset;
336 return __write_inode(trans, inode, inode_snapshot);
339 static int remove_backpointer(struct btree_trans *trans,
340 struct bch_inode_unpacked *inode)
342 struct btree_iter iter;
343 struct bkey_s_c_dirent d;
346 d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
347 POS(inode->bi_dir, inode->bi_dir_offset), 0,
350 __remove_dirent(trans, d.k->p);
351 bch2_trans_iter_exit(trans, &iter);
355 struct snapshots_seen_entry {
360 struct snapshots_seen {
362 DARRAY(struct snapshots_seen_entry) ids;
365 static inline void snapshots_seen_exit(struct snapshots_seen *s)
367 darray_exit(&s->ids);
370 static inline void snapshots_seen_init(struct snapshots_seen *s)
372 memset(s, 0, sizeof(*s));
375 static int snapshots_seen_add_inorder(struct bch_fs *c, struct snapshots_seen *s, u32 id)
377 struct snapshots_seen_entry *i, n = {
379 .equiv = bch2_snapshot_equiv(c, id),
383 __darray_for_each(s->ids, i) {
390 ret = darray_insert_item(&s->ids, i - s->ids.data, n);
392 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
397 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
398 enum btree_id btree_id, struct bpos pos)
400 struct snapshots_seen_entry n = {
402 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
406 if (!bkey_eq(s->pos, pos))
410 s->pos.snapshot = n.equiv;
412 darray_for_each(s->ids, i) {
417 * We currently don't rigorously track for snapshot cleanup
418 * needing to be run, so it shouldn't be a fsck error yet:
420 if (i->equiv == n.equiv) {
421 bch_err(c, "snapshot deletion did not finish:\n"
422 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
423 bch2_btree_id_str(btree_id),
424 pos.inode, pos.offset,
425 i->id, n.id, n.equiv);
426 set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
427 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_delete_dead_snapshots);
431 ret = darray_push(&s->ids, n);
433 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
439 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
440 * and @ancestor hasn't been overwritten in @seen
442 * @c: filesystem handle
443 * @seen: list of snapshot ids already seen at current position
444 * @id: descendent snapshot id
445 * @ancestor: ancestor snapshot id
447 * Returns: whether key in @ancestor snapshot is visible in @id snapshot
449 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
450 u32 id, u32 ancestor)
454 EBUG_ON(id > ancestor);
455 EBUG_ON(!bch2_snapshot_is_equiv(c, id));
456 EBUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
458 /* @ancestor should be the snapshot most recently added to @seen */
459 EBUG_ON(ancestor != seen->pos.snapshot);
460 EBUG_ON(ancestor != seen->ids.data[seen->ids.nr - 1].equiv);
465 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
469 * We know that @id is a descendant of @ancestor, we're checking if
470 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
471 * @ascestor and with @id as a descendent.
473 * But we already know that we're scanning IDs between @id and @ancestor
474 * numerically, since snapshot ID lists are kept sorted, so if we find
475 * an id that's an ancestor of @id we're done:
478 for (i = seen->ids.nr - 2;
479 i >= 0 && seen->ids.data[i].equiv >= id;
481 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv))
488 * ref_visible - given a key with snapshot id @src that points to a key with
489 * snapshot id @dst, test whether there is some snapshot in which @dst is
492 * @c: filesystem handle
493 * @s: list of snapshot IDs already seen at @src
494 * @src: snapshot ID of src key
495 * @dst: snapshot ID of dst key
496 * Returns: true if there is some snapshot in which @dst is visible
498 * Assumes we're visiting @src keys in natural key order
500 static bool ref_visible(struct bch_fs *c, struct snapshots_seen *s,
504 ? key_visible_in_snapshot(c, s, dst, src)
505 : bch2_snapshot_is_ancestor(c, src, dst);
508 static int ref_visible2(struct bch_fs *c,
509 u32 src, struct snapshots_seen *src_seen,
510 u32 dst, struct snapshots_seen *dst_seen)
512 src = bch2_snapshot_equiv(c, src);
513 dst = bch2_snapshot_equiv(c, dst);
517 swap(dst_seen, src_seen);
519 return key_visible_in_snapshot(c, src_seen, dst, src);
522 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
523 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
524 (_i)->snapshot <= (_snapshot); _i++) \
525 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
527 struct inode_walker_entry {
528 struct bch_inode_unpacked inode;
534 struct inode_walker {
535 bool first_this_inode;
536 bool recalculate_sums;
537 struct bpos last_pos;
539 DARRAY(struct inode_walker_entry) inodes;
542 static void inode_walker_exit(struct inode_walker *w)
544 darray_exit(&w->inodes);
547 static struct inode_walker inode_walker_init(void)
549 return (struct inode_walker) { 0, };
552 static int add_inode(struct bch_fs *c, struct inode_walker *w,
553 struct bkey_s_c inode)
555 struct bch_inode_unpacked u;
557 BUG_ON(bch2_inode_unpack(inode, &u));
559 return darray_push(&w->inodes, ((struct inode_walker_entry) {
561 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
565 static int get_inodes_all_snapshots(struct btree_trans *trans,
566 struct inode_walker *w, u64 inum)
568 struct bch_fs *c = trans->c;
569 struct btree_iter iter;
573 w->recalculate_sums = false;
576 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
577 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
578 if (k.k->p.offset != inum)
581 if (bkey_is_inode(k.k))
584 bch2_trans_iter_exit(trans, &iter);
589 w->first_this_inode = true;
593 static struct inode_walker_entry *
594 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w,
595 u32 snapshot, bool is_whiteout)
597 struct inode_walker_entry *i;
599 snapshot = bch2_snapshot_equiv(c, snapshot);
601 __darray_for_each(w->inodes, i)
602 if (bch2_snapshot_is_ancestor(c, snapshot, i->snapshot))
607 BUG_ON(snapshot > i->snapshot);
609 if (snapshot != i->snapshot && !is_whiteout) {
610 struct inode_walker_entry new = *i;
614 new.snapshot = snapshot;
617 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
618 w->last_pos.inode, snapshot, i->snapshot);
620 while (i > w->inodes.data && i[-1].snapshot > snapshot)
623 pos = i - w->inodes.data;
624 ret = darray_insert_item(&w->inodes, pos, new);
628 i = w->inodes.data + pos;
634 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
635 struct inode_walker *w, struct bpos pos,
638 if (w->last_pos.inode != pos.inode) {
639 int ret = get_inodes_all_snapshots(trans, w, pos.inode);
642 } else if (bkey_cmp(w->last_pos, pos)) {
643 darray_for_each(w->inodes, i)
644 i->seen_this_pos = false;
649 return lookup_inode_for_snapshot(trans->c, w, pos.snapshot, is_whiteout);
652 static int __get_visible_inodes(struct btree_trans *trans,
653 struct inode_walker *w,
654 struct snapshots_seen *s,
657 struct bch_fs *c = trans->c;
658 struct btree_iter iter;
664 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
665 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
666 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
668 if (k.k->p.offset != inum)
671 if (!ref_visible(c, s, s->pos.snapshot, equiv))
674 if (bkey_is_inode(k.k))
677 if (equiv >= s->pos.snapshot)
680 bch2_trans_iter_exit(trans, &iter);
685 static int check_key_has_snapshot(struct btree_trans *trans,
686 struct btree_iter *iter,
689 struct bch_fs *c = trans->c;
690 struct printbuf buf = PRINTBUF;
693 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
694 bkey_in_missing_snapshot,
695 "key in missing snapshot: %s",
696 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
697 ret = bch2_btree_delete_at(trans, iter,
698 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
704 static int hash_redo_key(struct btree_trans *trans,
705 const struct bch_hash_desc desc,
706 struct bch_hash_info *hash_info,
707 struct btree_iter *k_iter, struct bkey_s_c k)
709 struct bkey_i *delete;
712 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
714 return PTR_ERR(delete);
716 tmp = bch2_bkey_make_mut_noupdate(trans, k);
720 bkey_init(&delete->k);
721 delete->k.p = k_iter->pos;
722 return bch2_btree_iter_traverse(k_iter) ?:
723 bch2_trans_update(trans, k_iter, delete, 0) ?:
724 bch2_hash_set_snapshot(trans, desc, hash_info,
725 (subvol_inum) { 0, k.k->p.inode },
726 k.k->p.snapshot, tmp,
727 BCH_HASH_SET_MUST_CREATE,
728 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
729 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
732 static int hash_check_key(struct btree_trans *trans,
733 const struct bch_hash_desc desc,
734 struct bch_hash_info *hash_info,
735 struct btree_iter *k_iter, struct bkey_s_c hash_k)
737 struct bch_fs *c = trans->c;
738 struct btree_iter iter = { NULL };
739 struct printbuf buf = PRINTBUF;
744 if (hash_k.k->type != desc.key_type)
747 hash = desc.hash_bkey(hash_info, hash_k);
749 if (likely(hash == hash_k.k->p.offset))
752 if (hash_k.k->p.offset < hash)
755 for_each_btree_key_norestart(trans, iter, desc.btree_id,
756 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
757 BTREE_ITER_SLOTS, k, ret) {
758 if (bkey_eq(k.k->p, hash_k.k->p))
761 if (fsck_err_on(k.k->type == desc.key_type &&
762 !desc.cmp_bkey(k, hash_k), c,
763 hash_table_key_duplicate,
764 "duplicate hash table keys:\n%s",
765 (printbuf_reset(&buf),
766 bch2_bkey_val_to_text(&buf, c, hash_k),
768 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
772 if (bkey_deleted(k.k)) {
773 bch2_trans_iter_exit(trans, &iter);
778 bch2_trans_iter_exit(trans, &iter);
782 if (fsck_err(c, hash_table_key_wrong_offset,
783 "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
784 bch2_btree_id_str(desc.btree_id), hash_k.k->p.inode, hash_k.k->p.offset, hash,
785 (printbuf_reset(&buf),
786 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
787 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
791 ret = -BCH_ERR_transaction_restart_nested;
797 static int check_inode_deleted_list(struct btree_trans *trans, struct bpos p)
799 struct btree_iter iter;
800 struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_deleted_inodes, p, 0);
801 int ret = bkey_err(k);
805 bch2_trans_iter_exit(trans, &iter);
806 return k.k->type == KEY_TYPE_set;
809 static int check_inode(struct btree_trans *trans,
810 struct btree_iter *iter,
812 struct bch_inode_unpacked *prev,
813 struct snapshots_seen *s,
816 struct bch_fs *c = trans->c;
817 struct bch_inode_unpacked u;
818 bool do_update = false;
821 ret = check_key_has_snapshot(trans, iter, k);
827 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
831 if (!bkey_is_inode(k.k))
834 BUG_ON(bch2_inode_unpack(k, &u));
837 !(u.bi_flags & (BCH_INODE_i_size_dirty|
838 BCH_INODE_i_sectors_dirty|
839 BCH_INODE_unlinked)))
842 if (prev->bi_inum != u.bi_inum)
845 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
846 inode_d_type(prev) != inode_d_type(&u),
847 c, inode_snapshot_mismatch,
848 "inodes in different snapshots don't match")) {
849 bch_err(c, "repair not implemented yet");
850 return -BCH_ERR_fsck_repair_unimplemented;
853 if ((u.bi_flags & (BCH_INODE_i_size_dirty|BCH_INODE_unlinked)) &&
854 bch2_key_has_snapshot_overwrites(trans, BTREE_ID_inodes, k.k->p)) {
855 struct bpos new_min_pos;
857 ret = bch2_propagate_key_to_snapshot_leaves(trans, iter->btree_id, k, &new_min_pos);
861 u.bi_flags &= ~BCH_INODE_i_size_dirty|BCH_INODE_unlinked;
863 ret = __write_inode(trans, &u, iter->pos.snapshot);
864 bch_err_msg(c, ret, "in fsck updating inode");
868 if (!bpos_eq(new_min_pos, POS_MIN))
869 bch2_btree_iter_set_pos(iter, bpos_predecessor(new_min_pos));
873 if (u.bi_flags & BCH_INODE_unlinked &&
874 c->sb.version >= bcachefs_metadata_version_deleted_inodes) {
875 ret = check_inode_deleted_list(trans, k.k->p);
879 fsck_err_on(ret, c, unlinked_inode_not_on_deleted_list,
880 "inode %llu:%u unlinked, but not on deleted list",
881 u.bi_inum, k.k->p.snapshot);
885 if (u.bi_flags & BCH_INODE_unlinked &&
887 fsck_err(c, inode_unlinked_but_clean,
888 "filesystem marked clean, but inode %llu unlinked",
890 ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
891 bch_err_msg(c, ret, "in fsck deleting inode");
895 if (u.bi_flags & BCH_INODE_i_size_dirty &&
897 fsck_err(c, inode_i_size_dirty_but_clean,
898 "filesystem marked clean, but inode %llu has i_size dirty",
900 bch_verbose(c, "truncating inode %llu", u.bi_inum);
903 * XXX: need to truncate partial blocks too here - or ideally
904 * just switch units to bytes and that issue goes away
906 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
907 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
909 POS(u.bi_inum, U64_MAX),
911 bch_err_msg(c, ret, "in fsck truncating inode");
916 * We truncated without our normal sector accounting hook, just
917 * make sure we recalculate it:
919 u.bi_flags |= BCH_INODE_i_sectors_dirty;
921 u.bi_flags &= ~BCH_INODE_i_size_dirty;
925 if (u.bi_flags & BCH_INODE_i_sectors_dirty &&
927 fsck_err(c, inode_i_sectors_dirty_but_clean,
928 "filesystem marked clean, but inode %llu has i_sectors dirty",
932 bch_verbose(c, "recounting sectors for inode %llu",
935 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
937 bch_err_msg(c, sectors, "in fsck recounting inode sectors");
941 u.bi_sectors = sectors;
942 u.bi_flags &= ~BCH_INODE_i_sectors_dirty;
946 if (u.bi_flags & BCH_INODE_backptr_untrusted) {
949 u.bi_flags &= ~BCH_INODE_backptr_untrusted;
954 ret = __write_inode(trans, &u, iter->pos.snapshot);
955 bch_err_msg(c, ret, "in fsck updating inode");
965 int bch2_check_inodes(struct bch_fs *c)
967 bool full = c->opts.fsck;
968 struct bch_inode_unpacked prev = { 0 };
969 struct snapshots_seen s;
971 snapshots_seen_init(&s);
973 int ret = bch2_trans_run(c,
974 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
976 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
977 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
978 check_inode(trans, &iter, k, &prev, &s, full)));
980 snapshots_seen_exit(&s);
985 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
986 struct btree_iter *iter,
989 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
992 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
993 struct bkey_s_c_dirent d)
995 return inode->bi_dir == d.k->p.inode &&
996 inode->bi_dir_offset == d.k->p.offset;
999 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1000 struct bch_inode_unpacked *inode)
1002 return d.v->d_type == DT_SUBVOL
1003 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1004 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1007 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1009 struct bch_fs *c = trans->c;
1010 u32 restart_count = trans->restart_count;
1014 darray_for_each(w->inodes, i) {
1015 if (i->inode.bi_sectors == i->count)
1018 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1020 if (w->recalculate_sums)
1023 if (i->count != count2) {
1024 bch_err(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1025 w->last_pos.inode, i->snapshot, i->count, count2);
1026 return -BCH_ERR_internal_fsck_err;
1029 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_sectors_dirty),
1030 c, inode_i_sectors_wrong,
1031 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1032 w->last_pos.inode, i->snapshot,
1033 i->inode.bi_sectors, i->count)) {
1034 i->inode.bi_sectors = i->count;
1035 ret = fsck_write_inode(trans, &i->inode, i->snapshot);
1042 return ret ?: trans_was_restarted(trans, restart_count);
1048 struct snapshots_seen seen;
1051 struct extent_ends {
1052 struct bpos last_pos;
1053 DARRAY(struct extent_end) e;
1056 static void extent_ends_reset(struct extent_ends *extent_ends)
1058 darray_for_each(extent_ends->e, i)
1059 snapshots_seen_exit(&i->seen);
1060 extent_ends->e.nr = 0;
1063 static void extent_ends_exit(struct extent_ends *extent_ends)
1065 extent_ends_reset(extent_ends);
1066 darray_exit(&extent_ends->e);
1069 static void extent_ends_init(struct extent_ends *extent_ends)
1071 memset(extent_ends, 0, sizeof(*extent_ends));
1074 static int extent_ends_at(struct bch_fs *c,
1075 struct extent_ends *extent_ends,
1076 struct snapshots_seen *seen,
1079 struct extent_end *i, n = (struct extent_end) {
1080 .offset = k.k->p.offset,
1081 .snapshot = k.k->p.snapshot,
1085 n.seen.ids.data = kmemdup(seen->ids.data,
1086 sizeof(seen->ids.data[0]) * seen->ids.size,
1088 if (!n.seen.ids.data)
1089 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1091 __darray_for_each(extent_ends->e, i) {
1092 if (i->snapshot == k.k->p.snapshot) {
1093 snapshots_seen_exit(&i->seen);
1098 if (i->snapshot >= k.k->p.snapshot)
1102 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1105 static int overlapping_extents_found(struct btree_trans *trans,
1106 enum btree_id btree,
1107 struct bpos pos1, struct snapshots_seen *pos1_seen,
1110 struct extent_end *extent_end)
1112 struct bch_fs *c = trans->c;
1113 struct printbuf buf = PRINTBUF;
1114 struct btree_iter iter1, iter2 = { NULL };
1115 struct bkey_s_c k1, k2;
1118 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1120 bch2_trans_iter_init(trans, &iter1, btree, pos1,
1121 BTREE_ITER_ALL_SNAPSHOTS|
1122 BTREE_ITER_NOT_EXTENTS);
1123 k1 = bch2_btree_iter_peek_upto(&iter1, POS(pos1.inode, U64_MAX));
1128 prt_str(&buf, "\n ");
1129 bch2_bkey_val_to_text(&buf, c, k1);
1131 if (!bpos_eq(pos1, k1.k->p)) {
1132 prt_str(&buf, "\n wanted\n ");
1133 bch2_bpos_to_text(&buf, pos1);
1134 prt_str(&buf, "\n ");
1135 bch2_bkey_to_text(&buf, &pos2);
1137 bch_err(c, "%s: error finding first overlapping extent when repairing, got%s",
1139 ret = -BCH_ERR_internal_fsck_err;
1143 bch2_trans_copy_iter(&iter2, &iter1);
1146 bch2_btree_iter_advance(&iter2);
1148 k2 = bch2_btree_iter_peek_upto(&iter2, POS(pos1.inode, U64_MAX));
1153 if (bpos_ge(k2.k->p, pos2.p))
1157 prt_str(&buf, "\n ");
1158 bch2_bkey_val_to_text(&buf, c, k2);
1160 if (bpos_gt(k2.k->p, pos2.p) ||
1161 pos2.size != k2.k->size) {
1162 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1164 ret = -BCH_ERR_internal_fsck_err;
1168 prt_printf(&buf, "\n overwriting %s extent",
1169 pos1.snapshot >= pos2.p.snapshot ? "first" : "second");
1171 if (fsck_err(c, extent_overlapping,
1172 "overlapping extents%s", buf.buf)) {
1173 struct btree_iter *old_iter = &iter1;
1174 struct disk_reservation res = { 0 };
1176 if (pos1.snapshot < pos2.p.snapshot) {
1181 trans->extra_disk_res += bch2_bkey_sectors_compressed(k2);
1183 ret = bch2_trans_update_extent_overwrite(trans, old_iter,
1184 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE,
1186 bch2_trans_commit(trans, &res, NULL, BCH_TRANS_COMMIT_no_enospc);
1187 bch2_disk_reservation_put(c, &res);
1194 if (pos1.snapshot == pos2.p.snapshot) {
1196 * We overwrote the first extent, and did the overwrite
1197 * in the same snapshot:
1199 extent_end->offset = bkey_start_offset(&pos2);
1200 } else if (pos1.snapshot > pos2.p.snapshot) {
1202 * We overwrote the first extent in pos2's snapshot:
1204 ret = snapshots_seen_add_inorder(c, pos1_seen, pos2.p.snapshot);
1207 * We overwrote the second extent - restart
1208 * check_extent() from the top:
1210 ret = -BCH_ERR_transaction_restart_nested;
1215 bch2_trans_iter_exit(trans, &iter2);
1216 bch2_trans_iter_exit(trans, &iter1);
1217 printbuf_exit(&buf);
1221 static int check_overlapping_extents(struct btree_trans *trans,
1222 struct snapshots_seen *seen,
1223 struct extent_ends *extent_ends,
1226 struct btree_iter *iter,
1229 struct bch_fs *c = trans->c;
1232 /* transaction restart, running again */
1233 if (bpos_eq(extent_ends->last_pos, k.k->p))
1236 if (extent_ends->last_pos.inode != k.k->p.inode)
1237 extent_ends_reset(extent_ends);
1239 darray_for_each(extent_ends->e, i) {
1240 if (i->offset <= bkey_start_offset(k.k))
1243 if (!ref_visible2(c,
1244 k.k->p.snapshot, seen,
1245 i->snapshot, &i->seen))
1248 ret = overlapping_extents_found(trans, iter->btree_id,
1249 SPOS(iter->pos.inode,
1258 ret = extent_ends_at(c, extent_ends, seen, k);
1262 extent_ends->last_pos = k.k->p;
1267 static int check_extent_overbig(struct btree_trans *trans, struct btree_iter *iter,
1270 struct bch_fs *c = trans->c;
1271 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1272 struct bch_extent_crc_unpacked crc;
1273 const union bch_extent_entry *i;
1274 unsigned encoded_extent_max_sectors = c->opts.encoded_extent_max >> 9;
1276 bkey_for_each_crc(k.k, ptrs, crc, i)
1277 if (crc_is_encoded(crc) &&
1278 crc.uncompressed_size > encoded_extent_max_sectors) {
1279 struct printbuf buf = PRINTBUF;
1281 bch2_bkey_val_to_text(&buf, c, k);
1282 bch_err(c, "overbig encoded extent, please report this:\n %s", buf.buf);
1283 printbuf_exit(&buf);
1289 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1291 struct inode_walker *inode,
1292 struct snapshots_seen *s,
1293 struct extent_ends *extent_ends)
1295 struct bch_fs *c = trans->c;
1296 struct inode_walker_entry *i;
1297 struct printbuf buf = PRINTBUF;
1298 struct bpos equiv = k.k->p;
1301 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1303 ret = check_key_has_snapshot(trans, iter, k);
1305 ret = ret < 0 ? ret : 0;
1309 if (inode->last_pos.inode != k.k->p.inode) {
1310 ret = check_i_sectors(trans, inode);
1315 i = walk_inode(trans, inode, equiv, k.k->type == KEY_TYPE_whiteout);
1316 ret = PTR_ERR_OR_ZERO(i);
1320 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1324 if (k.k->type != KEY_TYPE_whiteout) {
1325 if (fsck_err_on(!i, c, extent_in_missing_inode,
1326 "extent in missing inode:\n %s",
1327 (printbuf_reset(&buf),
1328 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1331 if (fsck_err_on(i &&
1332 !S_ISREG(i->inode.bi_mode) &&
1333 !S_ISLNK(i->inode.bi_mode),
1334 c, extent_in_non_reg_inode,
1335 "extent in non regular inode mode %o:\n %s",
1337 (printbuf_reset(&buf),
1338 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1341 ret = check_overlapping_extents(trans, s, extent_ends, k,
1342 equiv.snapshot, iter,
1343 &inode->recalculate_sums);
1349 * Check inodes in reverse order, from oldest snapshots to newest,
1350 * starting from the inode that matches this extent's snapshot. If we
1351 * didn't have one, iterate over all inodes:
1354 i = inode->inodes.data + inode->inodes.nr - 1;
1357 inode->inodes.data && i >= inode->inodes.data;
1359 if (i->snapshot > equiv.snapshot ||
1360 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1363 if (k.k->type != KEY_TYPE_whiteout) {
1364 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_size_dirty) &&
1365 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1366 !bkey_extent_is_reservation(k),
1367 c, extent_past_end_of_inode,
1368 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1369 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1370 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1371 struct btree_iter iter2;
1373 bch2_trans_copy_iter(&iter2, iter);
1374 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1375 ret = bch2_btree_iter_traverse(&iter2) ?:
1376 bch2_btree_delete_at(trans, &iter2,
1377 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1378 bch2_trans_iter_exit(trans, &iter2);
1382 iter->k.type = KEY_TYPE_whiteout;
1385 if (bkey_extent_is_allocation(k.k))
1386 i->count += k.k->size;
1389 i->seen_this_pos = true;
1394 printbuf_exit(&buf);
1398 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1403 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1404 * that i_size an i_sectors are consistent
1406 int bch2_check_extents(struct bch_fs *c)
1408 struct inode_walker w = inode_walker_init();
1409 struct snapshots_seen s;
1410 struct extent_ends extent_ends;
1411 struct disk_reservation res = { 0 };
1413 snapshots_seen_init(&s);
1414 extent_ends_init(&extent_ends);
1416 int ret = bch2_trans_run(c,
1417 for_each_btree_key_commit(trans, iter, BTREE_ID_extents,
1418 POS(BCACHEFS_ROOT_INO, 0),
1419 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1421 BCH_TRANS_COMMIT_no_enospc, ({
1422 bch2_disk_reservation_put(c, &res);
1423 check_extent(trans, &iter, k, &w, &s, &extent_ends) ?:
1424 check_extent_overbig(trans, &iter, k);
1426 check_i_sectors(trans, &w));
1428 bch2_disk_reservation_put(c, &res);
1429 extent_ends_exit(&extent_ends);
1430 inode_walker_exit(&w);
1431 snapshots_seen_exit(&s);
1437 int bch2_check_indirect_extents(struct bch_fs *c)
1439 struct disk_reservation res = { 0 };
1441 int ret = bch2_trans_run(c,
1442 for_each_btree_key_commit(trans, iter, BTREE_ID_reflink,
1444 BTREE_ITER_PREFETCH, k,
1446 BCH_TRANS_COMMIT_no_enospc, ({
1447 bch2_disk_reservation_put(c, &res);
1448 check_extent_overbig(trans, &iter, k);
1451 bch2_disk_reservation_put(c, &res);
1456 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1458 struct bch_fs *c = trans->c;
1459 u32 restart_count = trans->restart_count;
1463 darray_for_each(w->inodes, i) {
1464 if (i->inode.bi_nlink == i->count)
1467 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1471 if (i->count != count2) {
1472 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1475 if (i->inode.bi_nlink == i->count)
1479 if (fsck_err_on(i->inode.bi_nlink != i->count,
1480 c, inode_dir_wrong_nlink,
1481 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1482 w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1483 i->inode.bi_nlink = i->count;
1484 ret = fsck_write_inode(trans, &i->inode, i->snapshot);
1491 return ret ?: trans_was_restarted(trans, restart_count);
1494 static int check_dirent_target(struct btree_trans *trans,
1495 struct btree_iter *iter,
1496 struct bkey_s_c_dirent d,
1497 struct bch_inode_unpacked *target,
1498 u32 target_snapshot)
1500 struct bch_fs *c = trans->c;
1501 struct bkey_i_dirent *n;
1502 struct printbuf buf = PRINTBUF;
1503 struct btree_iter bp_iter = { NULL };
1506 if (!target->bi_dir &&
1507 !target->bi_dir_offset) {
1508 target->bi_dir = d.k->p.inode;
1509 target->bi_dir_offset = d.k->p.offset;
1511 ret = __write_inode(trans, target, target_snapshot);
1516 if (!inode_points_to_dirent(target, d)) {
1517 struct bkey_s_c_dirent bp_dirent = dirent_get_by_pos(trans, &bp_iter,
1518 SPOS(target->bi_dir, target->bi_dir_offset, target_snapshot));
1519 ret = bkey_err(bp_dirent);
1520 if (ret && !bch2_err_matches(ret, ENOENT))
1523 bool backpointer_exists = !ret;
1526 bch2_bkey_val_to_text(&buf, c, d.s_c);
1528 if (backpointer_exists)
1529 bch2_bkey_val_to_text(&buf, c, bp_dirent.s_c);
1531 if (fsck_err_on(S_ISDIR(target->bi_mode) && backpointer_exists,
1532 c, inode_dir_multiple_links,
1533 "directory %llu:%u with multiple links\n%s",
1534 target->bi_inum, target_snapshot, buf.buf)) {
1535 ret = __remove_dirent(trans, d.k->p);
1540 * hardlinked file with nlink 0:
1541 * We're just adjusting nlink here so check_nlinks() will pick
1542 * it up, it ignores inodes with nlink 0
1544 if (fsck_err_on(backpointer_exists && !target->bi_nlink,
1545 c, inode_multiple_links_but_nlink_0,
1546 "inode %llu:%u type %s has multiple links but i_nlink 0\n%s",
1547 target->bi_inum, target_snapshot, bch2_d_types[d.v->d_type], buf.buf)) {
1549 target->bi_flags &= ~BCH_INODE_unlinked;
1551 ret = __write_inode(trans, target, target_snapshot);
1556 if (fsck_err_on(!backpointer_exists,
1557 c, inode_wrong_backpointer,
1558 "inode %llu:%u has wrong backpointer:\n"
1560 "should be %llu:%llu",
1561 target->bi_inum, target_snapshot,
1563 target->bi_dir_offset,
1566 target->bi_dir = d.k->p.inode;
1567 target->bi_dir_offset = d.k->p.offset;
1569 ret = __write_inode(trans, target, target_snapshot);
1575 if (fsck_err_on(d.v->d_type != inode_d_type(target),
1576 c, dirent_d_type_wrong,
1577 "incorrect d_type: got %s, should be %s:\n%s",
1578 bch2_d_type_str(d.v->d_type),
1579 bch2_d_type_str(inode_d_type(target)),
1580 (printbuf_reset(&buf),
1581 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1582 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1583 ret = PTR_ERR_OR_ZERO(n);
1587 bkey_reassemble(&n->k_i, d.s_c);
1588 n->v.d_type = inode_d_type(target);
1590 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1594 d = dirent_i_to_s_c(n);
1597 if (d.v->d_type == DT_SUBVOL &&
1598 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1599 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1600 fsck_err(c, dirent_d_parent_subvol_wrong,
1601 "dirent has wrong d_parent_subvol field: got %u, should be %u",
1602 le32_to_cpu(d.v->d_parent_subvol),
1603 target->bi_parent_subvol))) {
1604 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1605 ret = PTR_ERR_OR_ZERO(n);
1609 bkey_reassemble(&n->k_i, d.s_c);
1610 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1612 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1616 d = dirent_i_to_s_c(n);
1621 bch2_trans_iter_exit(trans, &bp_iter);
1622 printbuf_exit(&buf);
1627 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1629 struct bch_hash_info *hash_info,
1630 struct inode_walker *dir,
1631 struct inode_walker *target,
1632 struct snapshots_seen *s)
1634 struct bch_fs *c = trans->c;
1635 struct bkey_s_c_dirent d;
1636 struct inode_walker_entry *i;
1637 struct printbuf buf = PRINTBUF;
1641 ret = check_key_has_snapshot(trans, iter, k);
1643 ret = ret < 0 ? ret : 0;
1648 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1650 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1654 if (k.k->type == KEY_TYPE_whiteout)
1657 if (dir->last_pos.inode != k.k->p.inode) {
1658 ret = check_subdir_count(trans, dir);
1663 BUG_ON(!btree_iter_path(trans, iter)->should_be_locked);
1665 i = walk_inode(trans, dir, equiv, k.k->type == KEY_TYPE_whiteout);
1666 ret = PTR_ERR_OR_ZERO(i);
1670 if (dir->first_this_inode && dir->inodes.nr)
1671 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1672 dir->first_this_inode = false;
1674 if (fsck_err_on(!i, c, dirent_in_missing_dir_inode,
1675 "dirent in nonexisting directory:\n%s",
1676 (printbuf_reset(&buf),
1677 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1678 ret = bch2_btree_delete_at(trans, iter,
1679 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1686 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode),
1687 c, dirent_in_non_dir_inode,
1688 "dirent in non directory inode type %s:\n%s",
1689 bch2_d_type_str(inode_d_type(&i->inode)),
1690 (printbuf_reset(&buf),
1691 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1692 ret = bch2_btree_delete_at(trans, iter, 0);
1696 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1700 /* dirent has been deleted */
1705 if (k.k->type != KEY_TYPE_dirent)
1708 d = bkey_s_c_to_dirent(k);
1710 if (d.v->d_type == DT_SUBVOL) {
1711 struct bch_inode_unpacked subvol_root;
1712 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1713 u32 target_snapshot;
1716 ret = subvol_lookup(trans, target_subvol,
1717 &target_snapshot, &target_inum);
1718 if (ret && !bch2_err_matches(ret, ENOENT))
1721 if (fsck_err_on(ret, c, dirent_to_missing_subvol,
1722 "dirent points to missing subvolume %u",
1723 le32_to_cpu(d.v->d_child_subvol))) {
1724 ret = __remove_dirent(trans, d.k->p);
1728 ret = lookup_inode(trans, target_inum,
1729 &subvol_root, &target_snapshot);
1730 if (ret && !bch2_err_matches(ret, ENOENT))
1733 if (fsck_err_on(ret, c, subvol_to_missing_root,
1734 "subvolume %u points to missing subvolume root %llu",
1737 bch_err(c, "repair not implemented yet");
1742 if (fsck_err_on(subvol_root.bi_subvol != target_subvol,
1743 c, subvol_root_wrong_bi_subvol,
1744 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1746 subvol_root.bi_subvol, target_subvol)) {
1747 subvol_root.bi_subvol = target_subvol;
1748 ret = __write_inode(trans, &subvol_root, target_snapshot);
1753 ret = check_dirent_target(trans, iter, d, &subvol_root,
1758 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1762 if (fsck_err_on(!target->inodes.nr,
1763 c, dirent_to_missing_inode,
1764 "dirent points to missing inode: (equiv %u)\n%s",
1766 (printbuf_reset(&buf),
1767 bch2_bkey_val_to_text(&buf, c, k),
1769 ret = __remove_dirent(trans, d.k->p);
1774 darray_for_each(target->inodes, i) {
1775 ret = check_dirent_target(trans, iter, d,
1776 &i->inode, i->snapshot);
1782 if (d.v->d_type == DT_DIR)
1783 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1789 printbuf_exit(&buf);
1795 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1798 int bch2_check_dirents(struct bch_fs *c)
1800 struct inode_walker dir = inode_walker_init();
1801 struct inode_walker target = inode_walker_init();
1802 struct snapshots_seen s;
1803 struct bch_hash_info hash_info;
1805 snapshots_seen_init(&s);
1807 int ret = bch2_trans_run(c,
1808 for_each_btree_key_commit(trans, iter, BTREE_ID_dirents,
1809 POS(BCACHEFS_ROOT_INO, 0),
1810 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1813 BCH_TRANS_COMMIT_no_enospc,
1814 check_dirent(trans, &iter, k, &hash_info, &dir, &target, &s)));
1816 snapshots_seen_exit(&s);
1817 inode_walker_exit(&dir);
1818 inode_walker_exit(&target);
1823 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1825 struct bch_hash_info *hash_info,
1826 struct inode_walker *inode)
1828 struct bch_fs *c = trans->c;
1829 struct inode_walker_entry *i;
1832 ret = check_key_has_snapshot(trans, iter, k);
1836 i = walk_inode(trans, inode, k.k->p, k.k->type == KEY_TYPE_whiteout);
1837 ret = PTR_ERR_OR_ZERO(i);
1841 if (inode->first_this_inode && inode->inodes.nr)
1842 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1843 inode->first_this_inode = false;
1845 if (fsck_err_on(!i, c, xattr_in_missing_inode,
1846 "xattr for missing inode %llu",
1848 return bch2_btree_delete_at(trans, iter, 0);
1853 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1860 * Walk xattrs: verify that they all have a corresponding inode
1862 int bch2_check_xattrs(struct bch_fs *c)
1864 struct inode_walker inode = inode_walker_init();
1865 struct bch_hash_info hash_info;
1868 ret = bch2_trans_run(c,
1869 for_each_btree_key_commit(trans, iter, BTREE_ID_xattrs,
1870 POS(BCACHEFS_ROOT_INO, 0),
1871 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1874 BCH_TRANS_COMMIT_no_enospc,
1875 check_xattr(trans, &iter, k, &hash_info, &inode)));
1880 static int check_root_trans(struct btree_trans *trans)
1882 struct bch_fs *c = trans->c;
1883 struct bch_inode_unpacked root_inode;
1888 ret = subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1889 if (ret && !bch2_err_matches(ret, ENOENT))
1892 if (mustfix_fsck_err_on(ret, c, root_subvol_missing,
1893 "root subvol missing")) {
1894 struct bkey_i_subvolume root_subvol;
1897 inum = BCACHEFS_ROOT_INO;
1899 bkey_subvolume_init(&root_subvol.k_i);
1900 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1901 root_subvol.v.flags = 0;
1902 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1903 root_subvol.v.inode = cpu_to_le64(inum);
1904 ret = bch2_btree_insert_trans(trans, BTREE_ID_subvolumes, &root_subvol.k_i, 0);
1905 bch_err_msg(c, ret, "writing root subvol");
1910 ret = lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1911 if (ret && !bch2_err_matches(ret, ENOENT))
1914 if (mustfix_fsck_err_on(ret, c, root_dir_missing,
1915 "root directory missing") ||
1916 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode),
1917 c, root_inode_not_dir,
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);
1924 bch_err_msg(c, ret, "writing root inode");
1931 /* Get root directory, create if it doesn't exist: */
1932 int bch2_check_root(struct bch_fs *c)
1934 int ret = bch2_trans_do(c, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1935 check_root_trans(trans));
1940 struct pathbuf_entry {
1945 typedef DARRAY(struct pathbuf_entry) pathbuf;
1947 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1949 darray_for_each(*p, i)
1950 if (i->inum == inum &&
1951 i->snapshot == snapshot)
1956 static int path_down(struct bch_fs *c, pathbuf *p,
1957 u64 inum, u32 snapshot)
1959 int ret = darray_push(p, ((struct pathbuf_entry) {
1961 .snapshot = snapshot,
1965 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1971 * Check that a given inode is reachable from the root:
1973 * XXX: we should also be verifying that inodes are in the right subvolumes
1975 static int check_path(struct btree_trans *trans,
1977 struct bch_inode_unpacked *inode,
1980 struct bch_fs *c = trans->c;
1983 snapshot = bch2_snapshot_equiv(c, snapshot);
1986 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1987 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1988 struct btree_iter dirent_iter;
1989 struct bkey_s_c_dirent d;
1990 u32 parent_snapshot = snapshot;
1992 if (inode->bi_subvol) {
1995 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1996 &parent_snapshot, &inum);
2001 d = dirent_get_by_pos(trans, &dirent_iter,
2002 SPOS(inode->bi_dir, inode->bi_dir_offset,
2004 ret = bkey_err(d.s_c);
2005 if (ret && !bch2_err_matches(ret, ENOENT))
2008 if (!ret && !dirent_points_to_inode(d, inode)) {
2009 bch2_trans_iter_exit(trans, &dirent_iter);
2010 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
2013 if (bch2_err_matches(ret, ENOENT)) {
2014 if (fsck_err(c, inode_unreachable,
2015 "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2016 inode->bi_inum, snapshot,
2017 bch2_d_type_str(inode_d_type(inode)),
2020 inode->bi_dir_offset))
2021 ret = reattach_inode(trans, inode, snapshot);
2025 bch2_trans_iter_exit(trans, &dirent_iter);
2027 if (!S_ISDIR(inode->bi_mode))
2030 ret = path_down(c, p, inode->bi_inum, snapshot);
2032 bch_err(c, "memory allocation failure");
2036 snapshot = parent_snapshot;
2038 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2040 /* Should have been caught in dirents pass */
2041 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart))
2042 bch_err(c, "error looking up parent directory: %i", ret);
2046 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2047 /* XXX print path */
2048 bch_err(c, "directory structure loop");
2050 darray_for_each(*p, i)
2051 pr_err("%llu:%u", i->inum, i->snapshot);
2052 pr_err("%llu:%u", inode->bi_inum, snapshot);
2054 if (!fsck_err(c, dir_loop, "directory structure loop"))
2057 ret = remove_backpointer(trans, inode);
2058 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
2059 bch_err_msg(c, ret, "removing dirent");
2063 ret = reattach_inode(trans, inode, snapshot);
2064 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
2065 bch_err_msg(c, ret, "reattaching inode %llu", inode->bi_inum);
2075 * Check for unreachable inodes, as well as loops in the directory structure:
2076 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2079 int bch2_check_directory_structure(struct bch_fs *c)
2081 struct bch_inode_unpacked u;
2082 pathbuf path = { 0, };
2085 ret = bch2_trans_run(c,
2086 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes, POS_MIN,
2088 BTREE_ITER_PREFETCH|
2089 BTREE_ITER_ALL_SNAPSHOTS, k,
2090 NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
2091 if (!bkey_is_inode(k.k))
2094 BUG_ON(bch2_inode_unpack(k, &u));
2096 if (u.bi_flags & BCH_INODE_unlinked)
2099 check_path(trans, &path, &u, iter.pos.snapshot);
2107 struct nlink_table {
2118 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2119 u64 inum, u32 snapshot)
2121 if (t->nr == t->size) {
2122 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2123 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2126 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2128 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2132 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2140 t->d[t->nr++] = (struct nlink) {
2142 .snapshot = snapshot,
2148 static int nlink_cmp(const void *_l, const void *_r)
2150 const struct nlink *l = _l;
2151 const struct nlink *r = _r;
2153 return cmp_int(l->inum, r->inum);
2156 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2157 struct nlink_table *links,
2158 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2160 struct nlink *link, key = {
2161 .inum = inum, .snapshot = U32_MAX,
2164 if (inum < range_start || inum >= range_end)
2167 link = __inline_bsearch(&key, links->d, links->nr,
2168 sizeof(links->d[0]), nlink_cmp);
2172 while (link > links->d && link[0].inum == link[-1].inum)
2175 for (; link < links->d + links->nr && link->inum == inum; link++)
2176 if (ref_visible(c, s, snapshot, link->snapshot)) {
2178 if (link->snapshot >= snapshot)
2184 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2185 struct nlink_table *t,
2186 u64 start, u64 *end)
2188 int ret = bch2_trans_run(c,
2189 for_each_btree_key(trans, iter, BTREE_ID_inodes,
2192 BTREE_ITER_PREFETCH|
2193 BTREE_ITER_ALL_SNAPSHOTS, k, ({
2194 if (!bkey_is_inode(k.k))
2197 /* Should never fail, checked by bch2_inode_invalid: */
2198 struct bch_inode_unpacked u;
2199 BUG_ON(bch2_inode_unpack(k, &u));
2202 * Backpointer and directory structure checks are sufficient for
2203 * directories, since they can't have hardlinks:
2205 if (S_ISDIR(u.bi_mode))
2211 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2213 *end = k.k->p.offset;
2225 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2226 u64 range_start, u64 range_end)
2228 struct snapshots_seen s;
2230 snapshots_seen_init(&s);
2232 int ret = bch2_trans_run(c,
2233 for_each_btree_key(trans, iter, BTREE_ID_dirents, POS_MIN,
2235 BTREE_ITER_PREFETCH|
2236 BTREE_ITER_ALL_SNAPSHOTS, k, ({
2237 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2241 if (k.k->type == KEY_TYPE_dirent) {
2242 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
2244 if (d.v->d_type != DT_DIR &&
2245 d.v->d_type != DT_SUBVOL)
2246 inc_link(c, &s, links, range_start, range_end,
2247 le64_to_cpu(d.v->d_inum),
2248 bch2_snapshot_equiv(c, d.k->p.snapshot));
2253 snapshots_seen_exit(&s);
2259 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2261 struct nlink_table *links,
2262 size_t *idx, u64 range_end)
2264 struct bch_fs *c = trans->c;
2265 struct bch_inode_unpacked u;
2266 struct nlink *link = &links->d[*idx];
2269 if (k.k->p.offset >= range_end)
2272 if (!bkey_is_inode(k.k))
2275 BUG_ON(bch2_inode_unpack(k, &u));
2277 if (S_ISDIR(u.bi_mode))
2283 while ((cmp_int(link->inum, k.k->p.offset) ?:
2284 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2285 BUG_ON(*idx == links->nr);
2286 link = &links->d[++*idx];
2289 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count,
2290 c, inode_wrong_nlink,
2291 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2292 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2293 bch2_inode_nlink_get(&u), link->count)) {
2294 bch2_inode_nlink_set(&u, link->count);
2295 ret = __write_inode(trans, &u, k.k->p.snapshot);
2302 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2303 struct nlink_table *links,
2304 u64 range_start, u64 range_end)
2308 int ret = bch2_trans_run(c,
2309 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
2310 POS(0, range_start),
2311 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2312 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2313 check_nlinks_update_inode(trans, &iter, k, links, &idx, range_end)));
2315 bch_err(c, "error in fsck walking inodes: %s", bch2_err_str(ret));
2322 int bch2_check_nlinks(struct bch_fs *c)
2324 struct nlink_table links = { 0 };
2325 u64 this_iter_range_start, next_iter_range_start = 0;
2329 this_iter_range_start = next_iter_range_start;
2330 next_iter_range_start = U64_MAX;
2332 ret = check_nlinks_find_hardlinks(c, &links,
2333 this_iter_range_start,
2334 &next_iter_range_start);
2336 ret = check_nlinks_walk_dirents(c, &links,
2337 this_iter_range_start,
2338 next_iter_range_start);
2342 ret = check_nlinks_update_hardlinks(c, &links,
2343 this_iter_range_start,
2344 next_iter_range_start);
2349 } while (next_iter_range_start != U64_MAX);
2356 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2359 struct bkey_s_c_reflink_p p;
2360 struct bkey_i_reflink_p *u;
2362 if (k.k->type != KEY_TYPE_reflink_p)
2365 p = bkey_s_c_to_reflink_p(k);
2367 if (!p.v->front_pad && !p.v->back_pad)
2370 u = bch2_trans_kmalloc(trans, sizeof(*u));
2371 int ret = PTR_ERR_OR_ZERO(u);
2375 bkey_reassemble(&u->k_i, k);
2379 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2382 int bch2_fix_reflink_p(struct bch_fs *c)
2384 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2387 int ret = bch2_trans_run(c,
2388 for_each_btree_key_commit(trans, iter,
2389 BTREE_ID_extents, POS_MIN,
2390 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2391 BTREE_ITER_ALL_SNAPSHOTS, k,
2392 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2393 fix_reflink_p_key(trans, &iter, k)));