@@ -511,6 +511,73 @@ void zero_fill_bio(struct bio *bio)
}
EXPORT_SYMBOL(zero_fill_bio);
+bool bio_is_zero_filled(struct bio *bio)
+{
+ unsigned i, count;
+ unsigned long flags;
+ struct bio_vec bv;
+ struct bvec_iter iter;
+ bio_for_each_segment(bv, bio, iter) {
+ char *data = bvec_kmap_irq(&bv, &flags);
+ char *p = data;
+ uintptr_t *parch;
+ int left = bv.bv_len;
+
+ if (unlikely( data == NULL ))
+ continue;
+
+
+ /* check unaligned bytes at the beginning of p */
+ if (unlikely( ( (uintptr_t)p & (sizeof(uintptr_t)-1) ) != 0 )) {
+ count = sizeof(uintptr_t) - ( (uintptr_t)p & (sizeof(uintptr_t)-1) );
+ for (i = 0; i < count; i++) {
+ if (*p) {
+ bvec_kunmap_irq(data, &flags);
+ return false;
+ }
+ p++;
+ }
+ left -= count;
+ }
+
+ /* we should be word aligned now */
+ BUG_ON(unlikely( ((uintptr_t)p & (sizeof(uintptr_t)-1) ) != 0 ));
+
+ /* now check in word-sized chunks */
+ parch = (uintptr_t*)p;
+ count = left >> ilog2(sizeof(uintptr_t)); /* count = left / sizeof(uintptr_t) */;
+ for (i = 0; i < count; i++) {
+ if (*parch) {
+ bvec_kunmap_irq(data, &flags);
+ return false;
+ }
+ parch++;
+ }
+ left -= count << ilog2(sizeof(uintptr_t)); /* left -= count*sizeof(uintptr_t) */
+
+ /* check remaining unaligned values at the end */
+ p = (char*)parch;
+ if (unlikely(left > 0))
+ {
+ for (i = 0; i < left; i++) {
+ if (*p) {
+ bvec_kunmap_irq(data, &flags);
+ return false;
+ }
+ p++;
+ }
+ left = 0;
+ }
+
+ bvec_kunmap_irq(data, &flags);
+ BUG_ON(unlikely( left > 0 ));
+ BUG_ON(unlikely( data+bv.bv_len != p ));
+ }
+
+ return true;
+}
+EXPORT_SYMBOL(bio_is_zero_filled);
+
/**
* bio_put - release a reference to a bio
* @bio: bio to release reference to
@@ -1258,6 +1258,16 @@ static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block
return;
}
+ /*
+ * Optimize away writes of all zeroes, subsequent reads to
+ * associated unprovisioned blocks will be zero filled.
+ */
+ if (unlikely(bio_is_zero_filled(bio))) {
+ cell_defer_no_holder(tc, cell);
+ bio_endio(bio, 0);
+ return;
+ }
+
r = alloc_data_block(tc, &data_block);
switch (r) {
case 0:
@@ -419,6 +419,7 @@ extern struct bio *bio_copy_user_iov(struct request_queue *,
int, int, gfp_t);
extern int bio_uncopy_user(struct bio *);
void zero_fill_bio(struct bio *bio);
+bool bio_is_zero_filled(struct bio *bio);
extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
extern unsigned int bvec_nr_vecs(unsigned short idx);