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