Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2001-2003 Sistina Software (UK) Limited. | |
3 | * | |
4 | * This file is released under the GPL. | |
5 | */ | |
6 | ||
586e80e6 | 7 | #include <linux/device-mapper.h> |
1da177e4 LT |
8 | |
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> | |
6f3c3f0a | 14 | #include <linux/log2.h> |
1da177e4 | 15 | |
72d94861 | 16 | #define DM_MSG_PREFIX "striped" |
a25eb944 | 17 | #define DM_IO_ERROR_THRESHOLD 15 |
72d94861 | 18 | |
1da177e4 LT |
19 | struct stripe { |
20 | struct dm_dev *dev; | |
21 | sector_t physical_start; | |
a25eb944 BW |
22 | |
23 | atomic_t error_count; | |
1da177e4 LT |
24 | }; |
25 | ||
26 | struct stripe_c { | |
27 | uint32_t stripes; | |
c96053b7 | 28 | int stripes_shift; |
1da177e4 LT |
29 | |
30 | /* The size of this target / num. stripes */ | |
31 | sector_t stripe_width; | |
32 | ||
33 | /* stripe chunk size */ | |
34 | uint32_t chunk_shift; | |
35 | sector_t chunk_mask; | |
36 | ||
a25eb944 BW |
37 | /* Needed for handling events */ |
38 | struct dm_target *ti; | |
39 | ||
40 | /* Work struct used for triggering events*/ | |
f521f074 | 41 | struct work_struct trigger_event; |
a25eb944 | 42 | |
1da177e4 LT |
43 | struct stripe stripe[0]; |
44 | }; | |
45 | ||
a25eb944 BW |
46 | /* |
47 | * An event is triggered whenever a drive | |
48 | * drops out of a stripe volume. | |
49 | */ | |
50 | static void trigger_event(struct work_struct *work) | |
51 | { | |
f521f074 TH |
52 | struct stripe_c *sc = container_of(work, struct stripe_c, |
53 | trigger_event); | |
a25eb944 | 54 | dm_table_event(sc->ti->table); |
a25eb944 BW |
55 | } |
56 | ||
1da177e4 LT |
57 | static inline struct stripe_c *alloc_context(unsigned int stripes) |
58 | { | |
59 | size_t len; | |
60 | ||
d63a5ce3 MP |
61 | if (dm_array_too_big(sizeof(struct stripe_c), sizeof(struct stripe), |
62 | stripes)) | |
1da177e4 LT |
63 | return NULL; |
64 | ||
65 | len = sizeof(struct stripe_c) + (sizeof(struct stripe) * stripes); | |
66 | ||
67 | return kmalloc(len, GFP_KERNEL); | |
68 | } | |
69 | ||
70 | /* | |
71 | * Parse a single <dev> <sector> pair | |
72 | */ | |
73 | static int get_stripe(struct dm_target *ti, struct stripe_c *sc, | |
74 | unsigned int stripe, char **argv) | |
75 | { | |
4ee218cd | 76 | unsigned long long start; |
31998ef1 | 77 | char dummy; |
1da177e4 | 78 | |
31998ef1 | 79 | if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1) |
1da177e4 LT |
80 | return -EINVAL; |
81 | ||
8215d6ec | 82 | if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), |
1da177e4 LT |
83 | &sc->stripe[stripe].dev)) |
84 | return -ENXIO; | |
85 | ||
86 | sc->stripe[stripe].physical_start = start; | |
a25eb944 | 87 | |
1da177e4 LT |
88 | return 0; |
89 | } | |
90 | ||
91 | /* | |
92 | * Construct a striped mapping. | |
93 | * <number of stripes> <chunk size (2^^n)> [<dev_path> <offset>]+ | |
94 | */ | |
95 | static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |
96 | { | |
97 | struct stripe_c *sc; | |
98 | sector_t width; | |
99 | uint32_t stripes; | |
100 | uint32_t chunk_size; | |
1da177e4 LT |
101 | int r; |
102 | unsigned int i; | |
103 | ||
104 | if (argc < 2) { | |
72d94861 | 105 | ti->error = "Not enough arguments"; |
1da177e4 LT |
106 | return -EINVAL; |
107 | } | |
108 | ||
1a66a08a | 109 | if (kstrtouint(argv[0], 10, &stripes) || !stripes) { |
72d94861 | 110 | ti->error = "Invalid stripe count"; |
1da177e4 LT |
111 | return -EINVAL; |
112 | } | |
113 | ||
1a66a08a | 114 | if (kstrtouint(argv[1], 10, &chunk_size)) { |
72d94861 | 115 | ti->error = "Invalid chunk_size"; |
1da177e4 LT |
116 | return -EINVAL; |
117 | } | |
118 | ||
119 | /* | |
120 | * chunk_size is a power of two | |
121 | */ | |
6f3c3f0a | 122 | if (!is_power_of_2(chunk_size) || |
1da177e4 | 123 | (chunk_size < (PAGE_SIZE >> SECTOR_SHIFT))) { |
72d94861 | 124 | ti->error = "Invalid chunk size"; |
1da177e4 LT |
125 | return -EINVAL; |
126 | } | |
127 | ||
a22c96c7 | 128 | if (ti->len & (chunk_size - 1)) { |
72d94861 | 129 | ti->error = "Target length not divisible by " |
8ba32fde KC |
130 | "chunk size"; |
131 | return -EINVAL; | |
132 | } | |
133 | ||
1da177e4 | 134 | if (sector_div(width, stripes)) { |
72d94861 | 135 | ti->error = "Target length not divisible by " |
1da177e4 LT |
136 | "number of stripes"; |
137 | return -EINVAL; | |
138 | } | |
139 | ||
140 | /* | |
141 | * Do we have enough arguments for that many stripes ? | |
142 | */ | |
143 | if (argc != (2 + 2 * stripes)) { | |
72d94861 | 144 | ti->error = "Not enough destinations " |
1da177e4 LT |
145 | "specified"; |
146 | return -EINVAL; | |
147 | } | |
148 | ||
149 | sc = alloc_context(stripes); | |
150 | if (!sc) { | |
72d94861 | 151 | ti->error = "Memory allocation for striped context " |
1da177e4 LT |
152 | "failed"; |
153 | return -ENOMEM; | |
154 | } | |
155 | ||
f521f074 | 156 | INIT_WORK(&sc->trigger_event, trigger_event); |
a25eb944 BW |
157 | |
158 | /* Set pointer to dm target; used in trigger_event */ | |
159 | sc->ti = ti; | |
1da177e4 LT |
160 | sc->stripes = stripes; |
161 | sc->stripe_width = width; | |
c96053b7 MP |
162 | |
163 | if (stripes & (stripes - 1)) | |
164 | sc->stripes_shift = -1; | |
1df05483 MP |
165 | else |
166 | sc->stripes_shift = __ffs(stripes); | |
c96053b7 | 167 | |
542f9038 MS |
168 | r = dm_set_target_max_io_len(ti, chunk_size); |
169 | if (r) | |
170 | return r; | |
171 | ||
374bf7e7 | 172 | ti->num_flush_requests = stripes; |
7b76ec11 | 173 | ti->num_discard_requests = stripes; |
1da177e4 | 174 | |
c96053b7 | 175 | sc->chunk_shift = ffs(chunk_size) - 1; |
1da177e4 | 176 | sc->chunk_mask = ((sector_t) chunk_size) - 1; |
1da177e4 LT |
177 | |
178 | /* | |
179 | * Get the stripe destinations. | |
180 | */ | |
181 | for (i = 0; i < stripes; i++) { | |
182 | argv += 2; | |
183 | ||
184 | r = get_stripe(ti, sc, i, argv); | |
185 | if (r < 0) { | |
72d94861 | 186 | ti->error = "Couldn't parse stripe destination"; |
1da177e4 LT |
187 | while (i--) |
188 | dm_put_device(ti, sc->stripe[i].dev); | |
189 | kfree(sc); | |
190 | return r; | |
191 | } | |
a25eb944 | 192 | atomic_set(&(sc->stripe[i].error_count), 0); |
1da177e4 LT |
193 | } |
194 | ||
195 | ti->private = sc; | |
a25eb944 | 196 | |
1da177e4 LT |
197 | return 0; |
198 | } | |
199 | ||
200 | static void stripe_dtr(struct dm_target *ti) | |
201 | { | |
202 | unsigned int i; | |
203 | struct stripe_c *sc = (struct stripe_c *) ti->private; | |
204 | ||
205 | for (i = 0; i < sc->stripes; i++) | |
206 | dm_put_device(ti, sc->stripe[i].dev); | |
207 | ||
f521f074 | 208 | flush_work_sync(&sc->trigger_event); |
1da177e4 LT |
209 | kfree(sc); |
210 | } | |
211 | ||
65988525 MP |
212 | static void stripe_map_sector(struct stripe_c *sc, sector_t sector, |
213 | uint32_t *stripe, sector_t *result) | |
214 | { | |
215 | sector_t offset = dm_target_offset(sc->ti, sector); | |
216 | sector_t chunk = offset >> sc->chunk_shift; | |
217 | ||
c96053b7 MP |
218 | if (sc->stripes_shift < 0) |
219 | *stripe = sector_div(chunk, sc->stripes); | |
220 | else { | |
1df05483 | 221 | *stripe = chunk & (sc->stripes - 1); |
c96053b7 MP |
222 | chunk >>= sc->stripes_shift; |
223 | } | |
224 | ||
65988525 MP |
225 | *result = (chunk << sc->chunk_shift) | (offset & sc->chunk_mask); |
226 | } | |
227 | ||
7b76ec11 MP |
228 | static void stripe_map_range_sector(struct stripe_c *sc, sector_t sector, |
229 | uint32_t target_stripe, sector_t *result) | |
230 | { | |
231 | uint32_t stripe; | |
232 | ||
233 | stripe_map_sector(sc, sector, &stripe, result); | |
234 | if (stripe == target_stripe) | |
235 | return; | |
236 | *result &= ~sc->chunk_mask; /* round down */ | |
237 | if (target_stripe < stripe) | |
238 | *result += sc->chunk_mask + 1; /* next chunk */ | |
239 | } | |
240 | ||
241 | static int stripe_map_discard(struct stripe_c *sc, struct bio *bio, | |
242 | uint32_t target_stripe) | |
243 | { | |
244 | sector_t begin, end; | |
245 | ||
246 | stripe_map_range_sector(sc, bio->bi_sector, target_stripe, &begin); | |
247 | stripe_map_range_sector(sc, bio->bi_sector + bio_sectors(bio), | |
248 | target_stripe, &end); | |
249 | if (begin < end) { | |
250 | bio->bi_bdev = sc->stripe[target_stripe].dev->bdev; | |
251 | bio->bi_sector = begin + sc->stripe[target_stripe].physical_start; | |
252 | bio->bi_size = to_bytes(end - begin); | |
253 | return DM_MAPIO_REMAPPED; | |
254 | } else { | |
255 | /* The range doesn't map to the target stripe */ | |
256 | bio_endio(bio, 0); | |
257 | return DM_MAPIO_SUBMITTED; | |
258 | } | |
259 | } | |
260 | ||
1da177e4 LT |
261 | static int stripe_map(struct dm_target *ti, struct bio *bio, |
262 | union map_info *map_context) | |
263 | { | |
65988525 | 264 | struct stripe_c *sc = ti->private; |
374bf7e7 | 265 | uint32_t stripe; |
57cba5d3 | 266 | unsigned target_request_nr; |
1da177e4 | 267 | |
d87f4c14 | 268 | if (bio->bi_rw & REQ_FLUSH) { |
57cba5d3 MS |
269 | target_request_nr = map_context->target_request_nr; |
270 | BUG_ON(target_request_nr >= sc->stripes); | |
271 | bio->bi_bdev = sc->stripe[target_request_nr].dev->bdev; | |
374bf7e7 MP |
272 | return DM_MAPIO_REMAPPED; |
273 | } | |
7b76ec11 MP |
274 | if (unlikely(bio->bi_rw & REQ_DISCARD)) { |
275 | target_request_nr = map_context->target_request_nr; | |
276 | BUG_ON(target_request_nr >= sc->stripes); | |
277 | return stripe_map_discard(sc, bio, target_request_nr); | |
278 | } | |
374bf7e7 | 279 | |
65988525 | 280 | stripe_map_sector(sc, bio->bi_sector, &stripe, &bio->bi_sector); |
1da177e4 | 281 | |
65988525 | 282 | bio->bi_sector += sc->stripe[stripe].physical_start; |
1da177e4 | 283 | bio->bi_bdev = sc->stripe[stripe].dev->bdev; |
65988525 | 284 | |
d2a7ad29 | 285 | return DM_MAPIO_REMAPPED; |
1da177e4 LT |
286 | } |
287 | ||
4f7f5c67 BW |
288 | /* |
289 | * Stripe status: | |
290 | * | |
291 | * INFO | |
292 | * #stripes [stripe_name <stripe_name>] [group word count] | |
293 | * [error count 'A|D' <error count 'A|D'>] | |
294 | * | |
295 | * TABLE | |
296 | * #stripes [stripe chunk size] | |
297 | * [stripe_name physical_start <stripe_name physical_start>] | |
298 | * | |
299 | */ | |
300 | ||
1da177e4 LT |
301 | static int stripe_status(struct dm_target *ti, |
302 | status_type_t type, char *result, unsigned int maxlen) | |
303 | { | |
304 | struct stripe_c *sc = (struct stripe_c *) ti->private; | |
4f7f5c67 | 305 | char buffer[sc->stripes + 1]; |
1da177e4 LT |
306 | unsigned int sz = 0; |
307 | unsigned int i; | |
308 | ||
309 | switch (type) { | |
310 | case STATUSTYPE_INFO: | |
4f7f5c67 BW |
311 | DMEMIT("%d ", sc->stripes); |
312 | for (i = 0; i < sc->stripes; i++) { | |
313 | DMEMIT("%s ", sc->stripe[i].dev->name); | |
314 | buffer[i] = atomic_read(&(sc->stripe[i].error_count)) ? | |
315 | 'D' : 'A'; | |
316 | } | |
317 | buffer[i] = '\0'; | |
318 | DMEMIT("1 %s", buffer); | |
1da177e4 LT |
319 | break; |
320 | ||
321 | case STATUSTYPE_TABLE: | |
4ee218cd AM |
322 | DMEMIT("%d %llu", sc->stripes, |
323 | (unsigned long long)sc->chunk_mask + 1); | |
1da177e4 | 324 | for (i = 0; i < sc->stripes; i++) |
4ee218cd AM |
325 | DMEMIT(" %s %llu", sc->stripe[i].dev->name, |
326 | (unsigned long long)sc->stripe[i].physical_start); | |
1da177e4 LT |
327 | break; |
328 | } | |
329 | return 0; | |
330 | } | |
331 | ||
a25eb944 BW |
332 | static int stripe_end_io(struct dm_target *ti, struct bio *bio, |
333 | int error, union map_info *map_context) | |
334 | { | |
335 | unsigned i; | |
336 | char major_minor[16]; | |
337 | struct stripe_c *sc = ti->private; | |
338 | ||
339 | if (!error) | |
340 | return 0; /* I/O complete */ | |
341 | ||
7b6d91da | 342 | if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD)) |
a25eb944 BW |
343 | return error; |
344 | ||
345 | if (error == -EOPNOTSUPP) | |
346 | return error; | |
347 | ||
348 | memset(major_minor, 0, sizeof(major_minor)); | |
349 | sprintf(major_minor, "%d:%d", | |
f331c029 TH |
350 | MAJOR(disk_devt(bio->bi_bdev->bd_disk)), |
351 | MINOR(disk_devt(bio->bi_bdev->bd_disk))); | |
a25eb944 BW |
352 | |
353 | /* | |
354 | * Test to see which stripe drive triggered the event | |
355 | * and increment error count for all stripes on that device. | |
356 | * If the error count for a given device exceeds the threshold | |
357 | * value we will no longer trigger any further events. | |
358 | */ | |
359 | for (i = 0; i < sc->stripes; i++) | |
360 | if (!strcmp(sc->stripe[i].dev->name, major_minor)) { | |
361 | atomic_inc(&(sc->stripe[i].error_count)); | |
362 | if (atomic_read(&(sc->stripe[i].error_count)) < | |
363 | DM_IO_ERROR_THRESHOLD) | |
f521f074 | 364 | schedule_work(&sc->trigger_event); |
a25eb944 BW |
365 | } |
366 | ||
367 | return error; | |
368 | } | |
369 | ||
af4874e0 MS |
370 | static int stripe_iterate_devices(struct dm_target *ti, |
371 | iterate_devices_callout_fn fn, void *data) | |
372 | { | |
373 | struct stripe_c *sc = ti->private; | |
374 | int ret = 0; | |
375 | unsigned i = 0; | |
376 | ||
5dea271b | 377 | do { |
af4874e0 | 378 | ret = fn(ti, sc->stripe[i].dev, |
5dea271b MS |
379 | sc->stripe[i].physical_start, |
380 | sc->stripe_width, data); | |
381 | } while (!ret && ++i < sc->stripes); | |
af4874e0 MS |
382 | |
383 | return ret; | |
384 | } | |
385 | ||
40bea431 MS |
386 | static void stripe_io_hints(struct dm_target *ti, |
387 | struct queue_limits *limits) | |
388 | { | |
389 | struct stripe_c *sc = ti->private; | |
390 | unsigned chunk_size = (sc->chunk_mask + 1) << 9; | |
391 | ||
392 | blk_limits_io_min(limits, chunk_size); | |
3c5820c7 | 393 | blk_limits_io_opt(limits, chunk_size * sc->stripes); |
40bea431 MS |
394 | } |
395 | ||
29915202 MM |
396 | static int stripe_merge(struct dm_target *ti, struct bvec_merge_data *bvm, |
397 | struct bio_vec *biovec, int max_size) | |
398 | { | |
399 | struct stripe_c *sc = ti->private; | |
400 | sector_t bvm_sector = bvm->bi_sector; | |
401 | uint32_t stripe; | |
402 | struct request_queue *q; | |
403 | ||
404 | stripe_map_sector(sc, bvm_sector, &stripe, &bvm_sector); | |
405 | ||
406 | q = bdev_get_queue(sc->stripe[stripe].dev->bdev); | |
407 | if (!q->merge_bvec_fn) | |
408 | return max_size; | |
409 | ||
410 | bvm->bi_bdev = sc->stripe[stripe].dev->bdev; | |
411 | bvm->bi_sector = sc->stripe[stripe].physical_start + bvm_sector; | |
412 | ||
413 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); | |
414 | } | |
415 | ||
1da177e4 LT |
416 | static struct target_type stripe_target = { |
417 | .name = "striped", | |
29915202 | 418 | .version = {1, 4, 0}, |
1da177e4 LT |
419 | .module = THIS_MODULE, |
420 | .ctr = stripe_ctr, | |
421 | .dtr = stripe_dtr, | |
422 | .map = stripe_map, | |
a25eb944 | 423 | .end_io = stripe_end_io, |
1da177e4 | 424 | .status = stripe_status, |
af4874e0 | 425 | .iterate_devices = stripe_iterate_devices, |
40bea431 | 426 | .io_hints = stripe_io_hints, |
29915202 | 427 | .merge = stripe_merge, |
1da177e4 LT |
428 | }; |
429 | ||
430 | int __init dm_stripe_init(void) | |
431 | { | |
432 | int r; | |
433 | ||
434 | r = dm_register_target(&stripe_target); | |
6edebdee | 435 | if (r < 0) { |
72d94861 | 436 | DMWARN("target registration failed"); |
6edebdee HM |
437 | return r; |
438 | } | |
1da177e4 LT |
439 | |
440 | return r; | |
441 | } | |
442 | ||
443 | void dm_stripe_exit(void) | |
444 | { | |
10d3bd09 | 445 | dm_unregister_target(&stripe_target); |
1da177e4 | 446 | } |