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
9 RAID-0 management functions.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
16 You should have received a copy of the GNU General Public License
17 (for example /usr/src/linux/COPYING); if not, write to the Free
18 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/blkdev.h>
22 #include <linux/seq_file.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
29 static int raid0_congested(void *data
, int bits
)
31 mddev_t
*mddev
= data
;
32 raid0_conf_t
*conf
= mddev
->private;
33 mdk_rdev_t
**devlist
= conf
->devlist
;
34 int raid_disks
= conf
->strip_zone
[0].nb_dev
;
37 if (mddev_congested(mddev
, bits
))
40 for (i
= 0; i
< raid_disks
&& !ret
; i
++) {
41 struct request_queue
*q
= bdev_get_queue(devlist
[i
]->bdev
);
43 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
49 * inform the user of the raid configuration
51 static void dump_zones(mddev_t
*mddev
)
54 sector_t zone_size
= 0;
55 sector_t zone_start
= 0;
56 char b
[BDEVNAME_SIZE
];
57 raid0_conf_t
*conf
= mddev
->private;
58 int raid_disks
= conf
->strip_zone
[0].nb_dev
;
59 printk(KERN_INFO
"******* %s configuration *********\n",
62 for (j
= 0; j
< conf
->nr_strip_zones
; j
++) {
63 printk(KERN_INFO
"zone%d=[", j
);
64 for (k
= 0; k
< conf
->strip_zone
[j
].nb_dev
; k
++)
65 printk(KERN_CONT
"%s/",
66 bdevname(conf
->devlist
[j
*raid_disks
68 printk(KERN_CONT
"]\n");
70 zone_size
= conf
->strip_zone
[j
].zone_end
- zone_start
;
71 printk(KERN_INFO
" zone offset=%llukb "
72 "device offset=%llukb size=%llukb\n",
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
;
78 printk(KERN_INFO
"**********************************\n\n");
81 static int create_strip_zones(mddev_t
*mddev
, raid0_conf_t
**private_conf
)
84 sector_t curr_zone_end
, sectors
;
85 mdk_rdev_t
*smallest
, *rdev1
, *rdev2
, *rdev
, **dev
;
86 struct strip_zone
*zone
;
88 char b
[BDEVNAME_SIZE
];
89 raid0_conf_t
*conf
= kzalloc(sizeof(*conf
), GFP_KERNEL
);
93 list_for_each_entry(rdev1
, &mddev
->disks
, same_set
) {
94 printk(KERN_INFO
"md/raid0:%s: looking at %s\n",
96 bdevname(rdev1
->bdev
, b
));
99 /* round size to chunk_size */
100 sectors
= rdev1
->sectors
;
101 sector_div(sectors
, mddev
->chunk_sectors
);
102 rdev1
->sectors
= sectors
* mddev
->chunk_sectors
;
104 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
105 printk(KERN_INFO
"md/raid0:%s: comparing %s(%llu)",
107 bdevname(rdev1
->bdev
,b
),
108 (unsigned long long)rdev1
->sectors
);
109 printk(KERN_CONT
" with %s(%llu)\n",
110 bdevname(rdev2
->bdev
,b
),
111 (unsigned long long)rdev2
->sectors
);
112 if (rdev2
== rdev1
) {
113 printk(KERN_INFO
"md/raid0:%s: END\n",
117 if (rdev2
->sectors
== rdev1
->sectors
) {
119 * Not unique, don't count it as a new
122 printk(KERN_INFO
"md/raid0:%s: EQUAL\n",
127 printk(KERN_INFO
"md/raid0:%s: NOT EQUAL\n",
131 printk(KERN_INFO
"md/raid0:%s: ==> UNIQUE\n",
133 conf
->nr_strip_zones
++;
134 printk(KERN_INFO
"md/raid0:%s: %d zones\n",
135 mdname(mddev
), conf
->nr_strip_zones
);
138 printk(KERN_INFO
"md/raid0:%s: FINAL %d zones\n",
139 mdname(mddev
), conf
->nr_strip_zones
);
141 conf
->strip_zone
= kzalloc(sizeof(struct strip_zone
)*
142 conf
->nr_strip_zones
, GFP_KERNEL
);
143 if (!conf
->strip_zone
)
145 conf
->devlist
= kzalloc(sizeof(mdk_rdev_t
*)*
146 conf
->nr_strip_zones
*mddev
->raid_disks
,
151 /* The first zone must contain all devices, so here we check that
152 * there is a proper alignment of slots to devices and find them all
154 zone
= &conf
->strip_zone
[0];
159 list_for_each_entry(rdev1
, &mddev
->disks
, same_set
) {
160 int j
= rdev1
->raid_disk
;
162 if (mddev
->level
== 10) {
163 /* taking over a raid10-n2 array */
165 rdev1
->new_raid_disk
= j
;
168 if (mddev
->level
== 1) {
169 /* taiking over a raid1 array-
170 * we have only one active disk
173 rdev1
->new_raid_disk
= j
;
176 if (j
< 0 || j
>= mddev
->raid_disks
) {
177 printk(KERN_ERR
"md/raid0:%s: bad disk number %d - "
178 "aborting!\n", mdname(mddev
), j
);
182 printk(KERN_ERR
"md/raid0:%s: multiple devices for %d - "
183 "aborting!\n", mdname(mddev
), j
);
188 disk_stack_limits(mddev
->gendisk
, rdev1
->bdev
,
189 rdev1
->data_offset
<< 9);
190 /* as we don't honour merge_bvec_fn, we must never risk
191 * violating it, so limit ->max_segments to 1, lying within
195 if (rdev1
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
196 blk_queue_max_segments(mddev
->queue
, 1);
197 blk_queue_segment_boundary(mddev
->queue
,
198 PAGE_CACHE_SIZE
- 1);
200 if (!smallest
|| (rdev1
->sectors
< smallest
->sectors
))
204 if (cnt
!= mddev
->raid_disks
) {
205 printk(KERN_ERR
"md/raid0:%s: too few disks (%d of %d) - "
206 "aborting!\n", mdname(mddev
), cnt
, mddev
->raid_disks
);
210 zone
->zone_end
= smallest
->sectors
* cnt
;
212 curr_zone_end
= zone
->zone_end
;
214 /* now do the other zones */
215 for (i
= 1; i
< conf
->nr_strip_zones
; i
++)
219 zone
= conf
->strip_zone
+ i
;
220 dev
= conf
->devlist
+ i
* mddev
->raid_disks
;
222 printk(KERN_INFO
"md/raid0:%s: zone %d\n",
224 zone
->dev_start
= smallest
->sectors
;
228 for (j
=0; j
<cnt
; j
++) {
229 rdev
= conf
->devlist
[j
];
230 printk(KERN_INFO
"md/raid0:%s: checking %s ...",
232 bdevname(rdev
->bdev
, b
));
233 if (rdev
->sectors
<= zone
->dev_start
) {
234 printk(KERN_CONT
" nope.\n");
237 printk(KERN_CONT
" contained as device %d\n", c
);
240 if (!smallest
|| rdev
->sectors
< smallest
->sectors
) {
242 printk(KERN_INFO
"md/raid0:%s: (%llu) is smallest!.\n",
244 (unsigned long long)rdev
->sectors
);
249 sectors
= (smallest
->sectors
- zone
->dev_start
) * c
;
250 printk(KERN_INFO
"md/raid0:%s: zone->nb_dev: %d, sectors: %llu\n",
252 zone
->nb_dev
, (unsigned long long)sectors
);
254 curr_zone_end
+= sectors
;
255 zone
->zone_end
= curr_zone_end
;
257 printk(KERN_INFO
"md/raid0:%s: current zone start: %llu\n",
259 (unsigned long long)smallest
->sectors
);
261 mddev
->queue
->backing_dev_info
.congested_fn
= raid0_congested
;
262 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
265 * now since we have the hard sector sizes, we can make sure
266 * chunk size is a multiple of that sector size
268 if ((mddev
->chunk_sectors
<< 9) % queue_logical_block_size(mddev
->queue
)) {
269 printk(KERN_ERR
"md/raid0:%s: chunk_size of %d not valid\n",
271 mddev
->chunk_sectors
<< 9);
275 blk_queue_io_min(mddev
->queue
, mddev
->chunk_sectors
<< 9);
276 blk_queue_io_opt(mddev
->queue
,
277 (mddev
->chunk_sectors
<< 9) * mddev
->raid_disks
);
279 printk(KERN_INFO
"md/raid0:%s: done.\n", mdname(mddev
));
280 *private_conf
= conf
;
284 kfree(conf
->strip_zone
);
285 kfree(conf
->devlist
);
287 *private_conf
= NULL
;
292 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
294 * @bvm: properties of new bio
295 * @biovec: the request that could be merged to it.
297 * Return amount of bytes we can accept at this offset
299 static int raid0_mergeable_bvec(struct request_queue
*q
,
300 struct bvec_merge_data
*bvm
,
301 struct bio_vec
*biovec
)
303 mddev_t
*mddev
= q
->queuedata
;
304 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
306 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
307 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
309 if (is_power_of_2(chunk_sectors
))
310 max
= (chunk_sectors
- ((sector
& (chunk_sectors
-1))
311 + bio_sectors
)) << 9;
313 max
= (chunk_sectors
- (sector_div(sector
, chunk_sectors
)
314 + bio_sectors
)) << 9;
315 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
316 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
317 return biovec
->bv_len
;
322 static sector_t
raid0_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
324 sector_t array_sectors
= 0;
327 WARN_ONCE(sectors
|| raid_disks
,
328 "%s does not support generic reshape\n", __func__
);
330 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
331 array_sectors
+= rdev
->sectors
;
333 return array_sectors
;
336 static int raid0_run(mddev_t
*mddev
)
341 if (mddev
->chunk_sectors
== 0) {
342 printk(KERN_ERR
"md/raid0:%s: chunk size must be set.\n",
346 if (md_check_no_bitmap(mddev
))
348 blk_queue_max_hw_sectors(mddev
->queue
, mddev
->chunk_sectors
);
350 /* if private is not null, we are here after takeover */
351 if (mddev
->private == NULL
) {
352 ret
= create_strip_zones(mddev
, &conf
);
355 mddev
->private = conf
;
357 conf
= mddev
->private;
359 /* calculate array device size */
360 md_set_array_sectors(mddev
, raid0_size(mddev
, 0, 0));
362 printk(KERN_INFO
"md/raid0:%s: md_size is %llu sectors.\n",
364 (unsigned long long)mddev
->array_sectors
);
365 /* calculate the max read-ahead size.
366 * For read-ahead of large files to be effective, we need to
367 * readahead at least twice a whole stripe. i.e. number of devices
368 * multiplied by chunk size times 2.
369 * If an individual device has an ra_pages greater than the
370 * chunk size, then we will not drive that device as hard as it
371 * wants. We consider this a configuration error: a larger
372 * chunksize should be used in that case.
375 int stripe
= mddev
->raid_disks
*
376 (mddev
->chunk_sectors
<< 9) / PAGE_SIZE
;
377 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
378 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
381 blk_queue_merge_bvec(mddev
->queue
, raid0_mergeable_bvec
);
383 return md_integrity_register(mddev
);
386 static int raid0_stop(mddev_t
*mddev
)
388 raid0_conf_t
*conf
= mddev
->private;
390 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
391 kfree(conf
->strip_zone
);
392 kfree(conf
->devlist
);
394 mddev
->private = NULL
;
398 /* Find the zone which holds a particular offset
399 * Update *sectorp to be an offset in that zone
401 static struct strip_zone
*find_zone(struct raid0_private_data
*conf
,
405 struct strip_zone
*z
= conf
->strip_zone
;
406 sector_t sector
= *sectorp
;
408 for (i
= 0; i
< conf
->nr_strip_zones
; i
++)
409 if (sector
< z
[i
].zone_end
) {
411 *sectorp
= sector
- z
[i
-1].zone_end
;
418 * remaps the bio to the target device. we separate two flows.
419 * power 2 flow and a general flow for the sake of perfromance
421 static mdk_rdev_t
*map_sector(mddev_t
*mddev
, struct strip_zone
*zone
,
422 sector_t sector
, sector_t
*sector_offset
)
424 unsigned int sect_in_chunk
;
426 raid0_conf_t
*conf
= mddev
->private;
427 int raid_disks
= conf
->strip_zone
[0].nb_dev
;
428 unsigned int chunk_sects
= mddev
->chunk_sectors
;
430 if (is_power_of_2(chunk_sects
)) {
431 int chunksect_bits
= ffz(~chunk_sects
);
432 /* find the sector offset inside the chunk */
433 sect_in_chunk
= sector
& (chunk_sects
- 1);
434 sector
>>= chunksect_bits
;
436 chunk
= *sector_offset
;
437 /* quotient is the chunk in real device*/
438 sector_div(chunk
, zone
->nb_dev
<< chunksect_bits
);
440 sect_in_chunk
= sector_div(sector
, chunk_sects
);
441 chunk
= *sector_offset
;
442 sector_div(chunk
, chunk_sects
* zone
->nb_dev
);
445 * position the bio over the real device
446 * real sector = chunk in device + starting of zone
447 * + the position in the chunk
449 *sector_offset
= (chunk
* chunk_sects
) + sect_in_chunk
;
450 return conf
->devlist
[(zone
- conf
->strip_zone
)*raid_disks
451 + sector_div(sector
, zone
->nb_dev
)];
455 * Is io distribute over 1 or more chunks ?
457 static inline int is_io_in_chunk_boundary(mddev_t
*mddev
,
458 unsigned int chunk_sects
, struct bio
*bio
)
460 if (likely(is_power_of_2(chunk_sects
))) {
461 return chunk_sects
>= ((bio
->bi_sector
& (chunk_sects
-1))
462 + (bio
->bi_size
>> 9));
464 sector_t sector
= bio
->bi_sector
;
465 return chunk_sects
>= (sector_div(sector
, chunk_sects
)
466 + (bio
->bi_size
>> 9));
470 static int raid0_make_request(mddev_t
*mddev
, struct bio
*bio
)
472 unsigned int chunk_sects
;
473 sector_t sector_offset
;
474 struct strip_zone
*zone
;
477 if (unlikely(bio
->bi_rw
& REQ_FLUSH
)) {
478 md_flush_request(mddev
, bio
);
482 chunk_sects
= mddev
->chunk_sectors
;
483 if (unlikely(!is_io_in_chunk_boundary(mddev
, chunk_sects
, bio
))) {
484 sector_t sector
= bio
->bi_sector
;
486 /* Sanity check -- queue functions should prevent this happening */
487 if (bio
->bi_vcnt
!= 1 ||
490 /* This is a one page bio that upper layers
491 * refuse to split for us, so we need to split it.
493 if (likely(is_power_of_2(chunk_sects
)))
494 bp
= bio_split(bio
, chunk_sects
- (sector
&
497 bp
= bio_split(bio
, chunk_sects
-
498 sector_div(sector
, chunk_sects
));
499 if (raid0_make_request(mddev
, &bp
->bio1
))
500 generic_make_request(&bp
->bio1
);
501 if (raid0_make_request(mddev
, &bp
->bio2
))
502 generic_make_request(&bp
->bio2
);
504 bio_pair_release(bp
);
508 sector_offset
= bio
->bi_sector
;
509 zone
= find_zone(mddev
->private, §or_offset
);
510 tmp_dev
= map_sector(mddev
, zone
, bio
->bi_sector
,
512 bio
->bi_bdev
= tmp_dev
->bdev
;
513 bio
->bi_sector
= sector_offset
+ zone
->dev_start
+
514 tmp_dev
->data_offset
;
516 * Let the main block layer submit the IO and resolve recursion:
521 printk("md/raid0:%s: make_request bug: can't convert block across chunks"
522 " or bigger than %dk %llu %d\n",
523 mdname(mddev
), chunk_sects
/ 2,
524 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
530 static void raid0_status(struct seq_file
*seq
, mddev_t
*mddev
)
535 char b
[BDEVNAME_SIZE
];
536 raid0_conf_t
*conf
= mddev
->private;
537 int raid_disks
= conf
->strip_zone
[0].nb_dev
;
540 sector_t zone_start
= 0;
543 for (j
= 0; j
< conf
->nr_strip_zones
; j
++) {
544 seq_printf(seq
, " z%d", j
);
545 seq_printf(seq
, "=[");
546 for (k
= 0; k
< conf
->strip_zone
[j
].nb_dev
; k
++)
547 seq_printf(seq
, "%s/", bdevname(
548 conf
->devlist
[j
*raid_disks
+ k
]
551 zone_size
= conf
->strip_zone
[j
].zone_end
- zone_start
;
552 seq_printf(seq
, "] ze=%lld ds=%lld s=%lld\n",
553 (unsigned long long)zone_start
>>1,
554 (unsigned long long)conf
->strip_zone
[j
].dev_start
>>1,
555 (unsigned long long)zone_size
>>1);
556 zone_start
= conf
->strip_zone
[j
].zone_end
;
559 seq_printf(seq
, " %dk chunks", mddev
->chunk_sectors
/ 2);
563 static void *raid0_takeover_raid45(mddev_t
*mddev
)
566 raid0_conf_t
*priv_conf
;
568 if (mddev
->degraded
!= 1) {
569 printk(KERN_ERR
"md/raid0:%s: raid5 must be degraded! Degraded disks: %d\n",
572 return ERR_PTR(-EINVAL
);
575 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
576 /* check slot number for a disk */
577 if (rdev
->raid_disk
== mddev
->raid_disks
-1) {
578 printk(KERN_ERR
"md/raid0:%s: raid5 must have missing parity disk!\n",
580 return ERR_PTR(-EINVAL
);
584 /* Set new parameters */
585 mddev
->new_level
= 0;
586 mddev
->new_layout
= 0;
587 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
589 mddev
->delta_disks
= -1;
590 /* make sure it will be not marked as dirty */
591 mddev
->recovery_cp
= MaxSector
;
593 create_strip_zones(mddev
, &priv_conf
);
597 static void *raid0_takeover_raid10(mddev_t
*mddev
)
599 raid0_conf_t
*priv_conf
;
602 * - far_copies must be 1
603 * - near_copies must be 2
604 * - disks number must be even
605 * - all mirrors must be already degraded
607 if (mddev
->layout
!= ((1 << 8) + 2)) {
608 printk(KERN_ERR
"md/raid0:%s:: Raid0 cannot takover layout: 0x%x\n",
611 return ERR_PTR(-EINVAL
);
613 if (mddev
->raid_disks
& 1) {
614 printk(KERN_ERR
"md/raid0:%s: Raid0 cannot takover Raid10 with odd disk number.\n",
616 return ERR_PTR(-EINVAL
);
618 if (mddev
->degraded
!= (mddev
->raid_disks
>>1)) {
619 printk(KERN_ERR
"md/raid0:%s: All mirrors must be already degraded!\n",
621 return ERR_PTR(-EINVAL
);
624 /* Set new parameters */
625 mddev
->new_level
= 0;
626 mddev
->new_layout
= 0;
627 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
628 mddev
->delta_disks
= - mddev
->raid_disks
/ 2;
629 mddev
->raid_disks
+= mddev
->delta_disks
;
631 /* make sure it will be not marked as dirty */
632 mddev
->recovery_cp
= MaxSector
;
634 create_strip_zones(mddev
, &priv_conf
);
638 static void *raid0_takeover_raid1(mddev_t
*mddev
)
640 raid0_conf_t
*priv_conf
;
643 * - (N - 1) mirror drives must be already faulty
645 if ((mddev
->raid_disks
- 1) != mddev
->degraded
) {
646 printk(KERN_ERR
"md/raid0:%s: (N - 1) mirrors drives must be already faulty!\n",
648 return ERR_PTR(-EINVAL
);
651 /* Set new parameters */
652 mddev
->new_level
= 0;
653 mddev
->new_layout
= 0;
654 mddev
->new_chunk_sectors
= 128; /* by default set chunk size to 64k */
655 mddev
->delta_disks
= 1 - mddev
->raid_disks
;
656 mddev
->raid_disks
= 1;
657 /* make sure it will be not marked as dirty */
658 mddev
->recovery_cp
= MaxSector
;
660 create_strip_zones(mddev
, &priv_conf
);
664 static void *raid0_takeover(mddev_t
*mddev
)
666 /* raid0 can take over:
667 * raid4 - if all data disks are active.
668 * raid5 - providing it is Raid4 layout and one disk is faulty
669 * raid10 - assuming we have all necessary active disks
670 * raid1 - with (N -1) mirror drives faulty
672 if (mddev
->level
== 4)
673 return raid0_takeover_raid45(mddev
);
675 if (mddev
->level
== 5) {
676 if (mddev
->layout
== ALGORITHM_PARITY_N
)
677 return raid0_takeover_raid45(mddev
);
679 printk(KERN_ERR
"md/raid0:%s: Raid can only takeover Raid5 with layout: %d\n",
680 mdname(mddev
), ALGORITHM_PARITY_N
);
683 if (mddev
->level
== 10)
684 return raid0_takeover_raid10(mddev
);
686 if (mddev
->level
== 1)
687 return raid0_takeover_raid1(mddev
);
689 printk(KERN_ERR
"Takeover from raid%i to raid0 not supported\n",
692 return ERR_PTR(-EINVAL
);
695 static void raid0_quiesce(mddev_t
*mddev
, int state
)
699 static struct mdk_personality raid0_personality
=
703 .owner
= THIS_MODULE
,
704 .make_request
= raid0_make_request
,
707 .status
= raid0_status
,
709 .takeover
= raid0_takeover
,
710 .quiesce
= raid0_quiesce
,
713 static int __init
raid0_init (void)
715 return register_md_personality (&raid0_personality
);
718 static void raid0_exit (void)
720 unregister_md_personality (&raid0_personality
);
723 module_init(raid0_init
);
724 module_exit(raid0_exit
);
725 MODULE_LICENSE("GPL");
726 MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
727 MODULE_ALIAS("md-personality-2"); /* RAID0 */
728 MODULE_ALIAS("md-raid0");
729 MODULE_ALIAS("md-level-0");