blob: 6a20e9ea0bfb0883f053c48e5e0c5d5558f01a74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001-2003 Sistina Software (UK) Limited.
3 *
4 * This file is released under the GPL.
5 */
6
Mikulas Patocka586e80e2008-10-21 17:44:59 +01007#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
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>
vignesh babu6f3c3f02007-10-19 22:38:44 +010014#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Alasdair G Kergon72d94862006-06-26 00:27:35 -070016#define DM_MSG_PREFIX "striped"
Brian Wooda25eb942008-02-08 02:11:22 +000017#define DM_IO_ERROR_THRESHOLD 15
Alasdair G Kergon72d94862006-06-26 00:27:35 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019struct stripe {
20 struct dm_dev *dev;
21 sector_t physical_start;
Brian Wooda25eb942008-02-08 02:11:22 +000022
23 atomic_t error_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024};
25
26struct stripe_c {
27 uint32_t stripes;
Mikulas Patockac96053b2010-08-12 04:14:21 +010028 int stripes_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 /* The size of this target / num. stripes */
31 sector_t stripe_width;
32
Mike Snitzereb850de2012-07-27 15:08:01 +010033 uint32_t chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Brian Wooda25eb942008-02-08 02:11:22 +000035 /* Needed for handling events */
36 struct dm_target *ti;
37
38 /* Work struct used for triggering events*/
Tejun Heof521f072011-01-13 19:59:57 +000039 struct work_struct trigger_event;
Brian Wooda25eb942008-02-08 02:11:22 +000040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct stripe stripe[0];
42};
43
Brian Wooda25eb942008-02-08 02:11:22 +000044/*
45 * An event is triggered whenever a drive
46 * drops out of a stripe volume.
47 */
48static void trigger_event(struct work_struct *work)
49{
Tejun Heof521f072011-01-13 19:59:57 +000050 struct stripe_c *sc = container_of(work, struct stripe_c,
51 trigger_event);
Brian Wooda25eb942008-02-08 02:11:22 +000052 dm_table_event(sc->ti->table);
Brian Wooda25eb942008-02-08 02:11:22 +000053}
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static inline struct stripe_c *alloc_context(unsigned int stripes)
56{
57 size_t len;
58
Mikulas Patockad63a5ce2008-10-21 17:44:57 +010059 if (dm_array_too_big(sizeof(struct stripe_c), sizeof(struct stripe),
60 stripes))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return NULL;
62
63 len = sizeof(struct stripe_c) + (sizeof(struct stripe) * stripes);
64
65 return kmalloc(len, GFP_KERNEL);
66}
67
68/*
69 * Parse a single <dev> <sector> pair
70 */
71static int get_stripe(struct dm_target *ti, struct stripe_c *sc,
72 unsigned int stripe, char **argv)
73{
Andrew Morton4ee218c2006-03-27 01:17:48 -080074 unsigned long long start;
Mikulas Patocka31998ef2012-03-28 18:41:26 +010075 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Mikulas Patocka31998ef2012-03-28 18:41:26 +010077 if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EINVAL;
79
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +000080 if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 &sc->stripe[stripe].dev))
82 return -ENXIO;
83
84 sc->stripe[stripe].physical_start = start;
Brian Wooda25eb942008-02-08 02:11:22 +000085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 0;
87}
88
89/*
90 * Construct a striped mapping.
Mike Snitzereb850de2012-07-27 15:08:01 +010091 * <number of stripes> <chunk size> [<dev_path> <offset>]+
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
94{
95 struct stripe_c *sc;
96 sector_t width;
97 uint32_t stripes;
98 uint32_t chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int r;
100 unsigned int i;
101
102 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700103 ti->error = "Not enough arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -EINVAL;
105 }
106
majianpeng1a66a082012-07-27 15:07:59 +0100107 if (kstrtouint(argv[0], 10, &stripes) || !stripes) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700108 ti->error = "Invalid stripe count";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -EINVAL;
110 }
111
Mikulas Patocka8f069b42012-07-27 15:08:01 +0100112 if (kstrtouint(argv[1], 10, &chunk_size) || !chunk_size) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700113 ti->error = "Invalid chunk_size";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -EINVAL;
115 }
116
Mike Snitzereb850de2012-07-27 15:08:01 +0100117 width = ti->len;
118 if (sector_div(width, chunk_size)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700119 ti->error = "Target length not divisible by "
Kevin Corry8ba32fd2006-03-16 23:04:03 -0800120 "chunk size";
121 return -EINVAL;
122 }
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (sector_div(width, stripes)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700125 ti->error = "Target length not divisible by "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 "number of stripes";
127 return -EINVAL;
128 }
129
130 /*
131 * Do we have enough arguments for that many stripes ?
132 */
133 if (argc != (2 + 2 * stripes)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700134 ti->error = "Not enough destinations "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 "specified";
136 return -EINVAL;
137 }
138
139 sc = alloc_context(stripes);
140 if (!sc) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700141 ti->error = "Memory allocation for striped context "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 "failed";
143 return -ENOMEM;
144 }
145
Tejun Heof521f072011-01-13 19:59:57 +0000146 INIT_WORK(&sc->trigger_event, trigger_event);
Brian Wooda25eb942008-02-08 02:11:22 +0000147
148 /* Set pointer to dm target; used in trigger_event */
149 sc->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 sc->stripes = stripes;
151 sc->stripe_width = width;
Mikulas Patockac96053b2010-08-12 04:14:21 +0100152
153 if (stripes & (stripes - 1))
154 sc->stripes_shift = -1;
Mikulas Patocka1df05482012-07-27 15:08:00 +0100155 else
156 sc->stripes_shift = __ffs(stripes);
Mikulas Patockac96053b2010-08-12 04:14:21 +0100157
Mike Snitzer542f9032012-07-27 15:08:00 +0100158 r = dm_set_target_max_io_len(ti, chunk_size);
159 if (r)
160 return r;
161
Mikulas Patocka374bf7e2009-06-22 10:12:22 +0100162 ti->num_flush_requests = stripes;
Mikulas Patocka7b76ec12010-08-12 04:14:26 +0100163 ti->num_discard_requests = stripes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Mike Snitzereb850de2012-07-27 15:08:01 +0100165 sc->chunk_size = chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /*
168 * Get the stripe destinations.
169 */
170 for (i = 0; i < stripes; i++) {
171 argv += 2;
172
173 r = get_stripe(ti, sc, i, argv);
174 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700175 ti->error = "Couldn't parse stripe destination";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 while (i--)
177 dm_put_device(ti, sc->stripe[i].dev);
178 kfree(sc);
179 return r;
180 }
Brian Wooda25eb942008-02-08 02:11:22 +0000181 atomic_set(&(sc->stripe[i].error_count), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183
184 ti->private = sc;
Brian Wooda25eb942008-02-08 02:11:22 +0000185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 0;
187}
188
189static void stripe_dtr(struct dm_target *ti)
190{
191 unsigned int i;
192 struct stripe_c *sc = (struct stripe_c *) ti->private;
193
194 for (i = 0; i < sc->stripes; i++)
195 dm_put_device(ti, sc->stripe[i].dev);
196
Tejun Heof521f072011-01-13 19:59:57 +0000197 flush_work_sync(&sc->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 kfree(sc);
199}
200
Mikulas Patocka65988522010-08-12 04:14:14 +0100201static void stripe_map_sector(struct stripe_c *sc, sector_t sector,
202 uint32_t *stripe, sector_t *result)
203{
Mike Snitzereb850de2012-07-27 15:08:01 +0100204 sector_t chunk = dm_target_offset(sc->ti, sector);
205 sector_t chunk_offset = sector_div(chunk, sc->chunk_size);
Mikulas Patocka65988522010-08-12 04:14:14 +0100206
Mikulas Patockac96053b2010-08-12 04:14:21 +0100207 if (sc->stripes_shift < 0)
208 *stripe = sector_div(chunk, sc->stripes);
209 else {
Mikulas Patocka1df05482012-07-27 15:08:00 +0100210 *stripe = chunk & (sc->stripes - 1);
Mikulas Patockac96053b2010-08-12 04:14:21 +0100211 chunk >>= sc->stripes_shift;
212 }
213
Mike Snitzereb850de2012-07-27 15:08:01 +0100214 *result = (chunk * sc->chunk_size) + chunk_offset;
Mikulas Patocka65988522010-08-12 04:14:14 +0100215}
216
Mikulas Patocka7b76ec12010-08-12 04:14:26 +0100217static void stripe_map_range_sector(struct stripe_c *sc, sector_t sector,
218 uint32_t target_stripe, sector_t *result)
219{
220 uint32_t stripe;
221
222 stripe_map_sector(sc, sector, &stripe, result);
223 if (stripe == target_stripe)
224 return;
Mike Snitzereb850de2012-07-27 15:08:01 +0100225
226 /* round down */
227 sector = *result;
228 *result -= sector_div(sector, sc->chunk_size);
229
Mikulas Patocka7b76ec12010-08-12 04:14:26 +0100230 if (target_stripe < stripe)
Mike Snitzereb850de2012-07-27 15:08:01 +0100231 *result += sc->chunk_size; /* next chunk */
Mikulas Patocka7b76ec12010-08-12 04:14:26 +0100232}
233
234static int stripe_map_discard(struct stripe_c *sc, struct bio *bio,
235 uint32_t target_stripe)
236{
237 sector_t begin, end;
238
239 stripe_map_range_sector(sc, bio->bi_sector, target_stripe, &begin);
240 stripe_map_range_sector(sc, bio->bi_sector + bio_sectors(bio),
241 target_stripe, &end);
242 if (begin < end) {
243 bio->bi_bdev = sc->stripe[target_stripe].dev->bdev;
244 bio->bi_sector = begin + sc->stripe[target_stripe].physical_start;
245 bio->bi_size = to_bytes(end - begin);
246 return DM_MAPIO_REMAPPED;
247 } else {
248 /* The range doesn't map to the target stripe */
249 bio_endio(bio, 0);
250 return DM_MAPIO_SUBMITTED;
251 }
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254static int stripe_map(struct dm_target *ti, struct bio *bio,
255 union map_info *map_context)
256{
Mikulas Patocka65988522010-08-12 04:14:14 +0100257 struct stripe_c *sc = ti->private;
Mikulas Patocka374bf7e2009-06-22 10:12:22 +0100258 uint32_t stripe;
Mike Snitzer57cba5d2010-08-12 04:14:04 +0100259 unsigned target_request_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Tejun Heod87f4c12010-09-03 11:56:19 +0200261 if (bio->bi_rw & REQ_FLUSH) {
Mike Snitzer57cba5d2010-08-12 04:14:04 +0100262 target_request_nr = map_context->target_request_nr;
263 BUG_ON(target_request_nr >= sc->stripes);
264 bio->bi_bdev = sc->stripe[target_request_nr].dev->bdev;
Mikulas Patocka374bf7e2009-06-22 10:12:22 +0100265 return DM_MAPIO_REMAPPED;
266 }
Mikulas Patocka7b76ec12010-08-12 04:14:26 +0100267 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
268 target_request_nr = map_context->target_request_nr;
269 BUG_ON(target_request_nr >= sc->stripes);
270 return stripe_map_discard(sc, bio, target_request_nr);
271 }
Mikulas Patocka374bf7e2009-06-22 10:12:22 +0100272
Mikulas Patocka65988522010-08-12 04:14:14 +0100273 stripe_map_sector(sc, bio->bi_sector, &stripe, &bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Mikulas Patocka65988522010-08-12 04:14:14 +0100275 bio->bi_sector += sc->stripe[stripe].physical_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 bio->bi_bdev = sc->stripe[stripe].dev->bdev;
Mikulas Patocka65988522010-08-12 04:14:14 +0100277
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800278 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Brian Wood4f7f5c62008-02-08 02:11:24 +0000281/*
282 * Stripe status:
283 *
284 * INFO
285 * #stripes [stripe_name <stripe_name>] [group word count]
286 * [error count 'A|D' <error count 'A|D'>]
287 *
288 * TABLE
289 * #stripes [stripe chunk size]
290 * [stripe_name physical_start <stripe_name physical_start>]
291 *
292 */
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static int stripe_status(struct dm_target *ti,
295 status_type_t type, char *result, unsigned int maxlen)
296{
297 struct stripe_c *sc = (struct stripe_c *) ti->private;
Brian Wood4f7f5c62008-02-08 02:11:24 +0000298 char buffer[sc->stripes + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unsigned int sz = 0;
300 unsigned int i;
301
302 switch (type) {
303 case STATUSTYPE_INFO:
Brian Wood4f7f5c62008-02-08 02:11:24 +0000304 DMEMIT("%d ", sc->stripes);
305 for (i = 0; i < sc->stripes; i++) {
306 DMEMIT("%s ", sc->stripe[i].dev->name);
307 buffer[i] = atomic_read(&(sc->stripe[i].error_count)) ?
308 'D' : 'A';
309 }
310 buffer[i] = '\0';
311 DMEMIT("1 %s", buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
313
314 case STATUSTYPE_TABLE:
Andrew Morton4ee218c2006-03-27 01:17:48 -0800315 DMEMIT("%d %llu", sc->stripes,
Mike Snitzereb850de2012-07-27 15:08:01 +0100316 (unsigned long long)sc->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 for (i = 0; i < sc->stripes; i++)
Andrew Morton4ee218c2006-03-27 01:17:48 -0800318 DMEMIT(" %s %llu", sc->stripe[i].dev->name,
319 (unsigned long long)sc->stripe[i].physical_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 break;
321 }
322 return 0;
323}
324
Brian Wooda25eb942008-02-08 02:11:22 +0000325static int stripe_end_io(struct dm_target *ti, struct bio *bio,
326 int error, union map_info *map_context)
327{
328 unsigned i;
329 char major_minor[16];
330 struct stripe_c *sc = ti->private;
331
332 if (!error)
333 return 0; /* I/O complete */
334
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200335 if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
Brian Wooda25eb942008-02-08 02:11:22 +0000336 return error;
337
338 if (error == -EOPNOTSUPP)
339 return error;
340
341 memset(major_minor, 0, sizeof(major_minor));
342 sprintf(major_minor, "%d:%d",
Tejun Heof331c022008-09-03 09:01:48 +0200343 MAJOR(disk_devt(bio->bi_bdev->bd_disk)),
344 MINOR(disk_devt(bio->bi_bdev->bd_disk)));
Brian Wooda25eb942008-02-08 02:11:22 +0000345
346 /*
347 * Test to see which stripe drive triggered the event
348 * and increment error count for all stripes on that device.
349 * If the error count for a given device exceeds the threshold
350 * value we will no longer trigger any further events.
351 */
352 for (i = 0; i < sc->stripes; i++)
353 if (!strcmp(sc->stripe[i].dev->name, major_minor)) {
354 atomic_inc(&(sc->stripe[i].error_count));
355 if (atomic_read(&(sc->stripe[i].error_count)) <
356 DM_IO_ERROR_THRESHOLD)
Tejun Heof521f072011-01-13 19:59:57 +0000357 schedule_work(&sc->trigger_event);
Brian Wooda25eb942008-02-08 02:11:22 +0000358 }
359
360 return error;
361}
362
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100363static int stripe_iterate_devices(struct dm_target *ti,
364 iterate_devices_callout_fn fn, void *data)
365{
366 struct stripe_c *sc = ti->private;
367 int ret = 0;
368 unsigned i = 0;
369
Mike Snitzer5dea2712009-07-23 20:30:42 +0100370 do {
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100371 ret = fn(ti, sc->stripe[i].dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +0100372 sc->stripe[i].physical_start,
373 sc->stripe_width, data);
374 } while (!ret && ++i < sc->stripes);
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100375
376 return ret;
377}
378
Mike Snitzer40bea432009-09-04 20:40:25 +0100379static void stripe_io_hints(struct dm_target *ti,
380 struct queue_limits *limits)
381{
382 struct stripe_c *sc = ti->private;
Mike Snitzereb850de2012-07-27 15:08:01 +0100383 unsigned chunk_size = sc->chunk_size << SECTOR_SHIFT;
Mike Snitzer40bea432009-09-04 20:40:25 +0100384
385 blk_limits_io_min(limits, chunk_size);
Martin K. Petersen3c5820c2009-09-11 21:54:52 +0200386 blk_limits_io_opt(limits, chunk_size * sc->stripes);
Mike Snitzer40bea432009-09-04 20:40:25 +0100387}
388
Mustafa Mesanovic29915202011-03-24 13:54:35 +0000389static int stripe_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
390 struct bio_vec *biovec, int max_size)
391{
392 struct stripe_c *sc = ti->private;
393 sector_t bvm_sector = bvm->bi_sector;
394 uint32_t stripe;
395 struct request_queue *q;
396
397 stripe_map_sector(sc, bvm_sector, &stripe, &bvm_sector);
398
399 q = bdev_get_queue(sc->stripe[stripe].dev->bdev);
400 if (!q->merge_bvec_fn)
401 return max_size;
402
403 bvm->bi_bdev = sc->stripe[stripe].dev->bdev;
404 bvm->bi_sector = sc->stripe[stripe].physical_start + bvm_sector;
405
406 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static struct target_type stripe_target = {
410 .name = "striped",
Mike Snitzereb850de2012-07-27 15:08:01 +0100411 .version = {1, 5, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 .module = THIS_MODULE,
413 .ctr = stripe_ctr,
414 .dtr = stripe_dtr,
415 .map = stripe_map,
Brian Wooda25eb942008-02-08 02:11:22 +0000416 .end_io = stripe_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 .status = stripe_status,
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100418 .iterate_devices = stripe_iterate_devices,
Mike Snitzer40bea432009-09-04 20:40:25 +0100419 .io_hints = stripe_io_hints,
Mustafa Mesanovic29915202011-03-24 13:54:35 +0000420 .merge = stripe_merge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421};
422
423int __init dm_stripe_init(void)
424{
425 int r;
426
427 r = dm_register_target(&stripe_target);
Heinz Mauelshagen6edebde2008-11-13 23:38:56 +0000428 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700429 DMWARN("target registration failed");
Heinz Mauelshagen6edebde2008-11-13 23:38:56 +0000430 return r;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 return r;
434}
435
436void dm_stripe_exit(void)
437{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000438 dm_unregister_target(&stripe_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}