blob: 5975c99156841ecd25f1210457e6612dc4191227 [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>
NeilBrown109e3762016-11-18 13:22:04 +110024#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110025#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110026#include "linear.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
NeilBrownf72ffdd2014-09-30 14:23:59 +100029 * find which device holds a particular offset
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
NeilBrowna7120772011-10-11 16:48:49 +110031static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Sandeep K Sinhaaece3d12009-06-16 16:57:08 +100033 int lo, mid, hi;
NeilBrowne849b932011-10-11 16:48:54 +110034 struct linear_conf *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Sandeep K Sinhaaece3d12009-06-16 16:57:08 +100036 lo = 0;
37 hi = mddev->raid_disks - 1;
NeilBrown3be260c2014-12-15 12:56:57 +110038 conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Sandeep K Sinhaaece3d12009-06-16 16:57:08 +100040 /*
41 * Binary Search
42 */
43
44 while (hi > lo) {
45
46 mid = (hi + lo) / 2;
47 if (sector < conf->disks[mid].end_sector)
48 hi = mid;
49 else
50 lo = mid + 1;
51 }
52
53 return conf->disks + lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
NeilBrown5c675f82014-12-15 12:56:56 +110056static int linear_congested(struct mddev *mddev, int bits)
NeilBrown26be34d2006-10-03 01:15:53 -070057{
NeilBrowne849b932011-10-11 16:48:54 +110058 struct linear_conf *conf;
NeilBrown26be34d2006-10-03 01:15:53 -070059 int i, ret = 0;
60
NeilBrown3be260c2014-12-15 12:56:57 +110061 conf = mddev->private;
SandeepKsinhaaf11c392009-06-18 08:49:35 +100062
NeilBrown26be34d2006-10-03 01:15:53 -070063 for (i = 0; i < mddev->raid_disks && !ret ; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020064 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
NeilBrown26be34d2006-10-03 01:15:53 -070065 ret |= bdi_congested(&q->backing_dev_info, bits);
66 }
SandeepKsinhaaf11c392009-06-18 08:49:35 +100067
NeilBrown26be34d2006-10-03 01:15:53 -070068 return ret;
69}
70
NeilBrownfd01b882011-10-11 16:47:53 +110071static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -070072{
NeilBrowne849b932011-10-11 16:48:54 +110073 struct linear_conf *conf;
SandeepKsinhaaf11c392009-06-18 08:49:35 +100074 sector_t array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070075
NeilBrown3be260c2014-12-15 12:56:57 +110076 conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -070077 WARN_ONCE(sectors || raid_disks,
78 "%s does not support generic reshape\n", __func__);
SandeepKsinhaaf11c392009-06-18 08:49:35 +100079 array_sectors = conf->array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070080
SandeepKsinhaaf11c392009-06-18 08:49:35 +100081 return array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070082}
83
NeilBrowne849b932011-10-11 16:48:54 +110084static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
NeilBrowne849b932011-10-11 16:48:54 +110086 struct linear_conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +110087 struct md_rdev *rdev;
Sandeep K Sinha45d45822009-06-16 16:55:26 +100088 int i, cnt;
Shaohua Lif1cad2b2012-10-11 13:08:44 +110089 bool discard_supported = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
NeilBrowna7120772011-10-11 16:48:49 +110091 conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 GFP_KERNEL);
93 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)
133 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
134 else
135 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
136
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
NeilBrown7c7546c2006-06-26 00:27:41 -0700147 return conf;
148
149out:
150 kfree(conf);
151 return NULL;
152}
153
NeilBrownfd01b882011-10-11 16:47:53 +1100154static int linear_run (struct mddev *mddev)
NeilBrown7c7546c2006-06-26 00:27:41 -0700155{
NeilBrowne849b932011-10-11 16:48:54 +1100156 struct linear_conf *conf;
majianpeng98d55612012-04-02 09:48:37 +1000157 int ret;
NeilBrown7c7546c2006-06-26 00:27:41 -0700158
Andre Noll0894cc32009-06-18 08:49:23 +1000159 if (md_check_no_bitmap(mddev))
160 return -EINVAL;
NeilBrown7c7546c2006-06-26 00:27:41 -0700161 conf = linear_conf(mddev, mddev->raid_disks);
162
163 if (!conf)
164 return 1;
165 mddev->private = conf;
Dan Williams1f403622009-03-31 14:59:03 +1100166 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
NeilBrown7c7546c2006-06-26 00:27:41 -0700167
majianpeng98d55612012-04-02 09:48:37 +1000168 ret = md_integrity_register(mddev);
169 if (ret) {
170 kfree(conf);
171 mddev->private = NULL;
172 }
173 return ret;
NeilBrown7c7546c2006-06-26 00:27:41 -0700174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
NeilBrownfd01b882011-10-11 16:47:53 +1100176static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
NeilBrown7c7546c2006-06-26 00:27:41 -0700177{
178 /* Adding a drive to a linear array allows the array to grow.
179 * It is permitted if the new drive has a matching superblock
180 * already on it, with raid_disk equal to raid_disks.
181 * It is achieved by creating a new linear_private_data structure
182 * and swapping it in in-place of the current one.
183 * The current one is never freed until the array is stopped.
184 * This avoids races.
185 */
NeilBrowne849b932011-10-11 16:48:54 +1100186 struct linear_conf *newconf, *oldconf;
NeilBrown7c7546c2006-06-26 00:27:41 -0700187
NeilBrowna778b732007-05-23 13:58:10 -0700188 if (rdev->saved_raid_disk != mddev->raid_disks)
NeilBrown7c7546c2006-06-26 00:27:41 -0700189 return -EINVAL;
190
NeilBrowna778b732007-05-23 13:58:10 -0700191 rdev->raid_disk = rdev->saved_raid_disk;
NeilBrown09cd9272011-12-23 09:56:55 +1100192 rdev->saved_raid_disk = -1;
NeilBrowna778b732007-05-23 13:58:10 -0700193
NeilBrown7c7546c2006-06-26 00:27:41 -0700194 newconf = linear_conf(mddev,mddev->raid_disks+1);
195
196 if (!newconf)
197 return -ENOMEM;
198
NeilBrown3be260c2014-12-15 12:56:57 +1100199 mddev_suspend(mddev);
200 oldconf = mddev->private;
NeilBrown7c7546c2006-06-26 00:27:41 -0700201 mddev->raid_disks++;
NeilBrown3be260c2014-12-15 12:56:57 +1100202 mddev->private = newconf;
Dan Williams1f403622009-03-31 14:59:03 +1100203 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
Andre Nollf233ea52008-07-21 17:05:22 +1000204 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown3be260c2014-12-15 12:56:57 +1100205 mddev_resume(mddev);
NeilBrown449aad32009-08-03 10:59:58 +1000206 revalidate_disk(mddev->gendisk);
NeilBrown3be260c2014-12-15 12:56:57 +1100207 kfree(oldconf);
NeilBrown7c7546c2006-06-26 00:27:41 -0700208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
NeilBrownafa0f552014-12-15 12:56:58 +1100211static void linear_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
NeilBrownafa0f552014-12-15 12:56:58 +1100213 struct linear_conf *conf = priv;
SandeepKsinhaaf11c392009-06-18 08:49:35 +1000214
NeilBrown495d3572009-06-18 08:49:42 +1000215 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700218static void linear_make_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Kent Overstreet20d01892013-11-23 18:21:01 -0800220 char b[BDEVNAME_SIZE];
NeilBrowna7120772011-10-11 16:48:49 +1100221 struct dev_info *tmp_dev;
Kent Overstreet20d01892013-11-23 18:21:01 -0800222 struct bio *split;
223 sector_t start_sector, end_sector, data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Jens Axboe1eff9d32016-08-05 15:35:16 -0600225 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200226 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200227 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700228 }
229
Kent Overstreet20d01892013-11-23 18:21:01 -0800230 do {
NeilBrown109e3762016-11-18 13:22:04 +1100231 sector_t bio_sector = bio->bi_iter.bi_sector;
232 tmp_dev = which_dev(mddev, bio_sector);
Kent Overstreet20d01892013-11-23 18:21:01 -0800233 start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
234 end_sector = tmp_dev->end_sector;
235 data_offset = tmp_dev->rdev->data_offset;
236 bio->bi_bdev = tmp_dev->rdev->bdev;
Andre Noll62838152008-10-13 11:55:12 +1100237
NeilBrown109e3762016-11-18 13:22:04 +1100238 if (unlikely(bio_sector >= end_sector ||
239 bio_sector < start_sector))
Kent Overstreet20d01892013-11-23 18:21:01 -0800240 goto out_of_bounds;
Andre Noll62838152008-10-13 11:55:12 +1100241
Kent Overstreet20d01892013-11-23 18:21:01 -0800242 if (unlikely(bio_end_sector(bio) > end_sector)) {
243 /* This bio crosses a device boundary, so we have to
244 * split it.
245 */
NeilBrown109e3762016-11-18 13:22:04 +1100246 split = bio_split(bio, end_sector - bio_sector,
Kent Overstreet20d01892013-11-23 18:21:01 -0800247 GFP_NOIO, fs_bio_set);
248 bio_chain(split, bio);
249 } else {
250 split = bio;
251 }
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100252
Kent Overstreet20d01892013-11-23 18:21:01 -0800253 split->bi_iter.bi_sector = split->bi_iter.bi_sector -
254 start_sector + data_offset;
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100255
Mike Christie796a5cf2016-06-05 14:32:07 -0500256 if (unlikely((bio_op(split) == REQ_OP_DISCARD) &&
Kent Overstreet20d01892013-11-23 18:21:01 -0800257 !blk_queue_discard(bdev_get_queue(split->bi_bdev)))) {
258 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200259 bio_endio(split);
NeilBrown109e3762016-11-18 13:22:04 +1100260 } else {
261 if (mddev->gendisk)
262 trace_block_bio_remap(bdev_get_queue(split->bi_bdev),
263 split, disk_devt(mddev->gendisk),
264 bio_sector);
Kent Overstreet20d01892013-11-23 18:21:01 -0800265 generic_make_request(split);
NeilBrown109e3762016-11-18 13:22:04 +1100266 }
Kent Overstreet20d01892013-11-23 18:21:01 -0800267 } while (split != bio);
268 return;
269
270out_of_bounds:
NeilBrowna2e202a2016-11-02 14:16:49 +1100271 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 -0800272 mdname(mddev),
273 (unsigned long long)bio->bi_iter.bi_sector,
274 bdevname(tmp_dev->rdev->bdev, b),
275 (unsigned long long)tmp_dev->rdev->sectors,
276 (unsigned long long)start_sector);
277 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
NeilBrownfd01b882011-10-11 16:47:53 +1100280static void linear_status (struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Andre Noll9d8f0362009-06-18 08:45:01 +1000282 seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
NeilBrown3be260c2014-12-15 12:56:57 +1100285static void linear_quiesce(struct mddev *mddev, int state)
286{
287}
288
NeilBrown84fc4b52011-10-11 16:49:58 +1100289static struct md_personality linear_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 .name = "linear",
NeilBrown2604b702006-01-06 00:20:36 -0800292 .level = LEVEL_LINEAR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 .owner = THIS_MODULE,
294 .make_request = linear_make_request,
295 .run = linear_run,
NeilBrownafa0f552014-12-15 12:56:58 +1100296 .free = linear_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 .status = linear_status,
NeilBrown7c7546c2006-06-26 00:27:41 -0700298 .hot_add_disk = linear_add,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700299 .size = linear_size,
NeilBrown3be260c2014-12-15 12:56:57 +1100300 .quiesce = linear_quiesce,
NeilBrown5c675f82014-12-15 12:56:56 +1100301 .congested = linear_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
304static int __init linear_init (void)
305{
NeilBrown2604b702006-01-06 00:20:36 -0800306 return register_md_personality (&linear_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309static void linear_exit (void)
310{
NeilBrown2604b702006-01-06 00:20:36 -0800311 unregister_md_personality (&linear_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314module_init(linear_init);
315module_exit(linear_exit);
316MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100317MODULE_DESCRIPTION("Linear device concatenation personality for MD");
NeilBrownd9d166c2006-01-06 00:20:51 -0800318MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
319MODULE_ALIAS("md-linear");
NeilBrown2604b702006-01-06 00:20:36 -0800320MODULE_ALIAS("md-level--1");