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