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/blkdev.h>
20 #include <linux/raid/md_u.h>
21 #include <linux/seq_file.h>
26 * find which device holds a particular offset
28 static inline dev_info_t *which_dev(mddev_t *mddev, sector_t sector)
31 linear_conf_t *conf = mddev->private;
34 hi = mddev->raid_disks - 1;
43 if (sector < conf->disks[mid].end_sector)
49 return conf->disks + lo;
53 * linear_mergeable_bvec -- tell bio layer if two requests can be merged
55 * @bvm: properties of new bio
56 * @biovec: the request that could be merged to it.
58 * Return amount of bytes we can take at this offset
60 static int linear_mergeable_bvec(struct request_queue *q,
61 struct bvec_merge_data *bvm,
62 struct bio_vec *biovec)
64 mddev_t *mddev = q->queuedata;
66 unsigned long maxsectors, bio_sectors = bvm->bi_size >> 9;
67 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
69 dev0 = which_dev(mddev, sector);
70 maxsectors = dev0->end_sector - sector;
72 if (maxsectors < bio_sectors)
75 maxsectors -= bio_sectors;
77 if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0)
78 return biovec->bv_len;
79 /* The bytes available at this offset could be really big,
80 * so we cap at 2^31 to avoid overflow */
81 if (maxsectors > (1 << (31-9)))
83 return maxsectors << 9;
86 static void linear_unplug(struct request_queue *q)
88 mddev_t *mddev = q->queuedata;
89 linear_conf_t *conf = mddev->private;
92 for (i=0; i < mddev->raid_disks; i++) {
93 struct request_queue *r_queue = bdev_get_queue(conf->disks[i].rdev->bdev);
98 static int linear_congested(void *data, int bits)
100 mddev_t *mddev = data;
101 linear_conf_t *conf = mddev->private;
104 for (i = 0; i < mddev->raid_disks && !ret ; i++) {
105 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
106 ret |= bdi_congested(&q->backing_dev_info, bits);
111 static sector_t linear_size(mddev_t *mddev, sector_t sectors, int raid_disks)
113 linear_conf_t *conf = mddev->private;
115 WARN_ONCE(sectors || raid_disks,
116 "%s does not support generic reshape\n", __func__);
118 return conf->array_sectors;
121 static linear_conf_t *linear_conf(mddev_t *mddev, int raid_disks)
127 conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(dev_info_t),
133 conf->array_sectors = 0;
135 list_for_each_entry(rdev, &mddev->disks, same_set) {
136 int j = rdev->raid_disk;
137 dev_info_t *disk = conf->disks + j;
139 if (j < 0 || j >= raid_disks || disk->rdev) {
140 printk("linear: disk numbering problem. Aborting!\n");
146 blk_queue_stack_limits(mddev->queue,
147 rdev->bdev->bd_disk->queue);
148 /* as we don't honour merge_bvec_fn, we must never risk
149 * violating it, so limit ->max_sector to one PAGE, as
150 * a one page request is never in violation.
152 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
153 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
154 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
156 conf->array_sectors += rdev->sectors;
160 if (cnt != raid_disks) {
161 printk("linear: not enough drives present. Aborting!\n");
166 * Here we calculate the device offsets.
168 conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
170 for (i = 1; i < raid_disks; i++)
171 conf->disks[i].end_sector =
172 conf->disks[i-1].end_sector +
173 conf->disks[i].rdev->sectors;
182 static int linear_run (mddev_t *mddev)
186 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
187 conf = linear_conf(mddev, mddev->raid_disks);
191 mddev->private = conf;
192 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
194 blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
195 mddev->queue->unplug_fn = linear_unplug;
196 mddev->queue->backing_dev_info.congested_fn = linear_congested;
197 mddev->queue->backing_dev_info.congested_data = mddev;
201 static int linear_add(mddev_t *mddev, mdk_rdev_t *rdev)
203 /* Adding a drive to a linear array allows the array to grow.
204 * It is permitted if the new drive has a matching superblock
205 * already on it, with raid_disk equal to raid_disks.
206 * It is achieved by creating a new linear_private_data structure
207 * and swapping it in in-place of the current one.
208 * The current one is never freed until the array is stopped.
211 linear_conf_t *newconf;
213 if (rdev->saved_raid_disk != mddev->raid_disks)
216 rdev->raid_disk = rdev->saved_raid_disk;
218 newconf = linear_conf(mddev,mddev->raid_disks+1);
223 newconf->prev = mddev->private;
224 mddev->private = newconf;
226 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
227 set_capacity(mddev->gendisk, mddev->array_sectors);
231 static int linear_stop (mddev_t *mddev)
233 linear_conf_t *conf = mddev->private;
235 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
237 linear_conf_t *t = conf->prev;
245 static int linear_make_request (struct request_queue *q, struct bio *bio)
247 const int rw = bio_data_dir(bio);
248 mddev_t *mddev = q->queuedata;
250 sector_t start_sector;
253 if (unlikely(bio_barrier(bio))) {
254 bio_endio(bio, -EOPNOTSUPP);
258 cpu = part_stat_lock();
259 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
260 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
264 tmp_dev = which_dev(mddev, bio->bi_sector);
265 start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
267 if (unlikely(bio->bi_sector >= (tmp_dev->end_sector)
268 || (bio->bi_sector < start_sector))) {
269 char b[BDEVNAME_SIZE];
271 printk("linear_make_request: Sector %llu out of bounds on "
272 "dev %s: %llu sectors, offset %llu\n",
273 (unsigned long long)bio->bi_sector,
274 bdevname(tmp_dev->rdev->bdev, b),
275 (unsigned long long)tmp_dev->rdev->sectors,
276 (unsigned long long)start_sector);
280 if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
281 tmp_dev->end_sector)) {
282 /* This bio crosses a device boundary, so we have to
288 tmp_dev->end_sector - bio->bi_sector);
290 if (linear_make_request(q, &bp->bio1))
291 generic_make_request(&bp->bio1);
292 if (linear_make_request(q, &bp->bio2))
293 generic_make_request(&bp->bio2);
294 bio_pair_release(bp);
298 bio->bi_bdev = tmp_dev->rdev->bdev;
299 bio->bi_sector = bio->bi_sector - start_sector
300 + tmp_dev->rdev->data_offset;
305 static void linear_status (struct seq_file *seq, mddev_t *mddev)
308 seq_printf(seq, " %dk rounding", mddev->chunk_size/1024);
312 static struct mdk_personality linear_personality =
315 .level = LEVEL_LINEAR,
316 .owner = THIS_MODULE,
317 .make_request = linear_make_request,
320 .status = linear_status,
321 .hot_add_disk = linear_add,
325 static int __init linear_init (void)
327 return register_md_personality (&linear_personality);
330 static void linear_exit (void)
332 unregister_md_personality (&linear_personality);
336 module_init(linear_init);
337 module_exit(linear_exit);
338 MODULE_LICENSE("GPL");
339 MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
340 MODULE_ALIAS("md-linear");
341 MODULE_ALIAS("md-level--1");