2 * Copyright (C) 2001-2003 Sistina Software (UK) Limited.
4 * This file is released under the GPL.
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/blkdev.h>
12 #include <linux/bio.h>
13 #include <linux/slab.h>
15 #define DM_MSG_PREFIX "striped"
19 sector_t physical_start;
25 /* The size of this target / num. stripes */
26 sector_t stripe_width;
28 /* stripe chunk size */
32 struct stripe stripe[0];
35 static inline struct stripe_c *alloc_context(unsigned int stripes)
39 if (array_too_big(sizeof(struct stripe_c), sizeof(struct stripe),
43 len = sizeof(struct stripe_c) + (sizeof(struct stripe) * stripes);
45 return kmalloc(len, GFP_KERNEL);
49 * Parse a single <dev> <sector> pair
51 static int get_stripe(struct dm_target *ti, struct stripe_c *sc,
52 unsigned int stripe, char **argv)
54 unsigned long long start;
56 if (sscanf(argv[1], "%llu", &start) != 1)
59 if (dm_get_device(ti, argv[0], start, sc->stripe_width,
60 dm_table_get_mode(ti->table),
61 &sc->stripe[stripe].dev))
64 sc->stripe[stripe].physical_start = start;
69 * Construct a striped mapping.
70 * <number of stripes> <chunk size (2^^n)> [<dev_path> <offset>]+
72 static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
83 ti->error = "Not enough arguments";
87 stripes = simple_strtoul(argv[0], &end, 10);
89 ti->error = "Invalid stripe count";
93 chunk_size = simple_strtoul(argv[1], &end, 10);
95 ti->error = "Invalid chunk_size";
100 * chunk_size is a power of two
102 if (!chunk_size || (chunk_size & (chunk_size - 1)) ||
103 (chunk_size < (PAGE_SIZE >> SECTOR_SHIFT))) {
104 ti->error = "Invalid chunk size";
108 if (ti->len & (chunk_size - 1)) {
109 ti->error = "Target length not divisible by "
115 if (sector_div(width, stripes)) {
116 ti->error = "Target length not divisible by "
122 * Do we have enough arguments for that many stripes ?
124 if (argc != (2 + 2 * stripes)) {
125 ti->error = "Not enough destinations "
130 sc = alloc_context(stripes);
132 ti->error = "Memory allocation for striped context "
137 sc->stripes = stripes;
138 sc->stripe_width = width;
139 ti->split_io = chunk_size;
141 sc->chunk_mask = ((sector_t) chunk_size) - 1;
142 for (sc->chunk_shift = 0; chunk_size; sc->chunk_shift++)
147 * Get the stripe destinations.
149 for (i = 0; i < stripes; i++) {
152 r = get_stripe(ti, sc, i, argv);
154 ti->error = "Couldn't parse stripe destination";
156 dm_put_device(ti, sc->stripe[i].dev);
166 static void stripe_dtr(struct dm_target *ti)
169 struct stripe_c *sc = (struct stripe_c *) ti->private;
171 for (i = 0; i < sc->stripes; i++)
172 dm_put_device(ti, sc->stripe[i].dev);
177 static int stripe_map(struct dm_target *ti, struct bio *bio,
178 union map_info *map_context)
180 struct stripe_c *sc = (struct stripe_c *) ti->private;
182 sector_t offset = bio->bi_sector - ti->begin;
183 sector_t chunk = offset >> sc->chunk_shift;
184 uint32_t stripe = sector_div(chunk, sc->stripes);
186 bio->bi_bdev = sc->stripe[stripe].dev->bdev;
187 bio->bi_sector = sc->stripe[stripe].physical_start +
188 (chunk << sc->chunk_shift) + (offset & sc->chunk_mask);
189 return DM_MAPIO_REMAPPED;
192 static int stripe_status(struct dm_target *ti,
193 status_type_t type, char *result, unsigned int maxlen)
195 struct stripe_c *sc = (struct stripe_c *) ti->private;
200 case STATUSTYPE_INFO:
204 case STATUSTYPE_TABLE:
205 DMEMIT("%d %llu", sc->stripes,
206 (unsigned long long)sc->chunk_mask + 1);
207 for (i = 0; i < sc->stripes; i++)
208 DMEMIT(" %s %llu", sc->stripe[i].dev->name,
209 (unsigned long long)sc->stripe[i].physical_start);
215 static struct target_type stripe_target = {
218 .module = THIS_MODULE,
222 .status = stripe_status,
225 int __init dm_stripe_init(void)
229 r = dm_register_target(&stripe_target);
231 DMWARN("target registration failed");
236 void dm_stripe_exit(void)
238 if (dm_unregister_target(&stripe_target))
239 DMWARN("target unregistration failed");