nvme-tcp: have queue prod/cons send list become a llist
[linux-2.6-block.git] / drivers / md / raid0.c
CommitLineData
af1a8899 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 raid0.c : Multiple Devices driver for Linux
f72ffdd6 4 Copyright (C) 1994-96 Marc ZYNGIER
1da177e4
LT
5 <zyngier@ufr-info-p7.ibp.fr> or
6 <maz@gloups.fdn.fr>
f72ffdd6 7 Copyright (C) 1999, 2000 Ingo Molnar, Red Hat
1da177e4
LT
8
9 RAID-0 management functions.
10
1da177e4
LT
11*/
12
bff61975 13#include <linux/blkdev.h>
bff61975 14#include <linux/seq_file.h>
056075c7 15#include <linux/module.h>
5a0e3ad6 16#include <linux/slab.h>
109e3765 17#include <trace/events/block.h>
43b2e5d8 18#include "md.h"
ef740c37 19#include "raid0.h"
9af204cf 20#include "raid5.h"
1da177e4 21
c84a1372
N
22static int default_layout = 0;
23module_param(default_layout, int, 0644);
24
394ed8e4
SL
25#define UNSUPPORTED_MDDEV_FLAGS \
26 ((1L << MD_HAS_JOURNAL) | \
27 (1L << MD_JOURNAL_CLEAN) | \
ea0213e0 28 (1L << MD_FAILFAST_SUPPORTED) |\
ddc08823
PB
29 (1L << MD_HAS_PPL) | \
30 (1L << MD_HAS_MULTIPLE_PPLS))
394ed8e4 31
5c675f83 32static int raid0_congested(struct mddev *mddev, int bits)
26be34dc 33{
e373ab10 34 struct r0conf *conf = mddev->private;
3cb03002 35 struct md_rdev **devlist = conf->devlist;
84707f38 36 int raid_disks = conf->strip_zone[0].nb_dev;
26be34dc
N
37 int i, ret = 0;
38
84707f38 39 for (i = 0; i < raid_disks && !ret ; i++) {
165125e1 40 struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
26be34dc 41
dc3b17cc 42 ret |= bdi_congested(q->backing_dev_info, bits);
26be34dc
N
43 }
44 return ret;
45}
46
46994191 47/*
48 * inform the user of the raid configuration
49*/
fd01b88c 50static void dump_zones(struct mddev *mddev)
46994191 51{
50de8df4 52 int j, k;
46994191 53 sector_t zone_size = 0;
54 sector_t zone_start = 0;
55 char b[BDEVNAME_SIZE];
e373ab10 56 struct r0conf *conf = mddev->private;
84707f38 57 int raid_disks = conf->strip_zone[0].nb_dev;
76603884
N
58 pr_debug("md: RAID0 configuration for %s - %d zone%s\n",
59 mdname(mddev),
60 conf->nr_strip_zones, conf->nr_strip_zones==1?"":"s");
46994191 61 for (j = 0; j < conf->nr_strip_zones; j++) {
76603884
N
62 char line[200];
63 int len = 0;
64
46994191 65 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
76603884
N
66 len += snprintf(line+len, 200-len, "%s%s", k?"/":"",
67 bdevname(conf->devlist[j*raid_disks
68 + k]->bdev, b));
69 pr_debug("md: zone%d=[%s]\n", j, line);
46994191 70
71 zone_size = conf->strip_zone[j].zone_end - zone_start;
76603884 72 pr_debug(" zone-offset=%10lluKB, device-offset=%10lluKB, size=%10lluKB\n",
46994191 73 (unsigned long long)zone_start>>1,
74 (unsigned long long)conf->strip_zone[j].dev_start>>1,
75 (unsigned long long)zone_size>>1);
76 zone_start = conf->strip_zone[j].zone_end;
77 }
46994191 78}
79
e373ab10 80static int create_strip_zones(struct mddev *mddev, struct r0conf **private_conf)
1da177e4 81{
a9f326eb 82 int i, c, err;
49f357a2 83 sector_t curr_zone_end, sectors;
3cb03002 84 struct md_rdev *smallest, *rdev1, *rdev2, *rdev, **dev;
1da177e4
LT
85 struct strip_zone *zone;
86 int cnt;
87 char b[BDEVNAME_SIZE];
50de8df4 88 char b2[BDEVNAME_SIZE];
e373ab10 89 struct r0conf *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
ad6bf88a 90 unsigned blksize = 512;
ed7b0038 91
7dedd15d 92 *private_conf = ERR_PTR(-ENOMEM);
ed7b0038
AN
93 if (!conf)
94 return -ENOMEM;
dafb20fa 95 rdev_for_each(rdev1, mddev) {
50de8df4
N
96 pr_debug("md/raid0:%s: looking at %s\n",
97 mdname(mddev),
98 bdevname(rdev1->bdev, b));
1da177e4 99 c = 0;
13f2682b
N
100
101 /* round size to chunk_size */
102 sectors = rdev1->sectors;
103 sector_div(sectors, mddev->chunk_sectors);
104 rdev1->sectors = sectors * mddev->chunk_sectors;
105
199dc6ed
N
106 blksize = max(blksize, queue_logical_block_size(
107 rdev1->bdev->bd_disk->queue));
108
dafb20fa 109 rdev_for_each(rdev2, mddev) {
50de8df4
N
110 pr_debug("md/raid0:%s: comparing %s(%llu)"
111 " with %s(%llu)\n",
112 mdname(mddev),
113 bdevname(rdev1->bdev,b),
114 (unsigned long long)rdev1->sectors,
115 bdevname(rdev2->bdev,b2),
116 (unsigned long long)rdev2->sectors);
1da177e4 117 if (rdev2 == rdev1) {
50de8df4
N
118 pr_debug("md/raid0:%s: END\n",
119 mdname(mddev));
1da177e4
LT
120 break;
121 }
dd8ac336 122 if (rdev2->sectors == rdev1->sectors) {
1da177e4
LT
123 /*
124 * Not unique, don't count it as a new
125 * group
126 */
50de8df4
N
127 pr_debug("md/raid0:%s: EQUAL\n",
128 mdname(mddev));
1da177e4
LT
129 c = 1;
130 break;
131 }
50de8df4
N
132 pr_debug("md/raid0:%s: NOT EQUAL\n",
133 mdname(mddev));
1da177e4
LT
134 }
135 if (!c) {
50de8df4
N
136 pr_debug("md/raid0:%s: ==> UNIQUE\n",
137 mdname(mddev));
1da177e4 138 conf->nr_strip_zones++;
50de8df4
N
139 pr_debug("md/raid0:%s: %d zones\n",
140 mdname(mddev), conf->nr_strip_zones);
1da177e4
LT
141 }
142 }
50de8df4
N
143 pr_debug("md/raid0:%s: FINAL %d zones\n",
144 mdname(mddev), conf->nr_strip_zones);
c84a1372
N
145
146 if (conf->nr_strip_zones == 1) {
147 conf->layout = RAID0_ORIG_LAYOUT;
33f2c35a
N
148 } else if (mddev->layout == RAID0_ORIG_LAYOUT ||
149 mddev->layout == RAID0_ALT_MULTIZONE_LAYOUT) {
150 conf->layout = mddev->layout;
c84a1372
N
151 } else if (default_layout == RAID0_ORIG_LAYOUT ||
152 default_layout == RAID0_ALT_MULTIZONE_LAYOUT) {
153 conf->layout = default_layout;
154 } else {
155 pr_err("md/raid0:%s: cannot assemble multi-zone RAID0 with default_layout setting\n",
156 mdname(mddev));
3874d73e 157 pr_err("md/raid0: please set raid0.default_layout to 1 or 2\n");
c84a1372
N
158 err = -ENOTSUPP;
159 goto abort;
160 }
199dc6ed
N
161 /*
162 * now since we have the hard sector sizes, we can make sure
163 * chunk size is a multiple of that sector size
164 */
165 if ((mddev->chunk_sectors << 9) % blksize) {
76603884
N
166 pr_warn("md/raid0:%s: chunk_size of %d not multiple of block size %d\n",
167 mdname(mddev),
168 mddev->chunk_sectors << 9, blksize);
199dc6ed
N
169 err = -EINVAL;
170 goto abort;
171 }
172
ed7b0038 173 err = -ENOMEM;
6396bb22
KC
174 conf->strip_zone = kcalloc(conf->nr_strip_zones,
175 sizeof(struct strip_zone),
176 GFP_KERNEL);
1da177e4 177 if (!conf->strip_zone)
ed7b0038 178 goto abort;
6396bb22
KC
179 conf->devlist = kzalloc(array3_size(sizeof(struct md_rdev *),
180 conf->nr_strip_zones,
181 mddev->raid_disks),
1da177e4
LT
182 GFP_KERNEL);
183 if (!conf->devlist)
ed7b0038 184 goto abort;
1da177e4 185
1da177e4
LT
186 /* The first zone must contain all devices, so here we check that
187 * there is a proper alignment of slots to devices and find them all
188 */
189 zone = &conf->strip_zone[0];
190 cnt = 0;
191 smallest = NULL;
b414579f 192 dev = conf->devlist;
ed7b0038 193 err = -EINVAL;
dafb20fa 194 rdev_for_each(rdev1, mddev) {
1da177e4
LT
195 int j = rdev1->raid_disk;
196
e93f68a1 197 if (mddev->level == 10) {
9af204cf
TM
198 /* taking over a raid10-n2 array */
199 j /= 2;
e93f68a1
N
200 rdev1->new_raid_disk = j;
201 }
9af204cf 202
fc3a08b8
KW
203 if (mddev->level == 1) {
204 /* taiking over a raid1 array-
205 * we have only one active disk
206 */
207 j = 0;
208 rdev1->new_raid_disk = j;
209 }
210
f96c9f30 211 if (j < 0) {
76603884
N
212 pr_warn("md/raid0:%s: remove inactive devices before converting to RAID0\n",
213 mdname(mddev));
f96c9f30
N
214 goto abort;
215 }
216 if (j >= mddev->raid_disks) {
76603884
N
217 pr_warn("md/raid0:%s: bad disk number %d - aborting!\n",
218 mdname(mddev), j);
1da177e4
LT
219 goto abort;
220 }
b414579f 221 if (dev[j]) {
76603884
N
222 pr_warn("md/raid0:%s: multiple devices for %d - aborting!\n",
223 mdname(mddev), j);
1da177e4
LT
224 goto abort;
225 }
b414579f 226 dev[j] = rdev1;
1da177e4 227
dd8ac336 228 if (!smallest || (rdev1->sectors < smallest->sectors))
1da177e4
LT
229 smallest = rdev1;
230 cnt++;
231 }
232 if (cnt != mddev->raid_disks) {
76603884
N
233 pr_warn("md/raid0:%s: too few disks (%d of %d) - aborting!\n",
234 mdname(mddev), cnt, mddev->raid_disks);
1da177e4
LT
235 goto abort;
236 }
237 zone->nb_dev = cnt;
49f357a2 238 zone->zone_end = smallest->sectors * cnt;
1da177e4 239
49f357a2 240 curr_zone_end = zone->zone_end;
1da177e4
LT
241
242 /* now do the other zones */
243 for (i = 1; i < conf->nr_strip_zones; i++)
244 {
a9f326eb
N
245 int j;
246
1da177e4 247 zone = conf->strip_zone + i;
b414579f 248 dev = conf->devlist + i * mddev->raid_disks;
1da177e4 249
50de8df4 250 pr_debug("md/raid0:%s: zone %d\n", mdname(mddev), i);
d27a43ab 251 zone->dev_start = smallest->sectors;
1da177e4
LT
252 smallest = NULL;
253 c = 0;
254
255 for (j=0; j<cnt; j++) {
b414579f 256 rdev = conf->devlist[j];
d27a43ab 257 if (rdev->sectors <= zone->dev_start) {
50de8df4
N
258 pr_debug("md/raid0:%s: checking %s ... nope\n",
259 mdname(mddev),
260 bdevname(rdev->bdev, b));
dd8ac336
AN
261 continue;
262 }
50de8df4
N
263 pr_debug("md/raid0:%s: checking %s ..."
264 " contained as device %d\n",
265 mdname(mddev),
266 bdevname(rdev->bdev, b), c);
b414579f 267 dev[c] = rdev;
dd8ac336
AN
268 c++;
269 if (!smallest || rdev->sectors < smallest->sectors) {
270 smallest = rdev;
50de8df4
N
271 pr_debug("md/raid0:%s: (%llu) is smallest!.\n",
272 mdname(mddev),
273 (unsigned long long)rdev->sectors);
dd8ac336 274 }
1da177e4
LT
275 }
276
277 zone->nb_dev = c;
49f357a2 278 sectors = (smallest->sectors - zone->dev_start) * c;
50de8df4
N
279 pr_debug("md/raid0:%s: zone->nb_dev: %d, sectors: %llu\n",
280 mdname(mddev),
281 zone->nb_dev, (unsigned long long)sectors);
1da177e4 282
49f357a2 283 curr_zone_end += sectors;
d27a43ab 284 zone->zone_end = curr_zone_end;
1da177e4 285
50de8df4
N
286 pr_debug("md/raid0:%s: current zone start: %llu\n",
287 mdname(mddev),
288 (unsigned long long)smallest->sectors);
1da177e4 289 }
1da177e4 290
50de8df4 291 pr_debug("md/raid0:%s: done.\n", mdname(mddev));
9af204cf
TM
292 *private_conf = conf;
293
1da177e4 294 return 0;
5568a603 295abort:
ed7b0038
AN
296 kfree(conf->strip_zone);
297 kfree(conf->devlist);
298 kfree(conf);
58ebb34c 299 *private_conf = ERR_PTR(err);
ed7b0038 300 return err;
1da177e4
LT
301}
302
ba13da47
N
303/* Find the zone which holds a particular offset
304 * Update *sectorp to be an offset in that zone
305 */
306static struct strip_zone *find_zone(struct r0conf *conf,
307 sector_t *sectorp)
308{
309 int i;
310 struct strip_zone *z = conf->strip_zone;
311 sector_t sector = *sectorp;
312
313 for (i = 0; i < conf->nr_strip_zones; i++)
314 if (sector < z[i].zone_end) {
315 if (i)
316 *sectorp = sector - z[i-1].zone_end;
317 return z + i;
318 }
319 BUG();
320}
321
322/*
323 * remaps the bio to the target device. we separate two flows.
47d68979 324 * power 2 flow and a general flow for the sake of performance
ba13da47
N
325*/
326static struct md_rdev *map_sector(struct mddev *mddev, struct strip_zone *zone,
327 sector_t sector, sector_t *sector_offset)
328{
329 unsigned int sect_in_chunk;
330 sector_t chunk;
331 struct r0conf *conf = mddev->private;
332 int raid_disks = conf->strip_zone[0].nb_dev;
333 unsigned int chunk_sects = mddev->chunk_sectors;
334
335 if (is_power_of_2(chunk_sects)) {
336 int chunksect_bits = ffz(~chunk_sects);
337 /* find the sector offset inside the chunk */
338 sect_in_chunk = sector & (chunk_sects - 1);
339 sector >>= chunksect_bits;
340 /* chunk in zone */
341 chunk = *sector_offset;
342 /* quotient is the chunk in real device*/
343 sector_div(chunk, zone->nb_dev << chunksect_bits);
344 } else{
345 sect_in_chunk = sector_div(sector, chunk_sects);
346 chunk = *sector_offset;
347 sector_div(chunk, chunk_sects * zone->nb_dev);
348 }
349 /*
350 * position the bio over the real device
351 * real sector = chunk in device + starting of zone
352 * + the position in the chunk
353 */
354 *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
355 return conf->devlist[(zone - conf->strip_zone)*raid_disks
356 + sector_div(sector, zone->nb_dev)];
357}
358
fd01b88c 359static sector_t raid0_size(struct mddev *mddev, sector_t sectors, int raid_disks)
80c3a6ce
DW
360{
361 sector_t array_sectors = 0;
3cb03002 362 struct md_rdev *rdev;
80c3a6ce
DW
363
364 WARN_ONCE(sectors || raid_disks,
365 "%s does not support generic reshape\n", __func__);
366
dafb20fa 367 rdev_for_each(rdev, mddev)
a6468539
N
368 array_sectors += (rdev->sectors &
369 ~(sector_t)(mddev->chunk_sectors-1));
80c3a6ce
DW
370
371 return array_sectors;
372}
373
afa0f557 374static void raid0_free(struct mddev *mddev, void *priv);
0366ef84 375
fd01b88c 376static int raid0_run(struct mddev *mddev)
1da177e4 377{
e373ab10 378 struct r0conf *conf;
5568a603 379 int ret;
1da177e4 380
9d8f0363 381 if (mddev->chunk_sectors == 0) {
76603884 382 pr_warn("md/raid0:%s: chunk size must be set.\n", mdname(mddev));
2604b703
N
383 return -EINVAL;
384 }
0894cc30
AN
385 if (md_check_no_bitmap(mddev))
386 return -EINVAL;
753f2856 387
9af204cf
TM
388 /* if private is not null, we are here after takeover */
389 if (mddev->private == NULL) {
390 ret = create_strip_zones(mddev, &conf);
391 if (ret < 0)
392 return ret;
393 mddev->private = conf;
394 }
395 conf = mddev->private;
199dc6ed
N
396 if (mddev->queue) {
397 struct md_rdev *rdev;
398 bool discard_supported = false;
399
199dc6ed
N
400 blk_queue_max_hw_sectors(mddev->queue, mddev->chunk_sectors);
401 blk_queue_max_write_same_sectors(mddev->queue, mddev->chunk_sectors);
3deff1a7 402 blk_queue_max_write_zeroes_sectors(mddev->queue, mddev->chunk_sectors);
29efc390 403 blk_queue_max_discard_sectors(mddev->queue, UINT_MAX);
199dc6ed
N
404
405 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
406 blk_queue_io_opt(mddev->queue,
407 (mddev->chunk_sectors << 9) * mddev->raid_disks);
408
66eefe5d
N
409 rdev_for_each(rdev, mddev) {
410 disk_stack_limits(mddev->gendisk, rdev->bdev,
411 rdev->data_offset << 9);
412 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
413 discard_supported = true;
414 }
199dc6ed 415 if (!discard_supported)
8b904b5b 416 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, mddev->queue);
199dc6ed 417 else
8b904b5b 418 blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
199dc6ed 419 }
1da177e4
LT
420
421 /* calculate array device size */
1f403624 422 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
1da177e4 423
76603884
N
424 pr_debug("md/raid0:%s: md_size is %llu sectors.\n",
425 mdname(mddev),
426 (unsigned long long)mddev->array_sectors);
753f2856
HM
427
428 if (mddev->queue) {
429 /* calculate the max read-ahead size.
430 * For read-ahead of large files to be effective, we need to
431 * readahead at least twice a whole stripe. i.e. number of devices
432 * multiplied by chunk size times 2.
433 * If an individual device has an ra_pages greater than the
434 * chunk size, then we will not drive that device as hard as it
435 * wants. We consider this a configuration error: a larger
436 * chunksize should be used in that case.
437 */
9d8f0363
AN
438 int stripe = mddev->raid_disks *
439 (mddev->chunk_sectors << 9) / PAGE_SIZE;
dc3b17cc
JK
440 if (mddev->queue->backing_dev_info->ra_pages < 2* stripe)
441 mddev->queue->backing_dev_info->ra_pages = 2* stripe;
1da177e4
LT
442 }
443
46994191 444 dump_zones(mddev);
0366ef84 445
446 ret = md_integrity_register(mddev);
0366ef84 447
448 return ret;
1da177e4
LT
449}
450
afa0f557 451static void raid0_free(struct mddev *mddev, void *priv)
1da177e4 452{
afa0f557 453 struct r0conf *conf = priv;
1da177e4 454
990a8baf 455 kfree(conf->strip_zone);
fb5ab4b5 456 kfree(conf->devlist);
990a8baf 457 kfree(conf);
1da177e4
LT
458}
459
fbb704ef 460/*
461 * Is io distribute over 1 or more chunks ?
462*/
fd01b88c 463static inline int is_io_in_chunk_boundary(struct mddev *mddev,
fbb704ef 464 unsigned int chunk_sects, struct bio *bio)
465{
d6e412ea 466 if (likely(is_power_of_2(chunk_sects))) {
4f024f37
KO
467 return chunk_sects >=
468 ((bio->bi_iter.bi_sector & (chunk_sects-1))
aa8b57aa 469 + bio_sectors(bio));
fbb704ef 470 } else{
4f024f37 471 sector_t sector = bio->bi_iter.bi_sector;
fbb704ef 472 return chunk_sects >= (sector_div(sector, chunk_sects)
aa8b57aa 473 + bio_sectors(bio));
fbb704ef 474 }
475}
476
29efc390
SL
477static void raid0_handle_discard(struct mddev *mddev, struct bio *bio)
478{
479 struct r0conf *conf = mddev->private;
480 struct strip_zone *zone;
481 sector_t start = bio->bi_iter.bi_sector;
482 sector_t end;
483 unsigned int stripe_size;
484 sector_t first_stripe_index, last_stripe_index;
485 sector_t start_disk_offset;
486 unsigned int start_disk_index;
487 sector_t end_disk_offset;
488 unsigned int end_disk_index;
489 unsigned int disk;
490
491 zone = find_zone(conf, &start);
492
493 if (bio_end_sector(bio) > zone->zone_end) {
494 struct bio *split = bio_split(bio,
495 zone->zone_end - bio->bi_iter.bi_sector, GFP_NOIO,
afeee514 496 &mddev->bio_set);
29efc390 497 bio_chain(split, bio);
ed00aabd 498 submit_bio_noacct(bio);
29efc390
SL
499 bio = split;
500 end = zone->zone_end;
501 } else
502 end = bio_end_sector(bio);
503
504 if (zone != conf->strip_zone)
505 end = end - zone[-1].zone_end;
506
507 /* Now start and end is the offset in zone */
508 stripe_size = zone->nb_dev * mddev->chunk_sectors;
509
510 first_stripe_index = start;
511 sector_div(first_stripe_index, stripe_size);
512 last_stripe_index = end;
513 sector_div(last_stripe_index, stripe_size);
514
515 start_disk_index = (int)(start - first_stripe_index * stripe_size) /
516 mddev->chunk_sectors;
517 start_disk_offset = ((int)(start - first_stripe_index * stripe_size) %
518 mddev->chunk_sectors) +
519 first_stripe_index * mddev->chunk_sectors;
520 end_disk_index = (int)(end - last_stripe_index * stripe_size) /
521 mddev->chunk_sectors;
522 end_disk_offset = ((int)(end - last_stripe_index * stripe_size) %
523 mddev->chunk_sectors) +
524 last_stripe_index * mddev->chunk_sectors;
525
526 for (disk = 0; disk < zone->nb_dev; disk++) {
527 sector_t dev_start, dev_end;
528 struct bio *discard_bio = NULL;
529 struct md_rdev *rdev;
530
531 if (disk < start_disk_index)
532 dev_start = (first_stripe_index + 1) *
533 mddev->chunk_sectors;
534 else if (disk > start_disk_index)
535 dev_start = first_stripe_index * mddev->chunk_sectors;
536 else
537 dev_start = start_disk_offset;
538
539 if (disk < end_disk_index)
540 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
541 else if (disk > end_disk_index)
542 dev_end = last_stripe_index * mddev->chunk_sectors;
543 else
544 dev_end = end_disk_offset;
545
546 if (dev_end <= dev_start)
547 continue;
548
549 rdev = conf->devlist[(zone - conf->strip_zone) *
550 conf->strip_zone[0].nb_dev + disk];
551 if (__blkdev_issue_discard(rdev->bdev,
552 dev_start + zone->dev_start + rdev->data_offset,
553 dev_end - dev_start, GFP_NOIO, 0, &discard_bio) ||
554 !discard_bio)
555 continue;
556 bio_chain(discard_bio, bio);
db6638d7 557 bio_clone_blkg_association(discard_bio, bio);
29efc390
SL
558 if (mddev->gendisk)
559 trace_block_bio_remap(bdev_get_queue(rdev->bdev),
560 discard_bio, disk_devt(mddev->gendisk),
561 bio->bi_iter.bi_sector);
ed00aabd 562 submit_bio_noacct(discard_bio);
29efc390
SL
563 }
564 bio_endio(bio);
565}
566
cc27b0c7 567static bool raid0_make_request(struct mddev *mddev, struct bio *bio)
fbb704ef 568{
c84a1372 569 struct r0conf *conf = mddev->private;
1da177e4 570 struct strip_zone *zone;
3cb03002 571 struct md_rdev *tmp_dev;
f00d7c85
N
572 sector_t bio_sector;
573 sector_t sector;
c84a1372 574 sector_t orig_sector;
f00d7c85
N
575 unsigned chunk_sects;
576 unsigned sectors;
1da177e4 577
775d7831
DJ
578 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
579 && md_flush_request(mddev, bio))
cc27b0c7 580 return true;
e5dcdd80 581
29efc390
SL
582 if (unlikely((bio_op(bio) == REQ_OP_DISCARD))) {
583 raid0_handle_discard(mddev, bio);
cc27b0c7 584 return true;
29efc390
SL
585 }
586
f00d7c85
N
587 bio_sector = bio->bi_iter.bi_sector;
588 sector = bio_sector;
589 chunk_sects = mddev->chunk_sectors;
20d0189b 590
f00d7c85
N
591 sectors = chunk_sects -
592 (likely(is_power_of_2(chunk_sects))
593 ? (sector & (chunk_sects-1))
594 : sector_div(sector, chunk_sects));
20d0189b 595
f00d7c85
N
596 /* Restore due to sector_div */
597 sector = bio_sector;
a8115776 598
f00d7c85 599 if (sectors < bio_sectors(bio)) {
afeee514
KO
600 struct bio *split = bio_split(bio, sectors, GFP_NOIO,
601 &mddev->bio_set);
f00d7c85 602 bio_chain(split, bio);
ed00aabd 603 submit_bio_noacct(bio);
f00d7c85
N
604 bio = split;
605 }
1da177e4 606
c84a1372 607 orig_sector = sector;
f00d7c85 608 zone = find_zone(mddev->private, &sector);
c84a1372
N
609 switch (conf->layout) {
610 case RAID0_ORIG_LAYOUT:
611 tmp_dev = map_sector(mddev, zone, orig_sector, &sector);
612 break;
613 case RAID0_ALT_MULTIZONE_LAYOUT:
614 tmp_dev = map_sector(mddev, zone, sector, &sector);
615 break;
616 default:
e3fc3f3d 617 WARN(1, "md/raid0:%s: Invalid layout\n", mdname(mddev));
c84a1372
N
618 bio_io_error(bio);
619 return true;
620 }
62f7b198
GP
621
622 if (unlikely(is_mddev_broken(tmp_dev, "raid0"))) {
623 bio_io_error(bio);
624 return true;
625 }
626
74d46992 627 bio_set_dev(bio, tmp_dev->bdev);
f00d7c85
N
628 bio->bi_iter.bi_sector = sector + zone->dev_start +
629 tmp_dev->data_offset;
630
29efc390 631 if (mddev->gendisk)
74d46992
CH
632 trace_block_bio_remap(bio->bi_disk->queue, bio,
633 disk_devt(mddev->gendisk), bio_sector);
29efc390
SL
634 mddev_check_writesame(mddev, bio);
635 mddev_check_write_zeroes(mddev, bio);
ed00aabd 636 submit_bio_noacct(bio);
cc27b0c7 637 return true;
1da177e4 638}
8299d7f7 639
fd01b88c 640static void raid0_status(struct seq_file *seq, struct mddev *mddev)
1da177e4 641{
9d8f0363 642 seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
1da177e4
LT
643 return;
644}
645
fd01b88c 646static void *raid0_takeover_raid45(struct mddev *mddev)
9af204cf 647{
3cb03002 648 struct md_rdev *rdev;
e373ab10 649 struct r0conf *priv_conf;
9af204cf
TM
650
651 if (mddev->degraded != 1) {
76603884
N
652 pr_warn("md/raid0:%s: raid5 must be degraded! Degraded disks: %d\n",
653 mdname(mddev),
654 mddev->degraded);
9af204cf
TM
655 return ERR_PTR(-EINVAL);
656 }
657
dafb20fa 658 rdev_for_each(rdev, mddev) {
9af204cf
TM
659 /* check slot number for a disk */
660 if (rdev->raid_disk == mddev->raid_disks-1) {
76603884
N
661 pr_warn("md/raid0:%s: raid5 must have missing parity disk!\n",
662 mdname(mddev));
9af204cf
TM
663 return ERR_PTR(-EINVAL);
664 }
eea136d6 665 rdev->sectors = mddev->dev_sectors;
9af204cf
TM
666 }
667
668 /* Set new parameters */
669 mddev->new_level = 0;
001048a3 670 mddev->new_layout = 0;
9af204cf
TM
671 mddev->new_chunk_sectors = mddev->chunk_sectors;
672 mddev->raid_disks--;
673 mddev->delta_disks = -1;
674 /* make sure it will be not marked as dirty */
675 mddev->recovery_cp = MaxSector;
394ed8e4 676 mddev_clear_unsupported_flags(mddev, UNSUPPORTED_MDDEV_FLAGS);
9af204cf
TM
677
678 create_strip_zones(mddev, &priv_conf);
6995f0b2 679
9af204cf
TM
680 return priv_conf;
681}
682
fd01b88c 683static void *raid0_takeover_raid10(struct mddev *mddev)
9af204cf 684{
e373ab10 685 struct r0conf *priv_conf;
9af204cf
TM
686
687 /* Check layout:
688 * - far_copies must be 1
689 * - near_copies must be 2
690 * - disks number must be even
691 * - all mirrors must be already degraded
692 */
693 if (mddev->layout != ((1 << 8) + 2)) {
76603884
N
694 pr_warn("md/raid0:%s:: Raid0 cannot takeover layout: 0x%x\n",
695 mdname(mddev),
696 mddev->layout);
9af204cf
TM
697 return ERR_PTR(-EINVAL);
698 }
699 if (mddev->raid_disks & 1) {
76603884
N
700 pr_warn("md/raid0:%s: Raid0 cannot takeover Raid10 with odd disk number.\n",
701 mdname(mddev));
9af204cf
TM
702 return ERR_PTR(-EINVAL);
703 }
704 if (mddev->degraded != (mddev->raid_disks>>1)) {
76603884
N
705 pr_warn("md/raid0:%s: All mirrors must be already degraded!\n",
706 mdname(mddev));
9af204cf
TM
707 return ERR_PTR(-EINVAL);
708 }
709
710 /* Set new parameters */
711 mddev->new_level = 0;
001048a3 712 mddev->new_layout = 0;
9af204cf
TM
713 mddev->new_chunk_sectors = mddev->chunk_sectors;
714 mddev->delta_disks = - mddev->raid_disks / 2;
715 mddev->raid_disks += mddev->delta_disks;
716 mddev->degraded = 0;
717 /* make sure it will be not marked as dirty */
718 mddev->recovery_cp = MaxSector;
394ed8e4 719 mddev_clear_unsupported_flags(mddev, UNSUPPORTED_MDDEV_FLAGS);
9af204cf
TM
720
721 create_strip_zones(mddev, &priv_conf);
9af204cf
TM
722 return priv_conf;
723}
724
fd01b88c 725static void *raid0_takeover_raid1(struct mddev *mddev)
fc3a08b8 726{
e373ab10 727 struct r0conf *priv_conf;
24b961f8 728 int chunksect;
fc3a08b8
KW
729
730 /* Check layout:
731 * - (N - 1) mirror drives must be already faulty
732 */
733 if ((mddev->raid_disks - 1) != mddev->degraded) {
76603884 734 pr_err("md/raid0:%s: (N - 1) mirrors drives must be already faulty!\n",
fc3a08b8
KW
735 mdname(mddev));
736 return ERR_PTR(-EINVAL);
737 }
738
24b961f8
JS
739 /*
740 * a raid1 doesn't have the notion of chunk size, so
741 * figure out the largest suitable size we can use.
742 */
743 chunksect = 64 * 2; /* 64K by default */
744
745 /* The array must be an exact multiple of chunksize */
746 while (chunksect && (mddev->array_sectors & (chunksect - 1)))
747 chunksect >>= 1;
748
749 if ((chunksect << 9) < PAGE_SIZE)
750 /* array size does not allow a suitable chunk size */
751 return ERR_PTR(-EINVAL);
752
fc3a08b8
KW
753 /* Set new parameters */
754 mddev->new_level = 0;
755 mddev->new_layout = 0;
24b961f8
JS
756 mddev->new_chunk_sectors = chunksect;
757 mddev->chunk_sectors = chunksect;
fc3a08b8 758 mddev->delta_disks = 1 - mddev->raid_disks;
f7bee809 759 mddev->raid_disks = 1;
fc3a08b8
KW
760 /* make sure it will be not marked as dirty */
761 mddev->recovery_cp = MaxSector;
394ed8e4 762 mddev_clear_unsupported_flags(mddev, UNSUPPORTED_MDDEV_FLAGS);
fc3a08b8
KW
763
764 create_strip_zones(mddev, &priv_conf);
765 return priv_conf;
766}
767
fd01b88c 768static void *raid0_takeover(struct mddev *mddev)
9af204cf
TM
769{
770 /* raid0 can take over:
049d6c1e 771 * raid4 - if all data disks are active.
9af204cf
TM
772 * raid5 - providing it is Raid4 layout and one disk is faulty
773 * raid10 - assuming we have all necessary active disks
fc3a08b8 774 * raid1 - with (N -1) mirror drives faulty
9af204cf 775 */
a8461a61
N
776
777 if (mddev->bitmap) {
76603884
N
778 pr_warn("md/raid0: %s: cannot takeover array with bitmap\n",
779 mdname(mddev));
a8461a61
N
780 return ERR_PTR(-EBUSY);
781 }
049d6c1e
MT
782 if (mddev->level == 4)
783 return raid0_takeover_raid45(mddev);
784
9af204cf
TM
785 if (mddev->level == 5) {
786 if (mddev->layout == ALGORITHM_PARITY_N)
049d6c1e 787 return raid0_takeover_raid45(mddev);
9af204cf 788
76603884
N
789 pr_warn("md/raid0:%s: Raid can only takeover Raid5 with layout: %d\n",
790 mdname(mddev), ALGORITHM_PARITY_N);
9af204cf
TM
791 }
792
793 if (mddev->level == 10)
794 return raid0_takeover_raid10(mddev);
795
fc3a08b8
KW
796 if (mddev->level == 1)
797 return raid0_takeover_raid1(mddev);
798
76603884 799 pr_warn("Takeover from raid%i to raid0 not supported\n",
fc3a08b8
KW
800 mddev->level);
801
9af204cf
TM
802 return ERR_PTR(-EINVAL);
803}
804
b03e0ccb 805static void raid0_quiesce(struct mddev *mddev, int quiesce)
9af204cf
TM
806{
807}
808
84fc4b56 809static struct md_personality raid0_personality=
1da177e4
LT
810{
811 .name = "raid0",
2604b703 812 .level = 0,
1da177e4
LT
813 .owner = THIS_MODULE,
814 .make_request = raid0_make_request,
815 .run = raid0_run,
afa0f557 816 .free = raid0_free,
1da177e4 817 .status = raid0_status,
80c3a6ce 818 .size = raid0_size,
9af204cf
TM
819 .takeover = raid0_takeover,
820 .quiesce = raid0_quiesce,
5c675f83 821 .congested = raid0_congested,
1da177e4
LT
822};
823
824static int __init raid0_init (void)
825{
2604b703 826 return register_md_personality (&raid0_personality);
1da177e4
LT
827}
828
829static void raid0_exit (void)
830{
2604b703 831 unregister_md_personality (&raid0_personality);
1da177e4
LT
832}
833
834module_init(raid0_init);
835module_exit(raid0_exit);
836MODULE_LICENSE("GPL");
0efb9e61 837MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
1da177e4 838MODULE_ALIAS("md-personality-2"); /* RAID0 */
d9d166c2 839MODULE_ALIAS("md-raid0");
2604b703 840MODULE_ALIAS("md-level-0");