md: fix some comments.
[linux-2.6-block.git] / drivers / md / raid0.c
CommitLineData
1da177e4
LT
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
7
8
9 RAID-0 management functions.
10
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.
15
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.
19*/
20
bff61975 21#include <linux/blkdev.h>
bff61975 22#include <linux/seq_file.h>
43b2e5d8 23#include "md.h"
ef740c37 24#include "raid0.h"
1da177e4 25
165125e1 26static void raid0_unplug(struct request_queue *q)
1da177e4
LT
27{
28 mddev_t *mddev = q->queuedata;
070ec55d 29 raid0_conf_t *conf = mddev->private;
b414579f 30 mdk_rdev_t **devlist = conf->devlist;
1da177e4
LT
31 int i;
32
33 for (i=0; i<mddev->raid_disks; i++) {
165125e1 34 struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
1da177e4 35
2ad8b1ef 36 blk_unplug(r_queue);
1da177e4
LT
37 }
38}
39
26be34dc
N
40static int raid0_congested(void *data, int bits)
41{
42 mddev_t *mddev = data;
070ec55d 43 raid0_conf_t *conf = mddev->private;
b414579f 44 mdk_rdev_t **devlist = conf->devlist;
26be34dc
N
45 int i, ret = 0;
46
47 for (i = 0; i < mddev->raid_disks && !ret ; i++) {
165125e1 48 struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
26be34dc
N
49
50 ret |= bdi_congested(&q->backing_dev_info, bits);
51 }
52 return ret;
53}
54
46994191 55/*
56 * inform the user of the raid configuration
57*/
58static void dump_zones(mddev_t *mddev)
59{
60 int j, k, h;
61 sector_t zone_size = 0;
62 sector_t zone_start = 0;
63 char b[BDEVNAME_SIZE];
64 raid0_conf_t *conf = mddev->private;
65 printk(KERN_INFO "******* %s configuration *********\n",
66 mdname(mddev));
67 h = 0;
68 for (j = 0; j < conf->nr_strip_zones; j++) {
69 printk(KERN_INFO "zone%d=[", j);
70 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
71 printk("%s/",
72 bdevname(conf->devlist[j*mddev->raid_disks
73 + k]->bdev, b));
74 printk("]\n");
75
76 zone_size = conf->strip_zone[j].zone_end - zone_start;
77 printk(KERN_INFO " zone offset=%llukb "
78 "device offset=%llukb size=%llukb\n",
79 (unsigned long long)zone_start>>1,
80 (unsigned long long)conf->strip_zone[j].dev_start>>1,
81 (unsigned long long)zone_size>>1);
82 zone_start = conf->strip_zone[j].zone_end;
83 }
84 printk(KERN_INFO "**********************************\n\n");
85}
86
ed7b0038 87static int create_strip_zones(mddev_t *mddev)
1da177e4 88{
ed7b0038 89 int i, c, j, err;
49f357a2 90 sector_t curr_zone_end, sectors;
b414579f 91 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev, **dev;
1da177e4
LT
92 struct strip_zone *zone;
93 int cnt;
94 char b[BDEVNAME_SIZE];
ed7b0038
AN
95 raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
96
97 if (!conf)
98 return -ENOMEM;
159ec1fc 99 list_for_each_entry(rdev1, &mddev->disks, same_set) {
0825b87a 100 printk(KERN_INFO "raid0: looking at %s\n",
1da177e4
LT
101 bdevname(rdev1->bdev,b));
102 c = 0;
159ec1fc 103 list_for_each_entry(rdev2, &mddev->disks, same_set) {
0825b87a 104 printk(KERN_INFO "raid0: comparing %s(%llu)",
1da177e4 105 bdevname(rdev1->bdev,b),
dd8ac336 106 (unsigned long long)rdev1->sectors);
0825b87a 107 printk(KERN_INFO " with %s(%llu)\n",
1da177e4 108 bdevname(rdev2->bdev,b),
dd8ac336 109 (unsigned long long)rdev2->sectors);
1da177e4 110 if (rdev2 == rdev1) {
0825b87a 111 printk(KERN_INFO "raid0: END\n");
1da177e4
LT
112 break;
113 }
dd8ac336 114 if (rdev2->sectors == rdev1->sectors) {
1da177e4
LT
115 /*
116 * Not unique, don't count it as a new
117 * group
118 */
0825b87a 119 printk(KERN_INFO "raid0: EQUAL\n");
1da177e4
LT
120 c = 1;
121 break;
122 }
0825b87a 123 printk(KERN_INFO "raid0: NOT EQUAL\n");
1da177e4
LT
124 }
125 if (!c) {
0825b87a 126 printk(KERN_INFO "raid0: ==> UNIQUE\n");
1da177e4 127 conf->nr_strip_zones++;
0825b87a
AN
128 printk(KERN_INFO "raid0: %d zones\n",
129 conf->nr_strip_zones);
1da177e4
LT
130 }
131 }
0825b87a 132 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
ed7b0038 133 err = -ENOMEM;
9ffae0cf 134 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
1da177e4
LT
135 conf->nr_strip_zones, GFP_KERNEL);
136 if (!conf->strip_zone)
ed7b0038 137 goto abort;
9ffae0cf 138 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
1da177e4
LT
139 conf->nr_strip_zones*mddev->raid_disks,
140 GFP_KERNEL);
141 if (!conf->devlist)
ed7b0038 142 goto abort;
1da177e4 143
1da177e4
LT
144 /* The first zone must contain all devices, so here we check that
145 * there is a proper alignment of slots to devices and find them all
146 */
147 zone = &conf->strip_zone[0];
148 cnt = 0;
149 smallest = NULL;
b414579f 150 dev = conf->devlist;
ed7b0038 151 err = -EINVAL;
159ec1fc 152 list_for_each_entry(rdev1, &mddev->disks, same_set) {
1da177e4
LT
153 int j = rdev1->raid_disk;
154
155 if (j < 0 || j >= mddev->raid_disks) {
0825b87a
AN
156 printk(KERN_ERR "raid0: bad disk number %d - "
157 "aborting!\n", j);
1da177e4
LT
158 goto abort;
159 }
b414579f 160 if (dev[j]) {
0825b87a
AN
161 printk(KERN_ERR "raid0: multiple devices for %d - "
162 "aborting!\n", j);
1da177e4
LT
163 goto abort;
164 }
b414579f 165 dev[j] = rdev1;
1da177e4
LT
166
167 blk_queue_stack_limits(mddev->queue,
168 rdev1->bdev->bd_disk->queue);
169 /* as we don't honour merge_bvec_fn, we must never risk
170 * violating it, so limit ->max_sector to one PAGE, as
171 * a one page request is never in violation.
172 */
173
174 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
ae03bf63 175 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
1da177e4
LT
176 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
177
dd8ac336 178 if (!smallest || (rdev1->sectors < smallest->sectors))
1da177e4
LT
179 smallest = rdev1;
180 cnt++;
181 }
182 if (cnt != mddev->raid_disks) {
0825b87a
AN
183 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
184 "aborting!\n", cnt, mddev->raid_disks);
1da177e4
LT
185 goto abort;
186 }
187 zone->nb_dev = cnt;
49f357a2 188 zone->zone_end = smallest->sectors * cnt;
1da177e4 189
49f357a2 190 curr_zone_end = zone->zone_end;
1da177e4
LT
191
192 /* now do the other zones */
193 for (i = 1; i < conf->nr_strip_zones; i++)
194 {
195 zone = conf->strip_zone + i;
b414579f 196 dev = conf->devlist + i * mddev->raid_disks;
1da177e4 197
0825b87a 198 printk(KERN_INFO "raid0: zone %d\n", i);
d27a43ab 199 zone->dev_start = smallest->sectors;
1da177e4
LT
200 smallest = NULL;
201 c = 0;
202
203 for (j=0; j<cnt; j++) {
204 char b[BDEVNAME_SIZE];
b414579f 205 rdev = conf->devlist[j];
0825b87a
AN
206 printk(KERN_INFO "raid0: checking %s ...",
207 bdevname(rdev->bdev, b));
d27a43ab 208 if (rdev->sectors <= zone->dev_start) {
0825b87a 209 printk(KERN_INFO " nope.\n");
dd8ac336
AN
210 continue;
211 }
212 printk(KERN_INFO " contained as device %d\n", c);
b414579f 213 dev[c] = rdev;
dd8ac336
AN
214 c++;
215 if (!smallest || rdev->sectors < smallest->sectors) {
216 smallest = rdev;
217 printk(KERN_INFO " (%llu) is smallest!.\n",
218 (unsigned long long)rdev->sectors);
219 }
1da177e4
LT
220 }
221
222 zone->nb_dev = c;
49f357a2 223 sectors = (smallest->sectors - zone->dev_start) * c;
83838ed8 224 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
49f357a2 225 zone->nb_dev, (unsigned long long)sectors);
1da177e4 226
49f357a2 227 curr_zone_end += sectors;
d27a43ab 228 zone->zone_end = curr_zone_end;
1da177e4 229
6b8796cc 230 printk(KERN_INFO "raid0: current zone start: %llu\n",
d27a43ab 231 (unsigned long long)smallest->sectors);
1da177e4 232 }
1da177e4 233 mddev->queue->unplug_fn = raid0_unplug;
26be34dc
N
234 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
235 mddev->queue->backing_dev_info.congested_data = mddev;
1da177e4 236
92e59b6b 237 /*
238 * now since we have the hard sector sizes, we can make sure
239 * chunk size is a multiple of that sector size
240 */
9d8f0363 241 if ((mddev->chunk_sectors << 9) % queue_logical_block_size(mddev->queue)) {
92e59b6b 242 printk(KERN_ERR "%s chunk_size of %d not valid\n",
243 mdname(mddev),
9d8f0363 244 mddev->chunk_sectors << 9);
92e59b6b 245 goto abort;
246 }
0825b87a 247 printk(KERN_INFO "raid0: done.\n");
ed7b0038 248 mddev->private = conf;
1da177e4 249 return 0;
5568a603 250abort:
ed7b0038
AN
251 kfree(conf->strip_zone);
252 kfree(conf->devlist);
253 kfree(conf);
254 mddev->private = NULL;
255 return err;
1da177e4
LT
256}
257
258/**
259 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
260 * @q: request queue
cc371e66 261 * @bvm: properties of new bio
1da177e4
LT
262 * @biovec: the request that could be merged to it.
263 *
264 * Return amount of bytes we can accept at this offset
265 */
cc371e66
AK
266static int raid0_mergeable_bvec(struct request_queue *q,
267 struct bvec_merge_data *bvm,
268 struct bio_vec *biovec)
1da177e4
LT
269{
270 mddev_t *mddev = q->queuedata;
cc371e66 271 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
1da177e4 272 int max;
9d8f0363 273 unsigned int chunk_sectors = mddev->chunk_sectors;
cc371e66 274 unsigned int bio_sectors = bvm->bi_size >> 9;
1da177e4 275
9d8f0363 276 if (is_power_of_2(mddev->chunk_sectors))
fbb704ef 277 max = (chunk_sectors - ((sector & (chunk_sectors-1))
278 + bio_sectors)) << 9;
279 else
280 max = (chunk_sectors - (sector_div(sector, chunk_sectors)
281 + bio_sectors)) << 9;
1da177e4
LT
282 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
283 if (max <= biovec->bv_len && bio_sectors == 0)
284 return biovec->bv_len;
285 else
286 return max;
287}
288
80c3a6ce
DW
289static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
290{
291 sector_t array_sectors = 0;
292 mdk_rdev_t *rdev;
293
294 WARN_ONCE(sectors || raid_disks,
295 "%s does not support generic reshape\n", __func__);
296
297 list_for_each_entry(rdev, &mddev->disks, same_set)
298 array_sectors += rdev->sectors;
299
300 return array_sectors;
301}
302
8f79cfcd 303static int raid0_run(mddev_t *mddev)
1da177e4 304{
5568a603 305 int ret;
1da177e4 306
9d8f0363 307 if (mddev->chunk_sectors == 0) {
fbb704ef 308 printk(KERN_ERR "md/raid0: chunk size must be set.\n");
2604b703
N
309 return -EINVAL;
310 }
9d8f0363 311 blk_queue_max_sectors(mddev->queue, mddev->chunk_sectors);
e7e72bf6 312 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
1da177e4 313
5568a603
AN
314 ret = create_strip_zones(mddev);
315 if (ret < 0)
ed7b0038 316 return ret;
1da177e4
LT
317
318 /* calculate array device size */
1f403624 319 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
1da177e4 320
ccacc7d2
AN
321 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
322 (unsigned long long)mddev->array_sectors);
1da177e4
LT
323 /* calculate the max read-ahead size.
324 * For read-ahead of large files to be effective, we need to
325 * readahead at least twice a whole stripe. i.e. number of devices
326 * multiplied by chunk size times 2.
327 * If an individual device has an ra_pages greater than the
328 * chunk size, then we will not drive that device as hard as it
329 * wants. We consider this a configuration error: a larger
330 * chunksize should be used in that case.
331 */
332 {
9d8f0363
AN
333 int stripe = mddev->raid_disks *
334 (mddev->chunk_sectors << 9) / PAGE_SIZE;
1da177e4
LT
335 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
336 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
337 }
338
1da177e4 339 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
46994191 340 dump_zones(mddev);
1da177e4 341 return 0;
1da177e4
LT
342}
343
fb5ab4b5 344static int raid0_stop(mddev_t *mddev)
1da177e4 345{
070ec55d 346 raid0_conf_t *conf = mddev->private;
1da177e4
LT
347
348 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
990a8baf 349 kfree(conf->strip_zone);
fb5ab4b5 350 kfree(conf->devlist);
990a8baf 351 kfree(conf);
1da177e4 352 mddev->private = NULL;
1da177e4
LT
353 return 0;
354}
355
49f357a2
N
356/* Find the zone which holds a particular offset
357 * Update *sectorp to be an offset in that zone
358 */
dc582663 359static struct strip_zone *find_zone(struct raid0_private_data *conf,
49f357a2 360 sector_t *sectorp)
dc582663
AN
361{
362 int i;
363 struct strip_zone *z = conf->strip_zone;
49f357a2 364 sector_t sector = *sectorp;
dc582663
AN
365
366 for (i = 0; i < conf->nr_strip_zones; i++)
49f357a2
N
367 if (sector < z[i].zone_end) {
368 if (i)
369 *sectorp = sector - z[i-1].zone_end;
dc582663 370 return z + i;
49f357a2 371 }
dc582663
AN
372 BUG();
373}
374
fbb704ef 375/*
376 * remaps the bio to the target device. we separate two flows.
377 * power 2 flow and a general flow for the sake of perfromance
378*/
379static mdk_rdev_t *map_sector(mddev_t *mddev, struct strip_zone *zone,
380 sector_t sector, sector_t *sector_offset)
1da177e4 381{
fbb704ef 382 unsigned int sect_in_chunk;
383 sector_t chunk;
070ec55d 384 raid0_conf_t *conf = mddev->private;
9d8f0363 385 unsigned int chunk_sects = mddev->chunk_sectors;
fbb704ef 386
9d8f0363 387 if (is_power_of_2(mddev->chunk_sectors)) {
fbb704ef 388 int chunksect_bits = ffz(~chunk_sects);
389 /* find the sector offset inside the chunk */
390 sect_in_chunk = sector & (chunk_sects - 1);
391 sector >>= chunksect_bits;
392 /* chunk in zone */
393 chunk = *sector_offset;
394 /* quotient is the chunk in real device*/
395 sector_div(chunk, zone->nb_dev << chunksect_bits);
396 } else{
397 sect_in_chunk = sector_div(sector, chunk_sects);
398 chunk = *sector_offset;
399 sector_div(chunk, chunk_sects * zone->nb_dev);
400 }
401 /*
402 * position the bio over the real device
403 * real sector = chunk in device + starting of zone
404 * + the position in the chunk
405 */
406 *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
407 return conf->devlist[(zone - conf->strip_zone)*mddev->raid_disks
408 + sector_div(sector, zone->nb_dev)];
409}
410
411/*
412 * Is io distribute over 1 or more chunks ?
413*/
414static inline int is_io_in_chunk_boundary(mddev_t *mddev,
415 unsigned int chunk_sects, struct bio *bio)
416{
9d8f0363 417 if (likely(is_power_of_2(mddev->chunk_sectors))) {
fbb704ef 418 return chunk_sects >= ((bio->bi_sector & (chunk_sects-1))
419 + (bio->bi_size >> 9));
420 } else{
421 sector_t sector = bio->bi_sector;
422 return chunk_sects >= (sector_div(sector, chunk_sects)
423 + (bio->bi_size >> 9));
424 }
425}
426
427static int raid0_make_request(struct request_queue *q, struct bio *bio)
428{
429 mddev_t *mddev = q->queuedata;
430 unsigned int chunk_sects;
431 sector_t sector_offset;
1da177e4
LT
432 struct strip_zone *zone;
433 mdk_rdev_t *tmp_dev;
a362357b 434 const int rw = bio_data_dir(bio);
c9959059 435 int cpu;
1da177e4 436
e5dcdd80 437 if (unlikely(bio_barrier(bio))) {
6712ecf8 438 bio_endio(bio, -EOPNOTSUPP);
e5dcdd80
N
439 return 0;
440 }
441
074a7aca
TH
442 cpu = part_stat_lock();
443 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
444 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
445 bio_sectors(bio));
446 part_stat_unlock();
1da177e4 447
9d8f0363 448 chunk_sects = mddev->chunk_sectors;
fbb704ef 449 if (unlikely(!is_io_in_chunk_boundary(mddev, chunk_sects, bio))) {
450 sector_t sector = bio->bi_sector;
1da177e4
LT
451 struct bio_pair *bp;
452 /* Sanity check -- queue functions should prevent this happening */
453 if (bio->bi_vcnt != 1 ||
454 bio->bi_idx != 0)
455 goto bad_map;
456 /* This is a one page bio that upper layers
457 * refuse to split for us, so we need to split it.
458 */
9d8f0363 459 if (likely(is_power_of_2(mddev->chunk_sectors)))
fbb704ef 460 bp = bio_split(bio, chunk_sects - (sector &
461 (chunk_sects-1)));
462 else
463 bp = bio_split(bio, chunk_sects -
464 sector_div(sector, chunk_sects));
1da177e4
LT
465 if (raid0_make_request(q, &bp->bio1))
466 generic_make_request(&bp->bio1);
467 if (raid0_make_request(q, &bp->bio2))
468 generic_make_request(&bp->bio2);
469
470 bio_pair_release(bp);
471 return 0;
472 }
1da177e4 473
fbb704ef 474 sector_offset = bio->bi_sector;
475 zone = find_zone(mddev->private, &sector_offset);
476 tmp_dev = map_sector(mddev, zone, bio->bi_sector,
477 &sector_offset);
1da177e4 478 bio->bi_bdev = tmp_dev->bdev;
fbb704ef 479 bio->bi_sector = sector_offset + zone->dev_start +
480 tmp_dev->data_offset;
1da177e4
LT
481 /*
482 * Let the main block layer submit the IO and resolve recursion:
483 */
484 return 1;
485
486bad_map:
487 printk("raid0_make_request bug: can't convert block across chunks"
a4712005 488 " or bigger than %dk %llu %d\n", chunk_sects / 2,
1da177e4
LT
489 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
490
6712ecf8 491 bio_io_error(bio);
1da177e4
LT
492 return 0;
493}
8299d7f7 494
1b961429 495static void raid0_status(struct seq_file *seq, mddev_t *mddev)
1da177e4
LT
496{
497#undef MD_DEBUG
498#ifdef MD_DEBUG
499 int j, k, h;
500 char b[BDEVNAME_SIZE];
070ec55d 501 raid0_conf_t *conf = mddev->private;
8299d7f7 502
1b961429 503 sector_t zone_size;
504 sector_t zone_start = 0;
1da177e4 505 h = 0;
1b961429 506
1da177e4
LT
507 for (j = 0; j < conf->nr_strip_zones; j++) {
508 seq_printf(seq, " z%d", j);
1da177e4
LT
509 seq_printf(seq, "=[");
510 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
8299d7f7 511 seq_printf(seq, "%s/", bdevname(
1b961429 512 conf->devlist[j*mddev->raid_disks + k]
513 ->bdev, b));
514
515 zone_size = conf->strip_zone[j].zone_end - zone_start;
516 seq_printf(seq, "] ze=%lld ds=%lld s=%lld\n",
517 (unsigned long long)zone_start>>1,
518 (unsigned long long)conf->strip_zone[j].dev_start>>1,
519 (unsigned long long)zone_size>>1);
520 zone_start = conf->strip_zone[j].zone_end;
1da177e4
LT
521 }
522#endif
9d8f0363 523 seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
1da177e4
LT
524 return;
525}
526
2604b703 527static struct mdk_personality raid0_personality=
1da177e4
LT
528{
529 .name = "raid0",
2604b703 530 .level = 0,
1da177e4
LT
531 .owner = THIS_MODULE,
532 .make_request = raid0_make_request,
533 .run = raid0_run,
534 .stop = raid0_stop,
535 .status = raid0_status,
80c3a6ce 536 .size = raid0_size,
1da177e4
LT
537};
538
539static int __init raid0_init (void)
540{
2604b703 541 return register_md_personality (&raid0_personality);
1da177e4
LT
542}
543
544static void raid0_exit (void)
545{
2604b703 546 unregister_md_personality (&raid0_personality);
1da177e4
LT
547}
548
549module_init(raid0_init);
550module_exit(raid0_exit);
551MODULE_LICENSE("GPL");
552MODULE_ALIAS("md-personality-2"); /* RAID0 */
d9d166c2 553MODULE_ALIAS("md-raid0");
2604b703 554MODULE_ALIAS("md-level-0");