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 bch2_trans_iter_exit(trans, &iter);
126 static int lookup_dirent_in_snapshot(struct btree_trans *trans,
127 struct bch_hash_info hash_info,
128 subvol_inum dir, struct qstr *name,
129 u64 *target, unsigned *type, u32 snapshot)
131 struct btree_iter iter;
132 struct bkey_s_c_dirent d;
133 int ret = bch2_hash_lookup_in_snapshot(trans, &iter, bch2_dirent_hash_desc,
134 &hash_info, dir, name, 0, snapshot);
138 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
139 *target = le64_to_cpu(d.v->d_inum);
141 bch2_trans_iter_exit(trans, &iter);
145 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
147 struct bch_fs *c = trans->c;
148 struct btree_iter iter;
149 struct bch_inode_unpacked dir_inode;
150 struct bch_hash_info dir_hash_info;
153 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
157 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
159 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
161 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
162 &dir_hash_info, &iter,
163 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
164 bch2_trans_iter_exit(trans, &iter);
170 /* Get lost+found, create if it doesn't exist: */
171 static int lookup_lostfound(struct btree_trans *trans, u32 snapshot,
172 struct bch_inode_unpacked *lostfound)
174 struct bch_fs *c = trans->c;
175 struct qstr lostfound_str = QSTR("lost+found");
180 struct bch_snapshot_tree st;
181 ret = bch2_snapshot_tree_lookup(trans,
182 bch2_snapshot_tree(c, snapshot), &st);
186 subvol_inum root_inum = { .subvol = le32_to_cpu(st.master_subvol) };
189 ret = subvol_lookup(trans, le32_to_cpu(st.master_subvol),
190 &subvol_snapshot, &root_inum.inum);
191 bch_err_msg(c, ret, "looking up root subvol");
195 struct bch_inode_unpacked root_inode;
196 struct bch_hash_info root_hash_info;
197 u32 root_inode_snapshot = snapshot;
198 ret = lookup_inode(trans, root_inum.inum, &root_inode, &root_inode_snapshot);
199 bch_err_msg(c, ret, "looking up root inode");
203 root_hash_info = bch2_hash_info_init(c, &root_inode);
205 ret = lookup_dirent_in_snapshot(trans, root_hash_info, root_inum,
206 &lostfound_str, &inum, &d_type, snapshot);
207 if (bch2_err_matches(ret, ENOENT))
208 goto create_lostfound;
214 if (d_type != DT_DIR) {
215 bch_err(c, "error looking up lost+found: not a directory");
216 return -BCH_ERR_ENOENT_not_directory;
220 * The bch2_check_dirents pass has already run, dangling dirents
221 * shouldn't exist here:
223 ret = lookup_inode(trans, inum, lostfound, &snapshot);
224 bch_err_msg(c, ret, "looking up lost+found %llu:%u in (root inode %llu, snapshot root %u)",
225 inum, snapshot, root_inum.inum, bch2_snapshot_root(c, snapshot));
230 * XXX: we could have a nicer log message here if we had a nice way to
231 * walk backpointers to print a path
233 bch_notice(c, "creating lost+found in snapshot %u", le32_to_cpu(st.root_snapshot));
235 u64 now = bch2_current_time(c);
236 struct btree_iter lostfound_iter = { NULL };
237 u64 cpu = raw_smp_processor_id();
239 bch2_inode_init_early(c, lostfound);
240 bch2_inode_init_late(lostfound, now, 0, 0, S_IFDIR|0700, 0, &root_inode);
241 lostfound->bi_dir = root_inode.bi_inum;
243 root_inode.bi_nlink++;
245 ret = bch2_inode_create(trans, &lostfound_iter, lostfound, snapshot, cpu);
249 bch2_btree_iter_set_snapshot(&lostfound_iter, snapshot);
250 ret = bch2_btree_iter_traverse(&lostfound_iter);
254 ret = bch2_dirent_create_snapshot(trans,
255 0, root_inode.bi_inum, snapshot, &root_hash_info,
256 mode_to_type(lostfound->bi_mode),
259 &lostfound->bi_dir_offset,
260 BCH_HASH_SET_MUST_CREATE) ?:
261 bch2_inode_write_flags(trans, &lostfound_iter, lostfound,
262 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
264 bch_err_msg(c, ret, "creating lost+found");
265 bch2_trans_iter_exit(trans, &lostfound_iter);
269 static int reattach_inode(struct btree_trans *trans,
270 struct bch_inode_unpacked *inode,
273 struct bch_hash_info dir_hash;
274 struct bch_inode_unpacked lostfound;
278 u32 dirent_snapshot = inode_snapshot;
281 if (inode->bi_subvol) {
282 inode->bi_parent_subvol = BCACHEFS_ROOT_SUBVOL;
285 ret = subvol_lookup(trans, inode->bi_parent_subvol,
286 &dirent_snapshot, &root_inum);
290 snprintf(name_buf, sizeof(name_buf), "subvol-%u", inode->bi_subvol);
292 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
295 ret = lookup_lostfound(trans, dirent_snapshot, &lostfound);
299 if (S_ISDIR(inode->bi_mode)) {
300 lostfound.bi_nlink++;
302 ret = __bch2_fsck_write_inode(trans, &lostfound, U32_MAX);
307 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
309 name = (struct qstr) QSTR(name_buf);
311 ret = bch2_dirent_create_snapshot(trans,
312 inode->bi_parent_subvol, lostfound.bi_inum,
317 inode->bi_subvol ?: inode->bi_inum,
319 BCH_HASH_SET_MUST_CREATE);
323 inode->bi_dir = lostfound.bi_inum;
324 inode->bi_dir_offset = dir_offset;
326 return __bch2_fsck_write_inode(trans, inode, inode_snapshot);
329 static int remove_backpointer(struct btree_trans *trans,
330 struct bch_inode_unpacked *inode)
332 struct btree_iter iter;
333 struct bkey_s_c_dirent d;
336 d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
337 POS(inode->bi_dir, inode->bi_dir_offset), 0,
340 __remove_dirent(trans, d.k->p);
341 bch2_trans_iter_exit(trans, &iter);
345 static int reattach_subvol(struct btree_trans *trans, struct bkey_s_c_subvolume s)
347 struct bch_fs *c = trans->c;
349 struct bch_inode_unpacked inode;
350 int ret = bch2_inode_find_by_inum_trans(trans,
351 (subvol_inum) { s.k->p.offset, le64_to_cpu(s.v->inode) },
356 ret = remove_backpointer(trans, &inode);
357 bch_err_msg(c, ret, "removing dirent");
361 ret = reattach_inode(trans, &inode, le32_to_cpu(s.v->snapshot));
362 bch_err_msg(c, ret, "reattaching inode %llu", inode.bi_inum);
366 struct snapshots_seen_entry {
371 struct snapshots_seen {
373 DARRAY(struct snapshots_seen_entry) ids;
376 static inline void snapshots_seen_exit(struct snapshots_seen *s)
378 darray_exit(&s->ids);
381 static inline void snapshots_seen_init(struct snapshots_seen *s)
383 memset(s, 0, sizeof(*s));
386 static int snapshots_seen_add_inorder(struct bch_fs *c, struct snapshots_seen *s, u32 id)
388 struct snapshots_seen_entry *i, n = {
390 .equiv = bch2_snapshot_equiv(c, id),
394 __darray_for_each(s->ids, i) {
401 ret = darray_insert_item(&s->ids, i - s->ids.data, n);
403 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
408 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
409 enum btree_id btree_id, struct bpos pos)
411 struct snapshots_seen_entry n = {
413 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
417 if (!bkey_eq(s->pos, pos))
421 s->pos.snapshot = n.equiv;
423 darray_for_each(s->ids, i) {
428 * We currently don't rigorously track for snapshot cleanup
429 * needing to be run, so it shouldn't be a fsck error yet:
431 if (i->equiv == n.equiv) {
432 bch_err(c, "snapshot deletion did not finish:\n"
433 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
434 bch2_btree_id_str(btree_id),
435 pos.inode, pos.offset,
436 i->id, n.id, n.equiv);
437 set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
438 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_delete_dead_snapshots);
442 ret = darray_push(&s->ids, n);
444 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
450 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
451 * and @ancestor hasn't been overwritten in @seen
453 * @c: filesystem handle
454 * @seen: list of snapshot ids already seen at current position
455 * @id: descendent snapshot id
456 * @ancestor: ancestor snapshot id
458 * Returns: whether key in @ancestor snapshot is visible in @id snapshot
460 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
461 u32 id, u32 ancestor)
465 EBUG_ON(id > ancestor);
466 EBUG_ON(!bch2_snapshot_is_equiv(c, id));
467 EBUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
469 /* @ancestor should be the snapshot most recently added to @seen */
470 EBUG_ON(ancestor != seen->pos.snapshot);
471 EBUG_ON(ancestor != seen->ids.data[seen->ids.nr - 1].equiv);
476 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
480 * We know that @id is a descendant of @ancestor, we're checking if
481 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
482 * @ascestor and with @id as a descendent.
484 * But we already know that we're scanning IDs between @id and @ancestor
485 * numerically, since snapshot ID lists are kept sorted, so if we find
486 * an id that's an ancestor of @id we're done:
489 for (i = seen->ids.nr - 2;
490 i >= 0 && seen->ids.data[i].equiv >= id;
492 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv))
499 * ref_visible - given a key with snapshot id @src that points to a key with
500 * snapshot id @dst, test whether there is some snapshot in which @dst is
503 * @c: filesystem handle
504 * @s: list of snapshot IDs already seen at @src
505 * @src: snapshot ID of src key
506 * @dst: snapshot ID of dst key
507 * Returns: true if there is some snapshot in which @dst is visible
509 * Assumes we're visiting @src keys in natural key order
511 static bool ref_visible(struct bch_fs *c, struct snapshots_seen *s,
515 ? key_visible_in_snapshot(c, s, dst, src)
516 : bch2_snapshot_is_ancestor(c, src, dst);
519 static int ref_visible2(struct bch_fs *c,
520 u32 src, struct snapshots_seen *src_seen,
521 u32 dst, struct snapshots_seen *dst_seen)
523 src = bch2_snapshot_equiv(c, src);
524 dst = bch2_snapshot_equiv(c, dst);
528 swap(dst_seen, src_seen);
530 return key_visible_in_snapshot(c, src_seen, dst, src);
533 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
534 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
535 (_i)->snapshot <= (_snapshot); _i++) \
536 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
538 struct inode_walker_entry {
539 struct bch_inode_unpacked inode;
545 struct inode_walker {
546 bool first_this_inode;
547 bool recalculate_sums;
548 struct bpos last_pos;
550 DARRAY(struct inode_walker_entry) inodes;
553 static void inode_walker_exit(struct inode_walker *w)
555 darray_exit(&w->inodes);
558 static struct inode_walker inode_walker_init(void)
560 return (struct inode_walker) { 0, };
563 static int add_inode(struct bch_fs *c, struct inode_walker *w,
564 struct bkey_s_c inode)
566 struct bch_inode_unpacked u;
568 BUG_ON(bch2_inode_unpack(inode, &u));
570 return darray_push(&w->inodes, ((struct inode_walker_entry) {
572 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
576 static int get_inodes_all_snapshots(struct btree_trans *trans,
577 struct inode_walker *w, u64 inum)
579 struct bch_fs *c = trans->c;
580 struct btree_iter iter;
584 w->recalculate_sums = false;
587 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
588 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
589 if (k.k->p.offset != inum)
592 if (bkey_is_inode(k.k))
595 bch2_trans_iter_exit(trans, &iter);
600 w->first_this_inode = true;
604 static struct inode_walker_entry *
605 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w, struct bkey_s_c k)
607 bool is_whiteout = k.k->type == KEY_TYPE_whiteout;
608 u32 snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
610 struct inode_walker_entry *i;
611 __darray_for_each(w->inodes, i)
612 if (bch2_snapshot_is_ancestor(c, snapshot, i->snapshot))
617 BUG_ON(snapshot > i->snapshot);
619 if (snapshot != i->snapshot && !is_whiteout) {
620 struct inode_walker_entry new = *i;
622 new.snapshot = snapshot;
625 struct printbuf buf = PRINTBUF;
626 bch2_bkey_val_to_text(&buf, c, k);
628 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u\n"
629 "unexpected because we should always update the inode when we update a key in that inode\n"
631 w->last_pos.inode, snapshot, i->snapshot, buf.buf);
634 while (i > w->inodes.data && i[-1].snapshot > snapshot)
637 size_t pos = i - w->inodes.data;
638 int ret = darray_insert_item(&w->inodes, pos, new);
642 i = w->inodes.data + pos;
648 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
649 struct inode_walker *w,
652 if (w->last_pos.inode != k.k->p.inode) {
653 int ret = get_inodes_all_snapshots(trans, w, k.k->p.inode);
656 } else if (bkey_cmp(w->last_pos, k.k->p)) {
657 darray_for_each(w->inodes, i)
658 i->seen_this_pos = false;
661 w->last_pos = k.k->p;
663 return lookup_inode_for_snapshot(trans->c, w, k);
666 static int __get_visible_inodes(struct btree_trans *trans,
667 struct inode_walker *w,
668 struct snapshots_seen *s,
671 struct bch_fs *c = trans->c;
672 struct btree_iter iter;
678 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
679 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
680 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
682 if (k.k->p.offset != inum)
685 if (!ref_visible(c, s, s->pos.snapshot, equiv))
688 if (bkey_is_inode(k.k))
691 if (equiv >= s->pos.snapshot)
694 bch2_trans_iter_exit(trans, &iter);
699 static int check_key_has_snapshot(struct btree_trans *trans,
700 struct btree_iter *iter,
703 struct bch_fs *c = trans->c;
704 struct printbuf buf = PRINTBUF;
707 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
708 bkey_in_missing_snapshot,
709 "key in missing snapshot: %s",
710 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
711 ret = bch2_btree_delete_at(trans, iter,
712 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
718 static int hash_redo_key(struct btree_trans *trans,
719 const struct bch_hash_desc desc,
720 struct bch_hash_info *hash_info,
721 struct btree_iter *k_iter, struct bkey_s_c k)
723 struct bkey_i *delete;
726 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
728 return PTR_ERR(delete);
730 tmp = bch2_bkey_make_mut_noupdate(trans, k);
734 bkey_init(&delete->k);
735 delete->k.p = k_iter->pos;
736 return bch2_btree_iter_traverse(k_iter) ?:
737 bch2_trans_update(trans, k_iter, delete, 0) ?:
738 bch2_hash_set_in_snapshot(trans, desc, hash_info,
739 (subvol_inum) { 0, k.k->p.inode },
740 k.k->p.snapshot, tmp,
741 BCH_HASH_SET_MUST_CREATE,
742 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
743 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
746 static int hash_check_key(struct btree_trans *trans,
747 const struct bch_hash_desc desc,
748 struct bch_hash_info *hash_info,
749 struct btree_iter *k_iter, struct bkey_s_c hash_k)
751 struct bch_fs *c = trans->c;
752 struct btree_iter iter = { NULL };
753 struct printbuf buf = PRINTBUF;
758 if (hash_k.k->type != desc.key_type)
761 hash = desc.hash_bkey(hash_info, hash_k);
763 if (likely(hash == hash_k.k->p.offset))
766 if (hash_k.k->p.offset < hash)
769 for_each_btree_key_norestart(trans, iter, desc.btree_id,
770 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
771 BTREE_ITER_SLOTS, k, ret) {
772 if (bkey_eq(k.k->p, hash_k.k->p))
775 if (fsck_err_on(k.k->type == desc.key_type &&
776 !desc.cmp_bkey(k, hash_k), c,
777 hash_table_key_duplicate,
778 "duplicate hash table keys:\n%s",
779 (printbuf_reset(&buf),
780 bch2_bkey_val_to_text(&buf, c, hash_k),
782 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
786 if (bkey_deleted(k.k)) {
787 bch2_trans_iter_exit(trans, &iter);
792 bch2_trans_iter_exit(trans, &iter);
796 if (fsck_err(c, hash_table_key_wrong_offset,
797 "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
798 bch2_btree_id_str(desc.btree_id), hash_k.k->p.inode, hash_k.k->p.offset, hash,
799 (printbuf_reset(&buf),
800 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
801 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
805 ret = -BCH_ERR_transaction_restart_nested;
811 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
812 struct btree_iter *iter,
815 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
818 static struct bkey_s_c_dirent inode_get_dirent(struct btree_trans *trans,
819 struct btree_iter *iter,
820 struct bch_inode_unpacked *inode,
823 if (inode->bi_subvol) {
825 int ret = subvol_lookup(trans, inode->bi_parent_subvol, snapshot, &inum);
827 return ((struct bkey_s_c_dirent) { .k = ERR_PTR(ret) });
830 return dirent_get_by_pos(trans, iter, SPOS(inode->bi_dir, inode->bi_dir_offset, *snapshot));
833 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
834 struct bkey_s_c_dirent d)
836 return inode->bi_dir == d.k->p.inode &&
837 inode->bi_dir_offset == d.k->p.offset;
840 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
841 struct bch_inode_unpacked *inode)
843 return d.v->d_type == DT_SUBVOL
844 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
845 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
848 static int check_inode_deleted_list(struct btree_trans *trans, struct bpos p)
850 struct btree_iter iter;
851 struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_deleted_inodes, p, 0);
852 int ret = bkey_err(k) ?: k.k->type == KEY_TYPE_set;
853 bch2_trans_iter_exit(trans, &iter);
857 static int check_inode_dirent_inode(struct btree_trans *trans, struct bkey_s_c inode_k,
858 struct bch_inode_unpacked *inode,
859 u32 inode_snapshot, bool *write_inode)
861 struct bch_fs *c = trans->c;
862 struct printbuf buf = PRINTBUF;
864 struct btree_iter dirent_iter = {};
865 struct bkey_s_c_dirent d = inode_get_dirent(trans, &dirent_iter, inode, &inode_snapshot);
866 int ret = bkey_err(d);
867 if (ret && !bch2_err_matches(ret, ENOENT))
871 c, inode_points_to_missing_dirent,
872 "inode points to missing dirent\n%s",
873 (bch2_bkey_val_to_text(&buf, c, inode_k), buf.buf)) ||
874 fsck_err_on(!ret && !dirent_points_to_inode(d, inode),
875 c, inode_points_to_wrong_dirent,
876 "inode points to dirent that does not point back:\n%s",
877 (bch2_bkey_val_to_text(&buf, c, inode_k),
879 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
881 * We just clear the backpointer fields for now. If we find a
882 * dirent that points to this inode in check_dirents(), we'll
883 * update it then; then when we get to check_path() if the
884 * backpointer is still 0 we'll reattach it.
887 inode->bi_dir_offset = 0;
888 inode->bi_flags &= ~BCH_INODE_backptr_untrusted;
894 bch2_trans_iter_exit(trans, &dirent_iter);
900 static int check_inode(struct btree_trans *trans,
901 struct btree_iter *iter,
903 struct bch_inode_unpacked *prev,
904 struct snapshots_seen *s,
907 struct bch_fs *c = trans->c;
908 struct bch_inode_unpacked u;
909 bool do_update = false;
912 ret = check_key_has_snapshot(trans, iter, k);
918 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
922 if (!bkey_is_inode(k.k))
925 BUG_ON(bch2_inode_unpack(k, &u));
928 !(u.bi_flags & (BCH_INODE_i_size_dirty|
929 BCH_INODE_i_sectors_dirty|
930 BCH_INODE_unlinked)))
933 if (prev->bi_inum != u.bi_inum)
936 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
937 inode_d_type(prev) != inode_d_type(&u),
938 c, inode_snapshot_mismatch,
939 "inodes in different snapshots don't match")) {
940 bch_err(c, "repair not implemented yet");
941 return -BCH_ERR_fsck_repair_unimplemented;
944 if ((u.bi_flags & (BCH_INODE_i_size_dirty|BCH_INODE_unlinked)) &&
945 bch2_key_has_snapshot_overwrites(trans, BTREE_ID_inodes, k.k->p)) {
946 struct bpos new_min_pos;
948 ret = bch2_propagate_key_to_snapshot_leaves(trans, iter->btree_id, k, &new_min_pos);
952 u.bi_flags &= ~BCH_INODE_i_size_dirty|BCH_INODE_unlinked;
954 ret = __bch2_fsck_write_inode(trans, &u, iter->pos.snapshot);
956 bch_err_msg(c, ret, "in fsck updating inode");
960 if (!bpos_eq(new_min_pos, POS_MIN))
961 bch2_btree_iter_set_pos(iter, bpos_predecessor(new_min_pos));
965 if (u.bi_flags & BCH_INODE_unlinked) {
966 ret = check_inode_deleted_list(trans, k.k->p);
970 fsck_err_on(!ret, c, unlinked_inode_not_on_deleted_list,
971 "inode %llu:%u unlinked, but not on deleted list",
972 u.bi_inum, k.k->p.snapshot);
976 if (u.bi_flags & BCH_INODE_unlinked &&
978 fsck_err(c, inode_unlinked_but_clean,
979 "filesystem marked clean, but inode %llu unlinked",
981 ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
982 bch_err_msg(c, ret, "in fsck deleting inode");
986 if (u.bi_flags & BCH_INODE_i_size_dirty &&
988 fsck_err(c, inode_i_size_dirty_but_clean,
989 "filesystem marked clean, but inode %llu has i_size dirty",
991 bch_verbose(c, "truncating inode %llu", u.bi_inum);
994 * XXX: need to truncate partial blocks too here - or ideally
995 * just switch units to bytes and that issue goes away
997 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
998 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
1000 POS(u.bi_inum, U64_MAX),
1002 bch_err_msg(c, ret, "in fsck truncating inode");
1007 * We truncated without our normal sector accounting hook, just
1008 * make sure we recalculate it:
1010 u.bi_flags |= BCH_INODE_i_sectors_dirty;
1012 u.bi_flags &= ~BCH_INODE_i_size_dirty;
1016 if (u.bi_flags & BCH_INODE_i_sectors_dirty &&
1018 fsck_err(c, inode_i_sectors_dirty_but_clean,
1019 "filesystem marked clean, but inode %llu has i_sectors dirty",
1023 bch_verbose(c, "recounting sectors for inode %llu",
1026 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
1028 bch_err_msg(c, sectors, "in fsck recounting inode sectors");
1032 u.bi_sectors = sectors;
1033 u.bi_flags &= ~BCH_INODE_i_sectors_dirty;
1037 if (u.bi_flags & BCH_INODE_backptr_untrusted) {
1039 u.bi_dir_offset = 0;
1040 u.bi_flags &= ~BCH_INODE_backptr_untrusted;
1044 if (u.bi_dir || u.bi_dir_offset) {
1045 ret = check_inode_dirent_inode(trans, k, &u, k.k->p.snapshot, &do_update);
1050 if (fsck_err_on(u.bi_parent_subvol &&
1051 (u.bi_subvol == 0 ||
1052 u.bi_subvol == BCACHEFS_ROOT_SUBVOL),
1053 c, inode_bi_parent_nonzero,
1054 "inode %llu:%u has subvol %u but nonzero parent subvol %u",
1055 u.bi_inum, k.k->p.snapshot, u.bi_subvol, u.bi_parent_subvol)) {
1056 u.bi_parent_subvol = 0;
1061 struct bch_subvolume s;
1063 ret = bch2_subvolume_get(trans, u.bi_subvol, false, 0, &s);
1064 if (ret && !bch2_err_matches(ret, ENOENT))
1067 if (fsck_err_on(ret,
1068 c, inode_bi_subvol_missing,
1069 "inode %llu:%u bi_subvol points to missing subvolume %u",
1070 u.bi_inum, k.k->p.snapshot, u.bi_subvol) ||
1071 fsck_err_on(le64_to_cpu(s.inode) != u.bi_inum ||
1072 !bch2_snapshot_is_ancestor(c, le32_to_cpu(s.snapshot),
1074 c, inode_bi_subvol_wrong,
1075 "inode %llu:%u points to subvol %u, but subvol points to %llu:%u",
1076 u.bi_inum, k.k->p.snapshot, u.bi_subvol,
1077 le64_to_cpu(s.inode),
1078 le32_to_cpu(s.snapshot))) {
1080 u.bi_parent_subvol = 0;
1086 ret = __bch2_fsck_write_inode(trans, &u, iter->pos.snapshot);
1087 bch_err_msg(c, ret, "in fsck updating inode");
1097 int bch2_check_inodes(struct bch_fs *c)
1099 bool full = c->opts.fsck;
1100 struct bch_inode_unpacked prev = { 0 };
1101 struct snapshots_seen s;
1103 snapshots_seen_init(&s);
1105 int ret = bch2_trans_run(c,
1106 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
1108 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1109 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1110 check_inode(trans, &iter, k, &prev, &s, full)));
1112 snapshots_seen_exit(&s);
1117 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1119 struct bch_fs *c = trans->c;
1120 u32 restart_count = trans->restart_count;
1124 darray_for_each(w->inodes, i) {
1125 if (i->inode.bi_sectors == i->count)
1128 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1130 if (w->recalculate_sums)
1133 if (i->count != count2) {
1134 bch_err(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1135 w->last_pos.inode, i->snapshot, i->count, count2);
1136 return -BCH_ERR_internal_fsck_err;
1139 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_sectors_dirty),
1140 c, inode_i_sectors_wrong,
1141 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1142 w->last_pos.inode, i->snapshot,
1143 i->inode.bi_sectors, i->count)) {
1144 i->inode.bi_sectors = i->count;
1145 ret = bch2_fsck_write_inode(trans, &i->inode, i->snapshot);
1152 return ret ?: trans_was_restarted(trans, restart_count);
1158 struct snapshots_seen seen;
1161 struct extent_ends {
1162 struct bpos last_pos;
1163 DARRAY(struct extent_end) e;
1166 static void extent_ends_reset(struct extent_ends *extent_ends)
1168 darray_for_each(extent_ends->e, i)
1169 snapshots_seen_exit(&i->seen);
1170 extent_ends->e.nr = 0;
1173 static void extent_ends_exit(struct extent_ends *extent_ends)
1175 extent_ends_reset(extent_ends);
1176 darray_exit(&extent_ends->e);
1179 static void extent_ends_init(struct extent_ends *extent_ends)
1181 memset(extent_ends, 0, sizeof(*extent_ends));
1184 static int extent_ends_at(struct bch_fs *c,
1185 struct extent_ends *extent_ends,
1186 struct snapshots_seen *seen,
1189 struct extent_end *i, n = (struct extent_end) {
1190 .offset = k.k->p.offset,
1191 .snapshot = k.k->p.snapshot,
1195 n.seen.ids.data = kmemdup(seen->ids.data,
1196 sizeof(seen->ids.data[0]) * seen->ids.size,
1198 if (!n.seen.ids.data)
1199 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1201 __darray_for_each(extent_ends->e, i) {
1202 if (i->snapshot == k.k->p.snapshot) {
1203 snapshots_seen_exit(&i->seen);
1208 if (i->snapshot >= k.k->p.snapshot)
1212 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1215 static int overlapping_extents_found(struct btree_trans *trans,
1216 enum btree_id btree,
1217 struct bpos pos1, struct snapshots_seen *pos1_seen,
1220 struct extent_end *extent_end)
1222 struct bch_fs *c = trans->c;
1223 struct printbuf buf = PRINTBUF;
1224 struct btree_iter iter1, iter2 = { NULL };
1225 struct bkey_s_c k1, k2;
1228 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1230 bch2_trans_iter_init(trans, &iter1, btree, pos1,
1231 BTREE_ITER_ALL_SNAPSHOTS|
1232 BTREE_ITER_NOT_EXTENTS);
1233 k1 = bch2_btree_iter_peek_upto(&iter1, POS(pos1.inode, U64_MAX));
1238 prt_str(&buf, "\n ");
1239 bch2_bkey_val_to_text(&buf, c, k1);
1241 if (!bpos_eq(pos1, k1.k->p)) {
1242 prt_str(&buf, "\n wanted\n ");
1243 bch2_bpos_to_text(&buf, pos1);
1244 prt_str(&buf, "\n ");
1245 bch2_bkey_to_text(&buf, &pos2);
1247 bch_err(c, "%s: error finding first overlapping extent when repairing, got%s",
1249 ret = -BCH_ERR_internal_fsck_err;
1253 bch2_trans_copy_iter(&iter2, &iter1);
1256 bch2_btree_iter_advance(&iter2);
1258 k2 = bch2_btree_iter_peek_upto(&iter2, POS(pos1.inode, U64_MAX));
1263 if (bpos_ge(k2.k->p, pos2.p))
1267 prt_str(&buf, "\n ");
1268 bch2_bkey_val_to_text(&buf, c, k2);
1270 if (bpos_gt(k2.k->p, pos2.p) ||
1271 pos2.size != k2.k->size) {
1272 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1274 ret = -BCH_ERR_internal_fsck_err;
1278 prt_printf(&buf, "\n overwriting %s extent",
1279 pos1.snapshot >= pos2.p.snapshot ? "first" : "second");
1281 if (fsck_err(c, extent_overlapping,
1282 "overlapping extents%s", buf.buf)) {
1283 struct btree_iter *old_iter = &iter1;
1284 struct disk_reservation res = { 0 };
1286 if (pos1.snapshot < pos2.p.snapshot) {
1291 trans->extra_disk_res += bch2_bkey_sectors_compressed(k2);
1293 ret = bch2_trans_update_extent_overwrite(trans, old_iter,
1294 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE,
1296 bch2_trans_commit(trans, &res, NULL, BCH_TRANS_COMMIT_no_enospc);
1297 bch2_disk_reservation_put(c, &res);
1304 if (pos1.snapshot == pos2.p.snapshot) {
1306 * We overwrote the first extent, and did the overwrite
1307 * in the same snapshot:
1309 extent_end->offset = bkey_start_offset(&pos2);
1310 } else if (pos1.snapshot > pos2.p.snapshot) {
1312 * We overwrote the first extent in pos2's snapshot:
1314 ret = snapshots_seen_add_inorder(c, pos1_seen, pos2.p.snapshot);
1317 * We overwrote the second extent - restart
1318 * check_extent() from the top:
1320 ret = -BCH_ERR_transaction_restart_nested;
1325 bch2_trans_iter_exit(trans, &iter2);
1326 bch2_trans_iter_exit(trans, &iter1);
1327 printbuf_exit(&buf);
1331 static int check_overlapping_extents(struct btree_trans *trans,
1332 struct snapshots_seen *seen,
1333 struct extent_ends *extent_ends,
1336 struct btree_iter *iter,
1339 struct bch_fs *c = trans->c;
1342 /* transaction restart, running again */
1343 if (bpos_eq(extent_ends->last_pos, k.k->p))
1346 if (extent_ends->last_pos.inode != k.k->p.inode)
1347 extent_ends_reset(extent_ends);
1349 darray_for_each(extent_ends->e, i) {
1350 if (i->offset <= bkey_start_offset(k.k))
1353 if (!ref_visible2(c,
1354 k.k->p.snapshot, seen,
1355 i->snapshot, &i->seen))
1358 ret = overlapping_extents_found(trans, iter->btree_id,
1359 SPOS(iter->pos.inode,
1368 ret = extent_ends_at(c, extent_ends, seen, k);
1372 extent_ends->last_pos = k.k->p;
1377 static int check_extent_overbig(struct btree_trans *trans, struct btree_iter *iter,
1380 struct bch_fs *c = trans->c;
1381 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1382 struct bch_extent_crc_unpacked crc;
1383 const union bch_extent_entry *i;
1384 unsigned encoded_extent_max_sectors = c->opts.encoded_extent_max >> 9;
1386 bkey_for_each_crc(k.k, ptrs, crc, i)
1387 if (crc_is_encoded(crc) &&
1388 crc.uncompressed_size > encoded_extent_max_sectors) {
1389 struct printbuf buf = PRINTBUF;
1391 bch2_bkey_val_to_text(&buf, c, k);
1392 bch_err(c, "overbig encoded extent, please report this:\n %s", buf.buf);
1393 printbuf_exit(&buf);
1399 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1401 struct inode_walker *inode,
1402 struct snapshots_seen *s,
1403 struct extent_ends *extent_ends)
1405 struct bch_fs *c = trans->c;
1406 struct inode_walker_entry *i;
1407 struct printbuf buf = PRINTBUF;
1408 struct bpos equiv = k.k->p;
1411 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1413 ret = check_key_has_snapshot(trans, iter, k);
1415 ret = ret < 0 ? ret : 0;
1419 if (inode->last_pos.inode != k.k->p.inode) {
1420 ret = check_i_sectors(trans, inode);
1425 i = walk_inode(trans, inode, k);
1426 ret = PTR_ERR_OR_ZERO(i);
1430 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1434 if (k.k->type != KEY_TYPE_whiteout) {
1435 if (fsck_err_on(!i, c, extent_in_missing_inode,
1436 "extent in missing inode:\n %s",
1437 (printbuf_reset(&buf),
1438 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1441 if (fsck_err_on(i &&
1442 !S_ISREG(i->inode.bi_mode) &&
1443 !S_ISLNK(i->inode.bi_mode),
1444 c, extent_in_non_reg_inode,
1445 "extent in non regular inode mode %o:\n %s",
1447 (printbuf_reset(&buf),
1448 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1451 ret = check_overlapping_extents(trans, s, extent_ends, k,
1452 equiv.snapshot, iter,
1453 &inode->recalculate_sums);
1459 * Check inodes in reverse order, from oldest snapshots to newest,
1460 * starting from the inode that matches this extent's snapshot. If we
1461 * didn't have one, iterate over all inodes:
1464 i = inode->inodes.data + inode->inodes.nr - 1;
1467 inode->inodes.data && i >= inode->inodes.data;
1469 if (i->snapshot > equiv.snapshot ||
1470 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1473 if (k.k->type != KEY_TYPE_whiteout) {
1474 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_size_dirty) &&
1475 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1476 !bkey_extent_is_reservation(k),
1477 c, extent_past_end_of_inode,
1478 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1479 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1480 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1481 struct btree_iter iter2;
1483 bch2_trans_copy_iter(&iter2, iter);
1484 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1485 ret = bch2_btree_iter_traverse(&iter2) ?:
1486 bch2_btree_delete_at(trans, &iter2,
1487 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1488 bch2_trans_iter_exit(trans, &iter2);
1492 iter->k.type = KEY_TYPE_whiteout;
1495 if (bkey_extent_is_allocation(k.k))
1496 i->count += k.k->size;
1499 i->seen_this_pos = true;
1504 printbuf_exit(&buf);
1508 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1513 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1514 * that i_size an i_sectors are consistent
1516 int bch2_check_extents(struct bch_fs *c)
1518 struct inode_walker w = inode_walker_init();
1519 struct snapshots_seen s;
1520 struct extent_ends extent_ends;
1521 struct disk_reservation res = { 0 };
1523 snapshots_seen_init(&s);
1524 extent_ends_init(&extent_ends);
1526 int ret = bch2_trans_run(c,
1527 for_each_btree_key_commit(trans, iter, BTREE_ID_extents,
1528 POS(BCACHEFS_ROOT_INO, 0),
1529 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1531 BCH_TRANS_COMMIT_no_enospc, ({
1532 bch2_disk_reservation_put(c, &res);
1533 check_extent(trans, &iter, k, &w, &s, &extent_ends) ?:
1534 check_extent_overbig(trans, &iter, k);
1536 check_i_sectors(trans, &w));
1538 bch2_disk_reservation_put(c, &res);
1539 extent_ends_exit(&extent_ends);
1540 inode_walker_exit(&w);
1541 snapshots_seen_exit(&s);
1547 int bch2_check_indirect_extents(struct bch_fs *c)
1549 struct disk_reservation res = { 0 };
1551 int ret = bch2_trans_run(c,
1552 for_each_btree_key_commit(trans, iter, BTREE_ID_reflink,
1554 BTREE_ITER_PREFETCH, k,
1556 BCH_TRANS_COMMIT_no_enospc, ({
1557 bch2_disk_reservation_put(c, &res);
1558 check_extent_overbig(trans, &iter, k);
1561 bch2_disk_reservation_put(c, &res);
1566 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1568 struct bch_fs *c = trans->c;
1569 u32 restart_count = trans->restart_count;
1573 darray_for_each(w->inodes, i) {
1574 if (i->inode.bi_nlink == i->count)
1577 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1581 if (i->count != count2) {
1582 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1585 if (i->inode.bi_nlink == i->count)
1589 if (fsck_err_on(i->inode.bi_nlink != i->count,
1590 c, inode_dir_wrong_nlink,
1591 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1592 w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1593 i->inode.bi_nlink = i->count;
1594 ret = bch2_fsck_write_inode(trans, &i->inode, i->snapshot);
1601 return ret ?: trans_was_restarted(trans, restart_count);
1604 static int check_dirent_inode_dirent(struct btree_trans *trans,
1605 struct btree_iter *iter,
1606 struct bkey_s_c_dirent d,
1607 struct bch_inode_unpacked *target,
1608 u32 target_snapshot)
1610 struct bch_fs *c = trans->c;
1611 struct printbuf buf = PRINTBUF;
1614 if (inode_points_to_dirent(target, d))
1617 if (!target->bi_dir &&
1618 !target->bi_dir_offset) {
1619 target->bi_dir = d.k->p.inode;
1620 target->bi_dir_offset = d.k->p.offset;
1621 return __bch2_fsck_write_inode(trans, target, target_snapshot);
1624 struct btree_iter bp_iter = { NULL };
1625 struct bkey_s_c_dirent bp_dirent = dirent_get_by_pos(trans, &bp_iter,
1626 SPOS(target->bi_dir, target->bi_dir_offset, target_snapshot));
1627 ret = bkey_err(bp_dirent);
1628 if (ret && !bch2_err_matches(ret, ENOENT))
1631 bool backpointer_exists = !ret;
1634 if (fsck_err_on(!backpointer_exists,
1635 c, inode_wrong_backpointer,
1636 "inode %llu:%u has wrong backpointer:\n"
1638 "should be %llu:%llu",
1639 target->bi_inum, target_snapshot,
1641 target->bi_dir_offset,
1644 target->bi_dir = d.k->p.inode;
1645 target->bi_dir_offset = d.k->p.offset;
1646 ret = __bch2_fsck_write_inode(trans, target, target_snapshot);
1650 bch2_bkey_val_to_text(&buf, c, d.s_c);
1652 if (backpointer_exists)
1653 bch2_bkey_val_to_text(&buf, c, bp_dirent.s_c);
1655 if (fsck_err_on(backpointer_exists &&
1656 (S_ISDIR(target->bi_mode) ||
1658 c, inode_dir_multiple_links,
1659 "%s %llu:%u with multiple links\n%s",
1660 S_ISDIR(target->bi_mode) ? "directory" : "subvolume",
1661 target->bi_inum, target_snapshot, buf.buf)) {
1662 ret = __remove_dirent(trans, d.k->p);
1667 * hardlinked file with nlink 0:
1668 * We're just adjusting nlink here so check_nlinks() will pick
1669 * it up, it ignores inodes with nlink 0
1671 if (fsck_err_on(backpointer_exists && !target->bi_nlink,
1672 c, inode_multiple_links_but_nlink_0,
1673 "inode %llu:%u type %s has multiple links but i_nlink 0\n%s",
1674 target->bi_inum, target_snapshot, bch2_d_types[d.v->d_type], buf.buf)) {
1676 target->bi_flags &= ~BCH_INODE_unlinked;
1677 ret = __bch2_fsck_write_inode(trans, target, target_snapshot);
1684 bch2_trans_iter_exit(trans, &bp_iter);
1685 printbuf_exit(&buf);
1690 static int check_dirent_target(struct btree_trans *trans,
1691 struct btree_iter *iter,
1692 struct bkey_s_c_dirent d,
1693 struct bch_inode_unpacked *target,
1694 u32 target_snapshot)
1696 struct bch_fs *c = trans->c;
1697 struct bkey_i_dirent *n;
1698 struct printbuf buf = PRINTBUF;
1701 ret = check_dirent_inode_dirent(trans, iter, d, target, target_snapshot);
1705 if (fsck_err_on(d.v->d_type != inode_d_type(target),
1706 c, dirent_d_type_wrong,
1707 "incorrect d_type: got %s, should be %s:\n%s",
1708 bch2_d_type_str(d.v->d_type),
1709 bch2_d_type_str(inode_d_type(target)),
1710 (printbuf_reset(&buf),
1711 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1712 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1713 ret = PTR_ERR_OR_ZERO(n);
1717 bkey_reassemble(&n->k_i, d.s_c);
1718 n->v.d_type = inode_d_type(target);
1719 if (n->v.d_type == DT_SUBVOL) {
1720 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1721 n->v.d_child_subvol = cpu_to_le32(target->bi_subvol);
1723 n->v.d_inum = cpu_to_le64(target->bi_inum);
1726 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1730 d = dirent_i_to_s_c(n);
1734 printbuf_exit(&buf);
1739 /* find a subvolume that's a descendent of @snapshot: */
1740 static int find_snapshot_subvol(struct btree_trans *trans, u32 snapshot, u32 *subvolid)
1742 struct btree_iter iter;
1746 for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN, 0, k, ret) {
1747 if (k.k->type != KEY_TYPE_subvolume)
1750 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
1751 if (bch2_snapshot_is_ancestor(trans->c, le32_to_cpu(s.v->snapshot), snapshot)) {
1752 bch2_trans_iter_exit(trans, &iter);
1753 *subvolid = k.k->p.offset;
1760 bch2_trans_iter_exit(trans, &iter);
1764 static int check_dirent_to_subvol(struct btree_trans *trans, struct btree_iter *iter,
1765 struct bkey_s_c_dirent d)
1767 struct bch_fs *c = trans->c;
1768 struct btree_iter subvol_iter = {};
1769 struct bch_inode_unpacked subvol_root;
1770 u32 parent_subvol = le32_to_cpu(d.v->d_parent_subvol);
1771 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1772 u32 parent_snapshot;
1774 struct printbuf buf = PRINTBUF;
1777 ret = subvol_lookup(trans, parent_subvol, &parent_snapshot, &parent_inum);
1778 if (ret && !bch2_err_matches(ret, ENOENT))
1781 if (fsck_err_on(ret, c, dirent_to_missing_parent_subvol,
1782 "dirent parent_subvol points to missing subvolume\n%s",
1783 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf)) ||
1784 fsck_err_on(!ret && !bch2_snapshot_is_ancestor(c, parent_snapshot, d.k->p.snapshot),
1785 c, dirent_not_visible_in_parent_subvol,
1786 "dirent not visible in parent_subvol (not an ancestor of subvol snap %u)\n%s",
1788 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1789 u32 new_parent_subvol;
1790 ret = find_snapshot_subvol(trans, d.k->p.snapshot, &new_parent_subvol);
1794 struct bkey_i_dirent *new_dirent = bch2_bkey_make_mut_typed(trans, iter, &d.s_c, 0, dirent);
1795 ret = PTR_ERR_OR_ZERO(new_dirent);
1799 new_dirent->v.d_parent_subvol = cpu_to_le32(new_parent_subvol);
1802 struct bkey_s_c_subvolume s =
1803 bch2_bkey_get_iter_typed(trans, &subvol_iter,
1804 BTREE_ID_subvolumes, POS(0, target_subvol),
1806 ret = bkey_err(s.s_c);
1807 if (ret && !bch2_err_matches(ret, ENOENT))
1811 if (fsck_err(c, dirent_to_missing_subvol,
1812 "dirent points to missing subvolume\n%s",
1813 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf)))
1814 return __remove_dirent(trans, d.k->p);
1819 if (fsck_err_on(le32_to_cpu(s.v->fs_path_parent) != parent_subvol,
1820 c, subvol_fs_path_parent_wrong,
1821 "subvol with wrong fs_path_parent, should be be %u\n%s",
1823 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
1824 struct bkey_i_subvolume *n =
1825 bch2_bkey_make_mut_typed(trans, &subvol_iter, &s.s_c, 0, subvolume);
1826 ret = PTR_ERR_OR_ZERO(n);
1830 n->v.fs_path_parent = cpu_to_le32(parent_subvol);
1833 u64 target_inum = le64_to_cpu(s.v->inode);
1834 u32 target_snapshot = le32_to_cpu(s.v->snapshot);
1836 ret = lookup_inode(trans, target_inum, &subvol_root, &target_snapshot);
1837 if (ret && !bch2_err_matches(ret, ENOENT))
1840 if (fsck_err_on(parent_subvol != subvol_root.bi_parent_subvol,
1841 c, inode_bi_parent_wrong,
1842 "subvol root %llu has wrong bi_parent_subvol: got %u, should be %u",
1844 subvol_root.bi_parent_subvol, parent_subvol)) {
1845 subvol_root.bi_parent_subvol = parent_subvol;
1846 ret = __bch2_fsck_write_inode(trans, &subvol_root, target_snapshot);
1851 ret = check_dirent_target(trans, iter, d, &subvol_root,
1858 bch2_trans_iter_exit(trans, &subvol_iter);
1859 printbuf_exit(&buf);
1863 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1865 struct bch_hash_info *hash_info,
1866 struct inode_walker *dir,
1867 struct inode_walker *target,
1868 struct snapshots_seen *s)
1870 struct bch_fs *c = trans->c;
1871 struct bkey_s_c_dirent d;
1872 struct inode_walker_entry *i;
1873 struct printbuf buf = PRINTBUF;
1877 ret = check_key_has_snapshot(trans, iter, k);
1879 ret = ret < 0 ? ret : 0;
1884 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1886 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1890 if (k.k->type == KEY_TYPE_whiteout)
1893 if (dir->last_pos.inode != k.k->p.inode) {
1894 ret = check_subdir_count(trans, dir);
1899 BUG_ON(!btree_iter_path(trans, iter)->should_be_locked);
1901 i = walk_inode(trans, dir, k);
1902 ret = PTR_ERR_OR_ZERO(i);
1906 if (dir->first_this_inode && dir->inodes.nr)
1907 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1908 dir->first_this_inode = false;
1910 if (fsck_err_on(!i, c, dirent_in_missing_dir_inode,
1911 "dirent in nonexisting directory:\n%s",
1912 (printbuf_reset(&buf),
1913 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1914 ret = bch2_btree_delete_at(trans, iter,
1915 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1922 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode),
1923 c, dirent_in_non_dir_inode,
1924 "dirent in non directory inode type %s:\n%s",
1925 bch2_d_type_str(inode_d_type(&i->inode)),
1926 (printbuf_reset(&buf),
1927 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1928 ret = bch2_btree_delete_at(trans, iter, 0);
1932 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1936 /* dirent has been deleted */
1941 if (k.k->type != KEY_TYPE_dirent)
1944 d = bkey_s_c_to_dirent(k);
1946 if (d.v->d_type == DT_SUBVOL) {
1947 ret = check_dirent_to_subvol(trans, iter, d);
1951 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1955 if (fsck_err_on(!target->inodes.nr,
1956 c, dirent_to_missing_inode,
1957 "dirent points to missing inode: (equiv %u)\n%s",
1959 (printbuf_reset(&buf),
1960 bch2_bkey_val_to_text(&buf, c, k),
1962 ret = __remove_dirent(trans, d.k->p);
1967 darray_for_each(target->inodes, i) {
1968 ret = check_dirent_target(trans, iter, d,
1969 &i->inode, i->snapshot);
1974 if (d.v->d_type == DT_DIR)
1975 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1981 printbuf_exit(&buf);
1987 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1990 int bch2_check_dirents(struct bch_fs *c)
1992 struct inode_walker dir = inode_walker_init();
1993 struct inode_walker target = inode_walker_init();
1994 struct snapshots_seen s;
1995 struct bch_hash_info hash_info;
1997 snapshots_seen_init(&s);
1999 int ret = bch2_trans_run(c,
2000 for_each_btree_key_commit(trans, iter, BTREE_ID_dirents,
2001 POS(BCACHEFS_ROOT_INO, 0),
2002 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
2005 BCH_TRANS_COMMIT_no_enospc,
2006 check_dirent(trans, &iter, k, &hash_info, &dir, &target, &s)));
2008 snapshots_seen_exit(&s);
2009 inode_walker_exit(&dir);
2010 inode_walker_exit(&target);
2015 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
2017 struct bch_hash_info *hash_info,
2018 struct inode_walker *inode)
2020 struct bch_fs *c = trans->c;
2021 struct inode_walker_entry *i;
2024 ret = check_key_has_snapshot(trans, iter, k);
2028 i = walk_inode(trans, inode, k);
2029 ret = PTR_ERR_OR_ZERO(i);
2033 if (inode->first_this_inode && inode->inodes.nr)
2034 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
2035 inode->first_this_inode = false;
2037 if (fsck_err_on(!i, c, xattr_in_missing_inode,
2038 "xattr for missing inode %llu",
2040 return bch2_btree_delete_at(trans, iter, 0);
2045 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
2052 * Walk xattrs: verify that they all have a corresponding inode
2054 int bch2_check_xattrs(struct bch_fs *c)
2056 struct inode_walker inode = inode_walker_init();
2057 struct bch_hash_info hash_info;
2060 ret = bch2_trans_run(c,
2061 for_each_btree_key_commit(trans, iter, BTREE_ID_xattrs,
2062 POS(BCACHEFS_ROOT_INO, 0),
2063 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
2066 BCH_TRANS_COMMIT_no_enospc,
2067 check_xattr(trans, &iter, k, &hash_info, &inode)));
2072 static int check_root_trans(struct btree_trans *trans)
2074 struct bch_fs *c = trans->c;
2075 struct bch_inode_unpacked root_inode;
2080 ret = subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
2081 if (ret && !bch2_err_matches(ret, ENOENT))
2084 if (mustfix_fsck_err_on(ret, c, root_subvol_missing,
2085 "root subvol missing")) {
2086 struct bkey_i_subvolume root_subvol;
2089 inum = BCACHEFS_ROOT_INO;
2091 bkey_subvolume_init(&root_subvol.k_i);
2092 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
2093 root_subvol.v.flags = 0;
2094 root_subvol.v.snapshot = cpu_to_le32(snapshot);
2095 root_subvol.v.inode = cpu_to_le64(inum);
2096 ret = bch2_btree_insert_trans(trans, BTREE_ID_subvolumes, &root_subvol.k_i, 0);
2097 bch_err_msg(c, ret, "writing root subvol");
2102 ret = lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
2103 if (ret && !bch2_err_matches(ret, ENOENT))
2106 if (mustfix_fsck_err_on(ret, c, root_dir_missing,
2107 "root directory missing") ||
2108 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode),
2109 c, root_inode_not_dir,
2110 "root inode not a directory")) {
2111 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
2113 root_inode.bi_inum = inum;
2115 ret = __bch2_fsck_write_inode(trans, &root_inode, snapshot);
2116 bch_err_msg(c, ret, "writing root inode");
2123 /* Get root directory, create if it doesn't exist: */
2124 int bch2_check_root(struct bch_fs *c)
2126 int ret = bch2_trans_do(c, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2127 check_root_trans(trans));
2132 typedef DARRAY(u32) darray_u32;
2134 static bool darray_u32_has(darray_u32 *d, u32 v)
2136 darray_for_each(*d, i)
2143 * We've checked that inode backpointers point to valid dirents; here, it's
2144 * sufficient to check that the subvolume root has a dirent:
2146 static int subvol_has_dirent(struct btree_trans *trans, struct bkey_s_c_subvolume s)
2148 struct bch_inode_unpacked inode;
2149 int ret = bch2_inode_find_by_inum_trans(trans,
2150 (subvol_inum) { s.k->p.offset, le64_to_cpu(s.v->inode) },
2155 return inode.bi_dir != 0;
2158 static int check_subvol_path(struct btree_trans *trans, struct btree_iter *iter, struct bkey_s_c k)
2160 struct bch_fs *c = trans->c;
2161 struct btree_iter parent_iter = {};
2162 darray_u32 subvol_path = {};
2163 struct printbuf buf = PRINTBUF;
2166 if (k.k->type != KEY_TYPE_subvolume)
2169 while (k.k->p.offset != BCACHEFS_ROOT_SUBVOL) {
2170 ret = darray_push(&subvol_path, k.k->p.offset);
2174 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
2176 ret = subvol_has_dirent(trans, s);
2180 if (fsck_err_on(!ret,
2181 c, subvol_unreachable,
2182 "unreachable subvolume %s",
2183 (bch2_bkey_val_to_text(&buf, c, s.s_c),
2185 ret = reattach_subvol(trans, s);
2189 u32 parent = le32_to_cpu(s.v->fs_path_parent);
2191 if (darray_u32_has(&subvol_path, parent)) {
2192 if (fsck_err(c, subvol_loop, "subvolume loop"))
2193 ret = reattach_subvol(trans, s);
2197 bch2_trans_iter_exit(trans, &parent_iter);
2198 bch2_trans_iter_init(trans, &parent_iter,
2199 BTREE_ID_subvolumes, POS(0, parent), 0);
2200 k = bch2_btree_iter_peek_slot(&parent_iter);
2205 if (fsck_err_on(k.k->type != KEY_TYPE_subvolume,
2206 c, subvol_unreachable,
2207 "unreachable subvolume %s",
2208 (bch2_bkey_val_to_text(&buf, c, s.s_c),
2210 ret = reattach_subvol(trans, s);
2216 printbuf_exit(&buf);
2217 darray_exit(&subvol_path);
2218 bch2_trans_iter_exit(trans, &parent_iter);
2222 int bch2_check_subvolume_structure(struct bch_fs *c)
2224 int ret = bch2_trans_run(c,
2225 for_each_btree_key_commit(trans, iter,
2226 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
2227 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2228 check_subvol_path(trans, &iter, k)));
2233 struct pathbuf_entry {
2238 typedef DARRAY(struct pathbuf_entry) pathbuf;
2240 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
2242 darray_for_each(*p, i)
2243 if (i->inum == inum &&
2244 i->snapshot == snapshot)
2250 * Check that a given inode is reachable from its subvolume root - we already
2251 * verified subvolume connectivity:
2253 * XXX: we should also be verifying that inodes are in the right subvolumes
2255 static int check_path(struct btree_trans *trans, pathbuf *p, struct bkey_s_c inode_k)
2257 struct bch_fs *c = trans->c;
2258 struct btree_iter inode_iter = {};
2259 struct bch_inode_unpacked inode;
2260 struct printbuf buf = PRINTBUF;
2261 u32 snapshot = bch2_snapshot_equiv(c, inode_k.k->p.snapshot);
2266 BUG_ON(bch2_inode_unpack(inode_k, &inode));
2268 while (!inode.bi_subvol) {
2269 struct btree_iter dirent_iter;
2270 struct bkey_s_c_dirent d;
2271 u32 parent_snapshot = snapshot;
2273 d = inode_get_dirent(trans, &dirent_iter, &inode, &parent_snapshot);
2274 ret = bkey_err(d.s_c);
2275 if (ret && !bch2_err_matches(ret, ENOENT))
2278 if (!ret && !dirent_points_to_inode(d, &inode)) {
2279 bch2_trans_iter_exit(trans, &dirent_iter);
2280 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
2283 if (bch2_err_matches(ret, ENOENT)) {
2285 if (fsck_err(c, inode_unreachable,
2286 "unreachable inode\n%s",
2287 (printbuf_reset(&buf),
2288 bch2_bkey_val_to_text(&buf, c, inode_k),
2290 ret = reattach_inode(trans, &inode, snapshot);
2294 bch2_trans_iter_exit(trans, &dirent_iter);
2296 if (!S_ISDIR(inode.bi_mode))
2299 ret = darray_push(p, ((struct pathbuf_entry) {
2300 .inum = inode.bi_inum,
2301 .snapshot = snapshot,
2306 snapshot = parent_snapshot;
2308 bch2_trans_iter_exit(trans, &inode_iter);
2309 inode_k = bch2_bkey_get_iter(trans, &inode_iter, BTREE_ID_inodes,
2310 SPOS(0, inode.bi_dir, snapshot), 0);
2311 ret = bkey_err(inode_k) ?:
2312 !bkey_is_inode(inode_k.k) ? -BCH_ERR_ENOENT_inode
2313 : bch2_inode_unpack(inode_k, &inode);
2315 /* Should have been caught in dirents pass */
2316 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart))
2317 bch_err(c, "error looking up parent directory: %i", ret);
2321 snapshot = inode_k.k->p.snapshot;
2323 if (path_is_dup(p, inode.bi_inum, snapshot)) {
2324 /* XXX print path */
2325 bch_err(c, "directory structure loop");
2327 darray_for_each(*p, i)
2328 pr_err("%llu:%u", i->inum, i->snapshot);
2329 pr_err("%llu:%u", inode.bi_inum, snapshot);
2331 if (fsck_err(c, dir_loop, "directory structure loop")) {
2332 ret = remove_backpointer(trans, &inode);
2333 bch_err_msg(c, ret, "removing dirent");
2337 ret = reattach_inode(trans, &inode, snapshot);
2338 bch_err_msg(c, ret, "reattaching inode %llu", inode.bi_inum);
2345 bch2_trans_iter_exit(trans, &inode_iter);
2346 printbuf_exit(&buf);
2352 * Check for unreachable inodes, as well as loops in the directory structure:
2353 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2356 int bch2_check_directory_structure(struct bch_fs *c)
2358 pathbuf path = { 0, };
2361 ret = bch2_trans_run(c,
2362 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes, POS_MIN,
2364 BTREE_ITER_PREFETCH|
2365 BTREE_ITER_ALL_SNAPSHOTS, k,
2366 NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
2367 if (!bkey_is_inode(k.k))
2370 if (bch2_inode_flags(k) & BCH_INODE_unlinked)
2373 check_path(trans, &path, k);
2381 struct nlink_table {
2392 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2393 u64 inum, u32 snapshot)
2395 if (t->nr == t->size) {
2396 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2397 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2400 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2402 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2406 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2414 t->d[t->nr++] = (struct nlink) {
2416 .snapshot = snapshot,
2422 static int nlink_cmp(const void *_l, const void *_r)
2424 const struct nlink *l = _l;
2425 const struct nlink *r = _r;
2427 return cmp_int(l->inum, r->inum);
2430 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2431 struct nlink_table *links,
2432 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2434 struct nlink *link, key = {
2435 .inum = inum, .snapshot = U32_MAX,
2438 if (inum < range_start || inum >= range_end)
2441 link = __inline_bsearch(&key, links->d, links->nr,
2442 sizeof(links->d[0]), nlink_cmp);
2446 while (link > links->d && link[0].inum == link[-1].inum)
2449 for (; link < links->d + links->nr && link->inum == inum; link++)
2450 if (ref_visible(c, s, snapshot, link->snapshot)) {
2452 if (link->snapshot >= snapshot)
2458 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2459 struct nlink_table *t,
2460 u64 start, u64 *end)
2462 int ret = bch2_trans_run(c,
2463 for_each_btree_key(trans, iter, BTREE_ID_inodes,
2466 BTREE_ITER_PREFETCH|
2467 BTREE_ITER_ALL_SNAPSHOTS, k, ({
2468 if (!bkey_is_inode(k.k))
2471 /* Should never fail, checked by bch2_inode_invalid: */
2472 struct bch_inode_unpacked u;
2473 BUG_ON(bch2_inode_unpack(k, &u));
2476 * Backpointer and directory structure checks are sufficient for
2477 * directories, since they can't have hardlinks:
2479 if (S_ISDIR(u.bi_mode))
2485 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2487 *end = k.k->p.offset;
2499 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2500 u64 range_start, u64 range_end)
2502 struct snapshots_seen s;
2504 snapshots_seen_init(&s);
2506 int ret = bch2_trans_run(c,
2507 for_each_btree_key(trans, iter, BTREE_ID_dirents, POS_MIN,
2509 BTREE_ITER_PREFETCH|
2510 BTREE_ITER_ALL_SNAPSHOTS, k, ({
2511 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2515 if (k.k->type == KEY_TYPE_dirent) {
2516 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
2518 if (d.v->d_type != DT_DIR &&
2519 d.v->d_type != DT_SUBVOL)
2520 inc_link(c, &s, links, range_start, range_end,
2521 le64_to_cpu(d.v->d_inum),
2522 bch2_snapshot_equiv(c, d.k->p.snapshot));
2527 snapshots_seen_exit(&s);
2533 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2535 struct nlink_table *links,
2536 size_t *idx, u64 range_end)
2538 struct bch_fs *c = trans->c;
2539 struct bch_inode_unpacked u;
2540 struct nlink *link = &links->d[*idx];
2543 if (k.k->p.offset >= range_end)
2546 if (!bkey_is_inode(k.k))
2549 BUG_ON(bch2_inode_unpack(k, &u));
2551 if (S_ISDIR(u.bi_mode))
2557 while ((cmp_int(link->inum, k.k->p.offset) ?:
2558 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2559 BUG_ON(*idx == links->nr);
2560 link = &links->d[++*idx];
2563 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count,
2564 c, inode_wrong_nlink,
2565 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2566 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2567 bch2_inode_nlink_get(&u), link->count)) {
2568 bch2_inode_nlink_set(&u, link->count);
2569 ret = __bch2_fsck_write_inode(trans, &u, k.k->p.snapshot);
2576 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2577 struct nlink_table *links,
2578 u64 range_start, u64 range_end)
2582 int ret = bch2_trans_run(c,
2583 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
2584 POS(0, range_start),
2585 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2586 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2587 check_nlinks_update_inode(trans, &iter, k, links, &idx, range_end)));
2589 bch_err(c, "error in fsck walking inodes: %s", bch2_err_str(ret));
2596 int bch2_check_nlinks(struct bch_fs *c)
2598 struct nlink_table links = { 0 };
2599 u64 this_iter_range_start, next_iter_range_start = 0;
2603 this_iter_range_start = next_iter_range_start;
2604 next_iter_range_start = U64_MAX;
2606 ret = check_nlinks_find_hardlinks(c, &links,
2607 this_iter_range_start,
2608 &next_iter_range_start);
2610 ret = check_nlinks_walk_dirents(c, &links,
2611 this_iter_range_start,
2612 next_iter_range_start);
2616 ret = check_nlinks_update_hardlinks(c, &links,
2617 this_iter_range_start,
2618 next_iter_range_start);
2623 } while (next_iter_range_start != U64_MAX);
2630 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2633 struct bkey_s_c_reflink_p p;
2634 struct bkey_i_reflink_p *u;
2636 if (k.k->type != KEY_TYPE_reflink_p)
2639 p = bkey_s_c_to_reflink_p(k);
2641 if (!p.v->front_pad && !p.v->back_pad)
2644 u = bch2_trans_kmalloc(trans, sizeof(*u));
2645 int ret = PTR_ERR_OR_ZERO(u);
2649 bkey_reassemble(&u->k_i, k);
2653 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2656 int bch2_fix_reflink_p(struct bch_fs *c)
2658 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2661 int ret = bch2_trans_run(c,
2662 for_each_btree_key_commit(trans, iter,
2663 BTREE_ID_extents, POS_MIN,
2664 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2665 BTREE_ITER_ALL_SNAPSHOTS, k,
2666 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2667 fix_reflink_p_key(trans, &iter, k)));