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