1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
12 #include "subvolume.h"
16 #include <linux/bsearch.h>
17 #include <linux/dcache.h> /* struct qstr */
19 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
21 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
24 struct btree_iter iter;
29 for_each_btree_key(trans, iter, BTREE_ID_extents,
30 SPOS(inum, 0, snapshot), 0, k, ret) {
31 if (k.k->p.inode != inum)
34 if (bkey_extent_is_allocation(k.k))
38 bch2_trans_iter_exit(trans, &iter);
40 return ret ?: sectors;
43 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
46 struct btree_iter iter;
48 struct bkey_s_c_dirent d;
52 for_each_btree_key(trans, iter, BTREE_ID_dirents,
53 SPOS(inum, 0, snapshot), 0, k, ret) {
54 if (k.k->p.inode != inum)
57 if (k.k->type != KEY_TYPE_dirent)
60 d = bkey_s_c_to_dirent(k);
61 if (d.v->d_type == DT_DIR)
65 bch2_trans_iter_exit(trans, &iter);
67 return ret ?: subdirs;
70 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
73 struct btree_iter iter;
77 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
79 k = bch2_btree_iter_peek_slot(&iter);
84 if (k.k->type != KEY_TYPE_snapshot) {
85 bch_err(trans->c, "snapshot %u not fonud", snapshot);
90 *subvol = le32_to_cpu(bkey_s_c_to_snapshot(k).v->subvol);
92 bch2_trans_iter_exit(trans, &iter);
97 static int snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
100 return lockrestart_do(trans, __snapshot_lookup_subvol(trans, snapshot, subvol));
103 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
104 u32 *snapshot, u64 *inum)
106 struct btree_iter iter;
110 bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes,
112 k = bch2_btree_iter_peek_slot(&iter);
117 if (k.k->type != KEY_TYPE_subvolume) {
118 bch_err(trans->c, "subvolume %u not fonud", subvol);
123 *snapshot = le32_to_cpu(bkey_s_c_to_subvolume(k).v->snapshot);
124 *inum = le64_to_cpu(bkey_s_c_to_subvolume(k).v->inode);
126 bch2_trans_iter_exit(trans, &iter);
131 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
132 u32 *snapshot, u64 *inum)
134 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
137 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
138 struct bch_inode_unpacked *inode,
141 struct btree_iter iter;
145 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
146 SPOS(0, inode_nr, *snapshot), 0);
147 k = bch2_btree_iter_peek_slot(&iter);
152 *snapshot = iter.pos.snapshot;
153 ret = k.k->type == KEY_TYPE_inode
154 ? bch2_inode_unpack(bkey_s_c_to_inode(k), inode)
157 bch2_trans_iter_exit(trans, &iter);
161 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
162 struct bch_inode_unpacked *inode,
165 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
168 static int __lookup_dirent(struct btree_trans *trans,
169 struct bch_hash_info hash_info,
170 subvol_inum dir, struct qstr *name,
171 u64 *target, unsigned *type)
173 struct btree_iter iter;
174 struct bkey_s_c_dirent d;
177 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
178 &hash_info, dir, name, 0);
182 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
183 *target = le64_to_cpu(d.v->d_inum);
185 bch2_trans_iter_exit(trans, &iter);
189 static int lookup_dirent(struct btree_trans *trans,
190 struct bch_hash_info hash_info,
191 subvol_inum dir, struct qstr *name,
192 u64 *target, unsigned *type)
194 return lockrestart_do(trans,
195 __lookup_dirent(trans, hash_info, dir, name, target, type));
198 static int __write_inode(struct btree_trans *trans,
199 struct bch_inode_unpacked *inode,
202 struct btree_iter iter;
205 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
206 SPOS(0, inode->bi_inum, snapshot),
209 ret = bch2_btree_iter_traverse(&iter) ?:
210 bch2_inode_write(trans, &iter, inode);
211 bch2_trans_iter_exit(trans, &iter);
215 static int write_inode(struct btree_trans *trans,
216 struct bch_inode_unpacked *inode,
219 int ret = __bch2_trans_do(trans, NULL, NULL,
221 BTREE_INSERT_LAZY_RW,
222 __write_inode(trans, inode, snapshot));
224 bch_err(trans->c, "error in fsck: error %i updating inode", ret);
228 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
230 struct btree_iter iter = { NULL };
231 struct bkey_i_inode_generation delete;
232 struct bch_inode_unpacked inode_u;
236 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
237 SPOS(inum, 0, snapshot),
238 SPOS(inum, U64_MAX, snapshot),
240 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
241 SPOS(inum, 0, snapshot),
242 SPOS(inum, U64_MAX, snapshot),
244 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
245 SPOS(inum, 0, snapshot),
246 SPOS(inum, U64_MAX, snapshot),
251 bch2_trans_begin(trans);
253 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
254 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
255 k = bch2_btree_iter_peek_slot(&iter);
261 if (k.k->type != KEY_TYPE_inode) {
262 bch2_fs_inconsistent(trans->c,
263 "inode %llu:%u not found when deleting",
269 bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u);
271 /* Subvolume root? */
272 if (inode_u.bi_subvol) {
273 ret = bch2_subvolume_delete(trans, inode_u.bi_subvol, -1);
278 bkey_inode_generation_init(&delete.k_i);
279 delete.k.p = iter.pos;
280 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
282 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
283 bch2_trans_commit(trans, NULL, NULL,
284 BTREE_INSERT_NOFAIL);
286 bch2_trans_iter_exit(trans, &iter);
293 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
295 struct bch_fs *c = trans->c;
296 struct btree_iter iter;
297 struct bch_inode_unpacked dir_inode;
298 struct bch_hash_info dir_hash_info;
301 ret = lookup_inode(trans, pos.inode, &dir_inode, NULL);
305 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
307 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
309 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
310 &dir_hash_info, &iter, 0);
311 bch2_trans_iter_exit(trans, &iter);
315 static int remove_dirent(struct btree_trans *trans, struct bpos pos)
317 int ret = __bch2_trans_do(trans, NULL, NULL,
319 BTREE_INSERT_LAZY_RW,
320 __remove_dirent(trans, pos));
322 bch_err(trans->c, "remove_dirent: err %i deleting dirent", ret);
326 /* Get lost+found, create if it doesn't exist: */
327 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
328 struct bch_inode_unpacked *lostfound)
330 struct bch_fs *c = trans->c;
331 struct bch_inode_unpacked root;
332 struct bch_hash_info root_hash_info;
333 struct qstr lostfound_str = QSTR("lost+found");
334 subvol_inum root_inum = { .subvol = subvol };
340 ret = subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
344 ret = lookup_inode(trans, root_inum.inum, &root, &snapshot);
346 bch_err(c, "error fetching subvol root: %i", ret);
350 root_hash_info = bch2_hash_info_init(c, &root);
352 ret = lookup_dirent(trans, root_hash_info, root_inum,
353 &lostfound_str, &inum, &d_type);
354 if (ret == -ENOENT) {
355 bch_notice(c, "creating lost+found");
356 goto create_lostfound;
360 bch_err(c, "error looking up lost+found: %i", ret);
364 if (d_type != DT_DIR) {
365 bch_err(c, "error looking up lost+found: not a directory");
370 ret = lookup_inode(trans, inum, lostfound, &snapshot);
371 if (ret && ret != -ENOENT) {
373 * The check_dirents pass has already run, dangling dirents
374 * shouldn't exist here:
376 bch_err(c, "error looking up lost+found: %i", ret);
380 if (ret == -ENOENT) {
382 bch2_inode_init_early(c, lostfound);
384 ret = __bch2_trans_do(trans, NULL, NULL,
386 BTREE_INSERT_LAZY_RW,
387 bch2_create_trans(trans, root_inum, &root,
388 lostfound, &lostfound_str,
389 0, 0, S_IFDIR|0700, 0, NULL, NULL,
390 (subvol_inum) { }, 0));
392 bch_err(c, "error creating lost+found: %i", ret);
398 static int reattach_inode(struct btree_trans *trans,
399 struct bch_inode_unpacked *inode,
402 struct bch_hash_info dir_hash;
403 struct bch_inode_unpacked lostfound;
410 ret = snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
414 ret = lookup_lostfound(trans, subvol, &lostfound);
418 if (S_ISDIR(inode->bi_mode)) {
419 lostfound.bi_nlink++;
421 ret = write_inode(trans, &lostfound, U32_MAX);
426 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
428 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
429 name = (struct qstr) QSTR(name_buf);
431 ret = __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
432 bch2_dirent_create(trans,
435 .inum = lostfound.bi_inum,
438 mode_to_type(inode->bi_mode),
439 &name, inode->bi_inum, &dir_offset,
440 BCH_HASH_SET_MUST_CREATE));
442 bch_err(trans->c, "error %i reattaching inode %llu",
443 ret, inode->bi_inum);
447 inode->bi_dir = lostfound.bi_inum;
448 inode->bi_dir_offset = dir_offset;
450 return write_inode(trans, inode, inode_snapshot);
453 static int remove_backpointer(struct btree_trans *trans,
454 struct bch_inode_unpacked *inode)
456 struct btree_iter iter;
460 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
461 POS(inode->bi_dir, inode->bi_dir_offset), 0);
462 k = bch2_btree_iter_peek_slot(&iter);
466 if (k.k->type != KEY_TYPE_dirent) {
471 ret = remove_dirent(trans, k.k->p);
473 bch2_trans_iter_exit(trans, &iter);
477 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s, struct bpos pos)
479 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
481 if (bkey_cmp(s->pos, pos))
485 /* Might get called multiple times due to lock restarts */
486 if (s->nr && s->d[s->nr - 1] == pos.snapshot)
489 return snapshots_seen_add(c, s, pos.snapshot);
493 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
494 * and @ancestor hasn't been overwritten in @seen
496 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
498 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
499 u32 id, u32 ancestor)
503 BUG_ON(id > ancestor);
505 id = snapshot_t(c, id)->equiv;
506 ancestor = snapshot_t(c, ancestor)->equiv;
508 /* @ancestor should be the snapshot most recently added to @seen */
509 BUG_ON(!seen->nr || seen->d[seen->nr - 1] != ancestor);
510 BUG_ON(seen->pos.snapshot != ancestor);
515 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
518 for (i = seen->nr - 2;
519 i >= 0 && seen->d[i] >= id;
521 if (bch2_snapshot_is_ancestor(c, id, seen->d[i]) &&
522 bch2_snapshot_is_ancestor(c, seen->d[i], ancestor))
529 * ref_visible - given a key with snapshot id @src that points to a key with
530 * snapshot id @dst, test whether there is some snapshot in which @dst is
533 * This assumes we're visiting @src keys in natural key order.
535 * @s - list of snapshot IDs already seen at @src
536 * @src - snapshot ID of src key
537 * @dst - snapshot ID of dst key
539 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
543 ? key_visible_in_snapshot(c, s, dst, src)
544 : bch2_snapshot_is_ancestor(c, src, dst);
547 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
548 for (_i = (_w)->d; _i < (_w)->d + (_w)->nr && (_i)->snapshot <= (_snapshot); _i++)\
549 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
551 struct inode_walker {
552 bool first_this_inode;
557 struct inode_walker_entry {
558 struct bch_inode_unpacked inode;
564 static void inode_walker_exit(struct inode_walker *w)
570 static struct inode_walker inode_walker_init(void)
572 return (struct inode_walker) { 0, };
575 static int inode_walker_realloc(struct inode_walker *w)
577 if (w->nr == w->size) {
578 size_t new_size = max_t(size_t, 8UL, w->size * 2);
579 void *d = krealloc(w->d, new_size * sizeof(w->d[0]),
591 static int add_inode(struct bch_fs *c, struct inode_walker *w,
592 struct bkey_s_c_inode inode)
594 struct bch_inode_unpacked u;
597 ret = inode_walker_realloc(w);
601 BUG_ON(bch2_inode_unpack(inode, &u));
603 w->d[w->nr++] = (struct inode_walker_entry) {
605 .snapshot = snapshot_t(c, inode.k->p.snapshot)->equiv,
611 static int __walk_inode(struct btree_trans *trans,
612 struct inode_walker *w, struct bpos pos)
614 struct bch_fs *c = trans->c;
615 struct btree_iter iter;
617 unsigned i, ancestor_pos;
620 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
622 if (pos.inode == w->cur_inum) {
623 w->first_this_inode = false;
624 goto lookup_snapshot;
629 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
630 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
631 if (k.k->p.offset != pos.inode)
634 if (k.k->type == KEY_TYPE_inode)
635 add_inode(c, w, bkey_s_c_to_inode(k));
637 bch2_trans_iter_exit(trans, &iter);
642 w->cur_inum = pos.inode;
643 w->first_this_inode = true;
645 for (i = 0; i < w->nr; i++)
646 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->d[i].snapshot))
650 BUG_ON(pos.snapshot > w->d[i].snapshot);
652 if (pos.snapshot != w->d[i].snapshot) {
655 while (i && w->d[i - 1].snapshot > pos.snapshot)
658 ret = inode_walker_realloc(w);
662 array_insert_item(w->d, w->nr, i, w->d[ancestor_pos]);
663 w->d[i].snapshot = pos.snapshot;
670 static int walk_inode(struct btree_trans *trans,
671 struct inode_walker *w, struct bpos pos)
673 return lockrestart_do(trans, __walk_inode(trans, w, pos));
676 static int __get_visible_inodes(struct btree_trans *trans,
677 struct inode_walker *w,
678 struct snapshots_seen *s,
681 struct bch_fs *c = trans->c;
682 struct btree_iter iter;
688 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
689 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
690 if (k.k->p.offset != inum)
693 if (k.k->type != KEY_TYPE_inode)
696 if (ref_visible(c, s, s->pos.snapshot, k.k->p.snapshot)) {
697 add_inode(c, w, bkey_s_c_to_inode(k));
698 if (k.k->p.snapshot >= s->pos.snapshot)
702 bch2_trans_iter_exit(trans, &iter);
707 static int check_key_has_snapshot(struct btree_trans *trans,
708 struct btree_iter *iter,
711 struct bch_fs *c = trans->c;
715 if (fsck_err_on(!snapshot_t(c, k.k->p.snapshot)->equiv, c,
716 "key in missing snapshot: %s",
717 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf))) {
718 ret = __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
719 bch2_btree_delete_at(trans, iter,
720 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
721 return ret ?: -EINTR;
727 static int hash_redo_key(struct btree_trans *trans,
728 const struct bch_hash_desc desc,
729 struct bch_hash_info *hash_info,
730 struct btree_iter *k_iter, struct bkey_s_c k)
732 bch_err(trans->c, "hash_redo_key() not implemented yet");
735 struct bkey_i *delete;
738 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
740 return PTR_ERR(delete);
742 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
746 bkey_reassemble(tmp, k);
748 bkey_init(&delete->k);
749 delete->k.p = k_iter->pos;
750 return bch2_btree_iter_traverse(k_iter) ?:
751 bch2_trans_update(trans, k_iter, delete, 0) ?:
752 bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode, tmp, 0);
756 static int fsck_hash_delete_at(struct btree_trans *trans,
757 const struct bch_hash_desc desc,
758 struct bch_hash_info *info,
759 struct btree_iter *iter)
763 ret = bch2_hash_delete_at(trans, desc, info, iter, 0) ?:
764 bch2_trans_commit(trans, NULL, NULL,
766 BTREE_INSERT_LAZY_RW);
768 ret = bch2_btree_iter_traverse(iter);
776 static int hash_check_key(struct btree_trans *trans,
777 const struct bch_hash_desc desc,
778 struct bch_hash_info *hash_info,
779 struct btree_iter *k_iter, struct bkey_s_c hash_k)
781 struct bch_fs *c = trans->c;
782 struct btree_iter iter = { NULL };
788 if (hash_k.k->type != desc.key_type)
791 hash = desc.hash_bkey(hash_info, hash_k);
793 if (likely(hash == hash_k.k->p.offset))
796 if (hash_k.k->p.offset < hash)
799 for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
800 BTREE_ITER_SLOTS, k, ret) {
801 if (!bkey_cmp(k.k->p, hash_k.k->p))
804 if (fsck_err_on(k.k->type == desc.key_type &&
805 !desc.cmp_bkey(k, hash_k), c,
806 "duplicate hash table keys:\n%s",
807 (bch2_bkey_val_to_text(&PBUF(buf), c,
809 ret = fsck_hash_delete_at(trans, desc, hash_info, k_iter);
816 if (bkey_deleted(k.k)) {
817 bch2_trans_iter_exit(trans, &iter);
822 bch2_trans_iter_exit(trans, &iter);
825 if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
826 "hashed to %llu\n%s",
827 desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset, hash,
828 (bch2_bkey_val_to_text(&PBUF(buf), c, hash_k), buf)) == FSCK_ERR_IGNORE)
831 ret = __bch2_trans_do(trans, NULL, NULL,
832 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
833 hash_redo_key(trans, desc, hash_info, k_iter, hash_k));
835 bch_err(c, "hash_redo_key err %i", ret);
843 static int check_inode(struct btree_trans *trans,
844 struct btree_iter *iter,
845 struct bch_inode_unpacked *prev,
846 struct bch_inode_unpacked u)
848 struct bch_fs *c = trans->c;
849 bool do_update = false;
852 if (fsck_err_on(prev &&
853 (prev->bi_hash_seed != u.bi_hash_seed ||
854 mode_to_type(prev->bi_mode) != mode_to_type(u.bi_mode)), c,
855 "inodes in different snapshots don't match")) {
856 bch_err(c, "repair not implemented yet");
860 if (u.bi_flags & BCH_INODE_UNLINKED &&
862 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
864 bch2_trans_unlock(trans);
867 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
869 bch_err(c, "error in fsck: error %i while deleting inode", ret);
873 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
875 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
877 bch_verbose(c, "truncating inode %llu", u.bi_inum);
879 bch2_trans_unlock(trans);
883 * XXX: need to truncate partial blocks too here - or ideally
884 * just switch units to bytes and that issue goes away
886 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
887 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
889 POS(u.bi_inum, U64_MAX),
892 bch_err(c, "error in fsck: error %i truncating inode", ret);
897 * We truncated without our normal sector accounting hook, just
898 * make sure we recalculate it:
900 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
902 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
906 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
908 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
912 bch_verbose(c, "recounting sectors for inode %llu",
915 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
917 bch_err(c, "error in fsck: error %i recounting inode sectors",
922 u.bi_sectors = sectors;
923 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
927 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
930 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
935 ret = write_inode(trans, &u, iter->pos.snapshot);
937 bch_err(c, "error in fsck: error %i "
938 "updating inode", ret);
945 static int check_inodes(struct bch_fs *c, bool full)
947 struct btree_trans trans;
948 struct btree_iter iter;
950 struct bkey_s_c_inode inode;
951 struct bch_inode_unpacked prev, u;
954 memset(&prev, 0, sizeof(prev));
956 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
958 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
961 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
962 ret = check_key_has_snapshot(&trans, &iter, k);
967 * if snapshot id isn't a leaf node, skip it - deletion in
968 * particular is not atomic, so on the internal snapshot nodes
969 * we can see inodes marked for deletion after a clean shutdown
971 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
974 if (k.k->type != KEY_TYPE_inode)
977 inode = bkey_s_c_to_inode(k);
980 !(inode.v->bi_flags & (BCH_INODE_I_SIZE_DIRTY|
981 BCH_INODE_I_SECTORS_DIRTY|
982 BCH_INODE_UNLINKED)))
985 BUG_ON(bch2_inode_unpack(inode, &u));
987 ret = check_inode(&trans, &iter,
988 full && prev.bi_inum == u.bi_inum
995 bch2_trans_iter_exit(&trans, &iter);
997 BUG_ON(ret == -EINTR);
999 return bch2_trans_exit(&trans) ?: ret;
1003 static int check_subvols(struct bch_fs *c)
1005 struct btree_trans trans;
1006 struct btree_iter iter;
1010 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1012 for_each_btree_key(&trans, iter, BTREE_ID_subvolumes, POS_MIN,
1015 bch2_trans_iter_exit(&trans, &iter);
1017 bch2_trans_exit(&trans);
1022 * Checking for overlapping extents needs to be reimplemented
1025 static int fix_overlapping_extent(struct btree_trans *trans,
1026 struct bkey_s_c k, struct bpos cut_at)
1028 struct btree_iter iter;
1032 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1033 ret = PTR_ERR_OR_ZERO(u);
1037 bkey_reassemble(u, k);
1038 bch2_cut_front(cut_at, u);
1042 * We don't want to go through the extent_handle_overwrites path:
1044 * XXX: this is going to screw up disk accounting, extent triggers
1045 * assume things about extent overwrites - we should be running the
1046 * triggers manually here
1048 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1049 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1051 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1052 ret = bch2_btree_iter_traverse(&iter) ?:
1053 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1054 bch2_trans_commit(trans, NULL, NULL,
1055 BTREE_INSERT_NOFAIL|
1056 BTREE_INSERT_LAZY_RW);
1057 bch2_trans_iter_exit(trans, &iter);
1062 static int inode_backpointer_exists(struct btree_trans *trans,
1063 struct bch_inode_unpacked *inode,
1066 struct btree_iter iter;
1070 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
1071 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot), 0);
1072 k = bch2_btree_iter_peek_slot(&iter);
1076 if (k.k->type != KEY_TYPE_dirent)
1079 ret = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum) == inode->bi_inum;
1081 bch2_trans_iter_exit(trans, &iter);
1085 static bool inode_backpointer_matches(struct bkey_s_c_dirent d,
1086 struct bch_inode_unpacked *inode)
1088 return d.k->p.inode == inode->bi_dir &&
1089 d.k->p.offset == inode->bi_dir_offset;
1092 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1094 struct bch_fs *c = trans->c;
1095 struct inode_walker_entry *i;
1096 int ret = 0, ret2 = 0;
1099 for (i = w->d; i < w->d + w->nr; i++) {
1100 if (i->inode.bi_sectors == i->count)
1103 count2 = lockrestart_do(trans,
1104 bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot));
1106 if (i->count != count2) {
1107 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1110 if (i->inode.bi_sectors == i->count)
1114 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1115 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1116 w->cur_inum, i->snapshot,
1117 i->inode.bi_sectors, i->count) == FSCK_ERR_IGNORE)
1120 i->inode.bi_sectors = i->count;
1121 ret = write_inode(trans, &i->inode, i->snapshot);
1130 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1131 struct inode_walker *inode,
1132 struct snapshots_seen *s)
1134 struct bch_fs *c = trans->c;
1136 struct inode_walker_entry *i;
1140 k = bch2_btree_iter_peek(iter);
1148 ret = check_key_has_snapshot(trans, iter, k);
1152 ret = snapshots_seen_update(c, s, k.k->p);
1156 if (k.k->type == KEY_TYPE_whiteout)
1159 if (inode->cur_inum != k.k->p.inode) {
1160 ret = check_i_sectors(trans, inode);
1165 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
1169 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
1170 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
1172 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
1173 return fix_overlapping_extent(trans, k, prev.k->k.p) ?: -EINTR;
1176 ret = __walk_inode(trans, inode, k.k->p);
1180 if (fsck_err_on(ret == INT_MAX, c,
1181 "extent in missing inode:\n %s",
1182 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1183 return __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
1184 bch2_btree_delete_at(trans, iter,
1185 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
1193 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1194 !S_ISLNK(i->inode.bi_mode), c,
1195 "extent in non regular inode mode %o:\n %s",
1197 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1198 return __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
1199 bch2_btree_delete_at(trans, iter,
1200 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
1202 if (!bch2_snapshot_internal_node(c, k.k->p.snapshot)) {
1203 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i) {
1204 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1205 k.k->type != KEY_TYPE_reservation &&
1206 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9, c,
1207 "extent type %u offset %llu past end of inode %llu, i_size %llu",
1208 k.k->type, k.k->p.offset, k.k->p.inode, i->inode.bi_size)) {
1210 return bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1211 SPOS(k.k->p.inode, round_up(i->inode.bi_size, block_bytes(c)) >> 9,
1213 POS(k.k->p.inode, U64_MAX),
1219 if (bkey_extent_is_allocation(k.k))
1220 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i)
1221 i->count += k.k->size;
1223 bch2_bkey_buf_reassemble(&prev, c, k);
1231 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1232 * that i_size an i_sectors are consistent
1235 static int check_extents(struct bch_fs *c)
1237 struct inode_walker w = inode_walker_init();
1238 struct snapshots_seen s;
1239 struct btree_trans trans;
1240 struct btree_iter iter;
1244 struct bkey_buf prev;
1245 bch2_bkey_buf_init(&prev);
1246 prev.k->k = KEY(0, 0, 0);
1248 snapshots_seen_init(&s);
1249 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1251 bch_verbose(c, "checking extents");
1253 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
1254 POS(BCACHEFS_ROOT_INO, 0),
1256 BTREE_ITER_PREFETCH|
1257 BTREE_ITER_ALL_SNAPSHOTS);
1260 ret = lockrestart_do(&trans,
1261 check_extent(&trans, &iter, &w, &s));
1264 } while (bch2_btree_iter_advance(&iter));
1265 bch2_trans_iter_exit(&trans, &iter);
1267 bch2_bkey_buf_exit(&prev, c);
1269 inode_walker_exit(&w);
1270 bch2_trans_exit(&trans);
1271 snapshots_seen_exit(&s);
1276 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1278 struct bch_fs *c = trans->c;
1279 struct inode_walker_entry *i;
1280 int ret = 0, ret2 = 0;
1283 for (i = w->d; i < w->d + w->nr; i++) {
1284 if (i->inode.bi_nlink == i->count)
1287 count2 = lockrestart_do(trans,
1288 bch2_count_subdirs(trans, w->cur_inum, i->snapshot));
1290 if (i->count != count2) {
1291 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1294 if (i->inode.bi_nlink == i->count)
1298 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1299 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1300 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1301 i->inode.bi_nlink = i->count;
1302 ret = write_inode(trans, &i->inode, i->snapshot);
1312 static int check_dirent_target(struct btree_trans *trans,
1313 struct btree_iter *iter,
1314 struct bkey_s_c_dirent d,
1315 struct bch_inode_unpacked *target,
1316 u32 target_snapshot)
1318 struct bch_fs *c = trans->c;
1319 bool backpointer_exists = true;
1323 if (!target->bi_dir &&
1324 !target->bi_dir_offset) {
1325 target->bi_dir = d.k->p.inode;
1326 target->bi_dir_offset = d.k->p.offset;
1328 ret = write_inode(trans, target, target_snapshot);
1333 if (!inode_backpointer_matches(d, target)) {
1334 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1338 backpointer_exists = ret;
1341 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1342 backpointer_exists, c,
1343 "directory %llu with multiple links",
1345 ret = remove_dirent(trans, d.k->p);
1351 if (fsck_err_on(backpointer_exists &&
1352 !target->bi_nlink, c,
1353 "inode %llu has multiple links but i_nlink 0",
1356 target->bi_flags &= ~BCH_INODE_UNLINKED;
1358 ret = write_inode(trans, target, target_snapshot);
1363 if (fsck_err_on(!backpointer_exists, c,
1364 "inode %llu has wrong backpointer:\n"
1366 "should be %llu:%llu",
1369 target->bi_dir_offset,
1372 target->bi_dir = d.k->p.inode;
1373 target->bi_dir_offset = d.k->p.offset;
1375 ret = write_inode(trans, target, target_snapshot);
1381 if (fsck_err_on(vfs_d_type(d.v->d_type) != mode_to_type(target->bi_mode), c,
1382 "incorrect d_type: should be %u:\n%s",
1383 mode_to_type(target->bi_mode),
1384 (bch2_bkey_val_to_text(&PBUF(buf), c, d.s_c), buf))) {
1385 struct bkey_i_dirent *n;
1387 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
1393 bkey_reassemble(&n->k_i, d.s_c);
1394 n->v.d_type = mode_to_type(target->bi_mode);
1396 ret = __bch2_trans_do(trans, NULL, NULL,
1397 BTREE_INSERT_NOFAIL|
1398 BTREE_INSERT_LAZY_RW,
1399 bch2_trans_update(trans, iter, &n->k_i, 0));
1409 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1410 struct bch_hash_info *hash_info,
1411 struct inode_walker *dir,
1412 struct inode_walker *target,
1413 struct snapshots_seen *s)
1415 struct bch_fs *c = trans->c;
1417 struct bkey_s_c_dirent d;
1418 struct inode_walker_entry *i;
1419 u32 target_snapshot;
1425 k = bch2_btree_iter_peek(iter);
1433 ret = check_key_has_snapshot(trans, iter, k);
1437 ret = snapshots_seen_update(c, s, k.k->p);
1441 if (k.k->type == KEY_TYPE_whiteout)
1444 if (dir->cur_inum != k.k->p.inode) {
1445 ret = check_subdir_count(trans, dir);
1450 ret = __walk_inode(trans, dir, k.k->p);
1454 if (fsck_err_on(ret == INT_MAX, c,
1455 "dirent in nonexisting directory:\n%s",
1456 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1457 return __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
1458 bch2_btree_delete_at(trans, iter,
1459 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
1467 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1468 "dirent in non directory inode type %u:\n%s",
1469 mode_to_type(i->inode.bi_mode),
1470 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1471 return __bch2_trans_do(trans, NULL, NULL, 0,
1472 bch2_btree_delete_at(trans, iter, 0));
1474 if (dir->first_this_inode)
1475 *hash_info = bch2_hash_info_init(c, &dir->d[0].inode);
1477 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1478 hash_info, iter, k);
1481 if (ret) /* dirent has been deleted */
1484 if (k.k->type != KEY_TYPE_dirent)
1487 d = bkey_s_c_to_dirent(k);
1489 ret = __bch2_dirent_read_target(trans, d,
1494 if (ret && ret != -ENOENT)
1497 if (fsck_err_on(ret, c,
1498 "dirent points to missing subvolume %llu",
1499 le64_to_cpu(d.v->d_inum)))
1500 return remove_dirent(trans, d.k->p);
1502 if (target_subvol) {
1503 struct bch_inode_unpacked subvol_root;
1505 ret = __lookup_inode(trans, target_inum,
1506 &subvol_root, &target_snapshot);
1507 if (ret && ret != -ENOENT)
1510 if (fsck_err_on(ret, c,
1511 "subvolume %u points to missing subvolume root %llu",
1514 bch_err(c, "repair not implemented yet");
1518 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1519 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1521 subvol_root.bi_subvol, target_subvol)) {
1522 subvol_root.bi_subvol = target_subvol;
1523 ret = write_inode(trans, &subvol_root, target_snapshot);
1528 ret = check_dirent_target(trans, iter, d, &subvol_root,
1533 ret = __get_visible_inodes(trans, target, s, target_inum);
1537 if (fsck_err_on(!target->nr, c,
1538 "dirent points to missing inode:\n%s",
1539 (bch2_bkey_val_to_text(&PBUF(buf), c,
1541 ret = remove_dirent(trans, d.k->p);
1546 for (i = target->d; i < target->d + target->nr; i++) {
1547 ret = check_dirent_target(trans, iter, d,
1548 &i->inode, i->snapshot);
1554 if (d.v->d_type == DT_DIR)
1555 for_each_visible_inode(c, s, dir, d.k->p.snapshot, i)
1563 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1567 static int check_dirents(struct bch_fs *c)
1569 struct inode_walker dir = inode_walker_init();
1570 struct inode_walker target = inode_walker_init();
1571 struct snapshots_seen s;
1572 struct bch_hash_info hash_info;
1573 struct btree_trans trans;
1574 struct btree_iter iter;
1577 bch_verbose(c, "checking dirents");
1579 snapshots_seen_init(&s);
1580 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1582 bch2_trans_iter_init(&trans, &iter, BTREE_ID_dirents,
1583 POS(BCACHEFS_ROOT_INO, 0),
1585 BTREE_ITER_PREFETCH|
1586 BTREE_ITER_ALL_SNAPSHOTS);
1589 ret = lockrestart_do(&trans,
1590 check_dirent(&trans, &iter, &hash_info,
1591 &dir, &target, &s));
1594 } while (bch2_btree_iter_advance(&iter));
1595 bch2_trans_iter_exit(&trans, &iter);
1597 bch2_trans_exit(&trans);
1598 snapshots_seen_exit(&s);
1599 inode_walker_exit(&dir);
1600 inode_walker_exit(&target);
1605 * Walk xattrs: verify that they all have a corresponding inode
1608 static int check_xattrs(struct bch_fs *c)
1610 struct inode_walker w = inode_walker_init();
1611 struct bch_hash_info hash_info;
1612 struct btree_trans trans;
1613 struct btree_iter iter;
1617 bch_verbose(c, "checking xattrs");
1619 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1621 bch2_trans_iter_init(&trans, &iter, BTREE_ID_xattrs,
1622 POS(BCACHEFS_ROOT_INO, 0),
1624 BTREE_ITER_PREFETCH|
1625 BTREE_ITER_ALL_SNAPSHOTS);
1627 bch2_trans_begin(&trans);
1629 while ((k = bch2_btree_iter_peek(&iter)).k &&
1630 !(ret = bkey_err(k))) {
1631 ret = check_key_has_snapshot(&trans, &iter, k);
1635 ret = walk_inode(&trans, &w, k.k->p);
1639 if (fsck_err_on(ret == INT_MAX, c,
1640 "xattr for missing inode %llu",
1642 ret = bch2_btree_delete_at(&trans, &iter, 0);
1652 if (w.first_this_inode)
1653 hash_info = bch2_hash_info_init(c, &w.d[0].inode);
1655 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
1656 &hash_info, &iter, k);
1660 bch2_btree_iter_advance(&iter);
1666 bch2_trans_iter_exit(&trans, &iter);
1667 return bch2_trans_exit(&trans) ?: ret;
1670 /* Get root directory, create if it doesn't exist: */
1671 static int check_root(struct bch_fs *c)
1673 struct btree_trans trans;
1674 struct bch_inode_unpacked root_inode;
1679 bch2_trans_init(&trans, c, 0, 0);
1681 bch_verbose(c, "checking root directory");
1683 ret = subvol_lookup(&trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1684 if (ret && ret != -ENOENT)
1687 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1688 struct bkey_i_subvolume root_subvol;
1691 inum = BCACHEFS_ROOT_INO;
1693 bkey_subvolume_init(&root_subvol.k_i);
1694 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1695 root_subvol.v.flags = 0;
1696 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1697 root_subvol.v.inode = cpu_to_le64(inum);
1698 ret = __bch2_trans_do(&trans, NULL, NULL,
1699 BTREE_INSERT_NOFAIL|
1700 BTREE_INSERT_LAZY_RW,
1701 __bch2_btree_insert(&trans, BTREE_ID_subvolumes, &root_subvol.k_i));
1703 bch_err(c, "error writing root subvol: %i", ret);
1709 ret = lookup_inode(&trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1710 if (ret && ret != -ENOENT)
1713 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1714 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1715 "root inode not a directory")) {
1716 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1718 root_inode.bi_inum = inum;
1720 ret = write_inode(&trans, &root_inode, snapshot);
1722 bch_err(c, "error writing root inode: %i", ret);
1726 bch2_trans_exit(&trans);
1734 struct pathbuf_entry {
1739 static int path_down(struct pathbuf *p, u64 inum)
1741 if (p->nr == p->size) {
1742 size_t new_size = max_t(size_t, 256UL, p->size * 2);
1743 void *n = krealloc(p->entries,
1744 new_size * sizeof(p->entries[0]),
1754 p->entries[p->nr++] = (struct pathbuf_entry) {
1760 static int check_path(struct btree_trans *trans,
1762 struct bch_inode_unpacked *inode,
1765 struct bch_fs *c = trans->c;
1769 snapshot = snapshot_t(c, snapshot)->equiv;
1772 while (inode->bi_inum != BCACHEFS_ROOT_INO) {
1773 ret = lockrestart_do(trans,
1774 inode_backpointer_exists(trans, inode, snapshot));
1779 if (fsck_err(c, "unreachable inode %llu:%u, type %u nlink %u backptr %llu:%llu",
1780 inode->bi_inum, snapshot,
1781 mode_to_type(inode->bi_mode),
1784 inode->bi_dir_offset))
1785 ret = reattach_inode(trans, inode, snapshot);
1790 if (!S_ISDIR(inode->bi_mode))
1793 ret = path_down(p, inode->bi_inum);
1795 bch_err(c, "memory allocation failure");
1799 for (i = 0; i < p->nr; i++) {
1800 if (inode->bi_dir != p->entries[i].inum)
1803 /* XXX print path */
1804 if (!fsck_err(c, "directory structure loop"))
1807 ret = lockrestart_do(trans,
1808 remove_backpointer(trans, inode));
1810 bch_err(c, "error removing dirent: %i", ret);
1814 ret = reattach_inode(trans, inode, snapshot);
1818 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1820 /* Should have been caught in dirents pass */
1821 bch_err(c, "error looking up parent directory: %i", ret);
1827 bch_err(c, "%s: err %i", __func__, ret);
1832 * Check for unreachable inodes, as well as loops in the directory structure:
1833 * After check_dirents(), if an inode backpointer doesn't exist that means it's
1836 static int check_directory_structure(struct bch_fs *c)
1838 struct btree_trans trans;
1839 struct btree_iter iter;
1841 struct bch_inode_unpacked u;
1842 struct pathbuf path = { 0, 0, NULL };
1845 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1847 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
1849 BTREE_ITER_PREFETCH|
1850 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
1851 if (k.k->type != KEY_TYPE_inode)
1854 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), &u);
1856 /* Should have been caught earlier in fsck: */
1857 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1861 if (u.bi_flags & BCH_INODE_UNLINKED)
1864 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
1868 bch2_trans_iter_exit(&trans, &iter);
1870 BUG_ON(ret == -EINTR);
1872 kfree(path.entries);
1874 return bch2_trans_exit(&trans) ?: ret;
1877 struct nlink_table {
1888 static int add_nlink(struct nlink_table *t, u64 inum, u32 snapshot)
1890 if (t->nr == t->size) {
1891 size_t new_size = max_t(size_t, 128UL, t->size * 2);
1892 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
1898 memcpy(d, t->d, t->size * sizeof(t->d[0]));
1906 t->d[t->nr++] = (struct nlink) {
1908 .snapshot = snapshot,
1914 static int nlink_cmp(const void *_l, const void *_r)
1916 const struct nlink *l = _l;
1917 const struct nlink *r = _r;
1919 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
1922 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
1923 struct nlink_table *links,
1924 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
1926 struct nlink *link, key = {
1927 .inum = inum, .snapshot = U32_MAX,
1930 if (inum < range_start || inum >= range_end)
1933 link = __inline_bsearch(&key, links->d, links->nr,
1934 sizeof(links->d[0]), nlink_cmp);
1938 while (link > links->d && link[0].inum == link[-1].inum)
1941 for (; link < links->d + links->nr && link->inum == inum; link++)
1942 if (ref_visible(c, s, snapshot, link->snapshot)) {
1944 if (link->snapshot >= snapshot)
1950 static int check_nlinks_find_hardlinks(struct bch_fs *c,
1951 struct nlink_table *t,
1952 u64 start, u64 *end)
1954 struct btree_trans trans;
1955 struct btree_iter iter;
1957 struct bkey_s_c_inode inode;
1958 struct bch_inode_unpacked u;
1961 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1963 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
1966 BTREE_ITER_PREFETCH|
1967 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
1968 if (k.k->type != KEY_TYPE_inode)
1971 inode = bkey_s_c_to_inode(k);
1974 * Backpointer and directory structure checks are sufficient for
1975 * directories, since they can't have hardlinks:
1977 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
1980 /* Should never fail, checked by bch2_inode_invalid: */
1981 BUG_ON(bch2_inode_unpack(inode, &u));
1986 ret = add_nlink(t, k.k->p.offset, k.k->p.snapshot);
1988 *end = k.k->p.offset;
1994 bch2_trans_iter_exit(&trans, &iter);
1995 bch2_trans_exit(&trans);
1998 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2004 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2005 u64 range_start, u64 range_end)
2007 struct btree_trans trans;
2008 struct snapshots_seen s;
2009 struct btree_iter iter;
2011 struct bkey_s_c_dirent d;
2014 snapshots_seen_init(&s);
2016 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2018 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2020 BTREE_ITER_PREFETCH|
2021 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2022 ret = snapshots_seen_update(c, &s, k.k->p);
2026 switch (k.k->type) {
2027 case KEY_TYPE_dirent:
2028 d = bkey_s_c_to_dirent(k);
2030 if (d.v->d_type != DT_DIR &&
2031 d.v->d_type != DT_SUBVOL)
2032 inc_link(c, &s, links, range_start, range_end,
2033 le64_to_cpu(d.v->d_inum),
2038 bch2_trans_cond_resched(&trans);
2040 bch2_trans_iter_exit(&trans, &iter);
2043 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2045 bch2_trans_exit(&trans);
2046 snapshots_seen_exit(&s);
2051 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2052 struct nlink_table *links,
2053 u64 range_start, u64 range_end)
2055 struct btree_trans trans;
2056 struct btree_iter iter;
2058 struct bkey_s_c_inode inode;
2059 struct bch_inode_unpacked u;
2060 struct nlink *link = links->d;
2063 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2065 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2066 POS(0, range_start),
2068 BTREE_ITER_PREFETCH|
2069 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2070 if (k.k->p.offset >= range_end)
2073 if (k.k->type != KEY_TYPE_inode)
2076 inode = bkey_s_c_to_inode(k);
2077 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
2080 BUG_ON(bch2_inode_unpack(inode, &u));
2085 while ((cmp_int(link->inum, k.k->p.offset) ?:
2086 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2088 BUG_ON(link >= links->d + links->nr);
2091 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2092 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
2093 u.bi_inum, mode_to_type(u.bi_mode),
2094 bch2_inode_nlink_get(&u), link->count)) {
2095 bch2_inode_nlink_set(&u, link->count);
2097 ret = __bch2_trans_do(&trans, NULL, NULL,
2098 BTREE_INSERT_NOFAIL|
2099 BTREE_INSERT_LAZY_RW,
2100 bch2_btree_iter_traverse(&iter) ?:
2101 bch2_inode_write(&trans, &iter, &u));
2103 bch_err(c, "error in fsck: error %i updating inode", ret);
2107 bch2_trans_iter_exit(&trans, &iter);
2108 bch2_trans_exit(&trans);
2111 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2117 static int check_nlinks(struct bch_fs *c)
2119 struct nlink_table links = { 0 };
2120 u64 this_iter_range_start, next_iter_range_start = 0;
2123 bch_verbose(c, "checking inode nlinks");
2126 this_iter_range_start = next_iter_range_start;
2127 next_iter_range_start = U64_MAX;
2129 ret = check_nlinks_find_hardlinks(c, &links,
2130 this_iter_range_start,
2131 &next_iter_range_start);
2133 ret = check_nlinks_walk_dirents(c, &links,
2134 this_iter_range_start,
2135 next_iter_range_start);
2139 ret = check_nlinks_update_hardlinks(c, &links,
2140 this_iter_range_start,
2141 next_iter_range_start);
2146 } while (next_iter_range_start != U64_MAX);
2154 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2155 * Doesn't fix them yet, mainly because they haven't yet been observed:
2157 int bch2_fsck_full(struct bch_fs *c)
2159 return bch2_fs_snapshots_check(c) ?:
2160 check_inodes(c, true) ?:
2166 check_directory_structure(c) ?:
2170 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2172 return check_inodes(c, false);