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