2 raid0.c : Multiple Devices driver for Linux
3 Copyright (C) 1994-96 Marc ZYNGIER
4 <zyngier@ufr-info-p7.ibp.fr> or
6 Copyright (C) 1999, 2000 Ingo Molnar, Red Hat
8 RAID-0 management functions.
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)
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.
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>
29 static int raid0_congested(struct mddev
*mddev
, int bits
)
31 struct r0conf
*conf
= mddev
->private;
32 struct md_rdev
**devlist
= conf
->devlist
;
33 int raid_disks
= conf
->strip_zone
[0].nb_dev
;
36 for (i
= 0; i
< raid_disks
&& !ret
; i
++) {
37 struct request_queue
*q
= bdev_get_queue(devlist
[i
]->bdev
);
39 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
45 * inform the user of the raid configuration
47 static void dump_zones(struct mddev
*mddev
)
50 sector_t zone_size
= 0;
51 sector_t zone_start
= 0;
52 char b
[BDEVNAME_SIZE
];
53 struct r0conf
*conf
= mddev
->private;
54 int raid_disks
= conf
->strip_zone
[0].nb_dev
;
55 pr_debug("md: RAID0 configuration for %s - %d zone%s\n",
57 conf
->nr_strip_zones
, conf
->nr_strip_zones
==1?"":"s");
58 for (j
= 0; j
< conf
->nr_strip_zones
; j
++) {
62 for (k
= 0; k
< conf
->strip_zone
[j
].nb_dev
; k
++)
63 len
+= snprintf(line
+len
, 200-len
, "%s%s", k
?"/":"",
64 bdevname(conf
->devlist
[j
*raid_disks
66 pr_debug("md: zone%d=[%s]\n", j
, line
);
68 zone_size
= conf
->strip_zone
[j
].zone_end
- zone_start
;
69 pr_debug(" zone-offset=%10lluKB, device-offset=%10lluKB, size=%10lluKB\n",
70 (unsigned long long)zone_start
>>1,
71 (unsigned long long)conf
->strip_zone
[j
].dev_start
>>1,
72 (unsigned long long)zone_size
>>1);
73 zone_start
= conf
->strip_zone
[j
].zone_end
;
77 static int create_strip_zones(struct mddev
*mddev
, struct r0conf
**private_conf
)
80 sector_t curr_zone_end
, sectors
;
81 struct md_rdev
*smallest
, *rdev1
, *rdev2
, *rdev
, **dev
;
82 struct strip_zone
*zone
;
84 char b
[BDEVNAME_SIZE
];
85 char b2
[BDEVNAME_SIZE
];
86 struct r0conf
*conf
= kzalloc(sizeof(*conf
), GFP_KERNEL
);
87 unsigned short blksize
= 512;
89 *private_conf
= ERR_PTR(-ENOMEM
);
92 rdev_for_each(rdev1
, mddev
) {
93 pr_debug("md/raid0:%s: looking at %s\n",
95 bdevname(rdev1
->bdev
, b
));
98 /* round size to chunk_size */
99 sectors
= rdev1
->sectors
;
100 sector_div(sectors
, mddev
->chunk_sectors
);
101 rdev1
->sectors
= sectors
* mddev
->chunk_sectors
;
103 blksize
= max(blksize
, queue_logical_block_size(
104 rdev1
->bdev
->bd_disk
->queue
));
106 rdev_for_each(rdev2
, mddev
) {
107 pr_debug("md/raid0:%s: comparing %s(%llu)"
110 bdevname(rdev1
->bdev
,b
),
111 (unsigned long long)rdev1
->sectors
,
112 bdevname(rdev2
->bdev
,b2
),
113 (unsigned long long)rdev2
->sectors
);
114 if (rdev2
== rdev1
) {
115 pr_debug("md/raid0:%s: END\n",
119 if (rdev2
->sectors
== rdev1
->sectors
) {
121 * Not unique, don't count it as a new
124 pr_debug("md/raid0:%s: EQUAL\n",
129 pr_debug("md/raid0:%s: NOT EQUAL\n",
133 pr_debug("md/raid0:%s: ==> UNIQUE\n",
135 conf
->nr_strip_zones
++;
136 pr_debug("md/raid0:%s: %d zones\n",
137 mdname(mddev
), conf
->nr_strip_zones
);
140 pr_debug("md/raid0:%s: FINAL %d zones\n",
141 mdname(mddev
), conf
->nr_strip_zones
);
143 * now since we have the hard sector sizes, we can make sure
144 * chunk size is a multiple of that sector size
146 if ((mddev
->chunk_sectors
<< 9) % blksize
) {
147 pr_warn("md/raid0:%s: chunk_size of %d not multiple of block size %d\n",
149 mddev
->chunk_sectors
<< 9, blksize
);
155 conf
->strip_zone
= kzalloc(sizeof(struct strip_zone
)*
156 conf
->nr_strip_zones
, GFP_KERNEL
);
157 if (!conf
->strip_zone
)
159 conf
->devlist
= kzalloc(sizeof(struct md_rdev
*)*
160 conf
->nr_strip_zones
*mddev
->raid_disks
,
165 /* The first zone must contain all devices, so here we check that
166 * there is a proper alignment of slots to devices and find them all
168 zone
= &conf
->strip_zone
[0];
173 rdev_for_each(rdev1
, mddev
) {
174 int j
= rdev1
->raid_disk
;
176 if (mddev
->level
== 10) {
177 /* taking over a raid10-n2 array */
179 rdev1
->new_raid_disk
= j
;
182 if (mddev
->level
== 1) {
183 /* taiking over a raid1 array-
184 * we have only one active disk
187 rdev1
->new_raid_disk
= j
;
191 pr_warn("md/raid0:%s: remove inactive devices before converting to RAID0\n",
195 if (j
>= mddev
->raid_disks
) {
196 pr_warn("md/raid0:%s: bad disk number %d - aborting!\n",
201 pr_warn("md/raid0:%s: multiple devices for %d - aborting!\n",
207 if (!smallest
|| (rdev1
->sectors
< smallest
->sectors
))
211 if (cnt
!= mddev
->raid_disks
) {
212 pr_warn("md/raid0:%s: too few disks (%d of %d) - aborting!\n",
213 mdname(mddev
), cnt
, mddev
->raid_disks
);
217 zone
->zone_end
= smallest
->sectors
* cnt
;
219 curr_zone_end
= zone
->zone_end
;
221 /* now do the other zones */
222 for (i
= 1; i
< conf
->nr_strip_zones
; i
++)
226 zone
= conf
->strip_zone
+ i
;
227 dev
= conf
->devlist
+ i
* mddev
->raid_disks
;
229 pr_debug("md/raid0:%s: zone %d\n", mdname(mddev
), i
);
230 zone
->dev_start
= smallest
->sectors
;
234 for (j
=0; j
<cnt
; j
++) {
235 rdev
= conf
->devlist
[j
];
236 if (rdev
->sectors
<= zone
->dev_start
) {
237 pr_debug("md/raid0:%s: checking %s ... nope\n",
239 bdevname(rdev
->bdev
, b
));
242 pr_debug("md/raid0:%s: checking %s ..."
243 " contained as device %d\n",
245 bdevname(rdev
->bdev
, b
), c
);
248 if (!smallest
|| rdev
->sectors
< smallest
->sectors
) {
250 pr_debug("md/raid0:%s: (%llu) is smallest!.\n",
252 (unsigned long long)rdev
->sectors
);
257 sectors
= (smallest
->sectors
- zone
->dev_start
) * c
;
258 pr_debug("md/raid0:%s: zone->nb_dev: %d, sectors: %llu\n",
260 zone
->nb_dev
, (unsigned long long)sectors
);
262 curr_zone_end
+= sectors
;
263 zone
->zone_end
= curr_zone_end
;
265 pr_debug("md/raid0:%s: current zone start: %llu\n",
267 (unsigned long long)smallest
->sectors
);
270 pr_debug("md/raid0:%s: done.\n", mdname(mddev
));
271 *private_conf
= conf
;
275 kfree(conf
->strip_zone
);
276 kfree(conf
->devlist
);
278 *private_conf
= ERR_PTR(err
);
282 /* Find the zone which holds a particular offset
283 * Update *sectorp to be an offset in that zone
285 static struct strip_zone
*find_zone(struct r0conf
*conf
,
289 struct strip_zone
*z
= conf
->strip_zone
;
290 sector_t sector
= *sectorp
;
292 for (i
= 0; i
< conf
->nr_strip_zones
; i
++)
293 if (sector
< z
[i
].zone_end
) {
295 *sectorp
= sector
- z
[i
-1].zone_end
;
302 * remaps the bio to the target device. we separate two flows.
303 * power 2 flow and a general flow for the sake of performance
305 static struct md_rdev
*map_sector(struct mddev
*mddev
, struct strip_zone
*zone
,
306 sector_t sector
, sector_t
*sector_offset
)
308 unsigned int sect_in_chunk
;
310 struct r0conf
*conf
= mddev
->private;
311 int raid_disks
= conf
->strip_zone
[0].nb_dev
;
312 unsigned int chunk_sects
= mddev
->chunk_sectors
;
314 if (is_power_of_2(chunk_sects
)) {
315 int chunksect_bits
= ffz(~chunk_sects
);
316 /* find the sector offset inside the chunk */
317 sect_in_chunk
= sector
& (chunk_sects
- 1);
318 sector
>>= chunksect_bits
;
320 chunk
= *sector_offset
;
321 /* quotient is the chunk in real device*/
322 sector_div(chunk
, zone
->nb_dev
<< chunksect_bits
);
324 sect_in_chunk
= sector_div(sector
, chunk_sects
);
325 chunk
= *sector_offset
;
326 sector_div(chunk
, chunk_sects
* zone
->nb_dev
);
329 * position the bio over the real device
330 * real sector = chunk in device + starting of zone
331 * + the position in the chunk
333 *sector_offset
= (chunk
* chunk_sects
) + sect_in_chunk
;
334 return conf
->devlist
[(zone
- conf
->strip_zone
)*raid_disks
335 + sector_div(sector
, zone
->nb_dev
)];
338 static sector_t
raid0_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
340 sector_t array_sectors
= 0;
341 struct md_rdev
*rdev
;
343 WARN_ONCE(sectors
|| raid_disks
,
344 "%s does not support generic reshape\n", __func__
);
346 rdev_for_each(rdev
, mddev
)
347 array_sectors
+= (rdev
->sectors
&
348 ~(sector_t
)(mddev
->chunk_sectors
-1));
350 return array_sectors
;
353 static void raid0_free(struct mddev
*mddev
, void *priv
);
355 static int raid0_run(struct mddev
*mddev
)
360 if (mddev
->chunk_sectors
== 0) {
361 pr_warn("md/raid0:%s: chunk size must be set.\n", mdname(mddev
));
364 if (md_check_no_bitmap(mddev
))
367 /* if private is not null, we are here after takeover */
368 if (mddev
->private == NULL
) {
369 ret
= create_strip_zones(mddev
, &conf
);
372 mddev
->private = conf
;
374 conf
= mddev
->private;
376 struct md_rdev
*rdev
;
377 bool discard_supported
= false;
379 blk_queue_max_hw_sectors(mddev
->queue
, mddev
->chunk_sectors
);
380 blk_queue_max_write_same_sectors(mddev
->queue
, mddev
->chunk_sectors
);
381 blk_queue_max_discard_sectors(mddev
->queue
, mddev
->chunk_sectors
);
383 blk_queue_io_min(mddev
->queue
, mddev
->chunk_sectors
<< 9);
384 blk_queue_io_opt(mddev
->queue
,
385 (mddev
->chunk_sectors
<< 9) * mddev
->raid_disks
);
387 rdev_for_each(rdev
, mddev
) {
388 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
389 rdev
->data_offset
<< 9);
390 if (blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
391 discard_supported
= true;
393 if (!discard_supported
)
394 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
, mddev
->queue
);
396 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, mddev
->queue
);
399 /* calculate array device size */
400 md_set_array_sectors(mddev
, raid0_size(mddev
, 0, 0));
402 pr_debug("md/raid0:%s: md_size is %llu sectors.\n",
404 (unsigned long long)mddev
->array_sectors
);
407 /* calculate the max read-ahead size.
408 * For read-ahead of large files to be effective, we need to
409 * readahead at least twice a whole stripe. i.e. number of devices
410 * multiplied by chunk size times 2.
411 * If an individual device has an ra_pages greater than the
412 * chunk size, then we will not drive that device as hard as it
413 * wants. We consider this a configuration error: a larger
414 * chunksize should be used in that case.
416 int stripe
= mddev
->raid_disks
*
417 (mddev
->chunk_sectors
<< 9) / PAGE_SIZE
;
418 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
419 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
424 ret
= md_integrity_register(mddev
);
429 static void raid0_free(struct mddev
*mddev
, void *priv
)
431 struct r0conf
*conf
= priv
;
433 kfree(conf
->strip_zone
);
434 kfree(conf
->devlist
);
439 * Is io distribute over 1 or more chunks ?
441 static inline int is_io_in_chunk_boundary(struct mddev
*mddev
,
442 unsigned int chunk_sects
, struct bio
*bio
)
444 if (likely(is_power_of_2(chunk_sects
))) {
445 return chunk_sects
>=
446 ((bio
->bi_iter
.bi_sector
& (chunk_sects
-1))
449 sector_t sector
= bio
->bi_iter
.bi_sector
;
450 return chunk_sects
>= (sector_div(sector
, chunk_sects
)
455 static void raid0_make_request(struct mddev
*mddev
, struct bio
*bio
)
457 struct strip_zone
*zone
;
458 struct md_rdev
*tmp_dev
;
461 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
462 md_flush_request(mddev
, bio
);
467 sector_t bio_sector
= bio
->bi_iter
.bi_sector
;
468 sector_t sector
= bio_sector
;
469 unsigned chunk_sects
= mddev
->chunk_sectors
;
471 unsigned sectors
= chunk_sects
-
472 (likely(is_power_of_2(chunk_sects
))
473 ? (sector
& (chunk_sects
-1))
474 : sector_div(sector
, chunk_sects
));
476 /* Restore due to sector_div */
479 if (sectors
< bio_sectors(bio
)) {
480 split
= bio_split(bio
, sectors
, GFP_NOIO
, fs_bio_set
);
481 bio_chain(split
, bio
);
486 zone
= find_zone(mddev
->private, §or
);
487 tmp_dev
= map_sector(mddev
, zone
, sector
, §or
);
488 split
->bi_bdev
= tmp_dev
->bdev
;
489 split
->bi_iter
.bi_sector
= sector
+ zone
->dev_start
+
490 tmp_dev
->data_offset
;
492 if (unlikely((bio_op(split
) == REQ_OP_DISCARD
) &&
493 !blk_queue_discard(bdev_get_queue(split
->bi_bdev
)))) {
498 trace_block_bio_remap(bdev_get_queue(split
->bi_bdev
),
499 split
, disk_devt(mddev
->gendisk
),
501 generic_make_request(split
);
503 } while (split
!= bio
);
506 static void raid0_status(struct seq_file
*seq
, struct mddev
*mddev
)
508 seq_printf(seq
, " %dk chunks", mddev
->chunk_sectors
/ 2);
512 static void *raid0_takeover_raid45(struct mddev
*mddev
)
514 struct md_rdev
*rdev
;
515 struct r0conf
*priv_conf
;
517 if (mddev
->degraded
!= 1) {
518 pr_warn("md/raid0:%s: raid5 must be degraded! Degraded disks: %d\n",
521 return ERR_PTR(-EINVAL
);
524 rdev_for_each(rdev
, mddev
) {
525 /* check slot number for a disk */
526 if (rdev
->raid_disk
== mddev
->raid_disks
-1) {
527 pr_warn("md/raid0:%s: raid5 must have missing parity disk!\n",
529 return ERR_PTR(-EINVAL
);
531 rdev
->sectors
= mddev
->dev_sectors
;
534 /* Set new parameters */
535 mddev
->new_level
= 0;
536 mddev
->new_layout
= 0;
537 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
539 mddev
->delta_disks
= -1;
540 /* make sure it will be not marked as dirty */
541 mddev
->recovery_cp
= MaxSector
;
542 clear_bit(MD_HAS_JOURNAL
, &mddev
->flags
);
543 clear_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
);
545 create_strip_zones(mddev
, &priv_conf
);
550 static void *raid0_takeover_raid10(struct mddev
*mddev
)
552 struct r0conf
*priv_conf
;
555 * - far_copies must be 1
556 * - near_copies must be 2
557 * - disks number must be even
558 * - all mirrors must be already degraded
560 if (mddev
->layout
!= ((1 << 8) + 2)) {
561 pr_warn("md/raid0:%s:: Raid0 cannot takeover layout: 0x%x\n",
564 return ERR_PTR(-EINVAL
);
566 if (mddev
->raid_disks
& 1) {
567 pr_warn("md/raid0:%s: Raid0 cannot takeover Raid10 with odd disk number.\n",
569 return ERR_PTR(-EINVAL
);
571 if (mddev
->degraded
!= (mddev
->raid_disks
>>1)) {
572 pr_warn("md/raid0:%s: All mirrors must be already degraded!\n",
574 return ERR_PTR(-EINVAL
);
577 /* Set new parameters */
578 mddev
->new_level
= 0;
579 mddev
->new_layout
= 0;
580 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
581 mddev
->delta_disks
= - mddev
->raid_disks
/ 2;
582 mddev
->raid_disks
+= mddev
->delta_disks
;
584 /* make sure it will be not marked as dirty */
585 mddev
->recovery_cp
= MaxSector
;
586 clear_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
);
588 create_strip_zones(mddev
, &priv_conf
);
592 static void *raid0_takeover_raid1(struct mddev
*mddev
)
594 struct r0conf
*priv_conf
;
598 * - (N - 1) mirror drives must be already faulty
600 if ((mddev
->raid_disks
- 1) != mddev
->degraded
) {
601 pr_err("md/raid0:%s: (N - 1) mirrors drives must be already faulty!\n",
603 return ERR_PTR(-EINVAL
);
607 * a raid1 doesn't have the notion of chunk size, so
608 * figure out the largest suitable size we can use.
610 chunksect
= 64 * 2; /* 64K by default */
612 /* The array must be an exact multiple of chunksize */
613 while (chunksect
&& (mddev
->array_sectors
& (chunksect
- 1)))
616 if ((chunksect
<< 9) < PAGE_SIZE
)
617 /* array size does not allow a suitable chunk size */
618 return ERR_PTR(-EINVAL
);
620 /* Set new parameters */
621 mddev
->new_level
= 0;
622 mddev
->new_layout
= 0;
623 mddev
->new_chunk_sectors
= chunksect
;
624 mddev
->chunk_sectors
= chunksect
;
625 mddev
->delta_disks
= 1 - mddev
->raid_disks
;
626 mddev
->raid_disks
= 1;
627 /* make sure it will be not marked as dirty */
628 mddev
->recovery_cp
= MaxSector
;
629 clear_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
);
631 create_strip_zones(mddev
, &priv_conf
);
635 static void *raid0_takeover(struct mddev
*mddev
)
637 /* raid0 can take over:
638 * raid4 - if all data disks are active.
639 * raid5 - providing it is Raid4 layout and one disk is faulty
640 * raid10 - assuming we have all necessary active disks
641 * raid1 - with (N -1) mirror drives faulty
645 pr_warn("md/raid0: %s: cannot takeover array with bitmap\n",
647 return ERR_PTR(-EBUSY
);
649 if (mddev
->level
== 4)
650 return raid0_takeover_raid45(mddev
);
652 if (mddev
->level
== 5) {
653 if (mddev
->layout
== ALGORITHM_PARITY_N
)
654 return raid0_takeover_raid45(mddev
);
656 pr_warn("md/raid0:%s: Raid can only takeover Raid5 with layout: %d\n",
657 mdname(mddev
), ALGORITHM_PARITY_N
);
660 if (mddev
->level
== 10)
661 return raid0_takeover_raid10(mddev
);
663 if (mddev
->level
== 1)
664 return raid0_takeover_raid1(mddev
);
666 pr_warn("Takeover from raid%i to raid0 not supported\n",
669 return ERR_PTR(-EINVAL
);
672 static void raid0_quiesce(struct mddev
*mddev
, int state
)
676 static struct md_personality raid0_personality
=
680 .owner
= THIS_MODULE
,
681 .make_request
= raid0_make_request
,
684 .status
= raid0_status
,
686 .takeover
= raid0_takeover
,
687 .quiesce
= raid0_quiesce
,
688 .congested
= raid0_congested
,
691 static int __init
raid0_init (void)
693 return register_md_personality (&raid0_personality
);
696 static void raid0_exit (void)
698 unregister_md_personality (&raid0_personality
);
701 module_init(raid0_init
);
702 module_exit(raid0_exit
);
703 MODULE_LICENSE("GPL");
704 MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
705 MODULE_ALIAS("md-personality-2"); /* RAID0 */
706 MODULE_ALIAS("md-raid0");
707 MODULE_ALIAS("md-level-0");