blob: 43728a42fccf388b71fd5c01d2c968df2a021936 [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 Viroff01bb42011-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);
Shaohua Li9c573de2016-04-25 16:52:38 -0700287 /* bio could be mergeable after passing to underlayer */
288 bio->bi_rw &= ~REQ_NOMERGE;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200289 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100290
Gu Zheng74672d02015-04-03 08:44:47 +0800291 cpu = part_stat_lock();
292 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
293 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
294 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100295
NeilBrown409c57f2009-03-31 14:39:39 +1100296 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
297 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700298
299 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
NeilBrown9e35b992010-04-06 14:23:02 +1000302/* mddev_suspend makes sure no new requests are submitted
303 * to the device, and that any requests that have been submitted
304 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100305 * Once mddev_detach() is called and completes, the module will be
306 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000307 */
NeilBrownfd01b882011-10-11 16:47:53 +1100308void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100309{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200310 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100311 if (mddev->suspended++)
312 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100313 synchronize_rcu();
314 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
315 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500316
317 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100318}
NeilBrown390ee602010-06-01 19:37:27 +1000319EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100320
NeilBrownfd01b882011-10-11 16:47:53 +1100321void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100322{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100323 if (--mddev->suspended)
324 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100325 wake_up(&mddev->sb_wait);
326 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500327
Jonathan Brassow47525e52012-05-22 13:55:29 +1000328 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500329 md_wakeup_thread(mddev->thread);
330 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100331}
NeilBrown390ee602010-06-01 19:37:27 +1000332EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100333
NeilBrownfd01b882011-10-11 16:47:53 +1100334int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000335{
NeilBrown5c675f82014-12-15 12:56:56 +1100336 struct md_personality *pers = mddev->pers;
337 int ret = 0;
338
339 rcu_read_lock();
340 if (mddev->suspended)
341 ret = 1;
342 else if (pers && pers->congested)
343 ret = pers->congested(mddev, bits);
344 rcu_read_unlock();
345 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000346}
NeilBrown5c675f82014-12-15 12:56:56 +1100347EXPORT_SYMBOL_GPL(mddev_congested);
348static int md_congested(void *data, int bits)
349{
350 struct mddev *mddev = data;
351 return mddev_congested(mddev, bits);
352}
NeilBrown3fa841d2009-09-23 18:10:29 +1000353
NeilBrowna2826aa2009-12-14 12:49:49 +1100354/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200355 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100356 */
357
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200358static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100359{
NeilBrown3cb03002011-10-11 16:45:26 +1100360 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100361 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100362
363 rdev_dec_pending(rdev, mddev);
364
365 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200366 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200367 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100368 }
369 bio_put(bio);
370}
371
NeilBrowna7a07e62010-12-09 16:04:25 +1100372static void md_submit_flush_data(struct work_struct *ws);
373
NeilBrowna035fc32010-12-09 16:17:51 +1100374static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100375{
NeilBrownfd01b882011-10-11 16:47:53 +1100376 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100377 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100378
NeilBrowna7a07e62010-12-09 16:04:25 +1100379 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
380 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100381 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100382 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100383 if (rdev->raid_disk >= 0 &&
384 !test_bit(Faulty, &rdev->flags)) {
385 /* Take two references, one is dropped
386 * when request finishes, one after
387 * we reclaim rcu_read_lock
388 */
389 struct bio *bi;
390 atomic_inc(&rdev->nr_pending);
391 atomic_inc(&rdev->nr_pending);
392 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000393 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100395 bi->bi_private = rdev;
396 bi->bi_bdev = rdev->bdev;
397 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200398 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100399 rcu_read_lock();
400 rdev_dec_pending(rdev, mddev);
401 }
402 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100403 if (atomic_dec_and_test(&mddev->flush_pending))
404 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100405}
406
Tejun Heoe9c74692010-09-03 11:56:18 +0200407static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100408{
NeilBrownfd01b882011-10-11 16:47:53 +1100409 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200410 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100411
Kent Overstreet4f024f32013-10-11 15:44:27 -0700412 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100413 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200414 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100415 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200416 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200417 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100418 }
NeilBrown2b74e122010-12-09 15:59:01 +1100419
420 mddev->flush_bio = NULL;
421 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100422}
423
NeilBrownfd01b882011-10-11 16:47:53 +1100424void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100425{
NeilBrown85572d72014-12-15 12:56:56 +1100426 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100427 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200428 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100429 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200430 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100431 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100432
NeilBrowna035fc32010-12-09 16:17:51 +1100433 INIT_WORK(&mddev->flush_work, submit_flushes);
434 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100435}
Tejun Heoe9c74692010-09-03 11:56:18 +0200436EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100437
NeilBrown74018dc2012-07-31 09:08:15 +0200438void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000439{
NeilBrown9cbb1752012-07-31 09:08:14 +0200440 struct mddev *mddev = cb->data;
441 md_wakeup_thread(mddev->thread);
442 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000443}
NeilBrown9cbb1752012-07-31 09:08:14 +0200444EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000445
NeilBrownfd01b882011-10-11 16:47:53 +1100446static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 atomic_inc(&mddev->active);
449 return mddev;
450}
451
Dan Williams5fd3a172009-03-04 00:57:25 -0700452static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100453
NeilBrownfd01b882011-10-11 16:47:53 +1100454static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
NeilBrowna167f662010-10-26 18:31:13 +1100456 struct bio_set *bs = NULL;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
459 return;
NeilBrownd3374822009-01-09 08:31:10 +1100460 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100461 mddev->ctime == 0 && !mddev->hold_active) {
462 /* Array is not configured at all, and not held active,
463 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100464 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100465 bs = mddev->bio_set;
466 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100467 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200468 /* We did a probe so need to clean up. Call
469 * queue_work inside the spinlock so that
470 * flush_workqueue() after mddev_find will
471 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100472 */
473 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200474 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100475 } else
476 kfree(mddev);
477 }
478 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100479 if (bs)
480 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Sasha Levin25b2edf2015-07-24 18:19:58 -0400483static void md_safemode_timeout(unsigned long data);
484
NeilBrownfd01b882011-10-11 16:47:53 +1100485void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100486{
487 mutex_init(&mddev->open_mutex);
488 mutex_init(&mddev->reconfig_mutex);
489 mutex_init(&mddev->bitmap_info.mutex);
490 INIT_LIST_HEAD(&mddev->disks);
491 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400492 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
493 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100494 atomic_set(&mddev->active, 1);
495 atomic_set(&mddev->openers, 0);
496 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100497 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100498 atomic_set(&mddev->flush_pending, 0);
499 init_waitqueue_head(&mddev->sb_wait);
500 init_waitqueue_head(&mddev->recovery_wait);
501 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000502 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500503 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100504 mddev->resync_min = 0;
505 mddev->resync_max = MaxSector;
506 mddev->level = LEVEL_NONE;
507}
NeilBrown390ee602010-06-01 19:37:27 +1000508EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100509
NeilBrownf72ffdd2014-09-30 14:23:59 +1000510static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
NeilBrownfd01b882011-10-11 16:47:53 +1100512 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
NeilBrown8f5f02c2011-02-16 13:58:51 +1100514 if (unit && MAJOR(unit) != MD_MAJOR)
515 unit &= ~((1<<MdpMinorShift)-1);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 retry:
518 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
NeilBrownefeb53c2009-01-09 08:31:10 +1100520 if (unit) {
521 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
522 if (mddev->unit == unit) {
523 mddev_get(mddev);
524 spin_unlock(&all_mddevs_lock);
525 kfree(new);
526 return mddev;
527 }
528
529 if (new) {
530 list_add(&new->all_mddevs, &all_mddevs);
531 spin_unlock(&all_mddevs_lock);
532 new->hold_active = UNTIL_IOCTL;
533 return new;
534 }
535 } else if (new) {
536 /* find an unused unit number */
537 static int next_minor = 512;
538 int start = next_minor;
539 int is_free = 0;
540 int dev = 0;
541 while (!is_free) {
542 dev = MKDEV(MD_MAJOR, next_minor);
543 next_minor++;
544 if (next_minor > MINORMASK)
545 next_minor = 0;
546 if (next_minor == start) {
547 /* Oh dear, all in use. */
548 spin_unlock(&all_mddevs_lock);
549 kfree(new);
550 return NULL;
551 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000552
NeilBrownefeb53c2009-01-09 08:31:10 +1100553 is_free = 1;
554 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
555 if (mddev->unit == dev) {
556 is_free = 0;
557 break;
558 }
559 }
560 new->unit = dev;
561 new->md_minor = MINOR(dev);
562 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 list_add(&new->all_mddevs, &all_mddevs);
564 spin_unlock(&all_mddevs_lock);
565 return new;
566 }
567 spin_unlock(&all_mddevs_lock);
568
NeilBrown9ffae0c2006-01-06 00:20:32 -0800569 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (!new)
571 return NULL;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 new->unit = unit;
574 if (MAJOR(unit) == MD_MAJOR)
575 new->md_minor = MINOR(unit);
576 else
577 new->md_minor = MINOR(unit) >> MdpMinorShift;
578
NeilBrownfafd7fb2010-04-01 15:55:30 +1100579 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto retry;
582}
583
NeilBrownb6eb1272010-04-15 10:13:47 +1000584static struct attribute_group md_redundancy_group;
585
NeilBrown5c47daf2014-12-15 12:57:01 +1100586void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
NeilBrowna64c8762010-04-14 17:15:37 +1000588 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000589 /* These cannot be removed under reconfig_mutex as
590 * an access to the files will try to take reconfig_mutex
591 * while holding the file unremovable, which leads to
592 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000593 * So hold set sysfs_active while the remove in happeing,
594 * and anything else which might set ->to_remove or my
595 * otherwise change the sysfs namespace will fail with
596 * -EBUSY if sysfs_active is still set.
597 * We set sysfs_active under reconfig_mutex and elsewhere
598 * test it under the same mutex to ensure its correct value
599 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000600 */
NeilBrowna64c8762010-04-14 17:15:37 +1000601 struct attribute_group *to_remove = mddev->to_remove;
602 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000603 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000604 mutex_unlock(&mddev->reconfig_mutex);
605
NeilBrown00bcb4a2010-06-01 19:37:23 +1000606 if (mddev->kobj.sd) {
607 if (to_remove != &md_redundancy_group)
608 sysfs_remove_group(&mddev->kobj, to_remove);
609 if (mddev->pers == NULL ||
610 mddev->pers->sync_request == NULL) {
611 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
612 if (mddev->sysfs_action)
613 sysfs_put(mddev->sysfs_action);
614 mddev->sysfs_action = NULL;
615 }
NeilBrowna64c8762010-04-14 17:15:37 +1000616 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000617 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000618 } else
619 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Chris Dunlop751e67c2011-10-19 16:48:26 +1100621 /* As we've dropped the mutex we need a spinlock to
622 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000623 */
624 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700625 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000626 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
NeilBrown5c47daf2014-12-15 12:57:01 +1100628EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500630struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100631{
632 struct md_rdev *rdev;
633
634 rdev_for_each_rcu(rdev, mddev)
635 if (rdev->desc_nr == nr)
636 return rdev;
637
638 return NULL;
639}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500640EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100641
642static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
NeilBrown3cb03002011-10-11 16:45:26 +1100644 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
NeilBrowndafb20f2012-03-19 12:46:39 +1100646 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (rdev->bdev->bd_dev == dev)
648 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return NULL;
651}
652
NeilBrown1ca69c42012-10-11 13:37:33 +1100653static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
654{
655 struct md_rdev *rdev;
656
657 rdev_for_each_rcu(rdev, mddev)
658 if (rdev->bdev->bd_dev == dev)
659 return rdev;
660
661 return NULL;
662}
663
NeilBrown84fc4b52011-10-11 16:49:58 +1100664static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800665{
NeilBrown84fc4b52011-10-11 16:49:58 +1100666 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800667 list_for_each_entry(pers, &pers_list, list) {
668 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800669 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800670 if (strcmp(pers->name, clevel)==0)
671 return pers;
672 }
NeilBrown2604b702006-01-06 00:20:36 -0800673 return NULL;
674}
675
Andre Nollb73df2d2008-07-11 22:02:23 +1000676/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100677static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100679 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000680 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
NeilBrownf72ffdd2014-09-30 14:23:59 +1000683static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 rdev->sb_page = alloc_page(GFP_KERNEL);
686 if (!rdev->sb_page) {
687 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000688 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
691 return 0;
692}
693
NeilBrown545c8792012-05-22 13:54:30 +1000694void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800697 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 rdev->sb_loaded = 0;
699 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000700 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100701 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
NeilBrown2699b672011-07-28 11:31:47 +1000703 if (rdev->bb_page) {
704 put_page(rdev->bb_page);
705 rdev->bb_page = NULL;
706 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800707 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
NeilBrown545c8792012-05-22 13:54:30 +1000709EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200711static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700712{
NeilBrown3cb03002011-10-11 16:45:26 +1100713 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100714 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700715
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200716 if (bio->bi_error) {
717 printk("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800718 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700719 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700720
NeilBrowna9701a32005-11-08 21:39:34 -0800721 if (atomic_dec_and_test(&mddev->pending_writes))
722 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700723 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700724 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700725}
726
NeilBrownfd01b882011-10-11 16:47:53 +1100727void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700728 sector_t sector, int size, struct page *page)
729{
730 /* write first size bytes of page to sector of rdev
731 * Increment mddev->pending_writes before returning
732 * and decrement it on completion, waking up sb_wait
733 * if zero is reached.
734 * If an error occurred, call md_error
735 */
NeilBrowna167f662010-10-26 18:31:13 +1100736 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700737
Shaohua Lied3b98c2016-03-29 14:00:19 -0700738 atomic_inc(&rdev->nr_pending);
739
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100740 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700741 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700742 bio_add_page(bio, page, size, 0);
743 bio->bi_private = rdev;
744 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800745
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000747 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800748}
749
NeilBrownfd01b882011-10-11 16:47:53 +1100750void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800751{
Tejun Heoe9c74692010-09-03 11:56:18 +0200752 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000753 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700754}
755
NeilBrown3cb03002011-10-11 16:45:26 +1100756int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100757 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
NeilBrowna167f662010-10-26 18:31:13 +1100759 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int ret;
761
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100762 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
763 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100764 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700765 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000766 else if (rdev->mddev->reshape_position != MaxSector &&
767 (rdev->mddev->reshape_backwards ==
768 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700769 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100770 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700771 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700773 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200775 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 bio_put(bio);
777 return ret;
778}
NeilBrowna8745db2006-01-06 00:20:34 -0800779EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
NeilBrownf72ffdd2014-09-30 14:23:59 +1000781static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (rdev->sb_loaded)
786 return 0;
787
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100788 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 goto fail;
790 rdev->sb_loaded = 1;
791 return 0;
792
793fail:
794 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
795 bdevname(rdev->bdev,b));
796 return -EINVAL;
797}
798
799static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
800{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000801 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000802 sb1->set_uuid1 == sb2->set_uuid1 &&
803 sb1->set_uuid2 == sb2->set_uuid2 &&
804 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
808{
809 int ret;
810 mdp_super_t *tmp1, *tmp2;
811
812 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
813 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
814
815 if (!tmp1 || !tmp2) {
816 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100817 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto abort;
819 }
820
821 *tmp1 = *sb1;
822 *tmp2 = *sb2;
823
824 /*
825 * nr_disks is not constant
826 */
827 tmp1->nr_disks = 0;
828 tmp2->nr_disks = 0;
829
Andre Nollce0c8e02008-07-11 22:02:20 +1000830 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700832 kfree(tmp1);
833 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return ret;
835}
836
NeilBrown4d167f02007-05-09 02:35:37 -0700837static u32 md_csum_fold(u32 csum)
838{
839 csum = (csum & 0xffff) + (csum >> 16);
840 return (csum & 0xffff) + (csum >> 16);
841}
842
NeilBrownf72ffdd2014-09-30 14:23:59 +1000843static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
NeilBrown4d167f02007-05-09 02:35:37 -0700845 u64 newcsum = 0;
846 u32 *sb32 = (u32*)sb;
847 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unsigned int disk_csum, csum;
849
850 disk_csum = sb->sb_csum;
851 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700852
853 for (i = 0; i < MD_SB_BYTES/4 ; i++)
854 newcsum += sb32[i];
855 csum = (newcsum & 0xffffffff) + (newcsum>>32);
856
NeilBrown4d167f02007-05-09 02:35:37 -0700857#ifdef CONFIG_ALPHA
858 /* This used to use csum_partial, which was wrong for several
859 * reasons including that different results are returned on
860 * different architectures. It isn't critical that we get exactly
861 * the same return value as before (we always csum_fold before
862 * testing, and that removes any differences). However as we
863 * know that csum_partial always returned a 16bit value on
864 * alphas, do a fold to maximise conformity to previous behaviour.
865 */
866 sb->sb_csum = md_csum_fold(disk_csum);
867#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700869#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return csum;
871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
874 * Handle superblock details.
875 * We want to be able to handle multiple superblock formats
876 * so we have a common interface to them all, and an array of
877 * different handlers.
878 * We rely on user-space to write the initial superblock, and support
879 * reading and updating of superblocks.
880 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100881 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 * loads and validates a superblock on dev.
883 * if refdev != NULL, compare superblocks on both devices
884 * Return:
885 * 0 - dev has a superblock that is compatible with refdev
886 * 1 - dev has a superblock that is compatible and newer than refdev
887 * so dev should be used as the refdev in future
888 * -EINVAL superblock incompatible or invalid
889 * -othererror e.g. -EIO
890 *
NeilBrownfd01b882011-10-11 16:47:53 +1100891 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * Verify that dev is acceptable into mddev.
893 * The first time, mddev->raid_disks will be 0, and data from
894 * dev should be merged in. Subsequent calls check that dev
895 * is new enough. Return 0 or -EINVAL
896 *
NeilBrownfd01b882011-10-11 16:47:53 +1100897 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 * Update the superblock for rdev with data in mddev
899 * This does not write to disc.
900 *
901 */
902
903struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000904 char *name;
905 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000906 int (*load_super)(struct md_rdev *rdev,
907 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000908 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000909 int (*validate_super)(struct mddev *mddev,
910 struct md_rdev *rdev);
911 void (*sync_super)(struct mddev *mddev,
912 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100913 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000914 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000915 int (*allow_new_offset)(struct md_rdev *rdev,
916 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917};
918
919/*
Andre Noll0894cc32009-06-18 08:49:23 +1000920 * Check that the given mddev has no bitmap.
921 *
922 * This function is called from the run method of all personalities that do not
923 * support bitmaps. It prints an error message and returns non-zero if mddev
924 * has a bitmap. Otherwise, it returns 0.
925 *
926 */
NeilBrownfd01b882011-10-11 16:47:53 +1100927int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000928{
NeilBrownc3d97142009-12-14 12:49:52 +1100929 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000930 return 0;
931 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
932 mdname(mddev), mddev->pers->name);
933 return 1;
934}
935EXPORT_SYMBOL(md_check_no_bitmap);
936
937/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000938 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
NeilBrown3cb03002011-10-11 16:45:26 +1100940static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
943 mdp_super_t *sb;
944 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 /*
Andre Noll0f420352008-07-11 22:02:23 +1000947 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * it's at the end of the disk.
949 *
950 * It also happens to be a multiple of 4Kb.
951 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100952 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
NeilBrown0002b272005-09-09 16:23:53 -0700954 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (ret) return ret;
956
957 ret = -EINVAL;
958
959 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000960 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if (sb->md_magic != MD_SB_MAGIC) {
963 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
964 b);
965 goto abort;
966 }
967
968 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800969 sb->minor_version < 90 ||
970 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
972 sb->major_version, sb->minor_version,
973 b);
974 goto abort;
975 }
976
977 if (sb->raid_disks <= 0)
978 goto abort;
979
NeilBrown4d167f02007-05-09 02:35:37 -0700980 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
982 b);
983 goto abort;
984 }
985
986 rdev->preferred_minor = sb->md_minor;
987 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000988 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700989 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000990 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (sb->level == LEVEL_MULTIPATH)
993 rdev->desc_nr = -1;
994 else
995 rdev->desc_nr = sb->this_disk.number;
996
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700997 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700999 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001001 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (!uuid_equal(refsb, sb)) {
1003 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1004 b, bdevname(refdev->bdev,b2));
1005 goto abort;
1006 }
1007 if (!sb_equal(refsb, sb)) {
1008 printk(KERN_WARNING "md: %s has same UUID"
1009 " but different superblock to %s\n",
1010 b, bdevname(refdev->bdev, b2));
1011 goto abort;
1012 }
1013 ev1 = md_event(sb);
1014 ev2 = md_event(refsb);
1015 if (ev1 > ev2)
1016 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001017 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 ret = 0;
1019 }
NeilBrown8190e752009-06-18 08:48:58 +10001020 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001021 /* Limit to 4TB as metadata cannot record more than that.
1022 * (not needed for Linear and RAID0 as metadata doesn't
1023 * record this size)
1024 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001025 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1026 sb->level >= 1)
1027 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
NeilBrown27a7b262011-09-10 17:21:28 +10001029 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001030 /* "this cannot possibly happen" ... */
1031 ret = -EINVAL;
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 abort:
1034 return ret;
1035}
1036
1037/*
1038 * validate_super for 0.90.0
1039 */
NeilBrownfd01b882011-10-11 16:47:53 +11001040static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001043 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001044 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
NeilBrown41158c72005-06-21 17:17:25 -07001046 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001047 clear_bit(Faulty, &rdev->flags);
1048 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001049 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001050 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (mddev->raid_disks == 0) {
1053 mddev->major_version = 0;
1054 mddev->minor_version = sb->minor_version;
1055 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001056 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001057 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 mddev->ctime = sb->ctime;
1059 mddev->utime = sb->utime;
1060 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001061 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 mddev->layout = sb->layout;
1063 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001064 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001065 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001066 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001067 mddev->bitmap_info.space = 0;
1068 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001069 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001070 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001071 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
NeilBrownf6705572006-03-27 01:18:11 -08001073 if (mddev->minor_version >= 91) {
1074 mddev->reshape_position = sb->reshape_position;
1075 mddev->delta_disks = sb->delta_disks;
1076 mddev->new_level = sb->new_level;
1077 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001078 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001079 if (mddev->delta_disks < 0)
1080 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001081 } else {
1082 mddev->reshape_position = MaxSector;
1083 mddev->delta_disks = 0;
1084 mddev->new_level = mddev->level;
1085 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001086 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001087 }
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (sb->state & (1<<MD_SB_CLEAN))
1090 mddev->recovery_cp = MaxSector;
1091 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001092 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 sb->events_lo == sb->cp_events_lo) {
1094 mddev->recovery_cp = sb->recovery_cp;
1095 } else
1096 mddev->recovery_cp = 0;
1097 }
1098
1099 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1100 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1101 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1102 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1103
1104 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001105
1106 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001107 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001108 mddev->bitmap_info.offset =
1109 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001110 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001111 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001112 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001113
NeilBrown41158c72005-06-21 17:17:25 -07001114 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001115 /* Insist on good event counter while assembling, except
1116 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001118 if (sb->disks[rdev->desc_nr].state & (
1119 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001120 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001121 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001122 } else if (mddev->bitmap) {
1123 /* if adding to array with a bitmap, then we can accept an
1124 * older device ... but not too old.
1125 */
NeilBrown41158c72005-06-21 17:17:25 -07001126 if (ev1 < mddev->bitmap->events_cleared)
1127 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001128 if (ev1 < mddev->events)
1129 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001130 } else {
1131 if (ev1 < mddev->events)
1132 /* just a hot-add of a new device, leave raid_disk at -1 */
1133 return 0;
1134 }
NeilBrown41158c72005-06-21 17:17:25 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 desc = sb->disks + rdev->desc_nr;
1138
1139 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001140 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001141 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1142 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001143 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001145 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001146 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1147 /* active but not in sync implies recovery up to
1148 * reshape position. We don't know exactly where
1149 * that is, so set to zero for now */
1150 if (mddev->minor_version >= 91) {
1151 rdev->recovery_offset = 0;
1152 rdev->raid_disk = desc->raid_disk;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001155 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1156 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001157 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001158 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return 0;
1160}
1161
1162/*
1163 * sync_super for 0.90.0
1164 */
NeilBrownfd01b882011-10-11 16:47:53 +11001165static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001168 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /* make rdev->sb match mddev data..
1172 *
1173 * 1/ zero out disks
1174 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1175 * 3/ any empty disks < next_spare become removed
1176 *
1177 * disks[0] gets initialised to REMOVED because
1178 * we cannot be sure from other fields if it has
1179 * been initialised or not.
1180 */
1181 int i;
1182 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1183
NeilBrown61181562005-09-09 16:24:02 -07001184 rdev->sb_size = MD_SB_BYTES;
1185
Namhyung Kim65a06f062011-07-27 11:00:36 +10001186 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 memset(sb, 0, sizeof(*sb));
1189
1190 sb->md_magic = MD_SB_MAGIC;
1191 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 sb->patch_version = mddev->patch_version;
1193 sb->gvalid_words = 0; /* ignored */
1194 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1195 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1196 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1197 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1198
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001199 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001201 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 sb->raid_disks = mddev->raid_disks;
1203 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001204 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001205 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 sb->state = 0;
1207 sb->events_hi = (mddev->events>>32);
1208 sb->events_lo = (u32)mddev->events;
1209
NeilBrownf6705572006-03-27 01:18:11 -08001210 if (mddev->reshape_position == MaxSector)
1211 sb->minor_version = 90;
1212 else {
1213 sb->minor_version = 91;
1214 sb->reshape_position = mddev->reshape_position;
1215 sb->new_level = mddev->new_level;
1216 sb->delta_disks = mddev->delta_disks;
1217 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001218 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001219 }
1220 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (mddev->in_sync)
1222 {
1223 sb->recovery_cp = mddev->recovery_cp;
1224 sb->cp_events_hi = (mddev->events>>32);
1225 sb->cp_events_lo = (u32)mddev->events;
1226 if (mddev->recovery_cp == MaxSector)
1227 sb->state = (1<< MD_SB_CLEAN);
1228 } else
1229 sb->recovery_cp = 0;
1230
1231 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001232 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
NeilBrownc3d97142009-12-14 12:49:52 +11001234 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001235 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001238 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001240 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001241 int is_active = test_bit(In_sync, &rdev2->flags);
1242
1243 if (rdev2->raid_disk >= 0 &&
1244 sb->minor_version >= 91)
1245 /* we have nowhere to store the recovery_offset,
1246 * but if it is not below the reshape_position,
1247 * we can piggy-back on that.
1248 */
1249 is_active = 1;
1250 if (rdev2->raid_disk < 0 ||
1251 test_bit(Faulty, &rdev2->flags))
1252 is_active = 0;
1253 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001254 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001256 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001257 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 d = &sb->disks[rdev2->desc_nr];
1259 nr_disks++;
1260 d->number = rdev2->desc_nr;
1261 d->major = MAJOR(rdev2->bdev->bd_dev);
1262 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001263 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 d->raid_disk = rdev2->raid_disk;
1265 else
1266 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001267 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001269 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001271 if (test_bit(In_sync, &rdev2->flags))
1272 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 active++;
1274 working++;
1275 } else {
1276 d->state = 0;
1277 spare++;
1278 working++;
1279 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001280 if (test_bit(WriteMostly, &rdev2->flags))
1281 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* now set the "removed" and "faulty" bits on any missing devices */
1284 for (i=0 ; i < mddev->raid_disks ; i++) {
1285 mdp_disk_t *d = &sb->disks[i];
1286 if (d->state == 0 && d->number == 0) {
1287 d->number = i;
1288 d->raid_disk = i;
1289 d->state = (1<<MD_DISK_REMOVED);
1290 d->state |= (1<<MD_DISK_FAULTY);
1291 failed++;
1292 }
1293 }
1294 sb->nr_disks = nr_disks;
1295 sb->active_disks = active;
1296 sb->working_disks = working;
1297 sb->failed_disks = failed;
1298 sb->spare_disks = spare;
1299
1300 sb->this_disk = sb->disks[rdev->desc_nr];
1301 sb->sb_csum = calc_sb_csum(sb);
1302}
1303
1304/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001305 * rdev_size_change for 0.90.0
1306 */
1307static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001308super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001309{
Andre Noll58c0fed2009-03-31 14:33:13 +11001310 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001311 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001312 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001313 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001314 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001315 if (!num_sectors || num_sectors > rdev->sb_start)
1316 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001317 /* Limit to 4TB as metadata cannot record more than that.
1318 * 4TB == 2^32 KB, or 2*2^32 sectors.
1319 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001320 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1321 rdev->mddev->level >= 1)
1322 num_sectors = (sector_t)(2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001323 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001324 rdev->sb_page);
1325 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001326 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001327}
1328
NeilBrownc6563a82012-05-21 09:27:00 +10001329static int
1330super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1331{
1332 /* non-zero offset changes not possible with v0.90 */
1333 return new_offset == 0;
1334}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001335
1336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * version 1 superblock
1338 */
1339
NeilBrownf72ffdd2014-09-30 14:23:59 +10001340static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001342 __le32 disk_csum;
1343 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 unsigned long long newcsum;
1345 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001346 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 disk_csum = sb->sb_csum;
1349 sb->sb_csum = 0;
1350 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001351 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 newcsum += le32_to_cpu(*isuper++);
1353
1354 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001355 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1358 sb->sb_csum = disk_csum;
1359 return cpu_to_le32(csum);
1360}
1361
NeilBrown3cb03002011-10-11 16:45:26 +11001362static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct mdp_superblock_1 *sb;
1365 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001366 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001367 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001369 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /*
Andre Noll0f420352008-07-11 22:02:23 +10001372 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 * It is always aligned to a 4K boundary and
1374 * depeding on minor_version, it can be:
1375 * 0: At least 8K, but less than 12K, from end of device
1376 * 1: At start of device
1377 * 2: 4K from start of device.
1378 */
1379 switch(minor_version) {
1380 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001381 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001382 sb_start -= 8*2;
1383 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 break;
1385 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001386 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 break;
1388 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001389 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
1391 default:
1392 return -EINVAL;
1393 }
Andre Noll0f420352008-07-11 22:02:23 +10001394 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
NeilBrown0002b272005-09-09 16:23:53 -07001396 /* superblock is rarely larger than 1K, but it can be larger,
1397 * and it is safe to read 4k, so we do that
1398 */
1399 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (ret) return ret;
1401
Namhyung Kim65a06f062011-07-27 11:00:36 +10001402 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1405 sb->major_version != cpu_to_le32(1) ||
1406 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001407 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001408 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -EINVAL;
1410
1411 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1412 printk("md: invalid superblock checksum on %s\n",
1413 bdevname(rdev->bdev,b));
1414 return -EINVAL;
1415 }
1416 if (le64_to_cpu(sb->data_size) < 10) {
1417 printk("md: data_size too small on %s\n",
1418 bdevname(rdev->bdev,b));
1419 return -EINVAL;
1420 }
NeilBrownc6563a82012-05-21 09:27:00 +10001421 if (sb->pad0 ||
1422 sb->pad3[0] ||
1423 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1424 /* Some padding is non-zero, might be a new feature */
1425 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 rdev->preferred_minor = 0xffff;
1428 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001429 rdev->new_data_offset = rdev->data_offset;
1430 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1431 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1432 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001433 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
NeilBrown0002b272005-09-09 16:23:53 -07001435 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001436 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001437 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001438 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1439
1440 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001441 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001442 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001443 if (minor_version
1444 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1445 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001446
NeilBrown31b65a02006-07-10 04:44:14 -07001447 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1448 rdev->desc_nr = -1;
1449 else
1450 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1451
NeilBrown2699b672011-07-28 11:31:47 +10001452 if (!rdev->bb_page) {
1453 rdev->bb_page = alloc_page(GFP_KERNEL);
1454 if (!rdev->bb_page)
1455 return -ENOMEM;
1456 }
1457 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1458 rdev->badblocks.count == 0) {
1459 /* need to load the bad block list.
1460 * Currently we limit it to one page.
1461 */
1462 s32 offset;
1463 sector_t bb_sector;
1464 u64 *bbp;
1465 int i;
1466 int sectors = le16_to_cpu(sb->bblog_size);
1467 if (sectors > (PAGE_SIZE / 512))
1468 return -EINVAL;
1469 offset = le32_to_cpu(sb->bblog_offset);
1470 if (offset == 0)
1471 return -EINVAL;
1472 bb_sector = (long long)offset;
1473 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1474 rdev->bb_page, READ, true))
1475 return -EIO;
1476 bbp = (u64 *)page_address(rdev->bb_page);
1477 rdev->badblocks.shift = sb->bblog_shift;
1478 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1479 u64 bb = le64_to_cpu(*bbp);
1480 int count = bb & (0x3ff);
1481 u64 sector = bb >> 10;
1482 sector <<= sb->bblog_shift;
1483 count <<= sb->bblog_shift;
1484 if (bb + 1 == 0)
1485 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001486 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001487 return -EINVAL;
1488 }
NeilBrown486adf72013-04-24 11:42:44 +10001489 } else if (sb->bblog_offset != 0)
1490 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001491
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001492 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001493 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001494 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001496 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1499 sb->level != refsb->level ||
1500 sb->layout != refsb->layout ||
1501 sb->chunksize != refsb->chunksize) {
1502 printk(KERN_WARNING "md: %s has strangely different"
1503 " superblock to %s\n",
1504 bdevname(rdev->bdev,b),
1505 bdevname(refdev->bdev,b2));
1506 return -EINVAL;
1507 }
1508 ev1 = le64_to_cpu(sb->events);
1509 ev2 = le64_to_cpu(refsb->events);
1510
1511 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001512 ret = 1;
1513 else
1514 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
NeilBrownc6563a82012-05-21 09:27:00 +10001516 if (minor_version) {
1517 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1518 sectors -= rdev->data_offset;
1519 } else
1520 sectors = rdev->sb_start;
1521 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001523 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001524 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
NeilBrownfd01b882011-10-11 16:47:53 +11001527static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001529 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001530 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
NeilBrown41158c72005-06-21 17:17:25 -07001532 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001533 clear_bit(Faulty, &rdev->flags);
1534 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001535 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001536 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (mddev->raid_disks == 0) {
1539 mddev->major_version = 1;
1540 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001541 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001542 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001543 mddev->ctime = le64_to_cpu(sb->ctime);
1544 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001546 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 mddev->layout = le32_to_cpu(sb->layout);
1548 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001549 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001550 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001551 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001552 mddev->bitmap_info.space = 0;
1553 /* Default location for bitmap is 1K after superblock
1554 * using 3K - total of 4K
1555 */
NeilBrownc3d97142009-12-14 12:49:52 +11001556 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001557 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001558 mddev->reshape_backwards = 0;
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1561 memcpy(mddev->uuid, sb->set_uuid, 16);
1562
1563 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001564
NeilBrown71c08052005-09-09 16:23:51 -07001565 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001566 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001567 mddev->bitmap_info.offset =
1568 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001569 /* Metadata doesn't record how much space is available.
1570 * For 1.0, we assume we can use up to the superblock
1571 * if before, else to 4K beyond superblock.
1572 * For others, assume no change is possible.
1573 */
1574 if (mddev->minor_version > 0)
1575 mddev->bitmap_info.space = 0;
1576 else if (mddev->bitmap_info.offset > 0)
1577 mddev->bitmap_info.space =
1578 8 - mddev->bitmap_info.offset;
1579 else
1580 mddev->bitmap_info.space =
1581 -mddev->bitmap_info.offset;
1582 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001583
NeilBrownf6705572006-03-27 01:18:11 -08001584 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1585 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1586 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1587 mddev->new_level = le32_to_cpu(sb->new_level);
1588 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001589 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001590 if (mddev->delta_disks < 0 ||
1591 (mddev->delta_disks == 0 &&
1592 (le32_to_cpu(sb->feature_map)
1593 & MD_FEATURE_RESHAPE_BACKWARDS)))
1594 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001595 } else {
1596 mddev->reshape_position = MaxSector;
1597 mddev->delta_disks = 0;
1598 mddev->new_level = mddev->level;
1599 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001600 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001601 }
1602
Shaohua Lia62ab492016-01-06 14:37:13 -08001603 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) {
1604 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1605 if (mddev->recovery_cp == MaxSector)
1606 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
1607 }
NeilBrown41158c72005-06-21 17:17:25 -07001608 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001609 /* Insist of good event counter while assembling, except for
1610 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001612 if (rdev->desc_nr >= 0 &&
1613 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001614 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1615 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001616 if (ev1 < mddev->events)
1617 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001618 } else if (mddev->bitmap) {
1619 /* If adding to array with a bitmap, then we can accept an
1620 * older device, but not too old.
1621 */
NeilBrown41158c72005-06-21 17:17:25 -07001622 if (ev1 < mddev->bitmap->events_cleared)
1623 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001624 if (ev1 < mddev->events)
1625 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001626 } else {
1627 if (ev1 < mddev->events)
1628 /* just a hot-add of a new device, leave raid_disk at -1 */
1629 return 0;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (mddev->level != LEVEL_MULTIPATH) {
1632 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001633 if (rdev->desc_nr < 0 ||
1634 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001635 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001636 rdev->desc_nr = -1;
1637 } else
1638 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001640 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001642 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001643 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 break;
Song Liubac624f2015-08-13 14:31:55 -07001645 case MD_DISK_ROLE_JOURNAL: /* journal device */
1646 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1647 /* journal device without journal feature */
1648 printk(KERN_WARNING
1649 "md: journal device provided without journal feature, ignoring the device\n");
1650 return -EINVAL;
1651 }
1652 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001653 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001654 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 default:
NeilBrownf4667222013-12-09 12:04:56 +11001657 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001658 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001659 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001660 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001661 if (!(le32_to_cpu(sb->feature_map) &
1662 MD_FEATURE_RECOVERY_BITMAP))
1663 rdev->saved_raid_disk = -1;
1664 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001665 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 rdev->raid_disk = role;
1667 break;
1668 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001669 if (sb->devflags & WriteMostly1)
1670 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001671 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1672 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001673 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001674 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return 0;
1677}
1678
NeilBrownfd01b882011-10-11 16:47:53 +11001679static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
1681 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001682 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int max_dev, i;
1684 /* make rdev->sb match mddev and rdev data. */
1685
Namhyung Kim65a06f062011-07-27 11:00:36 +10001686 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 sb->feature_map = 0;
1689 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001690 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 memset(sb->pad3, 0, sizeof(sb->pad3));
1692
1693 sb->utime = cpu_to_le64((__u64)mddev->utime);
1694 sb->events = cpu_to_le64(mddev->events);
1695 if (mddev->in_sync)
1696 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001697 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1698 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 else
1700 sb->resync_offset = cpu_to_le64(0);
1701
NeilBrown1c05b4b2006-10-21 10:24:08 -07001702 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001703
NeilBrownf0ca3402006-02-02 14:28:04 -08001704 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001705 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001706 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001707 sb->level = cpu_to_le32(mddev->level);
1708 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001709
NeilBrownaeb9b2112011-08-25 14:43:08 +10001710 if (test_bit(WriteMostly, &rdev->flags))
1711 sb->devflags |= WriteMostly1;
1712 else
1713 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001714 sb->data_offset = cpu_to_le64(rdev->data_offset);
1715 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001716
NeilBrownc3d97142009-12-14 12:49:52 +11001717 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1718 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001719 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001720 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001721
Shaohua Lif2076e72015-10-08 21:54:12 -07001722 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001723 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001724 sb->feature_map |=
1725 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1726 sb->recovery_offset =
1727 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001728 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1729 sb->feature_map |=
1730 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001731 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001732 /* Note: recovery_offset and journal_tail share space */
1733 if (test_bit(Journal, &rdev->flags))
1734 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001735 if (test_bit(Replacement, &rdev->flags))
1736 sb->feature_map |=
1737 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001738
NeilBrownf6705572006-03-27 01:18:11 -08001739 if (mddev->reshape_position != MaxSector) {
1740 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1741 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1742 sb->new_layout = cpu_to_le32(mddev->new_layout);
1743 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1744 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001745 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001746 if (mddev->delta_disks == 0 &&
1747 mddev->reshape_backwards)
1748 sb->feature_map
1749 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001750 if (rdev->new_data_offset != rdev->data_offset) {
1751 sb->feature_map
1752 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1753 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1754 - rdev->data_offset));
1755 }
NeilBrownf6705572006-03-27 01:18:11 -08001756 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001757
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001758 if (mddev_is_clustered(mddev))
1759 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1760
NeilBrown2699b672011-07-28 11:31:47 +10001761 if (rdev->badblocks.count == 0)
1762 /* Nothing to do for bad blocks*/ ;
1763 else if (sb->bblog_offset == 0)
1764 /* Cannot record bad blocks on this device */
1765 md_error(mddev, rdev);
1766 else {
1767 struct badblocks *bb = &rdev->badblocks;
1768 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1769 u64 *p = bb->page;
1770 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1771 if (bb->changed) {
1772 unsigned seq;
1773
1774retry:
1775 seq = read_seqbegin(&bb->lock);
1776
1777 memset(bbp, 0xff, PAGE_SIZE);
1778
1779 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001780 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001781 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1782 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001783 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001784 }
NeilBrownd0962932012-03-19 12:46:41 +11001785 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001786 if (read_seqretry(&bb->lock, seq))
1787 goto retry;
1788
1789 bb->sector = (rdev->sb_start +
1790 (int)le32_to_cpu(sb->bblog_offset));
1791 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001792 }
1793 }
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001796 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (rdev2->desc_nr+1 > max_dev)
1798 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001799
NeilBrown70471da2009-08-03 10:59:57 +10001800 if (max_dev > le32_to_cpu(sb->max_dev)) {
1801 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001802 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001803 rdev->sb_size = max_dev * 2 + 256;
1804 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1805 if (rdev->sb_size & bmask)
1806 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001807 } else
1808 max_dev = le32_to_cpu(sb->max_dev);
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001811 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001812
Song Liua97b7892015-10-08 21:54:09 -07001813 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1814 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
NeilBrowndafb20f2012-03-19 12:46:39 +11001816 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001818 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001819 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001820 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001822 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001823 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001824 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001825 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 else
Song Liuc4d4c912015-08-13 14:31:54 -07001827 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 sb->sb_csum = calc_sb_1_csum(sb);
1831}
1832
Chris Webb0cd17fe2008-06-28 08:31:46 +10001833static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001834super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001835{
1836 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001837 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001838 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001839 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001840 if (rdev->data_offset != rdev->new_data_offset)
1841 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001842 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001843 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001844 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001845 max_sectors -= rdev->data_offset;
1846 if (!num_sectors || num_sectors > max_sectors)
1847 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001848 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001849 /* minor version 0 with bitmap we can't move */
1850 return 0;
1851 } else {
1852 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001853 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001854 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001855 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001856 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001857 if (!num_sectors || num_sectors > max_sectors)
1858 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001859 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001860 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001861 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001862 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001863 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001864 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001865 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001866 rdev->sb_page);
1867 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001868 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001869
1870}
1871
1872static int
1873super_1_allow_new_offset(struct md_rdev *rdev,
1874 unsigned long long new_offset)
1875{
1876 /* All necessary checks on new >= old have been done */
1877 struct bitmap *bitmap;
1878 if (new_offset >= rdev->data_offset)
1879 return 1;
1880
1881 /* with 1.0 metadata, there is no metadata to tread on
1882 * so we can always move back */
1883 if (rdev->mddev->minor_version == 0)
1884 return 1;
1885
1886 /* otherwise we must be sure not to step on
1887 * any metadata, so stay:
1888 * 36K beyond start of superblock
1889 * beyond end of badblocks
1890 * beyond write-intent bitmap
1891 */
1892 if (rdev->sb_start + (32+4)*2 > new_offset)
1893 return 0;
1894 bitmap = rdev->mddev->bitmap;
1895 if (bitmap && !rdev->mddev->bitmap_info.file &&
1896 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001897 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001898 return 0;
1899 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1900 return 0;
1901
1902 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001903}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Adrian Bunk75c96f82005-05-05 16:16:09 -07001905static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 [0] = {
1907 .name = "0.90.0",
1908 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001909 .load_super = super_90_load,
1910 .validate_super = super_90_validate,
1911 .sync_super = super_90_sync,
1912 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001913 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 },
1915 [1] = {
1916 .name = "md-1",
1917 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001918 .load_super = super_1_load,
1919 .validate_super = super_1_validate,
1920 .sync_super = super_1_sync,
1921 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001922 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 },
1924};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
NeilBrownfd01b882011-10-11 16:47:53 +11001926static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001927{
1928 if (mddev->sync_super) {
1929 mddev->sync_super(mddev, rdev);
1930 return;
1931 }
1932
1933 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1934
1935 super_types[mddev->major_version].sync_super(mddev, rdev);
1936}
1937
NeilBrownfd01b882011-10-11 16:47:53 +11001938static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
NeilBrown3cb03002011-10-11 16:45:26 +11001940 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
NeilBrown4b809912008-07-21 17:05:25 +10001942 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001943 rdev_for_each_rcu(rdev, mddev1) {
1944 if (test_bit(Faulty, &rdev->flags) ||
1945 test_bit(Journal, &rdev->flags) ||
1946 rdev->raid_disk == -1)
1947 continue;
1948 rdev_for_each_rcu(rdev2, mddev2) {
1949 if (test_bit(Faulty, &rdev2->flags) ||
1950 test_bit(Journal, &rdev2->flags) ||
1951 rdev2->raid_disk == -1)
1952 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001953 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001954 rdev2->bdev->bd_contains) {
1955 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001956 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001957 }
Song Liu0b020e82015-09-03 23:00:35 -07001958 }
1959 }
NeilBrown4b809912008-07-21 17:05:25 +10001960 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return 0;
1962}
1963
1964static LIST_HEAD(pending_raid_disks);
1965
Andre Nollac5e7112009-08-03 10:59:47 +10001966/*
1967 * Try to register data integrity profile for an mddev
1968 *
1969 * This is called when an array is started and after a disk has been kicked
1970 * from the array. It only succeeds if all working and active component devices
1971 * are integrity capable with matching profiles.
1972 */
NeilBrownfd01b882011-10-11 16:47:53 +11001973int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001974{
NeilBrown3cb03002011-10-11 16:45:26 +11001975 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001976
Andre Nollac5e7112009-08-03 10:59:47 +10001977 if (list_empty(&mddev->disks))
1978 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001979 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1980 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001981 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001982 /* skip spares and non-functional disks */
1983 if (test_bit(Faulty, &rdev->flags))
1984 continue;
1985 if (rdev->raid_disk < 0)
1986 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001987 if (!reference) {
1988 /* Use the first rdev as the reference */
1989 reference = rdev;
1990 continue;
1991 }
1992 /* does this rdev's profile match the reference profile? */
1993 if (blk_integrity_compare(reference->bdev->bd_disk,
1994 rdev->bdev->bd_disk) < 0)
1995 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001996 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001997 if (!reference || !bdev_get_integrity(reference->bdev))
1998 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001999 /*
2000 * All component devices are integrity capable and have matching
2001 * profiles, register the common profile for the md device.
2002 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002003 blk_integrity_register(mddev->gendisk,
2004 bdev_get_integrity(reference->bdev));
2005
Martin K. Petersena91a2782011-03-17 11:11:05 +01002006 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2007 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2008 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2009 mdname(mddev));
2010 return -EINVAL;
2011 }
Andre Nollac5e7112009-08-03 10:59:47 +10002012 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002013}
Andre Nollac5e7112009-08-03 10:59:47 +10002014EXPORT_SYMBOL(md_integrity_register);
2015
Dan Williams1501efa2016-01-13 16:00:07 -08002016/*
2017 * Attempt to add an rdev, but only if it is consistent with the current
2018 * integrity profile
2019 */
2020int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002021{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002022 struct blk_integrity *bi_rdev;
2023 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002024 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002025
2026 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002027 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002028
2029 bi_rdev = bdev_get_integrity(rdev->bdev);
2030 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002031
2032 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002033 return 0;
2034
2035 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2036 printk(KERN_NOTICE "%s: incompatible integrity profile for %s\n",
2037 mdname(mddev), bdevname(rdev->bdev, name));
2038 return -ENXIO;
2039 }
2040
2041 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002042}
2043EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002044
NeilBrownf72ffdd2014-09-30 14:23:59 +10002045static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002047 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002048 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002049 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Dan Williams11e2ede2008-04-30 00:52:32 -07002051 /* prevent duplicates */
2052 if (find_rdev(mddev, rdev->bdev->bd_dev))
2053 return -EEXIST;
2054
Andre Nolldd8ac332009-03-31 14:33:13 +11002055 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002056 if (!test_bit(Journal, &rdev->flags) &&
2057 rdev->sectors &&
2058 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002059 if (mddev->pers) {
2060 /* Cannot change size, so fail
2061 * If mddev->level <= 0, then we don't care
2062 * about aligning sizes (e.g. linear)
2063 */
2064 if (mddev->level > 0)
2065 return -ENOSPC;
2066 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002067 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 /* Verify rdev->desc_nr is unique.
2071 * If it is -1, assign a free number, else
2072 * check number is not in use
2073 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002074 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (rdev->desc_nr < 0) {
2076 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002077 if (mddev->pers)
2078 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002079 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 choice++;
2081 rdev->desc_nr = choice;
2082 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002083 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002084 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002088 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002089 if (!test_bit(Journal, &rdev->flags) &&
2090 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002091 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2092 mdname(mddev), mddev->max_disks);
2093 return -EBUSY;
2094 }
NeilBrown19133a42005-11-08 21:39:35 -08002095 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002096 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002099 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002100
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002101 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002102 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002103
Tejun Heo0762b8b2008-08-25 19:56:12 +09002104 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002105 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2106 /* failure here is OK */;
2107 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002108
NeilBrown4b809912008-07-21 17:05:25 +10002109 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002110 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002111
2112 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002113 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002116
2117 fail:
2118 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2119 b, mdname(mddev));
2120 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
NeilBrown177a99b2008-02-06 01:39:56 -08002123static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002124{
NeilBrown3cb03002011-10-11 16:45:26 +11002125 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002126 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002127 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002128}
2129
NeilBrownf72ffdd2014-09-30 14:23:59 +10002130static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002133
Tejun Heo49731ba2011-01-14 18:43:57 +01002134 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002135 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2137 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002138 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002139 sysfs_put(rdev->sysfs_state);
2140 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002141 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002142 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002143 * writing to 'remove' to "dev/state". We also need
2144 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002145 */
NeilBrown4b809912008-07-21 17:05:25 +10002146 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002147 INIT_WORK(&rdev->del_work, md_delayed_delete);
2148 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002149 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
2152/*
2153 * prevent the device from being mounted, repartitioned or
2154 * otherwise reused by a RAID array (or any other kernel
2155 * subsystem), by bd_claiming the device.
2156 */
NeilBrown3cb03002011-10-11 16:45:26 +11002157static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 int err = 0;
2160 struct block_device *bdev;
2161 char b[BDEVNAME_SIZE];
2162
Tejun Heod4d77622010-11-13 11:55:18 +01002163 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002164 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (IS_ERR(bdev)) {
2166 printk(KERN_ERR "md: could not open %s.\n",
2167 __bdevname(dev, b));
2168 return PTR_ERR(bdev);
2169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 rdev->bdev = bdev;
2171 return err;
2172}
2173
NeilBrown3cb03002011-10-11 16:45:26 +11002174static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 struct block_device *bdev = rdev->bdev;
2177 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002178 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181void md_autodetect_dev(dev_t dev);
2182
NeilBrownf72ffdd2014-09-30 14:23:59 +10002183static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 printk(KERN_INFO "md: export_rdev(%s)\n",
2188 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002189 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002191 if (test_bit(AutoDetected, &rdev->flags))
2192 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193#endif
2194 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002195 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002198void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 unbind_rdev_from_array(rdev);
2201 export_rdev(rdev);
2202}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002203EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
NeilBrownfd01b882011-10-11 16:47:53 +11002205static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
NeilBrown0638bb02014-09-25 17:43:47 +10002207 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
NeilBrown0638bb02014-09-25 17:43:47 +10002209 while (!list_empty(&mddev->disks)) {
2210 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2211 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002212 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 mddev->raid_disks = 0;
2215 mddev->major_version = 0;
2216}
2217
NeilBrownf72ffdd2014-09-30 14:23:59 +10002218static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
NeilBrown42543762006-06-26 00:27:57 -07002220 /* Update each superblock (in-memory image), but
2221 * if we are allowed to, skip spares which already
2222 * have the right event counter, or have one earlier
2223 * (which would mean they aren't being marked as dirty
2224 * with the rest of the array)
2225 */
NeilBrown3cb03002011-10-11 16:45:26 +11002226 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002227 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002228 if (rdev->sb_events == mddev->events ||
2229 (nospares &&
2230 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002231 rdev->sb_events+1 == mddev->events)) {
2232 /* Don't update this superblock */
2233 rdev->sb_loaded = 2;
2234 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002235 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002236 rdev->sb_loaded = 1;
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
2239}
2240
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002241static bool does_sb_need_changing(struct mddev *mddev)
2242{
2243 struct md_rdev *rdev;
2244 struct mdp_superblock_1 *sb;
2245 int role;
2246
2247 /* Find a good rdev */
2248 rdev_for_each(rdev, mddev)
2249 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2250 break;
2251
2252 /* No good device found. */
2253 if (!rdev)
2254 return false;
2255
2256 sb = page_address(rdev->sb_page);
2257 /* Check if a device has become faulty or a spare become active */
2258 rdev_for_each(rdev, mddev) {
2259 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2260 /* Device activated? */
2261 if (role == 0xffff && rdev->raid_disk >=0 &&
2262 !test_bit(Faulty, &rdev->flags))
2263 return true;
2264 /* Device turned faulty? */
2265 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2266 return true;
2267 }
2268
2269 /* Check if any mddev parameters have changed */
2270 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2271 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002272 (mddev->layout != le64_to_cpu(sb->layout)) ||
2273 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2274 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2275 return true;
2276
2277 return false;
2278}
2279
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002280void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
NeilBrown3cb03002011-10-11 16:45:26 +11002282 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002283 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002284 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002285 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002286 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
NeilBrownd87f0642013-04-24 11:42:40 +10002288 if (mddev->ro) {
2289 if (force_change)
2290 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2291 return;
2292 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002293
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002294repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002295 if (mddev_is_clustered(mddev)) {
2296 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2297 force_change = 1;
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002298 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2299 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002300 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002301 /* Has someone else has updated the sb */
2302 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002303 if (ret == 0)
2304 md_cluster_ops->metadata_update_cancel(mddev);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002305 bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING),
2306 BIT(MD_CHANGE_DEVS) |
2307 BIT(MD_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002308 return;
2309 }
2310 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002311
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002312 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002313 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002314 if (rdev->raid_disk >= 0 &&
2315 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002316 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002317 !test_bit(In_sync, &rdev->flags) &&
2318 mddev->curr_resync_completed > rdev->recovery_offset)
2319 rdev->recovery_offset = mddev->curr_resync_completed;
2320
NeilBrownf72ffdd2014-09-30 14:23:59 +10002321 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002322 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002323 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002324 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002325 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002326 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002327 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002328 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002329 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002330 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002331 md_error(mddev, rdev);
2332 }
2333 clear_bit(Blocked, &rdev->flags);
2334 clear_bit(BlockedBadBlocks, &rdev->flags);
2335 wake_up(&rdev->blocked_wait);
2336 }
2337 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002338 wake_up(&mddev->sb_wait);
2339 return;
2340 }
2341
NeilBrown85572d72014-12-15 12:56:56 +11002342 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002343
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002344 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002345
NeilBrown850b2b422006-10-03 01:15:46 -07002346 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2347 force_change = 1;
2348 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2349 /* just a clean<-> dirty transition, possibly leave spares alone,
2350 * though if events isn't the right even/odd, we will have to do
2351 * spares after all
2352 */
2353 nospares = 1;
2354 if (force_change)
2355 nospares = 0;
2356 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002357 /* If the array is degraded, then skipping spares is both
2358 * dangerous and fairly pointless.
2359 * Dangerous because a device that was removed from the array
2360 * might have a event_count that still looks up-to-date,
2361 * so it can be re-added without a resync.
2362 * Pointless because if there are any spares to skip,
2363 * then a recovery will happen and soon that array won't
2364 * be degraded any more and the spare can go back to sleep then.
2365 */
NeilBrown850b2b422006-10-03 01:15:46 -07002366 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002367
NeilBrown06d91a52005-06-21 17:17:12 -07002368 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002369
2370 /* If this is just a dirty<->clean transition, and the array is clean
2371 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002372 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002373 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002374 && mddev->can_decrease_events
2375 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002376 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002377 mddev->can_decrease_events = 0;
2378 } else {
NeilBrown42543762006-06-26 00:27:57 -07002379 /* otherwise we have to go forward and ... */
2380 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002381 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
NeilBrown403df472014-09-30 15:52:29 +10002384 /*
2385 * This 64-bit counter should never wrap.
2386 * Either we are in around ~1 trillion A.C., assuming
2387 * 1 reboot per second, or we have a bug...
2388 */
2389 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002390
NeilBrowndafb20f2012-03-19 12:46:39 +11002391 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002392 if (rdev->badblocks.changed)
2393 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002394 if (test_bit(Faulty, &rdev->flags))
2395 set_bit(FaultRecorded, &rdev->flags);
2396 }
NeilBrown2699b672011-07-28 11:31:47 +10002397
NeilBrowne6910632008-02-06 01:39:51 -08002398 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002399 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
NeilBrown36a4e1f2011-10-07 14:23:17 +11002401 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2402 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
NeilBrown4ad13662007-07-17 04:06:13 -07002404 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002405 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002407
NeilBrown42543762006-06-26 00:27:57 -07002408 if (rdev->sb_loaded != 1)
2409 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
NeilBrownf4667222013-12-09 12:04:56 +11002411 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002412 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002413 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002414 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002415 pr_debug("md: (write) %s's sb offset: %llu\n",
2416 bdevname(rdev->bdev, b),
2417 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002418 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002419 if (rdev->badblocks.size) {
2420 md_super_write(mddev, rdev,
2421 rdev->badblocks.sector,
2422 rdev->badblocks.size << 9,
2423 rdev->bb_page);
2424 rdev->badblocks.size = 0;
2425 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002426
NeilBrownf4667222013-12-09 12:04:56 +11002427 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002428 pr_debug("md: %s (skipping faulty)\n",
2429 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002430
NeilBrown7bfa19f2005-06-21 17:17:28 -07002431 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 /* only need to write one superblock... */
2433 break;
2434 }
NeilBrowna9701a32005-11-08 21:39:34 -08002435 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002436 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002437
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002438 if (mddev_is_clustered(mddev) && ret == 0)
2439 md_cluster_ops->metadata_update_finish(mddev);
2440
NeilBrown850b2b422006-10-03 01:15:46 -07002441 if (mddev->in_sync != sync_req ||
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002442 !bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING),
2443 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002444 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002445 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002446 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002447 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2448 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002449
NeilBrowndafb20f2012-03-19 12:46:39 +11002450 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002451 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2452 clear_bit(Blocked, &rdev->flags);
2453
2454 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002455 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002456 clear_bit(BlockedBadBlocks, &rdev->flags);
2457 wake_up(&rdev->blocked_wait);
2458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002460EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002462static int add_bound_rdev(struct md_rdev *rdev)
2463{
2464 struct mddev *mddev = rdev->mddev;
2465 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002466 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002467
Shaohua Li87d4d912016-01-06 14:37:14 -08002468 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002469 /* If there is hot_add_disk but no hot_remove_disk
2470 * then added disks for geometry changes,
2471 * and should be added immediately.
2472 */
2473 super_types[mddev->major_version].
2474 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002475 if (add_journal)
2476 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002477 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002478 if (add_journal)
2479 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002480 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002481 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002482 return err;
2483 }
2484 }
2485 sysfs_notify_dirent_safe(rdev->sysfs_state);
2486
2487 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2488 if (mddev->degraded)
2489 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2490 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2491 md_new_event(mddev);
2492 md_wakeup_thread(mddev->thread);
2493 return 0;
2494}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Andre Noll7f6ce762008-03-23 18:34:54 +01002496/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002497 * We want to accept with case. For this we use cmd_match.
2498 */
2499static int cmd_match(const char *cmd, const char *str)
2500{
2501 /* See if cmd, written into a sysfs file, matches
2502 * str. They must either be the same, or cmd can
2503 * have a trailing newline
2504 */
2505 while (*cmd && *str && *cmd == *str) {
2506 cmd++;
2507 str++;
2508 }
2509 if (*cmd == '\n')
2510 cmd++;
2511 if (*str || *cmd)
2512 return 0;
2513 return 1;
2514}
2515
NeilBrown86e6ffd2005-11-08 21:39:24 -08002516struct rdev_sysfs_entry {
2517 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002518 ssize_t (*show)(struct md_rdev *, char *);
2519 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002520};
2521
2522static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002523state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002524{
2525 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002526 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002527 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002528
NeilBrown758bfc82014-12-15 12:56:59 +11002529 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002530 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002531 len+= sprintf(page+len, "%sfaulty",sep);
2532 sep = ",";
2533 }
NeilBrown758bfc82014-12-15 12:56:59 +11002534 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002535 len += sprintf(page+len, "%sin_sync",sep);
2536 sep = ",";
2537 }
Shaohua Liac6096e2015-10-04 09:20:11 -07002538 if (test_bit(Journal, &flags)) {
2539 len += sprintf(page+len, "%sjournal",sep);
2540 sep = ",";
2541 }
NeilBrown758bfc82014-12-15 12:56:59 +11002542 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002543 len += sprintf(page+len, "%swrite_mostly",sep);
2544 sep = ",";
2545 }
NeilBrown758bfc82014-12-15 12:56:59 +11002546 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002547 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002548 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002549 len += sprintf(page+len, "%sblocked", sep);
2550 sep = ",";
2551 }
NeilBrown758bfc82014-12-15 12:56:59 +11002552 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002553 !test_bit(Journal, &flags) &&
NeilBrown758bfc82014-12-15 12:56:59 +11002554 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002555 len += sprintf(page+len, "%sspare", sep);
2556 sep = ",";
2557 }
NeilBrown758bfc82014-12-15 12:56:59 +11002558 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002559 len += sprintf(page+len, "%swrite_error", sep);
2560 sep = ",";
2561 }
NeilBrown758bfc82014-12-15 12:56:59 +11002562 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002563 len += sprintf(page+len, "%swant_replacement", sep);
2564 sep = ",";
2565 }
NeilBrown758bfc82014-12-15 12:56:59 +11002566 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002567 len += sprintf(page+len, "%sreplacement", sep);
2568 sep = ",";
2569 }
2570
NeilBrown86e6ffd2005-11-08 21:39:24 -08002571 return len+sprintf(page+len, "\n");
2572}
2573
NeilBrown45dc2de2006-06-26 00:27:58 -07002574static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002575state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002576{
2577 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002578 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002579 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002580 * writemostly - sets write_mostly
2581 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002582 * blocked - sets the Blocked flags
2583 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002584 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002585 * -insync - clear Insync for a device with a slot assigned,
2586 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002587 * write_error - sets WriteErrorSeen
2588 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002589 */
2590 int err = -EINVAL;
2591 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2592 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002593 if (test_bit(Faulty, &rdev->flags))
2594 err = 0;
2595 else
2596 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002597 } else if (cmd_match(buf, "remove")) {
2598 if (rdev->raid_disk >= 0)
2599 err = -EBUSY;
2600 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002601 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002602 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002603 if (mddev_is_clustered(mddev))
2604 err = md_cluster_ops->remove_disk(mddev, rdev);
2605
2606 if (err == 0) {
2607 md_kick_rdev_from_array(rdev);
2608 if (mddev->pers)
2609 md_update_sb(mddev, 1);
2610 md_new_event(mddev);
2611 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002612 }
NeilBrownf6556752006-06-26 00:28:01 -07002613 } else if (cmd_match(buf, "writemostly")) {
2614 set_bit(WriteMostly, &rdev->flags);
2615 err = 0;
2616 } else if (cmd_match(buf, "-writemostly")) {
2617 clear_bit(WriteMostly, &rdev->flags);
2618 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002619 } else if (cmd_match(buf, "blocked")) {
2620 set_bit(Blocked, &rdev->flags);
2621 err = 0;
2622 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002623 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002624 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002625 /* metadata handler doesn't understand badblocks,
2626 * so we need to fail the device
2627 */
2628 md_error(rdev->mddev, rdev);
2629 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002630 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002631 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002632 wake_up(&rdev->blocked_wait);
2633 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2634 md_wakeup_thread(rdev->mddev->thread);
2635
2636 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002637 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2638 set_bit(In_sync, &rdev->flags);
2639 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002640 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2641 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002642 if (rdev->mddev->pers == NULL) {
2643 clear_bit(In_sync, &rdev->flags);
2644 rdev->saved_raid_disk = rdev->raid_disk;
2645 rdev->raid_disk = -1;
2646 err = 0;
2647 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002648 } else if (cmd_match(buf, "write_error")) {
2649 set_bit(WriteErrorSeen, &rdev->flags);
2650 err = 0;
2651 } else if (cmd_match(buf, "-write_error")) {
2652 clear_bit(WriteErrorSeen, &rdev->flags);
2653 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002654 } else if (cmd_match(buf, "want_replacement")) {
2655 /* Any non-spare device that is not a replacement can
2656 * become want_replacement at any time, but we then need to
2657 * check if recovery is needed.
2658 */
2659 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002660 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002661 !test_bit(Replacement, &rdev->flags))
2662 set_bit(WantReplacement, &rdev->flags);
2663 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2664 md_wakeup_thread(rdev->mddev->thread);
2665 err = 0;
2666 } else if (cmd_match(buf, "-want_replacement")) {
2667 /* Clearing 'want_replacement' is always allowed.
2668 * Once replacements starts it is too late though.
2669 */
2670 err = 0;
2671 clear_bit(WantReplacement, &rdev->flags);
2672 } else if (cmd_match(buf, "replacement")) {
2673 /* Can only set a device as a replacement when array has not
2674 * yet been started. Once running, replacement is automatic
2675 * from spares, or by assigning 'slot'.
2676 */
2677 if (rdev->mddev->pers)
2678 err = -EBUSY;
2679 else {
2680 set_bit(Replacement, &rdev->flags);
2681 err = 0;
2682 }
2683 } else if (cmd_match(buf, "-replacement")) {
2684 /* Similarly, can only clear Replacement before start */
2685 if (rdev->mddev->pers)
2686 err = -EBUSY;
2687 else {
2688 clear_bit(Replacement, &rdev->flags);
2689 err = 0;
2690 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002691 } else if (cmd_match(buf, "re-add")) {
2692 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002693 /* clear_bit is performed _after_ all the devices
2694 * have their local Faulty bit cleared. If any writes
2695 * happen in the meantime in the local node, they
2696 * will land in the local bitmap, which will be synced
2697 * by this node eventually
2698 */
2699 if (!mddev_is_clustered(rdev->mddev) ||
2700 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2701 clear_bit(Faulty, &rdev->flags);
2702 err = add_bound_rdev(rdev);
2703 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002704 } else
2705 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002706 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002707 if (!err)
2708 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002709 return err ? err : len;
2710}
NeilBrown80ca3a42006-07-10 04:44:18 -07002711static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002712__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002713
2714static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002715errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002716{
2717 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2718}
2719
2720static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002721errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002722{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002723 unsigned int n;
2724 int rv;
2725
2726 rv = kstrtouint(buf, 10, &n);
2727 if (rv < 0)
2728 return rv;
2729 atomic_set(&rdev->corrected_errors, n);
2730 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002731}
2732static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002733__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002734
NeilBrown014236d2006-01-06 00:20:55 -08002735static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002736slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002737{
Shaohua Lif2076e72015-10-08 21:54:12 -07002738 if (test_bit(Journal, &rdev->flags))
2739 return sprintf(page, "journal\n");
2740 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002741 return sprintf(page, "none\n");
2742 else
2743 return sprintf(page, "%d\n", rdev->raid_disk);
2744}
2745
2746static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002747slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002748{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002749 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002750 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002751
Shaohua Lif2076e72015-10-08 21:54:12 -07002752 if (test_bit(Journal, &rdev->flags))
2753 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002754 if (strncmp(buf, "none", 4)==0)
2755 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002756 else {
2757 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2758 if (err < 0)
2759 return err;
2760 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002761 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002762 /* Setting 'slot' on an active array requires also
2763 * updating the 'rd%d' link, and communicating
2764 * with the personality with ->hot_*_disk.
2765 * For now we only support removing
2766 * failed/spare devices. This normally happens automatically,
2767 * but not when the metadata is externally managed.
2768 */
NeilBrownc303da62008-02-06 01:39:51 -08002769 if (rdev->raid_disk == -1)
2770 return -EEXIST;
2771 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002772 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002773 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002774 clear_bit(Blocked, &rdev->flags);
2775 remove_and_add_spares(rdev->mddev, rdev);
2776 if (rdev->raid_disk >= 0)
2777 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002778 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2779 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002780 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002781 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002782 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002783 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002784 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002785
2786 if (rdev->raid_disk != -1)
2787 return -EBUSY;
2788
NeilBrownc6751b22011-02-02 11:57:13 +11002789 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2790 return -EBUSY;
2791
Neil Brown6c2fce22008-06-28 08:31:31 +10002792 if (rdev->mddev->pers->hot_add_disk == NULL)
2793 return -EINVAL;
2794
NeilBrownba1b41b2011-01-14 09:14:34 +11002795 if (slot >= rdev->mddev->raid_disks &&
2796 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2797 return -ENOSPC;
2798
Neil Brown6c2fce22008-06-28 08:31:31 +10002799 rdev->raid_disk = slot;
2800 if (test_bit(In_sync, &rdev->flags))
2801 rdev->saved_raid_disk = slot;
2802 else
2803 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002804 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002805 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002806 err = rdev->mddev->pers->
2807 hot_add_disk(rdev->mddev, rdev);
2808 if (err) {
2809 rdev->raid_disk = -1;
2810 return err;
2811 } else
2812 sysfs_notify_dirent_safe(rdev->sysfs_state);
2813 if (sysfs_link_rdev(rdev->mddev, rdev))
2814 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002815 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002816 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002817 if (slot >= rdev->mddev->raid_disks &&
2818 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002819 return -ENOSPC;
2820 rdev->raid_disk = slot;
2821 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002822 clear_bit(Faulty, &rdev->flags);
2823 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002824 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002825 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002826 }
NeilBrown014236d2006-01-06 00:20:55 -08002827 return len;
2828}
2829
NeilBrown014236d2006-01-06 00:20:55 -08002830static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002831__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002832
NeilBrown93c8cad2006-01-06 00:20:56 -08002833static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002834offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002835{
Andrew Morton6961ece2006-01-06 00:20:59 -08002836 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002837}
2838
2839static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002840offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002841{
NeilBrownc6563a82012-05-21 09:27:00 +10002842 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002843 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002844 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002845 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002846 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002847 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002848 /* Must set offset before size, so overlap checks
2849 * can be sane */
2850 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002851 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002852 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002853 return len;
2854}
2855
2856static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002857__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002858
NeilBrownc6563a82012-05-21 09:27:00 +10002859static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2860{
2861 return sprintf(page, "%llu\n",
2862 (unsigned long long)rdev->new_data_offset);
2863}
2864
2865static ssize_t new_offset_store(struct md_rdev *rdev,
2866 const char *buf, size_t len)
2867{
2868 unsigned long long new_offset;
2869 struct mddev *mddev = rdev->mddev;
2870
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002871 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002872 return -EINVAL;
2873
NeilBrownf851b602014-12-11 10:02:10 +11002874 if (mddev->sync_thread ||
2875 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002876 return -EBUSY;
2877 if (new_offset == rdev->data_offset)
2878 /* reset is always permitted */
2879 ;
2880 else if (new_offset > rdev->data_offset) {
2881 /* must not push array size beyond rdev_sectors */
2882 if (new_offset - rdev->data_offset
2883 + mddev->dev_sectors > rdev->sectors)
2884 return -E2BIG;
2885 }
2886 /* Metadata worries about other space details. */
2887
2888 /* decreasing the offset is inconsistent with a backwards
2889 * reshape.
2890 */
2891 if (new_offset < rdev->data_offset &&
2892 mddev->reshape_backwards)
2893 return -EINVAL;
2894 /* Increasing offset is inconsistent with forwards
2895 * reshape. reshape_direction should be set to
2896 * 'backwards' first.
2897 */
2898 if (new_offset > rdev->data_offset &&
2899 !mddev->reshape_backwards)
2900 return -EINVAL;
2901
2902 if (mddev->pers && mddev->persistent &&
2903 !super_types[mddev->major_version]
2904 .allow_new_offset(rdev, new_offset))
2905 return -E2BIG;
2906 rdev->new_data_offset = new_offset;
2907 if (new_offset > rdev->data_offset)
2908 mddev->reshape_backwards = 1;
2909 else if (new_offset < rdev->data_offset)
2910 mddev->reshape_backwards = 0;
2911
2912 return len;
2913}
2914static struct rdev_sysfs_entry rdev_new_offset =
2915__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2916
NeilBrown83303b62006-01-06 00:21:06 -08002917static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002918rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002919{
Andre Nolldd8ac332009-03-31 14:33:13 +11002920 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002921}
2922
NeilBrownc5d79ad2008-02-06 01:39:54 -08002923static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2924{
2925 /* check if two start/length pairs overlap */
2926 if (s1+l1 <= s2)
2927 return 0;
2928 if (s2+l2 <= s1)
2929 return 0;
2930 return 1;
2931}
2932
Dan Williamsb522adc2009-03-31 15:00:31 +11002933static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2934{
2935 unsigned long long blocks;
2936 sector_t new;
2937
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002938 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002939 return -EINVAL;
2940
2941 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2942 return -EINVAL; /* sector conversion overflow */
2943
2944 new = blocks * 2;
2945 if (new != blocks * 2)
2946 return -EINVAL; /* unsigned long long to sector_t overflow */
2947
2948 *sectors = new;
2949 return 0;
2950}
2951
NeilBrown83303b62006-01-06 00:21:06 -08002952static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002953rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002954{
NeilBrownfd01b882011-10-11 16:47:53 +11002955 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002956 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002957 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002958
Shaohua Lif2076e72015-10-08 21:54:12 -07002959 if (test_bit(Journal, &rdev->flags))
2960 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002961 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002962 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002963 if (rdev->data_offset != rdev->new_data_offset)
2964 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002965 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002966 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002967 sectors = super_types[my_mddev->major_version].
2968 rdev_size_change(rdev, sectors);
2969 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002970 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002971 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002972 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002973 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002974 if (!my_mddev->pers->resize)
2975 /* Cannot change size for RAID0 or Linear etc */
2976 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002977 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002978 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002979 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002980
Andre Nolldd8ac332009-03-31 14:33:13 +11002981 rdev->sectors = sectors;
2982 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002983 /* Need to check that all other rdevs with the same
2984 * ->bdev do not overlap. 'rcu' is sufficient to walk
2985 * the rdev lists safely.
2986 * This check does not provide a hard guarantee, it
2987 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002988 */
NeilBrownfd01b882011-10-11 16:47:53 +11002989 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002990 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002991 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002992
NeilBrown8b1afc32014-09-29 15:33:20 +10002993 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002994 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002995 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002996
NeilBrowndafb20f2012-03-19 12:46:39 +11002997 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002998 if (rdev->bdev == rdev2->bdev &&
2999 rdev != rdev2 &&
3000 overlaps(rdev->data_offset, rdev->sectors,
3001 rdev2->data_offset,
3002 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003003 overlap = 1;
3004 break;
3005 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003006 if (overlap) {
3007 mddev_put(mddev);
3008 break;
3009 }
3010 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003011 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003012 if (overlap) {
3013 /* Someone else could have slipped in a size
3014 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003015 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003016 * safe, and trust userspace not to race with
3017 * itself
3018 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003019 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003020 return -EBUSY;
3021 }
3022 }
NeilBrown83303b62006-01-06 00:21:06 -08003023 return len;
3024}
3025
3026static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003027__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003028
NeilBrown3cb03002011-10-11 16:45:26 +11003029static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003030{
3031 unsigned long long recovery_start = rdev->recovery_offset;
3032
3033 if (test_bit(In_sync, &rdev->flags) ||
3034 recovery_start == MaxSector)
3035 return sprintf(page, "none\n");
3036
3037 return sprintf(page, "%llu\n", recovery_start);
3038}
3039
NeilBrown3cb03002011-10-11 16:45:26 +11003040static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003041{
3042 unsigned long long recovery_start;
3043
3044 if (cmd_match(buf, "none"))
3045 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003046 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003047 return -EINVAL;
3048
3049 if (rdev->mddev->pers &&
3050 rdev->raid_disk >= 0)
3051 return -EBUSY;
3052
3053 rdev->recovery_offset = recovery_start;
3054 if (recovery_start == MaxSector)
3055 set_bit(In_sync, &rdev->flags);
3056 else
3057 clear_bit(In_sync, &rdev->flags);
3058 return len;
3059}
3060
3061static struct rdev_sysfs_entry rdev_recovery_start =
3062__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3063
Vishal Vermafc974ee2015-12-24 19:20:34 -07003064/* sysfs access to bad-blocks list.
3065 * We present two files.
3066 * 'bad-blocks' lists sector numbers and lengths of ranges that
3067 * are recorded as bad. The list is truncated to fit within
3068 * the one-page limit of sysfs.
3069 * Writing "sector length" to this file adds an acknowledged
3070 * bad block list.
3071 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3072 * been acknowledged. Writing to this file adds bad blocks
3073 * without acknowledging them. This is largely for testing.
3074 */
NeilBrown3cb03002011-10-11 16:45:26 +11003075static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003076{
3077 return badblocks_show(&rdev->badblocks, page, 0);
3078}
NeilBrown3cb03002011-10-11 16:45:26 +11003079static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003080{
NeilBrownde393cd2011-07-28 11:31:48 +10003081 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3082 /* Maybe that ack was all we needed */
3083 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3084 wake_up(&rdev->blocked_wait);
3085 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003086}
3087static struct rdev_sysfs_entry rdev_bad_blocks =
3088__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3089
NeilBrown3cb03002011-10-11 16:45:26 +11003090static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003091{
3092 return badblocks_show(&rdev->badblocks, page, 1);
3093}
NeilBrown3cb03002011-10-11 16:45:26 +11003094static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003095{
3096 return badblocks_store(&rdev->badblocks, page, len, 1);
3097}
3098static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3099__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3100
NeilBrown86e6ffd2005-11-08 21:39:24 -08003101static struct attribute *rdev_default_attrs[] = {
3102 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003103 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003104 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003105 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003106 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003107 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003108 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003109 &rdev_bad_blocks.attr,
3110 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003111 NULL,
3112};
3113static ssize_t
3114rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3115{
3116 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003117 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003118
3119 if (!entry->show)
3120 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003121 if (!rdev->mddev)
3122 return -EBUSY;
3123 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003124}
3125
3126static ssize_t
3127rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3128 const char *page, size_t length)
3129{
3130 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003131 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003132 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003133 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003134
3135 if (!entry->store)
3136 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003137 if (!capable(CAP_SYS_ADMIN))
3138 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003139 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003140 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003141 if (rdev->mddev == NULL)
3142 rv = -EBUSY;
3143 else
3144 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003145 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003146 }
3147 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003148}
3149
3150static void rdev_free(struct kobject *ko)
3151{
NeilBrown3cb03002011-10-11 16:45:26 +11003152 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003153 kfree(rdev);
3154}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003155static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003156 .show = rdev_attr_show,
3157 .store = rdev_attr_store,
3158};
3159static struct kobj_type rdev_ktype = {
3160 .release = rdev_free,
3161 .sysfs_ops = &rdev_sysfs_ops,
3162 .default_attrs = rdev_default_attrs,
3163};
3164
NeilBrown3cb03002011-10-11 16:45:26 +11003165int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003166{
3167 rdev->desc_nr = -1;
3168 rdev->saved_raid_disk = -1;
3169 rdev->raid_disk = -1;
3170 rdev->flags = 0;
3171 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003172 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003173 rdev->sb_events = 0;
3174 rdev->last_read_error.tv_sec = 0;
3175 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003176 rdev->sb_loaded = 0;
3177 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003178 atomic_set(&rdev->nr_pending, 0);
3179 atomic_set(&rdev->read_errors, 0);
3180 atomic_set(&rdev->corrected_errors, 0);
3181
3182 INIT_LIST_HEAD(&rdev->same_set);
3183 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003184
3185 /* Add space to store bad block list.
3186 * This reserves the space even on arrays where it cannot
3187 * be used - I wonder if that matters
3188 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003189 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003190}
3191EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192/*
3193 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3194 *
3195 * mark the device faulty if:
3196 *
3197 * - the device is nonexistent (zero size)
3198 * - the device has no valid superblock
3199 *
3200 * a faulty rdev _never_ has rdev->sb set.
3201 */
NeilBrown3cb03002011-10-11 16:45:26 +11003202static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
3204 char b[BDEVNAME_SIZE];
3205 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003206 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 sector_t size;
3208
NeilBrown9ffae0c2006-01-06 00:20:32 -08003209 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 if (!rdev) {
3211 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3212 return ERR_PTR(-ENOMEM);
3213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
NeilBrown2230dfe2011-07-28 11:31:46 +10003215 err = md_rdev_init(rdev);
3216 if (err)
3217 goto abort_free;
3218 err = alloc_disk_sb(rdev);
3219 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 goto abort_free;
3221
NeilBrownc5d79ad2008-02-06 01:39:54 -08003222 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 if (err)
3224 goto abort_free;
3225
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003226 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003227
Mike Snitzer77304d22010-11-08 14:39:12 +01003228 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003230 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 "md: %s has zero or unknown size, marking faulty!\n",
3232 bdevname(rdev->bdev,b));
3233 err = -EINVAL;
3234 goto abort_free;
3235 }
3236
3237 if (super_format >= 0) {
3238 err = super_types[super_format].
3239 load_super(rdev, NULL, super_minor);
3240 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003241 printk(KERN_WARNING
3242 "md: %s does not have a valid v%d.%d "
3243 "superblock, not importing!\n",
3244 bdevname(rdev->bdev,b),
3245 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 goto abort_free;
3247 }
3248 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003249 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 "md: could not read %s's sb, not importing!\n",
3251 bdevname(rdev->bdev,b));
3252 goto abort_free;
3253 }
3254 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return rdev;
3257
3258abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003259 if (rdev->bdev)
3260 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003261 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 kfree(rdev);
3263 return ERR_PTR(err);
3264}
3265
3266/*
3267 * Check a full RAID array for plausibility
3268 */
3269
NeilBrownf72ffdd2014-09-30 14:23:59 +10003270static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
3272 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003273 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 char b[BDEVNAME_SIZE];
3275
3276 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003277 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 switch (super_types[mddev->major_version].
3279 load_super(rdev, freshest, mddev->minor_version)) {
3280 case 1:
3281 freshest = rdev;
3282 break;
3283 case 0:
3284 break;
3285 default:
3286 printk( KERN_ERR \
3287 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003288 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003290 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 }
3292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 super_types[mddev->major_version].
3294 validate_super(mddev, freshest);
3295
3296 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003297 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003298 if (mddev->max_disks &&
3299 (rdev->desc_nr >= mddev->max_disks ||
3300 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003301 printk(KERN_WARNING
3302 "md: %s: %s: only %d devices permitted\n",
3303 mdname(mddev), bdevname(rdev->bdev, b),
3304 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003305 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003306 continue;
3307 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003308 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 if (super_types[mddev->major_version].
3310 validate_super(mddev, rdev)) {
3311 printk(KERN_WARNING "md: kicking non-fresh %s"
3312 " from array!\n",
3313 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003314 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 continue;
3316 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 if (mddev->level == LEVEL_MULTIPATH) {
3319 rdev->desc_nr = i++;
3320 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003321 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003322 } else if (rdev->raid_disk >=
3323 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3324 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003325 rdev->raid_disk = -1;
3326 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 }
3328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
NeilBrown72e02072009-12-14 12:49:55 +11003331/* Read a fixed-point number.
3332 * Numbers in sysfs attributes should be in "standard" units where
3333 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003334 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003335 * milliseconds or jiffies.
3336 * This function takes a decimal number with a possible fractional
3337 * component, and produces an integer which is the result of
3338 * multiplying that number by 10^'scale'.
3339 * all without any floating-point arithmetic.
3340 */
3341int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3342{
3343 unsigned long result = 0;
3344 long decimals = -1;
3345 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3346 if (*cp == '.')
3347 decimals = 0;
3348 else if (decimals < scale) {
3349 unsigned int value;
3350 value = *cp - '0';
3351 result = result * 10 + value;
3352 if (decimals >= 0)
3353 decimals++;
3354 }
3355 cp++;
3356 }
3357 if (*cp == '\n')
3358 cp++;
3359 if (*cp)
3360 return -EINVAL;
3361 if (decimals < 0)
3362 decimals = 0;
3363 while (decimals < scale) {
3364 result *= 10;
3365 decimals ++;
3366 }
3367 *res = result;
3368 return 0;
3369}
3370
NeilBrowneae17012005-11-08 21:39:23 -08003371static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003372safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003373{
3374 int msec = (mddev->safemode_delay*1000)/HZ;
3375 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3376}
3377static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003378safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003379{
NeilBrown16f17b32006-06-26 00:27:37 -07003380 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003381
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003382 if (mddev_is_clustered(mddev)) {
3383 pr_info("md: Safemode is disabled for clustered mode\n");
3384 return -EINVAL;
3385 }
3386
NeilBrown72e02072009-12-14 12:49:55 +11003387 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003388 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003389 if (msec == 0)
3390 mddev->safemode_delay = 0;
3391 else {
NeilBrown19052c02008-08-05 15:54:13 +10003392 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003393 unsigned long new_delay = (msec*HZ)/1000;
3394
3395 if (new_delay == 0)
3396 new_delay = 1;
3397 mddev->safemode_delay = new_delay;
3398 if (new_delay < old_delay || old_delay == 0)
3399 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003400 }
3401 return len;
3402}
3403static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003404__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003405
3406static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003407level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003408{
NeilBrown36d091f2014-12-15 12:56:58 +11003409 struct md_personality *p;
3410 int ret;
3411 spin_lock(&mddev->lock);
3412 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003413 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003414 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003415 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003416 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003417 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003418 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003419 else
NeilBrown36d091f2014-12-15 12:56:58 +11003420 ret = 0;
3421 spin_unlock(&mddev->lock);
3422 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003423}
3424
NeilBrownd9d166c2006-01-06 00:20:51 -08003425static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003426level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003427{
Dan Williamsf2859af2010-05-02 10:04:16 -07003428 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003429 ssize_t rv;
3430 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003431 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003432 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003433 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003434 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003435
NeilBrown67918752014-12-15 12:57:01 +11003436 if (slen == 0 || slen >= sizeof(clevel))
3437 return -EINVAL;
3438
3439 rv = mddev_lock(mddev);
3440 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003441 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003442
3443 if (mddev->pers == NULL) {
3444 strncpy(mddev->clevel, buf, slen);
3445 if (mddev->clevel[slen-1] == '\n')
3446 slen--;
3447 mddev->clevel[slen] = 0;
3448 mddev->level = LEVEL_NONE;
3449 rv = len;
3450 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003451 }
NeilBrown67918752014-12-15 12:57:01 +11003452 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003453 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003454 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003455
3456 /* request to change the personality. Need to ensure:
3457 * - array is not engaged in resync/recovery/reshape
3458 * - old personality can be suspended
3459 * - new personality will access other array.
3460 */
3461
NeilBrown67918752014-12-15 12:57:01 +11003462 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003463 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003464 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003465 mddev->reshape_position != MaxSector ||
3466 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003467 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003468
NeilBrown67918752014-12-15 12:57:01 +11003469 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003470 if (!mddev->pers->quiesce) {
3471 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3472 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003473 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003474 }
3475
3476 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003477 strncpy(clevel, buf, slen);
3478 if (clevel[slen-1] == '\n')
3479 slen--;
3480 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003481 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003482 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003483
Dan Williamsf2859af2010-05-02 10:04:16 -07003484 if (request_module("md-%s", clevel) != 0)
3485 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003486 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003487 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003488 if (!pers || !try_module_get(pers->owner)) {
3489 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003490 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003491 rv = -EINVAL;
3492 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003493 }
3494 spin_unlock(&pers_lock);
3495
3496 if (pers == mddev->pers) {
3497 /* Nothing to do! */
3498 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003499 rv = len;
3500 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003501 }
3502 if (!pers->takeover) {
3503 module_put(pers->owner);
3504 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003505 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003506 rv = -EINVAL;
3507 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003508 }
3509
NeilBrowndafb20f2012-03-19 12:46:39 +11003510 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003511 rdev->new_raid_disk = rdev->raid_disk;
3512
NeilBrown245f46c2009-03-31 14:39:39 +11003513 /* ->takeover must set new_* and/or delta_disks
3514 * if it succeeds, and may set them when it fails.
3515 */
3516 priv = pers->takeover(mddev);
3517 if (IS_ERR(priv)) {
3518 mddev->new_level = mddev->level;
3519 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003520 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003521 mddev->raid_disks -= mddev->delta_disks;
3522 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003523 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003524 module_put(pers->owner);
3525 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003526 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003527 rv = PTR_ERR(priv);
3528 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003529 }
3530
3531 /* Looks like we have a winner */
3532 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003533 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003534
3535 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003536 oldpers = mddev->pers;
3537 oldpriv = mddev->private;
3538 mddev->pers = pers;
3539 mddev->private = priv;
3540 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3541 mddev->level = mddev->new_level;
3542 mddev->layout = mddev->new_layout;
3543 mddev->chunk_sectors = mddev->new_chunk_sectors;
3544 mddev->delta_disks = 0;
3545 mddev->reshape_backwards = 0;
3546 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003547 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003548
NeilBrowndb721d32014-12-15 12:56:58 +11003549 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003550 mddev->external) {
3551 /* We are converting from a no-redundancy array
3552 * to a redundancy array and metadata is managed
3553 * externally so we need to be sure that writes
3554 * won't block due to a need to transition
3555 * clean->dirty
3556 * until external management is started.
3557 */
3558 mddev->in_sync = 0;
3559 mddev->safemode_delay = 0;
3560 mddev->safemode = 0;
3561 }
3562
NeilBrowndb721d32014-12-15 12:56:58 +11003563 oldpers->free(mddev, oldpriv);
3564
3565 if (oldpers->sync_request == NULL &&
3566 pers->sync_request != NULL) {
3567 /* need to add the md_redundancy_group */
3568 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3569 printk(KERN_WARNING
3570 "md: cannot register extra attributes for %s\n",
3571 mdname(mddev));
3572 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3573 }
3574 if (oldpers->sync_request != NULL &&
3575 pers->sync_request == NULL) {
3576 /* need to remove the md_redundancy_group */
3577 if (mddev->to_remove == NULL)
3578 mddev->to_remove = &md_redundancy_group;
3579 }
3580
NeilBrowndafb20f2012-03-19 12:46:39 +11003581 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003582 if (rdev->raid_disk < 0)
3583 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003584 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003585 rdev->new_raid_disk = -1;
3586 if (rdev->new_raid_disk == rdev->raid_disk)
3587 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003588 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003589 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003590 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003591 if (rdev->raid_disk < 0)
3592 continue;
3593 if (rdev->new_raid_disk == rdev->raid_disk)
3594 continue;
3595 rdev->raid_disk = rdev->new_raid_disk;
3596 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003597 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003598 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003599 if (sysfs_link_rdev(mddev, rdev))
3600 printk(KERN_WARNING "md: cannot register rd%d"
3601 " for %s after level change\n",
3602 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003603 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003604 }
3605
NeilBrowndb721d32014-12-15 12:56:58 +11003606 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003607 /* this is now an array without redundancy, so
3608 * it must always be in_sync
3609 */
3610 mddev->in_sync = 1;
3611 del_timer_sync(&mddev->safemode_timer);
3612 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003613 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003614 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003615 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003616 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003617 if (!mddev->thread)
3618 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003619 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003620 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003621 rv = len;
3622out_unlock:
3623 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003624 return rv;
3625}
3626
3627static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003628__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003629
NeilBrownd4dbd022006-06-26 00:27:59 -07003630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003631layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003632{
3633 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003634 if (mddev->reshape_position != MaxSector &&
3635 mddev->layout != mddev->new_layout)
3636 return sprintf(page, "%d (%d)\n",
3637 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003638 return sprintf(page, "%d\n", mddev->layout);
3639}
3640
3641static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003642layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003643{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003644 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003645 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003646
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003647 err = kstrtouint(buf, 10, &n);
3648 if (err < 0)
3649 return err;
NeilBrown67918752014-12-15 12:57:01 +11003650 err = mddev_lock(mddev);
3651 if (err)
3652 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003653
NeilBrownb3546032009-03-31 14:56:41 +11003654 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003655 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003656 err = -EBUSY;
3657 else if (mddev->ro)
3658 err = -EROFS;
3659 else {
3660 mddev->new_layout = n;
3661 err = mddev->pers->check_reshape(mddev);
3662 if (err)
3663 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003664 }
NeilBrownb3546032009-03-31 14:56:41 +11003665 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003666 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003667 if (mddev->reshape_position == MaxSector)
3668 mddev->layout = n;
3669 }
NeilBrown67918752014-12-15 12:57:01 +11003670 mddev_unlock(mddev);
3671 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003672}
3673static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003674__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003675
NeilBrowneae17012005-11-08 21:39:23 -08003676static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003677raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003678{
NeilBrownbb636542005-11-08 21:39:45 -08003679 if (mddev->raid_disks == 0)
3680 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003681 if (mddev->reshape_position != MaxSector &&
3682 mddev->delta_disks != 0)
3683 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3684 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003685 return sprintf(page, "%d\n", mddev->raid_disks);
3686}
3687
NeilBrownfd01b882011-10-11 16:47:53 +11003688static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003689
3690static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003691raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003692{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003693 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003694 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003695
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003696 err = kstrtouint(buf, 10, &n);
3697 if (err < 0)
3698 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003699
NeilBrown67918752014-12-15 12:57:01 +11003700 err = mddev_lock(mddev);
3701 if (err)
3702 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003703 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003704 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003705 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003706 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003707 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003708
NeilBrown67918752014-12-15 12:57:01 +11003709 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003710 rdev_for_each(rdev, mddev) {
3711 if (olddisks < n &&
3712 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003713 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003714 if (olddisks > n &&
3715 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003716 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003717 }
NeilBrown67918752014-12-15 12:57:01 +11003718 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003719 mddev->delta_disks = n - olddisks;
3720 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003721 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003722 } else
NeilBrownda943b992006-01-06 00:20:54 -08003723 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003724out_unlock:
3725 mddev_unlock(mddev);
3726 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003727}
3728static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003729__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003730
NeilBrown24dd4692005-11-08 21:39:26 -08003731static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003732chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003733{
NeilBrown08a02ec2007-05-09 02:35:38 -07003734 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003735 mddev->chunk_sectors != mddev->new_chunk_sectors)
3736 return sprintf(page, "%d (%d)\n",
3737 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003738 mddev->chunk_sectors << 9);
3739 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003740}
3741
3742static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003743chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003744{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003745 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003746 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003747
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003748 err = kstrtoul(buf, 10, &n);
3749 if (err < 0)
3750 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003751
NeilBrown67918752014-12-15 12:57:01 +11003752 err = mddev_lock(mddev);
3753 if (err)
3754 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003755 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003756 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003757 err = -EBUSY;
3758 else if (mddev->ro)
3759 err = -EROFS;
3760 else {
3761 mddev->new_chunk_sectors = n >> 9;
3762 err = mddev->pers->check_reshape(mddev);
3763 if (err)
3764 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003765 }
NeilBrownb3546032009-03-31 14:56:41 +11003766 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003767 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003768 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003769 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003770 }
NeilBrown67918752014-12-15 12:57:01 +11003771 mddev_unlock(mddev);
3772 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003773}
3774static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003775__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003776
NeilBrowna94213b2006-06-26 00:28:00 -07003777static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003778resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003779{
NeilBrownd1a7c502009-03-31 15:24:32 +11003780 if (mddev->recovery_cp == MaxSector)
3781 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003782 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3783}
3784
3785static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003786resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003787{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003788 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003789 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003790
3791 if (cmd_match(buf, "none"))
3792 n = MaxSector;
3793 else {
3794 err = kstrtoull(buf, 10, &n);
3795 if (err < 0)
3796 return err;
3797 if (n != (sector_t)n)
3798 return -EINVAL;
3799 }
NeilBrowna94213b2006-06-26 00:28:00 -07003800
NeilBrown67918752014-12-15 12:57:01 +11003801 err = mddev_lock(mddev);
3802 if (err)
3803 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003804 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003805 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003806
NeilBrown67918752014-12-15 12:57:01 +11003807 if (!err) {
3808 mddev->recovery_cp = n;
3809 if (mddev->pers)
3810 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3811 }
3812 mddev_unlock(mddev);
3813 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003814}
3815static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003816__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3817 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003818
NeilBrown9e653b62006-06-26 00:27:58 -07003819/*
3820 * The array state can be:
3821 *
3822 * clear
3823 * No devices, no size, no level
3824 * Equivalent to STOP_ARRAY ioctl
3825 * inactive
3826 * May have some settings, but array is not active
3827 * all IO results in error
3828 * When written, doesn't tear down array, but just stops it
3829 * suspended (not supported yet)
3830 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003831 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003832 * readonly
3833 * no resync can happen. no superblocks get written.
3834 * write requests fail
3835 * read-auto
3836 * like readonly, but behaves like 'clean' on a write request.
3837 *
3838 * clean - no pending writes, but otherwise active.
3839 * When written to inactive array, starts without resync
3840 * If a write request arrives then
3841 * if metadata is known, mark 'dirty' and switch to 'active'.
3842 * if not known, block and switch to write-pending
3843 * If written to an active array that has pending writes, then fails.
3844 * active
3845 * fully active: IO and resync can be happening.
3846 * When written to inactive array, starts with resync
3847 *
3848 * write-pending
3849 * clean, but writes are blocked waiting for 'active' to be written.
3850 *
3851 * active-idle
3852 * like active, but no writes have been seen for a while (100msec).
3853 *
3854 */
3855enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3856 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003857static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003858 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3859 "write-pending", "active-idle", NULL };
3860
3861static int match_word(const char *word, char **list)
3862{
3863 int n;
3864 for (n=0; list[n]; n++)
3865 if (cmd_match(word, list[n]))
3866 break;
3867 return n;
3868}
3869
3870static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003871array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003872{
3873 enum array_state st = inactive;
3874
3875 if (mddev->pers)
3876 switch(mddev->ro) {
3877 case 1:
3878 st = readonly;
3879 break;
3880 case 2:
3881 st = read_auto;
3882 break;
3883 case 0:
3884 if (mddev->in_sync)
3885 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003886 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003887 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003888 else if (mddev->safemode)
3889 st = active_idle;
3890 else
3891 st = active;
3892 }
3893 else {
3894 if (list_empty(&mddev->disks) &&
3895 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003896 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003897 st = clear;
3898 else
3899 st = inactive;
3900 }
3901 return sprintf(page, "%s\n", array_states[st]);
3902}
3903
NeilBrownf72ffdd2014-09-30 14:23:59 +10003904static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3905static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3906static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003907static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003908
3909static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003910array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003911{
NeilBrown67918752014-12-15 12:57:01 +11003912 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003913 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003914
3915 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3916 /* don't take reconfig_mutex when toggling between
3917 * clean and active
3918 */
3919 spin_lock(&mddev->lock);
3920 if (st == active) {
3921 restart_array(mddev);
3922 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3923 wake_up(&mddev->sb_wait);
3924 err = 0;
3925 } else /* st == clean */ {
3926 restart_array(mddev);
3927 if (atomic_read(&mddev->writes_pending) == 0) {
3928 if (mddev->in_sync == 0) {
3929 mddev->in_sync = 1;
3930 if (mddev->safemode == 1)
3931 mddev->safemode = 0;
3932 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3933 }
3934 err = 0;
3935 } else
3936 err = -EBUSY;
3937 }
3938 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003939 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003940 }
3941 err = mddev_lock(mddev);
3942 if (err)
3943 return err;
3944 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003945 switch(st) {
3946 case bad_word:
3947 break;
3948 case clear:
3949 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003950 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003951 break;
3952 case inactive:
3953 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003954 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003955 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003956 else
NeilBrowne6910632008-02-06 01:39:51 -08003957 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003958 break;
3959 case suspended:
3960 break; /* not supported yet */
3961 case readonly:
3962 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003963 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003964 else {
3965 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003966 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003967 err = do_md_run(mddev);
3968 }
3969 break;
3970 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003971 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003972 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003973 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003974 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003975 err = restart_array(mddev);
3976 if (err == 0) {
3977 mddev->ro = 2;
3978 set_disk_ro(mddev->gendisk, 0);
3979 }
NeilBrown9e653b62006-06-26 00:27:58 -07003980 } else {
3981 mddev->ro = 2;
3982 err = do_md_run(mddev);
3983 }
3984 break;
3985 case clean:
3986 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003987 err = restart_array(mddev);
3988 if (err)
3989 break;
NeilBrown85572d72014-12-15 12:56:56 +11003990 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003991 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003992 if (mddev->in_sync == 0) {
3993 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003994 if (mddev->safemode == 1)
3995 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003996 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003997 }
3998 err = 0;
3999 } else
4000 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004001 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004002 } else
4003 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004004 break;
4005 case active:
4006 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004007 err = restart_array(mddev);
4008 if (err)
4009 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10004010 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004011 wake_up(&mddev->sb_wait);
4012 err = 0;
4013 } else {
4014 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004015 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004016 err = do_md_run(mddev);
4017 }
4018 break;
4019 case write_pending:
4020 case active_idle:
4021 /* these cannot be set */
4022 break;
4023 }
NeilBrown67918752014-12-15 12:57:01 +11004024
4025 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004026 if (mddev->hold_active == UNTIL_IOCTL)
4027 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004028 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004029 }
NeilBrown67918752014-12-15 12:57:01 +11004030 mddev_unlock(mddev);
4031 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004032}
NeilBrown80ca3a42006-07-10 04:44:18 -07004033static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004034__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004035
NeilBrown6d7ff7382006-01-06 00:21:16 -08004036static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004037max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004038 return sprintf(page, "%d\n",
4039 atomic_read(&mddev->max_corr_read_errors));
4040}
4041
4042static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004043max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004044{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004045 unsigned int n;
4046 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004047
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004048 rv = kstrtouint(buf, 10, &n);
4049 if (rv < 0)
4050 return rv;
4051 atomic_set(&mddev->max_corr_read_errors, n);
4052 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004053}
4054
4055static struct md_sysfs_entry max_corr_read_errors =
4056__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4057 max_corrected_read_errors_store);
4058
4059static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004060null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004061{
4062 return -EINVAL;
4063}
4064
4065static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004066new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004067{
4068 /* buf must be %d:%d\n? giving major and minor numbers */
4069 /* The new device is added to the array.
4070 * If the array has a persistent superblock, we read the
4071 * superblock to initialise info and check validity.
4072 * Otherwise, only checking done is that in bind_rdev_to_array,
4073 * which mainly checks size.
4074 */
4075 char *e;
4076 int major = simple_strtoul(buf, &e, 10);
4077 int minor;
4078 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004079 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004080 int err;
4081
4082 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4083 return -EINVAL;
4084 minor = simple_strtoul(e+1, &e, 10);
4085 if (*e && *e != '\n')
4086 return -EINVAL;
4087 dev = MKDEV(major, minor);
4088 if (major != MAJOR(dev) ||
4089 minor != MINOR(dev))
4090 return -EOVERFLOW;
4091
NeilBrown67918752014-12-15 12:57:01 +11004092 flush_workqueue(md_misc_wq);
4093
4094 err = mddev_lock(mddev);
4095 if (err)
4096 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004097 if (mddev->persistent) {
4098 rdev = md_import_device(dev, mddev->major_version,
4099 mddev->minor_version);
4100 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004101 struct md_rdev *rdev0
4102 = list_entry(mddev->disks.next,
4103 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004104 err = super_types[mddev->major_version]
4105 .load_super(rdev, rdev0, mddev->minor_version);
4106 if (err < 0)
4107 goto out;
4108 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004109 } else if (mddev->external)
4110 rdev = md_import_device(dev, -2, -1);
4111 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004112 rdev = md_import_device(dev, -1, -1);
4113
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004114 if (IS_ERR(rdev)) {
4115 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004116 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004117 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004118 err = bind_rdev_to_array(rdev, mddev);
4119 out:
4120 if (err)
4121 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004122 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004123 return err ? err : len;
4124}
4125
4126static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004127__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004128
4129static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004130bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004131{
4132 char *end;
4133 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004134 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004135
NeilBrown67918752014-12-15 12:57:01 +11004136 err = mddev_lock(mddev);
4137 if (err)
4138 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004139 if (!mddev->bitmap)
4140 goto out;
4141 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4142 while (*buf) {
4143 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4144 if (buf == end) break;
4145 if (*end == '-') { /* range */
4146 buf = end + 1;
4147 end_chunk = simple_strtoul(buf, &end, 0);
4148 if (buf == end) break;
4149 }
4150 if (*end && !isspace(*end)) break;
4151 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004152 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004153 }
4154 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4155out:
NeilBrown67918752014-12-15 12:57:01 +11004156 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004157 return len;
4158}
4159
4160static struct md_sysfs_entry md_bitmap =
4161__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4162
4163static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004164size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004165{
Andre Noll58c0fed2009-03-31 14:33:13 +11004166 return sprintf(page, "%llu\n",
4167 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004168}
4169
NeilBrownfd01b882011-10-11 16:47:53 +11004170static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004171
4172static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004173size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004174{
4175 /* If array is inactive, we can reduce the component size, but
4176 * not increase it (except from 0).
4177 * If array is active, we can try an on-line resize
4178 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004179 sector_t sectors;
4180 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004181
Andre Noll58c0fed2009-03-31 14:33:13 +11004182 if (err < 0)
4183 return err;
NeilBrown67918752014-12-15 12:57:01 +11004184 err = mddev_lock(mddev);
4185 if (err)
4186 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004187 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004188 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004189 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004190 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004191 if (mddev->dev_sectors == 0 ||
4192 mddev->dev_sectors > sectors)
4193 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004194 else
4195 err = -ENOSPC;
4196 }
NeilBrown67918752014-12-15 12:57:01 +11004197 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004198 return err ? err : len;
4199}
4200
4201static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004202__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004203
Masanari Iida83f0d772012-10-30 00:18:08 +09004204/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004205 * This is one of
4206 * 'none' for arrays with no metadata (good luck...)
4207 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004208 * or N.M for internally known formats
4209 */
4210static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004211metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004212{
4213 if (mddev->persistent)
4214 return sprintf(page, "%d.%d\n",
4215 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004216 else if (mddev->external)
4217 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004218 else
4219 return sprintf(page, "none\n");
4220}
4221
4222static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004223metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004224{
4225 int major, minor;
4226 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004227 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004228 /* Changing the details of 'external' metadata is
4229 * always permitted. Otherwise there must be
4230 * no devices attached to the array.
4231 */
NeilBrown67918752014-12-15 12:57:01 +11004232
4233 err = mddev_lock(mddev);
4234 if (err)
4235 return err;
4236 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004237 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4238 ;
4239 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004240 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004241
NeilBrown67918752014-12-15 12:57:01 +11004242 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004243 if (cmd_match(buf, "none")) {
4244 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004245 mddev->external = 0;
4246 mddev->major_version = 0;
4247 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004248 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004249 }
4250 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004251 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004252 if (namelen >= sizeof(mddev->metadata_type))
4253 namelen = sizeof(mddev->metadata_type)-1;
4254 strncpy(mddev->metadata_type, buf+9, namelen);
4255 mddev->metadata_type[namelen] = 0;
4256 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4257 mddev->metadata_type[--namelen] = 0;
4258 mddev->persistent = 0;
4259 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004260 mddev->major_version = 0;
4261 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004262 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004263 }
4264 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004265 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004266 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004267 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004268 buf = e+1;
4269 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004270 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004271 goto out_unlock;
4272 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004273 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004274 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004275 mddev->major_version = major;
4276 mddev->minor_version = minor;
4277 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004278 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004279 err = 0;
4280out_unlock:
4281 mddev_unlock(mddev);
4282 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004283}
4284
4285static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004286__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004287
NeilBrowna35b0d62006-01-06 00:20:49 -08004288static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004289action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004290{
NeilBrown7eec3142005-11-08 21:39:44 -08004291 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004292 unsigned long recovery = mddev->recovery;
4293 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004294 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004295 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4296 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4297 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004298 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004299 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4300 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004301 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004302 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004303 type = "check";
4304 else
4305 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004306 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004307 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004308 else if (mddev->reshape_position != MaxSector)
4309 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004310 }
4311 return sprintf(page, "%s\n", type);
4312}
4313
4314static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004315action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004316{
NeilBrown7eec3142005-11-08 21:39:44 -08004317 if (!mddev->pers || !mddev->pers->sync_request)
4318 return -EINVAL;
4319
NeilBrownb6a9ce62009-05-26 09:41:17 +10004320
4321 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004322 if (cmd_match(page, "frozen"))
4323 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4324 else
4325 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004326 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4327 mddev_lock(mddev) == 0) {
4328 flush_workqueue(md_misc_wq);
4329 if (mddev->sync_thread) {
4330 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004331 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004332 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004333 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004334 }
NeilBrown312045e2015-12-21 11:01:21 +11004335 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004336 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004337 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004338 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004339 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004340 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004341 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004342 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004343 int err;
4344 if (mddev->pers->start_reshape == NULL)
4345 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004346 err = mddev_lock(mddev);
4347 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004348 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4349 err = -EBUSY;
4350 else {
4351 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4352 err = mddev->pers->start_reshape(mddev);
4353 }
NeilBrown67918752014-12-15 12:57:01 +11004354 mddev_unlock(mddev);
4355 }
NeilBrown16484bf2006-03-27 01:18:13 -08004356 if (err)
4357 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004358 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004359 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004360 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004361 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004362 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004363 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004364 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004365 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4366 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004367 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004368 if (mddev->ro == 2) {
4369 /* A write to sync_action is enough to justify
4370 * canceling read-auto mode
4371 */
4372 mddev->ro = 0;
4373 md_wakeup_thread(mddev->sync_thread);
4374 }
NeilBrown03c902e2006-01-06 00:20:46 -08004375 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004376 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004377 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004378 return len;
4379}
4380
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004381static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004382__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004383
4384static ssize_t
4385last_sync_action_show(struct mddev *mddev, char *page)
4386{
4387 return sprintf(page, "%s\n", mddev->last_sync_action);
4388}
4389
4390static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4391
NeilBrown9d888832005-11-08 21:39:26 -08004392static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004393mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004394{
4395 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004396 (unsigned long long)
4397 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004398}
4399
NeilBrown80ca3a42006-07-10 04:44:18 -07004400static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004401
NeilBrown88202a02006-01-06 00:21:36 -08004402static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004403sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004404{
4405 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4406 mddev->sync_speed_min ? "local": "system");
4407}
4408
4409static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004410sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004411{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004412 unsigned int min;
4413 int rv;
4414
NeilBrown88202a02006-01-06 00:21:36 -08004415 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004416 min = 0;
4417 } else {
4418 rv = kstrtouint(buf, 10, &min);
4419 if (rv < 0)
4420 return rv;
4421 if (min == 0)
4422 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004423 }
NeilBrown88202a02006-01-06 00:21:36 -08004424 mddev->sync_speed_min = min;
4425 return len;
4426}
4427
4428static struct md_sysfs_entry md_sync_min =
4429__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4430
4431static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004432sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004433{
4434 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4435 mddev->sync_speed_max ? "local": "system");
4436}
4437
4438static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004439sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004440{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004441 unsigned int max;
4442 int rv;
4443
NeilBrown88202a02006-01-06 00:21:36 -08004444 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004445 max = 0;
4446 } else {
4447 rv = kstrtouint(buf, 10, &max);
4448 if (rv < 0)
4449 return rv;
4450 if (max == 0)
4451 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004452 }
NeilBrown88202a02006-01-06 00:21:36 -08004453 mddev->sync_speed_max = max;
4454 return len;
4455}
4456
4457static struct md_sysfs_entry md_sync_max =
4458__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4459
Iustin Popd7f3d292007-10-16 23:30:54 -07004460static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004461degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004462{
4463 return sprintf(page, "%d\n", mddev->degraded);
4464}
4465static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004466
4467static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004468sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004469{
4470 return sprintf(page, "%d\n", mddev->parallel_resync);
4471}
4472
4473static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004474sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004475{
4476 long n;
4477
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004478 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004479 return -EINVAL;
4480
4481 if (n != 0 && n != 1)
4482 return -EINVAL;
4483
4484 mddev->parallel_resync = n;
4485
4486 if (mddev->sync_thread)
4487 wake_up(&resync_wait);
4488
4489 return len;
4490}
4491
4492/* force parallel resync, even with shared block devices */
4493static struct md_sysfs_entry md_sync_force_parallel =
4494__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4495 sync_force_parallel_show, sync_force_parallel_store);
4496
4497static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004498sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004499{
4500 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004501 if (mddev->curr_resync == 0)
4502 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004503 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4504 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004505 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004506 db = resync - mddev->resync_mark_cnt;
4507 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004508}
4509
NeilBrown80ca3a42006-07-10 04:44:18 -07004510static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004511
4512static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004513sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004514{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004515 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004516
NeilBrownacb180b2009-04-14 16:28:34 +10004517 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4518 return sprintf(page, "none\n");
4519
NeilBrown72f36d52012-10-11 14:25:57 +11004520 if (mddev->curr_resync == 1 ||
4521 mddev->curr_resync == 2)
4522 return sprintf(page, "delayed\n");
4523
NeilBrownc804cde2012-05-21 09:28:33 +10004524 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4525 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004526 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004527 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004528 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004529
NeilBrownacb180b2009-04-14 16:28:34 +10004530 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004531 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004532}
4533
NeilBrown750f1992014-09-30 08:53:05 +10004534static struct md_sysfs_entry md_sync_completed =
4535 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004536
NeilBrowne464eaf2006-03-27 01:18:14 -08004537static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004538min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004539{
4540 return sprintf(page, "%llu\n",
4541 (unsigned long long)mddev->resync_min);
4542}
4543static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004544min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004545{
4546 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004547 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004548
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004549 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004550 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004551
4552 spin_lock(&mddev->lock);
4553 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004554 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004555 goto out_unlock;
4556
4557 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004558 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004559 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004560
NeilBrown50c37b12015-03-23 17:36:38 +11004561 /* Round down to multiple of 4K for safety */
4562 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004563 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004564
NeilBrown23da4222014-12-15 12:57:01 +11004565out_unlock:
4566 spin_unlock(&mddev->lock);
4567 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004568}
4569
4570static struct md_sysfs_entry md_min_sync =
4571__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4572
4573static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004574max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004575{
4576 if (mddev->resync_max == MaxSector)
4577 return sprintf(page, "max\n");
4578 else
4579 return sprintf(page, "%llu\n",
4580 (unsigned long long)mddev->resync_max);
4581}
4582static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004583max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004584{
NeilBrown23da4222014-12-15 12:57:01 +11004585 int err;
4586 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004587 if (strncmp(buf, "max", 3) == 0)
4588 mddev->resync_max = MaxSector;
4589 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004590 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004591 int chunk;
4592
4593 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004594 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004595 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004596 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004597 goto out_unlock;
4598
4599 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004600 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004601 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004602 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004603 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004604
4605 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004606 chunk = mddev->chunk_sectors;
4607 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004608 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004609
4610 err = -EINVAL;
4611 if (sector_div(temp, chunk))
4612 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004613 }
4614 mddev->resync_max = max;
4615 }
4616 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004617 err = 0;
4618out_unlock:
4619 spin_unlock(&mddev->lock);
4620 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004621}
4622
4623static struct md_sysfs_entry md_max_sync =
4624__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4625
4626static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004627suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004628{
4629 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4630}
4631
4632static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004633suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004634{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004635 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004636 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004637
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004638 err = kstrtoull(buf, 10, &new);
4639 if (err < 0)
4640 return err;
4641 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004642 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004643
NeilBrown67918752014-12-15 12:57:01 +11004644 err = mddev_lock(mddev);
4645 if (err)
4646 return err;
4647 err = -EINVAL;
4648 if (mddev->pers == NULL ||
4649 mddev->pers->quiesce == NULL)
4650 goto unlock;
4651 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004652 mddev->suspend_lo = new;
4653 if (new >= old)
4654 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004655 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004656 else {
4657 /* Expanding suspended region - need to wait */
4658 mddev->pers->quiesce(mddev, 1);
4659 mddev->pers->quiesce(mddev, 0);
4660 }
NeilBrown67918752014-12-15 12:57:01 +11004661 err = 0;
4662unlock:
4663 mddev_unlock(mddev);
4664 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004665}
4666static struct md_sysfs_entry md_suspend_lo =
4667__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4668
NeilBrowne464eaf2006-03-27 01:18:14 -08004669static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004670suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004671{
4672 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4673}
4674
4675static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004676suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004677{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004678 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004679 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004680
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004681 err = kstrtoull(buf, 10, &new);
4682 if (err < 0)
4683 return err;
4684 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004685 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004686
NeilBrown67918752014-12-15 12:57:01 +11004687 err = mddev_lock(mddev);
4688 if (err)
4689 return err;
4690 err = -EINVAL;
4691 if (mddev->pers == NULL ||
4692 mddev->pers->quiesce == NULL)
4693 goto unlock;
4694 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004695 mddev->suspend_hi = new;
4696 if (new <= old)
4697 /* Shrinking suspended region */
4698 mddev->pers->quiesce(mddev, 2);
4699 else {
4700 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004701 mddev->pers->quiesce(mddev, 1);
4702 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004703 }
NeilBrown67918752014-12-15 12:57:01 +11004704 err = 0;
4705unlock:
4706 mddev_unlock(mddev);
4707 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004708}
4709static struct md_sysfs_entry md_suspend_hi =
4710__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4711
NeilBrown08a02ec2007-05-09 02:35:38 -07004712static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004713reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004714{
4715 if (mddev->reshape_position != MaxSector)
4716 return sprintf(page, "%llu\n",
4717 (unsigned long long)mddev->reshape_position);
4718 strcpy(page, "none\n");
4719 return 5;
4720}
4721
4722static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004723reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004724{
NeilBrownc6563a82012-05-21 09:27:00 +10004725 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004726 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004727 int err;
NeilBrown67918752014-12-15 12:57:01 +11004728
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004729 err = kstrtoull(buf, 10, &new);
4730 if (err < 0)
4731 return err;
4732 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004733 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004734 err = mddev_lock(mddev);
4735 if (err)
4736 return err;
4737 err = -EBUSY;
4738 if (mddev->pers)
4739 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004740 mddev->reshape_position = new;
4741 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004742 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004743 mddev->new_level = mddev->level;
4744 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004745 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004746 rdev_for_each(rdev, mddev)
4747 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004748 err = 0;
4749unlock:
4750 mddev_unlock(mddev);
4751 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004752}
4753
4754static struct md_sysfs_entry md_reshape_position =
4755__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4756 reshape_position_store);
4757
Dan Williamsb522adc2009-03-31 15:00:31 +11004758static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004759reshape_direction_show(struct mddev *mddev, char *page)
4760{
4761 return sprintf(page, "%s\n",
4762 mddev->reshape_backwards ? "backwards" : "forwards");
4763}
4764
4765static ssize_t
4766reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4767{
4768 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004769 int err;
4770
NeilBrown2c810cd2012-05-21 09:27:00 +10004771 if (cmd_match(buf, "forwards"))
4772 backwards = 0;
4773 else if (cmd_match(buf, "backwards"))
4774 backwards = 1;
4775 else
4776 return -EINVAL;
4777 if (mddev->reshape_backwards == backwards)
4778 return len;
4779
NeilBrown67918752014-12-15 12:57:01 +11004780 err = mddev_lock(mddev);
4781 if (err)
4782 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004783 /* check if we are allowed to change */
4784 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004785 err = -EBUSY;
4786 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004787 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004788 err = -EINVAL;
4789 else
4790 mddev->reshape_backwards = backwards;
4791 mddev_unlock(mddev);
4792 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004793}
4794
4795static struct md_sysfs_entry md_reshape_direction =
4796__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4797 reshape_direction_store);
4798
4799static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004800array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004801{
4802 if (mddev->external_size)
4803 return sprintf(page, "%llu\n",
4804 (unsigned long long)mddev->array_sectors/2);
4805 else
4806 return sprintf(page, "default\n");
4807}
4808
4809static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004810array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004811{
4812 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004813 int err;
4814
4815 err = mddev_lock(mddev);
4816 if (err)
4817 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004818
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004819 /* cluster raid doesn't support change array_sectors */
4820 if (mddev_is_clustered(mddev))
4821 return -EINVAL;
4822
Dan Williamsb522adc2009-03-31 15:00:31 +11004823 if (strncmp(buf, "default", 7) == 0) {
4824 if (mddev->pers)
4825 sectors = mddev->pers->size(mddev, 0, 0);
4826 else
4827 sectors = mddev->array_sectors;
4828
4829 mddev->external_size = 0;
4830 } else {
4831 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004832 err = -EINVAL;
4833 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4834 err = -E2BIG;
4835 else
4836 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004837 }
4838
NeilBrown67918752014-12-15 12:57:01 +11004839 if (!err) {
4840 mddev->array_sectors = sectors;
4841 if (mddev->pers) {
4842 set_capacity(mddev->gendisk, mddev->array_sectors);
4843 revalidate_disk(mddev->gendisk);
4844 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004845 }
NeilBrown67918752014-12-15 12:57:01 +11004846 mddev_unlock(mddev);
4847 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004848}
4849
4850static struct md_sysfs_entry md_array_size =
4851__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4852 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004853
NeilBrowneae17012005-11-08 21:39:23 -08004854static struct attribute *md_default_attrs[] = {
4855 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004856 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004857 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004858 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004859 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004860 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004861 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004862 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004863 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004864 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004865 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004866 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004867 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004868 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004869 NULL,
4870};
4871
4872static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004873 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004874 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004875 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004876 &md_sync_min.attr,
4877 &md_sync_max.attr,
4878 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004879 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004880 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004881 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004882 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004883 &md_suspend_lo.attr,
4884 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004885 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004886 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004887 NULL,
4888};
NeilBrown411036f2005-11-08 21:39:40 -08004889static struct attribute_group md_redundancy_group = {
4890 .name = NULL,
4891 .attrs = md_redundancy_attrs,
4892};
4893
NeilBrowneae17012005-11-08 21:39:23 -08004894static ssize_t
4895md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4896{
4897 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004898 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004899 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004900
4901 if (!entry->show)
4902 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004903 spin_lock(&all_mddevs_lock);
4904 if (list_empty(&mddev->all_mddevs)) {
4905 spin_unlock(&all_mddevs_lock);
4906 return -EBUSY;
4907 }
4908 mddev_get(mddev);
4909 spin_unlock(&all_mddevs_lock);
4910
NeilBrownb7b17c92014-12-15 12:56:59 +11004911 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004912 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004913 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004914}
4915
4916static ssize_t
4917md_attr_store(struct kobject *kobj, struct attribute *attr,
4918 const char *page, size_t length)
4919{
4920 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004921 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004922 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004923
4924 if (!entry->store)
4925 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004926 if (!capable(CAP_SYS_ADMIN))
4927 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004928 spin_lock(&all_mddevs_lock);
4929 if (list_empty(&mddev->all_mddevs)) {
4930 spin_unlock(&all_mddevs_lock);
4931 return -EBUSY;
4932 }
4933 mddev_get(mddev);
4934 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004935 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004936 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004937 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004938}
4939
4940static void md_free(struct kobject *ko)
4941{
NeilBrownfd01b882011-10-11 16:47:53 +11004942 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004943
4944 if (mddev->sysfs_state)
4945 sysfs_put(mddev->sysfs_state);
4946
NeilBrown6cd18e72015-04-27 14:12:22 +10004947 if (mddev->queue)
4948 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004949 if (mddev->gendisk) {
4950 del_gendisk(mddev->gendisk);
4951 put_disk(mddev->gendisk);
4952 }
NeilBrowna21d1502009-01-09 08:31:09 +11004953
NeilBrowneae17012005-11-08 21:39:23 -08004954 kfree(mddev);
4955}
4956
Emese Revfy52cf25d2010-01-19 02:58:23 +01004957static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004958 .show = md_attr_show,
4959 .store = md_attr_store,
4960};
4961static struct kobj_type md_ktype = {
4962 .release = md_free,
4963 .sysfs_ops = &md_sysfs_ops,
4964 .default_attrs = md_default_attrs,
4965};
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967int mdp_major = 0;
4968
Dan Williams5fd3a172009-03-04 00:57:25 -07004969static void mddev_delayed_delete(struct work_struct *ws)
4970{
NeilBrownfd01b882011-10-11 16:47:53 +11004971 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004972
NeilBrown43a70502009-12-14 12:49:55 +11004973 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004974 kobject_del(&mddev->kobj);
4975 kobject_put(&mddev->kobj);
4976}
4977
NeilBrownefeb53c2009-01-09 08:31:10 +11004978static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004980 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004981 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004983 int partitioned;
4984 int shift;
4985 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004986 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
4988 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004989 return -ENODEV;
4990
4991 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4992 shift = partitioned ? MdpMinorShift : 0;
4993 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Tejun Heoe804ac72010-10-15 15:36:08 +02004995 /* wait for any previous instance of this device to be
4996 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004997 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004998 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004999
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005000 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005001 error = -EEXIST;
5002 if (mddev->gendisk)
5003 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005004
5005 if (name) {
5006 /* Need to ensure that 'name' is not a duplicate.
5007 */
NeilBrownfd01b882011-10-11 16:47:53 +11005008 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005009 spin_lock(&all_mddevs_lock);
5010
5011 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5012 if (mddev2->gendisk &&
5013 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5014 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005015 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005016 }
5017 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 }
NeilBrown8b765392009-01-09 08:31:08 +11005019
NeilBrown0909dc42009-07-01 12:27:21 +10005020 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005021 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005022 if (!mddev->queue)
5023 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005024 mddev->queue->queuedata = mddev;
5025
NeilBrown409c57f2009-03-31 14:39:39 +11005026 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005027 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 disk = alloc_disk(1 << shift);
5030 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005031 blk_cleanup_queue(mddev->queue);
5032 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005033 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005035 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005037 if (name)
5038 strcpy(disk->disk_name, name);
5039 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005041 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 disk->fops = &md_fops;
5044 disk->private_data = mddev;
5045 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005046 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005047 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005048 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005049 * remove it now.
5050 */
5051 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005053 /* As soon as we call add_disk(), another thread could get
5054 * through to md_open, so make sure it doesn't get too far
5055 */
5056 mutex_lock(&mddev->open_mutex);
5057 add_disk(disk);
5058
Tejun Heoed9e1982008-08-25 19:56:05 +09005059 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5060 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005061 if (error) {
5062 /* This isn't possible, but as kobject_init_and_add is marked
5063 * __must_check, we must do something with the result
5064 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005065 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5066 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005067 error = 0;
5068 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005069 if (mddev->kobj.sd &&
5070 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11005071 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005072 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005073 abort:
5074 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005075 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005076 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005077 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005078 }
NeilBrownd3374822009-01-09 08:31:10 +11005079 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005080 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005081}
5082
5083static struct kobject *md_probe(dev_t dev, int *part, void *data)
5084{
5085 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 return NULL;
5087}
5088
NeilBrownefeb53c2009-01-09 08:31:10 +11005089static int add_named_array(const char *val, struct kernel_param *kp)
5090{
5091 /* val must be "md_*" where * is not all digits.
5092 * We allocate an array with a large free minor number, and
5093 * set the name to val. val must not already be an active name.
5094 */
5095 int len = strlen(val);
5096 char buf[DISK_NAME_LEN];
5097
5098 while (len && val[len-1] == '\n')
5099 len--;
5100 if (len >= DISK_NAME_LEN)
5101 return -E2BIG;
5102 strlcpy(buf, val, len+1);
5103 if (strncmp(buf, "md_", 3) != 0)
5104 return -EINVAL;
5105 return md_alloc(0, buf);
5106}
5107
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108static void md_safemode_timeout(unsigned long data)
5109{
NeilBrownfd01b882011-10-11 16:47:53 +11005110 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
Neil Brown0fd62b82008-06-28 08:31:36 +10005112 if (!atomic_read(&mddev->writes_pending)) {
5113 mddev->safemode = 1;
5114 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005115 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 md_wakeup_thread(mddev->thread);
5118}
5119
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005120static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
NeilBrownfd01b882011-10-11 16:47:53 +11005122int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
NeilBrown2604b702006-01-06 00:20:36 -08005124 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005125 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005126 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
NeilBrowna757e642005-04-16 15:26:42 -07005128 if (list_empty(&mddev->disks))
5129 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
5132 if (mddev->pers)
5133 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005134 /* Cannot run until previous stop completes properly */
5135 if (mddev->sysfs_active)
5136 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005137
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 /*
5139 * Analyze all RAID superblock(s)
5140 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005141 if (!mddev->raid_disks) {
5142 if (!mddev->persistent)
5143 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005144 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
NeilBrownd9d166c2006-01-06 00:20:51 -08005147 if (mddev->level != LEVEL_NONE)
5148 request_module("md-level-%d", mddev->level);
5149 else if (mddev->clevel[0])
5150 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
5152 /*
5153 * Drop all container device buffers, from now on
5154 * the only valid external interface is through the md
5155 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005157 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005158 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 continue;
5160 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005161 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005162
5163 /* perform some consistency tests on the device.
5164 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005165 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005166 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005167 if (rdev->meta_bdev) {
5168 /* Nothing to check */;
5169 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005170 if (mddev->dev_sectors &&
5171 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005172 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005173 printk("md: %s: data overlaps metadata\n",
5174 mdname(mddev));
5175 return -EINVAL;
5176 }
5177 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005178 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005179 > rdev->data_offset) {
5180 printk("md: %s: metadata overlaps data\n",
5181 mdname(mddev));
5182 return -EINVAL;
5183 }
5184 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005185 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 }
5187
NeilBrowna167f662010-10-26 18:31:13 +11005188 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005189 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005190
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005192 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005193 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005195 if (mddev->level != LEVEL_NONE)
5196 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5197 mddev->level);
5198 else
5199 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5200 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 return -EINVAL;
5202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005204 if (mddev->level != pers->level) {
5205 mddev->level = pers->level;
5206 mddev->new_level = pers->level;
5207 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005208 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
NeilBrownf6705572006-03-27 01:18:11 -08005210 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005211 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005212 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005213 module_put(pers->owner);
5214 return -EINVAL;
5215 }
5216
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005217 if (pers->sync_request) {
5218 /* Warn if this is a potentially silly
5219 * configuration.
5220 */
5221 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005222 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005223 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005224
NeilBrowndafb20f2012-03-19 12:46:39 +11005225 rdev_for_each(rdev, mddev)
5226 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005227 if (rdev < rdev2 &&
5228 rdev->bdev->bd_contains ==
5229 rdev2->bdev->bd_contains) {
5230 printk(KERN_WARNING
5231 "%s: WARNING: %s appears to be"
5232 " on the same physical disk as"
5233 " %s.\n",
5234 mdname(mddev),
5235 bdevname(rdev->bdev,b),
5236 bdevname(rdev2->bdev,b2));
5237 warned = 1;
5238 }
5239 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005240
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005241 if (warned)
5242 printk(KERN_WARNING
5243 "True protection against single-disk"
5244 " failure might be compromised.\n");
5245 }
5246
NeilBrown657390d2005-08-26 18:34:16 -07005247 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005248 /* may be over-ridden by personality */
5249 mddev->resync_max_sectors = mddev->dev_sectors;
5250
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005251 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
NeilBrown0f9552b52009-12-30 12:08:50 +11005253 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005254 mddev->ro = 2; /* read-only, but switch on first write */
5255
NeilBrown36d091f2014-12-15 12:56:58 +11005256 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005257 if (err)
5258 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005259 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005260 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5261 " but 'external_size' not in effect?\n", __func__);
5262 printk(KERN_ERR
5263 "md: invalid array_size %llu > default size %llu\n",
5264 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005265 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005266 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005267 }
NeilBrown36d091f2014-12-15 12:56:58 +11005268 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005269 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005270 struct bitmap *bitmap;
5271
5272 bitmap = bitmap_create(mddev, -1);
5273 if (IS_ERR(bitmap)) {
5274 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005275 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5276 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005277 } else
5278 mddev->bitmap = bitmap;
5279
NeilBrownb15c2e52006-01-06 00:20:16 -08005280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005282 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005283 if (mddev->private)
5284 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005285 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005286 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005287 bitmap_destroy(mddev);
5288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 }
NeilBrown5c675f82014-12-15 12:56:56 +11005290 if (mddev->queue) {
5291 mddev->queue->backing_dev_info.congested_data = mddev;
5292 mddev->queue->backing_dev_info.congested_fn = md_congested;
5293 }
NeilBrown36d091f2014-12-15 12:56:58 +11005294 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005295 if (mddev->kobj.sd &&
5296 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005297 printk(KERN_WARNING
5298 "md: cannot register extra attributes for %s\n",
5299 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005300 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005301 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005302 mddev->ro = 0;
5303
NeilBrownf72ffdd2014-09-30 14:23:59 +10005304 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005305 atomic_set(&mddev->max_corr_read_errors,
5306 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005308 if (mddev_is_clustered(mddev))
5309 mddev->safemode_delay = 0;
5310 else
5311 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005313 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005314 spin_lock(&mddev->lock);
5315 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005316 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005317 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005318 if (rdev->raid_disk >= 0)
5319 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005320 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005321
NeilBrowna4a3d262015-07-17 11:57:30 +10005322 if (mddev->degraded && !mddev->ro)
5323 /* This ensures that recovering status is reported immediately
5324 * via sysfs - until a lack of spares is confirmed.
5325 */
5326 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005328
NeilBrown7a0a5352013-08-27 16:28:23 +10005329 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005330 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
NeilBrownd7603b72006-01-06 00:20:30 -08005332 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005333 sysfs_notify_dirent_safe(mddev->sysfs_state);
5334 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005335 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 return 0;
5337}
NeilBrown390ee602010-06-01 19:37:27 +10005338EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
NeilBrownfd01b882011-10-11 16:47:53 +11005340static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005341{
5342 int err;
5343
5344 err = md_run(mddev);
5345 if (err)
5346 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005347 err = bitmap_load(mddev);
5348 if (err) {
5349 bitmap_destroy(mddev);
5350 goto out;
5351 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005352
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005353 if (mddev_is_clustered(mddev))
5354 md_allow_write(mddev);
5355
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005356 md_wakeup_thread(mddev->thread);
5357 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5358
NeilBrownfe60b012010-03-29 11:10:42 +11005359 set_capacity(mddev->gendisk, mddev->array_sectors);
5360 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005361 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005362 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5363out:
5364 return err;
5365}
5366
NeilBrownfd01b882011-10-11 16:47:53 +11005367static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368{
5369 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
Andre Noll80fab1d2008-07-11 22:02:21 +10005371 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005373 return -ENXIO;
5374 if (!mddev->pers)
5375 return -EINVAL;
5376 if (!mddev->ro)
5377 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005378 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5379 struct md_rdev *rdev;
5380 bool has_journal = false;
5381
5382 rcu_read_lock();
5383 rdev_for_each_rcu(rdev, mddev) {
5384 if (test_bit(Journal, &rdev->flags) &&
5385 !test_bit(Faulty, &rdev->flags)) {
5386 has_journal = true;
5387 break;
5388 }
5389 }
5390 rcu_read_unlock();
5391
5392 /* Don't restart rw with journal missing/faulty */
5393 if (!has_journal)
5394 return -EINVAL;
5395 }
5396
Andre Noll80fab1d2008-07-11 22:02:21 +10005397 mddev->safemode = 0;
5398 mddev->ro = 0;
5399 set_disk_ro(disk, 0);
5400 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5401 mdname(mddev));
5402 /* Kick recovery or resync if necessary */
5403 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5404 md_wakeup_thread(mddev->thread);
5405 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005406 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408}
5409
NeilBrownfd01b882011-10-11 16:47:53 +11005410static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005411{
5412 mddev->array_sectors = 0;
5413 mddev->external_size = 0;
5414 mddev->dev_sectors = 0;
5415 mddev->raid_disks = 0;
5416 mddev->recovery_cp = 0;
5417 mddev->resync_min = 0;
5418 mddev->resync_max = MaxSector;
5419 mddev->reshape_position = MaxSector;
5420 mddev->external = 0;
5421 mddev->persistent = 0;
5422 mddev->level = LEVEL_NONE;
5423 mddev->clevel[0] = 0;
5424 mddev->flags = 0;
5425 mddev->ro = 0;
5426 mddev->metadata_type[0] = 0;
5427 mddev->chunk_sectors = 0;
5428 mddev->ctime = mddev->utime = 0;
5429 mddev->layout = 0;
5430 mddev->max_disks = 0;
5431 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005432 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005433 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005434 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005435 mddev->new_level = LEVEL_NONE;
5436 mddev->new_layout = 0;
5437 mddev->new_chunk_sectors = 0;
5438 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005439 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005440 mddev->suspend_lo = mddev->suspend_hi = 0;
5441 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5442 mddev->recovery = 0;
5443 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005444 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005445 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005446 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005447 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005448 mddev->bitmap_info.offset = 0;
5449 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005450 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005451 mddev->bitmap_info.chunksize = 0;
5452 mddev->bitmap_info.daemon_sleep = 0;
5453 mddev->bitmap_info.max_write_behind = 0;
5454}
5455
NeilBrownfd01b882011-10-11 16:47:53 +11005456static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005457{
NeilBrown6b6204e2013-05-09 09:48:30 +10005458 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005459 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005460 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005461 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005462 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005463 }
5464
5465 del_timer_sync(&mddev->safemode_timer);
5466
5467 bitmap_flush(mddev);
5468 md_super_wait(mddev);
5469
NeilBrownb6d428c2013-04-24 11:42:42 +10005470 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005471 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5472 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005473 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005474 if (!mddev_is_clustered(mddev))
5475 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005476 md_update_sb(mddev, 1);
5477 }
5478}
NeilBrowndefad612011-01-14 09:14:33 +11005479
NeilBrownfd01b882011-10-11 16:47:53 +11005480void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005481{
NeilBrown29f097c2013-11-14 17:54:51 +11005482 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005483 __md_stop_writes(mddev);
5484 mddev_unlock(mddev);
5485}
NeilBrown390ee602010-06-01 19:37:27 +10005486EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005487
NeilBrown5aa61f42014-12-15 12:56:57 +11005488static void mddev_detach(struct mddev *mddev)
5489{
5490 struct bitmap *bitmap = mddev->bitmap;
5491 /* wait for behind writes to complete */
5492 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5493 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5494 mdname(mddev));
5495 /* need to kick something here to make sure I/O goes? */
5496 wait_event(bitmap->behind_wait,
5497 atomic_read(&bitmap->behind_writes) == 0);
5498 }
NeilBrown36d091f2014-12-15 12:56:58 +11005499 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005500 mddev->pers->quiesce(mddev, 1);
5501 mddev->pers->quiesce(mddev, 0);
5502 }
5503 md_unregister_thread(&mddev->thread);
5504 if (mddev->queue)
5505 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5506}
5507
NeilBrown5eff3c42012-11-19 10:47:48 +11005508static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005509{
NeilBrown36d091f2014-12-15 12:56:58 +11005510 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005511 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005512 /* Ensure ->event_work is done */
5513 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005514 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005515 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005516 spin_unlock(&mddev->lock);
5517 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005518 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005519 if (pers->sync_request && mddev->to_remove == NULL)
5520 mddev->to_remove = &md_redundancy_group;
5521 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005522 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005523}
NeilBrown5eff3c42012-11-19 10:47:48 +11005524
5525void md_stop(struct mddev *mddev)
5526{
5527 /* stop the array and free an attached data structures.
5528 * This is called from dm-raid
5529 */
5530 __md_stop(mddev);
5531 bitmap_destroy(mddev);
5532 if (mddev->bio_set)
5533 bioset_free(mddev->bio_set);
5534}
5535
NeilBrown390ee602010-06-01 19:37:27 +10005536EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005537
NeilBrowna05b7ea2012-07-19 15:59:18 +10005538static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005539{
5540 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005541 int did_freeze = 0;
5542
5543 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5544 did_freeze = 1;
5545 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5546 md_wakeup_thread(mddev->thread);
5547 }
NeilBrownf851b602014-12-11 10:02:10 +11005548 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005549 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005550 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005551 /* Thread might be blocked waiting for metadata update
5552 * which will now never happen */
5553 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005554
NeilBrown88724bf2015-09-24 14:00:51 +10005555 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5556 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005557 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005558 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5559 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005560 wait_event(mddev->sb_wait,
5561 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005562 mddev_lock_nointr(mddev);
5563
NeilBrowna4bd82d2010-03-29 13:23:10 +11005564 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005565 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005566 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005567 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005568 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005569 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005570 if (did_freeze) {
5571 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005572 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005573 md_wakeup_thread(mddev->thread);
5574 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005575 err = -EBUSY;
5576 goto out;
5577 }
5578 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005579 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005580
5581 err = -ENXIO;
5582 if (mddev->ro==1)
5583 goto out;
5584 mddev->ro = 1;
5585 set_disk_ro(mddev->gendisk, 1);
5586 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005587 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5588 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005589 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005590 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005591 }
5592out:
5593 mutex_unlock(&mddev->open_mutex);
5594 return err;
5595}
5596
NeilBrown9e653b62006-06-26 00:27:58 -07005597/* mode:
5598 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005599 * 2 - stop but do not disassemble array
5600 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005601static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005602 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005605 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005606 int did_freeze = 0;
5607
5608 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5609 did_freeze = 1;
5610 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5611 md_wakeup_thread(mddev->thread);
5612 }
NeilBrownf851b602014-12-11 10:02:10 +11005613 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005614 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005615 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005616 /* Thread might be blocked waiting for metadata update
5617 * which will now never happen */
5618 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005619
NeilBrown30b8feb2013-11-14 15:16:17 +11005620 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005621 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5622 !test_bit(MD_RECOVERY_RUNNING,
5623 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005624 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
NeilBrownc8c00a62009-08-10 12:50:52 +10005626 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005627 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005628 mddev->sysfs_active ||
5629 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005630 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005631 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005632 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005633 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005634 if (did_freeze) {
5635 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005636 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005637 md_wakeup_thread(mddev->thread);
5638 }
NeilBrown260fa032013-08-27 16:44:13 +10005639 return -EBUSY;
5640 }
NeilBrown6e17b022010-08-07 21:41:19 +10005641 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005642 if (mddev->ro)
5643 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005644
NeilBrowndefad612011-01-14 09:14:33 +11005645 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005646 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005647 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005648
NeilBrowna4bd82d2010-03-29 13:23:10 +11005649 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005650 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005651
NeilBrowndafb20f2012-03-19 12:46:39 +11005652 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005653 if (rdev->raid_disk >= 0)
5654 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005655
NeilBrowna4bd82d2010-03-29 13:23:10 +11005656 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005657 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005658 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005659 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005660
NeilBrowna4bd82d2010-03-29 13:23:10 +11005661 if (mddev->ro)
5662 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005663 } else
5664 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 /*
5666 * Free resources if final stop
5667 */
NeilBrown9e653b62006-06-26 00:27:58 -07005668 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5670
NeilBrown978f9462006-02-02 14:28:05 -08005671 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005672 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005673 struct file *f = mddev->bitmap_info.file;
5674 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005675 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005676 spin_unlock(&mddev->lock);
5677 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005678 }
NeilBrownc3d97142009-12-14 12:49:52 +11005679 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005680
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 export_array(mddev);
5682
NeilBrown6177b472010-03-29 11:37:13 +11005683 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005684 if (mddev->hold_active == UNTIL_STOP)
5685 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005686 }
NeilBrownd7603b72006-01-06 00:20:30 -08005687 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005688 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690}
5691
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005692#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005693static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694{
NeilBrown3cb03002011-10-11 16:45:26 +11005695 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 int err;
5697
NeilBrowna757e642005-04-16 15:26:42 -07005698 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
5701 printk(KERN_INFO "md: running: ");
5702
NeilBrowndafb20f2012-03-19 12:46:39 +11005703 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 char b[BDEVNAME_SIZE];
5705 printk("<%s>", bdevname(rdev->bdev,b));
5706 }
5707 printk("\n");
5708
NeilBrownd710e132008-10-13 11:55:12 +11005709 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 if (err) {
5711 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005712 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 }
5714}
5715
5716/*
5717 * lets try to run arrays based on all disks that have arrived
5718 * until now. (those are in pending_raid_disks)
5719 *
5720 * the method: pick the first pending disk, collect all disks with
5721 * the same UUID, remove all from the pending list and put them into
5722 * the 'same_array' list. Then order this list based on superblock
5723 * update time (freshest comes first), kick out 'old' disks and
5724 * compare superblocks. If everything's fine then run it.
5725 *
5726 * If "unit" is allocated, then bump its reference count
5727 */
5728static void autorun_devices(int part)
5729{
NeilBrown3cb03002011-10-11 16:45:26 +11005730 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005731 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 char b[BDEVNAME_SIZE];
5733
5734 printk(KERN_INFO "md: autorun ...\n");
5735 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005736 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005738 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005740 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
5742 printk(KERN_INFO "md: considering %s ...\n",
5743 bdevname(rdev0->bdev,b));
5744 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005745 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 if (super_90_load(rdev, rdev0, 0) >= 0) {
5747 printk(KERN_INFO "md: adding %s ...\n",
5748 bdevname(rdev->bdev,b));
5749 list_move(&rdev->same_set, &candidates);
5750 }
5751 /*
5752 * now we have a set of devices, with all of them having
5753 * mostly sane superblocks. It's time to allocate the
5754 * mddev.
5755 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005756 if (part) {
5757 dev = MKDEV(mdp_major,
5758 rdev0->preferred_minor << MdpMinorShift);
5759 unit = MINOR(dev) >> MdpMinorShift;
5760 } else {
5761 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5762 unit = MINOR(dev);
5763 }
5764 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5766 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5767 break;
5768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
5770 md_probe(dev, NULL, NULL);
5771 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005772 if (!mddev || !mddev->gendisk) {
5773 if (mddev)
5774 mddev_put(mddev);
5775 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 "md: cannot allocate memory for md drive.\n");
5777 break;
5778 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005779 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 printk(KERN_WARNING "md: %s locked, cannot run\n",
5781 mdname(mddev));
5782 else if (mddev->raid_disks || mddev->major_version
5783 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005784 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 "md: %s already running, cannot run %s\n",
5786 mdname(mddev), bdevname(rdev0->bdev,b));
5787 mddev_unlock(mddev);
5788 } else {
5789 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005790 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005791 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 list_del_init(&rdev->same_set);
5793 if (bind_rdev_to_array(rdev, mddev))
5794 export_rdev(rdev);
5795 }
5796 autorun_array(mddev);
5797 mddev_unlock(mddev);
5798 }
5799 /* on success, candidates will be empty, on error
5800 * it won't...
5801 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005802 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005803 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 mddev_put(mddev);
5807 }
5808 printk(KERN_INFO "md: ... autorun DONE.\n");
5809}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005810#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
NeilBrownf72ffdd2014-09-30 14:23:59 +10005812static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813{
5814 mdu_version_t ver;
5815
5816 ver.major = MD_MAJOR_VERSION;
5817 ver.minor = MD_MINOR_VERSION;
5818 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5819
5820 if (copy_to_user(arg, &ver, sizeof(ver)))
5821 return -EFAULT;
5822
5823 return 0;
5824}
5825
NeilBrownf72ffdd2014-09-30 14:23:59 +10005826static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827{
5828 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005829 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005830 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
NeilBrown1ca69c42012-10-11 13:37:33 +11005832 nr = working = insync = failed = spare = 0;
5833 rcu_read_lock();
5834 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005836 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 failed++;
5838 else {
5839 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005840 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005841 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 else
5843 spare++;
5844 }
5845 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005846 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847
5848 info.major_version = mddev->major_version;
5849 info.minor_version = mddev->minor_version;
5850 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005851 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005853 info.size = mddev->dev_sectors / 2;
5854 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005855 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 info.nr_disks = nr;
5857 info.raid_disks = mddev->raid_disks;
5858 info.md_minor = mddev->md_minor;
5859 info.not_persistent= !mddev->persistent;
5860
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005861 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 info.state = 0;
5863 if (mddev->in_sync)
5864 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005865 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005866 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005867 if (mddev_is_clustered(mddev))
5868 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005869 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 info.working_disks = working;
5871 info.failed_disks = failed;
5872 info.spare_disks = spare;
5873
5874 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005875 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
5877 if (copy_to_user(arg, &info, sizeof(info)))
5878 return -EFAULT;
5879
5880 return 0;
5881}
5882
NeilBrownf72ffdd2014-09-30 14:23:59 +10005883static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005884{
5885 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005886 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005887 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005888
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005889 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005890 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005891 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005892
NeilBrown32a76272005-06-21 17:17:14 -07005893 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005894 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005895 /* bitmap enabled */
5896 if (mddev->bitmap_info.file) {
5897 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5898 sizeof(file->pathname));
5899 if (IS_ERR(ptr))
5900 err = PTR_ERR(ptr);
5901 else
5902 memmove(file->pathname, ptr,
5903 sizeof(file->pathname)-(ptr-file->pathname));
5904 }
NeilBrown4af1a042014-12-15 12:57:00 +11005905 spin_unlock(&mddev->lock);
5906
5907 if (err == 0 &&
5908 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005909 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005910
NeilBrown32a76272005-06-21 17:17:14 -07005911 kfree(file);
5912 return err;
5913}
5914
NeilBrownf72ffdd2014-09-30 14:23:59 +10005915static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916{
5917 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005918 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
5920 if (copy_from_user(&info, arg, sizeof(info)))
5921 return -EFAULT;
5922
NeilBrown1ca69c42012-10-11 13:37:33 +11005923 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005924 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 if (rdev) {
5926 info.major = MAJOR(rdev->bdev->bd_dev);
5927 info.minor = MINOR(rdev->bdev->bd_dev);
5928 info.raid_disk = rdev->raid_disk;
5929 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005930 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005932 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 info.state |= (1<<MD_DISK_ACTIVE);
5934 info.state |= (1<<MD_DISK_SYNC);
5935 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005936 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005937 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005938 if (test_bit(WriteMostly, &rdev->flags))
5939 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 } else {
5941 info.major = info.minor = 0;
5942 info.raid_disk = -1;
5943 info.state = (1<<MD_DISK_REMOVED);
5944 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005945 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
5947 if (copy_to_user(arg, &info, sizeof(info)))
5948 return -EFAULT;
5949
5950 return 0;
5951}
5952
NeilBrownf72ffdd2014-09-30 14:23:59 +10005953static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954{
5955 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005956 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 dev_t dev = MKDEV(info->major,info->minor);
5958
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005959 if (mddev_is_clustered(mddev) &&
5960 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005961 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005962 mdname(mddev));
5963 return -EINVAL;
5964 }
5965
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5967 return -EOVERFLOW;
5968
5969 if (!mddev->raid_disks) {
5970 int err;
5971 /* expecting a device which has a superblock */
5972 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5973 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005974 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 "md: md_import_device returned %ld\n",
5976 PTR_ERR(rdev));
5977 return PTR_ERR(rdev);
5978 }
5979 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005980 struct md_rdev *rdev0
5981 = list_entry(mddev->disks.next,
5982 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005983 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 .load_super(rdev, rdev0, mddev->minor_version);
5985 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005986 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005988 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 bdevname(rdev0->bdev,b2));
5990 export_rdev(rdev);
5991 return -EINVAL;
5992 }
5993 }
5994 err = bind_rdev_to_array(rdev, mddev);
5995 if (err)
5996 export_rdev(rdev);
5997 return err;
5998 }
5999
6000 /*
6001 * add_new_disk can be used once the array is assembled
6002 * to add "hot spares". They must already have a superblock
6003 * written
6004 */
6005 if (mddev->pers) {
6006 int err;
6007 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006008 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 "%s: personality does not support diskops!\n",
6010 mdname(mddev));
6011 return -EINVAL;
6012 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006013 if (mddev->persistent)
6014 rdev = md_import_device(dev, mddev->major_version,
6015 mddev->minor_version);
6016 else
6017 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006019 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 "md: md_import_device returned %ld\n",
6021 PTR_ERR(rdev));
6022 return PTR_ERR(rdev);
6023 }
NeilBrown1a855a02010-12-09 16:36:28 +11006024 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006025 if (!mddev->persistent) {
6026 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006027 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006028 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006029 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006030 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006031 } else
NeilBrown41158c72005-06-21 17:17:25 -07006032 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006033 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006034 } else
6035 super_types[mddev->major_version].
6036 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006037 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006038 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006039 /* This was a hot-add request, but events doesn't
6040 * match, so reject it.
6041 */
6042 export_rdev(rdev);
6043 return -EINVAL;
6044 }
6045
NeilBrownb2d444d2005-11-08 21:39:31 -08006046 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006047 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6048 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006049 else
6050 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006051
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006052 if (info->state & (1<<MD_DISK_JOURNAL)) {
6053 struct md_rdev *rdev2;
6054 bool has_journal = false;
6055
6056 /* make sure no existing journal disk */
6057 rdev_for_each(rdev2, mddev) {
6058 if (test_bit(Journal, &rdev2->flags)) {
6059 has_journal = true;
6060 break;
6061 }
6062 }
6063 if (has_journal) {
6064 export_rdev(rdev);
6065 return -EBUSY;
6066 }
Song Liubac624f2015-08-13 14:31:55 -07006067 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006068 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006069 /*
6070 * check whether the device shows up in other nodes
6071 */
6072 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006073 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006074 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006075 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006076 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006077 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006078 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006079 export_rdev(rdev);
6080 return err;
6081 }
6082 }
6083 }
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 rdev->raid_disk = -1;
6086 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006087
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 if (err)
6089 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006090
6091 if (mddev_is_clustered(mddev)) {
6092 if (info->state & (1 << MD_DISK_CANDIDATE))
6093 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6094 else {
6095 if (err)
6096 md_cluster_ops->add_new_disk_cancel(mddev);
6097 else
6098 err = add_bound_rdev(rdev);
6099 }
6100
6101 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006102 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006103
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 return err;
6105 }
6106
6107 /* otherwise, add_new_disk is only allowed
6108 * for major_version==0 superblocks
6109 */
6110 if (mddev->major_version != 0) {
6111 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6112 mdname(mddev));
6113 return -EINVAL;
6114 }
6115
6116 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6117 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006118 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006120 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 "md: error, md_import_device() returned %ld\n",
6122 PTR_ERR(rdev));
6123 return PTR_ERR(rdev);
6124 }
6125 rdev->desc_nr = info->number;
6126 if (info->raid_disk < mddev->raid_disks)
6127 rdev->raid_disk = info->raid_disk;
6128 else
6129 rdev->raid_disk = -1;
6130
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006132 if (info->state & (1<<MD_DISK_SYNC))
6133 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006135 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6136 set_bit(WriteMostly, &rdev->flags);
6137
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 if (!mddev->persistent) {
6139 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006140 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6141 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006142 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006143 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
NeilBrown2bf071b2006-01-06 00:20:55 -08006145 err = bind_rdev_to_array(rdev, mddev);
6146 if (err) {
6147 export_rdev(rdev);
6148 return err;
6149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 }
6151
6152 return 0;
6153}
6154
NeilBrownf72ffdd2014-09-30 14:23:59 +10006155static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156{
6157 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006158 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 rdev = find_rdev(mddev, dev);
6161 if (!rdev)
6162 return -ENXIO;
6163
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006164 if (rdev->raid_disk < 0)
6165 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006166
NeilBrown3ea8929d2013-04-24 11:42:41 +10006167 clear_bit(Blocked, &rdev->flags);
6168 remove_and_add_spares(mddev, rdev);
6169
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 if (rdev->raid_disk >= 0)
6171 goto busy;
6172
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006173kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006174 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006175 md_cluster_ops->remove_disk(mddev, rdev);
6176
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006177 md_kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07006178 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006179 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
6181 return 0;
6182busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006183 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 bdevname(rdev->bdev,b), mdname(mddev));
6185 return -EBUSY;
6186}
6187
NeilBrownf72ffdd2014-09-30 14:23:59 +10006188static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189{
6190 char b[BDEVNAME_SIZE];
6191 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006192 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
6194 if (!mddev->pers)
6195 return -ENODEV;
6196
6197 if (mddev->major_version != 0) {
6198 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6199 " version-0 superblocks.\n",
6200 mdname(mddev));
6201 return -EINVAL;
6202 }
6203 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006204 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 "%s: personality does not support diskops!\n",
6206 mdname(mddev));
6207 return -EINVAL;
6208 }
6209
NeilBrownd710e132008-10-13 11:55:12 +11006210 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006212 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 "md: error, md_import_device() returned %ld\n",
6214 PTR_ERR(rdev));
6215 return -EINVAL;
6216 }
6217
6218 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006219 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006221 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222
NeilBrown8190e752009-06-18 08:48:58 +10006223 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
NeilBrownb2d444d2005-11-08 21:39:31 -08006225 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006226 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 "md: can not hot-add faulty %s disk to %s!\n",
6228 bdevname(rdev->bdev,b), mdname(mddev));
6229 err = -EINVAL;
6230 goto abort_export;
6231 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006232
NeilBrownb2d444d2005-11-08 21:39:31 -08006233 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006235 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006236 err = bind_rdev_to_array(rdev, mddev);
6237 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006238 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239
6240 /*
6241 * The rest should better be atomic, we can have disk failures
6242 * noticed in interrupt contexts ...
6243 */
6244
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 rdev->raid_disk = -1;
6246
NeilBrown850b2b422006-10-03 01:15:46 -07006247 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 /*
6249 * Kick recovery, maybe this spare has to be added to the
6250 * array immediately.
6251 */
6252 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6253 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006254 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 return 0;
6256
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257abort_export:
6258 export_rdev(rdev);
6259 return err;
6260}
6261
NeilBrownfd01b882011-10-11 16:47:53 +11006262static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006263{
NeilBrown035328c2014-04-09 12:25:40 +10006264 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006265
NeilBrown36fa3062005-09-09 16:23:45 -07006266 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006267 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006268 return -EBUSY;
6269 if (mddev->recovery || mddev->sync_thread)
6270 return -EBUSY;
6271 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006272 }
6273
NeilBrown36fa3062005-09-09 16:23:45 -07006274 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006275 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006276 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006277
NeilBrown1e594bb2014-12-15 12:57:00 +11006278 if (mddev->bitmap || mddev->bitmap_info.file)
6279 return -EEXIST; /* cannot add when bitmap is present */
6280 f = fget(fd);
6281
6282 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006283 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6284 mdname(mddev));
6285 return -EBADF;
6286 }
6287
NeilBrown1e594bb2014-12-15 12:57:00 +11006288 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006289 if (!S_ISREG(inode->i_mode)) {
6290 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6291 mdname(mddev));
6292 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006293 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006294 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6295 mdname(mddev));
6296 err = -EBADF;
6297 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006298 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6299 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006300 err = -EBUSY;
6301 }
6302 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006303 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006304 return err;
6305 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006306 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006307 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006308 } else if (mddev->bitmap == NULL)
6309 return -ENOENT; /* cannot remove what isn't there */
6310 err = 0;
6311 if (mddev->pers) {
6312 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006313 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006314 struct bitmap *bitmap;
6315
6316 bitmap = bitmap_create(mddev, -1);
6317 if (!IS_ERR(bitmap)) {
6318 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006319 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006320 } else
6321 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006322 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006323 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006324 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006325 fd = -1; /* make sure to put the file */
6326 }
NeilBrown36fa3062005-09-09 16:23:45 -07006327 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006328 }
6329 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006330 struct file *f = mddev->bitmap_info.file;
6331 if (f) {
6332 spin_lock(&mddev->lock);
6333 mddev->bitmap_info.file = NULL;
6334 spin_unlock(&mddev->lock);
6335 fput(f);
6336 }
NeilBrown36fa3062005-09-09 16:23:45 -07006337 }
6338
NeilBrown32a76272005-06-21 17:17:14 -07006339 return err;
6340}
6341
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342/*
6343 * set_array_info is used two different ways
6344 * The original usage is when creating a new array.
6345 * In this usage, raid_disks is > 0 and it together with
6346 * level, size, not_persistent,layout,chunksize determine the
6347 * shape of the array.
6348 * This will always create an array with a type-0.90.0 superblock.
6349 * The newer usage is when assembling an array.
6350 * In this case raid_disks will be 0, and the major_version field is
6351 * use to determine which style super-blocks are to be found on the devices.
6352 * The minor and patch _version numbers are also kept incase the
6353 * super_block handler wishes to interpret them.
6354 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006355static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356{
6357
6358 if (info->raid_disks == 0) {
6359 /* just setting version number for superblock loading */
6360 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006361 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 super_types[info->major_version].name == NULL) {
6363 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006364 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 "md: superblock version %d not known\n",
6366 info->major_version);
6367 return -EINVAL;
6368 }
6369 mddev->major_version = info->major_version;
6370 mddev->minor_version = info->minor_version;
6371 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006372 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006373 /* ensure mddev_put doesn't delete this now that there
6374 * is some minimal configuration.
6375 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006376 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 return 0;
6378 }
6379 mddev->major_version = MD_MAJOR_VERSION;
6380 mddev->minor_version = MD_MINOR_VERSION;
6381 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006382 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
6384 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006385 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006386 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 mddev->raid_disks = info->raid_disks;
6388 /* don't set md_minor, it is determined by which /dev/md* was
6389 * openned
6390 */
6391 if (info->state & (1<<MD_SB_CLEAN))
6392 mddev->recovery_cp = MaxSector;
6393 else
6394 mddev->recovery_cp = 0;
6395 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006396 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
6398 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006399 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
6401 mddev->max_disks = MD_SB_DISKS;
6402
NeilBrowne6910632008-02-06 01:39:51 -08006403 if (mddev->persistent)
6404 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006405 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
NeilBrownc3d97142009-12-14 12:49:52 +11006407 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006408 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006409 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006410
NeilBrownf6705572006-03-27 01:18:11 -08006411 mddev->reshape_position = MaxSector;
6412
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 /*
6414 * Generate a 128 bit UUID
6415 */
6416 get_random_bytes(mddev->uuid, 16);
6417
NeilBrownf6705572006-03-27 01:18:11 -08006418 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006419 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006420 mddev->new_layout = mddev->layout;
6421 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006422 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006423
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 return 0;
6425}
6426
NeilBrownfd01b882011-10-11 16:47:53 +11006427void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006428{
Dan Williamsb522adc2009-03-31 15:00:31 +11006429 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6430
6431 if (mddev->external_size)
6432 return;
6433
Dan Williams1f403622009-03-31 14:59:03 +11006434 mddev->array_sectors = array_sectors;
6435}
6436EXPORT_SYMBOL(md_set_array_sectors);
6437
NeilBrownfd01b882011-10-11 16:47:53 +11006438static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006439{
NeilBrown3cb03002011-10-11 16:45:26 +11006440 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006441 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006442 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006443
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006444 /* cluster raid doesn't support update size */
6445 if (mddev_is_clustered(mddev))
6446 return -EINVAL;
6447
NeilBrowna35b0d62006-01-06 00:20:49 -08006448 if (mddev->pers->resize == NULL)
6449 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006450 /* The "num_sectors" is the number of sectors of each device that
6451 * is used. This can only make sense for arrays with redundancy.
6452 * linear and raid0 always use whatever space is available. We can only
6453 * consider changing this number if no resync or reconstruction is
6454 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006455 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006456 * of each device. If num_sectors is zero, we find the largest size
6457 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006458 */
NeilBrownf851b602014-12-11 10:02:10 +11006459 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6460 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006461 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006462 if (mddev->ro)
6463 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006464
NeilBrowndafb20f2012-03-19 12:46:39 +11006465 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006466 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006467
Andre Nolld71f9f82008-07-11 22:02:22 +10006468 if (fit && (num_sectors == 0 || num_sectors > avail))
6469 num_sectors = avail;
6470 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006471 return -ENOSPC;
6472 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006473 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006474 if (!rv)
6475 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006476 return rv;
6477}
6478
NeilBrownfd01b882011-10-11 16:47:53 +11006479static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006480{
6481 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006482 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006483 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006484 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006485 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006486 if (mddev->ro)
6487 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006488 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006489 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006490 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006491 if (mddev->sync_thread ||
6492 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6493 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006494 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006495
6496 rdev_for_each(rdev, mddev) {
6497 if (mddev->raid_disks < raid_disks &&
6498 rdev->data_offset < rdev->new_data_offset)
6499 return -EINVAL;
6500 if (mddev->raid_disks > raid_disks &&
6501 rdev->data_offset > rdev->new_data_offset)
6502 return -EINVAL;
6503 }
6504
NeilBrown63c70c42006-03-27 01:18:13 -08006505 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006506 if (mddev->delta_disks < 0)
6507 mddev->reshape_backwards = 1;
6508 else if (mddev->delta_disks > 0)
6509 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006510
6511 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006512 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006513 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006514 mddev->reshape_backwards = 0;
6515 }
NeilBrownda943b992006-01-06 00:20:54 -08006516 return rv;
6517}
6518
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519/*
6520 * update_array_info is used to change the configuration of an
6521 * on-line array.
6522 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6523 * fields in the info are checked against the array.
6524 * Any differences that cannot be handled will cause an error.
6525 * Normally, only one change can be managed at a time.
6526 */
NeilBrownfd01b882011-10-11 16:47:53 +11006527static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528{
6529 int rv = 0;
6530 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006531 int state = 0;
6532
6533 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006534 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006535 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536
6537 if (mddev->major_version != info->major_version ||
6538 mddev->minor_version != info->minor_version ||
6539/* mddev->patch_version != info->patch_version || */
6540 mddev->ctime != info->ctime ||
6541 mddev->level != info->level ||
6542/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006543 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006544 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006545 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6546 ((state^info->state) & 0xfffffe00)
6547 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 return -EINVAL;
6549 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006550 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6551 cnt++;
6552 if (mddev->raid_disks != info->raid_disks)
6553 cnt++;
6554 if (mddev->layout != info->layout)
6555 cnt++;
6556 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6557 cnt++;
6558 if (cnt == 0)
6559 return 0;
6560 if (cnt > 1)
6561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562
6563 if (mddev->layout != info->layout) {
6564 /* Change layout
6565 * we don't need to do anything at the md level, the
6566 * personality will take care of it all.
6567 */
NeilBrown50ac1682009-06-18 08:47:55 +10006568 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006570 else {
6571 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006572 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006573 if (rv)
6574 mddev->new_layout = mddev->layout;
6575 return rv;
6576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006578 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006579 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
NeilBrownda943b992006-01-06 00:20:54 -08006581 if (mddev->raid_disks != info->raid_disks)
6582 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
NeilBrown36fa3062005-09-09 16:23:45 -07006584 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006585 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6586 rv = -EINVAL;
6587 goto err;
6588 }
6589 if (mddev->recovery || mddev->sync_thread) {
6590 rv = -EBUSY;
6591 goto err;
6592 }
NeilBrown36fa3062005-09-09 16:23:45 -07006593 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006594 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006595 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006596 if (mddev->bitmap) {
6597 rv = -EEXIST;
6598 goto err;
6599 }
6600 if (mddev->bitmap_info.default_offset == 0) {
6601 rv = -EINVAL;
6602 goto err;
6603 }
NeilBrownc3d97142009-12-14 12:49:52 +11006604 mddev->bitmap_info.offset =
6605 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006606 mddev->bitmap_info.space =
6607 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006608 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006609 bitmap = bitmap_create(mddev, -1);
6610 if (!IS_ERR(bitmap)) {
6611 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006612 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006613 } else
6614 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006615 if (rv)
6616 bitmap_destroy(mddev);
6617 mddev->pers->quiesce(mddev, 0);
6618 } else {
6619 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006620 if (!mddev->bitmap) {
6621 rv = -ENOENT;
6622 goto err;
6623 }
6624 if (mddev->bitmap->storage.file) {
6625 rv = -EINVAL;
6626 goto err;
6627 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006628 if (mddev->bitmap_info.nodes) {
6629 /* hold PW on all the bitmap lock */
6630 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6631 printk("md: can't change bitmap to none since the"
6632 " array is in use by more than one node\n");
6633 rv = -EPERM;
6634 md_cluster_ops->unlock_all_bitmaps(mddev);
6635 goto err;
6636 }
6637
6638 mddev->bitmap_info.nodes = 0;
6639 md_cluster_ops->leave(mddev);
6640 }
NeilBrown36fa3062005-09-09 16:23:45 -07006641 mddev->pers->quiesce(mddev, 1);
6642 bitmap_destroy(mddev);
6643 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006644 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006645 }
6646 }
NeilBrown850b2b422006-10-03 01:15:46 -07006647 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006648 return rv;
6649err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 return rv;
6651}
6652
NeilBrownfd01b882011-10-11 16:47:53 +11006653static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654{
NeilBrown3cb03002011-10-11 16:45:26 +11006655 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006656 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657
6658 if (mddev->pers == NULL)
6659 return -ENODEV;
6660
NeilBrown1ca69c42012-10-11 13:37:33 +11006661 rcu_read_lock();
6662 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006664 err = -ENODEV;
6665 else {
6666 md_error(mddev, rdev);
6667 if (!test_bit(Faulty, &rdev->flags))
6668 err = -EBUSY;
6669 }
6670 rcu_read_unlock();
6671 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672}
6673
Andre Noll2f9618c2008-04-25 18:57:58 +02006674/*
6675 * We have a problem here : there is no easy way to give a CHS
6676 * virtual geometry. We currently pretend that we have a 2 heads
6677 * 4 sectors (with a BIG number of cylinders...). This drives
6678 * dosfs just mad... ;-)
6679 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006680static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6681{
NeilBrownfd01b882011-10-11 16:47:53 +11006682 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006683
6684 geo->heads = 2;
6685 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006686 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006687 return 0;
6688}
6689
Nicolas Schichancb335f82014-01-15 16:58:52 +01006690static inline bool md_ioctl_valid(unsigned int cmd)
6691{
6692 switch (cmd) {
6693 case ADD_NEW_DISK:
6694 case BLKROSET:
6695 case GET_ARRAY_INFO:
6696 case GET_BITMAP_FILE:
6697 case GET_DISK_INFO:
6698 case HOT_ADD_DISK:
6699 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006700 case RAID_AUTORUN:
6701 case RAID_VERSION:
6702 case RESTART_ARRAY_RW:
6703 case RUN_ARRAY:
6704 case SET_ARRAY_INFO:
6705 case SET_BITMAP_FILE:
6706 case SET_DISK_FAULTY:
6707 case STOP_ARRAY:
6708 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006709 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006710 return true;
6711 default:
6712 return false;
6713 }
6714}
6715
Al Viroa39907f2008-03-02 10:31:15 -05006716static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 unsigned int cmd, unsigned long arg)
6718{
6719 int err = 0;
6720 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006721 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006722 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723
Nicolas Schichancb335f82014-01-15 16:58:52 +01006724 if (!md_ioctl_valid(cmd))
6725 return -ENOTTY;
6726
NeilBrown506c9e42011-12-23 10:17:26 +11006727 switch (cmd) {
6728 case RAID_VERSION:
6729 case GET_ARRAY_INFO:
6730 case GET_DISK_INFO:
6731 break;
6732 default:
6733 if (!capable(CAP_SYS_ADMIN))
6734 return -EACCES;
6735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
6737 /*
6738 * Commands dealing with the RAID driver but not any
6739 * particular array:
6740 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006741 switch (cmd) {
6742 case RAID_VERSION:
6743 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006744 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006747 case RAID_AUTORUN:
6748 err = 0;
6749 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006750 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006752 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 }
6754
6755 /*
6756 * Commands creating/starting a new array:
6757 */
6758
Al Viroa39907f2008-03-02 10:31:15 -05006759 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
6761 if (!mddev) {
6762 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006763 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 }
6765
NeilBrown1ca69c42012-10-11 13:37:33 +11006766 /* Some actions do not requires the mutex */
6767 switch (cmd) {
6768 case GET_ARRAY_INFO:
6769 if (!mddev->raid_disks && !mddev->external)
6770 err = -ENODEV;
6771 else
6772 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006773 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006774
6775 case GET_DISK_INFO:
6776 if (!mddev->raid_disks && !mddev->external)
6777 err = -ENODEV;
6778 else
6779 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006780 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006781
6782 case SET_DISK_FAULTY:
6783 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006784 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006785
6786 case GET_BITMAP_FILE:
6787 err = get_bitmap_file(mddev, argp);
6788 goto out;
6789
NeilBrown1ca69c42012-10-11 13:37:33 +11006790 }
6791
NeilBrowna7a3f082012-12-11 13:35:54 +11006792 if (cmd == ADD_NEW_DISK)
6793 /* need to ensure md_delayed_delete() has completed */
6794 flush_workqueue(md_misc_wq);
6795
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006796 if (cmd == HOT_REMOVE_DISK)
6797 /* need to ensure recovery thread has run */
6798 wait_event_interruptible_timeout(mddev->sb_wait,
6799 !test_bit(MD_RECOVERY_NEEDED,
6800 &mddev->flags),
6801 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006802 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6803 /* Need to flush page cache, and ensure no-one else opens
6804 * and writes
6805 */
6806 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006807 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006808 mutex_unlock(&mddev->open_mutex);
6809 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006810 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006811 }
6812 set_bit(MD_STILL_CLOSED, &mddev->flags);
6813 mutex_unlock(&mddev->open_mutex);
6814 sync_blockdev(bdev);
6815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 err = mddev_lock(mddev);
6817 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006818 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6820 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 }
6823
NeilBrownc02c0ae2012-12-11 13:39:21 +11006824 if (cmd == SET_ARRAY_INFO) {
6825 mdu_array_info_t info;
6826 if (!arg)
6827 memset(&info, 0, sizeof(info));
6828 else if (copy_from_user(&info, argp, sizeof(info))) {
6829 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006830 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006831 }
6832 if (mddev->pers) {
6833 err = update_array_info(mddev, &info);
6834 if (err) {
6835 printk(KERN_WARNING "md: couldn't update"
6836 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006837 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006839 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006840 }
6841 if (!list_empty(&mddev->disks)) {
6842 printk(KERN_WARNING
6843 "md: array %s already has disks!\n",
6844 mdname(mddev));
6845 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006846 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006847 }
6848 if (mddev->raid_disks) {
6849 printk(KERN_WARNING
6850 "md: array %s already initialised!\n",
6851 mdname(mddev));
6852 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006853 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006854 }
6855 err = set_array_info(mddev, &info);
6856 if (err) {
6857 printk(KERN_WARNING "md: couldn't set"
6858 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006859 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006860 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006861 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 }
6863
6864 /*
6865 * Commands querying/configuring an existing array:
6866 */
NeilBrown32a76272005-06-21 17:17:14 -07006867 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006868 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006869 if ((!mddev->raid_disks && !mddev->external)
6870 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6871 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6872 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006874 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 }
6876
6877 /*
6878 * Commands even a read-only array can execute:
6879 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006880 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006881 case RESTART_ARRAY_RW:
6882 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006883 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006884
6885 case STOP_ARRAY:
6886 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006887 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006888
6889 case STOP_ARRAY_RO:
6890 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006891 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006892
NeilBrown3ea8929d2013-04-24 11:42:41 +10006893 case HOT_REMOVE_DISK:
6894 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006895 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006896
NeilBrown7ceb17e2013-04-24 11:42:42 +10006897 case ADD_NEW_DISK:
6898 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08006899 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10006900 * So require mddev->pers and MD_DISK_SYNC.
6901 */
6902 if (mddev->pers) {
6903 mdu_disk_info_t info;
6904 if (copy_from_user(&info, argp, sizeof(info)))
6905 err = -EFAULT;
6906 else if (!(info.state & (1<<MD_DISK_SYNC)))
6907 /* Need to clear read-only for this */
6908 break;
6909 else
6910 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006911 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006912 }
6913 break;
6914
NeilBrownc02c0ae2012-12-11 13:39:21 +11006915 case BLKROSET:
6916 if (get_user(ro, (int __user *)(arg))) {
6917 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006918 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006919 }
6920 err = -EINVAL;
6921
6922 /* if the bdev is going readonly the value of mddev->ro
6923 * does not matter, no writes are coming
6924 */
6925 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006926 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006927
NeilBrownc02c0ae2012-12-11 13:39:21 +11006928 /* are we are already prepared for writes? */
6929 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006930 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006931
6932 /* transitioning to readauto need only happen for
6933 * arrays that call md_write_start
6934 */
6935 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006937 if (err == 0) {
6938 mddev->ro = 2;
6939 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006940 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006941 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006942 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 }
6944
6945 /*
6946 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006947 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 */
NeilBrown326eb172014-09-30 15:36:28 +10006949 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006950 if (mddev->ro == 2) {
6951 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006952 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006953 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006954 /* mddev_unlock will wake thread */
6955 /* If a device failed while we were read-only, we
6956 * need to make sure the metadata is updated now.
6957 */
6958 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6959 mddev_unlock(mddev);
6960 wait_event(mddev->sb_wait,
6961 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6962 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006963 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006964 }
NeilBrownf91de922005-11-08 21:39:36 -08006965 } else {
6966 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006967 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 }
6970
NeilBrownc02c0ae2012-12-11 13:39:21 +11006971 switch (cmd) {
6972 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006974 mdu_disk_info_t info;
6975 if (copy_from_user(&info, argp, sizeof(info)))
6976 err = -EFAULT;
6977 else
6978 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006979 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006982 case CLUSTERED_DISK_NACK:
6983 if (mddev_is_clustered(mddev))
6984 md_cluster_ops->new_disk_ack(mddev, false);
6985 else
6986 err = -EINVAL;
6987 goto unlock;
6988
NeilBrownc02c0ae2012-12-11 13:39:21 +11006989 case HOT_ADD_DISK:
6990 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006991 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
NeilBrownc02c0ae2012-12-11 13:39:21 +11006993 case RUN_ARRAY:
6994 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006995 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
NeilBrownc02c0ae2012-12-11 13:39:21 +11006997 case SET_BITMAP_FILE:
6998 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006999 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007000
NeilBrownc02c0ae2012-12-11 13:39:21 +11007001 default:
7002 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007003 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 }
7005
NeilBrown3adc28d2014-09-30 15:46:41 +10007006unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007007 if (mddev->hold_active == UNTIL_IOCTL &&
7008 err != -EINVAL)
7009 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007011out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 return err;
7013}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007014#ifdef CONFIG_COMPAT
7015static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7016 unsigned int cmd, unsigned long arg)
7017{
7018 switch (cmd) {
7019 case HOT_REMOVE_DISK:
7020 case HOT_ADD_DISK:
7021 case SET_DISK_FAULTY:
7022 case SET_BITMAP_FILE:
7023 /* These take in integer arg, do not convert */
7024 break;
7025 default:
7026 arg = (unsigned long)compat_ptr(arg);
7027 break;
7028 }
7029
7030 return md_ioctl(bdev, mode, cmd, arg);
7031}
7032#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033
Al Viroa39907f2008-03-02 10:31:15 -05007034static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035{
7036 /*
7037 * Succeed if we can lock the mddev, which confirms that
7038 * it isn't being stopped right now.
7039 */
NeilBrownfd01b882011-10-11 16:47:53 +11007040 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 int err;
7042
Yuanhan Liu0c098222012-05-22 13:55:32 +10007043 if (!mddev)
7044 return -ENODEV;
7045
NeilBrownd3374822009-01-09 08:31:10 +11007046 if (mddev->gendisk != bdev->bd_disk) {
7047 /* we are racing with mddev_put which is discarding this
7048 * bd_disk.
7049 */
7050 mddev_put(mddev);
7051 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007052 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007053 /* Then retry the open from the top */
7054 return -ERESTARTSYS;
7055 }
7056 BUG_ON(mddev != bdev->bd_disk->private_data);
7057
NeilBrownc8c00a62009-08-10 12:50:52 +10007058 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 goto out;
7060
7061 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007062 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10007063 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10007064 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007066 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 out:
7068 return err;
7069}
7070
Al Virodb2a1442013-05-05 21:52:57 -04007071static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007073 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007075 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007076 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007079
7080static int md_media_changed(struct gendisk *disk)
7081{
NeilBrownfd01b882011-10-11 16:47:53 +11007082 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007083
7084 return mddev->changed;
7085}
7086
7087static int md_revalidate(struct gendisk *disk)
7088{
NeilBrownfd01b882011-10-11 16:47:53 +11007089 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007090
7091 mddev->changed = 0;
7092 return 0;
7093}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007094static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095{
7096 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007097 .open = md_open,
7098 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007099 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007100#ifdef CONFIG_COMPAT
7101 .compat_ioctl = md_compat_ioctl,
7102#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007103 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007104 .media_changed = md_media_changed,
7105 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106};
7107
NeilBrownf72ffdd2014-09-30 14:23:59 +10007108static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109{
NeilBrown2b8bf342011-10-11 16:48:23 +11007110 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 /*
7113 * md_thread is a 'system-thread', it's priority should be very
7114 * high. We avoid resource deadlocks individually in each
7115 * raid personality. (RAID5 does preallocation) We also use RR and
7116 * the very same RT priority as kswapd, thus we will never get
7117 * into a priority inversion deadlock.
7118 *
7119 * we definitely have to have equal or higher priority than
7120 * bdflush, otherwise bdflush will deadlock if there are too
7121 * many dirty RAID5 blocks.
7122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123
NeilBrown6985c432005-10-19 21:23:47 -07007124 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007125 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126
NeilBrown93588e22005-11-15 00:09:12 -08007127 /* We need to wait INTERRUPTIBLE so that
7128 * we don't add to the load-average.
7129 * That means we need to be sure no signals are
7130 * pending
7131 */
7132 if (signal_pending(current))
7133 flush_signals(current);
7134
7135 wait_event_interruptible_timeout
7136 (thread->wqueue,
7137 test_bit(THREAD_WAKEUP, &thread->flags)
7138 || kthread_should_stop(),
7139 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
NeilBrown6c987912011-01-14 09:13:53 +11007141 clear_bit(THREAD_WAKEUP, &thread->flags);
7142 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007143 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007145
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 return 0;
7147}
7148
NeilBrown2b8bf342011-10-11 16:48:23 +11007149void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150{
7151 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007152 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 set_bit(THREAD_WAKEUP, &thread->flags);
7154 wake_up(&thread->wqueue);
7155 }
7156}
NeilBrown6c144d32014-09-30 16:15:38 +10007157EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158
Shaohua Li4ed87312012-10-11 13:34:00 +11007159struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7160 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161{
NeilBrown2b8bf342011-10-11 16:48:23 +11007162 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163
NeilBrown2b8bf342011-10-11 16:48:23 +11007164 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 if (!thread)
7166 return NULL;
7167
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 init_waitqueue_head(&thread->wqueue);
7169
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 thread->run = run;
7171 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007172 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007173 thread->tsk = kthread_run(md_thread, thread,
7174 "%s_%s",
7175 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007176 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007177 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 kfree(thread);
7179 return NULL;
7180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 return thread;
7182}
NeilBrown6c144d32014-09-30 16:15:38 +10007183EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184
NeilBrown2b8bf342011-10-11 16:48:23 +11007185void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186{
NeilBrown2b8bf342011-10-11 16:48:23 +11007187 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007188 if (!thread)
7189 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007190 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007191 /* Locking ensures that mddev_unlock does not wake_up a
7192 * non-existent thread
7193 */
7194 spin_lock(&pers_lock);
7195 *threadp = NULL;
7196 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007197
7198 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 kfree(thread);
7200}
NeilBrown6c144d32014-09-30 16:15:38 +10007201EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202
NeilBrownfd01b882011-10-11 16:47:53 +11007203void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204{
NeilBrownb2d444d2005-11-08 21:39:31 -08007205 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007207
NeilBrownde393cd2011-07-28 11:31:48 +10007208 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 return;
7210 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007211 if (mddev->degraded)
7212 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007213 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7215 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7216 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007217 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007218 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007219 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220}
NeilBrown6c144d32014-09-30 16:15:38 +10007221EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222
7223/* seq_file implementation /proc/mdstat */
7224
7225static void status_unused(struct seq_file *seq)
7226{
7227 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007228 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229
7230 seq_printf(seq, "unused devices: ");
7231
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007232 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 char b[BDEVNAME_SIZE];
7234 i++;
7235 seq_printf(seq, "%s ",
7236 bdevname(rdev->bdev,b));
7237 }
7238 if (!i)
7239 seq_printf(seq, "<none>");
7240
7241 seq_printf(seq, "\n");
7242}
7243
NeilBrownf7851be2015-07-02 17:12:58 +10007244static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245{
NeilBrowndd71cf62009-05-07 12:49:35 +10007246 sector_t max_sectors, resync, res;
7247 unsigned long dt, db;
7248 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007249 int scale;
7250 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251
NeilBrownc804cde2012-05-21 09:28:33 +10007252 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7253 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007254 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007256 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257
NeilBrownf7851be2015-07-02 17:12:58 +10007258 resync = mddev->curr_resync;
7259 if (resync <= 3) {
7260 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7261 /* Still cleaning up */
7262 resync = max_sectors;
7263 } else
7264 resync -= atomic_read(&mddev->recovery_active);
7265
7266 if (resync == 0) {
7267 if (mddev->recovery_cp < MaxSector) {
7268 seq_printf(seq, "\tresync=PENDING");
7269 return 1;
7270 }
7271 return 0;
7272 }
7273 if (resync < 3) {
7274 seq_printf(seq, "\tresync=DELAYED");
7275 return 1;
7276 }
7277
NeilBrown403df472014-09-30 15:52:29 +10007278 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007279 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007280 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007281 * u32, as those are the requirements for sector_div.
7282 * Thus 'scale' must be at least 10
7283 */
7284 scale = 10;
7285 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007286 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007287 scale++;
7288 }
7289 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007290 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007291
7292 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 {
NeilBrown4588b422006-03-27 01:18:04 -08007294 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 seq_printf(seq, "[");
7296 for (i = 0; i < x; i++)
7297 seq_printf(seq, "=");
7298 seq_printf(seq, ">");
7299 for (i = 0; i < y; i++)
7300 seq_printf(seq, ".");
7301 seq_printf(seq, "] ");
7302 }
NeilBrown4588b422006-03-27 01:18:04 -08007303 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007304 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7305 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007306 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7307 "check" :
7308 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7309 "resync" : "recovery"))),
7310 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007311 (unsigned long long) resync/2,
7312 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313
7314 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315 * dt: time from mark until now
7316 * db: blocks written from mark until now
7317 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007318 *
7319 * rt is a sector_t, so could be 32bit or 64bit.
7320 * So we divide before multiply in case it is 32bit and close
7321 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007322 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007323 * near the end of resync when the number of remaining sectors
7324 * is close to 'db'.
7325 * We then divide rt by 32 after multiplying by db to compensate.
7326 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 */
7328 dt = ((jiffies - mddev->resync_mark) / HZ);
7329 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007330 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7331 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332
NeilBrowndd71cf62009-05-07 12:49:35 +10007333 rt = max_sectors - resync; /* number of remaining sectors */
7334 sector_div(rt, db/32+1);
7335 rt *= dt;
7336 rt >>= 5;
7337
7338 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7339 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340
NeilBrownff4e8d92006-07-10 04:44:16 -07007341 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007342 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343}
7344
7345static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7346{
7347 struct list_head *tmp;
7348 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007349 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350
7351 if (l >= 0x10000)
7352 return NULL;
7353 if (!l--)
7354 /* header */
7355 return (void*)1;
7356
7357 spin_lock(&all_mddevs_lock);
7358 list_for_each(tmp,&all_mddevs)
7359 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007360 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 mddev_get(mddev);
7362 spin_unlock(&all_mddevs_lock);
7363 return mddev;
7364 }
7365 spin_unlock(&all_mddevs_lock);
7366 if (!l--)
7367 return (void*)2;/* tail */
7368 return NULL;
7369}
7370
7371static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7372{
7373 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007374 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007375
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 ++*pos;
7377 if (v == (void*)2)
7378 return NULL;
7379
7380 spin_lock(&all_mddevs_lock);
7381 if (v == (void*)1)
7382 tmp = all_mddevs.next;
7383 else
7384 tmp = mddev->all_mddevs.next;
7385 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007386 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 else {
7388 next_mddev = (void*)2;
7389 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 spin_unlock(&all_mddevs_lock);
7392
7393 if (v != (void*)1)
7394 mddev_put(mddev);
7395 return next_mddev;
7396
7397}
7398
7399static void md_seq_stop(struct seq_file *seq, void *v)
7400{
NeilBrownfd01b882011-10-11 16:47:53 +11007401 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402
7403 if (mddev && v != (void*)1 && v != (void*)2)
7404 mddev_put(mddev);
7405}
7406
7407static int md_seq_show(struct seq_file *seq, void *v)
7408{
NeilBrownfd01b882011-10-11 16:47:53 +11007409 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007410 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007411 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
7413 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007414 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 seq_printf(seq, "Personalities : ");
7416 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007417 list_for_each_entry(pers, &pers_list, list)
7418 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419
7420 spin_unlock(&pers_lock);
7421 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007422 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 return 0;
7424 }
7425 if (v == (void*)2) {
7426 status_unused(seq);
7427 return 0;
7428 }
7429
NeilBrown36d091f2014-12-15 12:56:58 +11007430 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7432 seq_printf(seq, "%s : %sactive", mdname(mddev),
7433 mddev->pers ? "" : "in");
7434 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007435 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007437 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007438 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 seq_printf(seq, " %s", mddev->pers->name);
7440 }
7441
Andre Nolldd8ac332009-03-31 14:33:13 +11007442 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007443 rcu_read_lock();
7444 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 char b[BDEVNAME_SIZE];
7446 seq_printf(seq, " %s[%d]",
7447 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007448 if (test_bit(WriteMostly, &rdev->flags))
7449 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007450 if (test_bit(Journal, &rdev->flags))
7451 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007452 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 seq_printf(seq, "(F)");
7454 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007455 }
7456 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007457 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007458 if (test_bit(Replacement, &rdev->flags))
7459 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007460 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007462 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463
7464 if (!list_empty(&mddev->disks)) {
7465 if (mddev->pers)
7466 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007467 (unsigned long long)
7468 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 else
7470 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007471 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007473 if (mddev->persistent) {
7474 if (mddev->major_version != 0 ||
7475 mddev->minor_version != 90) {
7476 seq_printf(seq," super %d.%d",
7477 mddev->major_version,
7478 mddev->minor_version);
7479 }
NeilBrowne6910632008-02-06 01:39:51 -08007480 } else if (mddev->external)
7481 seq_printf(seq, " super external:%s",
7482 mddev->metadata_type);
7483 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007484 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
7486 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007487 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007488 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007489 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007490 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007491 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007492 }
NeilBrown32a76272005-06-21 17:17:14 -07007493 } else
7494 seq_printf(seq, "\n ");
7495
NeilBrown57148962012-03-19 12:46:40 +11007496 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497
7498 seq_printf(seq, "\n");
7499 }
NeilBrown36d091f2014-12-15 12:56:58 +11007500 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007501
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 return 0;
7503}
7504
Jan Engelhardt110518b2009-05-07 12:49:37 +10007505static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506 .start = md_seq_start,
7507 .next = md_seq_next,
7508 .stop = md_seq_stop,
7509 .show = md_seq_show,
7510};
7511
7512static int md_seq_open(struct inode *inode, struct file *file)
7513{
Kay Sieversf1514632011-07-12 20:48:39 +02007514 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 int error;
7516
7517 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007518 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007519 return error;
7520
7521 seq = file->private_data;
7522 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 return error;
7524}
7525
NeilBrowne2f23b62014-04-09 14:33:51 +10007526static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007527static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7528{
Kay Sieversf1514632011-07-12 20:48:39 +02007529 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007530 int mask;
7531
NeilBrowne2f23b62014-04-09 14:33:51 +10007532 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007533 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007534 poll_wait(filp, &md_event_waiters, wait);
7535
7536 /* always allow read */
7537 mask = POLLIN | POLLRDNORM;
7538
Kay Sieversf1514632011-07-12 20:48:39 +02007539 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007540 mask |= POLLERR | POLLPRI;
7541 return mask;
7542}
7543
Arjan van de Venfa027c22007-02-12 00:55:33 -08007544static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007545 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 .open = md_seq_open,
7547 .read = seq_read,
7548 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007549 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007550 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551};
7552
NeilBrown84fc4b52011-10-11 16:49:58 +11007553int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554{
Chao Yu50bd3772014-09-25 15:28:34 +08007555 printk(KERN_INFO "md: %s personality registered for level %d\n",
7556 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007558 list_add_tail(&p->list, &pers_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(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563
NeilBrown84fc4b52011-10-11 16:49:58 +11007564int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565{
NeilBrown2604b702006-01-06 00:20:36 -08007566 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007568 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 spin_unlock(&pers_lock);
7570 return 0;
7571}
NeilBrown6c144d32014-09-30 16:15:38 +10007572EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573
NeilBrown6022e752015-08-13 12:32:55 +10007574int register_md_cluster_operations(struct md_cluster_operations *ops,
7575 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007576{
NeilBrown6022e752015-08-13 12:32:55 +10007577 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007578 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007579 if (md_cluster_ops != NULL)
7580 ret = -EALREADY;
7581 else {
7582 md_cluster_ops = ops;
7583 md_cluster_mod = module;
7584 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007585 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007586 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007587}
7588EXPORT_SYMBOL(register_md_cluster_operations);
7589
7590int unregister_md_cluster_operations(void)
7591{
7592 spin_lock(&pers_lock);
7593 md_cluster_ops = NULL;
7594 spin_unlock(&pers_lock);
7595 return 0;
7596}
7597EXPORT_SYMBOL(unregister_md_cluster_operations);
7598
7599int md_setup_cluster(struct mddev *mddev, int nodes)
7600{
7601 int err;
7602
7603 err = request_module("md-cluster");
7604 if (err) {
7605 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007606 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007607 }
7608
7609 spin_lock(&pers_lock);
7610 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7611 spin_unlock(&pers_lock);
7612 return -ENOENT;
7613 }
7614 spin_unlock(&pers_lock);
7615
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007616 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007617}
7618
7619void md_cluster_stop(struct mddev *mddev)
7620{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007621 if (!md_cluster_ops)
7622 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007623 md_cluster_ops->leave(mddev);
7624 module_put(md_cluster_mod);
7625}
7626
NeilBrownfd01b882011-10-11 16:47:53 +11007627static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007629 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007631 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632
7633 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007634 rcu_read_lock();
7635 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007637 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7638 (int)part_stat_read(&disk->part0, sectors[1]) -
7639 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007640 /* sync IO will cause sync_io to increase before the disk_stats
7641 * as sync_io is counted when a request starts, and
7642 * disk_stats is counted when it completes.
7643 * So resync activity will cause curr_events to be smaller than
7644 * when there was no such activity.
7645 * non-sync IO will cause disk_stat to increase without
7646 * increasing sync_io so curr_events will (eventually)
7647 * be larger than it was before. Once it becomes
7648 * substantially larger, the test below will cause
7649 * the array to appear non-idle, and resync will slow
7650 * down.
7651 * If there is a lot of outstanding resync activity when
7652 * we set last_event to curr_events, then all that activity
7653 * completing might cause the array to appear non-idle
7654 * and resync will be slowed down even though there might
7655 * not have been non-resync activity. This will only
7656 * happen once though. 'last_events' will soon reflect
7657 * the state where there is little or no outstanding
7658 * resync requests, and further resync activity will
7659 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007660 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007662 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 rdev->last_events = curr_events;
7664 idle = 0;
7665 }
7666 }
NeilBrown4b809912008-07-21 17:05:25 +10007667 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 return idle;
7669}
7670
NeilBrownfd01b882011-10-11 16:47:53 +11007671void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672{
7673 /* another "blocks" (512byte) blocks have been synced */
7674 atomic_sub(blocks, &mddev->recovery_active);
7675 wake_up(&mddev->recovery_wait);
7676 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007677 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007678 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679 md_wakeup_thread(mddev->thread);
7680 // stop recovery, signal do_sync ....
7681 }
7682}
NeilBrown6c144d32014-09-30 16:15:38 +10007683EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684
NeilBrown06d91a52005-06-21 17:17:12 -07007685/* md_write_start(mddev, bi)
7686 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007687 * in superblock) before writing, schedule a superblock update
7688 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007689 */
NeilBrownfd01b882011-10-11 16:47:53 +11007690void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691{
Neil Brown0fd62b82008-06-28 08:31:36 +10007692 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007693 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007694 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007695
NeilBrownf91de922005-11-08 21:39:36 -08007696 BUG_ON(mddev->ro == 1);
7697 if (mddev->ro == 2) {
7698 /* need to switch to read/write */
7699 mddev->ro = 0;
7700 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7701 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007702 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007703 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007704 }
NeilBrown06d91a52005-06-21 17:17:12 -07007705 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007706 if (mddev->safemode == 1)
7707 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007708 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007709 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007710 if (mddev->in_sync) {
7711 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007712 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007713 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007714 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007715 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007716 }
NeilBrown85572d72014-12-15 12:56:56 +11007717 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007718 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007719 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007720 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007721 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007722 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723}
NeilBrown6c144d32014-09-30 16:15:38 +10007724EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725
NeilBrownfd01b882011-10-11 16:47:53 +11007726void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727{
7728 if (atomic_dec_and_test(&mddev->writes_pending)) {
7729 if (mddev->safemode == 2)
7730 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007731 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7733 }
7734}
NeilBrown6c144d32014-09-30 16:15:38 +10007735EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736
NeilBrown2a2275d2007-01-26 00:57:11 -08007737/* md_allow_write(mddev)
7738 * Calling this ensures that the array is marked 'active' so that writes
7739 * may proceed without blocking. It is important to call this before
7740 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7741 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007742 *
Guoqing Jiangabf35082015-12-21 10:51:01 +11007743 * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007744 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007745 */
NeilBrownfd01b882011-10-11 16:47:53 +11007746int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007747{
7748 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007749 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007750 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007751 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007752 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007753 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007754
NeilBrown85572d72014-12-15 12:56:56 +11007755 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007756 if (mddev->in_sync) {
7757 mddev->in_sync = 0;
7758 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007759 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007760 if (mddev->safemode_delay &&
7761 mddev->safemode == 0)
7762 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007763 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007764 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007765 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007766 } else
NeilBrown85572d72014-12-15 12:56:56 +11007767 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007768
NeilBrown070dc6d2010-08-30 17:33:34 +10007769 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007770 return -EAGAIN;
7771 else
7772 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007773}
7774EXPORT_SYMBOL_GPL(md_allow_write);
7775
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776#define SYNC_MARKS 10
7777#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007778#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007779void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780{
Shaohua Li4ed87312012-10-11 13:34:00 +11007781 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007782 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 unsigned int currspeed = 0,
7784 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007785 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007787 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788 sector_t mark_cnt[SYNC_MARKS];
7789 int last_mark,m;
7790 struct list_head *tmp;
7791 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007792 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007793 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007794 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007795 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007796 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797
7798 /* just incase thread restarts... */
7799 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7800 return;
NeilBrown3991b312014-05-28 13:39:23 +10007801 if (mddev->ro) {/* never try to sync a read-only array */
7802 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007803 return;
NeilBrown3991b312014-05-28 13:39:23 +10007804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007806 if (mddev_is_clustered(mddev)) {
7807 ret = md_cluster_ops->resync_start(mddev);
7808 if (ret)
7809 goto skip;
7810
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04007811 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007812 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7813 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
7814 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
7815 && ((unsigned long long)mddev->curr_resync_completed
7816 < (unsigned long long)mddev->resync_max_sectors))
7817 goto skip;
7818 }
7819
NeilBrown61df9d92006-10-03 01:15:57 -07007820 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007821 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007822 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007823 action = "check";
7824 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007825 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007826 action = "repair";
7827 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007828 desc = "resync";
7829 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7830 desc = "reshape";
7831 else
7832 desc = "recovery";
7833
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007834 mddev->last_sync_action = action ?: desc;
7835
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836 /* we overload curr_resync somewhat here.
7837 * 0 == not engaged in resync at all
7838 * 2 == checking that there is no conflict with another sync
7839 * 1 == like 2, but have yielded to allow conflicting resync to
7840 * commense
7841 * other == active in resync - this many blocks
7842 *
7843 * Before starting a resync we must have set curr_resync to
7844 * 2, and then checked that every "conflicting" array has curr_resync
7845 * less than ours. When we find one that is the same or higher
7846 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7847 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7848 * This will mean we have to start checking from the beginning again.
7849 *
7850 */
7851
7852 do {
7853 mddev->curr_resync = 2;
7854
7855 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007856 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007858 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859 if (mddev2 == mddev)
7860 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007861 if (!mddev->parallel_resync
7862 && mddev2->curr_resync
7863 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 DEFINE_WAIT(wq);
7865 if (mddev < mddev2 && mddev->curr_resync == 2) {
7866 /* arbitrarily yield */
7867 mddev->curr_resync = 1;
7868 wake_up(&resync_wait);
7869 }
7870 if (mddev > mddev2 && mddev->curr_resync == 1)
7871 /* no need to wait here, we can wait the next
7872 * time 'round when curr_resync == 2
7873 */
7874 continue;
NeilBrown97441972008-09-19 11:49:54 +10007875 /* We need to wait 'interruptible' so as not to
7876 * contribute to the load average, and not to
7877 * be caught by 'softlockup'
7878 */
7879 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007880 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007881 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007882 printk(KERN_INFO "md: delaying %s of %s"
7883 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007885 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007887 if (signal_pending(current))
7888 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 schedule();
7890 finish_wait(&resync_wait, &wq);
7891 goto try_again;
7892 }
7893 finish_wait(&resync_wait, &wq);
7894 }
7895 }
7896 } while (mddev->curr_resync < 2);
7897
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007898 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007899 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007901 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 */
7903 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007904 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007905 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007906 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7907 j = mddev->resync_min;
7908 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007909 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007910
NeilBrownccfcc3c2006-03-27 01:18:09 -08007911 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007912 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007913 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007915 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007916 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007917 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007918 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007919 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007920 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007921 !test_bit(Faulty, &rdev->flags) &&
7922 !test_bit(In_sync, &rdev->flags) &&
7923 rdev->recovery_offset < j)
7924 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007925 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007926
7927 /* If there is a bitmap, we need to make sure all
7928 * writes that started before we added a spare
7929 * complete before we start doing a recovery.
7930 * Otherwise the write might complete and (via
7931 * bitmap_endwrite) set a bit in the bitmap after the
7932 * recovery has checked that bit and skipped that
7933 * region.
7934 */
7935 if (mddev->bitmap) {
7936 mddev->pers->quiesce(mddev, 1);
7937 mddev->pers->quiesce(mddev, 0);
7938 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940
NeilBrown61df9d92006-10-03 01:15:57 -07007941 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7942 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7943 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007944 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007945 "(but not more than %d KB/sec) for %s.\n",
7946 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947
NeilBrowneea1bf32009-03-31 14:27:02 +11007948 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007949
NeilBrown57afd892005-06-21 17:17:13 -07007950 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951 for (m = 0; m < SYNC_MARKS; m++) {
7952 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007953 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007954 }
7955 last_mark = 0;
7956 mddev->resync_mark = mark[last_mark];
7957 mddev->resync_mark_cnt = mark_cnt[last_mark];
7958
7959 /*
7960 * Tune reconstruction:
7961 */
7962 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007963 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7964 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965
7966 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967 last_check = 0;
7968
7969 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007970 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007971 "md: resuming %s of %s from checkpoint.\n",
7972 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007974 } else
7975 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007976 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007977 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7978 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007979 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980
majianpeng7c2c57c2012-07-03 12:12:26 +10007981 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007983 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984
NeilBrown57afd892005-06-21 17:17:13 -07007985 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007986
NeilBrown7a91ee12009-05-26 12:57:21 +10007987 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7988 ((mddev->curr_resync > mddev->curr_resync_completed &&
7989 (mddev->curr_resync - mddev->curr_resync_completed)
7990 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007991 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007992 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007993 >= mddev->resync_max - mddev->curr_resync_completed ||
7994 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007995 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007996 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007997 wait_event(mddev->recovery_wait,
7998 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007999 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008000 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8001 j > mddev->recovery_cp)
8002 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008003 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10008004 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008005 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008006 }
NeilBrownacb180b2009-04-14 16:28:34 +10008007
NeilBrownc91abf52013-11-19 12:02:01 +11008008 while (j >= mddev->resync_max &&
8009 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008010 /* As this condition is controlled by user-space,
8011 * we can block indefinitely, so use '_interruptible'
8012 * to avoid triggering warnings.
8013 */
8014 flush_signals(current); /* just in case */
8015 wait_event_interruptible(mddev->recovery_wait,
8016 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008017 || test_bit(MD_RECOVERY_INTR,
8018 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008019 }
NeilBrownacb180b2009-04-14 16:28:34 +10008020
NeilBrownc91abf52013-11-19 12:02:01 +11008021 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8022 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008023
NeilBrown09314792015-02-19 16:04:40 +11008024 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008025 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008026 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008027 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 }
NeilBrown57afd892005-06-21 17:17:13 -07008029
8030 if (!skipped) { /* actual IO requested */
8031 io_sectors += sectors;
8032 atomic_add(sectors, &mddev->recovery_active);
8033 }
8034
NeilBrowne875ece2011-07-28 11:39:24 +10008035 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8036 break;
8037
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008039 if (j > max_sectors)
8040 /* when skipping, extra large numbers can be returned. */
8041 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008042 if (j > 2)
8043 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008044 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008045 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008046 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008047 * visible in /proc/mdstat
8048 */
8049 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008050
8051 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052 continue;
8053
NeilBrown57afd892005-06-21 17:17:13 -07008054 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055 repeat:
8056 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8057 /* step marks */
8058 int next = (last_mark+1) % SYNC_MARKS;
8059
8060 mddev->resync_mark = mark[next];
8061 mddev->resync_mark_cnt = mark_cnt[next];
8062 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008063 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064 last_mark = next;
8065 }
8066
NeilBrownc91abf52013-11-19 12:02:01 +11008067 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8068 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008069
8070 /*
8071 * this loop exits only if either when we are slower than
8072 * the 'hard' speed limit, or the system was IO-idle for
8073 * a jiffy.
8074 * the system might be non-idle CPU-wise, but we only care
8075 * about not overloading the IO subsystem. (things like an
8076 * e2fsck being done on the RAID array should execute fast)
8077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 cond_resched();
8079
Xiao Niac7e50a2014-08-07 09:37:41 -04008080 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8081 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008082 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083
NeilBrown88202a02006-01-06 00:21:36 -08008084 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008085 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008086 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087 goto repeat;
8088 }
NeilBrownac8fa412015-02-19 16:55:00 +11008089 if (!is_mddev_idle(mddev, 0)) {
8090 /*
8091 * Give other IO more of a chance.
8092 * The faster the devices, the less we wait.
8093 */
8094 wait_event(mddev->recovery_wait,
8095 !atomic_read(&mddev->recovery_active));
8096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097 }
8098 }
NeilBrownc91abf52013-11-19 12:02:01 +11008099 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8100 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8101 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008102 /*
8103 * this also signals 'finished resyncing' to md_stop
8104 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008105 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8107
NeilBrown5ed1df22015-07-24 13:27:08 +10008108 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8109 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8110 mddev->curr_resync > 2) {
8111 mddev->curr_resync_completed = mddev->curr_resync;
8112 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8113 }
NeilBrown09314792015-02-19 16:04:40 +11008114 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115
NeilBrowndfc70642008-05-23 13:04:39 -07008116 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008117 mddev->curr_resync > 2) {
8118 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8119 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8120 if (mddev->curr_resync >= mddev->recovery_cp) {
8121 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008122 "md: checkpointing %s of %s.\n",
8123 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008124 if (test_bit(MD_RECOVERY_ERROR,
8125 &mddev->recovery))
8126 mddev->recovery_cp =
8127 mddev->curr_resync_completed;
8128 else
8129 mddev->recovery_cp =
8130 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008131 }
8132 } else
8133 mddev->recovery_cp = MaxSector;
8134 } else {
8135 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8136 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008137 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008138 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008139 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008140 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008141 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008142 !test_bit(Faulty, &rdev->flags) &&
8143 !test_bit(In_sync, &rdev->flags) &&
8144 rdev->recovery_offset < mddev->curr_resync)
8145 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008146 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008149 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008150 /* set CHANGE_PENDING here since maybe another update is needed,
8151 * so other nodes are informed. It should be harmless for normal
8152 * raid */
8153 set_mask_bits(&mddev->flags, 0,
8154 BIT(MD_CHANGE_PENDING) | BIT(MD_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008155
NeilBrown23da4222014-12-15 12:57:01 +11008156 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008157 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8158 /* We completed so min/max setting can be forgotten if used. */
8159 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8160 mddev->resync_min = 0;
8161 mddev->resync_max = MaxSector;
8162 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8163 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008164 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008166 spin_unlock(&mddev->lock);
8167
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171}
NeilBrown29269552006-03-27 01:18:10 -08008172EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173
NeilBrown746d3202013-04-24 11:42:41 +10008174static int remove_and_add_spares(struct mddev *mddev,
8175 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008176{
NeilBrown3cb03002011-10-11 16:45:26 +11008177 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008178 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008179 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008180
NeilBrowndafb20f2012-03-19 12:46:39 +11008181 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008182 if ((this == NULL || rdev == this) &&
8183 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008184 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008185 (test_bit(Faulty, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008186 (!test_bit(In_sync, &rdev->flags) &&
8187 !test_bit(Journal, &rdev->flags))) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008188 atomic_read(&rdev->nr_pending)==0) {
8189 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008190 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008191 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008192 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008193 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008194 }
8195 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008196 if (removed && mddev->kobj.sd)
8197 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008198
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008199 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008200 goto no_add;
8201
NeilBrowndafb20f2012-03-19 12:46:39 +11008202 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008203 if (this && this != rdev)
8204 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008205 if (test_bit(Candidate, &rdev->flags))
8206 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008207 if (rdev->raid_disk >= 0 &&
8208 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008209 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008210 !test_bit(Faulty, &rdev->flags))
8211 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008212 if (rdev->raid_disk >= 0)
8213 continue;
8214 if (test_bit(Faulty, &rdev->flags))
8215 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008216 if (!test_bit(Journal, &rdev->flags)) {
8217 if (mddev->ro &&
8218 ! (rdev->saved_raid_disk >= 0 &&
8219 !test_bit(Bitmap_sync, &rdev->flags)))
8220 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008221
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008222 rdev->recovery_offset = 0;
8223 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008224 if (mddev->pers->
8225 hot_add_disk(mddev, rdev) == 0) {
8226 if (sysfs_link_rdev(mddev, rdev))
8227 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008228 if (!test_bit(Journal, &rdev->flags))
8229 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008230 md_new_event(mddev);
8231 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008232 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008233 }
NeilBrown746d3202013-04-24 11:42:41 +10008234no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008235 if (removed)
8236 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008237 return spares;
8238}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008239
NeilBrownac05f252014-09-30 08:10:42 +10008240static void md_start_sync(struct work_struct *ws)
8241{
8242 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008243 int ret = 0;
8244
NeilBrownac05f252014-09-30 08:10:42 +10008245 mddev->sync_thread = md_register_thread(md_do_sync,
8246 mddev,
8247 "resync");
8248 if (!mddev->sync_thread) {
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008249 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8250 printk(KERN_ERR "%s: could not start resync"
8251 " thread...\n",
8252 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008253 /* leave the spares where they are, it shouldn't hurt */
8254 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8255 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8256 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8257 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8258 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008259 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008260 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8261 &mddev->recovery))
8262 if (mddev->sysfs_action)
8263 sysfs_notify_dirent_safe(mddev->sysfs_action);
8264 } else
8265 md_wakeup_thread(mddev->sync_thread);
8266 sysfs_notify_dirent_safe(mddev->sysfs_action);
8267 md_new_event(mddev);
8268}
8269
Linus Torvalds1da177e2005-04-16 15:20:36 -07008270/*
8271 * This routine is regularly called by all per-raid-array threads to
8272 * deal with generic issues like resync and super-block update.
8273 * Raid personalities that don't have a thread (linear/raid0) do not
8274 * need this as they never do any recovery or update the superblock.
8275 *
8276 * It does not do any resync itself, but rather "forks" off other threads
8277 * to do that as needed.
8278 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8279 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008280 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008281 * and wakeups up this thread which will reap the thread and finish up.
8282 * This thread also removes any faulty devices (with nr_pending == 0).
8283 *
8284 * The overall approach is:
8285 * 1/ if the superblock needs updating, update it.
8286 * 2/ If a recovery thread is running, don't do anything else.
8287 * 3/ If recovery has finished, clean up, possibly marking spares active.
8288 * 4/ If there are any faulty devices, remove them.
8289 * 5/ If array is degraded, try to add spares devices
8290 * 6/ If array has spares or is not in-sync, start a resync thread.
8291 */
NeilBrownfd01b882011-10-11 16:47:53 +11008292void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008294 if (mddev->suspended)
8295 return;
8296
NeilBrown5f404022005-06-21 17:17:16 -07008297 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008298 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299
NeilBrownfca4d842005-06-21 17:17:11 -07008300 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008301 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008302 printk(KERN_INFO "md: %s in immediate safe mode\n",
8303 mdname(mddev));
8304 mddev->safemode = 2;
8305 }
8306 flush_signals(current);
8307 }
8308
NeilBrownc89a8ee2008-08-05 15:54:13 +10008309 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8310 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008312 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008314 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008315 test_bit(MD_RELOAD_SB, &mddev->flags) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008316 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008317 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8318 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 ))
8320 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008321
NeilBrowndf5b89b2006-03-27 01:18:20 -08008322 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008323 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008324
NeilBrownc89a8ee2008-08-05 15:54:13 +10008325 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008326 struct md_rdev *rdev;
8327 if (!mddev->external && mddev->in_sync)
8328 /* 'Blocked' flag not needed as failed devices
8329 * will be recorded if array switched to read/write.
8330 * Leaving it set will prevent the device
8331 * from being removed.
8332 */
8333 rdev_for_each(rdev, mddev)
8334 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008335 /* On a read-only array we can:
8336 * - remove failed devices
8337 * - add already-in_sync devices if the array itself
8338 * is in-sync.
8339 * As we only add devices that are already in-sync,
8340 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008341 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008342 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008343 /* There is no thread, but we need to call
8344 * ->spare_active and clear saved_raid_disk
8345 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008346 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008347 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008348 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008349 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008350 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008351 goto unlock;
8352 }
8353
Guoqing Jiang659b2542015-12-21 10:50:59 +11008354 if (mddev_is_clustered(mddev)) {
8355 struct md_rdev *rdev;
8356 /* kick the device if another node issued a
8357 * remove disk.
8358 */
8359 rdev_for_each(rdev, mddev) {
8360 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8361 rdev->raid_disk < 0)
8362 md_kick_rdev_from_array(rdev);
8363 }
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008364
8365 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8366 md_reload_sb(mddev, mddev->good_device_nr);
Guoqing Jiang659b2542015-12-21 10:50:59 +11008367 }
8368
NeilBrown31a59e32008-04-30 00:52:30 -07008369 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008370 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008371 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008372 if (mddev->safemode &&
8373 !atomic_read(&mddev->writes_pending) &&
8374 !mddev->in_sync &&
8375 mddev->recovery_cp == MaxSector) {
8376 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008377 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008378 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008379 }
8380 if (mddev->safemode == 1)
8381 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008382 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008383 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008384 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008385 }
NeilBrownfca4d842005-06-21 17:17:11 -07008386
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008387 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07008388 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008389
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8391 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8392 /* resync/recovery still happening */
8393 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8394 goto unlock;
8395 }
8396 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008397 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398 goto unlock;
8399 }
Neil Brown72a23c22008-06-28 08:31:41 +10008400 /* Set RUNNING before clearing NEEDED to avoid
8401 * any transients in the value of "sync_action".
8402 */
NeilBrown72f36d52012-10-11 14:25:57 +11008403 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008404 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008405 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008406 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008407 /* Clear some bits that don't mean anything, but
8408 * might be left set
8409 */
NeilBrown24dd4692005-11-08 21:39:26 -08008410 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8411 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412
NeilBrowned209582012-04-24 10:23:14 +10008413 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8414 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008415 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416 /* no recovery is running.
8417 * remove any failed drives, then
8418 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008419 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420 * the personality to fail the re-add.
8421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008422
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008423 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008424 if (mddev->pers->check_reshape == NULL ||
8425 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008426 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008427 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008428 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008429 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008430 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008431 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8432 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008433 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008434 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008435 } else if (mddev->recovery_cp < MaxSector) {
8436 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008437 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008438 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8439 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008440 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008441
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008443 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008444 /* We are adding a device or devices to an array
8445 * which has the bitmap stored on all devices.
8446 * So make sure all bitmap pages get written
8447 */
8448 bitmap_write_all(mddev->bitmap);
8449 }
NeilBrownac05f252014-09-30 08:10:42 +10008450 INIT_WORK(&mddev->del_work, md_start_sync);
8451 queue_work(md_misc_wq, &mddev->del_work);
8452 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453 }
NeilBrownac05f252014-09-30 08:10:42 +10008454 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008455 if (!mddev->sync_thread) {
8456 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008457 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008458 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8459 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008460 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008461 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008462 }
NeilBrownac05f252014-09-30 08:10:42 +10008463 unlock:
8464 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465 mddev_unlock(mddev);
8466 }
8467}
NeilBrown6c144d32014-09-30 16:15:38 +10008468EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008470void md_reap_sync_thread(struct mddev *mddev)
8471{
8472 struct md_rdev *rdev;
8473
8474 /* resync has finished, collect result */
8475 md_unregister_thread(&mddev->sync_thread);
8476 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8477 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8478 /* success...*/
8479 /* activate any spares */
8480 if (mddev->pers->spare_active(mddev)) {
8481 sysfs_notify(&mddev->kobj, NULL,
8482 "degraded");
8483 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8484 }
8485 }
8486 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8487 mddev->pers->finish_reshape)
8488 mddev->pers->finish_reshape(mddev);
8489
8490 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008491 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008492 */
NeilBrownf4667222013-12-09 12:04:56 +11008493 if (!mddev->degraded)
8494 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008495 rdev->saved_raid_disk = -1;
8496
8497 md_update_sb(mddev, 1);
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008498 /* MD_CHANGE_PENDING should be cleared by md_update_sb, so we can
8499 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8500 * clustered raid */
8501 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8502 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008503 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008504 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008505 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8506 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8507 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8508 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008509 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008510 /* flag recovery needed just to double check */
8511 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8512 sysfs_notify_dirent_safe(mddev->sysfs_action);
8513 md_new_event(mddev);
8514 if (mddev->event_work.func)
8515 queue_work(md_misc_wq, &mddev->event_work);
8516}
NeilBrown6c144d32014-09-30 16:15:38 +10008517EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008518
NeilBrownfd01b882011-10-11 16:47:53 +11008519void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008520{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008521 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008522 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008523 !test_bit(Blocked, &rdev->flags) &&
8524 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008525 msecs_to_jiffies(5000));
8526 rdev_dec_pending(rdev, mddev);
8527}
8528EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8529
NeilBrownc6563a82012-05-21 09:27:00 +10008530void md_finish_reshape(struct mddev *mddev)
8531{
8532 /* called be personality module when reshape completes. */
8533 struct md_rdev *rdev;
8534
8535 rdev_for_each(rdev, mddev) {
8536 if (rdev->data_offset > rdev->new_data_offset)
8537 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8538 else
8539 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8540 rdev->data_offset = rdev->new_data_offset;
8541 }
8542}
8543EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008544
Vishal Vermafc974ee2015-12-24 19:20:34 -07008545/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008546
Vishal Vermafc974ee2015-12-24 19:20:34 -07008547/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008548int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008549 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008550{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008551 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008552 int rv;
8553 if (is_new)
8554 s += rdev->new_data_offset;
8555 else
8556 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008557 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8558 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008559 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008560 sysfs_notify_dirent_safe(rdev->sysfs_state);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008561 set_mask_bits(&mddev->flags, 0,
8562 BIT(MD_CHANGE_CLEAN) | BIT(MD_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008563 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008564 return 1;
8565 } else
8566 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008567}
8568EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8569
NeilBrownc6563a82012-05-21 09:27:00 +10008570int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8571 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008572{
NeilBrownc6563a82012-05-21 09:27:00 +10008573 if (is_new)
8574 s += rdev->new_data_offset;
8575 else
8576 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008577 return badblocks_clear(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008578 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008579}
8580EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8581
Adrian Bunk75c96f82005-05-05 16:16:09 -07008582static int md_notify_reboot(struct notifier_block *this,
8583 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584{
8585 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008586 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008587 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588
NeilBrownc744a652012-03-19 12:46:37 +11008589 for_each_mddev(mddev, tmp) {
8590 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008591 if (mddev->pers)
8592 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008593 if (mddev->persistent)
8594 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008595 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008596 }
NeilBrownc744a652012-03-19 12:46:37 +11008597 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598 }
NeilBrownc744a652012-03-19 12:46:37 +11008599 /*
8600 * certain more exotic SCSI devices are known to be
8601 * volatile wrt too early system reboots. While the
8602 * right place to handle this issue is the given
8603 * driver, we do want to have a safe RAID driver ...
8604 */
8605 if (need_delay)
8606 mdelay(1000*1);
8607
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 return NOTIFY_DONE;
8609}
8610
Adrian Bunk75c96f82005-05-05 16:16:09 -07008611static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612 .notifier_call = md_notify_reboot,
8613 .next = NULL,
8614 .priority = INT_MAX, /* before any real devices */
8615};
8616
8617static void md_geninit(void)
8618{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008619 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008620
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008621 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622}
8623
Adrian Bunk75c96f82005-05-05 16:16:09 -07008624static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625{
Tejun Heoe804ac72010-10-15 15:36:08 +02008626 int ret = -ENOMEM;
8627
Tejun Heoada609e2011-01-25 14:35:54 +01008628 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008629 if (!md_wq)
8630 goto err_wq;
8631
8632 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8633 if (!md_misc_wq)
8634 goto err_misc_wq;
8635
8636 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8637 goto err_md;
8638
8639 if ((ret = register_blkdev(0, "mdp")) < 0)
8640 goto err_mdp;
8641 mdp_major = ret;
8642
NeilBrownaf5628f2014-07-31 13:54:54 +10008643 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008644 md_probe, NULL, NULL);
8645 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 md_probe, NULL, NULL);
8647
Linus Torvalds1da177e2005-04-16 15:20:36 -07008648 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008649 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008650
8651 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653
Tejun Heoe804ac72010-10-15 15:36:08 +02008654err_mdp:
8655 unregister_blkdev(MD_MAJOR, "md");
8656err_md:
8657 destroy_workqueue(md_misc_wq);
8658err_misc_wq:
8659 destroy_workqueue(md_wq);
8660err_wq:
8661 return ret;
8662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008664static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008665{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008666 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8667 struct md_rdev *rdev2;
8668 int role, ret;
8669 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008670
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008671 /* Check for change of roles in the active devices */
8672 rdev_for_each(rdev2, mddev) {
8673 if (test_bit(Faulty, &rdev2->flags))
8674 continue;
8675
8676 /* Check if the roles changed */
8677 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008678
8679 if (test_bit(Candidate, &rdev2->flags)) {
8680 if (role == 0xfffe) {
8681 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8682 md_kick_rdev_from_array(rdev2);
8683 continue;
8684 }
8685 else
8686 clear_bit(Candidate, &rdev2->flags);
8687 }
8688
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008689 if (role != rdev2->raid_disk) {
8690 /* got activated */
8691 if (rdev2->raid_disk == -1 && role != 0xffff) {
8692 rdev2->saved_raid_disk = role;
8693 ret = remove_and_add_spares(mddev, rdev2);
8694 pr_info("Activated spare: %s\n",
8695 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04008696 /* wakeup mddev->thread here, so array could
8697 * perform resync with the new activated disk */
8698 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8699 md_wakeup_thread(mddev->thread);
8700
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008701 }
8702 /* device faulty
8703 * We just want to do the minimum to mark the disk
8704 * as faulty. The recovery is performed by the
8705 * one who initiated the error.
8706 */
8707 if ((role == 0xfffe) || (role == 0xfffd)) {
8708 md_error(mddev, rdev2);
8709 clear_bit(Blocked, &rdev2->flags);
8710 }
8711 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008712 }
8713
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11008714 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8715 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008716
8717 /* Finally set the event to be up to date */
8718 mddev->events = le64_to_cpu(sb->events);
8719}
8720
8721static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8722{
8723 int err;
8724 struct page *swapout = rdev->sb_page;
8725 struct mdp_superblock_1 *sb;
8726
8727 /* Store the sb page of the rdev in the swapout temporary
8728 * variable in case we err in the future
8729 */
8730 rdev->sb_page = NULL;
8731 alloc_disk_sb(rdev);
8732 ClearPageUptodate(rdev->sb_page);
8733 rdev->sb_loaded = 0;
8734 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
8735
8736 if (err < 0) {
8737 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8738 __func__, __LINE__, rdev->desc_nr, err);
8739 put_page(rdev->sb_page);
8740 rdev->sb_page = swapout;
8741 rdev->sb_loaded = 1;
8742 return err;
8743 }
8744
8745 sb = page_address(rdev->sb_page);
8746 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8747 * is not set
8748 */
8749
8750 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8751 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8752
8753 /* The other node finished recovery, call spare_active to set
8754 * device In_sync and mddev->degraded
8755 */
8756 if (rdev->recovery_offset == MaxSector &&
8757 !test_bit(In_sync, &rdev->flags) &&
8758 mddev->pers->spare_active(mddev))
8759 sysfs_notify(&mddev->kobj, NULL, "degraded");
8760
8761 put_page(swapout);
8762 return 0;
8763}
8764
8765void md_reload_sb(struct mddev *mddev, int nr)
8766{
8767 struct md_rdev *rdev;
8768 int err;
8769
8770 /* Find the rdev */
8771 rdev_for_each_rcu(rdev, mddev) {
8772 if (rdev->desc_nr == nr)
8773 break;
8774 }
8775
8776 if (!rdev || rdev->desc_nr != nr) {
8777 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
8778 return;
8779 }
8780
8781 err = read_rdev(mddev, rdev);
8782 if (err < 0)
8783 return;
8784
8785 check_sb_changes(mddev, rdev);
8786
8787 /* Read all rdev's to update recovery_offset */
8788 rdev_for_each_rcu(rdev, mddev)
8789 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008790}
8791EXPORT_SYMBOL(md_reload_sb);
8792
Linus Torvalds1da177e2005-04-16 15:20:36 -07008793#ifndef MODULE
8794
8795/*
8796 * Searches all registered partitions for autorun RAID arrays
8797 * at boot time.
8798 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008799
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008800static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008801static LIST_HEAD(all_detected_devices);
8802struct detected_devices_node {
8803 struct list_head list;
8804 dev_t dev;
8805};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008806
8807void md_autodetect_dev(dev_t dev)
8808{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008809 struct detected_devices_node *node_detected_dev;
8810
8811 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8812 if (node_detected_dev) {
8813 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008814 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008815 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008816 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008817 } else {
8818 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8819 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821}
8822
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823static void autostart_arrays(int part)
8824{
NeilBrown3cb03002011-10-11 16:45:26 +11008825 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008826 struct detected_devices_node *node_detected_dev;
8827 dev_t dev;
8828 int i_scanned, i_passed;
8829
8830 i_scanned = 0;
8831 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008832
8833 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8834
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008835 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008836 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8837 i_scanned++;
8838 node_detected_dev = list_entry(all_detected_devices.next,
8839 struct detected_devices_node, list);
8840 list_del(&node_detected_dev->list);
8841 dev = node_detected_dev->dev;
8842 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008843 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844 if (IS_ERR(rdev))
8845 continue;
8846
NeilBrown403df472014-09-30 15:52:29 +10008847 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008848 continue;
NeilBrown403df472014-09-30 15:52:29 +10008849
NeilBrownd0fae182008-03-04 14:29:31 -08008850 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008852 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008854 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008855
8856 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8857 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858
8859 autorun_devices(part);
8860}
8861
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008862#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863
8864static __exit void md_exit(void)
8865{
NeilBrownfd01b882011-10-11 16:47:53 +11008866 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008868 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008869
NeilBrownaf5628f2014-07-31 13:54:54 +10008870 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008871 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008873 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874 unregister_blkdev(mdp_major, "mdp");
8875 unregister_reboot_notifier(&md_notifier);
8876 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008877
8878 /* We cannot unload the modules while some process is
8879 * waiting for us in select() or poll() - wake them up
8880 */
8881 md_unloading = 1;
8882 while (waitqueue_active(&md_event_waiters)) {
8883 /* not safe to leave yet */
8884 wake_up(&md_event_waiters);
8885 msleep(delay);
8886 delay += delay;
8887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008889
NeilBrown29ac4aa2008-02-06 01:39:58 -08008890 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008892 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008894 destroy_workqueue(md_misc_wq);
8895 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896}
8897
Dan Williams685784a2007-07-09 11:56:42 -07008898subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899module_exit(md_exit)
8900
NeilBrownf91de922005-11-08 21:39:36 -08008901static int get_ro(char *buffer, struct kernel_param *kp)
8902{
8903 return sprintf(buffer, "%d", start_readonly);
8904}
8905static int set_ro(const char *val, struct kernel_param *kp)
8906{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03008907 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08008908}
8909
NeilBrown80ca3a42006-07-10 04:44:18 -07008910module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8911module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008912module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008913
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008915MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008916MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008917MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);