@@ -883,6 +883,7 @@ static int cached_dev_cache_miss(struct btree *b, struct search *s,
unsigned int reada = 0;
struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
struct bio *miss, *cache_bio;
+ unsigned int max_miss_size;
s->cache_missed = 1;
@@ -899,6 +900,25 @@ static int cached_dev_cache_miss(struct btree *b, struct search *s,
get_capacity(bio->bi_bdev->bd_disk) -
bio_end_sector(bio));
+ /*
+ * Make sure sectors won't exceed two size limitations,
+ * - The bkey maximum size
+ * Size field in the bkey is 16 bits, the maximum permitted
+ * value is (1 << KEY_SIZE_BITS) - 1, in unit of sector.
+ * - The bio io vecs maximum number
+ * BIO_MAX_VECS is the maximum permitted io vecs number of a
+ * bio, any larger value will result a BUG() complain in bio
+ * layer code. When maximum size of each io vector is a page,
+ * BIO_MAX_VECS * PAGE_SECTORS is the maximum permitted value
+ * in unit of sectors.
+ * Then we are sure there is no overflow for key size of
+ * s->iop.replace_key and bio io vecs number of cache_bio.
+ */
+ max_miss_size = min_t(uint32_t,
+ (1 << KEY_SIZE_BITS) - 1, BIO_MAX_VECS * PAGE_SECTORS);
+ if (sectors > max_miss_size)
+ sectors = max_miss_size;
+
s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada);
s->iop.replace_key = KEY(s->iop.inode,