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>
26 static void raid0_unplug(struct request_queue *q)
28 mddev_t *mddev = q->queuedata;
29 raid0_conf_t *conf = mddev->private;
30 mdk_rdev_t **devlist = conf->devlist;
33 for (i=0; i<mddev->raid_disks; i++) {
34 struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
40 static int raid0_congested(void *data, int bits)
42 mddev_t *mddev = data;
43 raid0_conf_t *conf = mddev->private;
44 mdk_rdev_t **devlist = conf->devlist;
47 for (i = 0; i < mddev->raid_disks && !ret ; i++) {
48 struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
50 ret |= bdi_congested(&q->backing_dev_info, bits);
56 * inform the user of the raid configuration
58 static void dump_zones(mddev_t *mddev)
61 sector_t zone_size = 0;
62 sector_t zone_start = 0;
63 char b[BDEVNAME_SIZE];
64 raid0_conf_t *conf = mddev->private;
65 printk(KERN_INFO "******* %s configuration *********\n",
68 for (j = 0; j < conf->nr_strip_zones; j++) {
69 printk(KERN_INFO "zone%d=[", j);
70 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
72 bdevname(conf->devlist[j*mddev->raid_disks
76 zone_size = conf->strip_zone[j].zone_end - zone_start;
77 printk(KERN_INFO " zone offset=%llukb "
78 "device offset=%llukb size=%llukb\n",
79 (unsigned long long)zone_start>>1,
80 (unsigned long long)conf->strip_zone[j].dev_start>>1,
81 (unsigned long long)zone_size>>1);
82 zone_start = conf->strip_zone[j].zone_end;
84 printk(KERN_INFO "**********************************\n\n");
87 static int create_strip_zones(mddev_t *mddev)
90 sector_t curr_zone_end, sectors;
91 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev, **dev;
92 struct strip_zone *zone;
94 char b[BDEVNAME_SIZE];
95 raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
99 list_for_each_entry(rdev1, &mddev->disks, same_set) {
100 printk(KERN_INFO "raid0: looking at %s\n",
101 bdevname(rdev1->bdev,b));
103 list_for_each_entry(rdev2, &mddev->disks, same_set) {
104 printk(KERN_INFO "raid0: comparing %s(%llu)",
105 bdevname(rdev1->bdev,b),
106 (unsigned long long)rdev1->sectors);
107 printk(KERN_INFO " with %s(%llu)\n",
108 bdevname(rdev2->bdev,b),
109 (unsigned long long)rdev2->sectors);
110 if (rdev2 == rdev1) {
111 printk(KERN_INFO "raid0: END\n");
114 if (rdev2->sectors == rdev1->sectors) {
116 * Not unique, don't count it as a new
119 printk(KERN_INFO "raid0: EQUAL\n");
123 printk(KERN_INFO "raid0: NOT EQUAL\n");
126 printk(KERN_INFO "raid0: ==> UNIQUE\n");
127 conf->nr_strip_zones++;
128 printk(KERN_INFO "raid0: %d zones\n",
129 conf->nr_strip_zones);
132 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
134 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
135 conf->nr_strip_zones, GFP_KERNEL);
136 if (!conf->strip_zone)
138 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
139 conf->nr_strip_zones*mddev->raid_disks,
144 /* The first zone must contain all devices, so here we check that
145 * there is a proper alignment of slots to devices and find them all
147 zone = &conf->strip_zone[0];
152 list_for_each_entry(rdev1, &mddev->disks, same_set) {
153 int j = rdev1->raid_disk;
155 if (j < 0 || j >= mddev->raid_disks) {
156 printk(KERN_ERR "raid0: bad disk number %d - "
161 printk(KERN_ERR "raid0: multiple devices for %d - "
167 blk_queue_stack_limits(mddev->queue,
168 rdev1->bdev->bd_disk->queue);
169 /* as we don't honour merge_bvec_fn, we must never risk
170 * violating it, so limit ->max_sector to one PAGE, as
171 * a one page request is never in violation.
174 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
175 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
176 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
178 if (!smallest || (rdev1->sectors < smallest->sectors))
182 if (cnt != mddev->raid_disks) {
183 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
184 "aborting!\n", cnt, mddev->raid_disks);
188 zone->zone_end = smallest->sectors * cnt;
190 curr_zone_end = zone->zone_end;
192 /* now do the other zones */
193 for (i = 1; i < conf->nr_strip_zones; i++)
195 zone = conf->strip_zone + i;
196 dev = conf->devlist + i * mddev->raid_disks;
198 printk(KERN_INFO "raid0: zone %d\n", i);
199 zone->dev_start = smallest->sectors;
203 for (j=0; j<cnt; j++) {
204 char b[BDEVNAME_SIZE];
205 rdev = conf->devlist[j];
206 printk(KERN_INFO "raid0: checking %s ...",
207 bdevname(rdev->bdev, b));
208 if (rdev->sectors <= zone->dev_start) {
209 printk(KERN_INFO " nope.\n");
212 printk(KERN_INFO " contained as device %d\n", c);
215 if (!smallest || rdev->sectors < smallest->sectors) {
217 printk(KERN_INFO " (%llu) is smallest!.\n",
218 (unsigned long long)rdev->sectors);
223 sectors = (smallest->sectors - zone->dev_start) * c;
224 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
225 zone->nb_dev, (unsigned long long)sectors);
227 curr_zone_end += sectors;
228 zone->zone_end = curr_zone_end;
230 printk(KERN_INFO "raid0: current zone start: %llu\n",
231 (unsigned long long)smallest->sectors);
233 mddev->queue->unplug_fn = raid0_unplug;
234 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
235 mddev->queue->backing_dev_info.congested_data = mddev;
238 * now since we have the hard sector sizes, we can make sure
239 * chunk size is a multiple of that sector size
241 if (mddev->chunk_size % queue_logical_block_size(mddev->queue)) {
242 printk(KERN_ERR "%s chunk_size of %d not valid\n",
247 printk(KERN_INFO "raid0: done.\n");
248 mddev->private = conf;
251 kfree(conf->strip_zone);
252 kfree(conf->devlist);
254 mddev->private = NULL;
259 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
261 * @bvm: properties of new bio
262 * @biovec: the request that could be merged to it.
264 * Return amount of bytes we can accept at this offset
266 static int raid0_mergeable_bvec(struct request_queue *q,
267 struct bvec_merge_data *bvm,
268 struct bio_vec *biovec)
270 mddev_t *mddev = q->queuedata;
271 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
273 unsigned int chunk_sectors = mddev->chunk_size >> 9;
274 unsigned int bio_sectors = bvm->bi_size >> 9;
276 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
277 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
278 if (max <= biovec->bv_len && bio_sectors == 0)
279 return biovec->bv_len;
284 static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
286 sector_t array_sectors = 0;
289 WARN_ONCE(sectors || raid_disks,
290 "%s does not support generic reshape\n", __func__);
292 list_for_each_entry(rdev, &mddev->disks, same_set)
293 array_sectors += rdev->sectors;
295 return array_sectors;
298 static int raid0_run(mddev_t *mddev)
302 if (mddev->chunk_size == 0 ||
303 !is_power_of_2(mddev->chunk_size)) {
304 printk(KERN_ERR "md/raid0: chunk size must be a power of 2.\n");
307 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
308 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
310 ret = create_strip_zones(mddev);
314 /* calculate array device size */
315 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
317 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
318 (unsigned long long)mddev->array_sectors);
319 /* calculate the max read-ahead size.
320 * For read-ahead of large files to be effective, we need to
321 * readahead at least twice a whole stripe. i.e. number of devices
322 * multiplied by chunk size times 2.
323 * If an individual device has an ra_pages greater than the
324 * chunk size, then we will not drive that device as hard as it
325 * wants. We consider this a configuration error: a larger
326 * chunksize should be used in that case.
329 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
330 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
331 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
334 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
339 static int raid0_stop(mddev_t *mddev)
341 raid0_conf_t *conf = mddev->private;
343 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
344 kfree(conf->strip_zone);
345 kfree(conf->devlist);
347 mddev->private = NULL;
351 /* Find the zone which holds a particular offset
352 * Update *sectorp to be an offset in that zone
354 static struct strip_zone *find_zone(struct raid0_private_data *conf,
358 struct strip_zone *z = conf->strip_zone;
359 sector_t sector = *sectorp;
361 for (i = 0; i < conf->nr_strip_zones; i++)
362 if (sector < z[i].zone_end) {
364 *sectorp = sector - z[i-1].zone_end;
370 static int raid0_make_request (struct request_queue *q, struct bio *bio)
372 mddev_t *mddev = q->queuedata;
373 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
374 raid0_conf_t *conf = mddev->private;
375 struct strip_zone *zone;
378 sector_t sector, rsect, sector_offset;
379 const int rw = bio_data_dir(bio);
382 if (unlikely(bio_barrier(bio))) {
383 bio_endio(bio, -EOPNOTSUPP);
387 cpu = part_stat_lock();
388 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
389 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
393 chunk_sects = mddev->chunk_size >> 9;
394 chunksect_bits = ffz(~chunk_sects);
395 sector = bio->bi_sector;
397 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
399 /* Sanity check -- queue functions should prevent this happening */
400 if (bio->bi_vcnt != 1 ||
403 /* This is a one page bio that upper layers
404 * refuse to split for us, so we need to split it.
406 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
407 if (raid0_make_request(q, &bp->bio1))
408 generic_make_request(&bp->bio1);
409 if (raid0_make_request(q, &bp->bio2))
410 generic_make_request(&bp->bio2);
412 bio_pair_release(bp);
415 sector_offset = sector;
416 zone = find_zone(conf, §or_offset);
417 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
419 sector_t x = sector_offset >> chunksect_bits;
421 sector_div(x, zone->nb_dev);
424 x = sector >> chunksect_bits;
425 tmp_dev = conf->devlist[(zone - conf->strip_zone)*mddev->raid_disks
426 + sector_div(x, zone->nb_dev)];
428 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
430 bio->bi_bdev = tmp_dev->bdev;
431 bio->bi_sector = rsect + tmp_dev->data_offset;
434 * Let the main block layer submit the IO and resolve recursion:
439 printk("raid0_make_request bug: can't convert block across chunks"
440 " or bigger than %dk %llu %d\n", chunk_sects / 2,
441 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
447 static void raid0_status(struct seq_file *seq, mddev_t *mddev)
452 char b[BDEVNAME_SIZE];
453 raid0_conf_t *conf = mddev->private;
456 sector_t zone_start = 0;
459 for (j = 0; j < conf->nr_strip_zones; j++) {
460 seq_printf(seq, " z%d", j);
461 seq_printf(seq, "=[");
462 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
463 seq_printf(seq, "%s/", bdevname(
464 conf->devlist[j*mddev->raid_disks + k]
467 zone_size = conf->strip_zone[j].zone_end - zone_start;
468 seq_printf(seq, "] ze=%lld ds=%lld s=%lld\n",
469 (unsigned long long)zone_start>>1,
470 (unsigned long long)conf->strip_zone[j].dev_start>>1,
471 (unsigned long long)zone_size>>1);
472 zone_start = conf->strip_zone[j].zone_end;
475 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
479 static struct mdk_personality raid0_personality=
483 .owner = THIS_MODULE,
484 .make_request = raid0_make_request,
487 .status = raid0_status,
491 static int __init raid0_init (void)
493 return register_md_personality (&raid0_personality);
496 static void raid0_exit (void)
498 unregister_md_personality (&raid0_personality);
501 module_init(raid0_init);
502 module_exit(raid0_exit);
503 MODULE_LICENSE("GPL");
504 MODULE_ALIAS("md-personality-2"); /* RAID0 */
505 MODULE_ALIAS("md-raid0");
506 MODULE_ALIAS("md-level-0");