2 linear.c : Multiple Devices driver for Linux
3 Copyright (C) 1994-96 Marc ZYNGIER
4 <zyngier@ufr-info-p7.ibp.fr> or
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)
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
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 struct linear_hash *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 if ((sector>>1) >= (hash->dev0->size + hash->dev0->offset))
52 * linear_mergeable_bvec -- tell bio layer if a two requests can be merged
54 * @bio: the buffer head that's been built up so far
55 * @biovec: the request that could be merged to it.
57 * Return amount of bytes we can take at this offset
59 static int linear_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
61 mddev_t *mddev = q->queuedata;
63 unsigned long maxsectors, bio_sectors = bio->bi_size >> 9;
64 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
66 dev0 = which_dev(mddev, sector);
67 maxsectors = (dev0->size << 1) - (sector - (dev0->offset<<1));
69 if (maxsectors < bio_sectors)
72 maxsectors -= bio_sectors;
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)))
80 return maxsectors << 9;
83 static void linear_unplug(request_queue_t *q)
85 mddev_t *mddev = q->queuedata;
86 linear_conf_t *conf = mddev_to_conf(mddev);
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);
97 static int linear_run (mddev_t *mddev)
100 struct linear_hash *table;
102 int size, i, nb_zone, cnt;
103 unsigned int curr_offset;
104 struct list_head *tmp;
106 conf = kmalloc (sizeof (*conf) + mddev->raid_disks*sizeof(dev_info_t),
110 memset(conf, 0, sizeof(*conf) + mddev->raid_disks*sizeof(dev_info_t));
111 mddev->private = conf;
114 * Find the smallest device.
117 conf->smallest = NULL;
119 mddev->array_size = 0;
121 ITERATE_RDEV(mddev,rdev,tmp) {
122 int j = rdev->raid_disk;
123 dev_info_t *disk = conf->disks + j;
125 if (j < 0 || j > mddev->raid_disks || disk->rdev) {
126 printk("linear: disk numbering problem. Aborting!\n");
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.
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);
142 disk->size = rdev->size;
143 mddev->array_size += rdev->size;
145 if (!conf->smallest || (disk->size < conf->smallest->size))
146 conf->smallest = disk;
149 if (cnt != mddev->raid_disks) {
150 printk("linear: not enough drives present. Aborting!\n");
155 * This code was restructured to work around a gcc-2.95.3 internal
156 * compiler error. Alter it with care.
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);
169 conf->hash_table = kmalloc (sizeof (struct linear_hash) * nb_zone,
171 if (!conf->hash_table)
175 * Here we generate the linear hash table
177 table = conf->hash_table;
180 for (i = 0; i < cnt; i++) {
181 dev_info_t *disk = conf->disks + i;
183 disk->offset = curr_offset;
184 curr_offset += disk->size;
187 table[-1].dev1 = disk;
194 size -= conf->smallest->size;
198 if (table-conf->hash_table != nb_zone)
201 blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
202 mddev->queue->unplug_fn = linear_unplug;
211 static int linear_stop (mddev_t *mddev)
213 linear_conf_t *conf = mddev_to_conf(mddev);
215 kfree(conf->hash_table);
221 static int linear_make_request (request_queue_t *q, struct bio *bio)
223 mddev_t *mddev = q->queuedata;
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));
231 disk_stat_inc(mddev->gendisk, reads);
232 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
235 tmp_dev = which_dev(mddev, bio->bi_sector);
236 block = bio->bi_sector >> 1;
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);
245 if (unlikely(block >= (tmp_dev->size + tmp_dev->offset)
246 || block < tmp_dev->offset)) {
247 char b[BDEVNAME_SIZE];
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);
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
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);
274 bio->bi_bdev = tmp_dev->rdev->bdev;
275 bio->bi_sector = bio->bi_sector - (tmp_dev->offset << 1) + tmp_dev->rdev->data_offset;
280 static void linear_status (struct seq_file *seq, mddev_t *mddev)
286 linear_conf_t *conf = mddev_to_conf(mddev);
288 seq_printf(seq, " ");
289 for (j = 0; j < conf->nr_zones; j++)
291 char b[BDEVNAME_SIZE];
292 seq_printf(seq, "[%s",
293 bdevname(conf->hash_table[j].dev0->rdev->bdev,b));
295 if (conf->hash_table[j].dev1)
296 seq_printf(seq, "/%s] ",
297 bdevname(conf->hash_table[j].dev1->rdev->bdev,b));
299 seq_printf(seq, "] ");
301 seq_printf(seq, "\n");
303 seq_printf(seq, " %dk rounding", mddev->chunk_size/1024);
307 static mdk_personality_t linear_personality=
310 .owner = THIS_MODULE,
311 .make_request = linear_make_request,
314 .status = linear_status,
317 static int __init linear_init (void)
319 return register_md_personality (LINEAR, &linear_personality);
322 static void linear_exit (void)
324 unregister_md_personality (LINEAR);
328 module_init(linear_init);
329 module_exit(linear_exit);
330 MODULE_LICENSE("GPL");
331 MODULE_ALIAS("md-personality-1"); /* LINEAR */