Merge tag 'devicetree-fixes-for-6.6-1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / drivers / md / dm-stripe.c
CommitLineData
3bd94003 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * Copyright (C) 2001-2003 Sistina Software (UK) Limited.
4 *
5 * This file is released under the GPL.
6 */
7
7fff5e8f 8#include "dm.h"
586e80e6 9#include <linux/device-mapper.h>
1da177e4
LT
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/blkdev.h>
14#include <linux/bio.h>
817bf402 15#include <linux/dax.h>
1da177e4 16#include <linux/slab.h>
6f3c3f0a 17#include <linux/log2.h>
1da177e4 18
a7e8f7fb
TH
19static struct workqueue_struct *dm_stripe_wq;
20
72d94861 21#define DM_MSG_PREFIX "striped"
a25eb944 22#define DM_IO_ERROR_THRESHOLD 15
72d94861 23
1da177e4
LT
24struct stripe {
25 struct dm_dev *dev;
26 sector_t physical_start;
a25eb944
BW
27
28 atomic_t error_count;
1da177e4
LT
29};
30
31struct stripe_c {
32 uint32_t stripes;
c96053b7 33 int stripes_shift;
1da177e4
LT
34
35 /* The size of this target / num. stripes */
36 sector_t stripe_width;
37
eb850de6 38 uint32_t chunk_size;
33d07c0d 39 int chunk_size_shift;
1da177e4 40
a25eb944
BW
41 /* Needed for handling events */
42 struct dm_target *ti;
43
44 /* Work struct used for triggering events*/
f521f074 45 struct work_struct trigger_event;
a25eb944 46
b18ae8dd 47 struct stripe stripe[];
1da177e4
LT
48};
49
a25eb944
BW
50/*
51 * An event is triggered whenever a drive
52 * drops out of a stripe volume.
53 */
54static void trigger_event(struct work_struct *work)
55{
f521f074
TH
56 struct stripe_c *sc = container_of(work, struct stripe_c,
57 trigger_event);
a25eb944 58 dm_table_event(sc->ti->table);
a25eb944
BW
59}
60
1da177e4
LT
61/*
62 * Parse a single <dev> <sector> pair
63 */
64static int get_stripe(struct dm_target *ti, struct stripe_c *sc,
65 unsigned int stripe, char **argv)
66{
4ee218cd 67 unsigned long long start;
31998ef1 68 char dummy;
e80d1c80 69 int ret;
1da177e4 70
31998ef1 71 if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1)
1da177e4
LT
72 return -EINVAL;
73
e80d1c80
VG
74 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
75 &sc->stripe[stripe].dev);
76 if (ret)
77 return ret;
1da177e4
LT
78
79 sc->stripe[stripe].physical_start = start;
a25eb944 80
1da177e4
LT
81 return 0;
82}
83
84/*
85 * Construct a striped mapping.
eb850de6 86 * <number of stripes> <chunk size> [<dev_path> <offset>]+
1da177e4
LT
87 */
88static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
89{
90 struct stripe_c *sc;
d793e684 91 sector_t width, tmp_len;
1da177e4
LT
92 uint32_t stripes;
93 uint32_t chunk_size;
1da177e4
LT
94 int r;
95 unsigned int i;
96
97 if (argc < 2) {
72d94861 98 ti->error = "Not enough arguments";
1da177e4
LT
99 return -EINVAL;
100 }
101
1a66a08a 102 if (kstrtouint(argv[0], 10, &stripes) || !stripes) {
72d94861 103 ti->error = "Invalid stripe count";
1da177e4
LT
104 return -EINVAL;
105 }
106
8f069b41 107 if (kstrtouint(argv[1], 10, &chunk_size) || !chunk_size) {
eb850de6 108 ti->error = "Invalid chunk_size";
1da177e4
LT
109 return -EINVAL;
110 }
111
eb850de6 112 width = ti->len;
d793e684 113 if (sector_div(width, stripes)) {
2e84fecf 114 ti->error = "Target length not divisible by number of stripes";
8ba32fde
KC
115 return -EINVAL;
116 }
117
d793e684
MS
118 tmp_len = width;
119 if (sector_div(tmp_len, chunk_size)) {
2e84fecf 120 ti->error = "Target length not divisible by chunk size";
1da177e4
LT
121 return -EINVAL;
122 }
123
124 /*
125 * Do we have enough arguments for that many stripes ?
126 */
127 if (argc != (2 + 2 * stripes)) {
2e84fecf 128 ti->error = "Not enough destinations specified";
1da177e4
LT
129 return -EINVAL;
130 }
131
8adeac3b 132 sc = kmalloc(struct_size(sc, stripe, stripes), GFP_KERNEL);
1da177e4 133 if (!sc) {
2e84fecf 134 ti->error = "Memory allocation for striped context failed";
1da177e4
LT
135 return -ENOMEM;
136 }
137
f521f074 138 INIT_WORK(&sc->trigger_event, trigger_event);
a25eb944
BW
139
140 /* Set pointer to dm target; used in trigger_event */
141 sc->ti = ti;
1da177e4
LT
142 sc->stripes = stripes;
143 sc->stripe_width = width;
c96053b7
MP
144
145 if (stripes & (stripes - 1))
146 sc->stripes_shift = -1;
1df05483
MP
147 else
148 sc->stripes_shift = __ffs(stripes);
c96053b7 149
542f9038 150 r = dm_set_target_max_io_len(ti, chunk_size);
a3f2af25
PK
151 if (r) {
152 kfree(sc);
542f9038 153 return r;
a3f2af25 154 }
542f9038 155
55a62eef
AK
156 ti->num_flush_bios = stripes;
157 ti->num_discard_bios = stripes;
00716545 158 ti->num_secure_erase_bios = stripes;
ac62d620 159 ti->num_write_zeroes_bios = stripes;
1da177e4 160
eb850de6 161 sc->chunk_size = chunk_size;
33d07c0d
MP
162 if (chunk_size & (chunk_size - 1))
163 sc->chunk_size_shift = -1;
164 else
165 sc->chunk_size_shift = __ffs(chunk_size);
1da177e4
LT
166
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) {
72d94861 175 ti->error = "Couldn't parse stripe destination";
1da177e4
LT
176 while (i--)
177 dm_put_device(ti, sc->stripe[i].dev);
178 kfree(sc);
179 return r;
180 }
a25eb944 181 atomic_set(&(sc->stripe[i].error_count), 0);
1da177e4
LT
182 }
183
184 ti->private = sc;
a25eb944 185
1da177e4
LT
186 return 0;
187}
188
189static void stripe_dtr(struct dm_target *ti)
190{
191 unsigned int i;
26cb62a2 192 struct stripe_c *sc = ti->private;
1da177e4
LT
193
194 for (i = 0; i < sc->stripes; i++)
195 dm_put_device(ti, sc->stripe[i].dev);
196
43829731 197 flush_work(&sc->trigger_event);
1da177e4
LT
198 kfree(sc);
199}
200
65988525
MP
201static void stripe_map_sector(struct stripe_c *sc, sector_t sector,
202 uint32_t *stripe, sector_t *result)
203{
eb850de6 204 sector_t chunk = dm_target_offset(sc->ti, sector);
33d07c0d
MP
205 sector_t chunk_offset;
206
207 if (sc->chunk_size_shift < 0)
208 chunk_offset = sector_div(chunk, sc->chunk_size);
209 else {
210 chunk_offset = chunk & (sc->chunk_size - 1);
211 chunk >>= sc->chunk_size_shift;
212 }
65988525 213
c96053b7
MP
214 if (sc->stripes_shift < 0)
215 *stripe = sector_div(chunk, sc->stripes);
216 else {
1df05483 217 *stripe = chunk & (sc->stripes - 1);
c96053b7
MP
218 chunk >>= sc->stripes_shift;
219 }
220
33d07c0d
MP
221 if (sc->chunk_size_shift < 0)
222 chunk *= sc->chunk_size;
223 else
224 chunk <<= sc->chunk_size_shift;
225
226 *result = chunk + chunk_offset;
65988525
MP
227}
228
7b76ec11
MP
229static void stripe_map_range_sector(struct stripe_c *sc, sector_t sector,
230 uint32_t target_stripe, sector_t *result)
231{
232 uint32_t stripe;
233
234 stripe_map_sector(sc, sector, &stripe, result);
235 if (stripe == target_stripe)
236 return;
eb850de6
MS
237
238 /* round down */
239 sector = *result;
33d07c0d
MP
240 if (sc->chunk_size_shift < 0)
241 *result -= sector_div(sector, sc->chunk_size);
242 else
243 *result = sector & ~(sector_t)(sc->chunk_size - 1);
eb850de6 244
7b76ec11 245 if (target_stripe < stripe)
eb850de6 246 *result += sc->chunk_size; /* next chunk */
7b76ec11
MP
247}
248
45e621d4
MS
249static int stripe_map_range(struct stripe_c *sc, struct bio *bio,
250 uint32_t target_stripe)
7b76ec11
MP
251{
252 sector_t begin, end;
253
4f024f37
KO
254 stripe_map_range_sector(sc, bio->bi_iter.bi_sector,
255 target_stripe, &begin);
f73a1c7d 256 stripe_map_range_sector(sc, bio_end_sector(bio),
7b76ec11
MP
257 target_stripe, &end);
258 if (begin < end) {
74d46992 259 bio_set_dev(bio, sc->stripe[target_stripe].dev->bdev);
4f024f37
KO
260 bio->bi_iter.bi_sector = begin +
261 sc->stripe[target_stripe].physical_start;
262 bio->bi_iter.bi_size = to_bytes(end - begin);
7b76ec11 263 return DM_MAPIO_REMAPPED;
7b76ec11 264 }
1c3fe2fa
HM
265
266 /* The range doesn't map to the target stripe */
267 bio_endio(bio);
268 return DM_MAPIO_SUBMITTED;
7b76ec11
MP
269}
270
7de3ee57 271static int stripe_map(struct dm_target *ti, struct bio *bio)
1da177e4 272{
65988525 273 struct stripe_c *sc = ti->private;
374bf7e7 274 uint32_t stripe;
86a3238c 275 unsigned int target_bio_nr;
1da177e4 276
1eff9d32 277 if (bio->bi_opf & REQ_PREFLUSH) {
55a62eef
AK
278 target_bio_nr = dm_bio_get_target_bio_nr(bio);
279 BUG_ON(target_bio_nr >= sc->stripes);
74d46992 280 bio_set_dev(bio, sc->stripe[target_bio_nr].dev->bdev);
374bf7e7
MP
281 return DM_MAPIO_REMAPPED;
282 }
e6047149 283 if (unlikely(bio_op(bio) == REQ_OP_DISCARD) ||
00716545 284 unlikely(bio_op(bio) == REQ_OP_SECURE_ERASE) ||
a773187e 285 unlikely(bio_op(bio) == REQ_OP_WRITE_ZEROES)) {
55a62eef
AK
286 target_bio_nr = dm_bio_get_target_bio_nr(bio);
287 BUG_ON(target_bio_nr >= sc->stripes);
288 return stripe_map_range(sc, bio, target_bio_nr);
7b76ec11 289 }
374bf7e7 290
4f024f37
KO
291 stripe_map_sector(sc, bio->bi_iter.bi_sector,
292 &stripe, &bio->bi_iter.bi_sector);
1da177e4 293
4f024f37 294 bio->bi_iter.bi_sector += sc->stripe[stripe].physical_start;
74d46992 295 bio_set_dev(bio, sc->stripe[stripe].dev->bdev);
65988525 296
d2a7ad29 297 return DM_MAPIO_REMAPPED;
1da177e4
LT
298}
299
5d2a228b 300#if IS_ENABLED(CONFIG_FS_DAX)
2a68553e 301static struct dax_device *stripe_dax_pgoff(struct dm_target *ti, pgoff_t *pgoff)
beec25b4
TK
302{
303 struct stripe_c *sc = ti->private;
beec25b4 304 struct block_device *bdev;
2a68553e 305 sector_t dev_sector;
817bf402 306 uint32_t stripe;
beec25b4 307
2a68553e 308 stripe_map_sector(sc, *pgoff * PAGE_SECTORS, &stripe, &dev_sector);
817bf402 309 dev_sector += sc->stripe[stripe].physical_start;
beec25b4
TK
310 bdev = sc->stripe[stripe].dev->bdev;
311
2a68553e
CH
312 *pgoff = (get_start_sect(bdev) + dev_sector) >> PAGE_SECTORS_SHIFT;
313 return sc->stripe[stripe].dev->dax_dev;
314}
315
316static long stripe_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
e511c4a3
JC
317 long nr_pages, enum dax_access_mode mode, void **kaddr,
318 pfn_t *pfn)
2a68553e
CH
319{
320 struct dax_device *dax_dev = stripe_dax_pgoff(ti, &pgoff);
321
e511c4a3 322 return dax_direct_access(dax_dev, pgoff, nr_pages, mode, kaddr, pfn);
beec25b4
TK
323}
324
cdf6cdcd
VG
325static int stripe_dax_zero_page_range(struct dm_target *ti, pgoff_t pgoff,
326 size_t nr_pages)
327{
2a68553e 328 struct dax_device *dax_dev = stripe_dax_pgoff(ti, &pgoff);
cdf6cdcd 329
cdf6cdcd
VG
330 return dax_zero_page_range(dax_dev, pgoff, nr_pages);
331}
332
047218ec
JC
333static size_t stripe_dax_recovery_write(struct dm_target *ti, pgoff_t pgoff,
334 void *addr, size_t bytes, struct iov_iter *i)
335{
336 struct dax_device *dax_dev = stripe_dax_pgoff(ti, &pgoff);
337
338 return dax_recovery_write(dax_dev, pgoff, addr, bytes, i);
339}
340
976431b0
DW
341#else
342#define stripe_dax_direct_access NULL
cdf6cdcd 343#define stripe_dax_zero_page_range NULL
047218ec 344#define stripe_dax_recovery_write NULL
976431b0
DW
345#endif
346
4f7f5c67
BW
347/*
348 * Stripe status:
349 *
350 * INFO
351 * #stripes [stripe_name <stripe_name>] [group word count]
352 * [error count 'A|D' <error count 'A|D'>]
353 *
354 * TABLE
355 * #stripes [stripe chunk size]
356 * [stripe_name physical_start <stripe_name physical_start>]
357 *
358 */
359
fd7c092e 360static void stripe_status(struct dm_target *ti, status_type_t type,
86a3238c 361 unsigned int status_flags, char *result, unsigned int maxlen)
1da177e4 362{
26cb62a2 363 struct stripe_c *sc = ti->private;
1da177e4
LT
364 unsigned int sz = 0;
365 unsigned int i;
366
367 switch (type) {
368 case STATUSTYPE_INFO:
4f7f5c67 369 DMEMIT("%d ", sc->stripes);
2d0f25cb 370 for (i = 0; i < sc->stripes; i++)
4f7f5c67 371 DMEMIT("%s ", sc->stripe[i].dev->name);
2d0f25cb 372
706dd22f 373 DMEMIT("1 ");
2d0f25cb
HM
374 for (i = 0; i < sc->stripes; i++)
375 DMEMIT("%c", atomic_read(&(sc->stripe[i].error_count)) ? 'D' : 'A');
1da177e4
LT
376 break;
377
378 case STATUSTYPE_TABLE:
4ee218cd 379 DMEMIT("%d %llu", sc->stripes,
eb850de6 380 (unsigned long long)sc->chunk_size);
1da177e4 381 for (i = 0; i < sc->stripes; i++)
4ee218cd
AM
382 DMEMIT(" %s %llu", sc->stripe[i].dev->name,
383 (unsigned long long)sc->stripe[i].physical_start);
1da177e4 384 break;
8ec45662
TS
385
386 case STATUSTYPE_IMA:
387 DMEMIT_TARGET_NAME_VERSION(ti->type);
388 DMEMIT(",stripes=%d,chunk_size=%llu", sc->stripes,
389 (unsigned long long)sc->chunk_size);
390
391 for (i = 0; i < sc->stripes; i++) {
392 DMEMIT(",stripe_%d_device_name=%s", i, sc->stripe[i].dev->name);
393 DMEMIT(",stripe_%d_physical_start=%llu", i,
394 (unsigned long long)sc->stripe[i].physical_start);
395 DMEMIT(",stripe_%d_status=%c", i,
396 atomic_read(&(sc->stripe[i].error_count)) ? 'D' : 'A');
397 }
398 DMEMIT(";");
399 break;
1da177e4 400 }
1da177e4
LT
401}
402
4e4cbee9
CH
403static int stripe_end_io(struct dm_target *ti, struct bio *bio,
404 blk_status_t *error)
a25eb944 405{
86a3238c 406 unsigned int i;
a25eb944
BW
407 char major_minor[16];
408 struct stripe_c *sc = ti->private;
409
1be56909
CH
410 if (!*error)
411 return DM_ENDIO_DONE; /* I/O complete */
a25eb944 412
9966afaf 413 if (bio->bi_opf & REQ_RAHEAD)
1be56909 414 return DM_ENDIO_DONE;
a25eb944 415
4e4cbee9 416 if (*error == BLK_STS_NOTSUPP)
1be56909 417 return DM_ENDIO_DONE;
a25eb944
BW
418
419 memset(major_minor, 0, sizeof(major_minor));
74d46992 420 sprintf(major_minor, "%d:%d", MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)));
a25eb944
BW
421
422 /*
423 * Test to see which stripe drive triggered the event
424 * and increment error count for all stripes on that device.
425 * If the error count for a given device exceeds the threshold
426 * value we will no longer trigger any further events.
427 */
428 for (i = 0; i < sc->stripes; i++)
429 if (!strcmp(sc->stripe[i].dev->name, major_minor)) {
430 atomic_inc(&(sc->stripe[i].error_count));
431 if (atomic_read(&(sc->stripe[i].error_count)) <
432 DM_IO_ERROR_THRESHOLD)
a7e8f7fb 433 queue_work(dm_stripe_wq, &sc->trigger_event);
a25eb944
BW
434 }
435
1be56909 436 return DM_ENDIO_DONE;
a25eb944
BW
437}
438
af4874e0
MS
439static int stripe_iterate_devices(struct dm_target *ti,
440 iterate_devices_callout_fn fn, void *data)
441{
442 struct stripe_c *sc = ti->private;
443 int ret = 0;
86a3238c 444 unsigned int i = 0;
af4874e0 445
5dea271b 446 do {
af4874e0 447 ret = fn(ti, sc->stripe[i].dev,
5dea271b
MS
448 sc->stripe[i].physical_start,
449 sc->stripe_width, data);
450 } while (!ret && ++i < sc->stripes);
af4874e0
MS
451
452 return ret;
453}
454
40bea431
MS
455static void stripe_io_hints(struct dm_target *ti,
456 struct queue_limits *limits)
457{
458 struct stripe_c *sc = ti->private;
86a3238c 459 unsigned int chunk_size = sc->chunk_size << SECTOR_SHIFT;
40bea431
MS
460
461 blk_limits_io_min(limits, chunk_size);
3c5820c7 462 blk_limits_io_opt(limits, chunk_size * sc->stripes);
40bea431
MS
463}
464
1da177e4
LT
465static struct target_type stripe_target = {
466 .name = "striped",
beec25b4 467 .version = {1, 6, 0},
410fe220 468 .features = DM_TARGET_PASSES_INTEGRITY | DM_TARGET_NOWAIT,
1da177e4
LT
469 .module = THIS_MODULE,
470 .ctr = stripe_ctr,
471 .dtr = stripe_dtr,
472 .map = stripe_map,
a25eb944 473 .end_io = stripe_end_io,
1da177e4 474 .status = stripe_status,
af4874e0 475 .iterate_devices = stripe_iterate_devices,
40bea431 476 .io_hints = stripe_io_hints,
817bf402 477 .direct_access = stripe_dax_direct_access,
cdf6cdcd 478 .dax_zero_page_range = stripe_dax_zero_page_range,
047218ec 479 .dax_recovery_write = stripe_dax_recovery_write,
1da177e4
LT
480};
481
482int __init dm_stripe_init(void)
483{
484 int r;
485
a7e8f7fb
TH
486 dm_stripe_wq = alloc_workqueue("dm_stripe_wq", 0, 0);
487 if (!dm_stripe_wq)
488 return -ENOMEM;
1da177e4 489 r = dm_register_target(&stripe_target);
a7e8f7fb
TH
490 if (r < 0) {
491 destroy_workqueue(dm_stripe_wq);
72d94861 492 DMWARN("target registration failed");
a7e8f7fb 493 }
1da177e4
LT
494
495 return r;
496}
497
498void dm_stripe_exit(void)
499{
10d3bd09 500 dm_unregister_target(&stripe_target);
a7e8f7fb 501 destroy_workqueue(dm_stripe_wq);
1da177e4 502}