]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
bcachefs: Debug params for data corruption injection
authorKent Overstreet <kent.overstreet@linux.dev>
Sat, 8 Mar 2025 23:42:56 +0000 (18:42 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 16 Mar 2025 17:47:55 +0000 (13:47 -0400)
dm-flakey is busted, and this is simpler anyways - this lets us test the
checksum error retry ptahs

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/io_read.c
fs/bcachefs/io_write.c
fs/bcachefs/util.c
fs/bcachefs/util.h

index 70e5c5a32d01870aed2770acf36ae2d755a7c5ca..d39f321b51fc830f93756d7b7be32b12087b0170 100644 (file)
 
 #include <linux/sched/mm.h>
 
+#ifdef CONFIG_BCACHEFS_DEBUG
+static unsigned bch2_read_corrupt_ratio;
+module_param_named(read_corrupt_ratio, bch2_read_corrupt_ratio, uint, 0644);
+MODULE_PARM_DESC(read_corrupt_ratio, "");
+#endif
+
 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
 
 static bool bch2_target_congested(struct bch_fs *c, u16 target)
@@ -688,6 +694,8 @@ static void __bch2_read_endio(struct work_struct *work)
                src->bi_iter                    = rbio->bvec_iter;
        }
 
+       bch2_maybe_corrupt_bio(src, bch2_read_corrupt_ratio);
+
        csum = bch2_checksum_bio(c, crc.csum_type, nonce, src);
        bool csum_good = !bch2_crc_cmp(csum, rbio->pick.crc.csum) || c->opts.no_data_io;
 
index dbfcb28f003d2e82f496389d95964545ba52677b..48befbae0226bfc9b268e3900bbc9e9f45a605eb 100644 (file)
 #include <linux/random.h>
 #include <linux/sched/mm.h>
 
+#ifdef CONFIG_BCACHEFS_DEBUG
+static unsigned bch2_write_corrupt_ratio;
+module_param_named(write_corrupt_ratio, bch2_write_corrupt_ratio, uint, 0644);
+MODULE_PARM_DESC(write_corrupt_ratio, "");
+#endif
+
 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
 
 static inline void bch2_congested_acct(struct bch_dev *ca, u64 io_latency,
@@ -1005,6 +1011,15 @@ static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
                bounce = true;
        }
 
+#ifdef CONFIG_BCACHEFS_DEBUG
+       unsigned write_corrupt_ratio = READ_ONCE(bch2_write_corrupt_ratio);
+       if (!bounce && write_corrupt_ratio) {
+               dst = bch2_write_bio_alloc(c, wp, src,
+                                          &page_alloc_failed,
+                                          ec_buf);
+               bounce = true;
+       }
+#endif
        saved_iter = dst->bi_iter;
 
        do {
@@ -1114,6 +1129,14 @@ static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
 
                init_append_extent(op, wp, version, crc);
 
+#ifdef CONFIG_BCACHEFS_DEBUG
+               if (write_corrupt_ratio) {
+                       swap(dst->bi_iter.bi_size, dst_len);
+                       bch2_maybe_corrupt_bio(dst, write_corrupt_ratio);
+                       swap(dst->bi_iter.bi_size, dst_len);
+               }
+#endif
+
                if (dst != src)
                        bio_advance(dst, dst_len);
                bio_advance(src, src_len);
@@ -1394,6 +1417,7 @@ retry:
                bio->bi_private = &op->cl;
                bio->bi_opf |= REQ_OP_WRITE;
                closure_get(&op->cl);
+
                bch2_submit_wbio_replicas(to_wbio(bio), c, BCH_DATA_user,
                                          op->insert_keys.top, true);
 
index a7edbcca1a849a8b28031efa7f36e3af96685a95..553de8d8e3e5eebb7537c82986b77e65669eb6dd 100644 (file)
@@ -704,6 +704,27 @@ void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
        }
 }
 
+#ifdef CONFIG_BCACHEFS_DEBUG
+void bch2_corrupt_bio(struct bio *bio)
+{
+       struct bvec_iter iter;
+       struct bio_vec bv;
+       unsigned offset = get_random_u32_below(bio->bi_iter.bi_size / sizeof(u64));
+
+       bio_for_each_segment(bv, bio, iter) {
+               unsigned u64s = bv.bv_len / sizeof(u64);
+
+               if (offset < u64s) {
+                       u64 *segment = bvec_kmap_local(&bv);
+                       segment[offset] = get_random_u64();
+                       kunmap_local(segment);
+                       return;
+               }
+               offset -= u64s;
+       }
+}
+#endif
+
 #if 0
 void eytzinger1_test(void)
 {
index f4a4783219d9d9e1319e5e40f797ef959138d9da..d41e133acc4d6b955f494d0ef802d78427a5f785 100644 (file)
@@ -406,6 +406,18 @@ u64 bch2_get_random_u64_below(u64);
 void memcpy_to_bio(struct bio *, struct bvec_iter, const void *);
 void memcpy_from_bio(void *, struct bio *, struct bvec_iter);
 
+#ifdef CONFIG_BCACHEFS_DEBUG
+void bch2_corrupt_bio(struct bio *);
+
+static inline void bch2_maybe_corrupt_bio(struct bio *bio, unsigned ratio)
+{
+       if (ratio && !get_random_u32_below(ratio))
+               bch2_corrupt_bio(bio);
+}
+#else
+#define bch2_maybe_corrupt_bio(...)    do {} while (0)
+#endif
+
 static inline void memcpy_u64s_small(void *dst, const void *src,
                                     unsigned u64s)
 {