blob: 7354466ddc903561238b272818e53b116b5b3c2f [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
colyli@suse.de03a9e242017-01-28 21:11:49 +080049/*
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 */
NeilBrown5c675f82014-12-15 12:56:56 +110055static int linear_congested(struct mddev *mddev, int bits)
NeilBrown26be34d2006-10-03 01:15:53 -070056{
NeilBrowne849b932011-10-11 16:48:54 +110057 struct linear_conf *conf;
NeilBrown26be34d2006-10-03 01:15:53 -070058 int i, ret = 0;
59
colyli@suse.de03a9e242017-01-28 21:11:49 +080060 rcu_read_lock();
61 conf = rcu_dereference(mddev->private);
SandeepKsinhaaf11c392009-06-18 08:49:35 +100062
colyli@suse.de03a9e242017-01-28 21:11:49 +080063 for (i = 0; i < conf->raid_disks && !ret ; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020064 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
Jan Karadc3b17c2017-02-02 15:56:50 +010065 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown26be34d2006-10-03 01:15:53 -070066 }
SandeepKsinhaaf11c392009-06-18 08:49:35 +100067
colyli@suse.de03a9e242017-01-28 21:11:49 +080068 rcu_read_unlock();
NeilBrown26be34d2006-10-03 01:15:53 -070069 return ret;
70}
71
NeilBrownfd01b882011-10-11 16:47:53 +110072static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -070073{
NeilBrowne849b932011-10-11 16:48:54 +110074 struct linear_conf *conf;
SandeepKsinhaaf11c392009-06-18 08:49:35 +100075 sector_t array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070076
NeilBrown3be260c2014-12-15 12:56:57 +110077 conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -070078 WARN_ONCE(sectors || raid_disks,
79 "%s does not support generic reshape\n", __func__);
SandeepKsinhaaf11c392009-06-18 08:49:35 +100080 array_sectors = conf->array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070081
SandeepKsinhaaf11c392009-06-18 08:49:35 +100082 return array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070083}
84
NeilBrowne849b932011-10-11 16:48:54 +110085static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
NeilBrowne849b932011-10-11 16:48:54 +110087 struct linear_conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +110088 struct md_rdev *rdev;
Sandeep K Sinha45d45822009-06-16 16:55:26 +100089 int i, cnt;
Shaohua Lif1cad2b2012-10-11 13:08:44 +110090 bool discard_supported = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Gustavo A. R. Silvaf1e5b622019-01-07 11:45:38 -060092 conf = kzalloc(struct_size(conf, disks, raid_disks), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (!conf)
NeilBrown7c7546c2006-06-26 00:27:41 -070094 return NULL;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 cnt = 0;
Andre Nolld6e22152008-07-21 17:05:25 +100097 conf->array_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
NeilBrowndafb20f2012-03-19 12:46:39 +110099 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 int j = rdev->raid_disk;
NeilBrowna7120772011-10-11 16:48:49 +1100101 struct dev_info *disk = conf->disks + j;
NeilBrown13f26822009-06-18 08:48:55 +1000102 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Nikanth Karthikesan13864512008-06-28 08:31:19 +1000104 if (j < 0 || j >= raid_disks || disk->rdev) {
NeilBrowna2e202a2016-11-02 14:16:49 +1100105 pr_warn("md/linear:%s: disk numbering problem. Aborting!\n",
106 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 goto out;
108 }
109
110 disk->rdev = rdev;
NeilBrown13f26822009-06-18 08:48:55 +1000111 if (mddev->chunk_sectors) {
112 sectors = rdev->sectors;
113 sector_div(sectors, mddev->chunk_sectors);
114 rdev->sectors = sectors * mddev->chunk_sectors;
115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000117 disk_stack_limits(mddev->gendisk, rdev->bdev,
118 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Andre Nolldd8ac332009-03-31 14:33:13 +1100120 conf->array_sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 cnt++;
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000122
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100123 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
124 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
NeilBrown7c7546c2006-06-26 00:27:41 -0700126 if (cnt != raid_disks) {
NeilBrowna2e202a2016-11-02 14:16:49 +1100127 pr_warn("md/linear:%s: not enough drives present. Aborting!\n",
128 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 goto out;
130 }
131
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100132 if (!discard_supported)
Bart Van Assche8b904b52018-03-07 17:10:10 -0800133 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, mddev->queue);
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100134 else
Bart Van Assche8b904b52018-03-07 17:10:10 -0800135 blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /*
Sandeep K Sinha45d45822009-06-16 16:55:26 +1000138 * Here we calculate the device offsets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000140 conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
141
NeilBrowna778b732007-05-23 13:58:10 -0700142 for (i = 1; i < raid_disks; i++)
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000143 conf->disks[i].end_sector =
144 conf->disks[i-1].end_sector +
145 conf->disks[i].rdev->sectors;
NeilBrown15945fe2005-09-09 16:23:47 -0700146
colyli@suse.de03a9e242017-01-28 21:11:49 +0800147 /*
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
NeilBrown7c7546c2006-06-26 00:27:41 -0700160 return conf;
161
162out:
163 kfree(conf);
164 return NULL;
165}
166
NeilBrownfd01b882011-10-11 16:47:53 +1100167static int linear_run (struct mddev *mddev)
NeilBrown7c7546c2006-06-26 00:27:41 -0700168{
NeilBrowne849b932011-10-11 16:48:54 +1100169 struct linear_conf *conf;
majianpeng98d55612012-04-02 09:48:37 +1000170 int ret;
NeilBrown7c7546c2006-06-26 00:27:41 -0700171
Andre Noll0894cc32009-06-18 08:49:23 +1000172 if (md_check_no_bitmap(mddev))
173 return -EINVAL;
NeilBrown7c7546c2006-06-26 00:27:41 -0700174 conf = linear_conf(mddev, mddev->raid_disks);
175
176 if (!conf)
177 return 1;
178 mddev->private = conf;
Dan Williams1f403622009-03-31 14:59:03 +1100179 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
NeilBrown7c7546c2006-06-26 00:27:41 -0700180
majianpeng98d55612012-04-02 09:48:37 +1000181 ret = md_integrity_register(mddev);
182 if (ret) {
183 kfree(conf);
184 mddev->private = NULL;
185 }
186 return ret;
NeilBrown7c7546c2006-06-26 00:27:41 -0700187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
NeilBrownfd01b882011-10-11 16:47:53 +1100189static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
NeilBrown7c7546c2006-06-26 00:27:41 -0700190{
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 */
NeilBrowne849b932011-10-11 16:48:54 +1100199 struct linear_conf *newconf, *oldconf;
NeilBrown7c7546c2006-06-26 00:27:41 -0700200
NeilBrowna778b732007-05-23 13:58:10 -0700201 if (rdev->saved_raid_disk != mddev->raid_disks)
NeilBrown7c7546c2006-06-26 00:27:41 -0700202 return -EINVAL;
203
NeilBrowna778b732007-05-23 13:58:10 -0700204 rdev->raid_disk = rdev->saved_raid_disk;
NeilBrown09cd9272011-12-23 09:56:55 +1100205 rdev->saved_raid_disk = -1;
NeilBrowna778b732007-05-23 13:58:10 -0700206
NeilBrown7c7546c2006-06-26 00:27:41 -0700207 newconf = linear_conf(mddev,mddev->raid_disks+1);
208
209 if (!newconf)
210 return -ENOMEM;
211
colyli@suse.de03a9e242017-01-28 21:11:49 +0800212 /* 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 */
NeilBrown3be260c2014-12-15 12:56:57 +1100218 mddev_suspend(mddev);
Shaohua Lid939cdf2017-02-21 11:57:01 -0800219 oldconf = rcu_dereference_protected(mddev->private,
220 lockdep_is_held(&mddev->reconfig_mutex));
NeilBrown7c7546c2006-06-26 00:27:41 -0700221 mddev->raid_disks++;
colyli@suse.de03a9e242017-01-28 21:11:49 +0800222 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);
Dan Williams1f403622009-03-31 14:59:03 +1100225 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
Andre Nollf233ea52008-07-21 17:05:22 +1000226 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown3be260c2014-12-15 12:56:57 +1100227 mddev_resume(mddev);
NeilBrown449aad32009-08-03 10:59:58 +1000228 revalidate_disk(mddev->gendisk);
colyli@suse.de03a9e242017-01-28 21:11:49 +0800229 kfree_rcu(oldconf, rcu);
NeilBrown7c7546c2006-06-26 00:27:41 -0700230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
NeilBrownafa0f552014-12-15 12:56:58 +1100233static void linear_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
NeilBrownafa0f552014-12-15 12:56:58 +1100235 struct linear_conf *conf = priv;
SandeepKsinhaaf11c392009-06-18 08:49:35 +1000236
NeilBrown495d3572009-06-18 08:49:42 +1000237 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
NeilBrowncc27b0c2017-06-05 16:49:39 +1000240static bool linear_make_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Kent Overstreet20d01892013-11-23 18:21:01 -0800242 char b[BDEVNAME_SIZE];
NeilBrowna7120772011-10-11 16:48:49 +1100243 struct dev_info *tmp_dev;
Kent Overstreet20d01892013-11-23 18:21:01 -0800244 sector_t start_sector, end_sector, data_offset;
NeilBrown868f6042017-04-05 14:05:51 +1000245 sector_t bio_sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jens Axboe1eff9d32016-08-05 15:35:16 -0600247 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200248 md_flush_request(mddev, bio);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000249 return true;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700250 }
251
NeilBrown868f6042017-04-05 14:05:51 +1000252 tmp_dev = which_dev(mddev, bio_sector);
253 start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
254 end_sector = tmp_dev->end_sector;
255 data_offset = tmp_dev->rdev->data_offset;
Andre Noll62838152008-10-13 11:55:12 +1100256
NeilBrown868f6042017-04-05 14:05:51 +1000257 if (unlikely(bio_sector >= end_sector ||
258 bio_sector < start_sector))
259 goto out_of_bounds;
Andre Noll62838152008-10-13 11:55:12 +1100260
NeilBrown868f6042017-04-05 14:05:51 +1000261 if (unlikely(bio_end_sector(bio) > end_sector)) {
262 /* This bio crosses a device boundary, so we have to split it */
263 struct bio *split = bio_split(bio, end_sector - bio_sector,
Kent Overstreetafeee512018-05-20 18:25:52 -0400264 GFP_NOIO, &mddev->bio_set);
NeilBrown868f6042017-04-05 14:05:51 +1000265 bio_chain(split, bio);
266 generic_make_request(bio);
267 bio = split;
268 }
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100269
Christoph Hellwig74d46992017-08-23 19:10:32 +0200270 bio_set_dev(bio, tmp_dev->rdev->bdev);
NeilBrown868f6042017-04-05 14:05:51 +1000271 bio->bi_iter.bi_sector = bio->bi_iter.bi_sector -
272 start_sector + data_offset;
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100273
NeilBrown868f6042017-04-05 14:05:51 +1000274 if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
Christoph Hellwig74d46992017-08-23 19:10:32 +0200275 !blk_queue_discard(bio->bi_disk->queue))) {
NeilBrown868f6042017-04-05 14:05:51 +1000276 /* Just ignore it */
277 bio_endio(bio);
278 } else {
279 if (mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +0200280 trace_block_bio_remap(bio->bi_disk->queue,
NeilBrown868f6042017-04-05 14:05:51 +1000281 bio, disk_devt(mddev->gendisk),
282 bio_sector);
283 mddev_check_writesame(mddev, bio);
Shaohua Lie265eb3a2017-05-01 14:09:21 -0700284 mddev_check_write_zeroes(mddev, bio);
NeilBrown868f6042017-04-05 14:05:51 +1000285 generic_make_request(bio);
286 }
NeilBrowncc27b0c2017-06-05 16:49:39 +1000287 return true;
Kent Overstreet20d01892013-11-23 18:21:01 -0800288
289out_of_bounds:
NeilBrowna2e202a2016-11-02 14:16:49 +1100290 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 -0800291 mdname(mddev),
292 (unsigned long long)bio->bi_iter.bi_sector,
293 bdevname(tmp_dev->rdev->bdev, b),
294 (unsigned long long)tmp_dev->rdev->sectors,
295 (unsigned long long)start_sector);
296 bio_io_error(bio);
NeilBrowncc27b0c2017-06-05 16:49:39 +1000297 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
NeilBrownfd01b882011-10-11 16:47:53 +1100300static void linear_status (struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Andre Noll9d8f0362009-06-18 08:45:01 +1000302 seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
NeilBrown3be260c2014-12-15 12:56:57 +1100305static void linear_quiesce(struct mddev *mddev, int state)
306{
307}
308
NeilBrown84fc4b52011-10-11 16:49:58 +1100309static struct md_personality linear_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 .name = "linear",
NeilBrown2604b702006-01-06 00:20:36 -0800312 .level = LEVEL_LINEAR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 .owner = THIS_MODULE,
314 .make_request = linear_make_request,
315 .run = linear_run,
NeilBrownafa0f552014-12-15 12:56:58 +1100316 .free = linear_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .status = linear_status,
NeilBrown7c7546c2006-06-26 00:27:41 -0700318 .hot_add_disk = linear_add,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700319 .size = linear_size,
NeilBrown3be260c2014-12-15 12:56:57 +1100320 .quiesce = linear_quiesce,
NeilBrown5c675f82014-12-15 12:56:56 +1100321 .congested = linear_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322};
323
324static int __init linear_init (void)
325{
NeilBrown2604b702006-01-06 00:20:36 -0800326 return register_md_personality (&linear_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329static void linear_exit (void)
330{
NeilBrown2604b702006-01-06 00:20:36 -0800331 unregister_md_personality (&linear_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334module_init(linear_init);
335module_exit(linear_exit);
336MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100337MODULE_DESCRIPTION("Linear device concatenation personality for MD");
NeilBrownd9d166c2006-01-06 00:20:51 -0800338MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
339MODULE_ALIAS("md-linear");
NeilBrown2604b702006-01-06 00:20:36 -0800340MODULE_ALIAS("md-level--1");