blob: 29a4bbf62be533097c68514097c89bbf0d89d3f0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10003 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050056#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110059static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
NeilBrown01f96c02011-09-21 15:30:20 +100062/* pers_list is a list of registered personalities protected
63 * by pers_lock.
64 * pers_lock does extra service to protect accesses to
65 * mddev->thread when the mutex cannot be held.
66 */
NeilBrown2604b702006-01-06 00:20:36 -080067static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_SPINLOCK(pers_lock);
69
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050070struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050071EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050072struct module *md_cluster_mod;
73EXPORT_SYMBOL(md_cluster_mod);
74
Bernd Schubert90b08712008-05-23 13:04:38 -070075static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020076static struct workqueue_struct *md_wq;
77static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070078
NeilBrown746d3202013-04-24 11:42:41 +100079static int remove_and_add_spares(struct mddev *mddev,
80 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110081static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Robert Becker1e509152009-12-14 12:49:58 +110084 * Default number of read corrections we'll attempt on an rdev
85 * before ejecting it from the array. We divide the read error
86 * count by 2 for every hour elapsed between read errors.
87 */
88#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
89/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
91 * is 1000 KB/sec, so the extra system load does not show up that much.
92 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070093 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * subsystem is idle. There is also an 'absolute maximum' reconstruction
95 * speed limit - in case reconstruction slows down your system despite
96 * idle IO detection.
97 *
98 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080099 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101
102static int sysctl_speed_limit_min = 1000;
103static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_min ?
107 mddev->sync_speed_min : sysctl_speed_limit_min;
108}
109
NeilBrownfd01b882011-10-11 16:47:53 +1100110static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800111{
112 return mddev->sync_speed_max ?
113 mddev->sync_speed_max : sysctl_speed_limit_max;
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct ctl_table_header *raid_table_header;
117
Joe Perches82592c32013-11-14 15:16:18 +1100118static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .procname = "speed_limit_min",
121 .data = &sysctl_speed_limit_min,
122 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700123 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800124 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 },
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .procname = "speed_limit_max",
128 .data = &sysctl_speed_limit_max,
129 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700130 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800131 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800133 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Joe Perches82592c32013-11-14 15:16:18 +1100136static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .procname = "raid",
139 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .child = raid_table,
142 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800143 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Joe Perches82592c32013-11-14 15:16:18 +1100146static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .procname = "dev",
149 .maxlen = 0,
150 .mode = 0555,
151 .child = raid_dir_table,
152 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800153 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700156static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
NeilBrownf91de922005-11-08 21:39:36 -0800158static int start_readonly;
159
NeilBrowna167f662010-10-26 18:31:13 +1100160/* bio_clone_mddev
161 * like bio_clone, but with a local bio set
162 */
163
NeilBrowna167f662010-10-26 18:31:13 +1100164struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100165 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100166{
167 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100168
169 if (!mddev || !mddev->bio_set)
170 return bio_alloc(gfp_mask, nr_iovecs);
171
Kent Overstreet395c72a2012-09-06 15:34:55 -0700172 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100173 if (!b)
174 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100175 return b;
176}
177EXPORT_SYMBOL_GPL(bio_alloc_mddev);
178
179struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100180 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100181{
NeilBrowna167f662010-10-26 18:31:13 +1100182 if (!mddev || !mddev->bio_set)
183 return bio_clone(bio, gfp_mask);
184
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700185 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100186}
187EXPORT_SYMBOL_GPL(bio_clone_mddev);
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
NeilBrownd7603b72006-01-06 00:20:30 -0800190 * We have a system wide 'event count' that is incremented
191 * on any 'interesting' event, and readers of /proc/mdstat
192 * can use 'poll' or 'select' to find out when the event
193 * count increases.
194 *
195 * Events are:
196 * start array, stop array, error, add device, remove device,
197 * start build, activate spare
198 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800199static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800200static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100201void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800202{
203 atomic_inc(&md_event_count);
204 wake_up(&md_event_waiters);
205}
NeilBrown29269552006-03-27 01:18:10 -0800206EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800207
208/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * Enables to iterate over all existing md arrays
210 * all_mddevs_lock protects this list.
211 */
212static LIST_HEAD(all_mddevs);
213static DEFINE_SPINLOCK(all_mddevs_lock);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
216 * iterates through all used mddevs in the system.
217 * We take care to grab the all_mddevs_lock whenever navigating
218 * the list, and to always hold a refcount when unlocked.
219 * Any code which breaks out of this loop while own
220 * a reference to the current mddev and must mddev_put it.
221 */
NeilBrownfd01b882011-10-11 16:47:53 +1100222#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000224 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100225 _tmp = all_mddevs.next; \
226 _mddev = NULL;}); \
227 ({ if (_tmp != &all_mddevs) \
228 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100230 if (_mddev) mddev_put(_mddev); \
231 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
232 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100234 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 )
236
NeilBrown409c57f2009-03-31 14:39:39 +1100237/* Rather than calling directly into the personality make_request function,
238 * IO requests come here first so that we can check if the device is
239 * being suspended pending a reconfiguration.
240 * We hold a refcount over the call to ->make_request. By the time that
241 * call has finished, the bio has been linked into some internal structure
242 * and so is visible to ->quiesce(), so we don't need the refcount any more.
243 */
Jens Axboedece1632015-11-05 10:41:16 -0700244static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
NeilBrown49077322010-03-25 16:20:56 +1100246 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100247 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500248 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800249 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100250
Kent Overstreet54efd502015-04-23 22:37:18 -0700251 blk_queue_split(q, &bio, q->bio_split);
252
NeilBrown274d8cb2016-01-04 16:16:58 +1100253 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100254 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700255 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100256 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100257 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200258 if (bio_sectors(bio) != 0)
259 bio->bi_error = -EROFS;
260 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700261 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100262 }
NeilBrown0ca69882011-01-14 09:14:33 +1100263 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100264 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200265 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100266 DEFINE_WAIT(__wait);
267 for (;;) {
268 prepare_to_wait(&mddev->sb_wait, &__wait,
269 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200270 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100271 break;
272 rcu_read_unlock();
273 schedule();
274 rcu_read_lock();
275 }
276 finish_wait(&mddev->sb_wait, &__wait);
277 }
278 atomic_inc(&mddev->active_io);
279 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100280
Chris Masone91ece52011-02-07 19:21:48 -0500281 /*
282 * save the sectors now since our bio can
283 * go away inside make_request
284 */
285 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200286 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100287
Gu Zheng74672d02015-04-03 08:44:47 +0800288 cpu = part_stat_lock();
289 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
290 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
291 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100292
NeilBrown409c57f2009-03-31 14:39:39 +1100293 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
294 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700295
296 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
NeilBrown9e35b992010-04-06 14:23:02 +1000299/* mddev_suspend makes sure no new requests are submitted
300 * to the device, and that any requests that have been submitted
301 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100302 * Once mddev_detach() is called and completes, the module will be
303 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000304 */
NeilBrownfd01b882011-10-11 16:47:53 +1100305void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100306{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100307 if (mddev->suspended++)
308 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100309 synchronize_rcu();
310 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
311 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500312
313 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100314}
NeilBrown390ee602010-06-01 19:37:27 +1000315EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100316
NeilBrownfd01b882011-10-11 16:47:53 +1100317void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100318{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100319 if (--mddev->suspended)
320 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100321 wake_up(&mddev->sb_wait);
322 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500323
Jonathan Brassow47525e52012-05-22 13:55:29 +1000324 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500325 md_wakeup_thread(mddev->thread);
326 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100327}
NeilBrown390ee602010-06-01 19:37:27 +1000328EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100329
NeilBrownfd01b882011-10-11 16:47:53 +1100330int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000331{
NeilBrown5c675f82014-12-15 12:56:56 +1100332 struct md_personality *pers = mddev->pers;
333 int ret = 0;
334
335 rcu_read_lock();
336 if (mddev->suspended)
337 ret = 1;
338 else if (pers && pers->congested)
339 ret = pers->congested(mddev, bits);
340 rcu_read_unlock();
341 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000342}
NeilBrown5c675f82014-12-15 12:56:56 +1100343EXPORT_SYMBOL_GPL(mddev_congested);
344static int md_congested(void *data, int bits)
345{
346 struct mddev *mddev = data;
347 return mddev_congested(mddev, bits);
348}
NeilBrown3fa841d2009-09-23 18:10:29 +1000349
NeilBrowna2826aa2009-12-14 12:49:49 +1100350/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200351 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100352 */
353
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200354static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100355{
NeilBrown3cb03002011-10-11 16:45:26 +1100356 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100357 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100358
359 rdev_dec_pending(rdev, mddev);
360
361 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200362 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200363 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100364 }
365 bio_put(bio);
366}
367
NeilBrowna7a07e62010-12-09 16:04:25 +1100368static void md_submit_flush_data(struct work_struct *ws);
369
NeilBrowna035fc32010-12-09 16:17:51 +1100370static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100371{
NeilBrownfd01b882011-10-11 16:47:53 +1100372 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100373 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100374
NeilBrowna7a07e62010-12-09 16:04:25 +1100375 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
376 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100377 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100378 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 if (rdev->raid_disk >= 0 &&
380 !test_bit(Faulty, &rdev->flags)) {
381 /* Take two references, one is dropped
382 * when request finishes, one after
383 * we reclaim rcu_read_lock
384 */
385 struct bio *bi;
386 atomic_inc(&rdev->nr_pending);
387 atomic_inc(&rdev->nr_pending);
388 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000389 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200390 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100391 bi->bi_private = rdev;
392 bi->bi_bdev = rdev->bdev;
393 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100395 rcu_read_lock();
396 rdev_dec_pending(rdev, mddev);
397 }
398 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100399 if (atomic_dec_and_test(&mddev->flush_pending))
400 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100401}
402
Tejun Heoe9c74692010-09-03 11:56:18 +0200403static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100404{
NeilBrownfd01b882011-10-11 16:47:53 +1100405 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200406 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100407
Kent Overstreet4f024f32013-10-11 15:44:27 -0700408 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200410 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200412 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200413 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100414 }
NeilBrown2b74e122010-12-09 15:59:01 +1100415
416 mddev->flush_bio = NULL;
417 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100418}
419
NeilBrownfd01b882011-10-11 16:47:53 +1100420void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100421{
NeilBrown85572d72014-12-15 12:56:56 +1100422 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100423 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100425 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200426 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100427 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100428
NeilBrowna035fc32010-12-09 16:17:51 +1100429 INIT_WORK(&mddev->flush_work, submit_flushes);
430 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100431}
Tejun Heoe9c74692010-09-03 11:56:18 +0200432EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100433
NeilBrown74018dc2012-07-31 09:08:15 +0200434void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000435{
NeilBrown9cbb1752012-07-31 09:08:14 +0200436 struct mddev *mddev = cb->data;
437 md_wakeup_thread(mddev->thread);
438 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000439}
NeilBrown9cbb1752012-07-31 09:08:14 +0200440EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000441
NeilBrownfd01b882011-10-11 16:47:53 +1100442static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 atomic_inc(&mddev->active);
445 return mddev;
446}
447
Dan Williams5fd3a172009-03-04 00:57:25 -0700448static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100449
NeilBrownfd01b882011-10-11 16:47:53 +1100450static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
NeilBrowna167f662010-10-26 18:31:13 +1100452 struct bio_set *bs = NULL;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
455 return;
NeilBrownd3374822009-01-09 08:31:10 +1100456 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100457 mddev->ctime == 0 && !mddev->hold_active) {
458 /* Array is not configured at all, and not held active,
459 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100460 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100461 bs = mddev->bio_set;
462 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100463 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200464 /* We did a probe so need to clean up. Call
465 * queue_work inside the spinlock so that
466 * flush_workqueue() after mddev_find will
467 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100468 */
469 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200470 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100471 } else
472 kfree(mddev);
473 }
474 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100475 if (bs)
476 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Sasha Levin25b2edf2015-07-24 18:19:58 -0400479static void md_safemode_timeout(unsigned long data);
480
NeilBrownfd01b882011-10-11 16:47:53 +1100481void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100482{
483 mutex_init(&mddev->open_mutex);
484 mutex_init(&mddev->reconfig_mutex);
485 mutex_init(&mddev->bitmap_info.mutex);
486 INIT_LIST_HEAD(&mddev->disks);
487 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400488 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
489 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100490 atomic_set(&mddev->active, 1);
491 atomic_set(&mddev->openers, 0);
492 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100493 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100494 atomic_set(&mddev->flush_pending, 0);
495 init_waitqueue_head(&mddev->sb_wait);
496 init_waitqueue_head(&mddev->recovery_wait);
497 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000498 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500499 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100500 mddev->resync_min = 0;
501 mddev->resync_max = MaxSector;
502 mddev->level = LEVEL_NONE;
503}
NeilBrown390ee602010-06-01 19:37:27 +1000504EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100505
NeilBrownf72ffdd2014-09-30 14:23:59 +1000506static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
NeilBrownfd01b882011-10-11 16:47:53 +1100508 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
NeilBrown8f5f02c2011-02-16 13:58:51 +1100510 if (unit && MAJOR(unit) != MD_MAJOR)
511 unit &= ~((1<<MdpMinorShift)-1);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 retry:
514 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
NeilBrownefeb53c2009-01-09 08:31:10 +1100516 if (unit) {
517 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
518 if (mddev->unit == unit) {
519 mddev_get(mddev);
520 spin_unlock(&all_mddevs_lock);
521 kfree(new);
522 return mddev;
523 }
524
525 if (new) {
526 list_add(&new->all_mddevs, &all_mddevs);
527 spin_unlock(&all_mddevs_lock);
528 new->hold_active = UNTIL_IOCTL;
529 return new;
530 }
531 } else if (new) {
532 /* find an unused unit number */
533 static int next_minor = 512;
534 int start = next_minor;
535 int is_free = 0;
536 int dev = 0;
537 while (!is_free) {
538 dev = MKDEV(MD_MAJOR, next_minor);
539 next_minor++;
540 if (next_minor > MINORMASK)
541 next_minor = 0;
542 if (next_minor == start) {
543 /* Oh dear, all in use. */
544 spin_unlock(&all_mddevs_lock);
545 kfree(new);
546 return NULL;
547 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000548
NeilBrownefeb53c2009-01-09 08:31:10 +1100549 is_free = 1;
550 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
551 if (mddev->unit == dev) {
552 is_free = 0;
553 break;
554 }
555 }
556 new->unit = dev;
557 new->md_minor = MINOR(dev);
558 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 list_add(&new->all_mddevs, &all_mddevs);
560 spin_unlock(&all_mddevs_lock);
561 return new;
562 }
563 spin_unlock(&all_mddevs_lock);
564
NeilBrown9ffae0c2006-01-06 00:20:32 -0800565 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!new)
567 return NULL;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 new->unit = unit;
570 if (MAJOR(unit) == MD_MAJOR)
571 new->md_minor = MINOR(unit);
572 else
573 new->md_minor = MINOR(unit) >> MdpMinorShift;
574
NeilBrownfafd7fb2010-04-01 15:55:30 +1100575 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto retry;
578}
579
NeilBrownb6eb1272010-04-15 10:13:47 +1000580static struct attribute_group md_redundancy_group;
581
NeilBrown5c47daf2014-12-15 12:57:01 +1100582void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
NeilBrowna64c8762010-04-14 17:15:37 +1000584 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000585 /* These cannot be removed under reconfig_mutex as
586 * an access to the files will try to take reconfig_mutex
587 * while holding the file unremovable, which leads to
588 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000589 * So hold set sysfs_active while the remove in happeing,
590 * and anything else which might set ->to_remove or my
591 * otherwise change the sysfs namespace will fail with
592 * -EBUSY if sysfs_active is still set.
593 * We set sysfs_active under reconfig_mutex and elsewhere
594 * test it under the same mutex to ensure its correct value
595 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000596 */
NeilBrowna64c8762010-04-14 17:15:37 +1000597 struct attribute_group *to_remove = mddev->to_remove;
598 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000599 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000600 mutex_unlock(&mddev->reconfig_mutex);
601
NeilBrown00bcb4a2010-06-01 19:37:23 +1000602 if (mddev->kobj.sd) {
603 if (to_remove != &md_redundancy_group)
604 sysfs_remove_group(&mddev->kobj, to_remove);
605 if (mddev->pers == NULL ||
606 mddev->pers->sync_request == NULL) {
607 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
608 if (mddev->sysfs_action)
609 sysfs_put(mddev->sysfs_action);
610 mddev->sysfs_action = NULL;
611 }
NeilBrowna64c8762010-04-14 17:15:37 +1000612 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000613 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000614 } else
615 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Chris Dunlop751e67c2011-10-19 16:48:26 +1100617 /* As we've dropped the mutex we need a spinlock to
618 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000619 */
620 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700621 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000622 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
NeilBrown5c47daf2014-12-15 12:57:01 +1100624EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500626struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100627{
628 struct md_rdev *rdev;
629
630 rdev_for_each_rcu(rdev, mddev)
631 if (rdev->desc_nr == nr)
632 return rdev;
633
634 return NULL;
635}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500636EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100637
638static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
NeilBrown3cb03002011-10-11 16:45:26 +1100640 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
NeilBrowndafb20f2012-03-19 12:46:39 +1100642 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (rdev->bdev->bd_dev == dev)
644 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return NULL;
647}
648
NeilBrown1ca69c42012-10-11 13:37:33 +1100649static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
650{
651 struct md_rdev *rdev;
652
653 rdev_for_each_rcu(rdev, mddev)
654 if (rdev->bdev->bd_dev == dev)
655 return rdev;
656
657 return NULL;
658}
659
NeilBrown84fc4b52011-10-11 16:49:58 +1100660static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800661{
NeilBrown84fc4b52011-10-11 16:49:58 +1100662 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800663 list_for_each_entry(pers, &pers_list, list) {
664 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800665 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800666 if (strcmp(pers->name, clevel)==0)
667 return pers;
668 }
NeilBrown2604b702006-01-06 00:20:36 -0800669 return NULL;
670}
671
Andre Nollb73df2d2008-07-11 22:02:23 +1000672/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100673static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100675 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000676 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
NeilBrownf72ffdd2014-09-30 14:23:59 +1000679static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 rdev->sb_page = alloc_page(GFP_KERNEL);
682 if (!rdev->sb_page) {
683 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000684 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 return 0;
688}
689
NeilBrown545c8792012-05-22 13:54:30 +1000690void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800693 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 rdev->sb_loaded = 0;
695 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000696 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100697 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
NeilBrown2699b672011-07-28 11:31:47 +1000699 if (rdev->bb_page) {
700 put_page(rdev->bb_page);
701 rdev->bb_page = NULL;
702 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000703 kfree(rdev->badblocks.page);
704 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
NeilBrown545c8792012-05-22 13:54:30 +1000706EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200708static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700709{
NeilBrown3cb03002011-10-11 16:45:26 +1100710 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100711 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700712
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200713 if (bio->bi_error) {
714 printk("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800715 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700716 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700717
NeilBrowna9701a32005-11-08 21:39:34 -0800718 if (atomic_dec_and_test(&mddev->pending_writes))
719 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700720 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700721}
722
NeilBrownfd01b882011-10-11 16:47:53 +1100723void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700724 sector_t sector, int size, struct page *page)
725{
726 /* write first size bytes of page to sector of rdev
727 * Increment mddev->pending_writes before returning
728 * and decrement it on completion, waking up sb_wait
729 * if zero is reached.
730 * If an error occurred, call md_error
731 */
NeilBrowna167f662010-10-26 18:31:13 +1100732 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700733
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100734 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700735 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700736 bio_add_page(bio, page, size, 0);
737 bio->bi_private = rdev;
738 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800739
NeilBrown7bfa19f2005-06-21 17:17:28 -0700740 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000741 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800742}
743
NeilBrownfd01b882011-10-11 16:47:53 +1100744void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800745{
Tejun Heoe9c74692010-09-03 11:56:18 +0200746 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000747 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700748}
749
NeilBrown3cb03002011-10-11 16:45:26 +1100750int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100751 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
NeilBrowna167f662010-10-26 18:31:13 +1100753 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int ret;
755
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100756 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
757 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100758 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700759 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000760 else if (rdev->mddev->reshape_position != MaxSector &&
761 (rdev->mddev->reshape_backwards ==
762 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700763 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100764 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700765 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700767 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200769 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 bio_put(bio);
771 return ret;
772}
NeilBrowna8745db2006-01-06 00:20:34 -0800773EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
NeilBrownf72ffdd2014-09-30 14:23:59 +1000775static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (rdev->sb_loaded)
780 return 0;
781
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100782 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 goto fail;
784 rdev->sb_loaded = 1;
785 return 0;
786
787fail:
788 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
789 bdevname(rdev->bdev,b));
790 return -EINVAL;
791}
792
793static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
794{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000795 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000796 sb1->set_uuid1 == sb2->set_uuid1 &&
797 sb1->set_uuid2 == sb2->set_uuid2 &&
798 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
802{
803 int ret;
804 mdp_super_t *tmp1, *tmp2;
805
806 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
807 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
808
809 if (!tmp1 || !tmp2) {
810 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100811 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto abort;
813 }
814
815 *tmp1 = *sb1;
816 *tmp2 = *sb2;
817
818 /*
819 * nr_disks is not constant
820 */
821 tmp1->nr_disks = 0;
822 tmp2->nr_disks = 0;
823
Andre Nollce0c8e02008-07-11 22:02:20 +1000824 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700826 kfree(tmp1);
827 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return ret;
829}
830
NeilBrown4d167f02007-05-09 02:35:37 -0700831static u32 md_csum_fold(u32 csum)
832{
833 csum = (csum & 0xffff) + (csum >> 16);
834 return (csum & 0xffff) + (csum >> 16);
835}
836
NeilBrownf72ffdd2014-09-30 14:23:59 +1000837static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
NeilBrown4d167f02007-05-09 02:35:37 -0700839 u64 newcsum = 0;
840 u32 *sb32 = (u32*)sb;
841 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 unsigned int disk_csum, csum;
843
844 disk_csum = sb->sb_csum;
845 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700846
847 for (i = 0; i < MD_SB_BYTES/4 ; i++)
848 newcsum += sb32[i];
849 csum = (newcsum & 0xffffffff) + (newcsum>>32);
850
NeilBrown4d167f02007-05-09 02:35:37 -0700851#ifdef CONFIG_ALPHA
852 /* This used to use csum_partial, which was wrong for several
853 * reasons including that different results are returned on
854 * different architectures. It isn't critical that we get exactly
855 * the same return value as before (we always csum_fold before
856 * testing, and that removes any differences). However as we
857 * know that csum_partial always returned a 16bit value on
858 * alphas, do a fold to maximise conformity to previous behaviour.
859 */
860 sb->sb_csum = md_csum_fold(disk_csum);
861#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700863#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return csum;
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/*
868 * Handle superblock details.
869 * We want to be able to handle multiple superblock formats
870 * so we have a common interface to them all, and an array of
871 * different handlers.
872 * We rely on user-space to write the initial superblock, and support
873 * reading and updating of superblocks.
874 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100875 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * loads and validates a superblock on dev.
877 * if refdev != NULL, compare superblocks on both devices
878 * Return:
879 * 0 - dev has a superblock that is compatible with refdev
880 * 1 - dev has a superblock that is compatible and newer than refdev
881 * so dev should be used as the refdev in future
882 * -EINVAL superblock incompatible or invalid
883 * -othererror e.g. -EIO
884 *
NeilBrownfd01b882011-10-11 16:47:53 +1100885 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * Verify that dev is acceptable into mddev.
887 * The first time, mddev->raid_disks will be 0, and data from
888 * dev should be merged in. Subsequent calls check that dev
889 * is new enough. Return 0 or -EINVAL
890 *
NeilBrownfd01b882011-10-11 16:47:53 +1100891 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * Update the superblock for rdev with data in mddev
893 * This does not write to disc.
894 *
895 */
896
897struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000898 char *name;
899 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000900 int (*load_super)(struct md_rdev *rdev,
901 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000902 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000903 int (*validate_super)(struct mddev *mddev,
904 struct md_rdev *rdev);
905 void (*sync_super)(struct mddev *mddev,
906 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100907 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000908 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000909 int (*allow_new_offset)(struct md_rdev *rdev,
910 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911};
912
913/*
Andre Noll0894cc32009-06-18 08:49:23 +1000914 * Check that the given mddev has no bitmap.
915 *
916 * This function is called from the run method of all personalities that do not
917 * support bitmaps. It prints an error message and returns non-zero if mddev
918 * has a bitmap. Otherwise, it returns 0.
919 *
920 */
NeilBrownfd01b882011-10-11 16:47:53 +1100921int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000922{
NeilBrownc3d97142009-12-14 12:49:52 +1100923 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000924 return 0;
925 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
926 mdname(mddev), mddev->pers->name);
927 return 1;
928}
929EXPORT_SYMBOL(md_check_no_bitmap);
930
931/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000932 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
NeilBrown3cb03002011-10-11 16:45:26 +1100934static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
937 mdp_super_t *sb;
938 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /*
Andre Noll0f420352008-07-11 22:02:23 +1000941 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 * it's at the end of the disk.
943 *
944 * It also happens to be a multiple of 4Kb.
945 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100946 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
NeilBrown0002b272005-09-09 16:23:53 -0700948 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (ret) return ret;
950
951 ret = -EINVAL;
952
953 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000954 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (sb->md_magic != MD_SB_MAGIC) {
957 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
958 b);
959 goto abort;
960 }
961
962 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800963 sb->minor_version < 90 ||
964 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
966 sb->major_version, sb->minor_version,
967 b);
968 goto abort;
969 }
970
971 if (sb->raid_disks <= 0)
972 goto abort;
973
NeilBrown4d167f02007-05-09 02:35:37 -0700974 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
976 b);
977 goto abort;
978 }
979
980 rdev->preferred_minor = sb->md_minor;
981 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000982 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700983 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000984 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (sb->level == LEVEL_MULTIPATH)
987 rdev->desc_nr = -1;
988 else
989 rdev->desc_nr = sb->this_disk.number;
990
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700991 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700993 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +1000995 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!uuid_equal(refsb, sb)) {
997 printk(KERN_WARNING "md: %s has different UUID to %s\n",
998 b, bdevname(refdev->bdev,b2));
999 goto abort;
1000 }
1001 if (!sb_equal(refsb, sb)) {
1002 printk(KERN_WARNING "md: %s has same UUID"
1003 " but different superblock to %s\n",
1004 b, bdevname(refdev->bdev, b2));
1005 goto abort;
1006 }
1007 ev1 = md_event(sb);
1008 ev2 = md_event(refsb);
1009 if (ev1 > ev2)
1010 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001011 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ret = 0;
1013 }
NeilBrown8190e752009-06-18 08:48:58 +10001014 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001015 /* Limit to 4TB as metadata cannot record more than that.
1016 * (not needed for Linear and RAID0 as metadata doesn't
1017 * record this size)
1018 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001019 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1020 sb->level >= 1)
1021 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
NeilBrown27a7b262011-09-10 17:21:28 +10001023 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001024 /* "this cannot possibly happen" ... */
1025 ret = -EINVAL;
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 abort:
1028 return ret;
1029}
1030
1031/*
1032 * validate_super for 0.90.0
1033 */
NeilBrownfd01b882011-10-11 16:47:53 +11001034static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001037 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001038 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
NeilBrown41158c72005-06-21 17:17:25 -07001040 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001041 clear_bit(Faulty, &rdev->flags);
1042 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001043 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001044 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (mddev->raid_disks == 0) {
1047 mddev->major_version = 0;
1048 mddev->minor_version = sb->minor_version;
1049 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001050 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001051 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 mddev->ctime = sb->ctime;
1053 mddev->utime = sb->utime;
1054 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001055 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 mddev->layout = sb->layout;
1057 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001058 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001059 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001060 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001061 mddev->bitmap_info.space = 0;
1062 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001063 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001064 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001065 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
NeilBrownf6705572006-03-27 01:18:11 -08001067 if (mddev->minor_version >= 91) {
1068 mddev->reshape_position = sb->reshape_position;
1069 mddev->delta_disks = sb->delta_disks;
1070 mddev->new_level = sb->new_level;
1071 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001072 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001073 if (mddev->delta_disks < 0)
1074 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001075 } else {
1076 mddev->reshape_position = MaxSector;
1077 mddev->delta_disks = 0;
1078 mddev->new_level = mddev->level;
1079 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001080 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001081 }
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (sb->state & (1<<MD_SB_CLEAN))
1084 mddev->recovery_cp = MaxSector;
1085 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001086 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 sb->events_lo == sb->cp_events_lo) {
1088 mddev->recovery_cp = sb->recovery_cp;
1089 } else
1090 mddev->recovery_cp = 0;
1091 }
1092
1093 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1094 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1095 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1096 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1097
1098 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001099
1100 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001101 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001102 mddev->bitmap_info.offset =
1103 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001104 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001105 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001106 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001107
NeilBrown41158c72005-06-21 17:17:25 -07001108 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001109 /* Insist on good event counter while assembling, except
1110 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001112 if (sb->disks[rdev->desc_nr].state & (
1113 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001114 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001115 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001116 } else if (mddev->bitmap) {
1117 /* if adding to array with a bitmap, then we can accept an
1118 * older device ... but not too old.
1119 */
NeilBrown41158c72005-06-21 17:17:25 -07001120 if (ev1 < mddev->bitmap->events_cleared)
1121 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001122 if (ev1 < mddev->events)
1123 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001124 } else {
1125 if (ev1 < mddev->events)
1126 /* just a hot-add of a new device, leave raid_disk at -1 */
1127 return 0;
1128 }
NeilBrown41158c72005-06-21 17:17:25 -07001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 desc = sb->disks + rdev->desc_nr;
1132
1133 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001134 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001135 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1136 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001137 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001139 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001140 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1141 /* active but not in sync implies recovery up to
1142 * reshape position. We don't know exactly where
1143 * that is, so set to zero for now */
1144 if (mddev->minor_version >= 91) {
1145 rdev->recovery_offset = 0;
1146 rdev->raid_disk = desc->raid_disk;
1147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001149 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1150 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001151 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001152 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return 0;
1154}
1155
1156/*
1157 * sync_super for 0.90.0
1158 */
NeilBrownfd01b882011-10-11 16:47:53 +11001159static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001162 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* make rdev->sb match mddev data..
1166 *
1167 * 1/ zero out disks
1168 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1169 * 3/ any empty disks < next_spare become removed
1170 *
1171 * disks[0] gets initialised to REMOVED because
1172 * we cannot be sure from other fields if it has
1173 * been initialised or not.
1174 */
1175 int i;
1176 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1177
NeilBrown61181562005-09-09 16:24:02 -07001178 rdev->sb_size = MD_SB_BYTES;
1179
Namhyung Kim65a06f062011-07-27 11:00:36 +10001180 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 memset(sb, 0, sizeof(*sb));
1183
1184 sb->md_magic = MD_SB_MAGIC;
1185 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sb->patch_version = mddev->patch_version;
1187 sb->gvalid_words = 0; /* ignored */
1188 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1189 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1190 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1191 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1192
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001193 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001195 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 sb->raid_disks = mddev->raid_disks;
1197 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001198 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001199 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 sb->state = 0;
1201 sb->events_hi = (mddev->events>>32);
1202 sb->events_lo = (u32)mddev->events;
1203
NeilBrownf6705572006-03-27 01:18:11 -08001204 if (mddev->reshape_position == MaxSector)
1205 sb->minor_version = 90;
1206 else {
1207 sb->minor_version = 91;
1208 sb->reshape_position = mddev->reshape_position;
1209 sb->new_level = mddev->new_level;
1210 sb->delta_disks = mddev->delta_disks;
1211 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001212 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001213 }
1214 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (mddev->in_sync)
1216 {
1217 sb->recovery_cp = mddev->recovery_cp;
1218 sb->cp_events_hi = (mddev->events>>32);
1219 sb->cp_events_lo = (u32)mddev->events;
1220 if (mddev->recovery_cp == MaxSector)
1221 sb->state = (1<< MD_SB_CLEAN);
1222 } else
1223 sb->recovery_cp = 0;
1224
1225 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001226 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
NeilBrownc3d97142009-12-14 12:49:52 +11001228 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001229 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001232 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001234 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001235 int is_active = test_bit(In_sync, &rdev2->flags);
1236
1237 if (rdev2->raid_disk >= 0 &&
1238 sb->minor_version >= 91)
1239 /* we have nowhere to store the recovery_offset,
1240 * but if it is not below the reshape_position,
1241 * we can piggy-back on that.
1242 */
1243 is_active = 1;
1244 if (rdev2->raid_disk < 0 ||
1245 test_bit(Faulty, &rdev2->flags))
1246 is_active = 0;
1247 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001248 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001250 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001251 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 d = &sb->disks[rdev2->desc_nr];
1253 nr_disks++;
1254 d->number = rdev2->desc_nr;
1255 d->major = MAJOR(rdev2->bdev->bd_dev);
1256 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001257 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 d->raid_disk = rdev2->raid_disk;
1259 else
1260 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001261 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001263 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001265 if (test_bit(In_sync, &rdev2->flags))
1266 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 active++;
1268 working++;
1269 } else {
1270 d->state = 0;
1271 spare++;
1272 working++;
1273 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001274 if (test_bit(WriteMostly, &rdev2->flags))
1275 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* now set the "removed" and "faulty" bits on any missing devices */
1278 for (i=0 ; i < mddev->raid_disks ; i++) {
1279 mdp_disk_t *d = &sb->disks[i];
1280 if (d->state == 0 && d->number == 0) {
1281 d->number = i;
1282 d->raid_disk = i;
1283 d->state = (1<<MD_DISK_REMOVED);
1284 d->state |= (1<<MD_DISK_FAULTY);
1285 failed++;
1286 }
1287 }
1288 sb->nr_disks = nr_disks;
1289 sb->active_disks = active;
1290 sb->working_disks = working;
1291 sb->failed_disks = failed;
1292 sb->spare_disks = spare;
1293
1294 sb->this_disk = sb->disks[rdev->desc_nr];
1295 sb->sb_csum = calc_sb_csum(sb);
1296}
1297
1298/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001299 * rdev_size_change for 0.90.0
1300 */
1301static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001302super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001303{
Andre Noll58c0fed2009-03-31 14:33:13 +11001304 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001305 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001306 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001307 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001308 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001309 if (!num_sectors || num_sectors > rdev->sb_start)
1310 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001311 /* Limit to 4TB as metadata cannot record more than that.
1312 * 4TB == 2^32 KB, or 2*2^32 sectors.
1313 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001314 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1315 rdev->mddev->level >= 1)
1316 num_sectors = (sector_t)(2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001317 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001318 rdev->sb_page);
1319 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001320 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001321}
1322
NeilBrownc6563a82012-05-21 09:27:00 +10001323static int
1324super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1325{
1326 /* non-zero offset changes not possible with v0.90 */
1327 return new_offset == 0;
1328}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001329
1330/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * version 1 superblock
1332 */
1333
NeilBrownf72ffdd2014-09-30 14:23:59 +10001334static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001336 __le32 disk_csum;
1337 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 unsigned long long newcsum;
1339 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001340 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 disk_csum = sb->sb_csum;
1343 sb->sb_csum = 0;
1344 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001345 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 newcsum += le32_to_cpu(*isuper++);
1347
1348 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001349 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1352 sb->sb_csum = disk_csum;
1353 return cpu_to_le32(csum);
1354}
1355
NeilBrown2699b672011-07-28 11:31:47 +10001356static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1357 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001358static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct mdp_superblock_1 *sb;
1361 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001362 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001363 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001365 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 /*
Andre Noll0f420352008-07-11 22:02:23 +10001368 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * It is always aligned to a 4K boundary and
1370 * depeding on minor_version, it can be:
1371 * 0: At least 8K, but less than 12K, from end of device
1372 * 1: At start of device
1373 * 2: 4K from start of device.
1374 */
1375 switch(minor_version) {
1376 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001377 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001378 sb_start -= 8*2;
1379 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 break;
1381 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001382 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 break;
1384 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001385 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 break;
1387 default:
1388 return -EINVAL;
1389 }
Andre Noll0f420352008-07-11 22:02:23 +10001390 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
NeilBrown0002b272005-09-09 16:23:53 -07001392 /* superblock is rarely larger than 1K, but it can be larger,
1393 * and it is safe to read 4k, so we do that
1394 */
1395 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (ret) return ret;
1397
Namhyung Kim65a06f062011-07-27 11:00:36 +10001398 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1401 sb->major_version != cpu_to_le32(1) ||
1402 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001403 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001404 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return -EINVAL;
1406
1407 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1408 printk("md: invalid superblock checksum on %s\n",
1409 bdevname(rdev->bdev,b));
1410 return -EINVAL;
1411 }
1412 if (le64_to_cpu(sb->data_size) < 10) {
1413 printk("md: data_size too small on %s\n",
1414 bdevname(rdev->bdev,b));
1415 return -EINVAL;
1416 }
NeilBrownc6563a82012-05-21 09:27:00 +10001417 if (sb->pad0 ||
1418 sb->pad3[0] ||
1419 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1420 /* Some padding is non-zero, might be a new feature */
1421 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 rdev->preferred_minor = 0xffff;
1424 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001425 rdev->new_data_offset = rdev->data_offset;
1426 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1427 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1428 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001429 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
NeilBrown0002b272005-09-09 16:23:53 -07001431 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001432 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001433 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001434 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1435
1436 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001437 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001438 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001439 if (minor_version
1440 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1441 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001442
NeilBrown31b65a02006-07-10 04:44:14 -07001443 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1444 rdev->desc_nr = -1;
1445 else
1446 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1447
NeilBrown2699b672011-07-28 11:31:47 +10001448 if (!rdev->bb_page) {
1449 rdev->bb_page = alloc_page(GFP_KERNEL);
1450 if (!rdev->bb_page)
1451 return -ENOMEM;
1452 }
1453 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1454 rdev->badblocks.count == 0) {
1455 /* need to load the bad block list.
1456 * Currently we limit it to one page.
1457 */
1458 s32 offset;
1459 sector_t bb_sector;
1460 u64 *bbp;
1461 int i;
1462 int sectors = le16_to_cpu(sb->bblog_size);
1463 if (sectors > (PAGE_SIZE / 512))
1464 return -EINVAL;
1465 offset = le32_to_cpu(sb->bblog_offset);
1466 if (offset == 0)
1467 return -EINVAL;
1468 bb_sector = (long long)offset;
1469 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1470 rdev->bb_page, READ, true))
1471 return -EIO;
1472 bbp = (u64 *)page_address(rdev->bb_page);
1473 rdev->badblocks.shift = sb->bblog_shift;
1474 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1475 u64 bb = le64_to_cpu(*bbp);
1476 int count = bb & (0x3ff);
1477 u64 sector = bb >> 10;
1478 sector <<= sb->bblog_shift;
1479 count <<= sb->bblog_shift;
1480 if (bb + 1 == 0)
1481 break;
1482 if (md_set_badblocks(&rdev->badblocks,
1483 sector, count, 1) == 0)
1484 return -EINVAL;
1485 }
NeilBrown486adf72013-04-24 11:42:44 +10001486 } else if (sb->bblog_offset != 0)
1487 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001488
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001489 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001490 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001491 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001493 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1496 sb->level != refsb->level ||
1497 sb->layout != refsb->layout ||
1498 sb->chunksize != refsb->chunksize) {
1499 printk(KERN_WARNING "md: %s has strangely different"
1500 " superblock to %s\n",
1501 bdevname(rdev->bdev,b),
1502 bdevname(refdev->bdev,b2));
1503 return -EINVAL;
1504 }
1505 ev1 = le64_to_cpu(sb->events);
1506 ev2 = le64_to_cpu(refsb->events);
1507
1508 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001509 ret = 1;
1510 else
1511 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
NeilBrownc6563a82012-05-21 09:27:00 +10001513 if (minor_version) {
1514 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1515 sectors -= rdev->data_offset;
1516 } else
1517 sectors = rdev->sb_start;
1518 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001520 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001521 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
NeilBrownfd01b882011-10-11 16:47:53 +11001524static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001526 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001527 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
NeilBrown41158c72005-06-21 17:17:25 -07001529 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001530 clear_bit(Faulty, &rdev->flags);
1531 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001532 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001533 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (mddev->raid_disks == 0) {
1536 mddev->major_version = 1;
1537 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001538 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001539 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001540 mddev->ctime = le64_to_cpu(sb->ctime);
1541 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001543 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 mddev->layout = le32_to_cpu(sb->layout);
1545 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001546 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001547 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001548 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001549 mddev->bitmap_info.space = 0;
1550 /* Default location for bitmap is 1K after superblock
1551 * using 3K - total of 4K
1552 */
NeilBrownc3d97142009-12-14 12:49:52 +11001553 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001554 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001555 mddev->reshape_backwards = 0;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1558 memcpy(mddev->uuid, sb->set_uuid, 16);
1559
1560 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001561
NeilBrown71c08052005-09-09 16:23:51 -07001562 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001563 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001564 mddev->bitmap_info.offset =
1565 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001566 /* Metadata doesn't record how much space is available.
1567 * For 1.0, we assume we can use up to the superblock
1568 * if before, else to 4K beyond superblock.
1569 * For others, assume no change is possible.
1570 */
1571 if (mddev->minor_version > 0)
1572 mddev->bitmap_info.space = 0;
1573 else if (mddev->bitmap_info.offset > 0)
1574 mddev->bitmap_info.space =
1575 8 - mddev->bitmap_info.offset;
1576 else
1577 mddev->bitmap_info.space =
1578 -mddev->bitmap_info.offset;
1579 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001580
NeilBrownf6705572006-03-27 01:18:11 -08001581 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1582 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1583 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1584 mddev->new_level = le32_to_cpu(sb->new_level);
1585 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001586 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001587 if (mddev->delta_disks < 0 ||
1588 (mddev->delta_disks == 0 &&
1589 (le32_to_cpu(sb->feature_map)
1590 & MD_FEATURE_RESHAPE_BACKWARDS)))
1591 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001592 } else {
1593 mddev->reshape_position = MaxSector;
1594 mddev->delta_disks = 0;
1595 mddev->new_level = mddev->level;
1596 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001597 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001598 }
1599
Shaohua Lia62ab492016-01-06 14:37:13 -08001600 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) {
1601 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1602 if (mddev->recovery_cp == MaxSector)
1603 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
1604 }
NeilBrown41158c72005-06-21 17:17:25 -07001605 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001606 /* Insist of good event counter while assembling, except for
1607 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001609 if (rdev->desc_nr >= 0 &&
1610 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001611 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1612 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001613 if (ev1 < mddev->events)
1614 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001615 } else if (mddev->bitmap) {
1616 /* If adding to array with a bitmap, then we can accept an
1617 * older device, but not too old.
1618 */
NeilBrown41158c72005-06-21 17:17:25 -07001619 if (ev1 < mddev->bitmap->events_cleared)
1620 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001621 if (ev1 < mddev->events)
1622 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001623 } else {
1624 if (ev1 < mddev->events)
1625 /* just a hot-add of a new device, leave raid_disk at -1 */
1626 return 0;
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (mddev->level != LEVEL_MULTIPATH) {
1629 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001630 if (rdev->desc_nr < 0 ||
1631 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001632 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001633 rdev->desc_nr = -1;
1634 } else
1635 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001637 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001639 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001640 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
Song Liubac624f2015-08-13 14:31:55 -07001642 case MD_DISK_ROLE_JOURNAL: /* journal device */
1643 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1644 /* journal device without journal feature */
1645 printk(KERN_WARNING
1646 "md: journal device provided without journal feature, ignoring the device\n");
1647 return -EINVAL;
1648 }
1649 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001650 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001651 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 default:
NeilBrownf4667222013-12-09 12:04:56 +11001654 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001655 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001656 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001657 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001658 if (!(le32_to_cpu(sb->feature_map) &
1659 MD_FEATURE_RECOVERY_BITMAP))
1660 rdev->saved_raid_disk = -1;
1661 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001662 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 rdev->raid_disk = role;
1664 break;
1665 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001666 if (sb->devflags & WriteMostly1)
1667 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001668 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1669 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001670 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001671 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return 0;
1674}
1675
NeilBrownfd01b882011-10-11 16:47:53 +11001676static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001679 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 int max_dev, i;
1681 /* make rdev->sb match mddev and rdev data. */
1682
Namhyung Kim65a06f062011-07-27 11:00:36 +10001683 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 sb->feature_map = 0;
1686 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001687 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 memset(sb->pad3, 0, sizeof(sb->pad3));
1689
1690 sb->utime = cpu_to_le64((__u64)mddev->utime);
1691 sb->events = cpu_to_le64(mddev->events);
1692 if (mddev->in_sync)
1693 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001694 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1695 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 else
1697 sb->resync_offset = cpu_to_le64(0);
1698
NeilBrown1c05b4b2006-10-21 10:24:08 -07001699 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001700
NeilBrownf0ca3402006-02-02 14:28:04 -08001701 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001702 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001703 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001704 sb->level = cpu_to_le32(mddev->level);
1705 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001706
NeilBrownaeb9b2112011-08-25 14:43:08 +10001707 if (test_bit(WriteMostly, &rdev->flags))
1708 sb->devflags |= WriteMostly1;
1709 else
1710 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001711 sb->data_offset = cpu_to_le64(rdev->data_offset);
1712 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001713
NeilBrownc3d97142009-12-14 12:49:52 +11001714 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1715 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001716 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001717 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001718
Shaohua Lif2076e72015-10-08 21:54:12 -07001719 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001720 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001721 sb->feature_map |=
1722 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1723 sb->recovery_offset =
1724 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001725 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1726 sb->feature_map |=
1727 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001728 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001729 /* Note: recovery_offset and journal_tail share space */
1730 if (test_bit(Journal, &rdev->flags))
1731 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001732 if (test_bit(Replacement, &rdev->flags))
1733 sb->feature_map |=
1734 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001735
NeilBrownf6705572006-03-27 01:18:11 -08001736 if (mddev->reshape_position != MaxSector) {
1737 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1738 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1739 sb->new_layout = cpu_to_le32(mddev->new_layout);
1740 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1741 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001742 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001743 if (mddev->delta_disks == 0 &&
1744 mddev->reshape_backwards)
1745 sb->feature_map
1746 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001747 if (rdev->new_data_offset != rdev->data_offset) {
1748 sb->feature_map
1749 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1750 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1751 - rdev->data_offset));
1752 }
NeilBrownf6705572006-03-27 01:18:11 -08001753 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001754
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001755 if (mddev_is_clustered(mddev))
1756 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1757
NeilBrown2699b672011-07-28 11:31:47 +10001758 if (rdev->badblocks.count == 0)
1759 /* Nothing to do for bad blocks*/ ;
1760 else if (sb->bblog_offset == 0)
1761 /* Cannot record bad blocks on this device */
1762 md_error(mddev, rdev);
1763 else {
1764 struct badblocks *bb = &rdev->badblocks;
1765 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1766 u64 *p = bb->page;
1767 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1768 if (bb->changed) {
1769 unsigned seq;
1770
1771retry:
1772 seq = read_seqbegin(&bb->lock);
1773
1774 memset(bbp, 0xff, PAGE_SIZE);
1775
1776 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001777 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001778 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1779 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001780 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001781 }
NeilBrownd0962932012-03-19 12:46:41 +11001782 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001783 if (read_seqretry(&bb->lock, seq))
1784 goto retry;
1785
1786 bb->sector = (rdev->sb_start +
1787 (int)le32_to_cpu(sb->bblog_offset));
1788 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001789 }
1790 }
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001793 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (rdev2->desc_nr+1 > max_dev)
1795 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001796
NeilBrown70471da2009-08-03 10:59:57 +10001797 if (max_dev > le32_to_cpu(sb->max_dev)) {
1798 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001799 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001800 rdev->sb_size = max_dev * 2 + 256;
1801 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1802 if (rdev->sb_size & bmask)
1803 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001804 } else
1805 max_dev = le32_to_cpu(sb->max_dev);
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001808 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001809
Song Liua97b7892015-10-08 21:54:09 -07001810 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1811 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
NeilBrowndafb20f2012-03-19 12:46:39 +11001813 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001815 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001816 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001817 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001819 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001820 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001821 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001822 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 else
Song Liuc4d4c912015-08-13 14:31:54 -07001824 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 sb->sb_csum = calc_sb_1_csum(sb);
1828}
1829
Chris Webb0cd17fe2008-06-28 08:31:46 +10001830static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001831super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001832{
1833 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001834 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001835 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001836 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001837 if (rdev->data_offset != rdev->new_data_offset)
1838 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001839 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001840 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001841 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001842 max_sectors -= rdev->data_offset;
1843 if (!num_sectors || num_sectors > max_sectors)
1844 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001845 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001846 /* minor version 0 with bitmap we can't move */
1847 return 0;
1848 } else {
1849 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001850 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001851 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001852 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001853 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001854 if (!num_sectors || num_sectors > max_sectors)
1855 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001856 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001857 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001858 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001859 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001860 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001861 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001862 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001863 rdev->sb_page);
1864 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001865 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001866
1867}
1868
1869static int
1870super_1_allow_new_offset(struct md_rdev *rdev,
1871 unsigned long long new_offset)
1872{
1873 /* All necessary checks on new >= old have been done */
1874 struct bitmap *bitmap;
1875 if (new_offset >= rdev->data_offset)
1876 return 1;
1877
1878 /* with 1.0 metadata, there is no metadata to tread on
1879 * so we can always move back */
1880 if (rdev->mddev->minor_version == 0)
1881 return 1;
1882
1883 /* otherwise we must be sure not to step on
1884 * any metadata, so stay:
1885 * 36K beyond start of superblock
1886 * beyond end of badblocks
1887 * beyond write-intent bitmap
1888 */
1889 if (rdev->sb_start + (32+4)*2 > new_offset)
1890 return 0;
1891 bitmap = rdev->mddev->bitmap;
1892 if (bitmap && !rdev->mddev->bitmap_info.file &&
1893 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001894 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001895 return 0;
1896 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1897 return 0;
1898
1899 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001900}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Adrian Bunk75c96f82005-05-05 16:16:09 -07001902static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 [0] = {
1904 .name = "0.90.0",
1905 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001906 .load_super = super_90_load,
1907 .validate_super = super_90_validate,
1908 .sync_super = super_90_sync,
1909 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001910 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 },
1912 [1] = {
1913 .name = "md-1",
1914 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001915 .load_super = super_1_load,
1916 .validate_super = super_1_validate,
1917 .sync_super = super_1_sync,
1918 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001919 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 },
1921};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
NeilBrownfd01b882011-10-11 16:47:53 +11001923static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001924{
1925 if (mddev->sync_super) {
1926 mddev->sync_super(mddev, rdev);
1927 return;
1928 }
1929
1930 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1931
1932 super_types[mddev->major_version].sync_super(mddev, rdev);
1933}
1934
NeilBrownfd01b882011-10-11 16:47:53 +11001935static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
NeilBrown3cb03002011-10-11 16:45:26 +11001937 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
NeilBrown4b809912008-07-21 17:05:25 +10001939 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001940 rdev_for_each_rcu(rdev, mddev1) {
1941 if (test_bit(Faulty, &rdev->flags) ||
1942 test_bit(Journal, &rdev->flags) ||
1943 rdev->raid_disk == -1)
1944 continue;
1945 rdev_for_each_rcu(rdev2, mddev2) {
1946 if (test_bit(Faulty, &rdev2->flags) ||
1947 test_bit(Journal, &rdev2->flags) ||
1948 rdev2->raid_disk == -1)
1949 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001950 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001951 rdev2->bdev->bd_contains) {
1952 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001953 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001954 }
Song Liu0b020e82015-09-03 23:00:35 -07001955 }
1956 }
NeilBrown4b809912008-07-21 17:05:25 +10001957 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return 0;
1959}
1960
1961static LIST_HEAD(pending_raid_disks);
1962
Andre Nollac5e7112009-08-03 10:59:47 +10001963/*
1964 * Try to register data integrity profile for an mddev
1965 *
1966 * This is called when an array is started and after a disk has been kicked
1967 * from the array. It only succeeds if all working and active component devices
1968 * are integrity capable with matching profiles.
1969 */
NeilBrownfd01b882011-10-11 16:47:53 +11001970int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001971{
NeilBrown3cb03002011-10-11 16:45:26 +11001972 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001973
Andre Nollac5e7112009-08-03 10:59:47 +10001974 if (list_empty(&mddev->disks))
1975 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001976 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1977 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001978 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001979 /* skip spares and non-functional disks */
1980 if (test_bit(Faulty, &rdev->flags))
1981 continue;
1982 if (rdev->raid_disk < 0)
1983 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001984 if (!reference) {
1985 /* Use the first rdev as the reference */
1986 reference = rdev;
1987 continue;
1988 }
1989 /* does this rdev's profile match the reference profile? */
1990 if (blk_integrity_compare(reference->bdev->bd_disk,
1991 rdev->bdev->bd_disk) < 0)
1992 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001993 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001994 if (!reference || !bdev_get_integrity(reference->bdev))
1995 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001996 /*
1997 * All component devices are integrity capable and have matching
1998 * profiles, register the common profile for the md device.
1999 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002000 blk_integrity_register(mddev->gendisk,
2001 bdev_get_integrity(reference->bdev));
2002
Martin K. Petersena91a2782011-03-17 11:11:05 +01002003 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2004 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2005 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2006 mdname(mddev));
2007 return -EINVAL;
2008 }
Andre Nollac5e7112009-08-03 10:59:47 +10002009 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002010}
Andre Nollac5e7112009-08-03 10:59:47 +10002011EXPORT_SYMBOL(md_integrity_register);
2012
2013/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002014void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002015{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002016 struct blk_integrity *bi_rdev;
2017 struct blk_integrity *bi_mddev;
2018
2019 if (!mddev->gendisk)
2020 return;
2021
2022 bi_rdev = bdev_get_integrity(rdev->bdev);
2023 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002024
2025 if (!bi_mddev) /* nothing to do */
2026 return;
2027 if (rdev->raid_disk < 0) /* skip spares */
2028 return;
2029 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2030 rdev->bdev->bd_disk) >= 0)
2031 return;
Dan Williamsc7bfced2015-10-21 13:20:02 -04002032 WARN_ON_ONCE(!mddev->suspended);
Andre Nollac5e7112009-08-03 10:59:47 +10002033 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2034 blk_integrity_unregister(mddev->gendisk);
2035}
2036EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002037
NeilBrownf72ffdd2014-09-30 14:23:59 +10002038static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002040 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002041 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002042 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Dan Williams11e2ede2008-04-30 00:52:32 -07002044 /* prevent duplicates */
2045 if (find_rdev(mddev, rdev->bdev->bd_dev))
2046 return -EEXIST;
2047
Andre Nolldd8ac332009-03-31 14:33:13 +11002048 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002049 if (!test_bit(Journal, &rdev->flags) &&
2050 rdev->sectors &&
2051 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002052 if (mddev->pers) {
2053 /* Cannot change size, so fail
2054 * If mddev->level <= 0, then we don't care
2055 * about aligning sizes (e.g. linear)
2056 */
2057 if (mddev->level > 0)
2058 return -ENOSPC;
2059 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002060 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 /* Verify rdev->desc_nr is unique.
2064 * If it is -1, assign a free number, else
2065 * check number is not in use
2066 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002067 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (rdev->desc_nr < 0) {
2069 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002070 if (mddev->pers)
2071 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002072 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 choice++;
2074 rdev->desc_nr = choice;
2075 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002076 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002077 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002081 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002082 if (!test_bit(Journal, &rdev->flags) &&
2083 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002084 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2085 mdname(mddev), mddev->max_disks);
2086 return -EBUSY;
2087 }
NeilBrown19133a42005-11-08 21:39:35 -08002088 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002089 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002092 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002093
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002094 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002095 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002096
Tejun Heo0762b8b2008-08-25 19:56:12 +09002097 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002098 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2099 /* failure here is OK */;
2100 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002101
NeilBrown4b809912008-07-21 17:05:25 +10002102 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002103 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002104
2105 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002106 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002109
2110 fail:
2111 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2112 b, mdname(mddev));
2113 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
NeilBrown177a99b2008-02-06 01:39:56 -08002116static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002117{
NeilBrown3cb03002011-10-11 16:45:26 +11002118 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002119 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002120 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002121}
2122
NeilBrownf72ffdd2014-09-30 14:23:59 +10002123static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002126
Tejun Heo49731ba2011-01-14 18:43:57 +01002127 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002128 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2130 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002131 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002132 sysfs_put(rdev->sysfs_state);
2133 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002134 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002135 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002136 * writing to 'remove' to "dev/state". We also need
2137 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002138 */
NeilBrown4b809912008-07-21 17:05:25 +10002139 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002140 INIT_WORK(&rdev->del_work, md_delayed_delete);
2141 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002142 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145/*
2146 * prevent the device from being mounted, repartitioned or
2147 * otherwise reused by a RAID array (or any other kernel
2148 * subsystem), by bd_claiming the device.
2149 */
NeilBrown3cb03002011-10-11 16:45:26 +11002150static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 int err = 0;
2153 struct block_device *bdev;
2154 char b[BDEVNAME_SIZE];
2155
Tejun Heod4d77622010-11-13 11:55:18 +01002156 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002157 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (IS_ERR(bdev)) {
2159 printk(KERN_ERR "md: could not open %s.\n",
2160 __bdevname(dev, b));
2161 return PTR_ERR(bdev);
2162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 rdev->bdev = bdev;
2164 return err;
2165}
2166
NeilBrown3cb03002011-10-11 16:45:26 +11002167static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 struct block_device *bdev = rdev->bdev;
2170 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002171 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
2174void md_autodetect_dev(dev_t dev);
2175
NeilBrownf72ffdd2014-09-30 14:23:59 +10002176static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 printk(KERN_INFO "md: export_rdev(%s)\n",
2181 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002182 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002184 if (test_bit(AutoDetected, &rdev->flags))
2185 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186#endif
2187 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002188 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002191void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 unbind_rdev_from_array(rdev);
2194 export_rdev(rdev);
2195}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002196EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
NeilBrownfd01b882011-10-11 16:47:53 +11002198static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
NeilBrown0638bb02014-09-25 17:43:47 +10002200 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
NeilBrown0638bb02014-09-25 17:43:47 +10002202 while (!list_empty(&mddev->disks)) {
2203 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2204 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002205 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 mddev->raid_disks = 0;
2208 mddev->major_version = 0;
2209}
2210
NeilBrownf72ffdd2014-09-30 14:23:59 +10002211static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
NeilBrown42543762006-06-26 00:27:57 -07002213 /* Update each superblock (in-memory image), but
2214 * if we are allowed to, skip spares which already
2215 * have the right event counter, or have one earlier
2216 * (which would mean they aren't being marked as dirty
2217 * with the rest of the array)
2218 */
NeilBrown3cb03002011-10-11 16:45:26 +11002219 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002220 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002221 if (rdev->sb_events == mddev->events ||
2222 (nospares &&
2223 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002224 rdev->sb_events+1 == mddev->events)) {
2225 /* Don't update this superblock */
2226 rdev->sb_loaded = 2;
2227 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002228 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002229 rdev->sb_loaded = 1;
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232}
2233
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002234static bool does_sb_need_changing(struct mddev *mddev)
2235{
2236 struct md_rdev *rdev;
2237 struct mdp_superblock_1 *sb;
2238 int role;
2239
2240 /* Find a good rdev */
2241 rdev_for_each(rdev, mddev)
2242 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2243 break;
2244
2245 /* No good device found. */
2246 if (!rdev)
2247 return false;
2248
2249 sb = page_address(rdev->sb_page);
2250 /* Check if a device has become faulty or a spare become active */
2251 rdev_for_each(rdev, mddev) {
2252 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2253 /* Device activated? */
2254 if (role == 0xffff && rdev->raid_disk >=0 &&
2255 !test_bit(Faulty, &rdev->flags))
2256 return true;
2257 /* Device turned faulty? */
2258 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2259 return true;
2260 }
2261
2262 /* Check if any mddev parameters have changed */
2263 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2264 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002265 (mddev->layout != le64_to_cpu(sb->layout)) ||
2266 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2267 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2268 return true;
2269
2270 return false;
2271}
2272
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002273void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
NeilBrown3cb03002011-10-11 16:45:26 +11002275 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002276 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002277 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002278 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002279 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
NeilBrownd87f0642013-04-24 11:42:40 +10002281 if (mddev->ro) {
2282 if (force_change)
2283 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2284 return;
2285 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002286
2287 if (mddev_is_clustered(mddev)) {
2288 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2289 force_change = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002290 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002291 /* Has someone else has updated the sb */
2292 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002293 if (ret == 0)
2294 md_cluster_ops->metadata_update_cancel(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002295 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2296 return;
2297 }
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002300 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002301 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002302 if (rdev->raid_disk >= 0 &&
2303 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002304 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002305 !test_bit(In_sync, &rdev->flags) &&
2306 mddev->curr_resync_completed > rdev->recovery_offset)
2307 rdev->recovery_offset = mddev->curr_resync_completed;
2308
NeilBrownf72ffdd2014-09-30 14:23:59 +10002309 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002310 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002311 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002312 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002313 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002314 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002315 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002316 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002317 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002318 md_ack_all_badblocks(&rdev->badblocks);
2319 md_error(mddev, rdev);
2320 }
2321 clear_bit(Blocked, &rdev->flags);
2322 clear_bit(BlockedBadBlocks, &rdev->flags);
2323 wake_up(&rdev->blocked_wait);
2324 }
2325 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002326 wake_up(&mddev->sb_wait);
2327 return;
2328 }
2329
NeilBrown85572d72014-12-15 12:56:56 +11002330 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002331
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002332 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002333
NeilBrown850b2b422006-10-03 01:15:46 -07002334 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2335 force_change = 1;
2336 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2337 /* just a clean<-> dirty transition, possibly leave spares alone,
2338 * though if events isn't the right even/odd, we will have to do
2339 * spares after all
2340 */
2341 nospares = 1;
2342 if (force_change)
2343 nospares = 0;
2344 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002345 /* If the array is degraded, then skipping spares is both
2346 * dangerous and fairly pointless.
2347 * Dangerous because a device that was removed from the array
2348 * might have a event_count that still looks up-to-date,
2349 * so it can be re-added without a resync.
2350 * Pointless because if there are any spares to skip,
2351 * then a recovery will happen and soon that array won't
2352 * be degraded any more and the spare can go back to sleep then.
2353 */
NeilBrown850b2b422006-10-03 01:15:46 -07002354 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002355
NeilBrown06d91a52005-06-21 17:17:12 -07002356 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002357
2358 /* If this is just a dirty<->clean transition, and the array is clean
2359 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002360 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002361 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002362 && mddev->can_decrease_events
2363 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002364 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002365 mddev->can_decrease_events = 0;
2366 } else {
NeilBrown42543762006-06-26 00:27:57 -07002367 /* otherwise we have to go forward and ... */
2368 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002369 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
NeilBrown403df472014-09-30 15:52:29 +10002372 /*
2373 * This 64-bit counter should never wrap.
2374 * Either we are in around ~1 trillion A.C., assuming
2375 * 1 reboot per second, or we have a bug...
2376 */
2377 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002378
NeilBrowndafb20f2012-03-19 12:46:39 +11002379 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002380 if (rdev->badblocks.changed)
2381 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002382 if (test_bit(Faulty, &rdev->flags))
2383 set_bit(FaultRecorded, &rdev->flags);
2384 }
NeilBrown2699b672011-07-28 11:31:47 +10002385
NeilBrowne6910632008-02-06 01:39:51 -08002386 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002387 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
NeilBrown36a4e1f2011-10-07 14:23:17 +11002389 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2390 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
NeilBrown4ad13662007-07-17 04:06:13 -07002392 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002393 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002395
NeilBrown42543762006-06-26 00:27:57 -07002396 if (rdev->sb_loaded != 1)
2397 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
NeilBrownf4667222013-12-09 12:04:56 +11002399 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002400 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002401 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002402 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002403 pr_debug("md: (write) %s's sb offset: %llu\n",
2404 bdevname(rdev->bdev, b),
2405 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002406 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002407 if (rdev->badblocks.size) {
2408 md_super_write(mddev, rdev,
2409 rdev->badblocks.sector,
2410 rdev->badblocks.size << 9,
2411 rdev->bb_page);
2412 rdev->badblocks.size = 0;
2413 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002414
NeilBrownf4667222013-12-09 12:04:56 +11002415 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002416 pr_debug("md: %s (skipping faulty)\n",
2417 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002418
NeilBrown7bfa19f2005-06-21 17:17:28 -07002419 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 /* only need to write one superblock... */
2421 break;
2422 }
NeilBrowna9701a32005-11-08 21:39:34 -08002423 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002424 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002425
NeilBrown85572d72014-12-15 12:56:56 +11002426 spin_lock(&mddev->lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002427 if (mddev->in_sync != sync_req ||
2428 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002429 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002430 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002431 goto repeat;
2432 }
NeilBrown850b2b422006-10-03 01:15:46 -07002433 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002434 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002435 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002436 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2437 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002438
NeilBrowndafb20f2012-03-19 12:46:39 +11002439 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002440 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2441 clear_bit(Blocked, &rdev->flags);
2442
2443 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002444 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002445 clear_bit(BlockedBadBlocks, &rdev->flags);
2446 wake_up(&rdev->blocked_wait);
2447 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002448
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002449 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002450 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002452EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002454static int add_bound_rdev(struct md_rdev *rdev)
2455{
2456 struct mddev *mddev = rdev->mddev;
2457 int err = 0;
2458
2459 if (!mddev->pers->hot_remove_disk) {
2460 /* If there is hot_add_disk but no hot_remove_disk
2461 * then added disks for geometry changes,
2462 * and should be added immediately.
2463 */
2464 super_types[mddev->major_version].
2465 validate_super(mddev, rdev);
2466 err = mddev->pers->hot_add_disk(mddev, rdev);
2467 if (err) {
2468 unbind_rdev_from_array(rdev);
2469 export_rdev(rdev);
2470 return err;
2471 }
2472 }
2473 sysfs_notify_dirent_safe(rdev->sysfs_state);
2474
2475 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2476 if (mddev->degraded)
2477 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2478 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2479 md_new_event(mddev);
2480 md_wakeup_thread(mddev->thread);
2481 return 0;
2482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Andre Noll7f6ce762008-03-23 18:34:54 +01002484/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002485 * We want to accept with case. For this we use cmd_match.
2486 */
2487static int cmd_match(const char *cmd, const char *str)
2488{
2489 /* See if cmd, written into a sysfs file, matches
2490 * str. They must either be the same, or cmd can
2491 * have a trailing newline
2492 */
2493 while (*cmd && *str && *cmd == *str) {
2494 cmd++;
2495 str++;
2496 }
2497 if (*cmd == '\n')
2498 cmd++;
2499 if (*str || *cmd)
2500 return 0;
2501 return 1;
2502}
2503
NeilBrown86e6ffd2005-11-08 21:39:24 -08002504struct rdev_sysfs_entry {
2505 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002506 ssize_t (*show)(struct md_rdev *, char *);
2507 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002508};
2509
2510static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002511state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002512{
2513 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002514 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002515 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002516
NeilBrown758bfc82014-12-15 12:56:59 +11002517 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002518 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002519 len+= sprintf(page+len, "%sfaulty",sep);
2520 sep = ",";
2521 }
NeilBrown758bfc82014-12-15 12:56:59 +11002522 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002523 len += sprintf(page+len, "%sin_sync",sep);
2524 sep = ",";
2525 }
Shaohua Liac6096e2015-10-04 09:20:11 -07002526 if (test_bit(Journal, &flags)) {
2527 len += sprintf(page+len, "%sjournal",sep);
2528 sep = ",";
2529 }
NeilBrown758bfc82014-12-15 12:56:59 +11002530 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002531 len += sprintf(page+len, "%swrite_mostly",sep);
2532 sep = ",";
2533 }
NeilBrown758bfc82014-12-15 12:56:59 +11002534 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002535 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002536 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002537 len += sprintf(page+len, "%sblocked", sep);
2538 sep = ",";
2539 }
NeilBrown758bfc82014-12-15 12:56:59 +11002540 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002541 !test_bit(Journal, &flags) &&
NeilBrown758bfc82014-12-15 12:56:59 +11002542 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002543 len += sprintf(page+len, "%sspare", sep);
2544 sep = ",";
2545 }
NeilBrown758bfc82014-12-15 12:56:59 +11002546 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002547 len += sprintf(page+len, "%swrite_error", sep);
2548 sep = ",";
2549 }
NeilBrown758bfc82014-12-15 12:56:59 +11002550 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002551 len += sprintf(page+len, "%swant_replacement", sep);
2552 sep = ",";
2553 }
NeilBrown758bfc82014-12-15 12:56:59 +11002554 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002555 len += sprintf(page+len, "%sreplacement", sep);
2556 sep = ",";
2557 }
2558
NeilBrown86e6ffd2005-11-08 21:39:24 -08002559 return len+sprintf(page+len, "\n");
2560}
2561
NeilBrown45dc2de2006-06-26 00:27:58 -07002562static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002563state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002564{
2565 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002566 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002567 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002568 * writemostly - sets write_mostly
2569 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002570 * blocked - sets the Blocked flags
2571 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002572 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002573 * -insync - clear Insync for a device with a slot assigned,
2574 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002575 * write_error - sets WriteErrorSeen
2576 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002577 */
2578 int err = -EINVAL;
2579 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2580 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002581 if (test_bit(Faulty, &rdev->flags))
2582 err = 0;
2583 else
2584 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002585 } else if (cmd_match(buf, "remove")) {
2586 if (rdev->raid_disk >= 0)
2587 err = -EBUSY;
2588 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002589 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002590 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002591 if (mddev_is_clustered(mddev))
2592 err = md_cluster_ops->remove_disk(mddev, rdev);
2593
2594 if (err == 0) {
2595 md_kick_rdev_from_array(rdev);
2596 if (mddev->pers)
2597 md_update_sb(mddev, 1);
2598 md_new_event(mddev);
2599 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002600 }
NeilBrownf6556752006-06-26 00:28:01 -07002601 } else if (cmd_match(buf, "writemostly")) {
2602 set_bit(WriteMostly, &rdev->flags);
2603 err = 0;
2604 } else if (cmd_match(buf, "-writemostly")) {
2605 clear_bit(WriteMostly, &rdev->flags);
2606 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002607 } else if (cmd_match(buf, "blocked")) {
2608 set_bit(Blocked, &rdev->flags);
2609 err = 0;
2610 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002611 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002612 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002613 /* metadata handler doesn't understand badblocks,
2614 * so we need to fail the device
2615 */
2616 md_error(rdev->mddev, rdev);
2617 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002618 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002619 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002620 wake_up(&rdev->blocked_wait);
2621 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2622 md_wakeup_thread(rdev->mddev->thread);
2623
2624 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002625 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2626 set_bit(In_sync, &rdev->flags);
2627 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002628 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2629 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002630 if (rdev->mddev->pers == NULL) {
2631 clear_bit(In_sync, &rdev->flags);
2632 rdev->saved_raid_disk = rdev->raid_disk;
2633 rdev->raid_disk = -1;
2634 err = 0;
2635 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002636 } else if (cmd_match(buf, "write_error")) {
2637 set_bit(WriteErrorSeen, &rdev->flags);
2638 err = 0;
2639 } else if (cmd_match(buf, "-write_error")) {
2640 clear_bit(WriteErrorSeen, &rdev->flags);
2641 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002642 } else if (cmd_match(buf, "want_replacement")) {
2643 /* Any non-spare device that is not a replacement can
2644 * become want_replacement at any time, but we then need to
2645 * check if recovery is needed.
2646 */
2647 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002648 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002649 !test_bit(Replacement, &rdev->flags))
2650 set_bit(WantReplacement, &rdev->flags);
2651 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2652 md_wakeup_thread(rdev->mddev->thread);
2653 err = 0;
2654 } else if (cmd_match(buf, "-want_replacement")) {
2655 /* Clearing 'want_replacement' is always allowed.
2656 * Once replacements starts it is too late though.
2657 */
2658 err = 0;
2659 clear_bit(WantReplacement, &rdev->flags);
2660 } else if (cmd_match(buf, "replacement")) {
2661 /* Can only set a device as a replacement when array has not
2662 * yet been started. Once running, replacement is automatic
2663 * from spares, or by assigning 'slot'.
2664 */
2665 if (rdev->mddev->pers)
2666 err = -EBUSY;
2667 else {
2668 set_bit(Replacement, &rdev->flags);
2669 err = 0;
2670 }
2671 } else if (cmd_match(buf, "-replacement")) {
2672 /* Similarly, can only clear Replacement before start */
2673 if (rdev->mddev->pers)
2674 err = -EBUSY;
2675 else {
2676 clear_bit(Replacement, &rdev->flags);
2677 err = 0;
2678 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002679 } else if (cmd_match(buf, "re-add")) {
2680 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002681 /* clear_bit is performed _after_ all the devices
2682 * have their local Faulty bit cleared. If any writes
2683 * happen in the meantime in the local node, they
2684 * will land in the local bitmap, which will be synced
2685 * by this node eventually
2686 */
2687 if (!mddev_is_clustered(rdev->mddev) ||
2688 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2689 clear_bit(Faulty, &rdev->flags);
2690 err = add_bound_rdev(rdev);
2691 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002692 } else
2693 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002694 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002695 if (!err)
2696 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002697 return err ? err : len;
2698}
NeilBrown80ca3a42006-07-10 04:44:18 -07002699static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002700__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002701
2702static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002703errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002704{
2705 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2706}
2707
2708static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002709errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002710{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002711 unsigned int n;
2712 int rv;
2713
2714 rv = kstrtouint(buf, 10, &n);
2715 if (rv < 0)
2716 return rv;
2717 atomic_set(&rdev->corrected_errors, n);
2718 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002719}
2720static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002721__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002722
NeilBrown014236d2006-01-06 00:20:55 -08002723static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002724slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002725{
Shaohua Lif2076e72015-10-08 21:54:12 -07002726 if (test_bit(Journal, &rdev->flags))
2727 return sprintf(page, "journal\n");
2728 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002729 return sprintf(page, "none\n");
2730 else
2731 return sprintf(page, "%d\n", rdev->raid_disk);
2732}
2733
2734static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002735slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002736{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002737 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002738 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002739
Shaohua Lif2076e72015-10-08 21:54:12 -07002740 if (test_bit(Journal, &rdev->flags))
2741 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002742 if (strncmp(buf, "none", 4)==0)
2743 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002744 else {
2745 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2746 if (err < 0)
2747 return err;
2748 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002749 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002750 /* Setting 'slot' on an active array requires also
2751 * updating the 'rd%d' link, and communicating
2752 * with the personality with ->hot_*_disk.
2753 * For now we only support removing
2754 * failed/spare devices. This normally happens automatically,
2755 * but not when the metadata is externally managed.
2756 */
NeilBrownc303da62008-02-06 01:39:51 -08002757 if (rdev->raid_disk == -1)
2758 return -EEXIST;
2759 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002760 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002761 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002762 clear_bit(Blocked, &rdev->flags);
2763 remove_and_add_spares(rdev->mddev, rdev);
2764 if (rdev->raid_disk >= 0)
2765 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002766 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2767 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002768 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002769 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002770 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002771 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002772 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002773
2774 if (rdev->raid_disk != -1)
2775 return -EBUSY;
2776
NeilBrownc6751b22011-02-02 11:57:13 +11002777 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2778 return -EBUSY;
2779
Neil Brown6c2fce22008-06-28 08:31:31 +10002780 if (rdev->mddev->pers->hot_add_disk == NULL)
2781 return -EINVAL;
2782
NeilBrownba1b41b2011-01-14 09:14:34 +11002783 if (slot >= rdev->mddev->raid_disks &&
2784 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2785 return -ENOSPC;
2786
Neil Brown6c2fce22008-06-28 08:31:31 +10002787 rdev->raid_disk = slot;
2788 if (test_bit(In_sync, &rdev->flags))
2789 rdev->saved_raid_disk = slot;
2790 else
2791 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002792 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002793 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002794 err = rdev->mddev->pers->
2795 hot_add_disk(rdev->mddev, rdev);
2796 if (err) {
2797 rdev->raid_disk = -1;
2798 return err;
2799 } else
2800 sysfs_notify_dirent_safe(rdev->sysfs_state);
2801 if (sysfs_link_rdev(rdev->mddev, rdev))
2802 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002803 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002804 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002805 if (slot >= rdev->mddev->raid_disks &&
2806 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002807 return -ENOSPC;
2808 rdev->raid_disk = slot;
2809 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002810 clear_bit(Faulty, &rdev->flags);
2811 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002812 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002813 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002814 }
NeilBrown014236d2006-01-06 00:20:55 -08002815 return len;
2816}
2817
NeilBrown014236d2006-01-06 00:20:55 -08002818static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002819__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002820
NeilBrown93c8cad2006-01-06 00:20:56 -08002821static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002822offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002823{
Andrew Morton6961ece2006-01-06 00:20:59 -08002824 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002825}
2826
2827static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002828offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002829{
NeilBrownc6563a82012-05-21 09:27:00 +10002830 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002831 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002832 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002833 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002834 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002835 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002836 /* Must set offset before size, so overlap checks
2837 * can be sane */
2838 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002839 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002840 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002841 return len;
2842}
2843
2844static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002845__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002846
NeilBrownc6563a82012-05-21 09:27:00 +10002847static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2848{
2849 return sprintf(page, "%llu\n",
2850 (unsigned long long)rdev->new_data_offset);
2851}
2852
2853static ssize_t new_offset_store(struct md_rdev *rdev,
2854 const char *buf, size_t len)
2855{
2856 unsigned long long new_offset;
2857 struct mddev *mddev = rdev->mddev;
2858
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002859 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002860 return -EINVAL;
2861
NeilBrownf851b602014-12-11 10:02:10 +11002862 if (mddev->sync_thread ||
2863 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002864 return -EBUSY;
2865 if (new_offset == rdev->data_offset)
2866 /* reset is always permitted */
2867 ;
2868 else if (new_offset > rdev->data_offset) {
2869 /* must not push array size beyond rdev_sectors */
2870 if (new_offset - rdev->data_offset
2871 + mddev->dev_sectors > rdev->sectors)
2872 return -E2BIG;
2873 }
2874 /* Metadata worries about other space details. */
2875
2876 /* decreasing the offset is inconsistent with a backwards
2877 * reshape.
2878 */
2879 if (new_offset < rdev->data_offset &&
2880 mddev->reshape_backwards)
2881 return -EINVAL;
2882 /* Increasing offset is inconsistent with forwards
2883 * reshape. reshape_direction should be set to
2884 * 'backwards' first.
2885 */
2886 if (new_offset > rdev->data_offset &&
2887 !mddev->reshape_backwards)
2888 return -EINVAL;
2889
2890 if (mddev->pers && mddev->persistent &&
2891 !super_types[mddev->major_version]
2892 .allow_new_offset(rdev, new_offset))
2893 return -E2BIG;
2894 rdev->new_data_offset = new_offset;
2895 if (new_offset > rdev->data_offset)
2896 mddev->reshape_backwards = 1;
2897 else if (new_offset < rdev->data_offset)
2898 mddev->reshape_backwards = 0;
2899
2900 return len;
2901}
2902static struct rdev_sysfs_entry rdev_new_offset =
2903__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2904
NeilBrown83303b62006-01-06 00:21:06 -08002905static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002906rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002907{
Andre Nolldd8ac332009-03-31 14:33:13 +11002908 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002909}
2910
NeilBrownc5d79ad2008-02-06 01:39:54 -08002911static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2912{
2913 /* check if two start/length pairs overlap */
2914 if (s1+l1 <= s2)
2915 return 0;
2916 if (s2+l2 <= s1)
2917 return 0;
2918 return 1;
2919}
2920
Dan Williamsb522adc2009-03-31 15:00:31 +11002921static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2922{
2923 unsigned long long blocks;
2924 sector_t new;
2925
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002926 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002927 return -EINVAL;
2928
2929 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2930 return -EINVAL; /* sector conversion overflow */
2931
2932 new = blocks * 2;
2933 if (new != blocks * 2)
2934 return -EINVAL; /* unsigned long long to sector_t overflow */
2935
2936 *sectors = new;
2937 return 0;
2938}
2939
NeilBrown83303b62006-01-06 00:21:06 -08002940static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002941rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002942{
NeilBrownfd01b882011-10-11 16:47:53 +11002943 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002944 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002945 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002946
Shaohua Lif2076e72015-10-08 21:54:12 -07002947 if (test_bit(Journal, &rdev->flags))
2948 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002949 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002950 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002951 if (rdev->data_offset != rdev->new_data_offset)
2952 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002953 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002954 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002955 sectors = super_types[my_mddev->major_version].
2956 rdev_size_change(rdev, sectors);
2957 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002958 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002959 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002960 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002961 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002962 if (!my_mddev->pers->resize)
2963 /* Cannot change size for RAID0 or Linear etc */
2964 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002965 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002966 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002967 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002968
Andre Nolldd8ac332009-03-31 14:33:13 +11002969 rdev->sectors = sectors;
2970 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002971 /* Need to check that all other rdevs with the same
2972 * ->bdev do not overlap. 'rcu' is sufficient to walk
2973 * the rdev lists safely.
2974 * This check does not provide a hard guarantee, it
2975 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002976 */
NeilBrownfd01b882011-10-11 16:47:53 +11002977 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002978 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002979 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002980
NeilBrown8b1afc32014-09-29 15:33:20 +10002981 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002982 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002983 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002984
NeilBrowndafb20f2012-03-19 12:46:39 +11002985 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002986 if (rdev->bdev == rdev2->bdev &&
2987 rdev != rdev2 &&
2988 overlaps(rdev->data_offset, rdev->sectors,
2989 rdev2->data_offset,
2990 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002991 overlap = 1;
2992 break;
2993 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002994 if (overlap) {
2995 mddev_put(mddev);
2996 break;
2997 }
2998 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002999 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003000 if (overlap) {
3001 /* Someone else could have slipped in a size
3002 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003003 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003004 * safe, and trust userspace not to race with
3005 * itself
3006 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003007 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003008 return -EBUSY;
3009 }
3010 }
NeilBrown83303b62006-01-06 00:21:06 -08003011 return len;
3012}
3013
3014static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003015__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003016
NeilBrown3cb03002011-10-11 16:45:26 +11003017static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003018{
3019 unsigned long long recovery_start = rdev->recovery_offset;
3020
3021 if (test_bit(In_sync, &rdev->flags) ||
3022 recovery_start == MaxSector)
3023 return sprintf(page, "none\n");
3024
3025 return sprintf(page, "%llu\n", recovery_start);
3026}
3027
NeilBrown3cb03002011-10-11 16:45:26 +11003028static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003029{
3030 unsigned long long recovery_start;
3031
3032 if (cmd_match(buf, "none"))
3033 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003034 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003035 return -EINVAL;
3036
3037 if (rdev->mddev->pers &&
3038 rdev->raid_disk >= 0)
3039 return -EBUSY;
3040
3041 rdev->recovery_offset = recovery_start;
3042 if (recovery_start == MaxSector)
3043 set_bit(In_sync, &rdev->flags);
3044 else
3045 clear_bit(In_sync, &rdev->flags);
3046 return len;
3047}
3048
3049static struct rdev_sysfs_entry rdev_recovery_start =
3050__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3051
NeilBrown16c791a2011-07-28 11:31:47 +10003052static ssize_t
3053badblocks_show(struct badblocks *bb, char *page, int unack);
3054static ssize_t
3055badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3056
NeilBrown3cb03002011-10-11 16:45:26 +11003057static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003058{
3059 return badblocks_show(&rdev->badblocks, page, 0);
3060}
NeilBrown3cb03002011-10-11 16:45:26 +11003061static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003062{
NeilBrownde393cd2011-07-28 11:31:48 +10003063 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3064 /* Maybe that ack was all we needed */
3065 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3066 wake_up(&rdev->blocked_wait);
3067 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003068}
3069static struct rdev_sysfs_entry rdev_bad_blocks =
3070__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3071
NeilBrown3cb03002011-10-11 16:45:26 +11003072static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003073{
3074 return badblocks_show(&rdev->badblocks, page, 1);
3075}
NeilBrown3cb03002011-10-11 16:45:26 +11003076static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003077{
3078 return badblocks_store(&rdev->badblocks, page, len, 1);
3079}
3080static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3081__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3082
NeilBrown86e6ffd2005-11-08 21:39:24 -08003083static struct attribute *rdev_default_attrs[] = {
3084 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003085 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003086 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003087 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003088 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003089 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003090 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003091 &rdev_bad_blocks.attr,
3092 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003093 NULL,
3094};
3095static ssize_t
3096rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3097{
3098 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003099 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003100
3101 if (!entry->show)
3102 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003103 if (!rdev->mddev)
3104 return -EBUSY;
3105 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003106}
3107
3108static ssize_t
3109rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3110 const char *page, size_t length)
3111{
3112 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003113 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003114 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003115 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003116
3117 if (!entry->store)
3118 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003119 if (!capable(CAP_SYS_ADMIN))
3120 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003121 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003122 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003123 if (rdev->mddev == NULL)
3124 rv = -EBUSY;
3125 else
3126 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003127 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003128 }
3129 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003130}
3131
3132static void rdev_free(struct kobject *ko)
3133{
NeilBrown3cb03002011-10-11 16:45:26 +11003134 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003135 kfree(rdev);
3136}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003137static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003138 .show = rdev_attr_show,
3139 .store = rdev_attr_store,
3140};
3141static struct kobj_type rdev_ktype = {
3142 .release = rdev_free,
3143 .sysfs_ops = &rdev_sysfs_ops,
3144 .default_attrs = rdev_default_attrs,
3145};
3146
NeilBrown3cb03002011-10-11 16:45:26 +11003147int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003148{
3149 rdev->desc_nr = -1;
3150 rdev->saved_raid_disk = -1;
3151 rdev->raid_disk = -1;
3152 rdev->flags = 0;
3153 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003154 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003155 rdev->sb_events = 0;
3156 rdev->last_read_error.tv_sec = 0;
3157 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003158 rdev->sb_loaded = 0;
3159 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003160 atomic_set(&rdev->nr_pending, 0);
3161 atomic_set(&rdev->read_errors, 0);
3162 atomic_set(&rdev->corrected_errors, 0);
3163
3164 INIT_LIST_HEAD(&rdev->same_set);
3165 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003166
3167 /* Add space to store bad block list.
3168 * This reserves the space even on arrays where it cannot
3169 * be used - I wonder if that matters
3170 */
3171 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003172 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003173 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3174 seqlock_init(&rdev->badblocks.lock);
3175 if (rdev->badblocks.page == NULL)
3176 return -ENOMEM;
3177
3178 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003179}
3180EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181/*
3182 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3183 *
3184 * mark the device faulty if:
3185 *
3186 * - the device is nonexistent (zero size)
3187 * - the device has no valid superblock
3188 *
3189 * a faulty rdev _never_ has rdev->sb set.
3190 */
NeilBrown3cb03002011-10-11 16:45:26 +11003191static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
3193 char b[BDEVNAME_SIZE];
3194 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003195 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 sector_t size;
3197
NeilBrown9ffae0c2006-01-06 00:20:32 -08003198 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (!rdev) {
3200 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3201 return ERR_PTR(-ENOMEM);
3202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
NeilBrown2230dfe2011-07-28 11:31:46 +10003204 err = md_rdev_init(rdev);
3205 if (err)
3206 goto abort_free;
3207 err = alloc_disk_sb(rdev);
3208 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 goto abort_free;
3210
NeilBrownc5d79ad2008-02-06 01:39:54 -08003211 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 if (err)
3213 goto abort_free;
3214
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003215 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003216
Mike Snitzer77304d22010-11-08 14:39:12 +01003217 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003219 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 "md: %s has zero or unknown size, marking faulty!\n",
3221 bdevname(rdev->bdev,b));
3222 err = -EINVAL;
3223 goto abort_free;
3224 }
3225
3226 if (super_format >= 0) {
3227 err = super_types[super_format].
3228 load_super(rdev, NULL, super_minor);
3229 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003230 printk(KERN_WARNING
3231 "md: %s does not have a valid v%d.%d "
3232 "superblock, not importing!\n",
3233 bdevname(rdev->bdev,b),
3234 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 goto abort_free;
3236 }
3237 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003238 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 "md: could not read %s's sb, not importing!\n",
3240 bdevname(rdev->bdev,b));
3241 goto abort_free;
3242 }
3243 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return rdev;
3246
3247abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003248 if (rdev->bdev)
3249 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003250 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 kfree(rdev);
3252 return ERR_PTR(err);
3253}
3254
3255/*
3256 * Check a full RAID array for plausibility
3257 */
3258
NeilBrownf72ffdd2014-09-30 14:23:59 +10003259static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
3261 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003262 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 char b[BDEVNAME_SIZE];
3264
3265 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003266 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 switch (super_types[mddev->major_version].
3268 load_super(rdev, freshest, mddev->minor_version)) {
3269 case 1:
3270 freshest = rdev;
3271 break;
3272 case 0:
3273 break;
3274 default:
3275 printk( KERN_ERR \
3276 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003277 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003279 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 super_types[mddev->major_version].
3283 validate_super(mddev, freshest);
3284
3285 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003286 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003287 if (mddev->max_disks &&
3288 (rdev->desc_nr >= mddev->max_disks ||
3289 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003290 printk(KERN_WARNING
3291 "md: %s: %s: only %d devices permitted\n",
3292 mdname(mddev), bdevname(rdev->bdev, b),
3293 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003294 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003295 continue;
3296 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003297 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (super_types[mddev->major_version].
3299 validate_super(mddev, rdev)) {
3300 printk(KERN_WARNING "md: kicking non-fresh %s"
3301 " from array!\n",
3302 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003303 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 continue;
3305 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 if (mddev->level == LEVEL_MULTIPATH) {
3308 rdev->desc_nr = i++;
3309 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003310 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003311 } else if (rdev->raid_disk >=
3312 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3313 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003314 rdev->raid_disk = -1;
3315 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
3317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
NeilBrown72e02072009-12-14 12:49:55 +11003320/* Read a fixed-point number.
3321 * Numbers in sysfs attributes should be in "standard" units where
3322 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003323 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003324 * milliseconds or jiffies.
3325 * This function takes a decimal number with a possible fractional
3326 * component, and produces an integer which is the result of
3327 * multiplying that number by 10^'scale'.
3328 * all without any floating-point arithmetic.
3329 */
3330int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3331{
3332 unsigned long result = 0;
3333 long decimals = -1;
3334 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3335 if (*cp == '.')
3336 decimals = 0;
3337 else if (decimals < scale) {
3338 unsigned int value;
3339 value = *cp - '0';
3340 result = result * 10 + value;
3341 if (decimals >= 0)
3342 decimals++;
3343 }
3344 cp++;
3345 }
3346 if (*cp == '\n')
3347 cp++;
3348 if (*cp)
3349 return -EINVAL;
3350 if (decimals < 0)
3351 decimals = 0;
3352 while (decimals < scale) {
3353 result *= 10;
3354 decimals ++;
3355 }
3356 *res = result;
3357 return 0;
3358}
3359
NeilBrowneae17012005-11-08 21:39:23 -08003360static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003361safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003362{
3363 int msec = (mddev->safemode_delay*1000)/HZ;
3364 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3365}
3366static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003367safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003368{
NeilBrown16f17b32006-06-26 00:27:37 -07003369 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003370
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003371 if (mddev_is_clustered(mddev)) {
3372 pr_info("md: Safemode is disabled for clustered mode\n");
3373 return -EINVAL;
3374 }
3375
NeilBrown72e02072009-12-14 12:49:55 +11003376 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003377 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003378 if (msec == 0)
3379 mddev->safemode_delay = 0;
3380 else {
NeilBrown19052c02008-08-05 15:54:13 +10003381 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003382 unsigned long new_delay = (msec*HZ)/1000;
3383
3384 if (new_delay == 0)
3385 new_delay = 1;
3386 mddev->safemode_delay = new_delay;
3387 if (new_delay < old_delay || old_delay == 0)
3388 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003389 }
3390 return len;
3391}
3392static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003393__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003394
3395static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003396level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003397{
NeilBrown36d091f2014-12-15 12:56:58 +11003398 struct md_personality *p;
3399 int ret;
3400 spin_lock(&mddev->lock);
3401 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003402 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003403 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003404 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003405 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003406 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003407 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003408 else
NeilBrown36d091f2014-12-15 12:56:58 +11003409 ret = 0;
3410 spin_unlock(&mddev->lock);
3411 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003412}
3413
NeilBrownd9d166c2006-01-06 00:20:51 -08003414static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003415level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003416{
Dan Williamsf2859af2010-05-02 10:04:16 -07003417 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003418 ssize_t rv;
3419 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003420 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003421 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003422 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003423 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003424
NeilBrown67918752014-12-15 12:57:01 +11003425 if (slen == 0 || slen >= sizeof(clevel))
3426 return -EINVAL;
3427
3428 rv = mddev_lock(mddev);
3429 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003430 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003431
3432 if (mddev->pers == NULL) {
3433 strncpy(mddev->clevel, buf, slen);
3434 if (mddev->clevel[slen-1] == '\n')
3435 slen--;
3436 mddev->clevel[slen] = 0;
3437 mddev->level = LEVEL_NONE;
3438 rv = len;
3439 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003440 }
NeilBrown67918752014-12-15 12:57:01 +11003441 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003442 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003443 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003444
3445 /* request to change the personality. Need to ensure:
3446 * - array is not engaged in resync/recovery/reshape
3447 * - old personality can be suspended
3448 * - new personality will access other array.
3449 */
3450
NeilBrown67918752014-12-15 12:57:01 +11003451 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003452 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003453 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003454 mddev->reshape_position != MaxSector ||
3455 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003456 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003457
NeilBrown67918752014-12-15 12:57:01 +11003458 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003459 if (!mddev->pers->quiesce) {
3460 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3461 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003462 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003463 }
3464
3465 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003466 strncpy(clevel, buf, slen);
3467 if (clevel[slen-1] == '\n')
3468 slen--;
3469 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003470 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003471 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003472
Dan Williamsf2859af2010-05-02 10:04:16 -07003473 if (request_module("md-%s", clevel) != 0)
3474 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003475 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003476 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003477 if (!pers || !try_module_get(pers->owner)) {
3478 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003479 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003480 rv = -EINVAL;
3481 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003482 }
3483 spin_unlock(&pers_lock);
3484
3485 if (pers == mddev->pers) {
3486 /* Nothing to do! */
3487 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003488 rv = len;
3489 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003490 }
3491 if (!pers->takeover) {
3492 module_put(pers->owner);
3493 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003494 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003495 rv = -EINVAL;
3496 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003497 }
3498
NeilBrowndafb20f2012-03-19 12:46:39 +11003499 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003500 rdev->new_raid_disk = rdev->raid_disk;
3501
NeilBrown245f46c2009-03-31 14:39:39 +11003502 /* ->takeover must set new_* and/or delta_disks
3503 * if it succeeds, and may set them when it fails.
3504 */
3505 priv = pers->takeover(mddev);
3506 if (IS_ERR(priv)) {
3507 mddev->new_level = mddev->level;
3508 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003509 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003510 mddev->raid_disks -= mddev->delta_disks;
3511 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003512 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003513 module_put(pers->owner);
3514 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003515 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003516 rv = PTR_ERR(priv);
3517 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003518 }
3519
3520 /* Looks like we have a winner */
3521 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003522 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003523
3524 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003525 oldpers = mddev->pers;
3526 oldpriv = mddev->private;
3527 mddev->pers = pers;
3528 mddev->private = priv;
3529 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3530 mddev->level = mddev->new_level;
3531 mddev->layout = mddev->new_layout;
3532 mddev->chunk_sectors = mddev->new_chunk_sectors;
3533 mddev->delta_disks = 0;
3534 mddev->reshape_backwards = 0;
3535 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003536 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003537
NeilBrowndb721d32014-12-15 12:56:58 +11003538 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003539 mddev->external) {
3540 /* We are converting from a no-redundancy array
3541 * to a redundancy array and metadata is managed
3542 * externally so we need to be sure that writes
3543 * won't block due to a need to transition
3544 * clean->dirty
3545 * until external management is started.
3546 */
3547 mddev->in_sync = 0;
3548 mddev->safemode_delay = 0;
3549 mddev->safemode = 0;
3550 }
3551
NeilBrowndb721d32014-12-15 12:56:58 +11003552 oldpers->free(mddev, oldpriv);
3553
3554 if (oldpers->sync_request == NULL &&
3555 pers->sync_request != NULL) {
3556 /* need to add the md_redundancy_group */
3557 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3558 printk(KERN_WARNING
3559 "md: cannot register extra attributes for %s\n",
3560 mdname(mddev));
3561 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3562 }
3563 if (oldpers->sync_request != NULL &&
3564 pers->sync_request == NULL) {
3565 /* need to remove the md_redundancy_group */
3566 if (mddev->to_remove == NULL)
3567 mddev->to_remove = &md_redundancy_group;
3568 }
3569
NeilBrowndafb20f2012-03-19 12:46:39 +11003570 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003571 if (rdev->raid_disk < 0)
3572 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003573 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003574 rdev->new_raid_disk = -1;
3575 if (rdev->new_raid_disk == rdev->raid_disk)
3576 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003577 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003578 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003579 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003580 if (rdev->raid_disk < 0)
3581 continue;
3582 if (rdev->new_raid_disk == rdev->raid_disk)
3583 continue;
3584 rdev->raid_disk = rdev->new_raid_disk;
3585 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003586 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003587 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003588 if (sysfs_link_rdev(mddev, rdev))
3589 printk(KERN_WARNING "md: cannot register rd%d"
3590 " for %s after level change\n",
3591 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003592 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003593 }
3594
NeilBrowndb721d32014-12-15 12:56:58 +11003595 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003596 /* this is now an array without redundancy, so
3597 * it must always be in_sync
3598 */
3599 mddev->in_sync = 1;
3600 del_timer_sync(&mddev->safemode_timer);
3601 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003602 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003603 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003604 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003605 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003606 if (!mddev->thread)
3607 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003608 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003609 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003610 rv = len;
3611out_unlock:
3612 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003613 return rv;
3614}
3615
3616static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003617__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003618
NeilBrownd4dbd022006-06-26 00:27:59 -07003619static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003620layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003621{
3622 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003623 if (mddev->reshape_position != MaxSector &&
3624 mddev->layout != mddev->new_layout)
3625 return sprintf(page, "%d (%d)\n",
3626 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003627 return sprintf(page, "%d\n", mddev->layout);
3628}
3629
3630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003631layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003632{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003633 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003634 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003635
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003636 err = kstrtouint(buf, 10, &n);
3637 if (err < 0)
3638 return err;
NeilBrown67918752014-12-15 12:57:01 +11003639 err = mddev_lock(mddev);
3640 if (err)
3641 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003642
NeilBrownb3546032009-03-31 14:56:41 +11003643 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003644 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003645 err = -EBUSY;
3646 else if (mddev->ro)
3647 err = -EROFS;
3648 else {
3649 mddev->new_layout = n;
3650 err = mddev->pers->check_reshape(mddev);
3651 if (err)
3652 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003653 }
NeilBrownb3546032009-03-31 14:56:41 +11003654 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003655 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003656 if (mddev->reshape_position == MaxSector)
3657 mddev->layout = n;
3658 }
NeilBrown67918752014-12-15 12:57:01 +11003659 mddev_unlock(mddev);
3660 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003661}
3662static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003663__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003664
NeilBrowneae17012005-11-08 21:39:23 -08003665static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003666raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003667{
NeilBrownbb636542005-11-08 21:39:45 -08003668 if (mddev->raid_disks == 0)
3669 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003670 if (mddev->reshape_position != MaxSector &&
3671 mddev->delta_disks != 0)
3672 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3673 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003674 return sprintf(page, "%d\n", mddev->raid_disks);
3675}
3676
NeilBrownfd01b882011-10-11 16:47:53 +11003677static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003678
3679static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003680raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003681{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003682 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003683 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003684
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003685 err = kstrtouint(buf, 10, &n);
3686 if (err < 0)
3687 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003688
NeilBrown67918752014-12-15 12:57:01 +11003689 err = mddev_lock(mddev);
3690 if (err)
3691 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003692 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003693 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003694 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003695 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003696 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003697
NeilBrown67918752014-12-15 12:57:01 +11003698 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003699 rdev_for_each(rdev, mddev) {
3700 if (olddisks < n &&
3701 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003702 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003703 if (olddisks > n &&
3704 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003705 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003706 }
NeilBrown67918752014-12-15 12:57:01 +11003707 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003708 mddev->delta_disks = n - olddisks;
3709 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003710 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003711 } else
NeilBrownda943b992006-01-06 00:20:54 -08003712 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003713out_unlock:
3714 mddev_unlock(mddev);
3715 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003716}
3717static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003718__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003719
NeilBrown24dd4692005-11-08 21:39:26 -08003720static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003721chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003722{
NeilBrown08a02ec2007-05-09 02:35:38 -07003723 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003724 mddev->chunk_sectors != mddev->new_chunk_sectors)
3725 return sprintf(page, "%d (%d)\n",
3726 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003727 mddev->chunk_sectors << 9);
3728 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003729}
3730
3731static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003732chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003733{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003734 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003735 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003736
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003737 err = kstrtoul(buf, 10, &n);
3738 if (err < 0)
3739 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003740
NeilBrown67918752014-12-15 12:57:01 +11003741 err = mddev_lock(mddev);
3742 if (err)
3743 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003744 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003745 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003746 err = -EBUSY;
3747 else if (mddev->ro)
3748 err = -EROFS;
3749 else {
3750 mddev->new_chunk_sectors = n >> 9;
3751 err = mddev->pers->check_reshape(mddev);
3752 if (err)
3753 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003754 }
NeilBrownb3546032009-03-31 14:56:41 +11003755 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003756 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003757 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003758 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003759 }
NeilBrown67918752014-12-15 12:57:01 +11003760 mddev_unlock(mddev);
3761 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003762}
3763static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003764__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003765
NeilBrowna94213b2006-06-26 00:28:00 -07003766static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003767resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003768{
NeilBrownd1a7c502009-03-31 15:24:32 +11003769 if (mddev->recovery_cp == MaxSector)
3770 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003771 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3772}
3773
3774static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003775resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003776{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003777 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003778 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003779
3780 if (cmd_match(buf, "none"))
3781 n = MaxSector;
3782 else {
3783 err = kstrtoull(buf, 10, &n);
3784 if (err < 0)
3785 return err;
3786 if (n != (sector_t)n)
3787 return -EINVAL;
3788 }
NeilBrowna94213b2006-06-26 00:28:00 -07003789
NeilBrown67918752014-12-15 12:57:01 +11003790 err = mddev_lock(mddev);
3791 if (err)
3792 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003793 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003794 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003795
NeilBrown67918752014-12-15 12:57:01 +11003796 if (!err) {
3797 mddev->recovery_cp = n;
3798 if (mddev->pers)
3799 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3800 }
3801 mddev_unlock(mddev);
3802 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003803}
3804static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003805__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3806 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003807
NeilBrown9e653b62006-06-26 00:27:58 -07003808/*
3809 * The array state can be:
3810 *
3811 * clear
3812 * No devices, no size, no level
3813 * Equivalent to STOP_ARRAY ioctl
3814 * inactive
3815 * May have some settings, but array is not active
3816 * all IO results in error
3817 * When written, doesn't tear down array, but just stops it
3818 * suspended (not supported yet)
3819 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003820 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003821 * readonly
3822 * no resync can happen. no superblocks get written.
3823 * write requests fail
3824 * read-auto
3825 * like readonly, but behaves like 'clean' on a write request.
3826 *
3827 * clean - no pending writes, but otherwise active.
3828 * When written to inactive array, starts without resync
3829 * If a write request arrives then
3830 * if metadata is known, mark 'dirty' and switch to 'active'.
3831 * if not known, block and switch to write-pending
3832 * If written to an active array that has pending writes, then fails.
3833 * active
3834 * fully active: IO and resync can be happening.
3835 * When written to inactive array, starts with resync
3836 *
3837 * write-pending
3838 * clean, but writes are blocked waiting for 'active' to be written.
3839 *
3840 * active-idle
3841 * like active, but no writes have been seen for a while (100msec).
3842 *
3843 */
3844enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3845 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003846static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003847 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3848 "write-pending", "active-idle", NULL };
3849
3850static int match_word(const char *word, char **list)
3851{
3852 int n;
3853 for (n=0; list[n]; n++)
3854 if (cmd_match(word, list[n]))
3855 break;
3856 return n;
3857}
3858
3859static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003860array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003861{
3862 enum array_state st = inactive;
3863
3864 if (mddev->pers)
3865 switch(mddev->ro) {
3866 case 1:
3867 st = readonly;
3868 break;
3869 case 2:
3870 st = read_auto;
3871 break;
3872 case 0:
3873 if (mddev->in_sync)
3874 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003875 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003876 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003877 else if (mddev->safemode)
3878 st = active_idle;
3879 else
3880 st = active;
3881 }
3882 else {
3883 if (list_empty(&mddev->disks) &&
3884 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003885 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003886 st = clear;
3887 else
3888 st = inactive;
3889 }
3890 return sprintf(page, "%s\n", array_states[st]);
3891}
3892
NeilBrownf72ffdd2014-09-30 14:23:59 +10003893static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3894static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3895static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003896static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003897
3898static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003899array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003900{
NeilBrown67918752014-12-15 12:57:01 +11003901 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003902 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003903
3904 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3905 /* don't take reconfig_mutex when toggling between
3906 * clean and active
3907 */
3908 spin_lock(&mddev->lock);
3909 if (st == active) {
3910 restart_array(mddev);
3911 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3912 wake_up(&mddev->sb_wait);
3913 err = 0;
3914 } else /* st == clean */ {
3915 restart_array(mddev);
3916 if (atomic_read(&mddev->writes_pending) == 0) {
3917 if (mddev->in_sync == 0) {
3918 mddev->in_sync = 1;
3919 if (mddev->safemode == 1)
3920 mddev->safemode = 0;
3921 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3922 }
3923 err = 0;
3924 } else
3925 err = -EBUSY;
3926 }
3927 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003928 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003929 }
3930 err = mddev_lock(mddev);
3931 if (err)
3932 return err;
3933 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003934 switch(st) {
3935 case bad_word:
3936 break;
3937 case clear:
3938 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003939 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003940 break;
3941 case inactive:
3942 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003943 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003944 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003945 else
NeilBrowne6910632008-02-06 01:39:51 -08003946 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003947 break;
3948 case suspended:
3949 break; /* not supported yet */
3950 case readonly:
3951 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003952 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003953 else {
3954 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003955 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003956 err = do_md_run(mddev);
3957 }
3958 break;
3959 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003960 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003961 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003962 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003963 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003964 err = restart_array(mddev);
3965 if (err == 0) {
3966 mddev->ro = 2;
3967 set_disk_ro(mddev->gendisk, 0);
3968 }
NeilBrown9e653b62006-06-26 00:27:58 -07003969 } else {
3970 mddev->ro = 2;
3971 err = do_md_run(mddev);
3972 }
3973 break;
3974 case clean:
3975 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003976 err = restart_array(mddev);
3977 if (err)
3978 break;
NeilBrown85572d72014-12-15 12:56:56 +11003979 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003980 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003981 if (mddev->in_sync == 0) {
3982 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003983 if (mddev->safemode == 1)
3984 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003985 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003986 }
3987 err = 0;
3988 } else
3989 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003990 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003991 } else
3992 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003993 break;
3994 case active:
3995 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003996 err = restart_array(mddev);
3997 if (err)
3998 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10003999 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004000 wake_up(&mddev->sb_wait);
4001 err = 0;
4002 } else {
4003 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004004 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004005 err = do_md_run(mddev);
4006 }
4007 break;
4008 case write_pending:
4009 case active_idle:
4010 /* these cannot be set */
4011 break;
4012 }
NeilBrown67918752014-12-15 12:57:01 +11004013
4014 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004015 if (mddev->hold_active == UNTIL_IOCTL)
4016 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004017 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004018 }
NeilBrown67918752014-12-15 12:57:01 +11004019 mddev_unlock(mddev);
4020 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004021}
NeilBrown80ca3a42006-07-10 04:44:18 -07004022static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004023__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004024
NeilBrown6d7ff7382006-01-06 00:21:16 -08004025static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004026max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004027 return sprintf(page, "%d\n",
4028 atomic_read(&mddev->max_corr_read_errors));
4029}
4030
4031static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004032max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004033{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004034 unsigned int n;
4035 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004036
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004037 rv = kstrtouint(buf, 10, &n);
4038 if (rv < 0)
4039 return rv;
4040 atomic_set(&mddev->max_corr_read_errors, n);
4041 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004042}
4043
4044static struct md_sysfs_entry max_corr_read_errors =
4045__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4046 max_corrected_read_errors_store);
4047
4048static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004049null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004050{
4051 return -EINVAL;
4052}
4053
4054static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004055new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004056{
4057 /* buf must be %d:%d\n? giving major and minor numbers */
4058 /* The new device is added to the array.
4059 * If the array has a persistent superblock, we read the
4060 * superblock to initialise info and check validity.
4061 * Otherwise, only checking done is that in bind_rdev_to_array,
4062 * which mainly checks size.
4063 */
4064 char *e;
4065 int major = simple_strtoul(buf, &e, 10);
4066 int minor;
4067 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004068 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004069 int err;
4070
4071 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4072 return -EINVAL;
4073 minor = simple_strtoul(e+1, &e, 10);
4074 if (*e && *e != '\n')
4075 return -EINVAL;
4076 dev = MKDEV(major, minor);
4077 if (major != MAJOR(dev) ||
4078 minor != MINOR(dev))
4079 return -EOVERFLOW;
4080
NeilBrown67918752014-12-15 12:57:01 +11004081 flush_workqueue(md_misc_wq);
4082
4083 err = mddev_lock(mddev);
4084 if (err)
4085 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004086 if (mddev->persistent) {
4087 rdev = md_import_device(dev, mddev->major_version,
4088 mddev->minor_version);
4089 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004090 struct md_rdev *rdev0
4091 = list_entry(mddev->disks.next,
4092 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004093 err = super_types[mddev->major_version]
4094 .load_super(rdev, rdev0, mddev->minor_version);
4095 if (err < 0)
4096 goto out;
4097 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004098 } else if (mddev->external)
4099 rdev = md_import_device(dev, -2, -1);
4100 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004101 rdev = md_import_device(dev, -1, -1);
4102
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004103 if (IS_ERR(rdev)) {
4104 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004105 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004106 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004107 err = bind_rdev_to_array(rdev, mddev);
4108 out:
4109 if (err)
4110 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004111 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004112 return err ? err : len;
4113}
4114
4115static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004116__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004117
4118static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004119bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004120{
4121 char *end;
4122 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004123 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004124
NeilBrown67918752014-12-15 12:57:01 +11004125 err = mddev_lock(mddev);
4126 if (err)
4127 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004128 if (!mddev->bitmap)
4129 goto out;
4130 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4131 while (*buf) {
4132 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4133 if (buf == end) break;
4134 if (*end == '-') { /* range */
4135 buf = end + 1;
4136 end_chunk = simple_strtoul(buf, &end, 0);
4137 if (buf == end) break;
4138 }
4139 if (*end && !isspace(*end)) break;
4140 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004141 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004142 }
4143 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4144out:
NeilBrown67918752014-12-15 12:57:01 +11004145 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004146 return len;
4147}
4148
4149static struct md_sysfs_entry md_bitmap =
4150__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4151
4152static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004153size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004154{
Andre Noll58c0fed2009-03-31 14:33:13 +11004155 return sprintf(page, "%llu\n",
4156 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004157}
4158
NeilBrownfd01b882011-10-11 16:47:53 +11004159static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004160
4161static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004162size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004163{
4164 /* If array is inactive, we can reduce the component size, but
4165 * not increase it (except from 0).
4166 * If array is active, we can try an on-line resize
4167 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004168 sector_t sectors;
4169 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004170
Andre Noll58c0fed2009-03-31 14:33:13 +11004171 if (err < 0)
4172 return err;
NeilBrown67918752014-12-15 12:57:01 +11004173 err = mddev_lock(mddev);
4174 if (err)
4175 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004176 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004177 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004178 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004179 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004180 if (mddev->dev_sectors == 0 ||
4181 mddev->dev_sectors > sectors)
4182 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004183 else
4184 err = -ENOSPC;
4185 }
NeilBrown67918752014-12-15 12:57:01 +11004186 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004187 return err ? err : len;
4188}
4189
4190static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004191__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004192
Masanari Iida83f0d772012-10-30 00:18:08 +09004193/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004194 * This is one of
4195 * 'none' for arrays with no metadata (good luck...)
4196 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004197 * or N.M for internally known formats
4198 */
4199static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004200metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004201{
4202 if (mddev->persistent)
4203 return sprintf(page, "%d.%d\n",
4204 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004205 else if (mddev->external)
4206 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004207 else
4208 return sprintf(page, "none\n");
4209}
4210
4211static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004212metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004213{
4214 int major, minor;
4215 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004216 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004217 /* Changing the details of 'external' metadata is
4218 * always permitted. Otherwise there must be
4219 * no devices attached to the array.
4220 */
NeilBrown67918752014-12-15 12:57:01 +11004221
4222 err = mddev_lock(mddev);
4223 if (err)
4224 return err;
4225 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004226 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4227 ;
4228 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004229 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004230
NeilBrown67918752014-12-15 12:57:01 +11004231 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004232 if (cmd_match(buf, "none")) {
4233 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004234 mddev->external = 0;
4235 mddev->major_version = 0;
4236 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004237 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004238 }
4239 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004240 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004241 if (namelen >= sizeof(mddev->metadata_type))
4242 namelen = sizeof(mddev->metadata_type)-1;
4243 strncpy(mddev->metadata_type, buf+9, namelen);
4244 mddev->metadata_type[namelen] = 0;
4245 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4246 mddev->metadata_type[--namelen] = 0;
4247 mddev->persistent = 0;
4248 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004249 mddev->major_version = 0;
4250 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004251 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004252 }
4253 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004254 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004255 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004256 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004257 buf = e+1;
4258 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004259 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004260 goto out_unlock;
4261 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004262 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004263 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004264 mddev->major_version = major;
4265 mddev->minor_version = minor;
4266 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004267 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004268 err = 0;
4269out_unlock:
4270 mddev_unlock(mddev);
4271 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004272}
4273
4274static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004275__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004276
NeilBrowna35b0d62006-01-06 00:20:49 -08004277static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004278action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004279{
NeilBrown7eec3142005-11-08 21:39:44 -08004280 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004281 unsigned long recovery = mddev->recovery;
4282 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004283 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004284 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4285 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4286 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004287 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004288 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4289 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004290 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004291 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004292 type = "check";
4293 else
4294 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004295 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004296 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004297 else if (mddev->reshape_position != MaxSector)
4298 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004299 }
4300 return sprintf(page, "%s\n", type);
4301}
4302
4303static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004304action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004305{
NeilBrown7eec3142005-11-08 21:39:44 -08004306 if (!mddev->pers || !mddev->pers->sync_request)
4307 return -EINVAL;
4308
NeilBrownb6a9ce62009-05-26 09:41:17 +10004309
4310 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004311 if (cmd_match(page, "frozen"))
4312 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4313 else
4314 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004315 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4316 mddev_lock(mddev) == 0) {
4317 flush_workqueue(md_misc_wq);
4318 if (mddev->sync_thread) {
4319 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004320 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004321 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004322 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004323 }
NeilBrown312045e2015-12-21 11:01:21 +11004324 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004325 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004326 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004327 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004328 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004329 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004330 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004331 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004332 int err;
4333 if (mddev->pers->start_reshape == NULL)
4334 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004335 err = mddev_lock(mddev);
4336 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004337 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4338 err = -EBUSY;
4339 else {
4340 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4341 err = mddev->pers->start_reshape(mddev);
4342 }
NeilBrown67918752014-12-15 12:57:01 +11004343 mddev_unlock(mddev);
4344 }
NeilBrown16484bf2006-03-27 01:18:13 -08004345 if (err)
4346 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004347 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004348 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004349 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004350 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004351 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004352 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004353 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004354 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4355 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004356 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004357 if (mddev->ro == 2) {
4358 /* A write to sync_action is enough to justify
4359 * canceling read-auto mode
4360 */
4361 mddev->ro = 0;
4362 md_wakeup_thread(mddev->sync_thread);
4363 }
NeilBrown03c902e2006-01-06 00:20:46 -08004364 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004365 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004366 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004367 return len;
4368}
4369
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004370static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004371__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004372
4373static ssize_t
4374last_sync_action_show(struct mddev *mddev, char *page)
4375{
4376 return sprintf(page, "%s\n", mddev->last_sync_action);
4377}
4378
4379static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4380
NeilBrown9d888832005-11-08 21:39:26 -08004381static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004382mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004383{
4384 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004385 (unsigned long long)
4386 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004387}
4388
NeilBrown80ca3a42006-07-10 04:44:18 -07004389static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004390
NeilBrown88202a02006-01-06 00:21:36 -08004391static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004392sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004393{
4394 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4395 mddev->sync_speed_min ? "local": "system");
4396}
4397
4398static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004399sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004400{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004401 unsigned int min;
4402 int rv;
4403
NeilBrown88202a02006-01-06 00:21:36 -08004404 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004405 min = 0;
4406 } else {
4407 rv = kstrtouint(buf, 10, &min);
4408 if (rv < 0)
4409 return rv;
4410 if (min == 0)
4411 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004412 }
NeilBrown88202a02006-01-06 00:21:36 -08004413 mddev->sync_speed_min = min;
4414 return len;
4415}
4416
4417static struct md_sysfs_entry md_sync_min =
4418__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4419
4420static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004421sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004422{
4423 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4424 mddev->sync_speed_max ? "local": "system");
4425}
4426
4427static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004428sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004429{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004430 unsigned int max;
4431 int rv;
4432
NeilBrown88202a02006-01-06 00:21:36 -08004433 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004434 max = 0;
4435 } else {
4436 rv = kstrtouint(buf, 10, &max);
4437 if (rv < 0)
4438 return rv;
4439 if (max == 0)
4440 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004441 }
NeilBrown88202a02006-01-06 00:21:36 -08004442 mddev->sync_speed_max = max;
4443 return len;
4444}
4445
4446static struct md_sysfs_entry md_sync_max =
4447__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4448
Iustin Popd7f3d292007-10-16 23:30:54 -07004449static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004450degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004451{
4452 return sprintf(page, "%d\n", mddev->degraded);
4453}
4454static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004455
4456static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004457sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004458{
4459 return sprintf(page, "%d\n", mddev->parallel_resync);
4460}
4461
4462static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004463sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004464{
4465 long n;
4466
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004467 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004468 return -EINVAL;
4469
4470 if (n != 0 && n != 1)
4471 return -EINVAL;
4472
4473 mddev->parallel_resync = n;
4474
4475 if (mddev->sync_thread)
4476 wake_up(&resync_wait);
4477
4478 return len;
4479}
4480
4481/* force parallel resync, even with shared block devices */
4482static struct md_sysfs_entry md_sync_force_parallel =
4483__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4484 sync_force_parallel_show, sync_force_parallel_store);
4485
4486static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004487sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004488{
4489 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004490 if (mddev->curr_resync == 0)
4491 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004492 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4493 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004494 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004495 db = resync - mddev->resync_mark_cnt;
4496 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004497}
4498
NeilBrown80ca3a42006-07-10 04:44:18 -07004499static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004500
4501static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004502sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004503{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004504 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004505
NeilBrownacb180b2009-04-14 16:28:34 +10004506 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4507 return sprintf(page, "none\n");
4508
NeilBrown72f36d52012-10-11 14:25:57 +11004509 if (mddev->curr_resync == 1 ||
4510 mddev->curr_resync == 2)
4511 return sprintf(page, "delayed\n");
4512
NeilBrownc804cde2012-05-21 09:28:33 +10004513 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4514 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004515 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004516 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004517 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004518
NeilBrownacb180b2009-04-14 16:28:34 +10004519 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004520 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004521}
4522
NeilBrown750f1992014-09-30 08:53:05 +10004523static struct md_sysfs_entry md_sync_completed =
4524 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004525
NeilBrowne464eaf2006-03-27 01:18:14 -08004526static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004527min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004528{
4529 return sprintf(page, "%llu\n",
4530 (unsigned long long)mddev->resync_min);
4531}
4532static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004533min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004534{
4535 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004536 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004537
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004538 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004539 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004540
4541 spin_lock(&mddev->lock);
4542 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004543 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004544 goto out_unlock;
4545
4546 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004547 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004548 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004549
NeilBrown50c37b12015-03-23 17:36:38 +11004550 /* Round down to multiple of 4K for safety */
4551 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004552 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004553
NeilBrown23da4222014-12-15 12:57:01 +11004554out_unlock:
4555 spin_unlock(&mddev->lock);
4556 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004557}
4558
4559static struct md_sysfs_entry md_min_sync =
4560__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4561
4562static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004563max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004564{
4565 if (mddev->resync_max == MaxSector)
4566 return sprintf(page, "max\n");
4567 else
4568 return sprintf(page, "%llu\n",
4569 (unsigned long long)mddev->resync_max);
4570}
4571static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004572max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004573{
NeilBrown23da4222014-12-15 12:57:01 +11004574 int err;
4575 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004576 if (strncmp(buf, "max", 3) == 0)
4577 mddev->resync_max = MaxSector;
4578 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004579 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004580 int chunk;
4581
4582 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004583 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004584 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004585 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004586 goto out_unlock;
4587
4588 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004589 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004590 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004591 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004592 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004593
4594 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004595 chunk = mddev->chunk_sectors;
4596 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004597 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004598
4599 err = -EINVAL;
4600 if (sector_div(temp, chunk))
4601 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004602 }
4603 mddev->resync_max = max;
4604 }
4605 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004606 err = 0;
4607out_unlock:
4608 spin_unlock(&mddev->lock);
4609 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004610}
4611
4612static struct md_sysfs_entry md_max_sync =
4613__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4614
4615static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004616suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004617{
4618 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4619}
4620
4621static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004622suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004623{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004624 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004625 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004626
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004627 err = kstrtoull(buf, 10, &new);
4628 if (err < 0)
4629 return err;
4630 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004631 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004632
NeilBrown67918752014-12-15 12:57:01 +11004633 err = mddev_lock(mddev);
4634 if (err)
4635 return err;
4636 err = -EINVAL;
4637 if (mddev->pers == NULL ||
4638 mddev->pers->quiesce == NULL)
4639 goto unlock;
4640 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004641 mddev->suspend_lo = new;
4642 if (new >= old)
4643 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004644 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004645 else {
4646 /* Expanding suspended region - need to wait */
4647 mddev->pers->quiesce(mddev, 1);
4648 mddev->pers->quiesce(mddev, 0);
4649 }
NeilBrown67918752014-12-15 12:57:01 +11004650 err = 0;
4651unlock:
4652 mddev_unlock(mddev);
4653 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004654}
4655static struct md_sysfs_entry md_suspend_lo =
4656__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4657
NeilBrowne464eaf2006-03-27 01:18:14 -08004658static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004659suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004660{
4661 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4662}
4663
4664static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004665suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004666{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004667 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004668 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004669
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004670 err = kstrtoull(buf, 10, &new);
4671 if (err < 0)
4672 return err;
4673 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004674 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004675
NeilBrown67918752014-12-15 12:57:01 +11004676 err = mddev_lock(mddev);
4677 if (err)
4678 return err;
4679 err = -EINVAL;
4680 if (mddev->pers == NULL ||
4681 mddev->pers->quiesce == NULL)
4682 goto unlock;
4683 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004684 mddev->suspend_hi = new;
4685 if (new <= old)
4686 /* Shrinking suspended region */
4687 mddev->pers->quiesce(mddev, 2);
4688 else {
4689 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004690 mddev->pers->quiesce(mddev, 1);
4691 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004692 }
NeilBrown67918752014-12-15 12:57:01 +11004693 err = 0;
4694unlock:
4695 mddev_unlock(mddev);
4696 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004697}
4698static struct md_sysfs_entry md_suspend_hi =
4699__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4700
NeilBrown08a02ec2007-05-09 02:35:38 -07004701static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004702reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004703{
4704 if (mddev->reshape_position != MaxSector)
4705 return sprintf(page, "%llu\n",
4706 (unsigned long long)mddev->reshape_position);
4707 strcpy(page, "none\n");
4708 return 5;
4709}
4710
4711static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004712reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004713{
NeilBrownc6563a82012-05-21 09:27:00 +10004714 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004715 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004716 int err;
NeilBrown67918752014-12-15 12:57:01 +11004717
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004718 err = kstrtoull(buf, 10, &new);
4719 if (err < 0)
4720 return err;
4721 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004722 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004723 err = mddev_lock(mddev);
4724 if (err)
4725 return err;
4726 err = -EBUSY;
4727 if (mddev->pers)
4728 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004729 mddev->reshape_position = new;
4730 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004731 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004732 mddev->new_level = mddev->level;
4733 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004734 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004735 rdev_for_each(rdev, mddev)
4736 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004737 err = 0;
4738unlock:
4739 mddev_unlock(mddev);
4740 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004741}
4742
4743static struct md_sysfs_entry md_reshape_position =
4744__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4745 reshape_position_store);
4746
Dan Williamsb522adc2009-03-31 15:00:31 +11004747static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004748reshape_direction_show(struct mddev *mddev, char *page)
4749{
4750 return sprintf(page, "%s\n",
4751 mddev->reshape_backwards ? "backwards" : "forwards");
4752}
4753
4754static ssize_t
4755reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4756{
4757 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004758 int err;
4759
NeilBrown2c810cd2012-05-21 09:27:00 +10004760 if (cmd_match(buf, "forwards"))
4761 backwards = 0;
4762 else if (cmd_match(buf, "backwards"))
4763 backwards = 1;
4764 else
4765 return -EINVAL;
4766 if (mddev->reshape_backwards == backwards)
4767 return len;
4768
NeilBrown67918752014-12-15 12:57:01 +11004769 err = mddev_lock(mddev);
4770 if (err)
4771 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004772 /* check if we are allowed to change */
4773 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004774 err = -EBUSY;
4775 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004776 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004777 err = -EINVAL;
4778 else
4779 mddev->reshape_backwards = backwards;
4780 mddev_unlock(mddev);
4781 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004782}
4783
4784static struct md_sysfs_entry md_reshape_direction =
4785__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4786 reshape_direction_store);
4787
4788static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004789array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004790{
4791 if (mddev->external_size)
4792 return sprintf(page, "%llu\n",
4793 (unsigned long long)mddev->array_sectors/2);
4794 else
4795 return sprintf(page, "default\n");
4796}
4797
4798static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004799array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004800{
4801 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004802 int err;
4803
4804 err = mddev_lock(mddev);
4805 if (err)
4806 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004807
4808 if (strncmp(buf, "default", 7) == 0) {
4809 if (mddev->pers)
4810 sectors = mddev->pers->size(mddev, 0, 0);
4811 else
4812 sectors = mddev->array_sectors;
4813
4814 mddev->external_size = 0;
4815 } else {
4816 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004817 err = -EINVAL;
4818 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4819 err = -E2BIG;
4820 else
4821 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004822 }
4823
NeilBrown67918752014-12-15 12:57:01 +11004824 if (!err) {
4825 mddev->array_sectors = sectors;
4826 if (mddev->pers) {
4827 set_capacity(mddev->gendisk, mddev->array_sectors);
4828 revalidate_disk(mddev->gendisk);
4829 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004830 }
NeilBrown67918752014-12-15 12:57:01 +11004831 mddev_unlock(mddev);
4832 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004833}
4834
4835static struct md_sysfs_entry md_array_size =
4836__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4837 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004838
NeilBrowneae17012005-11-08 21:39:23 -08004839static struct attribute *md_default_attrs[] = {
4840 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004841 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004842 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004843 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004844 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004845 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004846 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004847 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004848 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004849 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004850 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004851 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004852 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004853 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004854 NULL,
4855};
4856
4857static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004858 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004859 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004860 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004861 &md_sync_min.attr,
4862 &md_sync_max.attr,
4863 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004864 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004865 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004866 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004867 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004868 &md_suspend_lo.attr,
4869 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004870 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004871 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004872 NULL,
4873};
NeilBrown411036f2005-11-08 21:39:40 -08004874static struct attribute_group md_redundancy_group = {
4875 .name = NULL,
4876 .attrs = md_redundancy_attrs,
4877};
4878
NeilBrowneae17012005-11-08 21:39:23 -08004879static ssize_t
4880md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4881{
4882 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004883 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004884 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004885
4886 if (!entry->show)
4887 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004888 spin_lock(&all_mddevs_lock);
4889 if (list_empty(&mddev->all_mddevs)) {
4890 spin_unlock(&all_mddevs_lock);
4891 return -EBUSY;
4892 }
4893 mddev_get(mddev);
4894 spin_unlock(&all_mddevs_lock);
4895
NeilBrownb7b17c92014-12-15 12:56:59 +11004896 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004897 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004898 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004899}
4900
4901static ssize_t
4902md_attr_store(struct kobject *kobj, struct attribute *attr,
4903 const char *page, size_t length)
4904{
4905 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004906 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004907 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004908
4909 if (!entry->store)
4910 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004911 if (!capable(CAP_SYS_ADMIN))
4912 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004913 spin_lock(&all_mddevs_lock);
4914 if (list_empty(&mddev->all_mddevs)) {
4915 spin_unlock(&all_mddevs_lock);
4916 return -EBUSY;
4917 }
4918 mddev_get(mddev);
4919 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004920 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004921 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004922 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004923}
4924
4925static void md_free(struct kobject *ko)
4926{
NeilBrownfd01b882011-10-11 16:47:53 +11004927 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004928
4929 if (mddev->sysfs_state)
4930 sysfs_put(mddev->sysfs_state);
4931
NeilBrown6cd18e72015-04-27 14:12:22 +10004932 if (mddev->queue)
4933 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004934 if (mddev->gendisk) {
4935 del_gendisk(mddev->gendisk);
4936 put_disk(mddev->gendisk);
4937 }
NeilBrowna21d1502009-01-09 08:31:09 +11004938
NeilBrowneae17012005-11-08 21:39:23 -08004939 kfree(mddev);
4940}
4941
Emese Revfy52cf25d2010-01-19 02:58:23 +01004942static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004943 .show = md_attr_show,
4944 .store = md_attr_store,
4945};
4946static struct kobj_type md_ktype = {
4947 .release = md_free,
4948 .sysfs_ops = &md_sysfs_ops,
4949 .default_attrs = md_default_attrs,
4950};
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952int mdp_major = 0;
4953
Dan Williams5fd3a172009-03-04 00:57:25 -07004954static void mddev_delayed_delete(struct work_struct *ws)
4955{
NeilBrownfd01b882011-10-11 16:47:53 +11004956 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004957
NeilBrown43a70502009-12-14 12:49:55 +11004958 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004959 kobject_del(&mddev->kobj);
4960 kobject_put(&mddev->kobj);
4961}
4962
NeilBrownefeb53c2009-01-09 08:31:10 +11004963static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004965 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004966 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004968 int partitioned;
4969 int shift;
4970 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004971 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
4973 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004974 return -ENODEV;
4975
4976 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4977 shift = partitioned ? MdpMinorShift : 0;
4978 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
Tejun Heoe804ac72010-10-15 15:36:08 +02004980 /* wait for any previous instance of this device to be
4981 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004982 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004983 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004984
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004985 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004986 error = -EEXIST;
4987 if (mddev->gendisk)
4988 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004989
4990 if (name) {
4991 /* Need to ensure that 'name' is not a duplicate.
4992 */
NeilBrownfd01b882011-10-11 16:47:53 +11004993 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004994 spin_lock(&all_mddevs_lock);
4995
4996 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4997 if (mddev2->gendisk &&
4998 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4999 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005000 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005001 }
5002 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 }
NeilBrown8b765392009-01-09 08:31:08 +11005004
NeilBrown0909dc42009-07-01 12:27:21 +10005005 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005006 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005007 if (!mddev->queue)
5008 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005009 mddev->queue->queuedata = mddev;
5010
NeilBrown409c57f2009-03-31 14:39:39 +11005011 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005012 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 disk = alloc_disk(1 << shift);
5015 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005016 blk_cleanup_queue(mddev->queue);
5017 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005018 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005020 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005022 if (name)
5023 strcpy(disk->disk_name, name);
5024 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005026 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 disk->fops = &md_fops;
5029 disk->private_data = mddev;
5030 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10005031 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11005032 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005033 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005034 * remove it now.
5035 */
5036 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005038 /* As soon as we call add_disk(), another thread could get
5039 * through to md_open, so make sure it doesn't get too far
5040 */
5041 mutex_lock(&mddev->open_mutex);
5042 add_disk(disk);
5043
Tejun Heoed9e1982008-08-25 19:56:05 +09005044 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5045 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005046 if (error) {
5047 /* This isn't possible, but as kobject_init_and_add is marked
5048 * __must_check, we must do something with the result
5049 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005050 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5051 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005052 error = 0;
5053 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005054 if (mddev->kobj.sd &&
5055 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11005056 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005057 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005058 abort:
5059 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005060 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005061 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005062 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005063 }
NeilBrownd3374822009-01-09 08:31:10 +11005064 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005065 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005066}
5067
5068static struct kobject *md_probe(dev_t dev, int *part, void *data)
5069{
5070 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 return NULL;
5072}
5073
NeilBrownefeb53c2009-01-09 08:31:10 +11005074static int add_named_array(const char *val, struct kernel_param *kp)
5075{
5076 /* val must be "md_*" where * is not all digits.
5077 * We allocate an array with a large free minor number, and
5078 * set the name to val. val must not already be an active name.
5079 */
5080 int len = strlen(val);
5081 char buf[DISK_NAME_LEN];
5082
5083 while (len && val[len-1] == '\n')
5084 len--;
5085 if (len >= DISK_NAME_LEN)
5086 return -E2BIG;
5087 strlcpy(buf, val, len+1);
5088 if (strncmp(buf, "md_", 3) != 0)
5089 return -EINVAL;
5090 return md_alloc(0, buf);
5091}
5092
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093static void md_safemode_timeout(unsigned long data)
5094{
NeilBrownfd01b882011-10-11 16:47:53 +11005095 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
Neil Brown0fd62b82008-06-28 08:31:36 +10005097 if (!atomic_read(&mddev->writes_pending)) {
5098 mddev->safemode = 1;
5099 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005100 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 md_wakeup_thread(mddev->thread);
5103}
5104
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005105static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
NeilBrownfd01b882011-10-11 16:47:53 +11005107int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108{
NeilBrown2604b702006-01-06 00:20:36 -08005109 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005110 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005111 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
NeilBrowna757e642005-04-16 15:26:42 -07005113 if (list_empty(&mddev->disks))
5114 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
5117 if (mddev->pers)
5118 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005119 /* Cannot run until previous stop completes properly */
5120 if (mddev->sysfs_active)
5121 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 /*
5124 * Analyze all RAID superblock(s)
5125 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005126 if (!mddev->raid_disks) {
5127 if (!mddev->persistent)
5128 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005129 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
NeilBrownd9d166c2006-01-06 00:20:51 -08005132 if (mddev->level != LEVEL_NONE)
5133 request_module("md-level-%d", mddev->level);
5134 else if (mddev->clevel[0])
5135 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 /*
5138 * Drop all container device buffers, from now on
5139 * the only valid external interface is through the md
5140 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005142 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005143 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 continue;
5145 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005146 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005147
5148 /* perform some consistency tests on the device.
5149 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005150 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005151 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005152 if (rdev->meta_bdev) {
5153 /* Nothing to check */;
5154 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005155 if (mddev->dev_sectors &&
5156 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005157 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005158 printk("md: %s: data overlaps metadata\n",
5159 mdname(mddev));
5160 return -EINVAL;
5161 }
5162 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005163 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005164 > rdev->data_offset) {
5165 printk("md: %s: metadata overlaps data\n",
5166 mdname(mddev));
5167 return -EINVAL;
5168 }
5169 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005170 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 }
5172
NeilBrowna167f662010-10-26 18:31:13 +11005173 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005174 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005177 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005178 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005180 if (mddev->level != LEVEL_NONE)
5181 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5182 mddev->level);
5183 else
5184 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5185 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 return -EINVAL;
5187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005189 if (mddev->level != pers->level) {
5190 mddev->level = pers->level;
5191 mddev->new_level = pers->level;
5192 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005193 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
NeilBrownf6705572006-03-27 01:18:11 -08005195 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005196 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005197 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005198 module_put(pers->owner);
5199 return -EINVAL;
5200 }
5201
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005202 if (pers->sync_request) {
5203 /* Warn if this is a potentially silly
5204 * configuration.
5205 */
5206 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005207 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005208 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005209
NeilBrowndafb20f2012-03-19 12:46:39 +11005210 rdev_for_each(rdev, mddev)
5211 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005212 if (rdev < rdev2 &&
5213 rdev->bdev->bd_contains ==
5214 rdev2->bdev->bd_contains) {
5215 printk(KERN_WARNING
5216 "%s: WARNING: %s appears to be"
5217 " on the same physical disk as"
5218 " %s.\n",
5219 mdname(mddev),
5220 bdevname(rdev->bdev,b),
5221 bdevname(rdev2->bdev,b2));
5222 warned = 1;
5223 }
5224 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005225
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005226 if (warned)
5227 printk(KERN_WARNING
5228 "True protection against single-disk"
5229 " failure might be compromised.\n");
5230 }
5231
NeilBrown657390d2005-08-26 18:34:16 -07005232 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005233 /* may be over-ridden by personality */
5234 mddev->resync_max_sectors = mddev->dev_sectors;
5235
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005236 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237
NeilBrown0f9552b52009-12-30 12:08:50 +11005238 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005239 mddev->ro = 2; /* read-only, but switch on first write */
5240
NeilBrown36d091f2014-12-15 12:56:58 +11005241 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005242 if (err)
5243 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005244 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005245 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5246 " but 'external_size' not in effect?\n", __func__);
5247 printk(KERN_ERR
5248 "md: invalid array_size %llu > default size %llu\n",
5249 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005250 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005251 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005252 }
NeilBrown36d091f2014-12-15 12:56:58 +11005253 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005254 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005255 struct bitmap *bitmap;
5256
5257 bitmap = bitmap_create(mddev, -1);
5258 if (IS_ERR(bitmap)) {
5259 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005260 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5261 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005262 } else
5263 mddev->bitmap = bitmap;
5264
NeilBrownb15c2e52006-01-06 00:20:16 -08005265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005267 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005268 if (mddev->private)
5269 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005270 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005271 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005272 bitmap_destroy(mddev);
5273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 }
NeilBrown5c675f82014-12-15 12:56:56 +11005275 if (mddev->queue) {
5276 mddev->queue->backing_dev_info.congested_data = mddev;
5277 mddev->queue->backing_dev_info.congested_fn = md_congested;
5278 }
NeilBrown36d091f2014-12-15 12:56:58 +11005279 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005280 if (mddev->kobj.sd &&
5281 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005282 printk(KERN_WARNING
5283 "md: cannot register extra attributes for %s\n",
5284 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005285 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005286 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005287 mddev->ro = 0;
5288
NeilBrownf72ffdd2014-09-30 14:23:59 +10005289 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005290 atomic_set(&mddev->max_corr_read_errors,
5291 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005293 if (mddev_is_clustered(mddev))
5294 mddev->safemode_delay = 0;
5295 else
5296 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005298 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005299 spin_lock(&mddev->lock);
5300 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005301 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005302 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005303 if (rdev->raid_disk >= 0)
5304 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005305 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005306
NeilBrowna4a3d262015-07-17 11:57:30 +10005307 if (mddev->degraded && !mddev->ro)
5308 /* This ensures that recovering status is reported immediately
5309 * via sysfs - until a lack of spares is confirmed.
5310 */
5311 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005313
NeilBrown7a0a5352013-08-27 16:28:23 +10005314 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005315 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
NeilBrownd7603b72006-01-06 00:20:30 -08005317 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005318 sysfs_notify_dirent_safe(mddev->sysfs_state);
5319 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005320 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 return 0;
5322}
NeilBrown390ee602010-06-01 19:37:27 +10005323EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
NeilBrownfd01b882011-10-11 16:47:53 +11005325static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005326{
5327 int err;
5328
5329 err = md_run(mddev);
5330 if (err)
5331 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005332 err = bitmap_load(mddev);
5333 if (err) {
5334 bitmap_destroy(mddev);
5335 goto out;
5336 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005337
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005338 if (mddev_is_clustered(mddev))
5339 md_allow_write(mddev);
5340
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005341 md_wakeup_thread(mddev->thread);
5342 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5343
NeilBrownfe60b012010-03-29 11:10:42 +11005344 set_capacity(mddev->gendisk, mddev->array_sectors);
5345 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005346 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005347 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5348out:
5349 return err;
5350}
5351
NeilBrownfd01b882011-10-11 16:47:53 +11005352static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
5354 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
Andre Noll80fab1d2008-07-11 22:02:21 +10005356 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005358 return -ENXIO;
5359 if (!mddev->pers)
5360 return -EINVAL;
5361 if (!mddev->ro)
5362 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005363 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5364 struct md_rdev *rdev;
5365 bool has_journal = false;
5366
5367 rcu_read_lock();
5368 rdev_for_each_rcu(rdev, mddev) {
5369 if (test_bit(Journal, &rdev->flags) &&
5370 !test_bit(Faulty, &rdev->flags)) {
5371 has_journal = true;
5372 break;
5373 }
5374 }
5375 rcu_read_unlock();
5376
5377 /* Don't restart rw with journal missing/faulty */
5378 if (!has_journal)
5379 return -EINVAL;
5380 }
5381
Andre Noll80fab1d2008-07-11 22:02:21 +10005382 mddev->safemode = 0;
5383 mddev->ro = 0;
5384 set_disk_ro(disk, 0);
5385 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5386 mdname(mddev));
5387 /* Kick recovery or resync if necessary */
5388 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5389 md_wakeup_thread(mddev->thread);
5390 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005391 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393}
5394
NeilBrownfd01b882011-10-11 16:47:53 +11005395static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005396{
5397 mddev->array_sectors = 0;
5398 mddev->external_size = 0;
5399 mddev->dev_sectors = 0;
5400 mddev->raid_disks = 0;
5401 mddev->recovery_cp = 0;
5402 mddev->resync_min = 0;
5403 mddev->resync_max = MaxSector;
5404 mddev->reshape_position = MaxSector;
5405 mddev->external = 0;
5406 mddev->persistent = 0;
5407 mddev->level = LEVEL_NONE;
5408 mddev->clevel[0] = 0;
5409 mddev->flags = 0;
5410 mddev->ro = 0;
5411 mddev->metadata_type[0] = 0;
5412 mddev->chunk_sectors = 0;
5413 mddev->ctime = mddev->utime = 0;
5414 mddev->layout = 0;
5415 mddev->max_disks = 0;
5416 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005417 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005418 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005419 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005420 mddev->new_level = LEVEL_NONE;
5421 mddev->new_layout = 0;
5422 mddev->new_chunk_sectors = 0;
5423 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005424 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005425 mddev->suspend_lo = mddev->suspend_hi = 0;
5426 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5427 mddev->recovery = 0;
5428 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005429 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005430 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005431 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005432 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005433 mddev->bitmap_info.offset = 0;
5434 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005435 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005436 mddev->bitmap_info.chunksize = 0;
5437 mddev->bitmap_info.daemon_sleep = 0;
5438 mddev->bitmap_info.max_write_behind = 0;
5439}
5440
NeilBrownfd01b882011-10-11 16:47:53 +11005441static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005442{
NeilBrown6b6204e2013-05-09 09:48:30 +10005443 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005444 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005445 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005446 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005447 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005448 }
5449
5450 del_timer_sync(&mddev->safemode_timer);
5451
5452 bitmap_flush(mddev);
5453 md_super_wait(mddev);
5454
NeilBrownb6d428c2013-04-24 11:42:42 +10005455 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005456 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5457 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005458 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005459 if (!mddev_is_clustered(mddev))
5460 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005461 md_update_sb(mddev, 1);
5462 }
5463}
NeilBrowndefad612011-01-14 09:14:33 +11005464
NeilBrownfd01b882011-10-11 16:47:53 +11005465void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005466{
NeilBrown29f097c2013-11-14 17:54:51 +11005467 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005468 __md_stop_writes(mddev);
5469 mddev_unlock(mddev);
5470}
NeilBrown390ee602010-06-01 19:37:27 +10005471EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005472
NeilBrown5aa61f42014-12-15 12:56:57 +11005473static void mddev_detach(struct mddev *mddev)
5474{
5475 struct bitmap *bitmap = mddev->bitmap;
5476 /* wait for behind writes to complete */
5477 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5478 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5479 mdname(mddev));
5480 /* need to kick something here to make sure I/O goes? */
5481 wait_event(bitmap->behind_wait,
5482 atomic_read(&bitmap->behind_writes) == 0);
5483 }
NeilBrown36d091f2014-12-15 12:56:58 +11005484 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005485 mddev->pers->quiesce(mddev, 1);
5486 mddev->pers->quiesce(mddev, 0);
5487 }
5488 md_unregister_thread(&mddev->thread);
5489 if (mddev->queue)
5490 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5491}
5492
NeilBrown5eff3c42012-11-19 10:47:48 +11005493static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005494{
NeilBrown36d091f2014-12-15 12:56:58 +11005495 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005496 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005497 /* Ensure ->event_work is done */
5498 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005499 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005500 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005501 spin_unlock(&mddev->lock);
5502 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005503 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005504 if (pers->sync_request && mddev->to_remove == NULL)
5505 mddev->to_remove = &md_redundancy_group;
5506 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005507 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005508}
NeilBrown5eff3c42012-11-19 10:47:48 +11005509
5510void md_stop(struct mddev *mddev)
5511{
5512 /* stop the array and free an attached data structures.
5513 * This is called from dm-raid
5514 */
5515 __md_stop(mddev);
5516 bitmap_destroy(mddev);
5517 if (mddev->bio_set)
5518 bioset_free(mddev->bio_set);
5519}
5520
NeilBrown390ee602010-06-01 19:37:27 +10005521EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005522
NeilBrowna05b7ea2012-07-19 15:59:18 +10005523static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005524{
5525 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005526 int did_freeze = 0;
5527
5528 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5529 did_freeze = 1;
5530 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5531 md_wakeup_thread(mddev->thread);
5532 }
NeilBrownf851b602014-12-11 10:02:10 +11005533 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005534 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005535 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005536 /* Thread might be blocked waiting for metadata update
5537 * which will now never happen */
5538 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005539
NeilBrown88724bf2015-09-24 14:00:51 +10005540 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5541 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005542 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005543 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5544 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005545 wait_event(mddev->sb_wait,
5546 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005547 mddev_lock_nointr(mddev);
5548
NeilBrowna4bd82d2010-03-29 13:23:10 +11005549 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005550 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005551 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005552 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005553 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005554 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005555 if (did_freeze) {
5556 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005557 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005558 md_wakeup_thread(mddev->thread);
5559 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005560 err = -EBUSY;
5561 goto out;
5562 }
5563 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005564 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005565
5566 err = -ENXIO;
5567 if (mddev->ro==1)
5568 goto out;
5569 mddev->ro = 1;
5570 set_disk_ro(mddev->gendisk, 1);
5571 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005572 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5573 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005574 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005575 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005576 }
5577out:
5578 mutex_unlock(&mddev->open_mutex);
5579 return err;
5580}
5581
NeilBrown9e653b62006-06-26 00:27:58 -07005582/* mode:
5583 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005584 * 2 - stop but do not disassemble array
5585 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005586static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005587 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005590 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005591 int did_freeze = 0;
5592
5593 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5594 did_freeze = 1;
5595 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5596 md_wakeup_thread(mddev->thread);
5597 }
NeilBrownf851b602014-12-11 10:02:10 +11005598 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005599 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005600 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005601 /* Thread might be blocked waiting for metadata update
5602 * which will now never happen */
5603 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005604
NeilBrown30b8feb2013-11-14 15:16:17 +11005605 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005606 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5607 !test_bit(MD_RECOVERY_RUNNING,
5608 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005609 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
NeilBrownc8c00a62009-08-10 12:50:52 +10005611 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005612 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005613 mddev->sysfs_active ||
5614 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005615 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005616 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005617 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005618 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005619 if (did_freeze) {
5620 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005621 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005622 md_wakeup_thread(mddev->thread);
5623 }
NeilBrown260fa032013-08-27 16:44:13 +10005624 return -EBUSY;
5625 }
NeilBrown6e17b022010-08-07 21:41:19 +10005626 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005627 if (mddev->ro)
5628 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005629
NeilBrowndefad612011-01-14 09:14:33 +11005630 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005631 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005632 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005633
NeilBrowna4bd82d2010-03-29 13:23:10 +11005634 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005635 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005636
NeilBrowndafb20f2012-03-19 12:46:39 +11005637 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005638 if (rdev->raid_disk >= 0)
5639 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005640
NeilBrowna4bd82d2010-03-29 13:23:10 +11005641 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005642 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005643 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005644 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005645
NeilBrowna4bd82d2010-03-29 13:23:10 +11005646 if (mddev->ro)
5647 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005648 } else
5649 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 /*
5651 * Free resources if final stop
5652 */
NeilBrown9e653b62006-06-26 00:27:58 -07005653 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5655
NeilBrown978f9462006-02-02 14:28:05 -08005656 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005657 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005658 struct file *f = mddev->bitmap_info.file;
5659 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005660 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005661 spin_unlock(&mddev->lock);
5662 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005663 }
NeilBrownc3d97142009-12-14 12:49:52 +11005664 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005665
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 export_array(mddev);
5667
NeilBrown6177b472010-03-29 11:37:13 +11005668 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005669 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005670 if (mddev->hold_active == UNTIL_STOP)
5671 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005672 }
NeilBrownd7603b72006-01-06 00:20:30 -08005673 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005674 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676}
5677
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005678#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005679static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680{
NeilBrown3cb03002011-10-11 16:45:26 +11005681 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 int err;
5683
NeilBrowna757e642005-04-16 15:26:42 -07005684 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
5687 printk(KERN_INFO "md: running: ");
5688
NeilBrowndafb20f2012-03-19 12:46:39 +11005689 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 char b[BDEVNAME_SIZE];
5691 printk("<%s>", bdevname(rdev->bdev,b));
5692 }
5693 printk("\n");
5694
NeilBrownd710e132008-10-13 11:55:12 +11005695 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 if (err) {
5697 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005698 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 }
5700}
5701
5702/*
5703 * lets try to run arrays based on all disks that have arrived
5704 * until now. (those are in pending_raid_disks)
5705 *
5706 * the method: pick the first pending disk, collect all disks with
5707 * the same UUID, remove all from the pending list and put them into
5708 * the 'same_array' list. Then order this list based on superblock
5709 * update time (freshest comes first), kick out 'old' disks and
5710 * compare superblocks. If everything's fine then run it.
5711 *
5712 * If "unit" is allocated, then bump its reference count
5713 */
5714static void autorun_devices(int part)
5715{
NeilBrown3cb03002011-10-11 16:45:26 +11005716 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005717 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 char b[BDEVNAME_SIZE];
5719
5720 printk(KERN_INFO "md: autorun ...\n");
5721 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005722 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005724 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005726 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
5728 printk(KERN_INFO "md: considering %s ...\n",
5729 bdevname(rdev0->bdev,b));
5730 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005731 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 if (super_90_load(rdev, rdev0, 0) >= 0) {
5733 printk(KERN_INFO "md: adding %s ...\n",
5734 bdevname(rdev->bdev,b));
5735 list_move(&rdev->same_set, &candidates);
5736 }
5737 /*
5738 * now we have a set of devices, with all of them having
5739 * mostly sane superblocks. It's time to allocate the
5740 * mddev.
5741 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005742 if (part) {
5743 dev = MKDEV(mdp_major,
5744 rdev0->preferred_minor << MdpMinorShift);
5745 unit = MINOR(dev) >> MdpMinorShift;
5746 } else {
5747 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5748 unit = MINOR(dev);
5749 }
5750 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5752 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5753 break;
5754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
5756 md_probe(dev, NULL, NULL);
5757 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005758 if (!mddev || !mddev->gendisk) {
5759 if (mddev)
5760 mddev_put(mddev);
5761 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 "md: cannot allocate memory for md drive.\n");
5763 break;
5764 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005765 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 printk(KERN_WARNING "md: %s locked, cannot run\n",
5767 mdname(mddev));
5768 else if (mddev->raid_disks || mddev->major_version
5769 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005770 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 "md: %s already running, cannot run %s\n",
5772 mdname(mddev), bdevname(rdev0->bdev,b));
5773 mddev_unlock(mddev);
5774 } else {
5775 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005776 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005777 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 list_del_init(&rdev->same_set);
5779 if (bind_rdev_to_array(rdev, mddev))
5780 export_rdev(rdev);
5781 }
5782 autorun_array(mddev);
5783 mddev_unlock(mddev);
5784 }
5785 /* on success, candidates will be empty, on error
5786 * it won't...
5787 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005788 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005789 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 mddev_put(mddev);
5793 }
5794 printk(KERN_INFO "md: ... autorun DONE.\n");
5795}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005796#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
NeilBrownf72ffdd2014-09-30 14:23:59 +10005798static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799{
5800 mdu_version_t ver;
5801
5802 ver.major = MD_MAJOR_VERSION;
5803 ver.minor = MD_MINOR_VERSION;
5804 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5805
5806 if (copy_to_user(arg, &ver, sizeof(ver)))
5807 return -EFAULT;
5808
5809 return 0;
5810}
5811
NeilBrownf72ffdd2014-09-30 14:23:59 +10005812static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813{
5814 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005815 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005816 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
NeilBrown1ca69c42012-10-11 13:37:33 +11005818 nr = working = insync = failed = spare = 0;
5819 rcu_read_lock();
5820 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005822 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 failed++;
5824 else {
5825 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005826 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005827 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 else
5829 spare++;
5830 }
5831 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005832 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
5834 info.major_version = mddev->major_version;
5835 info.minor_version = mddev->minor_version;
5836 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005837 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005839 info.size = mddev->dev_sectors / 2;
5840 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005841 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 info.nr_disks = nr;
5843 info.raid_disks = mddev->raid_disks;
5844 info.md_minor = mddev->md_minor;
5845 info.not_persistent= !mddev->persistent;
5846
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005847 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 info.state = 0;
5849 if (mddev->in_sync)
5850 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005851 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005852 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005853 if (mddev_is_clustered(mddev))
5854 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005855 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 info.working_disks = working;
5857 info.failed_disks = failed;
5858 info.spare_disks = spare;
5859
5860 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005861 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
5863 if (copy_to_user(arg, &info, sizeof(info)))
5864 return -EFAULT;
5865
5866 return 0;
5867}
5868
NeilBrownf72ffdd2014-09-30 14:23:59 +10005869static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005870{
5871 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005872 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005873 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005874
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005875 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005876 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005877 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005878
NeilBrown32a76272005-06-21 17:17:14 -07005879 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005880 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005881 /* bitmap enabled */
5882 if (mddev->bitmap_info.file) {
5883 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5884 sizeof(file->pathname));
5885 if (IS_ERR(ptr))
5886 err = PTR_ERR(ptr);
5887 else
5888 memmove(file->pathname, ptr,
5889 sizeof(file->pathname)-(ptr-file->pathname));
5890 }
NeilBrown4af1a042014-12-15 12:57:00 +11005891 spin_unlock(&mddev->lock);
5892
5893 if (err == 0 &&
5894 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005895 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005896
NeilBrown32a76272005-06-21 17:17:14 -07005897 kfree(file);
5898 return err;
5899}
5900
NeilBrownf72ffdd2014-09-30 14:23:59 +10005901static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902{
5903 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005904 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
5906 if (copy_from_user(&info, arg, sizeof(info)))
5907 return -EFAULT;
5908
NeilBrown1ca69c42012-10-11 13:37:33 +11005909 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005910 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 if (rdev) {
5912 info.major = MAJOR(rdev->bdev->bd_dev);
5913 info.minor = MINOR(rdev->bdev->bd_dev);
5914 info.raid_disk = rdev->raid_disk;
5915 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005916 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005918 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 info.state |= (1<<MD_DISK_ACTIVE);
5920 info.state |= (1<<MD_DISK_SYNC);
5921 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005922 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005923 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005924 if (test_bit(WriteMostly, &rdev->flags))
5925 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 } else {
5927 info.major = info.minor = 0;
5928 info.raid_disk = -1;
5929 info.state = (1<<MD_DISK_REMOVED);
5930 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005931 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932
5933 if (copy_to_user(arg, &info, sizeof(info)))
5934 return -EFAULT;
5935
5936 return 0;
5937}
5938
NeilBrownf72ffdd2014-09-30 14:23:59 +10005939static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940{
5941 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005942 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 dev_t dev = MKDEV(info->major,info->minor);
5944
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005945 if (mddev_is_clustered(mddev) &&
5946 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005947 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005948 mdname(mddev));
5949 return -EINVAL;
5950 }
5951
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5953 return -EOVERFLOW;
5954
5955 if (!mddev->raid_disks) {
5956 int err;
5957 /* expecting a device which has a superblock */
5958 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5959 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005960 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 "md: md_import_device returned %ld\n",
5962 PTR_ERR(rdev));
5963 return PTR_ERR(rdev);
5964 }
5965 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005966 struct md_rdev *rdev0
5967 = list_entry(mddev->disks.next,
5968 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005969 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 .load_super(rdev, rdev0, mddev->minor_version);
5971 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005972 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005974 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 bdevname(rdev0->bdev,b2));
5976 export_rdev(rdev);
5977 return -EINVAL;
5978 }
5979 }
5980 err = bind_rdev_to_array(rdev, mddev);
5981 if (err)
5982 export_rdev(rdev);
5983 return err;
5984 }
5985
5986 /*
5987 * add_new_disk can be used once the array is assembled
5988 * to add "hot spares". They must already have a superblock
5989 * written
5990 */
5991 if (mddev->pers) {
5992 int err;
5993 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005994 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 "%s: personality does not support diskops!\n",
5996 mdname(mddev));
5997 return -EINVAL;
5998 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005999 if (mddev->persistent)
6000 rdev = md_import_device(dev, mddev->major_version,
6001 mddev->minor_version);
6002 else
6003 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006005 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 "md: md_import_device returned %ld\n",
6007 PTR_ERR(rdev));
6008 return PTR_ERR(rdev);
6009 }
NeilBrown1a855a02010-12-09 16:36:28 +11006010 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006011 if (!mddev->persistent) {
6012 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006013 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006014 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006015 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006016 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006017 } else
NeilBrown41158c72005-06-21 17:17:25 -07006018 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006019 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006020 } else
6021 super_types[mddev->major_version].
6022 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006023 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006024 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006025 /* This was a hot-add request, but events doesn't
6026 * match, so reject it.
6027 */
6028 export_rdev(rdev);
6029 return -EINVAL;
6030 }
6031
NeilBrownb2d444d2005-11-08 21:39:31 -08006032 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006033 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6034 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006035 else
6036 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006037
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006038 if (info->state & (1<<MD_DISK_JOURNAL)) {
6039 struct md_rdev *rdev2;
6040 bool has_journal = false;
6041
6042 /* make sure no existing journal disk */
6043 rdev_for_each(rdev2, mddev) {
6044 if (test_bit(Journal, &rdev2->flags)) {
6045 has_journal = true;
6046 break;
6047 }
6048 }
6049 if (has_journal) {
6050 export_rdev(rdev);
6051 return -EBUSY;
6052 }
Song Liubac624f2015-08-13 14:31:55 -07006053 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006054 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006055 /*
6056 * check whether the device shows up in other nodes
6057 */
6058 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006059 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006060 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006061 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006062 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006063 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006064 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006065 export_rdev(rdev);
6066 return err;
6067 }
6068 }
6069 }
6070
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 rdev->raid_disk = -1;
6072 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006073
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 if (err)
6075 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006076
6077 if (mddev_is_clustered(mddev)) {
6078 if (info->state & (1 << MD_DISK_CANDIDATE))
6079 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6080 else {
6081 if (err)
6082 md_cluster_ops->add_new_disk_cancel(mddev);
6083 else
6084 err = add_bound_rdev(rdev);
6085 }
6086
6087 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006088 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006089
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 return err;
6091 }
6092
6093 /* otherwise, add_new_disk is only allowed
6094 * for major_version==0 superblocks
6095 */
6096 if (mddev->major_version != 0) {
6097 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6098 mdname(mddev));
6099 return -EINVAL;
6100 }
6101
6102 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6103 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006104 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006106 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 "md: error, md_import_device() returned %ld\n",
6108 PTR_ERR(rdev));
6109 return PTR_ERR(rdev);
6110 }
6111 rdev->desc_nr = info->number;
6112 if (info->raid_disk < mddev->raid_disks)
6113 rdev->raid_disk = info->raid_disk;
6114 else
6115 rdev->raid_disk = -1;
6116
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006118 if (info->state & (1<<MD_DISK_SYNC))
6119 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006121 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6122 set_bit(WriteMostly, &rdev->flags);
6123
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 if (!mddev->persistent) {
6125 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006126 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6127 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006128 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006129 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
NeilBrown2bf071b2006-01-06 00:20:55 -08006131 err = bind_rdev_to_array(rdev, mddev);
6132 if (err) {
6133 export_rdev(rdev);
6134 return err;
6135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 }
6137
6138 return 0;
6139}
6140
NeilBrownf72ffdd2014-09-30 14:23:59 +10006141static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142{
6143 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006144 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 rdev = find_rdev(mddev, dev);
6147 if (!rdev)
6148 return -ENXIO;
6149
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006150 if (rdev->raid_disk < 0)
6151 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006152
NeilBrown3ea8929d2013-04-24 11:42:41 +10006153 clear_bit(Blocked, &rdev->flags);
6154 remove_and_add_spares(mddev, rdev);
6155
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 if (rdev->raid_disk >= 0)
6157 goto busy;
6158
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006159kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006160 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006161 md_cluster_ops->remove_disk(mddev, rdev);
6162
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006163 md_kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07006164 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006165 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
6167 return 0;
6168busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006169 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 bdevname(rdev->bdev,b), mdname(mddev));
6171 return -EBUSY;
6172}
6173
NeilBrownf72ffdd2014-09-30 14:23:59 +10006174static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175{
6176 char b[BDEVNAME_SIZE];
6177 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006178 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179
6180 if (!mddev->pers)
6181 return -ENODEV;
6182
6183 if (mddev->major_version != 0) {
6184 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6185 " version-0 superblocks.\n",
6186 mdname(mddev));
6187 return -EINVAL;
6188 }
6189 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006190 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 "%s: personality does not support diskops!\n",
6192 mdname(mddev));
6193 return -EINVAL;
6194 }
6195
NeilBrownd710e132008-10-13 11:55:12 +11006196 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006198 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 "md: error, md_import_device() returned %ld\n",
6200 PTR_ERR(rdev));
6201 return -EINVAL;
6202 }
6203
6204 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006205 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006207 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
NeilBrown8190e752009-06-18 08:48:58 +10006209 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
NeilBrownb2d444d2005-11-08 21:39:31 -08006211 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006212 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 "md: can not hot-add faulty %s disk to %s!\n",
6214 bdevname(rdev->bdev,b), mdname(mddev));
6215 err = -EINVAL;
6216 goto abort_export;
6217 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006218
NeilBrownb2d444d2005-11-08 21:39:31 -08006219 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006221 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006222 err = bind_rdev_to_array(rdev, mddev);
6223 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006224 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
6226 /*
6227 * The rest should better be atomic, we can have disk failures
6228 * noticed in interrupt contexts ...
6229 */
6230
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 rdev->raid_disk = -1;
6232
NeilBrown850b2b422006-10-03 01:15:46 -07006233 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 /*
6235 * Kick recovery, maybe this spare has to be added to the
6236 * array immediately.
6237 */
6238 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6239 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006240 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 return 0;
6242
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243abort_export:
6244 export_rdev(rdev);
6245 return err;
6246}
6247
NeilBrownfd01b882011-10-11 16:47:53 +11006248static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006249{
NeilBrown035328c2014-04-09 12:25:40 +10006250 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006251
NeilBrown36fa3062005-09-09 16:23:45 -07006252 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006253 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006254 return -EBUSY;
6255 if (mddev->recovery || mddev->sync_thread)
6256 return -EBUSY;
6257 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006258 }
6259
NeilBrown36fa3062005-09-09 16:23:45 -07006260 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006261 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006262 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006263
NeilBrown1e594bb2014-12-15 12:57:00 +11006264 if (mddev->bitmap || mddev->bitmap_info.file)
6265 return -EEXIST; /* cannot add when bitmap is present */
6266 f = fget(fd);
6267
6268 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006269 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6270 mdname(mddev));
6271 return -EBADF;
6272 }
6273
NeilBrown1e594bb2014-12-15 12:57:00 +11006274 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006275 if (!S_ISREG(inode->i_mode)) {
6276 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6277 mdname(mddev));
6278 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006279 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006280 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6281 mdname(mddev));
6282 err = -EBADF;
6283 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006284 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6285 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006286 err = -EBUSY;
6287 }
6288 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006289 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006290 return err;
6291 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006292 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006293 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006294 } else if (mddev->bitmap == NULL)
6295 return -ENOENT; /* cannot remove what isn't there */
6296 err = 0;
6297 if (mddev->pers) {
6298 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006299 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006300 struct bitmap *bitmap;
6301
6302 bitmap = bitmap_create(mddev, -1);
6303 if (!IS_ERR(bitmap)) {
6304 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006305 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006306 } else
6307 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006308 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006309 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006310 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006311 fd = -1; /* make sure to put the file */
6312 }
NeilBrown36fa3062005-09-09 16:23:45 -07006313 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006314 }
6315 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006316 struct file *f = mddev->bitmap_info.file;
6317 if (f) {
6318 spin_lock(&mddev->lock);
6319 mddev->bitmap_info.file = NULL;
6320 spin_unlock(&mddev->lock);
6321 fput(f);
6322 }
NeilBrown36fa3062005-09-09 16:23:45 -07006323 }
6324
NeilBrown32a76272005-06-21 17:17:14 -07006325 return err;
6326}
6327
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328/*
6329 * set_array_info is used two different ways
6330 * The original usage is when creating a new array.
6331 * In this usage, raid_disks is > 0 and it together with
6332 * level, size, not_persistent,layout,chunksize determine the
6333 * shape of the array.
6334 * This will always create an array with a type-0.90.0 superblock.
6335 * The newer usage is when assembling an array.
6336 * In this case raid_disks will be 0, and the major_version field is
6337 * use to determine which style super-blocks are to be found on the devices.
6338 * The minor and patch _version numbers are also kept incase the
6339 * super_block handler wishes to interpret them.
6340 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006341static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342{
6343
6344 if (info->raid_disks == 0) {
6345 /* just setting version number for superblock loading */
6346 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006347 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 super_types[info->major_version].name == NULL) {
6349 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006350 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 "md: superblock version %d not known\n",
6352 info->major_version);
6353 return -EINVAL;
6354 }
6355 mddev->major_version = info->major_version;
6356 mddev->minor_version = info->minor_version;
6357 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006358 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006359 /* ensure mddev_put doesn't delete this now that there
6360 * is some minimal configuration.
6361 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006362 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 return 0;
6364 }
6365 mddev->major_version = MD_MAJOR_VERSION;
6366 mddev->minor_version = MD_MINOR_VERSION;
6367 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006368 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369
6370 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006371 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006372 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 mddev->raid_disks = info->raid_disks;
6374 /* don't set md_minor, it is determined by which /dev/md* was
6375 * openned
6376 */
6377 if (info->state & (1<<MD_SB_CLEAN))
6378 mddev->recovery_cp = MaxSector;
6379 else
6380 mddev->recovery_cp = 0;
6381 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006382 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
6384 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006385 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
6387 mddev->max_disks = MD_SB_DISKS;
6388
NeilBrowne6910632008-02-06 01:39:51 -08006389 if (mddev->persistent)
6390 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006391 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
NeilBrownc3d97142009-12-14 12:49:52 +11006393 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006394 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006395 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006396
NeilBrownf6705572006-03-27 01:18:11 -08006397 mddev->reshape_position = MaxSector;
6398
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 /*
6400 * Generate a 128 bit UUID
6401 */
6402 get_random_bytes(mddev->uuid, 16);
6403
NeilBrownf6705572006-03-27 01:18:11 -08006404 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006405 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006406 mddev->new_layout = mddev->layout;
6407 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006408 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006409
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 return 0;
6411}
6412
NeilBrownfd01b882011-10-11 16:47:53 +11006413void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006414{
Dan Williamsb522adc2009-03-31 15:00:31 +11006415 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6416
6417 if (mddev->external_size)
6418 return;
6419
Dan Williams1f403622009-03-31 14:59:03 +11006420 mddev->array_sectors = array_sectors;
6421}
6422EXPORT_SYMBOL(md_set_array_sectors);
6423
NeilBrownfd01b882011-10-11 16:47:53 +11006424static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006425{
NeilBrown3cb03002011-10-11 16:45:26 +11006426 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006427 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006428 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006429
6430 if (mddev->pers->resize == NULL)
6431 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006432 /* The "num_sectors" is the number of sectors of each device that
6433 * is used. This can only make sense for arrays with redundancy.
6434 * linear and raid0 always use whatever space is available. We can only
6435 * consider changing this number if no resync or reconstruction is
6436 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006437 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006438 * of each device. If num_sectors is zero, we find the largest size
6439 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006440 */
NeilBrownf851b602014-12-11 10:02:10 +11006441 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6442 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006443 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006444 if (mddev->ro)
6445 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006446
NeilBrowndafb20f2012-03-19 12:46:39 +11006447 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006448 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006449
Andre Nolld71f9f82008-07-11 22:02:22 +10006450 if (fit && (num_sectors == 0 || num_sectors > avail))
6451 num_sectors = avail;
6452 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006453 return -ENOSPC;
6454 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006455 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006456 if (!rv)
6457 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006458 return rv;
6459}
6460
NeilBrownfd01b882011-10-11 16:47:53 +11006461static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006462{
6463 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006464 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006465 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006466 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006467 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006468 if (mddev->ro)
6469 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006470 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006471 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006472 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006473 if (mddev->sync_thread ||
6474 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6475 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006476 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006477
6478 rdev_for_each(rdev, mddev) {
6479 if (mddev->raid_disks < raid_disks &&
6480 rdev->data_offset < rdev->new_data_offset)
6481 return -EINVAL;
6482 if (mddev->raid_disks > raid_disks &&
6483 rdev->data_offset > rdev->new_data_offset)
6484 return -EINVAL;
6485 }
6486
NeilBrown63c70c42006-03-27 01:18:13 -08006487 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006488 if (mddev->delta_disks < 0)
6489 mddev->reshape_backwards = 1;
6490 else if (mddev->delta_disks > 0)
6491 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006492
6493 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006494 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006495 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006496 mddev->reshape_backwards = 0;
6497 }
NeilBrownda943b992006-01-06 00:20:54 -08006498 return rv;
6499}
6500
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501/*
6502 * update_array_info is used to change the configuration of an
6503 * on-line array.
6504 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6505 * fields in the info are checked against the array.
6506 * Any differences that cannot be handled will cause an error.
6507 * Normally, only one change can be managed at a time.
6508 */
NeilBrownfd01b882011-10-11 16:47:53 +11006509static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510{
6511 int rv = 0;
6512 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006513 int state = 0;
6514
6515 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006516 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006517 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518
6519 if (mddev->major_version != info->major_version ||
6520 mddev->minor_version != info->minor_version ||
6521/* mddev->patch_version != info->patch_version || */
6522 mddev->ctime != info->ctime ||
6523 mddev->level != info->level ||
6524/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006525 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006526 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006527 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6528 ((state^info->state) & 0xfffffe00)
6529 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 return -EINVAL;
6531 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006532 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6533 cnt++;
6534 if (mddev->raid_disks != info->raid_disks)
6535 cnt++;
6536 if (mddev->layout != info->layout)
6537 cnt++;
6538 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6539 cnt++;
6540 if (cnt == 0)
6541 return 0;
6542 if (cnt > 1)
6543 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544
6545 if (mddev->layout != info->layout) {
6546 /* Change layout
6547 * we don't need to do anything at the md level, the
6548 * personality will take care of it all.
6549 */
NeilBrown50ac1682009-06-18 08:47:55 +10006550 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006552 else {
6553 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006554 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006555 if (rv)
6556 mddev->new_layout = mddev->layout;
6557 return rv;
6558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006560 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006561 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562
NeilBrownda943b992006-01-06 00:20:54 -08006563 if (mddev->raid_disks != info->raid_disks)
6564 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565
NeilBrown36fa3062005-09-09 16:23:45 -07006566 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006567 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6568 rv = -EINVAL;
6569 goto err;
6570 }
6571 if (mddev->recovery || mddev->sync_thread) {
6572 rv = -EBUSY;
6573 goto err;
6574 }
NeilBrown36fa3062005-09-09 16:23:45 -07006575 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006576 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006577 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006578 if (mddev->bitmap) {
6579 rv = -EEXIST;
6580 goto err;
6581 }
6582 if (mddev->bitmap_info.default_offset == 0) {
6583 rv = -EINVAL;
6584 goto err;
6585 }
NeilBrownc3d97142009-12-14 12:49:52 +11006586 mddev->bitmap_info.offset =
6587 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006588 mddev->bitmap_info.space =
6589 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006590 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006591 bitmap = bitmap_create(mddev, -1);
6592 if (!IS_ERR(bitmap)) {
6593 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006594 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006595 } else
6596 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006597 if (rv)
6598 bitmap_destroy(mddev);
6599 mddev->pers->quiesce(mddev, 0);
6600 } else {
6601 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006602 if (!mddev->bitmap) {
6603 rv = -ENOENT;
6604 goto err;
6605 }
6606 if (mddev->bitmap->storage.file) {
6607 rv = -EINVAL;
6608 goto err;
6609 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006610 if (mddev->bitmap_info.nodes) {
6611 /* hold PW on all the bitmap lock */
6612 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6613 printk("md: can't change bitmap to none since the"
6614 " array is in use by more than one node\n");
6615 rv = -EPERM;
6616 md_cluster_ops->unlock_all_bitmaps(mddev);
6617 goto err;
6618 }
6619
6620 mddev->bitmap_info.nodes = 0;
6621 md_cluster_ops->leave(mddev);
6622 }
NeilBrown36fa3062005-09-09 16:23:45 -07006623 mddev->pers->quiesce(mddev, 1);
6624 bitmap_destroy(mddev);
6625 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006626 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006627 }
6628 }
NeilBrown850b2b422006-10-03 01:15:46 -07006629 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006630 return rv;
6631err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 return rv;
6633}
6634
NeilBrownfd01b882011-10-11 16:47:53 +11006635static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636{
NeilBrown3cb03002011-10-11 16:45:26 +11006637 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006638 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
6640 if (mddev->pers == NULL)
6641 return -ENODEV;
6642
NeilBrown1ca69c42012-10-11 13:37:33 +11006643 rcu_read_lock();
6644 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006646 err = -ENODEV;
6647 else {
6648 md_error(mddev, rdev);
6649 if (!test_bit(Faulty, &rdev->flags))
6650 err = -EBUSY;
6651 }
6652 rcu_read_unlock();
6653 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654}
6655
Andre Noll2f9618c2008-04-25 18:57:58 +02006656/*
6657 * We have a problem here : there is no easy way to give a CHS
6658 * virtual geometry. We currently pretend that we have a 2 heads
6659 * 4 sectors (with a BIG number of cylinders...). This drives
6660 * dosfs just mad... ;-)
6661 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006662static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6663{
NeilBrownfd01b882011-10-11 16:47:53 +11006664 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006665
6666 geo->heads = 2;
6667 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006668 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006669 return 0;
6670}
6671
Nicolas Schichancb335f82014-01-15 16:58:52 +01006672static inline bool md_ioctl_valid(unsigned int cmd)
6673{
6674 switch (cmd) {
6675 case ADD_NEW_DISK:
6676 case BLKROSET:
6677 case GET_ARRAY_INFO:
6678 case GET_BITMAP_FILE:
6679 case GET_DISK_INFO:
6680 case HOT_ADD_DISK:
6681 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006682 case RAID_AUTORUN:
6683 case RAID_VERSION:
6684 case RESTART_ARRAY_RW:
6685 case RUN_ARRAY:
6686 case SET_ARRAY_INFO:
6687 case SET_BITMAP_FILE:
6688 case SET_DISK_FAULTY:
6689 case STOP_ARRAY:
6690 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006691 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006692 return true;
6693 default:
6694 return false;
6695 }
6696}
6697
Al Viroa39907f2008-03-02 10:31:15 -05006698static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 unsigned int cmd, unsigned long arg)
6700{
6701 int err = 0;
6702 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006703 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006704 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705
Nicolas Schichancb335f82014-01-15 16:58:52 +01006706 if (!md_ioctl_valid(cmd))
6707 return -ENOTTY;
6708
NeilBrown506c9e42011-12-23 10:17:26 +11006709 switch (cmd) {
6710 case RAID_VERSION:
6711 case GET_ARRAY_INFO:
6712 case GET_DISK_INFO:
6713 break;
6714 default:
6715 if (!capable(CAP_SYS_ADMIN))
6716 return -EACCES;
6717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718
6719 /*
6720 * Commands dealing with the RAID driver but not any
6721 * particular array:
6722 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006723 switch (cmd) {
6724 case RAID_VERSION:
6725 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006726 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006729 case RAID_AUTORUN:
6730 err = 0;
6731 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006732 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006734 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 }
6736
6737 /*
6738 * Commands creating/starting a new array:
6739 */
6740
Al Viroa39907f2008-03-02 10:31:15 -05006741 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
6743 if (!mddev) {
6744 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006745 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 }
6747
NeilBrown1ca69c42012-10-11 13:37:33 +11006748 /* Some actions do not requires the mutex */
6749 switch (cmd) {
6750 case GET_ARRAY_INFO:
6751 if (!mddev->raid_disks && !mddev->external)
6752 err = -ENODEV;
6753 else
6754 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006755 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006756
6757 case GET_DISK_INFO:
6758 if (!mddev->raid_disks && !mddev->external)
6759 err = -ENODEV;
6760 else
6761 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006762 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006763
6764 case SET_DISK_FAULTY:
6765 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006766 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006767
6768 case GET_BITMAP_FILE:
6769 err = get_bitmap_file(mddev, argp);
6770 goto out;
6771
NeilBrown1ca69c42012-10-11 13:37:33 +11006772 }
6773
NeilBrowna7a3f082012-12-11 13:35:54 +11006774 if (cmd == ADD_NEW_DISK)
6775 /* need to ensure md_delayed_delete() has completed */
6776 flush_workqueue(md_misc_wq);
6777
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006778 if (cmd == HOT_REMOVE_DISK)
6779 /* need to ensure recovery thread has run */
6780 wait_event_interruptible_timeout(mddev->sb_wait,
6781 !test_bit(MD_RECOVERY_NEEDED,
6782 &mddev->flags),
6783 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006784 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6785 /* Need to flush page cache, and ensure no-one else opens
6786 * and writes
6787 */
6788 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006789 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006790 mutex_unlock(&mddev->open_mutex);
6791 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006792 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006793 }
6794 set_bit(MD_STILL_CLOSED, &mddev->flags);
6795 mutex_unlock(&mddev->open_mutex);
6796 sync_blockdev(bdev);
6797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 err = mddev_lock(mddev);
6799 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006800 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6802 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006803 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 }
6805
NeilBrownc02c0ae2012-12-11 13:39:21 +11006806 if (cmd == SET_ARRAY_INFO) {
6807 mdu_array_info_t info;
6808 if (!arg)
6809 memset(&info, 0, sizeof(info));
6810 else if (copy_from_user(&info, argp, sizeof(info))) {
6811 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006812 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006813 }
6814 if (mddev->pers) {
6815 err = update_array_info(mddev, &info);
6816 if (err) {
6817 printk(KERN_WARNING "md: couldn't update"
6818 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006819 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006821 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006822 }
6823 if (!list_empty(&mddev->disks)) {
6824 printk(KERN_WARNING
6825 "md: array %s already has disks!\n",
6826 mdname(mddev));
6827 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006828 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006829 }
6830 if (mddev->raid_disks) {
6831 printk(KERN_WARNING
6832 "md: array %s already initialised!\n",
6833 mdname(mddev));
6834 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006835 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006836 }
6837 err = set_array_info(mddev, &info);
6838 if (err) {
6839 printk(KERN_WARNING "md: couldn't set"
6840 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006841 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006842 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006843 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 }
6845
6846 /*
6847 * Commands querying/configuring an existing array:
6848 */
NeilBrown32a76272005-06-21 17:17:14 -07006849 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006850 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006851 if ((!mddev->raid_disks && !mddev->external)
6852 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6853 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6854 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006856 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 }
6858
6859 /*
6860 * Commands even a read-only array can execute:
6861 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006862 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006863 case RESTART_ARRAY_RW:
6864 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006865 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006866
6867 case STOP_ARRAY:
6868 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006869 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006870
6871 case STOP_ARRAY_RO:
6872 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006873 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006874
NeilBrown3ea8929d2013-04-24 11:42:41 +10006875 case HOT_REMOVE_DISK:
6876 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006877 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006878
NeilBrown7ceb17e2013-04-24 11:42:42 +10006879 case ADD_NEW_DISK:
6880 /* We can support ADD_NEW_DISK on read-only arrays
6881 * on if we are re-adding a preexisting device.
6882 * So require mddev->pers and MD_DISK_SYNC.
6883 */
6884 if (mddev->pers) {
6885 mdu_disk_info_t info;
6886 if (copy_from_user(&info, argp, sizeof(info)))
6887 err = -EFAULT;
6888 else if (!(info.state & (1<<MD_DISK_SYNC)))
6889 /* Need to clear read-only for this */
6890 break;
6891 else
6892 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006893 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006894 }
6895 break;
6896
NeilBrownc02c0ae2012-12-11 13:39:21 +11006897 case BLKROSET:
6898 if (get_user(ro, (int __user *)(arg))) {
6899 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006900 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006901 }
6902 err = -EINVAL;
6903
6904 /* if the bdev is going readonly the value of mddev->ro
6905 * does not matter, no writes are coming
6906 */
6907 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006908 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006909
NeilBrownc02c0ae2012-12-11 13:39:21 +11006910 /* are we are already prepared for writes? */
6911 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006912 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006913
6914 /* transitioning to readauto need only happen for
6915 * arrays that call md_write_start
6916 */
6917 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006919 if (err == 0) {
6920 mddev->ro = 2;
6921 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006922 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006923 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006924 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 }
6926
6927 /*
6928 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006929 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930 */
NeilBrown326eb172014-09-30 15:36:28 +10006931 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006932 if (mddev->ro == 2) {
6933 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006934 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006935 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006936 /* mddev_unlock will wake thread */
6937 /* If a device failed while we were read-only, we
6938 * need to make sure the metadata is updated now.
6939 */
6940 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6941 mddev_unlock(mddev);
6942 wait_event(mddev->sb_wait,
6943 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6944 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006945 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006946 }
NeilBrownf91de922005-11-08 21:39:36 -08006947 } else {
6948 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006949 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 }
6952
NeilBrownc02c0ae2012-12-11 13:39:21 +11006953 switch (cmd) {
6954 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006956 mdu_disk_info_t info;
6957 if (copy_from_user(&info, argp, sizeof(info)))
6958 err = -EFAULT;
6959 else
6960 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006961 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006964 case CLUSTERED_DISK_NACK:
6965 if (mddev_is_clustered(mddev))
6966 md_cluster_ops->new_disk_ack(mddev, false);
6967 else
6968 err = -EINVAL;
6969 goto unlock;
6970
NeilBrownc02c0ae2012-12-11 13:39:21 +11006971 case HOT_ADD_DISK:
6972 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006973 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974
NeilBrownc02c0ae2012-12-11 13:39:21 +11006975 case RUN_ARRAY:
6976 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006977 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
NeilBrownc02c0ae2012-12-11 13:39:21 +11006979 case SET_BITMAP_FILE:
6980 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006981 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006982
NeilBrownc02c0ae2012-12-11 13:39:21 +11006983 default:
6984 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006985 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 }
6987
NeilBrown3adc28d2014-09-30 15:46:41 +10006988unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006989 if (mddev->hold_active == UNTIL_IOCTL &&
6990 err != -EINVAL)
6991 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006993out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 return err;
6995}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006996#ifdef CONFIG_COMPAT
6997static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6998 unsigned int cmd, unsigned long arg)
6999{
7000 switch (cmd) {
7001 case HOT_REMOVE_DISK:
7002 case HOT_ADD_DISK:
7003 case SET_DISK_FAULTY:
7004 case SET_BITMAP_FILE:
7005 /* These take in integer arg, do not convert */
7006 break;
7007 default:
7008 arg = (unsigned long)compat_ptr(arg);
7009 break;
7010 }
7011
7012 return md_ioctl(bdev, mode, cmd, arg);
7013}
7014#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015
Al Viroa39907f2008-03-02 10:31:15 -05007016static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017{
7018 /*
7019 * Succeed if we can lock the mddev, which confirms that
7020 * it isn't being stopped right now.
7021 */
NeilBrownfd01b882011-10-11 16:47:53 +11007022 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 int err;
7024
Yuanhan Liu0c098222012-05-22 13:55:32 +10007025 if (!mddev)
7026 return -ENODEV;
7027
NeilBrownd3374822009-01-09 08:31:10 +11007028 if (mddev->gendisk != bdev->bd_disk) {
7029 /* we are racing with mddev_put which is discarding this
7030 * bd_disk.
7031 */
7032 mddev_put(mddev);
7033 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007034 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007035 /* Then retry the open from the top */
7036 return -ERESTARTSYS;
7037 }
7038 BUG_ON(mddev != bdev->bd_disk->private_data);
7039
NeilBrownc8c00a62009-08-10 12:50:52 +10007040 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 goto out;
7042
7043 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007044 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10007045 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10007046 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007048 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 out:
7050 return err;
7051}
7052
Al Virodb2a1442013-05-05 21:52:57 -04007053static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007055 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007057 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007058 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007061
7062static int md_media_changed(struct gendisk *disk)
7063{
NeilBrownfd01b882011-10-11 16:47:53 +11007064 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007065
7066 return mddev->changed;
7067}
7068
7069static int md_revalidate(struct gendisk *disk)
7070{
NeilBrownfd01b882011-10-11 16:47:53 +11007071 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007072
7073 mddev->changed = 0;
7074 return 0;
7075}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007076static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077{
7078 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007079 .open = md_open,
7080 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007081 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007082#ifdef CONFIG_COMPAT
7083 .compat_ioctl = md_compat_ioctl,
7084#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007085 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007086 .media_changed = md_media_changed,
7087 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088};
7089
NeilBrownf72ffdd2014-09-30 14:23:59 +10007090static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091{
NeilBrown2b8bf342011-10-11 16:48:23 +11007092 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 /*
7095 * md_thread is a 'system-thread', it's priority should be very
7096 * high. We avoid resource deadlocks individually in each
7097 * raid personality. (RAID5 does preallocation) We also use RR and
7098 * the very same RT priority as kswapd, thus we will never get
7099 * into a priority inversion deadlock.
7100 *
7101 * we definitely have to have equal or higher priority than
7102 * bdflush, otherwise bdflush will deadlock if there are too
7103 * many dirty RAID5 blocks.
7104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105
NeilBrown6985c432005-10-19 21:23:47 -07007106 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007107 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108
NeilBrown93588e22005-11-15 00:09:12 -08007109 /* We need to wait INTERRUPTIBLE so that
7110 * we don't add to the load-average.
7111 * That means we need to be sure no signals are
7112 * pending
7113 */
7114 if (signal_pending(current))
7115 flush_signals(current);
7116
7117 wait_event_interruptible_timeout
7118 (thread->wqueue,
7119 test_bit(THREAD_WAKEUP, &thread->flags)
7120 || kthread_should_stop(),
7121 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
NeilBrown6c987912011-01-14 09:13:53 +11007123 clear_bit(THREAD_WAKEUP, &thread->flags);
7124 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007125 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007127
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 return 0;
7129}
7130
NeilBrown2b8bf342011-10-11 16:48:23 +11007131void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132{
7133 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007134 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 set_bit(THREAD_WAKEUP, &thread->flags);
7136 wake_up(&thread->wqueue);
7137 }
7138}
NeilBrown6c144d32014-09-30 16:15:38 +10007139EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
Shaohua Li4ed87312012-10-11 13:34:00 +11007141struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7142 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143{
NeilBrown2b8bf342011-10-11 16:48:23 +11007144 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145
NeilBrown2b8bf342011-10-11 16:48:23 +11007146 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 if (!thread)
7148 return NULL;
7149
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 init_waitqueue_head(&thread->wqueue);
7151
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 thread->run = run;
7153 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007154 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007155 thread->tsk = kthread_run(md_thread, thread,
7156 "%s_%s",
7157 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007158 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007159 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 kfree(thread);
7161 return NULL;
7162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 return thread;
7164}
NeilBrown6c144d32014-09-30 16:15:38 +10007165EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
NeilBrown2b8bf342011-10-11 16:48:23 +11007167void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168{
NeilBrown2b8bf342011-10-11 16:48:23 +11007169 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007170 if (!thread)
7171 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007172 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007173 /* Locking ensures that mddev_unlock does not wake_up a
7174 * non-existent thread
7175 */
7176 spin_lock(&pers_lock);
7177 *threadp = NULL;
7178 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007179
7180 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 kfree(thread);
7182}
NeilBrown6c144d32014-09-30 16:15:38 +10007183EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184
NeilBrownfd01b882011-10-11 16:47:53 +11007185void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186{
NeilBrownb2d444d2005-11-08 21:39:31 -08007187 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007189
NeilBrownde393cd2011-07-28 11:31:48 +10007190 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 return;
7192 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007193 if (mddev->degraded)
7194 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007195 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7197 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7198 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007199 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007200 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007201 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202}
NeilBrown6c144d32014-09-30 16:15:38 +10007203EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204
7205/* seq_file implementation /proc/mdstat */
7206
7207static void status_unused(struct seq_file *seq)
7208{
7209 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007210 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211
7212 seq_printf(seq, "unused devices: ");
7213
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007214 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 char b[BDEVNAME_SIZE];
7216 i++;
7217 seq_printf(seq, "%s ",
7218 bdevname(rdev->bdev,b));
7219 }
7220 if (!i)
7221 seq_printf(seq, "<none>");
7222
7223 seq_printf(seq, "\n");
7224}
7225
NeilBrownf7851be2015-07-02 17:12:58 +10007226static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227{
NeilBrowndd71cf62009-05-07 12:49:35 +10007228 sector_t max_sectors, resync, res;
7229 unsigned long dt, db;
7230 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007231 int scale;
7232 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233
NeilBrownc804cde2012-05-21 09:28:33 +10007234 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7235 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007236 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007238 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
NeilBrownf7851be2015-07-02 17:12:58 +10007240 resync = mddev->curr_resync;
7241 if (resync <= 3) {
7242 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7243 /* Still cleaning up */
7244 resync = max_sectors;
7245 } else
7246 resync -= atomic_read(&mddev->recovery_active);
7247
7248 if (resync == 0) {
7249 if (mddev->recovery_cp < MaxSector) {
7250 seq_printf(seq, "\tresync=PENDING");
7251 return 1;
7252 }
7253 return 0;
7254 }
7255 if (resync < 3) {
7256 seq_printf(seq, "\tresync=DELAYED");
7257 return 1;
7258 }
7259
NeilBrown403df472014-09-30 15:52:29 +10007260 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007261 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007262 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007263 * u32, as those are the requirements for sector_div.
7264 * Thus 'scale' must be at least 10
7265 */
7266 scale = 10;
7267 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007268 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007269 scale++;
7270 }
7271 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007272 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007273
7274 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275 {
NeilBrown4588b422006-03-27 01:18:04 -08007276 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 seq_printf(seq, "[");
7278 for (i = 0; i < x; i++)
7279 seq_printf(seq, "=");
7280 seq_printf(seq, ">");
7281 for (i = 0; i < y; i++)
7282 seq_printf(seq, ".");
7283 seq_printf(seq, "] ");
7284 }
NeilBrown4588b422006-03-27 01:18:04 -08007285 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007286 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7287 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007288 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7289 "check" :
7290 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7291 "resync" : "recovery"))),
7292 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007293 (unsigned long long) resync/2,
7294 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295
7296 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 * dt: time from mark until now
7298 * db: blocks written from mark until now
7299 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007300 *
7301 * rt is a sector_t, so could be 32bit or 64bit.
7302 * So we divide before multiply in case it is 32bit and close
7303 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007304 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007305 * near the end of resync when the number of remaining sectors
7306 * is close to 'db'.
7307 * We then divide rt by 32 after multiplying by db to compensate.
7308 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309 */
7310 dt = ((jiffies - mddev->resync_mark) / HZ);
7311 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007312 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7313 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314
NeilBrowndd71cf62009-05-07 12:49:35 +10007315 rt = max_sectors - resync; /* number of remaining sectors */
7316 sector_div(rt, db/32+1);
7317 rt *= dt;
7318 rt >>= 5;
7319
7320 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7321 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322
NeilBrownff4e8d92006-07-10 04:44:16 -07007323 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007324 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325}
7326
7327static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7328{
7329 struct list_head *tmp;
7330 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007331 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332
7333 if (l >= 0x10000)
7334 return NULL;
7335 if (!l--)
7336 /* header */
7337 return (void*)1;
7338
7339 spin_lock(&all_mddevs_lock);
7340 list_for_each(tmp,&all_mddevs)
7341 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007342 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 mddev_get(mddev);
7344 spin_unlock(&all_mddevs_lock);
7345 return mddev;
7346 }
7347 spin_unlock(&all_mddevs_lock);
7348 if (!l--)
7349 return (void*)2;/* tail */
7350 return NULL;
7351}
7352
7353static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7354{
7355 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007356 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007357
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 ++*pos;
7359 if (v == (void*)2)
7360 return NULL;
7361
7362 spin_lock(&all_mddevs_lock);
7363 if (v == (void*)1)
7364 tmp = all_mddevs.next;
7365 else
7366 tmp = mddev->all_mddevs.next;
7367 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007368 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 else {
7370 next_mddev = (void*)2;
7371 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 spin_unlock(&all_mddevs_lock);
7374
7375 if (v != (void*)1)
7376 mddev_put(mddev);
7377 return next_mddev;
7378
7379}
7380
7381static void md_seq_stop(struct seq_file *seq, void *v)
7382{
NeilBrownfd01b882011-10-11 16:47:53 +11007383 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384
7385 if (mddev && v != (void*)1 && v != (void*)2)
7386 mddev_put(mddev);
7387}
7388
7389static int md_seq_show(struct seq_file *seq, void *v)
7390{
NeilBrownfd01b882011-10-11 16:47:53 +11007391 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007392 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007393 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394
7395 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007396 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 seq_printf(seq, "Personalities : ");
7398 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007399 list_for_each_entry(pers, &pers_list, list)
7400 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401
7402 spin_unlock(&pers_lock);
7403 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007404 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 return 0;
7406 }
7407 if (v == (void*)2) {
7408 status_unused(seq);
7409 return 0;
7410 }
7411
NeilBrown36d091f2014-12-15 12:56:58 +11007412 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7414 seq_printf(seq, "%s : %sactive", mdname(mddev),
7415 mddev->pers ? "" : "in");
7416 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007417 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007419 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007420 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 seq_printf(seq, " %s", mddev->pers->name);
7422 }
7423
Andre Nolldd8ac332009-03-31 14:33:13 +11007424 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007425 rcu_read_lock();
7426 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 char b[BDEVNAME_SIZE];
7428 seq_printf(seq, " %s[%d]",
7429 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007430 if (test_bit(WriteMostly, &rdev->flags))
7431 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007432 if (test_bit(Journal, &rdev->flags))
7433 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007434 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 seq_printf(seq, "(F)");
7436 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007437 }
7438 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007439 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007440 if (test_bit(Replacement, &rdev->flags))
7441 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007442 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007444 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445
7446 if (!list_empty(&mddev->disks)) {
7447 if (mddev->pers)
7448 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007449 (unsigned long long)
7450 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 else
7452 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007453 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007455 if (mddev->persistent) {
7456 if (mddev->major_version != 0 ||
7457 mddev->minor_version != 90) {
7458 seq_printf(seq," super %d.%d",
7459 mddev->major_version,
7460 mddev->minor_version);
7461 }
NeilBrowne6910632008-02-06 01:39:51 -08007462 } else if (mddev->external)
7463 seq_printf(seq, " super external:%s",
7464 mddev->metadata_type);
7465 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007466 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467
7468 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007469 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007470 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007471 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007472 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007473 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007474 }
NeilBrown32a76272005-06-21 17:17:14 -07007475 } else
7476 seq_printf(seq, "\n ");
7477
NeilBrown57148962012-03-19 12:46:40 +11007478 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479
7480 seq_printf(seq, "\n");
7481 }
NeilBrown36d091f2014-12-15 12:56:58 +11007482 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007483
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 return 0;
7485}
7486
Jan Engelhardt110518b2009-05-07 12:49:37 +10007487static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 .start = md_seq_start,
7489 .next = md_seq_next,
7490 .stop = md_seq_stop,
7491 .show = md_seq_show,
7492};
7493
7494static int md_seq_open(struct inode *inode, struct file *file)
7495{
Kay Sieversf1514632011-07-12 20:48:39 +02007496 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 int error;
7498
7499 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007500 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007501 return error;
7502
7503 seq = file->private_data;
7504 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505 return error;
7506}
7507
NeilBrowne2f23b62014-04-09 14:33:51 +10007508static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007509static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7510{
Kay Sieversf1514632011-07-12 20:48:39 +02007511 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007512 int mask;
7513
NeilBrowne2f23b62014-04-09 14:33:51 +10007514 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007515 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007516 poll_wait(filp, &md_event_waiters, wait);
7517
7518 /* always allow read */
7519 mask = POLLIN | POLLRDNORM;
7520
Kay Sieversf1514632011-07-12 20:48:39 +02007521 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007522 mask |= POLLERR | POLLPRI;
7523 return mask;
7524}
7525
Arjan van de Venfa027c22007-02-12 00:55:33 -08007526static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007527 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 .open = md_seq_open,
7529 .read = seq_read,
7530 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007531 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007532 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533};
7534
NeilBrown84fc4b52011-10-11 16:49:58 +11007535int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536{
Chao Yu50bd3772014-09-25 15:28:34 +08007537 printk(KERN_INFO "md: %s personality registered for level %d\n",
7538 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007540 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541 spin_unlock(&pers_lock);
7542 return 0;
7543}
NeilBrown6c144d32014-09-30 16:15:38 +10007544EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545
NeilBrown84fc4b52011-10-11 16:49:58 +11007546int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547{
NeilBrown2604b702006-01-06 00:20:36 -08007548 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007550 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 spin_unlock(&pers_lock);
7552 return 0;
7553}
NeilBrown6c144d32014-09-30 16:15:38 +10007554EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555
NeilBrown6022e752015-08-13 12:32:55 +10007556int register_md_cluster_operations(struct md_cluster_operations *ops,
7557 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007558{
NeilBrown6022e752015-08-13 12:32:55 +10007559 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007560 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007561 if (md_cluster_ops != NULL)
7562 ret = -EALREADY;
7563 else {
7564 md_cluster_ops = ops;
7565 md_cluster_mod = module;
7566 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007567 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007568 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007569}
7570EXPORT_SYMBOL(register_md_cluster_operations);
7571
7572int unregister_md_cluster_operations(void)
7573{
7574 spin_lock(&pers_lock);
7575 md_cluster_ops = NULL;
7576 spin_unlock(&pers_lock);
7577 return 0;
7578}
7579EXPORT_SYMBOL(unregister_md_cluster_operations);
7580
7581int md_setup_cluster(struct mddev *mddev, int nodes)
7582{
7583 int err;
7584
7585 err = request_module("md-cluster");
7586 if (err) {
7587 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007588 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007589 }
7590
7591 spin_lock(&pers_lock);
7592 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7593 spin_unlock(&pers_lock);
7594 return -ENOENT;
7595 }
7596 spin_unlock(&pers_lock);
7597
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007598 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007599}
7600
7601void md_cluster_stop(struct mddev *mddev)
7602{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007603 if (!md_cluster_ops)
7604 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007605 md_cluster_ops->leave(mddev);
7606 module_put(md_cluster_mod);
7607}
7608
NeilBrownfd01b882011-10-11 16:47:53 +11007609static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007611 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007613 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614
7615 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007616 rcu_read_lock();
7617 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007619 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7620 (int)part_stat_read(&disk->part0, sectors[1]) -
7621 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007622 /* sync IO will cause sync_io to increase before the disk_stats
7623 * as sync_io is counted when a request starts, and
7624 * disk_stats is counted when it completes.
7625 * So resync activity will cause curr_events to be smaller than
7626 * when there was no such activity.
7627 * non-sync IO will cause disk_stat to increase without
7628 * increasing sync_io so curr_events will (eventually)
7629 * be larger than it was before. Once it becomes
7630 * substantially larger, the test below will cause
7631 * the array to appear non-idle, and resync will slow
7632 * down.
7633 * If there is a lot of outstanding resync activity when
7634 * we set last_event to curr_events, then all that activity
7635 * completing might cause the array to appear non-idle
7636 * and resync will be slowed down even though there might
7637 * not have been non-resync activity. This will only
7638 * happen once though. 'last_events' will soon reflect
7639 * the state where there is little or no outstanding
7640 * resync requests, and further resync activity will
7641 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007642 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007644 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645 rdev->last_events = curr_events;
7646 idle = 0;
7647 }
7648 }
NeilBrown4b809912008-07-21 17:05:25 +10007649 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 return idle;
7651}
7652
NeilBrownfd01b882011-10-11 16:47:53 +11007653void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654{
7655 /* another "blocks" (512byte) blocks have been synced */
7656 atomic_sub(blocks, &mddev->recovery_active);
7657 wake_up(&mddev->recovery_wait);
7658 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007659 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007660 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 md_wakeup_thread(mddev->thread);
7662 // stop recovery, signal do_sync ....
7663 }
7664}
NeilBrown6c144d32014-09-30 16:15:38 +10007665EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666
NeilBrown06d91a52005-06-21 17:17:12 -07007667/* md_write_start(mddev, bi)
7668 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007669 * in superblock) before writing, schedule a superblock update
7670 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007671 */
NeilBrownfd01b882011-10-11 16:47:53 +11007672void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673{
Neil Brown0fd62b82008-06-28 08:31:36 +10007674 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007675 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007676 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007677
NeilBrownf91de922005-11-08 21:39:36 -08007678 BUG_ON(mddev->ro == 1);
7679 if (mddev->ro == 2) {
7680 /* need to switch to read/write */
7681 mddev->ro = 0;
7682 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7683 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007684 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007685 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007686 }
NeilBrown06d91a52005-06-21 17:17:12 -07007687 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007688 if (mddev->safemode == 1)
7689 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007690 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007691 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007692 if (mddev->in_sync) {
7693 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007694 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007695 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007696 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007697 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007698 }
NeilBrown85572d72014-12-15 12:56:56 +11007699 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007700 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007701 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007702 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007703 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007704 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705}
NeilBrown6c144d32014-09-30 16:15:38 +10007706EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707
NeilBrownfd01b882011-10-11 16:47:53 +11007708void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709{
7710 if (atomic_dec_and_test(&mddev->writes_pending)) {
7711 if (mddev->safemode == 2)
7712 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007713 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7715 }
7716}
NeilBrown6c144d32014-09-30 16:15:38 +10007717EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718
NeilBrown2a2275d2007-01-26 00:57:11 -08007719/* md_allow_write(mddev)
7720 * Calling this ensures that the array is marked 'active' so that writes
7721 * may proceed without blocking. It is important to call this before
7722 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7723 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007724 *
Guoqing Jiangabf35082015-12-21 10:51:01 +11007725 * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007726 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007727 */
NeilBrownfd01b882011-10-11 16:47:53 +11007728int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007729{
7730 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007731 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007732 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007733 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007734 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007735 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007736
NeilBrown85572d72014-12-15 12:56:56 +11007737 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007738 if (mddev->in_sync) {
7739 mddev->in_sync = 0;
7740 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007741 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007742 if (mddev->safemode_delay &&
7743 mddev->safemode == 0)
7744 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007745 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007746 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007747 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007748 } else
NeilBrown85572d72014-12-15 12:56:56 +11007749 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007750
NeilBrown070dc6d2010-08-30 17:33:34 +10007751 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007752 return -EAGAIN;
7753 else
7754 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007755}
7756EXPORT_SYMBOL_GPL(md_allow_write);
7757
Linus Torvalds1da177e2005-04-16 15:20:36 -07007758#define SYNC_MARKS 10
7759#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007760#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007761void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762{
Shaohua Li4ed87312012-10-11 13:34:00 +11007763 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007764 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765 unsigned int currspeed = 0,
7766 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007767 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007769 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770 sector_t mark_cnt[SYNC_MARKS];
7771 int last_mark,m;
7772 struct list_head *tmp;
7773 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007774 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007775 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007776 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007777 struct blk_plug plug;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05007778 bool cluster_resync_finished = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779
7780 /* just incase thread restarts... */
7781 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7782 return;
NeilBrown3991b312014-05-28 13:39:23 +10007783 if (mddev->ro) {/* never try to sync a read-only array */
7784 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007785 return;
NeilBrown3991b312014-05-28 13:39:23 +10007786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787
NeilBrown61df9d92006-10-03 01:15:57 -07007788 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007789 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007790 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007791 action = "check";
7792 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007793 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007794 action = "repair";
7795 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007796 desc = "resync";
7797 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7798 desc = "reshape";
7799 else
7800 desc = "recovery";
7801
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007802 mddev->last_sync_action = action ?: desc;
7803
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 /* we overload curr_resync somewhat here.
7805 * 0 == not engaged in resync at all
7806 * 2 == checking that there is no conflict with another sync
7807 * 1 == like 2, but have yielded to allow conflicting resync to
7808 * commense
7809 * other == active in resync - this many blocks
7810 *
7811 * Before starting a resync we must have set curr_resync to
7812 * 2, and then checked that every "conflicting" array has curr_resync
7813 * less than ours. When we find one that is the same or higher
7814 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7815 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7816 * This will mean we have to start checking from the beginning again.
7817 *
7818 */
7819
7820 do {
7821 mddev->curr_resync = 2;
7822
7823 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007824 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007826 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 if (mddev2 == mddev)
7828 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007829 if (!mddev->parallel_resync
7830 && mddev2->curr_resync
7831 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 DEFINE_WAIT(wq);
7833 if (mddev < mddev2 && mddev->curr_resync == 2) {
7834 /* arbitrarily yield */
7835 mddev->curr_resync = 1;
7836 wake_up(&resync_wait);
7837 }
7838 if (mddev > mddev2 && mddev->curr_resync == 1)
7839 /* no need to wait here, we can wait the next
7840 * time 'round when curr_resync == 2
7841 */
7842 continue;
NeilBrown97441972008-09-19 11:49:54 +10007843 /* We need to wait 'interruptible' so as not to
7844 * contribute to the load average, and not to
7845 * be caught by 'softlockup'
7846 */
7847 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007848 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007849 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007850 printk(KERN_INFO "md: delaying %s of %s"
7851 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007853 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007855 if (signal_pending(current))
7856 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 schedule();
7858 finish_wait(&resync_wait, &wq);
7859 goto try_again;
7860 }
7861 finish_wait(&resync_wait, &wq);
7862 }
7863 }
7864 } while (mddev->curr_resync < 2);
7865
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007866 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007867 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007869 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 */
7871 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007872 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007873 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007874 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7875 j = mddev->resync_min;
7876 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007877 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007878
NeilBrownccfcc3c2006-03-27 01:18:09 -08007879 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007880 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007881 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007883 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007884 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007885 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007886 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007887 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007888 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007889 !test_bit(Faulty, &rdev->flags) &&
7890 !test_bit(In_sync, &rdev->flags) &&
7891 rdev->recovery_offset < j)
7892 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007893 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007894
7895 /* If there is a bitmap, we need to make sure all
7896 * writes that started before we added a spare
7897 * complete before we start doing a recovery.
7898 * Otherwise the write might complete and (via
7899 * bitmap_endwrite) set a bit in the bitmap after the
7900 * recovery has checked that bit and skipped that
7901 * region.
7902 */
7903 if (mddev->bitmap) {
7904 mddev->pers->quiesce(mddev, 1);
7905 mddev->pers->quiesce(mddev, 0);
7906 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908
NeilBrown61df9d92006-10-03 01:15:57 -07007909 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7910 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7911 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007912 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007913 "(but not more than %d KB/sec) for %s.\n",
7914 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915
NeilBrowneea1bf32009-03-31 14:27:02 +11007916 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007917
NeilBrown57afd892005-06-21 17:17:13 -07007918 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 for (m = 0; m < SYNC_MARKS; m++) {
7920 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007921 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 }
7923 last_mark = 0;
7924 mddev->resync_mark = mark[last_mark];
7925 mddev->resync_mark_cnt = mark_cnt[last_mark];
7926
7927 /*
7928 * Tune reconstruction:
7929 */
7930 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007931 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7932 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933
7934 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 last_check = 0;
7936
7937 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007938 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007939 "md: resuming %s of %s from checkpoint.\n",
7940 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007942 } else
7943 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007944 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007945 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7946 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007947 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948
majianpeng7c2c57c2012-07-03 12:12:26 +10007949 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007951 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952
NeilBrown57afd892005-06-21 17:17:13 -07007953 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007954
NeilBrown7a91ee12009-05-26 12:57:21 +10007955 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7956 ((mddev->curr_resync > mddev->curr_resync_completed &&
7957 (mddev->curr_resync - mddev->curr_resync_completed)
7958 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007959 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007960 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007961 >= mddev->resync_max - mddev->curr_resync_completed ||
7962 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007963 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007964 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007965 wait_event(mddev->recovery_wait,
7966 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007967 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007968 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7969 j > mddev->recovery_cp)
7970 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007971 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007972 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007973 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007974 }
NeilBrownacb180b2009-04-14 16:28:34 +10007975
NeilBrownc91abf52013-11-19 12:02:01 +11007976 while (j >= mddev->resync_max &&
7977 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007978 /* As this condition is controlled by user-space,
7979 * we can block indefinitely, so use '_interruptible'
7980 * to avoid triggering warnings.
7981 */
7982 flush_signals(current); /* just in case */
7983 wait_event_interruptible(mddev->recovery_wait,
7984 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007985 || test_bit(MD_RECOVERY_INTR,
7986 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007987 }
NeilBrownacb180b2009-04-14 16:28:34 +10007988
NeilBrownc91abf52013-11-19 12:02:01 +11007989 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7990 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007991
NeilBrown09314792015-02-19 16:04:40 +11007992 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07007993 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007994 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007995 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007996 }
NeilBrown57afd892005-06-21 17:17:13 -07007997
7998 if (!skipped) { /* actual IO requested */
7999 io_sectors += sectors;
8000 atomic_add(sectors, &mddev->recovery_active);
8001 }
8002
NeilBrowne875ece2011-07-28 11:39:24 +10008003 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8004 break;
8005
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008007 if (j > max_sectors)
8008 /* when skipping, extra large numbers can be returned. */
8009 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008010 if (j > 2)
8011 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008012 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008013 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008014 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008015 * visible in /proc/mdstat
8016 */
8017 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008018
8019 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020 continue;
8021
NeilBrown57afd892005-06-21 17:17:13 -07008022 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023 repeat:
8024 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8025 /* step marks */
8026 int next = (last_mark+1) % SYNC_MARKS;
8027
8028 mddev->resync_mark = mark[next];
8029 mddev->resync_mark_cnt = mark_cnt[next];
8030 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008031 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032 last_mark = next;
8033 }
8034
NeilBrownc91abf52013-11-19 12:02:01 +11008035 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037
8038 /*
8039 * this loop exits only if either when we are slower than
8040 * the 'hard' speed limit, or the system was IO-idle for
8041 * a jiffy.
8042 * the system might be non-idle CPU-wise, but we only care
8043 * about not overloading the IO subsystem. (things like an
8044 * e2fsck being done on the RAID array should execute fast)
8045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046 cond_resched();
8047
Xiao Niac7e50a2014-08-07 09:37:41 -04008048 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8049 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008050 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051
NeilBrown88202a02006-01-06 00:21:36 -08008052 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008053 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008054 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055 goto repeat;
8056 }
NeilBrownac8fa412015-02-19 16:55:00 +11008057 if (!is_mddev_idle(mddev, 0)) {
8058 /*
8059 * Give other IO more of a chance.
8060 * The faster the devices, the less we wait.
8061 */
8062 wait_event(mddev->recovery_wait,
8063 !atomic_read(&mddev->recovery_active));
8064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008065 }
8066 }
NeilBrownc91abf52013-11-19 12:02:01 +11008067 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8068 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8069 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070 /*
8071 * this also signals 'finished resyncing' to md_stop
8072 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008073 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8075
NeilBrown5ed1df22015-07-24 13:27:08 +10008076 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8077 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8078 mddev->curr_resync > 2) {
8079 mddev->curr_resync_completed = mddev->curr_resync;
8080 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8081 }
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008082 /* tell personality and other nodes that we are finished */
8083 if (mddev_is_clustered(mddev)) {
8084 md_cluster_ops->resync_finish(mddev);
8085 cluster_resync_finished = true;
8086 }
NeilBrown09314792015-02-19 16:04:40 +11008087 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088
NeilBrowndfc70642008-05-23 13:04:39 -07008089 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008090 mddev->curr_resync > 2) {
8091 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8092 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8093 if (mddev->curr_resync >= mddev->recovery_cp) {
8094 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008095 "md: checkpointing %s of %s.\n",
8096 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008097 if (test_bit(MD_RECOVERY_ERROR,
8098 &mddev->recovery))
8099 mddev->recovery_cp =
8100 mddev->curr_resync_completed;
8101 else
8102 mddev->recovery_cp =
8103 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008104 }
8105 } else
8106 mddev->recovery_cp = MaxSector;
8107 } else {
8108 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8109 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008110 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008111 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008112 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008113 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008114 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008115 !test_bit(Faulty, &rdev->flags) &&
8116 !test_bit(In_sync, &rdev->flags) &&
8117 rdev->recovery_offset < mddev->curr_resync)
8118 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008119 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008122 skip:
NeilBrown17571282006-12-10 02:20:52 -08008123 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008125 if (mddev_is_clustered(mddev) &&
8126 test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8127 !cluster_resync_finished)
8128 md_cluster_ops->resync_finish(mddev);
8129
NeilBrown23da4222014-12-15 12:57:01 +11008130 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008131 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8132 /* We completed so min/max setting can be forgotten if used. */
8133 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8134 mddev->resync_min = 0;
8135 mddev->resync_max = MaxSector;
8136 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8137 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008138 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008140 spin_unlock(&mddev->lock);
8141
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008143 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008144 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145}
NeilBrown29269552006-03-27 01:18:10 -08008146EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147
NeilBrown746d3202013-04-24 11:42:41 +10008148static int remove_and_add_spares(struct mddev *mddev,
8149 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008150{
NeilBrown3cb03002011-10-11 16:45:26 +11008151 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008152 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008153 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008154
NeilBrowndafb20f2012-03-19 12:46:39 +11008155 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008156 if ((this == NULL || rdev == this) &&
8157 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008158 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008159 (test_bit(Faulty, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008160 (!test_bit(In_sync, &rdev->flags) &&
8161 !test_bit(Journal, &rdev->flags))) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008162 atomic_read(&rdev->nr_pending)==0) {
8163 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008164 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008165 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008166 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008167 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008168 }
8169 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008170 if (removed && mddev->kobj.sd)
8171 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008172
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008173 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008174 goto no_add;
8175
NeilBrowndafb20f2012-03-19 12:46:39 +11008176 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008177 if (this && this != rdev)
8178 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008179 if (test_bit(Candidate, &rdev->flags))
8180 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008181 if (rdev->raid_disk >= 0 &&
8182 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008183 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008184 !test_bit(Faulty, &rdev->flags))
8185 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008186 if (rdev->raid_disk >= 0)
8187 continue;
8188 if (test_bit(Faulty, &rdev->flags))
8189 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008190 if (!test_bit(Journal, &rdev->flags)) {
8191 if (mddev->ro &&
8192 ! (rdev->saved_raid_disk >= 0 &&
8193 !test_bit(Bitmap_sync, &rdev->flags)))
8194 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008195
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008196 rdev->recovery_offset = 0;
8197 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008198 if (mddev->pers->
8199 hot_add_disk(mddev, rdev) == 0) {
8200 if (sysfs_link_rdev(mddev, rdev))
8201 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008202 if (!test_bit(Journal, &rdev->flags))
8203 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008204 md_new_event(mddev);
8205 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008206 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008207 }
NeilBrown746d3202013-04-24 11:42:41 +10008208no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008209 if (removed)
8210 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008211 return spares;
8212}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008213
NeilBrownac05f252014-09-30 08:10:42 +10008214static void md_start_sync(struct work_struct *ws)
8215{
8216 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008217 int ret = 0;
8218
8219 if (mddev_is_clustered(mddev)) {
8220 ret = md_cluster_ops->resync_start(mddev);
8221 if (ret) {
8222 mddev->sync_thread = NULL;
8223 goto out;
8224 }
8225 }
NeilBrownac05f252014-09-30 08:10:42 +10008226
8227 mddev->sync_thread = md_register_thread(md_do_sync,
8228 mddev,
8229 "resync");
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008230out:
NeilBrownac05f252014-09-30 08:10:42 +10008231 if (!mddev->sync_thread) {
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008232 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8233 printk(KERN_ERR "%s: could not start resync"
8234 " thread...\n",
8235 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008236 /* leave the spares where they are, it shouldn't hurt */
8237 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8238 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8239 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8240 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8241 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008242 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008243 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8244 &mddev->recovery))
8245 if (mddev->sysfs_action)
8246 sysfs_notify_dirent_safe(mddev->sysfs_action);
8247 } else
8248 md_wakeup_thread(mddev->sync_thread);
8249 sysfs_notify_dirent_safe(mddev->sysfs_action);
8250 md_new_event(mddev);
8251}
8252
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253/*
8254 * This routine is regularly called by all per-raid-array threads to
8255 * deal with generic issues like resync and super-block update.
8256 * Raid personalities that don't have a thread (linear/raid0) do not
8257 * need this as they never do any recovery or update the superblock.
8258 *
8259 * It does not do any resync itself, but rather "forks" off other threads
8260 * to do that as needed.
8261 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8262 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008263 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264 * and wakeups up this thread which will reap the thread and finish up.
8265 * This thread also removes any faulty devices (with nr_pending == 0).
8266 *
8267 * The overall approach is:
8268 * 1/ if the superblock needs updating, update it.
8269 * 2/ If a recovery thread is running, don't do anything else.
8270 * 3/ If recovery has finished, clean up, possibly marking spares active.
8271 * 4/ If there are any faulty devices, remove them.
8272 * 5/ If array is degraded, try to add spares devices
8273 * 6/ If array has spares or is not in-sync, start a resync thread.
8274 */
NeilBrownfd01b882011-10-11 16:47:53 +11008275void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008277 if (mddev->suspended)
8278 return;
8279
NeilBrown5f404022005-06-21 17:17:16 -07008280 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008281 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282
NeilBrownfca4d842005-06-21 17:17:11 -07008283 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008284 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008285 printk(KERN_INFO "md: %s in immediate safe mode\n",
8286 mdname(mddev));
8287 mddev->safemode = 2;
8288 }
8289 flush_signals(current);
8290 }
8291
NeilBrownc89a8ee2008-08-05 15:54:13 +10008292 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8293 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008295 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008297 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008298 test_bit(MD_RELOAD_SB, &mddev->flags) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008299 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008300 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8301 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302 ))
8303 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008304
NeilBrowndf5b89b2006-03-27 01:18:20 -08008305 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008306 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008307
NeilBrownc89a8ee2008-08-05 15:54:13 +10008308 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008309 struct md_rdev *rdev;
8310 if (!mddev->external && mddev->in_sync)
8311 /* 'Blocked' flag not needed as failed devices
8312 * will be recorded if array switched to read/write.
8313 * Leaving it set will prevent the device
8314 * from being removed.
8315 */
8316 rdev_for_each(rdev, mddev)
8317 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008318 /* On a read-only array we can:
8319 * - remove failed devices
8320 * - add already-in_sync devices if the array itself
8321 * is in-sync.
8322 * As we only add devices that are already in-sync,
8323 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008324 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008325 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008326 /* There is no thread, but we need to call
8327 * ->spare_active and clear saved_raid_disk
8328 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008329 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008330 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008331 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008332 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008333 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008334 goto unlock;
8335 }
8336
Guoqing Jiang659b2542015-12-21 10:50:59 +11008337 if (mddev_is_clustered(mddev)) {
8338 struct md_rdev *rdev;
8339 /* kick the device if another node issued a
8340 * remove disk.
8341 */
8342 rdev_for_each(rdev, mddev) {
8343 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8344 rdev->raid_disk < 0)
8345 md_kick_rdev_from_array(rdev);
8346 }
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008347
8348 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8349 md_reload_sb(mddev, mddev->good_device_nr);
Guoqing Jiang659b2542015-12-21 10:50:59 +11008350 }
8351
NeilBrown31a59e32008-04-30 00:52:30 -07008352 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008353 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008354 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008355 if (mddev->safemode &&
8356 !atomic_read(&mddev->writes_pending) &&
8357 !mddev->in_sync &&
8358 mddev->recovery_cp == MaxSector) {
8359 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008360 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008361 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008362 }
8363 if (mddev->safemode == 1)
8364 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008365 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008366 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008367 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008368 }
NeilBrownfca4d842005-06-21 17:17:11 -07008369
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008370 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07008371 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008372
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8374 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8375 /* resync/recovery still happening */
8376 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8377 goto unlock;
8378 }
8379 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008380 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 goto unlock;
8382 }
Neil Brown72a23c22008-06-28 08:31:41 +10008383 /* Set RUNNING before clearing NEEDED to avoid
8384 * any transients in the value of "sync_action".
8385 */
NeilBrown72f36d52012-10-11 14:25:57 +11008386 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008387 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008388 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008389 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008390 /* Clear some bits that don't mean anything, but
8391 * might be left set
8392 */
NeilBrown24dd4692005-11-08 21:39:26 -08008393 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8394 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395
NeilBrowned209582012-04-24 10:23:14 +10008396 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8397 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008398 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399 /* no recovery is running.
8400 * remove any failed drives, then
8401 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008402 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008403 * the personality to fail the re-add.
8404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008406 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008407 if (mddev->pers->check_reshape == NULL ||
8408 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008409 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008410 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008411 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008412 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008413 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008414 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8415 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008416 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008417 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008418 } else if (mddev->recovery_cp < MaxSector) {
8419 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008420 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008421 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8422 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008423 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008424
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008426 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008427 /* We are adding a device or devices to an array
8428 * which has the bitmap stored on all devices.
8429 * So make sure all bitmap pages get written
8430 */
8431 bitmap_write_all(mddev->bitmap);
8432 }
NeilBrownac05f252014-09-30 08:10:42 +10008433 INIT_WORK(&mddev->del_work, md_start_sync);
8434 queue_work(md_misc_wq, &mddev->del_work);
8435 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436 }
NeilBrownac05f252014-09-30 08:10:42 +10008437 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008438 if (!mddev->sync_thread) {
8439 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008440 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008441 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8442 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008443 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008444 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008445 }
NeilBrownac05f252014-09-30 08:10:42 +10008446 unlock:
8447 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448 mddev_unlock(mddev);
8449 }
8450}
NeilBrown6c144d32014-09-30 16:15:38 +10008451EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008453void md_reap_sync_thread(struct mddev *mddev)
8454{
8455 struct md_rdev *rdev;
8456
8457 /* resync has finished, collect result */
8458 md_unregister_thread(&mddev->sync_thread);
8459 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8460 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8461 /* success...*/
8462 /* activate any spares */
8463 if (mddev->pers->spare_active(mddev)) {
8464 sysfs_notify(&mddev->kobj, NULL,
8465 "degraded");
8466 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8467 }
8468 }
8469 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8470 mddev->pers->finish_reshape)
8471 mddev->pers->finish_reshape(mddev);
8472
8473 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008474 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008475 */
NeilBrownf4667222013-12-09 12:04:56 +11008476 if (!mddev->degraded)
8477 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008478 rdev->saved_raid_disk = -1;
8479
8480 md_update_sb(mddev, 1);
8481 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008482 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008483 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8484 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8485 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8486 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008487 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008488 /* flag recovery needed just to double check */
8489 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8490 sysfs_notify_dirent_safe(mddev->sysfs_action);
8491 md_new_event(mddev);
8492 if (mddev->event_work.func)
8493 queue_work(md_misc_wq, &mddev->event_work);
8494}
NeilBrown6c144d32014-09-30 16:15:38 +10008495EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008496
NeilBrownfd01b882011-10-11 16:47:53 +11008497void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008498{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008499 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008500 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008501 !test_bit(Blocked, &rdev->flags) &&
8502 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008503 msecs_to_jiffies(5000));
8504 rdev_dec_pending(rdev, mddev);
8505}
8506EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8507
NeilBrownc6563a82012-05-21 09:27:00 +10008508void md_finish_reshape(struct mddev *mddev)
8509{
8510 /* called be personality module when reshape completes. */
8511 struct md_rdev *rdev;
8512
8513 rdev_for_each(rdev, mddev) {
8514 if (rdev->data_offset > rdev->new_data_offset)
8515 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8516 else
8517 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8518 rdev->data_offset = rdev->new_data_offset;
8519 }
8520}
8521EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008522
8523/* Bad block management.
8524 * We can record which blocks on each device are 'bad' and so just
8525 * fail those blocks, or that stripe, rather than the whole device.
8526 * Entries in the bad-block table are 64bits wide. This comprises:
8527 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8528 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8529 * A 'shift' can be set so that larger blocks are tracked and
8530 * consequently larger devices can be covered.
8531 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8532 *
8533 * Locking of the bad-block table uses a seqlock so md_is_badblock
8534 * might need to retry if it is very unlucky.
8535 * We will sometimes want to check for bad blocks in a bi_end_io function,
8536 * so we use the write_seqlock_irq variant.
8537 *
8538 * When looking for a bad block we specify a range and want to
8539 * know if any block in the range is bad. So we binary-search
8540 * to the last range that starts at-or-before the given endpoint,
8541 * (or "before the sector after the target range")
8542 * then see if it ends after the given start.
8543 * We return
8544 * 0 if there are no known bad blocks in the range
8545 * 1 if there are known bad block which are all acknowledged
8546 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8547 * plus the start/length of the first bad section we overlap.
8548 */
8549int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8550 sector_t *first_bad, int *bad_sectors)
8551{
8552 int hi;
majianpengab056132012-11-06 17:13:44 +08008553 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008554 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008555 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008556 sector_t target = s + sectors;
8557 unsigned seq;
8558
8559 if (bb->shift > 0) {
8560 /* round the start down, and the end up */
8561 s >>= bb->shift;
8562 target += (1<<bb->shift) - 1;
8563 target >>= bb->shift;
8564 sectors = target - s;
8565 }
8566 /* 'target' is now the first block after the bad range */
8567
8568retry:
8569 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008570 lo = 0;
8571 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008572 hi = bb->count;
8573
8574 /* Binary search between lo and hi for 'target'
8575 * i.e. for the last range that starts before 'target'
8576 */
8577 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8578 * are known not to be the last range before target.
8579 * VARIANT: hi-lo is the number of possible
8580 * ranges, and decreases until it reaches 1
8581 */
8582 while (hi - lo > 1) {
8583 int mid = (lo + hi) / 2;
8584 sector_t a = BB_OFFSET(p[mid]);
8585 if (a < target)
8586 /* This could still be the one, earlier ranges
8587 * could not. */
8588 lo = mid;
8589 else
8590 /* This and later ranges are definitely out. */
8591 hi = mid;
8592 }
8593 /* 'lo' might be the last that started before target, but 'hi' isn't */
8594 if (hi > lo) {
8595 /* need to check all range that end after 's' to see if
8596 * any are unacknowledged.
8597 */
8598 while (lo >= 0 &&
8599 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8600 if (BB_OFFSET(p[lo]) < target) {
8601 /* starts before the end, and finishes after
8602 * the start, so they must overlap
8603 */
8604 if (rv != -1 && BB_ACK(p[lo]))
8605 rv = 1;
8606 else
8607 rv = -1;
8608 *first_bad = BB_OFFSET(p[lo]);
8609 *bad_sectors = BB_LEN(p[lo]);
8610 }
8611 lo--;
8612 }
8613 }
8614
8615 if (read_seqretry(&bb->lock, seq))
8616 goto retry;
8617
8618 return rv;
8619}
8620EXPORT_SYMBOL_GPL(md_is_badblock);
8621
8622/*
8623 * Add a range of bad blocks to the table.
8624 * This might extend the table, or might contract it
8625 * if two adjacent ranges can be merged.
8626 * We binary-search to find the 'insertion' point, then
8627 * decide how best to handle it.
8628 */
8629static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8630 int acknowledged)
8631{
8632 u64 *p;
8633 int lo, hi;
8634 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008635 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008636
8637 if (bb->shift < 0)
8638 /* badblocks are disabled */
8639 return 0;
8640
8641 if (bb->shift) {
8642 /* round the start down, and the end up */
8643 sector_t next = s + sectors;
8644 s >>= bb->shift;
8645 next += (1<<bb->shift) - 1;
8646 next >>= bb->shift;
8647 sectors = next - s;
8648 }
8649
Bian Yu905b0292013-10-12 01:10:03 -04008650 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008651
8652 p = bb->page;
8653 lo = 0;
8654 hi = bb->count;
8655 /* Find the last range that starts at-or-before 's' */
8656 while (hi - lo > 1) {
8657 int mid = (lo + hi) / 2;
8658 sector_t a = BB_OFFSET(p[mid]);
8659 if (a <= s)
8660 lo = mid;
8661 else
8662 hi = mid;
8663 }
8664 if (hi > lo && BB_OFFSET(p[lo]) > s)
8665 hi = lo;
8666
8667 if (hi > lo) {
8668 /* we found a range that might merge with the start
8669 * of our new range
8670 */
8671 sector_t a = BB_OFFSET(p[lo]);
8672 sector_t e = a + BB_LEN(p[lo]);
8673 int ack = BB_ACK(p[lo]);
8674 if (e >= s) {
8675 /* Yes, we can merge with a previous range */
8676 if (s == a && s + sectors >= e)
8677 /* new range covers old */
8678 ack = acknowledged;
8679 else
8680 ack = ack && acknowledged;
8681
8682 if (e < s + sectors)
8683 e = s + sectors;
8684 if (e - a <= BB_MAX_LEN) {
8685 p[lo] = BB_MAKE(a, e-a, ack);
8686 s = e;
8687 } else {
8688 /* does not all fit in one range,
8689 * make p[lo] maximal
8690 */
8691 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8692 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8693 s = a + BB_MAX_LEN;
8694 }
8695 sectors = e - s;
8696 }
8697 }
8698 if (sectors && hi < bb->count) {
8699 /* 'hi' points to the first range that starts after 's'.
8700 * Maybe we can merge with the start of that range */
8701 sector_t a = BB_OFFSET(p[hi]);
8702 sector_t e = a + BB_LEN(p[hi]);
8703 int ack = BB_ACK(p[hi]);
8704 if (a <= s + sectors) {
8705 /* merging is possible */
8706 if (e <= s + sectors) {
8707 /* full overlap */
8708 e = s + sectors;
8709 ack = acknowledged;
8710 } else
8711 ack = ack && acknowledged;
8712
8713 a = s;
8714 if (e - a <= BB_MAX_LEN) {
8715 p[hi] = BB_MAKE(a, e-a, ack);
8716 s = e;
8717 } else {
8718 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8719 s = a + BB_MAX_LEN;
8720 }
8721 sectors = e - s;
8722 lo = hi;
8723 hi++;
8724 }
8725 }
8726 if (sectors == 0 && hi < bb->count) {
8727 /* we might be able to combine lo and hi */
8728 /* Note: 's' is at the end of 'lo' */
8729 sector_t a = BB_OFFSET(p[hi]);
8730 int lolen = BB_LEN(p[lo]);
8731 int hilen = BB_LEN(p[hi]);
8732 int newlen = lolen + hilen - (s - a);
8733 if (s >= a && newlen < BB_MAX_LEN) {
8734 /* yes, we can combine them */
8735 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8736 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8737 memmove(p + hi, p + hi + 1,
8738 (bb->count - hi - 1) * 8);
8739 bb->count--;
8740 }
8741 }
8742 while (sectors) {
8743 /* didn't merge (it all).
8744 * Need to add a range just before 'hi' */
8745 if (bb->count >= MD_MAX_BADBLOCKS) {
8746 /* No room for more */
8747 rv = 0;
8748 break;
8749 } else {
8750 int this_sectors = sectors;
8751 memmove(p + hi + 1, p + hi,
8752 (bb->count - hi) * 8);
8753 bb->count++;
8754
8755 if (this_sectors > BB_MAX_LEN)
8756 this_sectors = BB_MAX_LEN;
8757 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8758 sectors -= this_sectors;
8759 s += this_sectors;
8760 }
8761 }
8762
8763 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008764 if (!acknowledged)
8765 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008766 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008767
8768 return rv;
8769}
8770
NeilBrown3cb03002011-10-11 16:45:26 +11008771int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008772 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008773{
NeilBrownc6563a82012-05-21 09:27:00 +10008774 int rv;
8775 if (is_new)
8776 s += rdev->new_data_offset;
8777 else
8778 s += rdev->data_offset;
8779 rv = md_set_badblocks(&rdev->badblocks,
8780 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008781 if (rv) {
8782 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008783 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008784 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
NeilBrown55ce74d2015-08-14 11:11:10 +10008785 set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008786 md_wakeup_thread(rdev->mddev->thread);
8787 }
8788 return rv;
8789}
8790EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8791
8792/*
8793 * Remove a range of bad blocks from the table.
8794 * This may involve extending the table if we spilt a region,
8795 * but it must not fail. So if the table becomes full, we just
8796 * drop the remove request.
8797 */
8798static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8799{
8800 u64 *p;
8801 int lo, hi;
8802 sector_t target = s + sectors;
8803 int rv = 0;
8804
8805 if (bb->shift > 0) {
8806 /* When clearing we round the start up and the end down.
8807 * This should not matter as the shift should align with
8808 * the block size and no rounding should ever be needed.
8809 * However it is better the think a block is bad when it
8810 * isn't than to think a block is not bad when it is.
8811 */
8812 s += (1<<bb->shift) - 1;
8813 s >>= bb->shift;
8814 target >>= bb->shift;
8815 sectors = target - s;
8816 }
8817
8818 write_seqlock_irq(&bb->lock);
8819
8820 p = bb->page;
8821 lo = 0;
8822 hi = bb->count;
8823 /* Find the last range that starts before 'target' */
8824 while (hi - lo > 1) {
8825 int mid = (lo + hi) / 2;
8826 sector_t a = BB_OFFSET(p[mid]);
8827 if (a < target)
8828 lo = mid;
8829 else
8830 hi = mid;
8831 }
8832 if (hi > lo) {
8833 /* p[lo] is the last range that could overlap the
8834 * current range. Earlier ranges could also overlap,
8835 * but only this one can overlap the end of the range.
8836 */
8837 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8838 /* Partial overlap, leave the tail of this range */
8839 int ack = BB_ACK(p[lo]);
8840 sector_t a = BB_OFFSET(p[lo]);
8841 sector_t end = a + BB_LEN(p[lo]);
8842
8843 if (a < s) {
8844 /* we need to split this range */
8845 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008846 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008847 goto out;
8848 }
8849 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8850 bb->count++;
8851 p[lo] = BB_MAKE(a, s-a, ack);
8852 lo++;
8853 }
8854 p[lo] = BB_MAKE(target, end - target, ack);
8855 /* there is no longer an overlap */
8856 hi = lo;
8857 lo--;
8858 }
8859 while (lo >= 0 &&
8860 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8861 /* This range does overlap */
8862 if (BB_OFFSET(p[lo]) < s) {
8863 /* Keep the early parts of this range. */
8864 int ack = BB_ACK(p[lo]);
8865 sector_t start = BB_OFFSET(p[lo]);
8866 p[lo] = BB_MAKE(start, s - start, ack);
8867 /* now low doesn't overlap, so.. */
8868 break;
8869 }
8870 lo--;
8871 }
8872 /* 'lo' is strictly before, 'hi' is strictly after,
8873 * anything between needs to be discarded
8874 */
8875 if (hi - lo > 1) {
8876 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8877 bb->count -= (hi - lo - 1);
8878 }
8879 }
8880
8881 bb->changed = 1;
8882out:
8883 write_sequnlock_irq(&bb->lock);
8884 return rv;
8885}
8886
NeilBrownc6563a82012-05-21 09:27:00 +10008887int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8888 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008889{
NeilBrownc6563a82012-05-21 09:27:00 +10008890 if (is_new)
8891 s += rdev->new_data_offset;
8892 else
8893 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008894 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008895 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008896}
8897EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8898
8899/*
8900 * Acknowledge all bad blocks in a list.
8901 * This only succeeds if ->changed is clear. It is used by
8902 * in-kernel metadata updates
8903 */
8904void md_ack_all_badblocks(struct badblocks *bb)
8905{
8906 if (bb->page == NULL || bb->changed)
8907 /* no point even trying */
8908 return;
8909 write_seqlock_irq(&bb->lock);
8910
majianpengecb178b2012-03-19 12:46:42 +11008911 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008912 u64 *p = bb->page;
8913 int i;
8914 for (i = 0; i < bb->count ; i++) {
8915 if (!BB_ACK(p[i])) {
8916 sector_t start = BB_OFFSET(p[i]);
8917 int len = BB_LEN(p[i]);
8918 p[i] = BB_MAKE(start, len, 1);
8919 }
8920 }
NeilBrownde393cd2011-07-28 11:31:48 +10008921 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008922 }
8923 write_sequnlock_irq(&bb->lock);
8924}
8925EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8926
NeilBrown16c791a2011-07-28 11:31:47 +10008927/* sysfs access to bad-blocks list.
8928 * We present two files.
8929 * 'bad-blocks' lists sector numbers and lengths of ranges that
8930 * are recorded as bad. The list is truncated to fit within
8931 * the one-page limit of sysfs.
8932 * Writing "sector length" to this file adds an acknowledged
8933 * bad block list.
8934 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8935 * been acknowledged. Writing to this file adds bad blocks
8936 * without acknowledging them. This is largely for testing.
8937 */
8938
8939static ssize_t
8940badblocks_show(struct badblocks *bb, char *page, int unack)
8941{
8942 size_t len;
8943 int i;
8944 u64 *p = bb->page;
8945 unsigned seq;
8946
8947 if (bb->shift < 0)
8948 return 0;
8949
8950retry:
8951 seq = read_seqbegin(&bb->lock);
8952
8953 len = 0;
8954 i = 0;
8955
8956 while (len < PAGE_SIZE && i < bb->count) {
8957 sector_t s = BB_OFFSET(p[i]);
8958 unsigned int length = BB_LEN(p[i]);
8959 int ack = BB_ACK(p[i]);
8960 i++;
8961
8962 if (unack && ack)
8963 continue;
8964
8965 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8966 (unsigned long long)s << bb->shift,
8967 length << bb->shift);
8968 }
NeilBrownde393cd2011-07-28 11:31:48 +10008969 if (unack && len == 0)
8970 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008971
8972 if (read_seqretry(&bb->lock, seq))
8973 goto retry;
8974
8975 return len;
8976}
8977
8978#define DO_DEBUG 1
8979
8980static ssize_t
8981badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8982{
8983 unsigned long long sector;
8984 int length;
8985 char newline;
8986#ifdef DO_DEBUG
8987 /* Allow clearing via sysfs *only* for testing/debugging.
8988 * Normally only a successful write may clear a badblock
8989 */
8990 int clear = 0;
8991 if (page[0] == '-') {
8992 clear = 1;
8993 page++;
8994 }
8995#endif /* DO_DEBUG */
8996
8997 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8998 case 3:
8999 if (newline != '\n')
9000 return -EINVAL;
9001 case 2:
9002 if (length <= 0)
9003 return -EINVAL;
9004 break;
9005 default:
9006 return -EINVAL;
9007 }
9008
9009#ifdef DO_DEBUG
9010 if (clear) {
9011 md_clear_badblocks(bb, sector, length);
9012 return len;
9013 }
9014#endif /* DO_DEBUG */
9015 if (md_set_badblocks(bb, sector, length, !unack))
9016 return len;
9017 else
9018 return -ENOSPC;
9019}
9020
Adrian Bunk75c96f82005-05-05 16:16:09 -07009021static int md_notify_reboot(struct notifier_block *this,
9022 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009023{
9024 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009025 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009026 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009027
NeilBrownc744a652012-03-19 12:46:37 +11009028 for_each_mddev(mddev, tmp) {
9029 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009030 if (mddev->pers)
9031 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009032 if (mddev->persistent)
9033 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009034 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009035 }
NeilBrownc744a652012-03-19 12:46:37 +11009036 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009037 }
NeilBrownc744a652012-03-19 12:46:37 +11009038 /*
9039 * certain more exotic SCSI devices are known to be
9040 * volatile wrt too early system reboots. While the
9041 * right place to handle this issue is the given
9042 * driver, we do want to have a safe RAID driver ...
9043 */
9044 if (need_delay)
9045 mdelay(1000*1);
9046
Linus Torvalds1da177e2005-04-16 15:20:36 -07009047 return NOTIFY_DONE;
9048}
9049
Adrian Bunk75c96f82005-05-05 16:16:09 -07009050static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 .notifier_call = md_notify_reboot,
9052 .next = NULL,
9053 .priority = INT_MAX, /* before any real devices */
9054};
9055
9056static void md_geninit(void)
9057{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009058 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009059
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009060 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009061}
9062
Adrian Bunk75c96f82005-05-05 16:16:09 -07009063static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064{
Tejun Heoe804ac72010-10-15 15:36:08 +02009065 int ret = -ENOMEM;
9066
Tejun Heoada609e2011-01-25 14:35:54 +01009067 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009068 if (!md_wq)
9069 goto err_wq;
9070
9071 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9072 if (!md_misc_wq)
9073 goto err_misc_wq;
9074
9075 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9076 goto err_md;
9077
9078 if ((ret = register_blkdev(0, "mdp")) < 0)
9079 goto err_mdp;
9080 mdp_major = ret;
9081
NeilBrownaf5628f2014-07-31 13:54:54 +10009082 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009083 md_probe, NULL, NULL);
9084 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085 md_probe, NULL, NULL);
9086
Linus Torvalds1da177e2005-04-16 15:20:36 -07009087 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009088 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009089
9090 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009092
Tejun Heoe804ac72010-10-15 15:36:08 +02009093err_mdp:
9094 unregister_blkdev(MD_MAJOR, "md");
9095err_md:
9096 destroy_workqueue(md_misc_wq);
9097err_misc_wq:
9098 destroy_workqueue(md_wq);
9099err_wq:
9100 return ret;
9101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009102
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009103static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009104{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009105 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9106 struct md_rdev *rdev2;
9107 int role, ret;
9108 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009109
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009110 /* Check for change of roles in the active devices */
9111 rdev_for_each(rdev2, mddev) {
9112 if (test_bit(Faulty, &rdev2->flags))
9113 continue;
9114
9115 /* Check if the roles changed */
9116 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009117
9118 if (test_bit(Candidate, &rdev2->flags)) {
9119 if (role == 0xfffe) {
9120 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9121 md_kick_rdev_from_array(rdev2);
9122 continue;
9123 }
9124 else
9125 clear_bit(Candidate, &rdev2->flags);
9126 }
9127
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009128 if (role != rdev2->raid_disk) {
9129 /* got activated */
9130 if (rdev2->raid_disk == -1 && role != 0xffff) {
9131 rdev2->saved_raid_disk = role;
9132 ret = remove_and_add_spares(mddev, rdev2);
9133 pr_info("Activated spare: %s\n",
9134 bdevname(rdev2->bdev,b));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009135 }
9136 /* device faulty
9137 * We just want to do the minimum to mark the disk
9138 * as faulty. The recovery is performed by the
9139 * one who initiated the error.
9140 */
9141 if ((role == 0xfffe) || (role == 0xfffd)) {
9142 md_error(mddev, rdev2);
9143 clear_bit(Blocked, &rdev2->flags);
9144 }
9145 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009146 }
9147
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009148 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9149 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009150
9151 /* Finally set the event to be up to date */
9152 mddev->events = le64_to_cpu(sb->events);
9153}
9154
9155static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9156{
9157 int err;
9158 struct page *swapout = rdev->sb_page;
9159 struct mdp_superblock_1 *sb;
9160
9161 /* Store the sb page of the rdev in the swapout temporary
9162 * variable in case we err in the future
9163 */
9164 rdev->sb_page = NULL;
9165 alloc_disk_sb(rdev);
9166 ClearPageUptodate(rdev->sb_page);
9167 rdev->sb_loaded = 0;
9168 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
9169
9170 if (err < 0) {
9171 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9172 __func__, __LINE__, rdev->desc_nr, err);
9173 put_page(rdev->sb_page);
9174 rdev->sb_page = swapout;
9175 rdev->sb_loaded = 1;
9176 return err;
9177 }
9178
9179 sb = page_address(rdev->sb_page);
9180 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9181 * is not set
9182 */
9183
9184 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9185 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9186
9187 /* The other node finished recovery, call spare_active to set
9188 * device In_sync and mddev->degraded
9189 */
9190 if (rdev->recovery_offset == MaxSector &&
9191 !test_bit(In_sync, &rdev->flags) &&
9192 mddev->pers->spare_active(mddev))
9193 sysfs_notify(&mddev->kobj, NULL, "degraded");
9194
9195 put_page(swapout);
9196 return 0;
9197}
9198
9199void md_reload_sb(struct mddev *mddev, int nr)
9200{
9201 struct md_rdev *rdev;
9202 int err;
9203
9204 /* Find the rdev */
9205 rdev_for_each_rcu(rdev, mddev) {
9206 if (rdev->desc_nr == nr)
9207 break;
9208 }
9209
9210 if (!rdev || rdev->desc_nr != nr) {
9211 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9212 return;
9213 }
9214
9215 err = read_rdev(mddev, rdev);
9216 if (err < 0)
9217 return;
9218
9219 check_sb_changes(mddev, rdev);
9220
9221 /* Read all rdev's to update recovery_offset */
9222 rdev_for_each_rcu(rdev, mddev)
9223 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009224}
9225EXPORT_SYMBOL(md_reload_sb);
9226
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227#ifndef MODULE
9228
9229/*
9230 * Searches all registered partitions for autorun RAID arrays
9231 * at boot time.
9232 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009233
9234static LIST_HEAD(all_detected_devices);
9235struct detected_devices_node {
9236 struct list_head list;
9237 dev_t dev;
9238};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009239
9240void md_autodetect_dev(dev_t dev)
9241{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009242 struct detected_devices_node *node_detected_dev;
9243
9244 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9245 if (node_detected_dev) {
9246 node_detected_dev->dev = dev;
9247 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9248 } else {
9249 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
9250 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
9251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009252}
9253
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254static void autostart_arrays(int part)
9255{
NeilBrown3cb03002011-10-11 16:45:26 +11009256 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009257 struct detected_devices_node *node_detected_dev;
9258 dev_t dev;
9259 int i_scanned, i_passed;
9260
9261 i_scanned = 0;
9262 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263
9264 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
9265
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009266 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9267 i_scanned++;
9268 node_detected_dev = list_entry(all_detected_devices.next,
9269 struct detected_devices_node, list);
9270 list_del(&node_detected_dev->list);
9271 dev = node_detected_dev->dev;
9272 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07009273 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009274 if (IS_ERR(rdev))
9275 continue;
9276
NeilBrown403df472014-09-30 15:52:29 +10009277 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278 continue;
NeilBrown403df472014-09-30 15:52:29 +10009279
NeilBrownd0fae182008-03-04 14:29:31 -08009280 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009282 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009283 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009284
9285 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
9286 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009287
9288 autorun_devices(part);
9289}
9290
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009291#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292
9293static __exit void md_exit(void)
9294{
NeilBrownfd01b882011-10-11 16:47:53 +11009295 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009297 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009298
NeilBrownaf5628f2014-07-31 13:54:54 +10009299 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009300 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009302 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303 unregister_blkdev(mdp_major, "mdp");
9304 unregister_reboot_notifier(&md_notifier);
9305 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009306
9307 /* We cannot unload the modules while some process is
9308 * waiting for us in select() or poll() - wake them up
9309 */
9310 md_unloading = 1;
9311 while (waitqueue_active(&md_event_waiters)) {
9312 /* not safe to leave yet */
9313 wake_up(&md_event_waiters);
9314 msleep(delay);
9315 delay += delay;
9316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009318
NeilBrown29ac4aa2008-02-06 01:39:58 -08009319 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009321 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009323 destroy_workqueue(md_misc_wq);
9324 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009325}
9326
Dan Williams685784a2007-07-09 11:56:42 -07009327subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328module_exit(md_exit)
9329
NeilBrownf91de922005-11-08 21:39:36 -08009330static int get_ro(char *buffer, struct kernel_param *kp)
9331{
9332 return sprintf(buffer, "%d", start_readonly);
9333}
9334static int set_ro(const char *val, struct kernel_param *kp)
9335{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009336 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009337}
9338
NeilBrown80ca3a42006-07-10 04:44:18 -07009339module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9340module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009341module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009342
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009344MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009345MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009346MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);