blob: b7fe7e9fc77730a4f7a1aca6e932aa1420b90967 [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
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
NeilBrown3be260c2014-12-15 12:56:57 +110060 conf = mddev->private;
SandeepKsinhaaf11c392009-06-18 08:49:35 +100061
NeilBrown26be34d2006-10-03 01:15:53 -070062 for (i = 0; i < mddev->raid_disks && !ret ; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020063 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
NeilBrown26be34d2006-10-03 01:15:53 -070064 ret |= bdi_congested(&q->backing_dev_info, bits);
65 }
SandeepKsinhaaf11c392009-06-18 08:49:35 +100066
NeilBrown26be34d2006-10-03 01:15:53 -070067 return ret;
68}
69
NeilBrownfd01b882011-10-11 16:47:53 +110070static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -070071{
NeilBrowne849b932011-10-11 16:48:54 +110072 struct linear_conf *conf;
SandeepKsinhaaf11c392009-06-18 08:49:35 +100073 sector_t array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070074
NeilBrown3be260c2014-12-15 12:56:57 +110075 conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -070076 WARN_ONCE(sectors || raid_disks,
77 "%s does not support generic reshape\n", __func__);
SandeepKsinhaaf11c392009-06-18 08:49:35 +100078 array_sectors = conf->array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070079
SandeepKsinhaaf11c392009-06-18 08:49:35 +100080 return array_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -070081}
82
NeilBrowne849b932011-10-11 16:48:54 +110083static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
NeilBrowne849b932011-10-11 16:48:54 +110085 struct linear_conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +110086 struct md_rdev *rdev;
Sandeep K Sinha45d45822009-06-16 16:55:26 +100087 int i, cnt;
Shaohua Lif1cad2b2012-10-11 13:08:44 +110088 bool discard_supported = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
NeilBrowna7120772011-10-11 16:48:49 +110090 conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 GFP_KERNEL);
92 if (!conf)
NeilBrown7c7546c2006-06-26 00:27:41 -070093 return NULL;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 cnt = 0;
Andre Nolld6e22152008-07-21 17:05:25 +100096 conf->array_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
NeilBrowndafb20f2012-03-19 12:46:39 +110098 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int j = rdev->raid_disk;
NeilBrowna7120772011-10-11 16:48:49 +1100100 struct dev_info *disk = conf->disks + j;
NeilBrown13f26822009-06-18 08:48:55 +1000101 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Nikanth Karthikesan13864512008-06-28 08:31:19 +1000103 if (j < 0 || j >= raid_disks || disk->rdev) {
NeilBrown2dc40f82010-05-03 15:12:04 +1000104 printk(KERN_ERR "md/linear:%s: disk numbering problem. Aborting!\n",
105 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto out;
107 }
108
109 disk->rdev = rdev;
NeilBrown13f26822009-06-18 08:48:55 +1000110 if (mddev->chunk_sectors) {
111 sectors = rdev->sectors;
112 sector_div(sectors, mddev->chunk_sectors);
113 rdev->sectors = sectors * mddev->chunk_sectors;
114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000116 disk_stack_limits(mddev->gendisk, rdev->bdev,
117 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Andre Nolldd8ac332009-03-31 14:33:13 +1100119 conf->array_sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 cnt++;
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000121
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100122 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
123 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
NeilBrown7c7546c2006-06-26 00:27:41 -0700125 if (cnt != raid_disks) {
NeilBrown2dc40f82010-05-03 15:12:04 +1000126 printk(KERN_ERR "md/linear:%s: not enough drives present. Aborting!\n",
127 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto out;
129 }
130
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100131 if (!discard_supported)
132 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
133 else
134 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /*
Sandeep K Sinha45d45822009-06-16 16:55:26 +1000137 * Here we calculate the device offsets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000139 conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
140
NeilBrowna778b732007-05-23 13:58:10 -0700141 for (i = 1; i < raid_disks; i++)
Sandeep K Sinha4db7cdc2009-06-16 16:56:13 +1000142 conf->disks[i].end_sector =
143 conf->disks[i-1].end_sector +
144 conf->disks[i].rdev->sectors;
NeilBrown15945fe2005-09-09 16:23:47 -0700145
NeilBrown7c7546c2006-06-26 00:27:41 -0700146 return conf;
147
148out:
149 kfree(conf);
150 return NULL;
151}
152
NeilBrownfd01b882011-10-11 16:47:53 +1100153static int linear_run (struct mddev *mddev)
NeilBrown7c7546c2006-06-26 00:27:41 -0700154{
NeilBrowne849b932011-10-11 16:48:54 +1100155 struct linear_conf *conf;
majianpeng98d55612012-04-02 09:48:37 +1000156 int ret;
NeilBrown7c7546c2006-06-26 00:27:41 -0700157
Andre Noll0894cc32009-06-18 08:49:23 +1000158 if (md_check_no_bitmap(mddev))
159 return -EINVAL;
NeilBrown7c7546c2006-06-26 00:27:41 -0700160 conf = linear_conf(mddev, mddev->raid_disks);
161
162 if (!conf)
163 return 1;
164 mddev->private = conf;
Dan Williams1f403622009-03-31 14:59:03 +1100165 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
NeilBrown7c7546c2006-06-26 00:27:41 -0700166
majianpeng98d55612012-04-02 09:48:37 +1000167 ret = md_integrity_register(mddev);
168 if (ret) {
169 kfree(conf);
170 mddev->private = NULL;
171 }
172 return ret;
NeilBrown7c7546c2006-06-26 00:27:41 -0700173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
NeilBrownfd01b882011-10-11 16:47:53 +1100175static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
NeilBrown7c7546c2006-06-26 00:27:41 -0700176{
177 /* Adding a drive to a linear array allows the array to grow.
178 * It is permitted if the new drive has a matching superblock
179 * already on it, with raid_disk equal to raid_disks.
180 * It is achieved by creating a new linear_private_data structure
181 * and swapping it in in-place of the current one.
182 * The current one is never freed until the array is stopped.
183 * This avoids races.
184 */
NeilBrowne849b932011-10-11 16:48:54 +1100185 struct linear_conf *newconf, *oldconf;
NeilBrown7c7546c2006-06-26 00:27:41 -0700186
NeilBrowna778b732007-05-23 13:58:10 -0700187 if (rdev->saved_raid_disk != mddev->raid_disks)
NeilBrown7c7546c2006-06-26 00:27:41 -0700188 return -EINVAL;
189
NeilBrowna778b732007-05-23 13:58:10 -0700190 rdev->raid_disk = rdev->saved_raid_disk;
NeilBrown09cd9272011-12-23 09:56:55 +1100191 rdev->saved_raid_disk = -1;
NeilBrowna778b732007-05-23 13:58:10 -0700192
NeilBrown7c7546c2006-06-26 00:27:41 -0700193 newconf = linear_conf(mddev,mddev->raid_disks+1);
194
195 if (!newconf)
196 return -ENOMEM;
197
NeilBrown3be260c2014-12-15 12:56:57 +1100198 mddev_suspend(mddev);
199 oldconf = mddev->private;
NeilBrown7c7546c2006-06-26 00:27:41 -0700200 mddev->raid_disks++;
NeilBrown3be260c2014-12-15 12:56:57 +1100201 mddev->private = newconf;
Dan Williams1f403622009-03-31 14:59:03 +1100202 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
Andre Nollf233ea52008-07-21 17:05:22 +1000203 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown3be260c2014-12-15 12:56:57 +1100204 mddev_resume(mddev);
NeilBrown449aad32009-08-03 10:59:58 +1000205 revalidate_disk(mddev->gendisk);
NeilBrown3be260c2014-12-15 12:56:57 +1100206 kfree(oldconf);
NeilBrown7c7546c2006-06-26 00:27:41 -0700207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
NeilBrownafa0f552014-12-15 12:56:58 +1100210static void linear_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
NeilBrownafa0f552014-12-15 12:56:58 +1100212 struct linear_conf *conf = priv;
SandeepKsinhaaf11c392009-06-18 08:49:35 +1000213
NeilBrown495d3572009-06-18 08:49:42 +1000214 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700217static void linear_make_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Kent Overstreet20d01892013-11-23 18:21:01 -0800219 char b[BDEVNAME_SIZE];
NeilBrowna7120772011-10-11 16:48:49 +1100220 struct dev_info *tmp_dev;
Kent Overstreet20d01892013-11-23 18:21:01 -0800221 struct bio *split;
222 sector_t start_sector, end_sector, data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Tejun Heoe9c74692010-09-03 11:56:18 +0200224 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
225 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200226 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700227 }
228
Kent Overstreet20d01892013-11-23 18:21:01 -0800229 do {
Kent Overstreet20d01892013-11-23 18:21:01 -0800230 tmp_dev = which_dev(mddev, bio->bi_iter.bi_sector);
231 start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
232 end_sector = tmp_dev->end_sector;
233 data_offset = tmp_dev->rdev->data_offset;
234 bio->bi_bdev = tmp_dev->rdev->bdev;
Andre Noll62838152008-10-13 11:55:12 +1100235
Kent Overstreet20d01892013-11-23 18:21:01 -0800236 if (unlikely(bio->bi_iter.bi_sector >= end_sector ||
237 bio->bi_iter.bi_sector < start_sector))
238 goto out_of_bounds;
Andre Noll62838152008-10-13 11:55:12 +1100239
Kent Overstreet20d01892013-11-23 18:21:01 -0800240 if (unlikely(bio_end_sector(bio) > end_sector)) {
241 /* This bio crosses a device boundary, so we have to
242 * split it.
243 */
244 split = bio_split(bio, end_sector -
245 bio->bi_iter.bi_sector,
246 GFP_NOIO, fs_bio_set);
247 bio_chain(split, bio);
248 } else {
249 split = bio;
250 }
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100251
Kent Overstreet20d01892013-11-23 18:21:01 -0800252 split->bi_iter.bi_sector = split->bi_iter.bi_sector -
253 start_sector + data_offset;
Shaohua Lif1cad2b2012-10-11 13:08:44 +1100254
Kent Overstreet20d01892013-11-23 18:21:01 -0800255 if (unlikely((split->bi_rw & REQ_DISCARD) &&
256 !blk_queue_discard(bdev_get_queue(split->bi_bdev)))) {
257 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200258 bio_endio(split);
Kent Overstreet20d01892013-11-23 18:21:01 -0800259 } else
260 generic_make_request(split);
261 } while (split != bio);
262 return;
263
264out_of_bounds:
265 printk(KERN_ERR
266 "md/linear:%s: make_request: Sector %llu out of bounds on "
267 "dev %s: %llu sectors, offset %llu\n",
268 mdname(mddev),
269 (unsigned long long)bio->bi_iter.bi_sector,
270 bdevname(tmp_dev->rdev->bdev, b),
271 (unsigned long long)tmp_dev->rdev->sectors,
272 (unsigned long long)start_sector);
273 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
NeilBrownfd01b882011-10-11 16:47:53 +1100276static void linear_status (struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278
Andre Noll9d8f0362009-06-18 08:45:01 +1000279 seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
NeilBrown3be260c2014-12-15 12:56:57 +1100282static void linear_quiesce(struct mddev *mddev, int state)
283{
284}
285
NeilBrown84fc4b52011-10-11 16:49:58 +1100286static struct md_personality linear_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 .name = "linear",
NeilBrown2604b702006-01-06 00:20:36 -0800289 .level = LEVEL_LINEAR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 .owner = THIS_MODULE,
291 .make_request = linear_make_request,
292 .run = linear_run,
NeilBrownafa0f552014-12-15 12:56:58 +1100293 .free = linear_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 .status = linear_status,
NeilBrown7c7546c2006-06-26 00:27:41 -0700295 .hot_add_disk = linear_add,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700296 .size = linear_size,
NeilBrown3be260c2014-12-15 12:56:57 +1100297 .quiesce = linear_quiesce,
NeilBrown5c675f82014-12-15 12:56:56 +1100298 .congested = linear_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299};
300
301static int __init linear_init (void)
302{
NeilBrown2604b702006-01-06 00:20:36 -0800303 return register_md_personality (&linear_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306static void linear_exit (void)
307{
NeilBrown2604b702006-01-06 00:20:36 -0800308 unregister_md_personality (&linear_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311module_init(linear_init);
312module_exit(linear_exit);
313MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100314MODULE_DESCRIPTION("Linear device concatenation personality for MD");
NeilBrownd9d166c2006-01-06 00:20:51 -0800315MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
316MODULE_ALIAS("md-linear");
NeilBrown2604b702006-01-06 00:20:36 -0800317MODULE_ALIAS("md-level--1");