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;
60 raid0_conf_t *conf = mddev_to_conf(mddev);
61 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
62 struct strip_zone *zone;
64 char b[BDEVNAME_SIZE];
67 * The number of 'same size groups'
69 conf->nr_strip_zones = 0;
71 list_for_each_entry(rdev1, &mddev->disks, same_set) {
72 printk(KERN_INFO "raid0: looking at %s\n",
73 bdevname(rdev1->bdev,b));
75 list_for_each_entry(rdev2, &mddev->disks, same_set) {
76 printk(KERN_INFO "raid0: comparing %s(%llu)",
77 bdevname(rdev1->bdev,b),
78 (unsigned long long)rdev1->sectors);
79 printk(KERN_INFO " with %s(%llu)\n",
80 bdevname(rdev2->bdev,b),
81 (unsigned long long)rdev2->sectors);
83 printk(KERN_INFO "raid0: END\n");
86 if (rdev2->sectors == rdev1->sectors) {
88 * Not unique, don't count it as a new
91 printk(KERN_INFO "raid0: EQUAL\n");
95 printk(KERN_INFO "raid0: NOT EQUAL\n");
98 printk(KERN_INFO "raid0: ==> UNIQUE\n");
99 conf->nr_strip_zones++;
100 printk(KERN_INFO "raid0: %d zones\n",
101 conf->nr_strip_zones);
104 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
106 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
107 conf->nr_strip_zones, GFP_KERNEL);
108 if (!conf->strip_zone)
110 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
111 conf->nr_strip_zones*mddev->raid_disks,
116 /* The first zone must contain all devices, so here we check that
117 * there is a proper alignment of slots to devices and find them all
119 zone = &conf->strip_zone[0];
122 zone->dev = conf->devlist;
123 list_for_each_entry(rdev1, &mddev->disks, same_set) {
124 int j = rdev1->raid_disk;
126 if (j < 0 || j >= mddev->raid_disks) {
127 printk(KERN_ERR "raid0: bad disk number %d - "
132 printk(KERN_ERR "raid0: multiple devices for %d - "
136 zone->dev[j] = rdev1;
138 blk_queue_stack_limits(mddev->queue,
139 rdev1->bdev->bd_disk->queue);
140 /* as we don't honour merge_bvec_fn, we must never risk
141 * violating it, so limit ->max_sector to one PAGE, as
142 * a one page request is never in violation.
145 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
146 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
147 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
149 if (!smallest || (rdev1->sectors < smallest->sectors))
153 if (cnt != mddev->raid_disks) {
154 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
155 "aborting!\n", cnt, mddev->raid_disks);
159 zone->sectors = smallest->sectors * cnt;
160 zone->zone_end = zone->sectors;
162 curr_zone_end = zone->sectors;
164 /* now do the other zones */
165 for (i = 1; i < conf->nr_strip_zones; i++)
167 zone = conf->strip_zone + i;
168 zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
170 printk(KERN_INFO "raid0: zone %d\n", i);
171 zone->dev_start = smallest->sectors;
175 for (j=0; j<cnt; j++) {
176 char b[BDEVNAME_SIZE];
177 rdev = conf->strip_zone[0].dev[j];
178 printk(KERN_INFO "raid0: checking %s ...",
179 bdevname(rdev->bdev, b));
180 if (rdev->sectors <= zone->dev_start) {
181 printk(KERN_INFO " nope.\n");
184 printk(KERN_INFO " contained as device %d\n", c);
187 if (!smallest || rdev->sectors < smallest->sectors) {
189 printk(KERN_INFO " (%llu) is smallest!.\n",
190 (unsigned long long)rdev->sectors);
195 zone->sectors = (smallest->sectors - zone->dev_start) * c;
196 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
197 zone->nb_dev, (unsigned long long)zone->sectors);
199 curr_zone_end += zone->sectors;
200 zone->zone_end = curr_zone_end;
202 printk(KERN_INFO "raid0: current zone start: %llu\n",
203 (unsigned long long)smallest->sectors);
205 /* Now find appropriate hash spacing.
206 * We want a number which causes most hash entries to cover
207 * at most two strips, but the hash table must be at most
208 * 1 PAGE. We choose the smallest strip, or contiguous collection
209 * of strips, that has big enough size. We never consider the last
210 * strip though as it's size has no bearing on the efficacy of the hash
213 conf->spacing = curr_zone_end;
214 min_spacing = curr_zone_end;
215 sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*));
216 for (i=0; i < conf->nr_strip_zones-1; i++) {
218 for (j = i; j < conf->nr_strip_zones - 1 &&
219 s < min_spacing; j++)
220 s += conf->strip_zone[j].sectors;
221 if (s >= min_spacing && s < conf->spacing)
225 mddev->queue->unplug_fn = raid0_unplug;
227 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
228 mddev->queue->backing_dev_info.congested_data = mddev;
230 printk(KERN_INFO "raid0: done.\n");
237 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
239 * @bvm: properties of new bio
240 * @biovec: the request that could be merged to it.
242 * Return amount of bytes we can accept at this offset
244 static int raid0_mergeable_bvec(struct request_queue *q,
245 struct bvec_merge_data *bvm,
246 struct bio_vec *biovec)
248 mddev_t *mddev = q->queuedata;
249 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
251 unsigned int chunk_sectors = mddev->chunk_size >> 9;
252 unsigned int bio_sectors = bvm->bi_size >> 9;
254 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
255 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
256 if (max <= biovec->bv_len && bio_sectors == 0)
257 return biovec->bv_len;
262 static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
264 sector_t array_sectors = 0;
267 WARN_ONCE(sectors || raid_disks,
268 "%s does not support generic reshape\n", __func__);
270 list_for_each_entry(rdev, &mddev->disks, same_set)
271 array_sectors += rdev->sectors;
273 return array_sectors;
276 static int raid0_run (mddev_t *mddev)
278 unsigned cur=0, i=0, nb_zone;
282 if (mddev->chunk_size == 0) {
283 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
286 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
288 mddev->chunk_size >> 9,
289 (mddev->chunk_size>>1)-1);
290 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
291 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
292 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
294 conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
297 mddev->private = (void *)conf;
299 conf->strip_zone = NULL;
300 conf->devlist = NULL;
301 if (create_strip_zones (mddev))
304 /* calculate array device size */
305 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
307 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
308 (unsigned long long)mddev->array_sectors);
309 printk(KERN_INFO "raid0 : conf->spacing is %llu sectors.\n",
310 (unsigned long long)conf->spacing);
312 sector_t s = raid0_size(mddev, 0, 0);
313 sector_t space = conf->spacing;
315 conf->sector_shift = 0;
316 if (sizeof(sector_t) > sizeof(u32)) {
317 /*shift down space and s so that sector_div will work */
318 while (space > (sector_t) (~(u32)0)) {
321 s += 1; /* force round-up */
322 conf->sector_shift++;
325 round = sector_div(s, (u32)space) ? 1 : 0;
328 printk(KERN_INFO "raid0 : nb_zone is %d.\n", nb_zone);
330 printk(KERN_INFO "raid0 : Allocating %zu bytes for hash.\n",
331 nb_zone*sizeof(struct strip_zone*));
332 conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL);
333 if (!conf->hash_table)
335 sectors = conf->strip_zone[cur].sectors;
337 conf->hash_table[0] = conf->strip_zone + cur;
338 for (i=1; i< nb_zone; i++) {
339 while (sectors <= conf->spacing) {
341 sectors += conf->strip_zone[cur].sectors;
343 sectors -= conf->spacing;
344 conf->hash_table[i] = conf->strip_zone + cur;
346 if (conf->sector_shift) {
347 conf->spacing >>= conf->sector_shift;
348 /* round spacing up so when we divide by it, we
349 * err on the side of too-low, which is safest
354 /* calculate the max read-ahead size.
355 * For read-ahead of large files to be effective, we need to
356 * readahead at least twice a whole stripe. i.e. number of devices
357 * multiplied by chunk size times 2.
358 * If an individual device has an ra_pages greater than the
359 * chunk size, then we will not drive that device as hard as it
360 * wants. We consider this a configuration error: a larger
361 * chunksize should be used in that case.
364 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
365 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
366 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
370 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
374 kfree(conf->strip_zone);
375 kfree(conf->devlist);
377 mddev->private = NULL;
382 static int raid0_stop (mddev_t *mddev)
384 raid0_conf_t *conf = mddev_to_conf(mddev);
386 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
387 kfree(conf->hash_table);
388 conf->hash_table = NULL;
389 kfree(conf->strip_zone);
390 conf->strip_zone = NULL;
392 mddev->private = NULL;
397 /* Find the zone which holds a particular offset */
398 static struct strip_zone *find_zone(struct raid0_private_data *conf,
402 struct strip_zone *z = conf->strip_zone;
404 for (i = 0; i < conf->nr_strip_zones; i++)
405 if (sector < z[i].zone_end)
410 static int raid0_make_request (struct request_queue *q, struct bio *bio)
412 mddev_t *mddev = q->queuedata;
413 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
414 raid0_conf_t *conf = mddev_to_conf(mddev);
415 struct strip_zone *zone;
418 sector_t sector, rsect;
419 const int rw = bio_data_dir(bio);
422 if (unlikely(bio_barrier(bio))) {
423 bio_endio(bio, -EOPNOTSUPP);
427 cpu = part_stat_lock();
428 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
429 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
433 chunk_sects = mddev->chunk_size >> 9;
434 chunksect_bits = ffz(~chunk_sects);
435 sector = bio->bi_sector;
437 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
439 /* Sanity check -- queue functions should prevent this happening */
440 if (bio->bi_vcnt != 1 ||
443 /* This is a one page bio that upper layers
444 * refuse to split for us, so we need to split it.
446 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
447 if (raid0_make_request(q, &bp->bio1))
448 generic_make_request(&bp->bio1);
449 if (raid0_make_request(q, &bp->bio2))
450 generic_make_request(&bp->bio2);
452 bio_pair_release(bp);
455 zone = find_zone(conf, sector);
456 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
458 sector_t x = (zone->sectors + sector - zone->zone_end)
461 sector_div(x, zone->nb_dev);
464 x = sector >> chunksect_bits;
465 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
467 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
469 bio->bi_bdev = tmp_dev->bdev;
470 bio->bi_sector = rsect + tmp_dev->data_offset;
473 * Let the main block layer submit the IO and resolve recursion:
478 printk("raid0_make_request bug: can't convert block across chunks"
479 " or bigger than %dk %llu %d\n", chunk_sects / 2,
480 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
486 static void raid0_status (struct seq_file *seq, mddev_t *mddev)
491 char b[BDEVNAME_SIZE];
492 raid0_conf_t *conf = mddev_to_conf(mddev);
495 for (j = 0; j < conf->nr_strip_zones; j++) {
496 seq_printf(seq, " z%d", j);
497 if (conf->hash_table[h] == conf->strip_zone+j)
498 seq_printf(seq, "(h%d)", h++);
499 seq_printf(seq, "=[");
500 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
501 seq_printf(seq, "%s/", bdevname(
502 conf->strip_zone[j].dev[k]->bdev,b));
504 seq_printf(seq, "] ze=%d ds=%d s=%d\n",
505 conf->strip_zone[j].zone_end,
506 conf->strip_zone[j].dev_start,
507 conf->strip_zone[j].sectors);
510 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
514 static struct mdk_personality raid0_personality=
518 .owner = THIS_MODULE,
519 .make_request = raid0_make_request,
522 .status = raid0_status,
526 static int __init raid0_init (void)
528 return register_md_personality (&raid0_personality);
531 static void raid0_exit (void)
533 unregister_md_personality (&raid0_personality);
536 module_init(raid0_init);
537 module_exit(raid0_exit);
538 MODULE_LICENSE("GPL");
539 MODULE_ALIAS("md-personality-2"); /* RAID0 */
540 MODULE_ALIAS("md-raid0");
541 MODULE_ALIAS("md-level-0");