treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / md / md-linear.c
blob26c75c0199fa1b3e8f54728fd76393ece6ced3f5
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>
8 Linear mode management functions.
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"
22 * find which device holds a particular offset
24 static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
26 int lo, mid, hi;
27 struct linear_conf *conf;
29 lo = 0;
30 hi = mddev->raid_disks - 1;
31 conf = mddev->private;
34 * Binary Search
37 while (hi > lo) {
39 mid = (hi + lo) / 2;
40 if (sector < conf->disks[mid].end_sector)
41 hi = mid;
42 else
43 lo = mid + 1;
46 return conf->disks + lo;
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.
55 static int linear_congested(struct mddev *mddev, int bits)
57 struct linear_conf *conf;
58 int i, ret = 0;
60 rcu_read_lock();
61 conf = rcu_dereference(mddev->private);
63 for (i = 0; i < conf->raid_disks && !ret ; i++) {
64 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
65 ret |= bdi_congested(q->backing_dev_info, bits);
68 rcu_read_unlock();
69 return ret;
72 static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
74 struct linear_conf *conf;
75 sector_t array_sectors;
77 conf = mddev->private;
78 WARN_ONCE(sectors || raid_disks,
79 "%s does not support generic reshape\n", __func__);
80 array_sectors = conf->array_sectors;
82 return array_sectors;
85 static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
87 struct linear_conf *conf;
88 struct md_rdev *rdev;
89 int i, cnt;
90 bool discard_supported = false;
92 conf = kzalloc(struct_size(conf, disks, raid_disks), GFP_KERNEL);
93 if (!conf)
94 return NULL;
96 cnt = 0;
97 conf->array_sectors = 0;
99 rdev_for_each(rdev, mddev) {
100 int j = rdev->raid_disk;
101 struct dev_info *disk = conf->disks + j;
102 sector_t sectors;
104 if (j < 0 || j >= raid_disks || disk->rdev) {
105 pr_warn("md/linear:%s: disk numbering problem. Aborting!\n",
106 mdname(mddev));
107 goto out;
110 disk->rdev = rdev;
111 if (mddev->chunk_sectors) {
112 sectors = rdev->sectors;
113 sector_div(sectors, mddev->chunk_sectors);
114 rdev->sectors = sectors * mddev->chunk_sectors;
117 disk_stack_limits(mddev->gendisk, rdev->bdev,
118 rdev->data_offset << 9);
120 conf->array_sectors += rdev->sectors;
121 cnt++;
123 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
124 discard_supported = true;
126 if (cnt != raid_disks) {
127 pr_warn("md/linear:%s: not enough drives present. Aborting!\n",
128 mdname(mddev));
129 goto out;
132 if (!discard_supported)
133 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, mddev->queue);
134 else
135 blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
138 * Here we calculate the device offsets.
140 conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
142 for (i = 1; i < raid_disks; i++)
143 conf->disks[i].end_sector =
144 conf->disks[i-1].end_sector +
145 conf->disks[i].rdev->sectors;
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.
158 conf->raid_disks = raid_disks;
160 return conf;
162 out:
163 kfree(conf);
164 return NULL;
167 static int linear_run (struct mddev *mddev)
169 struct linear_conf *conf;
170 int ret;
172 if (md_check_no_bitmap(mddev))
173 return -EINVAL;
174 conf = linear_conf(mddev, mddev->raid_disks);
176 if (!conf)
177 return 1;
178 mddev->private = conf;
179 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
181 ret = md_integrity_register(mddev);
182 if (ret) {
183 kfree(conf);
184 mddev->private = NULL;
186 return ret;
189 static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
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.
199 struct linear_conf *newconf, *oldconf;
201 if (rdev->saved_raid_disk != mddev->raid_disks)
202 return -EINVAL;
204 rdev->raid_disk = rdev->saved_raid_disk;
205 rdev->saved_raid_disk = -1;
207 newconf = linear_conf(mddev,mddev->raid_disks+1);
209 if (!newconf)
210 return -ENOMEM;
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.
218 mddev_suspend(mddev);
219 oldconf = rcu_dereference_protected(mddev->private,
220 lockdep_is_held(&mddev->reconfig_mutex));
221 mddev->raid_disks++;
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);
225 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
226 set_capacity(mddev->gendisk, mddev->array_sectors);
227 mddev_resume(mddev);
228 revalidate_disk(mddev->gendisk);
229 kfree_rcu(oldconf, rcu);
230 return 0;
233 static void linear_free(struct mddev *mddev, void *priv)
235 struct linear_conf *conf = priv;
237 kfree(conf);
240 static bool linear_make_request(struct mddev *mddev, struct bio *bio)
242 char b[BDEVNAME_SIZE];
243 struct dev_info *tmp_dev;
244 sector_t start_sector, end_sector, data_offset;
245 sector_t bio_sector = bio->bi_iter.bi_sector;
247 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
248 && md_flush_request(mddev, bio))
249 return true;
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;
256 if (unlikely(bio_sector >= end_sector ||
257 bio_sector < start_sector))
258 goto out_of_bounds;
260 if (unlikely(is_mddev_broken(tmp_dev->rdev, "linear"))) {
261 bio_io_error(bio);
262 return true;
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,
268 GFP_NOIO, &mddev->bio_set);
269 bio_chain(split, bio);
270 generic_make_request(bio);
271 bio = split;
274 bio_set_dev(bio, tmp_dev->rdev->bdev);
275 bio->bi_iter.bi_sector = bio->bi_iter.bi_sector -
276 start_sector + data_offset;
278 if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
279 !blk_queue_discard(bio->bi_disk->queue))) {
280 /* Just ignore it */
281 bio_endio(bio);
282 } else {
283 if (mddev->gendisk)
284 trace_block_bio_remap(bio->bi_disk->queue,
285 bio, disk_devt(mddev->gendisk),
286 bio_sector);
287 mddev_check_writesame(mddev, bio);
288 mddev_check_write_zeroes(mddev, bio);
289 generic_make_request(bio);
291 return true;
293 out_of_bounds:
294 pr_err("md/linear:%s: make_request: Sector %llu out of bounds on dev %s: %llu sectors, offset %llu\n",
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);
301 return true;
304 static void linear_status (struct seq_file *seq, struct mddev *mddev)
306 seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
309 static void linear_quiesce(struct mddev *mddev, int state)
313 static struct md_personality linear_personality =
315 .name = "linear",
316 .level = LEVEL_LINEAR,
317 .owner = THIS_MODULE,
318 .make_request = linear_make_request,
319 .run = linear_run,
320 .free = linear_free,
321 .status = linear_status,
322 .hot_add_disk = linear_add,
323 .size = linear_size,
324 .quiesce = linear_quiesce,
325 .congested = linear_congested,
328 static int __init linear_init (void)
330 return register_md_personality (&linear_personality);
333 static void linear_exit (void)
335 unregister_md_personality (&linear_personality);
338 module_init(linear_init);
339 module_exit(linear_exit);
340 MODULE_LICENSE("GPL");
341 MODULE_DESCRIPTION("Linear device concatenation personality for MD");
342 MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
343 MODULE_ALIAS("md-linear");
344 MODULE_ALIAS("md-level--1");