blob: b0c0aef92a375d30785b4ce719c83a43b6afae08 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 linear.c : Multiple Devices driver for Linux
3 Copyright (C) 1994-96 Marc ZYNGIER
4 <zyngier@ufr-info-p7.ibp.fr> or
5 <maz@gloups.fdn.fr>
6
7 Linear mode management functions.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
NeilBrownf72ffdd2014-09-30 14:23:59 +100013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 You should have received a copy of the GNU General Public License
15 (for example /usr/src/linux/COPYING); if not, write to the Free
NeilBrownf72ffdd2014-09-30 14:23:59 +100016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017*/
18
NeilBrownbff61972009-03-31 14:33:13 +110019#include <linux/blkdev.h>
20#include <linux/raid/md_u.h>
NeilBrownbff61972009-03-31 14:33:13 +110021#include <linux/seq_file.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040022#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110024#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110025#include "linear.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/*
NeilBrownf72ffdd2014-09-30 14:23:59 +100028 * find which device holds a particular offset
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
NeilBrowna7120772011-10-11 16:48:49 +110030static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Sandeep K Sinhaaece3d12009-06-16 16:57:08 +100032 int lo, mid, hi;
NeilBrowne849b932011-10-11 16:48:54 +110033 struct linear_conf *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Sandeep K Sinhaaece3d12009-06-16 16:57:08 +100035 lo = 0;
36 hi = mddev->raid_disks - 1;
NeilBrown3be260c2014-12-15 12:56:57 +110037 conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Sandeep K Sinhaaece3d12009-06-16 16:57:08 +100039 /*
40 * Binary Search
41 */
42
43 while (hi > lo) {
44
45 mid = (hi + lo) / 2;
46 if (sector < conf->disks[mid].end_sector)
47 hi = mid;
48 else
49 lo = mid + 1;
50 }
51
52 return conf->disks + lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
colyli@suse.de6f9c02a2017-01-28 21:11:49 +080055/*
56 * In linear_congested() conf->raid_disks is used as a copy of
57 * mddev->raid_disks to iterate conf->disks[], because conf->raid_disks
58 * and conf->disks[] are created in linear_conf(), they are always
59 * consitent with each other, but mddev->raid_disks does not.
60 */
NeilBrown5c675f82014-12-15 12:56:56 +110061static int linear_congested(struct mddev *mddev, int bits)
NeilBrown26be34d2006-10-03 01:15:53 -070062{
NeilBrowne849b932011-10-11 16:48:54 +110063 struct linear_conf *conf;
NeilBrown26be34d2006-10-03 01:15:53 -070064 int i, ret = 0;
65
colyli@suse.de6f9c02a2017-01-28 21:11:49 +080066 rcu_read_lock();
67 conf = rcu_dereference(mddev->private);
SandeepKsinhaaf11c392009-06-18 08:49:35 +100068
colyli@suse.de6f9c02a2017-01-28 21:11:49 +080069 for (i = 0; i < conf->raid_disks && !ret ; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020070 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
NeilBrown26be34d2006-10-03 01:15:53 -070071 ret |= bdi_congested(&q->backing_dev_info, bits);
72 }
SandeepKsinhaaf11c392009-06-18 08:49:35 +100073
colyli@suse.de6f9c02a2017-01-28 21:11:49 +080074 rcu_read_unlock();
NeilBrown26be34d2006-10-03 01:15:53 -070075 return ret;
76}
77
NeilBrownfd01b882011-10-11 16:47:53 +110078static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -070079{
NeilBrowne849b932011-10-11 16:48:54 +110080 struct linear_conf *conf;
SandeepKsinhaaf11c392009-06-18 08:49:35 +100081 sector_t array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070082
NeilBrown3be260c2014-12-15 12:56:57 +110083 conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -070084 WARN_ONCE(sectors || raid_disks,
85 "%s does not support generic reshape\n", __func__);
SandeepKsinhaaf11c392009-06-18 08:49:35 +100086 array_sectors = conf->array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070087
SandeepKsinhaaf11c392009-06-18 08:49:35 +100088 return array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070089}
90
NeilBrowne849b932011-10-11 16:48:54 +110091static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
NeilBrowne849b932011-10-11 16:48:54 +110093 struct linear_conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +110094 struct md_rdev *rdev;
Sandeep K Sinha45d45822009-06-16 16:55:26 +100095 int i, cnt;
Shaohua Lif1cad2b2012-10-11 13:08:44 +110096 bool discard_supported = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
NeilBrowna7120772011-10-11 16:48:49 +110098 conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 GFP_KERNEL);
100 if (!conf)
NeilBrown7c7546c2006-06-26 00:27:41 -0700101 return NULL;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 cnt = 0;
Andre Nolld6e22152008-07-21 17:05:25 +1000104 conf->array_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
NeilBrowndafb20f2012-03-19 12:46:39 +1100106 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int j = rdev->raid_disk;
NeilBrowna7120772011-10-11 16:48:49 +1100108 struct dev_info *disk = conf->disks + j;
NeilBrown13f26822009-06-18 08:48:55 +1000109 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Nikanth Karthikesan13864512008-06-28 08:31:19 +1000111 if (j < 0 || j >= raid_disks || disk->rdev) {
NeilBrown2dc40f82010-05-03 15:12:04 +1000112 printk(KERN_ERR "md/linear:%s: disk numbering problem. Aborting!\n",
113 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 goto out;
115 }
116
117 disk->rdev = rdev;
NeilBrown13f26822009-06-18 08:48:55 +1000118 if (mddev->chunk_sectors) {
119 sectors = rdev->sectors;
120 sector_div(sectors, mddev->chunk_sectors);
121 rdev->sectors = sectors * mddev->chunk_sectors;
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000124 disk_stack_limits(mddev->gendisk, rdev->bdev,
125 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Andre Nolldd8ac332009-03-31 14:33:13 +1100127 conf->array_sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 cnt++;
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000129
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100130 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
131 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
NeilBrown7c7546c2006-06-26 00:27:41 -0700133 if (cnt != raid_disks) {
NeilBrown2dc40f82010-05-03 15:12:04 +1000134 printk(KERN_ERR "md/linear:%s: not enough drives present. Aborting!\n",
135 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 goto out;
137 }
138
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100139 if (!discard_supported)
140 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
141 else
142 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /*
Sandeep K Sinha45d45822009-06-16 16:55:26 +1000145 * Here we calculate the device offsets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 */
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000147 conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
148
NeilBrowna778b732007-05-23 13:58:10 -0700149 for (i = 1; i < raid_disks; i++)
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000150 conf->disks[i].end_sector =
151 conf->disks[i-1].end_sector +
152 conf->disks[i].rdev->sectors;
NeilBrown15945fe2005-09-09 16:23:47 -0700153
colyli@suse.de6f9c02a2017-01-28 21:11:49 +0800154 /*
155 * conf->raid_disks is copy of mddev->raid_disks. The reason to
156 * keep a copy of mddev->raid_disks in struct linear_conf is,
157 * mddev->raid_disks may not be consistent with pointers number of
158 * conf->disks[] when it is updated in linear_add() and used to
159 * iterate old conf->disks[] earray in linear_congested().
160 * Here conf->raid_disks is always consitent with number of
161 * pointers in conf->disks[] array, and mddev->private is updated
162 * with rcu_assign_pointer() in linear_addr(), such race can be
163 * avoided.
164 */
165 conf->raid_disks = raid_disks;
166
NeilBrown7c7546c2006-06-26 00:27:41 -0700167 return conf;
168
169out:
170 kfree(conf);
171 return NULL;
172}
173
NeilBrownfd01b882011-10-11 16:47:53 +1100174static int linear_run (struct mddev *mddev)
NeilBrown7c7546c2006-06-26 00:27:41 -0700175{
NeilBrowne849b932011-10-11 16:48:54 +1100176 struct linear_conf *conf;
majianpeng98d55612012-04-02 09:48:37 +1000177 int ret;
NeilBrown7c7546c2006-06-26 00:27:41 -0700178
Andre Noll0894cc32009-06-18 08:49:23 +1000179 if (md_check_no_bitmap(mddev))
180 return -EINVAL;
NeilBrown7c7546c2006-06-26 00:27:41 -0700181 conf = linear_conf(mddev, mddev->raid_disks);
182
183 if (!conf)
184 return 1;
185 mddev->private = conf;
Dan Williams1f403622009-03-31 14:59:03 +1100186 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
NeilBrown7c7546c2006-06-26 00:27:41 -0700187
majianpeng98d55612012-04-02 09:48:37 +1000188 ret = md_integrity_register(mddev);
189 if (ret) {
190 kfree(conf);
191 mddev->private = NULL;
192 }
193 return ret;
NeilBrown7c7546c2006-06-26 00:27:41 -0700194}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
NeilBrownfd01b882011-10-11 16:47:53 +1100196static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
NeilBrown7c7546c2006-06-26 00:27:41 -0700197{
198 /* Adding a drive to a linear array allows the array to grow.
199 * It is permitted if the new drive has a matching superblock
200 * already on it, with raid_disk equal to raid_disks.
201 * It is achieved by creating a new linear_private_data structure
202 * and swapping it in in-place of the current one.
203 * The current one is never freed until the array is stopped.
204 * This avoids races.
205 */
NeilBrowne849b932011-10-11 16:48:54 +1100206 struct linear_conf *newconf, *oldconf;
NeilBrown7c7546c2006-06-26 00:27:41 -0700207
NeilBrowna778b732007-05-23 13:58:10 -0700208 if (rdev->saved_raid_disk != mddev->raid_disks)
NeilBrown7c7546c2006-06-26 00:27:41 -0700209 return -EINVAL;
210
NeilBrowna778b732007-05-23 13:58:10 -0700211 rdev->raid_disk = rdev->saved_raid_disk;
NeilBrown09cd9272011-12-23 09:56:55 +1100212 rdev->saved_raid_disk = -1;
NeilBrowna778b732007-05-23 13:58:10 -0700213
NeilBrown7c7546c2006-06-26 00:27:41 -0700214 newconf = linear_conf(mddev,mddev->raid_disks+1);
215
216 if (!newconf)
217 return -ENOMEM;
218
colyli@suse.de6f9c02a2017-01-28 21:11:49 +0800219 /* newconf->raid_disks already keeps a copy of * the increased
220 * value of mddev->raid_disks, WARN_ONCE() is just used to make
221 * sure of this. It is possible that oldconf is still referenced
222 * in linear_congested(), therefore kfree_rcu() is used to free
223 * oldconf until no one uses it anymore.
224 */
NeilBrown3be260c2014-12-15 12:56:57 +1100225 mddev_suspend(mddev);
colyli@suse.de6f9c02a2017-01-28 21:11:49 +0800226 oldconf = rcu_dereference(mddev->private);
NeilBrown7c7546c2006-06-26 00:27:41 -0700227 mddev->raid_disks++;
colyli@suse.de6f9c02a2017-01-28 21:11:49 +0800228 WARN_ONCE(mddev->raid_disks != newconf->raid_disks,
229 "copied raid_disks doesn't match mddev->raid_disks");
230 rcu_assign_pointer(mddev->private, newconf);
Dan Williams1f403622009-03-31 14:59:03 +1100231 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
Andre Nollf233ea52008-07-21 17:05:22 +1000232 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown3be260c2014-12-15 12:56:57 +1100233 mddev_resume(mddev);
NeilBrown449aad32009-08-03 10:59:58 +1000234 revalidate_disk(mddev->gendisk);
colyli@suse.de6f9c02a2017-01-28 21:11:49 +0800235 kfree_rcu(oldconf, rcu);
NeilBrown7c7546c2006-06-26 00:27:41 -0700236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
NeilBrownafa0f552014-12-15 12:56:58 +1100239static void linear_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
NeilBrownafa0f552014-12-15 12:56:58 +1100241 struct linear_conf *conf = priv;
SandeepKsinhaaf11c392009-06-18 08:49:35 +1000242
NeilBrown495d3572009-06-18 08:49:42 +1000243 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700246static void linear_make_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Kent Overstreet20d01892013-11-23 18:21:01 -0800248 char b[BDEVNAME_SIZE];
NeilBrowna7120772011-10-11 16:48:49 +1100249 struct dev_info *tmp_dev;
Kent Overstreet20d01892013-11-23 18:21:01 -0800250 struct bio *split;
251 sector_t start_sector, end_sector, data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Jens Axboe1eff9d32016-08-05 15:35:16 -0600253 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200254 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200255 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700256 }
257
Kent Overstreet20d01892013-11-23 18:21:01 -0800258 do {
Kent Overstreet20d01892013-11-23 18:21:01 -0800259 tmp_dev = which_dev(mddev, bio->bi_iter.bi_sector);
260 start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
261 end_sector = tmp_dev->end_sector;
262 data_offset = tmp_dev->rdev->data_offset;
263 bio->bi_bdev = tmp_dev->rdev->bdev;
Andre Noll62838152008-10-13 11:55:12 +1100264
Kent Overstreet20d01892013-11-23 18:21:01 -0800265 if (unlikely(bio->bi_iter.bi_sector >= end_sector ||
266 bio->bi_iter.bi_sector < start_sector))
267 goto out_of_bounds;
Andre Noll62838152008-10-13 11:55:12 +1100268
Kent Overstreet20d01892013-11-23 18:21:01 -0800269 if (unlikely(bio_end_sector(bio) > end_sector)) {
270 /* This bio crosses a device boundary, so we have to
271 * split it.
272 */
273 split = bio_split(bio, end_sector -
274 bio->bi_iter.bi_sector,
275 GFP_NOIO, fs_bio_set);
276 bio_chain(split, bio);
277 } else {
278 split = bio;
279 }
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100280
Kent Overstreet20d01892013-11-23 18:21:01 -0800281 split->bi_iter.bi_sector = split->bi_iter.bi_sector -
282 start_sector + data_offset;
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100283
Mike Christie796a5cf2016-06-05 14:32:07 -0500284 if (unlikely((bio_op(split) == REQ_OP_DISCARD) &&
Kent Overstreet20d01892013-11-23 18:21:01 -0800285 !blk_queue_discard(bdev_get_queue(split->bi_bdev)))) {
286 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200287 bio_endio(split);
Kent Overstreet20d01892013-11-23 18:21:01 -0800288 } else
289 generic_make_request(split);
290 } while (split != bio);
291 return;
292
293out_of_bounds:
294 printk(KERN_ERR
295 "md/linear:%s: make_request: Sector %llu out of bounds on "
296 "dev %s: %llu sectors, offset %llu\n",
297 mdname(mddev),
298 (unsigned long long)bio->bi_iter.bi_sector,
299 bdevname(tmp_dev->rdev->bdev, b),
300 (unsigned long long)tmp_dev->rdev->sectors,
301 (unsigned long long)start_sector);
302 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
NeilBrownfd01b882011-10-11 16:47:53 +1100305static void linear_status (struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307
Andre Noll9d8f0362009-06-18 08:45:01 +1000308 seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
NeilBrown3be260c2014-12-15 12:56:57 +1100311static void linear_quiesce(struct mddev *mddev, int state)
312{
313}
314
NeilBrown84fc4b52011-10-11 16:49:58 +1100315static struct md_personality linear_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 .name = "linear",
NeilBrown2604b702006-01-06 00:20:36 -0800318 .level = LEVEL_LINEAR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 .owner = THIS_MODULE,
320 .make_request = linear_make_request,
321 .run = linear_run,
NeilBrownafa0f552014-12-15 12:56:58 +1100322 .free = linear_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .status = linear_status,
NeilBrown7c7546c2006-06-26 00:27:41 -0700324 .hot_add_disk = linear_add,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700325 .size = linear_size,
NeilBrown3be260c2014-12-15 12:56:57 +1100326 .quiesce = linear_quiesce,
NeilBrown5c675f82014-12-15 12:56:56 +1100327 .congested = linear_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328};
329
330static int __init linear_init (void)
331{
NeilBrown2604b702006-01-06 00:20:36 -0800332 return register_md_personality (&linear_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335static void linear_exit (void)
336{
NeilBrown2604b702006-01-06 00:20:36 -0800337 unregister_md_personality (&linear_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340module_init(linear_init);
341module_exit(linear_exit);
342MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100343MODULE_DESCRIPTION("Linear device concatenation personality for MD");
NeilBrownd9d166c2006-01-06 00:20:51 -0800344MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
345MODULE_ALIAS("md-linear");
NeilBrown2604b702006-01-06 00:20:36 -0800346MODULE_ALIAS("md-level--1");