md: raid0: Remove hash table.
[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         sectors = conf->strip_zone[cur].sectors;
330
331         for (i=1; i< nb_zone; i++) {
332                 while (sectors <= conf->spacing) {
333                         cur++;
334                         sectors += conf->strip_zone[cur].sectors;
335                 }
336                 sectors -= conf->spacing;
337         }
338         if (conf->sector_shift) {
339                 conf->spacing >>= conf->sector_shift;
340                 /* round spacing up so when we divide by it, we
341                  * err on the side of too-low, which is safest
342                  */
343                 conf->spacing++;
344         }
345
346         /* calculate the max read-ahead size.
347          * For read-ahead of large files to be effective, we need to
348          * readahead at least twice a whole stripe. i.e. number of devices
349          * multiplied by chunk size times 2.
350          * If an individual device has an ra_pages greater than the
351          * chunk size, then we will not drive that device as hard as it
352          * wants.  We consider this a configuration error: a larger
353          * chunksize should be used in that case.
354          */
355         {
356                 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
357                 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
358                         mddev->queue->backing_dev_info.ra_pages = 2* stripe;
359         }
360
361
362         blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
363         return 0;
364
365 out_free_conf:
366         kfree(conf->strip_zone);
367         kfree(conf->devlist);
368         kfree(conf);
369         mddev->private = NULL;
370 out:
371         return -ENOMEM;
372 }
373
374 static int raid0_stop (mddev_t *mddev)
375 {
376         raid0_conf_t *conf = mddev_to_conf(mddev);
377
378         blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
379         kfree(conf->strip_zone);
380         conf->strip_zone = NULL;
381         kfree(conf);
382         mddev->private = NULL;
383
384         return 0;
385 }
386
387 /* Find the zone which holds a particular offset */
388 static struct strip_zone *find_zone(struct raid0_private_data *conf,
389                 sector_t sector)
390 {
391         int i;
392         struct strip_zone *z = conf->strip_zone;
393
394         for (i = 0; i < conf->nr_strip_zones; i++)
395                 if (sector < z[i].zone_end)
396                         return z + i;
397         BUG();
398 }
399
400 static int raid0_make_request (struct request_queue *q, struct bio *bio)
401 {
402         mddev_t *mddev = q->queuedata;
403         unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
404         raid0_conf_t *conf = mddev_to_conf(mddev);
405         struct strip_zone *zone;
406         mdk_rdev_t *tmp_dev;
407         sector_t chunk;
408         sector_t sector, rsect;
409         const int rw = bio_data_dir(bio);
410         int cpu;
411
412         if (unlikely(bio_barrier(bio))) {
413                 bio_endio(bio, -EOPNOTSUPP);
414                 return 0;
415         }
416
417         cpu = part_stat_lock();
418         part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
419         part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
420                       bio_sectors(bio));
421         part_stat_unlock();
422
423         chunk_sects = mddev->chunk_size >> 9;
424         chunksect_bits = ffz(~chunk_sects);
425         sector = bio->bi_sector;
426
427         if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
428                 struct bio_pair *bp;
429                 /* Sanity check -- queue functions should prevent this happening */
430                 if (bio->bi_vcnt != 1 ||
431                     bio->bi_idx != 0)
432                         goto bad_map;
433                 /* This is a one page bio that upper layers
434                  * refuse to split for us, so we need to split it.
435                  */
436                 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
437                 if (raid0_make_request(q, &bp->bio1))
438                         generic_make_request(&bp->bio1);
439                 if (raid0_make_request(q, &bp->bio2))
440                         generic_make_request(&bp->bio2);
441
442                 bio_pair_release(bp);
443                 return 0;
444         }
445         zone = find_zone(conf, sector);
446         sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
447         {
448                 sector_t x = (zone->sectors + sector - zone->zone_end)
449                                 >> chunksect_bits;
450
451                 sector_div(x, zone->nb_dev);
452                 chunk = x;
453
454                 x = sector >> chunksect_bits;
455                 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
456         }
457         rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
458  
459         bio->bi_bdev = tmp_dev->bdev;
460         bio->bi_sector = rsect + tmp_dev->data_offset;
461
462         /*
463          * Let the main block layer submit the IO and resolve recursion:
464          */
465         return 1;
466
467 bad_map:
468         printk("raid0_make_request bug: can't convert block across chunks"
469                 " or bigger than %dk %llu %d\n", chunk_sects / 2,
470                 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
471
472         bio_io_error(bio);
473         return 0;
474 }
475
476 static void raid0_status (struct seq_file *seq, mddev_t *mddev)
477 {
478 #undef MD_DEBUG
479 #ifdef MD_DEBUG
480         int j, k, h;
481         char b[BDEVNAME_SIZE];
482         raid0_conf_t *conf = mddev_to_conf(mddev);
483
484         h = 0;
485         for (j = 0; j < conf->nr_strip_zones; j++) {
486                 seq_printf(seq, "      z%d", j);
487                 seq_printf(seq, "=[");
488                 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
489                         seq_printf(seq, "%s/", bdevname(
490                                 conf->strip_zone[j].dev[k]->bdev,b));
491
492                 seq_printf(seq, "] ze=%d ds=%d s=%d\n",
493                                 conf->strip_zone[j].zone_end,
494                                 conf->strip_zone[j].dev_start,
495                                 conf->strip_zone[j].sectors);
496         }
497 #endif
498         seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
499         return;
500 }
501
502 static struct mdk_personality raid0_personality=
503 {
504         .name           = "raid0",
505         .level          = 0,
506         .owner          = THIS_MODULE,
507         .make_request   = raid0_make_request,
508         .run            = raid0_run,
509         .stop           = raid0_stop,
510         .status         = raid0_status,
511         .size           = raid0_size,
512 };
513
514 static int __init raid0_init (void)
515 {
516         return register_md_personality (&raid0_personality);
517 }
518
519 static void raid0_exit (void)
520 {
521         unregister_md_personality (&raid0_personality);
522 }
523
524 module_init(raid0_init);
525 module_exit(raid0_exit);
526 MODULE_LICENSE("GPL");
527 MODULE_ALIAS("md-personality-2"); /* RAID0 */
528 MODULE_ALIAS("md-raid0");
529 MODULE_ALIAS("md-level-0");