md: raid0: remove ->sectors from the strip_zone structure.
[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;
29 raid0_conf_t *conf = mddev_to_conf(mddev);
30 mdk_rdev_t **devlist = conf->strip_zone[0].dev;
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;
43 raid0_conf_t *conf = mddev_to_conf(mddev);
44 mdk_rdev_t **devlist = conf->strip_zone[0].dev;
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
ed7b0038 55static int create_strip_zones(mddev_t *mddev)
1da177e4 56{
ed7b0038 57 int i, c, j, err;
49f357a2 58 sector_t curr_zone_end, sectors;
1da177e4 59 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
1da177e4
LT
60 struct strip_zone *zone;
61 int cnt;
62 char b[BDEVNAME_SIZE];
ed7b0038
AN
63 raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
64
65 if (!conf)
66 return -ENOMEM;
159ec1fc 67 list_for_each_entry(rdev1, &mddev->disks, same_set) {
0825b87a 68 printk(KERN_INFO "raid0: looking at %s\n",
1da177e4
LT
69 bdevname(rdev1->bdev,b));
70 c = 0;
159ec1fc 71 list_for_each_entry(rdev2, &mddev->disks, same_set) {
0825b87a 72 printk(KERN_INFO "raid0: comparing %s(%llu)",
1da177e4 73 bdevname(rdev1->bdev,b),
dd8ac336 74 (unsigned long long)rdev1->sectors);
0825b87a 75 printk(KERN_INFO " with %s(%llu)\n",
1da177e4 76 bdevname(rdev2->bdev,b),
dd8ac336 77 (unsigned long long)rdev2->sectors);
1da177e4 78 if (rdev2 == rdev1) {
0825b87a 79 printk(KERN_INFO "raid0: END\n");
1da177e4
LT
80 break;
81 }
dd8ac336 82 if (rdev2->sectors == rdev1->sectors) {
1da177e4
LT
83 /*
84 * Not unique, don't count it as a new
85 * group
86 */
0825b87a 87 printk(KERN_INFO "raid0: EQUAL\n");
1da177e4
LT
88 c = 1;
89 break;
90 }
0825b87a 91 printk(KERN_INFO "raid0: NOT EQUAL\n");
1da177e4
LT
92 }
93 if (!c) {
0825b87a 94 printk(KERN_INFO "raid0: ==> UNIQUE\n");
1da177e4 95 conf->nr_strip_zones++;
0825b87a
AN
96 printk(KERN_INFO "raid0: %d zones\n",
97 conf->nr_strip_zones);
1da177e4
LT
98 }
99 }
0825b87a 100 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
ed7b0038 101 err = -ENOMEM;
9ffae0cf 102 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
1da177e4
LT
103 conf->nr_strip_zones, GFP_KERNEL);
104 if (!conf->strip_zone)
ed7b0038 105 goto abort;
9ffae0cf 106 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
1da177e4
LT
107 conf->nr_strip_zones*mddev->raid_disks,
108 GFP_KERNEL);
109 if (!conf->devlist)
ed7b0038 110 goto abort;
1da177e4 111
1da177e4
LT
112 /* The first zone must contain all devices, so here we check that
113 * there is a proper alignment of slots to devices and find them all
114 */
115 zone = &conf->strip_zone[0];
116 cnt = 0;
117 smallest = NULL;
118 zone->dev = conf->devlist;
ed7b0038 119 err = -EINVAL;
159ec1fc 120 list_for_each_entry(rdev1, &mddev->disks, same_set) {
1da177e4
LT
121 int j = rdev1->raid_disk;
122
123 if (j < 0 || j >= mddev->raid_disks) {
0825b87a
AN
124 printk(KERN_ERR "raid0: bad disk number %d - "
125 "aborting!\n", j);
1da177e4
LT
126 goto abort;
127 }
128 if (zone->dev[j]) {
0825b87a
AN
129 printk(KERN_ERR "raid0: multiple devices for %d - "
130 "aborting!\n", j);
1da177e4
LT
131 goto abort;
132 }
133 zone->dev[j] = rdev1;
134
135 blk_queue_stack_limits(mddev->queue,
136 rdev1->bdev->bd_disk->queue);
137 /* as we don't honour merge_bvec_fn, we must never risk
138 * violating it, so limit ->max_sector to one PAGE, as
139 * a one page request is never in violation.
140 */
141
142 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
ae03bf63 143 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
1da177e4
LT
144 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
145
dd8ac336 146 if (!smallest || (rdev1->sectors < smallest->sectors))
1da177e4
LT
147 smallest = rdev1;
148 cnt++;
149 }
150 if (cnt != mddev->raid_disks) {
0825b87a
AN
151 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
152 "aborting!\n", cnt, mddev->raid_disks);
1da177e4
LT
153 goto abort;
154 }
155 zone->nb_dev = cnt;
49f357a2 156 zone->zone_end = smallest->sectors * cnt;
1da177e4 157
49f357a2 158 curr_zone_end = zone->zone_end;
1da177e4
LT
159
160 /* now do the other zones */
161 for (i = 1; i < conf->nr_strip_zones; i++)
162 {
163 zone = conf->strip_zone + i;
164 zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
165
0825b87a 166 printk(KERN_INFO "raid0: zone %d\n", i);
d27a43ab 167 zone->dev_start = smallest->sectors;
1da177e4
LT
168 smallest = NULL;
169 c = 0;
170
171 for (j=0; j<cnt; j++) {
172 char b[BDEVNAME_SIZE];
173 rdev = conf->strip_zone[0].dev[j];
0825b87a
AN
174 printk(KERN_INFO "raid0: checking %s ...",
175 bdevname(rdev->bdev, b));
d27a43ab 176 if (rdev->sectors <= zone->dev_start) {
0825b87a 177 printk(KERN_INFO " nope.\n");
dd8ac336
AN
178 continue;
179 }
180 printk(KERN_INFO " contained as device %d\n", c);
181 zone->dev[c] = rdev;
182 c++;
183 if (!smallest || rdev->sectors < smallest->sectors) {
184 smallest = rdev;
185 printk(KERN_INFO " (%llu) is smallest!.\n",
186 (unsigned long long)rdev->sectors);
187 }
1da177e4
LT
188 }
189
190 zone->nb_dev = c;
49f357a2 191 sectors = (smallest->sectors - zone->dev_start) * c;
83838ed8 192 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
49f357a2 193 zone->nb_dev, (unsigned long long)sectors);
1da177e4 194
49f357a2 195 curr_zone_end += sectors;
d27a43ab 196 zone->zone_end = curr_zone_end;
1da177e4 197
6b8796cc 198 printk(KERN_INFO "raid0: current zone start: %llu\n",
d27a43ab 199 (unsigned long long)smallest->sectors);
1da177e4 200 }
1da177e4 201 mddev->queue->unplug_fn = raid0_unplug;
26be34dc
N
202 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
203 mddev->queue->backing_dev_info.congested_data = mddev;
1da177e4 204
0825b87a 205 printk(KERN_INFO "raid0: done.\n");
ed7b0038 206 mddev->private = conf;
1da177e4 207 return 0;
5568a603 208abort:
ed7b0038
AN
209 kfree(conf->strip_zone);
210 kfree(conf->devlist);
211 kfree(conf);
212 mddev->private = NULL;
213 return err;
1da177e4
LT
214}
215
216/**
217 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
218 * @q: request queue
cc371e66 219 * @bvm: properties of new bio
1da177e4
LT
220 * @biovec: the request that could be merged to it.
221 *
222 * Return amount of bytes we can accept at this offset
223 */
cc371e66
AK
224static int raid0_mergeable_bvec(struct request_queue *q,
225 struct bvec_merge_data *bvm,
226 struct bio_vec *biovec)
1da177e4
LT
227{
228 mddev_t *mddev = q->queuedata;
cc371e66 229 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
1da177e4
LT
230 int max;
231 unsigned int chunk_sectors = mddev->chunk_size >> 9;
cc371e66 232 unsigned int bio_sectors = bvm->bi_size >> 9;
1da177e4
LT
233
234 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
235 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
236 if (max <= biovec->bv_len && bio_sectors == 0)
237 return biovec->bv_len;
238 else
239 return max;
240}
241
80c3a6ce
DW
242static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
243{
244 sector_t array_sectors = 0;
245 mdk_rdev_t *rdev;
246
247 WARN_ONCE(sectors || raid_disks,
248 "%s does not support generic reshape\n", __func__);
249
250 list_for_each_entry(rdev, &mddev->disks, same_set)
251 array_sectors += rdev->sectors;
252
253 return array_sectors;
254}
255
8f79cfcd 256static int raid0_run(mddev_t *mddev)
1da177e4 257{
5568a603 258 int ret;
1da177e4 259
2604b703
N
260 if (mddev->chunk_size == 0) {
261 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
262 return -EINVAL;
263 }
264 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
1da177e4
LT
265 mdname(mddev),
266 mddev->chunk_size >> 9,
267 (mddev->chunk_size>>1)-1);
268 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
269 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
e7e72bf6 270 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
1da177e4 271
5568a603
AN
272 ret = create_strip_zones(mddev);
273 if (ret < 0)
ed7b0038 274 return ret;
1da177e4
LT
275
276 /* calculate array device size */
1f403624 277 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
1da177e4 278
ccacc7d2
AN
279 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
280 (unsigned long long)mddev->array_sectors);
1da177e4
LT
281 /* calculate the max read-ahead size.
282 * For read-ahead of large files to be effective, we need to
283 * readahead at least twice a whole stripe. i.e. number of devices
284 * multiplied by chunk size times 2.
285 * If an individual device has an ra_pages greater than the
286 * chunk size, then we will not drive that device as hard as it
287 * wants. We consider this a configuration error: a larger
288 * chunksize should be used in that case.
289 */
290 {
2d1f3b5d 291 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
1da177e4
LT
292 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
293 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
294 }
295
1da177e4
LT
296 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
297 return 0;
1da177e4
LT
298}
299
fb5ab4b5 300static int raid0_stop(mddev_t *mddev)
1da177e4
LT
301{
302 raid0_conf_t *conf = mddev_to_conf(mddev);
303
304 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
990a8baf 305 kfree(conf->strip_zone);
fb5ab4b5 306 kfree(conf->devlist);
990a8baf 307 kfree(conf);
1da177e4 308 mddev->private = NULL;
1da177e4
LT
309 return 0;
310}
311
49f357a2
N
312/* Find the zone which holds a particular offset
313 * Update *sectorp to be an offset in that zone
314 */
dc582663 315static struct strip_zone *find_zone(struct raid0_private_data *conf,
49f357a2 316 sector_t *sectorp)
dc582663
AN
317{
318 int i;
319 struct strip_zone *z = conf->strip_zone;
49f357a2 320 sector_t sector = *sectorp;
dc582663
AN
321
322 for (i = 0; i < conf->nr_strip_zones; i++)
49f357a2
N
323 if (sector < z[i].zone_end) {
324 if (i)
325 *sectorp = sector - z[i-1].zone_end;
dc582663 326 return z + i;
49f357a2 327 }
dc582663
AN
328 BUG();
329}
330
165125e1 331static int raid0_make_request (struct request_queue *q, struct bio *bio)
1da177e4
LT
332{
333 mddev_t *mddev = q->queuedata;
a4712005 334 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
1da177e4
LT
335 raid0_conf_t *conf = mddev_to_conf(mddev);
336 struct strip_zone *zone;
337 mdk_rdev_t *tmp_dev;
787f17fe 338 sector_t chunk;
49f357a2 339 sector_t sector, rsect, sector_offset;
a362357b 340 const int rw = bio_data_dir(bio);
c9959059 341 int cpu;
1da177e4 342
e5dcdd80 343 if (unlikely(bio_barrier(bio))) {
6712ecf8 344 bio_endio(bio, -EOPNOTSUPP);
e5dcdd80
N
345 return 0;
346 }
347
074a7aca
TH
348 cpu = part_stat_lock();
349 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
350 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
351 bio_sectors(bio));
352 part_stat_unlock();
1da177e4 353
1da177e4 354 chunk_sects = mddev->chunk_size >> 9;
1b7fdf8f 355 chunksect_bits = ffz(~chunk_sects);
e0f06868 356 sector = bio->bi_sector;
1da177e4
LT
357
358 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
359 struct bio_pair *bp;
360 /* Sanity check -- queue functions should prevent this happening */
361 if (bio->bi_vcnt != 1 ||
362 bio->bi_idx != 0)
363 goto bad_map;
364 /* This is a one page bio that upper layers
365 * refuse to split for us, so we need to split it.
366 */
6feef531 367 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
1da177e4
LT
368 if (raid0_make_request(q, &bp->bio1))
369 generic_make_request(&bp->bio1);
370 if (raid0_make_request(q, &bp->bio2))
371 generic_make_request(&bp->bio2);
372
373 bio_pair_release(bp);
374 return 0;
375 }
49f357a2
N
376 sector_offset = sector;
377 zone = find_zone(conf, &sector_offset);
a4712005 378 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
1da177e4 379 {
49f357a2 380 sector_t x = sector_offset >> chunksect_bits;
1da177e4
LT
381
382 sector_div(x, zone->nb_dev);
383 chunk = x;
1da177e4 384
e0f06868 385 x = sector >> chunksect_bits;
1da177e4
LT
386 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
387 }
019c4e2f 388 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
1da177e4
LT
389
390 bio->bi_bdev = tmp_dev->bdev;
391 bio->bi_sector = rsect + tmp_dev->data_offset;
392
393 /*
394 * Let the main block layer submit the IO and resolve recursion:
395 */
396 return 1;
397
398bad_map:
399 printk("raid0_make_request bug: can't convert block across chunks"
a4712005 400 " or bigger than %dk %llu %d\n", chunk_sects / 2,
1da177e4
LT
401 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
402
6712ecf8 403 bio_io_error(bio);
1da177e4
LT
404 return 0;
405}
8299d7f7 406
1da177e4
LT
407static void raid0_status (struct seq_file *seq, mddev_t *mddev)
408{
409#undef MD_DEBUG
410#ifdef MD_DEBUG
411 int j, k, h;
412 char b[BDEVNAME_SIZE];
413 raid0_conf_t *conf = mddev_to_conf(mddev);
8299d7f7 414
1da177e4
LT
415 h = 0;
416 for (j = 0; j < conf->nr_strip_zones; j++) {
417 seq_printf(seq, " z%d", j);
1da177e4
LT
418 seq_printf(seq, "=[");
419 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
8299d7f7 420 seq_printf(seq, "%s/", bdevname(
1da177e4
LT
421 conf->strip_zone[j].dev[k]->bdev,b));
422
dc582663
AN
423 seq_printf(seq, "] ze=%d ds=%d s=%d\n",
424 conf->strip_zone[j].zone_end,
019c4e2f 425 conf->strip_zone[j].dev_start,
83838ed8 426 conf->strip_zone[j].sectors);
1da177e4
LT
427 }
428#endif
429 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
430 return;
431}
432
2604b703 433static struct mdk_personality raid0_personality=
1da177e4
LT
434{
435 .name = "raid0",
2604b703 436 .level = 0,
1da177e4
LT
437 .owner = THIS_MODULE,
438 .make_request = raid0_make_request,
439 .run = raid0_run,
440 .stop = raid0_stop,
441 .status = raid0_status,
80c3a6ce 442 .size = raid0_size,
1da177e4
LT
443};
444
445static int __init raid0_init (void)
446{
2604b703 447 return register_md_personality (&raid0_personality);
1da177e4
LT
448}
449
450static void raid0_exit (void)
451{
2604b703 452 unregister_md_personality (&raid0_personality);
1da177e4
LT
453}
454
455module_init(raid0_init);
456module_exit(raid0_exit);
457MODULE_LICENSE("GPL");
458MODULE_ALIAS("md-personality-2"); /* RAID0 */
d9d166c2 459MODULE_ALIAS("md-raid0");
2604b703 460MODULE_ALIAS("md-level-0");