]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
bff61975 N |
19 | #include <linux/blkdev.h> |
20 | #include <linux/raid/md_u.h> | |
bff61975 | 21 | #include <linux/seq_file.h> |
43b2e5d8 | 22 | #include "md.h" |
ef740c37 | 23 | #include "linear.h" |
1da177e4 | 24 | |
1da177e4 LT |
25 | /* |
26 | * find which device holds a particular offset | |
27 | */ | |
28 | static inline dev_info_t *which_dev(mddev_t *mddev, sector_t sector) | |
29 | { | |
30 | dev_info_t *hash; | |
070ec55d | 31 | linear_conf_t *conf = mddev->private; |
1da177e4 | 32 | |
45d4582f | 33 | hash = conf->disks; |
1da177e4 | 34 | |
6283815d | 35 | while (sector >= hash->num_sectors + hash->start_sector) |
1da177e4 LT |
36 | hash++; |
37 | return hash; | |
38 | } | |
39 | ||
40 | /** | |
15945fee | 41 | * linear_mergeable_bvec -- tell bio layer if two requests can be merged |
1da177e4 | 42 | * @q: request queue |
cc371e66 | 43 | * @bvm: properties of new bio |
1da177e4 LT |
44 | * @biovec: the request that could be merged to it. |
45 | * | |
46 | * Return amount of bytes we can take at this offset | |
47 | */ | |
cc371e66 AK |
48 | static int linear_mergeable_bvec(struct request_queue *q, |
49 | struct bvec_merge_data *bvm, | |
50 | struct bio_vec *biovec) | |
1da177e4 LT |
51 | { |
52 | mddev_t *mddev = q->queuedata; | |
53 | dev_info_t *dev0; | |
cc371e66 AK |
54 | unsigned long maxsectors, bio_sectors = bvm->bi_size >> 9; |
55 | sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); | |
1da177e4 LT |
56 | |
57 | dev0 = which_dev(mddev, sector); | |
6283815d | 58 | maxsectors = dev0->num_sectors - (sector - dev0->start_sector); |
1da177e4 LT |
59 | |
60 | if (maxsectors < bio_sectors) | |
61 | maxsectors = 0; | |
62 | else | |
63 | maxsectors -= bio_sectors; | |
64 | ||
65 | if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0) | |
66 | return biovec->bv_len; | |
67 | /* The bytes available at this offset could be really big, | |
68 | * so we cap at 2^31 to avoid overflow */ | |
69 | if (maxsectors > (1 << (31-9))) | |
70 | return 1<<31; | |
71 | return maxsectors << 9; | |
72 | } | |
73 | ||
165125e1 | 74 | static void linear_unplug(struct request_queue *q) |
1da177e4 LT |
75 | { |
76 | mddev_t *mddev = q->queuedata; | |
070ec55d | 77 | linear_conf_t *conf = mddev->private; |
1da177e4 LT |
78 | int i; |
79 | ||
80 | for (i=0; i < mddev->raid_disks; i++) { | |
165125e1 | 81 | struct request_queue *r_queue = bdev_get_queue(conf->disks[i].rdev->bdev); |
2ad8b1ef | 82 | blk_unplug(r_queue); |
1da177e4 LT |
83 | } |
84 | } | |
85 | ||
26be34dc N |
86 | static int linear_congested(void *data, int bits) |
87 | { | |
88 | mddev_t *mddev = data; | |
070ec55d | 89 | linear_conf_t *conf = mddev->private; |
26be34dc N |
90 | int i, ret = 0; |
91 | ||
92 | for (i = 0; i < mddev->raid_disks && !ret ; i++) { | |
165125e1 | 93 | struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev); |
26be34dc N |
94 | ret |= bdi_congested(&q->backing_dev_info, bits); |
95 | } | |
96 | return ret; | |
97 | } | |
98 | ||
80c3a6ce DW |
99 | static sector_t linear_size(mddev_t *mddev, sector_t sectors, int raid_disks) |
100 | { | |
070ec55d | 101 | linear_conf_t *conf = mddev->private; |
80c3a6ce DW |
102 | |
103 | WARN_ONCE(sectors || raid_disks, | |
104 | "%s does not support generic reshape\n", __func__); | |
105 | ||
106 | return conf->array_sectors; | |
107 | } | |
108 | ||
7c7546cc | 109 | static linear_conf_t *linear_conf(mddev_t *mddev, int raid_disks) |
1da177e4 LT |
110 | { |
111 | linear_conf_t *conf; | |
1da177e4 | 112 | mdk_rdev_t *rdev; |
45d4582f | 113 | int i, cnt; |
1da177e4 | 114 | |
7c7546cc | 115 | conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(dev_info_t), |
1da177e4 LT |
116 | GFP_KERNEL); |
117 | if (!conf) | |
7c7546cc N |
118 | return NULL; |
119 | ||
1da177e4 | 120 | cnt = 0; |
d6e22150 | 121 | conf->array_sectors = 0; |
1da177e4 | 122 | |
159ec1fc | 123 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
1da177e4 LT |
124 | int j = rdev->raid_disk; |
125 | dev_info_t *disk = conf->disks + j; | |
126 | ||
13864515 | 127 | if (j < 0 || j >= raid_disks || disk->rdev) { |
1da177e4 LT |
128 | printk("linear: disk numbering problem. Aborting!\n"); |
129 | goto out; | |
130 | } | |
131 | ||
132 | disk->rdev = rdev; | |
133 | ||
134 | blk_queue_stack_limits(mddev->queue, | |
135 | rdev->bdev->bd_disk->queue); | |
136 | /* as we don't honour merge_bvec_fn, we must never risk | |
137 | * violating it, so limit ->max_sector to one PAGE, as | |
138 | * a one page request is never in violation. | |
139 | */ | |
140 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn && | |
ae03bf63 | 141 | queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9)) |
1da177e4 LT |
142 | blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9); |
143 | ||
dd8ac336 AN |
144 | disk->num_sectors = rdev->sectors; |
145 | conf->array_sectors += rdev->sectors; | |
1da177e4 | 146 | |
1da177e4 LT |
147 | cnt++; |
148 | } | |
7c7546cc | 149 | if (cnt != raid_disks) { |
1da177e4 LT |
150 | printk("linear: not enough drives present. Aborting!\n"); |
151 | goto out; | |
152 | } | |
153 | ||
154 | /* | |
45d4582f | 155 | * Here we calculate the device offsets. |
1da177e4 | 156 | */ |
6283815d | 157 | conf->disks[0].start_sector = 0; |
a778b73f | 158 | for (i = 1; i < raid_disks; i++) |
6283815d AN |
159 | conf->disks[i].start_sector = |
160 | conf->disks[i-1].start_sector + | |
161 | conf->disks[i-1].num_sectors; | |
15945fee | 162 | |
7c7546cc N |
163 | return conf; |
164 | ||
165 | out: | |
166 | kfree(conf); | |
167 | return NULL; | |
168 | } | |
169 | ||
170 | static int linear_run (mddev_t *mddev) | |
171 | { | |
172 | linear_conf_t *conf; | |
173 | ||
e7e72bf6 | 174 | mddev->queue->queue_lock = &mddev->queue->__queue_lock; |
7c7546cc N |
175 | conf = linear_conf(mddev, mddev->raid_disks); |
176 | ||
177 | if (!conf) | |
178 | return 1; | |
179 | mddev->private = conf; | |
1f403624 | 180 | md_set_array_sectors(mddev, linear_size(mddev, 0, 0)); |
7c7546cc | 181 | |
1da177e4 LT |
182 | blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec); |
183 | mddev->queue->unplug_fn = linear_unplug; | |
26be34dc N |
184 | mddev->queue->backing_dev_info.congested_fn = linear_congested; |
185 | mddev->queue->backing_dev_info.congested_data = mddev; | |
1da177e4 | 186 | return 0; |
7c7546cc | 187 | } |
1da177e4 | 188 | |
7c7546cc N |
189 | static int linear_add(mddev_t *mddev, mdk_rdev_t *rdev) |
190 | { | |
191 | /* Adding a drive to a linear array allows the array to grow. | |
192 | * It is permitted if the new drive has a matching superblock | |
193 | * already on it, with raid_disk equal to raid_disks. | |
194 | * It is achieved by creating a new linear_private_data structure | |
195 | * and swapping it in in-place of the current one. | |
196 | * The current one is never freed until the array is stopped. | |
197 | * This avoids races. | |
198 | */ | |
199 | linear_conf_t *newconf; | |
200 | ||
a778b73f | 201 | if (rdev->saved_raid_disk != mddev->raid_disks) |
7c7546cc N |
202 | return -EINVAL; |
203 | ||
a778b73f N |
204 | rdev->raid_disk = rdev->saved_raid_disk; |
205 | ||
7c7546cc N |
206 | newconf = linear_conf(mddev,mddev->raid_disks+1); |
207 | ||
208 | if (!newconf) | |
209 | return -ENOMEM; | |
210 | ||
070ec55d | 211 | newconf->prev = mddev->private; |
7c7546cc N |
212 | mddev->private = newconf; |
213 | mddev->raid_disks++; | |
1f403624 | 214 | md_set_array_sectors(mddev, linear_size(mddev, 0, 0)); |
f233ea5c | 215 | set_capacity(mddev->gendisk, mddev->array_sectors); |
7c7546cc | 216 | return 0; |
1da177e4 LT |
217 | } |
218 | ||
219 | static int linear_stop (mddev_t *mddev) | |
220 | { | |
070ec55d | 221 | linear_conf_t *conf = mddev->private; |
1da177e4 LT |
222 | |
223 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ | |
7c7546cc N |
224 | do { |
225 | linear_conf_t *t = conf->prev; | |
7c7546cc N |
226 | kfree(conf); |
227 | conf = t; | |
228 | } while (conf); | |
1da177e4 LT |
229 | |
230 | return 0; | |
231 | } | |
232 | ||
165125e1 | 233 | static int linear_make_request (struct request_queue *q, struct bio *bio) |
1da177e4 | 234 | { |
a362357b | 235 | const int rw = bio_data_dir(bio); |
1da177e4 LT |
236 | mddev_t *mddev = q->queuedata; |
237 | dev_info_t *tmp_dev; | |
c9959059 | 238 | int cpu; |
1da177e4 | 239 | |
e5dcdd80 | 240 | if (unlikely(bio_barrier(bio))) { |
6712ecf8 | 241 | bio_endio(bio, -EOPNOTSUPP); |
e5dcdd80 N |
242 | return 0; |
243 | } | |
244 | ||
074a7aca TH |
245 | cpu = part_stat_lock(); |
246 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); | |
247 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], | |
248 | bio_sectors(bio)); | |
249 | part_stat_unlock(); | |
1da177e4 LT |
250 | |
251 | tmp_dev = which_dev(mddev, bio->bi_sector); | |
1da177e4 | 252 | |
6283815d AN |
253 | if (unlikely(bio->bi_sector >= (tmp_dev->num_sectors + |
254 | tmp_dev->start_sector) | |
255 | || (bio->bi_sector < | |
256 | tmp_dev->start_sector))) { | |
1da177e4 LT |
257 | char b[BDEVNAME_SIZE]; |
258 | ||
6283815d AN |
259 | printk("linear_make_request: Sector %llu out of bounds on " |
260 | "dev %s: %llu sectors, offset %llu\n", | |
261 | (unsigned long long)bio->bi_sector, | |
1da177e4 | 262 | bdevname(tmp_dev->rdev->bdev, b), |
6283815d AN |
263 | (unsigned long long)tmp_dev->num_sectors, |
264 | (unsigned long long)tmp_dev->start_sector); | |
6712ecf8 | 265 | bio_io_error(bio); |
1da177e4 LT |
266 | return 0; |
267 | } | |
268 | if (unlikely(bio->bi_sector + (bio->bi_size >> 9) > | |
6283815d | 269 | tmp_dev->start_sector + tmp_dev->num_sectors)) { |
1da177e4 LT |
270 | /* This bio crosses a device boundary, so we have to |
271 | * split it. | |
272 | */ | |
273 | struct bio_pair *bp; | |
6283815d | 274 | |
6feef531 | 275 | bp = bio_split(bio, |
6283815d AN |
276 | tmp_dev->start_sector + tmp_dev->num_sectors |
277 | - bio->bi_sector); | |
278 | ||
1da177e4 LT |
279 | if (linear_make_request(q, &bp->bio1)) |
280 | generic_make_request(&bp->bio1); | |
281 | if (linear_make_request(q, &bp->bio2)) | |
282 | generic_make_request(&bp->bio2); | |
283 | bio_pair_release(bp); | |
284 | return 0; | |
285 | } | |
286 | ||
287 | bio->bi_bdev = tmp_dev->rdev->bdev; | |
6283815d AN |
288 | bio->bi_sector = bio->bi_sector - tmp_dev->start_sector |
289 | + tmp_dev->rdev->data_offset; | |
1da177e4 LT |
290 | |
291 | return 1; | |
292 | } | |
293 | ||
294 | static void linear_status (struct seq_file *seq, mddev_t *mddev) | |
295 | { | |
296 | ||
1da177e4 LT |
297 | seq_printf(seq, " %dk rounding", mddev->chunk_size/1024); |
298 | } | |
299 | ||
300 | ||
2604b703 | 301 | static struct mdk_personality linear_personality = |
1da177e4 LT |
302 | { |
303 | .name = "linear", | |
2604b703 | 304 | .level = LEVEL_LINEAR, |
1da177e4 LT |
305 | .owner = THIS_MODULE, |
306 | .make_request = linear_make_request, | |
307 | .run = linear_run, | |
308 | .stop = linear_stop, | |
309 | .status = linear_status, | |
7c7546cc | 310 | .hot_add_disk = linear_add, |
80c3a6ce | 311 | .size = linear_size, |
1da177e4 LT |
312 | }; |
313 | ||
314 | static int __init linear_init (void) | |
315 | { | |
2604b703 | 316 | return register_md_personality (&linear_personality); |
1da177e4 LT |
317 | } |
318 | ||
319 | static void linear_exit (void) | |
320 | { | |
2604b703 | 321 | unregister_md_personality (&linear_personality); |
1da177e4 LT |
322 | } |
323 | ||
324 | ||
325 | module_init(linear_init); | |
326 | module_exit(linear_exit); | |
327 | MODULE_LICENSE("GPL"); | |
d9d166c2 N |
328 | MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/ |
329 | MODULE_ALIAS("md-linear"); | |
2604b703 | 330 | MODULE_ALIAS("md-level--1"); |