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