blob: 2cf0e1c00b9a91d4db49fa1ce176c6d1386a23eb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10003 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050056#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110059static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
NeilBrown01f96c02011-09-21 15:30:20 +100062/* pers_list is a list of registered personalities protected
63 * by pers_lock.
64 * pers_lock does extra service to protect accesses to
65 * mddev->thread when the mutex cannot be held.
66 */
NeilBrown2604b702006-01-06 00:20:36 -080067static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_SPINLOCK(pers_lock);
69
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050070struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050071EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050072struct module *md_cluster_mod;
73EXPORT_SYMBOL(md_cluster_mod);
74
Bernd Schubert90b08712008-05-23 13:04:38 -070075static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020076static struct workqueue_struct *md_wq;
77static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070078
NeilBrown746d3202013-04-24 11:42:41 +100079static int remove_and_add_spares(struct mddev *mddev,
80 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110081static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Robert Becker1e509152009-12-14 12:49:58 +110084 * Default number of read corrections we'll attempt on an rdev
85 * before ejecting it from the array. We divide the read error
86 * count by 2 for every hour elapsed between read errors.
87 */
88#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
89/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
91 * is 1000 KB/sec, so the extra system load does not show up that much.
92 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070093 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * subsystem is idle. There is also an 'absolute maximum' reconstruction
95 * speed limit - in case reconstruction slows down your system despite
96 * idle IO detection.
97 *
98 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080099 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101
102static int sysctl_speed_limit_min = 1000;
103static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_min ?
107 mddev->sync_speed_min : sysctl_speed_limit_min;
108}
109
NeilBrownfd01b882011-10-11 16:47:53 +1100110static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800111{
112 return mddev->sync_speed_max ?
113 mddev->sync_speed_max : sysctl_speed_limit_max;
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct ctl_table_header *raid_table_header;
117
Joe Perches82592c32013-11-14 15:16:18 +1100118static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .procname = "speed_limit_min",
121 .data = &sysctl_speed_limit_min,
122 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700123 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800124 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 },
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .procname = "speed_limit_max",
128 .data = &sysctl_speed_limit_max,
129 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700130 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800131 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800133 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Joe Perches82592c32013-11-14 15:16:18 +1100136static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .procname = "raid",
139 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .child = raid_table,
142 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800143 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Joe Perches82592c32013-11-14 15:16:18 +1100146static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .procname = "dev",
149 .maxlen = 0,
150 .mode = 0555,
151 .child = raid_dir_table,
152 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800153 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700156static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
NeilBrownf91de922005-11-08 21:39:36 -0800158static int start_readonly;
159
NeilBrowna167f662010-10-26 18:31:13 +1100160/* bio_clone_mddev
161 * like bio_clone, but with a local bio set
162 */
163
NeilBrowna167f662010-10-26 18:31:13 +1100164struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100165 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100166{
167 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100168
169 if (!mddev || !mddev->bio_set)
170 return bio_alloc(gfp_mask, nr_iovecs);
171
Kent Overstreet395c72a2012-09-06 15:34:55 -0700172 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100173 if (!b)
174 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100175 return b;
176}
177EXPORT_SYMBOL_GPL(bio_alloc_mddev);
178
179struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100180 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100181{
NeilBrowna167f662010-10-26 18:31:13 +1100182 if (!mddev || !mddev->bio_set)
183 return bio_clone(bio, gfp_mask);
184
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700185 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100186}
187EXPORT_SYMBOL_GPL(bio_clone_mddev);
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
NeilBrownd7603b72006-01-06 00:20:30 -0800190 * We have a system wide 'event count' that is incremented
191 * on any 'interesting' event, and readers of /proc/mdstat
192 * can use 'poll' or 'select' to find out when the event
193 * count increases.
194 *
195 * Events are:
196 * start array, stop array, error, add device, remove device,
197 * start build, activate spare
198 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800199static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800200static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100201void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800202{
203 atomic_inc(&md_event_count);
204 wake_up(&md_event_waiters);
205}
NeilBrown29269552006-03-27 01:18:10 -0800206EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800207
208/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * Enables to iterate over all existing md arrays
210 * all_mddevs_lock protects this list.
211 */
212static LIST_HEAD(all_mddevs);
213static DEFINE_SPINLOCK(all_mddevs_lock);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
216 * iterates through all used mddevs in the system.
217 * We take care to grab the all_mddevs_lock whenever navigating
218 * the list, and to always hold a refcount when unlocked.
219 * Any code which breaks out of this loop while own
220 * a reference to the current mddev and must mddev_put it.
221 */
NeilBrownfd01b882011-10-11 16:47:53 +1100222#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000224 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100225 _tmp = all_mddevs.next; \
226 _mddev = NULL;}); \
227 ({ if (_tmp != &all_mddevs) \
228 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100230 if (_mddev) mddev_put(_mddev); \
231 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
232 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100234 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 )
236
NeilBrown409c57f2009-03-31 14:39:39 +1100237/* Rather than calling directly into the personality make_request function,
238 * IO requests come here first so that we can check if the device is
239 * being suspended pending a reconfiguration.
240 * We hold a refcount over the call to ->make_request. By the time that
241 * call has finished, the bio has been linked into some internal structure
242 * and so is visible to ->quiesce(), so we don't need the refcount any more.
243 */
Jens Axboedece1632015-11-05 10:41:16 -0700244static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
NeilBrown49077322010-03-25 16:20:56 +1100246 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100247 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500248 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800249 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100250
Kent Overstreet54efd502015-04-23 22:37:18 -0700251 blk_queue_split(q, &bio, q->bio_split);
252
NeilBrown274d8cb2016-01-04 16:16:58 +1100253 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100254 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700255 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100256 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100257 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200258 if (bio_sectors(bio) != 0)
259 bio->bi_error = -EROFS;
260 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700261 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100262 }
NeilBrown0ca69882011-01-14 09:14:33 +1100263 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100264 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200265 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100266 DEFINE_WAIT(__wait);
267 for (;;) {
268 prepare_to_wait(&mddev->sb_wait, &__wait,
269 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200270 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100271 break;
272 rcu_read_unlock();
273 schedule();
274 rcu_read_lock();
275 }
276 finish_wait(&mddev->sb_wait, &__wait);
277 }
278 atomic_inc(&mddev->active_io);
279 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100280
Chris Masone91ece52011-02-07 19:21:48 -0500281 /*
282 * save the sectors now since our bio can
283 * go away inside make_request
284 */
285 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200286 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100287
Gu Zheng74672d02015-04-03 08:44:47 +0800288 cpu = part_stat_lock();
289 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
290 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
291 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100292
NeilBrown409c57f2009-03-31 14:39:39 +1100293 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
294 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700295
296 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
NeilBrown9e35b992010-04-06 14:23:02 +1000299/* mddev_suspend makes sure no new requests are submitted
300 * to the device, and that any requests that have been submitted
301 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100302 * Once mddev_detach() is called and completes, the module will be
303 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000304 */
NeilBrownfd01b882011-10-11 16:47:53 +1100305void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100306{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100307 if (mddev->suspended++)
308 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100309 synchronize_rcu();
310 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
311 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500312
313 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100314}
NeilBrown390ee602010-06-01 19:37:27 +1000315EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100316
NeilBrownfd01b882011-10-11 16:47:53 +1100317void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100318{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100319 if (--mddev->suspended)
320 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100321 wake_up(&mddev->sb_wait);
322 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500323
Jonathan Brassow47525e52012-05-22 13:55:29 +1000324 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500325 md_wakeup_thread(mddev->thread);
326 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100327}
NeilBrown390ee602010-06-01 19:37:27 +1000328EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100329
NeilBrownfd01b882011-10-11 16:47:53 +1100330int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000331{
NeilBrown5c675f82014-12-15 12:56:56 +1100332 struct md_personality *pers = mddev->pers;
333 int ret = 0;
334
335 rcu_read_lock();
336 if (mddev->suspended)
337 ret = 1;
338 else if (pers && pers->congested)
339 ret = pers->congested(mddev, bits);
340 rcu_read_unlock();
341 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000342}
NeilBrown5c675f82014-12-15 12:56:56 +1100343EXPORT_SYMBOL_GPL(mddev_congested);
344static int md_congested(void *data, int bits)
345{
346 struct mddev *mddev = data;
347 return mddev_congested(mddev, bits);
348}
NeilBrown3fa841d2009-09-23 18:10:29 +1000349
NeilBrowna2826aa2009-12-14 12:49:49 +1100350/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200351 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100352 */
353
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200354static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100355{
NeilBrown3cb03002011-10-11 16:45:26 +1100356 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100357 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100358
359 rdev_dec_pending(rdev, mddev);
360
361 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200362 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200363 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100364 }
365 bio_put(bio);
366}
367
NeilBrowna7a07e62010-12-09 16:04:25 +1100368static void md_submit_flush_data(struct work_struct *ws);
369
NeilBrowna035fc32010-12-09 16:17:51 +1100370static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100371{
NeilBrownfd01b882011-10-11 16:47:53 +1100372 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100373 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100374
NeilBrowna7a07e62010-12-09 16:04:25 +1100375 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
376 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100377 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100378 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 if (rdev->raid_disk >= 0 &&
380 !test_bit(Faulty, &rdev->flags)) {
381 /* Take two references, one is dropped
382 * when request finishes, one after
383 * we reclaim rcu_read_lock
384 */
385 struct bio *bi;
386 atomic_inc(&rdev->nr_pending);
387 atomic_inc(&rdev->nr_pending);
388 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000389 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200390 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100391 bi->bi_private = rdev;
392 bi->bi_bdev = rdev->bdev;
393 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100395 rcu_read_lock();
396 rdev_dec_pending(rdev, mddev);
397 }
398 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100399 if (atomic_dec_and_test(&mddev->flush_pending))
400 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100401}
402
Tejun Heoe9c74692010-09-03 11:56:18 +0200403static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100404{
NeilBrownfd01b882011-10-11 16:47:53 +1100405 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200406 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100407
Kent Overstreet4f024f32013-10-11 15:44:27 -0700408 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200410 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200412 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200413 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100414 }
NeilBrown2b74e122010-12-09 15:59:01 +1100415
416 mddev->flush_bio = NULL;
417 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100418}
419
NeilBrownfd01b882011-10-11 16:47:53 +1100420void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100421{
NeilBrown85572d72014-12-15 12:56:56 +1100422 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100423 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100425 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200426 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100427 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100428
NeilBrowna035fc32010-12-09 16:17:51 +1100429 INIT_WORK(&mddev->flush_work, submit_flushes);
430 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100431}
Tejun Heoe9c74692010-09-03 11:56:18 +0200432EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100433
NeilBrown74018dc2012-07-31 09:08:15 +0200434void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000435{
NeilBrown9cbb1752012-07-31 09:08:14 +0200436 struct mddev *mddev = cb->data;
437 md_wakeup_thread(mddev->thread);
438 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000439}
NeilBrown9cbb1752012-07-31 09:08:14 +0200440EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000441
NeilBrownfd01b882011-10-11 16:47:53 +1100442static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 atomic_inc(&mddev->active);
445 return mddev;
446}
447
Dan Williams5fd3a172009-03-04 00:57:25 -0700448static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100449
NeilBrownfd01b882011-10-11 16:47:53 +1100450static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
NeilBrowna167f662010-10-26 18:31:13 +1100452 struct bio_set *bs = NULL;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
455 return;
NeilBrownd3374822009-01-09 08:31:10 +1100456 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100457 mddev->ctime == 0 && !mddev->hold_active) {
458 /* Array is not configured at all, and not held active,
459 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100460 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100461 bs = mddev->bio_set;
462 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100463 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200464 /* We did a probe so need to clean up. Call
465 * queue_work inside the spinlock so that
466 * flush_workqueue() after mddev_find will
467 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100468 */
469 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200470 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100471 } else
472 kfree(mddev);
473 }
474 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100475 if (bs)
476 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Sasha Levin25b2edf2015-07-24 18:19:58 -0400479static void md_safemode_timeout(unsigned long data);
480
NeilBrownfd01b882011-10-11 16:47:53 +1100481void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100482{
483 mutex_init(&mddev->open_mutex);
484 mutex_init(&mddev->reconfig_mutex);
485 mutex_init(&mddev->bitmap_info.mutex);
486 INIT_LIST_HEAD(&mddev->disks);
487 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400488 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
489 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100490 atomic_set(&mddev->active, 1);
491 atomic_set(&mddev->openers, 0);
492 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100493 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100494 atomic_set(&mddev->flush_pending, 0);
495 init_waitqueue_head(&mddev->sb_wait);
496 init_waitqueue_head(&mddev->recovery_wait);
497 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000498 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500499 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100500 mddev->resync_min = 0;
501 mddev->resync_max = MaxSector;
502 mddev->level = LEVEL_NONE;
503}
NeilBrown390ee602010-06-01 19:37:27 +1000504EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100505
NeilBrownf72ffdd2014-09-30 14:23:59 +1000506static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
NeilBrownfd01b882011-10-11 16:47:53 +1100508 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
NeilBrown8f5f02c2011-02-16 13:58:51 +1100510 if (unit && MAJOR(unit) != MD_MAJOR)
511 unit &= ~((1<<MdpMinorShift)-1);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 retry:
514 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
NeilBrownefeb53c2009-01-09 08:31:10 +1100516 if (unit) {
517 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
518 if (mddev->unit == unit) {
519 mddev_get(mddev);
520 spin_unlock(&all_mddevs_lock);
521 kfree(new);
522 return mddev;
523 }
524
525 if (new) {
526 list_add(&new->all_mddevs, &all_mddevs);
527 spin_unlock(&all_mddevs_lock);
528 new->hold_active = UNTIL_IOCTL;
529 return new;
530 }
531 } else if (new) {
532 /* find an unused unit number */
533 static int next_minor = 512;
534 int start = next_minor;
535 int is_free = 0;
536 int dev = 0;
537 while (!is_free) {
538 dev = MKDEV(MD_MAJOR, next_minor);
539 next_minor++;
540 if (next_minor > MINORMASK)
541 next_minor = 0;
542 if (next_minor == start) {
543 /* Oh dear, all in use. */
544 spin_unlock(&all_mddevs_lock);
545 kfree(new);
546 return NULL;
547 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000548
NeilBrownefeb53c2009-01-09 08:31:10 +1100549 is_free = 1;
550 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
551 if (mddev->unit == dev) {
552 is_free = 0;
553 break;
554 }
555 }
556 new->unit = dev;
557 new->md_minor = MINOR(dev);
558 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 list_add(&new->all_mddevs, &all_mddevs);
560 spin_unlock(&all_mddevs_lock);
561 return new;
562 }
563 spin_unlock(&all_mddevs_lock);
564
NeilBrown9ffae0c2006-01-06 00:20:32 -0800565 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!new)
567 return NULL;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 new->unit = unit;
570 if (MAJOR(unit) == MD_MAJOR)
571 new->md_minor = MINOR(unit);
572 else
573 new->md_minor = MINOR(unit) >> MdpMinorShift;
574
NeilBrownfafd7fb2010-04-01 15:55:30 +1100575 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto retry;
578}
579
NeilBrownb6eb1272010-04-15 10:13:47 +1000580static struct attribute_group md_redundancy_group;
581
NeilBrown5c47daf2014-12-15 12:57:01 +1100582void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
NeilBrowna64c8762010-04-14 17:15:37 +1000584 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000585 /* These cannot be removed under reconfig_mutex as
586 * an access to the files will try to take reconfig_mutex
587 * while holding the file unremovable, which leads to
588 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000589 * So hold set sysfs_active while the remove in happeing,
590 * and anything else which might set ->to_remove or my
591 * otherwise change the sysfs namespace will fail with
592 * -EBUSY if sysfs_active is still set.
593 * We set sysfs_active under reconfig_mutex and elsewhere
594 * test it under the same mutex to ensure its correct value
595 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000596 */
NeilBrowna64c8762010-04-14 17:15:37 +1000597 struct attribute_group *to_remove = mddev->to_remove;
598 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000599 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000600 mutex_unlock(&mddev->reconfig_mutex);
601
NeilBrown00bcb4a2010-06-01 19:37:23 +1000602 if (mddev->kobj.sd) {
603 if (to_remove != &md_redundancy_group)
604 sysfs_remove_group(&mddev->kobj, to_remove);
605 if (mddev->pers == NULL ||
606 mddev->pers->sync_request == NULL) {
607 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
608 if (mddev->sysfs_action)
609 sysfs_put(mddev->sysfs_action);
610 mddev->sysfs_action = NULL;
611 }
NeilBrowna64c8762010-04-14 17:15:37 +1000612 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000613 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000614 } else
615 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Chris Dunlop751e67c2011-10-19 16:48:26 +1100617 /* As we've dropped the mutex we need a spinlock to
618 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000619 */
620 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700621 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000622 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
NeilBrown5c47daf2014-12-15 12:57:01 +1100624EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500626struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100627{
628 struct md_rdev *rdev;
629
630 rdev_for_each_rcu(rdev, mddev)
631 if (rdev->desc_nr == nr)
632 return rdev;
633
634 return NULL;
635}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500636EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100637
638static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
NeilBrown3cb03002011-10-11 16:45:26 +1100640 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
NeilBrowndafb20f2012-03-19 12:46:39 +1100642 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (rdev->bdev->bd_dev == dev)
644 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return NULL;
647}
648
NeilBrown1ca69c42012-10-11 13:37:33 +1100649static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
650{
651 struct md_rdev *rdev;
652
653 rdev_for_each_rcu(rdev, mddev)
654 if (rdev->bdev->bd_dev == dev)
655 return rdev;
656
657 return NULL;
658}
659
NeilBrown84fc4b52011-10-11 16:49:58 +1100660static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800661{
NeilBrown84fc4b52011-10-11 16:49:58 +1100662 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800663 list_for_each_entry(pers, &pers_list, list) {
664 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800665 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800666 if (strcmp(pers->name, clevel)==0)
667 return pers;
668 }
NeilBrown2604b702006-01-06 00:20:36 -0800669 return NULL;
670}
671
Andre Nollb73df2d2008-07-11 22:02:23 +1000672/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100673static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100675 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000676 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
NeilBrownf72ffdd2014-09-30 14:23:59 +1000679static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 rdev->sb_page = alloc_page(GFP_KERNEL);
682 if (!rdev->sb_page) {
683 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000684 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 return 0;
688}
689
NeilBrown545c8792012-05-22 13:54:30 +1000690void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800693 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 rdev->sb_loaded = 0;
695 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000696 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100697 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
NeilBrown2699b672011-07-28 11:31:47 +1000699 if (rdev->bb_page) {
700 put_page(rdev->bb_page);
701 rdev->bb_page = NULL;
702 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000703 kfree(rdev->badblocks.page);
704 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
NeilBrown545c8792012-05-22 13:54:30 +1000706EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200708static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700709{
NeilBrown3cb03002011-10-11 16:45:26 +1100710 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100711 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700712
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200713 if (bio->bi_error) {
714 printk("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800715 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700716 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700717
NeilBrowna9701a32005-11-08 21:39:34 -0800718 if (atomic_dec_and_test(&mddev->pending_writes))
719 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700720 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700721}
722
NeilBrownfd01b882011-10-11 16:47:53 +1100723void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700724 sector_t sector, int size, struct page *page)
725{
726 /* write first size bytes of page to sector of rdev
727 * Increment mddev->pending_writes before returning
728 * and decrement it on completion, waking up sb_wait
729 * if zero is reached.
730 * If an error occurred, call md_error
731 */
NeilBrowna167f662010-10-26 18:31:13 +1100732 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700733
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100734 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700735 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700736 bio_add_page(bio, page, size, 0);
737 bio->bi_private = rdev;
738 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800739
NeilBrown7bfa19f2005-06-21 17:17:28 -0700740 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000741 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800742}
743
NeilBrownfd01b882011-10-11 16:47:53 +1100744void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800745{
Tejun Heoe9c74692010-09-03 11:56:18 +0200746 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000747 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700748}
749
NeilBrown3cb03002011-10-11 16:45:26 +1100750int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100751 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
NeilBrowna167f662010-10-26 18:31:13 +1100753 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int ret;
755
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100756 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
757 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100758 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700759 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000760 else if (rdev->mddev->reshape_position != MaxSector &&
761 (rdev->mddev->reshape_backwards ==
762 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700763 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100764 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700765 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700767 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200769 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 bio_put(bio);
771 return ret;
772}
NeilBrowna8745db2006-01-06 00:20:34 -0800773EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
NeilBrownf72ffdd2014-09-30 14:23:59 +1000775static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (rdev->sb_loaded)
780 return 0;
781
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100782 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 goto fail;
784 rdev->sb_loaded = 1;
785 return 0;
786
787fail:
788 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
789 bdevname(rdev->bdev,b));
790 return -EINVAL;
791}
792
793static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
794{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000795 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000796 sb1->set_uuid1 == sb2->set_uuid1 &&
797 sb1->set_uuid2 == sb2->set_uuid2 &&
798 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
802{
803 int ret;
804 mdp_super_t *tmp1, *tmp2;
805
806 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
807 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
808
809 if (!tmp1 || !tmp2) {
810 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100811 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto abort;
813 }
814
815 *tmp1 = *sb1;
816 *tmp2 = *sb2;
817
818 /*
819 * nr_disks is not constant
820 */
821 tmp1->nr_disks = 0;
822 tmp2->nr_disks = 0;
823
Andre Nollce0c8e02008-07-11 22:02:20 +1000824 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700826 kfree(tmp1);
827 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return ret;
829}
830
NeilBrown4d167f02007-05-09 02:35:37 -0700831static u32 md_csum_fold(u32 csum)
832{
833 csum = (csum & 0xffff) + (csum >> 16);
834 return (csum & 0xffff) + (csum >> 16);
835}
836
NeilBrownf72ffdd2014-09-30 14:23:59 +1000837static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
NeilBrown4d167f02007-05-09 02:35:37 -0700839 u64 newcsum = 0;
840 u32 *sb32 = (u32*)sb;
841 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 unsigned int disk_csum, csum;
843
844 disk_csum = sb->sb_csum;
845 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700846
847 for (i = 0; i < MD_SB_BYTES/4 ; i++)
848 newcsum += sb32[i];
849 csum = (newcsum & 0xffffffff) + (newcsum>>32);
850
NeilBrown4d167f02007-05-09 02:35:37 -0700851#ifdef CONFIG_ALPHA
852 /* This used to use csum_partial, which was wrong for several
853 * reasons including that different results are returned on
854 * different architectures. It isn't critical that we get exactly
855 * the same return value as before (we always csum_fold before
856 * testing, and that removes any differences). However as we
857 * know that csum_partial always returned a 16bit value on
858 * alphas, do a fold to maximise conformity to previous behaviour.
859 */
860 sb->sb_csum = md_csum_fold(disk_csum);
861#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700863#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return csum;
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/*
868 * Handle superblock details.
869 * We want to be able to handle multiple superblock formats
870 * so we have a common interface to them all, and an array of
871 * different handlers.
872 * We rely on user-space to write the initial superblock, and support
873 * reading and updating of superblocks.
874 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100875 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * loads and validates a superblock on dev.
877 * if refdev != NULL, compare superblocks on both devices
878 * Return:
879 * 0 - dev has a superblock that is compatible with refdev
880 * 1 - dev has a superblock that is compatible and newer than refdev
881 * so dev should be used as the refdev in future
882 * -EINVAL superblock incompatible or invalid
883 * -othererror e.g. -EIO
884 *
NeilBrownfd01b882011-10-11 16:47:53 +1100885 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * Verify that dev is acceptable into mddev.
887 * The first time, mddev->raid_disks will be 0, and data from
888 * dev should be merged in. Subsequent calls check that dev
889 * is new enough. Return 0 or -EINVAL
890 *
NeilBrownfd01b882011-10-11 16:47:53 +1100891 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * Update the superblock for rdev with data in mddev
893 * This does not write to disc.
894 *
895 */
896
897struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000898 char *name;
899 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000900 int (*load_super)(struct md_rdev *rdev,
901 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000902 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000903 int (*validate_super)(struct mddev *mddev,
904 struct md_rdev *rdev);
905 void (*sync_super)(struct mddev *mddev,
906 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100907 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000908 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000909 int (*allow_new_offset)(struct md_rdev *rdev,
910 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911};
912
913/*
Andre Noll0894cc32009-06-18 08:49:23 +1000914 * Check that the given mddev has no bitmap.
915 *
916 * This function is called from the run method of all personalities that do not
917 * support bitmaps. It prints an error message and returns non-zero if mddev
918 * has a bitmap. Otherwise, it returns 0.
919 *
920 */
NeilBrownfd01b882011-10-11 16:47:53 +1100921int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000922{
NeilBrownc3d97142009-12-14 12:49:52 +1100923 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000924 return 0;
925 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
926 mdname(mddev), mddev->pers->name);
927 return 1;
928}
929EXPORT_SYMBOL(md_check_no_bitmap);
930
931/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000932 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
NeilBrown3cb03002011-10-11 16:45:26 +1100934static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
937 mdp_super_t *sb;
938 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /*
Andre Noll0f420352008-07-11 22:02:23 +1000941 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 * it's at the end of the disk.
943 *
944 * It also happens to be a multiple of 4Kb.
945 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100946 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
NeilBrown0002b272005-09-09 16:23:53 -0700948 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (ret) return ret;
950
951 ret = -EINVAL;
952
953 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000954 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (sb->md_magic != MD_SB_MAGIC) {
957 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
958 b);
959 goto abort;
960 }
961
962 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800963 sb->minor_version < 90 ||
964 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
966 sb->major_version, sb->minor_version,
967 b);
968 goto abort;
969 }
970
971 if (sb->raid_disks <= 0)
972 goto abort;
973
NeilBrown4d167f02007-05-09 02:35:37 -0700974 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
976 b);
977 goto abort;
978 }
979
980 rdev->preferred_minor = sb->md_minor;
981 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000982 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700983 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000984 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (sb->level == LEVEL_MULTIPATH)
987 rdev->desc_nr = -1;
988 else
989 rdev->desc_nr = sb->this_disk.number;
990
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700991 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700993 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +1000995 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!uuid_equal(refsb, sb)) {
997 printk(KERN_WARNING "md: %s has different UUID to %s\n",
998 b, bdevname(refdev->bdev,b2));
999 goto abort;
1000 }
1001 if (!sb_equal(refsb, sb)) {
1002 printk(KERN_WARNING "md: %s has same UUID"
1003 " but different superblock to %s\n",
1004 b, bdevname(refdev->bdev, b2));
1005 goto abort;
1006 }
1007 ev1 = md_event(sb);
1008 ev2 = md_event(refsb);
1009 if (ev1 > ev2)
1010 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001011 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ret = 0;
1013 }
NeilBrown8190e752009-06-18 08:48:58 +10001014 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001015 /* Limit to 4TB as metadata cannot record more than that.
1016 * (not needed for Linear and RAID0 as metadata doesn't
1017 * record this size)
1018 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001019 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1020 sb->level >= 1)
1021 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
NeilBrown27a7b262011-09-10 17:21:28 +10001023 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001024 /* "this cannot possibly happen" ... */
1025 ret = -EINVAL;
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 abort:
1028 return ret;
1029}
1030
1031/*
1032 * validate_super for 0.90.0
1033 */
NeilBrownfd01b882011-10-11 16:47:53 +11001034static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001037 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001038 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
NeilBrown41158c72005-06-21 17:17:25 -07001040 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001041 clear_bit(Faulty, &rdev->flags);
1042 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001043 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001044 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (mddev->raid_disks == 0) {
1047 mddev->major_version = 0;
1048 mddev->minor_version = sb->minor_version;
1049 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001050 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001051 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 mddev->ctime = sb->ctime;
1053 mddev->utime = sb->utime;
1054 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001055 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 mddev->layout = sb->layout;
1057 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001058 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001059 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001060 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001061 mddev->bitmap_info.space = 0;
1062 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001063 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001064 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001065 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
NeilBrownf6705572006-03-27 01:18:11 -08001067 if (mddev->minor_version >= 91) {
1068 mddev->reshape_position = sb->reshape_position;
1069 mddev->delta_disks = sb->delta_disks;
1070 mddev->new_level = sb->new_level;
1071 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001072 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001073 if (mddev->delta_disks < 0)
1074 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001075 } else {
1076 mddev->reshape_position = MaxSector;
1077 mddev->delta_disks = 0;
1078 mddev->new_level = mddev->level;
1079 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001080 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001081 }
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (sb->state & (1<<MD_SB_CLEAN))
1084 mddev->recovery_cp = MaxSector;
1085 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001086 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 sb->events_lo == sb->cp_events_lo) {
1088 mddev->recovery_cp = sb->recovery_cp;
1089 } else
1090 mddev->recovery_cp = 0;
1091 }
1092
1093 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1094 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1095 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1096 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1097
1098 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001099
1100 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001101 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001102 mddev->bitmap_info.offset =
1103 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001104 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001105 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001106 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001107
NeilBrown41158c72005-06-21 17:17:25 -07001108 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001109 /* Insist on good event counter while assembling, except
1110 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001112 if (sb->disks[rdev->desc_nr].state & (
1113 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001114 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001115 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001116 } else if (mddev->bitmap) {
1117 /* if adding to array with a bitmap, then we can accept an
1118 * older device ... but not too old.
1119 */
NeilBrown41158c72005-06-21 17:17:25 -07001120 if (ev1 < mddev->bitmap->events_cleared)
1121 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001122 if (ev1 < mddev->events)
1123 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001124 } else {
1125 if (ev1 < mddev->events)
1126 /* just a hot-add of a new device, leave raid_disk at -1 */
1127 return 0;
1128 }
NeilBrown41158c72005-06-21 17:17:25 -07001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 desc = sb->disks + rdev->desc_nr;
1132
1133 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001134 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001135 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1136 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001137 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001139 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001140 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1141 /* active but not in sync implies recovery up to
1142 * reshape position. We don't know exactly where
1143 * that is, so set to zero for now */
1144 if (mddev->minor_version >= 91) {
1145 rdev->recovery_offset = 0;
1146 rdev->raid_disk = desc->raid_disk;
1147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001149 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1150 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001151 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001152 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return 0;
1154}
1155
1156/*
1157 * sync_super for 0.90.0
1158 */
NeilBrownfd01b882011-10-11 16:47:53 +11001159static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001162 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* make rdev->sb match mddev data..
1166 *
1167 * 1/ zero out disks
1168 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1169 * 3/ any empty disks < next_spare become removed
1170 *
1171 * disks[0] gets initialised to REMOVED because
1172 * we cannot be sure from other fields if it has
1173 * been initialised or not.
1174 */
1175 int i;
1176 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1177
NeilBrown61181562005-09-09 16:24:02 -07001178 rdev->sb_size = MD_SB_BYTES;
1179
Namhyung Kim65a06f062011-07-27 11:00:36 +10001180 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 memset(sb, 0, sizeof(*sb));
1183
1184 sb->md_magic = MD_SB_MAGIC;
1185 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sb->patch_version = mddev->patch_version;
1187 sb->gvalid_words = 0; /* ignored */
1188 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1189 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1190 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1191 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1192
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001193 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001195 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 sb->raid_disks = mddev->raid_disks;
1197 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001198 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001199 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 sb->state = 0;
1201 sb->events_hi = (mddev->events>>32);
1202 sb->events_lo = (u32)mddev->events;
1203
NeilBrownf6705572006-03-27 01:18:11 -08001204 if (mddev->reshape_position == MaxSector)
1205 sb->minor_version = 90;
1206 else {
1207 sb->minor_version = 91;
1208 sb->reshape_position = mddev->reshape_position;
1209 sb->new_level = mddev->new_level;
1210 sb->delta_disks = mddev->delta_disks;
1211 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001212 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001213 }
1214 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (mddev->in_sync)
1216 {
1217 sb->recovery_cp = mddev->recovery_cp;
1218 sb->cp_events_hi = (mddev->events>>32);
1219 sb->cp_events_lo = (u32)mddev->events;
1220 if (mddev->recovery_cp == MaxSector)
1221 sb->state = (1<< MD_SB_CLEAN);
1222 } else
1223 sb->recovery_cp = 0;
1224
1225 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001226 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
NeilBrownc3d97142009-12-14 12:49:52 +11001228 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001229 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001232 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001234 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001235 int is_active = test_bit(In_sync, &rdev2->flags);
1236
1237 if (rdev2->raid_disk >= 0 &&
1238 sb->minor_version >= 91)
1239 /* we have nowhere to store the recovery_offset,
1240 * but if it is not below the reshape_position,
1241 * we can piggy-back on that.
1242 */
1243 is_active = 1;
1244 if (rdev2->raid_disk < 0 ||
1245 test_bit(Faulty, &rdev2->flags))
1246 is_active = 0;
1247 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001248 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001250 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001251 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 d = &sb->disks[rdev2->desc_nr];
1253 nr_disks++;
1254 d->number = rdev2->desc_nr;
1255 d->major = MAJOR(rdev2->bdev->bd_dev);
1256 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001257 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 d->raid_disk = rdev2->raid_disk;
1259 else
1260 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001261 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001263 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001265 if (test_bit(In_sync, &rdev2->flags))
1266 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 active++;
1268 working++;
1269 } else {
1270 d->state = 0;
1271 spare++;
1272 working++;
1273 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001274 if (test_bit(WriteMostly, &rdev2->flags))
1275 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* now set the "removed" and "faulty" bits on any missing devices */
1278 for (i=0 ; i < mddev->raid_disks ; i++) {
1279 mdp_disk_t *d = &sb->disks[i];
1280 if (d->state == 0 && d->number == 0) {
1281 d->number = i;
1282 d->raid_disk = i;
1283 d->state = (1<<MD_DISK_REMOVED);
1284 d->state |= (1<<MD_DISK_FAULTY);
1285 failed++;
1286 }
1287 }
1288 sb->nr_disks = nr_disks;
1289 sb->active_disks = active;
1290 sb->working_disks = working;
1291 sb->failed_disks = failed;
1292 sb->spare_disks = spare;
1293
1294 sb->this_disk = sb->disks[rdev->desc_nr];
1295 sb->sb_csum = calc_sb_csum(sb);
1296}
1297
1298/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001299 * rdev_size_change for 0.90.0
1300 */
1301static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001302super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001303{
Andre Noll58c0fed2009-03-31 14:33:13 +11001304 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001305 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001306 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001307 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001308 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001309 if (!num_sectors || num_sectors > rdev->sb_start)
1310 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001311 /* Limit to 4TB as metadata cannot record more than that.
1312 * 4TB == 2^32 KB, or 2*2^32 sectors.
1313 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001314 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1315 rdev->mddev->level >= 1)
1316 num_sectors = (sector_t)(2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001317 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001318 rdev->sb_page);
1319 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001320 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001321}
1322
NeilBrownc6563a82012-05-21 09:27:00 +10001323static int
1324super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1325{
1326 /* non-zero offset changes not possible with v0.90 */
1327 return new_offset == 0;
1328}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001329
1330/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * version 1 superblock
1332 */
1333
NeilBrownf72ffdd2014-09-30 14:23:59 +10001334static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001336 __le32 disk_csum;
1337 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 unsigned long long newcsum;
1339 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001340 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 disk_csum = sb->sb_csum;
1343 sb->sb_csum = 0;
1344 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001345 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 newcsum += le32_to_cpu(*isuper++);
1347
1348 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001349 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1352 sb->sb_csum = disk_csum;
1353 return cpu_to_le32(csum);
1354}
1355
NeilBrown2699b672011-07-28 11:31:47 +10001356static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1357 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001358static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct mdp_superblock_1 *sb;
1361 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001362 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001363 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001365 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 /*
Andre Noll0f420352008-07-11 22:02:23 +10001368 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * It is always aligned to a 4K boundary and
1370 * depeding on minor_version, it can be:
1371 * 0: At least 8K, but less than 12K, from end of device
1372 * 1: At start of device
1373 * 2: 4K from start of device.
1374 */
1375 switch(minor_version) {
1376 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001377 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001378 sb_start -= 8*2;
1379 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 break;
1381 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001382 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 break;
1384 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001385 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 break;
1387 default:
1388 return -EINVAL;
1389 }
Andre Noll0f420352008-07-11 22:02:23 +10001390 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
NeilBrown0002b272005-09-09 16:23:53 -07001392 /* superblock is rarely larger than 1K, but it can be larger,
1393 * and it is safe to read 4k, so we do that
1394 */
1395 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (ret) return ret;
1397
Namhyung Kim65a06f062011-07-27 11:00:36 +10001398 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1401 sb->major_version != cpu_to_le32(1) ||
1402 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001403 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001404 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return -EINVAL;
1406
1407 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1408 printk("md: invalid superblock checksum on %s\n",
1409 bdevname(rdev->bdev,b));
1410 return -EINVAL;
1411 }
1412 if (le64_to_cpu(sb->data_size) < 10) {
1413 printk("md: data_size too small on %s\n",
1414 bdevname(rdev->bdev,b));
1415 return -EINVAL;
1416 }
NeilBrownc6563a82012-05-21 09:27:00 +10001417 if (sb->pad0 ||
1418 sb->pad3[0] ||
1419 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1420 /* Some padding is non-zero, might be a new feature */
1421 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 rdev->preferred_minor = 0xffff;
1424 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001425 rdev->new_data_offset = rdev->data_offset;
1426 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1427 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1428 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001429 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
NeilBrown0002b272005-09-09 16:23:53 -07001431 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001432 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001433 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001434 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1435
1436 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001437 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001438 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001439 if (minor_version
1440 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1441 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001442
NeilBrown31b65a02006-07-10 04:44:14 -07001443 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1444 rdev->desc_nr = -1;
1445 else
1446 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1447
NeilBrown2699b672011-07-28 11:31:47 +10001448 if (!rdev->bb_page) {
1449 rdev->bb_page = alloc_page(GFP_KERNEL);
1450 if (!rdev->bb_page)
1451 return -ENOMEM;
1452 }
1453 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1454 rdev->badblocks.count == 0) {
1455 /* need to load the bad block list.
1456 * Currently we limit it to one page.
1457 */
1458 s32 offset;
1459 sector_t bb_sector;
1460 u64 *bbp;
1461 int i;
1462 int sectors = le16_to_cpu(sb->bblog_size);
1463 if (sectors > (PAGE_SIZE / 512))
1464 return -EINVAL;
1465 offset = le32_to_cpu(sb->bblog_offset);
1466 if (offset == 0)
1467 return -EINVAL;
1468 bb_sector = (long long)offset;
1469 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1470 rdev->bb_page, READ, true))
1471 return -EIO;
1472 bbp = (u64 *)page_address(rdev->bb_page);
1473 rdev->badblocks.shift = sb->bblog_shift;
1474 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1475 u64 bb = le64_to_cpu(*bbp);
1476 int count = bb & (0x3ff);
1477 u64 sector = bb >> 10;
1478 sector <<= sb->bblog_shift;
1479 count <<= sb->bblog_shift;
1480 if (bb + 1 == 0)
1481 break;
1482 if (md_set_badblocks(&rdev->badblocks,
1483 sector, count, 1) == 0)
1484 return -EINVAL;
1485 }
NeilBrown486adf72013-04-24 11:42:44 +10001486 } else if (sb->bblog_offset != 0)
1487 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001488
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001489 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001490 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001491 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001493 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1496 sb->level != refsb->level ||
1497 sb->layout != refsb->layout ||
1498 sb->chunksize != refsb->chunksize) {
1499 printk(KERN_WARNING "md: %s has strangely different"
1500 " superblock to %s\n",
1501 bdevname(rdev->bdev,b),
1502 bdevname(refdev->bdev,b2));
1503 return -EINVAL;
1504 }
1505 ev1 = le64_to_cpu(sb->events);
1506 ev2 = le64_to_cpu(refsb->events);
1507
1508 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001509 ret = 1;
1510 else
1511 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
NeilBrownc6563a82012-05-21 09:27:00 +10001513 if (minor_version) {
1514 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1515 sectors -= rdev->data_offset;
1516 } else
1517 sectors = rdev->sb_start;
1518 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001520 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001521 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
NeilBrownfd01b882011-10-11 16:47:53 +11001524static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001526 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001527 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
NeilBrown41158c72005-06-21 17:17:25 -07001529 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001530 clear_bit(Faulty, &rdev->flags);
1531 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001532 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001533 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (mddev->raid_disks == 0) {
1536 mddev->major_version = 1;
1537 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001538 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001539 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001540 mddev->ctime = le64_to_cpu(sb->ctime);
1541 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001543 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 mddev->layout = le32_to_cpu(sb->layout);
1545 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001546 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001547 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001548 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001549 mddev->bitmap_info.space = 0;
1550 /* Default location for bitmap is 1K after superblock
1551 * using 3K - total of 4K
1552 */
NeilBrownc3d97142009-12-14 12:49:52 +11001553 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001554 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001555 mddev->reshape_backwards = 0;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1558 memcpy(mddev->uuid, sb->set_uuid, 16);
1559
1560 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001561
NeilBrown71c08052005-09-09 16:23:51 -07001562 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001563 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001564 mddev->bitmap_info.offset =
1565 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001566 /* Metadata doesn't record how much space is available.
1567 * For 1.0, we assume we can use up to the superblock
1568 * if before, else to 4K beyond superblock.
1569 * For others, assume no change is possible.
1570 */
1571 if (mddev->minor_version > 0)
1572 mddev->bitmap_info.space = 0;
1573 else if (mddev->bitmap_info.offset > 0)
1574 mddev->bitmap_info.space =
1575 8 - mddev->bitmap_info.offset;
1576 else
1577 mddev->bitmap_info.space =
1578 -mddev->bitmap_info.offset;
1579 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001580
NeilBrownf6705572006-03-27 01:18:11 -08001581 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1582 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1583 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1584 mddev->new_level = le32_to_cpu(sb->new_level);
1585 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001586 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001587 if (mddev->delta_disks < 0 ||
1588 (mddev->delta_disks == 0 &&
1589 (le32_to_cpu(sb->feature_map)
1590 & MD_FEATURE_RESHAPE_BACKWARDS)))
1591 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001592 } else {
1593 mddev->reshape_position = MaxSector;
1594 mddev->delta_disks = 0;
1595 mddev->new_level = mddev->level;
1596 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001597 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001598 }
1599
Shaohua Lia62ab492016-01-06 14:37:13 -08001600 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) {
1601 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1602 if (mddev->recovery_cp == MaxSector)
1603 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
1604 }
NeilBrown41158c72005-06-21 17:17:25 -07001605 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001606 /* Insist of good event counter while assembling, except for
1607 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001609 if (rdev->desc_nr >= 0 &&
1610 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001611 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1612 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001613 if (ev1 < mddev->events)
1614 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001615 } else if (mddev->bitmap) {
1616 /* If adding to array with a bitmap, then we can accept an
1617 * older device, but not too old.
1618 */
NeilBrown41158c72005-06-21 17:17:25 -07001619 if (ev1 < mddev->bitmap->events_cleared)
1620 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001621 if (ev1 < mddev->events)
1622 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001623 } else {
1624 if (ev1 < mddev->events)
1625 /* just a hot-add of a new device, leave raid_disk at -1 */
1626 return 0;
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (mddev->level != LEVEL_MULTIPATH) {
1629 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001630 if (rdev->desc_nr < 0 ||
1631 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001632 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001633 rdev->desc_nr = -1;
1634 } else
1635 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001637 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001639 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001640 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
Song Liubac624f2015-08-13 14:31:55 -07001642 case MD_DISK_ROLE_JOURNAL: /* journal device */
1643 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1644 /* journal device without journal feature */
1645 printk(KERN_WARNING
1646 "md: journal device provided without journal feature, ignoring the device\n");
1647 return -EINVAL;
1648 }
1649 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001650 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001651 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 default:
NeilBrownf4667222013-12-09 12:04:56 +11001654 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001655 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001656 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001657 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001658 if (!(le32_to_cpu(sb->feature_map) &
1659 MD_FEATURE_RECOVERY_BITMAP))
1660 rdev->saved_raid_disk = -1;
1661 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001662 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 rdev->raid_disk = role;
1664 break;
1665 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001666 if (sb->devflags & WriteMostly1)
1667 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001668 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1669 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001670 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001671 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return 0;
1674}
1675
NeilBrownfd01b882011-10-11 16:47:53 +11001676static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001679 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 int max_dev, i;
1681 /* make rdev->sb match mddev and rdev data. */
1682
Namhyung Kim65a06f062011-07-27 11:00:36 +10001683 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 sb->feature_map = 0;
1686 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001687 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 memset(sb->pad3, 0, sizeof(sb->pad3));
1689
1690 sb->utime = cpu_to_le64((__u64)mddev->utime);
1691 sb->events = cpu_to_le64(mddev->events);
1692 if (mddev->in_sync)
1693 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001694 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1695 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 else
1697 sb->resync_offset = cpu_to_le64(0);
1698
NeilBrown1c05b4b2006-10-21 10:24:08 -07001699 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001700
NeilBrownf0ca3402006-02-02 14:28:04 -08001701 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001702 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001703 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001704 sb->level = cpu_to_le32(mddev->level);
1705 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001706
NeilBrownaeb9b2112011-08-25 14:43:08 +10001707 if (test_bit(WriteMostly, &rdev->flags))
1708 sb->devflags |= WriteMostly1;
1709 else
1710 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001711 sb->data_offset = cpu_to_le64(rdev->data_offset);
1712 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001713
NeilBrownc3d97142009-12-14 12:49:52 +11001714 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1715 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001716 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001717 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001718
Shaohua Lif2076e72015-10-08 21:54:12 -07001719 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001720 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001721 sb->feature_map |=
1722 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1723 sb->recovery_offset =
1724 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001725 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1726 sb->feature_map |=
1727 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001728 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001729 /* Note: recovery_offset and journal_tail share space */
1730 if (test_bit(Journal, &rdev->flags))
1731 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001732 if (test_bit(Replacement, &rdev->flags))
1733 sb->feature_map |=
1734 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001735
NeilBrownf6705572006-03-27 01:18:11 -08001736 if (mddev->reshape_position != MaxSector) {
1737 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1738 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1739 sb->new_layout = cpu_to_le32(mddev->new_layout);
1740 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1741 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001742 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001743 if (mddev->delta_disks == 0 &&
1744 mddev->reshape_backwards)
1745 sb->feature_map
1746 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001747 if (rdev->new_data_offset != rdev->data_offset) {
1748 sb->feature_map
1749 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1750 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1751 - rdev->data_offset));
1752 }
NeilBrownf6705572006-03-27 01:18:11 -08001753 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001754
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001755 if (mddev_is_clustered(mddev))
1756 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1757
NeilBrown2699b672011-07-28 11:31:47 +10001758 if (rdev->badblocks.count == 0)
1759 /* Nothing to do for bad blocks*/ ;
1760 else if (sb->bblog_offset == 0)
1761 /* Cannot record bad blocks on this device */
1762 md_error(mddev, rdev);
1763 else {
1764 struct badblocks *bb = &rdev->badblocks;
1765 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1766 u64 *p = bb->page;
1767 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1768 if (bb->changed) {
1769 unsigned seq;
1770
1771retry:
1772 seq = read_seqbegin(&bb->lock);
1773
1774 memset(bbp, 0xff, PAGE_SIZE);
1775
1776 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001777 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001778 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1779 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001780 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001781 }
NeilBrownd0962932012-03-19 12:46:41 +11001782 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001783 if (read_seqretry(&bb->lock, seq))
1784 goto retry;
1785
1786 bb->sector = (rdev->sb_start +
1787 (int)le32_to_cpu(sb->bblog_offset));
1788 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001789 }
1790 }
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001793 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (rdev2->desc_nr+1 > max_dev)
1795 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001796
NeilBrown70471da2009-08-03 10:59:57 +10001797 if (max_dev > le32_to_cpu(sb->max_dev)) {
1798 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001799 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001800 rdev->sb_size = max_dev * 2 + 256;
1801 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1802 if (rdev->sb_size & bmask)
1803 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001804 } else
1805 max_dev = le32_to_cpu(sb->max_dev);
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001808 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001809
Song Liua97b7892015-10-08 21:54:09 -07001810 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1811 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
NeilBrowndafb20f2012-03-19 12:46:39 +11001813 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001815 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001816 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001817 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001819 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001820 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001821 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001822 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 else
Song Liuc4d4c912015-08-13 14:31:54 -07001824 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 sb->sb_csum = calc_sb_1_csum(sb);
1828}
1829
Chris Webb0cd17fe2008-06-28 08:31:46 +10001830static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001831super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001832{
1833 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001834 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001835 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001836 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001837 if (rdev->data_offset != rdev->new_data_offset)
1838 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001839 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001840 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001841 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001842 max_sectors -= rdev->data_offset;
1843 if (!num_sectors || num_sectors > max_sectors)
1844 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001845 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001846 /* minor version 0 with bitmap we can't move */
1847 return 0;
1848 } else {
1849 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001850 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001851 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001852 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001853 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001854 if (!num_sectors || num_sectors > max_sectors)
1855 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001856 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001857 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001858 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001859 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001860 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001861 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001862 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001863 rdev->sb_page);
1864 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001865 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001866
1867}
1868
1869static int
1870super_1_allow_new_offset(struct md_rdev *rdev,
1871 unsigned long long new_offset)
1872{
1873 /* All necessary checks on new >= old have been done */
1874 struct bitmap *bitmap;
1875 if (new_offset >= rdev->data_offset)
1876 return 1;
1877
1878 /* with 1.0 metadata, there is no metadata to tread on
1879 * so we can always move back */
1880 if (rdev->mddev->minor_version == 0)
1881 return 1;
1882
1883 /* otherwise we must be sure not to step on
1884 * any metadata, so stay:
1885 * 36K beyond start of superblock
1886 * beyond end of badblocks
1887 * beyond write-intent bitmap
1888 */
1889 if (rdev->sb_start + (32+4)*2 > new_offset)
1890 return 0;
1891 bitmap = rdev->mddev->bitmap;
1892 if (bitmap && !rdev->mddev->bitmap_info.file &&
1893 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001894 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001895 return 0;
1896 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1897 return 0;
1898
1899 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001900}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Adrian Bunk75c96f82005-05-05 16:16:09 -07001902static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 [0] = {
1904 .name = "0.90.0",
1905 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001906 .load_super = super_90_load,
1907 .validate_super = super_90_validate,
1908 .sync_super = super_90_sync,
1909 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001910 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 },
1912 [1] = {
1913 .name = "md-1",
1914 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001915 .load_super = super_1_load,
1916 .validate_super = super_1_validate,
1917 .sync_super = super_1_sync,
1918 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001919 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 },
1921};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
NeilBrownfd01b882011-10-11 16:47:53 +11001923static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001924{
1925 if (mddev->sync_super) {
1926 mddev->sync_super(mddev, rdev);
1927 return;
1928 }
1929
1930 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1931
1932 super_types[mddev->major_version].sync_super(mddev, rdev);
1933}
1934
NeilBrownfd01b882011-10-11 16:47:53 +11001935static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
NeilBrown3cb03002011-10-11 16:45:26 +11001937 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
NeilBrown4b809912008-07-21 17:05:25 +10001939 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001940 rdev_for_each_rcu(rdev, mddev1) {
1941 if (test_bit(Faulty, &rdev->flags) ||
1942 test_bit(Journal, &rdev->flags) ||
1943 rdev->raid_disk == -1)
1944 continue;
1945 rdev_for_each_rcu(rdev2, mddev2) {
1946 if (test_bit(Faulty, &rdev2->flags) ||
1947 test_bit(Journal, &rdev2->flags) ||
1948 rdev2->raid_disk == -1)
1949 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001950 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001951 rdev2->bdev->bd_contains) {
1952 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001953 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001954 }
Song Liu0b020e82015-09-03 23:00:35 -07001955 }
1956 }
NeilBrown4b809912008-07-21 17:05:25 +10001957 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return 0;
1959}
1960
1961static LIST_HEAD(pending_raid_disks);
1962
Andre Nollac5e7112009-08-03 10:59:47 +10001963/*
1964 * Try to register data integrity profile for an mddev
1965 *
1966 * This is called when an array is started and after a disk has been kicked
1967 * from the array. It only succeeds if all working and active component devices
1968 * are integrity capable with matching profiles.
1969 */
NeilBrownfd01b882011-10-11 16:47:53 +11001970int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001971{
NeilBrown3cb03002011-10-11 16:45:26 +11001972 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001973
Andre Nollac5e7112009-08-03 10:59:47 +10001974 if (list_empty(&mddev->disks))
1975 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001976 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1977 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001978 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001979 /* skip spares and non-functional disks */
1980 if (test_bit(Faulty, &rdev->flags))
1981 continue;
1982 if (rdev->raid_disk < 0)
1983 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001984 if (!reference) {
1985 /* Use the first rdev as the reference */
1986 reference = rdev;
1987 continue;
1988 }
1989 /* does this rdev's profile match the reference profile? */
1990 if (blk_integrity_compare(reference->bdev->bd_disk,
1991 rdev->bdev->bd_disk) < 0)
1992 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001993 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001994 if (!reference || !bdev_get_integrity(reference->bdev))
1995 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001996 /*
1997 * All component devices are integrity capable and have matching
1998 * profiles, register the common profile for the md device.
1999 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002000 blk_integrity_register(mddev->gendisk,
2001 bdev_get_integrity(reference->bdev));
2002
Martin K. Petersena91a2782011-03-17 11:11:05 +01002003 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2004 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2005 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2006 mdname(mddev));
2007 return -EINVAL;
2008 }
Andre Nollac5e7112009-08-03 10:59:47 +10002009 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002010}
Andre Nollac5e7112009-08-03 10:59:47 +10002011EXPORT_SYMBOL(md_integrity_register);
2012
Dan Williams1501efa2016-01-13 16:00:07 -08002013/*
2014 * Attempt to add an rdev, but only if it is consistent with the current
2015 * integrity profile
2016 */
2017int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002018{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002019 struct blk_integrity *bi_rdev;
2020 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002021 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002022
2023 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002024 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002025
2026 bi_rdev = bdev_get_integrity(rdev->bdev);
2027 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002028
2029 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002030 return 0;
2031
2032 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2033 printk(KERN_NOTICE "%s: incompatible integrity profile for %s\n",
2034 mdname(mddev), bdevname(rdev->bdev, name));
2035 return -ENXIO;
2036 }
2037
2038 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002039}
2040EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002041
NeilBrownf72ffdd2014-09-30 14:23:59 +10002042static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002044 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002045 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002046 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Dan Williams11e2ede2008-04-30 00:52:32 -07002048 /* prevent duplicates */
2049 if (find_rdev(mddev, rdev->bdev->bd_dev))
2050 return -EEXIST;
2051
Andre Nolldd8ac332009-03-31 14:33:13 +11002052 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002053 if (!test_bit(Journal, &rdev->flags) &&
2054 rdev->sectors &&
2055 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002056 if (mddev->pers) {
2057 /* Cannot change size, so fail
2058 * If mddev->level <= 0, then we don't care
2059 * about aligning sizes (e.g. linear)
2060 */
2061 if (mddev->level > 0)
2062 return -ENOSPC;
2063 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002064 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /* Verify rdev->desc_nr is unique.
2068 * If it is -1, assign a free number, else
2069 * check number is not in use
2070 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002071 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (rdev->desc_nr < 0) {
2073 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002074 if (mddev->pers)
2075 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002076 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 choice++;
2078 rdev->desc_nr = choice;
2079 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002080 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002081 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002085 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002086 if (!test_bit(Journal, &rdev->flags) &&
2087 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002088 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2089 mdname(mddev), mddev->max_disks);
2090 return -EBUSY;
2091 }
NeilBrown19133a42005-11-08 21:39:35 -08002092 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002093 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002096 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002097
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002098 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002099 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002100
Tejun Heo0762b8b2008-08-25 19:56:12 +09002101 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002102 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2103 /* failure here is OK */;
2104 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002105
NeilBrown4b809912008-07-21 17:05:25 +10002106 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002107 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002108
2109 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002110 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002113
2114 fail:
2115 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2116 b, mdname(mddev));
2117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
NeilBrown177a99b2008-02-06 01:39:56 -08002120static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002121{
NeilBrown3cb03002011-10-11 16:45:26 +11002122 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002123 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002124 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002125}
2126
NeilBrownf72ffdd2014-09-30 14:23:59 +10002127static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002130
Tejun Heo49731ba2011-01-14 18:43:57 +01002131 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002132 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2134 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002135 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002136 sysfs_put(rdev->sysfs_state);
2137 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002138 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002139 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002140 * writing to 'remove' to "dev/state". We also need
2141 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002142 */
NeilBrown4b809912008-07-21 17:05:25 +10002143 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002144 INIT_WORK(&rdev->del_work, md_delayed_delete);
2145 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002146 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149/*
2150 * prevent the device from being mounted, repartitioned or
2151 * otherwise reused by a RAID array (or any other kernel
2152 * subsystem), by bd_claiming the device.
2153 */
NeilBrown3cb03002011-10-11 16:45:26 +11002154static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 int err = 0;
2157 struct block_device *bdev;
2158 char b[BDEVNAME_SIZE];
2159
Tejun Heod4d77622010-11-13 11:55:18 +01002160 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002161 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (IS_ERR(bdev)) {
2163 printk(KERN_ERR "md: could not open %s.\n",
2164 __bdevname(dev, b));
2165 return PTR_ERR(bdev);
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 rdev->bdev = bdev;
2168 return err;
2169}
2170
NeilBrown3cb03002011-10-11 16:45:26 +11002171static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 struct block_device *bdev = rdev->bdev;
2174 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002175 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
2178void md_autodetect_dev(dev_t dev);
2179
NeilBrownf72ffdd2014-09-30 14:23:59 +10002180static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 printk(KERN_INFO "md: export_rdev(%s)\n",
2185 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002186 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002188 if (test_bit(AutoDetected, &rdev->flags))
2189 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#endif
2191 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002192 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002195void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 unbind_rdev_from_array(rdev);
2198 export_rdev(rdev);
2199}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002200EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
NeilBrownfd01b882011-10-11 16:47:53 +11002202static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
NeilBrown0638bb02014-09-25 17:43:47 +10002204 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
NeilBrown0638bb02014-09-25 17:43:47 +10002206 while (!list_empty(&mddev->disks)) {
2207 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2208 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002209 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 mddev->raid_disks = 0;
2212 mddev->major_version = 0;
2213}
2214
NeilBrownf72ffdd2014-09-30 14:23:59 +10002215static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
NeilBrown42543762006-06-26 00:27:57 -07002217 /* Update each superblock (in-memory image), but
2218 * if we are allowed to, skip spares which already
2219 * have the right event counter, or have one earlier
2220 * (which would mean they aren't being marked as dirty
2221 * with the rest of the array)
2222 */
NeilBrown3cb03002011-10-11 16:45:26 +11002223 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002224 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002225 if (rdev->sb_events == mddev->events ||
2226 (nospares &&
2227 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002228 rdev->sb_events+1 == mddev->events)) {
2229 /* Don't update this superblock */
2230 rdev->sb_loaded = 2;
2231 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002232 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002233 rdev->sb_loaded = 1;
2234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236}
2237
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002238static bool does_sb_need_changing(struct mddev *mddev)
2239{
2240 struct md_rdev *rdev;
2241 struct mdp_superblock_1 *sb;
2242 int role;
2243
2244 /* Find a good rdev */
2245 rdev_for_each(rdev, mddev)
2246 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2247 break;
2248
2249 /* No good device found. */
2250 if (!rdev)
2251 return false;
2252
2253 sb = page_address(rdev->sb_page);
2254 /* Check if a device has become faulty or a spare become active */
2255 rdev_for_each(rdev, mddev) {
2256 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2257 /* Device activated? */
2258 if (role == 0xffff && rdev->raid_disk >=0 &&
2259 !test_bit(Faulty, &rdev->flags))
2260 return true;
2261 /* Device turned faulty? */
2262 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2263 return true;
2264 }
2265
2266 /* Check if any mddev parameters have changed */
2267 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2268 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002269 (mddev->layout != le64_to_cpu(sb->layout)) ||
2270 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2271 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2272 return true;
2273
2274 return false;
2275}
2276
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002277void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
NeilBrown3cb03002011-10-11 16:45:26 +11002279 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002280 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002281 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002282 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002283 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
NeilBrownd87f0642013-04-24 11:42:40 +10002285 if (mddev->ro) {
2286 if (force_change)
2287 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2288 return;
2289 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002290
2291 if (mddev_is_clustered(mddev)) {
2292 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2293 force_change = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002294 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002295 /* Has someone else has updated the sb */
2296 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002297 if (ret == 0)
2298 md_cluster_ops->metadata_update_cancel(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002299 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2300 return;
2301 }
2302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002304 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002305 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002306 if (rdev->raid_disk >= 0 &&
2307 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002308 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002309 !test_bit(In_sync, &rdev->flags) &&
2310 mddev->curr_resync_completed > rdev->recovery_offset)
2311 rdev->recovery_offset = mddev->curr_resync_completed;
2312
NeilBrownf72ffdd2014-09-30 14:23:59 +10002313 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002314 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002315 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002316 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002317 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002318 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002319 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002320 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002321 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002322 md_ack_all_badblocks(&rdev->badblocks);
2323 md_error(mddev, rdev);
2324 }
2325 clear_bit(Blocked, &rdev->flags);
2326 clear_bit(BlockedBadBlocks, &rdev->flags);
2327 wake_up(&rdev->blocked_wait);
2328 }
2329 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002330 wake_up(&mddev->sb_wait);
2331 return;
2332 }
2333
NeilBrown85572d72014-12-15 12:56:56 +11002334 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002335
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002336 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002337
NeilBrown850b2b422006-10-03 01:15:46 -07002338 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2339 force_change = 1;
2340 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2341 /* just a clean<-> dirty transition, possibly leave spares alone,
2342 * though if events isn't the right even/odd, we will have to do
2343 * spares after all
2344 */
2345 nospares = 1;
2346 if (force_change)
2347 nospares = 0;
2348 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002349 /* If the array is degraded, then skipping spares is both
2350 * dangerous and fairly pointless.
2351 * Dangerous because a device that was removed from the array
2352 * might have a event_count that still looks up-to-date,
2353 * so it can be re-added without a resync.
2354 * Pointless because if there are any spares to skip,
2355 * then a recovery will happen and soon that array won't
2356 * be degraded any more and the spare can go back to sleep then.
2357 */
NeilBrown850b2b422006-10-03 01:15:46 -07002358 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002359
NeilBrown06d91a52005-06-21 17:17:12 -07002360 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002361
2362 /* If this is just a dirty<->clean transition, and the array is clean
2363 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002364 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002365 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002366 && mddev->can_decrease_events
2367 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002368 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002369 mddev->can_decrease_events = 0;
2370 } else {
NeilBrown42543762006-06-26 00:27:57 -07002371 /* otherwise we have to go forward and ... */
2372 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002373 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
NeilBrown403df472014-09-30 15:52:29 +10002376 /*
2377 * This 64-bit counter should never wrap.
2378 * Either we are in around ~1 trillion A.C., assuming
2379 * 1 reboot per second, or we have a bug...
2380 */
2381 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002382
NeilBrowndafb20f2012-03-19 12:46:39 +11002383 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002384 if (rdev->badblocks.changed)
2385 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002386 if (test_bit(Faulty, &rdev->flags))
2387 set_bit(FaultRecorded, &rdev->flags);
2388 }
NeilBrown2699b672011-07-28 11:31:47 +10002389
NeilBrowne6910632008-02-06 01:39:51 -08002390 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002391 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
NeilBrown36a4e1f2011-10-07 14:23:17 +11002393 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2394 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
NeilBrown4ad13662007-07-17 04:06:13 -07002396 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002397 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002399
NeilBrown42543762006-06-26 00:27:57 -07002400 if (rdev->sb_loaded != 1)
2401 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
NeilBrownf4667222013-12-09 12:04:56 +11002403 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002404 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002405 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002406 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002407 pr_debug("md: (write) %s's sb offset: %llu\n",
2408 bdevname(rdev->bdev, b),
2409 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002410 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002411 if (rdev->badblocks.size) {
2412 md_super_write(mddev, rdev,
2413 rdev->badblocks.sector,
2414 rdev->badblocks.size << 9,
2415 rdev->bb_page);
2416 rdev->badblocks.size = 0;
2417 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002418
NeilBrownf4667222013-12-09 12:04:56 +11002419 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002420 pr_debug("md: %s (skipping faulty)\n",
2421 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002422
NeilBrown7bfa19f2005-06-21 17:17:28 -07002423 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 /* only need to write one superblock... */
2425 break;
2426 }
NeilBrowna9701a32005-11-08 21:39:34 -08002427 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002428 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002429
NeilBrown85572d72014-12-15 12:56:56 +11002430 spin_lock(&mddev->lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002431 if (mddev->in_sync != sync_req ||
2432 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002433 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002434 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002435 goto repeat;
2436 }
NeilBrown850b2b422006-10-03 01:15:46 -07002437 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002438 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002439 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002440 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2441 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002442
NeilBrowndafb20f2012-03-19 12:46:39 +11002443 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002444 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2445 clear_bit(Blocked, &rdev->flags);
2446
2447 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002448 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002449 clear_bit(BlockedBadBlocks, &rdev->flags);
2450 wake_up(&rdev->blocked_wait);
2451 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002452
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002453 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002454 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002456EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002458static int add_bound_rdev(struct md_rdev *rdev)
2459{
2460 struct mddev *mddev = rdev->mddev;
2461 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002462 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002463
Shaohua Li87d4d912016-01-06 14:37:14 -08002464 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002465 /* If there is hot_add_disk but no hot_remove_disk
2466 * then added disks for geometry changes,
2467 * and should be added immediately.
2468 */
2469 super_types[mddev->major_version].
2470 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002471 if (add_journal)
2472 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002473 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002474 if (add_journal)
2475 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002476 if (err) {
2477 unbind_rdev_from_array(rdev);
2478 export_rdev(rdev);
2479 return err;
2480 }
2481 }
2482 sysfs_notify_dirent_safe(rdev->sysfs_state);
2483
2484 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2485 if (mddev->degraded)
2486 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2487 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2488 md_new_event(mddev);
2489 md_wakeup_thread(mddev->thread);
2490 return 0;
2491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Andre Noll7f6ce762008-03-23 18:34:54 +01002493/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002494 * We want to accept with case. For this we use cmd_match.
2495 */
2496static int cmd_match(const char *cmd, const char *str)
2497{
2498 /* See if cmd, written into a sysfs file, matches
2499 * str. They must either be the same, or cmd can
2500 * have a trailing newline
2501 */
2502 while (*cmd && *str && *cmd == *str) {
2503 cmd++;
2504 str++;
2505 }
2506 if (*cmd == '\n')
2507 cmd++;
2508 if (*str || *cmd)
2509 return 0;
2510 return 1;
2511}
2512
NeilBrown86e6ffd2005-11-08 21:39:24 -08002513struct rdev_sysfs_entry {
2514 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002515 ssize_t (*show)(struct md_rdev *, char *);
2516 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002517};
2518
2519static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002520state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002521{
2522 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002523 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002524 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002525
NeilBrown758bfc82014-12-15 12:56:59 +11002526 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002527 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002528 len+= sprintf(page+len, "%sfaulty",sep);
2529 sep = ",";
2530 }
NeilBrown758bfc82014-12-15 12:56:59 +11002531 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002532 len += sprintf(page+len, "%sin_sync",sep);
2533 sep = ",";
2534 }
Shaohua Liac6096e2015-10-04 09:20:11 -07002535 if (test_bit(Journal, &flags)) {
2536 len += sprintf(page+len, "%sjournal",sep);
2537 sep = ",";
2538 }
NeilBrown758bfc82014-12-15 12:56:59 +11002539 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002540 len += sprintf(page+len, "%swrite_mostly",sep);
2541 sep = ",";
2542 }
NeilBrown758bfc82014-12-15 12:56:59 +11002543 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002544 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002545 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002546 len += sprintf(page+len, "%sblocked", sep);
2547 sep = ",";
2548 }
NeilBrown758bfc82014-12-15 12:56:59 +11002549 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002550 !test_bit(Journal, &flags) &&
NeilBrown758bfc82014-12-15 12:56:59 +11002551 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002552 len += sprintf(page+len, "%sspare", sep);
2553 sep = ",";
2554 }
NeilBrown758bfc82014-12-15 12:56:59 +11002555 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002556 len += sprintf(page+len, "%swrite_error", sep);
2557 sep = ",";
2558 }
NeilBrown758bfc82014-12-15 12:56:59 +11002559 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002560 len += sprintf(page+len, "%swant_replacement", sep);
2561 sep = ",";
2562 }
NeilBrown758bfc82014-12-15 12:56:59 +11002563 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002564 len += sprintf(page+len, "%sreplacement", sep);
2565 sep = ",";
2566 }
2567
NeilBrown86e6ffd2005-11-08 21:39:24 -08002568 return len+sprintf(page+len, "\n");
2569}
2570
NeilBrown45dc2de2006-06-26 00:27:58 -07002571static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002572state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002573{
2574 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002575 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002576 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002577 * writemostly - sets write_mostly
2578 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002579 * blocked - sets the Blocked flags
2580 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002581 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002582 * -insync - clear Insync for a device with a slot assigned,
2583 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002584 * write_error - sets WriteErrorSeen
2585 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002586 */
2587 int err = -EINVAL;
2588 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2589 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002590 if (test_bit(Faulty, &rdev->flags))
2591 err = 0;
2592 else
2593 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002594 } else if (cmd_match(buf, "remove")) {
2595 if (rdev->raid_disk >= 0)
2596 err = -EBUSY;
2597 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002598 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002599 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002600 if (mddev_is_clustered(mddev))
2601 err = md_cluster_ops->remove_disk(mddev, rdev);
2602
2603 if (err == 0) {
2604 md_kick_rdev_from_array(rdev);
2605 if (mddev->pers)
2606 md_update_sb(mddev, 1);
2607 md_new_event(mddev);
2608 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002609 }
NeilBrownf6556752006-06-26 00:28:01 -07002610 } else if (cmd_match(buf, "writemostly")) {
2611 set_bit(WriteMostly, &rdev->flags);
2612 err = 0;
2613 } else if (cmd_match(buf, "-writemostly")) {
2614 clear_bit(WriteMostly, &rdev->flags);
2615 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002616 } else if (cmd_match(buf, "blocked")) {
2617 set_bit(Blocked, &rdev->flags);
2618 err = 0;
2619 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002620 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002621 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002622 /* metadata handler doesn't understand badblocks,
2623 * so we need to fail the device
2624 */
2625 md_error(rdev->mddev, rdev);
2626 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002627 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002628 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002629 wake_up(&rdev->blocked_wait);
2630 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2631 md_wakeup_thread(rdev->mddev->thread);
2632
2633 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002634 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2635 set_bit(In_sync, &rdev->flags);
2636 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002637 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2638 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002639 if (rdev->mddev->pers == NULL) {
2640 clear_bit(In_sync, &rdev->flags);
2641 rdev->saved_raid_disk = rdev->raid_disk;
2642 rdev->raid_disk = -1;
2643 err = 0;
2644 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002645 } else if (cmd_match(buf, "write_error")) {
2646 set_bit(WriteErrorSeen, &rdev->flags);
2647 err = 0;
2648 } else if (cmd_match(buf, "-write_error")) {
2649 clear_bit(WriteErrorSeen, &rdev->flags);
2650 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002651 } else if (cmd_match(buf, "want_replacement")) {
2652 /* Any non-spare device that is not a replacement can
2653 * become want_replacement at any time, but we then need to
2654 * check if recovery is needed.
2655 */
2656 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002657 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002658 !test_bit(Replacement, &rdev->flags))
2659 set_bit(WantReplacement, &rdev->flags);
2660 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2661 md_wakeup_thread(rdev->mddev->thread);
2662 err = 0;
2663 } else if (cmd_match(buf, "-want_replacement")) {
2664 /* Clearing 'want_replacement' is always allowed.
2665 * Once replacements starts it is too late though.
2666 */
2667 err = 0;
2668 clear_bit(WantReplacement, &rdev->flags);
2669 } else if (cmd_match(buf, "replacement")) {
2670 /* Can only set a device as a replacement when array has not
2671 * yet been started. Once running, replacement is automatic
2672 * from spares, or by assigning 'slot'.
2673 */
2674 if (rdev->mddev->pers)
2675 err = -EBUSY;
2676 else {
2677 set_bit(Replacement, &rdev->flags);
2678 err = 0;
2679 }
2680 } else if (cmd_match(buf, "-replacement")) {
2681 /* Similarly, can only clear Replacement before start */
2682 if (rdev->mddev->pers)
2683 err = -EBUSY;
2684 else {
2685 clear_bit(Replacement, &rdev->flags);
2686 err = 0;
2687 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002688 } else if (cmd_match(buf, "re-add")) {
2689 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002690 /* clear_bit is performed _after_ all the devices
2691 * have their local Faulty bit cleared. If any writes
2692 * happen in the meantime in the local node, they
2693 * will land in the local bitmap, which will be synced
2694 * by this node eventually
2695 */
2696 if (!mddev_is_clustered(rdev->mddev) ||
2697 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2698 clear_bit(Faulty, &rdev->flags);
2699 err = add_bound_rdev(rdev);
2700 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002701 } else
2702 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002703 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002704 if (!err)
2705 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002706 return err ? err : len;
2707}
NeilBrown80ca3a42006-07-10 04:44:18 -07002708static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002709__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002710
2711static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002712errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002713{
2714 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2715}
2716
2717static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002718errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002719{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002720 unsigned int n;
2721 int rv;
2722
2723 rv = kstrtouint(buf, 10, &n);
2724 if (rv < 0)
2725 return rv;
2726 atomic_set(&rdev->corrected_errors, n);
2727 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002728}
2729static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002730__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002731
NeilBrown014236d2006-01-06 00:20:55 -08002732static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002733slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002734{
Shaohua Lif2076e72015-10-08 21:54:12 -07002735 if (test_bit(Journal, &rdev->flags))
2736 return sprintf(page, "journal\n");
2737 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002738 return sprintf(page, "none\n");
2739 else
2740 return sprintf(page, "%d\n", rdev->raid_disk);
2741}
2742
2743static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002744slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002745{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002746 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002747 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002748
Shaohua Lif2076e72015-10-08 21:54:12 -07002749 if (test_bit(Journal, &rdev->flags))
2750 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002751 if (strncmp(buf, "none", 4)==0)
2752 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002753 else {
2754 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2755 if (err < 0)
2756 return err;
2757 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002758 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002759 /* Setting 'slot' on an active array requires also
2760 * updating the 'rd%d' link, and communicating
2761 * with the personality with ->hot_*_disk.
2762 * For now we only support removing
2763 * failed/spare devices. This normally happens automatically,
2764 * but not when the metadata is externally managed.
2765 */
NeilBrownc303da62008-02-06 01:39:51 -08002766 if (rdev->raid_disk == -1)
2767 return -EEXIST;
2768 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002769 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002770 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002771 clear_bit(Blocked, &rdev->flags);
2772 remove_and_add_spares(rdev->mddev, rdev);
2773 if (rdev->raid_disk >= 0)
2774 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002775 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2776 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002777 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002778 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002779 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002780 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002781 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002782
2783 if (rdev->raid_disk != -1)
2784 return -EBUSY;
2785
NeilBrownc6751b22011-02-02 11:57:13 +11002786 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2787 return -EBUSY;
2788
Neil Brown6c2fce22008-06-28 08:31:31 +10002789 if (rdev->mddev->pers->hot_add_disk == NULL)
2790 return -EINVAL;
2791
NeilBrownba1b41b2011-01-14 09:14:34 +11002792 if (slot >= rdev->mddev->raid_disks &&
2793 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2794 return -ENOSPC;
2795
Neil Brown6c2fce22008-06-28 08:31:31 +10002796 rdev->raid_disk = slot;
2797 if (test_bit(In_sync, &rdev->flags))
2798 rdev->saved_raid_disk = slot;
2799 else
2800 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002801 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002802 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002803 err = rdev->mddev->pers->
2804 hot_add_disk(rdev->mddev, rdev);
2805 if (err) {
2806 rdev->raid_disk = -1;
2807 return err;
2808 } else
2809 sysfs_notify_dirent_safe(rdev->sysfs_state);
2810 if (sysfs_link_rdev(rdev->mddev, rdev))
2811 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002812 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002813 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002814 if (slot >= rdev->mddev->raid_disks &&
2815 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002816 return -ENOSPC;
2817 rdev->raid_disk = slot;
2818 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002819 clear_bit(Faulty, &rdev->flags);
2820 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002821 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002822 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002823 }
NeilBrown014236d2006-01-06 00:20:55 -08002824 return len;
2825}
2826
NeilBrown014236d2006-01-06 00:20:55 -08002827static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002828__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002829
NeilBrown93c8cad2006-01-06 00:20:56 -08002830static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002831offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002832{
Andrew Morton6961ece2006-01-06 00:20:59 -08002833 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002834}
2835
2836static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002837offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002838{
NeilBrownc6563a82012-05-21 09:27:00 +10002839 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002840 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002841 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002842 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002843 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002844 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002845 /* Must set offset before size, so overlap checks
2846 * can be sane */
2847 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002848 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002849 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002850 return len;
2851}
2852
2853static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002854__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002855
NeilBrownc6563a82012-05-21 09:27:00 +10002856static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2857{
2858 return sprintf(page, "%llu\n",
2859 (unsigned long long)rdev->new_data_offset);
2860}
2861
2862static ssize_t new_offset_store(struct md_rdev *rdev,
2863 const char *buf, size_t len)
2864{
2865 unsigned long long new_offset;
2866 struct mddev *mddev = rdev->mddev;
2867
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002868 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002869 return -EINVAL;
2870
NeilBrownf851b602014-12-11 10:02:10 +11002871 if (mddev->sync_thread ||
2872 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002873 return -EBUSY;
2874 if (new_offset == rdev->data_offset)
2875 /* reset is always permitted */
2876 ;
2877 else if (new_offset > rdev->data_offset) {
2878 /* must not push array size beyond rdev_sectors */
2879 if (new_offset - rdev->data_offset
2880 + mddev->dev_sectors > rdev->sectors)
2881 return -E2BIG;
2882 }
2883 /* Metadata worries about other space details. */
2884
2885 /* decreasing the offset is inconsistent with a backwards
2886 * reshape.
2887 */
2888 if (new_offset < rdev->data_offset &&
2889 mddev->reshape_backwards)
2890 return -EINVAL;
2891 /* Increasing offset is inconsistent with forwards
2892 * reshape. reshape_direction should be set to
2893 * 'backwards' first.
2894 */
2895 if (new_offset > rdev->data_offset &&
2896 !mddev->reshape_backwards)
2897 return -EINVAL;
2898
2899 if (mddev->pers && mddev->persistent &&
2900 !super_types[mddev->major_version]
2901 .allow_new_offset(rdev, new_offset))
2902 return -E2BIG;
2903 rdev->new_data_offset = new_offset;
2904 if (new_offset > rdev->data_offset)
2905 mddev->reshape_backwards = 1;
2906 else if (new_offset < rdev->data_offset)
2907 mddev->reshape_backwards = 0;
2908
2909 return len;
2910}
2911static struct rdev_sysfs_entry rdev_new_offset =
2912__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2913
NeilBrown83303b62006-01-06 00:21:06 -08002914static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002915rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002916{
Andre Nolldd8ac332009-03-31 14:33:13 +11002917 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002918}
2919
NeilBrownc5d79ad2008-02-06 01:39:54 -08002920static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2921{
2922 /* check if two start/length pairs overlap */
2923 if (s1+l1 <= s2)
2924 return 0;
2925 if (s2+l2 <= s1)
2926 return 0;
2927 return 1;
2928}
2929
Dan Williamsb522adc2009-03-31 15:00:31 +11002930static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2931{
2932 unsigned long long blocks;
2933 sector_t new;
2934
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002935 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002936 return -EINVAL;
2937
2938 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2939 return -EINVAL; /* sector conversion overflow */
2940
2941 new = blocks * 2;
2942 if (new != blocks * 2)
2943 return -EINVAL; /* unsigned long long to sector_t overflow */
2944
2945 *sectors = new;
2946 return 0;
2947}
2948
NeilBrown83303b62006-01-06 00:21:06 -08002949static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002950rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002951{
NeilBrownfd01b882011-10-11 16:47:53 +11002952 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002953 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002954 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002955
Shaohua Lif2076e72015-10-08 21:54:12 -07002956 if (test_bit(Journal, &rdev->flags))
2957 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002958 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002959 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002960 if (rdev->data_offset != rdev->new_data_offset)
2961 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002962 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002963 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002964 sectors = super_types[my_mddev->major_version].
2965 rdev_size_change(rdev, sectors);
2966 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002967 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002968 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002969 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002970 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002971 if (!my_mddev->pers->resize)
2972 /* Cannot change size for RAID0 or Linear etc */
2973 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002974 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002975 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002976 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002977
Andre Nolldd8ac332009-03-31 14:33:13 +11002978 rdev->sectors = sectors;
2979 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002980 /* Need to check that all other rdevs with the same
2981 * ->bdev do not overlap. 'rcu' is sufficient to walk
2982 * the rdev lists safely.
2983 * This check does not provide a hard guarantee, it
2984 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002985 */
NeilBrownfd01b882011-10-11 16:47:53 +11002986 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002987 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002988 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002989
NeilBrown8b1afc32014-09-29 15:33:20 +10002990 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002991 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002992 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002993
NeilBrowndafb20f2012-03-19 12:46:39 +11002994 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002995 if (rdev->bdev == rdev2->bdev &&
2996 rdev != rdev2 &&
2997 overlaps(rdev->data_offset, rdev->sectors,
2998 rdev2->data_offset,
2999 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003000 overlap = 1;
3001 break;
3002 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003003 if (overlap) {
3004 mddev_put(mddev);
3005 break;
3006 }
3007 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003008 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003009 if (overlap) {
3010 /* Someone else could have slipped in a size
3011 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003012 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003013 * safe, and trust userspace not to race with
3014 * itself
3015 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003016 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003017 return -EBUSY;
3018 }
3019 }
NeilBrown83303b62006-01-06 00:21:06 -08003020 return len;
3021}
3022
3023static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003024__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003025
NeilBrown3cb03002011-10-11 16:45:26 +11003026static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003027{
3028 unsigned long long recovery_start = rdev->recovery_offset;
3029
3030 if (test_bit(In_sync, &rdev->flags) ||
3031 recovery_start == MaxSector)
3032 return sprintf(page, "none\n");
3033
3034 return sprintf(page, "%llu\n", recovery_start);
3035}
3036
NeilBrown3cb03002011-10-11 16:45:26 +11003037static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003038{
3039 unsigned long long recovery_start;
3040
3041 if (cmd_match(buf, "none"))
3042 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003043 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003044 return -EINVAL;
3045
3046 if (rdev->mddev->pers &&
3047 rdev->raid_disk >= 0)
3048 return -EBUSY;
3049
3050 rdev->recovery_offset = recovery_start;
3051 if (recovery_start == MaxSector)
3052 set_bit(In_sync, &rdev->flags);
3053 else
3054 clear_bit(In_sync, &rdev->flags);
3055 return len;
3056}
3057
3058static struct rdev_sysfs_entry rdev_recovery_start =
3059__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3060
NeilBrown16c791a2011-07-28 11:31:47 +10003061static ssize_t
3062badblocks_show(struct badblocks *bb, char *page, int unack);
3063static ssize_t
3064badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3065
NeilBrown3cb03002011-10-11 16:45:26 +11003066static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003067{
3068 return badblocks_show(&rdev->badblocks, page, 0);
3069}
NeilBrown3cb03002011-10-11 16:45:26 +11003070static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003071{
NeilBrownde393cd2011-07-28 11:31:48 +10003072 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3073 /* Maybe that ack was all we needed */
3074 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3075 wake_up(&rdev->blocked_wait);
3076 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003077}
3078static struct rdev_sysfs_entry rdev_bad_blocks =
3079__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3080
NeilBrown3cb03002011-10-11 16:45:26 +11003081static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003082{
3083 return badblocks_show(&rdev->badblocks, page, 1);
3084}
NeilBrown3cb03002011-10-11 16:45:26 +11003085static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003086{
3087 return badblocks_store(&rdev->badblocks, page, len, 1);
3088}
3089static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3090__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3091
NeilBrown86e6ffd2005-11-08 21:39:24 -08003092static struct attribute *rdev_default_attrs[] = {
3093 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003094 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003095 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003096 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003097 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003098 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003099 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003100 &rdev_bad_blocks.attr,
3101 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003102 NULL,
3103};
3104static ssize_t
3105rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3106{
3107 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003108 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003109
3110 if (!entry->show)
3111 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003112 if (!rdev->mddev)
3113 return -EBUSY;
3114 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003115}
3116
3117static ssize_t
3118rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3119 const char *page, size_t length)
3120{
3121 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003122 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003123 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003124 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003125
3126 if (!entry->store)
3127 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003128 if (!capable(CAP_SYS_ADMIN))
3129 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003130 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003131 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003132 if (rdev->mddev == NULL)
3133 rv = -EBUSY;
3134 else
3135 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003136 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003137 }
3138 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003139}
3140
3141static void rdev_free(struct kobject *ko)
3142{
NeilBrown3cb03002011-10-11 16:45:26 +11003143 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003144 kfree(rdev);
3145}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003146static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003147 .show = rdev_attr_show,
3148 .store = rdev_attr_store,
3149};
3150static struct kobj_type rdev_ktype = {
3151 .release = rdev_free,
3152 .sysfs_ops = &rdev_sysfs_ops,
3153 .default_attrs = rdev_default_attrs,
3154};
3155
NeilBrown3cb03002011-10-11 16:45:26 +11003156int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003157{
3158 rdev->desc_nr = -1;
3159 rdev->saved_raid_disk = -1;
3160 rdev->raid_disk = -1;
3161 rdev->flags = 0;
3162 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003163 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003164 rdev->sb_events = 0;
3165 rdev->last_read_error.tv_sec = 0;
3166 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003167 rdev->sb_loaded = 0;
3168 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003169 atomic_set(&rdev->nr_pending, 0);
3170 atomic_set(&rdev->read_errors, 0);
3171 atomic_set(&rdev->corrected_errors, 0);
3172
3173 INIT_LIST_HEAD(&rdev->same_set);
3174 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003175
3176 /* Add space to store bad block list.
3177 * This reserves the space even on arrays where it cannot
3178 * be used - I wonder if that matters
3179 */
3180 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003181 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003182 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3183 seqlock_init(&rdev->badblocks.lock);
3184 if (rdev->badblocks.page == NULL)
3185 return -ENOMEM;
3186
3187 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003188}
3189EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190/*
3191 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3192 *
3193 * mark the device faulty if:
3194 *
3195 * - the device is nonexistent (zero size)
3196 * - the device has no valid superblock
3197 *
3198 * a faulty rdev _never_ has rdev->sb set.
3199 */
NeilBrown3cb03002011-10-11 16:45:26 +11003200static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 char b[BDEVNAME_SIZE];
3203 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003204 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 sector_t size;
3206
NeilBrown9ffae0c2006-01-06 00:20:32 -08003207 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (!rdev) {
3209 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3210 return ERR_PTR(-ENOMEM);
3211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
NeilBrown2230dfe2011-07-28 11:31:46 +10003213 err = md_rdev_init(rdev);
3214 if (err)
3215 goto abort_free;
3216 err = alloc_disk_sb(rdev);
3217 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 goto abort_free;
3219
NeilBrownc5d79ad2008-02-06 01:39:54 -08003220 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if (err)
3222 goto abort_free;
3223
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003224 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003225
Mike Snitzer77304d22010-11-08 14:39:12 +01003226 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003228 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 "md: %s has zero or unknown size, marking faulty!\n",
3230 bdevname(rdev->bdev,b));
3231 err = -EINVAL;
3232 goto abort_free;
3233 }
3234
3235 if (super_format >= 0) {
3236 err = super_types[super_format].
3237 load_super(rdev, NULL, super_minor);
3238 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003239 printk(KERN_WARNING
3240 "md: %s does not have a valid v%d.%d "
3241 "superblock, not importing!\n",
3242 bdevname(rdev->bdev,b),
3243 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 goto abort_free;
3245 }
3246 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003247 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 "md: could not read %s's sb, not importing!\n",
3249 bdevname(rdev->bdev,b));
3250 goto abort_free;
3251 }
3252 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return rdev;
3255
3256abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003257 if (rdev->bdev)
3258 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003259 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 kfree(rdev);
3261 return ERR_PTR(err);
3262}
3263
3264/*
3265 * Check a full RAID array for plausibility
3266 */
3267
NeilBrownf72ffdd2014-09-30 14:23:59 +10003268static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003271 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 char b[BDEVNAME_SIZE];
3273
3274 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003275 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 switch (super_types[mddev->major_version].
3277 load_super(rdev, freshest, mddev->minor_version)) {
3278 case 1:
3279 freshest = rdev;
3280 break;
3281 case 0:
3282 break;
3283 default:
3284 printk( KERN_ERR \
3285 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003286 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003288 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 }
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 super_types[mddev->major_version].
3292 validate_super(mddev, freshest);
3293
3294 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003295 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003296 if (mddev->max_disks &&
3297 (rdev->desc_nr >= mddev->max_disks ||
3298 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003299 printk(KERN_WARNING
3300 "md: %s: %s: only %d devices permitted\n",
3301 mdname(mddev), bdevname(rdev->bdev, b),
3302 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003303 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003304 continue;
3305 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003306 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 if (super_types[mddev->major_version].
3308 validate_super(mddev, rdev)) {
3309 printk(KERN_WARNING "md: kicking non-fresh %s"
3310 " from array!\n",
3311 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003312 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 continue;
3314 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (mddev->level == LEVEL_MULTIPATH) {
3317 rdev->desc_nr = i++;
3318 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003319 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003320 } else if (rdev->raid_disk >=
3321 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3322 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003323 rdev->raid_disk = -1;
3324 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
3326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
NeilBrown72e02072009-12-14 12:49:55 +11003329/* Read a fixed-point number.
3330 * Numbers in sysfs attributes should be in "standard" units where
3331 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003332 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003333 * milliseconds or jiffies.
3334 * This function takes a decimal number with a possible fractional
3335 * component, and produces an integer which is the result of
3336 * multiplying that number by 10^'scale'.
3337 * all without any floating-point arithmetic.
3338 */
3339int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3340{
3341 unsigned long result = 0;
3342 long decimals = -1;
3343 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3344 if (*cp == '.')
3345 decimals = 0;
3346 else if (decimals < scale) {
3347 unsigned int value;
3348 value = *cp - '0';
3349 result = result * 10 + value;
3350 if (decimals >= 0)
3351 decimals++;
3352 }
3353 cp++;
3354 }
3355 if (*cp == '\n')
3356 cp++;
3357 if (*cp)
3358 return -EINVAL;
3359 if (decimals < 0)
3360 decimals = 0;
3361 while (decimals < scale) {
3362 result *= 10;
3363 decimals ++;
3364 }
3365 *res = result;
3366 return 0;
3367}
3368
NeilBrowneae17012005-11-08 21:39:23 -08003369static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003370safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003371{
3372 int msec = (mddev->safemode_delay*1000)/HZ;
3373 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3374}
3375static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003376safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003377{
NeilBrown16f17b32006-06-26 00:27:37 -07003378 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003379
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003380 if (mddev_is_clustered(mddev)) {
3381 pr_info("md: Safemode is disabled for clustered mode\n");
3382 return -EINVAL;
3383 }
3384
NeilBrown72e02072009-12-14 12:49:55 +11003385 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003386 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003387 if (msec == 0)
3388 mddev->safemode_delay = 0;
3389 else {
NeilBrown19052c02008-08-05 15:54:13 +10003390 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003391 unsigned long new_delay = (msec*HZ)/1000;
3392
3393 if (new_delay == 0)
3394 new_delay = 1;
3395 mddev->safemode_delay = new_delay;
3396 if (new_delay < old_delay || old_delay == 0)
3397 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003398 }
3399 return len;
3400}
3401static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003402__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003403
3404static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003405level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003406{
NeilBrown36d091f2014-12-15 12:56:58 +11003407 struct md_personality *p;
3408 int ret;
3409 spin_lock(&mddev->lock);
3410 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003411 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003412 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003413 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003414 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003415 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003416 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003417 else
NeilBrown36d091f2014-12-15 12:56:58 +11003418 ret = 0;
3419 spin_unlock(&mddev->lock);
3420 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003421}
3422
NeilBrownd9d166c2006-01-06 00:20:51 -08003423static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003424level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003425{
Dan Williamsf2859af2010-05-02 10:04:16 -07003426 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003427 ssize_t rv;
3428 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003429 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003430 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003431 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003432 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003433
NeilBrown67918752014-12-15 12:57:01 +11003434 if (slen == 0 || slen >= sizeof(clevel))
3435 return -EINVAL;
3436
3437 rv = mddev_lock(mddev);
3438 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003439 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003440
3441 if (mddev->pers == NULL) {
3442 strncpy(mddev->clevel, buf, slen);
3443 if (mddev->clevel[slen-1] == '\n')
3444 slen--;
3445 mddev->clevel[slen] = 0;
3446 mddev->level = LEVEL_NONE;
3447 rv = len;
3448 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003449 }
NeilBrown67918752014-12-15 12:57:01 +11003450 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003451 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003452 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003453
3454 /* request to change the personality. Need to ensure:
3455 * - array is not engaged in resync/recovery/reshape
3456 * - old personality can be suspended
3457 * - new personality will access other array.
3458 */
3459
NeilBrown67918752014-12-15 12:57:01 +11003460 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003461 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003462 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003463 mddev->reshape_position != MaxSector ||
3464 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003465 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003466
NeilBrown67918752014-12-15 12:57:01 +11003467 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003468 if (!mddev->pers->quiesce) {
3469 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3470 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003471 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003472 }
3473
3474 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003475 strncpy(clevel, buf, slen);
3476 if (clevel[slen-1] == '\n')
3477 slen--;
3478 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003479 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003480 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003481
Dan Williamsf2859af2010-05-02 10:04:16 -07003482 if (request_module("md-%s", clevel) != 0)
3483 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003484 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003485 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003486 if (!pers || !try_module_get(pers->owner)) {
3487 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003488 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003489 rv = -EINVAL;
3490 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003491 }
3492 spin_unlock(&pers_lock);
3493
3494 if (pers == mddev->pers) {
3495 /* Nothing to do! */
3496 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003497 rv = len;
3498 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003499 }
3500 if (!pers->takeover) {
3501 module_put(pers->owner);
3502 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003503 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003504 rv = -EINVAL;
3505 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003506 }
3507
NeilBrowndafb20f2012-03-19 12:46:39 +11003508 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003509 rdev->new_raid_disk = rdev->raid_disk;
3510
NeilBrown245f46c2009-03-31 14:39:39 +11003511 /* ->takeover must set new_* and/or delta_disks
3512 * if it succeeds, and may set them when it fails.
3513 */
3514 priv = pers->takeover(mddev);
3515 if (IS_ERR(priv)) {
3516 mddev->new_level = mddev->level;
3517 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003518 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003519 mddev->raid_disks -= mddev->delta_disks;
3520 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003521 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003522 module_put(pers->owner);
3523 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003524 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003525 rv = PTR_ERR(priv);
3526 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003527 }
3528
3529 /* Looks like we have a winner */
3530 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003531 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003532
3533 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003534 oldpers = mddev->pers;
3535 oldpriv = mddev->private;
3536 mddev->pers = pers;
3537 mddev->private = priv;
3538 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3539 mddev->level = mddev->new_level;
3540 mddev->layout = mddev->new_layout;
3541 mddev->chunk_sectors = mddev->new_chunk_sectors;
3542 mddev->delta_disks = 0;
3543 mddev->reshape_backwards = 0;
3544 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003545 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003546
NeilBrowndb721d32014-12-15 12:56:58 +11003547 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003548 mddev->external) {
3549 /* We are converting from a no-redundancy array
3550 * to a redundancy array and metadata is managed
3551 * externally so we need to be sure that writes
3552 * won't block due to a need to transition
3553 * clean->dirty
3554 * until external management is started.
3555 */
3556 mddev->in_sync = 0;
3557 mddev->safemode_delay = 0;
3558 mddev->safemode = 0;
3559 }
3560
NeilBrowndb721d32014-12-15 12:56:58 +11003561 oldpers->free(mddev, oldpriv);
3562
3563 if (oldpers->sync_request == NULL &&
3564 pers->sync_request != NULL) {
3565 /* need to add the md_redundancy_group */
3566 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3567 printk(KERN_WARNING
3568 "md: cannot register extra attributes for %s\n",
3569 mdname(mddev));
3570 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3571 }
3572 if (oldpers->sync_request != NULL &&
3573 pers->sync_request == NULL) {
3574 /* need to remove the md_redundancy_group */
3575 if (mddev->to_remove == NULL)
3576 mddev->to_remove = &md_redundancy_group;
3577 }
3578
NeilBrowndafb20f2012-03-19 12:46:39 +11003579 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003580 if (rdev->raid_disk < 0)
3581 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003582 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003583 rdev->new_raid_disk = -1;
3584 if (rdev->new_raid_disk == rdev->raid_disk)
3585 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003586 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003587 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003588 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003589 if (rdev->raid_disk < 0)
3590 continue;
3591 if (rdev->new_raid_disk == rdev->raid_disk)
3592 continue;
3593 rdev->raid_disk = rdev->new_raid_disk;
3594 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003595 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003596 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003597 if (sysfs_link_rdev(mddev, rdev))
3598 printk(KERN_WARNING "md: cannot register rd%d"
3599 " for %s after level change\n",
3600 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003601 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003602 }
3603
NeilBrowndb721d32014-12-15 12:56:58 +11003604 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003605 /* this is now an array without redundancy, so
3606 * it must always be in_sync
3607 */
3608 mddev->in_sync = 1;
3609 del_timer_sync(&mddev->safemode_timer);
3610 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003611 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003612 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003613 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003614 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003615 if (!mddev->thread)
3616 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003617 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003618 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003619 rv = len;
3620out_unlock:
3621 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003622 return rv;
3623}
3624
3625static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003626__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003627
NeilBrownd4dbd022006-06-26 00:27:59 -07003628static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003629layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003630{
3631 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003632 if (mddev->reshape_position != MaxSector &&
3633 mddev->layout != mddev->new_layout)
3634 return sprintf(page, "%d (%d)\n",
3635 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003636 return sprintf(page, "%d\n", mddev->layout);
3637}
3638
3639static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003640layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003641{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003642 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003643 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003644
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003645 err = kstrtouint(buf, 10, &n);
3646 if (err < 0)
3647 return err;
NeilBrown67918752014-12-15 12:57:01 +11003648 err = mddev_lock(mddev);
3649 if (err)
3650 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003651
NeilBrownb3546032009-03-31 14:56:41 +11003652 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003653 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003654 err = -EBUSY;
3655 else if (mddev->ro)
3656 err = -EROFS;
3657 else {
3658 mddev->new_layout = n;
3659 err = mddev->pers->check_reshape(mddev);
3660 if (err)
3661 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003662 }
NeilBrownb3546032009-03-31 14:56:41 +11003663 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003664 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003665 if (mddev->reshape_position == MaxSector)
3666 mddev->layout = n;
3667 }
NeilBrown67918752014-12-15 12:57:01 +11003668 mddev_unlock(mddev);
3669 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003670}
3671static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003672__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003673
NeilBrowneae17012005-11-08 21:39:23 -08003674static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003675raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003676{
NeilBrownbb636542005-11-08 21:39:45 -08003677 if (mddev->raid_disks == 0)
3678 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003679 if (mddev->reshape_position != MaxSector &&
3680 mddev->delta_disks != 0)
3681 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3682 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003683 return sprintf(page, "%d\n", mddev->raid_disks);
3684}
3685
NeilBrownfd01b882011-10-11 16:47:53 +11003686static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003687
3688static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003689raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003690{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003691 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003692 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003693
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003694 err = kstrtouint(buf, 10, &n);
3695 if (err < 0)
3696 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003697
NeilBrown67918752014-12-15 12:57:01 +11003698 err = mddev_lock(mddev);
3699 if (err)
3700 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003701 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003702 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003703 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003704 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003705 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003706
NeilBrown67918752014-12-15 12:57:01 +11003707 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003708 rdev_for_each(rdev, mddev) {
3709 if (olddisks < n &&
3710 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003711 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003712 if (olddisks > n &&
3713 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003714 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003715 }
NeilBrown67918752014-12-15 12:57:01 +11003716 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003717 mddev->delta_disks = n - olddisks;
3718 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003719 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003720 } else
NeilBrownda943b992006-01-06 00:20:54 -08003721 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003722out_unlock:
3723 mddev_unlock(mddev);
3724 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003725}
3726static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003727__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003728
NeilBrown24dd4692005-11-08 21:39:26 -08003729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003730chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003731{
NeilBrown08a02ec2007-05-09 02:35:38 -07003732 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003733 mddev->chunk_sectors != mddev->new_chunk_sectors)
3734 return sprintf(page, "%d (%d)\n",
3735 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003736 mddev->chunk_sectors << 9);
3737 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003738}
3739
3740static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003741chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003742{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003743 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003744 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003745
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003746 err = kstrtoul(buf, 10, &n);
3747 if (err < 0)
3748 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003749
NeilBrown67918752014-12-15 12:57:01 +11003750 err = mddev_lock(mddev);
3751 if (err)
3752 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003753 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003754 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003755 err = -EBUSY;
3756 else if (mddev->ro)
3757 err = -EROFS;
3758 else {
3759 mddev->new_chunk_sectors = n >> 9;
3760 err = mddev->pers->check_reshape(mddev);
3761 if (err)
3762 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003763 }
NeilBrownb3546032009-03-31 14:56:41 +11003764 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003765 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003766 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003767 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003768 }
NeilBrown67918752014-12-15 12:57:01 +11003769 mddev_unlock(mddev);
3770 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003771}
3772static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003773__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003774
NeilBrowna94213b2006-06-26 00:28:00 -07003775static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003776resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003777{
NeilBrownd1a7c502009-03-31 15:24:32 +11003778 if (mddev->recovery_cp == MaxSector)
3779 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003780 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3781}
3782
3783static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003784resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003785{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003786 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003787 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003788
3789 if (cmd_match(buf, "none"))
3790 n = MaxSector;
3791 else {
3792 err = kstrtoull(buf, 10, &n);
3793 if (err < 0)
3794 return err;
3795 if (n != (sector_t)n)
3796 return -EINVAL;
3797 }
NeilBrowna94213b2006-06-26 00:28:00 -07003798
NeilBrown67918752014-12-15 12:57:01 +11003799 err = mddev_lock(mddev);
3800 if (err)
3801 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003802 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003803 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003804
NeilBrown67918752014-12-15 12:57:01 +11003805 if (!err) {
3806 mddev->recovery_cp = n;
3807 if (mddev->pers)
3808 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3809 }
3810 mddev_unlock(mddev);
3811 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003812}
3813static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003814__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3815 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003816
NeilBrown9e653b62006-06-26 00:27:58 -07003817/*
3818 * The array state can be:
3819 *
3820 * clear
3821 * No devices, no size, no level
3822 * Equivalent to STOP_ARRAY ioctl
3823 * inactive
3824 * May have some settings, but array is not active
3825 * all IO results in error
3826 * When written, doesn't tear down array, but just stops it
3827 * suspended (not supported yet)
3828 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003829 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003830 * readonly
3831 * no resync can happen. no superblocks get written.
3832 * write requests fail
3833 * read-auto
3834 * like readonly, but behaves like 'clean' on a write request.
3835 *
3836 * clean - no pending writes, but otherwise active.
3837 * When written to inactive array, starts without resync
3838 * If a write request arrives then
3839 * if metadata is known, mark 'dirty' and switch to 'active'.
3840 * if not known, block and switch to write-pending
3841 * If written to an active array that has pending writes, then fails.
3842 * active
3843 * fully active: IO and resync can be happening.
3844 * When written to inactive array, starts with resync
3845 *
3846 * write-pending
3847 * clean, but writes are blocked waiting for 'active' to be written.
3848 *
3849 * active-idle
3850 * like active, but no writes have been seen for a while (100msec).
3851 *
3852 */
3853enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3854 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003855static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003856 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3857 "write-pending", "active-idle", NULL };
3858
3859static int match_word(const char *word, char **list)
3860{
3861 int n;
3862 for (n=0; list[n]; n++)
3863 if (cmd_match(word, list[n]))
3864 break;
3865 return n;
3866}
3867
3868static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003869array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003870{
3871 enum array_state st = inactive;
3872
3873 if (mddev->pers)
3874 switch(mddev->ro) {
3875 case 1:
3876 st = readonly;
3877 break;
3878 case 2:
3879 st = read_auto;
3880 break;
3881 case 0:
3882 if (mddev->in_sync)
3883 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003884 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003885 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003886 else if (mddev->safemode)
3887 st = active_idle;
3888 else
3889 st = active;
3890 }
3891 else {
3892 if (list_empty(&mddev->disks) &&
3893 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003894 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003895 st = clear;
3896 else
3897 st = inactive;
3898 }
3899 return sprintf(page, "%s\n", array_states[st]);
3900}
3901
NeilBrownf72ffdd2014-09-30 14:23:59 +10003902static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3903static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3904static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003905static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003906
3907static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003908array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003909{
NeilBrown67918752014-12-15 12:57:01 +11003910 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003911 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003912
3913 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3914 /* don't take reconfig_mutex when toggling between
3915 * clean and active
3916 */
3917 spin_lock(&mddev->lock);
3918 if (st == active) {
3919 restart_array(mddev);
3920 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3921 wake_up(&mddev->sb_wait);
3922 err = 0;
3923 } else /* st == clean */ {
3924 restart_array(mddev);
3925 if (atomic_read(&mddev->writes_pending) == 0) {
3926 if (mddev->in_sync == 0) {
3927 mddev->in_sync = 1;
3928 if (mddev->safemode == 1)
3929 mddev->safemode = 0;
3930 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3931 }
3932 err = 0;
3933 } else
3934 err = -EBUSY;
3935 }
3936 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003937 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003938 }
3939 err = mddev_lock(mddev);
3940 if (err)
3941 return err;
3942 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003943 switch(st) {
3944 case bad_word:
3945 break;
3946 case clear:
3947 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003948 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003949 break;
3950 case inactive:
3951 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003952 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003953 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003954 else
NeilBrowne6910632008-02-06 01:39:51 -08003955 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003956 break;
3957 case suspended:
3958 break; /* not supported yet */
3959 case readonly:
3960 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003961 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003962 else {
3963 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003964 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003965 err = do_md_run(mddev);
3966 }
3967 break;
3968 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003969 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003970 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003971 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003972 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003973 err = restart_array(mddev);
3974 if (err == 0) {
3975 mddev->ro = 2;
3976 set_disk_ro(mddev->gendisk, 0);
3977 }
NeilBrown9e653b62006-06-26 00:27:58 -07003978 } else {
3979 mddev->ro = 2;
3980 err = do_md_run(mddev);
3981 }
3982 break;
3983 case clean:
3984 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003985 err = restart_array(mddev);
3986 if (err)
3987 break;
NeilBrown85572d72014-12-15 12:56:56 +11003988 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003989 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003990 if (mddev->in_sync == 0) {
3991 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003992 if (mddev->safemode == 1)
3993 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003994 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003995 }
3996 err = 0;
3997 } else
3998 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003999 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004000 } else
4001 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004002 break;
4003 case active:
4004 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004005 err = restart_array(mddev);
4006 if (err)
4007 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10004008 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004009 wake_up(&mddev->sb_wait);
4010 err = 0;
4011 } else {
4012 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004013 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004014 err = do_md_run(mddev);
4015 }
4016 break;
4017 case write_pending:
4018 case active_idle:
4019 /* these cannot be set */
4020 break;
4021 }
NeilBrown67918752014-12-15 12:57:01 +11004022
4023 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004024 if (mddev->hold_active == UNTIL_IOCTL)
4025 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004026 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004027 }
NeilBrown67918752014-12-15 12:57:01 +11004028 mddev_unlock(mddev);
4029 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004030}
NeilBrown80ca3a42006-07-10 04:44:18 -07004031static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004032__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004033
NeilBrown6d7ff7382006-01-06 00:21:16 -08004034static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004035max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004036 return sprintf(page, "%d\n",
4037 atomic_read(&mddev->max_corr_read_errors));
4038}
4039
4040static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004041max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004042{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004043 unsigned int n;
4044 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004045
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004046 rv = kstrtouint(buf, 10, &n);
4047 if (rv < 0)
4048 return rv;
4049 atomic_set(&mddev->max_corr_read_errors, n);
4050 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004051}
4052
4053static struct md_sysfs_entry max_corr_read_errors =
4054__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4055 max_corrected_read_errors_store);
4056
4057static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004058null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004059{
4060 return -EINVAL;
4061}
4062
4063static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004064new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004065{
4066 /* buf must be %d:%d\n? giving major and minor numbers */
4067 /* The new device is added to the array.
4068 * If the array has a persistent superblock, we read the
4069 * superblock to initialise info and check validity.
4070 * Otherwise, only checking done is that in bind_rdev_to_array,
4071 * which mainly checks size.
4072 */
4073 char *e;
4074 int major = simple_strtoul(buf, &e, 10);
4075 int minor;
4076 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004077 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004078 int err;
4079
4080 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4081 return -EINVAL;
4082 minor = simple_strtoul(e+1, &e, 10);
4083 if (*e && *e != '\n')
4084 return -EINVAL;
4085 dev = MKDEV(major, minor);
4086 if (major != MAJOR(dev) ||
4087 minor != MINOR(dev))
4088 return -EOVERFLOW;
4089
NeilBrown67918752014-12-15 12:57:01 +11004090 flush_workqueue(md_misc_wq);
4091
4092 err = mddev_lock(mddev);
4093 if (err)
4094 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004095 if (mddev->persistent) {
4096 rdev = md_import_device(dev, mddev->major_version,
4097 mddev->minor_version);
4098 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004099 struct md_rdev *rdev0
4100 = list_entry(mddev->disks.next,
4101 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004102 err = super_types[mddev->major_version]
4103 .load_super(rdev, rdev0, mddev->minor_version);
4104 if (err < 0)
4105 goto out;
4106 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004107 } else if (mddev->external)
4108 rdev = md_import_device(dev, -2, -1);
4109 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004110 rdev = md_import_device(dev, -1, -1);
4111
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004112 if (IS_ERR(rdev)) {
4113 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004114 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004115 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004116 err = bind_rdev_to_array(rdev, mddev);
4117 out:
4118 if (err)
4119 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004120 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004121 return err ? err : len;
4122}
4123
4124static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004125__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004126
4127static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004128bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004129{
4130 char *end;
4131 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004132 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004133
NeilBrown67918752014-12-15 12:57:01 +11004134 err = mddev_lock(mddev);
4135 if (err)
4136 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004137 if (!mddev->bitmap)
4138 goto out;
4139 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4140 while (*buf) {
4141 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4142 if (buf == end) break;
4143 if (*end == '-') { /* range */
4144 buf = end + 1;
4145 end_chunk = simple_strtoul(buf, &end, 0);
4146 if (buf == end) break;
4147 }
4148 if (*end && !isspace(*end)) break;
4149 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004150 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004151 }
4152 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4153out:
NeilBrown67918752014-12-15 12:57:01 +11004154 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004155 return len;
4156}
4157
4158static struct md_sysfs_entry md_bitmap =
4159__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4160
4161static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004162size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004163{
Andre Noll58c0fed2009-03-31 14:33:13 +11004164 return sprintf(page, "%llu\n",
4165 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004166}
4167
NeilBrownfd01b882011-10-11 16:47:53 +11004168static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004169
4170static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004171size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004172{
4173 /* If array is inactive, we can reduce the component size, but
4174 * not increase it (except from 0).
4175 * If array is active, we can try an on-line resize
4176 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004177 sector_t sectors;
4178 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004179
Andre Noll58c0fed2009-03-31 14:33:13 +11004180 if (err < 0)
4181 return err;
NeilBrown67918752014-12-15 12:57:01 +11004182 err = mddev_lock(mddev);
4183 if (err)
4184 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004185 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004186 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004187 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004188 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004189 if (mddev->dev_sectors == 0 ||
4190 mddev->dev_sectors > sectors)
4191 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004192 else
4193 err = -ENOSPC;
4194 }
NeilBrown67918752014-12-15 12:57:01 +11004195 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004196 return err ? err : len;
4197}
4198
4199static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004200__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004201
Masanari Iida83f0d772012-10-30 00:18:08 +09004202/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004203 * This is one of
4204 * 'none' for arrays with no metadata (good luck...)
4205 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004206 * or N.M for internally known formats
4207 */
4208static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004209metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004210{
4211 if (mddev->persistent)
4212 return sprintf(page, "%d.%d\n",
4213 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004214 else if (mddev->external)
4215 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004216 else
4217 return sprintf(page, "none\n");
4218}
4219
4220static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004221metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004222{
4223 int major, minor;
4224 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004225 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004226 /* Changing the details of 'external' metadata is
4227 * always permitted. Otherwise there must be
4228 * no devices attached to the array.
4229 */
NeilBrown67918752014-12-15 12:57:01 +11004230
4231 err = mddev_lock(mddev);
4232 if (err)
4233 return err;
4234 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004235 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4236 ;
4237 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004238 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004239
NeilBrown67918752014-12-15 12:57:01 +11004240 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004241 if (cmd_match(buf, "none")) {
4242 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004243 mddev->external = 0;
4244 mddev->major_version = 0;
4245 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004246 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004247 }
4248 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004249 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004250 if (namelen >= sizeof(mddev->metadata_type))
4251 namelen = sizeof(mddev->metadata_type)-1;
4252 strncpy(mddev->metadata_type, buf+9, namelen);
4253 mddev->metadata_type[namelen] = 0;
4254 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4255 mddev->metadata_type[--namelen] = 0;
4256 mddev->persistent = 0;
4257 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004258 mddev->major_version = 0;
4259 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004260 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004261 }
4262 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004263 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004264 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004265 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004266 buf = e+1;
4267 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004268 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004269 goto out_unlock;
4270 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004271 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004272 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004273 mddev->major_version = major;
4274 mddev->minor_version = minor;
4275 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004276 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004277 err = 0;
4278out_unlock:
4279 mddev_unlock(mddev);
4280 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004281}
4282
4283static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004284__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004285
NeilBrowna35b0d62006-01-06 00:20:49 -08004286static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004287action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004288{
NeilBrown7eec3142005-11-08 21:39:44 -08004289 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004290 unsigned long recovery = mddev->recovery;
4291 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004292 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004293 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4294 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4295 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004296 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004297 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4298 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004299 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004300 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004301 type = "check";
4302 else
4303 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004304 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004305 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004306 else if (mddev->reshape_position != MaxSector)
4307 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004308 }
4309 return sprintf(page, "%s\n", type);
4310}
4311
4312static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004313action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004314{
NeilBrown7eec3142005-11-08 21:39:44 -08004315 if (!mddev->pers || !mddev->pers->sync_request)
4316 return -EINVAL;
4317
NeilBrownb6a9ce62009-05-26 09:41:17 +10004318
4319 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004320 if (cmd_match(page, "frozen"))
4321 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4322 else
4323 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004324 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4325 mddev_lock(mddev) == 0) {
4326 flush_workqueue(md_misc_wq);
4327 if (mddev->sync_thread) {
4328 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004329 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004330 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004331 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004332 }
NeilBrown312045e2015-12-21 11:01:21 +11004333 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004334 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004335 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004336 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004337 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004338 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004339 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004340 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004341 int err;
4342 if (mddev->pers->start_reshape == NULL)
4343 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004344 err = mddev_lock(mddev);
4345 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004346 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4347 err = -EBUSY;
4348 else {
4349 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4350 err = mddev->pers->start_reshape(mddev);
4351 }
NeilBrown67918752014-12-15 12:57:01 +11004352 mddev_unlock(mddev);
4353 }
NeilBrown16484bf2006-03-27 01:18:13 -08004354 if (err)
4355 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004356 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004357 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004358 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004359 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004360 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004361 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004362 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004363 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4364 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004365 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004366 if (mddev->ro == 2) {
4367 /* A write to sync_action is enough to justify
4368 * canceling read-auto mode
4369 */
4370 mddev->ro = 0;
4371 md_wakeup_thread(mddev->sync_thread);
4372 }
NeilBrown03c902e2006-01-06 00:20:46 -08004373 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004374 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004375 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004376 return len;
4377}
4378
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004379static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004380__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004381
4382static ssize_t
4383last_sync_action_show(struct mddev *mddev, char *page)
4384{
4385 return sprintf(page, "%s\n", mddev->last_sync_action);
4386}
4387
4388static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4389
NeilBrown9d888832005-11-08 21:39:26 -08004390static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004391mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004392{
4393 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004394 (unsigned long long)
4395 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004396}
4397
NeilBrown80ca3a42006-07-10 04:44:18 -07004398static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004399
NeilBrown88202a02006-01-06 00:21:36 -08004400static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004401sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004402{
4403 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4404 mddev->sync_speed_min ? "local": "system");
4405}
4406
4407static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004408sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004409{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004410 unsigned int min;
4411 int rv;
4412
NeilBrown88202a02006-01-06 00:21:36 -08004413 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004414 min = 0;
4415 } else {
4416 rv = kstrtouint(buf, 10, &min);
4417 if (rv < 0)
4418 return rv;
4419 if (min == 0)
4420 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004421 }
NeilBrown88202a02006-01-06 00:21:36 -08004422 mddev->sync_speed_min = min;
4423 return len;
4424}
4425
4426static struct md_sysfs_entry md_sync_min =
4427__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4428
4429static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004430sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004431{
4432 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4433 mddev->sync_speed_max ? "local": "system");
4434}
4435
4436static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004437sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004438{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004439 unsigned int max;
4440 int rv;
4441
NeilBrown88202a02006-01-06 00:21:36 -08004442 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004443 max = 0;
4444 } else {
4445 rv = kstrtouint(buf, 10, &max);
4446 if (rv < 0)
4447 return rv;
4448 if (max == 0)
4449 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004450 }
NeilBrown88202a02006-01-06 00:21:36 -08004451 mddev->sync_speed_max = max;
4452 return len;
4453}
4454
4455static struct md_sysfs_entry md_sync_max =
4456__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4457
Iustin Popd7f3d292007-10-16 23:30:54 -07004458static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004459degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004460{
4461 return sprintf(page, "%d\n", mddev->degraded);
4462}
4463static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004464
4465static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004466sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004467{
4468 return sprintf(page, "%d\n", mddev->parallel_resync);
4469}
4470
4471static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004472sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004473{
4474 long n;
4475
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004476 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004477 return -EINVAL;
4478
4479 if (n != 0 && n != 1)
4480 return -EINVAL;
4481
4482 mddev->parallel_resync = n;
4483
4484 if (mddev->sync_thread)
4485 wake_up(&resync_wait);
4486
4487 return len;
4488}
4489
4490/* force parallel resync, even with shared block devices */
4491static struct md_sysfs_entry md_sync_force_parallel =
4492__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4493 sync_force_parallel_show, sync_force_parallel_store);
4494
4495static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004496sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004497{
4498 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004499 if (mddev->curr_resync == 0)
4500 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004501 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4502 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004503 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004504 db = resync - mddev->resync_mark_cnt;
4505 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004506}
4507
NeilBrown80ca3a42006-07-10 04:44:18 -07004508static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004509
4510static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004511sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004512{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004513 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004514
NeilBrownacb180b2009-04-14 16:28:34 +10004515 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4516 return sprintf(page, "none\n");
4517
NeilBrown72f36d52012-10-11 14:25:57 +11004518 if (mddev->curr_resync == 1 ||
4519 mddev->curr_resync == 2)
4520 return sprintf(page, "delayed\n");
4521
NeilBrownc804cde2012-05-21 09:28:33 +10004522 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4523 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004524 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004525 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004526 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004527
NeilBrownacb180b2009-04-14 16:28:34 +10004528 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004529 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004530}
4531
NeilBrown750f1992014-09-30 08:53:05 +10004532static struct md_sysfs_entry md_sync_completed =
4533 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004534
NeilBrowne464eaf2006-03-27 01:18:14 -08004535static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004536min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004537{
4538 return sprintf(page, "%llu\n",
4539 (unsigned long long)mddev->resync_min);
4540}
4541static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004542min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004543{
4544 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004545 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004546
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004547 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004548 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004549
4550 spin_lock(&mddev->lock);
4551 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004552 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004553 goto out_unlock;
4554
4555 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004556 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004557 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004558
NeilBrown50c37b12015-03-23 17:36:38 +11004559 /* Round down to multiple of 4K for safety */
4560 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004561 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004562
NeilBrown23da4222014-12-15 12:57:01 +11004563out_unlock:
4564 spin_unlock(&mddev->lock);
4565 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004566}
4567
4568static struct md_sysfs_entry md_min_sync =
4569__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4570
4571static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004572max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004573{
4574 if (mddev->resync_max == MaxSector)
4575 return sprintf(page, "max\n");
4576 else
4577 return sprintf(page, "%llu\n",
4578 (unsigned long long)mddev->resync_max);
4579}
4580static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004581max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004582{
NeilBrown23da4222014-12-15 12:57:01 +11004583 int err;
4584 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004585 if (strncmp(buf, "max", 3) == 0)
4586 mddev->resync_max = MaxSector;
4587 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004588 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004589 int chunk;
4590
4591 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004592 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004593 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004594 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004595 goto out_unlock;
4596
4597 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004598 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004599 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004600 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004601 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004602
4603 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004604 chunk = mddev->chunk_sectors;
4605 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004606 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004607
4608 err = -EINVAL;
4609 if (sector_div(temp, chunk))
4610 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004611 }
4612 mddev->resync_max = max;
4613 }
4614 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004615 err = 0;
4616out_unlock:
4617 spin_unlock(&mddev->lock);
4618 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004619}
4620
4621static struct md_sysfs_entry md_max_sync =
4622__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4623
4624static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004625suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004626{
4627 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4628}
4629
4630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004631suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004632{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004633 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004634 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004635
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004636 err = kstrtoull(buf, 10, &new);
4637 if (err < 0)
4638 return err;
4639 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004640 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004641
NeilBrown67918752014-12-15 12:57:01 +11004642 err = mddev_lock(mddev);
4643 if (err)
4644 return err;
4645 err = -EINVAL;
4646 if (mddev->pers == NULL ||
4647 mddev->pers->quiesce == NULL)
4648 goto unlock;
4649 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004650 mddev->suspend_lo = new;
4651 if (new >= old)
4652 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004653 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004654 else {
4655 /* Expanding suspended region - need to wait */
4656 mddev->pers->quiesce(mddev, 1);
4657 mddev->pers->quiesce(mddev, 0);
4658 }
NeilBrown67918752014-12-15 12:57:01 +11004659 err = 0;
4660unlock:
4661 mddev_unlock(mddev);
4662 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004663}
4664static struct md_sysfs_entry md_suspend_lo =
4665__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4666
NeilBrowne464eaf2006-03-27 01:18:14 -08004667static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004668suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004669{
4670 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4671}
4672
4673static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004674suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004675{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004676 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004677 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004678
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004679 err = kstrtoull(buf, 10, &new);
4680 if (err < 0)
4681 return err;
4682 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004683 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004684
NeilBrown67918752014-12-15 12:57:01 +11004685 err = mddev_lock(mddev);
4686 if (err)
4687 return err;
4688 err = -EINVAL;
4689 if (mddev->pers == NULL ||
4690 mddev->pers->quiesce == NULL)
4691 goto unlock;
4692 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004693 mddev->suspend_hi = new;
4694 if (new <= old)
4695 /* Shrinking suspended region */
4696 mddev->pers->quiesce(mddev, 2);
4697 else {
4698 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004699 mddev->pers->quiesce(mddev, 1);
4700 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004701 }
NeilBrown67918752014-12-15 12:57:01 +11004702 err = 0;
4703unlock:
4704 mddev_unlock(mddev);
4705 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004706}
4707static struct md_sysfs_entry md_suspend_hi =
4708__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4709
NeilBrown08a02ec2007-05-09 02:35:38 -07004710static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004711reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004712{
4713 if (mddev->reshape_position != MaxSector)
4714 return sprintf(page, "%llu\n",
4715 (unsigned long long)mddev->reshape_position);
4716 strcpy(page, "none\n");
4717 return 5;
4718}
4719
4720static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004721reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004722{
NeilBrownc6563a82012-05-21 09:27:00 +10004723 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004724 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004725 int err;
NeilBrown67918752014-12-15 12:57:01 +11004726
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004727 err = kstrtoull(buf, 10, &new);
4728 if (err < 0)
4729 return err;
4730 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004731 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004732 err = mddev_lock(mddev);
4733 if (err)
4734 return err;
4735 err = -EBUSY;
4736 if (mddev->pers)
4737 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004738 mddev->reshape_position = new;
4739 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004740 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004741 mddev->new_level = mddev->level;
4742 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004743 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004744 rdev_for_each(rdev, mddev)
4745 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004746 err = 0;
4747unlock:
4748 mddev_unlock(mddev);
4749 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004750}
4751
4752static struct md_sysfs_entry md_reshape_position =
4753__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4754 reshape_position_store);
4755
Dan Williamsb522adc2009-03-31 15:00:31 +11004756static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004757reshape_direction_show(struct mddev *mddev, char *page)
4758{
4759 return sprintf(page, "%s\n",
4760 mddev->reshape_backwards ? "backwards" : "forwards");
4761}
4762
4763static ssize_t
4764reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4765{
4766 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004767 int err;
4768
NeilBrown2c810cd2012-05-21 09:27:00 +10004769 if (cmd_match(buf, "forwards"))
4770 backwards = 0;
4771 else if (cmd_match(buf, "backwards"))
4772 backwards = 1;
4773 else
4774 return -EINVAL;
4775 if (mddev->reshape_backwards == backwards)
4776 return len;
4777
NeilBrown67918752014-12-15 12:57:01 +11004778 err = mddev_lock(mddev);
4779 if (err)
4780 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004781 /* check if we are allowed to change */
4782 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004783 err = -EBUSY;
4784 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004785 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004786 err = -EINVAL;
4787 else
4788 mddev->reshape_backwards = backwards;
4789 mddev_unlock(mddev);
4790 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004791}
4792
4793static struct md_sysfs_entry md_reshape_direction =
4794__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4795 reshape_direction_store);
4796
4797static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004798array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004799{
4800 if (mddev->external_size)
4801 return sprintf(page, "%llu\n",
4802 (unsigned long long)mddev->array_sectors/2);
4803 else
4804 return sprintf(page, "default\n");
4805}
4806
4807static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004808array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004809{
4810 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004811 int err;
4812
4813 err = mddev_lock(mddev);
4814 if (err)
4815 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004816
4817 if (strncmp(buf, "default", 7) == 0) {
4818 if (mddev->pers)
4819 sectors = mddev->pers->size(mddev, 0, 0);
4820 else
4821 sectors = mddev->array_sectors;
4822
4823 mddev->external_size = 0;
4824 } else {
4825 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004826 err = -EINVAL;
4827 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4828 err = -E2BIG;
4829 else
4830 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004831 }
4832
NeilBrown67918752014-12-15 12:57:01 +11004833 if (!err) {
4834 mddev->array_sectors = sectors;
4835 if (mddev->pers) {
4836 set_capacity(mddev->gendisk, mddev->array_sectors);
4837 revalidate_disk(mddev->gendisk);
4838 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004839 }
NeilBrown67918752014-12-15 12:57:01 +11004840 mddev_unlock(mddev);
4841 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004842}
4843
4844static struct md_sysfs_entry md_array_size =
4845__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4846 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004847
NeilBrowneae17012005-11-08 21:39:23 -08004848static struct attribute *md_default_attrs[] = {
4849 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004850 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004851 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004852 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004853 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004854 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004855 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004856 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004857 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004858 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004859 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004860 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004861 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004862 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004863 NULL,
4864};
4865
4866static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004867 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004868 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004869 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004870 &md_sync_min.attr,
4871 &md_sync_max.attr,
4872 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004873 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004874 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004875 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004876 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004877 &md_suspend_lo.attr,
4878 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004879 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004880 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004881 NULL,
4882};
NeilBrown411036f2005-11-08 21:39:40 -08004883static struct attribute_group md_redundancy_group = {
4884 .name = NULL,
4885 .attrs = md_redundancy_attrs,
4886};
4887
NeilBrowneae17012005-11-08 21:39:23 -08004888static ssize_t
4889md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4890{
4891 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004892 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004893 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004894
4895 if (!entry->show)
4896 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004897 spin_lock(&all_mddevs_lock);
4898 if (list_empty(&mddev->all_mddevs)) {
4899 spin_unlock(&all_mddevs_lock);
4900 return -EBUSY;
4901 }
4902 mddev_get(mddev);
4903 spin_unlock(&all_mddevs_lock);
4904
NeilBrownb7b17c92014-12-15 12:56:59 +11004905 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004906 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004907 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004908}
4909
4910static ssize_t
4911md_attr_store(struct kobject *kobj, struct attribute *attr,
4912 const char *page, size_t length)
4913{
4914 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004915 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004916 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004917
4918 if (!entry->store)
4919 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004920 if (!capable(CAP_SYS_ADMIN))
4921 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004922 spin_lock(&all_mddevs_lock);
4923 if (list_empty(&mddev->all_mddevs)) {
4924 spin_unlock(&all_mddevs_lock);
4925 return -EBUSY;
4926 }
4927 mddev_get(mddev);
4928 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004929 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004930 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004931 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004932}
4933
4934static void md_free(struct kobject *ko)
4935{
NeilBrownfd01b882011-10-11 16:47:53 +11004936 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004937
4938 if (mddev->sysfs_state)
4939 sysfs_put(mddev->sysfs_state);
4940
NeilBrown6cd18e72015-04-27 14:12:22 +10004941 if (mddev->queue)
4942 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004943 if (mddev->gendisk) {
4944 del_gendisk(mddev->gendisk);
4945 put_disk(mddev->gendisk);
4946 }
NeilBrowna21d1502009-01-09 08:31:09 +11004947
NeilBrowneae17012005-11-08 21:39:23 -08004948 kfree(mddev);
4949}
4950
Emese Revfy52cf25d2010-01-19 02:58:23 +01004951static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004952 .show = md_attr_show,
4953 .store = md_attr_store,
4954};
4955static struct kobj_type md_ktype = {
4956 .release = md_free,
4957 .sysfs_ops = &md_sysfs_ops,
4958 .default_attrs = md_default_attrs,
4959};
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961int mdp_major = 0;
4962
Dan Williams5fd3a172009-03-04 00:57:25 -07004963static void mddev_delayed_delete(struct work_struct *ws)
4964{
NeilBrownfd01b882011-10-11 16:47:53 +11004965 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004966
NeilBrown43a70502009-12-14 12:49:55 +11004967 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004968 kobject_del(&mddev->kobj);
4969 kobject_put(&mddev->kobj);
4970}
4971
NeilBrownefeb53c2009-01-09 08:31:10 +11004972static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004974 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004975 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004977 int partitioned;
4978 int shift;
4979 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004980 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
4982 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004983 return -ENODEV;
4984
4985 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4986 shift = partitioned ? MdpMinorShift : 0;
4987 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
Tejun Heoe804ac72010-10-15 15:36:08 +02004989 /* wait for any previous instance of this device to be
4990 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004991 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004992 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004993
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004994 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004995 error = -EEXIST;
4996 if (mddev->gendisk)
4997 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004998
4999 if (name) {
5000 /* Need to ensure that 'name' is not a duplicate.
5001 */
NeilBrownfd01b882011-10-11 16:47:53 +11005002 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005003 spin_lock(&all_mddevs_lock);
5004
5005 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5006 if (mddev2->gendisk &&
5007 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5008 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005009 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005010 }
5011 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 }
NeilBrown8b765392009-01-09 08:31:08 +11005013
NeilBrown0909dc42009-07-01 12:27:21 +10005014 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005015 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005016 if (!mddev->queue)
5017 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005018 mddev->queue->queuedata = mddev;
5019
NeilBrown409c57f2009-03-31 14:39:39 +11005020 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005021 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 disk = alloc_disk(1 << shift);
5024 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005025 blk_cleanup_queue(mddev->queue);
5026 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005027 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005029 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005031 if (name)
5032 strcpy(disk->disk_name, name);
5033 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005035 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 disk->fops = &md_fops;
5038 disk->private_data = mddev;
5039 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10005040 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11005041 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005042 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005043 * remove it now.
5044 */
5045 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005047 /* As soon as we call add_disk(), another thread could get
5048 * through to md_open, so make sure it doesn't get too far
5049 */
5050 mutex_lock(&mddev->open_mutex);
5051 add_disk(disk);
5052
Tejun Heoed9e1982008-08-25 19:56:05 +09005053 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5054 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005055 if (error) {
5056 /* This isn't possible, but as kobject_init_and_add is marked
5057 * __must_check, we must do something with the result
5058 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005059 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5060 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005061 error = 0;
5062 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005063 if (mddev->kobj.sd &&
5064 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11005065 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005066 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005067 abort:
5068 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005069 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005070 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005071 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005072 }
NeilBrownd3374822009-01-09 08:31:10 +11005073 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005074 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005075}
5076
5077static struct kobject *md_probe(dev_t dev, int *part, void *data)
5078{
5079 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 return NULL;
5081}
5082
NeilBrownefeb53c2009-01-09 08:31:10 +11005083static int add_named_array(const char *val, struct kernel_param *kp)
5084{
5085 /* val must be "md_*" where * is not all digits.
5086 * We allocate an array with a large free minor number, and
5087 * set the name to val. val must not already be an active name.
5088 */
5089 int len = strlen(val);
5090 char buf[DISK_NAME_LEN];
5091
5092 while (len && val[len-1] == '\n')
5093 len--;
5094 if (len >= DISK_NAME_LEN)
5095 return -E2BIG;
5096 strlcpy(buf, val, len+1);
5097 if (strncmp(buf, "md_", 3) != 0)
5098 return -EINVAL;
5099 return md_alloc(0, buf);
5100}
5101
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102static void md_safemode_timeout(unsigned long data)
5103{
NeilBrownfd01b882011-10-11 16:47:53 +11005104 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
Neil Brown0fd62b82008-06-28 08:31:36 +10005106 if (!atomic_read(&mddev->writes_pending)) {
5107 mddev->safemode = 1;
5108 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005109 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 md_wakeup_thread(mddev->thread);
5112}
5113
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005114static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
NeilBrownfd01b882011-10-11 16:47:53 +11005116int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117{
NeilBrown2604b702006-01-06 00:20:36 -08005118 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005119 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005120 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
NeilBrowna757e642005-04-16 15:26:42 -07005122 if (list_empty(&mddev->disks))
5123 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
5126 if (mddev->pers)
5127 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005128 /* Cannot run until previous stop completes properly */
5129 if (mddev->sysfs_active)
5130 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005131
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 /*
5133 * Analyze all RAID superblock(s)
5134 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005135 if (!mddev->raid_disks) {
5136 if (!mddev->persistent)
5137 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005138 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
NeilBrownd9d166c2006-01-06 00:20:51 -08005141 if (mddev->level != LEVEL_NONE)
5142 request_module("md-level-%d", mddev->level);
5143 else if (mddev->clevel[0])
5144 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
5146 /*
5147 * Drop all container device buffers, from now on
5148 * the only valid external interface is through the md
5149 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005151 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005152 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 continue;
5154 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005155 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005156
5157 /* perform some consistency tests on the device.
5158 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005159 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005160 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005161 if (rdev->meta_bdev) {
5162 /* Nothing to check */;
5163 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005164 if (mddev->dev_sectors &&
5165 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005166 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005167 printk("md: %s: data overlaps metadata\n",
5168 mdname(mddev));
5169 return -EINVAL;
5170 }
5171 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005172 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005173 > rdev->data_offset) {
5174 printk("md: %s: metadata overlaps data\n",
5175 mdname(mddev));
5176 return -EINVAL;
5177 }
5178 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005179 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 }
5181
NeilBrowna167f662010-10-26 18:31:13 +11005182 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005183 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005184
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005186 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005187 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005189 if (mddev->level != LEVEL_NONE)
5190 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5191 mddev->level);
5192 else
5193 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5194 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 return -EINVAL;
5196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005198 if (mddev->level != pers->level) {
5199 mddev->level = pers->level;
5200 mddev->new_level = pers->level;
5201 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005202 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
NeilBrownf6705572006-03-27 01:18:11 -08005204 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005205 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005206 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005207 module_put(pers->owner);
5208 return -EINVAL;
5209 }
5210
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005211 if (pers->sync_request) {
5212 /* Warn if this is a potentially silly
5213 * configuration.
5214 */
5215 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005216 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005217 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005218
NeilBrowndafb20f2012-03-19 12:46:39 +11005219 rdev_for_each(rdev, mddev)
5220 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005221 if (rdev < rdev2 &&
5222 rdev->bdev->bd_contains ==
5223 rdev2->bdev->bd_contains) {
5224 printk(KERN_WARNING
5225 "%s: WARNING: %s appears to be"
5226 " on the same physical disk as"
5227 " %s.\n",
5228 mdname(mddev),
5229 bdevname(rdev->bdev,b),
5230 bdevname(rdev2->bdev,b2));
5231 warned = 1;
5232 }
5233 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005234
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005235 if (warned)
5236 printk(KERN_WARNING
5237 "True protection against single-disk"
5238 " failure might be compromised.\n");
5239 }
5240
NeilBrown657390d2005-08-26 18:34:16 -07005241 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005242 /* may be over-ridden by personality */
5243 mddev->resync_max_sectors = mddev->dev_sectors;
5244
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005245 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
NeilBrown0f9552b52009-12-30 12:08:50 +11005247 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005248 mddev->ro = 2; /* read-only, but switch on first write */
5249
NeilBrown36d091f2014-12-15 12:56:58 +11005250 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005251 if (err)
5252 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005253 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005254 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5255 " but 'external_size' not in effect?\n", __func__);
5256 printk(KERN_ERR
5257 "md: invalid array_size %llu > default size %llu\n",
5258 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005259 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005260 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005261 }
NeilBrown36d091f2014-12-15 12:56:58 +11005262 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005263 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005264 struct bitmap *bitmap;
5265
5266 bitmap = bitmap_create(mddev, -1);
5267 if (IS_ERR(bitmap)) {
5268 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005269 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5270 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005271 } else
5272 mddev->bitmap = bitmap;
5273
NeilBrownb15c2e52006-01-06 00:20:16 -08005274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005276 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005277 if (mddev->private)
5278 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005279 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005280 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005281 bitmap_destroy(mddev);
5282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 }
NeilBrown5c675f82014-12-15 12:56:56 +11005284 if (mddev->queue) {
5285 mddev->queue->backing_dev_info.congested_data = mddev;
5286 mddev->queue->backing_dev_info.congested_fn = md_congested;
5287 }
NeilBrown36d091f2014-12-15 12:56:58 +11005288 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005289 if (mddev->kobj.sd &&
5290 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005291 printk(KERN_WARNING
5292 "md: cannot register extra attributes for %s\n",
5293 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005294 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005295 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005296 mddev->ro = 0;
5297
NeilBrownf72ffdd2014-09-30 14:23:59 +10005298 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005299 atomic_set(&mddev->max_corr_read_errors,
5300 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005302 if (mddev_is_clustered(mddev))
5303 mddev->safemode_delay = 0;
5304 else
5305 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005307 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005308 spin_lock(&mddev->lock);
5309 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005310 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005311 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005312 if (rdev->raid_disk >= 0)
5313 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005314 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005315
NeilBrowna4a3d262015-07-17 11:57:30 +10005316 if (mddev->degraded && !mddev->ro)
5317 /* This ensures that recovering status is reported immediately
5318 * via sysfs - until a lack of spares is confirmed.
5319 */
5320 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005322
NeilBrown7a0a5352013-08-27 16:28:23 +10005323 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005324 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
NeilBrownd7603b72006-01-06 00:20:30 -08005326 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005327 sysfs_notify_dirent_safe(mddev->sysfs_state);
5328 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005329 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 return 0;
5331}
NeilBrown390ee602010-06-01 19:37:27 +10005332EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
NeilBrownfd01b882011-10-11 16:47:53 +11005334static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005335{
5336 int err;
5337
5338 err = md_run(mddev);
5339 if (err)
5340 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005341 err = bitmap_load(mddev);
5342 if (err) {
5343 bitmap_destroy(mddev);
5344 goto out;
5345 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005346
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005347 if (mddev_is_clustered(mddev))
5348 md_allow_write(mddev);
5349
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005350 md_wakeup_thread(mddev->thread);
5351 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5352
NeilBrownfe60b012010-03-29 11:10:42 +11005353 set_capacity(mddev->gendisk, mddev->array_sectors);
5354 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005355 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005356 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5357out:
5358 return err;
5359}
5360
NeilBrownfd01b882011-10-11 16:47:53 +11005361static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
5363 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
Andre Noll80fab1d2008-07-11 22:02:21 +10005365 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005367 return -ENXIO;
5368 if (!mddev->pers)
5369 return -EINVAL;
5370 if (!mddev->ro)
5371 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005372 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5373 struct md_rdev *rdev;
5374 bool has_journal = false;
5375
5376 rcu_read_lock();
5377 rdev_for_each_rcu(rdev, mddev) {
5378 if (test_bit(Journal, &rdev->flags) &&
5379 !test_bit(Faulty, &rdev->flags)) {
5380 has_journal = true;
5381 break;
5382 }
5383 }
5384 rcu_read_unlock();
5385
5386 /* Don't restart rw with journal missing/faulty */
5387 if (!has_journal)
5388 return -EINVAL;
5389 }
5390
Andre Noll80fab1d2008-07-11 22:02:21 +10005391 mddev->safemode = 0;
5392 mddev->ro = 0;
5393 set_disk_ro(disk, 0);
5394 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5395 mdname(mddev));
5396 /* Kick recovery or resync if necessary */
5397 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5398 md_wakeup_thread(mddev->thread);
5399 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005400 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
NeilBrownfd01b882011-10-11 16:47:53 +11005404static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005405{
5406 mddev->array_sectors = 0;
5407 mddev->external_size = 0;
5408 mddev->dev_sectors = 0;
5409 mddev->raid_disks = 0;
5410 mddev->recovery_cp = 0;
5411 mddev->resync_min = 0;
5412 mddev->resync_max = MaxSector;
5413 mddev->reshape_position = MaxSector;
5414 mddev->external = 0;
5415 mddev->persistent = 0;
5416 mddev->level = LEVEL_NONE;
5417 mddev->clevel[0] = 0;
5418 mddev->flags = 0;
5419 mddev->ro = 0;
5420 mddev->metadata_type[0] = 0;
5421 mddev->chunk_sectors = 0;
5422 mddev->ctime = mddev->utime = 0;
5423 mddev->layout = 0;
5424 mddev->max_disks = 0;
5425 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005426 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005427 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005428 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005429 mddev->new_level = LEVEL_NONE;
5430 mddev->new_layout = 0;
5431 mddev->new_chunk_sectors = 0;
5432 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005433 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005434 mddev->suspend_lo = mddev->suspend_hi = 0;
5435 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5436 mddev->recovery = 0;
5437 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005438 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005439 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005440 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005441 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005442 mddev->bitmap_info.offset = 0;
5443 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005444 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005445 mddev->bitmap_info.chunksize = 0;
5446 mddev->bitmap_info.daemon_sleep = 0;
5447 mddev->bitmap_info.max_write_behind = 0;
5448}
5449
NeilBrownfd01b882011-10-11 16:47:53 +11005450static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005451{
NeilBrown6b6204e2013-05-09 09:48:30 +10005452 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005453 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005454 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005455 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005456 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005457 }
5458
5459 del_timer_sync(&mddev->safemode_timer);
5460
5461 bitmap_flush(mddev);
5462 md_super_wait(mddev);
5463
NeilBrownb6d428c2013-04-24 11:42:42 +10005464 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005465 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5466 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005467 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005468 if (!mddev_is_clustered(mddev))
5469 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005470 md_update_sb(mddev, 1);
5471 }
5472}
NeilBrowndefad612011-01-14 09:14:33 +11005473
NeilBrownfd01b882011-10-11 16:47:53 +11005474void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005475{
NeilBrown29f097c2013-11-14 17:54:51 +11005476 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005477 __md_stop_writes(mddev);
5478 mddev_unlock(mddev);
5479}
NeilBrown390ee602010-06-01 19:37:27 +10005480EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005481
NeilBrown5aa61f42014-12-15 12:56:57 +11005482static void mddev_detach(struct mddev *mddev)
5483{
5484 struct bitmap *bitmap = mddev->bitmap;
5485 /* wait for behind writes to complete */
5486 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5487 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5488 mdname(mddev));
5489 /* need to kick something here to make sure I/O goes? */
5490 wait_event(bitmap->behind_wait,
5491 atomic_read(&bitmap->behind_writes) == 0);
5492 }
NeilBrown36d091f2014-12-15 12:56:58 +11005493 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005494 mddev->pers->quiesce(mddev, 1);
5495 mddev->pers->quiesce(mddev, 0);
5496 }
5497 md_unregister_thread(&mddev->thread);
5498 if (mddev->queue)
5499 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5500}
5501
NeilBrown5eff3c42012-11-19 10:47:48 +11005502static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005503{
NeilBrown36d091f2014-12-15 12:56:58 +11005504 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005505 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005506 /* Ensure ->event_work is done */
5507 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005508 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005509 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005510 spin_unlock(&mddev->lock);
5511 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005512 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005513 if (pers->sync_request && mddev->to_remove == NULL)
5514 mddev->to_remove = &md_redundancy_group;
5515 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005516 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005517}
NeilBrown5eff3c42012-11-19 10:47:48 +11005518
5519void md_stop(struct mddev *mddev)
5520{
5521 /* stop the array and free an attached data structures.
5522 * This is called from dm-raid
5523 */
5524 __md_stop(mddev);
5525 bitmap_destroy(mddev);
5526 if (mddev->bio_set)
5527 bioset_free(mddev->bio_set);
5528}
5529
NeilBrown390ee602010-06-01 19:37:27 +10005530EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005531
NeilBrowna05b7ea2012-07-19 15:59:18 +10005532static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005533{
5534 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005535 int did_freeze = 0;
5536
5537 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5538 did_freeze = 1;
5539 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5540 md_wakeup_thread(mddev->thread);
5541 }
NeilBrownf851b602014-12-11 10:02:10 +11005542 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005543 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005544 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005545 /* Thread might be blocked waiting for metadata update
5546 * which will now never happen */
5547 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005548
NeilBrown88724bf2015-09-24 14:00:51 +10005549 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5550 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005551 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005552 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5553 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005554 wait_event(mddev->sb_wait,
5555 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005556 mddev_lock_nointr(mddev);
5557
NeilBrowna4bd82d2010-03-29 13:23:10 +11005558 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005559 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005560 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005561 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005562 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005563 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005564 if (did_freeze) {
5565 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005566 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005567 md_wakeup_thread(mddev->thread);
5568 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005569 err = -EBUSY;
5570 goto out;
5571 }
5572 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005573 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005574
5575 err = -ENXIO;
5576 if (mddev->ro==1)
5577 goto out;
5578 mddev->ro = 1;
5579 set_disk_ro(mddev->gendisk, 1);
5580 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005581 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5582 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005583 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005584 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005585 }
5586out:
5587 mutex_unlock(&mddev->open_mutex);
5588 return err;
5589}
5590
NeilBrown9e653b62006-06-26 00:27:58 -07005591/* mode:
5592 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005593 * 2 - stop but do not disassemble array
5594 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005595static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005596 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005599 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005600 int did_freeze = 0;
5601
5602 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5603 did_freeze = 1;
5604 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5605 md_wakeup_thread(mddev->thread);
5606 }
NeilBrownf851b602014-12-11 10:02:10 +11005607 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005608 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005609 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005610 /* Thread might be blocked waiting for metadata update
5611 * which will now never happen */
5612 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005613
NeilBrown30b8feb2013-11-14 15:16:17 +11005614 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005615 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5616 !test_bit(MD_RECOVERY_RUNNING,
5617 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005618 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
NeilBrownc8c00a62009-08-10 12:50:52 +10005620 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005621 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005622 mddev->sysfs_active ||
5623 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005624 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005625 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005626 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005627 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005628 if (did_freeze) {
5629 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005630 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005631 md_wakeup_thread(mddev->thread);
5632 }
NeilBrown260fa032013-08-27 16:44:13 +10005633 return -EBUSY;
5634 }
NeilBrown6e17b022010-08-07 21:41:19 +10005635 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005636 if (mddev->ro)
5637 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005638
NeilBrowndefad612011-01-14 09:14:33 +11005639 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005640 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005641 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005642
NeilBrowna4bd82d2010-03-29 13:23:10 +11005643 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005644 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005645
NeilBrowndafb20f2012-03-19 12:46:39 +11005646 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005647 if (rdev->raid_disk >= 0)
5648 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005649
NeilBrowna4bd82d2010-03-29 13:23:10 +11005650 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005651 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005652 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005653 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005654
NeilBrowna4bd82d2010-03-29 13:23:10 +11005655 if (mddev->ro)
5656 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005657 } else
5658 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 /*
5660 * Free resources if final stop
5661 */
NeilBrown9e653b62006-06-26 00:27:58 -07005662 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5664
NeilBrown978f9462006-02-02 14:28:05 -08005665 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005666 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005667 struct file *f = mddev->bitmap_info.file;
5668 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005669 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005670 spin_unlock(&mddev->lock);
5671 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005672 }
NeilBrownc3d97142009-12-14 12:49:52 +11005673 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005674
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 export_array(mddev);
5676
NeilBrown6177b472010-03-29 11:37:13 +11005677 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005678 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005679 if (mddev->hold_active == UNTIL_STOP)
5680 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005681 }
NeilBrownd7603b72006-01-06 00:20:30 -08005682 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005683 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685}
5686
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005687#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005688static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689{
NeilBrown3cb03002011-10-11 16:45:26 +11005690 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 int err;
5692
NeilBrowna757e642005-04-16 15:26:42 -07005693 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
5696 printk(KERN_INFO "md: running: ");
5697
NeilBrowndafb20f2012-03-19 12:46:39 +11005698 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 char b[BDEVNAME_SIZE];
5700 printk("<%s>", bdevname(rdev->bdev,b));
5701 }
5702 printk("\n");
5703
NeilBrownd710e132008-10-13 11:55:12 +11005704 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 if (err) {
5706 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005707 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 }
5709}
5710
5711/*
5712 * lets try to run arrays based on all disks that have arrived
5713 * until now. (those are in pending_raid_disks)
5714 *
5715 * the method: pick the first pending disk, collect all disks with
5716 * the same UUID, remove all from the pending list and put them into
5717 * the 'same_array' list. Then order this list based on superblock
5718 * update time (freshest comes first), kick out 'old' disks and
5719 * compare superblocks. If everything's fine then run it.
5720 *
5721 * If "unit" is allocated, then bump its reference count
5722 */
5723static void autorun_devices(int part)
5724{
NeilBrown3cb03002011-10-11 16:45:26 +11005725 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005726 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 char b[BDEVNAME_SIZE];
5728
5729 printk(KERN_INFO "md: autorun ...\n");
5730 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005731 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005733 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005735 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736
5737 printk(KERN_INFO "md: considering %s ...\n",
5738 bdevname(rdev0->bdev,b));
5739 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005740 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 if (super_90_load(rdev, rdev0, 0) >= 0) {
5742 printk(KERN_INFO "md: adding %s ...\n",
5743 bdevname(rdev->bdev,b));
5744 list_move(&rdev->same_set, &candidates);
5745 }
5746 /*
5747 * now we have a set of devices, with all of them having
5748 * mostly sane superblocks. It's time to allocate the
5749 * mddev.
5750 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005751 if (part) {
5752 dev = MKDEV(mdp_major,
5753 rdev0->preferred_minor << MdpMinorShift);
5754 unit = MINOR(dev) >> MdpMinorShift;
5755 } else {
5756 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5757 unit = MINOR(dev);
5758 }
5759 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5761 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5762 break;
5763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
5765 md_probe(dev, NULL, NULL);
5766 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005767 if (!mddev || !mddev->gendisk) {
5768 if (mddev)
5769 mddev_put(mddev);
5770 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 "md: cannot allocate memory for md drive.\n");
5772 break;
5773 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005774 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 printk(KERN_WARNING "md: %s locked, cannot run\n",
5776 mdname(mddev));
5777 else if (mddev->raid_disks || mddev->major_version
5778 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005779 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 "md: %s already running, cannot run %s\n",
5781 mdname(mddev), bdevname(rdev0->bdev,b));
5782 mddev_unlock(mddev);
5783 } else {
5784 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005785 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005786 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 list_del_init(&rdev->same_set);
5788 if (bind_rdev_to_array(rdev, mddev))
5789 export_rdev(rdev);
5790 }
5791 autorun_array(mddev);
5792 mddev_unlock(mddev);
5793 }
5794 /* on success, candidates will be empty, on error
5795 * it won't...
5796 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005797 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005798 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 mddev_put(mddev);
5802 }
5803 printk(KERN_INFO "md: ... autorun DONE.\n");
5804}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005805#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
NeilBrownf72ffdd2014-09-30 14:23:59 +10005807static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808{
5809 mdu_version_t ver;
5810
5811 ver.major = MD_MAJOR_VERSION;
5812 ver.minor = MD_MINOR_VERSION;
5813 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5814
5815 if (copy_to_user(arg, &ver, sizeof(ver)))
5816 return -EFAULT;
5817
5818 return 0;
5819}
5820
NeilBrownf72ffdd2014-09-30 14:23:59 +10005821static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822{
5823 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005824 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005825 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
NeilBrown1ca69c42012-10-11 13:37:33 +11005827 nr = working = insync = failed = spare = 0;
5828 rcu_read_lock();
5829 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005831 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 failed++;
5833 else {
5834 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005835 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005836 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 else
5838 spare++;
5839 }
5840 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005841 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
5843 info.major_version = mddev->major_version;
5844 info.minor_version = mddev->minor_version;
5845 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005846 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005848 info.size = mddev->dev_sectors / 2;
5849 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005850 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 info.nr_disks = nr;
5852 info.raid_disks = mddev->raid_disks;
5853 info.md_minor = mddev->md_minor;
5854 info.not_persistent= !mddev->persistent;
5855
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005856 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 info.state = 0;
5858 if (mddev->in_sync)
5859 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005860 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005861 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005862 if (mddev_is_clustered(mddev))
5863 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005864 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 info.working_disks = working;
5866 info.failed_disks = failed;
5867 info.spare_disks = spare;
5868
5869 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005870 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
5872 if (copy_to_user(arg, &info, sizeof(info)))
5873 return -EFAULT;
5874
5875 return 0;
5876}
5877
NeilBrownf72ffdd2014-09-30 14:23:59 +10005878static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005879{
5880 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005881 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005882 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005883
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005884 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005885 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005886 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005887
NeilBrown32a76272005-06-21 17:17:14 -07005888 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005889 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005890 /* bitmap enabled */
5891 if (mddev->bitmap_info.file) {
5892 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5893 sizeof(file->pathname));
5894 if (IS_ERR(ptr))
5895 err = PTR_ERR(ptr);
5896 else
5897 memmove(file->pathname, ptr,
5898 sizeof(file->pathname)-(ptr-file->pathname));
5899 }
NeilBrown4af1a042014-12-15 12:57:00 +11005900 spin_unlock(&mddev->lock);
5901
5902 if (err == 0 &&
5903 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005904 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005905
NeilBrown32a76272005-06-21 17:17:14 -07005906 kfree(file);
5907 return err;
5908}
5909
NeilBrownf72ffdd2014-09-30 14:23:59 +10005910static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911{
5912 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005913 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
5915 if (copy_from_user(&info, arg, sizeof(info)))
5916 return -EFAULT;
5917
NeilBrown1ca69c42012-10-11 13:37:33 +11005918 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005919 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 if (rdev) {
5921 info.major = MAJOR(rdev->bdev->bd_dev);
5922 info.minor = MINOR(rdev->bdev->bd_dev);
5923 info.raid_disk = rdev->raid_disk;
5924 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005925 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005927 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 info.state |= (1<<MD_DISK_ACTIVE);
5929 info.state |= (1<<MD_DISK_SYNC);
5930 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005931 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005932 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005933 if (test_bit(WriteMostly, &rdev->flags))
5934 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 } else {
5936 info.major = info.minor = 0;
5937 info.raid_disk = -1;
5938 info.state = (1<<MD_DISK_REMOVED);
5939 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005940 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
5942 if (copy_to_user(arg, &info, sizeof(info)))
5943 return -EFAULT;
5944
5945 return 0;
5946}
5947
NeilBrownf72ffdd2014-09-30 14:23:59 +10005948static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949{
5950 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005951 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 dev_t dev = MKDEV(info->major,info->minor);
5953
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005954 if (mddev_is_clustered(mddev) &&
5955 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005956 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005957 mdname(mddev));
5958 return -EINVAL;
5959 }
5960
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5962 return -EOVERFLOW;
5963
5964 if (!mddev->raid_disks) {
5965 int err;
5966 /* expecting a device which has a superblock */
5967 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5968 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005969 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 "md: md_import_device returned %ld\n",
5971 PTR_ERR(rdev));
5972 return PTR_ERR(rdev);
5973 }
5974 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005975 struct md_rdev *rdev0
5976 = list_entry(mddev->disks.next,
5977 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005978 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 .load_super(rdev, rdev0, mddev->minor_version);
5980 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005981 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005983 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 bdevname(rdev0->bdev,b2));
5985 export_rdev(rdev);
5986 return -EINVAL;
5987 }
5988 }
5989 err = bind_rdev_to_array(rdev, mddev);
5990 if (err)
5991 export_rdev(rdev);
5992 return err;
5993 }
5994
5995 /*
5996 * add_new_disk can be used once the array is assembled
5997 * to add "hot spares". They must already have a superblock
5998 * written
5999 */
6000 if (mddev->pers) {
6001 int err;
6002 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006003 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 "%s: personality does not support diskops!\n",
6005 mdname(mddev));
6006 return -EINVAL;
6007 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006008 if (mddev->persistent)
6009 rdev = md_import_device(dev, mddev->major_version,
6010 mddev->minor_version);
6011 else
6012 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006014 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 "md: md_import_device returned %ld\n",
6016 PTR_ERR(rdev));
6017 return PTR_ERR(rdev);
6018 }
NeilBrown1a855a02010-12-09 16:36:28 +11006019 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006020 if (!mddev->persistent) {
6021 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006022 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006023 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006024 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006025 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006026 } else
NeilBrown41158c72005-06-21 17:17:25 -07006027 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006028 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006029 } else
6030 super_types[mddev->major_version].
6031 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006032 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006033 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006034 /* This was a hot-add request, but events doesn't
6035 * match, so reject it.
6036 */
6037 export_rdev(rdev);
6038 return -EINVAL;
6039 }
6040
NeilBrownb2d444d2005-11-08 21:39:31 -08006041 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006042 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6043 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006044 else
6045 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006046
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006047 if (info->state & (1<<MD_DISK_JOURNAL)) {
6048 struct md_rdev *rdev2;
6049 bool has_journal = false;
6050
6051 /* make sure no existing journal disk */
6052 rdev_for_each(rdev2, mddev) {
6053 if (test_bit(Journal, &rdev2->flags)) {
6054 has_journal = true;
6055 break;
6056 }
6057 }
6058 if (has_journal) {
6059 export_rdev(rdev);
6060 return -EBUSY;
6061 }
Song Liubac624f2015-08-13 14:31:55 -07006062 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006063 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006064 /*
6065 * check whether the device shows up in other nodes
6066 */
6067 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006068 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006069 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006070 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006071 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006072 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006073 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006074 export_rdev(rdev);
6075 return err;
6076 }
6077 }
6078 }
6079
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 rdev->raid_disk = -1;
6081 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006082
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 if (err)
6084 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006085
6086 if (mddev_is_clustered(mddev)) {
6087 if (info->state & (1 << MD_DISK_CANDIDATE))
6088 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6089 else {
6090 if (err)
6091 md_cluster_ops->add_new_disk_cancel(mddev);
6092 else
6093 err = add_bound_rdev(rdev);
6094 }
6095
6096 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006097 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006098
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 return err;
6100 }
6101
6102 /* otherwise, add_new_disk is only allowed
6103 * for major_version==0 superblocks
6104 */
6105 if (mddev->major_version != 0) {
6106 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6107 mdname(mddev));
6108 return -EINVAL;
6109 }
6110
6111 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6112 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006113 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006115 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 "md: error, md_import_device() returned %ld\n",
6117 PTR_ERR(rdev));
6118 return PTR_ERR(rdev);
6119 }
6120 rdev->desc_nr = info->number;
6121 if (info->raid_disk < mddev->raid_disks)
6122 rdev->raid_disk = info->raid_disk;
6123 else
6124 rdev->raid_disk = -1;
6125
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006127 if (info->state & (1<<MD_DISK_SYNC))
6128 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006130 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6131 set_bit(WriteMostly, &rdev->flags);
6132
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 if (!mddev->persistent) {
6134 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006135 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6136 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006137 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006138 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
NeilBrown2bf071b2006-01-06 00:20:55 -08006140 err = bind_rdev_to_array(rdev, mddev);
6141 if (err) {
6142 export_rdev(rdev);
6143 return err;
6144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 }
6146
6147 return 0;
6148}
6149
NeilBrownf72ffdd2014-09-30 14:23:59 +10006150static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151{
6152 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006153 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 rdev = find_rdev(mddev, dev);
6156 if (!rdev)
6157 return -ENXIO;
6158
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006159 if (rdev->raid_disk < 0)
6160 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006161
NeilBrown3ea8929d2013-04-24 11:42:41 +10006162 clear_bit(Blocked, &rdev->flags);
6163 remove_and_add_spares(mddev, rdev);
6164
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 if (rdev->raid_disk >= 0)
6166 goto busy;
6167
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006168kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006169 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006170 md_cluster_ops->remove_disk(mddev, rdev);
6171
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006172 md_kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07006173 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006174 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
6176 return 0;
6177busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006178 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 bdevname(rdev->bdev,b), mdname(mddev));
6180 return -EBUSY;
6181}
6182
NeilBrownf72ffdd2014-09-30 14:23:59 +10006183static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184{
6185 char b[BDEVNAME_SIZE];
6186 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006187 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188
6189 if (!mddev->pers)
6190 return -ENODEV;
6191
6192 if (mddev->major_version != 0) {
6193 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6194 " version-0 superblocks.\n",
6195 mdname(mddev));
6196 return -EINVAL;
6197 }
6198 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006199 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 "%s: personality does not support diskops!\n",
6201 mdname(mddev));
6202 return -EINVAL;
6203 }
6204
NeilBrownd710e132008-10-13 11:55:12 +11006205 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006207 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 "md: error, md_import_device() returned %ld\n",
6209 PTR_ERR(rdev));
6210 return -EINVAL;
6211 }
6212
6213 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006214 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006216 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
NeilBrown8190e752009-06-18 08:48:58 +10006218 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
NeilBrownb2d444d2005-11-08 21:39:31 -08006220 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006221 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 "md: can not hot-add faulty %s disk to %s!\n",
6223 bdevname(rdev->bdev,b), mdname(mddev));
6224 err = -EINVAL;
6225 goto abort_export;
6226 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006227
NeilBrownb2d444d2005-11-08 21:39:31 -08006228 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006230 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006231 err = bind_rdev_to_array(rdev, mddev);
6232 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006233 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
6235 /*
6236 * The rest should better be atomic, we can have disk failures
6237 * noticed in interrupt contexts ...
6238 */
6239
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 rdev->raid_disk = -1;
6241
NeilBrown850b2b422006-10-03 01:15:46 -07006242 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 /*
6244 * Kick recovery, maybe this spare has to be added to the
6245 * array immediately.
6246 */
6247 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6248 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006249 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 return 0;
6251
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252abort_export:
6253 export_rdev(rdev);
6254 return err;
6255}
6256
NeilBrownfd01b882011-10-11 16:47:53 +11006257static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006258{
NeilBrown035328c2014-04-09 12:25:40 +10006259 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006260
NeilBrown36fa3062005-09-09 16:23:45 -07006261 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006262 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006263 return -EBUSY;
6264 if (mddev->recovery || mddev->sync_thread)
6265 return -EBUSY;
6266 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006267 }
6268
NeilBrown36fa3062005-09-09 16:23:45 -07006269 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006270 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006271 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006272
NeilBrown1e594bb2014-12-15 12:57:00 +11006273 if (mddev->bitmap || mddev->bitmap_info.file)
6274 return -EEXIST; /* cannot add when bitmap is present */
6275 f = fget(fd);
6276
6277 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006278 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6279 mdname(mddev));
6280 return -EBADF;
6281 }
6282
NeilBrown1e594bb2014-12-15 12:57:00 +11006283 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006284 if (!S_ISREG(inode->i_mode)) {
6285 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6286 mdname(mddev));
6287 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006288 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006289 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6290 mdname(mddev));
6291 err = -EBADF;
6292 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006293 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6294 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006295 err = -EBUSY;
6296 }
6297 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006298 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006299 return err;
6300 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006301 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006302 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006303 } else if (mddev->bitmap == NULL)
6304 return -ENOENT; /* cannot remove what isn't there */
6305 err = 0;
6306 if (mddev->pers) {
6307 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006308 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006309 struct bitmap *bitmap;
6310
6311 bitmap = bitmap_create(mddev, -1);
6312 if (!IS_ERR(bitmap)) {
6313 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006314 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006315 } else
6316 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006317 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006318 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006319 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006320 fd = -1; /* make sure to put the file */
6321 }
NeilBrown36fa3062005-09-09 16:23:45 -07006322 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006323 }
6324 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006325 struct file *f = mddev->bitmap_info.file;
6326 if (f) {
6327 spin_lock(&mddev->lock);
6328 mddev->bitmap_info.file = NULL;
6329 spin_unlock(&mddev->lock);
6330 fput(f);
6331 }
NeilBrown36fa3062005-09-09 16:23:45 -07006332 }
6333
NeilBrown32a76272005-06-21 17:17:14 -07006334 return err;
6335}
6336
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337/*
6338 * set_array_info is used two different ways
6339 * The original usage is when creating a new array.
6340 * In this usage, raid_disks is > 0 and it together with
6341 * level, size, not_persistent,layout,chunksize determine the
6342 * shape of the array.
6343 * This will always create an array with a type-0.90.0 superblock.
6344 * The newer usage is when assembling an array.
6345 * In this case raid_disks will be 0, and the major_version field is
6346 * use to determine which style super-blocks are to be found on the devices.
6347 * The minor and patch _version numbers are also kept incase the
6348 * super_block handler wishes to interpret them.
6349 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006350static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351{
6352
6353 if (info->raid_disks == 0) {
6354 /* just setting version number for superblock loading */
6355 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006356 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 super_types[info->major_version].name == NULL) {
6358 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006359 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 "md: superblock version %d not known\n",
6361 info->major_version);
6362 return -EINVAL;
6363 }
6364 mddev->major_version = info->major_version;
6365 mddev->minor_version = info->minor_version;
6366 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006367 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006368 /* ensure mddev_put doesn't delete this now that there
6369 * is some minimal configuration.
6370 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006371 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 return 0;
6373 }
6374 mddev->major_version = MD_MAJOR_VERSION;
6375 mddev->minor_version = MD_MINOR_VERSION;
6376 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006377 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
6379 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006380 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006381 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 mddev->raid_disks = info->raid_disks;
6383 /* don't set md_minor, it is determined by which /dev/md* was
6384 * openned
6385 */
6386 if (info->state & (1<<MD_SB_CLEAN))
6387 mddev->recovery_cp = MaxSector;
6388 else
6389 mddev->recovery_cp = 0;
6390 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006391 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
6393 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006394 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
6396 mddev->max_disks = MD_SB_DISKS;
6397
NeilBrowne6910632008-02-06 01:39:51 -08006398 if (mddev->persistent)
6399 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006400 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401
NeilBrownc3d97142009-12-14 12:49:52 +11006402 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006403 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006404 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006405
NeilBrownf6705572006-03-27 01:18:11 -08006406 mddev->reshape_position = MaxSector;
6407
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 /*
6409 * Generate a 128 bit UUID
6410 */
6411 get_random_bytes(mddev->uuid, 16);
6412
NeilBrownf6705572006-03-27 01:18:11 -08006413 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006414 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006415 mddev->new_layout = mddev->layout;
6416 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006417 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006418
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 return 0;
6420}
6421
NeilBrownfd01b882011-10-11 16:47:53 +11006422void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006423{
Dan Williamsb522adc2009-03-31 15:00:31 +11006424 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6425
6426 if (mddev->external_size)
6427 return;
6428
Dan Williams1f403622009-03-31 14:59:03 +11006429 mddev->array_sectors = array_sectors;
6430}
6431EXPORT_SYMBOL(md_set_array_sectors);
6432
NeilBrownfd01b882011-10-11 16:47:53 +11006433static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006434{
NeilBrown3cb03002011-10-11 16:45:26 +11006435 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006436 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006437 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006438
6439 if (mddev->pers->resize == NULL)
6440 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006441 /* The "num_sectors" is the number of sectors of each device that
6442 * is used. This can only make sense for arrays with redundancy.
6443 * linear and raid0 always use whatever space is available. We can only
6444 * consider changing this number if no resync or reconstruction is
6445 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006446 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006447 * of each device. If num_sectors is zero, we find the largest size
6448 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006449 */
NeilBrownf851b602014-12-11 10:02:10 +11006450 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6451 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006452 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006453 if (mddev->ro)
6454 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006455
NeilBrowndafb20f2012-03-19 12:46:39 +11006456 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006457 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006458
Andre Nolld71f9f82008-07-11 22:02:22 +10006459 if (fit && (num_sectors == 0 || num_sectors > avail))
6460 num_sectors = avail;
6461 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006462 return -ENOSPC;
6463 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006464 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006465 if (!rv)
6466 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006467 return rv;
6468}
6469
NeilBrownfd01b882011-10-11 16:47:53 +11006470static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006471{
6472 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006473 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006474 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006475 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006476 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006477 if (mddev->ro)
6478 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006479 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006480 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006481 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006482 if (mddev->sync_thread ||
6483 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6484 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006485 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006486
6487 rdev_for_each(rdev, mddev) {
6488 if (mddev->raid_disks < raid_disks &&
6489 rdev->data_offset < rdev->new_data_offset)
6490 return -EINVAL;
6491 if (mddev->raid_disks > raid_disks &&
6492 rdev->data_offset > rdev->new_data_offset)
6493 return -EINVAL;
6494 }
6495
NeilBrown63c70c42006-03-27 01:18:13 -08006496 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006497 if (mddev->delta_disks < 0)
6498 mddev->reshape_backwards = 1;
6499 else if (mddev->delta_disks > 0)
6500 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006501
6502 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006503 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006504 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006505 mddev->reshape_backwards = 0;
6506 }
NeilBrownda943b992006-01-06 00:20:54 -08006507 return rv;
6508}
6509
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510/*
6511 * update_array_info is used to change the configuration of an
6512 * on-line array.
6513 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6514 * fields in the info are checked against the array.
6515 * Any differences that cannot be handled will cause an error.
6516 * Normally, only one change can be managed at a time.
6517 */
NeilBrownfd01b882011-10-11 16:47:53 +11006518static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519{
6520 int rv = 0;
6521 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006522 int state = 0;
6523
6524 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006525 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006526 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527
6528 if (mddev->major_version != info->major_version ||
6529 mddev->minor_version != info->minor_version ||
6530/* mddev->patch_version != info->patch_version || */
6531 mddev->ctime != info->ctime ||
6532 mddev->level != info->level ||
6533/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006534 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006535 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006536 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6537 ((state^info->state) & 0xfffffe00)
6538 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 return -EINVAL;
6540 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006541 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6542 cnt++;
6543 if (mddev->raid_disks != info->raid_disks)
6544 cnt++;
6545 if (mddev->layout != info->layout)
6546 cnt++;
6547 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6548 cnt++;
6549 if (cnt == 0)
6550 return 0;
6551 if (cnt > 1)
6552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553
6554 if (mddev->layout != info->layout) {
6555 /* Change layout
6556 * we don't need to do anything at the md level, the
6557 * personality will take care of it all.
6558 */
NeilBrown50ac1682009-06-18 08:47:55 +10006559 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006561 else {
6562 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006563 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006564 if (rv)
6565 mddev->new_layout = mddev->layout;
6566 return rv;
6567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006569 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006570 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571
NeilBrownda943b992006-01-06 00:20:54 -08006572 if (mddev->raid_disks != info->raid_disks)
6573 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574
NeilBrown36fa3062005-09-09 16:23:45 -07006575 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006576 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6577 rv = -EINVAL;
6578 goto err;
6579 }
6580 if (mddev->recovery || mddev->sync_thread) {
6581 rv = -EBUSY;
6582 goto err;
6583 }
NeilBrown36fa3062005-09-09 16:23:45 -07006584 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006585 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006586 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006587 if (mddev->bitmap) {
6588 rv = -EEXIST;
6589 goto err;
6590 }
6591 if (mddev->bitmap_info.default_offset == 0) {
6592 rv = -EINVAL;
6593 goto err;
6594 }
NeilBrownc3d97142009-12-14 12:49:52 +11006595 mddev->bitmap_info.offset =
6596 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006597 mddev->bitmap_info.space =
6598 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006599 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006600 bitmap = bitmap_create(mddev, -1);
6601 if (!IS_ERR(bitmap)) {
6602 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006603 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006604 } else
6605 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006606 if (rv)
6607 bitmap_destroy(mddev);
6608 mddev->pers->quiesce(mddev, 0);
6609 } else {
6610 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006611 if (!mddev->bitmap) {
6612 rv = -ENOENT;
6613 goto err;
6614 }
6615 if (mddev->bitmap->storage.file) {
6616 rv = -EINVAL;
6617 goto err;
6618 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006619 if (mddev->bitmap_info.nodes) {
6620 /* hold PW on all the bitmap lock */
6621 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6622 printk("md: can't change bitmap to none since the"
6623 " array is in use by more than one node\n");
6624 rv = -EPERM;
6625 md_cluster_ops->unlock_all_bitmaps(mddev);
6626 goto err;
6627 }
6628
6629 mddev->bitmap_info.nodes = 0;
6630 md_cluster_ops->leave(mddev);
6631 }
NeilBrown36fa3062005-09-09 16:23:45 -07006632 mddev->pers->quiesce(mddev, 1);
6633 bitmap_destroy(mddev);
6634 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006635 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006636 }
6637 }
NeilBrown850b2b422006-10-03 01:15:46 -07006638 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006639 return rv;
6640err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 return rv;
6642}
6643
NeilBrownfd01b882011-10-11 16:47:53 +11006644static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645{
NeilBrown3cb03002011-10-11 16:45:26 +11006646 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006647 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648
6649 if (mddev->pers == NULL)
6650 return -ENODEV;
6651
NeilBrown1ca69c42012-10-11 13:37:33 +11006652 rcu_read_lock();
6653 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006655 err = -ENODEV;
6656 else {
6657 md_error(mddev, rdev);
6658 if (!test_bit(Faulty, &rdev->flags))
6659 err = -EBUSY;
6660 }
6661 rcu_read_unlock();
6662 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663}
6664
Andre Noll2f9618c2008-04-25 18:57:58 +02006665/*
6666 * We have a problem here : there is no easy way to give a CHS
6667 * virtual geometry. We currently pretend that we have a 2 heads
6668 * 4 sectors (with a BIG number of cylinders...). This drives
6669 * dosfs just mad... ;-)
6670 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006671static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6672{
NeilBrownfd01b882011-10-11 16:47:53 +11006673 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006674
6675 geo->heads = 2;
6676 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006677 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006678 return 0;
6679}
6680
Nicolas Schichancb335f82014-01-15 16:58:52 +01006681static inline bool md_ioctl_valid(unsigned int cmd)
6682{
6683 switch (cmd) {
6684 case ADD_NEW_DISK:
6685 case BLKROSET:
6686 case GET_ARRAY_INFO:
6687 case GET_BITMAP_FILE:
6688 case GET_DISK_INFO:
6689 case HOT_ADD_DISK:
6690 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006691 case RAID_AUTORUN:
6692 case RAID_VERSION:
6693 case RESTART_ARRAY_RW:
6694 case RUN_ARRAY:
6695 case SET_ARRAY_INFO:
6696 case SET_BITMAP_FILE:
6697 case SET_DISK_FAULTY:
6698 case STOP_ARRAY:
6699 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006700 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006701 return true;
6702 default:
6703 return false;
6704 }
6705}
6706
Al Viroa39907f2008-03-02 10:31:15 -05006707static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 unsigned int cmd, unsigned long arg)
6709{
6710 int err = 0;
6711 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006712 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006713 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
Nicolas Schichancb335f82014-01-15 16:58:52 +01006715 if (!md_ioctl_valid(cmd))
6716 return -ENOTTY;
6717
NeilBrown506c9e42011-12-23 10:17:26 +11006718 switch (cmd) {
6719 case RAID_VERSION:
6720 case GET_ARRAY_INFO:
6721 case GET_DISK_INFO:
6722 break;
6723 default:
6724 if (!capable(CAP_SYS_ADMIN))
6725 return -EACCES;
6726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727
6728 /*
6729 * Commands dealing with the RAID driver but not any
6730 * particular array:
6731 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006732 switch (cmd) {
6733 case RAID_VERSION:
6734 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006735 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006738 case RAID_AUTORUN:
6739 err = 0;
6740 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006741 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006743 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 }
6745
6746 /*
6747 * Commands creating/starting a new array:
6748 */
6749
Al Viroa39907f2008-03-02 10:31:15 -05006750 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751
6752 if (!mddev) {
6753 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006754 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 }
6756
NeilBrown1ca69c42012-10-11 13:37:33 +11006757 /* Some actions do not requires the mutex */
6758 switch (cmd) {
6759 case GET_ARRAY_INFO:
6760 if (!mddev->raid_disks && !mddev->external)
6761 err = -ENODEV;
6762 else
6763 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006764 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006765
6766 case GET_DISK_INFO:
6767 if (!mddev->raid_disks && !mddev->external)
6768 err = -ENODEV;
6769 else
6770 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006771 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006772
6773 case SET_DISK_FAULTY:
6774 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006775 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006776
6777 case GET_BITMAP_FILE:
6778 err = get_bitmap_file(mddev, argp);
6779 goto out;
6780
NeilBrown1ca69c42012-10-11 13:37:33 +11006781 }
6782
NeilBrowna7a3f082012-12-11 13:35:54 +11006783 if (cmd == ADD_NEW_DISK)
6784 /* need to ensure md_delayed_delete() has completed */
6785 flush_workqueue(md_misc_wq);
6786
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006787 if (cmd == HOT_REMOVE_DISK)
6788 /* need to ensure recovery thread has run */
6789 wait_event_interruptible_timeout(mddev->sb_wait,
6790 !test_bit(MD_RECOVERY_NEEDED,
6791 &mddev->flags),
6792 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006793 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6794 /* Need to flush page cache, and ensure no-one else opens
6795 * and writes
6796 */
6797 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006798 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006799 mutex_unlock(&mddev->open_mutex);
6800 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006801 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006802 }
6803 set_bit(MD_STILL_CLOSED, &mddev->flags);
6804 mutex_unlock(&mddev->open_mutex);
6805 sync_blockdev(bdev);
6806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 err = mddev_lock(mddev);
6808 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006809 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6811 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006812 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 }
6814
NeilBrownc02c0ae2012-12-11 13:39:21 +11006815 if (cmd == SET_ARRAY_INFO) {
6816 mdu_array_info_t info;
6817 if (!arg)
6818 memset(&info, 0, sizeof(info));
6819 else if (copy_from_user(&info, argp, sizeof(info))) {
6820 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006821 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006822 }
6823 if (mddev->pers) {
6824 err = update_array_info(mddev, &info);
6825 if (err) {
6826 printk(KERN_WARNING "md: couldn't update"
6827 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006828 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006830 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006831 }
6832 if (!list_empty(&mddev->disks)) {
6833 printk(KERN_WARNING
6834 "md: array %s already has disks!\n",
6835 mdname(mddev));
6836 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006837 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006838 }
6839 if (mddev->raid_disks) {
6840 printk(KERN_WARNING
6841 "md: array %s already initialised!\n",
6842 mdname(mddev));
6843 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006844 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006845 }
6846 err = set_array_info(mddev, &info);
6847 if (err) {
6848 printk(KERN_WARNING "md: couldn't set"
6849 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006850 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006851 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006852 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 }
6854
6855 /*
6856 * Commands querying/configuring an existing array:
6857 */
NeilBrown32a76272005-06-21 17:17:14 -07006858 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006859 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006860 if ((!mddev->raid_disks && !mddev->external)
6861 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6862 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6863 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006865 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 }
6867
6868 /*
6869 * Commands even a read-only array can execute:
6870 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006871 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006872 case RESTART_ARRAY_RW:
6873 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006874 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006875
6876 case STOP_ARRAY:
6877 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006878 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006879
6880 case STOP_ARRAY_RO:
6881 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006882 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006883
NeilBrown3ea8929d2013-04-24 11:42:41 +10006884 case HOT_REMOVE_DISK:
6885 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006886 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006887
NeilBrown7ceb17e2013-04-24 11:42:42 +10006888 case ADD_NEW_DISK:
6889 /* We can support ADD_NEW_DISK on read-only arrays
6890 * on if we are re-adding a preexisting device.
6891 * So require mddev->pers and MD_DISK_SYNC.
6892 */
6893 if (mddev->pers) {
6894 mdu_disk_info_t info;
6895 if (copy_from_user(&info, argp, sizeof(info)))
6896 err = -EFAULT;
6897 else if (!(info.state & (1<<MD_DISK_SYNC)))
6898 /* Need to clear read-only for this */
6899 break;
6900 else
6901 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006902 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006903 }
6904 break;
6905
NeilBrownc02c0ae2012-12-11 13:39:21 +11006906 case BLKROSET:
6907 if (get_user(ro, (int __user *)(arg))) {
6908 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006909 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006910 }
6911 err = -EINVAL;
6912
6913 /* if the bdev is going readonly the value of mddev->ro
6914 * does not matter, no writes are coming
6915 */
6916 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006917 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006918
NeilBrownc02c0ae2012-12-11 13:39:21 +11006919 /* are we are already prepared for writes? */
6920 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006921 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006922
6923 /* transitioning to readauto need only happen for
6924 * arrays that call md_write_start
6925 */
6926 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006928 if (err == 0) {
6929 mddev->ro = 2;
6930 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006931 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006932 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006933 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 }
6935
6936 /*
6937 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006938 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 */
NeilBrown326eb172014-09-30 15:36:28 +10006940 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006941 if (mddev->ro == 2) {
6942 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006943 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006944 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006945 /* mddev_unlock will wake thread */
6946 /* If a device failed while we were read-only, we
6947 * need to make sure the metadata is updated now.
6948 */
6949 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6950 mddev_unlock(mddev);
6951 wait_event(mddev->sb_wait,
6952 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6953 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006954 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006955 }
NeilBrownf91de922005-11-08 21:39:36 -08006956 } else {
6957 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006958 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 }
6961
NeilBrownc02c0ae2012-12-11 13:39:21 +11006962 switch (cmd) {
6963 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006965 mdu_disk_info_t info;
6966 if (copy_from_user(&info, argp, sizeof(info)))
6967 err = -EFAULT;
6968 else
6969 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006970 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006973 case CLUSTERED_DISK_NACK:
6974 if (mddev_is_clustered(mddev))
6975 md_cluster_ops->new_disk_ack(mddev, false);
6976 else
6977 err = -EINVAL;
6978 goto unlock;
6979
NeilBrownc02c0ae2012-12-11 13:39:21 +11006980 case HOT_ADD_DISK:
6981 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006982 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983
NeilBrownc02c0ae2012-12-11 13:39:21 +11006984 case RUN_ARRAY:
6985 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006986 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
NeilBrownc02c0ae2012-12-11 13:39:21 +11006988 case SET_BITMAP_FILE:
6989 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006990 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006991
NeilBrownc02c0ae2012-12-11 13:39:21 +11006992 default:
6993 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006994 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 }
6996
NeilBrown3adc28d2014-09-30 15:46:41 +10006997unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006998 if (mddev->hold_active == UNTIL_IOCTL &&
6999 err != -EINVAL)
7000 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007002out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 return err;
7004}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007005#ifdef CONFIG_COMPAT
7006static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7007 unsigned int cmd, unsigned long arg)
7008{
7009 switch (cmd) {
7010 case HOT_REMOVE_DISK:
7011 case HOT_ADD_DISK:
7012 case SET_DISK_FAULTY:
7013 case SET_BITMAP_FILE:
7014 /* These take in integer arg, do not convert */
7015 break;
7016 default:
7017 arg = (unsigned long)compat_ptr(arg);
7018 break;
7019 }
7020
7021 return md_ioctl(bdev, mode, cmd, arg);
7022}
7023#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
Al Viroa39907f2008-03-02 10:31:15 -05007025static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026{
7027 /*
7028 * Succeed if we can lock the mddev, which confirms that
7029 * it isn't being stopped right now.
7030 */
NeilBrownfd01b882011-10-11 16:47:53 +11007031 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 int err;
7033
Yuanhan Liu0c098222012-05-22 13:55:32 +10007034 if (!mddev)
7035 return -ENODEV;
7036
NeilBrownd3374822009-01-09 08:31:10 +11007037 if (mddev->gendisk != bdev->bd_disk) {
7038 /* we are racing with mddev_put which is discarding this
7039 * bd_disk.
7040 */
7041 mddev_put(mddev);
7042 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007043 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007044 /* Then retry the open from the top */
7045 return -ERESTARTSYS;
7046 }
7047 BUG_ON(mddev != bdev->bd_disk->private_data);
7048
NeilBrownc8c00a62009-08-10 12:50:52 +10007049 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050 goto out;
7051
7052 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007053 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10007054 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10007055 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007057 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 out:
7059 return err;
7060}
7061
Al Virodb2a1442013-05-05 21:52:57 -04007062static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007064 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007066 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007067 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007070
7071static int md_media_changed(struct gendisk *disk)
7072{
NeilBrownfd01b882011-10-11 16:47:53 +11007073 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007074
7075 return mddev->changed;
7076}
7077
7078static int md_revalidate(struct gendisk *disk)
7079{
NeilBrownfd01b882011-10-11 16:47:53 +11007080 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007081
7082 mddev->changed = 0;
7083 return 0;
7084}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007085static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086{
7087 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007088 .open = md_open,
7089 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007090 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007091#ifdef CONFIG_COMPAT
7092 .compat_ioctl = md_compat_ioctl,
7093#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007094 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007095 .media_changed = md_media_changed,
7096 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097};
7098
NeilBrownf72ffdd2014-09-30 14:23:59 +10007099static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100{
NeilBrown2b8bf342011-10-11 16:48:23 +11007101 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 /*
7104 * md_thread is a 'system-thread', it's priority should be very
7105 * high. We avoid resource deadlocks individually in each
7106 * raid personality. (RAID5 does preallocation) We also use RR and
7107 * the very same RT priority as kswapd, thus we will never get
7108 * into a priority inversion deadlock.
7109 *
7110 * we definitely have to have equal or higher priority than
7111 * bdflush, otherwise bdflush will deadlock if there are too
7112 * many dirty RAID5 blocks.
7113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114
NeilBrown6985c432005-10-19 21:23:47 -07007115 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007116 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117
NeilBrown93588e22005-11-15 00:09:12 -08007118 /* We need to wait INTERRUPTIBLE so that
7119 * we don't add to the load-average.
7120 * That means we need to be sure no signals are
7121 * pending
7122 */
7123 if (signal_pending(current))
7124 flush_signals(current);
7125
7126 wait_event_interruptible_timeout
7127 (thread->wqueue,
7128 test_bit(THREAD_WAKEUP, &thread->flags)
7129 || kthread_should_stop(),
7130 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131
NeilBrown6c987912011-01-14 09:13:53 +11007132 clear_bit(THREAD_WAKEUP, &thread->flags);
7133 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007134 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007136
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 return 0;
7138}
7139
NeilBrown2b8bf342011-10-11 16:48:23 +11007140void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141{
7142 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007143 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 set_bit(THREAD_WAKEUP, &thread->flags);
7145 wake_up(&thread->wqueue);
7146 }
7147}
NeilBrown6c144d32014-09-30 16:15:38 +10007148EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Shaohua Li4ed87312012-10-11 13:34:00 +11007150struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7151 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152{
NeilBrown2b8bf342011-10-11 16:48:23 +11007153 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
NeilBrown2b8bf342011-10-11 16:48:23 +11007155 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 if (!thread)
7157 return NULL;
7158
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 init_waitqueue_head(&thread->wqueue);
7160
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 thread->run = run;
7162 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007163 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007164 thread->tsk = kthread_run(md_thread, thread,
7165 "%s_%s",
7166 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007167 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007168 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 kfree(thread);
7170 return NULL;
7171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 return thread;
7173}
NeilBrown6c144d32014-09-30 16:15:38 +10007174EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175
NeilBrown2b8bf342011-10-11 16:48:23 +11007176void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177{
NeilBrown2b8bf342011-10-11 16:48:23 +11007178 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007179 if (!thread)
7180 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007181 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007182 /* Locking ensures that mddev_unlock does not wake_up a
7183 * non-existent thread
7184 */
7185 spin_lock(&pers_lock);
7186 *threadp = NULL;
7187 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007188
7189 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 kfree(thread);
7191}
NeilBrown6c144d32014-09-30 16:15:38 +10007192EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193
NeilBrownfd01b882011-10-11 16:47:53 +11007194void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195{
NeilBrownb2d444d2005-11-08 21:39:31 -08007196 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007198
NeilBrownde393cd2011-07-28 11:31:48 +10007199 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 return;
7201 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007202 if (mddev->degraded)
7203 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007204 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7206 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7207 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007208 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007209 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007210 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211}
NeilBrown6c144d32014-09-30 16:15:38 +10007212EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213
7214/* seq_file implementation /proc/mdstat */
7215
7216static void status_unused(struct seq_file *seq)
7217{
7218 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007219 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220
7221 seq_printf(seq, "unused devices: ");
7222
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007223 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 char b[BDEVNAME_SIZE];
7225 i++;
7226 seq_printf(seq, "%s ",
7227 bdevname(rdev->bdev,b));
7228 }
7229 if (!i)
7230 seq_printf(seq, "<none>");
7231
7232 seq_printf(seq, "\n");
7233}
7234
NeilBrownf7851be2015-07-02 17:12:58 +10007235static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236{
NeilBrowndd71cf62009-05-07 12:49:35 +10007237 sector_t max_sectors, resync, res;
7238 unsigned long dt, db;
7239 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007240 int scale;
7241 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242
NeilBrownc804cde2012-05-21 09:28:33 +10007243 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7244 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007245 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007247 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
NeilBrownf7851be2015-07-02 17:12:58 +10007249 resync = mddev->curr_resync;
7250 if (resync <= 3) {
7251 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7252 /* Still cleaning up */
7253 resync = max_sectors;
7254 } else
7255 resync -= atomic_read(&mddev->recovery_active);
7256
7257 if (resync == 0) {
7258 if (mddev->recovery_cp < MaxSector) {
7259 seq_printf(seq, "\tresync=PENDING");
7260 return 1;
7261 }
7262 return 0;
7263 }
7264 if (resync < 3) {
7265 seq_printf(seq, "\tresync=DELAYED");
7266 return 1;
7267 }
7268
NeilBrown403df472014-09-30 15:52:29 +10007269 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007270 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007271 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007272 * u32, as those are the requirements for sector_div.
7273 * Thus 'scale' must be at least 10
7274 */
7275 scale = 10;
7276 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007277 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007278 scale++;
7279 }
7280 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007281 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007282
7283 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 {
NeilBrown4588b422006-03-27 01:18:04 -08007285 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 seq_printf(seq, "[");
7287 for (i = 0; i < x; i++)
7288 seq_printf(seq, "=");
7289 seq_printf(seq, ">");
7290 for (i = 0; i < y; i++)
7291 seq_printf(seq, ".");
7292 seq_printf(seq, "] ");
7293 }
NeilBrown4588b422006-03-27 01:18:04 -08007294 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007295 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7296 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007297 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7298 "check" :
7299 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7300 "resync" : "recovery"))),
7301 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007302 (unsigned long long) resync/2,
7303 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304
7305 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 * dt: time from mark until now
7307 * db: blocks written from mark until now
7308 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007309 *
7310 * rt is a sector_t, so could be 32bit or 64bit.
7311 * So we divide before multiply in case it is 32bit and close
7312 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007313 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007314 * near the end of resync when the number of remaining sectors
7315 * is close to 'db'.
7316 * We then divide rt by 32 after multiplying by db to compensate.
7317 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 */
7319 dt = ((jiffies - mddev->resync_mark) / HZ);
7320 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007321 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7322 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323
NeilBrowndd71cf62009-05-07 12:49:35 +10007324 rt = max_sectors - resync; /* number of remaining sectors */
7325 sector_div(rt, db/32+1);
7326 rt *= dt;
7327 rt >>= 5;
7328
7329 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7330 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331
NeilBrownff4e8d92006-07-10 04:44:16 -07007332 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007333 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334}
7335
7336static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7337{
7338 struct list_head *tmp;
7339 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007340 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341
7342 if (l >= 0x10000)
7343 return NULL;
7344 if (!l--)
7345 /* header */
7346 return (void*)1;
7347
7348 spin_lock(&all_mddevs_lock);
7349 list_for_each(tmp,&all_mddevs)
7350 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007351 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 mddev_get(mddev);
7353 spin_unlock(&all_mddevs_lock);
7354 return mddev;
7355 }
7356 spin_unlock(&all_mddevs_lock);
7357 if (!l--)
7358 return (void*)2;/* tail */
7359 return NULL;
7360}
7361
7362static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7363{
7364 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007365 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007366
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 ++*pos;
7368 if (v == (void*)2)
7369 return NULL;
7370
7371 spin_lock(&all_mddevs_lock);
7372 if (v == (void*)1)
7373 tmp = all_mddevs.next;
7374 else
7375 tmp = mddev->all_mddevs.next;
7376 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007377 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 else {
7379 next_mddev = (void*)2;
7380 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 spin_unlock(&all_mddevs_lock);
7383
7384 if (v != (void*)1)
7385 mddev_put(mddev);
7386 return next_mddev;
7387
7388}
7389
7390static void md_seq_stop(struct seq_file *seq, void *v)
7391{
NeilBrownfd01b882011-10-11 16:47:53 +11007392 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393
7394 if (mddev && v != (void*)1 && v != (void*)2)
7395 mddev_put(mddev);
7396}
7397
7398static int md_seq_show(struct seq_file *seq, void *v)
7399{
NeilBrownfd01b882011-10-11 16:47:53 +11007400 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007401 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007402 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403
7404 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007405 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 seq_printf(seq, "Personalities : ");
7407 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007408 list_for_each_entry(pers, &pers_list, list)
7409 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410
7411 spin_unlock(&pers_lock);
7412 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007413 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 return 0;
7415 }
7416 if (v == (void*)2) {
7417 status_unused(seq);
7418 return 0;
7419 }
7420
NeilBrown36d091f2014-12-15 12:56:58 +11007421 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7423 seq_printf(seq, "%s : %sactive", mdname(mddev),
7424 mddev->pers ? "" : "in");
7425 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007426 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007428 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007429 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 seq_printf(seq, " %s", mddev->pers->name);
7431 }
7432
Andre Nolldd8ac332009-03-31 14:33:13 +11007433 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007434 rcu_read_lock();
7435 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 char b[BDEVNAME_SIZE];
7437 seq_printf(seq, " %s[%d]",
7438 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007439 if (test_bit(WriteMostly, &rdev->flags))
7440 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007441 if (test_bit(Journal, &rdev->flags))
7442 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007443 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 seq_printf(seq, "(F)");
7445 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007446 }
7447 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007448 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007449 if (test_bit(Replacement, &rdev->flags))
7450 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007451 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007453 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454
7455 if (!list_empty(&mddev->disks)) {
7456 if (mddev->pers)
7457 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007458 (unsigned long long)
7459 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 else
7461 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007462 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007464 if (mddev->persistent) {
7465 if (mddev->major_version != 0 ||
7466 mddev->minor_version != 90) {
7467 seq_printf(seq," super %d.%d",
7468 mddev->major_version,
7469 mddev->minor_version);
7470 }
NeilBrowne6910632008-02-06 01:39:51 -08007471 } else if (mddev->external)
7472 seq_printf(seq, " super external:%s",
7473 mddev->metadata_type);
7474 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007475 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476
7477 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007478 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007479 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007480 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007481 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007482 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007483 }
NeilBrown32a76272005-06-21 17:17:14 -07007484 } else
7485 seq_printf(seq, "\n ");
7486
NeilBrown57148962012-03-19 12:46:40 +11007487 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488
7489 seq_printf(seq, "\n");
7490 }
NeilBrown36d091f2014-12-15 12:56:58 +11007491 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007492
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 return 0;
7494}
7495
Jan Engelhardt110518b2009-05-07 12:49:37 +10007496static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 .start = md_seq_start,
7498 .next = md_seq_next,
7499 .stop = md_seq_stop,
7500 .show = md_seq_show,
7501};
7502
7503static int md_seq_open(struct inode *inode, struct file *file)
7504{
Kay Sieversf1514632011-07-12 20:48:39 +02007505 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506 int error;
7507
7508 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007509 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007510 return error;
7511
7512 seq = file->private_data;
7513 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 return error;
7515}
7516
NeilBrowne2f23b62014-04-09 14:33:51 +10007517static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007518static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7519{
Kay Sieversf1514632011-07-12 20:48:39 +02007520 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007521 int mask;
7522
NeilBrowne2f23b62014-04-09 14:33:51 +10007523 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007524 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007525 poll_wait(filp, &md_event_waiters, wait);
7526
7527 /* always allow read */
7528 mask = POLLIN | POLLRDNORM;
7529
Kay Sieversf1514632011-07-12 20:48:39 +02007530 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007531 mask |= POLLERR | POLLPRI;
7532 return mask;
7533}
7534
Arjan van de Venfa027c22007-02-12 00:55:33 -08007535static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007536 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 .open = md_seq_open,
7538 .read = seq_read,
7539 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007540 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007541 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542};
7543
NeilBrown84fc4b52011-10-11 16:49:58 +11007544int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545{
Chao Yu50bd3772014-09-25 15:28:34 +08007546 printk(KERN_INFO "md: %s personality registered for level %d\n",
7547 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007549 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 spin_unlock(&pers_lock);
7551 return 0;
7552}
NeilBrown6c144d32014-09-30 16:15:38 +10007553EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554
NeilBrown84fc4b52011-10-11 16:49:58 +11007555int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556{
NeilBrown2604b702006-01-06 00:20:36 -08007557 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007559 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 spin_unlock(&pers_lock);
7561 return 0;
7562}
NeilBrown6c144d32014-09-30 16:15:38 +10007563EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564
NeilBrown6022e752015-08-13 12:32:55 +10007565int register_md_cluster_operations(struct md_cluster_operations *ops,
7566 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007567{
NeilBrown6022e752015-08-13 12:32:55 +10007568 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007569 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007570 if (md_cluster_ops != NULL)
7571 ret = -EALREADY;
7572 else {
7573 md_cluster_ops = ops;
7574 md_cluster_mod = module;
7575 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007576 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007577 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007578}
7579EXPORT_SYMBOL(register_md_cluster_operations);
7580
7581int unregister_md_cluster_operations(void)
7582{
7583 spin_lock(&pers_lock);
7584 md_cluster_ops = NULL;
7585 spin_unlock(&pers_lock);
7586 return 0;
7587}
7588EXPORT_SYMBOL(unregister_md_cluster_operations);
7589
7590int md_setup_cluster(struct mddev *mddev, int nodes)
7591{
7592 int err;
7593
7594 err = request_module("md-cluster");
7595 if (err) {
7596 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007597 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007598 }
7599
7600 spin_lock(&pers_lock);
7601 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7602 spin_unlock(&pers_lock);
7603 return -ENOENT;
7604 }
7605 spin_unlock(&pers_lock);
7606
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007607 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007608}
7609
7610void md_cluster_stop(struct mddev *mddev)
7611{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007612 if (!md_cluster_ops)
7613 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007614 md_cluster_ops->leave(mddev);
7615 module_put(md_cluster_mod);
7616}
7617
NeilBrownfd01b882011-10-11 16:47:53 +11007618static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007620 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007622 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623
7624 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007625 rcu_read_lock();
7626 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007628 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7629 (int)part_stat_read(&disk->part0, sectors[1]) -
7630 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007631 /* sync IO will cause sync_io to increase before the disk_stats
7632 * as sync_io is counted when a request starts, and
7633 * disk_stats is counted when it completes.
7634 * So resync activity will cause curr_events to be smaller than
7635 * when there was no such activity.
7636 * non-sync IO will cause disk_stat to increase without
7637 * increasing sync_io so curr_events will (eventually)
7638 * be larger than it was before. Once it becomes
7639 * substantially larger, the test below will cause
7640 * the array to appear non-idle, and resync will slow
7641 * down.
7642 * If there is a lot of outstanding resync activity when
7643 * we set last_event to curr_events, then all that activity
7644 * completing might cause the array to appear non-idle
7645 * and resync will be slowed down even though there might
7646 * not have been non-resync activity. This will only
7647 * happen once though. 'last_events' will soon reflect
7648 * the state where there is little or no outstanding
7649 * resync requests, and further resync activity will
7650 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007651 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007653 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654 rdev->last_events = curr_events;
7655 idle = 0;
7656 }
7657 }
NeilBrown4b809912008-07-21 17:05:25 +10007658 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659 return idle;
7660}
7661
NeilBrownfd01b882011-10-11 16:47:53 +11007662void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663{
7664 /* another "blocks" (512byte) blocks have been synced */
7665 atomic_sub(blocks, &mddev->recovery_active);
7666 wake_up(&mddev->recovery_wait);
7667 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007668 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007669 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 md_wakeup_thread(mddev->thread);
7671 // stop recovery, signal do_sync ....
7672 }
7673}
NeilBrown6c144d32014-09-30 16:15:38 +10007674EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675
NeilBrown06d91a52005-06-21 17:17:12 -07007676/* md_write_start(mddev, bi)
7677 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007678 * in superblock) before writing, schedule a superblock update
7679 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007680 */
NeilBrownfd01b882011-10-11 16:47:53 +11007681void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682{
Neil Brown0fd62b82008-06-28 08:31:36 +10007683 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007684 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007685 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007686
NeilBrownf91de922005-11-08 21:39:36 -08007687 BUG_ON(mddev->ro == 1);
7688 if (mddev->ro == 2) {
7689 /* need to switch to read/write */
7690 mddev->ro = 0;
7691 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7692 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007693 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007694 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007695 }
NeilBrown06d91a52005-06-21 17:17:12 -07007696 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007697 if (mddev->safemode == 1)
7698 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007699 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007700 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007701 if (mddev->in_sync) {
7702 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007703 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007704 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007705 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007706 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007707 }
NeilBrown85572d72014-12-15 12:56:56 +11007708 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007709 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007710 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007711 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007712 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007713 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714}
NeilBrown6c144d32014-09-30 16:15:38 +10007715EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716
NeilBrownfd01b882011-10-11 16:47:53 +11007717void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718{
7719 if (atomic_dec_and_test(&mddev->writes_pending)) {
7720 if (mddev->safemode == 2)
7721 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007722 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7724 }
7725}
NeilBrown6c144d32014-09-30 16:15:38 +10007726EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727
NeilBrown2a2275d2007-01-26 00:57:11 -08007728/* md_allow_write(mddev)
7729 * Calling this ensures that the array is marked 'active' so that writes
7730 * may proceed without blocking. It is important to call this before
7731 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7732 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007733 *
Guoqing Jiangabf35082015-12-21 10:51:01 +11007734 * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007735 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007736 */
NeilBrownfd01b882011-10-11 16:47:53 +11007737int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007738{
7739 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007740 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007741 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007742 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007743 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007744 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007745
NeilBrown85572d72014-12-15 12:56:56 +11007746 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007747 if (mddev->in_sync) {
7748 mddev->in_sync = 0;
7749 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007750 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007751 if (mddev->safemode_delay &&
7752 mddev->safemode == 0)
7753 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007754 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007755 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007756 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007757 } else
NeilBrown85572d72014-12-15 12:56:56 +11007758 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007759
NeilBrown070dc6d2010-08-30 17:33:34 +10007760 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007761 return -EAGAIN;
7762 else
7763 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007764}
7765EXPORT_SYMBOL_GPL(md_allow_write);
7766
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767#define SYNC_MARKS 10
7768#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007769#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007770void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771{
Shaohua Li4ed87312012-10-11 13:34:00 +11007772 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007773 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 unsigned int currspeed = 0,
7775 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007776 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007778 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 sector_t mark_cnt[SYNC_MARKS];
7780 int last_mark,m;
7781 struct list_head *tmp;
7782 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007783 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007784 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007785 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007786 struct blk_plug plug;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05007787 bool cluster_resync_finished = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788
7789 /* just incase thread restarts... */
7790 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7791 return;
NeilBrown3991b312014-05-28 13:39:23 +10007792 if (mddev->ro) {/* never try to sync a read-only array */
7793 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007794 return;
NeilBrown3991b312014-05-28 13:39:23 +10007795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796
NeilBrown61df9d92006-10-03 01:15:57 -07007797 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007798 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007799 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007800 action = "check";
7801 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007802 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007803 action = "repair";
7804 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007805 desc = "resync";
7806 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7807 desc = "reshape";
7808 else
7809 desc = "recovery";
7810
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007811 mddev->last_sync_action = action ?: desc;
7812
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 /* we overload curr_resync somewhat here.
7814 * 0 == not engaged in resync at all
7815 * 2 == checking that there is no conflict with another sync
7816 * 1 == like 2, but have yielded to allow conflicting resync to
7817 * commense
7818 * other == active in resync - this many blocks
7819 *
7820 * Before starting a resync we must have set curr_resync to
7821 * 2, and then checked that every "conflicting" array has curr_resync
7822 * less than ours. When we find one that is the same or higher
7823 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7824 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7825 * This will mean we have to start checking from the beginning again.
7826 *
7827 */
7828
7829 do {
7830 mddev->curr_resync = 2;
7831
7832 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007833 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007835 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836 if (mddev2 == mddev)
7837 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007838 if (!mddev->parallel_resync
7839 && mddev2->curr_resync
7840 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 DEFINE_WAIT(wq);
7842 if (mddev < mddev2 && mddev->curr_resync == 2) {
7843 /* arbitrarily yield */
7844 mddev->curr_resync = 1;
7845 wake_up(&resync_wait);
7846 }
7847 if (mddev > mddev2 && mddev->curr_resync == 1)
7848 /* no need to wait here, we can wait the next
7849 * time 'round when curr_resync == 2
7850 */
7851 continue;
NeilBrown97441972008-09-19 11:49:54 +10007852 /* We need to wait 'interruptible' so as not to
7853 * contribute to the load average, and not to
7854 * be caught by 'softlockup'
7855 */
7856 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007857 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007858 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007859 printk(KERN_INFO "md: delaying %s of %s"
7860 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007862 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007864 if (signal_pending(current))
7865 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 schedule();
7867 finish_wait(&resync_wait, &wq);
7868 goto try_again;
7869 }
7870 finish_wait(&resync_wait, &wq);
7871 }
7872 }
7873 } while (mddev->curr_resync < 2);
7874
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007875 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007876 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007878 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 */
7880 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007881 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007882 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007883 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7884 j = mddev->resync_min;
7885 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007886 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007887
NeilBrownccfcc3c2006-03-27 01:18:09 -08007888 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007889 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007890 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007892 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007893 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007894 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007895 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007896 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007897 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007898 !test_bit(Faulty, &rdev->flags) &&
7899 !test_bit(In_sync, &rdev->flags) &&
7900 rdev->recovery_offset < j)
7901 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007902 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007903
7904 /* If there is a bitmap, we need to make sure all
7905 * writes that started before we added a spare
7906 * complete before we start doing a recovery.
7907 * Otherwise the write might complete and (via
7908 * bitmap_endwrite) set a bit in the bitmap after the
7909 * recovery has checked that bit and skipped that
7910 * region.
7911 */
7912 if (mddev->bitmap) {
7913 mddev->pers->quiesce(mddev, 1);
7914 mddev->pers->quiesce(mddev, 0);
7915 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917
NeilBrown61df9d92006-10-03 01:15:57 -07007918 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7919 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7920 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007921 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007922 "(but not more than %d KB/sec) for %s.\n",
7923 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924
NeilBrowneea1bf32009-03-31 14:27:02 +11007925 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007926
NeilBrown57afd892005-06-21 17:17:13 -07007927 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 for (m = 0; m < SYNC_MARKS; m++) {
7929 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007930 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 }
7932 last_mark = 0;
7933 mddev->resync_mark = mark[last_mark];
7934 mddev->resync_mark_cnt = mark_cnt[last_mark];
7935
7936 /*
7937 * Tune reconstruction:
7938 */
7939 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007940 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7941 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942
7943 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944 last_check = 0;
7945
7946 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007947 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007948 "md: resuming %s of %s from checkpoint.\n",
7949 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007951 } else
7952 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007953 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007954 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7955 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007956 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957
majianpeng7c2c57c2012-07-03 12:12:26 +10007958 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007960 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961
NeilBrown57afd892005-06-21 17:17:13 -07007962 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007963
NeilBrown7a91ee12009-05-26 12:57:21 +10007964 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7965 ((mddev->curr_resync > mddev->curr_resync_completed &&
7966 (mddev->curr_resync - mddev->curr_resync_completed)
7967 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007968 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007969 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007970 >= mddev->resync_max - mddev->curr_resync_completed ||
7971 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007972 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007973 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007974 wait_event(mddev->recovery_wait,
7975 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007976 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007977 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7978 j > mddev->recovery_cp)
7979 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007980 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007981 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007982 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007983 }
NeilBrownacb180b2009-04-14 16:28:34 +10007984
NeilBrownc91abf52013-11-19 12:02:01 +11007985 while (j >= mddev->resync_max &&
7986 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007987 /* As this condition is controlled by user-space,
7988 * we can block indefinitely, so use '_interruptible'
7989 * to avoid triggering warnings.
7990 */
7991 flush_signals(current); /* just in case */
7992 wait_event_interruptible(mddev->recovery_wait,
7993 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007994 || test_bit(MD_RECOVERY_INTR,
7995 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007996 }
NeilBrownacb180b2009-04-14 16:28:34 +10007997
NeilBrownc91abf52013-11-19 12:02:01 +11007998 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7999 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008000
NeilBrown09314792015-02-19 16:04:40 +11008001 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008002 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008003 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005 }
NeilBrown57afd892005-06-21 17:17:13 -07008006
8007 if (!skipped) { /* actual IO requested */
8008 io_sectors += sectors;
8009 atomic_add(sectors, &mddev->recovery_active);
8010 }
8011
NeilBrowne875ece2011-07-28 11:39:24 +10008012 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8013 break;
8014
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008016 if (j > max_sectors)
8017 /* when skipping, extra large numbers can be returned. */
8018 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008019 if (j > 2)
8020 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008021 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008022 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008023 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008024 * visible in /proc/mdstat
8025 */
8026 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008027
8028 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 continue;
8030
NeilBrown57afd892005-06-21 17:17:13 -07008031 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032 repeat:
8033 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8034 /* step marks */
8035 int next = (last_mark+1) % SYNC_MARKS;
8036
8037 mddev->resync_mark = mark[next];
8038 mddev->resync_mark_cnt = mark_cnt[next];
8039 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008040 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041 last_mark = next;
8042 }
8043
NeilBrownc91abf52013-11-19 12:02:01 +11008044 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8045 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046
8047 /*
8048 * this loop exits only if either when we are slower than
8049 * the 'hard' speed limit, or the system was IO-idle for
8050 * a jiffy.
8051 * the system might be non-idle CPU-wise, but we only care
8052 * about not overloading the IO subsystem. (things like an
8053 * e2fsck being done on the RAID array should execute fast)
8054 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055 cond_resched();
8056
Xiao Niac7e50a2014-08-07 09:37:41 -04008057 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8058 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008059 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060
NeilBrown88202a02006-01-06 00:21:36 -08008061 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008062 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008063 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064 goto repeat;
8065 }
NeilBrownac8fa412015-02-19 16:55:00 +11008066 if (!is_mddev_idle(mddev, 0)) {
8067 /*
8068 * Give other IO more of a chance.
8069 * The faster the devices, the less we wait.
8070 */
8071 wait_event(mddev->recovery_wait,
8072 !atomic_read(&mddev->recovery_active));
8073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074 }
8075 }
NeilBrownc91abf52013-11-19 12:02:01 +11008076 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8077 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8078 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079 /*
8080 * this also signals 'finished resyncing' to md_stop
8081 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008082 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8084
NeilBrown5ed1df22015-07-24 13:27:08 +10008085 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8086 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8087 mddev->curr_resync > 2) {
8088 mddev->curr_resync_completed = mddev->curr_resync;
8089 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8090 }
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008091 /* tell personality and other nodes that we are finished */
8092 if (mddev_is_clustered(mddev)) {
8093 md_cluster_ops->resync_finish(mddev);
8094 cluster_resync_finished = true;
8095 }
NeilBrown09314792015-02-19 16:04:40 +11008096 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097
NeilBrowndfc70642008-05-23 13:04:39 -07008098 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008099 mddev->curr_resync > 2) {
8100 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8101 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8102 if (mddev->curr_resync >= mddev->recovery_cp) {
8103 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008104 "md: checkpointing %s of %s.\n",
8105 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008106 if (test_bit(MD_RECOVERY_ERROR,
8107 &mddev->recovery))
8108 mddev->recovery_cp =
8109 mddev->curr_resync_completed;
8110 else
8111 mddev->recovery_cp =
8112 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008113 }
8114 } else
8115 mddev->recovery_cp = MaxSector;
8116 } else {
8117 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8118 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008119 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008120 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008121 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008122 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008123 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008124 !test_bit(Faulty, &rdev->flags) &&
8125 !test_bit(In_sync, &rdev->flags) &&
8126 rdev->recovery_offset < mddev->curr_resync)
8127 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008128 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008130 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008131 skip:
NeilBrown17571282006-12-10 02:20:52 -08008132 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008134 if (mddev_is_clustered(mddev) &&
8135 test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8136 !cluster_resync_finished)
8137 md_cluster_ops->resync_finish(mddev);
8138
NeilBrown23da4222014-12-15 12:57:01 +11008139 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008140 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8141 /* We completed so min/max setting can be forgotten if used. */
8142 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8143 mddev->resync_min = 0;
8144 mddev->resync_max = MaxSector;
8145 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8146 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008147 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008149 spin_unlock(&mddev->lock);
8150
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008153 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154}
NeilBrown29269552006-03-27 01:18:10 -08008155EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156
NeilBrown746d3202013-04-24 11:42:41 +10008157static int remove_and_add_spares(struct mddev *mddev,
8158 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008159{
NeilBrown3cb03002011-10-11 16:45:26 +11008160 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008161 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008162 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008163
NeilBrowndafb20f2012-03-19 12:46:39 +11008164 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008165 if ((this == NULL || rdev == this) &&
8166 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008167 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008168 (test_bit(Faulty, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008169 (!test_bit(In_sync, &rdev->flags) &&
8170 !test_bit(Journal, &rdev->flags))) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008171 atomic_read(&rdev->nr_pending)==0) {
8172 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008173 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008174 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008175 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008176 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008177 }
8178 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008179 if (removed && mddev->kobj.sd)
8180 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008181
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008182 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008183 goto no_add;
8184
NeilBrowndafb20f2012-03-19 12:46:39 +11008185 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008186 if (this && this != rdev)
8187 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008188 if (test_bit(Candidate, &rdev->flags))
8189 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008190 if (rdev->raid_disk >= 0 &&
8191 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008192 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008193 !test_bit(Faulty, &rdev->flags))
8194 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008195 if (rdev->raid_disk >= 0)
8196 continue;
8197 if (test_bit(Faulty, &rdev->flags))
8198 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008199 if (!test_bit(Journal, &rdev->flags)) {
8200 if (mddev->ro &&
8201 ! (rdev->saved_raid_disk >= 0 &&
8202 !test_bit(Bitmap_sync, &rdev->flags)))
8203 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008204
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008205 rdev->recovery_offset = 0;
8206 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008207 if (mddev->pers->
8208 hot_add_disk(mddev, rdev) == 0) {
8209 if (sysfs_link_rdev(mddev, rdev))
8210 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008211 if (!test_bit(Journal, &rdev->flags))
8212 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008213 md_new_event(mddev);
8214 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008215 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008216 }
NeilBrown746d3202013-04-24 11:42:41 +10008217no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008218 if (removed)
8219 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008220 return spares;
8221}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008222
NeilBrownac05f252014-09-30 08:10:42 +10008223static void md_start_sync(struct work_struct *ws)
8224{
8225 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008226 int ret = 0;
8227
8228 if (mddev_is_clustered(mddev)) {
8229 ret = md_cluster_ops->resync_start(mddev);
8230 if (ret) {
8231 mddev->sync_thread = NULL;
8232 goto out;
8233 }
8234 }
NeilBrownac05f252014-09-30 08:10:42 +10008235
8236 mddev->sync_thread = md_register_thread(md_do_sync,
8237 mddev,
8238 "resync");
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008239out:
NeilBrownac05f252014-09-30 08:10:42 +10008240 if (!mddev->sync_thread) {
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008241 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8242 printk(KERN_ERR "%s: could not start resync"
8243 " thread...\n",
8244 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008245 /* leave the spares where they are, it shouldn't hurt */
8246 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8247 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8248 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8249 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8250 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008251 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008252 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8253 &mddev->recovery))
8254 if (mddev->sysfs_action)
8255 sysfs_notify_dirent_safe(mddev->sysfs_action);
8256 } else
8257 md_wakeup_thread(mddev->sync_thread);
8258 sysfs_notify_dirent_safe(mddev->sysfs_action);
8259 md_new_event(mddev);
8260}
8261
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262/*
8263 * This routine is regularly called by all per-raid-array threads to
8264 * deal with generic issues like resync and super-block update.
8265 * Raid personalities that don't have a thread (linear/raid0) do not
8266 * need this as they never do any recovery or update the superblock.
8267 *
8268 * It does not do any resync itself, but rather "forks" off other threads
8269 * to do that as needed.
8270 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8271 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008272 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273 * and wakeups up this thread which will reap the thread and finish up.
8274 * This thread also removes any faulty devices (with nr_pending == 0).
8275 *
8276 * The overall approach is:
8277 * 1/ if the superblock needs updating, update it.
8278 * 2/ If a recovery thread is running, don't do anything else.
8279 * 3/ If recovery has finished, clean up, possibly marking spares active.
8280 * 4/ If there are any faulty devices, remove them.
8281 * 5/ If array is degraded, try to add spares devices
8282 * 6/ If array has spares or is not in-sync, start a resync thread.
8283 */
NeilBrownfd01b882011-10-11 16:47:53 +11008284void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008286 if (mddev->suspended)
8287 return;
8288
NeilBrown5f404022005-06-21 17:17:16 -07008289 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008290 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291
NeilBrownfca4d842005-06-21 17:17:11 -07008292 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008293 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008294 printk(KERN_INFO "md: %s in immediate safe mode\n",
8295 mdname(mddev));
8296 mddev->safemode = 2;
8297 }
8298 flush_signals(current);
8299 }
8300
NeilBrownc89a8ee2008-08-05 15:54:13 +10008301 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8302 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008304 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008306 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008307 test_bit(MD_RELOAD_SB, &mddev->flags) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008308 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008309 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8310 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311 ))
8312 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008313
NeilBrowndf5b89b2006-03-27 01:18:20 -08008314 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008315 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008316
NeilBrownc89a8ee2008-08-05 15:54:13 +10008317 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008318 struct md_rdev *rdev;
8319 if (!mddev->external && mddev->in_sync)
8320 /* 'Blocked' flag not needed as failed devices
8321 * will be recorded if array switched to read/write.
8322 * Leaving it set will prevent the device
8323 * from being removed.
8324 */
8325 rdev_for_each(rdev, mddev)
8326 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008327 /* On a read-only array we can:
8328 * - remove failed devices
8329 * - add already-in_sync devices if the array itself
8330 * is in-sync.
8331 * As we only add devices that are already in-sync,
8332 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008333 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008334 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008335 /* There is no thread, but we need to call
8336 * ->spare_active and clear saved_raid_disk
8337 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008338 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008339 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008340 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008341 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008342 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008343 goto unlock;
8344 }
8345
Guoqing Jiang659b2542015-12-21 10:50:59 +11008346 if (mddev_is_clustered(mddev)) {
8347 struct md_rdev *rdev;
8348 /* kick the device if another node issued a
8349 * remove disk.
8350 */
8351 rdev_for_each(rdev, mddev) {
8352 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8353 rdev->raid_disk < 0)
8354 md_kick_rdev_from_array(rdev);
8355 }
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008356
8357 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8358 md_reload_sb(mddev, mddev->good_device_nr);
Guoqing Jiang659b2542015-12-21 10:50:59 +11008359 }
8360
NeilBrown31a59e32008-04-30 00:52:30 -07008361 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008362 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008363 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008364 if (mddev->safemode &&
8365 !atomic_read(&mddev->writes_pending) &&
8366 !mddev->in_sync &&
8367 mddev->recovery_cp == MaxSector) {
8368 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008369 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008370 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008371 }
8372 if (mddev->safemode == 1)
8373 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008374 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008375 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008376 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008377 }
NeilBrownfca4d842005-06-21 17:17:11 -07008378
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008379 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07008380 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008381
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8383 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8384 /* resync/recovery still happening */
8385 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8386 goto unlock;
8387 }
8388 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008389 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 goto unlock;
8391 }
Neil Brown72a23c22008-06-28 08:31:41 +10008392 /* Set RUNNING before clearing NEEDED to avoid
8393 * any transients in the value of "sync_action".
8394 */
NeilBrown72f36d52012-10-11 14:25:57 +11008395 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008396 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008397 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008398 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008399 /* Clear some bits that don't mean anything, but
8400 * might be left set
8401 */
NeilBrown24dd4692005-11-08 21:39:26 -08008402 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8403 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404
NeilBrowned209582012-04-24 10:23:14 +10008405 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8406 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008407 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 /* no recovery is running.
8409 * remove any failed drives, then
8410 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008411 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412 * the personality to fail the re-add.
8413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008415 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008416 if (mddev->pers->check_reshape == NULL ||
8417 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008418 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008419 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008420 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008421 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008422 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008423 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8424 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008425 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008426 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008427 } else if (mddev->recovery_cp < MaxSector) {
8428 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008429 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008430 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8431 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008432 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008433
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008435 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008436 /* We are adding a device or devices to an array
8437 * which has the bitmap stored on all devices.
8438 * So make sure all bitmap pages get written
8439 */
8440 bitmap_write_all(mddev->bitmap);
8441 }
NeilBrownac05f252014-09-30 08:10:42 +10008442 INIT_WORK(&mddev->del_work, md_start_sync);
8443 queue_work(md_misc_wq, &mddev->del_work);
8444 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445 }
NeilBrownac05f252014-09-30 08:10:42 +10008446 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008447 if (!mddev->sync_thread) {
8448 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008449 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008450 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8451 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008452 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008453 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008454 }
NeilBrownac05f252014-09-30 08:10:42 +10008455 unlock:
8456 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457 mddev_unlock(mddev);
8458 }
8459}
NeilBrown6c144d32014-09-30 16:15:38 +10008460EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008462void md_reap_sync_thread(struct mddev *mddev)
8463{
8464 struct md_rdev *rdev;
8465
8466 /* resync has finished, collect result */
8467 md_unregister_thread(&mddev->sync_thread);
8468 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8469 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8470 /* success...*/
8471 /* activate any spares */
8472 if (mddev->pers->spare_active(mddev)) {
8473 sysfs_notify(&mddev->kobj, NULL,
8474 "degraded");
8475 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8476 }
8477 }
8478 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8479 mddev->pers->finish_reshape)
8480 mddev->pers->finish_reshape(mddev);
8481
8482 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008483 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008484 */
NeilBrownf4667222013-12-09 12:04:56 +11008485 if (!mddev->degraded)
8486 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008487 rdev->saved_raid_disk = -1;
8488
8489 md_update_sb(mddev, 1);
8490 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008491 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008492 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8493 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8494 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8495 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008496 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008497 /* flag recovery needed just to double check */
8498 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8499 sysfs_notify_dirent_safe(mddev->sysfs_action);
8500 md_new_event(mddev);
8501 if (mddev->event_work.func)
8502 queue_work(md_misc_wq, &mddev->event_work);
8503}
NeilBrown6c144d32014-09-30 16:15:38 +10008504EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008505
NeilBrownfd01b882011-10-11 16:47:53 +11008506void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008507{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008508 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008509 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008510 !test_bit(Blocked, &rdev->flags) &&
8511 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008512 msecs_to_jiffies(5000));
8513 rdev_dec_pending(rdev, mddev);
8514}
8515EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8516
NeilBrownc6563a82012-05-21 09:27:00 +10008517void md_finish_reshape(struct mddev *mddev)
8518{
8519 /* called be personality module when reshape completes. */
8520 struct md_rdev *rdev;
8521
8522 rdev_for_each(rdev, mddev) {
8523 if (rdev->data_offset > rdev->new_data_offset)
8524 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8525 else
8526 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8527 rdev->data_offset = rdev->new_data_offset;
8528 }
8529}
8530EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008531
8532/* Bad block management.
8533 * We can record which blocks on each device are 'bad' and so just
8534 * fail those blocks, or that stripe, rather than the whole device.
8535 * Entries in the bad-block table are 64bits wide. This comprises:
8536 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8537 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8538 * A 'shift' can be set so that larger blocks are tracked and
8539 * consequently larger devices can be covered.
8540 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8541 *
8542 * Locking of the bad-block table uses a seqlock so md_is_badblock
8543 * might need to retry if it is very unlucky.
8544 * We will sometimes want to check for bad blocks in a bi_end_io function,
8545 * so we use the write_seqlock_irq variant.
8546 *
8547 * When looking for a bad block we specify a range and want to
8548 * know if any block in the range is bad. So we binary-search
8549 * to the last range that starts at-or-before the given endpoint,
8550 * (or "before the sector after the target range")
8551 * then see if it ends after the given start.
8552 * We return
8553 * 0 if there are no known bad blocks in the range
8554 * 1 if there are known bad block which are all acknowledged
8555 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8556 * plus the start/length of the first bad section we overlap.
8557 */
8558int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8559 sector_t *first_bad, int *bad_sectors)
8560{
8561 int hi;
majianpengab056132012-11-06 17:13:44 +08008562 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008563 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008564 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008565 sector_t target = s + sectors;
8566 unsigned seq;
8567
8568 if (bb->shift > 0) {
8569 /* round the start down, and the end up */
8570 s >>= bb->shift;
8571 target += (1<<bb->shift) - 1;
8572 target >>= bb->shift;
8573 sectors = target - s;
8574 }
8575 /* 'target' is now the first block after the bad range */
8576
8577retry:
8578 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008579 lo = 0;
8580 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008581 hi = bb->count;
8582
8583 /* Binary search between lo and hi for 'target'
8584 * i.e. for the last range that starts before 'target'
8585 */
8586 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8587 * are known not to be the last range before target.
8588 * VARIANT: hi-lo is the number of possible
8589 * ranges, and decreases until it reaches 1
8590 */
8591 while (hi - lo > 1) {
8592 int mid = (lo + hi) / 2;
8593 sector_t a = BB_OFFSET(p[mid]);
8594 if (a < target)
8595 /* This could still be the one, earlier ranges
8596 * could not. */
8597 lo = mid;
8598 else
8599 /* This and later ranges are definitely out. */
8600 hi = mid;
8601 }
8602 /* 'lo' might be the last that started before target, but 'hi' isn't */
8603 if (hi > lo) {
8604 /* need to check all range that end after 's' to see if
8605 * any are unacknowledged.
8606 */
8607 while (lo >= 0 &&
8608 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8609 if (BB_OFFSET(p[lo]) < target) {
8610 /* starts before the end, and finishes after
8611 * the start, so they must overlap
8612 */
8613 if (rv != -1 && BB_ACK(p[lo]))
8614 rv = 1;
8615 else
8616 rv = -1;
8617 *first_bad = BB_OFFSET(p[lo]);
8618 *bad_sectors = BB_LEN(p[lo]);
8619 }
8620 lo--;
8621 }
8622 }
8623
8624 if (read_seqretry(&bb->lock, seq))
8625 goto retry;
8626
8627 return rv;
8628}
8629EXPORT_SYMBOL_GPL(md_is_badblock);
8630
8631/*
8632 * Add a range of bad blocks to the table.
8633 * This might extend the table, or might contract it
8634 * if two adjacent ranges can be merged.
8635 * We binary-search to find the 'insertion' point, then
8636 * decide how best to handle it.
8637 */
8638static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8639 int acknowledged)
8640{
8641 u64 *p;
8642 int lo, hi;
8643 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008644 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008645
8646 if (bb->shift < 0)
8647 /* badblocks are disabled */
8648 return 0;
8649
8650 if (bb->shift) {
8651 /* round the start down, and the end up */
8652 sector_t next = s + sectors;
8653 s >>= bb->shift;
8654 next += (1<<bb->shift) - 1;
8655 next >>= bb->shift;
8656 sectors = next - s;
8657 }
8658
Bian Yu905b0292013-10-12 01:10:03 -04008659 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008660
8661 p = bb->page;
8662 lo = 0;
8663 hi = bb->count;
8664 /* Find the last range that starts at-or-before 's' */
8665 while (hi - lo > 1) {
8666 int mid = (lo + hi) / 2;
8667 sector_t a = BB_OFFSET(p[mid]);
8668 if (a <= s)
8669 lo = mid;
8670 else
8671 hi = mid;
8672 }
8673 if (hi > lo && BB_OFFSET(p[lo]) > s)
8674 hi = lo;
8675
8676 if (hi > lo) {
8677 /* we found a range that might merge with the start
8678 * of our new range
8679 */
8680 sector_t a = BB_OFFSET(p[lo]);
8681 sector_t e = a + BB_LEN(p[lo]);
8682 int ack = BB_ACK(p[lo]);
8683 if (e >= s) {
8684 /* Yes, we can merge with a previous range */
8685 if (s == a && s + sectors >= e)
8686 /* new range covers old */
8687 ack = acknowledged;
8688 else
8689 ack = ack && acknowledged;
8690
8691 if (e < s + sectors)
8692 e = s + sectors;
8693 if (e - a <= BB_MAX_LEN) {
8694 p[lo] = BB_MAKE(a, e-a, ack);
8695 s = e;
8696 } else {
8697 /* does not all fit in one range,
8698 * make p[lo] maximal
8699 */
8700 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8701 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8702 s = a + BB_MAX_LEN;
8703 }
8704 sectors = e - s;
8705 }
8706 }
8707 if (sectors && hi < bb->count) {
8708 /* 'hi' points to the first range that starts after 's'.
8709 * Maybe we can merge with the start of that range */
8710 sector_t a = BB_OFFSET(p[hi]);
8711 sector_t e = a + BB_LEN(p[hi]);
8712 int ack = BB_ACK(p[hi]);
8713 if (a <= s + sectors) {
8714 /* merging is possible */
8715 if (e <= s + sectors) {
8716 /* full overlap */
8717 e = s + sectors;
8718 ack = acknowledged;
8719 } else
8720 ack = ack && acknowledged;
8721
8722 a = s;
8723 if (e - a <= BB_MAX_LEN) {
8724 p[hi] = BB_MAKE(a, e-a, ack);
8725 s = e;
8726 } else {
8727 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8728 s = a + BB_MAX_LEN;
8729 }
8730 sectors = e - s;
8731 lo = hi;
8732 hi++;
8733 }
8734 }
8735 if (sectors == 0 && hi < bb->count) {
8736 /* we might be able to combine lo and hi */
8737 /* Note: 's' is at the end of 'lo' */
8738 sector_t a = BB_OFFSET(p[hi]);
8739 int lolen = BB_LEN(p[lo]);
8740 int hilen = BB_LEN(p[hi]);
8741 int newlen = lolen + hilen - (s - a);
8742 if (s >= a && newlen < BB_MAX_LEN) {
8743 /* yes, we can combine them */
8744 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8745 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8746 memmove(p + hi, p + hi + 1,
8747 (bb->count - hi - 1) * 8);
8748 bb->count--;
8749 }
8750 }
8751 while (sectors) {
8752 /* didn't merge (it all).
8753 * Need to add a range just before 'hi' */
8754 if (bb->count >= MD_MAX_BADBLOCKS) {
8755 /* No room for more */
8756 rv = 0;
8757 break;
8758 } else {
8759 int this_sectors = sectors;
8760 memmove(p + hi + 1, p + hi,
8761 (bb->count - hi) * 8);
8762 bb->count++;
8763
8764 if (this_sectors > BB_MAX_LEN)
8765 this_sectors = BB_MAX_LEN;
8766 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8767 sectors -= this_sectors;
8768 s += this_sectors;
8769 }
8770 }
8771
8772 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008773 if (!acknowledged)
8774 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008775 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008776
8777 return rv;
8778}
8779
NeilBrown3cb03002011-10-11 16:45:26 +11008780int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008781 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008782{
NeilBrownc6563a82012-05-21 09:27:00 +10008783 int rv;
8784 if (is_new)
8785 s += rdev->new_data_offset;
8786 else
8787 s += rdev->data_offset;
8788 rv = md_set_badblocks(&rdev->badblocks,
8789 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008790 if (rv) {
8791 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008792 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008793 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
NeilBrown55ce74d2015-08-14 11:11:10 +10008794 set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008795 md_wakeup_thread(rdev->mddev->thread);
8796 }
8797 return rv;
8798}
8799EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8800
8801/*
8802 * Remove a range of bad blocks from the table.
8803 * This may involve extending the table if we spilt a region,
8804 * but it must not fail. So if the table becomes full, we just
8805 * drop the remove request.
8806 */
8807static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8808{
8809 u64 *p;
8810 int lo, hi;
8811 sector_t target = s + sectors;
8812 int rv = 0;
8813
8814 if (bb->shift > 0) {
8815 /* When clearing we round the start up and the end down.
8816 * This should not matter as the shift should align with
8817 * the block size and no rounding should ever be needed.
8818 * However it is better the think a block is bad when it
8819 * isn't than to think a block is not bad when it is.
8820 */
8821 s += (1<<bb->shift) - 1;
8822 s >>= bb->shift;
8823 target >>= bb->shift;
8824 sectors = target - s;
8825 }
8826
8827 write_seqlock_irq(&bb->lock);
8828
8829 p = bb->page;
8830 lo = 0;
8831 hi = bb->count;
8832 /* Find the last range that starts before 'target' */
8833 while (hi - lo > 1) {
8834 int mid = (lo + hi) / 2;
8835 sector_t a = BB_OFFSET(p[mid]);
8836 if (a < target)
8837 lo = mid;
8838 else
8839 hi = mid;
8840 }
8841 if (hi > lo) {
8842 /* p[lo] is the last range that could overlap the
8843 * current range. Earlier ranges could also overlap,
8844 * but only this one can overlap the end of the range.
8845 */
8846 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8847 /* Partial overlap, leave the tail of this range */
8848 int ack = BB_ACK(p[lo]);
8849 sector_t a = BB_OFFSET(p[lo]);
8850 sector_t end = a + BB_LEN(p[lo]);
8851
8852 if (a < s) {
8853 /* we need to split this range */
8854 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008855 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008856 goto out;
8857 }
8858 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8859 bb->count++;
8860 p[lo] = BB_MAKE(a, s-a, ack);
8861 lo++;
8862 }
8863 p[lo] = BB_MAKE(target, end - target, ack);
8864 /* there is no longer an overlap */
8865 hi = lo;
8866 lo--;
8867 }
8868 while (lo >= 0 &&
8869 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8870 /* This range does overlap */
8871 if (BB_OFFSET(p[lo]) < s) {
8872 /* Keep the early parts of this range. */
8873 int ack = BB_ACK(p[lo]);
8874 sector_t start = BB_OFFSET(p[lo]);
8875 p[lo] = BB_MAKE(start, s - start, ack);
8876 /* now low doesn't overlap, so.. */
8877 break;
8878 }
8879 lo--;
8880 }
8881 /* 'lo' is strictly before, 'hi' is strictly after,
8882 * anything between needs to be discarded
8883 */
8884 if (hi - lo > 1) {
8885 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8886 bb->count -= (hi - lo - 1);
8887 }
8888 }
8889
8890 bb->changed = 1;
8891out:
8892 write_sequnlock_irq(&bb->lock);
8893 return rv;
8894}
8895
NeilBrownc6563a82012-05-21 09:27:00 +10008896int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8897 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008898{
NeilBrownc6563a82012-05-21 09:27:00 +10008899 if (is_new)
8900 s += rdev->new_data_offset;
8901 else
8902 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008903 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008904 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008905}
8906EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8907
8908/*
8909 * Acknowledge all bad blocks in a list.
8910 * This only succeeds if ->changed is clear. It is used by
8911 * in-kernel metadata updates
8912 */
8913void md_ack_all_badblocks(struct badblocks *bb)
8914{
8915 if (bb->page == NULL || bb->changed)
8916 /* no point even trying */
8917 return;
8918 write_seqlock_irq(&bb->lock);
8919
majianpengecb178b2012-03-19 12:46:42 +11008920 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008921 u64 *p = bb->page;
8922 int i;
8923 for (i = 0; i < bb->count ; i++) {
8924 if (!BB_ACK(p[i])) {
8925 sector_t start = BB_OFFSET(p[i]);
8926 int len = BB_LEN(p[i]);
8927 p[i] = BB_MAKE(start, len, 1);
8928 }
8929 }
NeilBrownde393cd2011-07-28 11:31:48 +10008930 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008931 }
8932 write_sequnlock_irq(&bb->lock);
8933}
8934EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8935
NeilBrown16c791a2011-07-28 11:31:47 +10008936/* sysfs access to bad-blocks list.
8937 * We present two files.
8938 * 'bad-blocks' lists sector numbers and lengths of ranges that
8939 * are recorded as bad. The list is truncated to fit within
8940 * the one-page limit of sysfs.
8941 * Writing "sector length" to this file adds an acknowledged
8942 * bad block list.
8943 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8944 * been acknowledged. Writing to this file adds bad blocks
8945 * without acknowledging them. This is largely for testing.
8946 */
8947
8948static ssize_t
8949badblocks_show(struct badblocks *bb, char *page, int unack)
8950{
8951 size_t len;
8952 int i;
8953 u64 *p = bb->page;
8954 unsigned seq;
8955
8956 if (bb->shift < 0)
8957 return 0;
8958
8959retry:
8960 seq = read_seqbegin(&bb->lock);
8961
8962 len = 0;
8963 i = 0;
8964
8965 while (len < PAGE_SIZE && i < bb->count) {
8966 sector_t s = BB_OFFSET(p[i]);
8967 unsigned int length = BB_LEN(p[i]);
8968 int ack = BB_ACK(p[i]);
8969 i++;
8970
8971 if (unack && ack)
8972 continue;
8973
8974 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8975 (unsigned long long)s << bb->shift,
8976 length << bb->shift);
8977 }
NeilBrownde393cd2011-07-28 11:31:48 +10008978 if (unack && len == 0)
8979 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008980
8981 if (read_seqretry(&bb->lock, seq))
8982 goto retry;
8983
8984 return len;
8985}
8986
8987#define DO_DEBUG 1
8988
8989static ssize_t
8990badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8991{
8992 unsigned long long sector;
8993 int length;
8994 char newline;
8995#ifdef DO_DEBUG
8996 /* Allow clearing via sysfs *only* for testing/debugging.
8997 * Normally only a successful write may clear a badblock
8998 */
8999 int clear = 0;
9000 if (page[0] == '-') {
9001 clear = 1;
9002 page++;
9003 }
9004#endif /* DO_DEBUG */
9005
9006 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
9007 case 3:
9008 if (newline != '\n')
9009 return -EINVAL;
9010 case 2:
9011 if (length <= 0)
9012 return -EINVAL;
9013 break;
9014 default:
9015 return -EINVAL;
9016 }
9017
9018#ifdef DO_DEBUG
9019 if (clear) {
9020 md_clear_badblocks(bb, sector, length);
9021 return len;
9022 }
9023#endif /* DO_DEBUG */
9024 if (md_set_badblocks(bb, sector, length, !unack))
9025 return len;
9026 else
9027 return -ENOSPC;
9028}
9029
Adrian Bunk75c96f82005-05-05 16:16:09 -07009030static int md_notify_reboot(struct notifier_block *this,
9031 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009032{
9033 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009034 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009035 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009036
NeilBrownc744a652012-03-19 12:46:37 +11009037 for_each_mddev(mddev, tmp) {
9038 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009039 if (mddev->pers)
9040 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009041 if (mddev->persistent)
9042 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009043 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009044 }
NeilBrownc744a652012-03-19 12:46:37 +11009045 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046 }
NeilBrownc744a652012-03-19 12:46:37 +11009047 /*
9048 * certain more exotic SCSI devices are known to be
9049 * volatile wrt too early system reboots. While the
9050 * right place to handle this issue is the given
9051 * driver, we do want to have a safe RAID driver ...
9052 */
9053 if (need_delay)
9054 mdelay(1000*1);
9055
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056 return NOTIFY_DONE;
9057}
9058
Adrian Bunk75c96f82005-05-05 16:16:09 -07009059static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060 .notifier_call = md_notify_reboot,
9061 .next = NULL,
9062 .priority = INT_MAX, /* before any real devices */
9063};
9064
9065static void md_geninit(void)
9066{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009067 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009068
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009069 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009070}
9071
Adrian Bunk75c96f82005-05-05 16:16:09 -07009072static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009073{
Tejun Heoe804ac72010-10-15 15:36:08 +02009074 int ret = -ENOMEM;
9075
Tejun Heoada609e2011-01-25 14:35:54 +01009076 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009077 if (!md_wq)
9078 goto err_wq;
9079
9080 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9081 if (!md_misc_wq)
9082 goto err_misc_wq;
9083
9084 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9085 goto err_md;
9086
9087 if ((ret = register_blkdev(0, "mdp")) < 0)
9088 goto err_mdp;
9089 mdp_major = ret;
9090
NeilBrownaf5628f2014-07-31 13:54:54 +10009091 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009092 md_probe, NULL, NULL);
9093 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094 md_probe, NULL, NULL);
9095
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009097 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009098
9099 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009101
Tejun Heoe804ac72010-10-15 15:36:08 +02009102err_mdp:
9103 unregister_blkdev(MD_MAJOR, "md");
9104err_md:
9105 destroy_workqueue(md_misc_wq);
9106err_misc_wq:
9107 destroy_workqueue(md_wq);
9108err_wq:
9109 return ret;
9110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009111
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009112static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009113{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009114 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9115 struct md_rdev *rdev2;
9116 int role, ret;
9117 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009118
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009119 /* Check for change of roles in the active devices */
9120 rdev_for_each(rdev2, mddev) {
9121 if (test_bit(Faulty, &rdev2->flags))
9122 continue;
9123
9124 /* Check if the roles changed */
9125 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009126
9127 if (test_bit(Candidate, &rdev2->flags)) {
9128 if (role == 0xfffe) {
9129 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9130 md_kick_rdev_from_array(rdev2);
9131 continue;
9132 }
9133 else
9134 clear_bit(Candidate, &rdev2->flags);
9135 }
9136
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009137 if (role != rdev2->raid_disk) {
9138 /* got activated */
9139 if (rdev2->raid_disk == -1 && role != 0xffff) {
9140 rdev2->saved_raid_disk = role;
9141 ret = remove_and_add_spares(mddev, rdev2);
9142 pr_info("Activated spare: %s\n",
9143 bdevname(rdev2->bdev,b));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009144 }
9145 /* device faulty
9146 * We just want to do the minimum to mark the disk
9147 * as faulty. The recovery is performed by the
9148 * one who initiated the error.
9149 */
9150 if ((role == 0xfffe) || (role == 0xfffd)) {
9151 md_error(mddev, rdev2);
9152 clear_bit(Blocked, &rdev2->flags);
9153 }
9154 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009155 }
9156
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009157 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9158 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009159
9160 /* Finally set the event to be up to date */
9161 mddev->events = le64_to_cpu(sb->events);
9162}
9163
9164static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9165{
9166 int err;
9167 struct page *swapout = rdev->sb_page;
9168 struct mdp_superblock_1 *sb;
9169
9170 /* Store the sb page of the rdev in the swapout temporary
9171 * variable in case we err in the future
9172 */
9173 rdev->sb_page = NULL;
9174 alloc_disk_sb(rdev);
9175 ClearPageUptodate(rdev->sb_page);
9176 rdev->sb_loaded = 0;
9177 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
9178
9179 if (err < 0) {
9180 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9181 __func__, __LINE__, rdev->desc_nr, err);
9182 put_page(rdev->sb_page);
9183 rdev->sb_page = swapout;
9184 rdev->sb_loaded = 1;
9185 return err;
9186 }
9187
9188 sb = page_address(rdev->sb_page);
9189 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9190 * is not set
9191 */
9192
9193 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9194 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9195
9196 /* The other node finished recovery, call spare_active to set
9197 * device In_sync and mddev->degraded
9198 */
9199 if (rdev->recovery_offset == MaxSector &&
9200 !test_bit(In_sync, &rdev->flags) &&
9201 mddev->pers->spare_active(mddev))
9202 sysfs_notify(&mddev->kobj, NULL, "degraded");
9203
9204 put_page(swapout);
9205 return 0;
9206}
9207
9208void md_reload_sb(struct mddev *mddev, int nr)
9209{
9210 struct md_rdev *rdev;
9211 int err;
9212
9213 /* Find the rdev */
9214 rdev_for_each_rcu(rdev, mddev) {
9215 if (rdev->desc_nr == nr)
9216 break;
9217 }
9218
9219 if (!rdev || rdev->desc_nr != nr) {
9220 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9221 return;
9222 }
9223
9224 err = read_rdev(mddev, rdev);
9225 if (err < 0)
9226 return;
9227
9228 check_sb_changes(mddev, rdev);
9229
9230 /* Read all rdev's to update recovery_offset */
9231 rdev_for_each_rcu(rdev, mddev)
9232 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009233}
9234EXPORT_SYMBOL(md_reload_sb);
9235
Linus Torvalds1da177e2005-04-16 15:20:36 -07009236#ifndef MODULE
9237
9238/*
9239 * Searches all registered partitions for autorun RAID arrays
9240 * at boot time.
9241 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009242
9243static LIST_HEAD(all_detected_devices);
9244struct detected_devices_node {
9245 struct list_head list;
9246 dev_t dev;
9247};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009248
9249void md_autodetect_dev(dev_t dev)
9250{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009251 struct detected_devices_node *node_detected_dev;
9252
9253 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9254 if (node_detected_dev) {
9255 node_detected_dev->dev = dev;
9256 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9257 } else {
9258 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
9259 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
9260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261}
9262
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263static void autostart_arrays(int part)
9264{
NeilBrown3cb03002011-10-11 16:45:26 +11009265 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009266 struct detected_devices_node *node_detected_dev;
9267 dev_t dev;
9268 int i_scanned, i_passed;
9269
9270 i_scanned = 0;
9271 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272
9273 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
9274
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009275 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9276 i_scanned++;
9277 node_detected_dev = list_entry(all_detected_devices.next,
9278 struct detected_devices_node, list);
9279 list_del(&node_detected_dev->list);
9280 dev = node_detected_dev->dev;
9281 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07009282 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009283 if (IS_ERR(rdev))
9284 continue;
9285
NeilBrown403df472014-09-30 15:52:29 +10009286 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009287 continue;
NeilBrown403df472014-09-30 15:52:29 +10009288
NeilBrownd0fae182008-03-04 14:29:31 -08009289 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009290 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009291 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009293
9294 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
9295 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296
9297 autorun_devices(part);
9298}
9299
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009300#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301
9302static __exit void md_exit(void)
9303{
NeilBrownfd01b882011-10-11 16:47:53 +11009304 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009306 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009307
NeilBrownaf5628f2014-07-31 13:54:54 +10009308 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009309 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009310
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009311 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 unregister_blkdev(mdp_major, "mdp");
9313 unregister_reboot_notifier(&md_notifier);
9314 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009315
9316 /* We cannot unload the modules while some process is
9317 * waiting for us in select() or poll() - wake them up
9318 */
9319 md_unloading = 1;
9320 while (waitqueue_active(&md_event_waiters)) {
9321 /* not safe to leave yet */
9322 wake_up(&md_event_waiters);
9323 msleep(delay);
9324 delay += delay;
9325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009327
NeilBrown29ac4aa2008-02-06 01:39:58 -08009328 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009330 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009331 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009332 destroy_workqueue(md_misc_wq);
9333 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334}
9335
Dan Williams685784a2007-07-09 11:56:42 -07009336subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009337module_exit(md_exit)
9338
NeilBrownf91de922005-11-08 21:39:36 -08009339static int get_ro(char *buffer, struct kernel_param *kp)
9340{
9341 return sprintf(buffer, "%d", start_readonly);
9342}
9343static int set_ro(const char *val, struct kernel_param *kp)
9344{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009345 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009346}
9347
NeilBrown80ca3a42006-07-10 04:44:18 -07009348module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9349module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009350module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009351
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009353MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009354MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009355MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);