blob: 29297e97cdea68c0e9c4f963c3aef42250dd229a [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
NeilBrown850b2b42006-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 */
NeilBrown850b2b42006-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 */
NeilBrown850b2b42006-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);
NeilBrown850b2b42006-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
NeilBrown850b2b42006-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")) {
NeilBrown8430e7e2016-06-02 16:19:52 +10002598 clear_bit(Blocked, &rdev->flags);
2599 remove_and_add_spares(rdev->mddev, rdev);
NeilBrown45dc2de2006-06-26 00:27:58 -07002600 if (rdev->raid_disk >= 0)
2601 err = -EBUSY;
2602 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002603 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002604 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002605 if (mddev_is_clustered(mddev))
2606 err = md_cluster_ops->remove_disk(mddev, rdev);
2607
2608 if (err == 0) {
2609 md_kick_rdev_from_array(rdev);
2610 if (mddev->pers)
2611 md_update_sb(mddev, 1);
2612 md_new_event(mddev);
2613 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002614 }
NeilBrownf6556752006-06-26 00:28:01 -07002615 } else if (cmd_match(buf, "writemostly")) {
2616 set_bit(WriteMostly, &rdev->flags);
2617 err = 0;
2618 } else if (cmd_match(buf, "-writemostly")) {
2619 clear_bit(WriteMostly, &rdev->flags);
2620 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002621 } else if (cmd_match(buf, "blocked")) {
2622 set_bit(Blocked, &rdev->flags);
2623 err = 0;
2624 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002625 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002626 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002627 /* metadata handler doesn't understand badblocks,
2628 * so we need to fail the device
2629 */
2630 md_error(rdev->mddev, rdev);
2631 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002632 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002633 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002634 wake_up(&rdev->blocked_wait);
2635 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2636 md_wakeup_thread(rdev->mddev->thread);
2637
2638 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002639 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2640 set_bit(In_sync, &rdev->flags);
2641 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002642 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2643 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002644 if (rdev->mddev->pers == NULL) {
2645 clear_bit(In_sync, &rdev->flags);
2646 rdev->saved_raid_disk = rdev->raid_disk;
2647 rdev->raid_disk = -1;
2648 err = 0;
2649 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002650 } else if (cmd_match(buf, "write_error")) {
2651 set_bit(WriteErrorSeen, &rdev->flags);
2652 err = 0;
2653 } else if (cmd_match(buf, "-write_error")) {
2654 clear_bit(WriteErrorSeen, &rdev->flags);
2655 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002656 } else if (cmd_match(buf, "want_replacement")) {
2657 /* Any non-spare device that is not a replacement can
2658 * become want_replacement at any time, but we then need to
2659 * check if recovery is needed.
2660 */
2661 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002662 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002663 !test_bit(Replacement, &rdev->flags))
2664 set_bit(WantReplacement, &rdev->flags);
2665 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2666 md_wakeup_thread(rdev->mddev->thread);
2667 err = 0;
2668 } else if (cmd_match(buf, "-want_replacement")) {
2669 /* Clearing 'want_replacement' is always allowed.
2670 * Once replacements starts it is too late though.
2671 */
2672 err = 0;
2673 clear_bit(WantReplacement, &rdev->flags);
2674 } else if (cmd_match(buf, "replacement")) {
2675 /* Can only set a device as a replacement when array has not
2676 * yet been started. Once running, replacement is automatic
2677 * from spares, or by assigning 'slot'.
2678 */
2679 if (rdev->mddev->pers)
2680 err = -EBUSY;
2681 else {
2682 set_bit(Replacement, &rdev->flags);
2683 err = 0;
2684 }
2685 } else if (cmd_match(buf, "-replacement")) {
2686 /* Similarly, can only clear Replacement before start */
2687 if (rdev->mddev->pers)
2688 err = -EBUSY;
2689 else {
2690 clear_bit(Replacement, &rdev->flags);
2691 err = 0;
2692 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002693 } else if (cmd_match(buf, "re-add")) {
2694 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002695 /* clear_bit is performed _after_ all the devices
2696 * have their local Faulty bit cleared. If any writes
2697 * happen in the meantime in the local node, they
2698 * will land in the local bitmap, which will be synced
2699 * by this node eventually
2700 */
2701 if (!mddev_is_clustered(rdev->mddev) ||
2702 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2703 clear_bit(Faulty, &rdev->flags);
2704 err = add_bound_rdev(rdev);
2705 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002706 } else
2707 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002708 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002709 if (!err)
2710 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002711 return err ? err : len;
2712}
NeilBrown80ca3a42006-07-10 04:44:18 -07002713static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002714__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002715
2716static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002717errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002718{
2719 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2720}
2721
2722static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002723errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002724{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002725 unsigned int n;
2726 int rv;
2727
2728 rv = kstrtouint(buf, 10, &n);
2729 if (rv < 0)
2730 return rv;
2731 atomic_set(&rdev->corrected_errors, n);
2732 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002733}
2734static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002735__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002736
NeilBrown014236d2006-01-06 00:20:55 -08002737static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002738slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002739{
Shaohua Lif2076e72015-10-08 21:54:12 -07002740 if (test_bit(Journal, &rdev->flags))
2741 return sprintf(page, "journal\n");
2742 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002743 return sprintf(page, "none\n");
2744 else
2745 return sprintf(page, "%d\n", rdev->raid_disk);
2746}
2747
2748static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002749slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002750{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002751 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002752 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002753
Shaohua Lif2076e72015-10-08 21:54:12 -07002754 if (test_bit(Journal, &rdev->flags))
2755 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002756 if (strncmp(buf, "none", 4)==0)
2757 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002758 else {
2759 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2760 if (err < 0)
2761 return err;
2762 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002763 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002764 /* Setting 'slot' on an active array requires also
2765 * updating the 'rd%d' link, and communicating
2766 * with the personality with ->hot_*_disk.
2767 * For now we only support removing
2768 * failed/spare devices. This normally happens automatically,
2769 * but not when the metadata is externally managed.
2770 */
NeilBrownc303da62008-02-06 01:39:51 -08002771 if (rdev->raid_disk == -1)
2772 return -EEXIST;
2773 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002774 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002775 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002776 clear_bit(Blocked, &rdev->flags);
2777 remove_and_add_spares(rdev->mddev, rdev);
2778 if (rdev->raid_disk >= 0)
2779 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002780 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2781 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002782 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002783 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002784 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002785 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002786 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002787
2788 if (rdev->raid_disk != -1)
2789 return -EBUSY;
2790
NeilBrownc6751b22011-02-02 11:57:13 +11002791 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2792 return -EBUSY;
2793
Neil Brown6c2fce22008-06-28 08:31:31 +10002794 if (rdev->mddev->pers->hot_add_disk == NULL)
2795 return -EINVAL;
2796
NeilBrownba1b41b2011-01-14 09:14:34 +11002797 if (slot >= rdev->mddev->raid_disks &&
2798 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2799 return -ENOSPC;
2800
Neil Brown6c2fce22008-06-28 08:31:31 +10002801 rdev->raid_disk = slot;
2802 if (test_bit(In_sync, &rdev->flags))
2803 rdev->saved_raid_disk = slot;
2804 else
2805 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002806 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002807 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002808 err = rdev->mddev->pers->
2809 hot_add_disk(rdev->mddev, rdev);
2810 if (err) {
2811 rdev->raid_disk = -1;
2812 return err;
2813 } else
2814 sysfs_notify_dirent_safe(rdev->sysfs_state);
2815 if (sysfs_link_rdev(rdev->mddev, rdev))
2816 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002817 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002818 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002819 if (slot >= rdev->mddev->raid_disks &&
2820 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002821 return -ENOSPC;
2822 rdev->raid_disk = slot;
2823 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002824 clear_bit(Faulty, &rdev->flags);
2825 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002826 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002827 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002828 }
NeilBrown014236d2006-01-06 00:20:55 -08002829 return len;
2830}
2831
NeilBrown014236d2006-01-06 00:20:55 -08002832static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002833__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002834
NeilBrown93c8cad2006-01-06 00:20:56 -08002835static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002836offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002837{
Andrew Morton6961ece2006-01-06 00:20:59 -08002838 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002839}
2840
2841static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002842offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002843{
NeilBrownc6563a82012-05-21 09:27:00 +10002844 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002845 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002846 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002847 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002848 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002849 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002850 /* Must set offset before size, so overlap checks
2851 * can be sane */
2852 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002853 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002854 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002855 return len;
2856}
2857
2858static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002859__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002860
NeilBrownc6563a82012-05-21 09:27:00 +10002861static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2862{
2863 return sprintf(page, "%llu\n",
2864 (unsigned long long)rdev->new_data_offset);
2865}
2866
2867static ssize_t new_offset_store(struct md_rdev *rdev,
2868 const char *buf, size_t len)
2869{
2870 unsigned long long new_offset;
2871 struct mddev *mddev = rdev->mddev;
2872
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002873 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002874 return -EINVAL;
2875
NeilBrownf851b602014-12-11 10:02:10 +11002876 if (mddev->sync_thread ||
2877 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002878 return -EBUSY;
2879 if (new_offset == rdev->data_offset)
2880 /* reset is always permitted */
2881 ;
2882 else if (new_offset > rdev->data_offset) {
2883 /* must not push array size beyond rdev_sectors */
2884 if (new_offset - rdev->data_offset
2885 + mddev->dev_sectors > rdev->sectors)
2886 return -E2BIG;
2887 }
2888 /* Metadata worries about other space details. */
2889
2890 /* decreasing the offset is inconsistent with a backwards
2891 * reshape.
2892 */
2893 if (new_offset < rdev->data_offset &&
2894 mddev->reshape_backwards)
2895 return -EINVAL;
2896 /* Increasing offset is inconsistent with forwards
2897 * reshape. reshape_direction should be set to
2898 * 'backwards' first.
2899 */
2900 if (new_offset > rdev->data_offset &&
2901 !mddev->reshape_backwards)
2902 return -EINVAL;
2903
2904 if (mddev->pers && mddev->persistent &&
2905 !super_types[mddev->major_version]
2906 .allow_new_offset(rdev, new_offset))
2907 return -E2BIG;
2908 rdev->new_data_offset = new_offset;
2909 if (new_offset > rdev->data_offset)
2910 mddev->reshape_backwards = 1;
2911 else if (new_offset < rdev->data_offset)
2912 mddev->reshape_backwards = 0;
2913
2914 return len;
2915}
2916static struct rdev_sysfs_entry rdev_new_offset =
2917__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2918
NeilBrown83303b62006-01-06 00:21:06 -08002919static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002920rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002921{
Andre Nolldd8ac332009-03-31 14:33:13 +11002922 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002923}
2924
NeilBrownc5d79ad2008-02-06 01:39:54 -08002925static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2926{
2927 /* check if two start/length pairs overlap */
2928 if (s1+l1 <= s2)
2929 return 0;
2930 if (s2+l2 <= s1)
2931 return 0;
2932 return 1;
2933}
2934
Dan Williamsb522adc2009-03-31 15:00:31 +11002935static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2936{
2937 unsigned long long blocks;
2938 sector_t new;
2939
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002940 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002941 return -EINVAL;
2942
2943 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2944 return -EINVAL; /* sector conversion overflow */
2945
2946 new = blocks * 2;
2947 if (new != blocks * 2)
2948 return -EINVAL; /* unsigned long long to sector_t overflow */
2949
2950 *sectors = new;
2951 return 0;
2952}
2953
NeilBrown83303b62006-01-06 00:21:06 -08002954static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002955rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002956{
NeilBrownfd01b882011-10-11 16:47:53 +11002957 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002958 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002959 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002960
Shaohua Lif2076e72015-10-08 21:54:12 -07002961 if (test_bit(Journal, &rdev->flags))
2962 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002963 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002964 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002965 if (rdev->data_offset != rdev->new_data_offset)
2966 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002967 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002968 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002969 sectors = super_types[my_mddev->major_version].
2970 rdev_size_change(rdev, sectors);
2971 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002972 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002973 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002974 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002975 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002976 if (!my_mddev->pers->resize)
2977 /* Cannot change size for RAID0 or Linear etc */
2978 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002979 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002980 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002981 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002982
Andre Nolldd8ac332009-03-31 14:33:13 +11002983 rdev->sectors = sectors;
2984 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002985 /* Need to check that all other rdevs with the same
2986 * ->bdev do not overlap. 'rcu' is sufficient to walk
2987 * the rdev lists safely.
2988 * This check does not provide a hard guarantee, it
2989 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002990 */
NeilBrownfd01b882011-10-11 16:47:53 +11002991 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002992 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002993 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002994
NeilBrown8b1afc32014-09-29 15:33:20 +10002995 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002996 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002997 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002998
NeilBrowndafb20f2012-03-19 12:46:39 +11002999 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003000 if (rdev->bdev == rdev2->bdev &&
3001 rdev != rdev2 &&
3002 overlaps(rdev->data_offset, rdev->sectors,
3003 rdev2->data_offset,
3004 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003005 overlap = 1;
3006 break;
3007 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003008 if (overlap) {
3009 mddev_put(mddev);
3010 break;
3011 }
3012 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003013 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003014 if (overlap) {
3015 /* Someone else could have slipped in a size
3016 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003017 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003018 * safe, and trust userspace not to race with
3019 * itself
3020 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003021 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003022 return -EBUSY;
3023 }
3024 }
NeilBrown83303b62006-01-06 00:21:06 -08003025 return len;
3026}
3027
3028static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003029__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003030
NeilBrown3cb03002011-10-11 16:45:26 +11003031static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003032{
3033 unsigned long long recovery_start = rdev->recovery_offset;
3034
3035 if (test_bit(In_sync, &rdev->flags) ||
3036 recovery_start == MaxSector)
3037 return sprintf(page, "none\n");
3038
3039 return sprintf(page, "%llu\n", recovery_start);
3040}
3041
NeilBrown3cb03002011-10-11 16:45:26 +11003042static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003043{
3044 unsigned long long recovery_start;
3045
3046 if (cmd_match(buf, "none"))
3047 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003048 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003049 return -EINVAL;
3050
3051 if (rdev->mddev->pers &&
3052 rdev->raid_disk >= 0)
3053 return -EBUSY;
3054
3055 rdev->recovery_offset = recovery_start;
3056 if (recovery_start == MaxSector)
3057 set_bit(In_sync, &rdev->flags);
3058 else
3059 clear_bit(In_sync, &rdev->flags);
3060 return len;
3061}
3062
3063static struct rdev_sysfs_entry rdev_recovery_start =
3064__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3065
Vishal Vermafc974ee2015-12-24 19:20:34 -07003066/* sysfs access to bad-blocks list.
3067 * We present two files.
3068 * 'bad-blocks' lists sector numbers and lengths of ranges that
3069 * are recorded as bad. The list is truncated to fit within
3070 * the one-page limit of sysfs.
3071 * Writing "sector length" to this file adds an acknowledged
3072 * bad block list.
3073 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3074 * been acknowledged. Writing to this file adds bad blocks
3075 * without acknowledging them. This is largely for testing.
3076 */
NeilBrown3cb03002011-10-11 16:45:26 +11003077static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003078{
3079 return badblocks_show(&rdev->badblocks, page, 0);
3080}
NeilBrown3cb03002011-10-11 16:45:26 +11003081static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003082{
NeilBrownde393cd2011-07-28 11:31:48 +10003083 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3084 /* Maybe that ack was all we needed */
3085 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3086 wake_up(&rdev->blocked_wait);
3087 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003088}
3089static struct rdev_sysfs_entry rdev_bad_blocks =
3090__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3091
NeilBrown3cb03002011-10-11 16:45:26 +11003092static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003093{
3094 return badblocks_show(&rdev->badblocks, page, 1);
3095}
NeilBrown3cb03002011-10-11 16:45:26 +11003096static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003097{
3098 return badblocks_store(&rdev->badblocks, page, len, 1);
3099}
3100static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3101__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3102
NeilBrown86e6ffd2005-11-08 21:39:24 -08003103static struct attribute *rdev_default_attrs[] = {
3104 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003105 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003106 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003107 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003108 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003109 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003110 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003111 &rdev_bad_blocks.attr,
3112 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003113 NULL,
3114};
3115static ssize_t
3116rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3117{
3118 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003119 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003120
3121 if (!entry->show)
3122 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003123 if (!rdev->mddev)
3124 return -EBUSY;
3125 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003126}
3127
3128static ssize_t
3129rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3130 const char *page, size_t length)
3131{
3132 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003133 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003134 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003135 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003136
3137 if (!entry->store)
3138 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003139 if (!capable(CAP_SYS_ADMIN))
3140 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003141 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003142 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003143 if (rdev->mddev == NULL)
3144 rv = -EBUSY;
3145 else
3146 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003147 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003148 }
3149 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003150}
3151
3152static void rdev_free(struct kobject *ko)
3153{
NeilBrown3cb03002011-10-11 16:45:26 +11003154 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003155 kfree(rdev);
3156}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003157static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003158 .show = rdev_attr_show,
3159 .store = rdev_attr_store,
3160};
3161static struct kobj_type rdev_ktype = {
3162 .release = rdev_free,
3163 .sysfs_ops = &rdev_sysfs_ops,
3164 .default_attrs = rdev_default_attrs,
3165};
3166
NeilBrown3cb03002011-10-11 16:45:26 +11003167int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003168{
3169 rdev->desc_nr = -1;
3170 rdev->saved_raid_disk = -1;
3171 rdev->raid_disk = -1;
3172 rdev->flags = 0;
3173 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003174 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003175 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003176 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003177 rdev->sb_loaded = 0;
3178 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003179 atomic_set(&rdev->nr_pending, 0);
3180 atomic_set(&rdev->read_errors, 0);
3181 atomic_set(&rdev->corrected_errors, 0);
3182
3183 INIT_LIST_HEAD(&rdev->same_set);
3184 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003185
3186 /* Add space to store bad block list.
3187 * This reserves the space even on arrays where it cannot
3188 * be used - I wonder if that matters
3189 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003190 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003191}
3192EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193/*
3194 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3195 *
3196 * mark the device faulty if:
3197 *
3198 * - the device is nonexistent (zero size)
3199 * - the device has no valid superblock
3200 *
3201 * a faulty rdev _never_ has rdev->sb set.
3202 */
NeilBrown3cb03002011-10-11 16:45:26 +11003203static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
3205 char b[BDEVNAME_SIZE];
3206 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003207 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 sector_t size;
3209
NeilBrown9ffae0c2006-01-06 00:20:32 -08003210 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (!rdev) {
3212 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3213 return ERR_PTR(-ENOMEM);
3214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
NeilBrown2230dfe2011-07-28 11:31:46 +10003216 err = md_rdev_init(rdev);
3217 if (err)
3218 goto abort_free;
3219 err = alloc_disk_sb(rdev);
3220 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 goto abort_free;
3222
NeilBrownc5d79ad2008-02-06 01:39:54 -08003223 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (err)
3225 goto abort_free;
3226
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003227 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003228
Mike Snitzer77304d22010-11-08 14:39:12 +01003229 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003231 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 "md: %s has zero or unknown size, marking faulty!\n",
3233 bdevname(rdev->bdev,b));
3234 err = -EINVAL;
3235 goto abort_free;
3236 }
3237
3238 if (super_format >= 0) {
3239 err = super_types[super_format].
3240 load_super(rdev, NULL, super_minor);
3241 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003242 printk(KERN_WARNING
3243 "md: %s does not have a valid v%d.%d "
3244 "superblock, not importing!\n",
3245 bdevname(rdev->bdev,b),
3246 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 goto abort_free;
3248 }
3249 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003250 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 "md: could not read %s's sb, not importing!\n",
3252 bdevname(rdev->bdev,b));
3253 goto abort_free;
3254 }
3255 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return rdev;
3258
3259abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003260 if (rdev->bdev)
3261 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003262 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 kfree(rdev);
3264 return ERR_PTR(err);
3265}
3266
3267/*
3268 * Check a full RAID array for plausibility
3269 */
3270
NeilBrownf72ffdd2014-09-30 14:23:59 +10003271static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
3273 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003274 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 char b[BDEVNAME_SIZE];
3276
3277 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003278 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 switch (super_types[mddev->major_version].
3280 load_super(rdev, freshest, mddev->minor_version)) {
3281 case 1:
3282 freshest = rdev;
3283 break;
3284 case 0:
3285 break;
3286 default:
3287 printk( KERN_ERR \
3288 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003289 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003291 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 }
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 super_types[mddev->major_version].
3295 validate_super(mddev, freshest);
3296
3297 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003298 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003299 if (mddev->max_disks &&
3300 (rdev->desc_nr >= mddev->max_disks ||
3301 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003302 printk(KERN_WARNING
3303 "md: %s: %s: only %d devices permitted\n",
3304 mdname(mddev), bdevname(rdev->bdev, b),
3305 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003306 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003307 continue;
3308 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003309 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 if (super_types[mddev->major_version].
3311 validate_super(mddev, rdev)) {
3312 printk(KERN_WARNING "md: kicking non-fresh %s"
3313 " from array!\n",
3314 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003315 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 continue;
3317 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 if (mddev->level == LEVEL_MULTIPATH) {
3320 rdev->desc_nr = i++;
3321 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003322 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003323 } else if (rdev->raid_disk >=
3324 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3325 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003326 rdev->raid_disk = -1;
3327 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 }
3329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
NeilBrown72e02072009-12-14 12:49:55 +11003332/* Read a fixed-point number.
3333 * Numbers in sysfs attributes should be in "standard" units where
3334 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003335 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003336 * milliseconds or jiffies.
3337 * This function takes a decimal number with a possible fractional
3338 * component, and produces an integer which is the result of
3339 * multiplying that number by 10^'scale'.
3340 * all without any floating-point arithmetic.
3341 */
3342int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3343{
3344 unsigned long result = 0;
3345 long decimals = -1;
3346 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3347 if (*cp == '.')
3348 decimals = 0;
3349 else if (decimals < scale) {
3350 unsigned int value;
3351 value = *cp - '0';
3352 result = result * 10 + value;
3353 if (decimals >= 0)
3354 decimals++;
3355 }
3356 cp++;
3357 }
3358 if (*cp == '\n')
3359 cp++;
3360 if (*cp)
3361 return -EINVAL;
3362 if (decimals < 0)
3363 decimals = 0;
3364 while (decimals < scale) {
3365 result *= 10;
3366 decimals ++;
3367 }
3368 *res = result;
3369 return 0;
3370}
3371
NeilBrowneae17012005-11-08 21:39:23 -08003372static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003373safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003374{
3375 int msec = (mddev->safemode_delay*1000)/HZ;
3376 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3377}
3378static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003379safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003380{
NeilBrown16f17b32006-06-26 00:27:37 -07003381 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003382
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003383 if (mddev_is_clustered(mddev)) {
3384 pr_info("md: Safemode is disabled for clustered mode\n");
3385 return -EINVAL;
3386 }
3387
NeilBrown72e02072009-12-14 12:49:55 +11003388 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003389 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003390 if (msec == 0)
3391 mddev->safemode_delay = 0;
3392 else {
NeilBrown19052c02008-08-05 15:54:13 +10003393 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003394 unsigned long new_delay = (msec*HZ)/1000;
3395
3396 if (new_delay == 0)
3397 new_delay = 1;
3398 mddev->safemode_delay = new_delay;
3399 if (new_delay < old_delay || old_delay == 0)
3400 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003401 }
3402 return len;
3403}
3404static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003405__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003406
3407static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003408level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003409{
NeilBrown36d091f2014-12-15 12:56:58 +11003410 struct md_personality *p;
3411 int ret;
3412 spin_lock(&mddev->lock);
3413 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003414 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003415 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003416 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003417 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003418 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003419 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003420 else
NeilBrown36d091f2014-12-15 12:56:58 +11003421 ret = 0;
3422 spin_unlock(&mddev->lock);
3423 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003424}
3425
NeilBrownd9d166c2006-01-06 00:20:51 -08003426static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003427level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003428{
Dan Williamsf2859af2010-05-02 10:04:16 -07003429 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003430 ssize_t rv;
3431 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003432 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003433 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003434 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003435 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003436
NeilBrown67918752014-12-15 12:57:01 +11003437 if (slen == 0 || slen >= sizeof(clevel))
3438 return -EINVAL;
3439
3440 rv = mddev_lock(mddev);
3441 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003442 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003443
3444 if (mddev->pers == NULL) {
3445 strncpy(mddev->clevel, buf, slen);
3446 if (mddev->clevel[slen-1] == '\n')
3447 slen--;
3448 mddev->clevel[slen] = 0;
3449 mddev->level = LEVEL_NONE;
3450 rv = len;
3451 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003452 }
NeilBrown67918752014-12-15 12:57:01 +11003453 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003454 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003455 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003456
3457 /* request to change the personality. Need to ensure:
3458 * - array is not engaged in resync/recovery/reshape
3459 * - old personality can be suspended
3460 * - new personality will access other array.
3461 */
3462
NeilBrown67918752014-12-15 12:57:01 +11003463 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003464 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003465 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003466 mddev->reshape_position != MaxSector ||
3467 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003468 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003469
NeilBrown67918752014-12-15 12:57:01 +11003470 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003471 if (!mddev->pers->quiesce) {
3472 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3473 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003474 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003475 }
3476
3477 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003478 strncpy(clevel, buf, slen);
3479 if (clevel[slen-1] == '\n')
3480 slen--;
3481 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003482 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003483 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003484
Dan Williamsf2859af2010-05-02 10:04:16 -07003485 if (request_module("md-%s", clevel) != 0)
3486 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003487 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003488 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003489 if (!pers || !try_module_get(pers->owner)) {
3490 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003491 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003492 rv = -EINVAL;
3493 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003494 }
3495 spin_unlock(&pers_lock);
3496
3497 if (pers == mddev->pers) {
3498 /* Nothing to do! */
3499 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003500 rv = len;
3501 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003502 }
3503 if (!pers->takeover) {
3504 module_put(pers->owner);
3505 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003506 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003507 rv = -EINVAL;
3508 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003509 }
3510
NeilBrowndafb20f2012-03-19 12:46:39 +11003511 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003512 rdev->new_raid_disk = rdev->raid_disk;
3513
NeilBrown245f46c2009-03-31 14:39:39 +11003514 /* ->takeover must set new_* and/or delta_disks
3515 * if it succeeds, and may set them when it fails.
3516 */
3517 priv = pers->takeover(mddev);
3518 if (IS_ERR(priv)) {
3519 mddev->new_level = mddev->level;
3520 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003521 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003522 mddev->raid_disks -= mddev->delta_disks;
3523 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003524 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003525 module_put(pers->owner);
3526 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003527 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003528 rv = PTR_ERR(priv);
3529 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003530 }
3531
3532 /* Looks like we have a winner */
3533 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003534 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003535
3536 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003537 oldpers = mddev->pers;
3538 oldpriv = mddev->private;
3539 mddev->pers = pers;
3540 mddev->private = priv;
3541 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3542 mddev->level = mddev->new_level;
3543 mddev->layout = mddev->new_layout;
3544 mddev->chunk_sectors = mddev->new_chunk_sectors;
3545 mddev->delta_disks = 0;
3546 mddev->reshape_backwards = 0;
3547 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003548 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003549
NeilBrowndb721d32014-12-15 12:56:58 +11003550 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003551 mddev->external) {
3552 /* We are converting from a no-redundancy array
3553 * to a redundancy array and metadata is managed
3554 * externally so we need to be sure that writes
3555 * won't block due to a need to transition
3556 * clean->dirty
3557 * until external management is started.
3558 */
3559 mddev->in_sync = 0;
3560 mddev->safemode_delay = 0;
3561 mddev->safemode = 0;
3562 }
3563
NeilBrowndb721d32014-12-15 12:56:58 +11003564 oldpers->free(mddev, oldpriv);
3565
3566 if (oldpers->sync_request == NULL &&
3567 pers->sync_request != NULL) {
3568 /* need to add the md_redundancy_group */
3569 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3570 printk(KERN_WARNING
3571 "md: cannot register extra attributes for %s\n",
3572 mdname(mddev));
3573 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3574 }
3575 if (oldpers->sync_request != NULL &&
3576 pers->sync_request == NULL) {
3577 /* need to remove the md_redundancy_group */
3578 if (mddev->to_remove == NULL)
3579 mddev->to_remove = &md_redundancy_group;
3580 }
3581
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003582 module_put(oldpers->owner);
3583
NeilBrowndafb20f2012-03-19 12:46:39 +11003584 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003585 if (rdev->raid_disk < 0)
3586 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003587 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003588 rdev->new_raid_disk = -1;
3589 if (rdev->new_raid_disk == rdev->raid_disk)
3590 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003591 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003592 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003593 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003594 if (rdev->raid_disk < 0)
3595 continue;
3596 if (rdev->new_raid_disk == rdev->raid_disk)
3597 continue;
3598 rdev->raid_disk = rdev->new_raid_disk;
3599 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003600 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003601 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003602 if (sysfs_link_rdev(mddev, rdev))
3603 printk(KERN_WARNING "md: cannot register rd%d"
3604 " for %s after level change\n",
3605 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003606 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003607 }
3608
NeilBrowndb721d32014-12-15 12:56:58 +11003609 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003610 /* this is now an array without redundancy, so
3611 * it must always be in_sync
3612 */
3613 mddev->in_sync = 1;
3614 del_timer_sync(&mddev->safemode_timer);
3615 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003616 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003617 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003618 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003619 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003620 if (!mddev->thread)
3621 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003622 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003623 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003624 rv = len;
3625out_unlock:
3626 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003627 return rv;
3628}
3629
3630static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003631__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003632
NeilBrownd4dbd022006-06-26 00:27:59 -07003633static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003634layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003635{
3636 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003637 if (mddev->reshape_position != MaxSector &&
3638 mddev->layout != mddev->new_layout)
3639 return sprintf(page, "%d (%d)\n",
3640 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003641 return sprintf(page, "%d\n", mddev->layout);
3642}
3643
3644static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003645layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003646{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003647 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003648 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003649
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003650 err = kstrtouint(buf, 10, &n);
3651 if (err < 0)
3652 return err;
NeilBrown67918752014-12-15 12:57:01 +11003653 err = mddev_lock(mddev);
3654 if (err)
3655 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003656
NeilBrownb3546032009-03-31 14:56:41 +11003657 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003658 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003659 err = -EBUSY;
3660 else if (mddev->ro)
3661 err = -EROFS;
3662 else {
3663 mddev->new_layout = n;
3664 err = mddev->pers->check_reshape(mddev);
3665 if (err)
3666 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003667 }
NeilBrownb3546032009-03-31 14:56:41 +11003668 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003669 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003670 if (mddev->reshape_position == MaxSector)
3671 mddev->layout = n;
3672 }
NeilBrown67918752014-12-15 12:57:01 +11003673 mddev_unlock(mddev);
3674 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003675}
3676static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003677__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003678
NeilBrowneae17012005-11-08 21:39:23 -08003679static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003680raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003681{
NeilBrownbb636542005-11-08 21:39:45 -08003682 if (mddev->raid_disks == 0)
3683 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003684 if (mddev->reshape_position != MaxSector &&
3685 mddev->delta_disks != 0)
3686 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3687 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003688 return sprintf(page, "%d\n", mddev->raid_disks);
3689}
3690
NeilBrownfd01b882011-10-11 16:47:53 +11003691static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003692
3693static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003694raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003695{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003696 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003697 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003698
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003699 err = kstrtouint(buf, 10, &n);
3700 if (err < 0)
3701 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003702
NeilBrown67918752014-12-15 12:57:01 +11003703 err = mddev_lock(mddev);
3704 if (err)
3705 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003706 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003707 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003708 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003709 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003710 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003711
NeilBrown67918752014-12-15 12:57:01 +11003712 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003713 rdev_for_each(rdev, mddev) {
3714 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 if (olddisks > n &&
3718 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003719 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003720 }
NeilBrown67918752014-12-15 12:57:01 +11003721 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003722 mddev->delta_disks = n - olddisks;
3723 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003724 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003725 } else
NeilBrownda943b992006-01-06 00:20:54 -08003726 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003727out_unlock:
3728 mddev_unlock(mddev);
3729 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003730}
3731static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003732__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003733
NeilBrown24dd4692005-11-08 21:39:26 -08003734static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003735chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003736{
NeilBrown08a02ec2007-05-09 02:35:38 -07003737 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003738 mddev->chunk_sectors != mddev->new_chunk_sectors)
3739 return sprintf(page, "%d (%d)\n",
3740 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003741 mddev->chunk_sectors << 9);
3742 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003743}
3744
3745static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003746chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003747{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003748 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003749 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003750
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003751 err = kstrtoul(buf, 10, &n);
3752 if (err < 0)
3753 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003754
NeilBrown67918752014-12-15 12:57:01 +11003755 err = mddev_lock(mddev);
3756 if (err)
3757 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003758 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003759 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003760 err = -EBUSY;
3761 else if (mddev->ro)
3762 err = -EROFS;
3763 else {
3764 mddev->new_chunk_sectors = n >> 9;
3765 err = mddev->pers->check_reshape(mddev);
3766 if (err)
3767 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003768 }
NeilBrownb3546032009-03-31 14:56:41 +11003769 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003770 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003771 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003772 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003773 }
NeilBrown67918752014-12-15 12:57:01 +11003774 mddev_unlock(mddev);
3775 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003776}
3777static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003778__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003779
NeilBrowna94213b2006-06-26 00:28:00 -07003780static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003781resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003782{
NeilBrownd1a7c502009-03-31 15:24:32 +11003783 if (mddev->recovery_cp == MaxSector)
3784 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003785 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3786}
3787
3788static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003789resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003790{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003791 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003792 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003793
3794 if (cmd_match(buf, "none"))
3795 n = MaxSector;
3796 else {
3797 err = kstrtoull(buf, 10, &n);
3798 if (err < 0)
3799 return err;
3800 if (n != (sector_t)n)
3801 return -EINVAL;
3802 }
NeilBrowna94213b2006-06-26 00:28:00 -07003803
NeilBrown67918752014-12-15 12:57:01 +11003804 err = mddev_lock(mddev);
3805 if (err)
3806 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003807 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003808 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003809
NeilBrown67918752014-12-15 12:57:01 +11003810 if (!err) {
3811 mddev->recovery_cp = n;
3812 if (mddev->pers)
3813 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3814 }
3815 mddev_unlock(mddev);
3816 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003817}
3818static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003819__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3820 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003821
NeilBrown9e653b62006-06-26 00:27:58 -07003822/*
3823 * The array state can be:
3824 *
3825 * clear
3826 * No devices, no size, no level
3827 * Equivalent to STOP_ARRAY ioctl
3828 * inactive
3829 * May have some settings, but array is not active
3830 * all IO results in error
3831 * When written, doesn't tear down array, but just stops it
3832 * suspended (not supported yet)
3833 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003834 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003835 * readonly
3836 * no resync can happen. no superblocks get written.
3837 * write requests fail
3838 * read-auto
3839 * like readonly, but behaves like 'clean' on a write request.
3840 *
3841 * clean - no pending writes, but otherwise active.
3842 * When written to inactive array, starts without resync
3843 * If a write request arrives then
3844 * if metadata is known, mark 'dirty' and switch to 'active'.
3845 * if not known, block and switch to write-pending
3846 * If written to an active array that has pending writes, then fails.
3847 * active
3848 * fully active: IO and resync can be happening.
3849 * When written to inactive array, starts with resync
3850 *
3851 * write-pending
3852 * clean, but writes are blocked waiting for 'active' to be written.
3853 *
3854 * active-idle
3855 * like active, but no writes have been seen for a while (100msec).
3856 *
3857 */
3858enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3859 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003860static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003861 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3862 "write-pending", "active-idle", NULL };
3863
3864static int match_word(const char *word, char **list)
3865{
3866 int n;
3867 for (n=0; list[n]; n++)
3868 if (cmd_match(word, list[n]))
3869 break;
3870 return n;
3871}
3872
3873static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003874array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003875{
3876 enum array_state st = inactive;
3877
3878 if (mddev->pers)
3879 switch(mddev->ro) {
3880 case 1:
3881 st = readonly;
3882 break;
3883 case 2:
3884 st = read_auto;
3885 break;
3886 case 0:
3887 if (mddev->in_sync)
3888 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003889 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003890 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003891 else if (mddev->safemode)
3892 st = active_idle;
3893 else
3894 st = active;
3895 }
3896 else {
3897 if (list_empty(&mddev->disks) &&
3898 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003899 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003900 st = clear;
3901 else
3902 st = inactive;
3903 }
3904 return sprintf(page, "%s\n", array_states[st]);
3905}
3906
NeilBrownf72ffdd2014-09-30 14:23:59 +10003907static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3908static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3909static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003910static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003911
3912static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003913array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003914{
NeilBrown67918752014-12-15 12:57:01 +11003915 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003916 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003917
3918 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3919 /* don't take reconfig_mutex when toggling between
3920 * clean and active
3921 */
3922 spin_lock(&mddev->lock);
3923 if (st == active) {
3924 restart_array(mddev);
3925 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3926 wake_up(&mddev->sb_wait);
3927 err = 0;
3928 } else /* st == clean */ {
3929 restart_array(mddev);
3930 if (atomic_read(&mddev->writes_pending) == 0) {
3931 if (mddev->in_sync == 0) {
3932 mddev->in_sync = 1;
3933 if (mddev->safemode == 1)
3934 mddev->safemode = 0;
3935 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3936 }
3937 err = 0;
3938 } else
3939 err = -EBUSY;
3940 }
3941 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003942 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003943 }
3944 err = mddev_lock(mddev);
3945 if (err)
3946 return err;
3947 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003948 switch(st) {
3949 case bad_word:
3950 break;
3951 case clear:
3952 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003953 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003954 break;
3955 case inactive:
3956 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003957 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003958 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003959 else
NeilBrowne6910632008-02-06 01:39:51 -08003960 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003961 break;
3962 case suspended:
3963 break; /* not supported yet */
3964 case readonly:
3965 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003966 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003967 else {
3968 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003969 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003970 err = do_md_run(mddev);
3971 }
3972 break;
3973 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003974 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003975 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003976 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003977 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003978 err = restart_array(mddev);
3979 if (err == 0) {
3980 mddev->ro = 2;
3981 set_disk_ro(mddev->gendisk, 0);
3982 }
NeilBrown9e653b62006-06-26 00:27:58 -07003983 } else {
3984 mddev->ro = 2;
3985 err = do_md_run(mddev);
3986 }
3987 break;
3988 case clean:
3989 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003990 err = restart_array(mddev);
3991 if (err)
3992 break;
NeilBrown85572d72014-12-15 12:56:56 +11003993 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003994 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003995 if (mddev->in_sync == 0) {
3996 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003997 if (mddev->safemode == 1)
3998 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003999 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08004000 }
4001 err = 0;
4002 } else
4003 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004004 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004005 } else
4006 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004007 break;
4008 case active:
4009 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004010 err = restart_array(mddev);
4011 if (err)
4012 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10004013 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004014 wake_up(&mddev->sb_wait);
4015 err = 0;
4016 } else {
4017 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004018 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004019 err = do_md_run(mddev);
4020 }
4021 break;
4022 case write_pending:
4023 case active_idle:
4024 /* these cannot be set */
4025 break;
4026 }
NeilBrown67918752014-12-15 12:57:01 +11004027
4028 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004029 if (mddev->hold_active == UNTIL_IOCTL)
4030 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004031 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004032 }
NeilBrown67918752014-12-15 12:57:01 +11004033 mddev_unlock(mddev);
4034 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004035}
NeilBrown80ca3a42006-07-10 04:44:18 -07004036static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004037__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004038
NeilBrown6d7ff7382006-01-06 00:21:16 -08004039static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004040max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004041 return sprintf(page, "%d\n",
4042 atomic_read(&mddev->max_corr_read_errors));
4043}
4044
4045static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004046max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004047{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004048 unsigned int n;
4049 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004050
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004051 rv = kstrtouint(buf, 10, &n);
4052 if (rv < 0)
4053 return rv;
4054 atomic_set(&mddev->max_corr_read_errors, n);
4055 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004056}
4057
4058static struct md_sysfs_entry max_corr_read_errors =
4059__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4060 max_corrected_read_errors_store);
4061
4062static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004063null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004064{
4065 return -EINVAL;
4066}
4067
4068static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004069new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004070{
4071 /* buf must be %d:%d\n? giving major and minor numbers */
4072 /* The new device is added to the array.
4073 * If the array has a persistent superblock, we read the
4074 * superblock to initialise info and check validity.
4075 * Otherwise, only checking done is that in bind_rdev_to_array,
4076 * which mainly checks size.
4077 */
4078 char *e;
4079 int major = simple_strtoul(buf, &e, 10);
4080 int minor;
4081 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004082 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004083 int err;
4084
4085 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4086 return -EINVAL;
4087 minor = simple_strtoul(e+1, &e, 10);
4088 if (*e && *e != '\n')
4089 return -EINVAL;
4090 dev = MKDEV(major, minor);
4091 if (major != MAJOR(dev) ||
4092 minor != MINOR(dev))
4093 return -EOVERFLOW;
4094
NeilBrown67918752014-12-15 12:57:01 +11004095 flush_workqueue(md_misc_wq);
4096
4097 err = mddev_lock(mddev);
4098 if (err)
4099 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004100 if (mddev->persistent) {
4101 rdev = md_import_device(dev, mddev->major_version,
4102 mddev->minor_version);
4103 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004104 struct md_rdev *rdev0
4105 = list_entry(mddev->disks.next,
4106 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004107 err = super_types[mddev->major_version]
4108 .load_super(rdev, rdev0, mddev->minor_version);
4109 if (err < 0)
4110 goto out;
4111 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004112 } else if (mddev->external)
4113 rdev = md_import_device(dev, -2, -1);
4114 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004115 rdev = md_import_device(dev, -1, -1);
4116
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004117 if (IS_ERR(rdev)) {
4118 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004119 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004120 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004121 err = bind_rdev_to_array(rdev, mddev);
4122 out:
4123 if (err)
4124 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004125 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004126 return err ? err : len;
4127}
4128
4129static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004130__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004131
4132static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004133bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004134{
4135 char *end;
4136 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004137 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004138
NeilBrown67918752014-12-15 12:57:01 +11004139 err = mddev_lock(mddev);
4140 if (err)
4141 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004142 if (!mddev->bitmap)
4143 goto out;
4144 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4145 while (*buf) {
4146 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4147 if (buf == end) break;
4148 if (*end == '-') { /* range */
4149 buf = end + 1;
4150 end_chunk = simple_strtoul(buf, &end, 0);
4151 if (buf == end) break;
4152 }
4153 if (*end && !isspace(*end)) break;
4154 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004155 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004156 }
4157 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4158out:
NeilBrown67918752014-12-15 12:57:01 +11004159 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004160 return len;
4161}
4162
4163static struct md_sysfs_entry md_bitmap =
4164__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4165
4166static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004167size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004168{
Andre Noll58c0fed2009-03-31 14:33:13 +11004169 return sprintf(page, "%llu\n",
4170 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004171}
4172
NeilBrownfd01b882011-10-11 16:47:53 +11004173static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004174
4175static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004176size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004177{
4178 /* If array is inactive, we can reduce the component size, but
4179 * not increase it (except from 0).
4180 * If array is active, we can try an on-line resize
4181 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004182 sector_t sectors;
4183 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004184
Andre Noll58c0fed2009-03-31 14:33:13 +11004185 if (err < 0)
4186 return err;
NeilBrown67918752014-12-15 12:57:01 +11004187 err = mddev_lock(mddev);
4188 if (err)
4189 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004190 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004191 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004192 if (err == 0)
4193 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004194 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004195 if (mddev->dev_sectors == 0 ||
4196 mddev->dev_sectors > sectors)
4197 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004198 else
4199 err = -ENOSPC;
4200 }
NeilBrown67918752014-12-15 12:57:01 +11004201 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004202 return err ? err : len;
4203}
4204
4205static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004206__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004207
Masanari Iida83f0d772012-10-30 00:18:08 +09004208/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004209 * This is one of
4210 * 'none' for arrays with no metadata (good luck...)
4211 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004212 * or N.M for internally known formats
4213 */
4214static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004215metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004216{
4217 if (mddev->persistent)
4218 return sprintf(page, "%d.%d\n",
4219 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004220 else if (mddev->external)
4221 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004222 else
4223 return sprintf(page, "none\n");
4224}
4225
4226static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004227metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004228{
4229 int major, minor;
4230 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004231 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004232 /* Changing the details of 'external' metadata is
4233 * always permitted. Otherwise there must be
4234 * no devices attached to the array.
4235 */
NeilBrown67918752014-12-15 12:57:01 +11004236
4237 err = mddev_lock(mddev);
4238 if (err)
4239 return err;
4240 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004241 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4242 ;
4243 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004244 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004245
NeilBrown67918752014-12-15 12:57:01 +11004246 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004247 if (cmd_match(buf, "none")) {
4248 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004249 mddev->external = 0;
4250 mddev->major_version = 0;
4251 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004252 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004253 }
4254 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004255 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004256 if (namelen >= sizeof(mddev->metadata_type))
4257 namelen = sizeof(mddev->metadata_type)-1;
4258 strncpy(mddev->metadata_type, buf+9, namelen);
4259 mddev->metadata_type[namelen] = 0;
4260 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4261 mddev->metadata_type[--namelen] = 0;
4262 mddev->persistent = 0;
4263 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004264 mddev->major_version = 0;
4265 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004266 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004267 }
4268 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004269 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004270 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004271 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004272 buf = e+1;
4273 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004274 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004275 goto out_unlock;
4276 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004277 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004278 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004279 mddev->major_version = major;
4280 mddev->minor_version = minor;
4281 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004282 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004283 err = 0;
4284out_unlock:
4285 mddev_unlock(mddev);
4286 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004287}
4288
4289static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004290__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004291
NeilBrowna35b0d62006-01-06 00:20:49 -08004292static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004293action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004294{
NeilBrown7eec3142005-11-08 21:39:44 -08004295 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004296 unsigned long recovery = mddev->recovery;
4297 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004298 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004299 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4300 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4301 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004302 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004303 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4304 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004305 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004306 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004307 type = "check";
4308 else
4309 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004310 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004311 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004312 else if (mddev->reshape_position != MaxSector)
4313 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004314 }
4315 return sprintf(page, "%s\n", type);
4316}
4317
4318static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004319action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004320{
NeilBrown7eec3142005-11-08 21:39:44 -08004321 if (!mddev->pers || !mddev->pers->sync_request)
4322 return -EINVAL;
4323
NeilBrownb6a9ce62009-05-26 09:41:17 +10004324
4325 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004326 if (cmd_match(page, "frozen"))
4327 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4328 else
4329 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004330 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4331 mddev_lock(mddev) == 0) {
4332 flush_workqueue(md_misc_wq);
4333 if (mddev->sync_thread) {
4334 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004335 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004336 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004337 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004338 }
NeilBrown312045e2015-12-21 11:01:21 +11004339 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004340 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004341 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004342 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004343 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004344 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004345 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004346 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004347 int err;
4348 if (mddev->pers->start_reshape == NULL)
4349 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004350 err = mddev_lock(mddev);
4351 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004352 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4353 err = -EBUSY;
4354 else {
4355 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4356 err = mddev->pers->start_reshape(mddev);
4357 }
NeilBrown67918752014-12-15 12:57:01 +11004358 mddev_unlock(mddev);
4359 }
NeilBrown16484bf2006-03-27 01:18:13 -08004360 if (err)
4361 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004362 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004363 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004364 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004365 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004366 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004367 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004368 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004369 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4370 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004371 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004372 if (mddev->ro == 2) {
4373 /* A write to sync_action is enough to justify
4374 * canceling read-auto mode
4375 */
4376 mddev->ro = 0;
4377 md_wakeup_thread(mddev->sync_thread);
4378 }
NeilBrown03c902e2006-01-06 00:20:46 -08004379 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004380 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004381 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004382 return len;
4383}
4384
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004385static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004386__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004387
4388static ssize_t
4389last_sync_action_show(struct mddev *mddev, char *page)
4390{
4391 return sprintf(page, "%s\n", mddev->last_sync_action);
4392}
4393
4394static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4395
NeilBrown9d888832005-11-08 21:39:26 -08004396static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004397mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004398{
4399 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004400 (unsigned long long)
4401 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004402}
4403
NeilBrown80ca3a42006-07-10 04:44:18 -07004404static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004405
NeilBrown88202a02006-01-06 00:21:36 -08004406static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004407sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004408{
4409 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4410 mddev->sync_speed_min ? "local": "system");
4411}
4412
4413static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004414sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004415{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004416 unsigned int min;
4417 int rv;
4418
NeilBrown88202a02006-01-06 00:21:36 -08004419 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004420 min = 0;
4421 } else {
4422 rv = kstrtouint(buf, 10, &min);
4423 if (rv < 0)
4424 return rv;
4425 if (min == 0)
4426 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004427 }
NeilBrown88202a02006-01-06 00:21:36 -08004428 mddev->sync_speed_min = min;
4429 return len;
4430}
4431
4432static struct md_sysfs_entry md_sync_min =
4433__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4434
4435static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004436sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004437{
4438 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4439 mddev->sync_speed_max ? "local": "system");
4440}
4441
4442static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004443sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004444{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004445 unsigned int max;
4446 int rv;
4447
NeilBrown88202a02006-01-06 00:21:36 -08004448 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004449 max = 0;
4450 } else {
4451 rv = kstrtouint(buf, 10, &max);
4452 if (rv < 0)
4453 return rv;
4454 if (max == 0)
4455 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004456 }
NeilBrown88202a02006-01-06 00:21:36 -08004457 mddev->sync_speed_max = max;
4458 return len;
4459}
4460
4461static struct md_sysfs_entry md_sync_max =
4462__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4463
Iustin Popd7f3d292007-10-16 23:30:54 -07004464static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004465degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004466{
4467 return sprintf(page, "%d\n", mddev->degraded);
4468}
4469static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004470
4471static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004472sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004473{
4474 return sprintf(page, "%d\n", mddev->parallel_resync);
4475}
4476
4477static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004478sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004479{
4480 long n;
4481
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004482 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004483 return -EINVAL;
4484
4485 if (n != 0 && n != 1)
4486 return -EINVAL;
4487
4488 mddev->parallel_resync = n;
4489
4490 if (mddev->sync_thread)
4491 wake_up(&resync_wait);
4492
4493 return len;
4494}
4495
4496/* force parallel resync, even with shared block devices */
4497static struct md_sysfs_entry md_sync_force_parallel =
4498__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4499 sync_force_parallel_show, sync_force_parallel_store);
4500
4501static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004502sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004503{
4504 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004505 if (mddev->curr_resync == 0)
4506 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004507 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4508 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004509 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004510 db = resync - mddev->resync_mark_cnt;
4511 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004512}
4513
NeilBrown80ca3a42006-07-10 04:44:18 -07004514static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004515
4516static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004517sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004518{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004519 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004520
NeilBrownacb180b2009-04-14 16:28:34 +10004521 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4522 return sprintf(page, "none\n");
4523
NeilBrown72f36d52012-10-11 14:25:57 +11004524 if (mddev->curr_resync == 1 ||
4525 mddev->curr_resync == 2)
4526 return sprintf(page, "delayed\n");
4527
NeilBrownc804cde2012-05-21 09:28:33 +10004528 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4529 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004530 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004531 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004532 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004533
NeilBrownacb180b2009-04-14 16:28:34 +10004534 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004535 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004536}
4537
NeilBrown750f1992014-09-30 08:53:05 +10004538static struct md_sysfs_entry md_sync_completed =
4539 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004540
NeilBrowne464eaf2006-03-27 01:18:14 -08004541static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004542min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004543{
4544 return sprintf(page, "%llu\n",
4545 (unsigned long long)mddev->resync_min);
4546}
4547static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004548min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004549{
4550 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004551 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004552
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004553 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004554 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004555
4556 spin_lock(&mddev->lock);
4557 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004558 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004559 goto out_unlock;
4560
4561 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004562 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004563 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004564
NeilBrown50c37b12015-03-23 17:36:38 +11004565 /* Round down to multiple of 4K for safety */
4566 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004567 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004568
NeilBrown23da4222014-12-15 12:57:01 +11004569out_unlock:
4570 spin_unlock(&mddev->lock);
4571 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004572}
4573
4574static struct md_sysfs_entry md_min_sync =
4575__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4576
4577static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004578max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004579{
4580 if (mddev->resync_max == MaxSector)
4581 return sprintf(page, "max\n");
4582 else
4583 return sprintf(page, "%llu\n",
4584 (unsigned long long)mddev->resync_max);
4585}
4586static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004587max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004588{
NeilBrown23da4222014-12-15 12:57:01 +11004589 int err;
4590 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004591 if (strncmp(buf, "max", 3) == 0)
4592 mddev->resync_max = MaxSector;
4593 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004594 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004595 int chunk;
4596
4597 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004598 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004599 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004600 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004601 goto out_unlock;
4602
4603 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004604 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004605 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004606 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004607 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004608
4609 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004610 chunk = mddev->chunk_sectors;
4611 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004612 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004613
4614 err = -EINVAL;
4615 if (sector_div(temp, chunk))
4616 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004617 }
4618 mddev->resync_max = max;
4619 }
4620 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004621 err = 0;
4622out_unlock:
4623 spin_unlock(&mddev->lock);
4624 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004625}
4626
4627static struct md_sysfs_entry md_max_sync =
4628__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4629
4630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004631suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004632{
4633 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4634}
4635
4636static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004637suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004638{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004639 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004640 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004641
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004642 err = kstrtoull(buf, 10, &new);
4643 if (err < 0)
4644 return err;
4645 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004646 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004647
NeilBrown67918752014-12-15 12:57:01 +11004648 err = mddev_lock(mddev);
4649 if (err)
4650 return err;
4651 err = -EINVAL;
4652 if (mddev->pers == NULL ||
4653 mddev->pers->quiesce == NULL)
4654 goto unlock;
4655 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004656 mddev->suspend_lo = new;
4657 if (new >= old)
4658 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004659 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004660 else {
4661 /* Expanding suspended region - need to wait */
4662 mddev->pers->quiesce(mddev, 1);
4663 mddev->pers->quiesce(mddev, 0);
4664 }
NeilBrown67918752014-12-15 12:57:01 +11004665 err = 0;
4666unlock:
4667 mddev_unlock(mddev);
4668 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004669}
4670static struct md_sysfs_entry md_suspend_lo =
4671__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4672
NeilBrowne464eaf2006-03-27 01:18:14 -08004673static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004674suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004675{
4676 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4677}
4678
4679static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004680suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004681{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004682 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004683 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004684
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004685 err = kstrtoull(buf, 10, &new);
4686 if (err < 0)
4687 return err;
4688 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004689 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004690
NeilBrown67918752014-12-15 12:57:01 +11004691 err = mddev_lock(mddev);
4692 if (err)
4693 return err;
4694 err = -EINVAL;
4695 if (mddev->pers == NULL ||
4696 mddev->pers->quiesce == NULL)
4697 goto unlock;
4698 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004699 mddev->suspend_hi = new;
4700 if (new <= old)
4701 /* Shrinking suspended region */
4702 mddev->pers->quiesce(mddev, 2);
4703 else {
4704 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004705 mddev->pers->quiesce(mddev, 1);
4706 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004707 }
NeilBrown67918752014-12-15 12:57:01 +11004708 err = 0;
4709unlock:
4710 mddev_unlock(mddev);
4711 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004712}
4713static struct md_sysfs_entry md_suspend_hi =
4714__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4715
NeilBrown08a02ec2007-05-09 02:35:38 -07004716static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004717reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004718{
4719 if (mddev->reshape_position != MaxSector)
4720 return sprintf(page, "%llu\n",
4721 (unsigned long long)mddev->reshape_position);
4722 strcpy(page, "none\n");
4723 return 5;
4724}
4725
4726static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004727reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004728{
NeilBrownc6563a82012-05-21 09:27:00 +10004729 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004730 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004731 int err;
NeilBrown67918752014-12-15 12:57:01 +11004732
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004733 err = kstrtoull(buf, 10, &new);
4734 if (err < 0)
4735 return err;
4736 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004737 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004738 err = mddev_lock(mddev);
4739 if (err)
4740 return err;
4741 err = -EBUSY;
4742 if (mddev->pers)
4743 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004744 mddev->reshape_position = new;
4745 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004746 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004747 mddev->new_level = mddev->level;
4748 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004749 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004750 rdev_for_each(rdev, mddev)
4751 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004752 err = 0;
4753unlock:
4754 mddev_unlock(mddev);
4755 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004756}
4757
4758static struct md_sysfs_entry md_reshape_position =
4759__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4760 reshape_position_store);
4761
Dan Williamsb522adc2009-03-31 15:00:31 +11004762static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004763reshape_direction_show(struct mddev *mddev, char *page)
4764{
4765 return sprintf(page, "%s\n",
4766 mddev->reshape_backwards ? "backwards" : "forwards");
4767}
4768
4769static ssize_t
4770reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4771{
4772 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004773 int err;
4774
NeilBrown2c810cd2012-05-21 09:27:00 +10004775 if (cmd_match(buf, "forwards"))
4776 backwards = 0;
4777 else if (cmd_match(buf, "backwards"))
4778 backwards = 1;
4779 else
4780 return -EINVAL;
4781 if (mddev->reshape_backwards == backwards)
4782 return len;
4783
NeilBrown67918752014-12-15 12:57:01 +11004784 err = mddev_lock(mddev);
4785 if (err)
4786 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004787 /* check if we are allowed to change */
4788 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004789 err = -EBUSY;
4790 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004791 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004792 err = -EINVAL;
4793 else
4794 mddev->reshape_backwards = backwards;
4795 mddev_unlock(mddev);
4796 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004797}
4798
4799static struct md_sysfs_entry md_reshape_direction =
4800__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4801 reshape_direction_store);
4802
4803static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004804array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004805{
4806 if (mddev->external_size)
4807 return sprintf(page, "%llu\n",
4808 (unsigned long long)mddev->array_sectors/2);
4809 else
4810 return sprintf(page, "default\n");
4811}
4812
4813static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004814array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004815{
4816 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004817 int err;
4818
4819 err = mddev_lock(mddev);
4820 if (err)
4821 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004822
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004823 /* cluster raid doesn't support change array_sectors */
4824 if (mddev_is_clustered(mddev))
4825 return -EINVAL;
4826
Dan Williamsb522adc2009-03-31 15:00:31 +11004827 if (strncmp(buf, "default", 7) == 0) {
4828 if (mddev->pers)
4829 sectors = mddev->pers->size(mddev, 0, 0);
4830 else
4831 sectors = mddev->array_sectors;
4832
4833 mddev->external_size = 0;
4834 } else {
4835 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004836 err = -EINVAL;
4837 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4838 err = -E2BIG;
4839 else
4840 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004841 }
4842
NeilBrown67918752014-12-15 12:57:01 +11004843 if (!err) {
4844 mddev->array_sectors = sectors;
4845 if (mddev->pers) {
4846 set_capacity(mddev->gendisk, mddev->array_sectors);
4847 revalidate_disk(mddev->gendisk);
4848 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004849 }
NeilBrown67918752014-12-15 12:57:01 +11004850 mddev_unlock(mddev);
4851 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004852}
4853
4854static struct md_sysfs_entry md_array_size =
4855__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4856 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004857
NeilBrowneae17012005-11-08 21:39:23 -08004858static struct attribute *md_default_attrs[] = {
4859 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004860 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004861 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004862 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004863 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004864 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004865 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004866 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004867 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004868 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004869 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004870 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004871 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004872 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004873 NULL,
4874};
4875
4876static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004877 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004878 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004879 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004880 &md_sync_min.attr,
4881 &md_sync_max.attr,
4882 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004883 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004884 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004885 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004886 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004887 &md_suspend_lo.attr,
4888 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004889 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004890 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004891 NULL,
4892};
NeilBrown411036f2005-11-08 21:39:40 -08004893static struct attribute_group md_redundancy_group = {
4894 .name = NULL,
4895 .attrs = md_redundancy_attrs,
4896};
4897
NeilBrowneae17012005-11-08 21:39:23 -08004898static ssize_t
4899md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4900{
4901 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004902 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004903 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004904
4905 if (!entry->show)
4906 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004907 spin_lock(&all_mddevs_lock);
4908 if (list_empty(&mddev->all_mddevs)) {
4909 spin_unlock(&all_mddevs_lock);
4910 return -EBUSY;
4911 }
4912 mddev_get(mddev);
4913 spin_unlock(&all_mddevs_lock);
4914
NeilBrownb7b17c92014-12-15 12:56:59 +11004915 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004916 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004917 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004918}
4919
4920static ssize_t
4921md_attr_store(struct kobject *kobj, struct attribute *attr,
4922 const char *page, size_t length)
4923{
4924 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004925 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004926 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004927
4928 if (!entry->store)
4929 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004930 if (!capable(CAP_SYS_ADMIN))
4931 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004932 spin_lock(&all_mddevs_lock);
4933 if (list_empty(&mddev->all_mddevs)) {
4934 spin_unlock(&all_mddevs_lock);
4935 return -EBUSY;
4936 }
4937 mddev_get(mddev);
4938 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004939 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004940 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004941 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004942}
4943
4944static void md_free(struct kobject *ko)
4945{
NeilBrownfd01b882011-10-11 16:47:53 +11004946 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004947
4948 if (mddev->sysfs_state)
4949 sysfs_put(mddev->sysfs_state);
4950
NeilBrown6cd18e72015-04-27 14:12:22 +10004951 if (mddev->queue)
4952 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004953 if (mddev->gendisk) {
4954 del_gendisk(mddev->gendisk);
4955 put_disk(mddev->gendisk);
4956 }
NeilBrowna21d1502009-01-09 08:31:09 +11004957
NeilBrowneae17012005-11-08 21:39:23 -08004958 kfree(mddev);
4959}
4960
Emese Revfy52cf25d2010-01-19 02:58:23 +01004961static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004962 .show = md_attr_show,
4963 .store = md_attr_store,
4964};
4965static struct kobj_type md_ktype = {
4966 .release = md_free,
4967 .sysfs_ops = &md_sysfs_ops,
4968 .default_attrs = md_default_attrs,
4969};
4970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971int mdp_major = 0;
4972
Dan Williams5fd3a172009-03-04 00:57:25 -07004973static void mddev_delayed_delete(struct work_struct *ws)
4974{
NeilBrownfd01b882011-10-11 16:47:53 +11004975 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004976
NeilBrown43a70502009-12-14 12:49:55 +11004977 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004978 kobject_del(&mddev->kobj);
4979 kobject_put(&mddev->kobj);
4980}
4981
NeilBrownefeb53c2009-01-09 08:31:10 +11004982static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004984 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004985 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004987 int partitioned;
4988 int shift;
4989 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004990 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
4992 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004993 return -ENODEV;
4994
4995 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4996 shift = partitioned ? MdpMinorShift : 0;
4997 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Tejun Heoe804ac72010-10-15 15:36:08 +02004999 /* wait for any previous instance of this device to be
5000 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005001 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005002 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005003
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005004 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005005 error = -EEXIST;
5006 if (mddev->gendisk)
5007 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005008
5009 if (name) {
5010 /* Need to ensure that 'name' is not a duplicate.
5011 */
NeilBrownfd01b882011-10-11 16:47:53 +11005012 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005013 spin_lock(&all_mddevs_lock);
5014
5015 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5016 if (mddev2->gendisk &&
5017 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5018 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005019 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005020 }
5021 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 }
NeilBrown8b765392009-01-09 08:31:08 +11005023
NeilBrown0909dc42009-07-01 12:27:21 +10005024 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005025 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005026 if (!mddev->queue)
5027 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005028 mddev->queue->queuedata = mddev;
5029
NeilBrown409c57f2009-03-31 14:39:39 +11005030 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005031 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005032
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 disk = alloc_disk(1 << shift);
5034 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005035 blk_cleanup_queue(mddev->queue);
5036 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005037 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005039 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005041 if (name)
5042 strcpy(disk->disk_name, name);
5043 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005045 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 disk->fops = &md_fops;
5048 disk->private_data = mddev;
5049 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005050 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005051 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005052 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005053 * remove it now.
5054 */
5055 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005057 /* As soon as we call add_disk(), another thread could get
5058 * through to md_open, so make sure it doesn't get too far
5059 */
5060 mutex_lock(&mddev->open_mutex);
5061 add_disk(disk);
5062
Tejun Heoed9e1982008-08-25 19:56:05 +09005063 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5064 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005065 if (error) {
5066 /* This isn't possible, but as kobject_init_and_add is marked
5067 * __must_check, we must do something with the result
5068 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005069 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5070 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005071 error = 0;
5072 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005073 if (mddev->kobj.sd &&
5074 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11005075 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005076 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005077 abort:
5078 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005079 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005080 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005081 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005082 }
NeilBrownd3374822009-01-09 08:31:10 +11005083 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005084 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005085}
5086
5087static struct kobject *md_probe(dev_t dev, int *part, void *data)
5088{
5089 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 return NULL;
5091}
5092
NeilBrownefeb53c2009-01-09 08:31:10 +11005093static int add_named_array(const char *val, struct kernel_param *kp)
5094{
5095 /* val must be "md_*" where * is not all digits.
5096 * We allocate an array with a large free minor number, and
5097 * set the name to val. val must not already be an active name.
5098 */
5099 int len = strlen(val);
5100 char buf[DISK_NAME_LEN];
5101
5102 while (len && val[len-1] == '\n')
5103 len--;
5104 if (len >= DISK_NAME_LEN)
5105 return -E2BIG;
5106 strlcpy(buf, val, len+1);
5107 if (strncmp(buf, "md_", 3) != 0)
5108 return -EINVAL;
5109 return md_alloc(0, buf);
5110}
5111
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112static void md_safemode_timeout(unsigned long data)
5113{
NeilBrownfd01b882011-10-11 16:47:53 +11005114 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
Neil Brown0fd62b82008-06-28 08:31:36 +10005116 if (!atomic_read(&mddev->writes_pending)) {
5117 mddev->safemode = 1;
5118 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005119 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 md_wakeup_thread(mddev->thread);
5122}
5123
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005124static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
NeilBrownfd01b882011-10-11 16:47:53 +11005126int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127{
NeilBrown2604b702006-01-06 00:20:36 -08005128 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005129 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005130 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
NeilBrowna757e642005-04-16 15:26:42 -07005132 if (list_empty(&mddev->disks))
5133 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
5136 if (mddev->pers)
5137 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005138 /* Cannot run until previous stop completes properly */
5139 if (mddev->sysfs_active)
5140 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005141
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 /*
5143 * Analyze all RAID superblock(s)
5144 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005145 if (!mddev->raid_disks) {
5146 if (!mddev->persistent)
5147 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005148 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
NeilBrownd9d166c2006-01-06 00:20:51 -08005151 if (mddev->level != LEVEL_NONE)
5152 request_module("md-level-%d", mddev->level);
5153 else if (mddev->clevel[0])
5154 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
5156 /*
5157 * Drop all container device buffers, from now on
5158 * the only valid external interface is through the md
5159 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005161 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005162 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 continue;
5164 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005165 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005166
5167 /* perform some consistency tests on the device.
5168 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005169 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005170 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005171 if (rdev->meta_bdev) {
5172 /* Nothing to check */;
5173 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005174 if (mddev->dev_sectors &&
5175 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005176 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005177 printk("md: %s: data overlaps metadata\n",
5178 mdname(mddev));
5179 return -EINVAL;
5180 }
5181 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005182 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005183 > rdev->data_offset) {
5184 printk("md: %s: metadata overlaps data\n",
5185 mdname(mddev));
5186 return -EINVAL;
5187 }
5188 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005189 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 }
5191
NeilBrowna167f662010-10-26 18:31:13 +11005192 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005193 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005196 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005197 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005199 if (mddev->level != LEVEL_NONE)
5200 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5201 mddev->level);
5202 else
5203 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5204 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 return -EINVAL;
5206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005208 if (mddev->level != pers->level) {
5209 mddev->level = pers->level;
5210 mddev->new_level = pers->level;
5211 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005212 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
NeilBrownf6705572006-03-27 01:18:11 -08005214 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005215 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005216 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005217 module_put(pers->owner);
5218 return -EINVAL;
5219 }
5220
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005221 if (pers->sync_request) {
5222 /* Warn if this is a potentially silly
5223 * configuration.
5224 */
5225 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005226 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005227 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005228
NeilBrowndafb20f2012-03-19 12:46:39 +11005229 rdev_for_each(rdev, mddev)
5230 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005231 if (rdev < rdev2 &&
5232 rdev->bdev->bd_contains ==
5233 rdev2->bdev->bd_contains) {
5234 printk(KERN_WARNING
5235 "%s: WARNING: %s appears to be"
5236 " on the same physical disk as"
5237 " %s.\n",
5238 mdname(mddev),
5239 bdevname(rdev->bdev,b),
5240 bdevname(rdev2->bdev,b2));
5241 warned = 1;
5242 }
5243 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005244
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005245 if (warned)
5246 printk(KERN_WARNING
5247 "True protection against single-disk"
5248 " failure might be compromised.\n");
5249 }
5250
NeilBrown657390d2005-08-26 18:34:16 -07005251 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005252 /* may be over-ridden by personality */
5253 mddev->resync_max_sectors = mddev->dev_sectors;
5254
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005255 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
NeilBrown0f9552b52009-12-30 12:08:50 +11005257 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005258 mddev->ro = 2; /* read-only, but switch on first write */
5259
NeilBrown36d091f2014-12-15 12:56:58 +11005260 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005261 if (err)
5262 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005263 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005264 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5265 " but 'external_size' not in effect?\n", __func__);
5266 printk(KERN_ERR
5267 "md: invalid array_size %llu > default size %llu\n",
5268 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005269 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005270 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005271 }
NeilBrown36d091f2014-12-15 12:56:58 +11005272 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005273 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005274 struct bitmap *bitmap;
5275
5276 bitmap = bitmap_create(mddev, -1);
5277 if (IS_ERR(bitmap)) {
5278 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005279 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5280 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005281 } else
5282 mddev->bitmap = bitmap;
5283
NeilBrownb15c2e52006-01-06 00:20:16 -08005284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005286 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005287 if (mddev->private)
5288 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005289 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005290 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005291 bitmap_destroy(mddev);
5292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 }
NeilBrown5c675f82014-12-15 12:56:56 +11005294 if (mddev->queue) {
5295 mddev->queue->backing_dev_info.congested_data = mddev;
5296 mddev->queue->backing_dev_info.congested_fn = md_congested;
5297 }
NeilBrown36d091f2014-12-15 12:56:58 +11005298 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005299 if (mddev->kobj.sd &&
5300 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005301 printk(KERN_WARNING
5302 "md: cannot register extra attributes for %s\n",
5303 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005304 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005305 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005306 mddev->ro = 0;
5307
NeilBrownf72ffdd2014-09-30 14:23:59 +10005308 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005309 atomic_set(&mddev->max_corr_read_errors,
5310 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005312 if (mddev_is_clustered(mddev))
5313 mddev->safemode_delay = 0;
5314 else
5315 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005317 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005318 spin_lock(&mddev->lock);
5319 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005320 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005321 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005322 if (rdev->raid_disk >= 0)
5323 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005324 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005325
NeilBrowna4a3d262015-07-17 11:57:30 +10005326 if (mddev->degraded && !mddev->ro)
5327 /* This ensures that recovering status is reported immediately
5328 * via sysfs - until a lack of spares is confirmed.
5329 */
5330 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005332
NeilBrown7a0a5352013-08-27 16:28:23 +10005333 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005334 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
NeilBrownd7603b72006-01-06 00:20:30 -08005336 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005337 sysfs_notify_dirent_safe(mddev->sysfs_state);
5338 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005339 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 return 0;
5341}
NeilBrown390ee602010-06-01 19:37:27 +10005342EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
NeilBrownfd01b882011-10-11 16:47:53 +11005344static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005345{
5346 int err;
5347
5348 err = md_run(mddev);
5349 if (err)
5350 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005351 err = bitmap_load(mddev);
5352 if (err) {
5353 bitmap_destroy(mddev);
5354 goto out;
5355 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005356
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005357 if (mddev_is_clustered(mddev))
5358 md_allow_write(mddev);
5359
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005360 md_wakeup_thread(mddev->thread);
5361 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5362
NeilBrownfe60b012010-03-29 11:10:42 +11005363 set_capacity(mddev->gendisk, mddev->array_sectors);
5364 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005365 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005366 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5367out:
5368 return err;
5369}
5370
NeilBrownfd01b882011-10-11 16:47:53 +11005371static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372{
5373 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
Andre Noll80fab1d2008-07-11 22:02:21 +10005375 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005377 return -ENXIO;
5378 if (!mddev->pers)
5379 return -EINVAL;
5380 if (!mddev->ro)
5381 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005382 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5383 struct md_rdev *rdev;
5384 bool has_journal = false;
5385
5386 rcu_read_lock();
5387 rdev_for_each_rcu(rdev, mddev) {
5388 if (test_bit(Journal, &rdev->flags) &&
5389 !test_bit(Faulty, &rdev->flags)) {
5390 has_journal = true;
5391 break;
5392 }
5393 }
5394 rcu_read_unlock();
5395
5396 /* Don't restart rw with journal missing/faulty */
5397 if (!has_journal)
5398 return -EINVAL;
5399 }
5400
Andre Noll80fab1d2008-07-11 22:02:21 +10005401 mddev->safemode = 0;
5402 mddev->ro = 0;
5403 set_disk_ro(disk, 0);
5404 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5405 mdname(mddev));
5406 /* Kick recovery or resync if necessary */
5407 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5408 md_wakeup_thread(mddev->thread);
5409 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005410 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412}
5413
NeilBrownfd01b882011-10-11 16:47:53 +11005414static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005415{
5416 mddev->array_sectors = 0;
5417 mddev->external_size = 0;
5418 mddev->dev_sectors = 0;
5419 mddev->raid_disks = 0;
5420 mddev->recovery_cp = 0;
5421 mddev->resync_min = 0;
5422 mddev->resync_max = MaxSector;
5423 mddev->reshape_position = MaxSector;
5424 mddev->external = 0;
5425 mddev->persistent = 0;
5426 mddev->level = LEVEL_NONE;
5427 mddev->clevel[0] = 0;
5428 mddev->flags = 0;
5429 mddev->ro = 0;
5430 mddev->metadata_type[0] = 0;
5431 mddev->chunk_sectors = 0;
5432 mddev->ctime = mddev->utime = 0;
5433 mddev->layout = 0;
5434 mddev->max_disks = 0;
5435 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005436 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005437 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005438 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005439 mddev->new_level = LEVEL_NONE;
5440 mddev->new_layout = 0;
5441 mddev->new_chunk_sectors = 0;
5442 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005443 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005444 mddev->suspend_lo = mddev->suspend_hi = 0;
5445 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5446 mddev->recovery = 0;
5447 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005448 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005449 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005450 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005451 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005452 mddev->bitmap_info.offset = 0;
5453 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005454 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005455 mddev->bitmap_info.chunksize = 0;
5456 mddev->bitmap_info.daemon_sleep = 0;
5457 mddev->bitmap_info.max_write_behind = 0;
5458}
5459
NeilBrownfd01b882011-10-11 16:47:53 +11005460static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005461{
NeilBrown6b6204e2013-05-09 09:48:30 +10005462 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005463 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005464 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005465 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005466 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005467 }
5468
5469 del_timer_sync(&mddev->safemode_timer);
5470
5471 bitmap_flush(mddev);
5472 md_super_wait(mddev);
5473
NeilBrownb6d428c2013-04-24 11:42:42 +10005474 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005475 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5476 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005477 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005478 if (!mddev_is_clustered(mddev))
5479 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005480 md_update_sb(mddev, 1);
5481 }
5482}
NeilBrowndefad612011-01-14 09:14:33 +11005483
NeilBrownfd01b882011-10-11 16:47:53 +11005484void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005485{
NeilBrown29f097c2013-11-14 17:54:51 +11005486 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005487 __md_stop_writes(mddev);
5488 mddev_unlock(mddev);
5489}
NeilBrown390ee602010-06-01 19:37:27 +10005490EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005491
NeilBrown5aa61f42014-12-15 12:56:57 +11005492static void mddev_detach(struct mddev *mddev)
5493{
5494 struct bitmap *bitmap = mddev->bitmap;
5495 /* wait for behind writes to complete */
5496 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5497 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5498 mdname(mddev));
5499 /* need to kick something here to make sure I/O goes? */
5500 wait_event(bitmap->behind_wait,
5501 atomic_read(&bitmap->behind_writes) == 0);
5502 }
NeilBrown36d091f2014-12-15 12:56:58 +11005503 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005504 mddev->pers->quiesce(mddev, 1);
5505 mddev->pers->quiesce(mddev, 0);
5506 }
5507 md_unregister_thread(&mddev->thread);
5508 if (mddev->queue)
5509 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5510}
5511
NeilBrown5eff3c42012-11-19 10:47:48 +11005512static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005513{
NeilBrown36d091f2014-12-15 12:56:58 +11005514 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005515 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005516 /* Ensure ->event_work is done */
5517 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005518 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005519 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005520 spin_unlock(&mddev->lock);
5521 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005522 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005523 if (pers->sync_request && mddev->to_remove == NULL)
5524 mddev->to_remove = &md_redundancy_group;
5525 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005526 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005527}
NeilBrown5eff3c42012-11-19 10:47:48 +11005528
5529void md_stop(struct mddev *mddev)
5530{
5531 /* stop the array and free an attached data structures.
5532 * This is called from dm-raid
5533 */
5534 __md_stop(mddev);
5535 bitmap_destroy(mddev);
5536 if (mddev->bio_set)
5537 bioset_free(mddev->bio_set);
5538}
5539
NeilBrown390ee602010-06-01 19:37:27 +10005540EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005541
NeilBrowna05b7ea2012-07-19 15:59:18 +10005542static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005543{
5544 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005545 int did_freeze = 0;
5546
5547 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5548 did_freeze = 1;
5549 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5550 md_wakeup_thread(mddev->thread);
5551 }
NeilBrownf851b602014-12-11 10:02:10 +11005552 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005553 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005554 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005555 /* Thread might be blocked waiting for metadata update
5556 * which will now never happen */
5557 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005558
NeilBrown88724bf2015-09-24 14:00:51 +10005559 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5560 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005561 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005562 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5563 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005564 wait_event(mddev->sb_wait,
5565 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005566 mddev_lock_nointr(mddev);
5567
NeilBrowna4bd82d2010-03-29 13:23:10 +11005568 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005569 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005570 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005571 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005572 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005573 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005574 if (did_freeze) {
5575 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005576 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005577 md_wakeup_thread(mddev->thread);
5578 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005579 err = -EBUSY;
5580 goto out;
5581 }
5582 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005583 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005584
5585 err = -ENXIO;
5586 if (mddev->ro==1)
5587 goto out;
5588 mddev->ro = 1;
5589 set_disk_ro(mddev->gendisk, 1);
5590 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005591 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5592 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005593 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005594 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005595 }
5596out:
5597 mutex_unlock(&mddev->open_mutex);
5598 return err;
5599}
5600
NeilBrown9e653b62006-06-26 00:27:58 -07005601/* mode:
5602 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005603 * 2 - stop but do not disassemble array
5604 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005605static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005606 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005609 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005610 int did_freeze = 0;
5611
5612 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5613 did_freeze = 1;
5614 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5615 md_wakeup_thread(mddev->thread);
5616 }
NeilBrownf851b602014-12-11 10:02:10 +11005617 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005618 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005619 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005620 /* Thread might be blocked waiting for metadata update
5621 * which will now never happen */
5622 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005623
NeilBrown30b8feb2013-11-14 15:16:17 +11005624 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005625 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5626 !test_bit(MD_RECOVERY_RUNNING,
5627 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005628 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
NeilBrownc8c00a62009-08-10 12:50:52 +10005630 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005631 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005632 mddev->sysfs_active ||
5633 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005634 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005635 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005636 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005637 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005638 if (did_freeze) {
5639 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005640 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005641 md_wakeup_thread(mddev->thread);
5642 }
NeilBrown260fa032013-08-27 16:44:13 +10005643 return -EBUSY;
5644 }
NeilBrown6e17b022010-08-07 21:41:19 +10005645 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005646 if (mddev->ro)
5647 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005648
NeilBrowndefad612011-01-14 09:14:33 +11005649 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005650 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005651 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005652
NeilBrowna4bd82d2010-03-29 13:23:10 +11005653 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005654 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005655
NeilBrowndafb20f2012-03-19 12:46:39 +11005656 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005657 if (rdev->raid_disk >= 0)
5658 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005659
NeilBrowna4bd82d2010-03-29 13:23:10 +11005660 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005661 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005662 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005663 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005664
NeilBrowna4bd82d2010-03-29 13:23:10 +11005665 if (mddev->ro)
5666 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005667 } else
5668 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 /*
5670 * Free resources if final stop
5671 */
NeilBrown9e653b62006-06-26 00:27:58 -07005672 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5674
NeilBrown978f9462006-02-02 14:28:05 -08005675 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005676 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005677 struct file *f = mddev->bitmap_info.file;
5678 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005679 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005680 spin_unlock(&mddev->lock);
5681 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005682 }
NeilBrownc3d97142009-12-14 12:49:52 +11005683 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005684
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 export_array(mddev);
5686
NeilBrown6177b472010-03-29 11:37:13 +11005687 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005688 if (mddev->hold_active == UNTIL_STOP)
5689 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005690 }
NeilBrownd7603b72006-01-06 00:20:30 -08005691 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005692 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694}
5695
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005696#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005697static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698{
NeilBrown3cb03002011-10-11 16:45:26 +11005699 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 int err;
5701
NeilBrowna757e642005-04-16 15:26:42 -07005702 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
5705 printk(KERN_INFO "md: running: ");
5706
NeilBrowndafb20f2012-03-19 12:46:39 +11005707 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 char b[BDEVNAME_SIZE];
5709 printk("<%s>", bdevname(rdev->bdev,b));
5710 }
5711 printk("\n");
5712
NeilBrownd710e132008-10-13 11:55:12 +11005713 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 if (err) {
5715 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005716 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 }
5718}
5719
5720/*
5721 * lets try to run arrays based on all disks that have arrived
5722 * until now. (those are in pending_raid_disks)
5723 *
5724 * the method: pick the first pending disk, collect all disks with
5725 * the same UUID, remove all from the pending list and put them into
5726 * the 'same_array' list. Then order this list based on superblock
5727 * update time (freshest comes first), kick out 'old' disks and
5728 * compare superblocks. If everything's fine then run it.
5729 *
5730 * If "unit" is allocated, then bump its reference count
5731 */
5732static void autorun_devices(int part)
5733{
NeilBrown3cb03002011-10-11 16:45:26 +11005734 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005735 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 char b[BDEVNAME_SIZE];
5737
5738 printk(KERN_INFO "md: autorun ...\n");
5739 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005740 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005742 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005744 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
5746 printk(KERN_INFO "md: considering %s ...\n",
5747 bdevname(rdev0->bdev,b));
5748 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005749 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 if (super_90_load(rdev, rdev0, 0) >= 0) {
5751 printk(KERN_INFO "md: adding %s ...\n",
5752 bdevname(rdev->bdev,b));
5753 list_move(&rdev->same_set, &candidates);
5754 }
5755 /*
5756 * now we have a set of devices, with all of them having
5757 * mostly sane superblocks. It's time to allocate the
5758 * mddev.
5759 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005760 if (part) {
5761 dev = MKDEV(mdp_major,
5762 rdev0->preferred_minor << MdpMinorShift);
5763 unit = MINOR(dev) >> MdpMinorShift;
5764 } else {
5765 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5766 unit = MINOR(dev);
5767 }
5768 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5770 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5771 break;
5772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
5774 md_probe(dev, NULL, NULL);
5775 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005776 if (!mddev || !mddev->gendisk) {
5777 if (mddev)
5778 mddev_put(mddev);
5779 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 "md: cannot allocate memory for md drive.\n");
5781 break;
5782 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005783 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 printk(KERN_WARNING "md: %s locked, cannot run\n",
5785 mdname(mddev));
5786 else if (mddev->raid_disks || mddev->major_version
5787 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005788 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 "md: %s already running, cannot run %s\n",
5790 mdname(mddev), bdevname(rdev0->bdev,b));
5791 mddev_unlock(mddev);
5792 } else {
5793 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005794 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005795 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 list_del_init(&rdev->same_set);
5797 if (bind_rdev_to_array(rdev, mddev))
5798 export_rdev(rdev);
5799 }
5800 autorun_array(mddev);
5801 mddev_unlock(mddev);
5802 }
5803 /* on success, candidates will be empty, on error
5804 * it won't...
5805 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005806 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005807 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 mddev_put(mddev);
5811 }
5812 printk(KERN_INFO "md: ... autorun DONE.\n");
5813}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005814#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
NeilBrownf72ffdd2014-09-30 14:23:59 +10005816static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817{
5818 mdu_version_t ver;
5819
5820 ver.major = MD_MAJOR_VERSION;
5821 ver.minor = MD_MINOR_VERSION;
5822 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5823
5824 if (copy_to_user(arg, &ver, sizeof(ver)))
5825 return -EFAULT;
5826
5827 return 0;
5828}
5829
NeilBrownf72ffdd2014-09-30 14:23:59 +10005830static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831{
5832 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005833 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005834 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
NeilBrown1ca69c42012-10-11 13:37:33 +11005836 nr = working = insync = failed = spare = 0;
5837 rcu_read_lock();
5838 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005840 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 failed++;
5842 else {
5843 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005844 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005845 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 else
5847 spare++;
5848 }
5849 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005850 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
5852 info.major_version = mddev->major_version;
5853 info.minor_version = mddev->minor_version;
5854 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005855 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005857 info.size = mddev->dev_sectors / 2;
5858 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005859 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 info.nr_disks = nr;
5861 info.raid_disks = mddev->raid_disks;
5862 info.md_minor = mddev->md_minor;
5863 info.not_persistent= !mddev->persistent;
5864
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005865 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 info.state = 0;
5867 if (mddev->in_sync)
5868 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005869 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005870 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005871 if (mddev_is_clustered(mddev))
5872 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005873 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 info.working_disks = working;
5875 info.failed_disks = failed;
5876 info.spare_disks = spare;
5877
5878 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005879 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
5881 if (copy_to_user(arg, &info, sizeof(info)))
5882 return -EFAULT;
5883
5884 return 0;
5885}
5886
NeilBrownf72ffdd2014-09-30 14:23:59 +10005887static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005888{
5889 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005890 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005891 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005892
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005893 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005894 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005895 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005896
NeilBrown32a76272005-06-21 17:17:14 -07005897 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005898 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005899 /* bitmap enabled */
5900 if (mddev->bitmap_info.file) {
5901 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5902 sizeof(file->pathname));
5903 if (IS_ERR(ptr))
5904 err = PTR_ERR(ptr);
5905 else
5906 memmove(file->pathname, ptr,
5907 sizeof(file->pathname)-(ptr-file->pathname));
5908 }
NeilBrown4af1a042014-12-15 12:57:00 +11005909 spin_unlock(&mddev->lock);
5910
5911 if (err == 0 &&
5912 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005913 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005914
NeilBrown32a76272005-06-21 17:17:14 -07005915 kfree(file);
5916 return err;
5917}
5918
NeilBrownf72ffdd2014-09-30 14:23:59 +10005919static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920{
5921 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005922 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
5924 if (copy_from_user(&info, arg, sizeof(info)))
5925 return -EFAULT;
5926
NeilBrown1ca69c42012-10-11 13:37:33 +11005927 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005928 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 if (rdev) {
5930 info.major = MAJOR(rdev->bdev->bd_dev);
5931 info.minor = MINOR(rdev->bdev->bd_dev);
5932 info.raid_disk = rdev->raid_disk;
5933 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005934 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005936 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 info.state |= (1<<MD_DISK_ACTIVE);
5938 info.state |= (1<<MD_DISK_SYNC);
5939 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005940 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005941 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005942 if (test_bit(WriteMostly, &rdev->flags))
5943 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 } else {
5945 info.major = info.minor = 0;
5946 info.raid_disk = -1;
5947 info.state = (1<<MD_DISK_REMOVED);
5948 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005949 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
5951 if (copy_to_user(arg, &info, sizeof(info)))
5952 return -EFAULT;
5953
5954 return 0;
5955}
5956
NeilBrownf72ffdd2014-09-30 14:23:59 +10005957static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958{
5959 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005960 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 dev_t dev = MKDEV(info->major,info->minor);
5962
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005963 if (mddev_is_clustered(mddev) &&
5964 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005965 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005966 mdname(mddev));
5967 return -EINVAL;
5968 }
5969
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5971 return -EOVERFLOW;
5972
5973 if (!mddev->raid_disks) {
5974 int err;
5975 /* expecting a device which has a superblock */
5976 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5977 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005978 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 "md: md_import_device returned %ld\n",
5980 PTR_ERR(rdev));
5981 return PTR_ERR(rdev);
5982 }
5983 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005984 struct md_rdev *rdev0
5985 = list_entry(mddev->disks.next,
5986 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005987 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 .load_super(rdev, rdev0, mddev->minor_version);
5989 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005990 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005992 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 bdevname(rdev0->bdev,b2));
5994 export_rdev(rdev);
5995 return -EINVAL;
5996 }
5997 }
5998 err = bind_rdev_to_array(rdev, mddev);
5999 if (err)
6000 export_rdev(rdev);
6001 return err;
6002 }
6003
6004 /*
6005 * add_new_disk can be used once the array is assembled
6006 * to add "hot spares". They must already have a superblock
6007 * written
6008 */
6009 if (mddev->pers) {
6010 int err;
6011 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006012 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 "%s: personality does not support diskops!\n",
6014 mdname(mddev));
6015 return -EINVAL;
6016 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006017 if (mddev->persistent)
6018 rdev = md_import_device(dev, mddev->major_version,
6019 mddev->minor_version);
6020 else
6021 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006023 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 "md: md_import_device returned %ld\n",
6025 PTR_ERR(rdev));
6026 return PTR_ERR(rdev);
6027 }
NeilBrown1a855a02010-12-09 16:36:28 +11006028 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006029 if (!mddev->persistent) {
6030 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006031 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006032 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006033 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006034 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006035 } else
NeilBrown41158c72005-06-21 17:17:25 -07006036 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006037 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006038 } else
6039 super_types[mddev->major_version].
6040 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006041 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006042 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006043 /* This was a hot-add request, but events doesn't
6044 * match, so reject it.
6045 */
6046 export_rdev(rdev);
6047 return -EINVAL;
6048 }
6049
NeilBrownb2d444d2005-11-08 21:39:31 -08006050 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006051 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6052 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006053 else
6054 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006055
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006056 if (info->state & (1<<MD_DISK_JOURNAL)) {
6057 struct md_rdev *rdev2;
6058 bool has_journal = false;
6059
6060 /* make sure no existing journal disk */
6061 rdev_for_each(rdev2, mddev) {
6062 if (test_bit(Journal, &rdev2->flags)) {
6063 has_journal = true;
6064 break;
6065 }
6066 }
6067 if (has_journal) {
6068 export_rdev(rdev);
6069 return -EBUSY;
6070 }
Song Liubac624f2015-08-13 14:31:55 -07006071 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006072 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006073 /*
6074 * check whether the device shows up in other nodes
6075 */
6076 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006077 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006078 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006079 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006080 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006081 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006082 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006083 export_rdev(rdev);
6084 return err;
6085 }
6086 }
6087 }
6088
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 rdev->raid_disk = -1;
6090 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006091
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 if (err)
6093 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006094
6095 if (mddev_is_clustered(mddev)) {
6096 if (info->state & (1 << MD_DISK_CANDIDATE))
6097 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6098 else {
6099 if (err)
6100 md_cluster_ops->add_new_disk_cancel(mddev);
6101 else
6102 err = add_bound_rdev(rdev);
6103 }
6104
6105 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006106 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006107
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 return err;
6109 }
6110
6111 /* otherwise, add_new_disk is only allowed
6112 * for major_version==0 superblocks
6113 */
6114 if (mddev->major_version != 0) {
6115 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6116 mdname(mddev));
6117 return -EINVAL;
6118 }
6119
6120 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6121 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006122 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006124 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 "md: error, md_import_device() returned %ld\n",
6126 PTR_ERR(rdev));
6127 return PTR_ERR(rdev);
6128 }
6129 rdev->desc_nr = info->number;
6130 if (info->raid_disk < mddev->raid_disks)
6131 rdev->raid_disk = info->raid_disk;
6132 else
6133 rdev->raid_disk = -1;
6134
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006136 if (info->state & (1<<MD_DISK_SYNC))
6137 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006139 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6140 set_bit(WriteMostly, &rdev->flags);
6141
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 if (!mddev->persistent) {
6143 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006144 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6145 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006146 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006147 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
NeilBrown2bf071b2006-01-06 00:20:55 -08006149 err = bind_rdev_to_array(rdev, mddev);
6150 if (err) {
6151 export_rdev(rdev);
6152 return err;
6153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 }
6155
6156 return 0;
6157}
6158
NeilBrownf72ffdd2014-09-30 14:23:59 +10006159static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160{
6161 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006162 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 rdev = find_rdev(mddev, dev);
6165 if (!rdev)
6166 return -ENXIO;
6167
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006168 if (rdev->raid_disk < 0)
6169 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006170
NeilBrown3ea8929d2013-04-24 11:42:41 +10006171 clear_bit(Blocked, &rdev->flags);
6172 remove_and_add_spares(mddev, rdev);
6173
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 if (rdev->raid_disk >= 0)
6175 goto busy;
6176
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006177kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006178 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006179 md_cluster_ops->remove_disk(mddev, rdev);
6180
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006181 md_kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07006182 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006183 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
6185 return 0;
6186busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006187 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 bdevname(rdev->bdev,b), mdname(mddev));
6189 return -EBUSY;
6190}
6191
NeilBrownf72ffdd2014-09-30 14:23:59 +10006192static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193{
6194 char b[BDEVNAME_SIZE];
6195 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006196 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
6198 if (!mddev->pers)
6199 return -ENODEV;
6200
6201 if (mddev->major_version != 0) {
6202 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6203 " version-0 superblocks.\n",
6204 mdname(mddev));
6205 return -EINVAL;
6206 }
6207 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006208 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 "%s: personality does not support diskops!\n",
6210 mdname(mddev));
6211 return -EINVAL;
6212 }
6213
NeilBrownd710e132008-10-13 11:55:12 +11006214 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006216 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 "md: error, md_import_device() returned %ld\n",
6218 PTR_ERR(rdev));
6219 return -EINVAL;
6220 }
6221
6222 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006223 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006225 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
NeilBrown8190e752009-06-18 08:48:58 +10006227 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
NeilBrownb2d444d2005-11-08 21:39:31 -08006229 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006230 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 "md: can not hot-add faulty %s disk to %s!\n",
6232 bdevname(rdev->bdev,b), mdname(mddev));
6233 err = -EINVAL;
6234 goto abort_export;
6235 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006236
NeilBrownb2d444d2005-11-08 21:39:31 -08006237 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006239 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006240 err = bind_rdev_to_array(rdev, mddev);
6241 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006242 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
6244 /*
6245 * The rest should better be atomic, we can have disk failures
6246 * noticed in interrupt contexts ...
6247 */
6248
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 rdev->raid_disk = -1;
6250
NeilBrown850b2b42006-10-03 01:15:46 -07006251 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 /*
6253 * Kick recovery, maybe this spare has to be added to the
6254 * array immediately.
6255 */
6256 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6257 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006258 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 return 0;
6260
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261abort_export:
6262 export_rdev(rdev);
6263 return err;
6264}
6265
NeilBrownfd01b882011-10-11 16:47:53 +11006266static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006267{
NeilBrown035328c2014-04-09 12:25:40 +10006268 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006269
NeilBrown36fa3062005-09-09 16:23:45 -07006270 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006271 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006272 return -EBUSY;
6273 if (mddev->recovery || mddev->sync_thread)
6274 return -EBUSY;
6275 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006276 }
6277
NeilBrown36fa3062005-09-09 16:23:45 -07006278 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006279 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006280 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006281
NeilBrown1e594bb2014-12-15 12:57:00 +11006282 if (mddev->bitmap || mddev->bitmap_info.file)
6283 return -EEXIST; /* cannot add when bitmap is present */
6284 f = fget(fd);
6285
6286 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006287 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6288 mdname(mddev));
6289 return -EBADF;
6290 }
6291
NeilBrown1e594bb2014-12-15 12:57:00 +11006292 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006293 if (!S_ISREG(inode->i_mode)) {
6294 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6295 mdname(mddev));
6296 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006297 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006298 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6299 mdname(mddev));
6300 err = -EBADF;
6301 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006302 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6303 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006304 err = -EBUSY;
6305 }
6306 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006307 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006308 return err;
6309 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006310 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006311 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006312 } else if (mddev->bitmap == NULL)
6313 return -ENOENT; /* cannot remove what isn't there */
6314 err = 0;
6315 if (mddev->pers) {
6316 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006317 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006318 struct bitmap *bitmap;
6319
6320 bitmap = bitmap_create(mddev, -1);
6321 if (!IS_ERR(bitmap)) {
6322 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006323 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006324 } else
6325 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006326 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006327 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006328 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006329 fd = -1; /* make sure to put the file */
6330 }
NeilBrown36fa3062005-09-09 16:23:45 -07006331 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006332 }
6333 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006334 struct file *f = mddev->bitmap_info.file;
6335 if (f) {
6336 spin_lock(&mddev->lock);
6337 mddev->bitmap_info.file = NULL;
6338 spin_unlock(&mddev->lock);
6339 fput(f);
6340 }
NeilBrown36fa3062005-09-09 16:23:45 -07006341 }
6342
NeilBrown32a76272005-06-21 17:17:14 -07006343 return err;
6344}
6345
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346/*
6347 * set_array_info is used two different ways
6348 * The original usage is when creating a new array.
6349 * In this usage, raid_disks is > 0 and it together with
6350 * level, size, not_persistent,layout,chunksize determine the
6351 * shape of the array.
6352 * This will always create an array with a type-0.90.0 superblock.
6353 * The newer usage is when assembling an array.
6354 * In this case raid_disks will be 0, and the major_version field is
6355 * use to determine which style super-blocks are to be found on the devices.
6356 * The minor and patch _version numbers are also kept incase the
6357 * super_block handler wishes to interpret them.
6358 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006359static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360{
6361
6362 if (info->raid_disks == 0) {
6363 /* just setting version number for superblock loading */
6364 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006365 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 super_types[info->major_version].name == NULL) {
6367 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006368 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 "md: superblock version %d not known\n",
6370 info->major_version);
6371 return -EINVAL;
6372 }
6373 mddev->major_version = info->major_version;
6374 mddev->minor_version = info->minor_version;
6375 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006376 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006377 /* ensure mddev_put doesn't delete this now that there
6378 * is some minimal configuration.
6379 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006380 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 return 0;
6382 }
6383 mddev->major_version = MD_MAJOR_VERSION;
6384 mddev->minor_version = MD_MINOR_VERSION;
6385 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006386 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
6388 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006389 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006390 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 mddev->raid_disks = info->raid_disks;
6392 /* don't set md_minor, it is determined by which /dev/md* was
6393 * openned
6394 */
6395 if (info->state & (1<<MD_SB_CLEAN))
6396 mddev->recovery_cp = MaxSector;
6397 else
6398 mddev->recovery_cp = 0;
6399 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006400 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401
6402 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006403 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404
6405 mddev->max_disks = MD_SB_DISKS;
6406
NeilBrowne6910632008-02-06 01:39:51 -08006407 if (mddev->persistent)
6408 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006409 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410
NeilBrownc3d97142009-12-14 12:49:52 +11006411 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006412 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006413 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006414
NeilBrownf6705572006-03-27 01:18:11 -08006415 mddev->reshape_position = MaxSector;
6416
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 /*
6418 * Generate a 128 bit UUID
6419 */
6420 get_random_bytes(mddev->uuid, 16);
6421
NeilBrownf6705572006-03-27 01:18:11 -08006422 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006423 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006424 mddev->new_layout = mddev->layout;
6425 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006426 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006427
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 return 0;
6429}
6430
NeilBrownfd01b882011-10-11 16:47:53 +11006431void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006432{
Dan Williamsb522adc2009-03-31 15:00:31 +11006433 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6434
6435 if (mddev->external_size)
6436 return;
6437
Dan Williams1f403622009-03-31 14:59:03 +11006438 mddev->array_sectors = array_sectors;
6439}
6440EXPORT_SYMBOL(md_set_array_sectors);
6441
NeilBrownfd01b882011-10-11 16:47:53 +11006442static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006443{
NeilBrown3cb03002011-10-11 16:45:26 +11006444 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006445 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006446 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006447
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006448 /* cluster raid doesn't support update size */
6449 if (mddev_is_clustered(mddev))
6450 return -EINVAL;
6451
NeilBrowna35b0d62006-01-06 00:20:49 -08006452 if (mddev->pers->resize == NULL)
6453 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006454 /* The "num_sectors" is the number of sectors of each device that
6455 * is used. This can only make sense for arrays with redundancy.
6456 * linear and raid0 always use whatever space is available. We can only
6457 * consider changing this number if no resync or reconstruction is
6458 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006459 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006460 * of each device. If num_sectors is zero, we find the largest size
6461 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006462 */
NeilBrownf851b602014-12-11 10:02:10 +11006463 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6464 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006465 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006466 if (mddev->ro)
6467 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006468
NeilBrowndafb20f2012-03-19 12:46:39 +11006469 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006470 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006471
Andre Nolld71f9f82008-07-11 22:02:22 +10006472 if (fit && (num_sectors == 0 || num_sectors > avail))
6473 num_sectors = avail;
6474 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006475 return -ENOSPC;
6476 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006477 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006478 if (!rv)
6479 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006480 return rv;
6481}
6482
NeilBrownfd01b882011-10-11 16:47:53 +11006483static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006484{
6485 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006486 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006487 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006488 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006489 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006490 if (mddev->ro)
6491 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006492 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006493 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006494 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006495 if (mddev->sync_thread ||
6496 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6497 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006498 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006499
6500 rdev_for_each(rdev, mddev) {
6501 if (mddev->raid_disks < raid_disks &&
6502 rdev->data_offset < rdev->new_data_offset)
6503 return -EINVAL;
6504 if (mddev->raid_disks > raid_disks &&
6505 rdev->data_offset > rdev->new_data_offset)
6506 return -EINVAL;
6507 }
6508
NeilBrown63c70c42006-03-27 01:18:13 -08006509 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006510 if (mddev->delta_disks < 0)
6511 mddev->reshape_backwards = 1;
6512 else if (mddev->delta_disks > 0)
6513 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006514
6515 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006516 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006517 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006518 mddev->reshape_backwards = 0;
6519 }
NeilBrownda943b992006-01-06 00:20:54 -08006520 return rv;
6521}
6522
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523/*
6524 * update_array_info is used to change the configuration of an
6525 * on-line array.
6526 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6527 * fields in the info are checked against the array.
6528 * Any differences that cannot be handled will cause an error.
6529 * Normally, only one change can be managed at a time.
6530 */
NeilBrownfd01b882011-10-11 16:47:53 +11006531static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532{
6533 int rv = 0;
6534 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006535 int state = 0;
6536
6537 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006538 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006539 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540
6541 if (mddev->major_version != info->major_version ||
6542 mddev->minor_version != info->minor_version ||
6543/* mddev->patch_version != info->patch_version || */
6544 mddev->ctime != info->ctime ||
6545 mddev->level != info->level ||
6546/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006547 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006548 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006549 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6550 ((state^info->state) & 0xfffffe00)
6551 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 return -EINVAL;
6553 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006554 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6555 cnt++;
6556 if (mddev->raid_disks != info->raid_disks)
6557 cnt++;
6558 if (mddev->layout != info->layout)
6559 cnt++;
6560 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6561 cnt++;
6562 if (cnt == 0)
6563 return 0;
6564 if (cnt > 1)
6565 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566
6567 if (mddev->layout != info->layout) {
6568 /* Change layout
6569 * we don't need to do anything at the md level, the
6570 * personality will take care of it all.
6571 */
NeilBrown50ac1682009-06-18 08:47:55 +10006572 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006574 else {
6575 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006576 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006577 if (rv)
6578 mddev->new_layout = mddev->layout;
6579 return rv;
6580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006582 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006583 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
NeilBrownda943b992006-01-06 00:20:54 -08006585 if (mddev->raid_disks != info->raid_disks)
6586 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587
NeilBrown36fa3062005-09-09 16:23:45 -07006588 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006589 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6590 rv = -EINVAL;
6591 goto err;
6592 }
6593 if (mddev->recovery || mddev->sync_thread) {
6594 rv = -EBUSY;
6595 goto err;
6596 }
NeilBrown36fa3062005-09-09 16:23:45 -07006597 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006598 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006599 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006600 if (mddev->bitmap) {
6601 rv = -EEXIST;
6602 goto err;
6603 }
6604 if (mddev->bitmap_info.default_offset == 0) {
6605 rv = -EINVAL;
6606 goto err;
6607 }
NeilBrownc3d97142009-12-14 12:49:52 +11006608 mddev->bitmap_info.offset =
6609 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006610 mddev->bitmap_info.space =
6611 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006612 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006613 bitmap = bitmap_create(mddev, -1);
6614 if (!IS_ERR(bitmap)) {
6615 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006616 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006617 } else
6618 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006619 if (rv)
6620 bitmap_destroy(mddev);
6621 mddev->pers->quiesce(mddev, 0);
6622 } else {
6623 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006624 if (!mddev->bitmap) {
6625 rv = -ENOENT;
6626 goto err;
6627 }
6628 if (mddev->bitmap->storage.file) {
6629 rv = -EINVAL;
6630 goto err;
6631 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006632 if (mddev->bitmap_info.nodes) {
6633 /* hold PW on all the bitmap lock */
6634 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6635 printk("md: can't change bitmap to none since the"
6636 " array is in use by more than one node\n");
6637 rv = -EPERM;
6638 md_cluster_ops->unlock_all_bitmaps(mddev);
6639 goto err;
6640 }
6641
6642 mddev->bitmap_info.nodes = 0;
6643 md_cluster_ops->leave(mddev);
6644 }
NeilBrown36fa3062005-09-09 16:23:45 -07006645 mddev->pers->quiesce(mddev, 1);
6646 bitmap_destroy(mddev);
6647 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006648 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006649 }
6650 }
NeilBrown850b2b42006-10-03 01:15:46 -07006651 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006652 return rv;
6653err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 return rv;
6655}
6656
NeilBrownfd01b882011-10-11 16:47:53 +11006657static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658{
NeilBrown3cb03002011-10-11 16:45:26 +11006659 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006660 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661
6662 if (mddev->pers == NULL)
6663 return -ENODEV;
6664
NeilBrown1ca69c42012-10-11 13:37:33 +11006665 rcu_read_lock();
6666 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006668 err = -ENODEV;
6669 else {
6670 md_error(mddev, rdev);
6671 if (!test_bit(Faulty, &rdev->flags))
6672 err = -EBUSY;
6673 }
6674 rcu_read_unlock();
6675 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676}
6677
Andre Noll2f9618c2008-04-25 18:57:58 +02006678/*
6679 * We have a problem here : there is no easy way to give a CHS
6680 * virtual geometry. We currently pretend that we have a 2 heads
6681 * 4 sectors (with a BIG number of cylinders...). This drives
6682 * dosfs just mad... ;-)
6683 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006684static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6685{
NeilBrownfd01b882011-10-11 16:47:53 +11006686 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006687
6688 geo->heads = 2;
6689 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006690 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006691 return 0;
6692}
6693
Nicolas Schichancb335f82014-01-15 16:58:52 +01006694static inline bool md_ioctl_valid(unsigned int cmd)
6695{
6696 switch (cmd) {
6697 case ADD_NEW_DISK:
6698 case BLKROSET:
6699 case GET_ARRAY_INFO:
6700 case GET_BITMAP_FILE:
6701 case GET_DISK_INFO:
6702 case HOT_ADD_DISK:
6703 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006704 case RAID_AUTORUN:
6705 case RAID_VERSION:
6706 case RESTART_ARRAY_RW:
6707 case RUN_ARRAY:
6708 case SET_ARRAY_INFO:
6709 case SET_BITMAP_FILE:
6710 case SET_DISK_FAULTY:
6711 case STOP_ARRAY:
6712 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006713 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006714 return true;
6715 default:
6716 return false;
6717 }
6718}
6719
Al Viroa39907f2008-03-02 10:31:15 -05006720static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 unsigned int cmd, unsigned long arg)
6722{
6723 int err = 0;
6724 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006725 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006726 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727
Nicolas Schichancb335f82014-01-15 16:58:52 +01006728 if (!md_ioctl_valid(cmd))
6729 return -ENOTTY;
6730
NeilBrown506c9e42011-12-23 10:17:26 +11006731 switch (cmd) {
6732 case RAID_VERSION:
6733 case GET_ARRAY_INFO:
6734 case GET_DISK_INFO:
6735 break;
6736 default:
6737 if (!capable(CAP_SYS_ADMIN))
6738 return -EACCES;
6739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740
6741 /*
6742 * Commands dealing with the RAID driver but not any
6743 * particular array:
6744 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006745 switch (cmd) {
6746 case RAID_VERSION:
6747 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006748 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006751 case RAID_AUTORUN:
6752 err = 0;
6753 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006754 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006756 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 }
6758
6759 /*
6760 * Commands creating/starting a new array:
6761 */
6762
Al Viroa39907f2008-03-02 10:31:15 -05006763 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
6765 if (!mddev) {
6766 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006767 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 }
6769
NeilBrown1ca69c42012-10-11 13:37:33 +11006770 /* Some actions do not requires the mutex */
6771 switch (cmd) {
6772 case GET_ARRAY_INFO:
6773 if (!mddev->raid_disks && !mddev->external)
6774 err = -ENODEV;
6775 else
6776 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006777 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006778
6779 case GET_DISK_INFO:
6780 if (!mddev->raid_disks && !mddev->external)
6781 err = -ENODEV;
6782 else
6783 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006784 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006785
6786 case SET_DISK_FAULTY:
6787 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006788 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006789
6790 case GET_BITMAP_FILE:
6791 err = get_bitmap_file(mddev, argp);
6792 goto out;
6793
NeilBrown1ca69c42012-10-11 13:37:33 +11006794 }
6795
NeilBrowna7a3f082012-12-11 13:35:54 +11006796 if (cmd == ADD_NEW_DISK)
6797 /* need to ensure md_delayed_delete() has completed */
6798 flush_workqueue(md_misc_wq);
6799
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006800 if (cmd == HOT_REMOVE_DISK)
6801 /* need to ensure recovery thread has run */
6802 wait_event_interruptible_timeout(mddev->sb_wait,
6803 !test_bit(MD_RECOVERY_NEEDED,
6804 &mddev->flags),
6805 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006806 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6807 /* Need to flush page cache, and ensure no-one else opens
6808 * and writes
6809 */
6810 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006811 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006812 mutex_unlock(&mddev->open_mutex);
6813 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006814 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006815 }
6816 set_bit(MD_STILL_CLOSED, &mddev->flags);
6817 mutex_unlock(&mddev->open_mutex);
6818 sync_blockdev(bdev);
6819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 err = mddev_lock(mddev);
6821 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006822 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6824 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 }
6827
NeilBrownc02c0ae2012-12-11 13:39:21 +11006828 if (cmd == SET_ARRAY_INFO) {
6829 mdu_array_info_t info;
6830 if (!arg)
6831 memset(&info, 0, sizeof(info));
6832 else if (copy_from_user(&info, argp, sizeof(info))) {
6833 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006834 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006835 }
6836 if (mddev->pers) {
6837 err = update_array_info(mddev, &info);
6838 if (err) {
6839 printk(KERN_WARNING "md: couldn't update"
6840 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006841 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006843 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006844 }
6845 if (!list_empty(&mddev->disks)) {
6846 printk(KERN_WARNING
6847 "md: array %s already has disks!\n",
6848 mdname(mddev));
6849 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006850 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006851 }
6852 if (mddev->raid_disks) {
6853 printk(KERN_WARNING
6854 "md: array %s already initialised!\n",
6855 mdname(mddev));
6856 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006857 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006858 }
6859 err = set_array_info(mddev, &info);
6860 if (err) {
6861 printk(KERN_WARNING "md: couldn't set"
6862 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006863 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006864 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006865 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 }
6867
6868 /*
6869 * Commands querying/configuring an existing array:
6870 */
NeilBrown32a76272005-06-21 17:17:14 -07006871 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006872 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006873 if ((!mddev->raid_disks && !mddev->external)
6874 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6875 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6876 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006878 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 }
6880
6881 /*
6882 * Commands even a read-only array can execute:
6883 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006884 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006885 case RESTART_ARRAY_RW:
6886 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006887 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006888
6889 case STOP_ARRAY:
6890 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006891 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006892
6893 case STOP_ARRAY_RO:
6894 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006895 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006896
NeilBrown3ea8929d2013-04-24 11:42:41 +10006897 case HOT_REMOVE_DISK:
6898 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006899 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006900
NeilBrown7ceb17e2013-04-24 11:42:42 +10006901 case ADD_NEW_DISK:
6902 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08006903 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10006904 * So require mddev->pers and MD_DISK_SYNC.
6905 */
6906 if (mddev->pers) {
6907 mdu_disk_info_t info;
6908 if (copy_from_user(&info, argp, sizeof(info)))
6909 err = -EFAULT;
6910 else if (!(info.state & (1<<MD_DISK_SYNC)))
6911 /* Need to clear read-only for this */
6912 break;
6913 else
6914 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006915 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006916 }
6917 break;
6918
NeilBrownc02c0ae2012-12-11 13:39:21 +11006919 case BLKROSET:
6920 if (get_user(ro, (int __user *)(arg))) {
6921 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006922 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006923 }
6924 err = -EINVAL;
6925
6926 /* if the bdev is going readonly the value of mddev->ro
6927 * does not matter, no writes are coming
6928 */
6929 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006930 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006931
NeilBrownc02c0ae2012-12-11 13:39:21 +11006932 /* are we are already prepared for writes? */
6933 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006934 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006935
6936 /* transitioning to readauto need only happen for
6937 * arrays that call md_write_start
6938 */
6939 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006941 if (err == 0) {
6942 mddev->ro = 2;
6943 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006944 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006945 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006946 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 }
6948
6949 /*
6950 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006951 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 */
NeilBrown326eb172014-09-30 15:36:28 +10006953 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006954 if (mddev->ro == 2) {
6955 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006956 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006957 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006958 /* mddev_unlock will wake thread */
6959 /* If a device failed while we were read-only, we
6960 * need to make sure the metadata is updated now.
6961 */
6962 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6963 mddev_unlock(mddev);
6964 wait_event(mddev->sb_wait,
6965 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6966 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006967 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006968 }
NeilBrownf91de922005-11-08 21:39:36 -08006969 } else {
6970 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006971 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 }
6974
NeilBrownc02c0ae2012-12-11 13:39:21 +11006975 switch (cmd) {
6976 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006978 mdu_disk_info_t info;
6979 if (copy_from_user(&info, argp, sizeof(info)))
6980 err = -EFAULT;
6981 else
6982 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006983 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006986 case CLUSTERED_DISK_NACK:
6987 if (mddev_is_clustered(mddev))
6988 md_cluster_ops->new_disk_ack(mddev, false);
6989 else
6990 err = -EINVAL;
6991 goto unlock;
6992
NeilBrownc02c0ae2012-12-11 13:39:21 +11006993 case HOT_ADD_DISK:
6994 err = hot_add_disk(mddev, new_decode_dev(arg));
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 RUN_ARRAY:
6998 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006999 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000
NeilBrownc02c0ae2012-12-11 13:39:21 +11007001 case SET_BITMAP_FILE:
7002 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007003 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007004
NeilBrownc02c0ae2012-12-11 13:39:21 +11007005 default:
7006 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007007 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 }
7009
NeilBrown3adc28d2014-09-30 15:46:41 +10007010unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007011 if (mddev->hold_active == UNTIL_IOCTL &&
7012 err != -EINVAL)
7013 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007015out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 return err;
7017}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007018#ifdef CONFIG_COMPAT
7019static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7020 unsigned int cmd, unsigned long arg)
7021{
7022 switch (cmd) {
7023 case HOT_REMOVE_DISK:
7024 case HOT_ADD_DISK:
7025 case SET_DISK_FAULTY:
7026 case SET_BITMAP_FILE:
7027 /* These take in integer arg, do not convert */
7028 break;
7029 default:
7030 arg = (unsigned long)compat_ptr(arg);
7031 break;
7032 }
7033
7034 return md_ioctl(bdev, mode, cmd, arg);
7035}
7036#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037
Al Viroa39907f2008-03-02 10:31:15 -05007038static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039{
7040 /*
7041 * Succeed if we can lock the mddev, which confirms that
7042 * it isn't being stopped right now.
7043 */
NeilBrownfd01b882011-10-11 16:47:53 +11007044 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 int err;
7046
Yuanhan Liu0c098222012-05-22 13:55:32 +10007047 if (!mddev)
7048 return -ENODEV;
7049
NeilBrownd3374822009-01-09 08:31:10 +11007050 if (mddev->gendisk != bdev->bd_disk) {
7051 /* we are racing with mddev_put which is discarding this
7052 * bd_disk.
7053 */
7054 mddev_put(mddev);
7055 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007056 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007057 /* Then retry the open from the top */
7058 return -ERESTARTSYS;
7059 }
7060 BUG_ON(mddev != bdev->bd_disk->private_data);
7061
NeilBrownc8c00a62009-08-10 12:50:52 +10007062 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 goto out;
7064
7065 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007066 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10007067 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10007068 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007070 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 out:
7072 return err;
7073}
7074
Al Virodb2a1442013-05-05 21:52:57 -04007075static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007077 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007079 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007080 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007083
7084static int md_media_changed(struct gendisk *disk)
7085{
NeilBrownfd01b882011-10-11 16:47:53 +11007086 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007087
7088 return mddev->changed;
7089}
7090
7091static int md_revalidate(struct gendisk *disk)
7092{
NeilBrownfd01b882011-10-11 16:47:53 +11007093 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007094
7095 mddev->changed = 0;
7096 return 0;
7097}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007098static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099{
7100 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007101 .open = md_open,
7102 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007103 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007104#ifdef CONFIG_COMPAT
7105 .compat_ioctl = md_compat_ioctl,
7106#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007107 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007108 .media_changed = md_media_changed,
7109 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110};
7111
NeilBrownf72ffdd2014-09-30 14:23:59 +10007112static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113{
NeilBrown2b8bf342011-10-11 16:48:23 +11007114 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 /*
7117 * md_thread is a 'system-thread', it's priority should be very
7118 * high. We avoid resource deadlocks individually in each
7119 * raid personality. (RAID5 does preallocation) We also use RR and
7120 * the very same RT priority as kswapd, thus we will never get
7121 * into a priority inversion deadlock.
7122 *
7123 * we definitely have to have equal or higher priority than
7124 * bdflush, otherwise bdflush will deadlock if there are too
7125 * many dirty RAID5 blocks.
7126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127
NeilBrown6985c432005-10-19 21:23:47 -07007128 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007129 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130
NeilBrown93588e22005-11-15 00:09:12 -08007131 /* We need to wait INTERRUPTIBLE so that
7132 * we don't add to the load-average.
7133 * That means we need to be sure no signals are
7134 * pending
7135 */
7136 if (signal_pending(current))
7137 flush_signals(current);
7138
7139 wait_event_interruptible_timeout
7140 (thread->wqueue,
7141 test_bit(THREAD_WAKEUP, &thread->flags)
7142 || kthread_should_stop(),
7143 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144
NeilBrown6c987912011-01-14 09:13:53 +11007145 clear_bit(THREAD_WAKEUP, &thread->flags);
7146 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007147 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007149
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 return 0;
7151}
7152
NeilBrown2b8bf342011-10-11 16:48:23 +11007153void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154{
7155 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007156 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 set_bit(THREAD_WAKEUP, &thread->flags);
7158 wake_up(&thread->wqueue);
7159 }
7160}
NeilBrown6c144d32014-09-30 16:15:38 +10007161EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162
Shaohua Li4ed87312012-10-11 13:34:00 +11007163struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7164 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165{
NeilBrown2b8bf342011-10-11 16:48:23 +11007166 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
NeilBrown2b8bf342011-10-11 16:48:23 +11007168 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 if (!thread)
7170 return NULL;
7171
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 init_waitqueue_head(&thread->wqueue);
7173
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 thread->run = run;
7175 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007176 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007177 thread->tsk = kthread_run(md_thread, thread,
7178 "%s_%s",
7179 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007180 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007181 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 kfree(thread);
7183 return NULL;
7184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 return thread;
7186}
NeilBrown6c144d32014-09-30 16:15:38 +10007187EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188
NeilBrown2b8bf342011-10-11 16:48:23 +11007189void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190{
NeilBrown2b8bf342011-10-11 16:48:23 +11007191 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007192 if (!thread)
7193 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007194 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007195 /* Locking ensures that mddev_unlock does not wake_up a
7196 * non-existent thread
7197 */
7198 spin_lock(&pers_lock);
7199 *threadp = NULL;
7200 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007201
7202 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 kfree(thread);
7204}
NeilBrown6c144d32014-09-30 16:15:38 +10007205EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206
NeilBrownfd01b882011-10-11 16:47:53 +11007207void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208{
NeilBrownb2d444d2005-11-08 21:39:31 -08007209 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007211
NeilBrownde393cd2011-07-28 11:31:48 +10007212 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 return;
7214 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007215 if (mddev->degraded)
7216 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007217 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7219 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7220 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007221 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007222 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007223 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224}
NeilBrown6c144d32014-09-30 16:15:38 +10007225EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226
7227/* seq_file implementation /proc/mdstat */
7228
7229static void status_unused(struct seq_file *seq)
7230{
7231 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007232 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233
7234 seq_printf(seq, "unused devices: ");
7235
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007236 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 char b[BDEVNAME_SIZE];
7238 i++;
7239 seq_printf(seq, "%s ",
7240 bdevname(rdev->bdev,b));
7241 }
7242 if (!i)
7243 seq_printf(seq, "<none>");
7244
7245 seq_printf(seq, "\n");
7246}
7247
NeilBrownf7851be2015-07-02 17:12:58 +10007248static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249{
NeilBrowndd71cf62009-05-07 12:49:35 +10007250 sector_t max_sectors, resync, res;
7251 unsigned long dt, db;
7252 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007253 int scale;
7254 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255
NeilBrownc804cde2012-05-21 09:28:33 +10007256 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7257 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007258 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007260 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261
NeilBrownf7851be2015-07-02 17:12:58 +10007262 resync = mddev->curr_resync;
7263 if (resync <= 3) {
7264 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7265 /* Still cleaning up */
7266 resync = max_sectors;
7267 } else
7268 resync -= atomic_read(&mddev->recovery_active);
7269
7270 if (resync == 0) {
7271 if (mddev->recovery_cp < MaxSector) {
7272 seq_printf(seq, "\tresync=PENDING");
7273 return 1;
7274 }
7275 return 0;
7276 }
7277 if (resync < 3) {
7278 seq_printf(seq, "\tresync=DELAYED");
7279 return 1;
7280 }
7281
NeilBrown403df472014-09-30 15:52:29 +10007282 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007283 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007284 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007285 * u32, as those are the requirements for sector_div.
7286 * Thus 'scale' must be at least 10
7287 */
7288 scale = 10;
7289 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007290 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007291 scale++;
7292 }
7293 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007294 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007295
7296 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 {
NeilBrown4588b422006-03-27 01:18:04 -08007298 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 seq_printf(seq, "[");
7300 for (i = 0; i < x; i++)
7301 seq_printf(seq, "=");
7302 seq_printf(seq, ">");
7303 for (i = 0; i < y; i++)
7304 seq_printf(seq, ".");
7305 seq_printf(seq, "] ");
7306 }
NeilBrown4588b422006-03-27 01:18:04 -08007307 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007308 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7309 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007310 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7311 "check" :
7312 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7313 "resync" : "recovery"))),
7314 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007315 (unsigned long long) resync/2,
7316 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317
7318 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 * dt: time from mark until now
7320 * db: blocks written from mark until now
7321 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007322 *
7323 * rt is a sector_t, so could be 32bit or 64bit.
7324 * So we divide before multiply in case it is 32bit and close
7325 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007326 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007327 * near the end of resync when the number of remaining sectors
7328 * is close to 'db'.
7329 * We then divide rt by 32 after multiplying by db to compensate.
7330 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 */
7332 dt = ((jiffies - mddev->resync_mark) / HZ);
7333 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007334 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7335 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336
NeilBrowndd71cf62009-05-07 12:49:35 +10007337 rt = max_sectors - resync; /* number of remaining sectors */
7338 sector_div(rt, db/32+1);
7339 rt *= dt;
7340 rt >>= 5;
7341
7342 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7343 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344
NeilBrownff4e8d92006-07-10 04:44:16 -07007345 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007346 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347}
7348
7349static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7350{
7351 struct list_head *tmp;
7352 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007353 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354
7355 if (l >= 0x10000)
7356 return NULL;
7357 if (!l--)
7358 /* header */
7359 return (void*)1;
7360
7361 spin_lock(&all_mddevs_lock);
7362 list_for_each(tmp,&all_mddevs)
7363 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007364 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 mddev_get(mddev);
7366 spin_unlock(&all_mddevs_lock);
7367 return mddev;
7368 }
7369 spin_unlock(&all_mddevs_lock);
7370 if (!l--)
7371 return (void*)2;/* tail */
7372 return NULL;
7373}
7374
7375static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7376{
7377 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007378 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007379
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 ++*pos;
7381 if (v == (void*)2)
7382 return NULL;
7383
7384 spin_lock(&all_mddevs_lock);
7385 if (v == (void*)1)
7386 tmp = all_mddevs.next;
7387 else
7388 tmp = mddev->all_mddevs.next;
7389 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007390 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 else {
7392 next_mddev = (void*)2;
7393 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395 spin_unlock(&all_mddevs_lock);
7396
7397 if (v != (void*)1)
7398 mddev_put(mddev);
7399 return next_mddev;
7400
7401}
7402
7403static void md_seq_stop(struct seq_file *seq, void *v)
7404{
NeilBrownfd01b882011-10-11 16:47:53 +11007405 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406
7407 if (mddev && v != (void*)1 && v != (void*)2)
7408 mddev_put(mddev);
7409}
7410
7411static int md_seq_show(struct seq_file *seq, void *v)
7412{
NeilBrownfd01b882011-10-11 16:47:53 +11007413 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007414 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007415 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416
7417 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007418 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 seq_printf(seq, "Personalities : ");
7420 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007421 list_for_each_entry(pers, &pers_list, list)
7422 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423
7424 spin_unlock(&pers_lock);
7425 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007426 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 return 0;
7428 }
7429 if (v == (void*)2) {
7430 status_unused(seq);
7431 return 0;
7432 }
7433
NeilBrown36d091f2014-12-15 12:56:58 +11007434 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7436 seq_printf(seq, "%s : %sactive", mdname(mddev),
7437 mddev->pers ? "" : "in");
7438 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007439 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007441 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007442 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 seq_printf(seq, " %s", mddev->pers->name);
7444 }
7445
Andre Nolldd8ac332009-03-31 14:33:13 +11007446 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007447 rcu_read_lock();
7448 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 char b[BDEVNAME_SIZE];
7450 seq_printf(seq, " %s[%d]",
7451 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007452 if (test_bit(WriteMostly, &rdev->flags))
7453 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007454 if (test_bit(Journal, &rdev->flags))
7455 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007456 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 seq_printf(seq, "(F)");
7458 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007459 }
7460 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007461 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007462 if (test_bit(Replacement, &rdev->flags))
7463 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007464 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007466 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467
7468 if (!list_empty(&mddev->disks)) {
7469 if (mddev->pers)
7470 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007471 (unsigned long long)
7472 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 else
7474 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007475 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007477 if (mddev->persistent) {
7478 if (mddev->major_version != 0 ||
7479 mddev->minor_version != 90) {
7480 seq_printf(seq," super %d.%d",
7481 mddev->major_version,
7482 mddev->minor_version);
7483 }
NeilBrowne6910632008-02-06 01:39:51 -08007484 } else if (mddev->external)
7485 seq_printf(seq, " super external:%s",
7486 mddev->metadata_type);
7487 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007488 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489
7490 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007491 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007492 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007493 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007494 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007495 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007496 }
NeilBrown32a76272005-06-21 17:17:14 -07007497 } else
7498 seq_printf(seq, "\n ");
7499
NeilBrown57148962012-03-19 12:46:40 +11007500 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501
7502 seq_printf(seq, "\n");
7503 }
NeilBrown36d091f2014-12-15 12:56:58 +11007504 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007505
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506 return 0;
7507}
7508
Jan Engelhardt110518b2009-05-07 12:49:37 +10007509static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 .start = md_seq_start,
7511 .next = md_seq_next,
7512 .stop = md_seq_stop,
7513 .show = md_seq_show,
7514};
7515
7516static int md_seq_open(struct inode *inode, struct file *file)
7517{
Kay Sieversf1514632011-07-12 20:48:39 +02007518 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519 int error;
7520
7521 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007522 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007523 return error;
7524
7525 seq = file->private_data;
7526 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 return error;
7528}
7529
NeilBrowne2f23b62014-04-09 14:33:51 +10007530static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007531static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7532{
Kay Sieversf1514632011-07-12 20:48:39 +02007533 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007534 int mask;
7535
NeilBrowne2f23b62014-04-09 14:33:51 +10007536 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007537 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007538 poll_wait(filp, &md_event_waiters, wait);
7539
7540 /* always allow read */
7541 mask = POLLIN | POLLRDNORM;
7542
Kay Sieversf1514632011-07-12 20:48:39 +02007543 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007544 mask |= POLLERR | POLLPRI;
7545 return mask;
7546}
7547
Arjan van de Venfa027c22007-02-12 00:55:33 -08007548static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007549 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 .open = md_seq_open,
7551 .read = seq_read,
7552 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007553 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007554 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555};
7556
NeilBrown84fc4b52011-10-11 16:49:58 +11007557int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558{
Chao Yu50bd3772014-09-25 15:28:34 +08007559 printk(KERN_INFO "md: %s personality registered for level %d\n",
7560 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007562 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 spin_unlock(&pers_lock);
7564 return 0;
7565}
NeilBrown6c144d32014-09-30 16:15:38 +10007566EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567
NeilBrown84fc4b52011-10-11 16:49:58 +11007568int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569{
NeilBrown2604b702006-01-06 00:20:36 -08007570 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007572 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 spin_unlock(&pers_lock);
7574 return 0;
7575}
NeilBrown6c144d32014-09-30 16:15:38 +10007576EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577
NeilBrown6022e752015-08-13 12:32:55 +10007578int register_md_cluster_operations(struct md_cluster_operations *ops,
7579 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007580{
NeilBrown6022e752015-08-13 12:32:55 +10007581 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007582 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007583 if (md_cluster_ops != NULL)
7584 ret = -EALREADY;
7585 else {
7586 md_cluster_ops = ops;
7587 md_cluster_mod = module;
7588 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007589 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007590 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007591}
7592EXPORT_SYMBOL(register_md_cluster_operations);
7593
7594int unregister_md_cluster_operations(void)
7595{
7596 spin_lock(&pers_lock);
7597 md_cluster_ops = NULL;
7598 spin_unlock(&pers_lock);
7599 return 0;
7600}
7601EXPORT_SYMBOL(unregister_md_cluster_operations);
7602
7603int md_setup_cluster(struct mddev *mddev, int nodes)
7604{
7605 int err;
7606
7607 err = request_module("md-cluster");
7608 if (err) {
7609 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007610 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007611 }
7612
7613 spin_lock(&pers_lock);
7614 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7615 spin_unlock(&pers_lock);
7616 return -ENOENT;
7617 }
7618 spin_unlock(&pers_lock);
7619
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007620 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007621}
7622
7623void md_cluster_stop(struct mddev *mddev)
7624{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007625 if (!md_cluster_ops)
7626 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007627 md_cluster_ops->leave(mddev);
7628 module_put(md_cluster_mod);
7629}
7630
NeilBrownfd01b882011-10-11 16:47:53 +11007631static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007633 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007635 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636
7637 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007638 rcu_read_lock();
7639 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007641 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7642 (int)part_stat_read(&disk->part0, sectors[1]) -
7643 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007644 /* sync IO will cause sync_io to increase before the disk_stats
7645 * as sync_io is counted when a request starts, and
7646 * disk_stats is counted when it completes.
7647 * So resync activity will cause curr_events to be smaller than
7648 * when there was no such activity.
7649 * non-sync IO will cause disk_stat to increase without
7650 * increasing sync_io so curr_events will (eventually)
7651 * be larger than it was before. Once it becomes
7652 * substantially larger, the test below will cause
7653 * the array to appear non-idle, and resync will slow
7654 * down.
7655 * If there is a lot of outstanding resync activity when
7656 * we set last_event to curr_events, then all that activity
7657 * completing might cause the array to appear non-idle
7658 * and resync will be slowed down even though there might
7659 * not have been non-resync activity. This will only
7660 * happen once though. 'last_events' will soon reflect
7661 * the state where there is little or no outstanding
7662 * resync requests, and further resync activity will
7663 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007664 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007666 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 rdev->last_events = curr_events;
7668 idle = 0;
7669 }
7670 }
NeilBrown4b809912008-07-21 17:05:25 +10007671 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 return idle;
7673}
7674
NeilBrownfd01b882011-10-11 16:47:53 +11007675void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676{
7677 /* another "blocks" (512byte) blocks have been synced */
7678 atomic_sub(blocks, &mddev->recovery_active);
7679 wake_up(&mddev->recovery_wait);
7680 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007681 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007682 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 md_wakeup_thread(mddev->thread);
7684 // stop recovery, signal do_sync ....
7685 }
7686}
NeilBrown6c144d32014-09-30 16:15:38 +10007687EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688
NeilBrown06d91a52005-06-21 17:17:12 -07007689/* md_write_start(mddev, bi)
7690 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007691 * in superblock) before writing, schedule a superblock update
7692 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007693 */
NeilBrownfd01b882011-10-11 16:47:53 +11007694void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695{
Neil Brown0fd62b82008-06-28 08:31:36 +10007696 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007697 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007698 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007699
NeilBrownf91de922005-11-08 21:39:36 -08007700 BUG_ON(mddev->ro == 1);
7701 if (mddev->ro == 2) {
7702 /* need to switch to read/write */
7703 mddev->ro = 0;
7704 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7705 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007706 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007707 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007708 }
NeilBrown06d91a52005-06-21 17:17:12 -07007709 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007710 if (mddev->safemode == 1)
7711 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007712 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007713 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007714 if (mddev->in_sync) {
7715 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007716 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007717 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007718 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007719 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007720 }
NeilBrown85572d72014-12-15 12:56:56 +11007721 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007722 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007723 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007724 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007725 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007726 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727}
NeilBrown6c144d32014-09-30 16:15:38 +10007728EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729
NeilBrownfd01b882011-10-11 16:47:53 +11007730void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007731{
7732 if (atomic_dec_and_test(&mddev->writes_pending)) {
7733 if (mddev->safemode == 2)
7734 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007735 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7737 }
7738}
NeilBrown6c144d32014-09-30 16:15:38 +10007739EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740
NeilBrown2a2275d2007-01-26 00:57:11 -08007741/* md_allow_write(mddev)
7742 * Calling this ensures that the array is marked 'active' so that writes
7743 * may proceed without blocking. It is important to call this before
7744 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7745 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007746 *
Guoqing Jiangabf35082015-12-21 10:51:01 +11007747 * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007748 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007749 */
NeilBrownfd01b882011-10-11 16:47:53 +11007750int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007751{
7752 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007753 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007754 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007755 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007756 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007757 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007758
NeilBrown85572d72014-12-15 12:56:56 +11007759 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007760 if (mddev->in_sync) {
7761 mddev->in_sync = 0;
7762 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007763 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007764 if (mddev->safemode_delay &&
7765 mddev->safemode == 0)
7766 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007767 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007768 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007769 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007770 } else
NeilBrown85572d72014-12-15 12:56:56 +11007771 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007772
NeilBrown070dc6d2010-08-30 17:33:34 +10007773 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007774 return -EAGAIN;
7775 else
7776 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007777}
7778EXPORT_SYMBOL_GPL(md_allow_write);
7779
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780#define SYNC_MARKS 10
7781#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007782#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007783void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784{
Shaohua Li4ed87312012-10-11 13:34:00 +11007785 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007786 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787 unsigned int currspeed = 0,
7788 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007789 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007791 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792 sector_t mark_cnt[SYNC_MARKS];
7793 int last_mark,m;
7794 struct list_head *tmp;
7795 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007796 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007797 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007798 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007799 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007800 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801
7802 /* just incase thread restarts... */
7803 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7804 return;
NeilBrown3991b312014-05-28 13:39:23 +10007805 if (mddev->ro) {/* never try to sync a read-only array */
7806 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007807 return;
NeilBrown3991b312014-05-28 13:39:23 +10007808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007810 if (mddev_is_clustered(mddev)) {
7811 ret = md_cluster_ops->resync_start(mddev);
7812 if (ret)
7813 goto skip;
7814
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04007815 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007816 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7817 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
7818 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
7819 && ((unsigned long long)mddev->curr_resync_completed
7820 < (unsigned long long)mddev->resync_max_sectors))
7821 goto skip;
7822 }
7823
NeilBrown61df9d92006-10-03 01:15:57 -07007824 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007825 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007826 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007827 action = "check";
7828 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007829 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007830 action = "repair";
7831 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007832 desc = "resync";
7833 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7834 desc = "reshape";
7835 else
7836 desc = "recovery";
7837
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007838 mddev->last_sync_action = action ?: desc;
7839
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 /* we overload curr_resync somewhat here.
7841 * 0 == not engaged in resync at all
7842 * 2 == checking that there is no conflict with another sync
7843 * 1 == like 2, but have yielded to allow conflicting resync to
7844 * commense
7845 * other == active in resync - this many blocks
7846 *
7847 * Before starting a resync we must have set curr_resync to
7848 * 2, and then checked that every "conflicting" array has curr_resync
7849 * less than ours. When we find one that is the same or higher
7850 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7851 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7852 * This will mean we have to start checking from the beginning again.
7853 *
7854 */
7855
7856 do {
7857 mddev->curr_resync = 2;
7858
7859 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007860 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007862 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 if (mddev2 == mddev)
7864 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007865 if (!mddev->parallel_resync
7866 && mddev2->curr_resync
7867 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 DEFINE_WAIT(wq);
7869 if (mddev < mddev2 && mddev->curr_resync == 2) {
7870 /* arbitrarily yield */
7871 mddev->curr_resync = 1;
7872 wake_up(&resync_wait);
7873 }
7874 if (mddev > mddev2 && mddev->curr_resync == 1)
7875 /* no need to wait here, we can wait the next
7876 * time 'round when curr_resync == 2
7877 */
7878 continue;
NeilBrown97441972008-09-19 11:49:54 +10007879 /* We need to wait 'interruptible' so as not to
7880 * contribute to the load average, and not to
7881 * be caught by 'softlockup'
7882 */
7883 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007884 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007885 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007886 printk(KERN_INFO "md: delaying %s of %s"
7887 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007889 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007891 if (signal_pending(current))
7892 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893 schedule();
7894 finish_wait(&resync_wait, &wq);
7895 goto try_again;
7896 }
7897 finish_wait(&resync_wait, &wq);
7898 }
7899 }
7900 } while (mddev->curr_resync < 2);
7901
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007902 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007903 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007905 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 */
7907 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007908 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007909 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007910 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7911 j = mddev->resync_min;
7912 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007913 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007914
NeilBrownccfcc3c2006-03-27 01:18:09 -08007915 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007916 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007917 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007919 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007920 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007921 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007922 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007923 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007924 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007925 !test_bit(Faulty, &rdev->flags) &&
7926 !test_bit(In_sync, &rdev->flags) &&
7927 rdev->recovery_offset < j)
7928 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007929 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007930
7931 /* If there is a bitmap, we need to make sure all
7932 * writes that started before we added a spare
7933 * complete before we start doing a recovery.
7934 * Otherwise the write might complete and (via
7935 * bitmap_endwrite) set a bit in the bitmap after the
7936 * recovery has checked that bit and skipped that
7937 * region.
7938 */
7939 if (mddev->bitmap) {
7940 mddev->pers->quiesce(mddev, 1);
7941 mddev->pers->quiesce(mddev, 0);
7942 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944
NeilBrown61df9d92006-10-03 01:15:57 -07007945 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7946 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7947 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007948 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007949 "(but not more than %d KB/sec) for %s.\n",
7950 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951
NeilBrowneea1bf32009-03-31 14:27:02 +11007952 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007953
NeilBrown57afd892005-06-21 17:17:13 -07007954 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955 for (m = 0; m < SYNC_MARKS; m++) {
7956 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007957 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958 }
7959 last_mark = 0;
7960 mddev->resync_mark = mark[last_mark];
7961 mddev->resync_mark_cnt = mark_cnt[last_mark];
7962
7963 /*
7964 * Tune reconstruction:
7965 */
7966 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007967 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7968 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969
7970 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971 last_check = 0;
7972
7973 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007974 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007975 "md: resuming %s of %s from checkpoint.\n",
7976 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007978 } else
7979 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007980 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007981 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7982 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007983 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984
majianpeng7c2c57c2012-07-03 12:12:26 +10007985 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007987 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988
NeilBrown57afd892005-06-21 17:17:13 -07007989 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007990
NeilBrown7a91ee12009-05-26 12:57:21 +10007991 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7992 ((mddev->curr_resync > mddev->curr_resync_completed &&
7993 (mddev->curr_resync - mddev->curr_resync_completed)
7994 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007995 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007996 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007997 >= mddev->resync_max - mddev->curr_resync_completed ||
7998 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007999 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008000 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008001 wait_event(mddev->recovery_wait,
8002 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008003 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008004 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8005 j > mddev->recovery_cp)
8006 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008007 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10008008 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008009 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008010 }
NeilBrownacb180b2009-04-14 16:28:34 +10008011
NeilBrownc91abf52013-11-19 12:02:01 +11008012 while (j >= mddev->resync_max &&
8013 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008014 /* As this condition is controlled by user-space,
8015 * we can block indefinitely, so use '_interruptible'
8016 * to avoid triggering warnings.
8017 */
8018 flush_signals(current); /* just in case */
8019 wait_event_interruptible(mddev->recovery_wait,
8020 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008021 || test_bit(MD_RECOVERY_INTR,
8022 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008023 }
NeilBrownacb180b2009-04-14 16:28:34 +10008024
NeilBrownc91abf52013-11-19 12:02:01 +11008025 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8026 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008027
NeilBrown09314792015-02-19 16:04:40 +11008028 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008029 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008030 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032 }
NeilBrown57afd892005-06-21 17:17:13 -07008033
8034 if (!skipped) { /* actual IO requested */
8035 io_sectors += sectors;
8036 atomic_add(sectors, &mddev->recovery_active);
8037 }
8038
NeilBrowne875ece2011-07-28 11:39:24 +10008039 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8040 break;
8041
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008043 if (j > max_sectors)
8044 /* when skipping, extra large numbers can be returned. */
8045 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008046 if (j > 2)
8047 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008048 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008049 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008050 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008051 * visible in /proc/mdstat
8052 */
8053 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008054
8055 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056 continue;
8057
NeilBrown57afd892005-06-21 17:17:13 -07008058 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059 repeat:
8060 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8061 /* step marks */
8062 int next = (last_mark+1) % SYNC_MARKS;
8063
8064 mddev->resync_mark = mark[next];
8065 mddev->resync_mark_cnt = mark_cnt[next];
8066 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008067 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 last_mark = next;
8069 }
8070
NeilBrownc91abf52013-11-19 12:02:01 +11008071 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8072 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073
8074 /*
8075 * this loop exits only if either when we are slower than
8076 * the 'hard' speed limit, or the system was IO-idle for
8077 * a jiffy.
8078 * the system might be non-idle CPU-wise, but we only care
8079 * about not overloading the IO subsystem. (things like an
8080 * e2fsck being done on the RAID array should execute fast)
8081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082 cond_resched();
8083
Xiao Niac7e50a2014-08-07 09:37:41 -04008084 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8085 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008086 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087
NeilBrown88202a02006-01-06 00:21:36 -08008088 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008089 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008090 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091 goto repeat;
8092 }
NeilBrownac8fa412015-02-19 16:55:00 +11008093 if (!is_mddev_idle(mddev, 0)) {
8094 /*
8095 * Give other IO more of a chance.
8096 * The faster the devices, the less we wait.
8097 */
8098 wait_event(mddev->recovery_wait,
8099 !atomic_read(&mddev->recovery_active));
8100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101 }
8102 }
NeilBrownc91abf52013-11-19 12:02:01 +11008103 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8104 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8105 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106 /*
8107 * this also signals 'finished resyncing' to md_stop
8108 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008109 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8111
NeilBrown5ed1df22015-07-24 13:27:08 +10008112 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8113 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8114 mddev->curr_resync > 2) {
8115 mddev->curr_resync_completed = mddev->curr_resync;
8116 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8117 }
NeilBrown09314792015-02-19 16:04:40 +11008118 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119
NeilBrowndfc70642008-05-23 13:04:39 -07008120 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008121 mddev->curr_resync > 2) {
8122 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8123 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8124 if (mddev->curr_resync >= mddev->recovery_cp) {
8125 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008126 "md: checkpointing %s of %s.\n",
8127 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008128 if (test_bit(MD_RECOVERY_ERROR,
8129 &mddev->recovery))
8130 mddev->recovery_cp =
8131 mddev->curr_resync_completed;
8132 else
8133 mddev->recovery_cp =
8134 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008135 }
8136 } else
8137 mddev->recovery_cp = MaxSector;
8138 } else {
8139 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8140 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008141 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008142 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008143 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008144 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008145 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008146 !test_bit(Faulty, &rdev->flags) &&
8147 !test_bit(In_sync, &rdev->flags) &&
8148 rdev->recovery_offset < mddev->curr_resync)
8149 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008150 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008153 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008154 /* set CHANGE_PENDING here since maybe another update is needed,
8155 * so other nodes are informed. It should be harmless for normal
8156 * raid */
8157 set_mask_bits(&mddev->flags, 0,
8158 BIT(MD_CHANGE_PENDING) | BIT(MD_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008159
NeilBrown23da4222014-12-15 12:57:01 +11008160 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008161 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8162 /* We completed so min/max setting can be forgotten if used. */
8163 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8164 mddev->resync_min = 0;
8165 mddev->resync_max = MaxSector;
8166 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8167 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008168 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008170 spin_unlock(&mddev->lock);
8171
Linus Torvalds1da177e2005-04-16 15:20:36 -07008172 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008174 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175}
NeilBrown29269552006-03-27 01:18:10 -08008176EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177
NeilBrown746d3202013-04-24 11:42:41 +10008178static int remove_and_add_spares(struct mddev *mddev,
8179 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008180{
NeilBrown3cb03002011-10-11 16:45:26 +11008181 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008182 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008183 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008184 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008185
NeilBrownd787be42016-06-02 16:19:53 +10008186 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008187 if ((this == NULL || rdev == this) &&
8188 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008189 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008190 test_bit(Faulty, &rdev->flags) &&
8191 atomic_read(&rdev->nr_pending)==0) {
8192 /* Faulty non-Blocked devices with nr_pending == 0
8193 * never get nr_pending incremented,
8194 * never get Faulty cleared, and never get Blocked set.
8195 * So we can synchronize_rcu now rather than once per device
8196 */
8197 remove_some = true;
8198 set_bit(RemoveSynchronized, &rdev->flags);
8199 }
8200 }
8201
8202 if (remove_some)
8203 synchronize_rcu();
8204 rdev_for_each(rdev, mddev) {
8205 if ((this == NULL || rdev == this) &&
8206 rdev->raid_disk >= 0 &&
8207 !test_bit(Blocked, &rdev->flags) &&
8208 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008209 (!test_bit(In_sync, &rdev->flags) &&
8210 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008211 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008212 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008213 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008214 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008215 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008216 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008217 }
8218 }
NeilBrownd787be42016-06-02 16:19:53 +10008219 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8220 clear_bit(RemoveSynchronized, &rdev->flags);
8221 }
8222
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008223 if (removed && mddev->kobj.sd)
8224 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008225
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008226 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008227 goto no_add;
8228
NeilBrowndafb20f2012-03-19 12:46:39 +11008229 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008230 if (this && this != rdev)
8231 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008232 if (test_bit(Candidate, &rdev->flags))
8233 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008234 if (rdev->raid_disk >= 0 &&
8235 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008236 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008237 !test_bit(Faulty, &rdev->flags))
8238 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008239 if (rdev->raid_disk >= 0)
8240 continue;
8241 if (test_bit(Faulty, &rdev->flags))
8242 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008243 if (!test_bit(Journal, &rdev->flags)) {
8244 if (mddev->ro &&
8245 ! (rdev->saved_raid_disk >= 0 &&
8246 !test_bit(Bitmap_sync, &rdev->flags)))
8247 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008248
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008249 rdev->recovery_offset = 0;
8250 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008251 if (mddev->pers->
8252 hot_add_disk(mddev, rdev) == 0) {
8253 if (sysfs_link_rdev(mddev, rdev))
8254 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008255 if (!test_bit(Journal, &rdev->flags))
8256 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008257 md_new_event(mddev);
8258 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008259 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008260 }
NeilBrown746d3202013-04-24 11:42:41 +10008261no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008262 if (removed)
8263 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008264 return spares;
8265}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008266
NeilBrownac05f252014-09-30 08:10:42 +10008267static void md_start_sync(struct work_struct *ws)
8268{
8269 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008270 int ret = 0;
8271
NeilBrownac05f252014-09-30 08:10:42 +10008272 mddev->sync_thread = md_register_thread(md_do_sync,
8273 mddev,
8274 "resync");
8275 if (!mddev->sync_thread) {
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008276 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8277 printk(KERN_ERR "%s: could not start resync"
8278 " thread...\n",
8279 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008280 /* leave the spares where they are, it shouldn't hurt */
8281 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8282 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8283 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8284 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8285 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008286 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008287 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8288 &mddev->recovery))
8289 if (mddev->sysfs_action)
8290 sysfs_notify_dirent_safe(mddev->sysfs_action);
8291 } else
8292 md_wakeup_thread(mddev->sync_thread);
8293 sysfs_notify_dirent_safe(mddev->sysfs_action);
8294 md_new_event(mddev);
8295}
8296
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297/*
8298 * This routine is regularly called by all per-raid-array threads to
8299 * deal with generic issues like resync and super-block update.
8300 * Raid personalities that don't have a thread (linear/raid0) do not
8301 * need this as they never do any recovery or update the superblock.
8302 *
8303 * It does not do any resync itself, but rather "forks" off other threads
8304 * to do that as needed.
8305 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8306 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008307 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308 * and wakeups up this thread which will reap the thread and finish up.
8309 * This thread also removes any faulty devices (with nr_pending == 0).
8310 *
8311 * The overall approach is:
8312 * 1/ if the superblock needs updating, update it.
8313 * 2/ If a recovery thread is running, don't do anything else.
8314 * 3/ If recovery has finished, clean up, possibly marking spares active.
8315 * 4/ If there are any faulty devices, remove them.
8316 * 5/ If array is degraded, try to add spares devices
8317 * 6/ If array has spares or is not in-sync, start a resync thread.
8318 */
NeilBrownfd01b882011-10-11 16:47:53 +11008319void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008321 if (mddev->suspended)
8322 return;
8323
NeilBrown5f404022005-06-21 17:17:16 -07008324 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008325 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326
NeilBrownfca4d842005-06-21 17:17:11 -07008327 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008328 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008329 printk(KERN_INFO "md: %s in immediate safe mode\n",
8330 mdname(mddev));
8331 mddev->safemode = 2;
8332 }
8333 flush_signals(current);
8334 }
8335
NeilBrownc89a8ee2008-08-05 15:54:13 +10008336 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8337 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008339 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008341 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008342 test_bit(MD_RELOAD_SB, &mddev->flags) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008343 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008344 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8345 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346 ))
8347 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008348
NeilBrowndf5b89b2006-03-27 01:18:20 -08008349 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008350 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008351
NeilBrownc89a8ee2008-08-05 15:54:13 +10008352 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008353 struct md_rdev *rdev;
8354 if (!mddev->external && mddev->in_sync)
8355 /* 'Blocked' flag not needed as failed devices
8356 * will be recorded if array switched to read/write.
8357 * Leaving it set will prevent the device
8358 * from being removed.
8359 */
8360 rdev_for_each(rdev, mddev)
8361 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008362 /* On a read-only array we can:
8363 * - remove failed devices
8364 * - add already-in_sync devices if the array itself
8365 * is in-sync.
8366 * As we only add devices that are already in-sync,
8367 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008368 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008369 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008370 /* There is no thread, but we need to call
8371 * ->spare_active and clear saved_raid_disk
8372 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008373 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008374 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008375 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008376 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008377 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008378 goto unlock;
8379 }
8380
Guoqing Jiang659b2542015-12-21 10:50:59 +11008381 if (mddev_is_clustered(mddev)) {
8382 struct md_rdev *rdev;
8383 /* kick the device if another node issued a
8384 * remove disk.
8385 */
8386 rdev_for_each(rdev, mddev) {
8387 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8388 rdev->raid_disk < 0)
8389 md_kick_rdev_from_array(rdev);
8390 }
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008391
8392 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8393 md_reload_sb(mddev, mddev->good_device_nr);
Guoqing Jiang659b2542015-12-21 10:50:59 +11008394 }
8395
NeilBrown31a59e32008-04-30 00:52:30 -07008396 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008397 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008398 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008399 if (mddev->safemode &&
8400 !atomic_read(&mddev->writes_pending) &&
8401 !mddev->in_sync &&
8402 mddev->recovery_cp == MaxSector) {
8403 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008404 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008405 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008406 }
8407 if (mddev->safemode == 1)
8408 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008409 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008410 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008411 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008412 }
NeilBrownfca4d842005-06-21 17:17:11 -07008413
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008414 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07008415 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008416
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8418 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8419 /* resync/recovery still happening */
8420 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8421 goto unlock;
8422 }
8423 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008424 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425 goto unlock;
8426 }
Neil Brown72a23c22008-06-28 08:31:41 +10008427 /* Set RUNNING before clearing NEEDED to avoid
8428 * any transients in the value of "sync_action".
8429 */
NeilBrown72f36d52012-10-11 14:25:57 +11008430 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008431 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008432 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008433 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008434 /* Clear some bits that don't mean anything, but
8435 * might be left set
8436 */
NeilBrown24dd4692005-11-08 21:39:26 -08008437 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8438 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439
NeilBrowned209582012-04-24 10:23:14 +10008440 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8441 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008442 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443 /* no recovery is running.
8444 * remove any failed drives, then
8445 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008446 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447 * the personality to fail the re-add.
8448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008450 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008451 if (mddev->pers->check_reshape == NULL ||
8452 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008453 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008454 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008455 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008456 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008457 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008458 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8459 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008460 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008461 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008462 } else if (mddev->recovery_cp < MaxSector) {
8463 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008464 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008465 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8466 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008467 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008468
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008470 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008471 /* We are adding a device or devices to an array
8472 * which has the bitmap stored on all devices.
8473 * So make sure all bitmap pages get written
8474 */
8475 bitmap_write_all(mddev->bitmap);
8476 }
NeilBrownac05f252014-09-30 08:10:42 +10008477 INIT_WORK(&mddev->del_work, md_start_sync);
8478 queue_work(md_misc_wq, &mddev->del_work);
8479 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480 }
NeilBrownac05f252014-09-30 08:10:42 +10008481 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008482 if (!mddev->sync_thread) {
8483 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008484 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008485 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8486 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008487 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008488 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008489 }
NeilBrownac05f252014-09-30 08:10:42 +10008490 unlock:
8491 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 mddev_unlock(mddev);
8493 }
8494}
NeilBrown6c144d32014-09-30 16:15:38 +10008495EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008497void md_reap_sync_thread(struct mddev *mddev)
8498{
8499 struct md_rdev *rdev;
8500
8501 /* resync has finished, collect result */
8502 md_unregister_thread(&mddev->sync_thread);
8503 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8504 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8505 /* success...*/
8506 /* activate any spares */
8507 if (mddev->pers->spare_active(mddev)) {
8508 sysfs_notify(&mddev->kobj, NULL,
8509 "degraded");
8510 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8511 }
8512 }
8513 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8514 mddev->pers->finish_reshape)
8515 mddev->pers->finish_reshape(mddev);
8516
8517 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008518 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008519 */
NeilBrownf4667222013-12-09 12:04:56 +11008520 if (!mddev->degraded)
8521 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008522 rdev->saved_raid_disk = -1;
8523
8524 md_update_sb(mddev, 1);
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008525 /* MD_CHANGE_PENDING should be cleared by md_update_sb, so we can
8526 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8527 * clustered raid */
8528 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8529 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008530 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008531 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008532 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8533 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8534 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8535 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008536 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008537 /* flag recovery needed just to double check */
8538 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8539 sysfs_notify_dirent_safe(mddev->sysfs_action);
8540 md_new_event(mddev);
8541 if (mddev->event_work.func)
8542 queue_work(md_misc_wq, &mddev->event_work);
8543}
NeilBrown6c144d32014-09-30 16:15:38 +10008544EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008545
NeilBrownfd01b882011-10-11 16:47:53 +11008546void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008547{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008548 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008549 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008550 !test_bit(Blocked, &rdev->flags) &&
8551 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008552 msecs_to_jiffies(5000));
8553 rdev_dec_pending(rdev, mddev);
8554}
8555EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8556
NeilBrownc6563a82012-05-21 09:27:00 +10008557void md_finish_reshape(struct mddev *mddev)
8558{
8559 /* called be personality module when reshape completes. */
8560 struct md_rdev *rdev;
8561
8562 rdev_for_each(rdev, mddev) {
8563 if (rdev->data_offset > rdev->new_data_offset)
8564 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8565 else
8566 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8567 rdev->data_offset = rdev->new_data_offset;
8568 }
8569}
8570EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008571
Vishal Vermafc974ee2015-12-24 19:20:34 -07008572/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008573
Vishal Vermafc974ee2015-12-24 19:20:34 -07008574/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008575int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008576 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008577{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008578 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008579 int rv;
8580 if (is_new)
8581 s += rdev->new_data_offset;
8582 else
8583 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008584 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8585 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008586 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008587 sysfs_notify_dirent_safe(rdev->sysfs_state);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008588 set_mask_bits(&mddev->flags, 0,
8589 BIT(MD_CHANGE_CLEAN) | BIT(MD_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008590 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008591 return 1;
8592 } else
8593 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008594}
8595EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8596
NeilBrownc6563a82012-05-21 09:27:00 +10008597int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8598 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008599{
NeilBrownc6563a82012-05-21 09:27:00 +10008600 if (is_new)
8601 s += rdev->new_data_offset;
8602 else
8603 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008604 return badblocks_clear(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008605 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008606}
8607EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8608
Adrian Bunk75c96f82005-05-05 16:16:09 -07008609static int md_notify_reboot(struct notifier_block *this,
8610 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008611{
8612 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008613 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008614 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615
NeilBrownc744a652012-03-19 12:46:37 +11008616 for_each_mddev(mddev, tmp) {
8617 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008618 if (mddev->pers)
8619 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008620 if (mddev->persistent)
8621 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008622 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008623 }
NeilBrownc744a652012-03-19 12:46:37 +11008624 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625 }
NeilBrownc744a652012-03-19 12:46:37 +11008626 /*
8627 * certain more exotic SCSI devices are known to be
8628 * volatile wrt too early system reboots. While the
8629 * right place to handle this issue is the given
8630 * driver, we do want to have a safe RAID driver ...
8631 */
8632 if (need_delay)
8633 mdelay(1000*1);
8634
Linus Torvalds1da177e2005-04-16 15:20:36 -07008635 return NOTIFY_DONE;
8636}
8637
Adrian Bunk75c96f82005-05-05 16:16:09 -07008638static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 .notifier_call = md_notify_reboot,
8640 .next = NULL,
8641 .priority = INT_MAX, /* before any real devices */
8642};
8643
8644static void md_geninit(void)
8645{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008646 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008648 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649}
8650
Adrian Bunk75c96f82005-05-05 16:16:09 -07008651static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008652{
Tejun Heoe804ac72010-10-15 15:36:08 +02008653 int ret = -ENOMEM;
8654
Tejun Heoada609e2011-01-25 14:35:54 +01008655 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008656 if (!md_wq)
8657 goto err_wq;
8658
8659 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8660 if (!md_misc_wq)
8661 goto err_misc_wq;
8662
8663 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8664 goto err_md;
8665
8666 if ((ret = register_blkdev(0, "mdp")) < 0)
8667 goto err_mdp;
8668 mdp_major = ret;
8669
NeilBrownaf5628f2014-07-31 13:54:54 +10008670 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008671 md_probe, NULL, NULL);
8672 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673 md_probe, NULL, NULL);
8674
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008676 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677
8678 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680
Tejun Heoe804ac72010-10-15 15:36:08 +02008681err_mdp:
8682 unregister_blkdev(MD_MAJOR, "md");
8683err_md:
8684 destroy_workqueue(md_misc_wq);
8685err_misc_wq:
8686 destroy_workqueue(md_wq);
8687err_wq:
8688 return ret;
8689}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008691static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008692{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008693 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8694 struct md_rdev *rdev2;
8695 int role, ret;
8696 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008697
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008698 /* Check for change of roles in the active devices */
8699 rdev_for_each(rdev2, mddev) {
8700 if (test_bit(Faulty, &rdev2->flags))
8701 continue;
8702
8703 /* Check if the roles changed */
8704 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008705
8706 if (test_bit(Candidate, &rdev2->flags)) {
8707 if (role == 0xfffe) {
8708 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8709 md_kick_rdev_from_array(rdev2);
8710 continue;
8711 }
8712 else
8713 clear_bit(Candidate, &rdev2->flags);
8714 }
8715
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008716 if (role != rdev2->raid_disk) {
8717 /* got activated */
8718 if (rdev2->raid_disk == -1 && role != 0xffff) {
8719 rdev2->saved_raid_disk = role;
8720 ret = remove_and_add_spares(mddev, rdev2);
8721 pr_info("Activated spare: %s\n",
8722 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04008723 /* wakeup mddev->thread here, so array could
8724 * perform resync with the new activated disk */
8725 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8726 md_wakeup_thread(mddev->thread);
8727
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008728 }
8729 /* device faulty
8730 * We just want to do the minimum to mark the disk
8731 * as faulty. The recovery is performed by the
8732 * one who initiated the error.
8733 */
8734 if ((role == 0xfffe) || (role == 0xfffd)) {
8735 md_error(mddev, rdev2);
8736 clear_bit(Blocked, &rdev2->flags);
8737 }
8738 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008739 }
8740
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11008741 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8742 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008743
8744 /* Finally set the event to be up to date */
8745 mddev->events = le64_to_cpu(sb->events);
8746}
8747
8748static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8749{
8750 int err;
8751 struct page *swapout = rdev->sb_page;
8752 struct mdp_superblock_1 *sb;
8753
8754 /* Store the sb page of the rdev in the swapout temporary
8755 * variable in case we err in the future
8756 */
8757 rdev->sb_page = NULL;
8758 alloc_disk_sb(rdev);
8759 ClearPageUptodate(rdev->sb_page);
8760 rdev->sb_loaded = 0;
8761 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
8762
8763 if (err < 0) {
8764 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8765 __func__, __LINE__, rdev->desc_nr, err);
8766 put_page(rdev->sb_page);
8767 rdev->sb_page = swapout;
8768 rdev->sb_loaded = 1;
8769 return err;
8770 }
8771
8772 sb = page_address(rdev->sb_page);
8773 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8774 * is not set
8775 */
8776
8777 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8778 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8779
8780 /* The other node finished recovery, call spare_active to set
8781 * device In_sync and mddev->degraded
8782 */
8783 if (rdev->recovery_offset == MaxSector &&
8784 !test_bit(In_sync, &rdev->flags) &&
8785 mddev->pers->spare_active(mddev))
8786 sysfs_notify(&mddev->kobj, NULL, "degraded");
8787
8788 put_page(swapout);
8789 return 0;
8790}
8791
8792void md_reload_sb(struct mddev *mddev, int nr)
8793{
8794 struct md_rdev *rdev;
8795 int err;
8796
8797 /* Find the rdev */
8798 rdev_for_each_rcu(rdev, mddev) {
8799 if (rdev->desc_nr == nr)
8800 break;
8801 }
8802
8803 if (!rdev || rdev->desc_nr != nr) {
8804 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
8805 return;
8806 }
8807
8808 err = read_rdev(mddev, rdev);
8809 if (err < 0)
8810 return;
8811
8812 check_sb_changes(mddev, rdev);
8813
8814 /* Read all rdev's to update recovery_offset */
8815 rdev_for_each_rcu(rdev, mddev)
8816 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008817}
8818EXPORT_SYMBOL(md_reload_sb);
8819
Linus Torvalds1da177e2005-04-16 15:20:36 -07008820#ifndef MODULE
8821
8822/*
8823 * Searches all registered partitions for autorun RAID arrays
8824 * at boot time.
8825 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008826
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008827static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008828static LIST_HEAD(all_detected_devices);
8829struct detected_devices_node {
8830 struct list_head list;
8831 dev_t dev;
8832};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833
8834void md_autodetect_dev(dev_t dev)
8835{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008836 struct detected_devices_node *node_detected_dev;
8837
8838 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8839 if (node_detected_dev) {
8840 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008841 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008842 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008843 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008844 } else {
8845 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8846 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008848}
8849
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850static void autostart_arrays(int part)
8851{
NeilBrown3cb03002011-10-11 16:45:26 +11008852 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008853 struct detected_devices_node *node_detected_dev;
8854 dev_t dev;
8855 int i_scanned, i_passed;
8856
8857 i_scanned = 0;
8858 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859
8860 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8861
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008862 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008863 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8864 i_scanned++;
8865 node_detected_dev = list_entry(all_detected_devices.next,
8866 struct detected_devices_node, list);
8867 list_del(&node_detected_dev->list);
8868 dev = node_detected_dev->dev;
8869 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008870 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008871 if (IS_ERR(rdev))
8872 continue;
8873
NeilBrown403df472014-09-30 15:52:29 +10008874 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008875 continue;
NeilBrown403df472014-09-30 15:52:29 +10008876
NeilBrownd0fae182008-03-04 14:29:31 -08008877 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008878 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008879 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008881 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008882
8883 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8884 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885
8886 autorun_devices(part);
8887}
8888
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008889#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890
8891static __exit void md_exit(void)
8892{
NeilBrownfd01b882011-10-11 16:47:53 +11008893 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008895 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008896
NeilBrownaf5628f2014-07-31 13:54:54 +10008897 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008898 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008900 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901 unregister_blkdev(mdp_major, "mdp");
8902 unregister_reboot_notifier(&md_notifier);
8903 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008904
8905 /* We cannot unload the modules while some process is
8906 * waiting for us in select() or poll() - wake them up
8907 */
8908 md_unloading = 1;
8909 while (waitqueue_active(&md_event_waiters)) {
8910 /* not safe to leave yet */
8911 wake_up(&md_event_waiters);
8912 msleep(delay);
8913 delay += delay;
8914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008916
NeilBrown29ac4aa2008-02-06 01:39:58 -08008917 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008919 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008920 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008921 destroy_workqueue(md_misc_wq);
8922 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923}
8924
Dan Williams685784a2007-07-09 11:56:42 -07008925subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926module_exit(md_exit)
8927
NeilBrownf91de922005-11-08 21:39:36 -08008928static int get_ro(char *buffer, struct kernel_param *kp)
8929{
8930 return sprintf(buffer, "%d", start_readonly);
8931}
8932static int set_ro(const char *val, struct kernel_param *kp)
8933{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03008934 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08008935}
8936
NeilBrown80ca3a42006-07-10 04:44:18 -07008937module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8938module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008939module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008940
Linus Torvalds1da177e2005-04-16 15:20:36 -07008941MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008942MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008943MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008944MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);