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)
258 if (mddev->chunk_size == 0) {
259 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
262 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
264 mddev->chunk_size >> 9,
265 (mddev->chunk_size>>1)-1);
266 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
267 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
268 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
270 conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
273 mddev->private = (void *)conf;
275 conf->strip_zone = NULL;
276 conf->devlist = NULL;
277 if (create_strip_zones (mddev))
280 /* calculate array device size */
281 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
283 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
284 (unsigned long long)mddev->array_sectors);
285 /* calculate the max read-ahead size.
286 * For read-ahead of large files to be effective, we need to
287 * readahead at least twice a whole stripe. i.e. number of devices
288 * multiplied by chunk size times 2.
289 * If an individual device has an ra_pages greater than the
290 * chunk size, then we will not drive that device as hard as it
291 * wants. We consider this a configuration error: a larger
292 * chunksize should be used in that case.
295 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
296 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
297 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
301 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
305 kfree(conf->strip_zone);
306 kfree(conf->devlist);
308 mddev->private = NULL;
313 static int raid0_stop (mddev_t *mddev)
315 raid0_conf_t *conf = mddev_to_conf(mddev);
317 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
318 kfree(conf->strip_zone);
319 conf->strip_zone = NULL;
321 mddev->private = NULL;
326 /* Find the zone which holds a particular offset */
327 static struct strip_zone *find_zone(struct raid0_private_data *conf,
331 struct strip_zone *z = conf->strip_zone;
333 for (i = 0; i < conf->nr_strip_zones; i++)
334 if (sector < z[i].zone_end)
339 static int raid0_make_request (struct request_queue *q, struct bio *bio)
341 mddev_t *mddev = q->queuedata;
342 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
343 raid0_conf_t *conf = mddev_to_conf(mddev);
344 struct strip_zone *zone;
347 sector_t sector, rsect;
348 const int rw = bio_data_dir(bio);
351 if (unlikely(bio_barrier(bio))) {
352 bio_endio(bio, -EOPNOTSUPP);
356 cpu = part_stat_lock();
357 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
358 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
362 chunk_sects = mddev->chunk_size >> 9;
363 chunksect_bits = ffz(~chunk_sects);
364 sector = bio->bi_sector;
366 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
368 /* Sanity check -- queue functions should prevent this happening */
369 if (bio->bi_vcnt != 1 ||
372 /* This is a one page bio that upper layers
373 * refuse to split for us, so we need to split it.
375 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
376 if (raid0_make_request(q, &bp->bio1))
377 generic_make_request(&bp->bio1);
378 if (raid0_make_request(q, &bp->bio2))
379 generic_make_request(&bp->bio2);
381 bio_pair_release(bp);
384 zone = find_zone(conf, sector);
385 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
387 sector_t x = (zone->sectors + sector - zone->zone_end)
390 sector_div(x, zone->nb_dev);
393 x = sector >> chunksect_bits;
394 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
396 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
398 bio->bi_bdev = tmp_dev->bdev;
399 bio->bi_sector = rsect + tmp_dev->data_offset;
402 * Let the main block layer submit the IO and resolve recursion:
407 printk("raid0_make_request bug: can't convert block across chunks"
408 " or bigger than %dk %llu %d\n", chunk_sects / 2,
409 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
415 static void raid0_status (struct seq_file *seq, mddev_t *mddev)
420 char b[BDEVNAME_SIZE];
421 raid0_conf_t *conf = mddev_to_conf(mddev);
424 for (j = 0; j < conf->nr_strip_zones; j++) {
425 seq_printf(seq, " z%d", j);
426 seq_printf(seq, "=[");
427 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
428 seq_printf(seq, "%s/", bdevname(
429 conf->strip_zone[j].dev[k]->bdev,b));
431 seq_printf(seq, "] ze=%d ds=%d s=%d\n",
432 conf->strip_zone[j].zone_end,
433 conf->strip_zone[j].dev_start,
434 conf->strip_zone[j].sectors);
437 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
441 static struct mdk_personality raid0_personality=
445 .owner = THIS_MODULE,
446 .make_request = raid0_make_request,
449 .status = raid0_status,
453 static int __init raid0_init (void)
455 return register_md_personality (&raid0_personality);
458 static void raid0_exit (void)
460 unregister_md_personality (&raid0_personality);
463 module_init(raid0_init);
464 module_exit(raid0_exit);
465 MODULE_LICENSE("GPL");
466 MODULE_ALIAS("md-personality-2"); /* RAID0 */
467 MODULE_ALIAS("md-raid0");
468 MODULE_ALIAS("md-level-0");