blob: 1db88d79549a74239068cd5b21241294917c1fc8 [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.
NeilBrown9d487392016-11-02 14:16:49 +110033
34 Errors, Warnings, etc.
35 Please use:
36 pr_crit() for error conditions that risk data loss
37 pr_err() for error conditions that are unexpected, like an IO error
38 or internal inconsistency
39 pr_warn() for error conditions that could have been predicated, like
40 adding a device to an array when it has incompatible metadata
41 pr_info() for every interesting, very rare events, like an array starting
42 or stopping, or resync starting or stopping
43 pr_debug() for everything else.
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045*/
46
Ingo Molnar3f07c012017-02-08 18:51:30 +010047#include <linux/sched/signal.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070048#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110049#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070050#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040053#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080054#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070055#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080056#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110057#include <linux/hdreg.h>
58#include <linux/proc_fs.h>
59#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040060#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110061#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070062#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110063#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110064#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110065#include <linux/raid/md_p.h>
66#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110068#include <linux/percpu-refcount.h>
69
Shaohua Li504634f2016-11-18 09:44:08 -080070#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110071#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110072#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050073#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110076static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78
NeilBrown01f96c02011-09-21 15:30:20 +100079/* pers_list is a list of registered personalities protected
80 * by pers_lock.
81 * pers_lock does extra service to protect accesses to
82 * mddev->thread when the mutex cannot be held.
83 */
NeilBrown2604b702006-01-06 00:20:36 -080084static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static DEFINE_SPINLOCK(pers_lock);
86
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050087struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050088EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050089struct module *md_cluster_mod;
90EXPORT_SYMBOL(md_cluster_mod);
91
Bernd Schubert90b08712008-05-23 13:04:38 -070092static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020093static struct workqueue_struct *md_wq;
94static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070095
NeilBrown746d3202013-04-24 11:42:41 +100096static int remove_and_add_spares(struct mddev *mddev,
97 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110098static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Robert Becker1e509152009-12-14 12:49:58 +1100101 * Default number of read corrections we'll attempt on an rdev
102 * before ejecting it from the array. We divide the read error
103 * count by 2 for every hour elapsed between read errors.
104 */
105#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
108 * is 1000 KB/sec, so the extra system load does not show up that much.
109 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700110 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * subsystem is idle. There is also an 'absolute maximum' reconstruction
112 * speed limit - in case reconstruction slows down your system despite
113 * idle IO detection.
114 *
115 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800116 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118
119static int sysctl_speed_limit_min = 1000;
120static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100121static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800122{
123 return mddev->sync_speed_min ?
124 mddev->sync_speed_min : sysctl_speed_limit_min;
125}
126
NeilBrownfd01b882011-10-11 16:47:53 +1100127static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800128{
129 return mddev->sync_speed_max ?
130 mddev->sync_speed_max : sysctl_speed_limit_max;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static struct ctl_table_header *raid_table_header;
134
Joe Perches82592c32013-11-14 15:16:18 +1100135static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .procname = "speed_limit_min",
138 .data = &sysctl_speed_limit_min,
139 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800141 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 },
143 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .procname = "speed_limit_max",
145 .data = &sysctl_speed_limit_max,
146 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700147 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800148 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Joe Perches82592c32013-11-14 15:16:18 +1100153static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .procname = "raid",
156 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700157 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .child = raid_table,
159 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800160 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Joe Perches82592c32013-11-14 15:16:18 +1100163static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .procname = "dev",
166 .maxlen = 0,
167 .mode = 0555,
168 .child = raid_dir_table,
169 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800170 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700173static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
NeilBrownf91de922005-11-08 21:39:36 -0800175static int start_readonly;
176
NeilBrowna167f662010-10-26 18:31:13 +1100177/* bio_clone_mddev
178 * like bio_clone, but with a local bio set
179 */
180
NeilBrowna167f662010-10-26 18:31:13 +1100181struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100182 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100183{
184 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100185
186 if (!mddev || !mddev->bio_set)
187 return bio_alloc(gfp_mask, nr_iovecs);
188
Kent Overstreet395c72a2012-09-06 15:34:55 -0700189 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100190 if (!b)
191 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100192 return b;
193}
194EXPORT_SYMBOL_GPL(bio_alloc_mddev);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
NeilBrownd7603b72006-01-06 00:20:30 -0800197 * We have a system wide 'event count' that is incremented
198 * on any 'interesting' event, and readers of /proc/mdstat
199 * can use 'poll' or 'select' to find out when the event
200 * count increases.
201 *
202 * Events are:
203 * start array, stop array, error, add device, remove device,
204 * start build, activate spare
205 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800206static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800207static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100208void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800209{
210 atomic_inc(&md_event_count);
211 wake_up(&md_event_waiters);
212}
NeilBrown29269552006-03-27 01:18:10 -0800213EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800214
215/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Enables to iterate over all existing md arrays
217 * all_mddevs_lock protects this list.
218 */
219static LIST_HEAD(all_mddevs);
220static DEFINE_SPINLOCK(all_mddevs_lock);
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
223 * iterates through all used mddevs in the system.
224 * We take care to grab the all_mddevs_lock whenever navigating
225 * the list, and to always hold a refcount when unlocked.
226 * Any code which breaks out of this loop while own
227 * a reference to the current mddev and must mddev_put it.
228 */
NeilBrownfd01b882011-10-11 16:47:53 +1100229#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000231 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100232 _tmp = all_mddevs.next; \
233 _mddev = NULL;}); \
234 ({ if (_tmp != &all_mddevs) \
235 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100237 if (_mddev) mddev_put(_mddev); \
238 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
239 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100241 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 )
243
NeilBrown409c57f2009-03-31 14:39:39 +1100244/* Rather than calling directly into the personality make_request function,
245 * IO requests come here first so that we can check if the device is
246 * being suspended pending a reconfiguration.
247 * We hold a refcount over the call to ->make_request. By the time that
248 * call has finished, the bio has been linked into some internal structure
249 * and so is visible to ->quiesce(), so we don't need the refcount any more.
250 */
Jens Axboedece1632015-11-05 10:41:16 -0700251static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
NeilBrown49077322010-03-25 16:20:56 +1100253 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100254 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500255 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800256 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100257
Kent Overstreet54efd502015-04-23 22:37:18 -0700258 blk_queue_split(q, &bio, q->bio_split);
259
NeilBrown274d8cb2016-01-04 16:16:58 +1100260 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100261 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700262 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100263 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100264 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200265 if (bio_sectors(bio) != 0)
266 bio->bi_error = -EROFS;
267 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700268 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100269 }
NeilBrown0ca69882011-01-14 09:14:33 +1100270 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100271 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200272 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100273 DEFINE_WAIT(__wait);
274 for (;;) {
275 prepare_to_wait(&mddev->sb_wait, &__wait,
276 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200277 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100278 break;
279 rcu_read_unlock();
280 schedule();
281 rcu_read_lock();
282 }
283 finish_wait(&mddev->sb_wait, &__wait);
284 }
285 atomic_inc(&mddev->active_io);
286 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100287
Chris Masone91ece52011-02-07 19:21:48 -0500288 /*
289 * save the sectors now since our bio can
290 * go away inside make_request
291 */
292 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700293 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600294 bio->bi_opf &= ~REQ_NOMERGE;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200295 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100296
Gu Zheng74672d02015-04-03 08:44:47 +0800297 cpu = part_stat_lock();
298 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
299 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
300 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100301
NeilBrown409c57f2009-03-31 14:39:39 +1100302 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
303 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700304
305 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
NeilBrown9e35b992010-04-06 14:23:02 +1000308/* mddev_suspend makes sure no new requests are submitted
309 * to the device, and that any requests that have been submitted
310 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100311 * Once mddev_detach() is called and completes, the module will be
312 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000313 */
NeilBrownfd01b882011-10-11 16:47:53 +1100314void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100315{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200316 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100317 if (mddev->suspended++)
318 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100319 synchronize_rcu();
320 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
321 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500322
323 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100324}
NeilBrown390ee602010-06-01 19:37:27 +1000325EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100326
NeilBrownfd01b882011-10-11 16:47:53 +1100327void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100328{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100329 if (--mddev->suspended)
330 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100331 wake_up(&mddev->sb_wait);
332 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500333
Jonathan Brassow47525e52012-05-22 13:55:29 +1000334 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500335 md_wakeup_thread(mddev->thread);
336 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100337}
NeilBrown390ee602010-06-01 19:37:27 +1000338EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100339
NeilBrownfd01b882011-10-11 16:47:53 +1100340int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000341{
NeilBrown5c675f82014-12-15 12:56:56 +1100342 struct md_personality *pers = mddev->pers;
343 int ret = 0;
344
345 rcu_read_lock();
346 if (mddev->suspended)
347 ret = 1;
348 else if (pers && pers->congested)
349 ret = pers->congested(mddev, bits);
350 rcu_read_unlock();
351 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000352}
NeilBrown5c675f82014-12-15 12:56:56 +1100353EXPORT_SYMBOL_GPL(mddev_congested);
354static int md_congested(void *data, int bits)
355{
356 struct mddev *mddev = data;
357 return mddev_congested(mddev, bits);
358}
NeilBrown3fa841d2009-09-23 18:10:29 +1000359
NeilBrowna2826aa2009-12-14 12:49:49 +1100360/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200361 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100362 */
363
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200364static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100365{
NeilBrown3cb03002011-10-11 16:45:26 +1100366 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100367 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100368
369 rdev_dec_pending(rdev, mddev);
370
371 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200372 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200373 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100374 }
375 bio_put(bio);
376}
377
NeilBrowna7a07e62010-12-09 16:04:25 +1100378static void md_submit_flush_data(struct work_struct *ws);
379
NeilBrowna035fc32010-12-09 16:17:51 +1100380static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100381{
NeilBrownfd01b882011-10-11 16:47:53 +1100382 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100383 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100384
NeilBrowna7a07e62010-12-09 16:04:25 +1100385 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
386 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100387 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100388 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100389 if (rdev->raid_disk >= 0 &&
390 !test_bit(Faulty, &rdev->flags)) {
391 /* Take two references, one is dropped
392 * when request finishes, one after
393 * we reclaim rcu_read_lock
394 */
395 struct bio *bi;
396 atomic_inc(&rdev->nr_pending);
397 atomic_inc(&rdev->nr_pending);
398 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000399 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200400 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100401 bi->bi_private = rdev;
402 bi->bi_bdev = rdev->bdev;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600403 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrowna2826aa2009-12-14 12:49:49 +1100404 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500405 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100406 rcu_read_lock();
407 rdev_dec_pending(rdev, mddev);
408 }
409 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100410 if (atomic_dec_and_test(&mddev->flush_pending))
411 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100412}
413
Tejun Heoe9c74692010-09-03 11:56:18 +0200414static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100415{
NeilBrownfd01b882011-10-11 16:47:53 +1100416 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200417 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100418
Kent Overstreet4f024f32013-10-11 15:44:27 -0700419 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100420 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200421 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100422 else {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600423 bio->bi_opf &= ~REQ_PREFLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200424 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 }
NeilBrown2b74e122010-12-09 15:59:01 +1100426
427 mddev->flush_bio = NULL;
428 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100429}
430
NeilBrownfd01b882011-10-11 16:47:53 +1100431void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100432{
NeilBrown85572d72014-12-15 12:56:56 +1100433 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100434 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200435 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100436 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200437 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100438 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100439
NeilBrowna035fc32010-12-09 16:17:51 +1100440 INIT_WORK(&mddev->flush_work, submit_flushes);
441 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100442}
Tejun Heoe9c74692010-09-03 11:56:18 +0200443EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100444
NeilBrownfd01b882011-10-11 16:47:53 +1100445static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 atomic_inc(&mddev->active);
448 return mddev;
449}
450
Dan Williams5fd3a172009-03-04 00:57:25 -0700451static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100452
NeilBrownfd01b882011-10-11 16:47:53 +1100453static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
NeilBrowna167f662010-10-26 18:31:13 +1100455 struct bio_set *bs = NULL;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
458 return;
NeilBrownd3374822009-01-09 08:31:10 +1100459 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100460 mddev->ctime == 0 && !mddev->hold_active) {
461 /* Array is not configured at all, and not held active,
462 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100463 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100464 bs = mddev->bio_set;
465 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100466 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200467 /* We did a probe so need to clean up. Call
468 * queue_work inside the spinlock so that
469 * flush_workqueue() after mddev_find will
470 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100471 */
472 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200473 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100474 } else
475 kfree(mddev);
476 }
477 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100478 if (bs)
479 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Sasha Levin25b2edf2015-07-24 18:19:58 -0400482static void md_safemode_timeout(unsigned long data);
483
NeilBrownfd01b882011-10-11 16:47:53 +1100484void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100485{
486 mutex_init(&mddev->open_mutex);
487 mutex_init(&mddev->reconfig_mutex);
488 mutex_init(&mddev->bitmap_info.mutex);
489 INIT_LIST_HEAD(&mddev->disks);
490 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400491 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
492 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100493 atomic_set(&mddev->active, 1);
494 atomic_set(&mddev->openers, 0);
495 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100496 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100497 atomic_set(&mddev->flush_pending, 0);
498 init_waitqueue_head(&mddev->sb_wait);
499 init_waitqueue_head(&mddev->recovery_wait);
500 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000501 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500502 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100503 mddev->resync_min = 0;
504 mddev->resync_max = MaxSector;
505 mddev->level = LEVEL_NONE;
506}
NeilBrown390ee602010-06-01 19:37:27 +1000507EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100508
NeilBrownf72ffdd2014-09-30 14:23:59 +1000509static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
NeilBrownfd01b882011-10-11 16:47:53 +1100511 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
NeilBrown8f5f02c2011-02-16 13:58:51 +1100513 if (unit && MAJOR(unit) != MD_MAJOR)
514 unit &= ~((1<<MdpMinorShift)-1);
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 retry:
517 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
NeilBrownefeb53c2009-01-09 08:31:10 +1100519 if (unit) {
520 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
521 if (mddev->unit == unit) {
522 mddev_get(mddev);
523 spin_unlock(&all_mddevs_lock);
524 kfree(new);
525 return mddev;
526 }
527
528 if (new) {
529 list_add(&new->all_mddevs, &all_mddevs);
530 spin_unlock(&all_mddevs_lock);
531 new->hold_active = UNTIL_IOCTL;
532 return new;
533 }
534 } else if (new) {
535 /* find an unused unit number */
536 static int next_minor = 512;
537 int start = next_minor;
538 int is_free = 0;
539 int dev = 0;
540 while (!is_free) {
541 dev = MKDEV(MD_MAJOR, next_minor);
542 next_minor++;
543 if (next_minor > MINORMASK)
544 next_minor = 0;
545 if (next_minor == start) {
546 /* Oh dear, all in use. */
547 spin_unlock(&all_mddevs_lock);
548 kfree(new);
549 return NULL;
550 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000551
NeilBrownefeb53c2009-01-09 08:31:10 +1100552 is_free = 1;
553 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
554 if (mddev->unit == dev) {
555 is_free = 0;
556 break;
557 }
558 }
559 new->unit = dev;
560 new->md_minor = MINOR(dev);
561 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 list_add(&new->all_mddevs, &all_mddevs);
563 spin_unlock(&all_mddevs_lock);
564 return new;
565 }
566 spin_unlock(&all_mddevs_lock);
567
NeilBrown9ffae0c2006-01-06 00:20:32 -0800568 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!new)
570 return NULL;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 new->unit = unit;
573 if (MAJOR(unit) == MD_MAJOR)
574 new->md_minor = MINOR(unit);
575 else
576 new->md_minor = MINOR(unit) >> MdpMinorShift;
577
NeilBrownfafd7fb2010-04-01 15:55:30 +1100578 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 goto retry;
581}
582
NeilBrownb6eb1272010-04-15 10:13:47 +1000583static struct attribute_group md_redundancy_group;
584
NeilBrown5c47daf2014-12-15 12:57:01 +1100585void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
NeilBrowna64c8762010-04-14 17:15:37 +1000587 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000588 /* These cannot be removed under reconfig_mutex as
589 * an access to the files will try to take reconfig_mutex
590 * while holding the file unremovable, which leads to
591 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000592 * So hold set sysfs_active while the remove in happeing,
593 * and anything else which might set ->to_remove or my
594 * otherwise change the sysfs namespace will fail with
595 * -EBUSY if sysfs_active is still set.
596 * We set sysfs_active under reconfig_mutex and elsewhere
597 * test it under the same mutex to ensure its correct value
598 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000599 */
NeilBrowna64c8762010-04-14 17:15:37 +1000600 struct attribute_group *to_remove = mddev->to_remove;
601 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000602 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000603 mutex_unlock(&mddev->reconfig_mutex);
604
NeilBrown00bcb4a2010-06-01 19:37:23 +1000605 if (mddev->kobj.sd) {
606 if (to_remove != &md_redundancy_group)
607 sysfs_remove_group(&mddev->kobj, to_remove);
608 if (mddev->pers == NULL ||
609 mddev->pers->sync_request == NULL) {
610 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
611 if (mddev->sysfs_action)
612 sysfs_put(mddev->sysfs_action);
613 mddev->sysfs_action = NULL;
614 }
NeilBrowna64c8762010-04-14 17:15:37 +1000615 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000616 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000617 } else
618 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Chris Dunlop751e67c2011-10-19 16:48:26 +1100620 /* As we've dropped the mutex we need a spinlock to
621 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000622 */
623 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700624 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000625 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
NeilBrown5c47daf2014-12-15 12:57:01 +1100627EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500629struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100630{
631 struct md_rdev *rdev;
632
633 rdev_for_each_rcu(rdev, mddev)
634 if (rdev->desc_nr == nr)
635 return rdev;
636
637 return NULL;
638}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500639EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100640
641static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
NeilBrown3cb03002011-10-11 16:45:26 +1100643 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
NeilBrowndafb20f2012-03-19 12:46:39 +1100645 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (rdev->bdev->bd_dev == dev)
647 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return NULL;
650}
651
NeilBrown1ca69c42012-10-11 13:37:33 +1100652static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
653{
654 struct md_rdev *rdev;
655
656 rdev_for_each_rcu(rdev, mddev)
657 if (rdev->bdev->bd_dev == dev)
658 return rdev;
659
660 return NULL;
661}
662
NeilBrown84fc4b52011-10-11 16:49:58 +1100663static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800664{
NeilBrown84fc4b52011-10-11 16:49:58 +1100665 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800666 list_for_each_entry(pers, &pers_list, list) {
667 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800668 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800669 if (strcmp(pers->name, clevel)==0)
670 return pers;
671 }
NeilBrown2604b702006-01-06 00:20:36 -0800672 return NULL;
673}
674
Andre Nollb73df2d2008-07-11 22:02:23 +1000675/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100676static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100678 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000679 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
NeilBrownf72ffdd2014-09-30 14:23:59 +1000682static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100685 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000686 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 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 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800703 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
NeilBrown545c8792012-05-22 13:54:30 +1000705EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200707static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700708{
NeilBrown3cb03002011-10-11 16:45:26 +1100709 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100710 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700711
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200712 if (bio->bi_error) {
NeilBrown9d487392016-11-02 14:16:49 +1100713 pr_err("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800714 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100715 if (!test_bit(Faulty, &rdev->flags)
716 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800717 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100718 set_bit(LastDev, &rdev->flags);
719 }
720 } else
721 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700722
NeilBrowna9701a32005-11-08 21:39:34 -0800723 if (atomic_dec_and_test(&mddev->pending_writes))
724 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700725 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700726 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700727}
728
NeilBrownfd01b882011-10-11 16:47:53 +1100729void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700730 sector_t sector, int size, struct page *page)
731{
732 /* write first size bytes of page to sector of rdev
733 * Increment mddev->pending_writes before returning
734 * and decrement it on completion, waking up sb_wait
735 * if zero is reached.
736 * If an error occurred, call md_error
737 */
NeilBrown46533ff2016-11-18 16:16:11 +1100738 struct bio *bio;
739 int ff = 0;
740
741 if (test_bit(Faulty, &rdev->flags))
742 return;
743
744 bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700745
Shaohua Lied3b98c2016-03-29 14:00:19 -0700746 atomic_inc(&rdev->nr_pending);
747
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100748 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700749 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700750 bio_add_page(bio, page, size, 0);
751 bio->bi_private = rdev;
752 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100753
754 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
755 test_bit(FailFast, &rdev->flags) &&
756 !test_bit(LastDev, &rdev->flags))
757 ff = MD_FAILFAST;
Shaohua Li20737732016-12-13 12:40:15 -0800758 bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800759
NeilBrown7bfa19f2005-06-21 17:17:28 -0700760 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500761 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800762}
763
NeilBrown46533ff2016-11-18 16:16:11 +1100764int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800765{
Tejun Heoe9c74692010-09-03 11:56:18 +0200766 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000767 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800768 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100769 return -EAGAIN;
770 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700771}
772
NeilBrown3cb03002011-10-11 16:45:26 +1100773int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500774 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
NeilBrowna167f662010-10-26 18:31:13 +1100776 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int ret;
778
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100779 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
780 rdev->meta_bdev : rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -0500781 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100782 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700783 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000784 else if (rdev->mddev->reshape_position != MaxSector &&
785 (rdev->mddev->reshape_backwards ==
786 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700787 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100788 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700789 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500791
792 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200794 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 bio_put(bio);
796 return ret;
797}
NeilBrowna8745db2006-01-06 00:20:34 -0800798EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
NeilBrownf72ffdd2014-09-30 14:23:59 +1000800static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (rdev->sb_loaded)
805 return 0;
806
Mike Christie796a5cf2016-06-05 14:32:07 -0500807 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto fail;
809 rdev->sb_loaded = 1;
810 return 0;
811
812fail:
NeilBrown9d487392016-11-02 14:16:49 +1100813 pr_err("md: disabled device %s, could not read superblock.\n",
814 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return -EINVAL;
816}
817
818static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
819{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000820 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000821 sb1->set_uuid1 == sb2->set_uuid1 &&
822 sb1->set_uuid2 == sb2->set_uuid2 &&
823 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
827{
828 int ret;
829 mdp_super_t *tmp1, *tmp2;
830
831 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
832 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
833
834 if (!tmp1 || !tmp2) {
835 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto abort;
837 }
838
839 *tmp1 = *sb1;
840 *tmp2 = *sb2;
841
842 /*
843 * nr_disks is not constant
844 */
845 tmp1->nr_disks = 0;
846 tmp2->nr_disks = 0;
847
Andre Nollce0c8e02008-07-11 22:02:20 +1000848 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700850 kfree(tmp1);
851 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return ret;
853}
854
NeilBrown4d167f02007-05-09 02:35:37 -0700855static u32 md_csum_fold(u32 csum)
856{
857 csum = (csum & 0xffff) + (csum >> 16);
858 return (csum & 0xffff) + (csum >> 16);
859}
860
NeilBrownf72ffdd2014-09-30 14:23:59 +1000861static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
NeilBrown4d167f02007-05-09 02:35:37 -0700863 u64 newcsum = 0;
864 u32 *sb32 = (u32*)sb;
865 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 unsigned int disk_csum, csum;
867
868 disk_csum = sb->sb_csum;
869 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700870
871 for (i = 0; i < MD_SB_BYTES/4 ; i++)
872 newcsum += sb32[i];
873 csum = (newcsum & 0xffffffff) + (newcsum>>32);
874
NeilBrown4d167f02007-05-09 02:35:37 -0700875#ifdef CONFIG_ALPHA
876 /* This used to use csum_partial, which was wrong for several
877 * reasons including that different results are returned on
878 * different architectures. It isn't critical that we get exactly
879 * the same return value as before (we always csum_fold before
880 * testing, and that removes any differences). However as we
881 * know that csum_partial always returned a 16bit value on
882 * alphas, do a fold to maximise conformity to previous behaviour.
883 */
884 sb->sb_csum = md_csum_fold(disk_csum);
885#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700887#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return csum;
889}
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891/*
892 * Handle superblock details.
893 * We want to be able to handle multiple superblock formats
894 * so we have a common interface to them all, and an array of
895 * different handlers.
896 * We rely on user-space to write the initial superblock, and support
897 * reading and updating of superblocks.
898 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100899 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 * loads and validates a superblock on dev.
901 * if refdev != NULL, compare superblocks on both devices
902 * Return:
903 * 0 - dev has a superblock that is compatible with refdev
904 * 1 - dev has a superblock that is compatible and newer than refdev
905 * so dev should be used as the refdev in future
906 * -EINVAL superblock incompatible or invalid
907 * -othererror e.g. -EIO
908 *
NeilBrownfd01b882011-10-11 16:47:53 +1100909 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * Verify that dev is acceptable into mddev.
911 * The first time, mddev->raid_disks will be 0, and data from
912 * dev should be merged in. Subsequent calls check that dev
913 * is new enough. Return 0 or -EINVAL
914 *
NeilBrownfd01b882011-10-11 16:47:53 +1100915 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * Update the superblock for rdev with data in mddev
917 * This does not write to disc.
918 *
919 */
920
921struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000922 char *name;
923 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000924 int (*load_super)(struct md_rdev *rdev,
925 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000926 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000927 int (*validate_super)(struct mddev *mddev,
928 struct md_rdev *rdev);
929 void (*sync_super)(struct mddev *mddev,
930 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100931 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000932 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000933 int (*allow_new_offset)(struct md_rdev *rdev,
934 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935};
936
937/*
Andre Noll0894cc32009-06-18 08:49:23 +1000938 * Check that the given mddev has no bitmap.
939 *
940 * This function is called from the run method of all personalities that do not
941 * support bitmaps. It prints an error message and returns non-zero if mddev
942 * has a bitmap. Otherwise, it returns 0.
943 *
944 */
NeilBrownfd01b882011-10-11 16:47:53 +1100945int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000946{
NeilBrownc3d97142009-12-14 12:49:52 +1100947 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000948 return 0;
NeilBrown9d487392016-11-02 14:16:49 +1100949 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +1000950 mdname(mddev), mddev->pers->name);
951 return 1;
952}
953EXPORT_SYMBOL(md_check_no_bitmap);
954
955/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000956 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
NeilBrown3cb03002011-10-11 16:45:26 +1100958static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
961 mdp_super_t *sb;
962 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /*
Andre Noll0f420352008-07-11 22:02:23 +1000965 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 * it's at the end of the disk.
967 *
968 * It also happens to be a multiple of 4Kb.
969 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100970 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
NeilBrown0002b272005-09-09 16:23:53 -0700972 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +1100973 if (ret)
974 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 ret = -EINVAL;
977
978 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000979 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +1100982 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto abort;
984 }
985
986 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800987 sb->minor_version < 90 ||
988 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +1100989 pr_warn("Bad version number %d.%d on %s\n",
990 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto abort;
992 }
993
994 if (sb->raid_disks <= 0)
995 goto abort;
996
NeilBrown4d167f02007-05-09 02:35:37 -0700997 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +1100998 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 goto abort;
1000 }
1001
1002 rdev->preferred_minor = sb->md_minor;
1003 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001004 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001005 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001006 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 if (sb->level == LEVEL_MULTIPATH)
1009 rdev->desc_nr = -1;
1010 else
1011 rdev->desc_nr = sb->this_disk.number;
1012
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001013 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001015 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001017 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001019 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 b, bdevname(refdev->bdev,b2));
1021 goto abort;
1022 }
1023 if (!sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001024 pr_warn("md: %s has same UUID but different superblock to %s\n",
1025 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 goto abort;
1027 }
1028 ev1 = md_event(sb);
1029 ev2 = md_event(refsb);
1030 if (ev1 > ev2)
1031 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001032 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 ret = 0;
1034 }
NeilBrown8190e752009-06-18 08:48:58 +10001035 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001036 /* Limit to 4TB as metadata cannot record more than that.
1037 * (not needed for Linear and RAID0 as metadata doesn't
1038 * record this size)
1039 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001040 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1041 sb->level >= 1)
1042 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
NeilBrown27a7b262011-09-10 17:21:28 +10001044 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001045 /* "this cannot possibly happen" ... */
1046 ret = -EINVAL;
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 abort:
1049 return ret;
1050}
1051
1052/*
1053 * validate_super for 0.90.0
1054 */
NeilBrownfd01b882011-10-11 16:47:53 +11001055static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001058 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001059 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
NeilBrown41158c72005-06-21 17:17:25 -07001061 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001062 clear_bit(Faulty, &rdev->flags);
1063 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001064 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001065 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (mddev->raid_disks == 0) {
1068 mddev->major_version = 0;
1069 mddev->minor_version = sb->minor_version;
1070 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001071 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001072 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 mddev->ctime = sb->ctime;
1074 mddev->utime = sb->utime;
1075 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001076 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 mddev->layout = sb->layout;
1078 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001079 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001080 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001081 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001082 mddev->bitmap_info.space = 0;
1083 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001084 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001085 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001086 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
NeilBrownf6705572006-03-27 01:18:11 -08001088 if (mddev->minor_version >= 91) {
1089 mddev->reshape_position = sb->reshape_position;
1090 mddev->delta_disks = sb->delta_disks;
1091 mddev->new_level = sb->new_level;
1092 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001093 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001094 if (mddev->delta_disks < 0)
1095 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001096 } else {
1097 mddev->reshape_position = MaxSector;
1098 mddev->delta_disks = 0;
1099 mddev->new_level = mddev->level;
1100 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001101 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001102 }
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (sb->state & (1<<MD_SB_CLEAN))
1105 mddev->recovery_cp = MaxSector;
1106 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001107 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 sb->events_lo == sb->cp_events_lo) {
1109 mddev->recovery_cp = sb->recovery_cp;
1110 } else
1111 mddev->recovery_cp = 0;
1112 }
1113
1114 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1115 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1116 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1117 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1118
1119 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001120
1121 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001122 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001123 mddev->bitmap_info.offset =
1124 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001125 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001126 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001127 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001128
NeilBrown41158c72005-06-21 17:17:25 -07001129 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001130 /* Insist on good event counter while assembling, except
1131 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001133 if (sb->disks[rdev->desc_nr].state & (
1134 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001135 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001136 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001137 } else if (mddev->bitmap) {
1138 /* if adding to array with a bitmap, then we can accept an
1139 * older device ... but not too old.
1140 */
NeilBrown41158c72005-06-21 17:17:25 -07001141 if (ev1 < mddev->bitmap->events_cleared)
1142 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001143 if (ev1 < mddev->events)
1144 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001145 } else {
1146 if (ev1 < mddev->events)
1147 /* just a hot-add of a new device, leave raid_disk at -1 */
1148 return 0;
1149 }
NeilBrown41158c72005-06-21 17:17:25 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 desc = sb->disks + rdev->desc_nr;
1153
1154 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001155 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001156 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1157 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001158 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001160 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001161 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1162 /* active but not in sync implies recovery up to
1163 * reshape position. We don't know exactly where
1164 * that is, so set to zero for now */
1165 if (mddev->minor_version >= 91) {
1166 rdev->recovery_offset = 0;
1167 rdev->raid_disk = desc->raid_disk;
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001170 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1171 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001172 if (desc->state & (1<<MD_DISK_FAILFAST))
1173 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001174 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001175 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return 0;
1177}
1178
1179/*
1180 * sync_super for 0.90.0
1181 */
NeilBrownfd01b882011-10-11 16:47:53 +11001182static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001185 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /* make rdev->sb match mddev data..
1189 *
1190 * 1/ zero out disks
1191 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1192 * 3/ any empty disks < next_spare become removed
1193 *
1194 * disks[0] gets initialised to REMOVED because
1195 * we cannot be sure from other fields if it has
1196 * been initialised or not.
1197 */
1198 int i;
1199 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1200
NeilBrown61181562005-09-09 16:24:02 -07001201 rdev->sb_size = MD_SB_BYTES;
1202
Namhyung Kim65a06f062011-07-27 11:00:36 +10001203 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 memset(sb, 0, sizeof(*sb));
1206
1207 sb->md_magic = MD_SB_MAGIC;
1208 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 sb->patch_version = mddev->patch_version;
1210 sb->gvalid_words = 0; /* ignored */
1211 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1212 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1213 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1214 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1215
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001216 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001218 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 sb->raid_disks = mddev->raid_disks;
1220 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001221 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001222 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 sb->state = 0;
1224 sb->events_hi = (mddev->events>>32);
1225 sb->events_lo = (u32)mddev->events;
1226
NeilBrownf6705572006-03-27 01:18:11 -08001227 if (mddev->reshape_position == MaxSector)
1228 sb->minor_version = 90;
1229 else {
1230 sb->minor_version = 91;
1231 sb->reshape_position = mddev->reshape_position;
1232 sb->new_level = mddev->new_level;
1233 sb->delta_disks = mddev->delta_disks;
1234 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001235 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001236 }
1237 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (mddev->in_sync)
1239 {
1240 sb->recovery_cp = mddev->recovery_cp;
1241 sb->cp_events_hi = (mddev->events>>32);
1242 sb->cp_events_lo = (u32)mddev->events;
1243 if (mddev->recovery_cp == MaxSector)
1244 sb->state = (1<< MD_SB_CLEAN);
1245 } else
1246 sb->recovery_cp = 0;
1247
1248 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001249 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
NeilBrownc3d97142009-12-14 12:49:52 +11001251 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001252 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001255 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001257 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001258 int is_active = test_bit(In_sync, &rdev2->flags);
1259
1260 if (rdev2->raid_disk >= 0 &&
1261 sb->minor_version >= 91)
1262 /* we have nowhere to store the recovery_offset,
1263 * but if it is not below the reshape_position,
1264 * we can piggy-back on that.
1265 */
1266 is_active = 1;
1267 if (rdev2->raid_disk < 0 ||
1268 test_bit(Faulty, &rdev2->flags))
1269 is_active = 0;
1270 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001271 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001273 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001274 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 d = &sb->disks[rdev2->desc_nr];
1276 nr_disks++;
1277 d->number = rdev2->desc_nr;
1278 d->major = MAJOR(rdev2->bdev->bd_dev);
1279 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001280 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 d->raid_disk = rdev2->raid_disk;
1282 else
1283 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001284 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001286 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001288 if (test_bit(In_sync, &rdev2->flags))
1289 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 active++;
1291 working++;
1292 } else {
1293 d->state = 0;
1294 spare++;
1295 working++;
1296 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001297 if (test_bit(WriteMostly, &rdev2->flags))
1298 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001299 if (test_bit(FailFast, &rdev2->flags))
1300 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /* now set the "removed" and "faulty" bits on any missing devices */
1303 for (i=0 ; i < mddev->raid_disks ; i++) {
1304 mdp_disk_t *d = &sb->disks[i];
1305 if (d->state == 0 && d->number == 0) {
1306 d->number = i;
1307 d->raid_disk = i;
1308 d->state = (1<<MD_DISK_REMOVED);
1309 d->state |= (1<<MD_DISK_FAULTY);
1310 failed++;
1311 }
1312 }
1313 sb->nr_disks = nr_disks;
1314 sb->active_disks = active;
1315 sb->working_disks = working;
1316 sb->failed_disks = failed;
1317 sb->spare_disks = spare;
1318
1319 sb->this_disk = sb->disks[rdev->desc_nr];
1320 sb->sb_csum = calc_sb_csum(sb);
1321}
1322
1323/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001324 * rdev_size_change for 0.90.0
1325 */
1326static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001327super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001328{
Andre Noll58c0fed2009-03-31 14:33:13 +11001329 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001330 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001331 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001332 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001333 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001334 if (!num_sectors || num_sectors > rdev->sb_start)
1335 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001336 /* Limit to 4TB as metadata cannot record more than that.
1337 * 4TB == 2^32 KB, or 2*2^32 sectors.
1338 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001339 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1340 rdev->mddev->level >= 1)
1341 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001342 do {
1343 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001344 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001345 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001346 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001347}
1348
NeilBrownc6563a82012-05-21 09:27:00 +10001349static int
1350super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1351{
1352 /* non-zero offset changes not possible with v0.90 */
1353 return new_offset == 0;
1354}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001355
1356/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * version 1 superblock
1358 */
1359
NeilBrownf72ffdd2014-09-30 14:23:59 +10001360static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001362 __le32 disk_csum;
1363 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 unsigned long long newcsum;
1365 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001366 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 disk_csum = sb->sb_csum;
1369 sb->sb_csum = 0;
1370 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001371 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 newcsum += le32_to_cpu(*isuper++);
1373
1374 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001375 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1378 sb->sb_csum = disk_csum;
1379 return cpu_to_le32(csum);
1380}
1381
NeilBrown3cb03002011-10-11 16:45:26 +11001382static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 struct mdp_superblock_1 *sb;
1385 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001386 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001387 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001389 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /*
Andre Noll0f420352008-07-11 22:02:23 +10001392 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * It is always aligned to a 4K boundary and
1394 * depeding on minor_version, it can be:
1395 * 0: At least 8K, but less than 12K, from end of device
1396 * 1: At start of device
1397 * 2: 4K from start of device.
1398 */
1399 switch(minor_version) {
1400 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001401 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001402 sb_start -= 8*2;
1403 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001406 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 break;
1408 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001409 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 break;
1411 default:
1412 return -EINVAL;
1413 }
Andre Noll0f420352008-07-11 22:02:23 +10001414 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
NeilBrown0002b272005-09-09 16:23:53 -07001416 /* superblock is rarely larger than 1K, but it can be larger,
1417 * and it is safe to read 4k, so we do that
1418 */
1419 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (ret) return ret;
1421
Namhyung Kim65a06f062011-07-27 11:00:36 +10001422 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1425 sb->major_version != cpu_to_le32(1) ||
1426 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001427 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001428 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -EINVAL;
1430
1431 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001432 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 bdevname(rdev->bdev,b));
1434 return -EINVAL;
1435 }
1436 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001437 pr_warn("md: data_size too small on %s\n",
1438 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -EINVAL;
1440 }
NeilBrownc6563a82012-05-21 09:27:00 +10001441 if (sb->pad0 ||
1442 sb->pad3[0] ||
1443 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1444 /* Some padding is non-zero, might be a new feature */
1445 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 rdev->preferred_minor = 0xffff;
1448 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001449 rdev->new_data_offset = rdev->data_offset;
1450 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1451 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1452 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001453 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
NeilBrown0002b272005-09-09 16:23:53 -07001455 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001456 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001457 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001458 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1459
1460 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001461 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001462 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001463 if (minor_version
1464 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1465 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001466
NeilBrown31b65a02006-07-10 04:44:14 -07001467 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1468 rdev->desc_nr = -1;
1469 else
1470 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1471
NeilBrown2699b672011-07-28 11:31:47 +10001472 if (!rdev->bb_page) {
1473 rdev->bb_page = alloc_page(GFP_KERNEL);
1474 if (!rdev->bb_page)
1475 return -ENOMEM;
1476 }
1477 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1478 rdev->badblocks.count == 0) {
1479 /* need to load the bad block list.
1480 * Currently we limit it to one page.
1481 */
1482 s32 offset;
1483 sector_t bb_sector;
1484 u64 *bbp;
1485 int i;
1486 int sectors = le16_to_cpu(sb->bblog_size);
1487 if (sectors > (PAGE_SIZE / 512))
1488 return -EINVAL;
1489 offset = le32_to_cpu(sb->bblog_offset);
1490 if (offset == 0)
1491 return -EINVAL;
1492 bb_sector = (long long)offset;
1493 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001494 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001495 return -EIO;
1496 bbp = (u64 *)page_address(rdev->bb_page);
1497 rdev->badblocks.shift = sb->bblog_shift;
1498 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1499 u64 bb = le64_to_cpu(*bbp);
1500 int count = bb & (0x3ff);
1501 u64 sector = bb >> 10;
1502 sector <<= sb->bblog_shift;
1503 count <<= sb->bblog_shift;
1504 if (bb + 1 == 0)
1505 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001506 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001507 return -EINVAL;
1508 }
NeilBrown486adf72013-04-24 11:42:44 +10001509 } else if (sb->bblog_offset != 0)
1510 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001511
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001512 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) {
1513 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1514 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1515 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1516 }
1517
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001518 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001519 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001520 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001522 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1525 sb->level != refsb->level ||
1526 sb->layout != refsb->layout ||
1527 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001528 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 bdevname(rdev->bdev,b),
1530 bdevname(refdev->bdev,b2));
1531 return -EINVAL;
1532 }
1533 ev1 = le64_to_cpu(sb->events);
1534 ev2 = le64_to_cpu(refsb->events);
1535
1536 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001537 ret = 1;
1538 else
1539 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
NeilBrownc6563a82012-05-21 09:27:00 +10001541 if (minor_version) {
1542 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1543 sectors -= rdev->data_offset;
1544 } else
1545 sectors = rdev->sb_start;
1546 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001548 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001549 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
NeilBrownfd01b882011-10-11 16:47:53 +11001552static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001554 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001555 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
NeilBrown41158c72005-06-21 17:17:25 -07001557 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001558 clear_bit(Faulty, &rdev->flags);
1559 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001560 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001561 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (mddev->raid_disks == 0) {
1564 mddev->major_version = 1;
1565 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001566 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001567 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001568 mddev->ctime = le64_to_cpu(sb->ctime);
1569 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001571 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 mddev->layout = le32_to_cpu(sb->layout);
1573 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001574 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001575 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001576 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001577 mddev->bitmap_info.space = 0;
1578 /* Default location for bitmap is 1K after superblock
1579 * using 3K - total of 4K
1580 */
NeilBrownc3d97142009-12-14 12:49:52 +11001581 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001582 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001583 mddev->reshape_backwards = 0;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1586 memcpy(mddev->uuid, sb->set_uuid, 16);
1587
1588 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001589
NeilBrown71c08052005-09-09 16:23:51 -07001590 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001591 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001592 mddev->bitmap_info.offset =
1593 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001594 /* Metadata doesn't record how much space is available.
1595 * For 1.0, we assume we can use up to the superblock
1596 * if before, else to 4K beyond superblock.
1597 * For others, assume no change is possible.
1598 */
1599 if (mddev->minor_version > 0)
1600 mddev->bitmap_info.space = 0;
1601 else if (mddev->bitmap_info.offset > 0)
1602 mddev->bitmap_info.space =
1603 8 - mddev->bitmap_info.offset;
1604 else
1605 mddev->bitmap_info.space =
1606 -mddev->bitmap_info.offset;
1607 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001608
NeilBrownf6705572006-03-27 01:18:11 -08001609 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1610 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1611 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1612 mddev->new_level = le32_to_cpu(sb->new_level);
1613 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001614 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001615 if (mddev->delta_disks < 0 ||
1616 (mddev->delta_disks == 0 &&
1617 (le32_to_cpu(sb->feature_map)
1618 & MD_FEATURE_RESHAPE_BACKWARDS)))
1619 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001620 } else {
1621 mddev->reshape_position = MaxSector;
1622 mddev->delta_disks = 0;
1623 mddev->new_level = mddev->level;
1624 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001625 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001626 }
1627
Song Liu486b0f72016-08-19 15:34:01 -07001628 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001629 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001630
1631 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) {
1632 if (le32_to_cpu(sb->feature_map) &
1633 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1634 return -EINVAL;
1635 set_bit(MD_HAS_PPL, &mddev->flags);
1636 }
NeilBrown41158c72005-06-21 17:17:25 -07001637 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001638 /* Insist of good event counter while assembling, except for
1639 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001641 if (rdev->desc_nr >= 0 &&
1642 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001643 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1644 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001645 if (ev1 < mddev->events)
1646 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001647 } else if (mddev->bitmap) {
1648 /* If adding to array with a bitmap, then we can accept an
1649 * older device, but not too old.
1650 */
NeilBrown41158c72005-06-21 17:17:25 -07001651 if (ev1 < mddev->bitmap->events_cleared)
1652 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001653 if (ev1 < mddev->events)
1654 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001655 } else {
1656 if (ev1 < mddev->events)
1657 /* just a hot-add of a new device, leave raid_disk at -1 */
1658 return 0;
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (mddev->level != LEVEL_MULTIPATH) {
1661 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001662 if (rdev->desc_nr < 0 ||
1663 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001664 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001665 rdev->desc_nr = -1;
1666 } else
1667 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001669 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001671 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001672 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 break;
Song Liubac624f2015-08-13 14:31:55 -07001674 case MD_DISK_ROLE_JOURNAL: /* journal device */
1675 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1676 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001677 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001678 return -EINVAL;
1679 }
1680 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001681 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001682 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 default:
NeilBrownf4667222013-12-09 12:04:56 +11001685 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001686 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001687 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001688 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001689 if (!(le32_to_cpu(sb->feature_map) &
1690 MD_FEATURE_RECOVERY_BITMAP))
1691 rdev->saved_raid_disk = -1;
1692 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001693 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 rdev->raid_disk = role;
1695 break;
1696 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001697 if (sb->devflags & WriteMostly1)
1698 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001699 if (sb->devflags & FailFast1)
1700 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001701 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1702 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001703 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001704 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return 0;
1707}
1708
NeilBrownfd01b882011-10-11 16:47:53 +11001709static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001712 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 int max_dev, i;
1714 /* make rdev->sb match mddev and rdev data. */
1715
Namhyung Kim65a06f062011-07-27 11:00:36 +10001716 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 sb->feature_map = 0;
1719 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001720 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 memset(sb->pad3, 0, sizeof(sb->pad3));
1722
1723 sb->utime = cpu_to_le64((__u64)mddev->utime);
1724 sb->events = cpu_to_le64(mddev->events);
1725 if (mddev->in_sync)
1726 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001727 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1728 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 else
1730 sb->resync_offset = cpu_to_le64(0);
1731
NeilBrown1c05b4b2006-10-21 10:24:08 -07001732 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001733
NeilBrownf0ca3402006-02-02 14:28:04 -08001734 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001735 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001736 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001737 sb->level = cpu_to_le32(mddev->level);
1738 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001739 if (test_bit(FailFast, &rdev->flags))
1740 sb->devflags |= FailFast1;
1741 else
1742 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001743
NeilBrownaeb9b2112011-08-25 14:43:08 +10001744 if (test_bit(WriteMostly, &rdev->flags))
1745 sb->devflags |= WriteMostly1;
1746 else
1747 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001748 sb->data_offset = cpu_to_le64(rdev->data_offset);
1749 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001750
NeilBrownc3d97142009-12-14 12:49:52 +11001751 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1752 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001753 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001754 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001755
Shaohua Lif2076e72015-10-08 21:54:12 -07001756 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001757 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001758 sb->feature_map |=
1759 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1760 sb->recovery_offset =
1761 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001762 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1763 sb->feature_map |=
1764 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001765 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001766 /* Note: recovery_offset and journal_tail share space */
1767 if (test_bit(Journal, &rdev->flags))
1768 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001769 if (test_bit(Replacement, &rdev->flags))
1770 sb->feature_map |=
1771 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001772
NeilBrownf6705572006-03-27 01:18:11 -08001773 if (mddev->reshape_position != MaxSector) {
1774 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1775 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1776 sb->new_layout = cpu_to_le32(mddev->new_layout);
1777 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1778 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001779 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001780 if (mddev->delta_disks == 0 &&
1781 mddev->reshape_backwards)
1782 sb->feature_map
1783 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001784 if (rdev->new_data_offset != rdev->data_offset) {
1785 sb->feature_map
1786 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1787 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1788 - rdev->data_offset));
1789 }
NeilBrownf6705572006-03-27 01:18:11 -08001790 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001791
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001792 if (mddev_is_clustered(mddev))
1793 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1794
NeilBrown2699b672011-07-28 11:31:47 +10001795 if (rdev->badblocks.count == 0)
1796 /* Nothing to do for bad blocks*/ ;
1797 else if (sb->bblog_offset == 0)
1798 /* Cannot record bad blocks on this device */
1799 md_error(mddev, rdev);
1800 else {
1801 struct badblocks *bb = &rdev->badblocks;
1802 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1803 u64 *p = bb->page;
1804 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1805 if (bb->changed) {
1806 unsigned seq;
1807
1808retry:
1809 seq = read_seqbegin(&bb->lock);
1810
1811 memset(bbp, 0xff, PAGE_SIZE);
1812
1813 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001814 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001815 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1816 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001817 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001818 }
NeilBrownd0962932012-03-19 12:46:41 +11001819 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001820 if (read_seqretry(&bb->lock, seq))
1821 goto retry;
1822
1823 bb->sector = (rdev->sb_start +
1824 (int)le32_to_cpu(sb->bblog_offset));
1825 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001826 }
1827 }
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001830 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (rdev2->desc_nr+1 > max_dev)
1832 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001833
NeilBrown70471da2009-08-03 10:59:57 +10001834 if (max_dev > le32_to_cpu(sb->max_dev)) {
1835 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001836 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001837 rdev->sb_size = max_dev * 2 + 256;
1838 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1839 if (rdev->sb_size & bmask)
1840 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001841 } else
1842 max_dev = le32_to_cpu(sb->max_dev);
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001845 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001846
Song Liua97b7892015-10-08 21:54:09 -07001847 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1848 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001850 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
1851 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
1852 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1853 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1854 }
1855
NeilBrowndafb20f2012-03-19 12:46:39 +11001856 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001858 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001859 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001860 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001862 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001863 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001864 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001865 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 else
Song Liuc4d4c912015-08-13 14:31:54 -07001867 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 sb->sb_csum = calc_sb_1_csum(sb);
1871}
1872
Chris Webb0cd17fe2008-06-28 08:31:46 +10001873static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001874super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001875{
1876 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001877 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001878 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001879 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001880 if (rdev->data_offset != rdev->new_data_offset)
1881 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001882 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001883 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001884 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001885 max_sectors -= rdev->data_offset;
1886 if (!num_sectors || num_sectors > max_sectors)
1887 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001888 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001889 /* minor version 0 with bitmap we can't move */
1890 return 0;
1891 } else {
1892 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001893 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001894 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001895 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001896 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001897 if (!num_sectors || num_sectors > max_sectors)
1898 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001899 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001900 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001901 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001902 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08001903 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10001904 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11001905 do {
1906 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1907 rdev->sb_page);
1908 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001909 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001910
1911}
1912
1913static int
1914super_1_allow_new_offset(struct md_rdev *rdev,
1915 unsigned long long new_offset)
1916{
1917 /* All necessary checks on new >= old have been done */
1918 struct bitmap *bitmap;
1919 if (new_offset >= rdev->data_offset)
1920 return 1;
1921
1922 /* with 1.0 metadata, there is no metadata to tread on
1923 * so we can always move back */
1924 if (rdev->mddev->minor_version == 0)
1925 return 1;
1926
1927 /* otherwise we must be sure not to step on
1928 * any metadata, so stay:
1929 * 36K beyond start of superblock
1930 * beyond end of badblocks
1931 * beyond write-intent bitmap
1932 */
1933 if (rdev->sb_start + (32+4)*2 > new_offset)
1934 return 0;
1935 bitmap = rdev->mddev->bitmap;
1936 if (bitmap && !rdev->mddev->bitmap_info.file &&
1937 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001938 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001939 return 0;
1940 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1941 return 0;
1942
1943 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001944}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Adrian Bunk75c96f82005-05-05 16:16:09 -07001946static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 [0] = {
1948 .name = "0.90.0",
1949 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001950 .load_super = super_90_load,
1951 .validate_super = super_90_validate,
1952 .sync_super = super_90_sync,
1953 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001954 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 },
1956 [1] = {
1957 .name = "md-1",
1958 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001959 .load_super = super_1_load,
1960 .validate_super = super_1_validate,
1961 .sync_super = super_1_sync,
1962 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001963 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 },
1965};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
NeilBrownfd01b882011-10-11 16:47:53 +11001967static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001968{
1969 if (mddev->sync_super) {
1970 mddev->sync_super(mddev, rdev);
1971 return;
1972 }
1973
1974 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1975
1976 super_types[mddev->major_version].sync_super(mddev, rdev);
1977}
1978
NeilBrownfd01b882011-10-11 16:47:53 +11001979static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
NeilBrown3cb03002011-10-11 16:45:26 +11001981 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
NeilBrown4b809912008-07-21 17:05:25 +10001983 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001984 rdev_for_each_rcu(rdev, mddev1) {
1985 if (test_bit(Faulty, &rdev->flags) ||
1986 test_bit(Journal, &rdev->flags) ||
1987 rdev->raid_disk == -1)
1988 continue;
1989 rdev_for_each_rcu(rdev2, mddev2) {
1990 if (test_bit(Faulty, &rdev2->flags) ||
1991 test_bit(Journal, &rdev2->flags) ||
1992 rdev2->raid_disk == -1)
1993 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001994 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001995 rdev2->bdev->bd_contains) {
1996 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001997 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001998 }
Song Liu0b020e82015-09-03 23:00:35 -07001999 }
2000 }
NeilBrown4b809912008-07-21 17:05:25 +10002001 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return 0;
2003}
2004
2005static LIST_HEAD(pending_raid_disks);
2006
Andre Nollac5e7112009-08-03 10:59:47 +10002007/*
2008 * Try to register data integrity profile for an mddev
2009 *
2010 * This is called when an array is started and after a disk has been kicked
2011 * from the array. It only succeeds if all working and active component devices
2012 * are integrity capable with matching profiles.
2013 */
NeilBrownfd01b882011-10-11 16:47:53 +11002014int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002015{
NeilBrown3cb03002011-10-11 16:45:26 +11002016 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002017
Andre Nollac5e7112009-08-03 10:59:47 +10002018 if (list_empty(&mddev->disks))
2019 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002020 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2021 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002022 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002023 /* skip spares and non-functional disks */
2024 if (test_bit(Faulty, &rdev->flags))
2025 continue;
2026 if (rdev->raid_disk < 0)
2027 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002028 if (!reference) {
2029 /* Use the first rdev as the reference */
2030 reference = rdev;
2031 continue;
2032 }
2033 /* does this rdev's profile match the reference profile? */
2034 if (blk_integrity_compare(reference->bdev->bd_disk,
2035 rdev->bdev->bd_disk) < 0)
2036 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002037 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002038 if (!reference || !bdev_get_integrity(reference->bdev))
2039 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002040 /*
2041 * All component devices are integrity capable and have matching
2042 * profiles, register the common profile for the md device.
2043 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002044 blk_integrity_register(mddev->gendisk,
2045 bdev_get_integrity(reference->bdev));
2046
NeilBrown9d487392016-11-02 14:16:49 +11002047 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Martin K. Petersena91a2782011-03-17 11:11:05 +01002048 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002049 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002050 mdname(mddev));
2051 return -EINVAL;
2052 }
Andre Nollac5e7112009-08-03 10:59:47 +10002053 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002054}
Andre Nollac5e7112009-08-03 10:59:47 +10002055EXPORT_SYMBOL(md_integrity_register);
2056
Dan Williams1501efa2016-01-13 16:00:07 -08002057/*
2058 * Attempt to add an rdev, but only if it is consistent with the current
2059 * integrity profile
2060 */
2061int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002062{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002063 struct blk_integrity *bi_rdev;
2064 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002065 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002066
2067 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002068 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002069
2070 bi_rdev = bdev_get_integrity(rdev->bdev);
2071 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002072
2073 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002074 return 0;
2075
2076 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002077 pr_err("%s: incompatible integrity profile for %s\n",
2078 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002079 return -ENXIO;
2080 }
2081
2082 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002083}
2084EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002085
NeilBrownf72ffdd2014-09-30 14:23:59 +10002086static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002088 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002089 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002090 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Dan Williams11e2ede2008-04-30 00:52:32 -07002092 /* prevent duplicates */
2093 if (find_rdev(mddev, rdev->bdev->bd_dev))
2094 return -EEXIST;
2095
Andre Nolldd8ac332009-03-31 14:33:13 +11002096 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002097 if (!test_bit(Journal, &rdev->flags) &&
2098 rdev->sectors &&
2099 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002100 if (mddev->pers) {
2101 /* Cannot change size, so fail
2102 * If mddev->level <= 0, then we don't care
2103 * about aligning sizes (e.g. linear)
2104 */
2105 if (mddev->level > 0)
2106 return -ENOSPC;
2107 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002108 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 /* Verify rdev->desc_nr is unique.
2112 * If it is -1, assign a free number, else
2113 * check number is not in use
2114 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002115 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (rdev->desc_nr < 0) {
2117 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002118 if (mddev->pers)
2119 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002120 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 choice++;
2122 rdev->desc_nr = choice;
2123 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002124 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002125 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002129 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002130 if (!test_bit(Journal, &rdev->flags) &&
2131 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002132 pr_warn("md: %s: array is limited to %d devices\n",
2133 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002134 return -EBUSY;
2135 }
NeilBrown19133a42005-11-08 21:39:35 -08002136 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002137 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002140 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002141
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002142 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002143 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002144
Tejun Heo0762b8b2008-08-25 19:56:12 +09002145 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002146 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2147 /* failure here is OK */;
2148 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002149
NeilBrown4b809912008-07-21 17:05:25 +10002150 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002151 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002152
2153 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002154 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002157
2158 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002159 pr_warn("md: failed to register dev-%s for %s\n",
2160 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002161 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
NeilBrown177a99b2008-02-06 01:39:56 -08002164static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002165{
NeilBrown3cb03002011-10-11 16:45:26 +11002166 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002167 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002168 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002169}
2170
NeilBrownf72ffdd2014-09-30 14:23:59 +10002171static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002174
Tejun Heo49731ba2011-01-14 18:43:57 +01002175 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002176 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002177 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002179 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002180 sysfs_put(rdev->sysfs_state);
2181 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002182 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002183 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002184 * writing to 'remove' to "dev/state". We also need
2185 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002186 */
NeilBrown4b809912008-07-21 17:05:25 +10002187 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002188 INIT_WORK(&rdev->del_work, md_delayed_delete);
2189 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002190 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
2193/*
2194 * prevent the device from being mounted, repartitioned or
2195 * otherwise reused by a RAID array (or any other kernel
2196 * subsystem), by bd_claiming the device.
2197 */
NeilBrown3cb03002011-10-11 16:45:26 +11002198static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 int err = 0;
2201 struct block_device *bdev;
2202 char b[BDEVNAME_SIZE];
2203
Tejun Heod4d77622010-11-13 11:55:18 +01002204 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002205 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002207 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return PTR_ERR(bdev);
2209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 rdev->bdev = bdev;
2211 return err;
2212}
2213
NeilBrown3cb03002011-10-11 16:45:26 +11002214static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 struct block_device *bdev = rdev->bdev;
2217 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002218 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
2220
2221void md_autodetect_dev(dev_t dev);
2222
NeilBrownf72ffdd2014-09-30 14:23:59 +10002223static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002226
NeilBrown9d487392016-11-02 14:16:49 +11002227 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002228 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002230 if (test_bit(AutoDetected, &rdev->flags))
2231 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232#endif
2233 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002234 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002237void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
2239 unbind_rdev_from_array(rdev);
2240 export_rdev(rdev);
2241}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002242EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
NeilBrownfd01b882011-10-11 16:47:53 +11002244static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
NeilBrown0638bb02014-09-25 17:43:47 +10002246 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
NeilBrown0638bb02014-09-25 17:43:47 +10002248 while (!list_empty(&mddev->disks)) {
2249 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2250 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002251 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 mddev->raid_disks = 0;
2254 mddev->major_version = 0;
2255}
2256
NeilBrown6497709b2017-03-15 14:05:14 +11002257static bool set_in_sync(struct mddev *mddev)
2258{
2259 WARN_ON_ONCE(!spin_is_locked(&mddev->lock));
NeilBrown4ad23a972017-03-15 14:05:14 +11002260 if (!mddev->in_sync) {
2261 mddev->sync_checkers++;
2262 spin_unlock(&mddev->lock);
2263 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2264 spin_lock(&mddev->lock);
2265 if (!mddev->in_sync &&
2266 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002267 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002268 /*
2269 * Ensure ->in_sync is visible before we clear
2270 * ->sync_checkers.
2271 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002272 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002273 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2274 sysfs_notify_dirent_safe(mddev->sysfs_state);
2275 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002276 if (--mddev->sync_checkers == 0)
2277 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002278 }
2279 if (mddev->safemode == 1)
2280 mddev->safemode = 0;
2281 return mddev->in_sync;
2282}
2283
NeilBrownf72ffdd2014-09-30 14:23:59 +10002284static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
NeilBrown42543762006-06-26 00:27:57 -07002286 /* Update each superblock (in-memory image), but
2287 * if we are allowed to, skip spares which already
2288 * have the right event counter, or have one earlier
2289 * (which would mean they aren't being marked as dirty
2290 * with the rest of the array)
2291 */
NeilBrown3cb03002011-10-11 16:45:26 +11002292 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002293 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002294 if (rdev->sb_events == mddev->events ||
2295 (nospares &&
2296 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002297 rdev->sb_events+1 == mddev->events)) {
2298 /* Don't update this superblock */
2299 rdev->sb_loaded = 2;
2300 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002301 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002302 rdev->sb_loaded = 1;
2303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305}
2306
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002307static bool does_sb_need_changing(struct mddev *mddev)
2308{
2309 struct md_rdev *rdev;
2310 struct mdp_superblock_1 *sb;
2311 int role;
2312
2313 /* Find a good rdev */
2314 rdev_for_each(rdev, mddev)
2315 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2316 break;
2317
2318 /* No good device found. */
2319 if (!rdev)
2320 return false;
2321
2322 sb = page_address(rdev->sb_page);
2323 /* Check if a device has become faulty or a spare become active */
2324 rdev_for_each(rdev, mddev) {
2325 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2326 /* Device activated? */
2327 if (role == 0xffff && rdev->raid_disk >=0 &&
2328 !test_bit(Faulty, &rdev->flags))
2329 return true;
2330 /* Device turned faulty? */
2331 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2332 return true;
2333 }
2334
2335 /* Check if any mddev parameters have changed */
2336 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2337 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002338 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002339 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2340 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2341 return true;
2342
2343 return false;
2344}
2345
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002346void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
NeilBrown3cb03002011-10-11 16:45:26 +11002348 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002349 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002350 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002351 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002352 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
NeilBrownd87f0642013-04-24 11:42:40 +10002354 if (mddev->ro) {
2355 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002356 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002357 return;
2358 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002359
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002360repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002361 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002362 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002363 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002364 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002365 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002366 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002367 /* Has someone else has updated the sb */
2368 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002369 if (ret == 0)
2370 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002371 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2372 BIT(MD_SB_CHANGE_DEVS) |
2373 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002374 return;
2375 }
2376 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002377
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002378 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002379 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002380 if (rdev->raid_disk >= 0 &&
2381 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002382 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002383 !test_bit(In_sync, &rdev->flags) &&
2384 mddev->curr_resync_completed > rdev->recovery_offset)
2385 rdev->recovery_offset = mddev->curr_resync_completed;
2386
NeilBrownf72ffdd2014-09-30 14:23:59 +10002387 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002388 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002389 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2390 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002391 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002392 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002393 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002394 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002395 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002396 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002397 md_error(mddev, rdev);
2398 }
2399 clear_bit(Blocked, &rdev->flags);
2400 clear_bit(BlockedBadBlocks, &rdev->flags);
2401 wake_up(&rdev->blocked_wait);
2402 }
2403 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002404 wake_up(&mddev->sb_wait);
2405 return;
2406 }
2407
NeilBrown85572d72014-12-15 12:56:56 +11002408 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002409
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002410 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002411
Shaohua Li29530792016-12-08 15:48:19 -08002412 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002413 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002414 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002415 /* just a clean<-> dirty transition, possibly leave spares alone,
2416 * though if events isn't the right even/odd, we will have to do
2417 * spares after all
2418 */
2419 nospares = 1;
2420 if (force_change)
2421 nospares = 0;
2422 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002423 /* If the array is degraded, then skipping spares is both
2424 * dangerous and fairly pointless.
2425 * Dangerous because a device that was removed from the array
2426 * might have a event_count that still looks up-to-date,
2427 * so it can be re-added without a resync.
2428 * Pointless because if there are any spares to skip,
2429 * then a recovery will happen and soon that array won't
2430 * be degraded any more and the spare can go back to sleep then.
2431 */
NeilBrown850b2b422006-10-03 01:15:46 -07002432 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002433
NeilBrown06d91a52005-06-21 17:17:12 -07002434 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002435
2436 /* If this is just a dirty<->clean transition, and the array is clean
2437 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002438 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002439 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002440 && mddev->can_decrease_events
2441 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002442 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002443 mddev->can_decrease_events = 0;
2444 } else {
NeilBrown42543762006-06-26 00:27:57 -07002445 /* otherwise we have to go forward and ... */
2446 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002447 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
NeilBrown403df472014-09-30 15:52:29 +10002450 /*
2451 * This 64-bit counter should never wrap.
2452 * Either we are in around ~1 trillion A.C., assuming
2453 * 1 reboot per second, or we have a bug...
2454 */
2455 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002456
NeilBrowndafb20f2012-03-19 12:46:39 +11002457 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002458 if (rdev->badblocks.changed)
2459 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002460 if (test_bit(Faulty, &rdev->flags))
2461 set_bit(FaultRecorded, &rdev->flags);
2462 }
NeilBrown2699b672011-07-28 11:31:47 +10002463
NeilBrowne6910632008-02-06 01:39:51 -08002464 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002465 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
NeilBrown36a4e1f2011-10-07 14:23:17 +11002467 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2468 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Shaohua Li504634f2016-11-18 09:44:08 -08002470 if (mddev->queue)
2471 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002472rewrite:
NeilBrown4ad13662007-07-17 04:06:13 -07002473 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002474 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002476
NeilBrown42543762006-06-26 00:27:57 -07002477 if (rdev->sb_loaded != 1)
2478 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
NeilBrownf4667222013-12-09 12:04:56 +11002480 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002481 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002482 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002483 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002484 pr_debug("md: (write) %s's sb offset: %llu\n",
2485 bdevname(rdev->bdev, b),
2486 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002487 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002488 if (rdev->badblocks.size) {
2489 md_super_write(mddev, rdev,
2490 rdev->badblocks.sector,
2491 rdev->badblocks.size << 9,
2492 rdev->bb_page);
2493 rdev->badblocks.size = 0;
2494 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002495
NeilBrownf4667222013-12-09 12:04:56 +11002496 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002497 pr_debug("md: %s (skipping faulty)\n",
2498 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002499
NeilBrown7bfa19f2005-06-21 17:17:28 -07002500 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 /* only need to write one superblock... */
2502 break;
2503 }
NeilBrown46533ff2016-11-18 16:16:11 +11002504 if (md_super_wait(mddev) < 0)
2505 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002506 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002507
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002508 if (mddev_is_clustered(mddev) && ret == 0)
2509 md_cluster_ops->metadata_update_finish(mddev);
2510
NeilBrown850b2b422006-10-03 01:15:46 -07002511 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002512 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2513 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002514 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002515 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002516 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002517 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2518 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002519
NeilBrowndafb20f2012-03-19 12:46:39 +11002520 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002521 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2522 clear_bit(Blocked, &rdev->flags);
2523
2524 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002525 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002526 clear_bit(BlockedBadBlocks, &rdev->flags);
2527 wake_up(&rdev->blocked_wait);
2528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002530EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002532static int add_bound_rdev(struct md_rdev *rdev)
2533{
2534 struct mddev *mddev = rdev->mddev;
2535 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002536 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002537
Shaohua Li87d4d912016-01-06 14:37:14 -08002538 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002539 /* If there is hot_add_disk but no hot_remove_disk
2540 * then added disks for geometry changes,
2541 * and should be added immediately.
2542 */
2543 super_types[mddev->major_version].
2544 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002545 if (add_journal)
2546 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002547 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002548 if (add_journal)
2549 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002550 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002551 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002552 return err;
2553 }
2554 }
2555 sysfs_notify_dirent_safe(rdev->sysfs_state);
2556
Shaohua Li29530792016-12-08 15:48:19 -08002557 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002558 if (mddev->degraded)
2559 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2560 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2561 md_new_event(mddev);
2562 md_wakeup_thread(mddev->thread);
2563 return 0;
2564}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Andre Noll7f6ce762008-03-23 18:34:54 +01002566/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002567 * We want to accept with case. For this we use cmd_match.
2568 */
2569static int cmd_match(const char *cmd, const char *str)
2570{
2571 /* See if cmd, written into a sysfs file, matches
2572 * str. They must either be the same, or cmd can
2573 * have a trailing newline
2574 */
2575 while (*cmd && *str && *cmd == *str) {
2576 cmd++;
2577 str++;
2578 }
2579 if (*cmd == '\n')
2580 cmd++;
2581 if (*str || *cmd)
2582 return 0;
2583 return 1;
2584}
2585
NeilBrown86e6ffd2005-11-08 21:39:24 -08002586struct rdev_sysfs_entry {
2587 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002588 ssize_t (*show)(struct md_rdev *, char *);
2589 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002590};
2591
2592static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002593state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002594{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002595 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002596 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002597 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002598
NeilBrown758bfc82014-12-15 12:56:59 +11002599 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002600 (!test_bit(ExternalBbl, &flags) &&
2601 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002602 len += sprintf(page+len, "faulty%s", sep);
2603 if (test_bit(In_sync, &flags))
2604 len += sprintf(page+len, "in_sync%s", sep);
2605 if (test_bit(Journal, &flags))
2606 len += sprintf(page+len, "journal%s", sep);
2607 if (test_bit(WriteMostly, &flags))
2608 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002609 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002610 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002611 && !test_bit(Faulty, &flags)))
2612 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002613 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002614 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002615 !test_bit(In_sync, &flags))
2616 len += sprintf(page+len, "spare%s", sep);
2617 if (test_bit(WriteErrorSeen, &flags))
2618 len += sprintf(page+len, "write_error%s", sep);
2619 if (test_bit(WantReplacement, &flags))
2620 len += sprintf(page+len, "want_replacement%s", sep);
2621 if (test_bit(Replacement, &flags))
2622 len += sprintf(page+len, "replacement%s", sep);
2623 if (test_bit(ExternalBbl, &flags))
2624 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002625 if (test_bit(FailFast, &flags))
2626 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002627
2628 if (len)
2629 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002630
NeilBrown86e6ffd2005-11-08 21:39:24 -08002631 return len+sprintf(page+len, "\n");
2632}
2633
NeilBrown45dc2de2006-06-26 00:27:58 -07002634static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002635state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002636{
2637 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002638 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002639 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002640 * writemostly - sets write_mostly
2641 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002642 * blocked - sets the Blocked flags
2643 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002644 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002645 * -insync - clear Insync for a device with a slot assigned,
2646 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002647 * write_error - sets WriteErrorSeen
2648 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002649 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002650 */
2651 int err = -EINVAL;
2652 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2653 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002654 if (test_bit(Faulty, &rdev->flags))
2655 err = 0;
2656 else
2657 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002658 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002659 if (rdev->mddev->pers) {
2660 clear_bit(Blocked, &rdev->flags);
2661 remove_and_add_spares(rdev->mddev, rdev);
2662 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002663 if (rdev->raid_disk >= 0)
2664 err = -EBUSY;
2665 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002666 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002667 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002668 if (mddev_is_clustered(mddev))
2669 err = md_cluster_ops->remove_disk(mddev, rdev);
2670
2671 if (err == 0) {
2672 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002673 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002674 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002675 md_wakeup_thread(mddev->thread);
2676 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002677 md_new_event(mddev);
2678 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002679 }
NeilBrownf6556752006-06-26 00:28:01 -07002680 } else if (cmd_match(buf, "writemostly")) {
2681 set_bit(WriteMostly, &rdev->flags);
2682 err = 0;
2683 } else if (cmd_match(buf, "-writemostly")) {
2684 clear_bit(WriteMostly, &rdev->flags);
2685 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002686 } else if (cmd_match(buf, "blocked")) {
2687 set_bit(Blocked, &rdev->flags);
2688 err = 0;
2689 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002690 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002691 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002692 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002693 /* metadata handler doesn't understand badblocks,
2694 * so we need to fail the device
2695 */
2696 md_error(rdev->mddev, rdev);
2697 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002698 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002699 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002700 wake_up(&rdev->blocked_wait);
2701 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2702 md_wakeup_thread(rdev->mddev->thread);
2703
2704 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002705 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2706 set_bit(In_sync, &rdev->flags);
2707 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002708 } else if (cmd_match(buf, "failfast")) {
2709 set_bit(FailFast, &rdev->flags);
2710 err = 0;
2711 } else if (cmd_match(buf, "-failfast")) {
2712 clear_bit(FailFast, &rdev->flags);
2713 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002714 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2715 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002716 if (rdev->mddev->pers == NULL) {
2717 clear_bit(In_sync, &rdev->flags);
2718 rdev->saved_raid_disk = rdev->raid_disk;
2719 rdev->raid_disk = -1;
2720 err = 0;
2721 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002722 } else if (cmd_match(buf, "write_error")) {
2723 set_bit(WriteErrorSeen, &rdev->flags);
2724 err = 0;
2725 } else if (cmd_match(buf, "-write_error")) {
2726 clear_bit(WriteErrorSeen, &rdev->flags);
2727 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002728 } else if (cmd_match(buf, "want_replacement")) {
2729 /* Any non-spare device that is not a replacement can
2730 * become want_replacement at any time, but we then need to
2731 * check if recovery is needed.
2732 */
2733 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002734 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002735 !test_bit(Replacement, &rdev->flags))
2736 set_bit(WantReplacement, &rdev->flags);
2737 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2738 md_wakeup_thread(rdev->mddev->thread);
2739 err = 0;
2740 } else if (cmd_match(buf, "-want_replacement")) {
2741 /* Clearing 'want_replacement' is always allowed.
2742 * Once replacements starts it is too late though.
2743 */
2744 err = 0;
2745 clear_bit(WantReplacement, &rdev->flags);
2746 } else if (cmd_match(buf, "replacement")) {
2747 /* Can only set a device as a replacement when array has not
2748 * yet been started. Once running, replacement is automatic
2749 * from spares, or by assigning 'slot'.
2750 */
2751 if (rdev->mddev->pers)
2752 err = -EBUSY;
2753 else {
2754 set_bit(Replacement, &rdev->flags);
2755 err = 0;
2756 }
2757 } else if (cmd_match(buf, "-replacement")) {
2758 /* Similarly, can only clear Replacement before start */
2759 if (rdev->mddev->pers)
2760 err = -EBUSY;
2761 else {
2762 clear_bit(Replacement, &rdev->flags);
2763 err = 0;
2764 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002765 } else if (cmd_match(buf, "re-add")) {
2766 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002767 /* clear_bit is performed _after_ all the devices
2768 * have their local Faulty bit cleared. If any writes
2769 * happen in the meantime in the local node, they
2770 * will land in the local bitmap, which will be synced
2771 * by this node eventually
2772 */
2773 if (!mddev_is_clustered(rdev->mddev) ||
2774 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2775 clear_bit(Faulty, &rdev->flags);
2776 err = add_bound_rdev(rdev);
2777 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002778 } else
2779 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002780 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2781 set_bit(ExternalBbl, &rdev->flags);
2782 rdev->badblocks.shift = 0;
2783 err = 0;
2784 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2785 clear_bit(ExternalBbl, &rdev->flags);
2786 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002787 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002788 if (!err)
2789 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002790 return err ? err : len;
2791}
NeilBrown80ca3a42006-07-10 04:44:18 -07002792static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002793__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002794
2795static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002796errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002797{
2798 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2799}
2800
2801static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002802errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002803{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002804 unsigned int n;
2805 int rv;
2806
2807 rv = kstrtouint(buf, 10, &n);
2808 if (rv < 0)
2809 return rv;
2810 atomic_set(&rdev->corrected_errors, n);
2811 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002812}
2813static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002814__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002815
NeilBrown014236d2006-01-06 00:20:55 -08002816static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002817slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002818{
Shaohua Lif2076e72015-10-08 21:54:12 -07002819 if (test_bit(Journal, &rdev->flags))
2820 return sprintf(page, "journal\n");
2821 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002822 return sprintf(page, "none\n");
2823 else
2824 return sprintf(page, "%d\n", rdev->raid_disk);
2825}
2826
2827static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002828slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002829{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002830 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002831 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002832
Shaohua Lif2076e72015-10-08 21:54:12 -07002833 if (test_bit(Journal, &rdev->flags))
2834 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002835 if (strncmp(buf, "none", 4)==0)
2836 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002837 else {
2838 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2839 if (err < 0)
2840 return err;
2841 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002842 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002843 /* Setting 'slot' on an active array requires also
2844 * updating the 'rd%d' link, and communicating
2845 * with the personality with ->hot_*_disk.
2846 * For now we only support removing
2847 * failed/spare devices. This normally happens automatically,
2848 * but not when the metadata is externally managed.
2849 */
NeilBrownc303da62008-02-06 01:39:51 -08002850 if (rdev->raid_disk == -1)
2851 return -EEXIST;
2852 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002853 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002854 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002855 clear_bit(Blocked, &rdev->flags);
2856 remove_and_add_spares(rdev->mddev, rdev);
2857 if (rdev->raid_disk >= 0)
2858 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002859 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2860 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002861 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002862 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002863 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002864 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002865 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002866
2867 if (rdev->raid_disk != -1)
2868 return -EBUSY;
2869
NeilBrownc6751b22011-02-02 11:57:13 +11002870 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2871 return -EBUSY;
2872
Neil Brown6c2fce22008-06-28 08:31:31 +10002873 if (rdev->mddev->pers->hot_add_disk == NULL)
2874 return -EINVAL;
2875
NeilBrownba1b41b2011-01-14 09:14:34 +11002876 if (slot >= rdev->mddev->raid_disks &&
2877 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2878 return -ENOSPC;
2879
Neil Brown6c2fce22008-06-28 08:31:31 +10002880 rdev->raid_disk = slot;
2881 if (test_bit(In_sync, &rdev->flags))
2882 rdev->saved_raid_disk = slot;
2883 else
2884 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002885 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002886 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002887 err = rdev->mddev->pers->
2888 hot_add_disk(rdev->mddev, rdev);
2889 if (err) {
2890 rdev->raid_disk = -1;
2891 return err;
2892 } else
2893 sysfs_notify_dirent_safe(rdev->sysfs_state);
2894 if (sysfs_link_rdev(rdev->mddev, rdev))
2895 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002896 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002897 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002898 if (slot >= rdev->mddev->raid_disks &&
2899 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002900 return -ENOSPC;
2901 rdev->raid_disk = slot;
2902 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002903 clear_bit(Faulty, &rdev->flags);
2904 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002905 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002906 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002907 }
NeilBrown014236d2006-01-06 00:20:55 -08002908 return len;
2909}
2910
NeilBrown014236d2006-01-06 00:20:55 -08002911static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002912__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002913
NeilBrown93c8cad2006-01-06 00:20:56 -08002914static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002915offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002916{
Andrew Morton6961ece2006-01-06 00:20:59 -08002917 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002918}
2919
2920static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002921offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002922{
NeilBrownc6563a82012-05-21 09:27:00 +10002923 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002924 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002925 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002926 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002927 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002928 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002929 /* Must set offset before size, so overlap checks
2930 * can be sane */
2931 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002932 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002933 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002934 return len;
2935}
2936
2937static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002938__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002939
NeilBrownc6563a82012-05-21 09:27:00 +10002940static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2941{
2942 return sprintf(page, "%llu\n",
2943 (unsigned long long)rdev->new_data_offset);
2944}
2945
2946static ssize_t new_offset_store(struct md_rdev *rdev,
2947 const char *buf, size_t len)
2948{
2949 unsigned long long new_offset;
2950 struct mddev *mddev = rdev->mddev;
2951
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002952 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002953 return -EINVAL;
2954
NeilBrownf851b602014-12-11 10:02:10 +11002955 if (mddev->sync_thread ||
2956 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002957 return -EBUSY;
2958 if (new_offset == rdev->data_offset)
2959 /* reset is always permitted */
2960 ;
2961 else if (new_offset > rdev->data_offset) {
2962 /* must not push array size beyond rdev_sectors */
2963 if (new_offset - rdev->data_offset
2964 + mddev->dev_sectors > rdev->sectors)
2965 return -E2BIG;
2966 }
2967 /* Metadata worries about other space details. */
2968
2969 /* decreasing the offset is inconsistent with a backwards
2970 * reshape.
2971 */
2972 if (new_offset < rdev->data_offset &&
2973 mddev->reshape_backwards)
2974 return -EINVAL;
2975 /* Increasing offset is inconsistent with forwards
2976 * reshape. reshape_direction should be set to
2977 * 'backwards' first.
2978 */
2979 if (new_offset > rdev->data_offset &&
2980 !mddev->reshape_backwards)
2981 return -EINVAL;
2982
2983 if (mddev->pers && mddev->persistent &&
2984 !super_types[mddev->major_version]
2985 .allow_new_offset(rdev, new_offset))
2986 return -E2BIG;
2987 rdev->new_data_offset = new_offset;
2988 if (new_offset > rdev->data_offset)
2989 mddev->reshape_backwards = 1;
2990 else if (new_offset < rdev->data_offset)
2991 mddev->reshape_backwards = 0;
2992
2993 return len;
2994}
2995static struct rdev_sysfs_entry rdev_new_offset =
2996__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2997
NeilBrown83303b62006-01-06 00:21:06 -08002998static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002999rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003000{
Andre Nolldd8ac332009-03-31 14:33:13 +11003001 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003002}
3003
NeilBrownc5d79ad2008-02-06 01:39:54 -08003004static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3005{
3006 /* check if two start/length pairs overlap */
3007 if (s1+l1 <= s2)
3008 return 0;
3009 if (s2+l2 <= s1)
3010 return 0;
3011 return 1;
3012}
3013
Dan Williamsb522adc2009-03-31 15:00:31 +11003014static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3015{
3016 unsigned long long blocks;
3017 sector_t new;
3018
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003019 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003020 return -EINVAL;
3021
3022 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3023 return -EINVAL; /* sector conversion overflow */
3024
3025 new = blocks * 2;
3026 if (new != blocks * 2)
3027 return -EINVAL; /* unsigned long long to sector_t overflow */
3028
3029 *sectors = new;
3030 return 0;
3031}
3032
NeilBrown83303b62006-01-06 00:21:06 -08003033static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003034rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003035{
NeilBrownfd01b882011-10-11 16:47:53 +11003036 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003037 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003038 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003039
Shaohua Lif2076e72015-10-08 21:54:12 -07003040 if (test_bit(Journal, &rdev->flags))
3041 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003042 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003043 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003044 if (rdev->data_offset != rdev->new_data_offset)
3045 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003046 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003047 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003048 sectors = super_types[my_mddev->major_version].
3049 rdev_size_change(rdev, sectors);
3050 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003051 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003052 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003053 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003054 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003055 if (!my_mddev->pers->resize)
3056 /* Cannot change size for RAID0 or Linear etc */
3057 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003058 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003059 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003060 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003061
Andre Nolldd8ac332009-03-31 14:33:13 +11003062 rdev->sectors = sectors;
3063 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003064 /* Need to check that all other rdevs with the same
3065 * ->bdev do not overlap. 'rcu' is sufficient to walk
3066 * the rdev lists safely.
3067 * This check does not provide a hard guarantee, it
3068 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003069 */
NeilBrownfd01b882011-10-11 16:47:53 +11003070 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003071 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003072 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003073
NeilBrown8b1afc32014-09-29 15:33:20 +10003074 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003075 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003076 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003077
NeilBrowndafb20f2012-03-19 12:46:39 +11003078 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003079 if (rdev->bdev == rdev2->bdev &&
3080 rdev != rdev2 &&
3081 overlaps(rdev->data_offset, rdev->sectors,
3082 rdev2->data_offset,
3083 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003084 overlap = 1;
3085 break;
3086 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003087 if (overlap) {
3088 mddev_put(mddev);
3089 break;
3090 }
3091 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003092 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003093 if (overlap) {
3094 /* Someone else could have slipped in a size
3095 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003096 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003097 * safe, and trust userspace not to race with
3098 * itself
3099 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003100 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003101 return -EBUSY;
3102 }
3103 }
NeilBrown83303b62006-01-06 00:21:06 -08003104 return len;
3105}
3106
3107static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003108__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003109
NeilBrown3cb03002011-10-11 16:45:26 +11003110static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003111{
3112 unsigned long long recovery_start = rdev->recovery_offset;
3113
3114 if (test_bit(In_sync, &rdev->flags) ||
3115 recovery_start == MaxSector)
3116 return sprintf(page, "none\n");
3117
3118 return sprintf(page, "%llu\n", recovery_start);
3119}
3120
NeilBrown3cb03002011-10-11 16:45:26 +11003121static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003122{
3123 unsigned long long recovery_start;
3124
3125 if (cmd_match(buf, "none"))
3126 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003127 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003128 return -EINVAL;
3129
3130 if (rdev->mddev->pers &&
3131 rdev->raid_disk >= 0)
3132 return -EBUSY;
3133
3134 rdev->recovery_offset = recovery_start;
3135 if (recovery_start == MaxSector)
3136 set_bit(In_sync, &rdev->flags);
3137 else
3138 clear_bit(In_sync, &rdev->flags);
3139 return len;
3140}
3141
3142static struct rdev_sysfs_entry rdev_recovery_start =
3143__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3144
Vishal Vermafc974ee2015-12-24 19:20:34 -07003145/* sysfs access to bad-blocks list.
3146 * We present two files.
3147 * 'bad-blocks' lists sector numbers and lengths of ranges that
3148 * are recorded as bad. The list is truncated to fit within
3149 * the one-page limit of sysfs.
3150 * Writing "sector length" to this file adds an acknowledged
3151 * bad block list.
3152 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3153 * been acknowledged. Writing to this file adds bad blocks
3154 * without acknowledging them. This is largely for testing.
3155 */
NeilBrown3cb03002011-10-11 16:45:26 +11003156static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003157{
3158 return badblocks_show(&rdev->badblocks, page, 0);
3159}
NeilBrown3cb03002011-10-11 16:45:26 +11003160static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003161{
NeilBrownde393cd2011-07-28 11:31:48 +10003162 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3163 /* Maybe that ack was all we needed */
3164 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3165 wake_up(&rdev->blocked_wait);
3166 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003167}
3168static struct rdev_sysfs_entry rdev_bad_blocks =
3169__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3170
NeilBrown3cb03002011-10-11 16:45:26 +11003171static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003172{
3173 return badblocks_show(&rdev->badblocks, page, 1);
3174}
NeilBrown3cb03002011-10-11 16:45:26 +11003175static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003176{
3177 return badblocks_store(&rdev->badblocks, page, len, 1);
3178}
3179static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3180__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3181
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003182static ssize_t
3183ppl_sector_show(struct md_rdev *rdev, char *page)
3184{
3185 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3186}
3187
3188static ssize_t
3189ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3190{
3191 unsigned long long sector;
3192
3193 if (kstrtoull(buf, 10, &sector) < 0)
3194 return -EINVAL;
3195 if (sector != (sector_t)sector)
3196 return -EINVAL;
3197
3198 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3199 rdev->raid_disk >= 0)
3200 return -EBUSY;
3201
3202 if (rdev->mddev->persistent) {
3203 if (rdev->mddev->major_version == 0)
3204 return -EINVAL;
3205 if ((sector > rdev->sb_start &&
3206 sector - rdev->sb_start > S16_MAX) ||
3207 (sector < rdev->sb_start &&
3208 rdev->sb_start - sector > -S16_MIN))
3209 return -EINVAL;
3210 rdev->ppl.offset = sector - rdev->sb_start;
3211 } else if (!rdev->mddev->external) {
3212 return -EBUSY;
3213 }
3214 rdev->ppl.sector = sector;
3215 return len;
3216}
3217
3218static struct rdev_sysfs_entry rdev_ppl_sector =
3219__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3220
3221static ssize_t
3222ppl_size_show(struct md_rdev *rdev, char *page)
3223{
3224 return sprintf(page, "%u\n", rdev->ppl.size);
3225}
3226
3227static ssize_t
3228ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3229{
3230 unsigned int size;
3231
3232 if (kstrtouint(buf, 10, &size) < 0)
3233 return -EINVAL;
3234
3235 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3236 rdev->raid_disk >= 0)
3237 return -EBUSY;
3238
3239 if (rdev->mddev->persistent) {
3240 if (rdev->mddev->major_version == 0)
3241 return -EINVAL;
3242 if (size > U16_MAX)
3243 return -EINVAL;
3244 } else if (!rdev->mddev->external) {
3245 return -EBUSY;
3246 }
3247 rdev->ppl.size = size;
3248 return len;
3249}
3250
3251static struct rdev_sysfs_entry rdev_ppl_size =
3252__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3253
NeilBrown86e6ffd2005-11-08 21:39:24 -08003254static struct attribute *rdev_default_attrs[] = {
3255 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003256 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003257 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003258 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003259 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003260 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003261 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003262 &rdev_bad_blocks.attr,
3263 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003264 &rdev_ppl_sector.attr,
3265 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003266 NULL,
3267};
3268static ssize_t
3269rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3270{
3271 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003272 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003273
3274 if (!entry->show)
3275 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003276 if (!rdev->mddev)
3277 return -EBUSY;
3278 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003279}
3280
3281static ssize_t
3282rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3283 const char *page, size_t length)
3284{
3285 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003286 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003287 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003288 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003289
3290 if (!entry->store)
3291 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003292 if (!capable(CAP_SYS_ADMIN))
3293 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003294 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003295 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003296 if (rdev->mddev == NULL)
3297 rv = -EBUSY;
3298 else
3299 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003300 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003301 }
3302 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003303}
3304
3305static void rdev_free(struct kobject *ko)
3306{
NeilBrown3cb03002011-10-11 16:45:26 +11003307 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003308 kfree(rdev);
3309}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003310static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003311 .show = rdev_attr_show,
3312 .store = rdev_attr_store,
3313};
3314static struct kobj_type rdev_ktype = {
3315 .release = rdev_free,
3316 .sysfs_ops = &rdev_sysfs_ops,
3317 .default_attrs = rdev_default_attrs,
3318};
3319
NeilBrown3cb03002011-10-11 16:45:26 +11003320int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003321{
3322 rdev->desc_nr = -1;
3323 rdev->saved_raid_disk = -1;
3324 rdev->raid_disk = -1;
3325 rdev->flags = 0;
3326 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003327 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003328 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003329 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003330 rdev->sb_loaded = 0;
3331 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003332 atomic_set(&rdev->nr_pending, 0);
3333 atomic_set(&rdev->read_errors, 0);
3334 atomic_set(&rdev->corrected_errors, 0);
3335
3336 INIT_LIST_HEAD(&rdev->same_set);
3337 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003338
3339 /* Add space to store bad block list.
3340 * This reserves the space even on arrays where it cannot
3341 * be used - I wonder if that matters
3342 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003343 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003344}
3345EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346/*
3347 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3348 *
3349 * mark the device faulty if:
3350 *
3351 * - the device is nonexistent (zero size)
3352 * - the device has no valid superblock
3353 *
3354 * a faulty rdev _never_ has rdev->sb set.
3355 */
NeilBrown3cb03002011-10-11 16:45:26 +11003356static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
3358 char b[BDEVNAME_SIZE];
3359 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003360 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 sector_t size;
3362
NeilBrown9ffae0c2006-01-06 00:20:32 -08003363 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003364 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
NeilBrown2230dfe2011-07-28 11:31:46 +10003367 err = md_rdev_init(rdev);
3368 if (err)
3369 goto abort_free;
3370 err = alloc_disk_sb(rdev);
3371 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 goto abort_free;
3373
NeilBrownc5d79ad2008-02-06 01:39:54 -08003374 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 if (err)
3376 goto abort_free;
3377
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003378 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003379
Mike Snitzer77304d22010-11-08 14:39:12 +01003380 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003382 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 bdevname(rdev->bdev,b));
3384 err = -EINVAL;
3385 goto abort_free;
3386 }
3387
3388 if (super_format >= 0) {
3389 err = super_types[super_format].
3390 load_super(rdev, NULL, super_minor);
3391 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003392 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003393 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003394 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 goto abort_free;
3396 }
3397 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003398 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 bdevname(rdev->bdev,b));
3400 goto abort_free;
3401 }
3402 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return rdev;
3405
3406abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003407 if (rdev->bdev)
3408 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003409 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 kfree(rdev);
3411 return ERR_PTR(err);
3412}
3413
3414/*
3415 * Check a full RAID array for plausibility
3416 */
3417
NeilBrownf72ffdd2014-09-30 14:23:59 +10003418static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
3420 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003421 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 char b[BDEVNAME_SIZE];
3423
3424 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003425 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 switch (super_types[mddev->major_version].
3427 load_super(rdev, freshest, mddev->minor_version)) {
3428 case 1:
3429 freshest = rdev;
3430 break;
3431 case 0:
3432 break;
3433 default:
NeilBrown9d487392016-11-02 14:16:49 +11003434 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003436 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 }
3438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 super_types[mddev->major_version].
3440 validate_super(mddev, freshest);
3441
3442 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003443 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003444 if (mddev->max_disks &&
3445 (rdev->desc_nr >= mddev->max_disks ||
3446 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003447 pr_warn("md: %s: %s: only %d devices permitted\n",
3448 mdname(mddev), bdevname(rdev->bdev, b),
3449 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003450 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003451 continue;
3452 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003453 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 if (super_types[mddev->major_version].
3455 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003456 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003458 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 continue;
3460 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (mddev->level == LEVEL_MULTIPATH) {
3463 rdev->desc_nr = i++;
3464 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003465 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003466 } else if (rdev->raid_disk >=
3467 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3468 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003469 rdev->raid_disk = -1;
3470 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
3472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473}
3474
NeilBrown72e02072009-12-14 12:49:55 +11003475/* Read a fixed-point number.
3476 * Numbers in sysfs attributes should be in "standard" units where
3477 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003478 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003479 * milliseconds or jiffies.
3480 * This function takes a decimal number with a possible fractional
3481 * component, and produces an integer which is the result of
3482 * multiplying that number by 10^'scale'.
3483 * all without any floating-point arithmetic.
3484 */
3485int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3486{
3487 unsigned long result = 0;
3488 long decimals = -1;
3489 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3490 if (*cp == '.')
3491 decimals = 0;
3492 else if (decimals < scale) {
3493 unsigned int value;
3494 value = *cp - '0';
3495 result = result * 10 + value;
3496 if (decimals >= 0)
3497 decimals++;
3498 }
3499 cp++;
3500 }
3501 if (*cp == '\n')
3502 cp++;
3503 if (*cp)
3504 return -EINVAL;
3505 if (decimals < 0)
3506 decimals = 0;
3507 while (decimals < scale) {
3508 result *= 10;
3509 decimals ++;
3510 }
3511 *res = result;
3512 return 0;
3513}
3514
NeilBrowneae17012005-11-08 21:39:23 -08003515static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003516safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003517{
3518 int msec = (mddev->safemode_delay*1000)/HZ;
3519 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3520}
3521static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003522safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003523{
NeilBrown16f17b32006-06-26 00:27:37 -07003524 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003525
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003526 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003527 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003528 return -EINVAL;
3529 }
3530
NeilBrown72e02072009-12-14 12:49:55 +11003531 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003532 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003533 if (msec == 0)
3534 mddev->safemode_delay = 0;
3535 else {
NeilBrown19052c02008-08-05 15:54:13 +10003536 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003537 unsigned long new_delay = (msec*HZ)/1000;
3538
3539 if (new_delay == 0)
3540 new_delay = 1;
3541 mddev->safemode_delay = new_delay;
3542 if (new_delay < old_delay || old_delay == 0)
3543 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003544 }
3545 return len;
3546}
3547static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003548__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003549
3550static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003551level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003552{
NeilBrown36d091f2014-12-15 12:56:58 +11003553 struct md_personality *p;
3554 int ret;
3555 spin_lock(&mddev->lock);
3556 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003557 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003558 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003559 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003560 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003561 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003562 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003563 else
NeilBrown36d091f2014-12-15 12:56:58 +11003564 ret = 0;
3565 spin_unlock(&mddev->lock);
3566 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003567}
3568
NeilBrownd9d166c2006-01-06 00:20:51 -08003569static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003570level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003571{
Dan Williamsf2859af2010-05-02 10:04:16 -07003572 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003573 ssize_t rv;
3574 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003575 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003576 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003577 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003578 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003579
NeilBrown67918752014-12-15 12:57:01 +11003580 if (slen == 0 || slen >= sizeof(clevel))
3581 return -EINVAL;
3582
3583 rv = mddev_lock(mddev);
3584 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003585 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003586
3587 if (mddev->pers == NULL) {
3588 strncpy(mddev->clevel, buf, slen);
3589 if (mddev->clevel[slen-1] == '\n')
3590 slen--;
3591 mddev->clevel[slen] = 0;
3592 mddev->level = LEVEL_NONE;
3593 rv = len;
3594 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003595 }
NeilBrown67918752014-12-15 12:57:01 +11003596 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003597 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003598 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003599
3600 /* request to change the personality. Need to ensure:
3601 * - array is not engaged in resync/recovery/reshape
3602 * - old personality can be suspended
3603 * - new personality will access other array.
3604 */
3605
NeilBrown67918752014-12-15 12:57:01 +11003606 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003607 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003608 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003609 mddev->reshape_position != MaxSector ||
3610 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003611 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003612
NeilBrown67918752014-12-15 12:57:01 +11003613 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003614 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003615 pr_warn("md: %s: %s does not support online personality change\n",
3616 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003617 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003618 }
3619
3620 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003621 strncpy(clevel, buf, slen);
3622 if (clevel[slen-1] == '\n')
3623 slen--;
3624 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003625 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003626 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003627
Dan Williamsf2859af2010-05-02 10:04:16 -07003628 if (request_module("md-%s", clevel) != 0)
3629 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003630 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003631 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003632 if (!pers || !try_module_get(pers->owner)) {
3633 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003634 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003635 rv = -EINVAL;
3636 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003637 }
3638 spin_unlock(&pers_lock);
3639
3640 if (pers == mddev->pers) {
3641 /* Nothing to do! */
3642 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003643 rv = len;
3644 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003645 }
3646 if (!pers->takeover) {
3647 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003648 pr_warn("md: %s: %s does not support personality takeover\n",
3649 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003650 rv = -EINVAL;
3651 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003652 }
3653
NeilBrowndafb20f2012-03-19 12:46:39 +11003654 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003655 rdev->new_raid_disk = rdev->raid_disk;
3656
NeilBrown245f46c2009-03-31 14:39:39 +11003657 /* ->takeover must set new_* and/or delta_disks
3658 * if it succeeds, and may set them when it fails.
3659 */
3660 priv = pers->takeover(mddev);
3661 if (IS_ERR(priv)) {
3662 mddev->new_level = mddev->level;
3663 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003664 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003665 mddev->raid_disks -= mddev->delta_disks;
3666 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003667 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003668 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003669 pr_warn("md: %s: %s would not accept array\n",
3670 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003671 rv = PTR_ERR(priv);
3672 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003673 }
3674
3675 /* Looks like we have a winner */
3676 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003677 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003678
3679 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003680 oldpers = mddev->pers;
3681 oldpriv = mddev->private;
3682 mddev->pers = pers;
3683 mddev->private = priv;
3684 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3685 mddev->level = mddev->new_level;
3686 mddev->layout = mddev->new_layout;
3687 mddev->chunk_sectors = mddev->new_chunk_sectors;
3688 mddev->delta_disks = 0;
3689 mddev->reshape_backwards = 0;
3690 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003691 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003692
NeilBrowndb721d32014-12-15 12:56:58 +11003693 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003694 mddev->external) {
3695 /* We are converting from a no-redundancy array
3696 * to a redundancy array and metadata is managed
3697 * externally so we need to be sure that writes
3698 * won't block due to a need to transition
3699 * clean->dirty
3700 * until external management is started.
3701 */
3702 mddev->in_sync = 0;
3703 mddev->safemode_delay = 0;
3704 mddev->safemode = 0;
3705 }
3706
NeilBrowndb721d32014-12-15 12:56:58 +11003707 oldpers->free(mddev, oldpriv);
3708
3709 if (oldpers->sync_request == NULL &&
3710 pers->sync_request != NULL) {
3711 /* need to add the md_redundancy_group */
3712 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003713 pr_warn("md: cannot register extra attributes for %s\n",
3714 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003715 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3716 }
3717 if (oldpers->sync_request != NULL &&
3718 pers->sync_request == NULL) {
3719 /* need to remove the md_redundancy_group */
3720 if (mddev->to_remove == NULL)
3721 mddev->to_remove = &md_redundancy_group;
3722 }
3723
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003724 module_put(oldpers->owner);
3725
NeilBrowndafb20f2012-03-19 12:46:39 +11003726 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003727 if (rdev->raid_disk < 0)
3728 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003729 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003730 rdev->new_raid_disk = -1;
3731 if (rdev->new_raid_disk == rdev->raid_disk)
3732 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003733 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003734 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003735 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003736 if (rdev->raid_disk < 0)
3737 continue;
3738 if (rdev->new_raid_disk == rdev->raid_disk)
3739 continue;
3740 rdev->raid_disk = rdev->new_raid_disk;
3741 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003742 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003743 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003744 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003745 pr_warn("md: cannot register rd%d for %s after level change\n",
3746 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003747 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003748 }
3749
NeilBrowndb721d32014-12-15 12:56:58 +11003750 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003751 /* this is now an array without redundancy, so
3752 * it must always be in_sync
3753 */
3754 mddev->in_sync = 1;
3755 del_timer_sync(&mddev->safemode_timer);
3756 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003757 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003758 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003759 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003760 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003761 if (!mddev->thread)
3762 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003763 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003764 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003765 rv = len;
3766out_unlock:
3767 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003768 return rv;
3769}
3770
3771static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003772__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003773
NeilBrownd4dbd022006-06-26 00:27:59 -07003774static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003775layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003776{
3777 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003778 if (mddev->reshape_position != MaxSector &&
3779 mddev->layout != mddev->new_layout)
3780 return sprintf(page, "%d (%d)\n",
3781 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003782 return sprintf(page, "%d\n", mddev->layout);
3783}
3784
3785static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003786layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003787{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003788 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003789 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003790
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003791 err = kstrtouint(buf, 10, &n);
3792 if (err < 0)
3793 return err;
NeilBrown67918752014-12-15 12:57:01 +11003794 err = mddev_lock(mddev);
3795 if (err)
3796 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003797
NeilBrownb3546032009-03-31 14:56:41 +11003798 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003799 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003800 err = -EBUSY;
3801 else if (mddev->ro)
3802 err = -EROFS;
3803 else {
3804 mddev->new_layout = n;
3805 err = mddev->pers->check_reshape(mddev);
3806 if (err)
3807 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003808 }
NeilBrownb3546032009-03-31 14:56:41 +11003809 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003810 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003811 if (mddev->reshape_position == MaxSector)
3812 mddev->layout = n;
3813 }
NeilBrown67918752014-12-15 12:57:01 +11003814 mddev_unlock(mddev);
3815 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003816}
3817static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003818__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003819
NeilBrowneae17012005-11-08 21:39:23 -08003820static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003821raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003822{
NeilBrownbb636542005-11-08 21:39:45 -08003823 if (mddev->raid_disks == 0)
3824 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003825 if (mddev->reshape_position != MaxSector &&
3826 mddev->delta_disks != 0)
3827 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3828 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003829 return sprintf(page, "%d\n", mddev->raid_disks);
3830}
3831
NeilBrownfd01b882011-10-11 16:47:53 +11003832static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003833
3834static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003835raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003836{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003837 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003838 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003839
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003840 err = kstrtouint(buf, 10, &n);
3841 if (err < 0)
3842 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003843
NeilBrown67918752014-12-15 12:57:01 +11003844 err = mddev_lock(mddev);
3845 if (err)
3846 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003847 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003848 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003849 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003850 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003851 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003852
NeilBrown67918752014-12-15 12:57:01 +11003853 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003854 rdev_for_each(rdev, mddev) {
3855 if (olddisks < n &&
3856 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003857 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003858 if (olddisks > n &&
3859 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003860 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003861 }
NeilBrown67918752014-12-15 12:57:01 +11003862 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003863 mddev->delta_disks = n - olddisks;
3864 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003865 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003866 } else
NeilBrownda943b992006-01-06 00:20:54 -08003867 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003868out_unlock:
3869 mddev_unlock(mddev);
3870 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003871}
3872static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003873__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003874
NeilBrown24dd4692005-11-08 21:39:26 -08003875static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003876chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003877{
NeilBrown08a02ec2007-05-09 02:35:38 -07003878 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003879 mddev->chunk_sectors != mddev->new_chunk_sectors)
3880 return sprintf(page, "%d (%d)\n",
3881 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003882 mddev->chunk_sectors << 9);
3883 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003884}
3885
3886static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003887chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003888{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003889 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003890 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003891
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003892 err = kstrtoul(buf, 10, &n);
3893 if (err < 0)
3894 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003895
NeilBrown67918752014-12-15 12:57:01 +11003896 err = mddev_lock(mddev);
3897 if (err)
3898 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003899 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003900 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003901 err = -EBUSY;
3902 else if (mddev->ro)
3903 err = -EROFS;
3904 else {
3905 mddev->new_chunk_sectors = n >> 9;
3906 err = mddev->pers->check_reshape(mddev);
3907 if (err)
3908 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003909 }
NeilBrownb3546032009-03-31 14:56:41 +11003910 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003911 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003912 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003913 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003914 }
NeilBrown67918752014-12-15 12:57:01 +11003915 mddev_unlock(mddev);
3916 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003917}
3918static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003919__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003920
NeilBrowna94213b2006-06-26 00:28:00 -07003921static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003922resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003923{
NeilBrownd1a7c502009-03-31 15:24:32 +11003924 if (mddev->recovery_cp == MaxSector)
3925 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003926 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3927}
3928
3929static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003930resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003931{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003932 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003933 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003934
3935 if (cmd_match(buf, "none"))
3936 n = MaxSector;
3937 else {
3938 err = kstrtoull(buf, 10, &n);
3939 if (err < 0)
3940 return err;
3941 if (n != (sector_t)n)
3942 return -EINVAL;
3943 }
NeilBrowna94213b2006-06-26 00:28:00 -07003944
NeilBrown67918752014-12-15 12:57:01 +11003945 err = mddev_lock(mddev);
3946 if (err)
3947 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003948 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003949 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003950
NeilBrown67918752014-12-15 12:57:01 +11003951 if (!err) {
3952 mddev->recovery_cp = n;
3953 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08003954 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11003955 }
3956 mddev_unlock(mddev);
3957 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003958}
3959static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003960__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3961 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003962
NeilBrown9e653b62006-06-26 00:27:58 -07003963/*
3964 * The array state can be:
3965 *
3966 * clear
3967 * No devices, no size, no level
3968 * Equivalent to STOP_ARRAY ioctl
3969 * inactive
3970 * May have some settings, but array is not active
3971 * all IO results in error
3972 * When written, doesn't tear down array, but just stops it
3973 * suspended (not supported yet)
3974 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003975 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003976 * readonly
3977 * no resync can happen. no superblocks get written.
3978 * write requests fail
3979 * read-auto
3980 * like readonly, but behaves like 'clean' on a write request.
3981 *
3982 * clean - no pending writes, but otherwise active.
3983 * When written to inactive array, starts without resync
3984 * If a write request arrives then
3985 * if metadata is known, mark 'dirty' and switch to 'active'.
3986 * if not known, block and switch to write-pending
3987 * If written to an active array that has pending writes, then fails.
3988 * active
3989 * fully active: IO and resync can be happening.
3990 * When written to inactive array, starts with resync
3991 *
3992 * write-pending
3993 * clean, but writes are blocked waiting for 'active' to be written.
3994 *
3995 * active-idle
3996 * like active, but no writes have been seen for a while (100msec).
3997 *
3998 */
3999enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4000 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004001static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004002 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4003 "write-pending", "active-idle", NULL };
4004
4005static int match_word(const char *word, char **list)
4006{
4007 int n;
4008 for (n=0; list[n]; n++)
4009 if (cmd_match(word, list[n]))
4010 break;
4011 return n;
4012}
4013
4014static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004015array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004016{
4017 enum array_state st = inactive;
4018
4019 if (mddev->pers)
4020 switch(mddev->ro) {
4021 case 1:
4022 st = readonly;
4023 break;
4024 case 2:
4025 st = read_auto;
4026 break;
4027 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004028 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004029 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004030 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004031 else if (mddev->in_sync)
4032 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004033 else if (mddev->safemode)
4034 st = active_idle;
4035 else
4036 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004037 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004038 }
4039 else {
4040 if (list_empty(&mddev->disks) &&
4041 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004042 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004043 st = clear;
4044 else
4045 st = inactive;
4046 }
4047 return sprintf(page, "%s\n", array_states[st]);
4048}
4049
NeilBrownf72ffdd2014-09-30 14:23:59 +10004050static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4051static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4052static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004053static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004054
4055static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004056array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004057{
NeilBrown6497709b2017-03-15 14:05:14 +11004058 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004059 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004060
4061 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4062 /* don't take reconfig_mutex when toggling between
4063 * clean and active
4064 */
4065 spin_lock(&mddev->lock);
4066 if (st == active) {
4067 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004068 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004069 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004070 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004071 } else /* st == clean */ {
4072 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004073 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004074 err = -EBUSY;
4075 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004076 if (!err)
4077 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004078 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004079 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004080 }
4081 err = mddev_lock(mddev);
4082 if (err)
4083 return err;
4084 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004085 switch(st) {
4086 case bad_word:
4087 break;
4088 case clear:
4089 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004090 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004091 break;
4092 case inactive:
4093 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004094 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004095 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004096 else
NeilBrowne6910632008-02-06 01:39:51 -08004097 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004098 break;
4099 case suspended:
4100 break; /* not supported yet */
4101 case readonly:
4102 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004103 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004104 else {
4105 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004106 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004107 err = do_md_run(mddev);
4108 }
4109 break;
4110 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004111 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004112 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004113 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004114 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004115 err = restart_array(mddev);
4116 if (err == 0) {
4117 mddev->ro = 2;
4118 set_disk_ro(mddev->gendisk, 0);
4119 }
NeilBrown9e653b62006-06-26 00:27:58 -07004120 } else {
4121 mddev->ro = 2;
4122 err = do_md_run(mddev);
4123 }
4124 break;
4125 case clean:
4126 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004127 err = restart_array(mddev);
4128 if (err)
4129 break;
NeilBrown85572d72014-12-15 12:56:56 +11004130 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004131 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004132 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004133 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004134 } else
4135 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004136 break;
4137 case active:
4138 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004139 err = restart_array(mddev);
4140 if (err)
4141 break;
Shaohua Li29530792016-12-08 15:48:19 -08004142 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004143 wake_up(&mddev->sb_wait);
4144 err = 0;
4145 } else {
4146 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004147 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004148 err = do_md_run(mddev);
4149 }
4150 break;
4151 case write_pending:
4152 case active_idle:
4153 /* these cannot be set */
4154 break;
4155 }
NeilBrown67918752014-12-15 12:57:01 +11004156
4157 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004158 if (mddev->hold_active == UNTIL_IOCTL)
4159 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004160 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004161 }
NeilBrown67918752014-12-15 12:57:01 +11004162 mddev_unlock(mddev);
4163 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004164}
NeilBrown80ca3a42006-07-10 04:44:18 -07004165static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004166__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004167
NeilBrown6d7ff7382006-01-06 00:21:16 -08004168static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004169max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004170 return sprintf(page, "%d\n",
4171 atomic_read(&mddev->max_corr_read_errors));
4172}
4173
4174static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004175max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004176{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004177 unsigned int n;
4178 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004179
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004180 rv = kstrtouint(buf, 10, &n);
4181 if (rv < 0)
4182 return rv;
4183 atomic_set(&mddev->max_corr_read_errors, n);
4184 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004185}
4186
4187static struct md_sysfs_entry max_corr_read_errors =
4188__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4189 max_corrected_read_errors_store);
4190
4191static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004192null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004193{
4194 return -EINVAL;
4195}
4196
4197static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004198new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004199{
4200 /* buf must be %d:%d\n? giving major and minor numbers */
4201 /* The new device is added to the array.
4202 * If the array has a persistent superblock, we read the
4203 * superblock to initialise info and check validity.
4204 * Otherwise, only checking done is that in bind_rdev_to_array,
4205 * which mainly checks size.
4206 */
4207 char *e;
4208 int major = simple_strtoul(buf, &e, 10);
4209 int minor;
4210 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004211 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004212 int err;
4213
4214 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4215 return -EINVAL;
4216 minor = simple_strtoul(e+1, &e, 10);
4217 if (*e && *e != '\n')
4218 return -EINVAL;
4219 dev = MKDEV(major, minor);
4220 if (major != MAJOR(dev) ||
4221 minor != MINOR(dev))
4222 return -EOVERFLOW;
4223
NeilBrown67918752014-12-15 12:57:01 +11004224 flush_workqueue(md_misc_wq);
4225
4226 err = mddev_lock(mddev);
4227 if (err)
4228 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004229 if (mddev->persistent) {
4230 rdev = md_import_device(dev, mddev->major_version,
4231 mddev->minor_version);
4232 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004233 struct md_rdev *rdev0
4234 = list_entry(mddev->disks.next,
4235 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004236 err = super_types[mddev->major_version]
4237 .load_super(rdev, rdev0, mddev->minor_version);
4238 if (err < 0)
4239 goto out;
4240 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004241 } else if (mddev->external)
4242 rdev = md_import_device(dev, -2, -1);
4243 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004244 rdev = md_import_device(dev, -1, -1);
4245
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004246 if (IS_ERR(rdev)) {
4247 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004248 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004249 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004250 err = bind_rdev_to_array(rdev, mddev);
4251 out:
4252 if (err)
4253 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004254 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004255 return err ? err : len;
4256}
4257
4258static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004259__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004260
4261static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004262bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004263{
4264 char *end;
4265 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004266 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004267
NeilBrown67918752014-12-15 12:57:01 +11004268 err = mddev_lock(mddev);
4269 if (err)
4270 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004271 if (!mddev->bitmap)
4272 goto out;
4273 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4274 while (*buf) {
4275 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4276 if (buf == end) break;
4277 if (*end == '-') { /* range */
4278 buf = end + 1;
4279 end_chunk = simple_strtoul(buf, &end, 0);
4280 if (buf == end) break;
4281 }
4282 if (*end && !isspace(*end)) break;
4283 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004284 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004285 }
4286 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4287out:
NeilBrown67918752014-12-15 12:57:01 +11004288 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004289 return len;
4290}
4291
4292static struct md_sysfs_entry md_bitmap =
4293__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4294
4295static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004296size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004297{
Andre Noll58c0fed2009-03-31 14:33:13 +11004298 return sprintf(page, "%llu\n",
4299 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004300}
4301
NeilBrownfd01b882011-10-11 16:47:53 +11004302static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004303
4304static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004305size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004306{
4307 /* If array is inactive, we can reduce the component size, but
4308 * not increase it (except from 0).
4309 * If array is active, we can try an on-line resize
4310 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004311 sector_t sectors;
4312 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004313
Andre Noll58c0fed2009-03-31 14:33:13 +11004314 if (err < 0)
4315 return err;
NeilBrown67918752014-12-15 12:57:01 +11004316 err = mddev_lock(mddev);
4317 if (err)
4318 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004319 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004320 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004321 if (err == 0)
4322 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004323 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004324 if (mddev->dev_sectors == 0 ||
4325 mddev->dev_sectors > sectors)
4326 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004327 else
4328 err = -ENOSPC;
4329 }
NeilBrown67918752014-12-15 12:57:01 +11004330 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004331 return err ? err : len;
4332}
4333
4334static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004335__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004336
Masanari Iida83f0d772012-10-30 00:18:08 +09004337/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004338 * This is one of
4339 * 'none' for arrays with no metadata (good luck...)
4340 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004341 * or N.M for internally known formats
4342 */
4343static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004344metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004345{
4346 if (mddev->persistent)
4347 return sprintf(page, "%d.%d\n",
4348 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004349 else if (mddev->external)
4350 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004351 else
4352 return sprintf(page, "none\n");
4353}
4354
4355static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004356metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004357{
4358 int major, minor;
4359 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004360 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004361 /* Changing the details of 'external' metadata is
4362 * always permitted. Otherwise there must be
4363 * no devices attached to the array.
4364 */
NeilBrown67918752014-12-15 12:57:01 +11004365
4366 err = mddev_lock(mddev);
4367 if (err)
4368 return err;
4369 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004370 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4371 ;
4372 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004373 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004374
NeilBrown67918752014-12-15 12:57:01 +11004375 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004376 if (cmd_match(buf, "none")) {
4377 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004378 mddev->external = 0;
4379 mddev->major_version = 0;
4380 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004381 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004382 }
4383 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004384 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004385 if (namelen >= sizeof(mddev->metadata_type))
4386 namelen = sizeof(mddev->metadata_type)-1;
4387 strncpy(mddev->metadata_type, buf+9, namelen);
4388 mddev->metadata_type[namelen] = 0;
4389 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4390 mddev->metadata_type[--namelen] = 0;
4391 mddev->persistent = 0;
4392 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004393 mddev->major_version = 0;
4394 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004395 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004396 }
4397 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004398 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004399 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004400 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004401 buf = e+1;
4402 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004403 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004404 goto out_unlock;
4405 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004406 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004407 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004408 mddev->major_version = major;
4409 mddev->minor_version = minor;
4410 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004411 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004412 err = 0;
4413out_unlock:
4414 mddev_unlock(mddev);
4415 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004416}
4417
4418static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004419__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004420
NeilBrowna35b0d62006-01-06 00:20:49 -08004421static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004422action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004423{
NeilBrown7eec3142005-11-08 21:39:44 -08004424 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004425 unsigned long recovery = mddev->recovery;
4426 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004427 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004428 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4429 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4430 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004431 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004432 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4433 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004434 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004435 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004436 type = "check";
4437 else
4438 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004439 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004440 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004441 else if (mddev->reshape_position != MaxSector)
4442 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004443 }
4444 return sprintf(page, "%s\n", type);
4445}
4446
4447static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004448action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004449{
NeilBrown7eec3142005-11-08 21:39:44 -08004450 if (!mddev->pers || !mddev->pers->sync_request)
4451 return -EINVAL;
4452
NeilBrownb6a9ce62009-05-26 09:41:17 +10004453
4454 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004455 if (cmd_match(page, "frozen"))
4456 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4457 else
4458 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004459 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4460 mddev_lock(mddev) == 0) {
4461 flush_workqueue(md_misc_wq);
4462 if (mddev->sync_thread) {
4463 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004464 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004465 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004466 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004467 }
NeilBrown312045e2015-12-21 11:01:21 +11004468 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004469 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004470 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004471 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004472 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004473 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004474 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004475 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004476 int err;
4477 if (mddev->pers->start_reshape == NULL)
4478 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004479 err = mddev_lock(mddev);
4480 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004481 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4482 err = -EBUSY;
4483 else {
4484 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4485 err = mddev->pers->start_reshape(mddev);
4486 }
NeilBrown67918752014-12-15 12:57:01 +11004487 mddev_unlock(mddev);
4488 }
NeilBrown16484bf2006-03-27 01:18:13 -08004489 if (err)
4490 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004491 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004492 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004493 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004494 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004495 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004496 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004497 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004498 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4499 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004500 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004501 if (mddev->ro == 2) {
4502 /* A write to sync_action is enough to justify
4503 * canceling read-auto mode
4504 */
4505 mddev->ro = 0;
4506 md_wakeup_thread(mddev->sync_thread);
4507 }
NeilBrown03c902e2006-01-06 00:20:46 -08004508 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004509 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004510 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004511 return len;
4512}
4513
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004514static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004515__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004516
4517static ssize_t
4518last_sync_action_show(struct mddev *mddev, char *page)
4519{
4520 return sprintf(page, "%s\n", mddev->last_sync_action);
4521}
4522
4523static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4524
NeilBrown9d888832005-11-08 21:39:26 -08004525static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004526mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004527{
4528 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004529 (unsigned long long)
4530 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004531}
4532
NeilBrown80ca3a42006-07-10 04:44:18 -07004533static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004534
NeilBrown88202a02006-01-06 00:21:36 -08004535static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004536sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004537{
4538 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4539 mddev->sync_speed_min ? "local": "system");
4540}
4541
4542static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004543sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004544{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004545 unsigned int min;
4546 int rv;
4547
NeilBrown88202a02006-01-06 00:21:36 -08004548 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004549 min = 0;
4550 } else {
4551 rv = kstrtouint(buf, 10, &min);
4552 if (rv < 0)
4553 return rv;
4554 if (min == 0)
4555 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004556 }
NeilBrown88202a02006-01-06 00:21:36 -08004557 mddev->sync_speed_min = min;
4558 return len;
4559}
4560
4561static struct md_sysfs_entry md_sync_min =
4562__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4563
4564static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004565sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004566{
4567 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4568 mddev->sync_speed_max ? "local": "system");
4569}
4570
4571static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004572sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004573{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004574 unsigned int max;
4575 int rv;
4576
NeilBrown88202a02006-01-06 00:21:36 -08004577 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004578 max = 0;
4579 } else {
4580 rv = kstrtouint(buf, 10, &max);
4581 if (rv < 0)
4582 return rv;
4583 if (max == 0)
4584 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004585 }
NeilBrown88202a02006-01-06 00:21:36 -08004586 mddev->sync_speed_max = max;
4587 return len;
4588}
4589
4590static struct md_sysfs_entry md_sync_max =
4591__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4592
Iustin Popd7f3d292007-10-16 23:30:54 -07004593static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004594degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004595{
4596 return sprintf(page, "%d\n", mddev->degraded);
4597}
4598static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004599
4600static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004601sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004602{
4603 return sprintf(page, "%d\n", mddev->parallel_resync);
4604}
4605
4606static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004607sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004608{
4609 long n;
4610
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004611 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004612 return -EINVAL;
4613
4614 if (n != 0 && n != 1)
4615 return -EINVAL;
4616
4617 mddev->parallel_resync = n;
4618
4619 if (mddev->sync_thread)
4620 wake_up(&resync_wait);
4621
4622 return len;
4623}
4624
4625/* force parallel resync, even with shared block devices */
4626static struct md_sysfs_entry md_sync_force_parallel =
4627__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4628 sync_force_parallel_show, sync_force_parallel_store);
4629
4630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004631sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004632{
4633 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004634 if (mddev->curr_resync == 0)
4635 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004636 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4637 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004638 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004639 db = resync - mddev->resync_mark_cnt;
4640 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004641}
4642
NeilBrown80ca3a42006-07-10 04:44:18 -07004643static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004644
4645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004646sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004647{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004648 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004649
NeilBrownacb180b2009-04-14 16:28:34 +10004650 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4651 return sprintf(page, "none\n");
4652
NeilBrown72f36d52012-10-11 14:25:57 +11004653 if (mddev->curr_resync == 1 ||
4654 mddev->curr_resync == 2)
4655 return sprintf(page, "delayed\n");
4656
NeilBrownc804cde2012-05-21 09:28:33 +10004657 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4658 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004659 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004660 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004661 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004662
NeilBrownacb180b2009-04-14 16:28:34 +10004663 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004664 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004665}
4666
NeilBrown750f1992014-09-30 08:53:05 +10004667static struct md_sysfs_entry md_sync_completed =
4668 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004669
NeilBrowne464eaf2006-03-27 01:18:14 -08004670static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004671min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004672{
4673 return sprintf(page, "%llu\n",
4674 (unsigned long long)mddev->resync_min);
4675}
4676static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004677min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004678{
4679 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004680 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004681
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004682 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004683 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004684
4685 spin_lock(&mddev->lock);
4686 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004687 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004688 goto out_unlock;
4689
4690 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004691 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004692 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004693
NeilBrown50c37b12015-03-23 17:36:38 +11004694 /* Round down to multiple of 4K for safety */
4695 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004696 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004697
NeilBrown23da4222014-12-15 12:57:01 +11004698out_unlock:
4699 spin_unlock(&mddev->lock);
4700 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004701}
4702
4703static struct md_sysfs_entry md_min_sync =
4704__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4705
4706static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004707max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004708{
4709 if (mddev->resync_max == MaxSector)
4710 return sprintf(page, "max\n");
4711 else
4712 return sprintf(page, "%llu\n",
4713 (unsigned long long)mddev->resync_max);
4714}
4715static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004716max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004717{
NeilBrown23da4222014-12-15 12:57:01 +11004718 int err;
4719 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004720 if (strncmp(buf, "max", 3) == 0)
4721 mddev->resync_max = MaxSector;
4722 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004723 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004724 int chunk;
4725
4726 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004727 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004728 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004729 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004730 goto out_unlock;
4731
4732 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004733 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004734 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004735 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004736 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004737
4738 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004739 chunk = mddev->chunk_sectors;
4740 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004741 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004742
4743 err = -EINVAL;
4744 if (sector_div(temp, chunk))
4745 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004746 }
4747 mddev->resync_max = max;
4748 }
4749 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004750 err = 0;
4751out_unlock:
4752 spin_unlock(&mddev->lock);
4753 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004754}
4755
4756static struct md_sysfs_entry md_max_sync =
4757__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4758
4759static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004760suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004761{
4762 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4763}
4764
4765static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004766suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004767{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004768 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004769 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004770
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004771 err = kstrtoull(buf, 10, &new);
4772 if (err < 0)
4773 return err;
4774 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004775 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004776
NeilBrown67918752014-12-15 12:57:01 +11004777 err = mddev_lock(mddev);
4778 if (err)
4779 return err;
4780 err = -EINVAL;
4781 if (mddev->pers == NULL ||
4782 mddev->pers->quiesce == NULL)
4783 goto unlock;
4784 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004785 mddev->suspend_lo = new;
4786 if (new >= old)
4787 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004788 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004789 else {
4790 /* Expanding suspended region - need to wait */
4791 mddev->pers->quiesce(mddev, 1);
4792 mddev->pers->quiesce(mddev, 0);
4793 }
NeilBrown67918752014-12-15 12:57:01 +11004794 err = 0;
4795unlock:
4796 mddev_unlock(mddev);
4797 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004798}
4799static struct md_sysfs_entry md_suspend_lo =
4800__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4801
NeilBrowne464eaf2006-03-27 01:18:14 -08004802static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004803suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004804{
4805 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4806}
4807
4808static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004809suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004810{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004811 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004812 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004813
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004814 err = kstrtoull(buf, 10, &new);
4815 if (err < 0)
4816 return err;
4817 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004818 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004819
NeilBrown67918752014-12-15 12:57:01 +11004820 err = mddev_lock(mddev);
4821 if (err)
4822 return err;
4823 err = -EINVAL;
4824 if (mddev->pers == NULL ||
4825 mddev->pers->quiesce == NULL)
4826 goto unlock;
4827 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004828 mddev->suspend_hi = new;
4829 if (new <= old)
4830 /* Shrinking suspended region */
4831 mddev->pers->quiesce(mddev, 2);
4832 else {
4833 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004834 mddev->pers->quiesce(mddev, 1);
4835 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004836 }
NeilBrown67918752014-12-15 12:57:01 +11004837 err = 0;
4838unlock:
4839 mddev_unlock(mddev);
4840 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004841}
4842static struct md_sysfs_entry md_suspend_hi =
4843__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4844
NeilBrown08a02ec2007-05-09 02:35:38 -07004845static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004846reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004847{
4848 if (mddev->reshape_position != MaxSector)
4849 return sprintf(page, "%llu\n",
4850 (unsigned long long)mddev->reshape_position);
4851 strcpy(page, "none\n");
4852 return 5;
4853}
4854
4855static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004856reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004857{
NeilBrownc6563a82012-05-21 09:27:00 +10004858 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004859 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004860 int err;
NeilBrown67918752014-12-15 12:57:01 +11004861
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004862 err = kstrtoull(buf, 10, &new);
4863 if (err < 0)
4864 return err;
4865 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004866 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004867 err = mddev_lock(mddev);
4868 if (err)
4869 return err;
4870 err = -EBUSY;
4871 if (mddev->pers)
4872 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004873 mddev->reshape_position = new;
4874 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004875 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004876 mddev->new_level = mddev->level;
4877 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004878 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004879 rdev_for_each(rdev, mddev)
4880 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004881 err = 0;
4882unlock:
4883 mddev_unlock(mddev);
4884 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004885}
4886
4887static struct md_sysfs_entry md_reshape_position =
4888__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4889 reshape_position_store);
4890
Dan Williamsb522adc2009-03-31 15:00:31 +11004891static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004892reshape_direction_show(struct mddev *mddev, char *page)
4893{
4894 return sprintf(page, "%s\n",
4895 mddev->reshape_backwards ? "backwards" : "forwards");
4896}
4897
4898static ssize_t
4899reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4900{
4901 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004902 int err;
4903
NeilBrown2c810cd2012-05-21 09:27:00 +10004904 if (cmd_match(buf, "forwards"))
4905 backwards = 0;
4906 else if (cmd_match(buf, "backwards"))
4907 backwards = 1;
4908 else
4909 return -EINVAL;
4910 if (mddev->reshape_backwards == backwards)
4911 return len;
4912
NeilBrown67918752014-12-15 12:57:01 +11004913 err = mddev_lock(mddev);
4914 if (err)
4915 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004916 /* check if we are allowed to change */
4917 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004918 err = -EBUSY;
4919 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004920 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004921 err = -EINVAL;
4922 else
4923 mddev->reshape_backwards = backwards;
4924 mddev_unlock(mddev);
4925 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004926}
4927
4928static struct md_sysfs_entry md_reshape_direction =
4929__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4930 reshape_direction_store);
4931
4932static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004933array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004934{
4935 if (mddev->external_size)
4936 return sprintf(page, "%llu\n",
4937 (unsigned long long)mddev->array_sectors/2);
4938 else
4939 return sprintf(page, "default\n");
4940}
4941
4942static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004943array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004944{
4945 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004946 int err;
4947
4948 err = mddev_lock(mddev);
4949 if (err)
4950 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004951
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004952 /* cluster raid doesn't support change array_sectors */
4953 if (mddev_is_clustered(mddev))
4954 return -EINVAL;
4955
Dan Williamsb522adc2009-03-31 15:00:31 +11004956 if (strncmp(buf, "default", 7) == 0) {
4957 if (mddev->pers)
4958 sectors = mddev->pers->size(mddev, 0, 0);
4959 else
4960 sectors = mddev->array_sectors;
4961
4962 mddev->external_size = 0;
4963 } else {
4964 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004965 err = -EINVAL;
4966 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4967 err = -E2BIG;
4968 else
4969 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004970 }
4971
NeilBrown67918752014-12-15 12:57:01 +11004972 if (!err) {
4973 mddev->array_sectors = sectors;
4974 if (mddev->pers) {
4975 set_capacity(mddev->gendisk, mddev->array_sectors);
4976 revalidate_disk(mddev->gendisk);
4977 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004978 }
NeilBrown67918752014-12-15 12:57:01 +11004979 mddev_unlock(mddev);
4980 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004981}
4982
4983static struct md_sysfs_entry md_array_size =
4984__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4985 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004986
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01004987static ssize_t
4988consistency_policy_show(struct mddev *mddev, char *page)
4989{
4990 int ret;
4991
4992 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
4993 ret = sprintf(page, "journal\n");
4994 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
4995 ret = sprintf(page, "ppl\n");
4996 } else if (mddev->bitmap) {
4997 ret = sprintf(page, "bitmap\n");
4998 } else if (mddev->pers) {
4999 if (mddev->pers->sync_request)
5000 ret = sprintf(page, "resync\n");
5001 else
5002 ret = sprintf(page, "none\n");
5003 } else {
5004 ret = sprintf(page, "unknown\n");
5005 }
5006
5007 return ret;
5008}
5009
5010static ssize_t
5011consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5012{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005013 int err = 0;
5014
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005015 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005016 if (mddev->pers->change_consistency_policy)
5017 err = mddev->pers->change_consistency_policy(mddev, buf);
5018 else
5019 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005020 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5021 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005022 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005023 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005024 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005025
5026 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005027}
5028
5029static struct md_sysfs_entry md_consistency_policy =
5030__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5031 consistency_policy_store);
5032
NeilBrowneae17012005-11-08 21:39:23 -08005033static struct attribute *md_default_attrs[] = {
5034 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005035 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005036 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005037 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005038 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005039 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005040 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005041 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005042 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005043 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005044 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005045 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005046 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005047 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005048 &md_consistency_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005049 NULL,
5050};
5051
5052static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005053 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005054 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005055 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005056 &md_sync_min.attr,
5057 &md_sync_max.attr,
5058 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005059 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005060 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005061 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005062 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005063 &md_suspend_lo.attr,
5064 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005065 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005066 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005067 NULL,
5068};
NeilBrown411036f2005-11-08 21:39:40 -08005069static struct attribute_group md_redundancy_group = {
5070 .name = NULL,
5071 .attrs = md_redundancy_attrs,
5072};
5073
NeilBrowneae17012005-11-08 21:39:23 -08005074static ssize_t
5075md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5076{
5077 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005078 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005079 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005080
5081 if (!entry->show)
5082 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005083 spin_lock(&all_mddevs_lock);
5084 if (list_empty(&mddev->all_mddevs)) {
5085 spin_unlock(&all_mddevs_lock);
5086 return -EBUSY;
5087 }
5088 mddev_get(mddev);
5089 spin_unlock(&all_mddevs_lock);
5090
NeilBrownb7b17c92014-12-15 12:56:59 +11005091 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005092 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005093 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005094}
5095
5096static ssize_t
5097md_attr_store(struct kobject *kobj, struct attribute *attr,
5098 const char *page, size_t length)
5099{
5100 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005101 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005102 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005103
5104 if (!entry->store)
5105 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005106 if (!capable(CAP_SYS_ADMIN))
5107 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005108 spin_lock(&all_mddevs_lock);
5109 if (list_empty(&mddev->all_mddevs)) {
5110 spin_unlock(&all_mddevs_lock);
5111 return -EBUSY;
5112 }
5113 mddev_get(mddev);
5114 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005115 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005116 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005117 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005118}
5119
5120static void md_free(struct kobject *ko)
5121{
NeilBrownfd01b882011-10-11 16:47:53 +11005122 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005123
5124 if (mddev->sysfs_state)
5125 sysfs_put(mddev->sysfs_state);
5126
NeilBrown6cd18e72015-04-27 14:12:22 +10005127 if (mddev->queue)
5128 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11005129 if (mddev->gendisk) {
5130 del_gendisk(mddev->gendisk);
5131 put_disk(mddev->gendisk);
5132 }
NeilBrown4ad23a972017-03-15 14:05:14 +11005133 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005134
NeilBrowneae17012005-11-08 21:39:23 -08005135 kfree(mddev);
5136}
5137
Emese Revfy52cf25d2010-01-19 02:58:23 +01005138static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005139 .show = md_attr_show,
5140 .store = md_attr_store,
5141};
5142static struct kobj_type md_ktype = {
5143 .release = md_free,
5144 .sysfs_ops = &md_sysfs_ops,
5145 .default_attrs = md_default_attrs,
5146};
5147
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148int mdp_major = 0;
5149
Dan Williams5fd3a172009-03-04 00:57:25 -07005150static void mddev_delayed_delete(struct work_struct *ws)
5151{
NeilBrownfd01b882011-10-11 16:47:53 +11005152 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005153
NeilBrown43a70502009-12-14 12:49:55 +11005154 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005155 kobject_del(&mddev->kobj);
5156 kobject_put(&mddev->kobj);
5157}
5158
NeilBrown4ad23a972017-03-15 14:05:14 +11005159static void no_op(struct percpu_ref *r) {}
5160
NeilBrownefeb53c2009-01-09 08:31:10 +11005161static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005163 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005164 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005166 int partitioned;
5167 int shift;
5168 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005169 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
5171 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005172 return -ENODEV;
5173
5174 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5175 shift = partitioned ? MdpMinorShift : 0;
5176 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Tejun Heoe804ac72010-10-15 15:36:08 +02005178 /* wait for any previous instance of this device to be
5179 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005180 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005181 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005182
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005183 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005184 error = -EEXIST;
5185 if (mddev->gendisk)
5186 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005187
5188 if (name) {
5189 /* Need to ensure that 'name' is not a duplicate.
5190 */
NeilBrownfd01b882011-10-11 16:47:53 +11005191 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005192 spin_lock(&all_mddevs_lock);
5193
5194 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5195 if (mddev2->gendisk &&
5196 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5197 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005198 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005199 }
5200 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 }
NeilBrown8b765392009-01-09 08:31:08 +11005202
NeilBrown0909dc42009-07-01 12:27:21 +10005203 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005204 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005205 if (!mddev->queue)
5206 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005207 mddev->queue->queuedata = mddev;
5208
NeilBrown409c57f2009-03-31 14:39:39 +11005209 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005210 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005211
NeilBrown4ad23a972017-03-15 14:05:14 +11005212 if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0)
5213 goto abort;
5214 /* We want to start with the refcount at zero */
5215 percpu_ref_put(&mddev->writes_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 disk = alloc_disk(1 << shift);
5217 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005218 blk_cleanup_queue(mddev->queue);
5219 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005220 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005222 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005224 if (name)
5225 strcpy(disk->disk_name, name);
5226 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005228 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 disk->fops = &md_fops;
5231 disk->private_data = mddev;
5232 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005233 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005234 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005235 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005236 * remove it now.
5237 */
5238 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005240 /* As soon as we call add_disk(), another thread could get
5241 * through to md_open, so make sure it doesn't get too far
5242 */
5243 mutex_lock(&mddev->open_mutex);
5244 add_disk(disk);
5245
Tejun Heoed9e1982008-08-25 19:56:05 +09005246 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5247 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005248 if (error) {
5249 /* This isn't possible, but as kobject_init_and_add is marked
5250 * __must_check, we must do something with the result
5251 */
NeilBrown9d487392016-11-02 14:16:49 +11005252 pr_debug("md: cannot register %s/md - name in use\n",
5253 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005254 error = 0;
5255 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005256 if (mddev->kobj.sd &&
5257 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005258 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005259 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005260 abort:
5261 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005262 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005263 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005264 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005265 }
NeilBrownd3374822009-01-09 08:31:10 +11005266 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005267 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005268}
5269
5270static struct kobject *md_probe(dev_t dev, int *part, void *data)
5271{
5272 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 return NULL;
5274}
5275
NeilBrownefeb53c2009-01-09 08:31:10 +11005276static int add_named_array(const char *val, struct kernel_param *kp)
5277{
5278 /* val must be "md_*" where * is not all digits.
5279 * We allocate an array with a large free minor number, and
5280 * set the name to val. val must not already be an active name.
5281 */
5282 int len = strlen(val);
5283 char buf[DISK_NAME_LEN];
5284
5285 while (len && val[len-1] == '\n')
5286 len--;
5287 if (len >= DISK_NAME_LEN)
5288 return -E2BIG;
5289 strlcpy(buf, val, len+1);
5290 if (strncmp(buf, "md_", 3) != 0)
5291 return -EINVAL;
5292 return md_alloc(0, buf);
5293}
5294
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295static void md_safemode_timeout(unsigned long data)
5296{
NeilBrownfd01b882011-10-11 16:47:53 +11005297 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
NeilBrown4ad23a972017-03-15 14:05:14 +11005299 mddev->safemode = 1;
5300 if (mddev->external)
5301 sysfs_notify_dirent_safe(mddev->sysfs_state);
5302
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 md_wakeup_thread(mddev->thread);
5304}
5305
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005306static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
NeilBrownfd01b882011-10-11 16:47:53 +11005308int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
NeilBrown2604b702006-01-06 00:20:36 -08005310 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005311 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005312 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
NeilBrowna757e642005-04-16 15:26:42 -07005314 if (list_empty(&mddev->disks))
5315 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317
5318 if (mddev->pers)
5319 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005320 /* Cannot run until previous stop completes properly */
5321 if (mddev->sysfs_active)
5322 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 /*
5325 * Analyze all RAID superblock(s)
5326 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005327 if (!mddev->raid_disks) {
5328 if (!mddev->persistent)
5329 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005330 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
NeilBrownd9d166c2006-01-06 00:20:51 -08005333 if (mddev->level != LEVEL_NONE)
5334 request_module("md-level-%d", mddev->level);
5335 else if (mddev->clevel[0])
5336 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
5338 /*
5339 * Drop all container device buffers, from now on
5340 * the only valid external interface is through the md
5341 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005343 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005344 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 continue;
5346 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005347 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005348
5349 /* perform some consistency tests on the device.
5350 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005351 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005352 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005353 if (rdev->meta_bdev) {
5354 /* Nothing to check */;
5355 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005356 if (mddev->dev_sectors &&
5357 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005358 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005359 pr_warn("md: %s: data overlaps metadata\n",
5360 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005361 return -EINVAL;
5362 }
5363 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005364 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005365 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005366 pr_warn("md: %s: metadata overlaps data\n",
5367 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005368 return -EINVAL;
5369 }
5370 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005371 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 }
5373
Ming Lei10273172017-02-14 23:29:00 +08005374 if (mddev->bio_set == NULL) {
Kent Overstreet395c72a2012-09-06 15:34:55 -07005375 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
Ming Lei10273172017-02-14 23:29:00 +08005376 if (!mddev->bio_set)
5377 return -ENOMEM;
5378 }
NeilBrowna167f662010-10-26 18:31:13 +11005379
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005381 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005382 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005384 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005385 pr_warn("md: personality for level %d is not loaded!\n",
5386 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005387 else
NeilBrown9d487392016-11-02 14:16:49 +11005388 pr_warn("md: personality for level %s is not loaded!\n",
5389 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 return -EINVAL;
5391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005393 if (mddev->level != pers->level) {
5394 mddev->level = pers->level;
5395 mddev->new_level = pers->level;
5396 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005397 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
NeilBrownf6705572006-03-27 01:18:11 -08005399 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005400 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005401 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005402 module_put(pers->owner);
5403 return -EINVAL;
5404 }
5405
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005406 if (pers->sync_request) {
5407 /* Warn if this is a potentially silly
5408 * configuration.
5409 */
5410 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005411 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005412 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005413
NeilBrowndafb20f2012-03-19 12:46:39 +11005414 rdev_for_each(rdev, mddev)
5415 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005416 if (rdev < rdev2 &&
5417 rdev->bdev->bd_contains ==
5418 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005419 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5420 mdname(mddev),
5421 bdevname(rdev->bdev,b),
5422 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005423 warned = 1;
5424 }
5425 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005426
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005427 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005428 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005429 }
5430
NeilBrown657390d2005-08-26 18:34:16 -07005431 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005432 /* may be over-ridden by personality */
5433 mddev->resync_max_sectors = mddev->dev_sectors;
5434
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005435 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
NeilBrown0f9552b52009-12-30 12:08:50 +11005437 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005438 mddev->ro = 2; /* read-only, but switch on first write */
5439
Song Liua85dd7b2017-01-23 17:12:57 -08005440 /*
5441 * NOTE: some pers->run(), for example r5l_recovery_log(), wakes
5442 * up mddev->thread. It is important to initialize critical
5443 * resources for mddev->thread BEFORE calling pers->run().
5444 */
NeilBrown36d091f2014-12-15 12:56:58 +11005445 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005446 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005447 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005448 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005449 WARN_ONCE(!mddev->external_size,
5450 "%s: default size too small, but 'external_size' not in effect?\n",
5451 __func__);
5452 pr_warn("md: invalid array_size %llu > default size %llu\n",
5453 (unsigned long long)mddev->array_sectors / 2,
5454 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005455 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005456 }
NeilBrown36d091f2014-12-15 12:56:58 +11005457 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005458 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005459 struct bitmap *bitmap;
5460
5461 bitmap = bitmap_create(mddev, -1);
5462 if (IS_ERR(bitmap)) {
5463 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005464 pr_warn("%s: failed to create bitmap (%d)\n",
5465 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005466 } else
5467 mddev->bitmap = bitmap;
5468
NeilBrownb15c2e52006-01-06 00:20:16 -08005469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005471 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005472 if (mddev->private)
5473 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005474 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005475 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005476 bitmap_destroy(mddev);
5477 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 }
NeilBrown5c675f82014-12-15 12:56:56 +11005479 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005480 bool nonrot = true;
5481
5482 rdev_for_each(rdev, mddev) {
5483 if (rdev->raid_disk >= 0 &&
5484 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5485 nonrot = false;
5486 break;
5487 }
5488 }
5489 if (mddev->degraded)
5490 nonrot = false;
5491 if (nonrot)
5492 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
5493 else
5494 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005495 mddev->queue->backing_dev_info->congested_data = mddev;
5496 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005497 }
NeilBrown36d091f2014-12-15 12:56:58 +11005498 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005499 if (mddev->kobj.sd &&
5500 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005501 pr_warn("md: cannot register extra attributes for %s\n",
5502 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005503 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005504 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005505 mddev->ro = 0;
5506
Robert Becker1e509152009-12-14 12:49:58 +11005507 atomic_set(&mddev->max_corr_read_errors,
5508 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005510 if (mddev_is_clustered(mddev))
5511 mddev->safemode_delay = 0;
5512 else
5513 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005515 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005516 spin_lock(&mddev->lock);
5517 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005518 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005519 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005520 if (rdev->raid_disk >= 0)
5521 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005522 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005523
NeilBrowna4a3d262015-07-17 11:57:30 +10005524 if (mddev->degraded && !mddev->ro)
5525 /* This ensures that recovering status is reported immediately
5526 * via sysfs - until a lack of spares is confirmed.
5527 */
5528 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005530
Shaohua Li29530792016-12-08 15:48:19 -08005531 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005532 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533
NeilBrownd7603b72006-01-06 00:20:30 -08005534 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005535 sysfs_notify_dirent_safe(mddev->sysfs_state);
5536 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005537 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 return 0;
5539}
NeilBrown390ee602010-06-01 19:37:27 +10005540EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
NeilBrownfd01b882011-10-11 16:47:53 +11005542static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005543{
5544 int err;
5545
5546 err = md_run(mddev);
5547 if (err)
5548 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005549 err = bitmap_load(mddev);
5550 if (err) {
5551 bitmap_destroy(mddev);
5552 goto out;
5553 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005554
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005555 if (mddev_is_clustered(mddev))
5556 md_allow_write(mddev);
5557
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005558 md_wakeup_thread(mddev->thread);
5559 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5560
NeilBrownfe60b012010-03-29 11:10:42 +11005561 set_capacity(mddev->gendisk, mddev->array_sectors);
5562 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005563 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005564 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5565out:
5566 return err;
5567}
5568
NeilBrownfd01b882011-10-11 16:47:53 +11005569static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570{
5571 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Andre Noll80fab1d2008-07-11 22:02:21 +10005573 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005575 return -ENXIO;
5576 if (!mddev->pers)
5577 return -EINVAL;
5578 if (!mddev->ro)
5579 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005580 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5581 struct md_rdev *rdev;
5582 bool has_journal = false;
5583
5584 rcu_read_lock();
5585 rdev_for_each_rcu(rdev, mddev) {
5586 if (test_bit(Journal, &rdev->flags) &&
5587 !test_bit(Faulty, &rdev->flags)) {
5588 has_journal = true;
5589 break;
5590 }
5591 }
5592 rcu_read_unlock();
5593
5594 /* Don't restart rw with journal missing/faulty */
5595 if (!has_journal)
5596 return -EINVAL;
5597 }
5598
Andre Noll80fab1d2008-07-11 22:02:21 +10005599 mddev->safemode = 0;
5600 mddev->ro = 0;
5601 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005602 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005603 /* Kick recovery or resync if necessary */
5604 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5605 md_wakeup_thread(mddev->thread);
5606 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005607 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609}
5610
NeilBrownfd01b882011-10-11 16:47:53 +11005611static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005612{
5613 mddev->array_sectors = 0;
5614 mddev->external_size = 0;
5615 mddev->dev_sectors = 0;
5616 mddev->raid_disks = 0;
5617 mddev->recovery_cp = 0;
5618 mddev->resync_min = 0;
5619 mddev->resync_max = MaxSector;
5620 mddev->reshape_position = MaxSector;
5621 mddev->external = 0;
5622 mddev->persistent = 0;
5623 mddev->level = LEVEL_NONE;
5624 mddev->clevel[0] = 0;
5625 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005626 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005627 mddev->ro = 0;
5628 mddev->metadata_type[0] = 0;
5629 mddev->chunk_sectors = 0;
5630 mddev->ctime = mddev->utime = 0;
5631 mddev->layout = 0;
5632 mddev->max_disks = 0;
5633 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005634 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005635 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005636 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005637 mddev->new_level = LEVEL_NONE;
5638 mddev->new_layout = 0;
5639 mddev->new_chunk_sectors = 0;
5640 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005641 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005642 mddev->suspend_lo = mddev->suspend_hi = 0;
5643 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5644 mddev->recovery = 0;
5645 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005646 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005647 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005648 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005649 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005650 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005651 mddev->bitmap_info.offset = 0;
5652 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005653 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005654 mddev->bitmap_info.chunksize = 0;
5655 mddev->bitmap_info.daemon_sleep = 0;
5656 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005657 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005658}
5659
NeilBrownfd01b882011-10-11 16:47:53 +11005660static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005661{
NeilBrown6b6204e2013-05-09 09:48:30 +10005662 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005663 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005664 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005665 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005666 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005667 }
5668
5669 del_timer_sync(&mddev->safemode_timer);
5670
Shaohua Li034e33f2016-11-21 10:29:19 -08005671 if (mddev->pers && mddev->pers->quiesce) {
5672 mddev->pers->quiesce(mddev, 1);
5673 mddev->pers->quiesce(mddev, 0);
5674 }
NeilBrowna047e122010-03-29 12:07:53 +11005675 bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005676
NeilBrownb6d428c2013-04-24 11:42:42 +10005677 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005678 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005679 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005680 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005681 if (!mddev_is_clustered(mddev))
5682 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005683 md_update_sb(mddev, 1);
5684 }
5685}
NeilBrowndefad612011-01-14 09:14:33 +11005686
NeilBrownfd01b882011-10-11 16:47:53 +11005687void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005688{
NeilBrown29f097c2013-11-14 17:54:51 +11005689 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005690 __md_stop_writes(mddev);
5691 mddev_unlock(mddev);
5692}
NeilBrown390ee602010-06-01 19:37:27 +10005693EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005694
NeilBrown5aa61f42014-12-15 12:56:57 +11005695static void mddev_detach(struct mddev *mddev)
5696{
Guoqing Jiang48df4982017-03-14 09:40:20 +08005697 bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005698 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005699 mddev->pers->quiesce(mddev, 1);
5700 mddev->pers->quiesce(mddev, 0);
5701 }
5702 md_unregister_thread(&mddev->thread);
5703 if (mddev->queue)
5704 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5705}
5706
NeilBrown5eff3c42012-11-19 10:47:48 +11005707static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005708{
NeilBrown36d091f2014-12-15 12:56:58 +11005709 struct md_personality *pers = mddev->pers;
Guoqing Jiang48df4982017-03-14 09:40:20 +08005710 bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005711 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005712 /* Ensure ->event_work is done */
5713 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005714 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005715 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005716 spin_unlock(&mddev->lock);
5717 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005718 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005719 if (pers->sync_request && mddev->to_remove == NULL)
5720 mddev->to_remove = &md_redundancy_group;
5721 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005722 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005723}
NeilBrown5eff3c42012-11-19 10:47:48 +11005724
5725void md_stop(struct mddev *mddev)
5726{
5727 /* stop the array and free an attached data structures.
5728 * This is called from dm-raid
5729 */
5730 __md_stop(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005731 if (mddev->bio_set)
5732 bioset_free(mddev->bio_set);
5733}
5734
NeilBrown390ee602010-06-01 19:37:27 +10005735EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005736
NeilBrowna05b7ea2012-07-19 15:59:18 +10005737static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005738{
5739 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005740 int did_freeze = 0;
5741
5742 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5743 did_freeze = 1;
5744 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5745 md_wakeup_thread(mddev->thread);
5746 }
NeilBrownf851b602014-12-11 10:02:10 +11005747 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005748 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005749 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005750 /* Thread might be blocked waiting for metadata update
5751 * which will now never happen */
5752 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005753
Shaohua Li29530792016-12-08 15:48:19 -08005754 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10005755 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005756 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005757 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5758 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005759 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005760 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005761 mddev_lock_nointr(mddev);
5762
NeilBrowna4bd82d2010-03-29 13:23:10 +11005763 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005764 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005765 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005766 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005767 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005768 if (did_freeze) {
5769 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005770 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005771 md_wakeup_thread(mddev->thread);
5772 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005773 err = -EBUSY;
5774 goto out;
5775 }
5776 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005777 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005778
5779 err = -ENXIO;
5780 if (mddev->ro==1)
5781 goto out;
5782 mddev->ro = 1;
5783 set_disk_ro(mddev->gendisk, 1);
5784 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005785 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5786 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005787 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005788 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005789 }
5790out:
5791 mutex_unlock(&mddev->open_mutex);
5792 return err;
5793}
5794
NeilBrown9e653b62006-06-26 00:27:58 -07005795/* mode:
5796 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005797 * 2 - stop but do not disassemble array
5798 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005799static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005800 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005803 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005804 int did_freeze = 0;
5805
5806 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5807 did_freeze = 1;
5808 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5809 md_wakeup_thread(mddev->thread);
5810 }
NeilBrownf851b602014-12-11 10:02:10 +11005811 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005812 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005813 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005814 /* Thread might be blocked waiting for metadata update
5815 * which will now never happen */
5816 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005817
NeilBrown30b8feb2013-11-14 15:16:17 +11005818 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005819 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5820 !test_bit(MD_RECOVERY_RUNNING,
5821 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005822 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
NeilBrownc8c00a62009-08-10 12:50:52 +10005824 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005825 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005826 mddev->sysfs_active ||
5827 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005828 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11005829 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005830 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005831 if (did_freeze) {
5832 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005833 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005834 md_wakeup_thread(mddev->thread);
5835 }
NeilBrown260fa032013-08-27 16:44:13 +10005836 return -EBUSY;
5837 }
NeilBrown6e17b022010-08-07 21:41:19 +10005838 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005839 if (mddev->ro)
5840 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005841
NeilBrowndefad612011-01-14 09:14:33 +11005842 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005843 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01005844 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005845
NeilBrowna4bd82d2010-03-29 13:23:10 +11005846 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005847 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005848
NeilBrowndafb20f2012-03-19 12:46:39 +11005849 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005850 if (rdev->raid_disk >= 0)
5851 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005852
NeilBrowna4bd82d2010-03-29 13:23:10 +11005853 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005854 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005855 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005856 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005857
NeilBrowna4bd82d2010-03-29 13:23:10 +11005858 if (mddev->ro)
5859 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005860 } else
5861 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 /*
5863 * Free resources if final stop
5864 */
NeilBrown9e653b62006-06-26 00:27:58 -07005865 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005866 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
NeilBrownc3d97142009-12-14 12:49:52 +11005868 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005869 struct file *f = mddev->bitmap_info.file;
5870 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005871 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005872 spin_unlock(&mddev->lock);
5873 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005874 }
NeilBrownc3d97142009-12-14 12:49:52 +11005875 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005876
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 export_array(mddev);
5878
NeilBrown6177b472010-03-29 11:37:13 +11005879 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005880 if (mddev->hold_active == UNTIL_STOP)
5881 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005882 }
NeilBrownd7603b72006-01-06 00:20:30 -08005883 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005884 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886}
5887
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005888#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005889static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890{
NeilBrown3cb03002011-10-11 16:45:26 +11005891 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 int err;
5893
NeilBrowna757e642005-04-16 15:26:42 -07005894 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
NeilBrown9d487392016-11-02 14:16:49 +11005897 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898
NeilBrowndafb20f2012-03-19 12:46:39 +11005899 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11005901 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 }
NeilBrown9d487392016-11-02 14:16:49 +11005903 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
NeilBrownd710e132008-10-13 11:55:12 +11005905 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11005907 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005908 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 }
5910}
5911
5912/*
5913 * lets try to run arrays based on all disks that have arrived
5914 * until now. (those are in pending_raid_disks)
5915 *
5916 * the method: pick the first pending disk, collect all disks with
5917 * the same UUID, remove all from the pending list and put them into
5918 * the 'same_array' list. Then order this list based on superblock
5919 * update time (freshest comes first), kick out 'old' disks and
5920 * compare superblocks. If everything's fine then run it.
5921 *
5922 * If "unit" is allocated, then bump its reference count
5923 */
5924static void autorun_devices(int part)
5925{
NeilBrown3cb03002011-10-11 16:45:26 +11005926 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005927 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 char b[BDEVNAME_SIZE];
5929
NeilBrown9d487392016-11-02 14:16:49 +11005930 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005932 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005934 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005936 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
NeilBrown9d487392016-11-02 14:16:49 +11005938 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005940 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11005942 pr_debug("md: adding %s ...\n",
5943 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 list_move(&rdev->same_set, &candidates);
5945 }
5946 /*
5947 * now we have a set of devices, with all of them having
5948 * mostly sane superblocks. It's time to allocate the
5949 * mddev.
5950 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005951 if (part) {
5952 dev = MKDEV(mdp_major,
5953 rdev0->preferred_minor << MdpMinorShift);
5954 unit = MINOR(dev) >> MdpMinorShift;
5955 } else {
5956 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5957 unit = MINOR(dev);
5958 }
5959 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11005960 pr_warn("md: unit number in %s is bad: %d\n",
5961 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 break;
5963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
5965 md_probe(dev, NULL, NULL);
5966 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005967 if (!mddev || !mddev->gendisk) {
5968 if (mddev)
5969 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 break;
5971 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005972 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11005973 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 else if (mddev->raid_disks || mddev->major_version
5975 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11005976 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 mdname(mddev), bdevname(rdev0->bdev,b));
5978 mddev_unlock(mddev);
5979 } else {
NeilBrown9d487392016-11-02 14:16:49 +11005980 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005981 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005982 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 list_del_init(&rdev->same_set);
5984 if (bind_rdev_to_array(rdev, mddev))
5985 export_rdev(rdev);
5986 }
5987 autorun_array(mddev);
5988 mddev_unlock(mddev);
5989 }
5990 /* on success, candidates will be empty, on error
5991 * it won't...
5992 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005993 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005994 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 mddev_put(mddev);
5998 }
NeilBrown9d487392016-11-02 14:16:49 +11005999 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006001#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
NeilBrownf72ffdd2014-09-30 14:23:59 +10006003static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004{
6005 mdu_version_t ver;
6006
6007 ver.major = MD_MAJOR_VERSION;
6008 ver.minor = MD_MINOR_VERSION;
6009 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6010
6011 if (copy_to_user(arg, &ver, sizeof(ver)))
6012 return -EFAULT;
6013
6014 return 0;
6015}
6016
NeilBrownf72ffdd2014-09-30 14:23:59 +10006017static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018{
6019 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006020 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006021 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
NeilBrown1ca69c42012-10-11 13:37:33 +11006023 nr = working = insync = failed = spare = 0;
6024 rcu_read_lock();
6025 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006027 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 failed++;
6029 else {
6030 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006031 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006032 insync++;
Song Liub347af82016-08-11 17:14:45 -07006033 else if (test_bit(Journal, &rdev->flags))
6034 /* TODO: add journal count to md_u.h */
6035 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 else
6037 spare++;
6038 }
6039 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006040 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041
6042 info.major_version = mddev->major_version;
6043 info.minor_version = mddev->minor_version;
6044 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006045 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006047 info.size = mddev->dev_sectors / 2;
6048 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006049 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 info.nr_disks = nr;
6051 info.raid_disks = mddev->raid_disks;
6052 info.md_minor = mddev->md_minor;
6053 info.not_persistent= !mddev->persistent;
6054
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006055 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 info.state = 0;
6057 if (mddev->in_sync)
6058 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006059 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006060 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006061 if (mddev_is_clustered(mddev))
6062 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006063 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 info.working_disks = working;
6065 info.failed_disks = failed;
6066 info.spare_disks = spare;
6067
6068 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006069 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
6071 if (copy_to_user(arg, &info, sizeof(info)))
6072 return -EFAULT;
6073
6074 return 0;
6075}
6076
NeilBrownf72ffdd2014-09-30 14:23:59 +10006077static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006078{
6079 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006080 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006081 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006082
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006083 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006084 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006085 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006086
NeilBrown32a76272005-06-21 17:17:14 -07006087 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006088 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006089 /* bitmap enabled */
6090 if (mddev->bitmap_info.file) {
6091 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6092 sizeof(file->pathname));
6093 if (IS_ERR(ptr))
6094 err = PTR_ERR(ptr);
6095 else
6096 memmove(file->pathname, ptr,
6097 sizeof(file->pathname)-(ptr-file->pathname));
6098 }
NeilBrown4af1a042014-12-15 12:57:00 +11006099 spin_unlock(&mddev->lock);
6100
6101 if (err == 0 &&
6102 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006103 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006104
NeilBrown32a76272005-06-21 17:17:14 -07006105 kfree(file);
6106 return err;
6107}
6108
NeilBrownf72ffdd2014-09-30 14:23:59 +10006109static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110{
6111 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006112 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
6114 if (copy_from_user(&info, arg, sizeof(info)))
6115 return -EFAULT;
6116
NeilBrown1ca69c42012-10-11 13:37:33 +11006117 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006118 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 if (rdev) {
6120 info.major = MAJOR(rdev->bdev->bd_dev);
6121 info.minor = MINOR(rdev->bdev->bd_dev);
6122 info.raid_disk = rdev->raid_disk;
6123 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006124 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006126 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 info.state |= (1<<MD_DISK_ACTIVE);
6128 info.state |= (1<<MD_DISK_SYNC);
6129 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006130 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006131 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006132 if (test_bit(WriteMostly, &rdev->flags))
6133 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006134 if (test_bit(FailFast, &rdev->flags))
6135 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 } else {
6137 info.major = info.minor = 0;
6138 info.raid_disk = -1;
6139 info.state = (1<<MD_DISK_REMOVED);
6140 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006141 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
6143 if (copy_to_user(arg, &info, sizeof(info)))
6144 return -EFAULT;
6145
6146 return 0;
6147}
6148
NeilBrownf72ffdd2014-09-30 14:23:59 +10006149static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150{
6151 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006152 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 dev_t dev = MKDEV(info->major,info->minor);
6154
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006155 if (mddev_is_clustered(mddev) &&
6156 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006157 pr_warn("%s: Cannot add to clustered mddev.\n",
6158 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006159 return -EINVAL;
6160 }
6161
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6163 return -EOVERFLOW;
6164
6165 if (!mddev->raid_disks) {
6166 int err;
6167 /* expecting a device which has a superblock */
6168 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6169 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006170 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 PTR_ERR(rdev));
6172 return PTR_ERR(rdev);
6173 }
6174 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006175 struct md_rdev *rdev0
6176 = list_entry(mddev->disks.next,
6177 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006178 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 .load_super(rdev, rdev0, mddev->minor_version);
6180 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006181 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006182 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 bdevname(rdev0->bdev,b2));
6184 export_rdev(rdev);
6185 return -EINVAL;
6186 }
6187 }
6188 err = bind_rdev_to_array(rdev, mddev);
6189 if (err)
6190 export_rdev(rdev);
6191 return err;
6192 }
6193
6194 /*
6195 * add_new_disk can be used once the array is assembled
6196 * to add "hot spares". They must already have a superblock
6197 * written
6198 */
6199 if (mddev->pers) {
6200 int err;
6201 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006202 pr_warn("%s: personality does not support diskops!\n",
6203 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 return -EINVAL;
6205 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006206 if (mddev->persistent)
6207 rdev = md_import_device(dev, mddev->major_version,
6208 mddev->minor_version);
6209 else
6210 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006212 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 PTR_ERR(rdev));
6214 return PTR_ERR(rdev);
6215 }
NeilBrown1a855a02010-12-09 16:36:28 +11006216 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006217 if (!mddev->persistent) {
6218 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006219 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006220 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006221 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006222 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006223 } else
NeilBrown41158c72005-06-21 17:17:25 -07006224 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006225 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006226 } else
6227 super_types[mddev->major_version].
6228 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006229 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006230 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006231 /* This was a hot-add request, but events doesn't
6232 * match, so reject it.
6233 */
6234 export_rdev(rdev);
6235 return -EINVAL;
6236 }
6237
NeilBrownb2d444d2005-11-08 21:39:31 -08006238 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006239 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6240 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006241 else
6242 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006243 if (info->state & (1<<MD_DISK_FAILFAST))
6244 set_bit(FailFast, &rdev->flags);
6245 else
6246 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006247
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006248 if (info->state & (1<<MD_DISK_JOURNAL)) {
6249 struct md_rdev *rdev2;
6250 bool has_journal = false;
6251
6252 /* make sure no existing journal disk */
6253 rdev_for_each(rdev2, mddev) {
6254 if (test_bit(Journal, &rdev2->flags)) {
6255 has_journal = true;
6256 break;
6257 }
6258 }
6259 if (has_journal) {
6260 export_rdev(rdev);
6261 return -EBUSY;
6262 }
Song Liubac624f2015-08-13 14:31:55 -07006263 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006264 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006265 /*
6266 * check whether the device shows up in other nodes
6267 */
6268 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006269 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006270 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006271 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006272 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006273 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006274 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006275 export_rdev(rdev);
6276 return err;
6277 }
6278 }
6279 }
6280
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 rdev->raid_disk = -1;
6282 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006283
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 if (err)
6285 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006286
6287 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006288 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6289 if (!err) {
6290 err = md_cluster_ops->new_disk_ack(mddev,
6291 err == 0);
6292 if (err)
6293 md_kick_rdev_from_array(rdev);
6294 }
6295 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006296 if (err)
6297 md_cluster_ops->add_new_disk_cancel(mddev);
6298 else
6299 err = add_bound_rdev(rdev);
6300 }
6301
6302 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006303 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006304
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 return err;
6306 }
6307
6308 /* otherwise, add_new_disk is only allowed
6309 * for major_version==0 superblocks
6310 */
6311 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006312 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 return -EINVAL;
6314 }
6315
6316 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6317 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006318 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006320 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 PTR_ERR(rdev));
6322 return PTR_ERR(rdev);
6323 }
6324 rdev->desc_nr = info->number;
6325 if (info->raid_disk < mddev->raid_disks)
6326 rdev->raid_disk = info->raid_disk;
6327 else
6328 rdev->raid_disk = -1;
6329
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006331 if (info->state & (1<<MD_DISK_SYNC))
6332 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006334 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6335 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006336 if (info->state & (1<<MD_DISK_FAILFAST))
6337 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006338
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006340 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006341 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6342 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006343 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006344 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345
NeilBrown2bf071b2006-01-06 00:20:55 -08006346 err = bind_rdev_to_array(rdev, mddev);
6347 if (err) {
6348 export_rdev(rdev);
6349 return err;
6350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 }
6352
6353 return 0;
6354}
6355
NeilBrownf72ffdd2014-09-30 14:23:59 +10006356static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357{
6358 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006359 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 rdev = find_rdev(mddev, dev);
6362 if (!rdev)
6363 return -ENXIO;
6364
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006365 if (rdev->raid_disk < 0)
6366 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006367
NeilBrown3ea8929d2013-04-24 11:42:41 +10006368 clear_bit(Blocked, &rdev->flags);
6369 remove_and_add_spares(mddev, rdev);
6370
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 if (rdev->raid_disk >= 0)
6372 goto busy;
6373
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006374kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006375 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006376 md_cluster_ops->remove_disk(mddev, rdev);
6377
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006378 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006379 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006380 if (mddev->thread)
6381 md_wakeup_thread(mddev->thread);
6382 else
6383 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006384 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
6386 return 0;
6387busy:
NeilBrown9d487392016-11-02 14:16:49 +11006388 pr_debug("md: cannot remove active disk %s from %s ...\n",
6389 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 return -EBUSY;
6391}
6392
NeilBrownf72ffdd2014-09-30 14:23:59 +10006393static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394{
6395 char b[BDEVNAME_SIZE];
6396 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006397 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398
6399 if (!mddev->pers)
6400 return -ENODEV;
6401
6402 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006403 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 mdname(mddev));
6405 return -EINVAL;
6406 }
6407 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006408 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 mdname(mddev));
6410 return -EINVAL;
6411 }
6412
NeilBrownd710e132008-10-13 11:55:12 +11006413 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006415 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 PTR_ERR(rdev));
6417 return -EINVAL;
6418 }
6419
6420 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006421 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006423 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424
NeilBrown8190e752009-06-18 08:48:58 +10006425 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
NeilBrownb2d444d2005-11-08 21:39:31 -08006427 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006428 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 bdevname(rdev->bdev,b), mdname(mddev));
6430 err = -EINVAL;
6431 goto abort_export;
6432 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006433
NeilBrownb2d444d2005-11-08 21:39:31 -08006434 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006436 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006437 err = bind_rdev_to_array(rdev, mddev);
6438 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006439 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440
6441 /*
6442 * The rest should better be atomic, we can have disk failures
6443 * noticed in interrupt contexts ...
6444 */
6445
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 rdev->raid_disk = -1;
6447
Shaohua Li29530792016-12-08 15:48:19 -08006448 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006449 if (!mddev->thread)
6450 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 /*
6452 * Kick recovery, maybe this spare has to be added to the
6453 * array immediately.
6454 */
6455 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6456 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006457 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 return 0;
6459
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460abort_export:
6461 export_rdev(rdev);
6462 return err;
6463}
6464
NeilBrownfd01b882011-10-11 16:47:53 +11006465static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006466{
NeilBrown035328c2014-04-09 12:25:40 +10006467 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006468
NeilBrown36fa3062005-09-09 16:23:45 -07006469 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006470 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006471 return -EBUSY;
6472 if (mddev->recovery || mddev->sync_thread)
6473 return -EBUSY;
6474 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006475 }
6476
NeilBrown36fa3062005-09-09 16:23:45 -07006477 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006478 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006479 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006480
NeilBrown1e594bb2014-12-15 12:57:00 +11006481 if (mddev->bitmap || mddev->bitmap_info.file)
6482 return -EEXIST; /* cannot add when bitmap is present */
6483 f = fget(fd);
6484
6485 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006486 pr_warn("%s: error: failed to get bitmap file\n",
6487 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006488 return -EBADF;
6489 }
6490
NeilBrown1e594bb2014-12-15 12:57:00 +11006491 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006492 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006493 pr_warn("%s: error: bitmap file must be a regular file\n",
6494 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006495 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006496 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006497 pr_warn("%s: error: bitmap file must open for write\n",
6498 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006499 err = -EBADF;
6500 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006501 pr_warn("%s: error: bitmap file is already in use\n",
6502 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006503 err = -EBUSY;
6504 }
6505 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006506 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006507 return err;
6508 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006509 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006510 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006511 } else if (mddev->bitmap == NULL)
6512 return -ENOENT; /* cannot remove what isn't there */
6513 err = 0;
6514 if (mddev->pers) {
6515 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006516 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006517 struct bitmap *bitmap;
6518
6519 bitmap = bitmap_create(mddev, -1);
6520 if (!IS_ERR(bitmap)) {
6521 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006522 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006523 } else
6524 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006525 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006526 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006527 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006528 fd = -1; /* make sure to put the file */
6529 }
NeilBrown36fa3062005-09-09 16:23:45 -07006530 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006531 }
6532 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006533 struct file *f = mddev->bitmap_info.file;
6534 if (f) {
6535 spin_lock(&mddev->lock);
6536 mddev->bitmap_info.file = NULL;
6537 spin_unlock(&mddev->lock);
6538 fput(f);
6539 }
NeilBrown36fa3062005-09-09 16:23:45 -07006540 }
6541
NeilBrown32a76272005-06-21 17:17:14 -07006542 return err;
6543}
6544
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545/*
6546 * set_array_info is used two different ways
6547 * The original usage is when creating a new array.
6548 * In this usage, raid_disks is > 0 and it together with
6549 * level, size, not_persistent,layout,chunksize determine the
6550 * shape of the array.
6551 * This will always create an array with a type-0.90.0 superblock.
6552 * The newer usage is when assembling an array.
6553 * In this case raid_disks will be 0, and the major_version field is
6554 * use to determine which style super-blocks are to be found on the devices.
6555 * The minor and patch _version numbers are also kept incase the
6556 * super_block handler wishes to interpret them.
6557 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006558static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559{
6560
6561 if (info->raid_disks == 0) {
6562 /* just setting version number for superblock loading */
6563 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006564 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 super_types[info->major_version].name == NULL) {
6566 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006567 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 info->major_version);
6569 return -EINVAL;
6570 }
6571 mddev->major_version = info->major_version;
6572 mddev->minor_version = info->minor_version;
6573 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006574 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006575 /* ensure mddev_put doesn't delete this now that there
6576 * is some minimal configuration.
6577 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006578 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 return 0;
6580 }
6581 mddev->major_version = MD_MAJOR_VERSION;
6582 mddev->minor_version = MD_MINOR_VERSION;
6583 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006584 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585
6586 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006587 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006588 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 mddev->raid_disks = info->raid_disks;
6590 /* don't set md_minor, it is determined by which /dev/md* was
6591 * openned
6592 */
6593 if (info->state & (1<<MD_SB_CLEAN))
6594 mddev->recovery_cp = MaxSector;
6595 else
6596 mddev->recovery_cp = 0;
6597 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006598 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599
6600 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006601 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
Shaohua Li29530792016-12-08 15:48:19 -08006603 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006604 mddev->max_disks = MD_SB_DISKS;
6605 mddev->flags = 0;
6606 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006607 }
6608 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609
NeilBrownc3d97142009-12-14 12:49:52 +11006610 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006611 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006612 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006613
NeilBrownf6705572006-03-27 01:18:11 -08006614 mddev->reshape_position = MaxSector;
6615
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 /*
6617 * Generate a 128 bit UUID
6618 */
6619 get_random_bytes(mddev->uuid, 16);
6620
NeilBrownf6705572006-03-27 01:18:11 -08006621 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006622 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006623 mddev->new_layout = mddev->layout;
6624 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006625 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006626
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 return 0;
6628}
6629
NeilBrownfd01b882011-10-11 16:47:53 +11006630void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006631{
Dan Williamsb522adc2009-03-31 15:00:31 +11006632 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6633
6634 if (mddev->external_size)
6635 return;
6636
Dan Williams1f403622009-03-31 14:59:03 +11006637 mddev->array_sectors = array_sectors;
6638}
6639EXPORT_SYMBOL(md_set_array_sectors);
6640
NeilBrownfd01b882011-10-11 16:47:53 +11006641static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006642{
NeilBrown3cb03002011-10-11 16:45:26 +11006643 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006644 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006645 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006646 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006647
NeilBrowna35b0d62006-01-06 00:20:49 -08006648 if (mddev->pers->resize == NULL)
6649 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006650 /* The "num_sectors" is the number of sectors of each device that
6651 * is used. This can only make sense for arrays with redundancy.
6652 * linear and raid0 always use whatever space is available. We can only
6653 * consider changing this number if no resync or reconstruction is
6654 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006655 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006656 * of each device. If num_sectors is zero, we find the largest size
6657 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006658 */
NeilBrownf851b602014-12-11 10:02:10 +11006659 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6660 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006661 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006662 if (mddev->ro)
6663 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006664
NeilBrowndafb20f2012-03-19 12:46:39 +11006665 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006666 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006667
Andre Nolld71f9f82008-07-11 22:02:22 +10006668 if (fit && (num_sectors == 0 || num_sectors > avail))
6669 num_sectors = avail;
6670 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006671 return -ENOSPC;
6672 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006673 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006674 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006675 if (mddev_is_clustered(mddev))
6676 md_cluster_ops->update_size(mddev, old_dev_sectors);
6677 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006678 set_capacity(mddev->gendisk, mddev->array_sectors);
6679 revalidate_disk(mddev->gendisk);
6680 }
6681 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006682 return rv;
6683}
6684
NeilBrownfd01b882011-10-11 16:47:53 +11006685static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006686{
6687 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006688 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006689 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006690 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006691 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006692 if (mddev->ro)
6693 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006694 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006695 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006696 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006697 if (mddev->sync_thread ||
6698 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6699 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006700 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006701
6702 rdev_for_each(rdev, mddev) {
6703 if (mddev->raid_disks < raid_disks &&
6704 rdev->data_offset < rdev->new_data_offset)
6705 return -EINVAL;
6706 if (mddev->raid_disks > raid_disks &&
6707 rdev->data_offset > rdev->new_data_offset)
6708 return -EINVAL;
6709 }
6710
NeilBrown63c70c42006-03-27 01:18:13 -08006711 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006712 if (mddev->delta_disks < 0)
6713 mddev->reshape_backwards = 1;
6714 else if (mddev->delta_disks > 0)
6715 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006716
6717 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006718 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006719 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006720 mddev->reshape_backwards = 0;
6721 }
NeilBrownda943b992006-01-06 00:20:54 -08006722 return rv;
6723}
6724
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725/*
6726 * update_array_info is used to change the configuration of an
6727 * on-line array.
6728 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6729 * fields in the info are checked against the array.
6730 * Any differences that cannot be handled will cause an error.
6731 * Normally, only one change can be managed at a time.
6732 */
NeilBrownfd01b882011-10-11 16:47:53 +11006733static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734{
6735 int rv = 0;
6736 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006737 int state = 0;
6738
6739 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006740 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006741 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
6743 if (mddev->major_version != info->major_version ||
6744 mddev->minor_version != info->minor_version ||
6745/* mddev->patch_version != info->patch_version || */
6746 mddev->ctime != info->ctime ||
6747 mddev->level != info->level ||
6748/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006749 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006750 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006751 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6752 ((state^info->state) & 0xfffffe00)
6753 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 return -EINVAL;
6755 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006756 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6757 cnt++;
6758 if (mddev->raid_disks != info->raid_disks)
6759 cnt++;
6760 if (mddev->layout != info->layout)
6761 cnt++;
6762 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6763 cnt++;
6764 if (cnt == 0)
6765 return 0;
6766 if (cnt > 1)
6767 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
6769 if (mddev->layout != info->layout) {
6770 /* Change layout
6771 * we don't need to do anything at the md level, the
6772 * personality will take care of it all.
6773 */
NeilBrown50ac1682009-06-18 08:47:55 +10006774 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006776 else {
6777 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006778 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006779 if (rv)
6780 mddev->new_layout = mddev->layout;
6781 return rv;
6782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006784 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006785 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
NeilBrownda943b992006-01-06 00:20:54 -08006787 if (mddev->raid_disks != info->raid_disks)
6788 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
NeilBrown36fa3062005-09-09 16:23:45 -07006790 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006791 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6792 rv = -EINVAL;
6793 goto err;
6794 }
6795 if (mddev->recovery || mddev->sync_thread) {
6796 rv = -EBUSY;
6797 goto err;
6798 }
NeilBrown36fa3062005-09-09 16:23:45 -07006799 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006800 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006801 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006802 if (mddev->bitmap) {
6803 rv = -EEXIST;
6804 goto err;
6805 }
6806 if (mddev->bitmap_info.default_offset == 0) {
6807 rv = -EINVAL;
6808 goto err;
6809 }
NeilBrownc3d97142009-12-14 12:49:52 +11006810 mddev->bitmap_info.offset =
6811 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006812 mddev->bitmap_info.space =
6813 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006814 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006815 bitmap = bitmap_create(mddev, -1);
6816 if (!IS_ERR(bitmap)) {
6817 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006818 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006819 } else
6820 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006821 if (rv)
6822 bitmap_destroy(mddev);
6823 mddev->pers->quiesce(mddev, 0);
6824 } else {
6825 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006826 if (!mddev->bitmap) {
6827 rv = -ENOENT;
6828 goto err;
6829 }
6830 if (mddev->bitmap->storage.file) {
6831 rv = -EINVAL;
6832 goto err;
6833 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006834 if (mddev->bitmap_info.nodes) {
6835 /* hold PW on all the bitmap lock */
6836 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006837 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006838 rv = -EPERM;
6839 md_cluster_ops->unlock_all_bitmaps(mddev);
6840 goto err;
6841 }
6842
6843 mddev->bitmap_info.nodes = 0;
6844 md_cluster_ops->leave(mddev);
6845 }
NeilBrown36fa3062005-09-09 16:23:45 -07006846 mddev->pers->quiesce(mddev, 1);
6847 bitmap_destroy(mddev);
6848 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006849 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006850 }
6851 }
NeilBrown850b2b422006-10-03 01:15:46 -07006852 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006853 return rv;
6854err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 return rv;
6856}
6857
NeilBrownfd01b882011-10-11 16:47:53 +11006858static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859{
NeilBrown3cb03002011-10-11 16:45:26 +11006860 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006861 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862
6863 if (mddev->pers == NULL)
6864 return -ENODEV;
6865
NeilBrown1ca69c42012-10-11 13:37:33 +11006866 rcu_read_lock();
6867 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006869 err = -ENODEV;
6870 else {
6871 md_error(mddev, rdev);
6872 if (!test_bit(Faulty, &rdev->flags))
6873 err = -EBUSY;
6874 }
6875 rcu_read_unlock();
6876 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877}
6878
Andre Noll2f9618c2008-04-25 18:57:58 +02006879/*
6880 * We have a problem here : there is no easy way to give a CHS
6881 * virtual geometry. We currently pretend that we have a 2 heads
6882 * 4 sectors (with a BIG number of cylinders...). This drives
6883 * dosfs just mad... ;-)
6884 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006885static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6886{
NeilBrownfd01b882011-10-11 16:47:53 +11006887 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006888
6889 geo->heads = 2;
6890 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006891 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006892 return 0;
6893}
6894
Nicolas Schichancb335f82014-01-15 16:58:52 +01006895static inline bool md_ioctl_valid(unsigned int cmd)
6896{
6897 switch (cmd) {
6898 case ADD_NEW_DISK:
6899 case BLKROSET:
6900 case GET_ARRAY_INFO:
6901 case GET_BITMAP_FILE:
6902 case GET_DISK_INFO:
6903 case HOT_ADD_DISK:
6904 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006905 case RAID_AUTORUN:
6906 case RAID_VERSION:
6907 case RESTART_ARRAY_RW:
6908 case RUN_ARRAY:
6909 case SET_ARRAY_INFO:
6910 case SET_BITMAP_FILE:
6911 case SET_DISK_FAULTY:
6912 case STOP_ARRAY:
6913 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006914 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006915 return true;
6916 default:
6917 return false;
6918 }
6919}
6920
Al Viroa39907f2008-03-02 10:31:15 -05006921static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922 unsigned int cmd, unsigned long arg)
6923{
6924 int err = 0;
6925 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006926 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006927 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928
Nicolas Schichancb335f82014-01-15 16:58:52 +01006929 if (!md_ioctl_valid(cmd))
6930 return -ENOTTY;
6931
NeilBrown506c9e42011-12-23 10:17:26 +11006932 switch (cmd) {
6933 case RAID_VERSION:
6934 case GET_ARRAY_INFO:
6935 case GET_DISK_INFO:
6936 break;
6937 default:
6938 if (!capable(CAP_SYS_ADMIN))
6939 return -EACCES;
6940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
6942 /*
6943 * Commands dealing with the RAID driver but not any
6944 * particular array:
6945 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006946 switch (cmd) {
6947 case RAID_VERSION:
6948 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006952 case RAID_AUTORUN:
6953 err = 0;
6954 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006955 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006957 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 }
6959
6960 /*
6961 * Commands creating/starting a new array:
6962 */
6963
Al Viroa39907f2008-03-02 10:31:15 -05006964 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
6966 if (!mddev) {
6967 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006968 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 }
6970
NeilBrown1ca69c42012-10-11 13:37:33 +11006971 /* Some actions do not requires the mutex */
6972 switch (cmd) {
6973 case GET_ARRAY_INFO:
6974 if (!mddev->raid_disks && !mddev->external)
6975 err = -ENODEV;
6976 else
6977 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006978 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006979
6980 case GET_DISK_INFO:
6981 if (!mddev->raid_disks && !mddev->external)
6982 err = -ENODEV;
6983 else
6984 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006985 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006986
6987 case SET_DISK_FAULTY:
6988 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006989 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006990
6991 case GET_BITMAP_FILE:
6992 err = get_bitmap_file(mddev, argp);
6993 goto out;
6994
NeilBrown1ca69c42012-10-11 13:37:33 +11006995 }
6996
NeilBrowna7a3f082012-12-11 13:35:54 +11006997 if (cmd == ADD_NEW_DISK)
6998 /* need to ensure md_delayed_delete() has completed */
6999 flush_workqueue(md_misc_wq);
7000
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007001 if (cmd == HOT_REMOVE_DISK)
7002 /* need to ensure recovery thread has run */
7003 wait_event_interruptible_timeout(mddev->sb_wait,
7004 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007005 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007006 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007007 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7008 /* Need to flush page cache, and ensure no-one else opens
7009 * and writes
7010 */
7011 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007012 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007013 mutex_unlock(&mddev->open_mutex);
7014 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007015 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007016 }
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007017 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown260fa032013-08-27 16:44:13 +10007018 mutex_unlock(&mddev->open_mutex);
7019 sync_blockdev(bdev);
7020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 err = mddev_lock(mddev);
7022 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007023 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7024 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007025 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 }
7027
NeilBrownc02c0ae2012-12-11 13:39:21 +11007028 if (cmd == SET_ARRAY_INFO) {
7029 mdu_array_info_t info;
7030 if (!arg)
7031 memset(&info, 0, sizeof(info));
7032 else if (copy_from_user(&info, argp, sizeof(info))) {
7033 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007034 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007035 }
7036 if (mddev->pers) {
7037 err = update_array_info(mddev, &info);
7038 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007039 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007040 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007042 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007043 }
7044 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007045 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007046 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007047 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007048 }
7049 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007050 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007051 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007052 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007053 }
7054 err = set_array_info(mddev, &info);
7055 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007056 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007057 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007058 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007059 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 }
7061
7062 /*
7063 * Commands querying/configuring an existing array:
7064 */
NeilBrown32a76272005-06-21 17:17:14 -07007065 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007066 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007067 if ((!mddev->raid_disks && !mddev->external)
7068 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7069 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7070 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007072 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 }
7074
7075 /*
7076 * Commands even a read-only array can execute:
7077 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007078 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007079 case RESTART_ARRAY_RW:
7080 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007081 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007082
7083 case STOP_ARRAY:
7084 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007085 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007086
7087 case STOP_ARRAY_RO:
7088 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007089 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007090
NeilBrown3ea8929d2013-04-24 11:42:41 +10007091 case HOT_REMOVE_DISK:
7092 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007093 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007094
NeilBrown7ceb17e2013-04-24 11:42:42 +10007095 case ADD_NEW_DISK:
7096 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007097 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007098 * So require mddev->pers and MD_DISK_SYNC.
7099 */
7100 if (mddev->pers) {
7101 mdu_disk_info_t info;
7102 if (copy_from_user(&info, argp, sizeof(info)))
7103 err = -EFAULT;
7104 else if (!(info.state & (1<<MD_DISK_SYNC)))
7105 /* Need to clear read-only for this */
7106 break;
7107 else
7108 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007109 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007110 }
7111 break;
7112
NeilBrownc02c0ae2012-12-11 13:39:21 +11007113 case BLKROSET:
7114 if (get_user(ro, (int __user *)(arg))) {
7115 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007116 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007117 }
7118 err = -EINVAL;
7119
7120 /* if the bdev is going readonly the value of mddev->ro
7121 * does not matter, no writes are coming
7122 */
7123 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007124 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007125
NeilBrownc02c0ae2012-12-11 13:39:21 +11007126 /* are we are already prepared for writes? */
7127 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007128 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007129
7130 /* transitioning to readauto need only happen for
7131 * arrays that call md_write_start
7132 */
7133 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007135 if (err == 0) {
7136 mddev->ro = 2;
7137 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007138 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007139 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007140 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 }
7142
7143 /*
7144 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007145 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 */
NeilBrown326eb172014-09-30 15:36:28 +10007147 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007148 if (mddev->ro == 2) {
7149 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007150 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007151 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007152 /* mddev_unlock will wake thread */
7153 /* If a device failed while we were read-only, we
7154 * need to make sure the metadata is updated now.
7155 */
Shaohua Li29530792016-12-08 15:48:19 -08007156 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007157 mddev_unlock(mddev);
7158 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007159 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7160 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007161 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007162 }
NeilBrownf91de922005-11-08 21:39:36 -08007163 } else {
7164 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007165 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 }
7168
NeilBrownc02c0ae2012-12-11 13:39:21 +11007169 switch (cmd) {
7170 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007172 mdu_disk_info_t info;
7173 if (copy_from_user(&info, argp, sizeof(info)))
7174 err = -EFAULT;
7175 else
7176 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007177 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007180 case CLUSTERED_DISK_NACK:
7181 if (mddev_is_clustered(mddev))
7182 md_cluster_ops->new_disk_ack(mddev, false);
7183 else
7184 err = -EINVAL;
7185 goto unlock;
7186
NeilBrownc02c0ae2012-12-11 13:39:21 +11007187 case HOT_ADD_DISK:
7188 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007189 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190
NeilBrownc02c0ae2012-12-11 13:39:21 +11007191 case RUN_ARRAY:
7192 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007193 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194
NeilBrownc02c0ae2012-12-11 13:39:21 +11007195 case SET_BITMAP_FILE:
7196 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007197 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007198
NeilBrownc02c0ae2012-12-11 13:39:21 +11007199 default:
7200 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007201 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 }
7203
NeilBrown3adc28d2014-09-30 15:46:41 +10007204unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007205 if (mddev->hold_active == UNTIL_IOCTL &&
7206 err != -EINVAL)
7207 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007209out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 return err;
7211}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007212#ifdef CONFIG_COMPAT
7213static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7214 unsigned int cmd, unsigned long arg)
7215{
7216 switch (cmd) {
7217 case HOT_REMOVE_DISK:
7218 case HOT_ADD_DISK:
7219 case SET_DISK_FAULTY:
7220 case SET_BITMAP_FILE:
7221 /* These take in integer arg, do not convert */
7222 break;
7223 default:
7224 arg = (unsigned long)compat_ptr(arg);
7225 break;
7226 }
7227
7228 return md_ioctl(bdev, mode, cmd, arg);
7229}
7230#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231
Al Viroa39907f2008-03-02 10:31:15 -05007232static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233{
7234 /*
7235 * Succeed if we can lock the mddev, which confirms that
7236 * it isn't being stopped right now.
7237 */
NeilBrownfd01b882011-10-11 16:47:53 +11007238 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 int err;
7240
Yuanhan Liu0c098222012-05-22 13:55:32 +10007241 if (!mddev)
7242 return -ENODEV;
7243
NeilBrownd3374822009-01-09 08:31:10 +11007244 if (mddev->gendisk != bdev->bd_disk) {
7245 /* we are racing with mddev_put which is discarding this
7246 * bd_disk.
7247 */
7248 mddev_put(mddev);
7249 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007250 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007251 /* Then retry the open from the top */
7252 return -ERESTARTSYS;
7253 }
7254 BUG_ON(mddev != bdev->bd_disk->private_data);
7255
NeilBrownc8c00a62009-08-10 12:50:52 +10007256 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 goto out;
7258
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007259 if (test_bit(MD_CLOSING, &mddev->flags)) {
7260 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007261 err = -ENODEV;
7262 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007263 }
7264
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007266 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007267 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007269 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007271 if (err)
7272 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 return err;
7274}
7275
Al Virodb2a1442013-05-05 21:52:57 -04007276static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007278 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007280 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007281 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007284
7285static int md_media_changed(struct gendisk *disk)
7286{
NeilBrownfd01b882011-10-11 16:47:53 +11007287 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007288
7289 return mddev->changed;
7290}
7291
7292static int md_revalidate(struct gendisk *disk)
7293{
NeilBrownfd01b882011-10-11 16:47:53 +11007294 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007295
7296 mddev->changed = 0;
7297 return 0;
7298}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007299static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300{
7301 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007302 .open = md_open,
7303 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007304 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007305#ifdef CONFIG_COMPAT
7306 .compat_ioctl = md_compat_ioctl,
7307#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007308 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007309 .media_changed = md_media_changed,
7310 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311};
7312
NeilBrownf72ffdd2014-09-30 14:23:59 +10007313static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314{
NeilBrown2b8bf342011-10-11 16:48:23 +11007315 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 /*
7318 * md_thread is a 'system-thread', it's priority should be very
7319 * high. We avoid resource deadlocks individually in each
7320 * raid personality. (RAID5 does preallocation) We also use RR and
7321 * the very same RT priority as kswapd, thus we will never get
7322 * into a priority inversion deadlock.
7323 *
7324 * we definitely have to have equal or higher priority than
7325 * bdflush, otherwise bdflush will deadlock if there are too
7326 * many dirty RAID5 blocks.
7327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328
NeilBrown6985c432005-10-19 21:23:47 -07007329 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007330 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331
NeilBrown93588e22005-11-15 00:09:12 -08007332 /* We need to wait INTERRUPTIBLE so that
7333 * we don't add to the load-average.
7334 * That means we need to be sure no signals are
7335 * pending
7336 */
7337 if (signal_pending(current))
7338 flush_signals(current);
7339
7340 wait_event_interruptible_timeout
7341 (thread->wqueue,
7342 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007343 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007344 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345
NeilBrown6c987912011-01-14 09:13:53 +11007346 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007347 if (kthread_should_park())
7348 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007349 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007350 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007352
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 return 0;
7354}
7355
NeilBrown2b8bf342011-10-11 16:48:23 +11007356void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357{
7358 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007359 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
NeilBrown4ad23a972017-03-15 14:05:14 +11007360 if (!test_and_set_bit(THREAD_WAKEUP, &thread->flags))
7361 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 }
7363}
NeilBrown6c144d32014-09-30 16:15:38 +10007364EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
Shaohua Li4ed87312012-10-11 13:34:00 +11007366struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7367 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368{
NeilBrown2b8bf342011-10-11 16:48:23 +11007369 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370
NeilBrown2b8bf342011-10-11 16:48:23 +11007371 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 if (!thread)
7373 return NULL;
7374
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 init_waitqueue_head(&thread->wqueue);
7376
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377 thread->run = run;
7378 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007379 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007380 thread->tsk = kthread_run(md_thread, thread,
7381 "%s_%s",
7382 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007383 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007384 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 kfree(thread);
7386 return NULL;
7387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 return thread;
7389}
NeilBrown6c144d32014-09-30 16:15:38 +10007390EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391
NeilBrown2b8bf342011-10-11 16:48:23 +11007392void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393{
NeilBrown2b8bf342011-10-11 16:48:23 +11007394 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007395 if (!thread)
7396 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007397 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007398 /* Locking ensures that mddev_unlock does not wake_up a
7399 * non-existent thread
7400 */
7401 spin_lock(&pers_lock);
7402 *threadp = NULL;
7403 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007404
7405 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 kfree(thread);
7407}
NeilBrown6c144d32014-09-30 16:15:38 +10007408EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
NeilBrownfd01b882011-10-11 16:47:53 +11007410void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411{
NeilBrownb2d444d2005-11-08 21:39:31 -08007412 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007414
NeilBrownde393cd2011-07-28 11:31:48 +10007415 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 return;
7417 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007418 if (mddev->degraded)
7419 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007420 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7422 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7423 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007424 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007425 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007426 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427}
NeilBrown6c144d32014-09-30 16:15:38 +10007428EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429
7430/* seq_file implementation /proc/mdstat */
7431
7432static void status_unused(struct seq_file *seq)
7433{
7434 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007435 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436
7437 seq_printf(seq, "unused devices: ");
7438
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007439 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 char b[BDEVNAME_SIZE];
7441 i++;
7442 seq_printf(seq, "%s ",
7443 bdevname(rdev->bdev,b));
7444 }
7445 if (!i)
7446 seq_printf(seq, "<none>");
7447
7448 seq_printf(seq, "\n");
7449}
7450
NeilBrownf7851be2015-07-02 17:12:58 +10007451static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452{
NeilBrowndd71cf62009-05-07 12:49:35 +10007453 sector_t max_sectors, resync, res;
7454 unsigned long dt, db;
7455 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007456 int scale;
7457 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458
NeilBrownc804cde2012-05-21 09:28:33 +10007459 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7460 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007461 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007463 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464
NeilBrownf7851be2015-07-02 17:12:58 +10007465 resync = mddev->curr_resync;
7466 if (resync <= 3) {
7467 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7468 /* Still cleaning up */
7469 resync = max_sectors;
7470 } else
7471 resync -= atomic_read(&mddev->recovery_active);
7472
7473 if (resync == 0) {
7474 if (mddev->recovery_cp < MaxSector) {
7475 seq_printf(seq, "\tresync=PENDING");
7476 return 1;
7477 }
7478 return 0;
7479 }
7480 if (resync < 3) {
7481 seq_printf(seq, "\tresync=DELAYED");
7482 return 1;
7483 }
7484
NeilBrown403df472014-09-30 15:52:29 +10007485 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007486 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007487 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007488 * u32, as those are the requirements for sector_div.
7489 * Thus 'scale' must be at least 10
7490 */
7491 scale = 10;
7492 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007493 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007494 scale++;
7495 }
7496 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007497 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007498
7499 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 {
NeilBrown4588b422006-03-27 01:18:04 -08007501 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 seq_printf(seq, "[");
7503 for (i = 0; i < x; i++)
7504 seq_printf(seq, "=");
7505 seq_printf(seq, ">");
7506 for (i = 0; i < y; i++)
7507 seq_printf(seq, ".");
7508 seq_printf(seq, "] ");
7509 }
NeilBrown4588b422006-03-27 01:18:04 -08007510 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007511 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7512 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007513 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7514 "check" :
7515 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7516 "resync" : "recovery"))),
7517 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007518 (unsigned long long) resync/2,
7519 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520
7521 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 * dt: time from mark until now
7523 * db: blocks written from mark until now
7524 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007525 *
7526 * rt is a sector_t, so could be 32bit or 64bit.
7527 * So we divide before multiply in case it is 32bit and close
7528 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007529 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007530 * near the end of resync when the number of remaining sectors
7531 * is close to 'db'.
7532 * We then divide rt by 32 after multiplying by db to compensate.
7533 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 */
7535 dt = ((jiffies - mddev->resync_mark) / HZ);
7536 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007537 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7538 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539
NeilBrowndd71cf62009-05-07 12:49:35 +10007540 rt = max_sectors - resync; /* number of remaining sectors */
7541 sector_div(rt, db/32+1);
7542 rt *= dt;
7543 rt >>= 5;
7544
7545 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7546 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547
NeilBrownff4e8d92006-07-10 04:44:16 -07007548 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007549 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550}
7551
7552static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7553{
7554 struct list_head *tmp;
7555 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007556 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557
7558 if (l >= 0x10000)
7559 return NULL;
7560 if (!l--)
7561 /* header */
7562 return (void*)1;
7563
7564 spin_lock(&all_mddevs_lock);
7565 list_for_each(tmp,&all_mddevs)
7566 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007567 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 mddev_get(mddev);
7569 spin_unlock(&all_mddevs_lock);
7570 return mddev;
7571 }
7572 spin_unlock(&all_mddevs_lock);
7573 if (!l--)
7574 return (void*)2;/* tail */
7575 return NULL;
7576}
7577
7578static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7579{
7580 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007581 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007582
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 ++*pos;
7584 if (v == (void*)2)
7585 return NULL;
7586
7587 spin_lock(&all_mddevs_lock);
7588 if (v == (void*)1)
7589 tmp = all_mddevs.next;
7590 else
7591 tmp = mddev->all_mddevs.next;
7592 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007593 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 else {
7595 next_mddev = (void*)2;
7596 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598 spin_unlock(&all_mddevs_lock);
7599
7600 if (v != (void*)1)
7601 mddev_put(mddev);
7602 return next_mddev;
7603
7604}
7605
7606static void md_seq_stop(struct seq_file *seq, void *v)
7607{
NeilBrownfd01b882011-10-11 16:47:53 +11007608 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609
7610 if (mddev && v != (void*)1 && v != (void*)2)
7611 mddev_put(mddev);
7612}
7613
7614static int md_seq_show(struct seq_file *seq, void *v)
7615{
NeilBrownfd01b882011-10-11 16:47:53 +11007616 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007617 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007618 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619
7620 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007621 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 seq_printf(seq, "Personalities : ");
7623 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007624 list_for_each_entry(pers, &pers_list, list)
7625 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626
7627 spin_unlock(&pers_lock);
7628 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007629 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 return 0;
7631 }
7632 if (v == (void*)2) {
7633 status_unused(seq);
7634 return 0;
7635 }
7636
NeilBrown36d091f2014-12-15 12:56:58 +11007637 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7639 seq_printf(seq, "%s : %sactive", mdname(mddev),
7640 mddev->pers ? "" : "in");
7641 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007642 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007644 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007645 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 seq_printf(seq, " %s", mddev->pers->name);
7647 }
7648
Andre Nolldd8ac332009-03-31 14:33:13 +11007649 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007650 rcu_read_lock();
7651 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 char b[BDEVNAME_SIZE];
7653 seq_printf(seq, " %s[%d]",
7654 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007655 if (test_bit(WriteMostly, &rdev->flags))
7656 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007657 if (test_bit(Journal, &rdev->flags))
7658 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007659 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 seq_printf(seq, "(F)");
7661 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007662 }
7663 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007664 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007665 if (test_bit(Replacement, &rdev->flags))
7666 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007667 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007669 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670
7671 if (!list_empty(&mddev->disks)) {
7672 if (mddev->pers)
7673 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007674 (unsigned long long)
7675 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 else
7677 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007678 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007680 if (mddev->persistent) {
7681 if (mddev->major_version != 0 ||
7682 mddev->minor_version != 90) {
7683 seq_printf(seq," super %d.%d",
7684 mddev->major_version,
7685 mddev->minor_version);
7686 }
NeilBrowne6910632008-02-06 01:39:51 -08007687 } else if (mddev->external)
7688 seq_printf(seq, " super external:%s",
7689 mddev->metadata_type);
7690 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007691 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692
7693 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007694 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007695 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007696 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007697 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007698 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007699 }
NeilBrown32a76272005-06-21 17:17:14 -07007700 } else
7701 seq_printf(seq, "\n ");
7702
NeilBrown57148962012-03-19 12:46:40 +11007703 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704
7705 seq_printf(seq, "\n");
7706 }
NeilBrown36d091f2014-12-15 12:56:58 +11007707 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007708
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 return 0;
7710}
7711
Jan Engelhardt110518b2009-05-07 12:49:37 +10007712static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 .start = md_seq_start,
7714 .next = md_seq_next,
7715 .stop = md_seq_stop,
7716 .show = md_seq_show,
7717};
7718
7719static int md_seq_open(struct inode *inode, struct file *file)
7720{
Kay Sieversf1514632011-07-12 20:48:39 +02007721 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722 int error;
7723
7724 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007725 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007726 return error;
7727
7728 seq = file->private_data;
7729 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730 return error;
7731}
7732
NeilBrowne2f23b62014-04-09 14:33:51 +10007733static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007734static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7735{
Kay Sieversf1514632011-07-12 20:48:39 +02007736 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007737 int mask;
7738
NeilBrowne2f23b62014-04-09 14:33:51 +10007739 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007740 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007741 poll_wait(filp, &md_event_waiters, wait);
7742
7743 /* always allow read */
7744 mask = POLLIN | POLLRDNORM;
7745
Kay Sieversf1514632011-07-12 20:48:39 +02007746 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007747 mask |= POLLERR | POLLPRI;
7748 return mask;
7749}
7750
Arjan van de Venfa027c22007-02-12 00:55:33 -08007751static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007752 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753 .open = md_seq_open,
7754 .read = seq_read,
7755 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007756 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007757 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007758};
7759
NeilBrown84fc4b52011-10-11 16:49:58 +11007760int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761{
NeilBrown9d487392016-11-02 14:16:49 +11007762 pr_debug("md: %s personality registered for level %d\n",
7763 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007765 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766 spin_unlock(&pers_lock);
7767 return 0;
7768}
NeilBrown6c144d32014-09-30 16:15:38 +10007769EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770
NeilBrown84fc4b52011-10-11 16:49:58 +11007771int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772{
NeilBrown9d487392016-11-02 14:16:49 +11007773 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007775 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776 spin_unlock(&pers_lock);
7777 return 0;
7778}
NeilBrown6c144d32014-09-30 16:15:38 +10007779EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780
NeilBrown6022e752015-08-13 12:32:55 +10007781int register_md_cluster_operations(struct md_cluster_operations *ops,
7782 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007783{
NeilBrown6022e752015-08-13 12:32:55 +10007784 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007785 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007786 if (md_cluster_ops != NULL)
7787 ret = -EALREADY;
7788 else {
7789 md_cluster_ops = ops;
7790 md_cluster_mod = module;
7791 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007792 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007793 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007794}
7795EXPORT_SYMBOL(register_md_cluster_operations);
7796
7797int unregister_md_cluster_operations(void)
7798{
7799 spin_lock(&pers_lock);
7800 md_cluster_ops = NULL;
7801 spin_unlock(&pers_lock);
7802 return 0;
7803}
7804EXPORT_SYMBOL(unregister_md_cluster_operations);
7805
7806int md_setup_cluster(struct mddev *mddev, int nodes)
7807{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007808 if (!md_cluster_ops)
7809 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007810 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007811 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007812 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11007813 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007814 spin_unlock(&pers_lock);
7815 return -ENOENT;
7816 }
7817 spin_unlock(&pers_lock);
7818
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007819 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007820}
7821
7822void md_cluster_stop(struct mddev *mddev)
7823{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007824 if (!md_cluster_ops)
7825 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007826 md_cluster_ops->leave(mddev);
7827 module_put(md_cluster_mod);
7828}
7829
NeilBrownfd01b882011-10-11 16:47:53 +11007830static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007832 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007834 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835
7836 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007837 rcu_read_lock();
7838 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007840 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7841 (int)part_stat_read(&disk->part0, sectors[1]) -
7842 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007843 /* sync IO will cause sync_io to increase before the disk_stats
7844 * as sync_io is counted when a request starts, and
7845 * disk_stats is counted when it completes.
7846 * So resync activity will cause curr_events to be smaller than
7847 * when there was no such activity.
7848 * non-sync IO will cause disk_stat to increase without
7849 * increasing sync_io so curr_events will (eventually)
7850 * be larger than it was before. Once it becomes
7851 * substantially larger, the test below will cause
7852 * the array to appear non-idle, and resync will slow
7853 * down.
7854 * If there is a lot of outstanding resync activity when
7855 * we set last_event to curr_events, then all that activity
7856 * completing might cause the array to appear non-idle
7857 * and resync will be slowed down even though there might
7858 * not have been non-resync activity. This will only
7859 * happen once though. 'last_events' will soon reflect
7860 * the state where there is little or no outstanding
7861 * resync requests, and further resync activity will
7862 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007863 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007865 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 rdev->last_events = curr_events;
7867 idle = 0;
7868 }
7869 }
NeilBrown4b809912008-07-21 17:05:25 +10007870 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 return idle;
7872}
7873
NeilBrownfd01b882011-10-11 16:47:53 +11007874void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875{
7876 /* another "blocks" (512byte) blocks have been synced */
7877 atomic_sub(blocks, &mddev->recovery_active);
7878 wake_up(&mddev->recovery_wait);
7879 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007880 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007881 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882 md_wakeup_thread(mddev->thread);
7883 // stop recovery, signal do_sync ....
7884 }
7885}
NeilBrown6c144d32014-09-30 16:15:38 +10007886EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887
NeilBrown06d91a52005-06-21 17:17:12 -07007888/* md_write_start(mddev, bi)
7889 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007890 * in superblock) before writing, schedule a superblock update
7891 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007892 */
NeilBrownfd01b882011-10-11 16:47:53 +11007893void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894{
Neil Brown0fd62b82008-06-28 08:31:36 +10007895 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007896 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007897 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007898
NeilBrownf91de922005-11-08 21:39:36 -08007899 BUG_ON(mddev->ro == 1);
7900 if (mddev->ro == 2) {
7901 /* need to switch to read/write */
7902 mddev->ro = 0;
7903 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7904 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007905 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007906 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007907 }
NeilBrown4ad23a972017-03-15 14:05:14 +11007908 rcu_read_lock();
7909 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11007910 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07007911 if (mddev->safemode == 1)
7912 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11007913 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
7914 if (mddev->in_sync || !mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11007915 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007916 if (mddev->in_sync) {
7917 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007918 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7919 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007920 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007921 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007922 }
NeilBrown85572d72014-12-15 12:56:56 +11007923 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007924 }
NeilBrown4ad23a972017-03-15 14:05:14 +11007925 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10007926 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007927 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007928 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007929 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930}
NeilBrown6c144d32014-09-30 16:15:38 +10007931EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932
NeilBrown49728052017-03-15 14:05:12 +11007933/* md_write_inc can only be called when md_write_start() has
7934 * already been called at least once of the current request.
7935 * It increments the counter and is useful when a single request
7936 * is split into several parts. Each part causes an increment and
7937 * so needs a matching md_write_end().
7938 * Unlike md_write_start(), it is safe to call md_write_inc() inside
7939 * a spinlocked region.
7940 */
7941void md_write_inc(struct mddev *mddev, struct bio *bi)
7942{
7943 if (bio_data_dir(bi) != WRITE)
7944 return;
7945 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11007946 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11007947}
7948EXPORT_SYMBOL(md_write_inc);
7949
NeilBrownfd01b882011-10-11 16:47:53 +11007950void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951{
NeilBrown4ad23a972017-03-15 14:05:14 +11007952 percpu_ref_put(&mddev->writes_pending);
7953
7954 if (mddev->safemode == 2)
7955 md_wakeup_thread(mddev->thread);
7956 else if (mddev->safemode_delay)
7957 /* The roundup() ensures this only performs locking once
7958 * every ->safemode_delay jiffies
7959 */
7960 mod_timer(&mddev->safemode_timer,
7961 roundup(jiffies, mddev->safemode_delay) +
7962 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963}
NeilBrown4ad23a972017-03-15 14:05:14 +11007964
NeilBrown6c144d32014-09-30 16:15:38 +10007965EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966
NeilBrown2a2275d2007-01-26 00:57:11 -08007967/* md_allow_write(mddev)
7968 * Calling this ensures that the array is marked 'active' so that writes
7969 * may proceed without blocking. It is important to call this before
7970 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7971 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007972 *
Shaohua Li29530792016-12-08 15:48:19 -08007973 * In the ->external case MD_SB_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007974 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007975 */
NeilBrownfd01b882011-10-11 16:47:53 +11007976int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007977{
7978 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007979 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007980 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007981 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007982 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007983 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007984
NeilBrown85572d72014-12-15 12:56:56 +11007985 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007986 if (mddev->in_sync) {
7987 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007988 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
7989 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007990 if (mddev->safemode_delay &&
7991 mddev->safemode == 0)
7992 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007993 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007994 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007995 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007996 } else
NeilBrown85572d72014-12-15 12:56:56 +11007997 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007998
Shaohua Li29530792016-12-08 15:48:19 -08007999 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07008000 return -EAGAIN;
8001 else
8002 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08008003}
8004EXPORT_SYMBOL_GPL(md_allow_write);
8005
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006#define SYNC_MARKS 10
8007#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008008#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008009void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010{
Shaohua Li4ed87312012-10-11 13:34:00 +11008011 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008012 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013 unsigned int currspeed = 0,
8014 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008015 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008017 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008018 sector_t mark_cnt[SYNC_MARKS];
8019 int last_mark,m;
8020 struct list_head *tmp;
8021 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008022 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008023 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008024 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008025 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008026 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027
8028 /* just incase thread restarts... */
8029 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8030 return;
NeilBrown3991b312014-05-28 13:39:23 +10008031 if (mddev->ro) {/* never try to sync a read-only array */
8032 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008033 return;
NeilBrown3991b312014-05-28 13:39:23 +10008034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008036 if (mddev_is_clustered(mddev)) {
8037 ret = md_cluster_ops->resync_start(mddev);
8038 if (ret)
8039 goto skip;
8040
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008041 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008042 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8043 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8044 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8045 && ((unsigned long long)mddev->curr_resync_completed
8046 < (unsigned long long)mddev->resync_max_sectors))
8047 goto skip;
8048 }
8049
NeilBrown61df9d92006-10-03 01:15:57 -07008050 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008051 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008052 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008053 action = "check";
8054 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008055 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008056 action = "repair";
8057 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008058 desc = "resync";
8059 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8060 desc = "reshape";
8061 else
8062 desc = "recovery";
8063
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008064 mddev->last_sync_action = action ?: desc;
8065
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066 /* we overload curr_resync somewhat here.
8067 * 0 == not engaged in resync at all
8068 * 2 == checking that there is no conflict with another sync
8069 * 1 == like 2, but have yielded to allow conflicting resync to
8070 * commense
8071 * other == active in resync - this many blocks
8072 *
8073 * Before starting a resync we must have set curr_resync to
8074 * 2, and then checked that every "conflicting" array has curr_resync
8075 * less than ours. When we find one that is the same or higher
8076 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8077 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8078 * This will mean we have to start checking from the beginning again.
8079 *
8080 */
8081
8082 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008083 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008084 mddev->curr_resync = 2;
8085
8086 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008087 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008089 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 if (mddev2 == mddev)
8091 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008092 if (!mddev->parallel_resync
8093 && mddev2->curr_resync
8094 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095 DEFINE_WAIT(wq);
8096 if (mddev < mddev2 && mddev->curr_resync == 2) {
8097 /* arbitrarily yield */
8098 mddev->curr_resync = 1;
8099 wake_up(&resync_wait);
8100 }
8101 if (mddev > mddev2 && mddev->curr_resync == 1)
8102 /* no need to wait here, we can wait the next
8103 * time 'round when curr_resync == 2
8104 */
8105 continue;
NeilBrown97441972008-09-19 11:49:54 +10008106 /* We need to wait 'interruptible' so as not to
8107 * contribute to the load average, and not to
8108 * be caught by 'softlockup'
8109 */
8110 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008111 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008112 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008113 if (mddev2_minor != mddev2->md_minor) {
8114 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008115 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8116 desc, mdname(mddev),
8117 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008120 if (signal_pending(current))
8121 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122 schedule();
8123 finish_wait(&resync_wait, &wq);
8124 goto try_again;
8125 }
8126 finish_wait(&resync_wait, &wq);
8127 }
8128 }
8129 } while (mddev->curr_resync < 2);
8130
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008131 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008132 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008134 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135 */
8136 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008137 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008138 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008139 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8140 j = mddev->resync_min;
8141 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008142 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008143
NeilBrownccfcc3c2006-03-27 01:18:09 -08008144 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10008145 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008146 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008148 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008149 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008150 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008151 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008152 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008153 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008154 !test_bit(Faulty, &rdev->flags) &&
8155 !test_bit(In_sync, &rdev->flags) &&
8156 rdev->recovery_offset < j)
8157 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008158 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008159
8160 /* If there is a bitmap, we need to make sure all
8161 * writes that started before we added a spare
8162 * complete before we start doing a recovery.
8163 * Otherwise the write might complete and (via
8164 * bitmap_endwrite) set a bit in the bitmap after the
8165 * recovery has checked that bit and skipped that
8166 * region.
8167 */
8168 if (mddev->bitmap) {
8169 mddev->pers->quiesce(mddev, 1);
8170 mddev->pers->quiesce(mddev, 0);
8171 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173
NeilBrown9d487392016-11-02 14:16:49 +11008174 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8175 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8176 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8177 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178
NeilBrowneea1bf32009-03-31 14:27:02 +11008179 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008180
NeilBrown57afd892005-06-21 17:17:13 -07008181 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 for (m = 0; m < SYNC_MARKS; m++) {
8183 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008184 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 }
8186 last_mark = 0;
8187 mddev->resync_mark = mark[last_mark];
8188 mddev->resync_mark_cnt = mark_cnt[last_mark];
8189
8190 /*
8191 * Tune reconstruction:
8192 */
8193 window = 32*(PAGE_SIZE/512);
NeilBrown9d487392016-11-02 14:16:49 +11008194 pr_debug("md: using %dk window, over a total of %lluk.\n",
8195 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196
8197 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008198 last_check = 0;
8199
8200 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008201 pr_debug("md: resuming %s of %s from checkpoint.\n",
8202 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008204 } else
8205 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008206 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008207 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8208 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008209 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210
majianpeng7c2c57c2012-07-03 12:12:26 +10008211 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008213 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214
NeilBrown57afd892005-06-21 17:17:13 -07008215 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008216
NeilBrown7a91ee12009-05-26 12:57:21 +10008217 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8218 ((mddev->curr_resync > mddev->curr_resync_completed &&
8219 (mddev->curr_resync - mddev->curr_resync_completed)
8220 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008221 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008222 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008223 >= mddev->resync_max - mddev->curr_resync_completed ||
8224 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008225 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008226 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008227 wait_event(mddev->recovery_wait,
8228 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008229 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008230 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8231 j > mddev->recovery_cp)
8232 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008233 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008234 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008235 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008236 }
NeilBrownacb180b2009-04-14 16:28:34 +10008237
NeilBrownc91abf52013-11-19 12:02:01 +11008238 while (j >= mddev->resync_max &&
8239 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008240 /* As this condition is controlled by user-space,
8241 * we can block indefinitely, so use '_interruptible'
8242 * to avoid triggering warnings.
8243 */
8244 flush_signals(current); /* just in case */
8245 wait_event_interruptible(mddev->recovery_wait,
8246 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008247 || test_bit(MD_RECOVERY_INTR,
8248 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008249 }
NeilBrownacb180b2009-04-14 16:28:34 +10008250
NeilBrownc91abf52013-11-19 12:02:01 +11008251 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8252 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008253
NeilBrown09314792015-02-19 16:04:40 +11008254 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008255 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008256 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008257 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008258 }
NeilBrown57afd892005-06-21 17:17:13 -07008259
8260 if (!skipped) { /* actual IO requested */
8261 io_sectors += sectors;
8262 atomic_add(sectors, &mddev->recovery_active);
8263 }
8264
NeilBrowne875ece2011-07-28 11:39:24 +10008265 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8266 break;
8267
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008269 if (j > max_sectors)
8270 /* when skipping, extra large numbers can be returned. */
8271 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008272 if (j > 2)
8273 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008274 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008275 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008276 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008277 * visible in /proc/mdstat
8278 */
8279 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008280
8281 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282 continue;
8283
NeilBrown57afd892005-06-21 17:17:13 -07008284 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285 repeat:
8286 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8287 /* step marks */
8288 int next = (last_mark+1) % SYNC_MARKS;
8289
8290 mddev->resync_mark = mark[next];
8291 mddev->resync_mark_cnt = mark_cnt[next];
8292 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008293 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 last_mark = next;
8295 }
8296
NeilBrownc91abf52013-11-19 12:02:01 +11008297 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8298 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299
8300 /*
8301 * this loop exits only if either when we are slower than
8302 * the 'hard' speed limit, or the system was IO-idle for
8303 * a jiffy.
8304 * the system might be non-idle CPU-wise, but we only care
8305 * about not overloading the IO subsystem. (things like an
8306 * e2fsck being done on the RAID array should execute fast)
8307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308 cond_resched();
8309
Xiao Niac7e50a2014-08-07 09:37:41 -04008310 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8311 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008312 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313
NeilBrown88202a02006-01-06 00:21:36 -08008314 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008315 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008316 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317 goto repeat;
8318 }
NeilBrownac8fa412015-02-19 16:55:00 +11008319 if (!is_mddev_idle(mddev, 0)) {
8320 /*
8321 * Give other IO more of a chance.
8322 * The faster the devices, the less we wait.
8323 */
8324 wait_event(mddev->recovery_wait,
8325 !atomic_read(&mddev->recovery_active));
8326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 }
8328 }
NeilBrown9d487392016-11-02 14:16:49 +11008329 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8330 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8331 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332 /*
8333 * this also signals 'finished resyncing' to md_stop
8334 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008335 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8337
NeilBrown5ed1df22015-07-24 13:27:08 +10008338 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8339 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008340 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008341 mddev->curr_resync_completed = mddev->curr_resync;
8342 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8343 }
NeilBrown09314792015-02-19 16:04:40 +11008344 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008345
NeilBrowndfc70642008-05-23 13:04:39 -07008346 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008347 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008348 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8349 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8350 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008351 pr_debug("md: checkpointing %s of %s.\n",
8352 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008353 if (test_bit(MD_RECOVERY_ERROR,
8354 &mddev->recovery))
8355 mddev->recovery_cp =
8356 mddev->curr_resync_completed;
8357 else
8358 mddev->recovery_cp =
8359 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008360 }
8361 } else
8362 mddev->recovery_cp = MaxSector;
8363 } else {
8364 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8365 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008366 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008367 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008368 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008369 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008370 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008371 !test_bit(Faulty, &rdev->flags) &&
8372 !test_bit(In_sync, &rdev->flags) &&
8373 rdev->recovery_offset < mddev->curr_resync)
8374 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008375 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008378 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008379 /* set CHANGE_PENDING here since maybe another update is needed,
8380 * so other nodes are informed. It should be harmless for normal
8381 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008382 set_mask_bits(&mddev->sb_flags, 0,
8383 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008384
NeilBrown23da4222014-12-15 12:57:01 +11008385 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008386 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8387 /* We completed so min/max setting can be forgotten if used. */
8388 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8389 mddev->resync_min = 0;
8390 mddev->resync_max = MaxSector;
8391 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8392 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008393 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008395 spin_unlock(&mddev->lock);
8396
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008399 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400}
NeilBrown29269552006-03-27 01:18:10 -08008401EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402
NeilBrown746d3202013-04-24 11:42:41 +10008403static int remove_and_add_spares(struct mddev *mddev,
8404 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008405{
NeilBrown3cb03002011-10-11 16:45:26 +11008406 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008407 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008408 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008409 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008410
NeilBrownd787be42016-06-02 16:19:53 +10008411 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008412 if ((this == NULL || rdev == this) &&
8413 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008414 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008415 test_bit(Faulty, &rdev->flags) &&
8416 atomic_read(&rdev->nr_pending)==0) {
8417 /* Faulty non-Blocked devices with nr_pending == 0
8418 * never get nr_pending incremented,
8419 * never get Faulty cleared, and never get Blocked set.
8420 * So we can synchronize_rcu now rather than once per device
8421 */
8422 remove_some = true;
8423 set_bit(RemoveSynchronized, &rdev->flags);
8424 }
8425 }
8426
8427 if (remove_some)
8428 synchronize_rcu();
8429 rdev_for_each(rdev, mddev) {
8430 if ((this == NULL || rdev == this) &&
8431 rdev->raid_disk >= 0 &&
8432 !test_bit(Blocked, &rdev->flags) &&
8433 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008434 (!test_bit(In_sync, &rdev->flags) &&
8435 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008436 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008437 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008438 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008439 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008440 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008441 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008442 }
8443 }
NeilBrownd787be42016-06-02 16:19:53 +10008444 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8445 clear_bit(RemoveSynchronized, &rdev->flags);
8446 }
8447
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008448 if (removed && mddev->kobj.sd)
8449 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008450
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008451 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008452 goto no_add;
8453
NeilBrowndafb20f2012-03-19 12:46:39 +11008454 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008455 if (this && this != rdev)
8456 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008457 if (test_bit(Candidate, &rdev->flags))
8458 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008459 if (rdev->raid_disk >= 0 &&
8460 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008461 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008462 !test_bit(Faulty, &rdev->flags))
8463 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008464 if (rdev->raid_disk >= 0)
8465 continue;
8466 if (test_bit(Faulty, &rdev->flags))
8467 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008468 if (!test_bit(Journal, &rdev->flags)) {
8469 if (mddev->ro &&
8470 ! (rdev->saved_raid_disk >= 0 &&
8471 !test_bit(Bitmap_sync, &rdev->flags)))
8472 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008473
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008474 rdev->recovery_offset = 0;
8475 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008476 if (mddev->pers->
8477 hot_add_disk(mddev, rdev) == 0) {
8478 if (sysfs_link_rdev(mddev, rdev))
8479 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008480 if (!test_bit(Journal, &rdev->flags))
8481 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008482 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008483 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008484 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008485 }
NeilBrown746d3202013-04-24 11:42:41 +10008486no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008487 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008488 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008489 return spares;
8490}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008491
NeilBrownac05f252014-09-30 08:10:42 +10008492static void md_start_sync(struct work_struct *ws)
8493{
8494 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008495
NeilBrownac05f252014-09-30 08:10:42 +10008496 mddev->sync_thread = md_register_thread(md_do_sync,
8497 mddev,
8498 "resync");
8499 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008500 pr_warn("%s: could not start resync thread...\n",
8501 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008502 /* leave the spares where they are, it shouldn't hurt */
8503 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8504 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8505 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8506 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8507 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008508 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008509 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8510 &mddev->recovery))
8511 if (mddev->sysfs_action)
8512 sysfs_notify_dirent_safe(mddev->sysfs_action);
8513 } else
8514 md_wakeup_thread(mddev->sync_thread);
8515 sysfs_notify_dirent_safe(mddev->sysfs_action);
8516 md_new_event(mddev);
8517}
8518
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519/*
8520 * This routine is regularly called by all per-raid-array threads to
8521 * deal with generic issues like resync and super-block update.
8522 * Raid personalities that don't have a thread (linear/raid0) do not
8523 * need this as they never do any recovery or update the superblock.
8524 *
8525 * It does not do any resync itself, but rather "forks" off other threads
8526 * to do that as needed.
8527 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8528 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008529 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 * and wakeups up this thread which will reap the thread and finish up.
8531 * This thread also removes any faulty devices (with nr_pending == 0).
8532 *
8533 * The overall approach is:
8534 * 1/ if the superblock needs updating, update it.
8535 * 2/ If a recovery thread is running, don't do anything else.
8536 * 3/ If recovery has finished, clean up, possibly marking spares active.
8537 * 4/ If there are any faulty devices, remove them.
8538 * 5/ If array is degraded, try to add spares devices
8539 * 6/ If array has spares or is not in-sync, start a resync thread.
8540 */
NeilBrownfd01b882011-10-11 16:47:53 +11008541void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008543 if (mddev->suspended)
8544 return;
8545
NeilBrown5f404022005-06-21 17:17:16 -07008546 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008547 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548
NeilBrownfca4d842005-06-21 17:17:11 -07008549 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008550 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008551 pr_debug("md: %s in immediate safe mode\n",
8552 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008553 mddev->safemode = 2;
8554 }
8555 flush_signals(current);
8556 }
8557
NeilBrownc89a8ee2008-08-05 15:54:13 +10008558 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8559 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008561 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008562 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008563 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008564 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008565 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008566 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567 ))
8568 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008569
NeilBrowndf5b89b2006-03-27 01:18:20 -08008570 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008571 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008572
NeilBrownc89a8ee2008-08-05 15:54:13 +10008573 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008574 struct md_rdev *rdev;
8575 if (!mddev->external && mddev->in_sync)
8576 /* 'Blocked' flag not needed as failed devices
8577 * will be recorded if array switched to read/write.
8578 * Leaving it set will prevent the device
8579 * from being removed.
8580 */
8581 rdev_for_each(rdev, mddev)
8582 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008583 /* On a read-only array we can:
8584 * - remove failed devices
8585 * - add already-in_sync devices if the array itself
8586 * is in-sync.
8587 * As we only add devices that are already in-sync,
8588 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008589 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008590 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008591 /* There is no thread, but we need to call
8592 * ->spare_active and clear saved_raid_disk
8593 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008594 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008595 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008596 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008597 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008598 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008599 goto unlock;
8600 }
8601
Guoqing Jiang659b2542015-12-21 10:50:59 +11008602 if (mddev_is_clustered(mddev)) {
8603 struct md_rdev *rdev;
8604 /* kick the device if another node issued a
8605 * remove disk.
8606 */
8607 rdev_for_each(rdev, mddev) {
8608 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8609 rdev->raid_disk < 0)
8610 md_kick_rdev_from_array(rdev);
8611 }
8612 }
8613
NeilBrown6497709b2017-03-15 14:05:14 +11008614 if (!mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008615 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008616 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008617 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008618 }
NeilBrownfca4d842005-06-21 17:17:11 -07008619
Shaohua Li29530792016-12-08 15:48:19 -08008620 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008621 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008622
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8624 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8625 /* resync/recovery still happening */
8626 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8627 goto unlock;
8628 }
8629 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008630 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008631 goto unlock;
8632 }
Neil Brown72a23c22008-06-28 08:31:41 +10008633 /* Set RUNNING before clearing NEEDED to avoid
8634 * any transients in the value of "sync_action".
8635 */
NeilBrown72f36d52012-10-11 14:25:57 +11008636 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008637 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008638 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008639 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008640 /* Clear some bits that don't mean anything, but
8641 * might be left set
8642 */
NeilBrown24dd4692005-11-08 21:39:26 -08008643 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8644 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645
NeilBrowned209582012-04-24 10:23:14 +10008646 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8647 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008648 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 /* no recovery is running.
8650 * remove any failed drives, then
8651 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008652 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653 * the personality to fail the re-add.
8654 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008656 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008657 if (mddev->pers->check_reshape == NULL ||
8658 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008659 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008660 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008661 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008662 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008663 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008664 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8665 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008666 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008667 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008668 } else if (mddev->recovery_cp < MaxSector) {
8669 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008670 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008671 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8672 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008673 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008674
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008676 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008677 /* We are adding a device or devices to an array
8678 * which has the bitmap stored on all devices.
8679 * So make sure all bitmap pages get written
8680 */
8681 bitmap_write_all(mddev->bitmap);
8682 }
NeilBrownac05f252014-09-30 08:10:42 +10008683 INIT_WORK(&mddev->del_work, md_start_sync);
8684 queue_work(md_misc_wq, &mddev->del_work);
8685 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008686 }
NeilBrownac05f252014-09-30 08:10:42 +10008687 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008688 if (!mddev->sync_thread) {
8689 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008690 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008691 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8692 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008693 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008694 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008695 }
NeilBrownac05f252014-09-30 08:10:42 +10008696 unlock:
8697 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698 mddev_unlock(mddev);
8699 }
8700}
NeilBrown6c144d32014-09-30 16:15:38 +10008701EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008703void md_reap_sync_thread(struct mddev *mddev)
8704{
8705 struct md_rdev *rdev;
8706
8707 /* resync has finished, collect result */
8708 md_unregister_thread(&mddev->sync_thread);
8709 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8710 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8711 /* success...*/
8712 /* activate any spares */
8713 if (mddev->pers->spare_active(mddev)) {
8714 sysfs_notify(&mddev->kobj, NULL,
8715 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08008716 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008717 }
8718 }
8719 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8720 mddev->pers->finish_reshape)
8721 mddev->pers->finish_reshape(mddev);
8722
8723 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008724 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008725 */
NeilBrownf4667222013-12-09 12:04:56 +11008726 if (!mddev->degraded)
8727 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008728 rdev->saved_raid_disk = -1;
8729
8730 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08008731 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008732 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8733 * clustered raid */
8734 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8735 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008736 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008737 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008738 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8739 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8740 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8741 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008742 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008743 /* flag recovery needed just to double check */
8744 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8745 sysfs_notify_dirent_safe(mddev->sysfs_action);
8746 md_new_event(mddev);
8747 if (mddev->event_work.func)
8748 queue_work(md_misc_wq, &mddev->event_work);
8749}
NeilBrown6c144d32014-09-30 16:15:38 +10008750EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008751
NeilBrownfd01b882011-10-11 16:47:53 +11008752void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008753{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008754 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008755 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008756 !test_bit(Blocked, &rdev->flags) &&
8757 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008758 msecs_to_jiffies(5000));
8759 rdev_dec_pending(rdev, mddev);
8760}
8761EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8762
NeilBrownc6563a82012-05-21 09:27:00 +10008763void md_finish_reshape(struct mddev *mddev)
8764{
8765 /* called be personality module when reshape completes. */
8766 struct md_rdev *rdev;
8767
8768 rdev_for_each(rdev, mddev) {
8769 if (rdev->data_offset > rdev->new_data_offset)
8770 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8771 else
8772 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8773 rdev->data_offset = rdev->new_data_offset;
8774 }
8775}
8776EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008777
Vishal Vermafc974ee2015-12-24 19:20:34 -07008778/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008779
Vishal Vermafc974ee2015-12-24 19:20:34 -07008780/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008781int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008782 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008783{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008784 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008785 int rv;
8786 if (is_new)
8787 s += rdev->new_data_offset;
8788 else
8789 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008790 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8791 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008792 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008793 if (test_bit(ExternalBbl, &rdev->flags))
8794 sysfs_notify(&rdev->kobj, NULL,
8795 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008796 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08008797 set_mask_bits(&mddev->sb_flags, 0,
8798 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008799 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008800 return 1;
8801 } else
8802 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008803}
8804EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8805
NeilBrownc6563a82012-05-21 09:27:00 +10008806int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8807 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008808{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008809 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10008810 if (is_new)
8811 s += rdev->new_data_offset;
8812 else
8813 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02008814 rv = badblocks_clear(&rdev->badblocks, s, sectors);
8815 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
8816 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
8817 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008818}
8819EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8820
Adrian Bunk75c96f82005-05-05 16:16:09 -07008821static int md_notify_reboot(struct notifier_block *this,
8822 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823{
8824 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008825 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008826 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827
NeilBrownc744a652012-03-19 12:46:37 +11008828 for_each_mddev(mddev, tmp) {
8829 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008830 if (mddev->pers)
8831 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008832 if (mddev->persistent)
8833 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008834 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008835 }
NeilBrownc744a652012-03-19 12:46:37 +11008836 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837 }
NeilBrownc744a652012-03-19 12:46:37 +11008838 /*
8839 * certain more exotic SCSI devices are known to be
8840 * volatile wrt too early system reboots. While the
8841 * right place to handle this issue is the given
8842 * driver, we do want to have a safe RAID driver ...
8843 */
8844 if (need_delay)
8845 mdelay(1000*1);
8846
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847 return NOTIFY_DONE;
8848}
8849
Adrian Bunk75c96f82005-05-05 16:16:09 -07008850static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851 .notifier_call = md_notify_reboot,
8852 .next = NULL,
8853 .priority = INT_MAX, /* before any real devices */
8854};
8855
8856static void md_geninit(void)
8857{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008858 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008860 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008861}
8862
Adrian Bunk75c96f82005-05-05 16:16:09 -07008863static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008864{
Tejun Heoe804ac72010-10-15 15:36:08 +02008865 int ret = -ENOMEM;
8866
Tejun Heoada609e2011-01-25 14:35:54 +01008867 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008868 if (!md_wq)
8869 goto err_wq;
8870
8871 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8872 if (!md_misc_wq)
8873 goto err_misc_wq;
8874
8875 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8876 goto err_md;
8877
8878 if ((ret = register_blkdev(0, "mdp")) < 0)
8879 goto err_mdp;
8880 mdp_major = ret;
8881
NeilBrownaf5628f2014-07-31 13:54:54 +10008882 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008883 md_probe, NULL, NULL);
8884 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885 md_probe, NULL, NULL);
8886
Linus Torvalds1da177e2005-04-16 15:20:36 -07008887 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008888 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889
8890 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008892
Tejun Heoe804ac72010-10-15 15:36:08 +02008893err_mdp:
8894 unregister_blkdev(MD_MAJOR, "md");
8895err_md:
8896 destroy_workqueue(md_misc_wq);
8897err_misc_wq:
8898 destroy_workqueue(md_wq);
8899err_wq:
8900 return ret;
8901}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008903static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008904{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008905 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8906 struct md_rdev *rdev2;
8907 int role, ret;
8908 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008909
Guoqing Jiang818da592017-03-01 16:42:40 +08008910 /*
8911 * If size is changed in another node then we need to
8912 * do resize as well.
8913 */
8914 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
8915 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
8916 if (ret)
8917 pr_info("md-cluster: resize failed\n");
8918 else
8919 bitmap_update_sb(mddev->bitmap);
8920 }
8921
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008922 /* Check for change of roles in the active devices */
8923 rdev_for_each(rdev2, mddev) {
8924 if (test_bit(Faulty, &rdev2->flags))
8925 continue;
8926
8927 /* Check if the roles changed */
8928 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008929
8930 if (test_bit(Candidate, &rdev2->flags)) {
8931 if (role == 0xfffe) {
8932 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8933 md_kick_rdev_from_array(rdev2);
8934 continue;
8935 }
8936 else
8937 clear_bit(Candidate, &rdev2->flags);
8938 }
8939
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008940 if (role != rdev2->raid_disk) {
8941 /* got activated */
8942 if (rdev2->raid_disk == -1 && role != 0xffff) {
8943 rdev2->saved_raid_disk = role;
8944 ret = remove_and_add_spares(mddev, rdev2);
8945 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11008946 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04008947 /* wakeup mddev->thread here, so array could
8948 * perform resync with the new activated disk */
8949 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8950 md_wakeup_thread(mddev->thread);
8951
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008952 }
8953 /* device faulty
8954 * We just want to do the minimum to mark the disk
8955 * as faulty. The recovery is performed by the
8956 * one who initiated the error.
8957 */
8958 if ((role == 0xfffe) || (role == 0xfffd)) {
8959 md_error(mddev, rdev2);
8960 clear_bit(Blocked, &rdev2->flags);
8961 }
8962 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008963 }
8964
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11008965 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8966 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008967
8968 /* Finally set the event to be up to date */
8969 mddev->events = le64_to_cpu(sb->events);
8970}
8971
8972static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8973{
8974 int err;
8975 struct page *swapout = rdev->sb_page;
8976 struct mdp_superblock_1 *sb;
8977
8978 /* Store the sb page of the rdev in the swapout temporary
8979 * variable in case we err in the future
8980 */
8981 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11008982 err = alloc_disk_sb(rdev);
8983 if (err == 0) {
8984 ClearPageUptodate(rdev->sb_page);
8985 rdev->sb_loaded = 0;
8986 err = super_types[mddev->major_version].
8987 load_super(rdev, NULL, mddev->minor_version);
8988 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008989 if (err < 0) {
8990 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8991 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11008992 if (rdev->sb_page)
8993 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008994 rdev->sb_page = swapout;
8995 rdev->sb_loaded = 1;
8996 return err;
8997 }
8998
8999 sb = page_address(rdev->sb_page);
9000 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9001 * is not set
9002 */
9003
9004 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9005 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9006
9007 /* The other node finished recovery, call spare_active to set
9008 * device In_sync and mddev->degraded
9009 */
9010 if (rdev->recovery_offset == MaxSector &&
9011 !test_bit(In_sync, &rdev->flags) &&
9012 mddev->pers->spare_active(mddev))
9013 sysfs_notify(&mddev->kobj, NULL, "degraded");
9014
9015 put_page(swapout);
9016 return 0;
9017}
9018
9019void md_reload_sb(struct mddev *mddev, int nr)
9020{
9021 struct md_rdev *rdev;
9022 int err;
9023
9024 /* Find the rdev */
9025 rdev_for_each_rcu(rdev, mddev) {
9026 if (rdev->desc_nr == nr)
9027 break;
9028 }
9029
9030 if (!rdev || rdev->desc_nr != nr) {
9031 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9032 return;
9033 }
9034
9035 err = read_rdev(mddev, rdev);
9036 if (err < 0)
9037 return;
9038
9039 check_sb_changes(mddev, rdev);
9040
9041 /* Read all rdev's to update recovery_offset */
9042 rdev_for_each_rcu(rdev, mddev)
9043 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009044}
9045EXPORT_SYMBOL(md_reload_sb);
9046
Linus Torvalds1da177e2005-04-16 15:20:36 -07009047#ifndef MODULE
9048
9049/*
9050 * Searches all registered partitions for autorun RAID arrays
9051 * at boot time.
9052 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009053
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009054static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009055static LIST_HEAD(all_detected_devices);
9056struct detected_devices_node {
9057 struct list_head list;
9058 dev_t dev;
9059};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060
9061void md_autodetect_dev(dev_t dev)
9062{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009063 struct detected_devices_node *node_detected_dev;
9064
9065 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9066 if (node_detected_dev) {
9067 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009068 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009069 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009070 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072}
9073
Linus Torvalds1da177e2005-04-16 15:20:36 -07009074static void autostart_arrays(int part)
9075{
NeilBrown3cb03002011-10-11 16:45:26 +11009076 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009077 struct detected_devices_node *node_detected_dev;
9078 dev_t dev;
9079 int i_scanned, i_passed;
9080
9081 i_scanned = 0;
9082 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009083
NeilBrown9d487392016-11-02 14:16:49 +11009084 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009086 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009087 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9088 i_scanned++;
9089 node_detected_dev = list_entry(all_detected_devices.next,
9090 struct detected_devices_node, list);
9091 list_del(&node_detected_dev->list);
9092 dev = node_detected_dev->dev;
9093 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009094 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009095 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009096 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009097 if (IS_ERR(rdev))
9098 continue;
9099
NeilBrown403df472014-09-30 15:52:29 +10009100 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009101 continue;
NeilBrown403df472014-09-30 15:52:29 +10009102
NeilBrownd0fae182008-03-04 14:29:31 -08009103 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009105 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009107 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009108
NeilBrown9d487392016-11-02 14:16:49 +11009109 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009110
9111 autorun_devices(part);
9112}
9113
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009114#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115
9116static __exit void md_exit(void)
9117{
NeilBrownfd01b882011-10-11 16:47:53 +11009118 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009119 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009120 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009121
NeilBrownaf5628f2014-07-31 13:54:54 +10009122 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009123 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009125 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009126 unregister_blkdev(mdp_major, "mdp");
9127 unregister_reboot_notifier(&md_notifier);
9128 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009129
9130 /* We cannot unload the modules while some process is
9131 * waiting for us in select() or poll() - wake them up
9132 */
9133 md_unloading = 1;
9134 while (waitqueue_active(&md_event_waiters)) {
9135 /* not safe to leave yet */
9136 wake_up(&md_event_waiters);
9137 msleep(delay);
9138 delay += delay;
9139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009140 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009141
NeilBrown29ac4aa2008-02-06 01:39:58 -08009142 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009143 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009144 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009145 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009146 /*
9147 * for_each_mddev() will call mddev_put() at the end of each
9148 * iteration. As the mddev is now fully clear, this will
9149 * schedule the mddev for destruction by a workqueue, and the
9150 * destroy_workqueue() below will wait for that to complete.
9151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009152 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009153 destroy_workqueue(md_misc_wq);
9154 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009155}
9156
Dan Williams685784a2007-07-09 11:56:42 -07009157subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009158module_exit(md_exit)
9159
NeilBrownf91de922005-11-08 21:39:36 -08009160static int get_ro(char *buffer, struct kernel_param *kp)
9161{
9162 return sprintf(buffer, "%d", start_readonly);
9163}
9164static int set_ro(const char *val, struct kernel_param *kp)
9165{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009166 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009167}
9168
NeilBrown80ca3a42006-07-10 04:44:18 -07009169module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9170module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009171module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009172
Linus Torvalds1da177e2005-04-16 15:20:36 -07009173MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009174MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009175MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009176MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);