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;
122 int size, i, nb_zone, cnt;
123 unsigned int curr_offset;
124 struct list_head *tmp;
126 conf = kmalloc (sizeof (*conf) + mddev->raid_disks*sizeof(dev_info_t),
130 memset(conf, 0, sizeof(*conf) + mddev->raid_disks*sizeof(dev_info_t));
131 mddev->private = conf;
134 * Find the smallest device.
137 conf->smallest = NULL;
139 mddev->array_size = 0;
141 ITERATE_RDEV(mddev,rdev,tmp) {
142 int j = rdev->raid_disk;
143 dev_info_t *disk = conf->disks + j;
145 if (j < 0 || j > mddev->raid_disks || disk->rdev) {
146 printk("linear: disk numbering problem. Aborting!\n");
152 blk_queue_stack_limits(mddev->queue,
153 rdev->bdev->bd_disk->queue);
154 /* as we don't honour merge_bvec_fn, we must never risk
155 * violating it, so limit ->max_sector to one PAGE, as
156 * a one page request is never in violation.
158 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
159 mddev->queue->max_sectors > (PAGE_SIZE>>9))
160 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
162 disk->size = rdev->size;
163 mddev->array_size += rdev->size;
165 if (!conf->smallest || (disk->size < conf->smallest->size))
166 conf->smallest = disk;
169 if (cnt != mddev->raid_disks) {
170 printk("linear: not enough drives present. Aborting!\n");
175 * This code was restructured to work around a gcc-2.95.3 internal
176 * compiler error. Alter it with care.
183 sz = mddev->array_size;
184 base = conf->smallest->size;
185 round = sector_div(sz, base);
186 nb_zone = conf->nr_zones = sz + (round ? 1 : 0);
189 conf->hash_table = kmalloc (sizeof (struct linear_hash) * nb_zone,
191 if (!conf->hash_table)
195 * Here we generate the linear hash table
197 table = conf->hash_table;
200 for (i = 0; i < cnt; i++) {
201 dev_info_t *disk = conf->disks + i;
203 disk->offset = curr_offset;
204 curr_offset += disk->size;
207 table[-1].dev1 = disk;
214 size -= conf->smallest->size;
218 if (table-conf->hash_table != nb_zone)
221 blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
222 mddev->queue->unplug_fn = linear_unplug;
223 mddev->queue->issue_flush_fn = linear_issue_flush;
232 static int linear_stop (mddev_t *mddev)
234 linear_conf_t *conf = mddev_to_conf(mddev);
236 kfree(conf->hash_table);
242 static int linear_make_request (request_queue_t *q, struct bio *bio)
244 mddev_t *mddev = q->queuedata;
248 if (bio_data_dir(bio)==WRITE) {
249 disk_stat_inc(mddev->gendisk, writes);
250 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
252 disk_stat_inc(mddev->gendisk, reads);
253 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
256 tmp_dev = which_dev(mddev, bio->bi_sector);
257 block = bio->bi_sector >> 1;
259 if (unlikely(!tmp_dev)) {
260 printk("linear_make_request: hash->dev1==NULL for block %llu\n",
261 (unsigned long long)block);
262 bio_io_error(bio, bio->bi_size);
266 if (unlikely(block >= (tmp_dev->size + tmp_dev->offset)
267 || block < tmp_dev->offset)) {
268 char b[BDEVNAME_SIZE];
270 printk("linear_make_request: Block %llu out of bounds on "
271 "dev %s size %ld offset %ld\n",
272 (unsigned long long)block,
273 bdevname(tmp_dev->rdev->bdev, b),
274 tmp_dev->size, tmp_dev->offset);
275 bio_io_error(bio, bio->bi_size);
278 if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
279 (tmp_dev->offset + tmp_dev->size)<<1)) {
280 /* This bio crosses a device boundary, so we have to
284 bp = bio_split(bio, bio_split_pool,
285 (bio->bi_sector + (bio->bi_size >> 9) -
286 (tmp_dev->offset + tmp_dev->size))<<1);
287 if (linear_make_request(q, &bp->bio1))
288 generic_make_request(&bp->bio1);
289 if (linear_make_request(q, &bp->bio2))
290 generic_make_request(&bp->bio2);
291 bio_pair_release(bp);
295 bio->bi_bdev = tmp_dev->rdev->bdev;
296 bio->bi_sector = bio->bi_sector - (tmp_dev->offset << 1) + tmp_dev->rdev->data_offset;
301 static void linear_status (struct seq_file *seq, mddev_t *mddev)
307 linear_conf_t *conf = mddev_to_conf(mddev);
309 seq_printf(seq, " ");
310 for (j = 0; j < conf->nr_zones; j++)
312 char b[BDEVNAME_SIZE];
313 seq_printf(seq, "[%s",
314 bdevname(conf->hash_table[j].dev0->rdev->bdev,b));
316 if (conf->hash_table[j].dev1)
317 seq_printf(seq, "/%s] ",
318 bdevname(conf->hash_table[j].dev1->rdev->bdev,b));
320 seq_printf(seq, "] ");
322 seq_printf(seq, "\n");
324 seq_printf(seq, " %dk rounding", mddev->chunk_size/1024);
328 static mdk_personality_t linear_personality=
331 .owner = THIS_MODULE,
332 .make_request = linear_make_request,
335 .status = linear_status,
338 static int __init linear_init (void)
340 return register_md_personality (LINEAR, &linear_personality);
343 static void linear_exit (void)
345 unregister_md_personality (LINEAR);
349 module_init(linear_init);
350 module_exit(linear_exit);
351 MODULE_LICENSE("GPL");
352 MODULE_ALIAS("md-personality-1"); /* LINEAR */