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>
17 sector_t physical_start;
23 /* The size of this target / num. stripes */
24 uint32_t stripe_width;
26 /* stripe chunk size */
30 struct stripe stripe[0];
33 static inline struct stripe_c *alloc_context(unsigned int stripes)
37 if (array_too_big(sizeof(struct stripe_c), sizeof(struct stripe),
41 len = sizeof(struct stripe_c) + (sizeof(struct stripe) * stripes);
43 return kmalloc(len, GFP_KERNEL);
47 * Parse a single <dev> <sector> pair
49 static int get_stripe(struct dm_target *ti, struct stripe_c *sc,
50 unsigned int stripe, char **argv)
54 if (sscanf(argv[1], SECTOR_FORMAT, &start) != 1)
57 if (dm_get_device(ti, argv[0], start, sc->stripe_width,
58 dm_table_get_mode(ti->table),
59 &sc->stripe[stripe].dev))
62 sc->stripe[stripe].physical_start = start;
67 * Construct a striped mapping.
68 * <number of stripes> <chunk size (2^^n)> [<dev_path> <offset>]+
70 static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
81 ti->error = "dm-stripe: Not enough arguments";
85 stripes = simple_strtoul(argv[0], &end, 10);
87 ti->error = "dm-stripe: Invalid stripe count";
91 chunk_size = simple_strtoul(argv[1], &end, 10);
93 ti->error = "dm-stripe: Invalid chunk_size";
98 * chunk_size is a power of two
100 if (!chunk_size || (chunk_size & (chunk_size - 1)) ||
101 (chunk_size < (PAGE_SIZE >> SECTOR_SHIFT))) {
102 ti->error = "dm-stripe: Invalid chunk size";
107 if (sector_div(width, stripes)) {
108 ti->error = "dm-stripe: Target length not divisable by "
114 * Do we have enough arguments for that many stripes ?
116 if (argc != (2 + 2 * stripes)) {
117 ti->error = "dm-stripe: Not enough destinations "
122 sc = alloc_context(stripes);
124 ti->error = "dm-stripe: Memory allocation for striped context "
129 sc->stripes = stripes;
130 sc->stripe_width = width;
131 ti->split_io = chunk_size;
133 sc->chunk_mask = ((sector_t) chunk_size) - 1;
134 for (sc->chunk_shift = 0; chunk_size; sc->chunk_shift++)
139 * Get the stripe destinations.
141 for (i = 0; i < stripes; i++) {
144 r = get_stripe(ti, sc, i, argv);
146 ti->error = "dm-stripe: Couldn't parse stripe "
149 dm_put_device(ti, sc->stripe[i].dev);
159 static void stripe_dtr(struct dm_target *ti)
162 struct stripe_c *sc = (struct stripe_c *) ti->private;
164 for (i = 0; i < sc->stripes; i++)
165 dm_put_device(ti, sc->stripe[i].dev);
170 static int stripe_map(struct dm_target *ti, struct bio *bio,
171 union map_info *map_context)
173 struct stripe_c *sc = (struct stripe_c *) ti->private;
175 sector_t offset = bio->bi_sector - ti->begin;
176 uint32_t chunk = (uint32_t) (offset >> sc->chunk_shift);
177 uint32_t stripe = chunk % sc->stripes; /* 32bit modulus */
178 chunk = chunk / sc->stripes;
180 bio->bi_bdev = sc->stripe[stripe].dev->bdev;
181 bio->bi_sector = sc->stripe[stripe].physical_start +
182 (chunk << sc->chunk_shift) + (offset & sc->chunk_mask);
186 static int stripe_status(struct dm_target *ti,
187 status_type_t type, char *result, unsigned int maxlen)
189 struct stripe_c *sc = (struct stripe_c *) ti->private;
194 #define EMIT(x...) sz += ((sz >= maxlen) ? \
195 0 : scnprintf(result + sz, maxlen - sz, x))
198 case STATUSTYPE_INFO:
202 case STATUSTYPE_TABLE:
203 EMIT("%d " SECTOR_FORMAT, sc->stripes, sc->chunk_mask + 1);
204 for (i = 0; i < sc->stripes; i++) {
205 format_dev_t(buffer, sc->stripe[i].dev->bdev->bd_dev);
206 EMIT(" %s " SECTOR_FORMAT, buffer,
207 sc->stripe[i].physical_start);
214 static struct target_type stripe_target = {
217 .module = THIS_MODULE,
221 .status = stripe_status,
224 int __init dm_stripe_init(void)
228 r = dm_register_target(&stripe_target);
230 DMWARN("striped target registration failed");
235 void dm_stripe_exit(void)
237 if (dm_unregister_target(&stripe_target))
238 DMWARN("striped target unregistration failed");