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_to_conf(mddev);
30 mdk_rdev_t **devlist = conf->strip_zone[0].dev;
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_to_conf(mddev);
44 mdk_rdev_t **devlist = conf->strip_zone[0].dev;
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);
55 static int create_strip_zones(mddev_t *mddev)
58 sector_t curr_zone_end;
59 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
60 struct strip_zone *zone;
62 char b[BDEVNAME_SIZE];
63 raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
67 list_for_each_entry(rdev1, &mddev->disks, same_set) {
68 printk(KERN_INFO "raid0: looking at %s\n",
69 bdevname(rdev1->bdev,b));
71 list_for_each_entry(rdev2, &mddev->disks, same_set) {
72 printk(KERN_INFO "raid0: comparing %s(%llu)",
73 bdevname(rdev1->bdev,b),
74 (unsigned long long)rdev1->sectors);
75 printk(KERN_INFO " with %s(%llu)\n",
76 bdevname(rdev2->bdev,b),
77 (unsigned long long)rdev2->sectors);
79 printk(KERN_INFO "raid0: END\n");
82 if (rdev2->sectors == rdev1->sectors) {
84 * Not unique, don't count it as a new
87 printk(KERN_INFO "raid0: EQUAL\n");
91 printk(KERN_INFO "raid0: NOT EQUAL\n");
94 printk(KERN_INFO "raid0: ==> UNIQUE\n");
95 conf->nr_strip_zones++;
96 printk(KERN_INFO "raid0: %d zones\n",
97 conf->nr_strip_zones);
100 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
102 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
103 conf->nr_strip_zones, GFP_KERNEL);
104 if (!conf->strip_zone)
106 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
107 conf->nr_strip_zones*mddev->raid_disks,
112 /* The first zone must contain all devices, so here we check that
113 * there is a proper alignment of slots to devices and find them all
115 zone = &conf->strip_zone[0];
118 zone->dev = conf->devlist;
120 list_for_each_entry(rdev1, &mddev->disks, same_set) {
121 int j = rdev1->raid_disk;
123 if (j < 0 || j >= mddev->raid_disks) {
124 printk(KERN_ERR "raid0: bad disk number %d - "
129 printk(KERN_ERR "raid0: multiple devices for %d - "
133 zone->dev[j] = rdev1;
135 blk_queue_stack_limits(mddev->queue,
136 rdev1->bdev->bd_disk->queue);
137 /* as we don't honour merge_bvec_fn, we must never risk
138 * violating it, so limit ->max_sector to one PAGE, as
139 * a one page request is never in violation.
142 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
143 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
144 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
146 if (!smallest || (rdev1->sectors < smallest->sectors))
150 if (cnt != mddev->raid_disks) {
151 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
152 "aborting!\n", cnt, mddev->raid_disks);
156 zone->sectors = smallest->sectors * cnt;
157 zone->zone_end = zone->sectors;
159 curr_zone_end = zone->sectors;
161 /* now do the other zones */
162 for (i = 1; i < conf->nr_strip_zones; i++)
164 zone = conf->strip_zone + i;
165 zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
167 printk(KERN_INFO "raid0: zone %d\n", i);
168 zone->dev_start = smallest->sectors;
172 for (j=0; j<cnt; j++) {
173 char b[BDEVNAME_SIZE];
174 rdev = conf->strip_zone[0].dev[j];
175 printk(KERN_INFO "raid0: checking %s ...",
176 bdevname(rdev->bdev, b));
177 if (rdev->sectors <= zone->dev_start) {
178 printk(KERN_INFO " nope.\n");
181 printk(KERN_INFO " contained as device %d\n", c);
184 if (!smallest || rdev->sectors < smallest->sectors) {
186 printk(KERN_INFO " (%llu) is smallest!.\n",
187 (unsigned long long)rdev->sectors);
192 zone->sectors = (smallest->sectors - zone->dev_start) * c;
193 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
194 zone->nb_dev, (unsigned long long)zone->sectors);
196 curr_zone_end += zone->sectors;
197 zone->zone_end = curr_zone_end;
199 printk(KERN_INFO "raid0: current zone start: %llu\n",
200 (unsigned long long)smallest->sectors);
202 mddev->queue->unplug_fn = raid0_unplug;
203 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
204 mddev->queue->backing_dev_info.congested_data = mddev;
206 printk(KERN_INFO "raid0: done.\n");
207 mddev->private = conf;
210 kfree(conf->strip_zone);
211 kfree(conf->devlist);
213 mddev->private = NULL;
218 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
220 * @bvm: properties of new bio
221 * @biovec: the request that could be merged to it.
223 * Return amount of bytes we can accept at this offset
225 static int raid0_mergeable_bvec(struct request_queue *q,
226 struct bvec_merge_data *bvm,
227 struct bio_vec *biovec)
229 mddev_t *mddev = q->queuedata;
230 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
232 unsigned int chunk_sectors = mddev->chunk_size >> 9;
233 unsigned int bio_sectors = bvm->bi_size >> 9;
235 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
236 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
237 if (max <= biovec->bv_len && bio_sectors == 0)
238 return biovec->bv_len;
243 static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
245 sector_t array_sectors = 0;
248 WARN_ONCE(sectors || raid_disks,
249 "%s does not support generic reshape\n", __func__);
251 list_for_each_entry(rdev, &mddev->disks, same_set)
252 array_sectors += rdev->sectors;
254 return array_sectors;
257 static int raid0_run(mddev_t *mddev)
261 if (mddev->chunk_size == 0) {
262 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
265 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
267 mddev->chunk_size >> 9,
268 (mddev->chunk_size>>1)-1);
269 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
270 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
271 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
273 ret = create_strip_zones(mddev);
277 /* calculate array device size */
278 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
280 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
281 (unsigned long long)mddev->array_sectors);
282 /* calculate the max read-ahead size.
283 * For read-ahead of large files to be effective, we need to
284 * readahead at least twice a whole stripe. i.e. number of devices
285 * multiplied by chunk size times 2.
286 * If an individual device has an ra_pages greater than the
287 * chunk size, then we will not drive that device as hard as it
288 * wants. We consider this a configuration error: a larger
289 * chunksize should be used in that case.
292 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
293 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
294 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
297 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
301 static int raid0_stop(mddev_t *mddev)
303 raid0_conf_t *conf = mddev_to_conf(mddev);
305 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
306 kfree(conf->strip_zone);
307 kfree(conf->devlist);
309 mddev->private = NULL;
313 /* Find the zone which holds a particular offset */
314 static struct strip_zone *find_zone(struct raid0_private_data *conf,
318 struct strip_zone *z = conf->strip_zone;
320 for (i = 0; i < conf->nr_strip_zones; i++)
321 if (sector < z[i].zone_end)
326 static int raid0_make_request (struct request_queue *q, struct bio *bio)
328 mddev_t *mddev = q->queuedata;
329 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
330 raid0_conf_t *conf = mddev_to_conf(mddev);
331 struct strip_zone *zone;
334 sector_t sector, rsect;
335 const int rw = bio_data_dir(bio);
338 if (unlikely(bio_barrier(bio))) {
339 bio_endio(bio, -EOPNOTSUPP);
343 cpu = part_stat_lock();
344 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
345 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
349 chunk_sects = mddev->chunk_size >> 9;
350 chunksect_bits = ffz(~chunk_sects);
351 sector = bio->bi_sector;
353 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
355 /* Sanity check -- queue functions should prevent this happening */
356 if (bio->bi_vcnt != 1 ||
359 /* This is a one page bio that upper layers
360 * refuse to split for us, so we need to split it.
362 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
363 if (raid0_make_request(q, &bp->bio1))
364 generic_make_request(&bp->bio1);
365 if (raid0_make_request(q, &bp->bio2))
366 generic_make_request(&bp->bio2);
368 bio_pair_release(bp);
371 zone = find_zone(conf, sector);
372 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
374 sector_t x = (zone->sectors + sector - zone->zone_end)
377 sector_div(x, zone->nb_dev);
380 x = sector >> chunksect_bits;
381 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
383 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
385 bio->bi_bdev = tmp_dev->bdev;
386 bio->bi_sector = rsect + tmp_dev->data_offset;
389 * Let the main block layer submit the IO and resolve recursion:
394 printk("raid0_make_request bug: can't convert block across chunks"
395 " or bigger than %dk %llu %d\n", chunk_sects / 2,
396 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
402 static void raid0_status (struct seq_file *seq, mddev_t *mddev)
407 char b[BDEVNAME_SIZE];
408 raid0_conf_t *conf = mddev_to_conf(mddev);
411 for (j = 0; j < conf->nr_strip_zones; j++) {
412 seq_printf(seq, " z%d", j);
413 seq_printf(seq, "=[");
414 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
415 seq_printf(seq, "%s/", bdevname(
416 conf->strip_zone[j].dev[k]->bdev,b));
418 seq_printf(seq, "] ze=%d ds=%d s=%d\n",
419 conf->strip_zone[j].zone_end,
420 conf->strip_zone[j].dev_start,
421 conf->strip_zone[j].sectors);
424 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
428 static struct mdk_personality raid0_personality=
432 .owner = THIS_MODULE,
433 .make_request = raid0_make_request,
436 .status = raid0_status,
440 static int __init raid0_init (void)
442 return register_md_personality (&raid0_personality);
445 static void raid0_exit (void)
447 unregister_md_personality (&raid0_personality);
450 module_init(raid0_init);
451 module_exit(raid0_exit);
452 MODULE_LICENSE("GPL");
453 MODULE_ALIAS("md-personality-2"); /* RAID0 */
454 MODULE_ALIAS("md-raid0");
455 MODULE_ALIAS("md-level-0");