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))
51 * linear_mergeable_bvec -- tell bio layer if a two requests can be merged
53 * @bio: the buffer head that's been built up so far
54 * @biovec: the request that could be merged to it.
56 * Return amount of bytes we can take at this offset
58 static int linear_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
60 mddev_t *mddev = q->queuedata;
62 unsigned long maxsectors, bio_sectors = bio->bi_size >> 9;
63 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
65 dev0 = which_dev(mddev, sector);
66 maxsectors = (dev0->size << 1) - (sector - (dev0->offset<<1));
68 if (maxsectors < bio_sectors)
71 maxsectors -= bio_sectors;
73 if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0)
74 return biovec->bv_len;
75 /* The bytes available at this offset could be really big,
76 * so we cap at 2^31 to avoid overflow */
77 if (maxsectors > (1 << (31-9)))
79 return maxsectors << 9;
82 static void linear_unplug(request_queue_t *q)
84 mddev_t *mddev = q->queuedata;
85 linear_conf_t *conf = mddev_to_conf(mddev);
88 for (i=0; i < mddev->raid_disks; i++) {
89 request_queue_t *r_queue = bdev_get_queue(conf->disks[i].rdev->bdev);
90 if (r_queue->unplug_fn)
91 r_queue->unplug_fn(r_queue);
95 static int linear_issue_flush(request_queue_t *q, struct gendisk *disk,
96 sector_t *error_sector)
98 mddev_t *mddev = q->queuedata;
99 linear_conf_t *conf = mddev_to_conf(mddev);
102 for (i=0; i < mddev->raid_disks; i++) {
103 struct block_device *bdev = conf->disks[i].rdev->bdev;
104 request_queue_t *r_queue = bdev_get_queue(bdev);
106 if (!r_queue->issue_flush_fn) {
110 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, error_sector);
117 static int linear_run (mddev_t *mddev)
120 struct linear_hash *table;
124 unsigned int curr_offset;
125 struct list_head *tmp;
127 conf = kmalloc (sizeof (*conf) + mddev->raid_disks*sizeof(dev_info_t),
131 memset(conf, 0, sizeof(*conf) + mddev->raid_disks*sizeof(dev_info_t));
132 mddev->private = conf;
135 * Find the smallest device.
138 conf->smallest = NULL;
140 mddev->array_size = 0;
142 ITERATE_RDEV(mddev,rdev,tmp) {
143 int j = rdev->raid_disk;
144 dev_info_t *disk = conf->disks + j;
146 if (j < 0 || j > mddev->raid_disks || disk->rdev) {
147 printk("linear: disk numbering problem. Aborting!\n");
153 blk_queue_stack_limits(mddev->queue,
154 rdev->bdev->bd_disk->queue);
155 /* as we don't honour merge_bvec_fn, we must never risk
156 * violating it, so limit ->max_sector to one PAGE, as
157 * a one page request is never in violation.
159 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
160 mddev->queue->max_sectors > (PAGE_SIZE>>9))
161 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
163 disk->size = rdev->size;
164 mddev->array_size += rdev->size;
166 if (!conf->smallest || (disk->size < conf->smallest->size))
167 conf->smallest = disk;
170 if (cnt != mddev->raid_disks) {
171 printk("linear: not enough drives present. Aborting!\n");
176 * This code was restructured to work around a gcc-2.95.3 internal
177 * compiler error. Alter it with care.
184 sz = mddev->array_size;
185 base = conf->smallest->size;
186 round = sector_div(sz, base);
187 nb_zone = conf->nr_zones = sz + (round ? 1 : 0);
190 conf->hash_table = kmalloc (sizeof (struct linear_hash) * nb_zone,
192 if (!conf->hash_table)
196 * Here we generate the linear hash table
198 table = conf->hash_table;
201 for (i = 0; i < cnt; i++) {
202 dev_info_t *disk = conf->disks + i;
204 disk->offset = curr_offset;
205 curr_offset += disk->size;
208 table[-1].dev1 = disk;
215 size -= conf->smallest->size;
219 if (table-conf->hash_table != nb_zone)
222 blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
223 mddev->queue->unplug_fn = linear_unplug;
224 mddev->queue->issue_flush_fn = linear_issue_flush;
233 static int linear_stop (mddev_t *mddev)
235 linear_conf_t *conf = mddev_to_conf(mddev);
237 kfree(conf->hash_table);
243 static int linear_make_request (request_queue_t *q, struct bio *bio)
245 mddev_t *mddev = q->queuedata;
249 if (bio_data_dir(bio)==WRITE) {
250 disk_stat_inc(mddev->gendisk, writes);
251 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
253 disk_stat_inc(mddev->gendisk, reads);
254 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
257 tmp_dev = which_dev(mddev, bio->bi_sector);
258 block = bio->bi_sector >> 1;
260 if (unlikely(!tmp_dev)) {
261 printk("linear_make_request: hash->dev1==NULL for block %llu\n",
262 (unsigned long long)block);
263 bio_io_error(bio, bio->bi_size);
267 if (unlikely(block >= (tmp_dev->size + tmp_dev->offset)
268 || block < tmp_dev->offset)) {
269 char b[BDEVNAME_SIZE];
271 printk("linear_make_request: Block %llu out of bounds on "
272 "dev %s size %llu offset %llu\n",
273 (unsigned long long)block,
274 bdevname(tmp_dev->rdev->bdev, b),
275 (unsigned long long)tmp_dev->size,
276 (unsigned long long)tmp_dev->offset);
277 bio_io_error(bio, bio->bi_size);
280 if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
281 (tmp_dev->offset + tmp_dev->size)<<1)) {
282 /* This bio crosses a device boundary, so we have to
286 bp = bio_split(bio, bio_split_pool,
287 (bio->bi_sector + (bio->bi_size >> 9) -
288 (tmp_dev->offset + tmp_dev->size))<<1);
289 if (linear_make_request(q, &bp->bio1))
290 generic_make_request(&bp->bio1);
291 if (linear_make_request(q, &bp->bio2))
292 generic_make_request(&bp->bio2);
293 bio_pair_release(bp);
297 bio->bi_bdev = tmp_dev->rdev->bdev;
298 bio->bi_sector = bio->bi_sector - (tmp_dev->offset << 1) + tmp_dev->rdev->data_offset;
303 static void linear_status (struct seq_file *seq, mddev_t *mddev)
309 linear_conf_t *conf = mddev_to_conf(mddev);
311 seq_printf(seq, " ");
312 for (j = 0; j < conf->nr_zones; j++)
314 char b[BDEVNAME_SIZE];
315 seq_printf(seq, "[%s",
316 bdevname(conf->hash_table[j].dev0->rdev->bdev,b));
318 if (conf->hash_table[j].dev1)
319 seq_printf(seq, "/%s] ",
320 bdevname(conf->hash_table[j].dev1->rdev->bdev,b));
322 seq_printf(seq, "] ");
324 seq_printf(seq, "\n");
326 seq_printf(seq, " %dk rounding", mddev->chunk_size/1024);
330 static mdk_personality_t linear_personality=
333 .owner = THIS_MODULE,
334 .make_request = linear_make_request,
337 .status = linear_status,
340 static int __init linear_init (void)
342 return register_md_personality (LINEAR, &linear_personality);
345 static void linear_exit (void)
347 unregister_md_personality (LINEAR);
351 module_init(linear_init);
352 module_exit(linear_exit);
353 MODULE_LICENSE("GPL");
354 MODULE_ALIAS("md-personality-1"); /* LINEAR */