blob: 194580fba7fd8b7a965ea87f9856cada61e15be6 [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>
Vishal Vermafc974ee2015-12-24 19:20:34 -070037#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110039#include <linux/seq_file.h>
Al Viroff01bb482011-09-16 02:31:11 -040040#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080041#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070042#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080043#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110044#include <linux/hdreg.h>
45#include <linux/proc_fs.h>
46#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040047#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110048#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070049#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110050#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110051#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/raid/md_p.h>
53#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110055#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110056#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050057#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110060static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
NeilBrown01f96c02011-09-21 15:30:20 +100063/* pers_list is a list of registered personalities protected
64 * by pers_lock.
65 * pers_lock does extra service to protect accesses to
66 * mddev->thread when the mutex cannot be held.
67 */
NeilBrown2604b702006-01-06 00:20:36 -080068static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static DEFINE_SPINLOCK(pers_lock);
70
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050071struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050072EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050073struct module *md_cluster_mod;
74EXPORT_SYMBOL(md_cluster_mod);
75
Bernd Schubert90b08712008-05-23 13:04:38 -070076static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020077static struct workqueue_struct *md_wq;
78static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070079
NeilBrown746d3202013-04-24 11:42:41 +100080static int remove_and_add_spares(struct mddev *mddev,
81 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110082static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
Robert Becker1e509152009-12-14 12:49:58 +110085 * Default number of read corrections we'll attempt on an rdev
86 * before ejecting it from the array. We divide the read error
87 * count by 2 for every hour elapsed between read errors.
88 */
89#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
90/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
92 * is 1000 KB/sec, so the extra system load does not show up that much.
93 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070094 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * subsystem is idle. There is also an 'absolute maximum' reconstruction
96 * speed limit - in case reconstruction slows down your system despite
97 * idle IO detection.
98 *
99 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800100 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
102
103static int sysctl_speed_limit_min = 1000;
104static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100105static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800106{
107 return mddev->sync_speed_min ?
108 mddev->sync_speed_min : sysctl_speed_limit_min;
109}
110
NeilBrownfd01b882011-10-11 16:47:53 +1100111static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800112{
113 return mddev->sync_speed_max ?
114 mddev->sync_speed_max : sysctl_speed_limit_max;
115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117static struct ctl_table_header *raid_table_header;
118
Joe Perches82592c32013-11-14 15:16:18 +1100119static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .procname = "speed_limit_min",
122 .data = &sysctl_speed_limit_min,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 },
127 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .procname = "speed_limit_max",
129 .data = &sysctl_speed_limit_max,
130 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700131 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800132 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800134 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Joe Perches82592c32013-11-14 15:16:18 +1100137static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .procname = "raid",
140 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700141 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .child = raid_table,
143 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800144 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Joe Perches82592c32013-11-14 15:16:18 +1100147static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .procname = "dev",
150 .maxlen = 0,
151 .mode = 0555,
152 .child = raid_dir_table,
153 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800154 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700157static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
NeilBrownf91de922005-11-08 21:39:36 -0800159static int start_readonly;
160
NeilBrowna167f662010-10-26 18:31:13 +1100161/* bio_clone_mddev
162 * like bio_clone, but with a local bio set
163 */
164
NeilBrowna167f662010-10-26 18:31:13 +1100165struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100166 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100167{
168 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100169
170 if (!mddev || !mddev->bio_set)
171 return bio_alloc(gfp_mask, nr_iovecs);
172
Kent Overstreet395c72a2012-09-06 15:34:55 -0700173 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100174 if (!b)
175 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100176 return b;
177}
178EXPORT_SYMBOL_GPL(bio_alloc_mddev);
179
180struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100181 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100182{
NeilBrowna167f662010-10-26 18:31:13 +1100183 if (!mddev || !mddev->bio_set)
184 return bio_clone(bio, gfp_mask);
185
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700186 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100187}
188EXPORT_SYMBOL_GPL(bio_clone_mddev);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
NeilBrownd7603b72006-01-06 00:20:30 -0800191 * We have a system wide 'event count' that is incremented
192 * on any 'interesting' event, and readers of /proc/mdstat
193 * can use 'poll' or 'select' to find out when the event
194 * count increases.
195 *
196 * Events are:
197 * start array, stop array, error, add device, remove device,
198 * start build, activate spare
199 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800200static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800201static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100202void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800203{
204 atomic_inc(&md_event_count);
205 wake_up(&md_event_waiters);
206}
NeilBrown29269552006-03-27 01:18:10 -0800207EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800208
209/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * Enables to iterate over all existing md arrays
211 * all_mddevs_lock protects this list.
212 */
213static LIST_HEAD(all_mddevs);
214static DEFINE_SPINLOCK(all_mddevs_lock);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
217 * iterates through all used mddevs in the system.
218 * We take care to grab the all_mddevs_lock whenever navigating
219 * the list, and to always hold a refcount when unlocked.
220 * Any code which breaks out of this loop while own
221 * a reference to the current mddev and must mddev_put it.
222 */
NeilBrownfd01b882011-10-11 16:47:53 +1100223#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000225 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100226 _tmp = all_mddevs.next; \
227 _mddev = NULL;}); \
228 ({ if (_tmp != &all_mddevs) \
229 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100231 if (_mddev) mddev_put(_mddev); \
232 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
233 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100235 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 )
237
NeilBrown409c57f2009-03-31 14:39:39 +1100238/* Rather than calling directly into the personality make_request function,
239 * IO requests come here first so that we can check if the device is
240 * being suspended pending a reconfiguration.
241 * We hold a refcount over the call to ->make_request. By the time that
242 * call has finished, the bio has been linked into some internal structure
243 * and so is visible to ->quiesce(), so we don't need the refcount any more.
244 */
Jens Axboedece1632015-11-05 10:41:16 -0700245static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
NeilBrown49077322010-03-25 16:20:56 +1100247 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100248 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500249 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800250 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100251
Kent Overstreet54efd502015-04-23 22:37:18 -0700252 blk_queue_split(q, &bio, q->bio_split);
253
NeilBrown274d8cb2016-01-04 16:16:58 +1100254 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100255 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700256 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100257 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100258 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200259 if (bio_sectors(bio) != 0)
260 bio->bi_error = -EROFS;
261 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700262 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100263 }
NeilBrown0ca69882011-01-14 09:14:33 +1100264 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100265 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200266 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100267 DEFINE_WAIT(__wait);
268 for (;;) {
269 prepare_to_wait(&mddev->sb_wait, &__wait,
270 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200271 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100272 break;
273 rcu_read_unlock();
274 schedule();
275 rcu_read_lock();
276 }
277 finish_wait(&mddev->sb_wait, &__wait);
278 }
279 atomic_inc(&mddev->active_io);
280 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100281
Chris Masone91ece52011-02-07 19:21:48 -0500282 /*
283 * save the sectors now since our bio can
284 * go away inside make_request
285 */
286 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200287 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100288
Gu Zheng74672d02015-04-03 08:44:47 +0800289 cpu = part_stat_lock();
290 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
291 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
292 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100293
NeilBrown409c57f2009-03-31 14:39:39 +1100294 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
295 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700296
297 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
NeilBrown9e35b992010-04-06 14:23:02 +1000300/* mddev_suspend makes sure no new requests are submitted
301 * to the device, and that any requests that have been submitted
302 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100303 * Once mddev_detach() is called and completes, the module will be
304 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000305 */
NeilBrownfd01b882011-10-11 16:47:53 +1100306void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100307{
Shaohua Li70d9798b2016-02-24 17:41:01 -0800308 WARN_ON_ONCE(current == mddev->thread->tsk);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100309 if (mddev->suspended++)
310 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100311 synchronize_rcu();
312 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
313 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500314
315 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100316}
NeilBrown390ee602010-06-01 19:37:27 +1000317EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100318
NeilBrownfd01b882011-10-11 16:47:53 +1100319void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100320{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100321 if (--mddev->suspended)
322 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100323 wake_up(&mddev->sb_wait);
324 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500325
Jonathan Brassow47525e52012-05-22 13:55:29 +1000326 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500327 md_wakeup_thread(mddev->thread);
328 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100329}
NeilBrown390ee602010-06-01 19:37:27 +1000330EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100331
NeilBrownfd01b882011-10-11 16:47:53 +1100332int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000333{
NeilBrown5c675f82014-12-15 12:56:56 +1100334 struct md_personality *pers = mddev->pers;
335 int ret = 0;
336
337 rcu_read_lock();
338 if (mddev->suspended)
339 ret = 1;
340 else if (pers && pers->congested)
341 ret = pers->congested(mddev, bits);
342 rcu_read_unlock();
343 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000344}
NeilBrown5c675f82014-12-15 12:56:56 +1100345EXPORT_SYMBOL_GPL(mddev_congested);
346static int md_congested(void *data, int bits)
347{
348 struct mddev *mddev = data;
349 return mddev_congested(mddev, bits);
350}
NeilBrown3fa841d2009-09-23 18:10:29 +1000351
NeilBrowna2826aa2009-12-14 12:49:49 +1100352/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200353 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100354 */
355
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200356static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100357{
NeilBrown3cb03002011-10-11 16:45:26 +1100358 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100359 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100360
361 rdev_dec_pending(rdev, mddev);
362
363 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200364 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200365 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100366 }
367 bio_put(bio);
368}
369
NeilBrowna7a07e62010-12-09 16:04:25 +1100370static void md_submit_flush_data(struct work_struct *ws);
371
NeilBrowna035fc32010-12-09 16:17:51 +1100372static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100373{
NeilBrownfd01b882011-10-11 16:47:53 +1100374 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100375 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100376
NeilBrowna7a07e62010-12-09 16:04:25 +1100377 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
378 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100380 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100381 if (rdev->raid_disk >= 0 &&
382 !test_bit(Faulty, &rdev->flags)) {
383 /* Take two references, one is dropped
384 * when request finishes, one after
385 * we reclaim rcu_read_lock
386 */
387 struct bio *bi;
388 atomic_inc(&rdev->nr_pending);
389 atomic_inc(&rdev->nr_pending);
390 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000391 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200392 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100393 bi->bi_private = rdev;
394 bi->bi_bdev = rdev->bdev;
395 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200396 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100397 rcu_read_lock();
398 rdev_dec_pending(rdev, mddev);
399 }
400 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100401 if (atomic_dec_and_test(&mddev->flush_pending))
402 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100403}
404
Tejun Heoe9c74692010-09-03 11:56:18 +0200405static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100406{
NeilBrownfd01b882011-10-11 16:47:53 +1100407 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200408 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100409
Kent Overstreet4f024f32013-10-11 15:44:27 -0700410 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200412 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100413 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200414 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200415 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100416 }
NeilBrown2b74e122010-12-09 15:59:01 +1100417
418 mddev->flush_bio = NULL;
419 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100420}
421
NeilBrownfd01b882011-10-11 16:47:53 +1100422void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100423{
NeilBrown85572d72014-12-15 12:56:56 +1100424 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200426 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100427 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200428 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100429 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100430
NeilBrowna035fc32010-12-09 16:17:51 +1100431 INIT_WORK(&mddev->flush_work, submit_flushes);
432 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100433}
Tejun Heoe9c74692010-09-03 11:56:18 +0200434EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100435
NeilBrown74018dc2012-07-31 09:08:15 +0200436void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000437{
NeilBrown9cbb1752012-07-31 09:08:14 +0200438 struct mddev *mddev = cb->data;
439 md_wakeup_thread(mddev->thread);
440 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000441}
NeilBrown9cbb1752012-07-31 09:08:14 +0200442EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000443
NeilBrownfd01b882011-10-11 16:47:53 +1100444static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 atomic_inc(&mddev->active);
447 return mddev;
448}
449
Dan Williams5fd3a172009-03-04 00:57:25 -0700450static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100451
NeilBrownfd01b882011-10-11 16:47:53 +1100452static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
NeilBrowna167f662010-10-26 18:31:13 +1100454 struct bio_set *bs = NULL;
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
457 return;
NeilBrownd3374822009-01-09 08:31:10 +1100458 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100459 mddev->ctime == 0 && !mddev->hold_active) {
460 /* Array is not configured at all, and not held active,
461 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100462 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100463 bs = mddev->bio_set;
464 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100465 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200466 /* We did a probe so need to clean up. Call
467 * queue_work inside the spinlock so that
468 * flush_workqueue() after mddev_find will
469 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100470 */
471 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200472 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100473 } else
474 kfree(mddev);
475 }
476 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100477 if (bs)
478 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Sasha Levin25b2edf2015-07-24 18:19:58 -0400481static void md_safemode_timeout(unsigned long data);
482
NeilBrownfd01b882011-10-11 16:47:53 +1100483void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100484{
485 mutex_init(&mddev->open_mutex);
486 mutex_init(&mddev->reconfig_mutex);
487 mutex_init(&mddev->bitmap_info.mutex);
488 INIT_LIST_HEAD(&mddev->disks);
489 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400490 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
491 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100492 atomic_set(&mddev->active, 1);
493 atomic_set(&mddev->openers, 0);
494 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100495 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100496 atomic_set(&mddev->flush_pending, 0);
497 init_waitqueue_head(&mddev->sb_wait);
498 init_waitqueue_head(&mddev->recovery_wait);
499 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000500 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500501 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100502 mddev->resync_min = 0;
503 mddev->resync_max = MaxSector;
504 mddev->level = LEVEL_NONE;
505}
NeilBrown390ee602010-06-01 19:37:27 +1000506EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100507
NeilBrownf72ffdd2014-09-30 14:23:59 +1000508static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
NeilBrownfd01b882011-10-11 16:47:53 +1100510 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
NeilBrown8f5f02c2011-02-16 13:58:51 +1100512 if (unit && MAJOR(unit) != MD_MAJOR)
513 unit &= ~((1<<MdpMinorShift)-1);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 retry:
516 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
NeilBrownefeb53c2009-01-09 08:31:10 +1100518 if (unit) {
519 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
520 if (mddev->unit == unit) {
521 mddev_get(mddev);
522 spin_unlock(&all_mddevs_lock);
523 kfree(new);
524 return mddev;
525 }
526
527 if (new) {
528 list_add(&new->all_mddevs, &all_mddevs);
529 spin_unlock(&all_mddevs_lock);
530 new->hold_active = UNTIL_IOCTL;
531 return new;
532 }
533 } else if (new) {
534 /* find an unused unit number */
535 static int next_minor = 512;
536 int start = next_minor;
537 int is_free = 0;
538 int dev = 0;
539 while (!is_free) {
540 dev = MKDEV(MD_MAJOR, next_minor);
541 next_minor++;
542 if (next_minor > MINORMASK)
543 next_minor = 0;
544 if (next_minor == start) {
545 /* Oh dear, all in use. */
546 spin_unlock(&all_mddevs_lock);
547 kfree(new);
548 return NULL;
549 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000550
NeilBrownefeb53c2009-01-09 08:31:10 +1100551 is_free = 1;
552 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
553 if (mddev->unit == dev) {
554 is_free = 0;
555 break;
556 }
557 }
558 new->unit = dev;
559 new->md_minor = MINOR(dev);
560 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 list_add(&new->all_mddevs, &all_mddevs);
562 spin_unlock(&all_mddevs_lock);
563 return new;
564 }
565 spin_unlock(&all_mddevs_lock);
566
NeilBrown9ffae0c2006-01-06 00:20:32 -0800567 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!new)
569 return NULL;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 new->unit = unit;
572 if (MAJOR(unit) == MD_MAJOR)
573 new->md_minor = MINOR(unit);
574 else
575 new->md_minor = MINOR(unit) >> MdpMinorShift;
576
NeilBrownfafd7fb2010-04-01 15:55:30 +1100577 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto retry;
580}
581
NeilBrownb6eb1272010-04-15 10:13:47 +1000582static struct attribute_group md_redundancy_group;
583
NeilBrown5c47daf2014-12-15 12:57:01 +1100584void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
NeilBrowna64c8762010-04-14 17:15:37 +1000586 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000587 /* These cannot be removed under reconfig_mutex as
588 * an access to the files will try to take reconfig_mutex
589 * while holding the file unremovable, which leads to
590 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000591 * So hold set sysfs_active while the remove in happeing,
592 * and anything else which might set ->to_remove or my
593 * otherwise change the sysfs namespace will fail with
594 * -EBUSY if sysfs_active is still set.
595 * We set sysfs_active under reconfig_mutex and elsewhere
596 * test it under the same mutex to ensure its correct value
597 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000598 */
NeilBrowna64c8762010-04-14 17:15:37 +1000599 struct attribute_group *to_remove = mddev->to_remove;
600 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000601 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000602 mutex_unlock(&mddev->reconfig_mutex);
603
NeilBrown00bcb4a2010-06-01 19:37:23 +1000604 if (mddev->kobj.sd) {
605 if (to_remove != &md_redundancy_group)
606 sysfs_remove_group(&mddev->kobj, to_remove);
607 if (mddev->pers == NULL ||
608 mddev->pers->sync_request == NULL) {
609 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
610 if (mddev->sysfs_action)
611 sysfs_put(mddev->sysfs_action);
612 mddev->sysfs_action = NULL;
613 }
NeilBrowna64c8762010-04-14 17:15:37 +1000614 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000615 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000616 } else
617 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Chris Dunlop751e67c2011-10-19 16:48:26 +1100619 /* As we've dropped the mutex we need a spinlock to
620 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000621 */
622 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700623 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000624 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
NeilBrown5c47daf2014-12-15 12:57:01 +1100626EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500628struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100629{
630 struct md_rdev *rdev;
631
632 rdev_for_each_rcu(rdev, mddev)
633 if (rdev->desc_nr == nr)
634 return rdev;
635
636 return NULL;
637}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500638EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100639
640static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
NeilBrown3cb03002011-10-11 16:45:26 +1100642 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
NeilBrowndafb20f2012-03-19 12:46:39 +1100644 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (rdev->bdev->bd_dev == dev)
646 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return NULL;
649}
650
NeilBrown1ca69c42012-10-11 13:37:33 +1100651static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
652{
653 struct md_rdev *rdev;
654
655 rdev_for_each_rcu(rdev, mddev)
656 if (rdev->bdev->bd_dev == dev)
657 return rdev;
658
659 return NULL;
660}
661
NeilBrown84fc4b52011-10-11 16:49:58 +1100662static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800663{
NeilBrown84fc4b52011-10-11 16:49:58 +1100664 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800665 list_for_each_entry(pers, &pers_list, list) {
666 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800667 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800668 if (strcmp(pers->name, clevel)==0)
669 return pers;
670 }
NeilBrown2604b702006-01-06 00:20:36 -0800671 return NULL;
672}
673
Andre Nollb73df2d2008-07-11 22:02:23 +1000674/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100675static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100677 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000678 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
NeilBrownf72ffdd2014-09-30 14:23:59 +1000681static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 rdev->sb_page = alloc_page(GFP_KERNEL);
684 if (!rdev->sb_page) {
685 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000686 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
689 return 0;
690}
691
NeilBrown545c8792012-05-22 13:54:30 +1000692void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800695 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 rdev->sb_loaded = 0;
697 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000698 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100699 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
NeilBrown2699b672011-07-28 11:31:47 +1000701 if (rdev->bb_page) {
702 put_page(rdev->bb_page);
703 rdev->bb_page = NULL;
704 }
Dan Williamsd3b407f2016-01-06 12:19:22 -0800705 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
NeilBrown545c8792012-05-22 13:54:30 +1000707EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200709static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700710{
NeilBrown3cb03002011-10-11 16:45:26 +1100711 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100712 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700713
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200714 if (bio->bi_error) {
715 printk("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800716 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700717 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700718
NeilBrowna9701a32005-11-08 21:39:34 -0800719 if (atomic_dec_and_test(&mddev->pending_writes))
720 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700721 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700722 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700723}
724
NeilBrownfd01b882011-10-11 16:47:53 +1100725void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700726 sector_t sector, int size, struct page *page)
727{
728 /* write first size bytes of page to sector of rdev
729 * Increment mddev->pending_writes before returning
730 * and decrement it on completion, waking up sb_wait
731 * if zero is reached.
732 * If an error occurred, call md_error
733 */
NeilBrowna167f662010-10-26 18:31:13 +1100734 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700735
Shaohua Lied3b98c2016-03-29 14:00:19 -0700736 atomic_inc(&rdev->nr_pending);
737
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100738 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700739 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700740 bio_add_page(bio, page, size, 0);
741 bio->bi_private = rdev;
742 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800743
NeilBrown7bfa19f2005-06-21 17:17:28 -0700744 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000745 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800746}
747
NeilBrownfd01b882011-10-11 16:47:53 +1100748void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800749{
Tejun Heoe9c74692010-09-03 11:56:18 +0200750 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000751 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700752}
753
NeilBrown3cb03002011-10-11 16:45:26 +1100754int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100755 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
NeilBrowna167f662010-10-26 18:31:13 +1100757 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 int ret;
759
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100760 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
761 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100762 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700763 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000764 else if (rdev->mddev->reshape_position != MaxSector &&
765 (rdev->mddev->reshape_backwards ==
766 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700767 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100768 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700769 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700771 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200773 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 bio_put(bio);
775 return ret;
776}
NeilBrowna8745db2006-01-06 00:20:34 -0800777EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
NeilBrownf72ffdd2014-09-30 14:23:59 +1000779static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (rdev->sb_loaded)
784 return 0;
785
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100786 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto fail;
788 rdev->sb_loaded = 1;
789 return 0;
790
791fail:
792 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
793 bdevname(rdev->bdev,b));
794 return -EINVAL;
795}
796
797static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
798{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000799 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000800 sb1->set_uuid1 == sb2->set_uuid1 &&
801 sb1->set_uuid2 == sb2->set_uuid2 &&
802 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
806{
807 int ret;
808 mdp_super_t *tmp1, *tmp2;
809
810 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
811 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
812
813 if (!tmp1 || !tmp2) {
814 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100815 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 goto abort;
817 }
818
819 *tmp1 = *sb1;
820 *tmp2 = *sb2;
821
822 /*
823 * nr_disks is not constant
824 */
825 tmp1->nr_disks = 0;
826 tmp2->nr_disks = 0;
827
Andre Nollce0c8e02008-07-11 22:02:20 +1000828 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700830 kfree(tmp1);
831 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return ret;
833}
834
NeilBrown4d167f02007-05-09 02:35:37 -0700835static u32 md_csum_fold(u32 csum)
836{
837 csum = (csum & 0xffff) + (csum >> 16);
838 return (csum & 0xffff) + (csum >> 16);
839}
840
NeilBrownf72ffdd2014-09-30 14:23:59 +1000841static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
NeilBrown4d167f02007-05-09 02:35:37 -0700843 u64 newcsum = 0;
844 u32 *sb32 = (u32*)sb;
845 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 unsigned int disk_csum, csum;
847
848 disk_csum = sb->sb_csum;
849 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700850
851 for (i = 0; i < MD_SB_BYTES/4 ; i++)
852 newcsum += sb32[i];
853 csum = (newcsum & 0xffffffff) + (newcsum>>32);
854
NeilBrown4d167f02007-05-09 02:35:37 -0700855#ifdef CONFIG_ALPHA
856 /* This used to use csum_partial, which was wrong for several
857 * reasons including that different results are returned on
858 * different architectures. It isn't critical that we get exactly
859 * the same return value as before (we always csum_fold before
860 * testing, and that removes any differences). However as we
861 * know that csum_partial always returned a 16bit value on
862 * alphas, do a fold to maximise conformity to previous behaviour.
863 */
864 sb->sb_csum = md_csum_fold(disk_csum);
865#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700867#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return csum;
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
872 * Handle superblock details.
873 * We want to be able to handle multiple superblock formats
874 * so we have a common interface to them all, and an array of
875 * different handlers.
876 * We rely on user-space to write the initial superblock, and support
877 * reading and updating of superblocks.
878 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100879 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * loads and validates a superblock on dev.
881 * if refdev != NULL, compare superblocks on both devices
882 * Return:
883 * 0 - dev has a superblock that is compatible with refdev
884 * 1 - dev has a superblock that is compatible and newer than refdev
885 * so dev should be used as the refdev in future
886 * -EINVAL superblock incompatible or invalid
887 * -othererror e.g. -EIO
888 *
NeilBrownfd01b882011-10-11 16:47:53 +1100889 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 * Verify that dev is acceptable into mddev.
891 * The first time, mddev->raid_disks will be 0, and data from
892 * dev should be merged in. Subsequent calls check that dev
893 * is new enough. Return 0 or -EINVAL
894 *
NeilBrownfd01b882011-10-11 16:47:53 +1100895 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * Update the superblock for rdev with data in mddev
897 * This does not write to disc.
898 *
899 */
900
901struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000902 char *name;
903 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000904 int (*load_super)(struct md_rdev *rdev,
905 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000906 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000907 int (*validate_super)(struct mddev *mddev,
908 struct md_rdev *rdev);
909 void (*sync_super)(struct mddev *mddev,
910 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100911 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000912 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000913 int (*allow_new_offset)(struct md_rdev *rdev,
914 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915};
916
917/*
Andre Noll0894cc32009-06-18 08:49:23 +1000918 * Check that the given mddev has no bitmap.
919 *
920 * This function is called from the run method of all personalities that do not
921 * support bitmaps. It prints an error message and returns non-zero if mddev
922 * has a bitmap. Otherwise, it returns 0.
923 *
924 */
NeilBrownfd01b882011-10-11 16:47:53 +1100925int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000926{
NeilBrownc3d97142009-12-14 12:49:52 +1100927 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000928 return 0;
929 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
930 mdname(mddev), mddev->pers->name);
931 return 1;
932}
933EXPORT_SYMBOL(md_check_no_bitmap);
934
935/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000936 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
NeilBrown3cb03002011-10-11 16:45:26 +1100938static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
941 mdp_super_t *sb;
942 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /*
Andre Noll0f420352008-07-11 22:02:23 +1000945 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * it's at the end of the disk.
947 *
948 * It also happens to be a multiple of 4Kb.
949 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100950 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
NeilBrown0002b272005-09-09 16:23:53 -0700952 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (ret) return ret;
954
955 ret = -EINVAL;
956
957 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000958 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (sb->md_magic != MD_SB_MAGIC) {
961 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
962 b);
963 goto abort;
964 }
965
966 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800967 sb->minor_version < 90 ||
968 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
970 sb->major_version, sb->minor_version,
971 b);
972 goto abort;
973 }
974
975 if (sb->raid_disks <= 0)
976 goto abort;
977
NeilBrown4d167f02007-05-09 02:35:37 -0700978 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
980 b);
981 goto abort;
982 }
983
984 rdev->preferred_minor = sb->md_minor;
985 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000986 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700987 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000988 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (sb->level == LEVEL_MULTIPATH)
991 rdev->desc_nr = -1;
992 else
993 rdev->desc_nr = sb->this_disk.number;
994
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700995 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700997 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +1000999 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!uuid_equal(refsb, sb)) {
1001 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1002 b, bdevname(refdev->bdev,b2));
1003 goto abort;
1004 }
1005 if (!sb_equal(refsb, sb)) {
1006 printk(KERN_WARNING "md: %s has same UUID"
1007 " but different superblock to %s\n",
1008 b, bdevname(refdev->bdev, b2));
1009 goto abort;
1010 }
1011 ev1 = md_event(sb);
1012 ev2 = md_event(refsb);
1013 if (ev1 > ev2)
1014 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001015 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 ret = 0;
1017 }
NeilBrown8190e752009-06-18 08:48:58 +10001018 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001019 /* Limit to 4TB as metadata cannot record more than that.
1020 * (not needed for Linear and RAID0 as metadata doesn't
1021 * record this size)
1022 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001023 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1024 sb->level >= 1)
1025 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
NeilBrown27a7b262011-09-10 17:21:28 +10001027 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001028 /* "this cannot possibly happen" ... */
1029 ret = -EINVAL;
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 abort:
1032 return ret;
1033}
1034
1035/*
1036 * validate_super for 0.90.0
1037 */
NeilBrownfd01b882011-10-11 16:47:53 +11001038static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001041 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001042 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
NeilBrown41158c72005-06-21 17:17:25 -07001044 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001045 clear_bit(Faulty, &rdev->flags);
1046 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001047 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001048 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (mddev->raid_disks == 0) {
1051 mddev->major_version = 0;
1052 mddev->minor_version = sb->minor_version;
1053 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001054 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001055 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 mddev->ctime = sb->ctime;
1057 mddev->utime = sb->utime;
1058 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001059 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 mddev->layout = sb->layout;
1061 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001062 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001063 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001064 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001065 mddev->bitmap_info.space = 0;
1066 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001067 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001068 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001069 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
NeilBrownf6705572006-03-27 01:18:11 -08001071 if (mddev->minor_version >= 91) {
1072 mddev->reshape_position = sb->reshape_position;
1073 mddev->delta_disks = sb->delta_disks;
1074 mddev->new_level = sb->new_level;
1075 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001076 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001077 if (mddev->delta_disks < 0)
1078 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001079 } else {
1080 mddev->reshape_position = MaxSector;
1081 mddev->delta_disks = 0;
1082 mddev->new_level = mddev->level;
1083 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001084 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001085 }
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (sb->state & (1<<MD_SB_CLEAN))
1088 mddev->recovery_cp = MaxSector;
1089 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001090 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 sb->events_lo == sb->cp_events_lo) {
1092 mddev->recovery_cp = sb->recovery_cp;
1093 } else
1094 mddev->recovery_cp = 0;
1095 }
1096
1097 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1098 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1099 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1100 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1101
1102 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001103
1104 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001105 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001106 mddev->bitmap_info.offset =
1107 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001108 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001109 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001110 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001111
NeilBrown41158c72005-06-21 17:17:25 -07001112 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001113 /* Insist on good event counter while assembling, except
1114 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001116 if (sb->disks[rdev->desc_nr].state & (
1117 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001118 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001119 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001120 } else if (mddev->bitmap) {
1121 /* if adding to array with a bitmap, then we can accept an
1122 * older device ... but not too old.
1123 */
NeilBrown41158c72005-06-21 17:17:25 -07001124 if (ev1 < mddev->bitmap->events_cleared)
1125 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001126 if (ev1 < mddev->events)
1127 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001128 } else {
1129 if (ev1 < mddev->events)
1130 /* just a hot-add of a new device, leave raid_disk at -1 */
1131 return 0;
1132 }
NeilBrown41158c72005-06-21 17:17:25 -07001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 desc = sb->disks + rdev->desc_nr;
1136
1137 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001138 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001139 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1140 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001141 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001143 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001144 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1145 /* active but not in sync implies recovery up to
1146 * reshape position. We don't know exactly where
1147 * that is, so set to zero for now */
1148 if (mddev->minor_version >= 91) {
1149 rdev->recovery_offset = 0;
1150 rdev->raid_disk = desc->raid_disk;
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001153 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1154 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001155 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001156 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return 0;
1158}
1159
1160/*
1161 * sync_super for 0.90.0
1162 */
NeilBrownfd01b882011-10-11 16:47:53 +11001163static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001166 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* make rdev->sb match mddev data..
1170 *
1171 * 1/ zero out disks
1172 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1173 * 3/ any empty disks < next_spare become removed
1174 *
1175 * disks[0] gets initialised to REMOVED because
1176 * we cannot be sure from other fields if it has
1177 * been initialised or not.
1178 */
1179 int i;
1180 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1181
NeilBrown61181562005-09-09 16:24:02 -07001182 rdev->sb_size = MD_SB_BYTES;
1183
Namhyung Kim65a06f062011-07-27 11:00:36 +10001184 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 memset(sb, 0, sizeof(*sb));
1187
1188 sb->md_magic = MD_SB_MAGIC;
1189 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 sb->patch_version = mddev->patch_version;
1191 sb->gvalid_words = 0; /* ignored */
1192 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1193 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1194 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1195 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1196
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001197 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001199 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 sb->raid_disks = mddev->raid_disks;
1201 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001202 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001203 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 sb->state = 0;
1205 sb->events_hi = (mddev->events>>32);
1206 sb->events_lo = (u32)mddev->events;
1207
NeilBrownf6705572006-03-27 01:18:11 -08001208 if (mddev->reshape_position == MaxSector)
1209 sb->minor_version = 90;
1210 else {
1211 sb->minor_version = 91;
1212 sb->reshape_position = mddev->reshape_position;
1213 sb->new_level = mddev->new_level;
1214 sb->delta_disks = mddev->delta_disks;
1215 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001216 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001217 }
1218 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (mddev->in_sync)
1220 {
1221 sb->recovery_cp = mddev->recovery_cp;
1222 sb->cp_events_hi = (mddev->events>>32);
1223 sb->cp_events_lo = (u32)mddev->events;
1224 if (mddev->recovery_cp == MaxSector)
1225 sb->state = (1<< MD_SB_CLEAN);
1226 } else
1227 sb->recovery_cp = 0;
1228
1229 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001230 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
NeilBrownc3d97142009-12-14 12:49:52 +11001232 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001233 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001236 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001238 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001239 int is_active = test_bit(In_sync, &rdev2->flags);
1240
1241 if (rdev2->raid_disk >= 0 &&
1242 sb->minor_version >= 91)
1243 /* we have nowhere to store the recovery_offset,
1244 * but if it is not below the reshape_position,
1245 * we can piggy-back on that.
1246 */
1247 is_active = 1;
1248 if (rdev2->raid_disk < 0 ||
1249 test_bit(Faulty, &rdev2->flags))
1250 is_active = 0;
1251 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001252 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001254 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001255 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 d = &sb->disks[rdev2->desc_nr];
1257 nr_disks++;
1258 d->number = rdev2->desc_nr;
1259 d->major = MAJOR(rdev2->bdev->bd_dev);
1260 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001261 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 d->raid_disk = rdev2->raid_disk;
1263 else
1264 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001265 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001267 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001269 if (test_bit(In_sync, &rdev2->flags))
1270 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 active++;
1272 working++;
1273 } else {
1274 d->state = 0;
1275 spare++;
1276 working++;
1277 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001278 if (test_bit(WriteMostly, &rdev2->flags))
1279 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /* now set the "removed" and "faulty" bits on any missing devices */
1282 for (i=0 ; i < mddev->raid_disks ; i++) {
1283 mdp_disk_t *d = &sb->disks[i];
1284 if (d->state == 0 && d->number == 0) {
1285 d->number = i;
1286 d->raid_disk = i;
1287 d->state = (1<<MD_DISK_REMOVED);
1288 d->state |= (1<<MD_DISK_FAULTY);
1289 failed++;
1290 }
1291 }
1292 sb->nr_disks = nr_disks;
1293 sb->active_disks = active;
1294 sb->working_disks = working;
1295 sb->failed_disks = failed;
1296 sb->spare_disks = spare;
1297
1298 sb->this_disk = sb->disks[rdev->desc_nr];
1299 sb->sb_csum = calc_sb_csum(sb);
1300}
1301
1302/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001303 * rdev_size_change for 0.90.0
1304 */
1305static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001306super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001307{
Andre Noll58c0fed2009-03-31 14:33:13 +11001308 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001309 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001310 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001311 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001312 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001313 if (!num_sectors || num_sectors > rdev->sb_start)
1314 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001315 /* Limit to 4TB as metadata cannot record more than that.
1316 * 4TB == 2^32 KB, or 2*2^32 sectors.
1317 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001318 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1319 rdev->mddev->level >= 1)
1320 num_sectors = (sector_t)(2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001321 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001322 rdev->sb_page);
1323 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001324 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325}
1326
NeilBrownc6563a82012-05-21 09:27:00 +10001327static int
1328super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1329{
1330 /* non-zero offset changes not possible with v0.90 */
1331 return new_offset == 0;
1332}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001333
1334/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * version 1 superblock
1336 */
1337
NeilBrownf72ffdd2014-09-30 14:23:59 +10001338static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001340 __le32 disk_csum;
1341 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 unsigned long long newcsum;
1343 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001344 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 disk_csum = sb->sb_csum;
1347 sb->sb_csum = 0;
1348 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001349 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 newcsum += le32_to_cpu(*isuper++);
1351
1352 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001353 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1356 sb->sb_csum = disk_csum;
1357 return cpu_to_le32(csum);
1358}
1359
NeilBrown3cb03002011-10-11 16:45:26 +11001360static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct mdp_superblock_1 *sb;
1363 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001364 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001365 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001367 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /*
Andre Noll0f420352008-07-11 22:02:23 +10001370 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * It is always aligned to a 4K boundary and
1372 * depeding on minor_version, it can be:
1373 * 0: At least 8K, but less than 12K, from end of device
1374 * 1: At start of device
1375 * 2: 4K from start of device.
1376 */
1377 switch(minor_version) {
1378 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001379 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001380 sb_start -= 8*2;
1381 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 break;
1383 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001384 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 break;
1386 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001387 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389 default:
1390 return -EINVAL;
1391 }
Andre Noll0f420352008-07-11 22:02:23 +10001392 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
NeilBrown0002b272005-09-09 16:23:53 -07001394 /* superblock is rarely larger than 1K, but it can be larger,
1395 * and it is safe to read 4k, so we do that
1396 */
1397 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (ret) return ret;
1399
Namhyung Kim65a06f062011-07-27 11:00:36 +10001400 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1403 sb->major_version != cpu_to_le32(1) ||
1404 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001405 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001406 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return -EINVAL;
1408
1409 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1410 printk("md: invalid superblock checksum on %s\n",
1411 bdevname(rdev->bdev,b));
1412 return -EINVAL;
1413 }
1414 if (le64_to_cpu(sb->data_size) < 10) {
1415 printk("md: data_size too small on %s\n",
1416 bdevname(rdev->bdev,b));
1417 return -EINVAL;
1418 }
NeilBrownc6563a82012-05-21 09:27:00 +10001419 if (sb->pad0 ||
1420 sb->pad3[0] ||
1421 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1422 /* Some padding is non-zero, might be a new feature */
1423 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 rdev->preferred_minor = 0xffff;
1426 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001427 rdev->new_data_offset = rdev->data_offset;
1428 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1429 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1430 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001431 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
NeilBrown0002b272005-09-09 16:23:53 -07001433 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001434 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001435 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001436 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1437
1438 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001439 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001440 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001441 if (minor_version
1442 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1443 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001444
NeilBrown31b65a02006-07-10 04:44:14 -07001445 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1446 rdev->desc_nr = -1;
1447 else
1448 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1449
NeilBrown2699b672011-07-28 11:31:47 +10001450 if (!rdev->bb_page) {
1451 rdev->bb_page = alloc_page(GFP_KERNEL);
1452 if (!rdev->bb_page)
1453 return -ENOMEM;
1454 }
1455 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1456 rdev->badblocks.count == 0) {
1457 /* need to load the bad block list.
1458 * Currently we limit it to one page.
1459 */
1460 s32 offset;
1461 sector_t bb_sector;
1462 u64 *bbp;
1463 int i;
1464 int sectors = le16_to_cpu(sb->bblog_size);
1465 if (sectors > (PAGE_SIZE / 512))
1466 return -EINVAL;
1467 offset = le32_to_cpu(sb->bblog_offset);
1468 if (offset == 0)
1469 return -EINVAL;
1470 bb_sector = (long long)offset;
1471 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1472 rdev->bb_page, READ, true))
1473 return -EIO;
1474 bbp = (u64 *)page_address(rdev->bb_page);
1475 rdev->badblocks.shift = sb->bblog_shift;
1476 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1477 u64 bb = le64_to_cpu(*bbp);
1478 int count = bb & (0x3ff);
1479 u64 sector = bb >> 10;
1480 sector <<= sb->bblog_shift;
1481 count <<= sb->bblog_shift;
1482 if (bb + 1 == 0)
1483 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001484 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001485 return -EINVAL;
1486 }
NeilBrown486adf72013-04-24 11:42:44 +10001487 } else if (sb->bblog_offset != 0)
1488 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001489
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001490 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001491 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001492 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001494 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1497 sb->level != refsb->level ||
1498 sb->layout != refsb->layout ||
1499 sb->chunksize != refsb->chunksize) {
1500 printk(KERN_WARNING "md: %s has strangely different"
1501 " superblock to %s\n",
1502 bdevname(rdev->bdev,b),
1503 bdevname(refdev->bdev,b2));
1504 return -EINVAL;
1505 }
1506 ev1 = le64_to_cpu(sb->events);
1507 ev2 = le64_to_cpu(refsb->events);
1508
1509 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001510 ret = 1;
1511 else
1512 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
NeilBrownc6563a82012-05-21 09:27:00 +10001514 if (minor_version) {
1515 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1516 sectors -= rdev->data_offset;
1517 } else
1518 sectors = rdev->sb_start;
1519 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001521 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001522 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
NeilBrownfd01b882011-10-11 16:47:53 +11001525static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001527 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001528 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
NeilBrown41158c72005-06-21 17:17:25 -07001530 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001531 clear_bit(Faulty, &rdev->flags);
1532 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001533 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001534 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (mddev->raid_disks == 0) {
1537 mddev->major_version = 1;
1538 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001539 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001540 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001541 mddev->ctime = le64_to_cpu(sb->ctime);
1542 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001544 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 mddev->layout = le32_to_cpu(sb->layout);
1546 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001547 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001548 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001549 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001550 mddev->bitmap_info.space = 0;
1551 /* Default location for bitmap is 1K after superblock
1552 * using 3K - total of 4K
1553 */
NeilBrownc3d97142009-12-14 12:49:52 +11001554 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001555 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001556 mddev->reshape_backwards = 0;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1559 memcpy(mddev->uuid, sb->set_uuid, 16);
1560
1561 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001562
NeilBrown71c08052005-09-09 16:23:51 -07001563 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001564 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001565 mddev->bitmap_info.offset =
1566 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001567 /* Metadata doesn't record how much space is available.
1568 * For 1.0, we assume we can use up to the superblock
1569 * if before, else to 4K beyond superblock.
1570 * For others, assume no change is possible.
1571 */
1572 if (mddev->minor_version > 0)
1573 mddev->bitmap_info.space = 0;
1574 else if (mddev->bitmap_info.offset > 0)
1575 mddev->bitmap_info.space =
1576 8 - mddev->bitmap_info.offset;
1577 else
1578 mddev->bitmap_info.space =
1579 -mddev->bitmap_info.offset;
1580 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001581
NeilBrownf6705572006-03-27 01:18:11 -08001582 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1583 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1584 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1585 mddev->new_level = le32_to_cpu(sb->new_level);
1586 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001587 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001588 if (mddev->delta_disks < 0 ||
1589 (mddev->delta_disks == 0 &&
1590 (le32_to_cpu(sb->feature_map)
1591 & MD_FEATURE_RESHAPE_BACKWARDS)))
1592 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001593 } else {
1594 mddev->reshape_position = MaxSector;
1595 mddev->delta_disks = 0;
1596 mddev->new_level = mddev->level;
1597 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001598 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001599 }
1600
Shaohua Lia62ab492016-01-06 14:37:13 -08001601 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) {
1602 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1603 if (mddev->recovery_cp == MaxSector)
1604 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
1605 }
NeilBrown41158c72005-06-21 17:17:25 -07001606 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001607 /* Insist of good event counter while assembling, except for
1608 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001610 if (rdev->desc_nr >= 0 &&
1611 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001612 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1613 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001614 if (ev1 < mddev->events)
1615 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001616 } else if (mddev->bitmap) {
1617 /* If adding to array with a bitmap, then we can accept an
1618 * older device, but not too old.
1619 */
NeilBrown41158c72005-06-21 17:17:25 -07001620 if (ev1 < mddev->bitmap->events_cleared)
1621 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001622 if (ev1 < mddev->events)
1623 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001624 } else {
1625 if (ev1 < mddev->events)
1626 /* just a hot-add of a new device, leave raid_disk at -1 */
1627 return 0;
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (mddev->level != LEVEL_MULTIPATH) {
1630 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001631 if (rdev->desc_nr < 0 ||
1632 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001633 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001634 rdev->desc_nr = -1;
1635 } else
1636 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001638 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001640 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001641 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 break;
Song Liubac624f2015-08-13 14:31:55 -07001643 case MD_DISK_ROLE_JOURNAL: /* journal device */
1644 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1645 /* journal device without journal feature */
1646 printk(KERN_WARNING
1647 "md: journal device provided without journal feature, ignoring the device\n");
1648 return -EINVAL;
1649 }
1650 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001651 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001652 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 default:
NeilBrownf4667222013-12-09 12:04:56 +11001655 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001656 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001657 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001658 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001659 if (!(le32_to_cpu(sb->feature_map) &
1660 MD_FEATURE_RECOVERY_BITMAP))
1661 rdev->saved_raid_disk = -1;
1662 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001663 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 rdev->raid_disk = role;
1665 break;
1666 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001667 if (sb->devflags & WriteMostly1)
1668 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001669 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1670 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001671 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001672 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return 0;
1675}
1676
NeilBrownfd01b882011-10-11 16:47:53 +11001677static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001680 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int max_dev, i;
1682 /* make rdev->sb match mddev and rdev data. */
1683
Namhyung Kim65a06f062011-07-27 11:00:36 +10001684 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 sb->feature_map = 0;
1687 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001688 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 memset(sb->pad3, 0, sizeof(sb->pad3));
1690
1691 sb->utime = cpu_to_le64((__u64)mddev->utime);
1692 sb->events = cpu_to_le64(mddev->events);
1693 if (mddev->in_sync)
1694 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001695 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1696 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 else
1698 sb->resync_offset = cpu_to_le64(0);
1699
NeilBrown1c05b4b2006-10-21 10:24:08 -07001700 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001701
NeilBrownf0ca3402006-02-02 14:28:04 -08001702 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001703 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001704 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001705 sb->level = cpu_to_le32(mddev->level);
1706 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001707
NeilBrownaeb9b212011-08-25 14:43:08 +10001708 if (test_bit(WriteMostly, &rdev->flags))
1709 sb->devflags |= WriteMostly1;
1710 else
1711 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001712 sb->data_offset = cpu_to_le64(rdev->data_offset);
1713 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001714
NeilBrownc3d97142009-12-14 12:49:52 +11001715 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1716 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001717 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001718 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001719
Shaohua Lif2076e72015-10-08 21:54:12 -07001720 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001721 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001722 sb->feature_map |=
1723 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1724 sb->recovery_offset =
1725 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001726 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1727 sb->feature_map |=
1728 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001729 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001730 /* Note: recovery_offset and journal_tail share space */
1731 if (test_bit(Journal, &rdev->flags))
1732 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001733 if (test_bit(Replacement, &rdev->flags))
1734 sb->feature_map |=
1735 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001736
NeilBrownf6705572006-03-27 01:18:11 -08001737 if (mddev->reshape_position != MaxSector) {
1738 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1739 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1740 sb->new_layout = cpu_to_le32(mddev->new_layout);
1741 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1742 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001743 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001744 if (mddev->delta_disks == 0 &&
1745 mddev->reshape_backwards)
1746 sb->feature_map
1747 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001748 if (rdev->new_data_offset != rdev->data_offset) {
1749 sb->feature_map
1750 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1751 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1752 - rdev->data_offset));
1753 }
NeilBrownf6705572006-03-27 01:18:11 -08001754 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001755
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001756 if (mddev_is_clustered(mddev))
1757 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1758
NeilBrown2699b672011-07-28 11:31:47 +10001759 if (rdev->badblocks.count == 0)
1760 /* Nothing to do for bad blocks*/ ;
1761 else if (sb->bblog_offset == 0)
1762 /* Cannot record bad blocks on this device */
1763 md_error(mddev, rdev);
1764 else {
1765 struct badblocks *bb = &rdev->badblocks;
1766 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1767 u64 *p = bb->page;
1768 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1769 if (bb->changed) {
1770 unsigned seq;
1771
1772retry:
1773 seq = read_seqbegin(&bb->lock);
1774
1775 memset(bbp, 0xff, PAGE_SIZE);
1776
1777 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001778 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001779 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1780 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001781 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001782 }
NeilBrownd0962932012-03-19 12:46:41 +11001783 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001784 if (read_seqretry(&bb->lock, seq))
1785 goto retry;
1786
1787 bb->sector = (rdev->sb_start +
1788 (int)le32_to_cpu(sb->bblog_offset));
1789 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001790 }
1791 }
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001794 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (rdev2->desc_nr+1 > max_dev)
1796 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001797
NeilBrown70471da2009-08-03 10:59:57 +10001798 if (max_dev > le32_to_cpu(sb->max_dev)) {
1799 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001800 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001801 rdev->sb_size = max_dev * 2 + 256;
1802 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1803 if (rdev->sb_size & bmask)
1804 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001805 } else
1806 max_dev = le32_to_cpu(sb->max_dev);
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001809 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001810
Song Liua97b7892015-10-08 21:54:09 -07001811 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1812 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
NeilBrowndafb20f2012-03-19 12:46:39 +11001814 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001816 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001817 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001818 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001820 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001821 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001822 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001823 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 else
Song Liuc4d4c912015-08-13 14:31:54 -07001825 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 sb->sb_csum = calc_sb_1_csum(sb);
1829}
1830
Chris Webb0cd17fe2008-06-28 08:31:46 +10001831static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001832super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001833{
1834 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001835 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001836 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001837 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001838 if (rdev->data_offset != rdev->new_data_offset)
1839 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001840 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001841 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001842 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001843 max_sectors -= rdev->data_offset;
1844 if (!num_sectors || num_sectors > max_sectors)
1845 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001846 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001847 /* minor version 0 with bitmap we can't move */
1848 return 0;
1849 } else {
1850 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001851 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001852 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001853 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001854 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001855 if (!num_sectors || num_sectors > max_sectors)
1856 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001857 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001858 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001859 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001860 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001861 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001862 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001863 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001864 rdev->sb_page);
1865 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001866 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001867
1868}
1869
1870static int
1871super_1_allow_new_offset(struct md_rdev *rdev,
1872 unsigned long long new_offset)
1873{
1874 /* All necessary checks on new >= old have been done */
1875 struct bitmap *bitmap;
1876 if (new_offset >= rdev->data_offset)
1877 return 1;
1878
1879 /* with 1.0 metadata, there is no metadata to tread on
1880 * so we can always move back */
1881 if (rdev->mddev->minor_version == 0)
1882 return 1;
1883
1884 /* otherwise we must be sure not to step on
1885 * any metadata, so stay:
1886 * 36K beyond start of superblock
1887 * beyond end of badblocks
1888 * beyond write-intent bitmap
1889 */
1890 if (rdev->sb_start + (32+4)*2 > new_offset)
1891 return 0;
1892 bitmap = rdev->mddev->bitmap;
1893 if (bitmap && !rdev->mddev->bitmap_info.file &&
1894 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001895 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001896 return 0;
1897 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1898 return 0;
1899
1900 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001901}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Adrian Bunk75c96f82005-05-05 16:16:09 -07001903static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 [0] = {
1905 .name = "0.90.0",
1906 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001907 .load_super = super_90_load,
1908 .validate_super = super_90_validate,
1909 .sync_super = super_90_sync,
1910 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001911 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 },
1913 [1] = {
1914 .name = "md-1",
1915 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001916 .load_super = super_1_load,
1917 .validate_super = super_1_validate,
1918 .sync_super = super_1_sync,
1919 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001920 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 },
1922};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
NeilBrownfd01b882011-10-11 16:47:53 +11001924static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001925{
1926 if (mddev->sync_super) {
1927 mddev->sync_super(mddev, rdev);
1928 return;
1929 }
1930
1931 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1932
1933 super_types[mddev->major_version].sync_super(mddev, rdev);
1934}
1935
NeilBrownfd01b882011-10-11 16:47:53 +11001936static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
NeilBrown3cb03002011-10-11 16:45:26 +11001938 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
NeilBrown4b809912008-07-21 17:05:25 +10001940 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001941 rdev_for_each_rcu(rdev, mddev1) {
1942 if (test_bit(Faulty, &rdev->flags) ||
1943 test_bit(Journal, &rdev->flags) ||
1944 rdev->raid_disk == -1)
1945 continue;
1946 rdev_for_each_rcu(rdev2, mddev2) {
1947 if (test_bit(Faulty, &rdev2->flags) ||
1948 test_bit(Journal, &rdev2->flags) ||
1949 rdev2->raid_disk == -1)
1950 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001951 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001952 rdev2->bdev->bd_contains) {
1953 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001954 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001955 }
Song Liu0b020e82015-09-03 23:00:35 -07001956 }
1957 }
NeilBrown4b809912008-07-21 17:05:25 +10001958 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return 0;
1960}
1961
1962static LIST_HEAD(pending_raid_disks);
1963
Andre Nollac5e7112009-08-03 10:59:47 +10001964/*
1965 * Try to register data integrity profile for an mddev
1966 *
1967 * This is called when an array is started and after a disk has been kicked
1968 * from the array. It only succeeds if all working and active component devices
1969 * are integrity capable with matching profiles.
1970 */
NeilBrownfd01b882011-10-11 16:47:53 +11001971int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001972{
NeilBrown3cb03002011-10-11 16:45:26 +11001973 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001974
Andre Nollac5e7112009-08-03 10:59:47 +10001975 if (list_empty(&mddev->disks))
1976 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001977 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1978 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001979 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001980 /* skip spares and non-functional disks */
1981 if (test_bit(Faulty, &rdev->flags))
1982 continue;
1983 if (rdev->raid_disk < 0)
1984 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001985 if (!reference) {
1986 /* Use the first rdev as the reference */
1987 reference = rdev;
1988 continue;
1989 }
1990 /* does this rdev's profile match the reference profile? */
1991 if (blk_integrity_compare(reference->bdev->bd_disk,
1992 rdev->bdev->bd_disk) < 0)
1993 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001994 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001995 if (!reference || !bdev_get_integrity(reference->bdev))
1996 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001997 /*
1998 * All component devices are integrity capable and have matching
1999 * profiles, register the common profile for the md device.
2000 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002001 blk_integrity_register(mddev->gendisk,
2002 bdev_get_integrity(reference->bdev));
2003
Martin K. Petersena91a2782011-03-17 11:11:05 +01002004 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2005 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2006 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2007 mdname(mddev));
2008 return -EINVAL;
2009 }
Andre Nollac5e7112009-08-03 10:59:47 +10002010 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002011}
Andre Nollac5e7112009-08-03 10:59:47 +10002012EXPORT_SYMBOL(md_integrity_register);
2013
Dan Williams1501efa2016-01-13 16:00:07 -08002014/*
2015 * Attempt to add an rdev, but only if it is consistent with the current
2016 * integrity profile
2017 */
2018int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002019{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002020 struct blk_integrity *bi_rdev;
2021 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002022 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002023
2024 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002025 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002026
2027 bi_rdev = bdev_get_integrity(rdev->bdev);
2028 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002029
2030 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002031 return 0;
2032
2033 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2034 printk(KERN_NOTICE "%s: incompatible integrity profile for %s\n",
2035 mdname(mddev), bdevname(rdev->bdev, name));
2036 return -ENXIO;
2037 }
2038
2039 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002040}
2041EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002042
NeilBrownf72ffdd2014-09-30 14:23:59 +10002043static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002045 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002046 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002047 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Dan Williams11e2ede2008-04-30 00:52:32 -07002049 /* prevent duplicates */
2050 if (find_rdev(mddev, rdev->bdev->bd_dev))
2051 return -EEXIST;
2052
Andre Nolldd8ac332009-03-31 14:33:13 +11002053 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002054 if (!test_bit(Journal, &rdev->flags) &&
2055 rdev->sectors &&
2056 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002057 if (mddev->pers) {
2058 /* Cannot change size, so fail
2059 * If mddev->level <= 0, then we don't care
2060 * about aligning sizes (e.g. linear)
2061 */
2062 if (mddev->level > 0)
2063 return -ENOSPC;
2064 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002065 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 /* Verify rdev->desc_nr is unique.
2069 * If it is -1, assign a free number, else
2070 * check number is not in use
2071 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002072 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (rdev->desc_nr < 0) {
2074 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002075 if (mddev->pers)
2076 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002077 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 choice++;
2079 rdev->desc_nr = choice;
2080 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002081 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002082 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002086 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002087 if (!test_bit(Journal, &rdev->flags) &&
2088 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002089 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2090 mdname(mddev), mddev->max_disks);
2091 return -EBUSY;
2092 }
NeilBrown19133a42005-11-08 21:39:35 -08002093 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002094 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002097 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002098
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002099 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002100 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002101
Tejun Heo0762b8b2008-08-25 19:56:12 +09002102 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002103 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2104 /* failure here is OK */;
2105 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002106
NeilBrown4b809912008-07-21 17:05:25 +10002107 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002108 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002109
2110 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002111 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002114
2115 fail:
2116 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2117 b, mdname(mddev));
2118 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
NeilBrown177a99b2008-02-06 01:39:56 -08002121static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002122{
NeilBrown3cb03002011-10-11 16:45:26 +11002123 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002124 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002125 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002126}
2127
NeilBrownf72ffdd2014-09-30 14:23:59 +10002128static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002131
Tejun Heo49731ba2011-01-14 18:43:57 +01002132 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002133 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2135 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002136 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002137 sysfs_put(rdev->sysfs_state);
2138 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002139 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002140 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002141 * writing to 'remove' to "dev/state". We also need
2142 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002143 */
NeilBrown4b809912008-07-21 17:05:25 +10002144 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002145 INIT_WORK(&rdev->del_work, md_delayed_delete);
2146 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002147 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150/*
2151 * prevent the device from being mounted, repartitioned or
2152 * otherwise reused by a RAID array (or any other kernel
2153 * subsystem), by bd_claiming the device.
2154 */
NeilBrown3cb03002011-10-11 16:45:26 +11002155static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 int err = 0;
2158 struct block_device *bdev;
2159 char b[BDEVNAME_SIZE];
2160
Tejun Heod4d77622010-11-13 11:55:18 +01002161 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002162 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (IS_ERR(bdev)) {
2164 printk(KERN_ERR "md: could not open %s.\n",
2165 __bdevname(dev, b));
2166 return PTR_ERR(bdev);
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 rdev->bdev = bdev;
2169 return err;
2170}
2171
NeilBrown3cb03002011-10-11 16:45:26 +11002172static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 struct block_device *bdev = rdev->bdev;
2175 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002176 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
2179void md_autodetect_dev(dev_t dev);
2180
NeilBrownf72ffdd2014-09-30 14:23:59 +10002181static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 printk(KERN_INFO "md: export_rdev(%s)\n",
2186 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002187 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002189 if (test_bit(AutoDetected, &rdev->flags))
2190 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#endif
2192 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002193 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002196void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 unbind_rdev_from_array(rdev);
2199 export_rdev(rdev);
2200}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002201EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
NeilBrownfd01b882011-10-11 16:47:53 +11002203static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
NeilBrown0638bb02014-09-25 17:43:47 +10002205 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
NeilBrown0638bb02014-09-25 17:43:47 +10002207 while (!list_empty(&mddev->disks)) {
2208 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2209 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002210 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 mddev->raid_disks = 0;
2213 mddev->major_version = 0;
2214}
2215
NeilBrownf72ffdd2014-09-30 14:23:59 +10002216static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
NeilBrown42543762006-06-26 00:27:57 -07002218 /* Update each superblock (in-memory image), but
2219 * if we are allowed to, skip spares which already
2220 * have the right event counter, or have one earlier
2221 * (which would mean they aren't being marked as dirty
2222 * with the rest of the array)
2223 */
NeilBrown3cb03002011-10-11 16:45:26 +11002224 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002225 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002226 if (rdev->sb_events == mddev->events ||
2227 (nospares &&
2228 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002229 rdev->sb_events+1 == mddev->events)) {
2230 /* Don't update this superblock */
2231 rdev->sb_loaded = 2;
2232 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002233 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002234 rdev->sb_loaded = 1;
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237}
2238
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002239static bool does_sb_need_changing(struct mddev *mddev)
2240{
2241 struct md_rdev *rdev;
2242 struct mdp_superblock_1 *sb;
2243 int role;
2244
2245 /* Find a good rdev */
2246 rdev_for_each(rdev, mddev)
2247 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2248 break;
2249
2250 /* No good device found. */
2251 if (!rdev)
2252 return false;
2253
2254 sb = page_address(rdev->sb_page);
2255 /* Check if a device has become faulty or a spare become active */
2256 rdev_for_each(rdev, mddev) {
2257 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2258 /* Device activated? */
2259 if (role == 0xffff && rdev->raid_disk >=0 &&
2260 !test_bit(Faulty, &rdev->flags))
2261 return true;
2262 /* Device turned faulty? */
2263 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2264 return true;
2265 }
2266
2267 /* Check if any mddev parameters have changed */
2268 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2269 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002270 (mddev->layout != le64_to_cpu(sb->layout)) ||
2271 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2272 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2273 return true;
2274
2275 return false;
2276}
2277
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002278void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
NeilBrown3cb03002011-10-11 16:45:26 +11002280 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002281 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002282 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002283 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002284 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
NeilBrownd87f0642013-04-24 11:42:40 +10002286 if (mddev->ro) {
2287 if (force_change)
2288 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2289 return;
2290 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002291
2292 if (mddev_is_clustered(mddev)) {
2293 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2294 force_change = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002295 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002296 /* Has someone else has updated the sb */
2297 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002298 if (ret == 0)
2299 md_cluster_ops->metadata_update_cancel(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002300 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2301 return;
2302 }
2303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002305 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002306 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002307 if (rdev->raid_disk >= 0 &&
2308 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002309 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002310 !test_bit(In_sync, &rdev->flags) &&
2311 mddev->curr_resync_completed > rdev->recovery_offset)
2312 rdev->recovery_offset = mddev->curr_resync_completed;
2313
NeilBrownf72ffdd2014-09-30 14:23:59 +10002314 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002315 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002316 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002317 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002318 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002319 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002320 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002321 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002322 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002323 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002324 md_error(mddev, rdev);
2325 }
2326 clear_bit(Blocked, &rdev->flags);
2327 clear_bit(BlockedBadBlocks, &rdev->flags);
2328 wake_up(&rdev->blocked_wait);
2329 }
2330 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002331 wake_up(&mddev->sb_wait);
2332 return;
2333 }
2334
NeilBrown85572d72014-12-15 12:56:56 +11002335 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002336
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002337 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002338
NeilBrown850b2b42006-10-03 01:15:46 -07002339 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2340 force_change = 1;
2341 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2342 /* just a clean<-> dirty transition, possibly leave spares alone,
2343 * though if events isn't the right even/odd, we will have to do
2344 * spares after all
2345 */
2346 nospares = 1;
2347 if (force_change)
2348 nospares = 0;
2349 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002350 /* If the array is degraded, then skipping spares is both
2351 * dangerous and fairly pointless.
2352 * Dangerous because a device that was removed from the array
2353 * might have a event_count that still looks up-to-date,
2354 * so it can be re-added without a resync.
2355 * Pointless because if there are any spares to skip,
2356 * then a recovery will happen and soon that array won't
2357 * be degraded any more and the spare can go back to sleep then.
2358 */
NeilBrown850b2b42006-10-03 01:15:46 -07002359 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002360
NeilBrown06d91a52005-06-21 17:17:12 -07002361 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002362
2363 /* If this is just a dirty<->clean transition, and the array is clean
2364 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002365 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002366 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002367 && mddev->can_decrease_events
2368 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002369 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002370 mddev->can_decrease_events = 0;
2371 } else {
NeilBrown42543762006-06-26 00:27:57 -07002372 /* otherwise we have to go forward and ... */
2373 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002374 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
NeilBrown403df472014-09-30 15:52:29 +10002377 /*
2378 * This 64-bit counter should never wrap.
2379 * Either we are in around ~1 trillion A.C., assuming
2380 * 1 reboot per second, or we have a bug...
2381 */
2382 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002383
NeilBrowndafb20f2012-03-19 12:46:39 +11002384 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002385 if (rdev->badblocks.changed)
2386 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002387 if (test_bit(Faulty, &rdev->flags))
2388 set_bit(FaultRecorded, &rdev->flags);
2389 }
NeilBrown2699b672011-07-28 11:31:47 +10002390
NeilBrowne6910632008-02-06 01:39:51 -08002391 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002392 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
NeilBrown36a4e1f2011-10-07 14:23:17 +11002394 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2395 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
NeilBrown4ad13662007-07-17 04:06:13 -07002397 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002398 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002400
NeilBrown42543762006-06-26 00:27:57 -07002401 if (rdev->sb_loaded != 1)
2402 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
NeilBrownf4667222013-12-09 12:04:56 +11002404 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002405 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002406 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002407 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002408 pr_debug("md: (write) %s's sb offset: %llu\n",
2409 bdevname(rdev->bdev, b),
2410 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002411 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002412 if (rdev->badblocks.size) {
2413 md_super_write(mddev, rdev,
2414 rdev->badblocks.sector,
2415 rdev->badblocks.size << 9,
2416 rdev->bb_page);
2417 rdev->badblocks.size = 0;
2418 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002419
NeilBrownf4667222013-12-09 12:04:56 +11002420 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002421 pr_debug("md: %s (skipping faulty)\n",
2422 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002423
NeilBrown7bfa19f2005-06-21 17:17:28 -07002424 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 /* only need to write one superblock... */
2426 break;
2427 }
NeilBrowna9701a32005-11-08 21:39:34 -08002428 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002429 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002430
NeilBrown85572d72014-12-15 12:56:56 +11002431 spin_lock(&mddev->lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002432 if (mddev->in_sync != sync_req ||
2433 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002434 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002435 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002436 goto repeat;
2437 }
NeilBrown850b2b42006-10-03 01:15:46 -07002438 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002439 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002440 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002441 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2442 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002443
NeilBrowndafb20f2012-03-19 12:46:39 +11002444 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002445 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2446 clear_bit(Blocked, &rdev->flags);
2447
2448 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002449 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002450 clear_bit(BlockedBadBlocks, &rdev->flags);
2451 wake_up(&rdev->blocked_wait);
2452 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002453
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002454 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002455 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002457EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002459static int add_bound_rdev(struct md_rdev *rdev)
2460{
2461 struct mddev *mddev = rdev->mddev;
2462 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002463 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002464
Shaohua Li87d4d912016-01-06 14:37:14 -08002465 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002466 /* If there is hot_add_disk but no hot_remove_disk
2467 * then added disks for geometry changes,
2468 * and should be added immediately.
2469 */
2470 super_types[mddev->major_version].
2471 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002472 if (add_journal)
2473 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002474 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002475 if (add_journal)
2476 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002477 if (err) {
2478 unbind_rdev_from_array(rdev);
2479 export_rdev(rdev);
2480 return err;
2481 }
2482 }
2483 sysfs_notify_dirent_safe(rdev->sysfs_state);
2484
2485 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2486 if (mddev->degraded)
2487 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2488 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2489 md_new_event(mddev);
2490 md_wakeup_thread(mddev->thread);
2491 return 0;
2492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Andre Noll7f6ce762008-03-23 18:34:54 +01002494/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002495 * We want to accept with case. For this we use cmd_match.
2496 */
2497static int cmd_match(const char *cmd, const char *str)
2498{
2499 /* See if cmd, written into a sysfs file, matches
2500 * str. They must either be the same, or cmd can
2501 * have a trailing newline
2502 */
2503 while (*cmd && *str && *cmd == *str) {
2504 cmd++;
2505 str++;
2506 }
2507 if (*cmd == '\n')
2508 cmd++;
2509 if (*str || *cmd)
2510 return 0;
2511 return 1;
2512}
2513
NeilBrown86e6ffd2005-11-08 21:39:24 -08002514struct rdev_sysfs_entry {
2515 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002516 ssize_t (*show)(struct md_rdev *, char *);
2517 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002518};
2519
2520static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002521state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002522{
2523 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002524 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002525 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002526
NeilBrown758bfc82014-12-15 12:56:59 +11002527 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002528 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002529 len+= sprintf(page+len, "%sfaulty",sep);
2530 sep = ",";
2531 }
NeilBrown758bfc82014-12-15 12:56:59 +11002532 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002533 len += sprintf(page+len, "%sin_sync",sep);
2534 sep = ",";
2535 }
Shaohua Liac6096e2015-10-04 09:20:11 -07002536 if (test_bit(Journal, &flags)) {
2537 len += sprintf(page+len, "%sjournal",sep);
2538 sep = ",";
2539 }
NeilBrown758bfc82014-12-15 12:56:59 +11002540 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002541 len += sprintf(page+len, "%swrite_mostly",sep);
2542 sep = ",";
2543 }
NeilBrown758bfc82014-12-15 12:56:59 +11002544 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002545 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002546 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002547 len += sprintf(page+len, "%sblocked", sep);
2548 sep = ",";
2549 }
NeilBrown758bfc82014-12-15 12:56:59 +11002550 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002551 !test_bit(Journal, &flags) &&
NeilBrown758bfc82014-12-15 12:56:59 +11002552 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002553 len += sprintf(page+len, "%sspare", sep);
2554 sep = ",";
2555 }
NeilBrown758bfc82014-12-15 12:56:59 +11002556 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002557 len += sprintf(page+len, "%swrite_error", sep);
2558 sep = ",";
2559 }
NeilBrown758bfc82014-12-15 12:56:59 +11002560 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002561 len += sprintf(page+len, "%swant_replacement", sep);
2562 sep = ",";
2563 }
NeilBrown758bfc82014-12-15 12:56:59 +11002564 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002565 len += sprintf(page+len, "%sreplacement", sep);
2566 sep = ",";
2567 }
2568
NeilBrown86e6ffd2005-11-08 21:39:24 -08002569 return len+sprintf(page+len, "\n");
2570}
2571
NeilBrown45dc2de2006-06-26 00:27:58 -07002572static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002573state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002574{
2575 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002576 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002577 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002578 * writemostly - sets write_mostly
2579 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002580 * blocked - sets the Blocked flags
2581 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002582 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002583 * -insync - clear Insync for a device with a slot assigned,
2584 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002585 * write_error - sets WriteErrorSeen
2586 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002587 */
2588 int err = -EINVAL;
2589 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2590 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002591 if (test_bit(Faulty, &rdev->flags))
2592 err = 0;
2593 else
2594 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002595 } else if (cmd_match(buf, "remove")) {
2596 if (rdev->raid_disk >= 0)
2597 err = -EBUSY;
2598 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002599 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002600 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002601 if (mddev_is_clustered(mddev))
2602 err = md_cluster_ops->remove_disk(mddev, rdev);
2603
2604 if (err == 0) {
2605 md_kick_rdev_from_array(rdev);
2606 if (mddev->pers)
2607 md_update_sb(mddev, 1);
2608 md_new_event(mddev);
2609 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002610 }
NeilBrownf6556752006-06-26 00:28:01 -07002611 } else if (cmd_match(buf, "writemostly")) {
2612 set_bit(WriteMostly, &rdev->flags);
2613 err = 0;
2614 } else if (cmd_match(buf, "-writemostly")) {
2615 clear_bit(WriteMostly, &rdev->flags);
2616 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002617 } else if (cmd_match(buf, "blocked")) {
2618 set_bit(Blocked, &rdev->flags);
2619 err = 0;
2620 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002621 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002622 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002623 /* metadata handler doesn't understand badblocks,
2624 * so we need to fail the device
2625 */
2626 md_error(rdev->mddev, rdev);
2627 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002628 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002629 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002630 wake_up(&rdev->blocked_wait);
2631 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2632 md_wakeup_thread(rdev->mddev->thread);
2633
2634 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002635 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2636 set_bit(In_sync, &rdev->flags);
2637 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002638 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2639 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002640 if (rdev->mddev->pers == NULL) {
2641 clear_bit(In_sync, &rdev->flags);
2642 rdev->saved_raid_disk = rdev->raid_disk;
2643 rdev->raid_disk = -1;
2644 err = 0;
2645 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002646 } else if (cmd_match(buf, "write_error")) {
2647 set_bit(WriteErrorSeen, &rdev->flags);
2648 err = 0;
2649 } else if (cmd_match(buf, "-write_error")) {
2650 clear_bit(WriteErrorSeen, &rdev->flags);
2651 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002652 } else if (cmd_match(buf, "want_replacement")) {
2653 /* Any non-spare device that is not a replacement can
2654 * become want_replacement at any time, but we then need to
2655 * check if recovery is needed.
2656 */
2657 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002658 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002659 !test_bit(Replacement, &rdev->flags))
2660 set_bit(WantReplacement, &rdev->flags);
2661 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2662 md_wakeup_thread(rdev->mddev->thread);
2663 err = 0;
2664 } else if (cmd_match(buf, "-want_replacement")) {
2665 /* Clearing 'want_replacement' is always allowed.
2666 * Once replacements starts it is too late though.
2667 */
2668 err = 0;
2669 clear_bit(WantReplacement, &rdev->flags);
2670 } else if (cmd_match(buf, "replacement")) {
2671 /* Can only set a device as a replacement when array has not
2672 * yet been started. Once running, replacement is automatic
2673 * from spares, or by assigning 'slot'.
2674 */
2675 if (rdev->mddev->pers)
2676 err = -EBUSY;
2677 else {
2678 set_bit(Replacement, &rdev->flags);
2679 err = 0;
2680 }
2681 } else if (cmd_match(buf, "-replacement")) {
2682 /* Similarly, can only clear Replacement before start */
2683 if (rdev->mddev->pers)
2684 err = -EBUSY;
2685 else {
2686 clear_bit(Replacement, &rdev->flags);
2687 err = 0;
2688 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002689 } else if (cmd_match(buf, "re-add")) {
2690 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002691 /* clear_bit is performed _after_ all the devices
2692 * have their local Faulty bit cleared. If any writes
2693 * happen in the meantime in the local node, they
2694 * will land in the local bitmap, which will be synced
2695 * by this node eventually
2696 */
2697 if (!mddev_is_clustered(rdev->mddev) ||
2698 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2699 clear_bit(Faulty, &rdev->flags);
2700 err = add_bound_rdev(rdev);
2701 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002702 } else
2703 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002704 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002705 if (!err)
2706 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002707 return err ? err : len;
2708}
NeilBrown80ca3a42006-07-10 04:44:18 -07002709static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002710__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002711
2712static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002713errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002714{
2715 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2716}
2717
2718static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002719errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002720{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002721 unsigned int n;
2722 int rv;
2723
2724 rv = kstrtouint(buf, 10, &n);
2725 if (rv < 0)
2726 return rv;
2727 atomic_set(&rdev->corrected_errors, n);
2728 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002729}
2730static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002731__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002732
NeilBrown014236d2006-01-06 00:20:55 -08002733static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002734slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002735{
Shaohua Lif2076e72015-10-08 21:54:12 -07002736 if (test_bit(Journal, &rdev->flags))
2737 return sprintf(page, "journal\n");
2738 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002739 return sprintf(page, "none\n");
2740 else
2741 return sprintf(page, "%d\n", rdev->raid_disk);
2742}
2743
2744static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002745slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002746{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002747 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002748 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002749
Shaohua Lif2076e72015-10-08 21:54:12 -07002750 if (test_bit(Journal, &rdev->flags))
2751 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002752 if (strncmp(buf, "none", 4)==0)
2753 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002754 else {
2755 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2756 if (err < 0)
2757 return err;
2758 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002759 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002760 /* Setting 'slot' on an active array requires also
2761 * updating the 'rd%d' link, and communicating
2762 * with the personality with ->hot_*_disk.
2763 * For now we only support removing
2764 * failed/spare devices. This normally happens automatically,
2765 * but not when the metadata is externally managed.
2766 */
NeilBrownc303da62008-02-06 01:39:51 -08002767 if (rdev->raid_disk == -1)
2768 return -EEXIST;
2769 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002770 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002771 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002772 clear_bit(Blocked, &rdev->flags);
2773 remove_and_add_spares(rdev->mddev, rdev);
2774 if (rdev->raid_disk >= 0)
2775 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002776 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2777 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002778 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002779 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002780 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002781 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002782 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002783
2784 if (rdev->raid_disk != -1)
2785 return -EBUSY;
2786
NeilBrownc6751b22011-02-02 11:57:13 +11002787 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2788 return -EBUSY;
2789
Neil Brown6c2fce22008-06-28 08:31:31 +10002790 if (rdev->mddev->pers->hot_add_disk == NULL)
2791 return -EINVAL;
2792
NeilBrownba1b41b2011-01-14 09:14:34 +11002793 if (slot >= rdev->mddev->raid_disks &&
2794 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2795 return -ENOSPC;
2796
Neil Brown6c2fce22008-06-28 08:31:31 +10002797 rdev->raid_disk = slot;
2798 if (test_bit(In_sync, &rdev->flags))
2799 rdev->saved_raid_disk = slot;
2800 else
2801 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002802 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002803 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002804 err = rdev->mddev->pers->
2805 hot_add_disk(rdev->mddev, rdev);
2806 if (err) {
2807 rdev->raid_disk = -1;
2808 return err;
2809 } else
2810 sysfs_notify_dirent_safe(rdev->sysfs_state);
2811 if (sysfs_link_rdev(rdev->mddev, rdev))
2812 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002813 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002814 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002815 if (slot >= rdev->mddev->raid_disks &&
2816 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002817 return -ENOSPC;
2818 rdev->raid_disk = slot;
2819 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002820 clear_bit(Faulty, &rdev->flags);
2821 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002822 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002823 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002824 }
NeilBrown014236d2006-01-06 00:20:55 -08002825 return len;
2826}
2827
NeilBrown014236d2006-01-06 00:20:55 -08002828static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002829__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002830
NeilBrown93c8cad2006-01-06 00:20:56 -08002831static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002832offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002833{
Andrew Morton6961ece2006-01-06 00:20:59 -08002834 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002835}
2836
2837static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002838offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002839{
NeilBrownc6563a82012-05-21 09:27:00 +10002840 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002841 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002842 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002843 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002844 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002845 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002846 /* Must set offset before size, so overlap checks
2847 * can be sane */
2848 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002849 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002850 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002851 return len;
2852}
2853
2854static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002855__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002856
NeilBrownc6563a82012-05-21 09:27:00 +10002857static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2858{
2859 return sprintf(page, "%llu\n",
2860 (unsigned long long)rdev->new_data_offset);
2861}
2862
2863static ssize_t new_offset_store(struct md_rdev *rdev,
2864 const char *buf, size_t len)
2865{
2866 unsigned long long new_offset;
2867 struct mddev *mddev = rdev->mddev;
2868
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002869 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002870 return -EINVAL;
2871
NeilBrownf851b602014-12-11 10:02:10 +11002872 if (mddev->sync_thread ||
2873 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002874 return -EBUSY;
2875 if (new_offset == rdev->data_offset)
2876 /* reset is always permitted */
2877 ;
2878 else if (new_offset > rdev->data_offset) {
2879 /* must not push array size beyond rdev_sectors */
2880 if (new_offset - rdev->data_offset
2881 + mddev->dev_sectors > rdev->sectors)
2882 return -E2BIG;
2883 }
2884 /* Metadata worries about other space details. */
2885
2886 /* decreasing the offset is inconsistent with a backwards
2887 * reshape.
2888 */
2889 if (new_offset < rdev->data_offset &&
2890 mddev->reshape_backwards)
2891 return -EINVAL;
2892 /* Increasing offset is inconsistent with forwards
2893 * reshape. reshape_direction should be set to
2894 * 'backwards' first.
2895 */
2896 if (new_offset > rdev->data_offset &&
2897 !mddev->reshape_backwards)
2898 return -EINVAL;
2899
2900 if (mddev->pers && mddev->persistent &&
2901 !super_types[mddev->major_version]
2902 .allow_new_offset(rdev, new_offset))
2903 return -E2BIG;
2904 rdev->new_data_offset = new_offset;
2905 if (new_offset > rdev->data_offset)
2906 mddev->reshape_backwards = 1;
2907 else if (new_offset < rdev->data_offset)
2908 mddev->reshape_backwards = 0;
2909
2910 return len;
2911}
2912static struct rdev_sysfs_entry rdev_new_offset =
2913__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2914
NeilBrown83303b62006-01-06 00:21:06 -08002915static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002916rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002917{
Andre Nolldd8ac332009-03-31 14:33:13 +11002918 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002919}
2920
NeilBrownc5d79ad2008-02-06 01:39:54 -08002921static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2922{
2923 /* check if two start/length pairs overlap */
2924 if (s1+l1 <= s2)
2925 return 0;
2926 if (s2+l2 <= s1)
2927 return 0;
2928 return 1;
2929}
2930
Dan Williamsb522adc2009-03-31 15:00:31 +11002931static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2932{
2933 unsigned long long blocks;
2934 sector_t new;
2935
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002936 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002937 return -EINVAL;
2938
2939 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2940 return -EINVAL; /* sector conversion overflow */
2941
2942 new = blocks * 2;
2943 if (new != blocks * 2)
2944 return -EINVAL; /* unsigned long long to sector_t overflow */
2945
2946 *sectors = new;
2947 return 0;
2948}
2949
NeilBrown83303b62006-01-06 00:21:06 -08002950static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002951rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002952{
NeilBrownfd01b882011-10-11 16:47:53 +11002953 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002954 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002955 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002956
Shaohua Lif2076e72015-10-08 21:54:12 -07002957 if (test_bit(Journal, &rdev->flags))
2958 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002959 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002960 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002961 if (rdev->data_offset != rdev->new_data_offset)
2962 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002963 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002964 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002965 sectors = super_types[my_mddev->major_version].
2966 rdev_size_change(rdev, sectors);
2967 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002968 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002969 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002970 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002971 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002972 if (!my_mddev->pers->resize)
2973 /* Cannot change size for RAID0 or Linear etc */
2974 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002975 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002976 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002977 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002978
Andre Nolldd8ac332009-03-31 14:33:13 +11002979 rdev->sectors = sectors;
2980 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002981 /* Need to check that all other rdevs with the same
2982 * ->bdev do not overlap. 'rcu' is sufficient to walk
2983 * the rdev lists safely.
2984 * This check does not provide a hard guarantee, it
2985 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002986 */
NeilBrownfd01b882011-10-11 16:47:53 +11002987 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002988 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002989 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002990
NeilBrown8b1afc32014-09-29 15:33:20 +10002991 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002992 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002993 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002994
NeilBrowndafb20f2012-03-19 12:46:39 +11002995 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002996 if (rdev->bdev == rdev2->bdev &&
2997 rdev != rdev2 &&
2998 overlaps(rdev->data_offset, rdev->sectors,
2999 rdev2->data_offset,
3000 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003001 overlap = 1;
3002 break;
3003 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003004 if (overlap) {
3005 mddev_put(mddev);
3006 break;
3007 }
3008 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003009 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003010 if (overlap) {
3011 /* Someone else could have slipped in a size
3012 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003013 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003014 * safe, and trust userspace not to race with
3015 * itself
3016 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003017 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003018 return -EBUSY;
3019 }
3020 }
NeilBrown83303b62006-01-06 00:21:06 -08003021 return len;
3022}
3023
3024static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003025__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003026
NeilBrown3cb03002011-10-11 16:45:26 +11003027static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003028{
3029 unsigned long long recovery_start = rdev->recovery_offset;
3030
3031 if (test_bit(In_sync, &rdev->flags) ||
3032 recovery_start == MaxSector)
3033 return sprintf(page, "none\n");
3034
3035 return sprintf(page, "%llu\n", recovery_start);
3036}
3037
NeilBrown3cb03002011-10-11 16:45:26 +11003038static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003039{
3040 unsigned long long recovery_start;
3041
3042 if (cmd_match(buf, "none"))
3043 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003044 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003045 return -EINVAL;
3046
3047 if (rdev->mddev->pers &&
3048 rdev->raid_disk >= 0)
3049 return -EBUSY;
3050
3051 rdev->recovery_offset = recovery_start;
3052 if (recovery_start == MaxSector)
3053 set_bit(In_sync, &rdev->flags);
3054 else
3055 clear_bit(In_sync, &rdev->flags);
3056 return len;
3057}
3058
3059static struct rdev_sysfs_entry rdev_recovery_start =
3060__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3061
Vishal Vermafc974ee2015-12-24 19:20:34 -07003062/* sysfs access to bad-blocks list.
3063 * We present two files.
3064 * 'bad-blocks' lists sector numbers and lengths of ranges that
3065 * are recorded as bad. The list is truncated to fit within
3066 * the one-page limit of sysfs.
3067 * Writing "sector length" to this file adds an acknowledged
3068 * bad block list.
3069 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3070 * been acknowledged. Writing to this file adds bad blocks
3071 * without acknowledging them. This is largely for testing.
3072 */
NeilBrown3cb03002011-10-11 16:45:26 +11003073static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003074{
3075 return badblocks_show(&rdev->badblocks, page, 0);
3076}
NeilBrown3cb03002011-10-11 16:45:26 +11003077static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003078{
NeilBrownde393cd2011-07-28 11:31:48 +10003079 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3080 /* Maybe that ack was all we needed */
3081 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3082 wake_up(&rdev->blocked_wait);
3083 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003084}
3085static struct rdev_sysfs_entry rdev_bad_blocks =
3086__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3087
NeilBrown3cb03002011-10-11 16:45:26 +11003088static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003089{
3090 return badblocks_show(&rdev->badblocks, page, 1);
3091}
NeilBrown3cb03002011-10-11 16:45:26 +11003092static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003093{
3094 return badblocks_store(&rdev->badblocks, page, len, 1);
3095}
3096static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3097__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3098
NeilBrown86e6ffd2005-11-08 21:39:24 -08003099static struct attribute *rdev_default_attrs[] = {
3100 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003101 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003102 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003103 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003104 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003105 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003106 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003107 &rdev_bad_blocks.attr,
3108 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003109 NULL,
3110};
3111static ssize_t
3112rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3113{
3114 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003115 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003116
3117 if (!entry->show)
3118 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003119 if (!rdev->mddev)
3120 return -EBUSY;
3121 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003122}
3123
3124static ssize_t
3125rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3126 const char *page, size_t length)
3127{
3128 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003129 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003130 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003131 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003132
3133 if (!entry->store)
3134 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003135 if (!capable(CAP_SYS_ADMIN))
3136 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003137 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003138 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003139 if (rdev->mddev == NULL)
3140 rv = -EBUSY;
3141 else
3142 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003143 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003144 }
3145 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003146}
3147
3148static void rdev_free(struct kobject *ko)
3149{
NeilBrown3cb03002011-10-11 16:45:26 +11003150 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003151 kfree(rdev);
3152}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003153static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003154 .show = rdev_attr_show,
3155 .store = rdev_attr_store,
3156};
3157static struct kobj_type rdev_ktype = {
3158 .release = rdev_free,
3159 .sysfs_ops = &rdev_sysfs_ops,
3160 .default_attrs = rdev_default_attrs,
3161};
3162
NeilBrown3cb03002011-10-11 16:45:26 +11003163int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003164{
3165 rdev->desc_nr = -1;
3166 rdev->saved_raid_disk = -1;
3167 rdev->raid_disk = -1;
3168 rdev->flags = 0;
3169 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003170 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003171 rdev->sb_events = 0;
3172 rdev->last_read_error.tv_sec = 0;
3173 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003174 rdev->sb_loaded = 0;
3175 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003176 atomic_set(&rdev->nr_pending, 0);
3177 atomic_set(&rdev->read_errors, 0);
3178 atomic_set(&rdev->corrected_errors, 0);
3179
3180 INIT_LIST_HEAD(&rdev->same_set);
3181 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003182
3183 /* Add space to store bad block list.
3184 * This reserves the space even on arrays where it cannot
3185 * be used - I wonder if that matters
3186 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003187 return badblocks_init(&rdev->badblocks, 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 */
NeilBrowna05b7ea02012-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)
NeilBrowna05b7ea02012-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)
NeilBrowna05b7ea02012-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)
NeilBrowna05b7ea02012-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
NeilBrown6d7ff732006-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)
NeilBrown6d7ff732006-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)
NeilBrown6d7ff732006-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;
NeilBrown6d7ff732006-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;
NeilBrown6d7ff732006-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);
NeilBrown6d7ff732006-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
NeilBrown6d7ff732006-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);
NeilBrown6d7ff732006-01-06 00:21:16 -08004114 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004115 }
NeilBrown6d7ff732006-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);
NeilBrown6d7ff732006-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);
NeilBrown850b2b42006-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,
NeilBrown6d7ff732006-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-Hartmance7b0f42005-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
NeilBrown0f9552b2009-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)
NeilBrown850b2b42006-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
NeilBrowna05b7ea02012-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,
NeilBrowna05b7ea02012-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);
NeilBrownefeb53c2009-01-09 08:31:10 +11005678 if (mddev->hold_active == UNTIL_STOP)
5679 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005680 }
NeilBrownd7603b72006-01-06 00:20:30 -08005681 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005682 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684}
5685
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005686#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005687static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688{
NeilBrown3cb03002011-10-11 16:45:26 +11005689 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 int err;
5691
NeilBrowna757e642005-04-16 15:26:42 -07005692 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
5695 printk(KERN_INFO "md: running: ");
5696
NeilBrowndafb20f2012-03-19 12:46:39 +11005697 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 char b[BDEVNAME_SIZE];
5699 printk("<%s>", bdevname(rdev->bdev,b));
5700 }
5701 printk("\n");
5702
NeilBrownd710e132008-10-13 11:55:12 +11005703 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 if (err) {
5705 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005706 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 }
5708}
5709
5710/*
5711 * lets try to run arrays based on all disks that have arrived
5712 * until now. (those are in pending_raid_disks)
5713 *
5714 * the method: pick the first pending disk, collect all disks with
5715 * the same UUID, remove all from the pending list and put them into
5716 * the 'same_array' list. Then order this list based on superblock
5717 * update time (freshest comes first), kick out 'old' disks and
5718 * compare superblocks. If everything's fine then run it.
5719 *
5720 * If "unit" is allocated, then bump its reference count
5721 */
5722static void autorun_devices(int part)
5723{
NeilBrown3cb03002011-10-11 16:45:26 +11005724 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005725 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 char b[BDEVNAME_SIZE];
5727
5728 printk(KERN_INFO "md: autorun ...\n");
5729 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005730 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005732 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005734 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
5736 printk(KERN_INFO "md: considering %s ...\n",
5737 bdevname(rdev0->bdev,b));
5738 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005739 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 if (super_90_load(rdev, rdev0, 0) >= 0) {
5741 printk(KERN_INFO "md: adding %s ...\n",
5742 bdevname(rdev->bdev,b));
5743 list_move(&rdev->same_set, &candidates);
5744 }
5745 /*
5746 * now we have a set of devices, with all of them having
5747 * mostly sane superblocks. It's time to allocate the
5748 * mddev.
5749 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005750 if (part) {
5751 dev = MKDEV(mdp_major,
5752 rdev0->preferred_minor << MdpMinorShift);
5753 unit = MINOR(dev) >> MdpMinorShift;
5754 } else {
5755 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5756 unit = MINOR(dev);
5757 }
5758 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5760 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5761 break;
5762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
5764 md_probe(dev, NULL, NULL);
5765 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005766 if (!mddev || !mddev->gendisk) {
5767 if (mddev)
5768 mddev_put(mddev);
5769 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 "md: cannot allocate memory for md drive.\n");
5771 break;
5772 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005773 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 printk(KERN_WARNING "md: %s locked, cannot run\n",
5775 mdname(mddev));
5776 else if (mddev->raid_disks || mddev->major_version
5777 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005778 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 "md: %s already running, cannot run %s\n",
5780 mdname(mddev), bdevname(rdev0->bdev,b));
5781 mddev_unlock(mddev);
5782 } else {
5783 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005784 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005785 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 list_del_init(&rdev->same_set);
5787 if (bind_rdev_to_array(rdev, mddev))
5788 export_rdev(rdev);
5789 }
5790 autorun_array(mddev);
5791 mddev_unlock(mddev);
5792 }
5793 /* on success, candidates will be empty, on error
5794 * it won't...
5795 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005796 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005797 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 mddev_put(mddev);
5801 }
5802 printk(KERN_INFO "md: ... autorun DONE.\n");
5803}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005804#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
NeilBrownf72ffdd2014-09-30 14:23:59 +10005806static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807{
5808 mdu_version_t ver;
5809
5810 ver.major = MD_MAJOR_VERSION;
5811 ver.minor = MD_MINOR_VERSION;
5812 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5813
5814 if (copy_to_user(arg, &ver, sizeof(ver)))
5815 return -EFAULT;
5816
5817 return 0;
5818}
5819
NeilBrownf72ffdd2014-09-30 14:23:59 +10005820static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821{
5822 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005823 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005824 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
NeilBrown1ca69c42012-10-11 13:37:33 +11005826 nr = working = insync = failed = spare = 0;
5827 rcu_read_lock();
5828 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005830 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 failed++;
5832 else {
5833 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005834 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005835 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 else
5837 spare++;
5838 }
5839 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005840 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
5842 info.major_version = mddev->major_version;
5843 info.minor_version = mddev->minor_version;
5844 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005845 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005847 info.size = mddev->dev_sectors / 2;
5848 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005849 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 info.nr_disks = nr;
5851 info.raid_disks = mddev->raid_disks;
5852 info.md_minor = mddev->md_minor;
5853 info.not_persistent= !mddev->persistent;
5854
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005855 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 info.state = 0;
5857 if (mddev->in_sync)
5858 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005859 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005860 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005861 if (mddev_is_clustered(mddev))
5862 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005863 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 info.working_disks = working;
5865 info.failed_disks = failed;
5866 info.spare_disks = spare;
5867
5868 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005869 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 if (copy_to_user(arg, &info, sizeof(info)))
5872 return -EFAULT;
5873
5874 return 0;
5875}
5876
NeilBrownf72ffdd2014-09-30 14:23:59 +10005877static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005878{
5879 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005880 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005881 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005882
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005883 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005884 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005885 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005886
NeilBrown32a76272005-06-21 17:17:14 -07005887 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005888 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005889 /* bitmap enabled */
5890 if (mddev->bitmap_info.file) {
5891 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5892 sizeof(file->pathname));
5893 if (IS_ERR(ptr))
5894 err = PTR_ERR(ptr);
5895 else
5896 memmove(file->pathname, ptr,
5897 sizeof(file->pathname)-(ptr-file->pathname));
5898 }
NeilBrown4af1a042014-12-15 12:57:00 +11005899 spin_unlock(&mddev->lock);
5900
5901 if (err == 0 &&
5902 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005903 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005904
NeilBrown32a76272005-06-21 17:17:14 -07005905 kfree(file);
5906 return err;
5907}
5908
NeilBrownf72ffdd2014-09-30 14:23:59 +10005909static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910{
5911 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005912 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913
5914 if (copy_from_user(&info, arg, sizeof(info)))
5915 return -EFAULT;
5916
NeilBrown1ca69c42012-10-11 13:37:33 +11005917 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005918 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 if (rdev) {
5920 info.major = MAJOR(rdev->bdev->bd_dev);
5921 info.minor = MINOR(rdev->bdev->bd_dev);
5922 info.raid_disk = rdev->raid_disk;
5923 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005924 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005926 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 info.state |= (1<<MD_DISK_ACTIVE);
5928 info.state |= (1<<MD_DISK_SYNC);
5929 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005930 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005931 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005932 if (test_bit(WriteMostly, &rdev->flags))
5933 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 } else {
5935 info.major = info.minor = 0;
5936 info.raid_disk = -1;
5937 info.state = (1<<MD_DISK_REMOVED);
5938 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005939 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
5941 if (copy_to_user(arg, &info, sizeof(info)))
5942 return -EFAULT;
5943
5944 return 0;
5945}
5946
NeilBrownf72ffdd2014-09-30 14:23:59 +10005947static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948{
5949 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005950 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 dev_t dev = MKDEV(info->major,info->minor);
5952
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005953 if (mddev_is_clustered(mddev) &&
5954 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005955 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005956 mdname(mddev));
5957 return -EINVAL;
5958 }
5959
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5961 return -EOVERFLOW;
5962
5963 if (!mddev->raid_disks) {
5964 int err;
5965 /* expecting a device which has a superblock */
5966 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5967 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005968 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 "md: md_import_device returned %ld\n",
5970 PTR_ERR(rdev));
5971 return PTR_ERR(rdev);
5972 }
5973 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005974 struct md_rdev *rdev0
5975 = list_entry(mddev->disks.next,
5976 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005977 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 .load_super(rdev, rdev0, mddev->minor_version);
5979 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005980 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005982 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 bdevname(rdev0->bdev,b2));
5984 export_rdev(rdev);
5985 return -EINVAL;
5986 }
5987 }
5988 err = bind_rdev_to_array(rdev, mddev);
5989 if (err)
5990 export_rdev(rdev);
5991 return err;
5992 }
5993
5994 /*
5995 * add_new_disk can be used once the array is assembled
5996 * to add "hot spares". They must already have a superblock
5997 * written
5998 */
5999 if (mddev->pers) {
6000 int err;
6001 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006002 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 "%s: personality does not support diskops!\n",
6004 mdname(mddev));
6005 return -EINVAL;
6006 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006007 if (mddev->persistent)
6008 rdev = md_import_device(dev, mddev->major_version,
6009 mddev->minor_version);
6010 else
6011 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006013 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 "md: md_import_device returned %ld\n",
6015 PTR_ERR(rdev));
6016 return PTR_ERR(rdev);
6017 }
NeilBrown1a855a02010-12-09 16:36:28 +11006018 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006019 if (!mddev->persistent) {
6020 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006021 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006022 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006023 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006024 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006025 } else
NeilBrown41158c72005-06-21 17:17:25 -07006026 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006027 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006028 } else
6029 super_types[mddev->major_version].
6030 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006031 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006032 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006033 /* This was a hot-add request, but events doesn't
6034 * match, so reject it.
6035 */
6036 export_rdev(rdev);
6037 return -EINVAL;
6038 }
6039
NeilBrownb2d444d2005-11-08 21:39:31 -08006040 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006041 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6042 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006043 else
6044 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006045
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006046 if (info->state & (1<<MD_DISK_JOURNAL)) {
6047 struct md_rdev *rdev2;
6048 bool has_journal = false;
6049
6050 /* make sure no existing journal disk */
6051 rdev_for_each(rdev2, mddev) {
6052 if (test_bit(Journal, &rdev2->flags)) {
6053 has_journal = true;
6054 break;
6055 }
6056 }
6057 if (has_journal) {
6058 export_rdev(rdev);
6059 return -EBUSY;
6060 }
Song Liubac624f2015-08-13 14:31:55 -07006061 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006062 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006063 /*
6064 * check whether the device shows up in other nodes
6065 */
6066 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006067 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006068 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006069 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006070 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006071 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006072 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006073 export_rdev(rdev);
6074 return err;
6075 }
6076 }
6077 }
6078
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 rdev->raid_disk = -1;
6080 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 if (err)
6083 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006084
6085 if (mddev_is_clustered(mddev)) {
6086 if (info->state & (1 << MD_DISK_CANDIDATE))
6087 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6088 else {
6089 if (err)
6090 md_cluster_ops->add_new_disk_cancel(mddev);
6091 else
6092 err = add_bound_rdev(rdev);
6093 }
6094
6095 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006096 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006097
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 return err;
6099 }
6100
6101 /* otherwise, add_new_disk is only allowed
6102 * for major_version==0 superblocks
6103 */
6104 if (mddev->major_version != 0) {
6105 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6106 mdname(mddev));
6107 return -EINVAL;
6108 }
6109
6110 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6111 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006112 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006114 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 "md: error, md_import_device() returned %ld\n",
6116 PTR_ERR(rdev));
6117 return PTR_ERR(rdev);
6118 }
6119 rdev->desc_nr = info->number;
6120 if (info->raid_disk < mddev->raid_disks)
6121 rdev->raid_disk = info->raid_disk;
6122 else
6123 rdev->raid_disk = -1;
6124
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006126 if (info->state & (1<<MD_DISK_SYNC))
6127 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006129 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6130 set_bit(WriteMostly, &rdev->flags);
6131
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 if (!mddev->persistent) {
6133 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006134 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6135 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006136 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006137 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138
NeilBrown2bf071b2006-01-06 00:20:55 -08006139 err = bind_rdev_to_array(rdev, mddev);
6140 if (err) {
6141 export_rdev(rdev);
6142 return err;
6143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 }
6145
6146 return 0;
6147}
6148
NeilBrownf72ffdd2014-09-30 14:23:59 +10006149static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150{
6151 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006152 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 rdev = find_rdev(mddev, dev);
6155 if (!rdev)
6156 return -ENXIO;
6157
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006158 if (rdev->raid_disk < 0)
6159 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006160
NeilBrown3ea8929d2013-04-24 11:42:41 +10006161 clear_bit(Blocked, &rdev->flags);
6162 remove_and_add_spares(mddev, rdev);
6163
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 if (rdev->raid_disk >= 0)
6165 goto busy;
6166
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006167kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006168 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006169 md_cluster_ops->remove_disk(mddev, rdev);
6170
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006171 md_kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07006172 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006173 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
6175 return 0;
6176busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006177 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 bdevname(rdev->bdev,b), mdname(mddev));
6179 return -EBUSY;
6180}
6181
NeilBrownf72ffdd2014-09-30 14:23:59 +10006182static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183{
6184 char b[BDEVNAME_SIZE];
6185 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006186 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187
6188 if (!mddev->pers)
6189 return -ENODEV;
6190
6191 if (mddev->major_version != 0) {
6192 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6193 " version-0 superblocks.\n",
6194 mdname(mddev));
6195 return -EINVAL;
6196 }
6197 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006198 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 "%s: personality does not support diskops!\n",
6200 mdname(mddev));
6201 return -EINVAL;
6202 }
6203
NeilBrownd710e132008-10-13 11:55:12 +11006204 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006206 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 "md: error, md_import_device() returned %ld\n",
6208 PTR_ERR(rdev));
6209 return -EINVAL;
6210 }
6211
6212 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006213 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006215 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
NeilBrown8190e752009-06-18 08:48:58 +10006217 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
NeilBrownb2d444d2005-11-08 21:39:31 -08006219 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006220 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 "md: can not hot-add faulty %s disk to %s!\n",
6222 bdevname(rdev->bdev,b), mdname(mddev));
6223 err = -EINVAL;
6224 goto abort_export;
6225 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006226
NeilBrownb2d444d2005-11-08 21:39:31 -08006227 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006229 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006230 err = bind_rdev_to_array(rdev, mddev);
6231 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006232 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
6234 /*
6235 * The rest should better be atomic, we can have disk failures
6236 * noticed in interrupt contexts ...
6237 */
6238
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 rdev->raid_disk = -1;
6240
NeilBrown850b2b42006-10-03 01:15:46 -07006241 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 /*
6243 * Kick recovery, maybe this spare has to be added to the
6244 * array immediately.
6245 */
6246 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6247 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006248 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 return 0;
6250
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251abort_export:
6252 export_rdev(rdev);
6253 return err;
6254}
6255
NeilBrownfd01b882011-10-11 16:47:53 +11006256static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006257{
NeilBrown035328c2014-04-09 12:25:40 +10006258 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006259
NeilBrown36fa3062005-09-09 16:23:45 -07006260 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006261 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006262 return -EBUSY;
6263 if (mddev->recovery || mddev->sync_thread)
6264 return -EBUSY;
6265 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006266 }
6267
NeilBrown36fa3062005-09-09 16:23:45 -07006268 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006269 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006270 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006271
NeilBrown1e594bb2014-12-15 12:57:00 +11006272 if (mddev->bitmap || mddev->bitmap_info.file)
6273 return -EEXIST; /* cannot add when bitmap is present */
6274 f = fget(fd);
6275
6276 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006277 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6278 mdname(mddev));
6279 return -EBADF;
6280 }
6281
NeilBrown1e594bb2014-12-15 12:57:00 +11006282 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006283 if (!S_ISREG(inode->i_mode)) {
6284 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6285 mdname(mddev));
6286 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006287 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006288 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6289 mdname(mddev));
6290 err = -EBADF;
6291 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006292 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6293 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006294 err = -EBUSY;
6295 }
6296 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006297 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006298 return err;
6299 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006300 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006301 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006302 } else if (mddev->bitmap == NULL)
6303 return -ENOENT; /* cannot remove what isn't there */
6304 err = 0;
6305 if (mddev->pers) {
6306 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006307 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006308 struct bitmap *bitmap;
6309
6310 bitmap = bitmap_create(mddev, -1);
6311 if (!IS_ERR(bitmap)) {
6312 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006313 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006314 } else
6315 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006316 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006317 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006318 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006319 fd = -1; /* make sure to put the file */
6320 }
NeilBrown36fa3062005-09-09 16:23:45 -07006321 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006322 }
6323 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006324 struct file *f = mddev->bitmap_info.file;
6325 if (f) {
6326 spin_lock(&mddev->lock);
6327 mddev->bitmap_info.file = NULL;
6328 spin_unlock(&mddev->lock);
6329 fput(f);
6330 }
NeilBrown36fa3062005-09-09 16:23:45 -07006331 }
6332
NeilBrown32a76272005-06-21 17:17:14 -07006333 return err;
6334}
6335
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336/*
6337 * set_array_info is used two different ways
6338 * The original usage is when creating a new array.
6339 * In this usage, raid_disks is > 0 and it together with
6340 * level, size, not_persistent,layout,chunksize determine the
6341 * shape of the array.
6342 * This will always create an array with a type-0.90.0 superblock.
6343 * The newer usage is when assembling an array.
6344 * In this case raid_disks will be 0, and the major_version field is
6345 * use to determine which style super-blocks are to be found on the devices.
6346 * The minor and patch _version numbers are also kept incase the
6347 * super_block handler wishes to interpret them.
6348 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006349static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350{
6351
6352 if (info->raid_disks == 0) {
6353 /* just setting version number for superblock loading */
6354 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006355 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 super_types[info->major_version].name == NULL) {
6357 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006358 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 "md: superblock version %d not known\n",
6360 info->major_version);
6361 return -EINVAL;
6362 }
6363 mddev->major_version = info->major_version;
6364 mddev->minor_version = info->minor_version;
6365 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006366 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006367 /* ensure mddev_put doesn't delete this now that there
6368 * is some minimal configuration.
6369 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006370 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 return 0;
6372 }
6373 mddev->major_version = MD_MAJOR_VERSION;
6374 mddev->minor_version = MD_MINOR_VERSION;
6375 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006376 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
6378 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006379 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006380 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 mddev->raid_disks = info->raid_disks;
6382 /* don't set md_minor, it is determined by which /dev/md* was
6383 * openned
6384 */
6385 if (info->state & (1<<MD_SB_CLEAN))
6386 mddev->recovery_cp = MaxSector;
6387 else
6388 mddev->recovery_cp = 0;
6389 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006390 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
6392 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006393 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394
6395 mddev->max_disks = MD_SB_DISKS;
6396
NeilBrowne6910632008-02-06 01:39:51 -08006397 if (mddev->persistent)
6398 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006399 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
NeilBrownc3d97142009-12-14 12:49:52 +11006401 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006402 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006403 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006404
NeilBrownf6705572006-03-27 01:18:11 -08006405 mddev->reshape_position = MaxSector;
6406
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 /*
6408 * Generate a 128 bit UUID
6409 */
6410 get_random_bytes(mddev->uuid, 16);
6411
NeilBrownf6705572006-03-27 01:18:11 -08006412 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006413 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006414 mddev->new_layout = mddev->layout;
6415 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006416 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006417
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 return 0;
6419}
6420
NeilBrownfd01b882011-10-11 16:47:53 +11006421void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006422{
Dan Williamsb522adc2009-03-31 15:00:31 +11006423 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6424
6425 if (mddev->external_size)
6426 return;
6427
Dan Williams1f403622009-03-31 14:59:03 +11006428 mddev->array_sectors = array_sectors;
6429}
6430EXPORT_SYMBOL(md_set_array_sectors);
6431
NeilBrownfd01b882011-10-11 16:47:53 +11006432static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006433{
NeilBrown3cb03002011-10-11 16:45:26 +11006434 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006435 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006436 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006437
6438 if (mddev->pers->resize == NULL)
6439 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006440 /* The "num_sectors" is the number of sectors of each device that
6441 * is used. This can only make sense for arrays with redundancy.
6442 * linear and raid0 always use whatever space is available. We can only
6443 * consider changing this number if no resync or reconstruction is
6444 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006445 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006446 * of each device. If num_sectors is zero, we find the largest size
6447 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006448 */
NeilBrownf851b602014-12-11 10:02:10 +11006449 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6450 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006451 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006452 if (mddev->ro)
6453 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006454
NeilBrowndafb20f2012-03-19 12:46:39 +11006455 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006456 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006457
Andre Nolld71f9f82008-07-11 22:02:22 +10006458 if (fit && (num_sectors == 0 || num_sectors > avail))
6459 num_sectors = avail;
6460 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006461 return -ENOSPC;
6462 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006463 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006464 if (!rv)
6465 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006466 return rv;
6467}
6468
NeilBrownfd01b882011-10-11 16:47:53 +11006469static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006470{
6471 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006472 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006473 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006474 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006475 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006476 if (mddev->ro)
6477 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006478 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006479 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006480 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006481 if (mddev->sync_thread ||
6482 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6483 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006484 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006485
6486 rdev_for_each(rdev, mddev) {
6487 if (mddev->raid_disks < raid_disks &&
6488 rdev->data_offset < rdev->new_data_offset)
6489 return -EINVAL;
6490 if (mddev->raid_disks > raid_disks &&
6491 rdev->data_offset > rdev->new_data_offset)
6492 return -EINVAL;
6493 }
6494
NeilBrown63c70c42006-03-27 01:18:13 -08006495 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006496 if (mddev->delta_disks < 0)
6497 mddev->reshape_backwards = 1;
6498 else if (mddev->delta_disks > 0)
6499 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006500
6501 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006502 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006503 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006504 mddev->reshape_backwards = 0;
6505 }
NeilBrownda943b992006-01-06 00:20:54 -08006506 return rv;
6507}
6508
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509/*
6510 * update_array_info is used to change the configuration of an
6511 * on-line array.
6512 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6513 * fields in the info are checked against the array.
6514 * Any differences that cannot be handled will cause an error.
6515 * Normally, only one change can be managed at a time.
6516 */
NeilBrownfd01b882011-10-11 16:47:53 +11006517static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518{
6519 int rv = 0;
6520 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006521 int state = 0;
6522
6523 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006524 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006525 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526
6527 if (mddev->major_version != info->major_version ||
6528 mddev->minor_version != info->minor_version ||
6529/* mddev->patch_version != info->patch_version || */
6530 mddev->ctime != info->ctime ||
6531 mddev->level != info->level ||
6532/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006533 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006534 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006535 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6536 ((state^info->state) & 0xfffffe00)
6537 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 return -EINVAL;
6539 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006540 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6541 cnt++;
6542 if (mddev->raid_disks != info->raid_disks)
6543 cnt++;
6544 if (mddev->layout != info->layout)
6545 cnt++;
6546 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6547 cnt++;
6548 if (cnt == 0)
6549 return 0;
6550 if (cnt > 1)
6551 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552
6553 if (mddev->layout != info->layout) {
6554 /* Change layout
6555 * we don't need to do anything at the md level, the
6556 * personality will take care of it all.
6557 */
NeilBrown50ac1682009-06-18 08:47:55 +10006558 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006560 else {
6561 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006562 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006563 if (rv)
6564 mddev->new_layout = mddev->layout;
6565 return rv;
6566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006568 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006569 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
NeilBrownda943b992006-01-06 00:20:54 -08006571 if (mddev->raid_disks != info->raid_disks)
6572 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573
NeilBrown36fa3062005-09-09 16:23:45 -07006574 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006575 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6576 rv = -EINVAL;
6577 goto err;
6578 }
6579 if (mddev->recovery || mddev->sync_thread) {
6580 rv = -EBUSY;
6581 goto err;
6582 }
NeilBrown36fa3062005-09-09 16:23:45 -07006583 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006584 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006585 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006586 if (mddev->bitmap) {
6587 rv = -EEXIST;
6588 goto err;
6589 }
6590 if (mddev->bitmap_info.default_offset == 0) {
6591 rv = -EINVAL;
6592 goto err;
6593 }
NeilBrownc3d97142009-12-14 12:49:52 +11006594 mddev->bitmap_info.offset =
6595 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006596 mddev->bitmap_info.space =
6597 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006598 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006599 bitmap = bitmap_create(mddev, -1);
6600 if (!IS_ERR(bitmap)) {
6601 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006602 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006603 } else
6604 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006605 if (rv)
6606 bitmap_destroy(mddev);
6607 mddev->pers->quiesce(mddev, 0);
6608 } else {
6609 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006610 if (!mddev->bitmap) {
6611 rv = -ENOENT;
6612 goto err;
6613 }
6614 if (mddev->bitmap->storage.file) {
6615 rv = -EINVAL;
6616 goto err;
6617 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006618 if (mddev->bitmap_info.nodes) {
6619 /* hold PW on all the bitmap lock */
6620 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6621 printk("md: can't change bitmap to none since the"
6622 " array is in use by more than one node\n");
6623 rv = -EPERM;
6624 md_cluster_ops->unlock_all_bitmaps(mddev);
6625 goto err;
6626 }
6627
6628 mddev->bitmap_info.nodes = 0;
6629 md_cluster_ops->leave(mddev);
6630 }
NeilBrown36fa3062005-09-09 16:23:45 -07006631 mddev->pers->quiesce(mddev, 1);
6632 bitmap_destroy(mddev);
6633 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006634 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006635 }
6636 }
NeilBrown850b2b42006-10-03 01:15:46 -07006637 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006638 return rv;
6639err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 return rv;
6641}
6642
NeilBrownfd01b882011-10-11 16:47:53 +11006643static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644{
NeilBrown3cb03002011-10-11 16:45:26 +11006645 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006646 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647
6648 if (mddev->pers == NULL)
6649 return -ENODEV;
6650
NeilBrown1ca69c42012-10-11 13:37:33 +11006651 rcu_read_lock();
6652 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006654 err = -ENODEV;
6655 else {
6656 md_error(mddev, rdev);
6657 if (!test_bit(Faulty, &rdev->flags))
6658 err = -EBUSY;
6659 }
6660 rcu_read_unlock();
6661 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662}
6663
Andre Noll2f9618c2008-04-25 18:57:58 +02006664/*
6665 * We have a problem here : there is no easy way to give a CHS
6666 * virtual geometry. We currently pretend that we have a 2 heads
6667 * 4 sectors (with a BIG number of cylinders...). This drives
6668 * dosfs just mad... ;-)
6669 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006670static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6671{
NeilBrownfd01b882011-10-11 16:47:53 +11006672 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006673
6674 geo->heads = 2;
6675 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006676 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006677 return 0;
6678}
6679
Nicolas Schichancb335f82014-01-15 16:58:52 +01006680static inline bool md_ioctl_valid(unsigned int cmd)
6681{
6682 switch (cmd) {
6683 case ADD_NEW_DISK:
6684 case BLKROSET:
6685 case GET_ARRAY_INFO:
6686 case GET_BITMAP_FILE:
6687 case GET_DISK_INFO:
6688 case HOT_ADD_DISK:
6689 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006690 case RAID_AUTORUN:
6691 case RAID_VERSION:
6692 case RESTART_ARRAY_RW:
6693 case RUN_ARRAY:
6694 case SET_ARRAY_INFO:
6695 case SET_BITMAP_FILE:
6696 case SET_DISK_FAULTY:
6697 case STOP_ARRAY:
6698 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006699 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006700 return true;
6701 default:
6702 return false;
6703 }
6704}
6705
Al Viroa39907f2008-03-02 10:31:15 -05006706static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 unsigned int cmd, unsigned long arg)
6708{
6709 int err = 0;
6710 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006711 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006712 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
Nicolas Schichancb335f82014-01-15 16:58:52 +01006714 if (!md_ioctl_valid(cmd))
6715 return -ENOTTY;
6716
NeilBrown506c9e42011-12-23 10:17:26 +11006717 switch (cmd) {
6718 case RAID_VERSION:
6719 case GET_ARRAY_INFO:
6720 case GET_DISK_INFO:
6721 break;
6722 default:
6723 if (!capable(CAP_SYS_ADMIN))
6724 return -EACCES;
6725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726
6727 /*
6728 * Commands dealing with the RAID driver but not any
6729 * particular array:
6730 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006731 switch (cmd) {
6732 case RAID_VERSION:
6733 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006734 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006737 case RAID_AUTORUN:
6738 err = 0;
6739 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006740 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006742 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 }
6744
6745 /*
6746 * Commands creating/starting a new array:
6747 */
6748
Al Viroa39907f2008-03-02 10:31:15 -05006749 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750
6751 if (!mddev) {
6752 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006753 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 }
6755
NeilBrown1ca69c42012-10-11 13:37:33 +11006756 /* Some actions do not requires the mutex */
6757 switch (cmd) {
6758 case GET_ARRAY_INFO:
6759 if (!mddev->raid_disks && !mddev->external)
6760 err = -ENODEV;
6761 else
6762 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006763 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006764
6765 case GET_DISK_INFO:
6766 if (!mddev->raid_disks && !mddev->external)
6767 err = -ENODEV;
6768 else
6769 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006770 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006771
6772 case SET_DISK_FAULTY:
6773 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006774 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006775
6776 case GET_BITMAP_FILE:
6777 err = get_bitmap_file(mddev, argp);
6778 goto out;
6779
NeilBrown1ca69c42012-10-11 13:37:33 +11006780 }
6781
NeilBrowna7a3f082012-12-11 13:35:54 +11006782 if (cmd == ADD_NEW_DISK)
6783 /* need to ensure md_delayed_delete() has completed */
6784 flush_workqueue(md_misc_wq);
6785
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006786 if (cmd == HOT_REMOVE_DISK)
6787 /* need to ensure recovery thread has run */
6788 wait_event_interruptible_timeout(mddev->sb_wait,
6789 !test_bit(MD_RECOVERY_NEEDED,
6790 &mddev->flags),
6791 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006792 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6793 /* Need to flush page cache, and ensure no-one else opens
6794 * and writes
6795 */
6796 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006797 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006798 mutex_unlock(&mddev->open_mutex);
6799 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006800 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006801 }
6802 set_bit(MD_STILL_CLOSED, &mddev->flags);
6803 mutex_unlock(&mddev->open_mutex);
6804 sync_blockdev(bdev);
6805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 err = mddev_lock(mddev);
6807 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006808 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6810 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006811 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 }
6813
NeilBrownc02c0ae2012-12-11 13:39:21 +11006814 if (cmd == SET_ARRAY_INFO) {
6815 mdu_array_info_t info;
6816 if (!arg)
6817 memset(&info, 0, sizeof(info));
6818 else if (copy_from_user(&info, argp, sizeof(info))) {
6819 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006820 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006821 }
6822 if (mddev->pers) {
6823 err = update_array_info(mddev, &info);
6824 if (err) {
6825 printk(KERN_WARNING "md: couldn't update"
6826 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006827 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006829 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006830 }
6831 if (!list_empty(&mddev->disks)) {
6832 printk(KERN_WARNING
6833 "md: array %s already has disks!\n",
6834 mdname(mddev));
6835 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006836 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006837 }
6838 if (mddev->raid_disks) {
6839 printk(KERN_WARNING
6840 "md: array %s already initialised!\n",
6841 mdname(mddev));
6842 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006843 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006844 }
6845 err = set_array_info(mddev, &info);
6846 if (err) {
6847 printk(KERN_WARNING "md: couldn't set"
6848 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006849 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006850 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006851 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 }
6853
6854 /*
6855 * Commands querying/configuring an existing array:
6856 */
NeilBrown32a76272005-06-21 17:17:14 -07006857 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006858 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006859 if ((!mddev->raid_disks && !mddev->external)
6860 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6861 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6862 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006864 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 }
6866
6867 /*
6868 * Commands even a read-only array can execute:
6869 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006870 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006871 case RESTART_ARRAY_RW:
6872 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006873 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006874
6875 case STOP_ARRAY:
6876 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006877 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006878
6879 case STOP_ARRAY_RO:
6880 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006881 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006882
NeilBrown3ea8929d2013-04-24 11:42:41 +10006883 case HOT_REMOVE_DISK:
6884 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006885 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006886
NeilBrown7ceb17e2013-04-24 11:42:42 +10006887 case ADD_NEW_DISK:
6888 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08006889 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10006890 * So require mddev->pers and MD_DISK_SYNC.
6891 */
6892 if (mddev->pers) {
6893 mdu_disk_info_t info;
6894 if (copy_from_user(&info, argp, sizeof(info)))
6895 err = -EFAULT;
6896 else if (!(info.state & (1<<MD_DISK_SYNC)))
6897 /* Need to clear read-only for this */
6898 break;
6899 else
6900 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006901 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006902 }
6903 break;
6904
NeilBrownc02c0ae2012-12-11 13:39:21 +11006905 case BLKROSET:
6906 if (get_user(ro, (int __user *)(arg))) {
6907 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006908 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006909 }
6910 err = -EINVAL;
6911
6912 /* if the bdev is going readonly the value of mddev->ro
6913 * does not matter, no writes are coming
6914 */
6915 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006916 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006917
NeilBrownc02c0ae2012-12-11 13:39:21 +11006918 /* are we are already prepared for writes? */
6919 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006920 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006921
6922 /* transitioning to readauto need only happen for
6923 * arrays that call md_write_start
6924 */
6925 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006927 if (err == 0) {
6928 mddev->ro = 2;
6929 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006930 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006931 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006932 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 }
6934
6935 /*
6936 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006937 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 */
NeilBrown326eb172014-09-30 15:36:28 +10006939 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006940 if (mddev->ro == 2) {
6941 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006942 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006943 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006944 /* mddev_unlock will wake thread */
6945 /* If a device failed while we were read-only, we
6946 * need to make sure the metadata is updated now.
6947 */
6948 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6949 mddev_unlock(mddev);
6950 wait_event(mddev->sb_wait,
6951 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6952 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006953 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006954 }
NeilBrownf91de922005-11-08 21:39:36 -08006955 } else {
6956 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006957 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 }
6960
NeilBrownc02c0ae2012-12-11 13:39:21 +11006961 switch (cmd) {
6962 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006964 mdu_disk_info_t info;
6965 if (copy_from_user(&info, argp, sizeof(info)))
6966 err = -EFAULT;
6967 else
6968 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006969 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006972 case CLUSTERED_DISK_NACK:
6973 if (mddev_is_clustered(mddev))
6974 md_cluster_ops->new_disk_ack(mddev, false);
6975 else
6976 err = -EINVAL;
6977 goto unlock;
6978
NeilBrownc02c0ae2012-12-11 13:39:21 +11006979 case HOT_ADD_DISK:
6980 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006981 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982
NeilBrownc02c0ae2012-12-11 13:39:21 +11006983 case RUN_ARRAY:
6984 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006985 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986
NeilBrownc02c0ae2012-12-11 13:39:21 +11006987 case SET_BITMAP_FILE:
6988 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006989 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006990
NeilBrownc02c0ae2012-12-11 13:39:21 +11006991 default:
6992 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006993 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 }
6995
NeilBrown3adc28d2014-09-30 15:46:41 +10006996unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006997 if (mddev->hold_active == UNTIL_IOCTL &&
6998 err != -EINVAL)
6999 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 return err;
7003}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007004#ifdef CONFIG_COMPAT
7005static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7006 unsigned int cmd, unsigned long arg)
7007{
7008 switch (cmd) {
7009 case HOT_REMOVE_DISK:
7010 case HOT_ADD_DISK:
7011 case SET_DISK_FAULTY:
7012 case SET_BITMAP_FILE:
7013 /* These take in integer arg, do not convert */
7014 break;
7015 default:
7016 arg = (unsigned long)compat_ptr(arg);
7017 break;
7018 }
7019
7020 return md_ioctl(bdev, mode, cmd, arg);
7021}
7022#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023
Al Viroa39907f2008-03-02 10:31:15 -05007024static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025{
7026 /*
7027 * Succeed if we can lock the mddev, which confirms that
7028 * it isn't being stopped right now.
7029 */
NeilBrownfd01b882011-10-11 16:47:53 +11007030 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031 int err;
7032
Yuanhan Liu0c098222012-05-22 13:55:32 +10007033 if (!mddev)
7034 return -ENODEV;
7035
NeilBrownd3374822009-01-09 08:31:10 +11007036 if (mddev->gendisk != bdev->bd_disk) {
7037 /* we are racing with mddev_put which is discarding this
7038 * bd_disk.
7039 */
7040 mddev_put(mddev);
7041 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007042 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007043 /* Then retry the open from the top */
7044 return -ERESTARTSYS;
7045 }
7046 BUG_ON(mddev != bdev->bd_disk->private_data);
7047
NeilBrownc8c00a62009-08-10 12:50:52 +10007048 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 goto out;
7050
7051 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007052 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10007053 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10007054 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007056 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 out:
7058 return err;
7059}
7060
Al Virodb2a1442013-05-05 21:52:57 -04007061static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007063 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007065 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007066 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007069
7070static int md_media_changed(struct gendisk *disk)
7071{
NeilBrownfd01b882011-10-11 16:47:53 +11007072 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007073
7074 return mddev->changed;
7075}
7076
7077static int md_revalidate(struct gendisk *disk)
7078{
NeilBrownfd01b882011-10-11 16:47:53 +11007079 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007080
7081 mddev->changed = 0;
7082 return 0;
7083}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007084static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085{
7086 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007087 .open = md_open,
7088 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007089 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007090#ifdef CONFIG_COMPAT
7091 .compat_ioctl = md_compat_ioctl,
7092#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007093 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007094 .media_changed = md_media_changed,
7095 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096};
7097
NeilBrownf72ffdd2014-09-30 14:23:59 +10007098static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099{
NeilBrown2b8bf342011-10-11 16:48:23 +11007100 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 /*
7103 * md_thread is a 'system-thread', it's priority should be very
7104 * high. We avoid resource deadlocks individually in each
7105 * raid personality. (RAID5 does preallocation) We also use RR and
7106 * the very same RT priority as kswapd, thus we will never get
7107 * into a priority inversion deadlock.
7108 *
7109 * we definitely have to have equal or higher priority than
7110 * bdflush, otherwise bdflush will deadlock if there are too
7111 * many dirty RAID5 blocks.
7112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113
NeilBrown6985c432005-10-19 21:23:47 -07007114 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007115 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116
NeilBrown93588e22005-11-15 00:09:12 -08007117 /* We need to wait INTERRUPTIBLE so that
7118 * we don't add to the load-average.
7119 * That means we need to be sure no signals are
7120 * pending
7121 */
7122 if (signal_pending(current))
7123 flush_signals(current);
7124
7125 wait_event_interruptible_timeout
7126 (thread->wqueue,
7127 test_bit(THREAD_WAKEUP, &thread->flags)
7128 || kthread_should_stop(),
7129 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130
NeilBrown6c987912011-01-14 09:13:53 +11007131 clear_bit(THREAD_WAKEUP, &thread->flags);
7132 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007133 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007135
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 return 0;
7137}
7138
NeilBrown2b8bf342011-10-11 16:48:23 +11007139void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140{
7141 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007142 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 set_bit(THREAD_WAKEUP, &thread->flags);
7144 wake_up(&thread->wqueue);
7145 }
7146}
NeilBrown6c144d32014-09-30 16:15:38 +10007147EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148
Shaohua Li4ed87312012-10-11 13:34:00 +11007149struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7150 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151{
NeilBrown2b8bf342011-10-11 16:48:23 +11007152 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153
NeilBrown2b8bf342011-10-11 16:48:23 +11007154 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 if (!thread)
7156 return NULL;
7157
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 init_waitqueue_head(&thread->wqueue);
7159
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 thread->run = run;
7161 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007162 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007163 thread->tsk = kthread_run(md_thread, thread,
7164 "%s_%s",
7165 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007166 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007167 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 kfree(thread);
7169 return NULL;
7170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 return thread;
7172}
NeilBrown6c144d32014-09-30 16:15:38 +10007173EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174
NeilBrown2b8bf342011-10-11 16:48:23 +11007175void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176{
NeilBrown2b8bf342011-10-11 16:48:23 +11007177 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007178 if (!thread)
7179 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007180 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007181 /* Locking ensures that mddev_unlock does not wake_up a
7182 * non-existent thread
7183 */
7184 spin_lock(&pers_lock);
7185 *threadp = NULL;
7186 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007187
7188 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 kfree(thread);
7190}
NeilBrown6c144d32014-09-30 16:15:38 +10007191EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192
NeilBrownfd01b882011-10-11 16:47:53 +11007193void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194{
NeilBrownb2d444d2005-11-08 21:39:31 -08007195 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007197
NeilBrownde393cd2011-07-28 11:31:48 +10007198 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 return;
7200 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007201 if (mddev->degraded)
7202 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007203 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7205 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7206 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007207 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007208 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007209 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210}
NeilBrown6c144d32014-09-30 16:15:38 +10007211EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212
7213/* seq_file implementation /proc/mdstat */
7214
7215static void status_unused(struct seq_file *seq)
7216{
7217 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007218 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219
7220 seq_printf(seq, "unused devices: ");
7221
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007222 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 char b[BDEVNAME_SIZE];
7224 i++;
7225 seq_printf(seq, "%s ",
7226 bdevname(rdev->bdev,b));
7227 }
7228 if (!i)
7229 seq_printf(seq, "<none>");
7230
7231 seq_printf(seq, "\n");
7232}
7233
NeilBrownf7851be2015-07-02 17:12:58 +10007234static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235{
NeilBrowndd71cf62009-05-07 12:49:35 +10007236 sector_t max_sectors, resync, res;
7237 unsigned long dt, db;
7238 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007239 int scale;
7240 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241
NeilBrownc804cde2012-05-21 09:28:33 +10007242 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7243 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007244 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007246 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247
NeilBrownf7851be2015-07-02 17:12:58 +10007248 resync = mddev->curr_resync;
7249 if (resync <= 3) {
7250 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7251 /* Still cleaning up */
7252 resync = max_sectors;
7253 } else
7254 resync -= atomic_read(&mddev->recovery_active);
7255
7256 if (resync == 0) {
7257 if (mddev->recovery_cp < MaxSector) {
7258 seq_printf(seq, "\tresync=PENDING");
7259 return 1;
7260 }
7261 return 0;
7262 }
7263 if (resync < 3) {
7264 seq_printf(seq, "\tresync=DELAYED");
7265 return 1;
7266 }
7267
NeilBrown403df472014-09-30 15:52:29 +10007268 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007269 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007270 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007271 * u32, as those are the requirements for sector_div.
7272 * Thus 'scale' must be at least 10
7273 */
7274 scale = 10;
7275 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007276 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007277 scale++;
7278 }
7279 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007280 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007281
7282 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 {
NeilBrown4588b422006-03-27 01:18:04 -08007284 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 seq_printf(seq, "[");
7286 for (i = 0; i < x; i++)
7287 seq_printf(seq, "=");
7288 seq_printf(seq, ">");
7289 for (i = 0; i < y; i++)
7290 seq_printf(seq, ".");
7291 seq_printf(seq, "] ");
7292 }
NeilBrown4588b422006-03-27 01:18:04 -08007293 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007294 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7295 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007296 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7297 "check" :
7298 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7299 "resync" : "recovery"))),
7300 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007301 (unsigned long long) resync/2,
7302 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303
7304 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 * dt: time from mark until now
7306 * db: blocks written from mark until now
7307 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007308 *
7309 * rt is a sector_t, so could be 32bit or 64bit.
7310 * So we divide before multiply in case it is 32bit and close
7311 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007312 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007313 * near the end of resync when the number of remaining sectors
7314 * is close to 'db'.
7315 * We then divide rt by 32 after multiplying by db to compensate.
7316 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 */
7318 dt = ((jiffies - mddev->resync_mark) / HZ);
7319 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007320 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7321 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322
NeilBrowndd71cf62009-05-07 12:49:35 +10007323 rt = max_sectors - resync; /* number of remaining sectors */
7324 sector_div(rt, db/32+1);
7325 rt *= dt;
7326 rt >>= 5;
7327
7328 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7329 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330
NeilBrownff4e8d92006-07-10 04:44:16 -07007331 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007332 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333}
7334
7335static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7336{
7337 struct list_head *tmp;
7338 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007339 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340
7341 if (l >= 0x10000)
7342 return NULL;
7343 if (!l--)
7344 /* header */
7345 return (void*)1;
7346
7347 spin_lock(&all_mddevs_lock);
7348 list_for_each(tmp,&all_mddevs)
7349 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007350 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 mddev_get(mddev);
7352 spin_unlock(&all_mddevs_lock);
7353 return mddev;
7354 }
7355 spin_unlock(&all_mddevs_lock);
7356 if (!l--)
7357 return (void*)2;/* tail */
7358 return NULL;
7359}
7360
7361static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7362{
7363 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007364 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007365
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 ++*pos;
7367 if (v == (void*)2)
7368 return NULL;
7369
7370 spin_lock(&all_mddevs_lock);
7371 if (v == (void*)1)
7372 tmp = all_mddevs.next;
7373 else
7374 tmp = mddev->all_mddevs.next;
7375 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007376 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377 else {
7378 next_mddev = (void*)2;
7379 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 spin_unlock(&all_mddevs_lock);
7382
7383 if (v != (void*)1)
7384 mddev_put(mddev);
7385 return next_mddev;
7386
7387}
7388
7389static void md_seq_stop(struct seq_file *seq, void *v)
7390{
NeilBrownfd01b882011-10-11 16:47:53 +11007391 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392
7393 if (mddev && v != (void*)1 && v != (void*)2)
7394 mddev_put(mddev);
7395}
7396
7397static int md_seq_show(struct seq_file *seq, void *v)
7398{
NeilBrownfd01b882011-10-11 16:47:53 +11007399 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007400 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007401 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402
7403 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007404 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 seq_printf(seq, "Personalities : ");
7406 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007407 list_for_each_entry(pers, &pers_list, list)
7408 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
7410 spin_unlock(&pers_lock);
7411 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007412 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 return 0;
7414 }
7415 if (v == (void*)2) {
7416 status_unused(seq);
7417 return 0;
7418 }
7419
NeilBrown36d091f2014-12-15 12:56:58 +11007420 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7422 seq_printf(seq, "%s : %sactive", mdname(mddev),
7423 mddev->pers ? "" : "in");
7424 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007425 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007427 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007428 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 seq_printf(seq, " %s", mddev->pers->name);
7430 }
7431
Andre Nolldd8ac332009-03-31 14:33:13 +11007432 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007433 rcu_read_lock();
7434 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 char b[BDEVNAME_SIZE];
7436 seq_printf(seq, " %s[%d]",
7437 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007438 if (test_bit(WriteMostly, &rdev->flags))
7439 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007440 if (test_bit(Journal, &rdev->flags))
7441 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007442 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 seq_printf(seq, "(F)");
7444 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007445 }
7446 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007447 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007448 if (test_bit(Replacement, &rdev->flags))
7449 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007450 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007452 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453
7454 if (!list_empty(&mddev->disks)) {
7455 if (mddev->pers)
7456 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007457 (unsigned long long)
7458 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 else
7460 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007461 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007463 if (mddev->persistent) {
7464 if (mddev->major_version != 0 ||
7465 mddev->minor_version != 90) {
7466 seq_printf(seq," super %d.%d",
7467 mddev->major_version,
7468 mddev->minor_version);
7469 }
NeilBrowne6910632008-02-06 01:39:51 -08007470 } else if (mddev->external)
7471 seq_printf(seq, " super external:%s",
7472 mddev->metadata_type);
7473 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007474 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475
7476 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007477 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007478 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007479 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007480 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007481 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007482 }
NeilBrown32a76272005-06-21 17:17:14 -07007483 } else
7484 seq_printf(seq, "\n ");
7485
NeilBrown57148962012-03-19 12:46:40 +11007486 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487
7488 seq_printf(seq, "\n");
7489 }
NeilBrown36d091f2014-12-15 12:56:58 +11007490 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007491
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492 return 0;
7493}
7494
Jan Engelhardt110518b2009-05-07 12:49:37 +10007495static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 .start = md_seq_start,
7497 .next = md_seq_next,
7498 .stop = md_seq_stop,
7499 .show = md_seq_show,
7500};
7501
7502static int md_seq_open(struct inode *inode, struct file *file)
7503{
Kay Sieversf1514632011-07-12 20:48:39 +02007504 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505 int error;
7506
7507 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007508 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007509 return error;
7510
7511 seq = file->private_data;
7512 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 return error;
7514}
7515
NeilBrowne2f23b62014-04-09 14:33:51 +10007516static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007517static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7518{
Kay Sieversf1514632011-07-12 20:48:39 +02007519 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007520 int mask;
7521
NeilBrowne2f23b62014-04-09 14:33:51 +10007522 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007523 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007524 poll_wait(filp, &md_event_waiters, wait);
7525
7526 /* always allow read */
7527 mask = POLLIN | POLLRDNORM;
7528
Kay Sieversf1514632011-07-12 20:48:39 +02007529 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007530 mask |= POLLERR | POLLPRI;
7531 return mask;
7532}
7533
Arjan van de Venfa027c22007-02-12 00:55:33 -08007534static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007535 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 .open = md_seq_open,
7537 .read = seq_read,
7538 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007539 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007540 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541};
7542
NeilBrown84fc4b52011-10-11 16:49:58 +11007543int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544{
Chao Yu50bd3772014-09-25 15:28:34 +08007545 printk(KERN_INFO "md: %s personality registered for level %d\n",
7546 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007548 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 spin_unlock(&pers_lock);
7550 return 0;
7551}
NeilBrown6c144d32014-09-30 16:15:38 +10007552EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553
NeilBrown84fc4b52011-10-11 16:49:58 +11007554int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555{
NeilBrown2604b702006-01-06 00:20:36 -08007556 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007558 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 spin_unlock(&pers_lock);
7560 return 0;
7561}
NeilBrown6c144d32014-09-30 16:15:38 +10007562EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563
NeilBrown6022e752015-08-13 12:32:55 +10007564int register_md_cluster_operations(struct md_cluster_operations *ops,
7565 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007566{
NeilBrown6022e752015-08-13 12:32:55 +10007567 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007568 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007569 if (md_cluster_ops != NULL)
7570 ret = -EALREADY;
7571 else {
7572 md_cluster_ops = ops;
7573 md_cluster_mod = module;
7574 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007575 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007576 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007577}
7578EXPORT_SYMBOL(register_md_cluster_operations);
7579
7580int unregister_md_cluster_operations(void)
7581{
7582 spin_lock(&pers_lock);
7583 md_cluster_ops = NULL;
7584 spin_unlock(&pers_lock);
7585 return 0;
7586}
7587EXPORT_SYMBOL(unregister_md_cluster_operations);
7588
7589int md_setup_cluster(struct mddev *mddev, int nodes)
7590{
7591 int err;
7592
7593 err = request_module("md-cluster");
7594 if (err) {
7595 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007596 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007597 }
7598
7599 spin_lock(&pers_lock);
7600 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7601 spin_unlock(&pers_lock);
7602 return -ENOENT;
7603 }
7604 spin_unlock(&pers_lock);
7605
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007606 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007607}
7608
7609void md_cluster_stop(struct mddev *mddev)
7610{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007611 if (!md_cluster_ops)
7612 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007613 md_cluster_ops->leave(mddev);
7614 module_put(md_cluster_mod);
7615}
7616
NeilBrownfd01b882011-10-11 16:47:53 +11007617static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007619 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007621 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622
7623 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007624 rcu_read_lock();
7625 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007627 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7628 (int)part_stat_read(&disk->part0, sectors[1]) -
7629 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007630 /* sync IO will cause sync_io to increase before the disk_stats
7631 * as sync_io is counted when a request starts, and
7632 * disk_stats is counted when it completes.
7633 * So resync activity will cause curr_events to be smaller than
7634 * when there was no such activity.
7635 * non-sync IO will cause disk_stat to increase without
7636 * increasing sync_io so curr_events will (eventually)
7637 * be larger than it was before. Once it becomes
7638 * substantially larger, the test below will cause
7639 * the array to appear non-idle, and resync will slow
7640 * down.
7641 * If there is a lot of outstanding resync activity when
7642 * we set last_event to curr_events, then all that activity
7643 * completing might cause the array to appear non-idle
7644 * and resync will be slowed down even though there might
7645 * not have been non-resync activity. This will only
7646 * happen once though. 'last_events' will soon reflect
7647 * the state where there is little or no outstanding
7648 * resync requests, and further resync activity will
7649 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007650 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007652 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 rdev->last_events = curr_events;
7654 idle = 0;
7655 }
7656 }
NeilBrown4b809912008-07-21 17:05:25 +10007657 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658 return idle;
7659}
7660
NeilBrownfd01b882011-10-11 16:47:53 +11007661void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662{
7663 /* another "blocks" (512byte) blocks have been synced */
7664 atomic_sub(blocks, &mddev->recovery_active);
7665 wake_up(&mddev->recovery_wait);
7666 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007667 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007668 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 md_wakeup_thread(mddev->thread);
7670 // stop recovery, signal do_sync ....
7671 }
7672}
NeilBrown6c144d32014-09-30 16:15:38 +10007673EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674
NeilBrown06d91a52005-06-21 17:17:12 -07007675/* md_write_start(mddev, bi)
7676 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007677 * in superblock) before writing, schedule a superblock update
7678 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007679 */
NeilBrownfd01b882011-10-11 16:47:53 +11007680void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681{
Neil Brown0fd62b82008-06-28 08:31:36 +10007682 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007683 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007684 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007685
NeilBrownf91de922005-11-08 21:39:36 -08007686 BUG_ON(mddev->ro == 1);
7687 if (mddev->ro == 2) {
7688 /* need to switch to read/write */
7689 mddev->ro = 0;
7690 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7691 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007692 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007693 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007694 }
NeilBrown06d91a52005-06-21 17:17:12 -07007695 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007696 if (mddev->safemode == 1)
7697 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007698 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007699 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007700 if (mddev->in_sync) {
7701 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007702 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007703 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007704 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007705 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007706 }
NeilBrown85572d72014-12-15 12:56:56 +11007707 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007708 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007709 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007710 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007711 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007712 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713}
NeilBrown6c144d32014-09-30 16:15:38 +10007714EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715
NeilBrownfd01b882011-10-11 16:47:53 +11007716void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717{
7718 if (atomic_dec_and_test(&mddev->writes_pending)) {
7719 if (mddev->safemode == 2)
7720 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007721 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7723 }
7724}
NeilBrown6c144d32014-09-30 16:15:38 +10007725EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726
NeilBrown2a2275d2007-01-26 00:57:11 -08007727/* md_allow_write(mddev)
7728 * Calling this ensures that the array is marked 'active' so that writes
7729 * may proceed without blocking. It is important to call this before
7730 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7731 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007732 *
Guoqing Jiangabf35082015-12-21 10:51:01 +11007733 * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007734 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007735 */
NeilBrownfd01b882011-10-11 16:47:53 +11007736int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007737{
7738 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007739 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007740 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007741 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007742 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007743 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007744
NeilBrown85572d72014-12-15 12:56:56 +11007745 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007746 if (mddev->in_sync) {
7747 mddev->in_sync = 0;
7748 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007749 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007750 if (mddev->safemode_delay &&
7751 mddev->safemode == 0)
7752 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007753 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007754 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007755 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007756 } else
NeilBrown85572d72014-12-15 12:56:56 +11007757 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007758
NeilBrown070dc6d2010-08-30 17:33:34 +10007759 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007760 return -EAGAIN;
7761 else
7762 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007763}
7764EXPORT_SYMBOL_GPL(md_allow_write);
7765
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766#define SYNC_MARKS 10
7767#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007768#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007769void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770{
Shaohua Li4ed87312012-10-11 13:34:00 +11007771 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007772 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773 unsigned int currspeed = 0,
7774 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007775 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007777 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007778 sector_t mark_cnt[SYNC_MARKS];
7779 int last_mark,m;
7780 struct list_head *tmp;
7781 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007782 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007783 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007784 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007785 struct blk_plug plug;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05007786 bool cluster_resync_finished = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787
7788 /* just incase thread restarts... */
7789 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7790 return;
NeilBrown3991b312014-05-28 13:39:23 +10007791 if (mddev->ro) {/* never try to sync a read-only array */
7792 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007793 return;
NeilBrown3991b312014-05-28 13:39:23 +10007794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795
NeilBrown61df9d92006-10-03 01:15:57 -07007796 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007797 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007798 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007799 action = "check";
7800 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007801 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007802 action = "repair";
7803 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007804 desc = "resync";
7805 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7806 desc = "reshape";
7807 else
7808 desc = "recovery";
7809
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007810 mddev->last_sync_action = action ?: desc;
7811
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812 /* we overload curr_resync somewhat here.
7813 * 0 == not engaged in resync at all
7814 * 2 == checking that there is no conflict with another sync
7815 * 1 == like 2, but have yielded to allow conflicting resync to
7816 * commense
7817 * other == active in resync - this many blocks
7818 *
7819 * Before starting a resync we must have set curr_resync to
7820 * 2, and then checked that every "conflicting" array has curr_resync
7821 * less than ours. When we find one that is the same or higher
7822 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7823 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7824 * This will mean we have to start checking from the beginning again.
7825 *
7826 */
7827
7828 do {
7829 mddev->curr_resync = 2;
7830
7831 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007832 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007834 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 if (mddev2 == mddev)
7836 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007837 if (!mddev->parallel_resync
7838 && mddev2->curr_resync
7839 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 DEFINE_WAIT(wq);
7841 if (mddev < mddev2 && mddev->curr_resync == 2) {
7842 /* arbitrarily yield */
7843 mddev->curr_resync = 1;
7844 wake_up(&resync_wait);
7845 }
7846 if (mddev > mddev2 && mddev->curr_resync == 1)
7847 /* no need to wait here, we can wait the next
7848 * time 'round when curr_resync == 2
7849 */
7850 continue;
NeilBrown97441972008-09-19 11:49:54 +10007851 /* We need to wait 'interruptible' so as not to
7852 * contribute to the load average, and not to
7853 * be caught by 'softlockup'
7854 */
7855 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007856 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007857 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007858 printk(KERN_INFO "md: delaying %s of %s"
7859 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007861 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007863 if (signal_pending(current))
7864 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 schedule();
7866 finish_wait(&resync_wait, &wq);
7867 goto try_again;
7868 }
7869 finish_wait(&resync_wait, &wq);
7870 }
7871 }
7872 } while (mddev->curr_resync < 2);
7873
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007874 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007875 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007877 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 */
7879 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007880 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007881 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007882 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7883 j = mddev->resync_min;
7884 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007885 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007886
NeilBrownccfcc3c2006-03-27 01:18:09 -08007887 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007888 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007889 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007891 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007892 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007893 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007894 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007895 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007896 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007897 !test_bit(Faulty, &rdev->flags) &&
7898 !test_bit(In_sync, &rdev->flags) &&
7899 rdev->recovery_offset < j)
7900 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007901 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007902
7903 /* If there is a bitmap, we need to make sure all
7904 * writes that started before we added a spare
7905 * complete before we start doing a recovery.
7906 * Otherwise the write might complete and (via
7907 * bitmap_endwrite) set a bit in the bitmap after the
7908 * recovery has checked that bit and skipped that
7909 * region.
7910 */
7911 if (mddev->bitmap) {
7912 mddev->pers->quiesce(mddev, 1);
7913 mddev->pers->quiesce(mddev, 0);
7914 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916
NeilBrown61df9d92006-10-03 01:15:57 -07007917 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7918 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7919 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007920 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007921 "(but not more than %d KB/sec) for %s.\n",
7922 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923
NeilBrowneea1bf32009-03-31 14:27:02 +11007924 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007925
NeilBrown57afd892005-06-21 17:17:13 -07007926 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 for (m = 0; m < SYNC_MARKS; m++) {
7928 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007929 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930 }
7931 last_mark = 0;
7932 mddev->resync_mark = mark[last_mark];
7933 mddev->resync_mark_cnt = mark_cnt[last_mark];
7934
7935 /*
7936 * Tune reconstruction:
7937 */
7938 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007939 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7940 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941
7942 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943 last_check = 0;
7944
7945 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007946 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007947 "md: resuming %s of %s from checkpoint.\n",
7948 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007950 } else
7951 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007952 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007953 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7954 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007955 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956
majianpeng7c2c57c2012-07-03 12:12:26 +10007957 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007959 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007960
NeilBrown57afd892005-06-21 17:17:13 -07007961 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007962
NeilBrown7a91ee12009-05-26 12:57:21 +10007963 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7964 ((mddev->curr_resync > mddev->curr_resync_completed &&
7965 (mddev->curr_resync - mddev->curr_resync_completed)
7966 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007967 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007968 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007969 >= mddev->resync_max - mddev->curr_resync_completed ||
7970 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007971 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007972 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007973 wait_event(mddev->recovery_wait,
7974 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007975 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007976 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7977 j > mddev->recovery_cp)
7978 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007979 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007980 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007981 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007982 }
NeilBrownacb180b2009-04-14 16:28:34 +10007983
NeilBrownc91abf52013-11-19 12:02:01 +11007984 while (j >= mddev->resync_max &&
7985 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007986 /* As this condition is controlled by user-space,
7987 * we can block indefinitely, so use '_interruptible'
7988 * to avoid triggering warnings.
7989 */
7990 flush_signals(current); /* just in case */
7991 wait_event_interruptible(mddev->recovery_wait,
7992 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007993 || test_bit(MD_RECOVERY_INTR,
7994 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007995 }
NeilBrownacb180b2009-04-14 16:28:34 +10007996
NeilBrownc91abf52013-11-19 12:02:01 +11007997 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7998 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007999
NeilBrown09314792015-02-19 16:04:40 +11008000 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008001 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008002 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004 }
NeilBrown57afd892005-06-21 17:17:13 -07008005
8006 if (!skipped) { /* actual IO requested */
8007 io_sectors += sectors;
8008 atomic_add(sectors, &mddev->recovery_active);
8009 }
8010
NeilBrowne875ece2011-07-28 11:39:24 +10008011 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8012 break;
8013
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008015 if (j > max_sectors)
8016 /* when skipping, extra large numbers can be returned. */
8017 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008018 if (j > 2)
8019 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008020 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008021 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008022 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008023 * visible in /proc/mdstat
8024 */
8025 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008026
8027 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 continue;
8029
NeilBrown57afd892005-06-21 17:17:13 -07008030 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 repeat:
8032 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8033 /* step marks */
8034 int next = (last_mark+1) % SYNC_MARKS;
8035
8036 mddev->resync_mark = mark[next];
8037 mddev->resync_mark_cnt = mark_cnt[next];
8038 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008039 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040 last_mark = next;
8041 }
8042
NeilBrownc91abf52013-11-19 12:02:01 +11008043 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045
8046 /*
8047 * this loop exits only if either when we are slower than
8048 * the 'hard' speed limit, or the system was IO-idle for
8049 * a jiffy.
8050 * the system might be non-idle CPU-wise, but we only care
8051 * about not overloading the IO subsystem. (things like an
8052 * e2fsck being done on the RAID array should execute fast)
8053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054 cond_resched();
8055
Xiao Niac7e50a2014-08-07 09:37:41 -04008056 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8057 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008058 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059
NeilBrown88202a02006-01-06 00:21:36 -08008060 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008061 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008062 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063 goto repeat;
8064 }
NeilBrownac8fa412015-02-19 16:55:00 +11008065 if (!is_mddev_idle(mddev, 0)) {
8066 /*
8067 * Give other IO more of a chance.
8068 * The faster the devices, the less we wait.
8069 */
8070 wait_event(mddev->recovery_wait,
8071 !atomic_read(&mddev->recovery_active));
8072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 }
8074 }
NeilBrownc91abf52013-11-19 12:02:01 +11008075 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8076 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8077 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 /*
8079 * this also signals 'finished resyncing' to md_stop
8080 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008081 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8083
NeilBrown5ed1df22015-07-24 13:27:08 +10008084 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8085 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8086 mddev->curr_resync > 2) {
8087 mddev->curr_resync_completed = mddev->curr_resync;
8088 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8089 }
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008090 /* tell personality and other nodes that we are finished */
8091 if (mddev_is_clustered(mddev)) {
8092 md_cluster_ops->resync_finish(mddev);
8093 cluster_resync_finished = true;
8094 }
NeilBrown09314792015-02-19 16:04:40 +11008095 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096
NeilBrowndfc70642008-05-23 13:04:39 -07008097 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008098 mddev->curr_resync > 2) {
8099 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8100 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8101 if (mddev->curr_resync >= mddev->recovery_cp) {
8102 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008103 "md: checkpointing %s of %s.\n",
8104 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008105 if (test_bit(MD_RECOVERY_ERROR,
8106 &mddev->recovery))
8107 mddev->recovery_cp =
8108 mddev->curr_resync_completed;
8109 else
8110 mddev->recovery_cp =
8111 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008112 }
8113 } else
8114 mddev->recovery_cp = MaxSector;
8115 } else {
8116 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8117 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008118 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008119 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008120 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008121 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008122 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008123 !test_bit(Faulty, &rdev->flags) &&
8124 !test_bit(In_sync, &rdev->flags) &&
8125 rdev->recovery_offset < mddev->curr_resync)
8126 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008127 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008130 skip:
NeilBrown17571282006-12-10 02:20:52 -08008131 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008133 if (mddev_is_clustered(mddev) &&
8134 test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8135 !cluster_resync_finished)
8136 md_cluster_ops->resync_finish(mddev);
8137
NeilBrown23da4222014-12-15 12:57:01 +11008138 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008139 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8140 /* We completed so min/max setting can be forgotten if used. */
8141 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8142 mddev->resync_min = 0;
8143 mddev->resync_max = MaxSector;
8144 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8145 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008146 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008148 spin_unlock(&mddev->lock);
8149
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008152 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153}
NeilBrown29269552006-03-27 01:18:10 -08008154EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155
NeilBrown746d3202013-04-24 11:42:41 +10008156static int remove_and_add_spares(struct mddev *mddev,
8157 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008158{
NeilBrown3cb03002011-10-11 16:45:26 +11008159 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008160 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008161 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008162
NeilBrowndafb20f2012-03-19 12:46:39 +11008163 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008164 if ((this == NULL || rdev == this) &&
8165 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008166 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008167 (test_bit(Faulty, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008168 (!test_bit(In_sync, &rdev->flags) &&
8169 !test_bit(Journal, &rdev->flags))) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008170 atomic_read(&rdev->nr_pending)==0) {
8171 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008172 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008173 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008174 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008175 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008176 }
8177 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008178 if (removed && mddev->kobj.sd)
8179 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008180
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008181 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008182 goto no_add;
8183
NeilBrowndafb20f2012-03-19 12:46:39 +11008184 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008185 if (this && this != rdev)
8186 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008187 if (test_bit(Candidate, &rdev->flags))
8188 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008189 if (rdev->raid_disk >= 0 &&
8190 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008191 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008192 !test_bit(Faulty, &rdev->flags))
8193 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008194 if (rdev->raid_disk >= 0)
8195 continue;
8196 if (test_bit(Faulty, &rdev->flags))
8197 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008198 if (!test_bit(Journal, &rdev->flags)) {
8199 if (mddev->ro &&
8200 ! (rdev->saved_raid_disk >= 0 &&
8201 !test_bit(Bitmap_sync, &rdev->flags)))
8202 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008203
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008204 rdev->recovery_offset = 0;
8205 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008206 if (mddev->pers->
8207 hot_add_disk(mddev, rdev) == 0) {
8208 if (sysfs_link_rdev(mddev, rdev))
8209 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008210 if (!test_bit(Journal, &rdev->flags))
8211 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008212 md_new_event(mddev);
8213 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008214 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008215 }
NeilBrown746d3202013-04-24 11:42:41 +10008216no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008217 if (removed)
8218 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008219 return spares;
8220}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008221
NeilBrownac05f252014-09-30 08:10:42 +10008222static void md_start_sync(struct work_struct *ws)
8223{
8224 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008225 int ret = 0;
8226
8227 if (mddev_is_clustered(mddev)) {
8228 ret = md_cluster_ops->resync_start(mddev);
8229 if (ret) {
8230 mddev->sync_thread = NULL;
8231 goto out;
8232 }
8233 }
NeilBrownac05f252014-09-30 08:10:42 +10008234
8235 mddev->sync_thread = md_register_thread(md_do_sync,
8236 mddev,
8237 "resync");
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008238out:
NeilBrownac05f252014-09-30 08:10:42 +10008239 if (!mddev->sync_thread) {
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008240 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8241 printk(KERN_ERR "%s: could not start resync"
8242 " thread...\n",
8243 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008244 /* leave the spares where they are, it shouldn't hurt */
8245 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8246 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8247 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8248 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8249 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008250 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008251 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8252 &mddev->recovery))
8253 if (mddev->sysfs_action)
8254 sysfs_notify_dirent_safe(mddev->sysfs_action);
8255 } else
8256 md_wakeup_thread(mddev->sync_thread);
8257 sysfs_notify_dirent_safe(mddev->sysfs_action);
8258 md_new_event(mddev);
8259}
8260
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261/*
8262 * This routine is regularly called by all per-raid-array threads to
8263 * deal with generic issues like resync and super-block update.
8264 * Raid personalities that don't have a thread (linear/raid0) do not
8265 * need this as they never do any recovery or update the superblock.
8266 *
8267 * It does not do any resync itself, but rather "forks" off other threads
8268 * to do that as needed.
8269 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8270 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008271 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272 * and wakeups up this thread which will reap the thread and finish up.
8273 * This thread also removes any faulty devices (with nr_pending == 0).
8274 *
8275 * The overall approach is:
8276 * 1/ if the superblock needs updating, update it.
8277 * 2/ If a recovery thread is running, don't do anything else.
8278 * 3/ If recovery has finished, clean up, possibly marking spares active.
8279 * 4/ If there are any faulty devices, remove them.
8280 * 5/ If array is degraded, try to add spares devices
8281 * 6/ If array has spares or is not in-sync, start a resync thread.
8282 */
NeilBrownfd01b882011-10-11 16:47:53 +11008283void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008285 if (mddev->suspended)
8286 return;
8287
NeilBrown5f404022005-06-21 17:17:16 -07008288 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008289 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290
NeilBrownfca4d842005-06-21 17:17:11 -07008291 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008292 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008293 printk(KERN_INFO "md: %s in immediate safe mode\n",
8294 mdname(mddev));
8295 mddev->safemode = 2;
8296 }
8297 flush_signals(current);
8298 }
8299
NeilBrownc89a8ee2008-08-05 15:54:13 +10008300 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8301 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008303 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008304 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008305 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008306 test_bit(MD_RELOAD_SB, &mddev->flags) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008307 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008308 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8309 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310 ))
8311 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008312
NeilBrowndf5b89b2006-03-27 01:18:20 -08008313 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008314 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008315
NeilBrownc89a8ee2008-08-05 15:54:13 +10008316 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008317 struct md_rdev *rdev;
8318 if (!mddev->external && mddev->in_sync)
8319 /* 'Blocked' flag not needed as failed devices
8320 * will be recorded if array switched to read/write.
8321 * Leaving it set will prevent the device
8322 * from being removed.
8323 */
8324 rdev_for_each(rdev, mddev)
8325 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008326 /* On a read-only array we can:
8327 * - remove failed devices
8328 * - add already-in_sync devices if the array itself
8329 * is in-sync.
8330 * As we only add devices that are already in-sync,
8331 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008332 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008333 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008334 /* There is no thread, but we need to call
8335 * ->spare_active and clear saved_raid_disk
8336 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008337 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008338 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008339 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008340 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008341 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008342 goto unlock;
8343 }
8344
Guoqing Jiang659b2542015-12-21 10:50:59 +11008345 if (mddev_is_clustered(mddev)) {
8346 struct md_rdev *rdev;
8347 /* kick the device if another node issued a
8348 * remove disk.
8349 */
8350 rdev_for_each(rdev, mddev) {
8351 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8352 rdev->raid_disk < 0)
8353 md_kick_rdev_from_array(rdev);
8354 }
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008355
8356 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8357 md_reload_sb(mddev, mddev->good_device_nr);
Guoqing Jiang659b2542015-12-21 10:50:59 +11008358 }
8359
NeilBrown31a59e32008-04-30 00:52:30 -07008360 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008361 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008362 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008363 if (mddev->safemode &&
8364 !atomic_read(&mddev->writes_pending) &&
8365 !mddev->in_sync &&
8366 mddev->recovery_cp == MaxSector) {
8367 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008368 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008369 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008370 }
8371 if (mddev->safemode == 1)
8372 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008373 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008374 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008375 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008376 }
NeilBrownfca4d842005-06-21 17:17:11 -07008377
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008378 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07008379 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008380
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8382 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8383 /* resync/recovery still happening */
8384 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8385 goto unlock;
8386 }
8387 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008388 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389 goto unlock;
8390 }
Neil Brown72a23c22008-06-28 08:31:41 +10008391 /* Set RUNNING before clearing NEEDED to avoid
8392 * any transients in the value of "sync_action".
8393 */
NeilBrown72f36d52012-10-11 14:25:57 +11008394 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008395 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008396 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008397 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008398 /* Clear some bits that don't mean anything, but
8399 * might be left set
8400 */
NeilBrown24dd4692005-11-08 21:39:26 -08008401 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8402 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008403
NeilBrowned209582012-04-24 10:23:14 +10008404 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8405 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008406 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407 /* no recovery is running.
8408 * remove any failed drives, then
8409 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008410 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411 * the personality to fail the re-add.
8412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008414 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008415 if (mddev->pers->check_reshape == NULL ||
8416 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008417 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008418 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008419 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008420 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008421 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008422 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8423 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008424 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008425 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008426 } else if (mddev->recovery_cp < MaxSector) {
8427 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008428 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008429 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8430 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008431 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008432
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008434 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008435 /* We are adding a device or devices to an array
8436 * which has the bitmap stored on all devices.
8437 * So make sure all bitmap pages get written
8438 */
8439 bitmap_write_all(mddev->bitmap);
8440 }
NeilBrownac05f252014-09-30 08:10:42 +10008441 INIT_WORK(&mddev->del_work, md_start_sync);
8442 queue_work(md_misc_wq, &mddev->del_work);
8443 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444 }
NeilBrownac05f252014-09-30 08:10:42 +10008445 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008446 if (!mddev->sync_thread) {
8447 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008448 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008449 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8450 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008451 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008452 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008453 }
NeilBrownac05f252014-09-30 08:10:42 +10008454 unlock:
8455 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456 mddev_unlock(mddev);
8457 }
8458}
NeilBrown6c144d32014-09-30 16:15:38 +10008459EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008461void md_reap_sync_thread(struct mddev *mddev)
8462{
8463 struct md_rdev *rdev;
8464
8465 /* resync has finished, collect result */
8466 md_unregister_thread(&mddev->sync_thread);
8467 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8468 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8469 /* success...*/
8470 /* activate any spares */
8471 if (mddev->pers->spare_active(mddev)) {
8472 sysfs_notify(&mddev->kobj, NULL,
8473 "degraded");
8474 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8475 }
8476 }
8477 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8478 mddev->pers->finish_reshape)
8479 mddev->pers->finish_reshape(mddev);
8480
8481 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008482 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008483 */
NeilBrownf4667222013-12-09 12:04:56 +11008484 if (!mddev->degraded)
8485 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008486 rdev->saved_raid_disk = -1;
8487
8488 md_update_sb(mddev, 1);
8489 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008490 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008491 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8492 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8493 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8494 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008495 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008496 /* flag recovery needed just to double check */
8497 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8498 sysfs_notify_dirent_safe(mddev->sysfs_action);
8499 md_new_event(mddev);
8500 if (mddev->event_work.func)
8501 queue_work(md_misc_wq, &mddev->event_work);
8502}
NeilBrown6c144d32014-09-30 16:15:38 +10008503EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008504
NeilBrownfd01b882011-10-11 16:47:53 +11008505void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008506{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008507 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008508 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008509 !test_bit(Blocked, &rdev->flags) &&
8510 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008511 msecs_to_jiffies(5000));
8512 rdev_dec_pending(rdev, mddev);
8513}
8514EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8515
NeilBrownc6563a82012-05-21 09:27:00 +10008516void md_finish_reshape(struct mddev *mddev)
8517{
8518 /* called be personality module when reshape completes. */
8519 struct md_rdev *rdev;
8520
8521 rdev_for_each(rdev, mddev) {
8522 if (rdev->data_offset > rdev->new_data_offset)
8523 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8524 else
8525 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8526 rdev->data_offset = rdev->new_data_offset;
8527 }
8528}
8529EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008530
Vishal Vermafc974ee2015-12-24 19:20:34 -07008531/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008532
Vishal Vermafc974ee2015-12-24 19:20:34 -07008533/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008534int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008535 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008536{
NeilBrownc6563a82012-05-21 09:27:00 +10008537 int rv;
8538 if (is_new)
8539 s += rdev->new_data_offset;
8540 else
8541 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008542 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8543 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008544 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008545 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008546 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
NeilBrown55ce74d2015-08-14 11:11:10 +10008547 set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008548 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008549 return 1;
8550 } else
8551 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008552}
8553EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8554
NeilBrownc6563a82012-05-21 09:27:00 +10008555int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8556 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008557{
NeilBrownc6563a82012-05-21 09:27:00 +10008558 if (is_new)
8559 s += rdev->new_data_offset;
8560 else
8561 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008562 return badblocks_clear(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008563 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008564}
8565EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8566
Adrian Bunk75c96f82005-05-05 16:16:09 -07008567static int md_notify_reboot(struct notifier_block *this,
8568 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569{
8570 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008571 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008572 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573
NeilBrownc744a652012-03-19 12:46:37 +11008574 for_each_mddev(mddev, tmp) {
8575 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008576 if (mddev->pers)
8577 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008578 if (mddev->persistent)
8579 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008580 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008581 }
NeilBrownc744a652012-03-19 12:46:37 +11008582 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583 }
NeilBrownc744a652012-03-19 12:46:37 +11008584 /*
8585 * certain more exotic SCSI devices are known to be
8586 * volatile wrt too early system reboots. While the
8587 * right place to handle this issue is the given
8588 * driver, we do want to have a safe RAID driver ...
8589 */
8590 if (need_delay)
8591 mdelay(1000*1);
8592
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593 return NOTIFY_DONE;
8594}
8595
Adrian Bunk75c96f82005-05-05 16:16:09 -07008596static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597 .notifier_call = md_notify_reboot,
8598 .next = NULL,
8599 .priority = INT_MAX, /* before any real devices */
8600};
8601
8602static void md_geninit(void)
8603{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008604 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008606 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607}
8608
Adrian Bunk75c96f82005-05-05 16:16:09 -07008609static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610{
Tejun Heoe804ac72010-10-15 15:36:08 +02008611 int ret = -ENOMEM;
8612
Tejun Heoada609e2011-01-25 14:35:54 +01008613 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008614 if (!md_wq)
8615 goto err_wq;
8616
8617 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8618 if (!md_misc_wq)
8619 goto err_misc_wq;
8620
8621 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8622 goto err_md;
8623
8624 if ((ret = register_blkdev(0, "mdp")) < 0)
8625 goto err_mdp;
8626 mdp_major = ret;
8627
NeilBrownaf5628f2014-07-31 13:54:54 +10008628 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008629 md_probe, NULL, NULL);
8630 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008631 md_probe, NULL, NULL);
8632
Linus Torvalds1da177e2005-04-16 15:20:36 -07008633 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008634 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008635
8636 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638
Tejun Heoe804ac72010-10-15 15:36:08 +02008639err_mdp:
8640 unregister_blkdev(MD_MAJOR, "md");
8641err_md:
8642 destroy_workqueue(md_misc_wq);
8643err_misc_wq:
8644 destroy_workqueue(md_wq);
8645err_wq:
8646 return ret;
8647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008648
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008649static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008650{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008651 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8652 struct md_rdev *rdev2;
8653 int role, ret;
8654 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008655
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008656 /* Check for change of roles in the active devices */
8657 rdev_for_each(rdev2, mddev) {
8658 if (test_bit(Faulty, &rdev2->flags))
8659 continue;
8660
8661 /* Check if the roles changed */
8662 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008663
8664 if (test_bit(Candidate, &rdev2->flags)) {
8665 if (role == 0xfffe) {
8666 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8667 md_kick_rdev_from_array(rdev2);
8668 continue;
8669 }
8670 else
8671 clear_bit(Candidate, &rdev2->flags);
8672 }
8673
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008674 if (role != rdev2->raid_disk) {
8675 /* got activated */
8676 if (rdev2->raid_disk == -1 && role != 0xffff) {
8677 rdev2->saved_raid_disk = role;
8678 ret = remove_and_add_spares(mddev, rdev2);
8679 pr_info("Activated spare: %s\n",
8680 bdevname(rdev2->bdev,b));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008681 }
8682 /* device faulty
8683 * We just want to do the minimum to mark the disk
8684 * as faulty. The recovery is performed by the
8685 * one who initiated the error.
8686 */
8687 if ((role == 0xfffe) || (role == 0xfffd)) {
8688 md_error(mddev, rdev2);
8689 clear_bit(Blocked, &rdev2->flags);
8690 }
8691 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008692 }
8693
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11008694 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8695 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008696
8697 /* Finally set the event to be up to date */
8698 mddev->events = le64_to_cpu(sb->events);
8699}
8700
8701static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8702{
8703 int err;
8704 struct page *swapout = rdev->sb_page;
8705 struct mdp_superblock_1 *sb;
8706
8707 /* Store the sb page of the rdev in the swapout temporary
8708 * variable in case we err in the future
8709 */
8710 rdev->sb_page = NULL;
8711 alloc_disk_sb(rdev);
8712 ClearPageUptodate(rdev->sb_page);
8713 rdev->sb_loaded = 0;
8714 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
8715
8716 if (err < 0) {
8717 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8718 __func__, __LINE__, rdev->desc_nr, err);
8719 put_page(rdev->sb_page);
8720 rdev->sb_page = swapout;
8721 rdev->sb_loaded = 1;
8722 return err;
8723 }
8724
8725 sb = page_address(rdev->sb_page);
8726 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8727 * is not set
8728 */
8729
8730 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8731 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8732
8733 /* The other node finished recovery, call spare_active to set
8734 * device In_sync and mddev->degraded
8735 */
8736 if (rdev->recovery_offset == MaxSector &&
8737 !test_bit(In_sync, &rdev->flags) &&
8738 mddev->pers->spare_active(mddev))
8739 sysfs_notify(&mddev->kobj, NULL, "degraded");
8740
8741 put_page(swapout);
8742 return 0;
8743}
8744
8745void md_reload_sb(struct mddev *mddev, int nr)
8746{
8747 struct md_rdev *rdev;
8748 int err;
8749
8750 /* Find the rdev */
8751 rdev_for_each_rcu(rdev, mddev) {
8752 if (rdev->desc_nr == nr)
8753 break;
8754 }
8755
8756 if (!rdev || rdev->desc_nr != nr) {
8757 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
8758 return;
8759 }
8760
8761 err = read_rdev(mddev, rdev);
8762 if (err < 0)
8763 return;
8764
8765 check_sb_changes(mddev, rdev);
8766
8767 /* Read all rdev's to update recovery_offset */
8768 rdev_for_each_rcu(rdev, mddev)
8769 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008770}
8771EXPORT_SYMBOL(md_reload_sb);
8772
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773#ifndef MODULE
8774
8775/*
8776 * Searches all registered partitions for autorun RAID arrays
8777 * at boot time.
8778 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008779
8780static LIST_HEAD(all_detected_devices);
8781struct detected_devices_node {
8782 struct list_head list;
8783 dev_t dev;
8784};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008785
8786void md_autodetect_dev(dev_t dev)
8787{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008788 struct detected_devices_node *node_detected_dev;
8789
8790 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8791 if (node_detected_dev) {
8792 node_detected_dev->dev = dev;
8793 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8794 } else {
8795 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8796 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798}
8799
Linus Torvalds1da177e2005-04-16 15:20:36 -07008800static void autostart_arrays(int part)
8801{
NeilBrown3cb03002011-10-11 16:45:26 +11008802 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008803 struct detected_devices_node *node_detected_dev;
8804 dev_t dev;
8805 int i_scanned, i_passed;
8806
8807 i_scanned = 0;
8808 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008809
8810 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8811
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008812 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8813 i_scanned++;
8814 node_detected_dev = list_entry(all_detected_devices.next,
8815 struct detected_devices_node, list);
8816 list_del(&node_detected_dev->list);
8817 dev = node_detected_dev->dev;
8818 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008819 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008820 if (IS_ERR(rdev))
8821 continue;
8822
NeilBrown403df472014-09-30 15:52:29 +10008823 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 continue;
NeilBrown403df472014-09-30 15:52:29 +10008825
NeilBrownd0fae182008-03-04 14:29:31 -08008826 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008828 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008829 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008830
8831 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8832 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833
8834 autorun_devices(part);
8835}
8836
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008837#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838
8839static __exit void md_exit(void)
8840{
NeilBrownfd01b882011-10-11 16:47:53 +11008841 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008843 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008844
NeilBrownaf5628f2014-07-31 13:54:54 +10008845 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008846 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008848 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008849 unregister_blkdev(mdp_major, "mdp");
8850 unregister_reboot_notifier(&md_notifier);
8851 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008852
8853 /* We cannot unload the modules while some process is
8854 * waiting for us in select() or poll() - wake them up
8855 */
8856 md_unloading = 1;
8857 while (waitqueue_active(&md_event_waiters)) {
8858 /* not safe to leave yet */
8859 wake_up(&md_event_waiters);
8860 msleep(delay);
8861 delay += delay;
8862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008864
NeilBrown29ac4aa2008-02-06 01:39:58 -08008865 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008867 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008869 destroy_workqueue(md_misc_wq);
8870 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008871}
8872
Dan Williams685784a2007-07-09 11:56:42 -07008873subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874module_exit(md_exit)
8875
NeilBrownf91de922005-11-08 21:39:36 -08008876static int get_ro(char *buffer, struct kernel_param *kp)
8877{
8878 return sprintf(buffer, "%d", start_readonly);
8879}
8880static int set_ro(const char *val, struct kernel_param *kp)
8881{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03008882 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08008883}
8884
NeilBrown80ca3a42006-07-10 04:44:18 -07008885module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8886module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008887module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008888
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008890MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008891MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008892MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);