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