ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / md / linear.c
1 /*
2    linear.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
7    Linear mode management functions.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2, or (at your option)
12    any later version.
13    
14    You should have received a copy of the GNU General Public License
15    (for example /usr/src/linux/COPYING); if not, write to the Free
16    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
17 */
18
19 #include <linux/module.h>
20
21 #include <linux/raid/md.h>
22 #include <linux/slab.h>
23 #include <linux/raid/linear.h>
24
25 #define MAJOR_NR MD_MAJOR
26 #define MD_DRIVER
27 #define MD_PERSONALITY
28
29 /*
30  * find which device holds a particular offset 
31  */
32 static inline dev_info_t *which_dev(mddev_t *mddev, sector_t sector)
33 {
34         struct linear_hash *hash;
35         linear_conf_t *conf = mddev_to_conf(mddev);
36         sector_t block = sector >> 1;
37
38         /*
39          * sector_div(a,b) returns the remainer and sets a to a/b
40          */
41         (void)sector_div(block, conf->smallest->size);
42         hash = conf->hash_table + block;
43
44         if ((sector>>1) >= (hash->dev0->size + hash->dev0->offset))
45                 return hash->dev1;
46         else
47                 return hash->dev0;
48 }
49
50
51 /**
52  *      linear_mergeable_bvec -- tell bio layer if a two requests can be merged
53  *      @q: request queue
54  *      @bio: the buffer head that's been built up so far
55  *      @biovec: the request that could be merged to it.
56  *
57  *      Return amount of bytes we can take at this offset
58  */
59 static int linear_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
60 {
61         mddev_t *mddev = q->queuedata;
62         dev_info_t *dev0;
63         unsigned long maxsectors, bio_sectors = bio->bi_size >> 9;
64         sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
65
66         dev0 = which_dev(mddev, sector);
67         maxsectors = (dev0->size << 1) - (sector - (dev0->offset<<1));
68
69         if (maxsectors < bio_sectors)
70                 maxsectors = 0;
71         else
72                 maxsectors -= bio_sectors;
73
74         if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0)
75                 return biovec->bv_len;
76         /* The bytes available at this offset could be really big,
77          * so we cap at 2^31 to avoid overflow */
78         if (maxsectors > (1 << (31-9)))
79                 return 1<<31;
80         return maxsectors << 9;
81 }
82
83 static void linear_unplug(request_queue_t *q)
84 {
85         mddev_t *mddev = q->queuedata;
86         linear_conf_t *conf = mddev_to_conf(mddev);
87         int i;
88
89         for (i=0; i < mddev->raid_disks; i++) {
90                 request_queue_t *r_queue = bdev_get_queue(conf->disks[i].rdev->bdev);
91                 if (r_queue->unplug_fn)
92                         r_queue->unplug_fn(r_queue);
93         }
94 }
95
96
97 static int linear_run (mddev_t *mddev)
98 {
99         linear_conf_t *conf;
100         struct linear_hash *table;
101         mdk_rdev_t *rdev;
102         int size, i, nb_zone, cnt;
103         unsigned int curr_offset;
104         struct list_head *tmp;
105
106         conf = kmalloc (sizeof (*conf) + mddev->raid_disks*sizeof(dev_info_t),
107                         GFP_KERNEL);
108         if (!conf)
109                 goto out;
110         memset(conf, 0, sizeof(*conf) + mddev->raid_disks*sizeof(dev_info_t));
111         mddev->private = conf;
112
113         /*
114          * Find the smallest device.
115          */
116
117         conf->smallest = NULL;
118         cnt = 0;
119         mddev->array_size = 0;
120
121         ITERATE_RDEV(mddev,rdev,tmp) {
122                 int j = rdev->raid_disk;
123                 dev_info_t *disk = conf->disks + j;
124
125                 if (j < 0 || j > mddev->raid_disks || disk->rdev) {
126                         printk("linear: disk numbering problem. Aborting!\n");
127                         goto out;
128                 }
129
130                 disk->rdev = rdev;
131
132                 blk_queue_stack_limits(mddev->queue,
133                                        rdev->bdev->bd_disk->queue);
134                 /* as we don't honour merge_bvec_fn, we must never risk
135                  * violating it, so limit ->max_sector to one PAGE, as
136                  * a one page request is never in violation.
137                  */
138                 if (rdev->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                 disk->size = rdev->size;
143                 mddev->array_size += rdev->size;
144
145                 if (!conf->smallest || (disk->size < conf->smallest->size))
146                         conf->smallest = disk;
147                 cnt++;
148         }
149         if (cnt != mddev->raid_disks) {
150                 printk("linear: not enough drives present. Aborting!\n");
151                 goto out;
152         }
153
154         /*
155          * This code was restructured to work around a gcc-2.95.3 internal
156          * compiler error.  Alter it with care.
157          */
158         {
159                 sector_t sz;
160                 unsigned round;
161                 unsigned long base;
162
163                 sz = mddev->array_size;
164                 base = conf->smallest->size;
165                 round = sector_div(sz, base);
166                 nb_zone = conf->nr_zones = sz + (round ? 1 : 0);
167         }
168                         
169         conf->hash_table = kmalloc (sizeof (struct linear_hash) * nb_zone,
170                                         GFP_KERNEL);
171         if (!conf->hash_table)
172                 goto out;
173
174         /*
175          * Here we generate the linear hash table
176          */
177         table = conf->hash_table;
178         size = 0;
179         curr_offset = 0;
180         for (i = 0; i < cnt; i++) {
181                 dev_info_t *disk = conf->disks + i;
182
183                 disk->offset = curr_offset;
184                 curr_offset += disk->size;
185
186                 if (size < 0) {
187                         table[-1].dev1 = disk;
188                 }
189                 size += disk->size;
190
191                 while (size>0) {
192                         table->dev0 = disk;
193                         table->dev1 = NULL;
194                         size -= conf->smallest->size;
195                         table++;
196                 }
197         }
198         if (table-conf->hash_table != nb_zone)
199                 BUG();
200
201         blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
202         mddev->queue->unplug_fn = linear_unplug;
203         return 0;
204
205 out:
206         if (conf)
207                 kfree(conf);
208         return 1;
209 }
210
211 static int linear_stop (mddev_t *mddev)
212 {
213         linear_conf_t *conf = mddev_to_conf(mddev);
214   
215         kfree(conf->hash_table);
216         kfree(conf);
217
218         return 0;
219 }
220
221 static int linear_make_request (request_queue_t *q, struct bio *bio)
222 {
223         mddev_t *mddev = q->queuedata;
224         dev_info_t *tmp_dev;
225         sector_t block;
226
227         if (bio_data_dir(bio)==WRITE) {
228                 disk_stat_inc(mddev->gendisk, writes);
229                 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
230         } else {
231                 disk_stat_inc(mddev->gendisk, reads);
232                 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
233         }
234
235         tmp_dev = which_dev(mddev, bio->bi_sector);
236         block = bio->bi_sector >> 1;
237   
238         if (unlikely(!tmp_dev)) {
239                 printk("linear_make_request: hash->dev1==NULL for block %llu\n",
240                         (unsigned long long)block);
241                 bio_io_error(bio, bio->bi_size);
242                 return 0;
243         }
244     
245         if (unlikely(block >= (tmp_dev->size + tmp_dev->offset)
246                      || block < tmp_dev->offset)) {
247                 char b[BDEVNAME_SIZE];
248
249                 printk("linear_make_request: Block %llu out of bounds on "
250                         "dev %s size %ld offset %ld\n",
251                         (unsigned long long)block,
252                         bdevname(tmp_dev->rdev->bdev, b),
253                         tmp_dev->size, tmp_dev->offset);
254                 bio_io_error(bio, bio->bi_size);
255                 return 0;
256         }
257         if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
258                      (tmp_dev->offset + tmp_dev->size)<<1)) {
259                 /* This bio crosses a device boundary, so we have to
260                  * split it.
261                  */
262                 struct bio_pair *bp;
263                 bp = bio_split(bio, bio_split_pool, 
264                                (bio->bi_sector + (bio->bi_size >> 9) -
265                                 (tmp_dev->offset + tmp_dev->size))<<1);
266                 if (linear_make_request(q, &bp->bio1))
267                         generic_make_request(&bp->bio1);
268                 if (linear_make_request(q, &bp->bio2))
269                         generic_make_request(&bp->bio2);
270                 bio_pair_release(bp);
271                 return 0;
272         }
273                     
274         bio->bi_bdev = tmp_dev->rdev->bdev;
275         bio->bi_sector = bio->bi_sector - (tmp_dev->offset << 1) + tmp_dev->rdev->data_offset;
276
277         return 1;
278 }
279
280 static void linear_status (struct seq_file *seq, mddev_t *mddev)
281 {
282
283 #undef MD_DEBUG
284 #ifdef MD_DEBUG
285         int j;
286         linear_conf_t *conf = mddev_to_conf(mddev);
287   
288         seq_printf(seq, "      ");
289         for (j = 0; j < conf->nr_zones; j++)
290         {
291                 char b[BDEVNAME_SIZE];
292                 seq_printf(seq, "[%s",
293                            bdevname(conf->hash_table[j].dev0->rdev->bdev,b));
294
295                 if (conf->hash_table[j].dev1)
296                         seq_printf(seq, "/%s] ",
297                                    bdevname(conf->hash_table[j].dev1->rdev->bdev,b));
298                 else
299                         seq_printf(seq, "] ");
300         }
301         seq_printf(seq, "\n");
302 #endif
303         seq_printf(seq, " %dk rounding", mddev->chunk_size/1024);
304 }
305
306
307 static mdk_personality_t linear_personality=
308 {
309         .name           = "linear",
310         .owner          = THIS_MODULE,
311         .make_request   = linear_make_request,
312         .run            = linear_run,
313         .stop           = linear_stop,
314         .status         = linear_status,
315 };
316
317 static int __init linear_init (void)
318 {
319         return register_md_personality (LINEAR, &linear_personality);
320 }
321
322 static void linear_exit (void)
323 {
324         unregister_md_personality (LINEAR);
325 }
326
327
328 module_init(linear_init);
329 module_exit(linear_exit);
330 MODULE_LICENSE("GPL");
331 MODULE_ALIAS("md-personality-1"); /* LINEAR */