vserver 1.9.3
[linux-2.6.git] / 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/module.h>
22 #include <linux/raid/raid0.h>
23
24 #define MAJOR_NR MD_MAJOR
25 #define MD_DRIVER
26 #define MD_PERSONALITY
27
28 static void raid0_unplug(request_queue_t *q)
29 {
30         mddev_t *mddev = q->queuedata;
31         raid0_conf_t *conf = mddev_to_conf(mddev);
32         mdk_rdev_t **devlist = conf->strip_zone[0].dev;
33         int i;
34
35         for (i=0; i<mddev->raid_disks; i++) {
36                 request_queue_t *r_queue = bdev_get_queue(devlist[i]->bdev);
37
38                 if (r_queue->unplug_fn)
39                         r_queue->unplug_fn(r_queue);
40         }
41 }
42
43 static int raid0_issue_flush(request_queue_t *q, struct gendisk *disk,
44                              sector_t *error_sector)
45 {
46         mddev_t *mddev = q->queuedata;
47         raid0_conf_t *conf = mddev_to_conf(mddev);
48         mdk_rdev_t **devlist = conf->strip_zone[0].dev;
49         int i, ret = 0;
50
51         for (i=0; i<mddev->raid_disks; i++) {
52                 struct block_device *bdev = devlist[i]->bdev;
53                 request_queue_t *r_queue = bdev_get_queue(bdev);
54
55                 if (!r_queue->issue_flush_fn) {
56                         ret = -EOPNOTSUPP;
57                         break;
58                 }
59
60                 ret =r_queue->issue_flush_fn(r_queue, bdev->bd_disk, error_sector);
61                 if (ret)
62                         break;
63         }
64         return ret;
65 }
66
67
68 static int create_strip_zones (mddev_t *mddev)
69 {
70         int i, c, j;
71         sector_t current_offset, curr_zone_offset;
72         sector_t min_spacing;
73         raid0_conf_t *conf = mddev_to_conf(mddev);
74         mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
75         struct list_head *tmp1, *tmp2;
76         struct strip_zone *zone;
77         int cnt;
78         char b[BDEVNAME_SIZE];
79  
80         /*
81          * The number of 'same size groups'
82          */
83         conf->nr_strip_zones = 0;
84  
85         ITERATE_RDEV(mddev,rdev1,tmp1) {
86                 printk("raid0: looking at %s\n",
87                         bdevname(rdev1->bdev,b));
88                 c = 0;
89                 ITERATE_RDEV(mddev,rdev2,tmp2) {
90                         printk("raid0:   comparing %s(%llu)",
91                                bdevname(rdev1->bdev,b),
92                                (unsigned long long)rdev1->size);
93                         printk(" with %s(%llu)\n",
94                                bdevname(rdev2->bdev,b),
95                                (unsigned long long)rdev2->size);
96                         if (rdev2 == rdev1) {
97                                 printk("raid0:   END\n");
98                                 break;
99                         }
100                         if (rdev2->size == rdev1->size)
101                         {
102                                 /*
103                                  * Not unique, don't count it as a new
104                                  * group
105                                  */
106                                 printk("raid0:   EQUAL\n");
107                                 c = 1;
108                                 break;
109                         }
110                         printk("raid0:   NOT EQUAL\n");
111                 }
112                 if (!c) {
113                         printk("raid0:   ==> UNIQUE\n");
114                         conf->nr_strip_zones++;
115                         printk("raid0: %d zones\n", conf->nr_strip_zones);
116                 }
117         }
118         printk("raid0: FINAL %d zones\n", conf->nr_strip_zones);
119
120         conf->strip_zone = kmalloc(sizeof(struct strip_zone)*
121                                 conf->nr_strip_zones, GFP_KERNEL);
122         if (!conf->strip_zone)
123                 return 1;
124         conf->devlist = kmalloc(sizeof(mdk_rdev_t*)*
125                                 conf->nr_strip_zones*mddev->raid_disks,
126                                 GFP_KERNEL);
127         if (!conf->devlist)
128                 return 1;
129
130         memset(conf->strip_zone, 0,sizeof(struct strip_zone)*
131                                    conf->nr_strip_zones);
132         memset(conf->devlist, 0,
133                sizeof(mdk_rdev_t*) * conf->nr_strip_zones * mddev->raid_disks);
134
135         /* The first zone must contain all devices, so here we check that
136          * there is a proper alignment of slots to devices and find them all
137          */
138         zone = &conf->strip_zone[0];
139         cnt = 0;
140         smallest = NULL;
141         zone->dev = conf->devlist;
142         ITERATE_RDEV(mddev, rdev1, tmp1) {
143                 int j = rdev1->raid_disk;
144
145                 if (j < 0 || j >= mddev->raid_disks) {
146                         printk("raid0: bad disk number %d - aborting!\n", j);
147                         goto abort;
148                 }
149                 if (zone->dev[j]) {
150                         printk("raid0: multiple devices for %d - aborting!\n",
151                                 j);
152                         goto abort;
153                 }
154                 zone->dev[j] = rdev1;
155
156                 blk_queue_stack_limits(mddev->queue,
157                                        rdev1->bdev->bd_disk->queue);
158                 /* as we don't honour merge_bvec_fn, we must never risk
159                  * violating it, so limit ->max_sector to one PAGE, as
160                  * a one page request is never in violation.
161                  */
162
163                 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
164                     mddev->queue->max_sectors > (PAGE_SIZE>>9))
165                         blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
166
167                 if (!smallest || (rdev1->size <smallest->size))
168                         smallest = rdev1;
169                 cnt++;
170         }
171         if (cnt != mddev->raid_disks) {
172                 printk("raid0: too few disks (%d of %d) - aborting!\n",
173                         cnt, mddev->raid_disks);
174                 goto abort;
175         }
176         zone->nb_dev = cnt;
177         zone->size = smallest->size * cnt;
178         zone->zone_offset = 0;
179
180         current_offset = smallest->size;
181         curr_zone_offset = zone->size;
182
183         /* now do the other zones */
184         for (i = 1; i < conf->nr_strip_zones; i++)
185         {
186                 zone = conf->strip_zone + i;
187                 zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
188
189                 printk("raid0: zone %d\n", i);
190                 zone->dev_offset = current_offset;
191                 smallest = NULL;
192                 c = 0;
193
194                 for (j=0; j<cnt; j++) {
195                         char b[BDEVNAME_SIZE];
196                         rdev = conf->strip_zone[0].dev[j];
197                         printk("raid0: checking %s ...", bdevname(rdev->bdev,b));
198                         if (rdev->size > current_offset)
199                         {
200                                 printk(" contained as device %d\n", c);
201                                 zone->dev[c] = rdev;
202                                 c++;
203                                 if (!smallest || (rdev->size <smallest->size)) {
204                                         smallest = rdev;
205                                         printk("  (%llu) is smallest!.\n", 
206                                                 (unsigned long long)rdev->size);
207                                 }
208                         } else
209                                 printk(" nope.\n");
210                 }
211
212                 zone->nb_dev = c;
213                 zone->size = (smallest->size - current_offset) * c;
214                 printk("raid0: zone->nb_dev: %d, size: %llu\n",
215                         zone->nb_dev, (unsigned long long)zone->size);
216
217                 zone->zone_offset = curr_zone_offset;
218                 curr_zone_offset += zone->size;
219
220                 current_offset = smallest->size;
221                 printk("raid0: current zone offset: %llu\n",
222                         (unsigned long long)current_offset);
223         }
224
225         /* Now find appropriate hash spacing.
226          * We want a number which causes most hash entries to cover
227          * at most two strips, but the hash table must be at most
228          * 1 PAGE.  We choose the smallest strip, or contiguous collection
229          * of strips, that has big enough size.  We never consider the last
230          * strip though as it's size has no bearing on the efficacy of the hash
231          * table.
232          */
233         conf->hash_spacing = curr_zone_offset;
234         min_spacing = curr_zone_offset;
235         sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*));
236         for (i=0; i < conf->nr_strip_zones-1; i++) {
237                 sector_t sz = 0;
238                 for (j=i; j<conf->nr_strip_zones-1 &&
239                              sz < min_spacing ; j++)
240                         sz += conf->strip_zone[j].size;
241                 if (sz >= min_spacing && sz < conf->hash_spacing)
242                         conf->hash_spacing = sz;
243         }
244
245         mddev->queue->unplug_fn = raid0_unplug;
246
247         mddev->queue->issue_flush_fn = raid0_issue_flush;
248
249         printk("raid0: done.\n");
250         return 0;
251  abort:
252         return 1;
253 }
254
255 /**
256  *      raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
257  *      @q: request queue
258  *      @bio: the buffer head that's been built up so far
259  *      @biovec: the request that could be merged to it.
260  *
261  *      Return amount of bytes we can accept at this offset
262  */
263 static int raid0_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
264 {
265         mddev_t *mddev = q->queuedata;
266         sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
267         int max;
268         unsigned int chunk_sectors = mddev->chunk_size >> 9;
269         unsigned int bio_sectors = bio->bi_size >> 9;
270
271         max =  (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
272         if (max < 0) max = 0; /* bio_add cannot handle a negative return */
273         if (max <= biovec->bv_len && bio_sectors == 0)
274                 return biovec->bv_len;
275         else 
276                 return max;
277 }
278
279 static int raid0_run (mddev_t *mddev)
280 {
281         unsigned  cur=0, i=0, nb_zone;
282         s64 size;
283         raid0_conf_t *conf;
284         mdk_rdev_t *rdev;
285         struct list_head *tmp;
286
287         printk("%s: setting max_sectors to %d, segment boundary to %d\n",
288                mdname(mddev),
289                mddev->chunk_size >> 9,
290                (mddev->chunk_size>>1)-1);
291         blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
292         blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
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         mddev->array_size = 0;
306         ITERATE_RDEV(mddev,rdev,tmp)
307                 mddev->array_size += rdev->size;
308
309         printk("raid0 : md_size is %llu blocks.\n", 
310                 (unsigned long long)mddev->array_size);
311         printk("raid0 : conf->hash_spacing is %llu blocks.\n",
312                 (unsigned long long)conf->hash_spacing);
313         {
314 #if __GNUC__ < 3
315                 volatile
316 #endif
317                 sector_t s = mddev->array_size;
318                 sector_t space = conf->hash_spacing;
319                 int round;
320                 conf->preshift = 0;
321                 if (sizeof(sector_t) > sizeof(unsigned long)) {
322                         /*shift down space and s so that sector_div will work */
323                         while (space > (sector_t) (~(unsigned long)0)) {
324                                 s >>= 1;
325                                 space >>= 1;
326                                 s += 1; /* force round-up */
327                                 conf->preshift++;
328                         }
329                 }
330                 round = sector_div(s, (unsigned long)space) ? 1 : 0;
331                 nb_zone = s + round;
332         }
333         printk("raid0 : nb_zone is %d.\n", nb_zone);
334
335         printk("raid0 : Allocating %Zd bytes for hash.\n",
336                                 nb_zone*sizeof(struct strip_zone*));
337         conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL);
338         if (!conf->hash_table)
339                 goto out_free_conf;
340         size = conf->strip_zone[cur].size;
341
342         for (i=0; i< nb_zone; i++) {
343                 conf->hash_table[i] = conf->strip_zone + cur;
344                 while (size <= conf->hash_spacing) {
345                         cur++;
346                         size += conf->strip_zone[cur].size;
347                 }
348                 size -= conf->hash_spacing;
349         }
350         if (conf->preshift) {
351                 conf->hash_spacing >>= conf->preshift;
352                 /* round hash_spacing up so when we divide by it, we
353                  * err on the side of too-low, which is safest
354                  */
355                 conf->hash_spacing++;
356         }
357
358         /* calculate the max read-ahead size.
359          * For read-ahead of large files to be effective, we need to
360          * readahead at least twice a whole stripe. i.e. number of devices
361          * multiplied by chunk size times 2.
362          * If an individual device has an ra_pages greater than the
363          * chunk size, then we will not drive that device as hard as it
364          * wants.  We consider this a configuration error: a larger
365          * chunksize should be used in that case.
366          */
367         {
368                 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_CACHE_SIZE;
369                 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
370                         mddev->queue->backing_dev_info.ra_pages = 2* stripe;
371         }
372
373
374         blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
375         return 0;
376
377 out_free_conf:
378         if (conf->strip_zone)
379                 kfree(conf->strip_zone);
380         if (conf->devlist)
381                 kfree (conf->devlist);
382         kfree(conf);
383         mddev->private = NULL;
384 out:
385         return 1;
386 }
387
388 static int raid0_stop (mddev_t *mddev)
389 {
390         raid0_conf_t *conf = mddev_to_conf(mddev);
391
392         kfree (conf->hash_table);
393         conf->hash_table = NULL;
394         kfree (conf->strip_zone);
395         conf->strip_zone = NULL;
396         kfree (conf);
397         mddev->private = NULL;
398
399         return 0;
400 }
401
402 static int raid0_make_request (request_queue_t *q, struct bio *bio)
403 {
404         mddev_t *mddev = q->queuedata;
405         unsigned int sect_in_chunk, chunksize_bits,  chunk_size, chunk_sects;
406         raid0_conf_t *conf = mddev_to_conf(mddev);
407         struct strip_zone *zone;
408         mdk_rdev_t *tmp_dev;
409         unsigned long chunk;
410         sector_t block, rsect;
411
412         if (bio_data_dir(bio)==WRITE) {
413                 disk_stat_inc(mddev->gendisk, writes);
414                 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
415         } else {
416                 disk_stat_inc(mddev->gendisk, reads);
417                 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
418         }
419
420         chunk_size = mddev->chunk_size >> 10;
421         chunk_sects = mddev->chunk_size >> 9;
422         chunksize_bits = ffz(~chunk_size);
423         block = bio->bi_sector >> 1;
424         
425
426         if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
427                 struct bio_pair *bp;
428                 /* Sanity check -- queue functions should prevent this happening */
429                 if (bio->bi_vcnt != 1 ||
430                     bio->bi_idx != 0)
431                         goto bad_map;
432                 /* This is a one page bio that upper layers
433                  * refuse to split for us, so we need to split it.
434                  */
435                 bp = bio_split(bio, bio_split_pool, chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
436                 if (raid0_make_request(q, &bp->bio1))
437                         generic_make_request(&bp->bio1);
438                 if (raid0_make_request(q, &bp->bio2))
439                         generic_make_request(&bp->bio2);
440
441                 bio_pair_release(bp);
442                 return 0;
443         }
444  
445
446         {
447 #if __GNUC__ < 3
448                 volatile
449 #endif
450                 sector_t x = block >> conf->preshift;
451                 sector_div(x, (unsigned long)conf->hash_spacing);
452                 zone = conf->hash_table[x];
453         }
454  
455         while (block >= (zone->zone_offset + zone->size)) 
456                 zone++;
457     
458         sect_in_chunk = bio->bi_sector & ((chunk_size<<1) -1);
459
460
461         {
462                 sector_t x =  (block - zone->zone_offset) >> chunksize_bits;
463
464                 sector_div(x, zone->nb_dev);
465                 chunk = x;
466                 BUG_ON(x != (sector_t)chunk);
467
468                 x = block >> chunksize_bits;
469                 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
470         }
471         rsect = (((chunk << chunksize_bits) + zone->dev_offset)<<1)
472                 + sect_in_chunk;
473  
474         bio->bi_bdev = tmp_dev->bdev;
475         bio->bi_sector = rsect + tmp_dev->data_offset;
476
477         /*
478          * Let the main block layer submit the IO and resolve recursion:
479          */
480         return 1;
481
482 bad_map:
483         printk("raid0_make_request bug: can't convert block across chunks"
484                 " or bigger than %dk %llu %d\n", chunk_size, 
485                 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
486
487         bio_io_error(bio, bio->bi_size);
488         return 0;
489 }
490                            
491 static void raid0_status (struct seq_file *seq, mddev_t *mddev)
492 {
493 #undef MD_DEBUG
494 #ifdef MD_DEBUG
495         int j, k, h;
496         char b[BDEVNAME_SIZE];
497         raid0_conf_t *conf = mddev_to_conf(mddev);
498   
499         h = 0;
500         for (j = 0; j < conf->nr_strip_zones; j++) {
501                 seq_printf(seq, "      z%d", j);
502                 if (conf->hash_table[h] == conf->strip_zone+j)
503                         seq_printf("(h%d)", h++);
504                 seq_printf(seq, "=[");
505                 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
506                         seq_printf (seq, "%s/", bdevname(
507                                 conf->strip_zone[j].dev[k]->bdev,b));
508
509                 seq_printf (seq, "] zo=%d do=%d s=%d\n",
510                                 conf->strip_zone[j].zone_offset,
511                                 conf->strip_zone[j].dev_offset,
512                                 conf->strip_zone[j].size);
513         }
514 #endif
515         seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
516         return;
517 }
518
519 static mdk_personality_t raid0_personality=
520 {
521         .name           = "raid0",
522         .owner          = THIS_MODULE,
523         .make_request   = raid0_make_request,
524         .run            = raid0_run,
525         .stop           = raid0_stop,
526         .status         = raid0_status,
527 };
528
529 static int __init raid0_init (void)
530 {
531         return register_md_personality (RAID0, &raid0_personality);
532 }
533
534 static void raid0_exit (void)
535 {
536         unregister_md_personality (RAID0);
537 }
538
539 module_init(raid0_init);
540 module_exit(raid0_exit);
541 MODULE_LICENSE("GPL");
542 MODULE_ALIAS("md-personality-2"); /* RAID0 */