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