Commit | Line | Data |
---|---|---|
af1a8899 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | linear.c : Multiple Devices driver for Linux | |
4 | Copyright (C) 1994-96 Marc ZYNGIER | |
5 | <zyngier@ufr-info-p7.ibp.fr> or | |
6 | <maz@gloups.fdn.fr> | |
7 | ||
8 | Linear mode management functions. | |
9 | ||
1da177e4 LT |
10 | */ |
11 | ||
bff61975 N |
12 | #include <linux/blkdev.h> |
13 | #include <linux/raid/md_u.h> | |
bff61975 | 14 | #include <linux/seq_file.h> |
056075c7 | 15 | #include <linux/module.h> |
5a0e3ad6 | 16 | #include <linux/slab.h> |
109e3765 | 17 | #include <trace/events/block.h> |
43b2e5d8 | 18 | #include "md.h" |
935fe098 | 19 | #include "md-linear.h" |
1da177e4 | 20 | |
1da177e4 | 21 | /* |
f72ffdd6 | 22 | * find which device holds a particular offset |
1da177e4 | 23 | */ |
a7120771 | 24 | static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector) |
1da177e4 | 25 | { |
aece3d1f | 26 | int lo, mid, hi; |
e849b938 | 27 | struct linear_conf *conf; |
1da177e4 | 28 | |
aece3d1f SS |
29 | lo = 0; |
30 | hi = mddev->raid_disks - 1; | |
3be260cc | 31 | conf = mddev->private; |
1da177e4 | 32 | |
aece3d1f SS |
33 | /* |
34 | * Binary Search | |
35 | */ | |
36 | ||
37 | while (hi > lo) { | |
38 | ||
39 | mid = (hi + lo) / 2; | |
40 | if (sector < conf->disks[mid].end_sector) | |
41 | hi = mid; | |
42 | else | |
43 | lo = mid + 1; | |
44 | } | |
45 | ||
46 | return conf->disks + lo; | |
1da177e4 LT |
47 | } |
48 | ||
03a9e24e | 49 | /* |
50 | * In linear_congested() conf->raid_disks is used as a copy of | |
51 | * mddev->raid_disks to iterate conf->disks[], because conf->raid_disks | |
52 | * and conf->disks[] are created in linear_conf(), they are always | |
53 | * consitent with each other, but mddev->raid_disks does not. | |
54 | */ | |
5c675f83 | 55 | static int linear_congested(struct mddev *mddev, int bits) |
26be34dc | 56 | { |
e849b938 | 57 | struct linear_conf *conf; |
26be34dc N |
58 | int i, ret = 0; |
59 | ||
03a9e24e | 60 | rcu_read_lock(); |
61 | conf = rcu_dereference(mddev->private); | |
af11c397 | 62 | |
03a9e24e | 63 | for (i = 0; i < conf->raid_disks && !ret ; i++) { |
165125e1 | 64 | struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev); |
dc3b17cc | 65 | ret |= bdi_congested(q->backing_dev_info, bits); |
26be34dc | 66 | } |
af11c397 | 67 | |
03a9e24e | 68 | rcu_read_unlock(); |
26be34dc N |
69 | return ret; |
70 | } | |
71 | ||
fd01b88c | 72 | static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
80c3a6ce | 73 | { |
e849b938 | 74 | struct linear_conf *conf; |
af11c397 | 75 | sector_t array_sectors; |
80c3a6ce | 76 | |
3be260cc | 77 | conf = mddev->private; |
80c3a6ce DW |
78 | WARN_ONCE(sectors || raid_disks, |
79 | "%s does not support generic reshape\n", __func__); | |
af11c397 | 80 | array_sectors = conf->array_sectors; |
80c3a6ce | 81 | |
af11c397 | 82 | return array_sectors; |
80c3a6ce DW |
83 | } |
84 | ||
e849b938 | 85 | static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks) |
1da177e4 | 86 | { |
e849b938 | 87 | struct linear_conf *conf; |
3cb03002 | 88 | struct md_rdev *rdev; |
45d4582f | 89 | int i, cnt; |
f1cad2b6 | 90 | bool discard_supported = false; |
1da177e4 | 91 | |
f1e5b623 | 92 | conf = kzalloc(struct_size(conf, disks, raid_disks), GFP_KERNEL); |
1da177e4 | 93 | if (!conf) |
7c7546cc N |
94 | return NULL; |
95 | ||
1da177e4 | 96 | cnt = 0; |
d6e22150 | 97 | conf->array_sectors = 0; |
1da177e4 | 98 | |
dafb20fa | 99 | rdev_for_each(rdev, mddev) { |
1da177e4 | 100 | int j = rdev->raid_disk; |
a7120771 | 101 | struct dev_info *disk = conf->disks + j; |
13f2682b | 102 | sector_t sectors; |
1da177e4 | 103 | |
13864515 | 104 | if (j < 0 || j >= raid_disks || disk->rdev) { |
a2e202af N |
105 | pr_warn("md/linear:%s: disk numbering problem. Aborting!\n", |
106 | mdname(mddev)); | |
1da177e4 LT |
107 | goto out; |
108 | } | |
109 | ||
110 | disk->rdev = rdev; | |
13f2682b N |
111 | if (mddev->chunk_sectors) { |
112 | sectors = rdev->sectors; | |
113 | sector_div(sectors, mddev->chunk_sectors); | |
114 | rdev->sectors = sectors * mddev->chunk_sectors; | |
115 | } | |
1da177e4 | 116 | |
8f6c2e4b MP |
117 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
118 | rdev->data_offset << 9); | |
1da177e4 | 119 | |
dd8ac336 | 120 | conf->array_sectors += rdev->sectors; |
1da177e4 | 121 | cnt++; |
4db7cdc8 | 122 | |
f1cad2b6 SL |
123 | if (blk_queue_discard(bdev_get_queue(rdev->bdev))) |
124 | discard_supported = true; | |
1da177e4 | 125 | } |
7c7546cc | 126 | if (cnt != raid_disks) { |
a2e202af N |
127 | pr_warn("md/linear:%s: not enough drives present. Aborting!\n", |
128 | mdname(mddev)); | |
1da177e4 LT |
129 | goto out; |
130 | } | |
131 | ||
f1cad2b6 | 132 | if (!discard_supported) |
8b904b5b | 133 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, mddev->queue); |
f1cad2b6 | 134 | else |
8b904b5b | 135 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue); |
f1cad2b6 | 136 | |
1da177e4 | 137 | /* |
45d4582f | 138 | * Here we calculate the device offsets. |
1da177e4 | 139 | */ |
4db7cdc8 SS |
140 | conf->disks[0].end_sector = conf->disks[0].rdev->sectors; |
141 | ||
a778b73f | 142 | for (i = 1; i < raid_disks; i++) |
4db7cdc8 SS |
143 | conf->disks[i].end_sector = |
144 | conf->disks[i-1].end_sector + | |
145 | conf->disks[i].rdev->sectors; | |
15945fee | 146 | |
03a9e24e | 147 | /* |
148 | * conf->raid_disks is copy of mddev->raid_disks. The reason to | |
149 | * keep a copy of mddev->raid_disks in struct linear_conf is, | |
150 | * mddev->raid_disks may not be consistent with pointers number of | |
151 | * conf->disks[] when it is updated in linear_add() and used to | |
152 | * iterate old conf->disks[] earray in linear_congested(). | |
153 | * Here conf->raid_disks is always consitent with number of | |
154 | * pointers in conf->disks[] array, and mddev->private is updated | |
155 | * with rcu_assign_pointer() in linear_addr(), such race can be | |
156 | * avoided. | |
157 | */ | |
158 | conf->raid_disks = raid_disks; | |
159 | ||
7c7546cc N |
160 | return conf; |
161 | ||
162 | out: | |
163 | kfree(conf); | |
164 | return NULL; | |
165 | } | |
166 | ||
fd01b88c | 167 | static int linear_run (struct mddev *mddev) |
7c7546cc | 168 | { |
e849b938 | 169 | struct linear_conf *conf; |
98d5561b | 170 | int ret; |
7c7546cc | 171 | |
0894cc30 AN |
172 | if (md_check_no_bitmap(mddev)) |
173 | return -EINVAL; | |
7c7546cc N |
174 | conf = linear_conf(mddev, mddev->raid_disks); |
175 | ||
176 | if (!conf) | |
177 | return 1; | |
178 | mddev->private = conf; | |
1f403624 | 179 | md_set_array_sectors(mddev, linear_size(mddev, 0, 0)); |
7c7546cc | 180 | |
98d5561b | 181 | ret = md_integrity_register(mddev); |
182 | if (ret) { | |
183 | kfree(conf); | |
184 | mddev->private = NULL; | |
185 | } | |
186 | return ret; | |
7c7546cc | 187 | } |
1da177e4 | 188 | |
fd01b88c | 189 | static int linear_add(struct mddev *mddev, struct md_rdev *rdev) |
7c7546cc N |
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 | */ | |
e849b938 | 199 | struct linear_conf *newconf, *oldconf; |
7c7546cc | 200 | |
a778b73f | 201 | if (rdev->saved_raid_disk != mddev->raid_disks) |
7c7546cc N |
202 | return -EINVAL; |
203 | ||
a778b73f | 204 | rdev->raid_disk = rdev->saved_raid_disk; |
09cd9270 | 205 | rdev->saved_raid_disk = -1; |
a778b73f | 206 | |
7c7546cc N |
207 | newconf = linear_conf(mddev,mddev->raid_disks+1); |
208 | ||
209 | if (!newconf) | |
210 | return -ENOMEM; | |
211 | ||
03a9e24e | 212 | /* newconf->raid_disks already keeps a copy of * the increased |
213 | * value of mddev->raid_disks, WARN_ONCE() is just used to make | |
214 | * sure of this. It is possible that oldconf is still referenced | |
215 | * in linear_congested(), therefore kfree_rcu() is used to free | |
216 | * oldconf until no one uses it anymore. | |
217 | */ | |
3be260cc | 218 | mddev_suspend(mddev); |
d939cdfd SL |
219 | oldconf = rcu_dereference_protected(mddev->private, |
220 | lockdep_is_held(&mddev->reconfig_mutex)); | |
7c7546cc | 221 | mddev->raid_disks++; |
03a9e24e | 222 | WARN_ONCE(mddev->raid_disks != newconf->raid_disks, |
223 | "copied raid_disks doesn't match mddev->raid_disks"); | |
224 | rcu_assign_pointer(mddev->private, newconf); | |
1f403624 | 225 | md_set_array_sectors(mddev, linear_size(mddev, 0, 0)); |
f233ea5c | 226 | set_capacity(mddev->gendisk, mddev->array_sectors); |
3be260cc | 227 | mddev_resume(mddev); |
449aad3e | 228 | revalidate_disk(mddev->gendisk); |
03a9e24e | 229 | kfree_rcu(oldconf, rcu); |
7c7546cc | 230 | return 0; |
1da177e4 LT |
231 | } |
232 | ||
afa0f557 | 233 | static void linear_free(struct mddev *mddev, void *priv) |
1da177e4 | 234 | { |
afa0f557 | 235 | struct linear_conf *conf = priv; |
af11c397 | 236 | |
495d3573 | 237 | kfree(conf); |
1da177e4 LT |
238 | } |
239 | ||
cc27b0c7 | 240 | static bool linear_make_request(struct mddev *mddev, struct bio *bio) |
1da177e4 | 241 | { |
20d0189b | 242 | char b[BDEVNAME_SIZE]; |
a7120771 | 243 | struct dev_info *tmp_dev; |
20d0189b | 244 | sector_t start_sector, end_sector, data_offset; |
868f604b | 245 | sector_t bio_sector = bio->bi_iter.bi_sector; |
1da177e4 | 246 | |
775d7831 DJ |
247 | if (unlikely(bio->bi_opf & REQ_PREFLUSH) |
248 | && md_flush_request(mddev, bio)) | |
cc27b0c7 | 249 | return true; |
e5dcdd80 | 250 | |
868f604b N |
251 | tmp_dev = which_dev(mddev, bio_sector); |
252 | start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors; | |
253 | end_sector = tmp_dev->end_sector; | |
254 | data_offset = tmp_dev->rdev->data_offset; | |
255 | ||
256 | if (unlikely(bio_sector >= end_sector || | |
257 | bio_sector < start_sector)) | |
258 | goto out_of_bounds; | |
259 | ||
62f7b198 GP |
260 | if (unlikely(is_mddev_broken(tmp_dev->rdev, "linear"))) { |
261 | bio_io_error(bio); | |
262 | return true; | |
263 | } | |
264 | ||
868f604b N |
265 | if (unlikely(bio_end_sector(bio) > end_sector)) { |
266 | /* This bio crosses a device boundary, so we have to split it */ | |
267 | struct bio *split = bio_split(bio, end_sector - bio_sector, | |
afeee514 | 268 | GFP_NOIO, &mddev->bio_set); |
868f604b | 269 | bio_chain(split, bio); |
ed00aabd | 270 | submit_bio_noacct(bio); |
868f604b N |
271 | bio = split; |
272 | } | |
f1cad2b6 | 273 | |
74d46992 | 274 | bio_set_dev(bio, tmp_dev->rdev->bdev); |
868f604b N |
275 | bio->bi_iter.bi_sector = bio->bi_iter.bi_sector - |
276 | start_sector + data_offset; | |
277 | ||
278 | if (unlikely((bio_op(bio) == REQ_OP_DISCARD) && | |
74d46992 | 279 | !blk_queue_discard(bio->bi_disk->queue))) { |
868f604b N |
280 | /* Just ignore it */ |
281 | bio_endio(bio); | |
282 | } else { | |
283 | if (mddev->gendisk) | |
74d46992 | 284 | trace_block_bio_remap(bio->bi_disk->queue, |
868f604b N |
285 | bio, disk_devt(mddev->gendisk), |
286 | bio_sector); | |
287 | mddev_check_writesame(mddev, bio); | |
e265eb3a | 288 | mddev_check_write_zeroes(mddev, bio); |
ed00aabd | 289 | submit_bio_noacct(bio); |
868f604b | 290 | } |
cc27b0c7 | 291 | return true; |
20d0189b KO |
292 | |
293 | out_of_bounds: | |
a2e202af | 294 | pr_err("md/linear:%s: make_request: Sector %llu out of bounds on dev %s: %llu sectors, offset %llu\n", |
20d0189b KO |
295 | mdname(mddev), |
296 | (unsigned long long)bio->bi_iter.bi_sector, | |
297 | bdevname(tmp_dev->rdev->bdev, b), | |
298 | (unsigned long long)tmp_dev->rdev->sectors, | |
299 | (unsigned long long)start_sector); | |
300 | bio_io_error(bio); | |
cc27b0c7 | 301 | return true; |
1da177e4 LT |
302 | } |
303 | ||
fd01b88c | 304 | static void linear_status (struct seq_file *seq, struct mddev *mddev) |
1da177e4 | 305 | { |
9d8f0363 | 306 | seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2); |
1da177e4 LT |
307 | } |
308 | ||
3be260cc N |
309 | static void linear_quiesce(struct mddev *mddev, int state) |
310 | { | |
311 | } | |
312 | ||
84fc4b56 | 313 | static struct md_personality linear_personality = |
1da177e4 LT |
314 | { |
315 | .name = "linear", | |
2604b703 | 316 | .level = LEVEL_LINEAR, |
1da177e4 LT |
317 | .owner = THIS_MODULE, |
318 | .make_request = linear_make_request, | |
319 | .run = linear_run, | |
afa0f557 | 320 | .free = linear_free, |
1da177e4 | 321 | .status = linear_status, |
7c7546cc | 322 | .hot_add_disk = linear_add, |
80c3a6ce | 323 | .size = linear_size, |
3be260cc | 324 | .quiesce = linear_quiesce, |
5c675f83 | 325 | .congested = linear_congested, |
1da177e4 LT |
326 | }; |
327 | ||
328 | static int __init linear_init (void) | |
329 | { | |
2604b703 | 330 | return register_md_personality (&linear_personality); |
1da177e4 LT |
331 | } |
332 | ||
333 | static void linear_exit (void) | |
334 | { | |
2604b703 | 335 | unregister_md_personality (&linear_personality); |
1da177e4 LT |
336 | } |
337 | ||
1da177e4 LT |
338 | module_init(linear_init); |
339 | module_exit(linear_exit); | |
340 | MODULE_LICENSE("GPL"); | |
0efb9e61 | 341 | MODULE_DESCRIPTION("Linear device concatenation personality for MD"); |
d9d166c2 N |
342 | MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/ |
343 | MODULE_ALIAS("md-linear"); | |
2604b703 | 344 | MODULE_ALIAS("md-level--1"); |