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/slab.h>
27 static void raid0_unplug(struct request_queue
*q
)
29 mddev_t
*mddev
= q
->queuedata
;
30 raid0_conf_t
*conf
= mddev
->private;
31 mdk_rdev_t
**devlist
= conf
->devlist
;
34 for (i
=0; i
<mddev
->raid_disks
; i
++) {
35 struct request_queue
*r_queue
= bdev_get_queue(devlist
[i
]->bdev
);
41 static int raid0_congested(void *data
, int bits
)
43 mddev_t
*mddev
= data
;
44 raid0_conf_t
*conf
= mddev
->private;
45 mdk_rdev_t
**devlist
= conf
->devlist
;
48 if (mddev_congested(mddev
, bits
))
51 for (i
= 0; i
< mddev
->raid_disks
&& !ret
; i
++) {
52 struct request_queue
*q
= bdev_get_queue(devlist
[i
]->bdev
);
54 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
60 * inform the user of the raid configuration
62 static void dump_zones(mddev_t
*mddev
)
65 sector_t zone_size
= 0;
66 sector_t zone_start
= 0;
67 char b
[BDEVNAME_SIZE
];
68 raid0_conf_t
*conf
= mddev
->private;
69 printk(KERN_INFO
"******* %s configuration *********\n",
72 for (j
= 0; j
< conf
->nr_strip_zones
; j
++) {
73 printk(KERN_INFO
"zone%d=[", j
);
74 for (k
= 0; k
< conf
->strip_zone
[j
].nb_dev
; k
++)
76 bdevname(conf
->devlist
[j
*mddev
->raid_disks
80 zone_size
= conf
->strip_zone
[j
].zone_end
- zone_start
;
81 printk(KERN_INFO
" zone offset=%llukb "
82 "device offset=%llukb size=%llukb\n",
83 (unsigned long long)zone_start
>>1,
84 (unsigned long long)conf
->strip_zone
[j
].dev_start
>>1,
85 (unsigned long long)zone_size
>>1);
86 zone_start
= conf
->strip_zone
[j
].zone_end
;
88 printk(KERN_INFO
"**********************************\n\n");
91 static int create_strip_zones(mddev_t
*mddev
)
94 sector_t curr_zone_end
, sectors
;
95 mdk_rdev_t
*smallest
, *rdev1
, *rdev2
, *rdev
, **dev
;
96 struct strip_zone
*zone
;
98 char b
[BDEVNAME_SIZE
];
99 raid0_conf_t
*conf
= kzalloc(sizeof(*conf
), GFP_KERNEL
);
103 list_for_each_entry(rdev1
, &mddev
->disks
, same_set
) {
104 printk(KERN_INFO
"raid0: looking at %s\n",
105 bdevname(rdev1
->bdev
,b
));
108 /* round size to chunk_size */
109 sectors
= rdev1
->sectors
;
110 sector_div(sectors
, mddev
->chunk_sectors
);
111 rdev1
->sectors
= sectors
* mddev
->chunk_sectors
;
113 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
114 printk(KERN_INFO
"raid0: comparing %s(%llu)",
115 bdevname(rdev1
->bdev
,b
),
116 (unsigned long long)rdev1
->sectors
);
117 printk(KERN_INFO
" with %s(%llu)\n",
118 bdevname(rdev2
->bdev
,b
),
119 (unsigned long long)rdev2
->sectors
);
120 if (rdev2
== rdev1
) {
121 printk(KERN_INFO
"raid0: END\n");
124 if (rdev2
->sectors
== rdev1
->sectors
) {
126 * Not unique, don't count it as a new
129 printk(KERN_INFO
"raid0: EQUAL\n");
133 printk(KERN_INFO
"raid0: NOT EQUAL\n");
136 printk(KERN_INFO
"raid0: ==> UNIQUE\n");
137 conf
->nr_strip_zones
++;
138 printk(KERN_INFO
"raid0: %d zones\n",
139 conf
->nr_strip_zones
);
142 printk(KERN_INFO
"raid0: FINAL %d zones\n", conf
->nr_strip_zones
);
144 conf
->strip_zone
= kzalloc(sizeof(struct strip_zone
)*
145 conf
->nr_strip_zones
, GFP_KERNEL
);
146 if (!conf
->strip_zone
)
148 conf
->devlist
= kzalloc(sizeof(mdk_rdev_t
*)*
149 conf
->nr_strip_zones
*mddev
->raid_disks
,
154 /* The first zone must contain all devices, so here we check that
155 * there is a proper alignment of slots to devices and find them all
157 zone
= &conf
->strip_zone
[0];
162 list_for_each_entry(rdev1
, &mddev
->disks
, same_set
) {
163 int j
= rdev1
->raid_disk
;
165 if (j
< 0 || j
>= mddev
->raid_disks
) {
166 printk(KERN_ERR
"raid0: bad disk number %d - "
171 printk(KERN_ERR
"raid0: multiple devices for %d - "
177 disk_stack_limits(mddev
->gendisk
, rdev1
->bdev
,
178 rdev1
->data_offset
<< 9);
179 /* as we don't honour merge_bvec_fn, we must never risk
180 * violating it, so limit ->max_segments to 1, lying within
184 if (rdev1
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
185 blk_queue_max_segments(mddev
->queue
, 1);
186 blk_queue_segment_boundary(mddev
->queue
,
187 PAGE_CACHE_SIZE
- 1);
189 if (!smallest
|| (rdev1
->sectors
< smallest
->sectors
))
193 if (cnt
!= mddev
->raid_disks
) {
194 printk(KERN_ERR
"raid0: too few disks (%d of %d) - "
195 "aborting!\n", cnt
, mddev
->raid_disks
);
199 zone
->zone_end
= smallest
->sectors
* cnt
;
201 curr_zone_end
= zone
->zone_end
;
203 /* now do the other zones */
204 for (i
= 1; i
< conf
->nr_strip_zones
; i
++)
208 zone
= conf
->strip_zone
+ i
;
209 dev
= conf
->devlist
+ i
* mddev
->raid_disks
;
211 printk(KERN_INFO
"raid0: zone %d\n", i
);
212 zone
->dev_start
= smallest
->sectors
;
216 for (j
=0; j
<cnt
; j
++) {
217 rdev
= conf
->devlist
[j
];
218 printk(KERN_INFO
"raid0: checking %s ...",
219 bdevname(rdev
->bdev
, b
));
220 if (rdev
->sectors
<= zone
->dev_start
) {
221 printk(KERN_INFO
" nope.\n");
224 printk(KERN_INFO
" contained as device %d\n", c
);
227 if (!smallest
|| rdev
->sectors
< smallest
->sectors
) {
229 printk(KERN_INFO
" (%llu) is smallest!.\n",
230 (unsigned long long)rdev
->sectors
);
235 sectors
= (smallest
->sectors
- zone
->dev_start
) * c
;
236 printk(KERN_INFO
"raid0: zone->nb_dev: %d, sectors: %llu\n",
237 zone
->nb_dev
, (unsigned long long)sectors
);
239 curr_zone_end
+= sectors
;
240 zone
->zone_end
= curr_zone_end
;
242 printk(KERN_INFO
"raid0: current zone start: %llu\n",
243 (unsigned long long)smallest
->sectors
);
245 mddev
->queue
->unplug_fn
= raid0_unplug
;
246 mddev
->queue
->backing_dev_info
.congested_fn
= raid0_congested
;
247 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
250 * now since we have the hard sector sizes, we can make sure
251 * chunk size is a multiple of that sector size
253 if ((mddev
->chunk_sectors
<< 9) % queue_logical_block_size(mddev
->queue
)) {
254 printk(KERN_ERR
"%s chunk_size of %d not valid\n",
256 mddev
->chunk_sectors
<< 9);
260 blk_queue_io_min(mddev
->queue
, mddev
->chunk_sectors
<< 9);
261 blk_queue_io_opt(mddev
->queue
,
262 (mddev
->chunk_sectors
<< 9) * mddev
->raid_disks
);
264 printk(KERN_INFO
"raid0: done.\n");
265 mddev
->private = conf
;
268 kfree(conf
->strip_zone
);
269 kfree(conf
->devlist
);
271 mddev
->private = NULL
;
276 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
278 * @bvm: properties of new bio
279 * @biovec: the request that could be merged to it.
281 * Return amount of bytes we can accept at this offset
283 static int raid0_mergeable_bvec(struct request_queue
*q
,
284 struct bvec_merge_data
*bvm
,
285 struct bio_vec
*biovec
)
287 mddev_t
*mddev
= q
->queuedata
;
288 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
290 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
291 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
293 if (is_power_of_2(chunk_sectors
))
294 max
= (chunk_sectors
- ((sector
& (chunk_sectors
-1))
295 + bio_sectors
)) << 9;
297 max
= (chunk_sectors
- (sector_div(sector
, chunk_sectors
)
298 + bio_sectors
)) << 9;
299 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
300 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
301 return biovec
->bv_len
;
306 static sector_t
raid0_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
308 sector_t array_sectors
= 0;
311 WARN_ONCE(sectors
|| raid_disks
,
312 "%s does not support generic reshape\n", __func__
);
314 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
315 array_sectors
+= rdev
->sectors
;
317 return array_sectors
;
320 static int raid0_run(mddev_t
*mddev
)
324 if (mddev
->chunk_sectors
== 0) {
325 printk(KERN_ERR
"md/raid0: chunk size must be set.\n");
328 if (md_check_no_bitmap(mddev
))
330 blk_queue_max_hw_sectors(mddev
->queue
, mddev
->chunk_sectors
);
331 mddev
->queue
->queue_lock
= &mddev
->queue
->__queue_lock
;
333 ret
= create_strip_zones(mddev
);
337 /* calculate array device size */
338 md_set_array_sectors(mddev
, raid0_size(mddev
, 0, 0));
340 printk(KERN_INFO
"raid0 : md_size is %llu sectors.\n",
341 (unsigned long long)mddev
->array_sectors
);
342 /* calculate the max read-ahead size.
343 * For read-ahead of large files to be effective, we need to
344 * readahead at least twice a whole stripe. i.e. number of devices
345 * multiplied by chunk size times 2.
346 * If an individual device has an ra_pages greater than the
347 * chunk size, then we will not drive that device as hard as it
348 * wants. We consider this a configuration error: a larger
349 * chunksize should be used in that case.
352 int stripe
= mddev
->raid_disks
*
353 (mddev
->chunk_sectors
<< 9) / PAGE_SIZE
;
354 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
355 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
358 blk_queue_merge_bvec(mddev
->queue
, raid0_mergeable_bvec
);
360 md_integrity_register(mddev
);
364 static int raid0_stop(mddev_t
*mddev
)
366 raid0_conf_t
*conf
= mddev
->private;
368 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
369 kfree(conf
->strip_zone
);
370 kfree(conf
->devlist
);
372 mddev
->private = NULL
;
376 /* Find the zone which holds a particular offset
377 * Update *sectorp to be an offset in that zone
379 static struct strip_zone
*find_zone(struct raid0_private_data
*conf
,
383 struct strip_zone
*z
= conf
->strip_zone
;
384 sector_t sector
= *sectorp
;
386 for (i
= 0; i
< conf
->nr_strip_zones
; i
++)
387 if (sector
< z
[i
].zone_end
) {
389 *sectorp
= sector
- z
[i
-1].zone_end
;
396 * remaps the bio to the target device. we separate two flows.
397 * power 2 flow and a general flow for the sake of perfromance
399 static mdk_rdev_t
*map_sector(mddev_t
*mddev
, struct strip_zone
*zone
,
400 sector_t sector
, sector_t
*sector_offset
)
402 unsigned int sect_in_chunk
;
404 raid0_conf_t
*conf
= mddev
->private;
405 unsigned int chunk_sects
= mddev
->chunk_sectors
;
407 if (is_power_of_2(chunk_sects
)) {
408 int chunksect_bits
= ffz(~chunk_sects
);
409 /* find the sector offset inside the chunk */
410 sect_in_chunk
= sector
& (chunk_sects
- 1);
411 sector
>>= chunksect_bits
;
413 chunk
= *sector_offset
;
414 /* quotient is the chunk in real device*/
415 sector_div(chunk
, zone
->nb_dev
<< chunksect_bits
);
417 sect_in_chunk
= sector_div(sector
, chunk_sects
);
418 chunk
= *sector_offset
;
419 sector_div(chunk
, chunk_sects
* zone
->nb_dev
);
422 * position the bio over the real device
423 * real sector = chunk in device + starting of zone
424 * + the position in the chunk
426 *sector_offset
= (chunk
* chunk_sects
) + sect_in_chunk
;
427 return conf
->devlist
[(zone
- conf
->strip_zone
)*mddev
->raid_disks
428 + sector_div(sector
, zone
->nb_dev
)];
432 * Is io distribute over 1 or more chunks ?
434 static inline int is_io_in_chunk_boundary(mddev_t
*mddev
,
435 unsigned int chunk_sects
, struct bio
*bio
)
437 if (likely(is_power_of_2(chunk_sects
))) {
438 return chunk_sects
>= ((bio
->bi_sector
& (chunk_sects
-1))
439 + (bio
->bi_size
>> 9));
441 sector_t sector
= bio
->bi_sector
;
442 return chunk_sects
>= (sector_div(sector
, chunk_sects
)
443 + (bio
->bi_size
>> 9));
447 static int raid0_make_request(struct request_queue
*q
, struct bio
*bio
)
449 mddev_t
*mddev
= q
->queuedata
;
450 unsigned int chunk_sects
;
451 sector_t sector_offset
;
452 struct strip_zone
*zone
;
454 const int rw
= bio_data_dir(bio
);
457 if (unlikely(bio_rw_flagged(bio
, BIO_RW_BARRIER
))) {
458 md_barrier_request(mddev
, bio
);
462 cpu
= part_stat_lock();
463 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
464 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
],
468 chunk_sects
= mddev
->chunk_sectors
;
469 if (unlikely(!is_io_in_chunk_boundary(mddev
, chunk_sects
, bio
))) {
470 sector_t sector
= bio
->bi_sector
;
472 /* Sanity check -- queue functions should prevent this happening */
473 if (bio
->bi_vcnt
!= 1 ||
476 /* This is a one page bio that upper layers
477 * refuse to split for us, so we need to split it.
479 if (likely(is_power_of_2(chunk_sects
)))
480 bp
= bio_split(bio
, chunk_sects
- (sector
&
483 bp
= bio_split(bio
, chunk_sects
-
484 sector_div(sector
, chunk_sects
));
485 if (raid0_make_request(q
, &bp
->bio1
))
486 generic_make_request(&bp
->bio1
);
487 if (raid0_make_request(q
, &bp
->bio2
))
488 generic_make_request(&bp
->bio2
);
490 bio_pair_release(bp
);
494 sector_offset
= bio
->bi_sector
;
495 zone
= find_zone(mddev
->private, §or_offset
);
496 tmp_dev
= map_sector(mddev
, zone
, bio
->bi_sector
,
498 bio
->bi_bdev
= tmp_dev
->bdev
;
499 bio
->bi_sector
= sector_offset
+ zone
->dev_start
+
500 tmp_dev
->data_offset
;
502 * Let the main block layer submit the IO and resolve recursion:
507 printk("raid0_make_request bug: can't convert block across chunks"
508 " or bigger than %dk %llu %d\n", chunk_sects
/ 2,
509 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
515 static void raid0_status(struct seq_file
*seq
, mddev_t
*mddev
)
520 char b
[BDEVNAME_SIZE
];
521 raid0_conf_t
*conf
= mddev
->private;
524 sector_t zone_start
= 0;
527 for (j
= 0; j
< conf
->nr_strip_zones
; j
++) {
528 seq_printf(seq
, " z%d", j
);
529 seq_printf(seq
, "=[");
530 for (k
= 0; k
< conf
->strip_zone
[j
].nb_dev
; k
++)
531 seq_printf(seq
, "%s/", bdevname(
532 conf
->devlist
[j
*mddev
->raid_disks
+ k
]
535 zone_size
= conf
->strip_zone
[j
].zone_end
- zone_start
;
536 seq_printf(seq
, "] ze=%lld ds=%lld s=%lld\n",
537 (unsigned long long)zone_start
>>1,
538 (unsigned long long)conf
->strip_zone
[j
].dev_start
>>1,
539 (unsigned long long)zone_size
>>1);
540 zone_start
= conf
->strip_zone
[j
].zone_end
;
543 seq_printf(seq
, " %dk chunks", mddev
->chunk_sectors
/ 2);
547 static struct mdk_personality raid0_personality
=
551 .owner
= THIS_MODULE
,
552 .make_request
= raid0_make_request
,
555 .status
= raid0_status
,
559 static int __init
raid0_init (void)
561 return register_md_personality (&raid0_personality
);
564 static void raid0_exit (void)
566 unregister_md_personality (&raid0_personality
);
569 module_init(raid0_init
);
570 module_exit(raid0_exit
);
571 MODULE_LICENSE("GPL");
572 MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
573 MODULE_ALIAS("md-personality-2"); /* RAID0 */
574 MODULE_ALIAS("md-raid0");
575 MODULE_ALIAS("md-level-0");