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