xref: /openbmc/linux/drivers/md/md-linear.c (revision 4c5a116a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
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 
10 */
11 
12 #include <linux/blkdev.h>
13 #include <linux/raid/md_u.h>
14 #include <linux/seq_file.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <trace/events/block.h>
18 #include "md.h"
19 #include "md-linear.h"
20 
21 /*
22  * find which device holds a particular offset
23  */
24 static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
25 {
26 	int lo, mid, hi;
27 	struct linear_conf *conf;
28 
29 	lo = 0;
30 	hi = mddev->raid_disks - 1;
31 	conf = mddev->private;
32 
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;
47 }
48 
49 static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
50 {
51 	struct linear_conf *conf;
52 	sector_t array_sectors;
53 
54 	conf = mddev->private;
55 	WARN_ONCE(sectors || raid_disks,
56 		  "%s does not support generic reshape\n", __func__);
57 	array_sectors = conf->array_sectors;
58 
59 	return array_sectors;
60 }
61 
62 static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
63 {
64 	struct linear_conf *conf;
65 	struct md_rdev *rdev;
66 	int i, cnt;
67 	bool discard_supported = false;
68 
69 	conf = kzalloc(struct_size(conf, disks, raid_disks), GFP_KERNEL);
70 	if (!conf)
71 		return NULL;
72 
73 	cnt = 0;
74 	conf->array_sectors = 0;
75 
76 	rdev_for_each(rdev, mddev) {
77 		int j = rdev->raid_disk;
78 		struct dev_info *disk = conf->disks + j;
79 		sector_t sectors;
80 
81 		if (j < 0 || j >= raid_disks || disk->rdev) {
82 			pr_warn("md/linear:%s: disk numbering problem. Aborting!\n",
83 				mdname(mddev));
84 			goto out;
85 		}
86 
87 		disk->rdev = rdev;
88 		if (mddev->chunk_sectors) {
89 			sectors = rdev->sectors;
90 			sector_div(sectors, mddev->chunk_sectors);
91 			rdev->sectors = sectors * mddev->chunk_sectors;
92 		}
93 
94 		disk_stack_limits(mddev->gendisk, rdev->bdev,
95 				  rdev->data_offset << 9);
96 
97 		conf->array_sectors += rdev->sectors;
98 		cnt++;
99 
100 		if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
101 			discard_supported = true;
102 	}
103 	if (cnt != raid_disks) {
104 		pr_warn("md/linear:%s: not enough drives present. Aborting!\n",
105 			mdname(mddev));
106 		goto out;
107 	}
108 
109 	if (!discard_supported)
110 		blk_queue_flag_clear(QUEUE_FLAG_DISCARD, mddev->queue);
111 	else
112 		blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
113 
114 	/*
115 	 * Here we calculate the device offsets.
116 	 */
117 	conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
118 
119 	for (i = 1; i < raid_disks; i++)
120 		conf->disks[i].end_sector =
121 			conf->disks[i-1].end_sector +
122 			conf->disks[i].rdev->sectors;
123 
124 	/*
125 	 * conf->raid_disks is copy of mddev->raid_disks. The reason to
126 	 * keep a copy of mddev->raid_disks in struct linear_conf is,
127 	 * mddev->raid_disks may not be consistent with pointers number of
128 	 * conf->disks[] when it is updated in linear_add() and used to
129 	 * iterate old conf->disks[] earray in linear_congested().
130 	 * Here conf->raid_disks is always consitent with number of
131 	 * pointers in conf->disks[] array, and mddev->private is updated
132 	 * with rcu_assign_pointer() in linear_addr(), such race can be
133 	 * avoided.
134 	 */
135 	conf->raid_disks = raid_disks;
136 
137 	return conf;
138 
139 out:
140 	kfree(conf);
141 	return NULL;
142 }
143 
144 static int linear_run (struct mddev *mddev)
145 {
146 	struct linear_conf *conf;
147 	int ret;
148 
149 	if (md_check_no_bitmap(mddev))
150 		return -EINVAL;
151 	conf = linear_conf(mddev, mddev->raid_disks);
152 
153 	if (!conf)
154 		return 1;
155 	mddev->private = conf;
156 	md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
157 
158 	ret =  md_integrity_register(mddev);
159 	if (ret) {
160 		kfree(conf);
161 		mddev->private = NULL;
162 	}
163 	return ret;
164 }
165 
166 static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
167 {
168 	/* Adding a drive to a linear array allows the array to grow.
169 	 * It is permitted if the new drive has a matching superblock
170 	 * already on it, with raid_disk equal to raid_disks.
171 	 * It is achieved by creating a new linear_private_data structure
172 	 * and swapping it in in-place of the current one.
173 	 * The current one is never freed until the array is stopped.
174 	 * This avoids races.
175 	 */
176 	struct linear_conf *newconf, *oldconf;
177 
178 	if (rdev->saved_raid_disk != mddev->raid_disks)
179 		return -EINVAL;
180 
181 	rdev->raid_disk = rdev->saved_raid_disk;
182 	rdev->saved_raid_disk = -1;
183 
184 	newconf = linear_conf(mddev,mddev->raid_disks+1);
185 
186 	if (!newconf)
187 		return -ENOMEM;
188 
189 	/* newconf->raid_disks already keeps a copy of * the increased
190 	 * value of mddev->raid_disks, WARN_ONCE() is just used to make
191 	 * sure of this. It is possible that oldconf is still referenced
192 	 * in linear_congested(), therefore kfree_rcu() is used to free
193 	 * oldconf until no one uses it anymore.
194 	 */
195 	mddev_suspend(mddev);
196 	oldconf = rcu_dereference_protected(mddev->private,
197 			lockdep_is_held(&mddev->reconfig_mutex));
198 	mddev->raid_disks++;
199 	WARN_ONCE(mddev->raid_disks != newconf->raid_disks,
200 		"copied raid_disks doesn't match mddev->raid_disks");
201 	rcu_assign_pointer(mddev->private, newconf);
202 	md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
203 	set_capacity(mddev->gendisk, mddev->array_sectors);
204 	mddev_resume(mddev);
205 	revalidate_disk(mddev->gendisk);
206 	kfree_rcu(oldconf, rcu);
207 	return 0;
208 }
209 
210 static void linear_free(struct mddev *mddev, void *priv)
211 {
212 	struct linear_conf *conf = priv;
213 
214 	kfree(conf);
215 }
216 
217 static bool linear_make_request(struct mddev *mddev, struct bio *bio)
218 {
219 	char b[BDEVNAME_SIZE];
220 	struct dev_info *tmp_dev;
221 	sector_t start_sector, end_sector, data_offset;
222 	sector_t bio_sector = bio->bi_iter.bi_sector;
223 
224 	if (unlikely(bio->bi_opf & REQ_PREFLUSH)
225 	    && md_flush_request(mddev, bio))
226 		return true;
227 
228 	tmp_dev = which_dev(mddev, bio_sector);
229 	start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
230 	end_sector = tmp_dev->end_sector;
231 	data_offset = tmp_dev->rdev->data_offset;
232 
233 	if (unlikely(bio_sector >= end_sector ||
234 		     bio_sector < start_sector))
235 		goto out_of_bounds;
236 
237 	if (unlikely(is_mddev_broken(tmp_dev->rdev, "linear"))) {
238 		bio_io_error(bio);
239 		return true;
240 	}
241 
242 	if (unlikely(bio_end_sector(bio) > end_sector)) {
243 		/* This bio crosses a device boundary, so we have to split it */
244 		struct bio *split = bio_split(bio, end_sector - bio_sector,
245 					      GFP_NOIO, &mddev->bio_set);
246 		bio_chain(split, bio);
247 		submit_bio_noacct(bio);
248 		bio = split;
249 	}
250 
251 	bio_set_dev(bio, tmp_dev->rdev->bdev);
252 	bio->bi_iter.bi_sector = bio->bi_iter.bi_sector -
253 		start_sector + data_offset;
254 
255 	if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
256 		     !blk_queue_discard(bio->bi_disk->queue))) {
257 		/* Just ignore it */
258 		bio_endio(bio);
259 	} else {
260 		if (mddev->gendisk)
261 			trace_block_bio_remap(bio->bi_disk->queue,
262 					      bio, disk_devt(mddev->gendisk),
263 					      bio_sector);
264 		mddev_check_writesame(mddev, bio);
265 		mddev_check_write_zeroes(mddev, bio);
266 		submit_bio_noacct(bio);
267 	}
268 	return true;
269 
270 out_of_bounds:
271 	pr_err("md/linear:%s: make_request: Sector %llu out of bounds on dev %s: %llu sectors, offset %llu\n",
272 	       mdname(mddev),
273 	       (unsigned long long)bio->bi_iter.bi_sector,
274 	       bdevname(tmp_dev->rdev->bdev, b),
275 	       (unsigned long long)tmp_dev->rdev->sectors,
276 	       (unsigned long long)start_sector);
277 	bio_io_error(bio);
278 	return true;
279 }
280 
281 static void linear_status (struct seq_file *seq, struct mddev *mddev)
282 {
283 	seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
284 }
285 
286 static void linear_quiesce(struct mddev *mddev, int state)
287 {
288 }
289 
290 static struct md_personality linear_personality =
291 {
292 	.name		= "linear",
293 	.level		= LEVEL_LINEAR,
294 	.owner		= THIS_MODULE,
295 	.make_request	= linear_make_request,
296 	.run		= linear_run,
297 	.free		= linear_free,
298 	.status		= linear_status,
299 	.hot_add_disk	= linear_add,
300 	.size		= linear_size,
301 	.quiesce	= linear_quiesce,
302 };
303 
304 static int __init linear_init (void)
305 {
306 	return register_md_personality (&linear_personality);
307 }
308 
309 static void linear_exit (void)
310 {
311 	unregister_md_personality (&linear_personality);
312 }
313 
314 module_init(linear_init);
315 module_exit(linear_exit);
316 MODULE_LICENSE("GPL");
317 MODULE_DESCRIPTION("Linear device concatenation personality for MD");
318 MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
319 MODULE_ALIAS("md-linear");
320 MODULE_ALIAS("md-level--1");
321