blob: 1ff51647a682243800b2441980962a8d166c68d9 [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010*/
11
NeilBrownbff61972009-03-31 14:33:13 +110012#include <linux/blkdev.h>
13#include <linux/raid/md_u.h>
NeilBrownbff61972009-03-31 14:33:13 +110014#include <linux/seq_file.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040015#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
NeilBrown109e3762016-11-18 13:22:04 +110017#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110018#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040019#include "md-linear.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/*
NeilBrownf72ffdd2014-09-30 14:23:59 +100022 * find which device holds a particular offset
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
NeilBrowna7120772011-10-11 16:48:49 +110024static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Sandeep K Sinhaaece3d12009-06-16 16:57:08 +100026 int lo, mid, hi;
NeilBrowne849b932011-10-11 16:48:54 +110027 struct linear_conf *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Sandeep K Sinhaaece3d12009-06-16 16:57:08 +100029 lo = 0;
30 hi = mddev->raid_disks - 1;
NeilBrown3be260c2014-12-15 12:56:57 +110031 conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Sandeep K Sinhaaece3d12009-06-16 16:57:08 +100033 /*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
NeilBrownfd01b882011-10-11 16:47:53 +110049static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -070050{
NeilBrowne849b932011-10-11 16:48:54 +110051 struct linear_conf *conf;
SandeepKsinhaaf11c392009-06-18 08:49:35 +100052 sector_t array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070053
NeilBrown3be260c2014-12-15 12:56:57 +110054 conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -070055 WARN_ONCE(sectors || raid_disks,
56 "%s does not support generic reshape\n", __func__);
SandeepKsinhaaf11c392009-06-18 08:49:35 +100057 array_sectors = conf->array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070058
SandeepKsinhaaf11c392009-06-18 08:49:35 +100059 return array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070060}
61
NeilBrowne849b932011-10-11 16:48:54 +110062static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
NeilBrowne849b932011-10-11 16:48:54 +110064 struct linear_conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +110065 struct md_rdev *rdev;
Sandeep K Sinha45d45822009-06-16 16:55:26 +100066 int i, cnt;
Shaohua Lif1cad2b2012-10-11 13:08:44 +110067 bool discard_supported = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Gustavo A. R. Silvaf1e5b622019-01-07 11:45:38 -060069 conf = kzalloc(struct_size(conf, disks, raid_disks), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (!conf)
NeilBrown7c7546c2006-06-26 00:27:41 -070071 return NULL;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 cnt = 0;
Andre Nolld6e22152008-07-21 17:05:25 +100074 conf->array_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
NeilBrowndafb20f2012-03-19 12:46:39 +110076 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 int j = rdev->raid_disk;
NeilBrowna7120772011-10-11 16:48:49 +110078 struct dev_info *disk = conf->disks + j;
NeilBrown13f26822009-06-18 08:48:55 +100079 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Nikanth Karthikesan13864512008-06-28 08:31:19 +100081 if (j < 0 || j >= raid_disks || disk->rdev) {
NeilBrowna2e202a2016-11-02 14:16:49 +110082 pr_warn("md/linear:%s: disk numbering problem. Aborting!\n",
83 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 goto out;
85 }
86
87 disk->rdev = rdev;
NeilBrown13f26822009-06-18 08:48:55 +100088 if (mddev->chunk_sectors) {
89 sectors = rdev->sectors;
90 sector_div(sectors, mddev->chunk_sectors);
91 rdev->sectors = sectors * mddev->chunk_sectors;
92 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +100094 disk_stack_limits(mddev->gendisk, rdev->bdev,
95 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Andre Nolldd8ac332009-03-31 14:33:13 +110097 conf->array_sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 cnt++;
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +100099
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100100 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
101 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
NeilBrown7c7546c2006-06-26 00:27:41 -0700103 if (cnt != raid_disks) {
NeilBrowna2e202a2016-11-02 14:16:49 +1100104 pr_warn("md/linear:%s: not enough drives present. Aborting!\n",
105 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto out;
107 }
108
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100109 if (!discard_supported)
Bart Van Assche8b904b52018-03-07 17:10:10 -0800110 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, mddev->queue);
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100111 else
Bart Van Assche8b904b52018-03-07 17:10:10 -0800112 blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /*
Sandeep K Sinha45d45822009-06-16 16:55:26 +1000115 * Here we calculate the device offsets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000117 conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
118
NeilBrowna778b732007-05-23 13:58:10 -0700119 for (i = 1; i < raid_disks; i++)
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000120 conf->disks[i].end_sector =
121 conf->disks[i-1].end_sector +
122 conf->disks[i].rdev->sectors;
NeilBrown15945fe2005-09-09 16:23:47 -0700123
colyli@suse.de03a9e242017-01-28 21:11:49 +0800124 /*
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
NeilBrown7c7546c2006-06-26 00:27:41 -0700137 return conf;
138
139out:
140 kfree(conf);
141 return NULL;
142}
143
NeilBrownfd01b882011-10-11 16:47:53 +1100144static int linear_run (struct mddev *mddev)
NeilBrown7c7546c2006-06-26 00:27:41 -0700145{
NeilBrowne849b932011-10-11 16:48:54 +1100146 struct linear_conf *conf;
majianpeng98d55612012-04-02 09:48:37 +1000147 int ret;
NeilBrown7c7546c2006-06-26 00:27:41 -0700148
Andre Noll0894cc32009-06-18 08:49:23 +1000149 if (md_check_no_bitmap(mddev))
150 return -EINVAL;
NeilBrown7c7546c2006-06-26 00:27:41 -0700151 conf = linear_conf(mddev, mddev->raid_disks);
152
153 if (!conf)
154 return 1;
155 mddev->private = conf;
Dan Williams1f403622009-03-31 14:59:03 +1100156 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
NeilBrown7c7546c2006-06-26 00:27:41 -0700157
majianpeng98d55612012-04-02 09:48:37 +1000158 ret = md_integrity_register(mddev);
159 if (ret) {
160 kfree(conf);
161 mddev->private = NULL;
162 }
163 return ret;
NeilBrown7c7546c2006-06-26 00:27:41 -0700164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
NeilBrownfd01b882011-10-11 16:47:53 +1100166static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
NeilBrown7c7546c2006-06-26 00:27:41 -0700167{
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 */
NeilBrowne849b932011-10-11 16:48:54 +1100176 struct linear_conf *newconf, *oldconf;
NeilBrown7c7546c2006-06-26 00:27:41 -0700177
NeilBrowna778b732007-05-23 13:58:10 -0700178 if (rdev->saved_raid_disk != mddev->raid_disks)
NeilBrown7c7546c2006-06-26 00:27:41 -0700179 return -EINVAL;
180
NeilBrowna778b732007-05-23 13:58:10 -0700181 rdev->raid_disk = rdev->saved_raid_disk;
NeilBrown09cd9272011-12-23 09:56:55 +1100182 rdev->saved_raid_disk = -1;
NeilBrowna778b732007-05-23 13:58:10 -0700183
NeilBrown7c7546c2006-06-26 00:27:41 -0700184 newconf = linear_conf(mddev,mddev->raid_disks+1);
185
186 if (!newconf)
187 return -ENOMEM;
188
colyli@suse.de03a9e242017-01-28 21:11:49 +0800189 /* 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 */
NeilBrown3be260c2014-12-15 12:56:57 +1100195 mddev_suspend(mddev);
Shaohua Lid939cdf2017-02-21 11:57:01 -0800196 oldconf = rcu_dereference_protected(mddev->private,
197 lockdep_is_held(&mddev->reconfig_mutex));
NeilBrown7c7546c2006-06-26 00:27:41 -0700198 mddev->raid_disks++;
colyli@suse.de03a9e242017-01-28 21:11:49 +0800199 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);
Dan Williams1f403622009-03-31 14:59:03 +1100202 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
Christoph Hellwig2c247c52020-11-16 15:57:11 +0100203 set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
NeilBrown3be260c2014-12-15 12:56:57 +1100204 mddev_resume(mddev);
colyli@suse.de03a9e242017-01-28 21:11:49 +0800205 kfree_rcu(oldconf, rcu);
NeilBrown7c7546c2006-06-26 00:27:41 -0700206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
NeilBrownafa0f552014-12-15 12:56:58 +1100209static void linear_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
NeilBrownafa0f552014-12-15 12:56:58 +1100211 struct linear_conf *conf = priv;
SandeepKsinhaaf11c392009-06-18 08:49:35 +1000212
NeilBrown495d3572009-06-18 08:49:42 +1000213 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
NeilBrowncc27b0c2017-06-05 16:49:39 +1000216static bool linear_make_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Kent Overstreet20d01892013-11-23 18:21:01 -0800218 char b[BDEVNAME_SIZE];
NeilBrowna7120772011-10-11 16:48:49 +1100219 struct dev_info *tmp_dev;
Kent Overstreet20d01892013-11-23 18:21:01 -0800220 sector_t start_sector, end_sector, data_offset;
NeilBrown868f6042017-04-05 14:05:51 +1000221 sector_t bio_sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
David Jeffery775d7832019-09-16 13:15:14 -0400223 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
224 && md_flush_request(mddev, bio))
NeilBrowncc27b0c2017-06-05 16:49:39 +1000225 return true;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700226
NeilBrown868f6042017-04-05 14:05:51 +1000227 tmp_dev = which_dev(mddev, bio_sector);
228 start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
229 end_sector = tmp_dev->end_sector;
230 data_offset = tmp_dev->rdev->data_offset;
Andre Noll62838152008-10-13 11:55:12 +1100231
NeilBrown868f6042017-04-05 14:05:51 +1000232 if (unlikely(bio_sector >= end_sector ||
233 bio_sector < start_sector))
234 goto out_of_bounds;
Andre Noll62838152008-10-13 11:55:12 +1100235
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -0300236 if (unlikely(is_mddev_broken(tmp_dev->rdev, "linear"))) {
237 bio_io_error(bio);
238 return true;
239 }
240
NeilBrown868f6042017-04-05 14:05:51 +1000241 if (unlikely(bio_end_sector(bio) > end_sector)) {
242 /* This bio crosses a device boundary, so we have to split it */
243 struct bio *split = bio_split(bio, end_sector - bio_sector,
Kent Overstreetafeee512018-05-20 18:25:52 -0400244 GFP_NOIO, &mddev->bio_set);
NeilBrown868f6042017-04-05 14:05:51 +1000245 bio_chain(split, bio);
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200246 submit_bio_noacct(bio);
NeilBrown868f6042017-04-05 14:05:51 +1000247 bio = split;
248 }
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100249
Christoph Hellwig74d46992017-08-23 19:10:32 +0200250 bio_set_dev(bio, tmp_dev->rdev->bdev);
NeilBrown868f6042017-04-05 14:05:51 +1000251 bio->bi_iter.bi_sector = bio->bi_iter.bi_sector -
252 start_sector + data_offset;
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100253
NeilBrown868f6042017-04-05 14:05:51 +1000254 if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
Christoph Hellwig309dca302021-01-24 11:02:34 +0100255 !blk_queue_discard(bio->bi_bdev->bd_disk->queue))) {
NeilBrown868f6042017-04-05 14:05:51 +1000256 /* Just ignore it */
257 bio_endio(bio);
258 } else {
259 if (mddev->gendisk)
Christoph Hellwig1c02fca2020-12-03 17:21:38 +0100260 trace_block_bio_remap(bio, disk_devt(mddev->gendisk),
NeilBrown868f6042017-04-05 14:05:51 +1000261 bio_sector);
262 mddev_check_writesame(mddev, bio);
Shaohua Lie265eb3a2017-05-01 14:09:21 -0700263 mddev_check_write_zeroes(mddev, bio);
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200264 submit_bio_noacct(bio);
NeilBrown868f6042017-04-05 14:05:51 +1000265 }
NeilBrowncc27b0c2017-06-05 16:49:39 +1000266 return true;
Kent Overstreet20d01892013-11-23 18:21:01 -0800267
268out_of_bounds:
NeilBrowna2e202a2016-11-02 14:16:49 +1100269 pr_err("md/linear:%s: make_request: Sector %llu out of bounds on dev %s: %llu sectors, offset %llu\n",
Kent Overstreet20d01892013-11-23 18:21:01 -0800270 mdname(mddev),
271 (unsigned long long)bio->bi_iter.bi_sector,
272 bdevname(tmp_dev->rdev->bdev, b),
273 (unsigned long long)tmp_dev->rdev->sectors,
274 (unsigned long long)start_sector);
275 bio_io_error(bio);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000276 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
NeilBrownfd01b882011-10-11 16:47:53 +1100279static void linear_status (struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Andre Noll9d8f0362009-06-18 08:45:01 +1000281 seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
NeilBrown3be260c2014-12-15 12:56:57 +1100284static void linear_quiesce(struct mddev *mddev, int state)
285{
286}
287
NeilBrown84fc4b52011-10-11 16:49:58 +1100288static struct md_personality linear_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 .name = "linear",
NeilBrown2604b702006-01-06 00:20:36 -0800291 .level = LEVEL_LINEAR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 .owner = THIS_MODULE,
293 .make_request = linear_make_request,
294 .run = linear_run,
NeilBrownafa0f552014-12-15 12:56:58 +1100295 .free = linear_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 .status = linear_status,
NeilBrown7c7546c2006-06-26 00:27:41 -0700297 .hot_add_disk = linear_add,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700298 .size = linear_size,
NeilBrown3be260c2014-12-15 12:56:57 +1100299 .quiesce = linear_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
302static int __init linear_init (void)
303{
NeilBrown2604b702006-01-06 00:20:36 -0800304 return register_md_personality (&linear_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307static void linear_exit (void)
308{
NeilBrown2604b702006-01-06 00:20:36 -0800309 unregister_md_personality (&linear_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312module_init(linear_init);
313module_exit(linear_exit);
314MODULE_LICENSE("GPL");
Guoqing Jiang608f52e2021-05-25 17:46:23 +0800315MODULE_DESCRIPTION("Linear device concatenation personality for MD (deprecated)");
NeilBrownd9d166c2006-01-06 00:20:51 -0800316MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
317MODULE_ALIAS("md-linear");
NeilBrown2604b702006-01-06 00:20:36 -0800318MODULE_ALIAS("md-level--1");