blob: 161e9aa872917da3cc3bb2cc4719850317e7b9e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
6
7 Linear mode management functions.
8
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.
13
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.
17*/
18
19#include <linux/module.h>
20
21#include <linux/raid/md.h>
22#include <linux/slab.h>
23#include <linux/raid/linear.h>
24
25#define MAJOR_NR MD_MAJOR
26#define MD_DRIVER
27#define MD_PERSONALITY
28
29/*
30 * find which device holds a particular offset
31 */
32static inline dev_info_t *which_dev(mddev_t *mddev, sector_t sector)
33{
34 dev_info_t *hash;
35 linear_conf_t *conf = mddev_to_conf(mddev);
36 sector_t block = sector >> 1;
37
38 /*
39 * sector_div(a,b) returns the remainer and sets a to a/b
40 */
41 (void)sector_div(block, conf->smallest->size);
42 hash = conf->hash_table[block];
43
44 while ((sector>>1) >= (hash->size + hash->offset))
45 hash++;
46 return hash;
47}
48
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.
54 *
55 * Return amount of bytes we can take at this offset
56 */
57static int linear_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
58{
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);
63
64 dev0 = which_dev(mddev, sector);
65 maxsectors = (dev0->size << 1) - (sector - (dev0->offset<<1));
66
67 if (maxsectors < bio_sectors)
68 maxsectors = 0;
69 else
70 maxsectors -= bio_sectors;
71
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;
79}
80
81static void linear_unplug(request_queue_t *q)
82{
83 mddev_t *mddev = q->queuedata;
84 linear_conf_t *conf = mddev_to_conf(mddev);
85 int i;
86
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);
91 }
92}
93
94static int linear_issue_flush(request_queue_t *q, struct gendisk *disk,
95 sector_t *error_sector)
96{
97 mddev_t *mddev = q->queuedata;
98 linear_conf_t *conf = mddev_to_conf(mddev);
99 int i, ret = 0;
100
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);
104
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);
109 }
110 return ret;
111}
112
113static int linear_run (mddev_t *mddev)
114{
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;
122
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;
129
130 /*
131 * Find the smallest device.
132 */
133
134 conf->smallest = NULL;
135 cnt = 0;
136 mddev->array_size = 0;
137
138 ITERATE_RDEV(mddev,rdev,tmp) {
139 int j = rdev->raid_disk;
140 dev_info_t *disk = conf->disks + j;
141
142 if (j < 0 || j > mddev->raid_disks || disk->rdev) {
143 printk("linear: disk numbering problem. Aborting!\n");
144 goto out;
145 }
146
147 disk->rdev = rdev;
148
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.
154 */
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);
158
159 disk->size = rdev->size;
160 mddev->array_size += rdev->size;
161
162 if (!conf->smallest || (disk->size < conf->smallest->size))
163 conf->smallest = disk;
164 cnt++;
165 }
166 if (cnt != mddev->raid_disks) {
167 printk("linear: not enough drives present. Aborting!\n");
168 goto out;
169 }
170
171 /*
172 * This code was restructured to work around a gcc-2.95.3 internal
173 * compiler error. Alter it with care.
174 */
175 {
176 sector_t sz;
177 unsigned round;
178 unsigned long base;
179
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);
184 }
185
186 conf->hash_table = kmalloc (sizeof (dev_info_t*) * nb_zone,
187 GFP_KERNEL);
188 if (!conf->hash_table)
189 goto out;
190
191 /*
192 * Here we generate the linear hash table
193 */
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;
199
200 disk->offset = curr_offset;
201 curr_offset += disk->size;
202
203 /* 'curr_offset' is the end of this disk
204 * 'start' is the start of table
205 */
206 while (start < curr_offset) {
207 *table++ = disk;
208 start += conf->smallest->size;
209 }
210 }
211 if (table-conf->hash_table != nb_zone)
212 BUG();
213
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;
218
219out:
220 if (conf)
221 kfree(conf);
222 return 1;
223}
224
225static int linear_stop (mddev_t *mddev)
226{
227 linear_conf_t *conf = mddev_to_conf(mddev);
228
229 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
230 kfree(conf->hash_table);
231 kfree(conf);
232
233 return 0;
234}
235
236static int linear_make_request (request_queue_t *q, struct bio *bio)
237{
238 mddev_t *mddev = q->queuedata;
239 dev_info_t *tmp_dev;
240 sector_t block;
241
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));
248 }
249
250 tmp_dev = which_dev(mddev, bio->bi_sector);
251 block = bio->bi_sector >> 1;
252
253 if (unlikely(block >= (tmp_dev->size + tmp_dev->offset)
254 || block < tmp_dev->offset)) {
255 char b[BDEVNAME_SIZE];
256
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;
265 }
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.
270 */
271 struct bio_pair *bp;
272 bp = bio_split(bio, bio_split_pool,
273 (bio->bi_sector + (bio->bi_size >> 9) -
274 (tmp_dev->offset + tmp_dev->size))<<1);
275 if (linear_make_request(q, &bp->bio1))
276 generic_make_request(&bp->bio1);
277 if (linear_make_request(q, &bp->bio2))
278 generic_make_request(&bp->bio2);
279 bio_pair_release(bp);
280 return 0;
281 }
282
283 bio->bi_bdev = tmp_dev->rdev->bdev;
284 bio->bi_sector = bio->bi_sector - (tmp_dev->offset << 1) + tmp_dev->rdev->data_offset;
285
286 return 1;
287}
288
289static void linear_status (struct seq_file *seq, mddev_t *mddev)
290{
291
292#undef MD_DEBUG
293#ifdef MD_DEBUG
294 int j;
295 linear_conf_t *conf = mddev_to_conf(mddev);
296 sector_t s = 0;
297
298 seq_printf(seq, " ");
299 for (j = 0; j < conf->nr_zones; j++)
300 {
301 char b[BDEVNAME_SIZE];
302 s += conf->smallest_size;
303 seq_printf(seq, "[%s",
304 bdevname(conf->hash_table[j][0].rdev->bdev,b));
305
306 while (s > conf->hash_table[j][0].offset +
307 conf->hash_table[j][0].size)
308 seq_printf(seq, "/%s] ",
309 bdevname(conf->hash_table[j][1].rdev->bdev,b));
310 else
311 seq_printf(seq, "] ");
312 }
313 seq_printf(seq, "\n");
314#endif
315 seq_printf(seq, " %dk rounding", mddev->chunk_size/1024);
316}
317
318
319static mdk_personality_t linear_personality=
320{
321 .name = "linear",
322 .owner = THIS_MODULE,
323 .make_request = linear_make_request,
324 .run = linear_run,
325 .stop = linear_stop,
326 .status = linear_status,
327};
328
329static int __init linear_init (void)
330{
331 return register_md_personality (LINEAR, &linear_personality);
332}
333
334static void linear_exit (void)
335{
336 unregister_md_personality (LINEAR);
337}
338
339
340module_init(linear_init);
341module_exit(linear_exit);
342MODULE_LICENSE("GPL");
343MODULE_ALIAS("md-personality-1"); /* LINEAR */