bcachefs: Check for too-large encoded extents
authorKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 15:33:02 +0000 (11:33 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Tue, 31 Oct 2023 16:18:37 +0000 (12:18 -0400)
We don't yet repair (split) them, just check.

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

index 1b25f84e4b9cb883fe36dd70bfe43a8df10484aa..38077b3886d798035344fdc4ebc36e52a7e623dd 100644 (file)
@@ -1207,6 +1207,14 @@ int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k,
                                return -BCH_ERR_invalid_bkey;
                        }
                        crc_since_last_ptr = true;
+
+                       if (crc_is_encoded(crc) &&
+                           (crc.uncompressed_size > c->opts.encoded_extent_max >> 9) &&
+                           (flags & (BKEY_INVALID_WRITE|BKEY_INVALID_COMMIT))) {
+                               prt_printf(err, "too large encoded extent");
+                               return -BCH_ERR_invalid_bkey;
+                       }
+
                        break;
                case BCH_EXTENT_ENTRY_stripe_ptr:
                        if (have_ec) {
index acf78f55bdff60c2993f5a6e4d90f3c6b81baab9..ef1b9f18719d126cf6eede809fd4ecda2e9efd7d 100644 (file)
@@ -190,6 +190,11 @@ static inline bool crc_is_compressed(struct bch_extent_crc_unpacked crc)
                crc.compression_type != BCH_COMPRESSION_TYPE_incompressible);
 }
 
+static inline bool crc_is_encoded(struct bch_extent_crc_unpacked crc)
+{
+       return crc.csum_type != BCH_CSUM_none || crc_is_compressed(crc);
+}
+
 /* bkey_ptrs: generically over any key type that has ptrs */
 
 struct bkey_ptrs_c {
index f26b824e70a84bedd1d16c45ea966b4f8589971c..328cb3b3e21338878f3ea594cf7342983cf15168 100644 (file)
@@ -1299,6 +1299,28 @@ err:
        return ret;
 }
 
+static int check_extent_overbig(struct btree_trans *trans, struct btree_iter *iter,
+                               struct bkey_s_c k)
+{
+       struct bch_fs *c = trans->c;
+       struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
+       struct bch_extent_crc_unpacked crc;
+       const union bch_extent_entry *i;
+       unsigned encoded_extent_max_sectors = c->opts.encoded_extent_max >> 9;
+
+       bkey_for_each_crc(k.k, ptrs, crc, i)
+               if (crc_is_encoded(crc) &&
+                   crc.uncompressed_size > encoded_extent_max_sectors) {
+                       struct printbuf buf = PRINTBUF;
+
+                       bch2_bkey_val_to_text(&buf, c, k);
+                       bch_err(c, "overbig encoded extent, please report this:\n  %s", buf.buf);
+                       printbuf_exit(&buf);
+               }
+
+       return 0;
+}
+
 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
                        struct bkey_s_c k,
                        struct inode_walker *inode,
@@ -1434,7 +1456,8 @@ int bch2_check_extents(struct bch_fs *c)
                        &res, NULL,
                        BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
                bch2_disk_reservation_put(c, &res);
-               check_extent(trans, &iter, k, &w, &s, &extent_ends);
+               check_extent(trans, &iter, k, &w, &s, &extent_ends) ?:
+               check_extent_overbig(trans, &iter, k);
        })) ?:
        check_i_sectors(trans, &w);
 
@@ -1448,6 +1471,30 @@ int bch2_check_extents(struct bch_fs *c)
        return ret;
 }
 
+int bch2_check_indirect_extents(struct bch_fs *c)
+{
+       struct btree_trans *trans = bch2_trans_get(c);
+       struct btree_iter iter;
+       struct bkey_s_c k;
+       struct disk_reservation res = { 0 };
+       int ret = 0;
+
+       ret = for_each_btree_key_commit(trans, iter, BTREE_ID_reflink,
+                       POS_MIN,
+                       BTREE_ITER_PREFETCH, k,
+                       &res, NULL,
+                       BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
+               bch2_disk_reservation_put(c, &res);
+               check_extent_overbig(trans, &iter, k);
+       }));
+
+       bch2_disk_reservation_put(c, &res);
+       bch2_trans_put(trans);
+
+       bch_err_fn(c, ret);
+       return ret;
+}
+
 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
 {
        struct bch_fs *c = trans->c;
index 90c87b5089a01403bceabeeed390fa3adfea760e..da991e8cf27eb493ed5aac5a3e3da606ae089968 100644 (file)
@@ -4,6 +4,7 @@
 
 int bch2_check_inodes(struct bch_fs *);
 int bch2_check_extents(struct bch_fs *);
+int bch2_check_indirect_extents(struct bch_fs *);
 int bch2_check_dirents(struct bch_fs *);
 int bch2_check_xattrs(struct bch_fs *);
 int bch2_check_root(struct bch_fs *);
index 4a666f4d2dcc2eee761499eda9aabfda06da98ce..f7461f60d760a57d401242c547b681b0ff5e51c5 100644 (file)
@@ -1092,9 +1092,7 @@ static bool bch2_extent_is_writeable(struct bch_write_op *op,
 
        e = bkey_s_c_to_extent(k);
        extent_for_each_ptr_decode(e, p, entry) {
-               if (p.crc.csum_type ||
-                   crc_is_compressed(p.crc) ||
-                   p.has_ec)
+               if (crc_is_encoded(p.crc) || p.has_ec)
                        return false;
 
                replicas += bch2_extent_ptr_durability(c, &p);
index bf43e13c4560657b22005325ecae0f07f446ef92..e2d8771909efe2b826a94d5b825b06b361e315d0 100644 (file)
@@ -34,6 +34,7 @@
        x(resume_logged_ops,            PASS_ALWAYS)                                            \
        x(check_inodes,                 PASS_FSCK)                                              \
        x(check_extents,                PASS_FSCK)                                              \
+       x(check_indirect_extents,       PASS_FSCK)                                              \
        x(check_dirents,                PASS_FSCK)                                              \
        x(check_xattrs,                 PASS_FSCK)                                              \
        x(check_root,                   PASS_FSCK)                                              \