md: raid0: make hash_spacing and preshift sector-based.
[linux-2.6.git] / drivers / md / raid0.c
blob90f5b24f6e6cf6e2d314409ca89a9f0a44a6b194
1 /*
2 raid0.c : Multiple Devices driver for Linux
3 Copyright (C) 1994-96 Marc ZYNGIER
4 <zyngier@ufr-info-p7.ibp.fr> or
5 <maz@gloups.fdn.fr>
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)
14 any later version.
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/raid/raid0.h>
23 static void raid0_unplug(struct request_queue *q)
25 mddev_t *mddev = q->queuedata;
26 raid0_conf_t *conf = mddev_to_conf(mddev);
27 mdk_rdev_t **devlist = conf->strip_zone[0].dev;
28 int i;
30 for (i=0; i<mddev->raid_disks; i++) {
31 struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
33 blk_unplug(r_queue);
37 static int raid0_congested(void *data, int bits)
39 mddev_t *mddev = data;
40 raid0_conf_t *conf = mddev_to_conf(mddev);
41 mdk_rdev_t **devlist = conf->strip_zone[0].dev;
42 int i, ret = 0;
44 for (i = 0; i < mddev->raid_disks && !ret ; i++) {
45 struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
47 ret |= bdi_congested(&q->backing_dev_info, bits);
49 return ret;
53 static int create_strip_zones (mddev_t *mddev)
55 int i, c, j;
56 sector_t current_start, curr_zone_start;
57 sector_t min_spacing;
58 raid0_conf_t *conf = mddev_to_conf(mddev);
59 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
60 struct list_head *tmp1, *tmp2;
61 struct strip_zone *zone;
62 int cnt;
63 char b[BDEVNAME_SIZE];
66 * The number of 'same size groups'
68 conf->nr_strip_zones = 0;
70 rdev_for_each(rdev1, tmp1, mddev) {
71 printk(KERN_INFO "raid0: looking at %s\n",
72 bdevname(rdev1->bdev,b));
73 c = 0;
74 rdev_for_each(rdev2, tmp2, mddev) {
75 printk(KERN_INFO "raid0: comparing %s(%llu)",
76 bdevname(rdev1->bdev,b),
77 (unsigned long long)rdev1->size);
78 printk(KERN_INFO " with %s(%llu)\n",
79 bdevname(rdev2->bdev,b),
80 (unsigned long long)rdev2->size);
81 if (rdev2 == rdev1) {
82 printk(KERN_INFO "raid0: END\n");
83 break;
85 if (rdev2->size == rdev1->size)
88 * Not unique, don't count it as a new
89 * group
91 printk(KERN_INFO "raid0: EQUAL\n");
92 c = 1;
93 break;
95 printk(KERN_INFO "raid0: NOT EQUAL\n");
97 if (!c) {
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)
109 return 1;
110 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
111 conf->nr_strip_zones*mddev->raid_disks,
112 GFP_KERNEL);
113 if (!conf->devlist)
114 return 1;
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];
120 cnt = 0;
121 smallest = NULL;
122 zone->dev = conf->devlist;
123 rdev_for_each(rdev1, tmp1, mddev) {
124 int j = rdev1->raid_disk;
126 if (j < 0 || j >= mddev->raid_disks) {
127 printk(KERN_ERR "raid0: bad disk number %d - "
128 "aborting!\n", j);
129 goto abort;
131 if (zone->dev[j]) {
132 printk(KERN_ERR "raid0: multiple devices for %d - "
133 "aborting!\n", j);
134 goto abort;
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 mddev->queue->max_sectors > (PAGE_SIZE>>9))
147 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
149 if (!smallest || (rdev1->size <smallest->size))
150 smallest = rdev1;
151 cnt++;
153 if (cnt != mddev->raid_disks) {
154 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
155 "aborting!\n", cnt, mddev->raid_disks);
156 goto abort;
158 zone->nb_dev = cnt;
159 zone->sectors = smallest->size * cnt * 2;
160 zone->zone_start = 0;
162 current_start = smallest->size * 2;
163 curr_zone_start = zone->sectors;
165 /* now do the other zones */
166 for (i = 1; i < conf->nr_strip_zones; i++)
168 zone = conf->strip_zone + i;
169 zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
171 printk(KERN_INFO "raid0: zone %d\n", i);
172 zone->dev_start = current_start;
173 smallest = NULL;
174 c = 0;
176 for (j=0; j<cnt; j++) {
177 char b[BDEVNAME_SIZE];
178 rdev = conf->strip_zone[0].dev[j];
179 printk(KERN_INFO "raid0: checking %s ...",
180 bdevname(rdev->bdev, b));
181 if (rdev->size > current_start / 2) {
182 printk(KERN_INFO " contained as device %d\n",
184 zone->dev[c] = rdev;
185 c++;
186 if (!smallest || (rdev->size <smallest->size)) {
187 smallest = rdev;
188 printk(KERN_INFO " (%llu) is smallest!.\n",
189 (unsigned long long)rdev->size);
191 } else
192 printk(KERN_INFO " nope.\n");
195 zone->nb_dev = c;
196 zone->sectors = (smallest->size * 2 - current_start) * c;
197 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
198 zone->nb_dev, (unsigned long long)zone->sectors);
200 zone->zone_start = curr_zone_start;
201 curr_zone_start += zone->sectors;
203 current_start = smallest->size * 2;
204 printk(KERN_INFO "raid0: current zone start: %llu\n",
205 (unsigned long long)current_start);
208 /* Now find appropriate hash spacing.
209 * We want a number which causes most hash entries to cover
210 * at most two strips, but the hash table must be at most
211 * 1 PAGE. We choose the smallest strip, or contiguous collection
212 * of strips, that has big enough size. We never consider the last
213 * strip though as it's size has no bearing on the efficacy of the hash
214 * table.
216 conf->spacing = curr_zone_start;
217 min_spacing = curr_zone_start;
218 sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*));
219 for (i=0; i < conf->nr_strip_zones-1; i++) {
220 sector_t s = 0;
221 for (j = i; j < conf->nr_strip_zones - 1 &&
222 s < min_spacing; j++)
223 s += conf->strip_zone[j].sectors;
224 if (s >= min_spacing && s < conf->spacing)
225 conf->spacing = s;
228 mddev->queue->unplug_fn = raid0_unplug;
230 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
231 mddev->queue->backing_dev_info.congested_data = mddev;
233 printk(KERN_INFO "raid0: done.\n");
234 return 0;
235 abort:
236 return 1;
240 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
241 * @q: request queue
242 * @bvm: properties of new bio
243 * @biovec: the request that could be merged to it.
245 * Return amount of bytes we can accept at this offset
247 static int raid0_mergeable_bvec(struct request_queue *q,
248 struct bvec_merge_data *bvm,
249 struct bio_vec *biovec)
251 mddev_t *mddev = q->queuedata;
252 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
253 int max;
254 unsigned int chunk_sectors = mddev->chunk_size >> 9;
255 unsigned int bio_sectors = bvm->bi_size >> 9;
257 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
258 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
259 if (max <= biovec->bv_len && bio_sectors == 0)
260 return biovec->bv_len;
261 else
262 return max;
265 static int raid0_run (mddev_t *mddev)
267 unsigned cur=0, i=0, nb_zone;
268 s64 sectors;
269 raid0_conf_t *conf;
270 mdk_rdev_t *rdev;
271 struct list_head *tmp;
273 if (mddev->chunk_size == 0) {
274 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
275 return -EINVAL;
277 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
278 mdname(mddev),
279 mddev->chunk_size >> 9,
280 (mddev->chunk_size>>1)-1);
281 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
282 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
283 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
285 conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
286 if (!conf)
287 goto out;
288 mddev->private = (void *)conf;
290 conf->strip_zone = NULL;
291 conf->devlist = NULL;
292 if (create_strip_zones (mddev))
293 goto out_free_conf;
295 /* calculate array device size */
296 mddev->array_sectors = 0;
297 rdev_for_each(rdev, tmp, mddev)
298 mddev->array_sectors += rdev->size * 2;
300 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
301 (unsigned long long)mddev->array_sectors);
302 printk(KERN_INFO "raid0 : conf->spacing is %llu sectors.\n",
303 (unsigned long long)conf->spacing);
305 sector_t s = mddev->array_sectors;
306 sector_t space = conf->spacing;
307 int round;
308 conf->sector_shift = 0;
309 if (sizeof(sector_t) > sizeof(u32)) {
310 /*shift down space and s so that sector_div will work */
311 while (space > (sector_t) (~(u32)0)) {
312 s >>= 1;
313 space >>= 1;
314 s += 1; /* force round-up */
315 conf->sector_shift++;
318 round = sector_div(s, (u32)space) ? 1 : 0;
319 nb_zone = s + round;
321 printk(KERN_INFO "raid0 : nb_zone is %d.\n", nb_zone);
323 printk(KERN_INFO "raid0 : Allocating %zu bytes for hash.\n",
324 nb_zone*sizeof(struct strip_zone*));
325 conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL);
326 if (!conf->hash_table)
327 goto out_free_conf;
328 sectors = conf->strip_zone[cur].sectors;
330 conf->hash_table[0] = conf->strip_zone + cur;
331 for (i=1; i< nb_zone; i++) {
332 while (sectors <= conf->spacing) {
333 cur++;
334 sectors += conf->strip_zone[cur].sectors;
336 sectors -= conf->spacing;
337 conf->hash_table[i] = conf->strip_zone + cur;
339 if (conf->sector_shift) {
340 conf->spacing >>= conf->sector_shift;
341 /* round spacing up so when we divide by it, we
342 * err on the side of too-low, which is safest
344 conf->spacing++;
347 /* calculate the max read-ahead size.
348 * For read-ahead of large files to be effective, we need to
349 * readahead at least twice a whole stripe. i.e. number of devices
350 * multiplied by chunk size times 2.
351 * If an individual device has an ra_pages greater than the
352 * chunk size, then we will not drive that device as hard as it
353 * wants. We consider this a configuration error: a larger
354 * chunksize should be used in that case.
357 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
358 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
359 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
363 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
364 return 0;
366 out_free_conf:
367 kfree(conf->strip_zone);
368 kfree(conf->devlist);
369 kfree(conf);
370 mddev->private = NULL;
371 out:
372 return -ENOMEM;
375 static int raid0_stop (mddev_t *mddev)
377 raid0_conf_t *conf = mddev_to_conf(mddev);
379 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
380 kfree(conf->hash_table);
381 conf->hash_table = NULL;
382 kfree(conf->strip_zone);
383 conf->strip_zone = NULL;
384 kfree(conf);
385 mddev->private = NULL;
387 return 0;
390 static int raid0_make_request (struct request_queue *q, struct bio *bio)
392 mddev_t *mddev = q->queuedata;
393 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
394 raid0_conf_t *conf = mddev_to_conf(mddev);
395 struct strip_zone *zone;
396 mdk_rdev_t *tmp_dev;
397 sector_t chunk;
398 sector_t sector, rsect;
399 const int rw = bio_data_dir(bio);
400 int cpu;
402 if (unlikely(bio_barrier(bio))) {
403 bio_endio(bio, -EOPNOTSUPP);
404 return 0;
407 cpu = part_stat_lock();
408 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
409 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
410 bio_sectors(bio));
411 part_stat_unlock();
413 chunk_sects = mddev->chunk_size >> 9;
414 chunksect_bits = ffz(~chunk_sects);
415 sector = bio->bi_sector;
417 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
418 struct bio_pair *bp;
419 /* Sanity check -- queue functions should prevent this happening */
420 if (bio->bi_vcnt != 1 ||
421 bio->bi_idx != 0)
422 goto bad_map;
423 /* This is a one page bio that upper layers
424 * refuse to split for us, so we need to split it.
426 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
427 if (raid0_make_request(q, &bp->bio1))
428 generic_make_request(&bp->bio1);
429 if (raid0_make_request(q, &bp->bio2))
430 generic_make_request(&bp->bio2);
432 bio_pair_release(bp);
433 return 0;
438 sector_t x = sector >> conf->sector_shift;
439 sector_div(x, (u32)conf->spacing);
440 zone = conf->hash_table[x];
443 while (sector >= zone->zone_start + zone->sectors)
444 zone++;
446 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
450 sector_t x = (sector - zone->zone_start) >> chunksect_bits;
452 sector_div(x, zone->nb_dev);
453 chunk = x;
455 x = sector >> chunksect_bits;
456 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
458 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
460 bio->bi_bdev = tmp_dev->bdev;
461 bio->bi_sector = rsect + tmp_dev->data_offset;
464 * Let the main block layer submit the IO and resolve recursion:
466 return 1;
468 bad_map:
469 printk("raid0_make_request bug: can't convert block across chunks"
470 " or bigger than %dk %llu %d\n", chunk_sects / 2,
471 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
473 bio_io_error(bio);
474 return 0;
477 static void raid0_status (struct seq_file *seq, mddev_t *mddev)
479 #undef MD_DEBUG
480 #ifdef MD_DEBUG
481 int j, k, h;
482 char b[BDEVNAME_SIZE];
483 raid0_conf_t *conf = mddev_to_conf(mddev);
485 h = 0;
486 for (j = 0; j < conf->nr_strip_zones; j++) {
487 seq_printf(seq, " z%d", j);
488 if (conf->hash_table[h] == conf->strip_zone+j)
489 seq_printf(seq, "(h%d)", h++);
490 seq_printf(seq, "=[");
491 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
492 seq_printf(seq, "%s/", bdevname(
493 conf->strip_zone[j].dev[k]->bdev,b));
495 seq_printf(seq, "] zs=%d ds=%d s=%d\n",
496 conf->strip_zone[j].zone_start,
497 conf->strip_zone[j].dev_start,
498 conf->strip_zone[j].sectors);
500 #endif
501 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
502 return;
505 static struct mdk_personality raid0_personality=
507 .name = "raid0",
508 .level = 0,
509 .owner = THIS_MODULE,
510 .make_request = raid0_make_request,
511 .run = raid0_run,
512 .stop = raid0_stop,
513 .status = raid0_status,
516 static int __init raid0_init (void)
518 return register_md_personality (&raid0_personality);
521 static void raid0_exit (void)
523 unregister_md_personality (&raid0_personality);
526 module_init(raid0_init);
527 module_exit(raid0_exit);
528 MODULE_LICENSE("GPL");
529 MODULE_ALIAS("md-personality-2"); /* RAID0 */
530 MODULE_ALIAS("md-raid0");
531 MODULE_ALIAS("md-level-0");