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 raid0_conf_t *conf = mddev_to_conf(mddev);
60 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
61 struct strip_zone *zone;
63 char b[BDEVNAME_SIZE];
66 * The number of 'same size groups'
68 conf->nr_strip_zones = 0;
70 list_for_each_entry(rdev1, &mddev->disks, same_set) {
71 printk(KERN_INFO "raid0: looking at %s\n",
72 bdevname(rdev1->bdev,b));
74 list_for_each_entry(rdev2, &mddev->disks, same_set) {
75 printk(KERN_INFO "raid0: comparing %s(%llu)",
76 bdevname(rdev1->bdev,b),
77 (unsigned long long)rdev1->sectors);
78 printk(KERN_INFO " with %s(%llu)\n",
79 bdevname(rdev2->bdev,b),
80 (unsigned long long)rdev2->sectors);
82 printk(KERN_INFO "raid0: END\n");
85 if (rdev2->sectors == rdev1->sectors) {
87 * Not unique, don't count it as a new
90 printk(KERN_INFO "raid0: EQUAL\n");
94 printk(KERN_INFO "raid0: NOT EQUAL\n");
97 printk(KERN_INFO "raid0: ==> UNIQUE\n");
98 conf->nr_strip_zones++;
99 printk(KERN_INFO "raid0: %d zones\n",
100 conf->nr_strip_zones);
103 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
105 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
106 conf->nr_strip_zones, GFP_KERNEL);
107 if (!conf->strip_zone)
109 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
110 conf->nr_strip_zones*mddev->raid_disks,
115 /* The first zone must contain all devices, so here we check that
116 * there is a proper alignment of slots to devices and find them all
118 zone = &conf->strip_zone[0];
121 zone->dev = conf->devlist;
122 list_for_each_entry(rdev1, &mddev->disks, same_set) {
123 int j = rdev1->raid_disk;
125 if (j < 0 || j >= mddev->raid_disks) {
126 printk(KERN_ERR "raid0: bad disk number %d - "
131 printk(KERN_ERR "raid0: multiple devices for %d - "
135 zone->dev[j] = rdev1;
137 blk_queue_stack_limits(mddev->queue,
138 rdev1->bdev->bd_disk->queue);
139 /* as we don't honour merge_bvec_fn, we must never risk
140 * violating it, so limit ->max_sector to one PAGE, as
141 * a one page request is never in violation.
144 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
145 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
146 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
148 if (!smallest || (rdev1->sectors < smallest->sectors))
152 if (cnt != mddev->raid_disks) {
153 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
154 "aborting!\n", cnt, mddev->raid_disks);
158 zone->sectors = smallest->sectors * cnt;
159 zone->zone_end = zone->sectors;
161 curr_zone_end = zone->sectors;
163 /* now do the other zones */
164 for (i = 1; i < conf->nr_strip_zones; i++)
166 zone = conf->strip_zone + i;
167 zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
169 printk(KERN_INFO "raid0: zone %d\n", i);
170 zone->dev_start = smallest->sectors;
174 for (j=0; j<cnt; j++) {
175 char b[BDEVNAME_SIZE];
176 rdev = conf->strip_zone[0].dev[j];
177 printk(KERN_INFO "raid0: checking %s ...",
178 bdevname(rdev->bdev, b));
179 if (rdev->sectors <= zone->dev_start) {
180 printk(KERN_INFO " nope.\n");
183 printk(KERN_INFO " contained as device %d\n", c);
186 if (!smallest || rdev->sectors < smallest->sectors) {
188 printk(KERN_INFO " (%llu) is smallest!.\n",
189 (unsigned long long)rdev->sectors);
194 zone->sectors = (smallest->sectors - zone->dev_start) * c;
195 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
196 zone->nb_dev, (unsigned long long)zone->sectors);
198 curr_zone_end += zone->sectors;
199 zone->zone_end = curr_zone_end;
201 printk(KERN_INFO "raid0: current zone start: %llu\n",
202 (unsigned long long)smallest->sectors);
204 mddev->queue->unplug_fn = raid0_unplug;
205 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
206 mddev->queue->backing_dev_info.congested_data = mddev;
208 printk(KERN_INFO "raid0: done.\n");
215 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
217 * @bvm: properties of new bio
218 * @biovec: the request that could be merged to it.
220 * Return amount of bytes we can accept at this offset
222 static int raid0_mergeable_bvec(struct request_queue *q,
223 struct bvec_merge_data *bvm,
224 struct bio_vec *biovec)
226 mddev_t *mddev = q->queuedata;
227 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
229 unsigned int chunk_sectors = mddev->chunk_size >> 9;
230 unsigned int bio_sectors = bvm->bi_size >> 9;
232 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
233 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
234 if (max <= biovec->bv_len && bio_sectors == 0)
235 return biovec->bv_len;
240 static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
242 sector_t array_sectors = 0;
245 WARN_ONCE(sectors || raid_disks,
246 "%s does not support generic reshape\n", __func__);
248 list_for_each_entry(rdev, &mddev->disks, same_set)
249 array_sectors += rdev->sectors;
251 return array_sectors;
254 static int raid0_run(mddev_t *mddev)
259 if (mddev->chunk_size == 0) {
260 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
263 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
265 mddev->chunk_size >> 9,
266 (mddev->chunk_size>>1)-1);
267 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
268 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
269 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
271 conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
274 mddev->private = (void *)conf;
276 conf->strip_zone = NULL;
277 conf->devlist = NULL;
278 ret = create_strip_zones(mddev);
282 /* calculate array device size */
283 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
285 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
286 (unsigned long long)mddev->array_sectors);
287 /* calculate the max read-ahead size.
288 * For read-ahead of large files to be effective, we need to
289 * readahead at least twice a whole stripe. i.e. number of devices
290 * multiplied by chunk size times 2.
291 * If an individual device has an ra_pages greater than the
292 * chunk size, then we will not drive that device as hard as it
293 * wants. We consider this a configuration error: a larger
294 * chunksize should be used in that case.
297 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
298 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
299 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
303 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
307 kfree(conf->strip_zone);
308 kfree(conf->devlist);
310 mddev->private = NULL;
314 static int raid0_stop (mddev_t *mddev)
316 raid0_conf_t *conf = mddev_to_conf(mddev);
318 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
319 kfree(conf->strip_zone);
320 conf->strip_zone = NULL;
322 mddev->private = NULL;
327 /* Find the zone which holds a particular offset */
328 static struct strip_zone *find_zone(struct raid0_private_data *conf,
332 struct strip_zone *z = conf->strip_zone;
334 for (i = 0; i < conf->nr_strip_zones; i++)
335 if (sector < z[i].zone_end)
340 static int raid0_make_request (struct request_queue *q, struct bio *bio)
342 mddev_t *mddev = q->queuedata;
343 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
344 raid0_conf_t *conf = mddev_to_conf(mddev);
345 struct strip_zone *zone;
348 sector_t sector, rsect;
349 const int rw = bio_data_dir(bio);
352 if (unlikely(bio_barrier(bio))) {
353 bio_endio(bio, -EOPNOTSUPP);
357 cpu = part_stat_lock();
358 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
359 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
363 chunk_sects = mddev->chunk_size >> 9;
364 chunksect_bits = ffz(~chunk_sects);
365 sector = bio->bi_sector;
367 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
369 /* Sanity check -- queue functions should prevent this happening */
370 if (bio->bi_vcnt != 1 ||
373 /* This is a one page bio that upper layers
374 * refuse to split for us, so we need to split it.
376 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
377 if (raid0_make_request(q, &bp->bio1))
378 generic_make_request(&bp->bio1);
379 if (raid0_make_request(q, &bp->bio2))
380 generic_make_request(&bp->bio2);
382 bio_pair_release(bp);
385 zone = find_zone(conf, sector);
386 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
388 sector_t x = (zone->sectors + sector - zone->zone_end)
391 sector_div(x, zone->nb_dev);
394 x = sector >> chunksect_bits;
395 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
397 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
399 bio->bi_bdev = tmp_dev->bdev;
400 bio->bi_sector = rsect + tmp_dev->data_offset;
403 * Let the main block layer submit the IO and resolve recursion:
408 printk("raid0_make_request bug: can't convert block across chunks"
409 " or bigger than %dk %llu %d\n", chunk_sects / 2,
410 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
416 static void raid0_status (struct seq_file *seq, mddev_t *mddev)
421 char b[BDEVNAME_SIZE];
422 raid0_conf_t *conf = mddev_to_conf(mddev);
425 for (j = 0; j < conf->nr_strip_zones; j++) {
426 seq_printf(seq, " z%d", j);
427 seq_printf(seq, "=[");
428 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
429 seq_printf(seq, "%s/", bdevname(
430 conf->strip_zone[j].dev[k]->bdev,b));
432 seq_printf(seq, "] ze=%d ds=%d s=%d\n",
433 conf->strip_zone[j].zone_end,
434 conf->strip_zone[j].dev_start,
435 conf->strip_zone[j].sectors);
438 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
442 static struct mdk_personality raid0_personality=
446 .owner = THIS_MODULE,
447 .make_request = raid0_make_request,
450 .status = raid0_status,
454 static int __init raid0_init (void)
456 return register_md_personality (&raid0_personality);
459 static void raid0_exit (void)
461 unregister_md_personality (&raid0_personality);
464 module_init(raid0_init);
465 module_exit(raid0_exit);
466 MODULE_LICENSE("GPL");
467 MODULE_ALIAS("md-personality-2"); /* RAID0 */
468 MODULE_ALIAS("md-raid0");
469 MODULE_ALIAS("md-level-0");