Skip to contentSkip to conversation

dm: introduce get_num_duplicates method in dm_target

PATCH New Mike Snitzer 1 participant 2 messages Dec 7, 2012 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 dm: introduce get_num_duplicates method in dm_target

This allows a target to dynamically tell dm core how many copies of a bio it needs. The dm-cache target will make use of this for it's write-through support.

Introduce __split_and_issue_bio_to_target to allow split_bvec to be used with get_num_duplicates.

We already have hard coded instances of this (eg, num_flush_requests, num_discard_requests). These have been left for now, we can switch everything over to use get_num_duplicates later.

(Based heavily on Joe's original work; ported to work with Mikulas' new bio cloning via front padding)

---
 drivers/md/dm.c               |   83 ++++++++++++++++++++++++++++++-----------
 include/linux/device-mapper.h |    8 ++++
 2 files changed, 69 insertions(+), 22 deletions(-)
drivers/md/dm.c +61 -22
index 08eea14..506c73a 100644
@@ -1038,7 +1038,7 @@ struct clone_info {
  */
 static void split_bvec(struct dm_target_io *tio, struct bio *bio,
 		       sector_t sector, unsigned short idx, unsigned int offset,
-		       unsigned int len, struct bio_set *bs)
+		       unsigned int len)
 {
 	struct bio *clone = &tio->clone;
 	struct bio_vec *bv = bio->bi_io_vec + idx;
@@ -1104,6 +1104,20 @@ static struct dm_target_io *alloc_tio(struct clone_info *ci,
 	return tio;
 }
 
+static unsigned num_duplicate_bios_needed(struct dm_target *ti, struct bio *bio)
+{
+	if (bio->bi_rw & REQ_FLUSH)
+		return ti->num_flush_requests;
+
+	if (bio->bi_rw & REQ_DISCARD)
+		return ti->num_discard_requests;
+
+	if (ti->type->get_num_duplicates)
+		return ti->type->get_num_duplicates(ti, bio);
+
+	return 1;
+}
+
 static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
 				   unsigned request_nr, sector_t len)
 {
@@ -1128,12 +1142,12 @@ static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
 }
 
 static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
-				    unsigned num_requests, sector_t len)
+				    sector_t len)
 {
-	unsigned request_nr;
+	unsigned i, num_requests = num_duplicate_bios_needed(ti, ci->bio);
 
-	for (request_nr = 0; request_nr < num_requests; request_nr++)
-		__issue_target_request(ci, ti, request_nr, len);
+	for (i = 0; i < num_requests; i++)
+		__issue_target_request(ci, ti, i, len);
 }
 
 static int __clone_and_map_empty_flush(struct clone_info *ci)
@@ -1143,23 +1157,54 @@ static int __clone_and_map_empty_flush(struct clone_info *ci)
 
 	BUG_ON(bio_has_data(ci->bio));
 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
-		__issue_target_requests(ci, ti, ti->num_flush_requests, 0);
+		__issue_target_requests(ci, ti, 0);
 
 	return 0;
 }
 
+static void __issue_bio_to_target(struct clone_info *ci, struct dm_target *ti,
+				  struct bio *bio, sector_t sector,
+				  unsigned short idx, unsigned bv_count,
+				  unsigned len, struct bio_set *bs)
+{
+	unsigned i, num_duplicates = num_duplicate_bios_needed(ti, bio);
+	struct dm_target_io *tio;
+
+	for (i = 0; i < num_duplicates; i++) {
+		tio = alloc_tio(ci, ti, bio->bi_max_vecs);
+		tio->target_request_nr = i;
+		clone_bio(tio, bio, sector, idx, bv_count, len, bs);
+		__map_bio(ti, tio);
+	}
+}
+
+static void __split_and_issue_bio_to_target(struct clone_info *ci,
+					    struct dm_target *ti,
+					    struct bio *bio, sector_t sector,
+					    unsigned short idx, unsigned bv_count,
+					    unsigned len)
+{
+	unsigned i, num_duplicates = num_duplicate_bios_needed(ti, bio);
+	struct dm_target_io *tio;
+
+	for (i = 0; i < num_duplicates; i++) {
+		tio = alloc_tio(ci, ti, 1);
+		tio->target_request_nr = i;
+		split_bvec(tio, bio, sector, idx, bv_count, len);
+		__map_bio(ti, tio);
+	}
+}
+
 /*
  * Perform all io with a single clone.
  */
 static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
 {
 	struct bio *bio = ci->bio;
-	struct dm_target_io *tio;
 
-	tio = alloc_tio(ci, ti, bio->bi_max_vecs);
-	clone_bio(tio, bio, ci->sector, ci->idx, bio->bi_vcnt - ci->idx,
-		  ci->sector_count, ci->md->bs);
-	__map_bio(ti, tio);
+	__issue_bio_to_target(ci, ti, bio, ci->sector, ci->idx,
+			      bio->bi_vcnt - ci->idx, ci->sector_count,
+			      ci->md->bs);
 	ci->sector_count = 0;
 }
 
@@ -1187,7 +1232,7 @@ static int __clone_and_map_discard(struct clone_info *ci)
 		else
 			len = min(ci->sector_count, max_io_len(ci->sector, ti));
 
-		__issue_target_requests(ci, ti, ti->num_discard_requests, len);
+		__issue_target_requests(ci, ti, len);
 
 		ci->sector += len;
 	} while (ci->sector_count -= len);
@@ -1200,7 +1245,6 @@ static int __clone_and_map(struct clone_info *ci)
 	struct bio *bio = ci->bio;
 	struct dm_target *ti;
 	sector_t len = 0, max;
-	struct dm_target_io *tio;
 
 	if (unlikely(bio->bi_rw & REQ_DISCARD))
 		return __clone_and_map_discard(ci);
@@ -1237,10 +1281,8 @@ static int __clone_and_map(struct clone_info *ci)
 			len += bv_len;
 		}
 
-		tio = alloc_tio(ci, ti, bio->bi_max_vecs);
-		clone_bio(tio, bio, ci->sector, ci->idx, i - ci->idx, len,
-			  ci->md->bs);
-		__map_bio(ti, tio);
+		__issue_bio_to_target(ci, ti, bio, ci->sector, ci->idx,
+				      i - ci->idx, len, ci->md->bs);
 
 		ci->sector += len;
 		ci->sector_count -= len;
@@ -1265,11 +1307,8 @@ static int __clone_and_map(struct clone_info *ci)
 
 			len = min(remaining, max);
 
-			tio = alloc_tio(ci, ti, 1);
-			split_bvec(tio, bio, ci->sector, ci->idx,
-				   bv->bv_offset + offset, len, ci->md->bs);
-
-			__map_bio(ti, tio);
+			__split_and_issue_bio_to_target(ci, ti, bio, ci->sector, ci->idx,
+							bv->bv_offset + offset, len);
 
 			ci->sector += len;
 			ci->sector_count -= len;
include/linux/device-mapper.h +8 -0
index 1687f9e..2d65c70 100644
@@ -39,6 +39,13 @@ typedef int (*dm_ctr_fn) (struct dm_target *target,
 typedef void (*dm_dtr_fn) (struct dm_target *ti);
 
 /*
+ * Some targets need to process multiple copies of a bio.  For instance a
+ * cache target in 'writethrough' mode will need to issue io to both the
+ * fast and slow devices.
+ */
+typedef unsigned (*dm_get_num_duplicates_fn)(struct dm_target *ti, struct bio *bio);
+
+/*
  * The map function must return:
  * < 0: error
  * = 0: The target will handle the io by resubmitting it later
@@ -131,6 +138,7 @@ struct target_type {
 	unsigned version[3];
 	dm_ctr_fn ctr;
 	dm_dtr_fn dtr;
+	dm_get_num_duplicates_fn get_num_duplicates;
 	dm_map_fn map;
 	dm_map_request_fn map_rq;
 	dm_endio_fn end_io;
Signature · 1 line
1.7.1
#1 lore raw

PATCH 2/2 dm: additional cleanup for clone_bio

Port split up of clone_bio as originally coded by Joe (prior to Mikulas' per-bio patches). In doing so it became clear there is no need to pass bio_set to clone_bio.

---
 drivers/md/dm.c |   62 +++++++++++++++++++++++++++++++-----------------------
 1 files changed, 36 insertions(+), 26 deletions(-)
drivers/md/dm.c +36 -26
index 506c73a..739fd2b 100644
@@ -1061,23 +1061,23 @@ static void split_bvec(struct dm_target_io *tio, struct bio *bio,
 	}
 }
 
-/*
- * Creates a bio that consists of range of complete bvecs.
- */
-static void clone_bio(struct dm_target_io *tio, struct bio *bio,
-		      sector_t sector, unsigned short idx,
-		      unsigned short bv_count, unsigned int len,
-		      struct bio_set *bs)
+static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
 {
-	struct bio *clone = &tio->clone;
+	bio->bi_sector = sector;
+	bio->bi_size = to_bytes(len);
+}
 
-	__bio_clone(clone, bio);
-	clone->bi_sector = sector;
-	clone->bi_idx = idx;
-	clone->bi_vcnt = idx + bv_count;
-	clone->bi_size = to_bytes(len);
-	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
+static void bio_setup_bv(struct bio *bio, unsigned short idx,
+			 unsigned short bv_count)
+{
+	bio->bi_idx = idx;
+	bio->bi_vcnt = idx + bv_count;
+	bio->bi_flags &= ~(1 << BIO_SEG_VALID);
+}
 
+static void clone_bio_integrity(struct bio *bio, struct bio *clone,
+				unsigned short idx, unsigned int len)
+{
 	if (bio_integrity(bio)) {
 		bio_integrity_clone(clone, bio, GFP_NOIO);
 
@@ -1087,6 +1087,21 @@ static void clone_bio(struct dm_target_io *tio, struct bio *bio,
 	}
 }
 
+/*
+ * Creates a bio that consists of range of complete bvecs.
+ */
+static void clone_bio(struct dm_target_io *tio, struct bio *bio,
+		      sector_t sector, unsigned short idx,
+		      unsigned short bv_count, unsigned int len)
+{
+	struct bio *clone = &tio->clone;
+
+	__bio_clone(clone, bio);
+	bio_setup_sector(clone, sector, len);
+	bio_setup_bv(clone, idx, bv_count);
+	clone_bio_integrity(bio, clone, idx, len);
+}
+
 static struct dm_target_io *alloc_tio(struct clone_info *ci,
 				      struct dm_target *ti, int nr_iovecs)
 {
@@ -1131,13 +1146,9 @@ static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
 	 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
 	 * and discard, so no need for concern about wasted bvec allocations.
 	 */
-
-	 __bio_clone(clone, ci->bio);
-	if (len) {
-		clone->bi_sector = ci->sector;
-		clone->bi_size = to_bytes(len);
-	}
-
+	__bio_clone(clone, ci->bio);
+	if (len)
+		bio_setup_sector(clone, ci->sector, len);
 	__map_bio(ti, tio);
 }
 
@@ -1165,7 +1176,7 @@ static int __clone_and_map_empty_flush(struct clone_info *ci)
 static void __issue_bio_to_target(struct clone_info *ci, struct dm_target *ti,
 				  struct bio *bio, sector_t sector,
 				  unsigned short idx, unsigned bv_count,
-				  unsigned len, struct bio_set *bs)
+				  unsigned len)
 {
 	unsigned i, num_duplicates = num_duplicate_bios_needed(ti, bio);
 	struct dm_target_io *tio;
@@ -1173,7 +1184,7 @@ static void __issue_bio_to_target(struct clone_info *ci, struct dm_target *ti,
 	for (i = 0; i < num_duplicates; i++) {
 		tio = alloc_tio(ci, ti, bio->bi_max_vecs);
 		tio->target_request_nr = i;
-		clone_bio(tio, bio, sector, idx, bv_count, len, bs);
+		clone_bio(tio, bio, sector, idx, bv_count, len);
 		__map_bio(ti, tio);
 	}
 }
@@ -1203,8 +1214,7 @@ static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
 	struct bio *bio = ci->bio;
 
 	__issue_bio_to_target(ci, ti, bio, ci->sector, ci->idx,
-			      bio->bi_vcnt - ci->idx, ci->sector_count,
-			      ci->md->bs);
+			      bio->bi_vcnt - ci->idx, ci->sector_count);
 	ci->sector_count = 0;
 }
 
@@ -1282,7 +1292,7 @@ static int __clone_and_map(struct clone_info *ci)
 		}
 
 		__issue_bio_to_target(ci, ti, bio, ci->sector, ci->idx,
-				      i - ci->idx, len, ci->md->bs);
+				      i - ci->idx, len);
 
 		ci->sector += len;
 		ci->sector_count -= len;
Signature · 1 line
1.7.1