Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / drivers / md / linear.c
blobb1941b887f46fd6f7f5a991cbb969778ecd31c61
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>
7 Linear mode management functions.
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.
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.
19 #include <linux/module.h>
21 #include <linux/raid/md.h>
22 #include <linux/slab.h>
23 #include <linux/raid/linear.h>
25 #define MAJOR_NR MD_MAJOR
26 #define MD_DRIVER
27 #define MD_PERSONALITY
30 * find which device holds a particular offset
32 static inline dev_info_t *which_dev(mddev_t *mddev, sector_t sector)
34 dev_info_t *hash;
35 linear_conf_t *conf = mddev_to_conf(mddev);
36 sector_t block = sector >> 1;
39 * sector_div(a,b) returns the remainer and sets a to a/b
41 (void)sector_div(block, conf->smallest->size);
42 hash = conf->hash_table[block];
44 while ((sector>>1) >= (hash->size + hash->offset))
45 hash++;
46 return hash;
49 /**
50 * linear_mergeable_bvec -- tell bio layer if a two requests can be merged
51 * @q: request queue
52 * @bio: the buffer head that's been built up so far
53 * @biovec: the request that could be merged to it.
55 * Return amount of bytes we can take at this offset
57 static int linear_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
59 mddev_t *mddev = q->queuedata;
60 dev_info_t *dev0;
61 unsigned long maxsectors, bio_sectors = bio->bi_size >> 9;
62 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
64 dev0 = which_dev(mddev, sector);
65 maxsectors = (dev0->size << 1) - (sector - (dev0->offset<<1));
67 if (maxsectors < bio_sectors)
68 maxsectors = 0;
69 else
70 maxsectors -= bio_sectors;
72 if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0)
73 return biovec->bv_len;
74 /* The bytes available at this offset could be really big,
75 * so we cap at 2^31 to avoid overflow */
76 if (maxsectors > (1 << (31-9)))
77 return 1<<31;
78 return maxsectors << 9;
81 static void linear_unplug(request_queue_t *q)
83 mddev_t *mddev = q->queuedata;
84 linear_conf_t *conf = mddev_to_conf(mddev);
85 int i;
87 for (i=0; i < mddev->raid_disks; i++) {
88 request_queue_t *r_queue = bdev_get_queue(conf->disks[i].rdev->bdev);
89 if (r_queue->unplug_fn)
90 r_queue->unplug_fn(r_queue);
94 static int linear_issue_flush(request_queue_t *q, struct gendisk *disk,
95 sector_t *error_sector)
97 mddev_t *mddev = q->queuedata;
98 linear_conf_t *conf = mddev_to_conf(mddev);
99 int i, ret = 0;
101 for (i=0; i < mddev->raid_disks && ret == 0; i++) {
102 struct block_device *bdev = conf->disks[i].rdev->bdev;
103 request_queue_t *r_queue = bdev_get_queue(bdev);
105 if (!r_queue->issue_flush_fn)
106 ret = -EOPNOTSUPP;
107 else
108 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, error_sector);
110 return ret;
113 static int linear_run (mddev_t *mddev)
115 linear_conf_t *conf;
116 dev_info_t **table;
117 mdk_rdev_t *rdev;
118 int i, nb_zone, cnt;
119 sector_t start;
120 sector_t curr_offset;
121 struct list_head *tmp;
123 conf = kmalloc (sizeof (*conf) + mddev->raid_disks*sizeof(dev_info_t),
124 GFP_KERNEL);
125 if (!conf)
126 goto out;
127 memset(conf, 0, sizeof(*conf) + mddev->raid_disks*sizeof(dev_info_t));
128 mddev->private = conf;
131 * Find the smallest device.
134 conf->smallest = NULL;
135 cnt = 0;
136 mddev->array_size = 0;
138 ITERATE_RDEV(mddev,rdev,tmp) {
139 int j = rdev->raid_disk;
140 dev_info_t *disk = conf->disks + j;
142 if (j < 0 || j > mddev->raid_disks || disk->rdev) {
143 printk("linear: disk numbering problem. Aborting!\n");
144 goto out;
147 disk->rdev = rdev;
149 blk_queue_stack_limits(mddev->queue,
150 rdev->bdev->bd_disk->queue);
151 /* as we don't honour merge_bvec_fn, we must never risk
152 * violating it, so limit ->max_sector to one PAGE, as
153 * a one page request is never in violation.
155 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
156 mddev->queue->max_sectors > (PAGE_SIZE>>9))
157 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
159 disk->size = rdev->size;
160 mddev->array_size += rdev->size;
162 if (!conf->smallest || (disk->size < conf->smallest->size))
163 conf->smallest = disk;
164 cnt++;
166 if (cnt != mddev->raid_disks) {
167 printk("linear: not enough drives present. Aborting!\n");
168 goto out;
172 * This code was restructured to work around a gcc-2.95.3 internal
173 * compiler error. Alter it with care.
176 sector_t sz;
177 unsigned round;
178 unsigned long base;
180 sz = mddev->array_size;
181 base = conf->smallest->size;
182 round = sector_div(sz, base);
183 nb_zone = conf->nr_zones = sz + (round ? 1 : 0);
186 conf->hash_table = kmalloc (sizeof (dev_info_t*) * nb_zone,
187 GFP_KERNEL);
188 if (!conf->hash_table)
189 goto out;
192 * Here we generate the linear hash table
194 table = conf->hash_table;
195 start = 0;
196 curr_offset = 0;
197 for (i = 0; i < cnt; i++) {
198 dev_info_t *disk = conf->disks + i;
200 disk->offset = curr_offset;
201 curr_offset += disk->size;
203 /* 'curr_offset' is the end of this disk
204 * 'start' is the start of table
206 while (start < curr_offset) {
207 *table++ = disk;
208 start += conf->smallest->size;
211 if (table-conf->hash_table != nb_zone)
212 BUG();
214 blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
215 mddev->queue->unplug_fn = linear_unplug;
216 mddev->queue->issue_flush_fn = linear_issue_flush;
217 return 0;
219 out:
220 if (conf)
221 kfree(conf);
222 return 1;
225 static int linear_stop (mddev_t *mddev)
227 linear_conf_t *conf = mddev_to_conf(mddev);
229 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
230 kfree(conf->hash_table);
231 kfree(conf);
233 return 0;
236 static int linear_make_request (request_queue_t *q, struct bio *bio)
238 mddev_t *mddev = q->queuedata;
239 dev_info_t *tmp_dev;
240 sector_t block;
242 if (bio_data_dir(bio)==WRITE) {
243 disk_stat_inc(mddev->gendisk, writes);
244 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
245 } else {
246 disk_stat_inc(mddev->gendisk, reads);
247 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
250 tmp_dev = which_dev(mddev, bio->bi_sector);
251 block = bio->bi_sector >> 1;
253 if (unlikely(block >= (tmp_dev->size + tmp_dev->offset)
254 || block < tmp_dev->offset)) {
255 char b[BDEVNAME_SIZE];
257 printk("linear_make_request: Block %llu out of bounds on "
258 "dev %s size %llu offset %llu\n",
259 (unsigned long long)block,
260 bdevname(tmp_dev->rdev->bdev, b),
261 (unsigned long long)tmp_dev->size,
262 (unsigned long long)tmp_dev->offset);
263 bio_io_error(bio, bio->bi_size);
264 return 0;
266 if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
267 (tmp_dev->offset + tmp_dev->size)<<1)) {
268 /* This bio crosses a device boundary, so we have to
269 * split it.
271 struct bio_pair *bp;
272 bp = bio_split(bio, bio_split_pool,
273 ((tmp_dev->offset + tmp_dev->size)<<1) - bio->bi_sector);
274 if (linear_make_request(q, &bp->bio1))
275 generic_make_request(&bp->bio1);
276 if (linear_make_request(q, &bp->bio2))
277 generic_make_request(&bp->bio2);
278 bio_pair_release(bp);
279 return 0;
282 bio->bi_bdev = tmp_dev->rdev->bdev;
283 bio->bi_sector = bio->bi_sector - (tmp_dev->offset << 1) + tmp_dev->rdev->data_offset;
285 return 1;
288 static void linear_status (struct seq_file *seq, mddev_t *mddev)
291 #undef MD_DEBUG
292 #ifdef MD_DEBUG
293 int j;
294 linear_conf_t *conf = mddev_to_conf(mddev);
295 sector_t s = 0;
297 seq_printf(seq, " ");
298 for (j = 0; j < conf->nr_zones; j++)
300 char b[BDEVNAME_SIZE];
301 s += conf->smallest_size;
302 seq_printf(seq, "[%s",
303 bdevname(conf->hash_table[j][0].rdev->bdev,b));
305 while (s > conf->hash_table[j][0].offset +
306 conf->hash_table[j][0].size)
307 seq_printf(seq, "/%s] ",
308 bdevname(conf->hash_table[j][1].rdev->bdev,b));
309 else
310 seq_printf(seq, "] ");
312 seq_printf(seq, "\n");
313 #endif
314 seq_printf(seq, " %dk rounding", mddev->chunk_size/1024);
318 static mdk_personality_t linear_personality=
320 .name = "linear",
321 .owner = THIS_MODULE,
322 .make_request = linear_make_request,
323 .run = linear_run,
324 .stop = linear_stop,
325 .status = linear_status,
328 static int __init linear_init (void)
330 return register_md_personality (LINEAR, &linear_personality);
333 static void linear_exit (void)
335 unregister_md_personality (LINEAR);
339 module_init(linear_init);
340 module_exit(linear_exit);
341 MODULE_LICENSE("GPL");
342 MODULE_ALIAS("md-personality-1"); /* LINEAR */