blob: 457b538631179f4b568af510d4aaa3526fa37472 [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 */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600288 bio->bi_opf &= ~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;
Mike Christie796a5cf2016-06-05 14:32:07 -0500397 bio_set_op_attrs(bi, REQ_OP_WRITE, WRITE_FLUSH);
NeilBrowna2826aa2009-12-14 12:49:49 +1100398 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500399 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100400 rcu_read_lock();
401 rdev_dec_pending(rdev, mddev);
402 }
403 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100404 if (atomic_dec_and_test(&mddev->flush_pending))
405 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100406}
407
Tejun Heoe9c74692010-09-03 11:56:18 +0200408static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409{
NeilBrownfd01b882011-10-11 16:47:53 +1100410 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200411 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100412
Kent Overstreet4f024f32013-10-11 15:44:27 -0700413 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100414 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200415 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100416 else {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600417 bio->bi_opf &= ~REQ_PREFLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200418 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100419 }
NeilBrown2b74e122010-12-09 15:59:01 +1100420
421 mddev->flush_bio = NULL;
422 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100423}
424
NeilBrownfd01b882011-10-11 16:47:53 +1100425void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100426{
NeilBrown85572d72014-12-15 12:56:56 +1100427 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100428 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200429 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100430 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200431 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100432 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100433
NeilBrowna035fc32010-12-09 16:17:51 +1100434 INIT_WORK(&mddev->flush_work, submit_flushes);
435 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100436}
Tejun Heoe9c74692010-09-03 11:56:18 +0200437EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100438
NeilBrown74018dc2012-07-31 09:08:15 +0200439void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000440{
NeilBrown9cbb1752012-07-31 09:08:14 +0200441 struct mddev *mddev = cb->data;
442 md_wakeup_thread(mddev->thread);
443 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000444}
NeilBrown9cbb1752012-07-31 09:08:14 +0200445EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000446
NeilBrownfd01b882011-10-11 16:47:53 +1100447static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 atomic_inc(&mddev->active);
450 return mddev;
451}
452
Dan Williams5fd3a172009-03-04 00:57:25 -0700453static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100454
NeilBrownfd01b882011-10-11 16:47:53 +1100455static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
NeilBrowna167f662010-10-26 18:31:13 +1100457 struct bio_set *bs = NULL;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
460 return;
NeilBrownd3374822009-01-09 08:31:10 +1100461 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100462 mddev->ctime == 0 && !mddev->hold_active) {
463 /* Array is not configured at all, and not held active,
464 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100465 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100466 bs = mddev->bio_set;
467 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100468 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200469 /* We did a probe so need to clean up. Call
470 * queue_work inside the spinlock so that
471 * flush_workqueue() after mddev_find will
472 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100473 */
474 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200475 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100476 } else
477 kfree(mddev);
478 }
479 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100480 if (bs)
481 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Sasha Levin25b2edf2015-07-24 18:19:58 -0400484static void md_safemode_timeout(unsigned long data);
485
NeilBrownfd01b882011-10-11 16:47:53 +1100486void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100487{
488 mutex_init(&mddev->open_mutex);
489 mutex_init(&mddev->reconfig_mutex);
490 mutex_init(&mddev->bitmap_info.mutex);
491 INIT_LIST_HEAD(&mddev->disks);
492 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400493 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
494 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100495 atomic_set(&mddev->active, 1);
496 atomic_set(&mddev->openers, 0);
497 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100498 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100499 atomic_set(&mddev->flush_pending, 0);
500 init_waitqueue_head(&mddev->sb_wait);
501 init_waitqueue_head(&mddev->recovery_wait);
502 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000503 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500504 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100505 mddev->resync_min = 0;
506 mddev->resync_max = MaxSector;
507 mddev->level = LEVEL_NONE;
508}
NeilBrown390ee602010-06-01 19:37:27 +1000509EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100510
NeilBrownf72ffdd2014-09-30 14:23:59 +1000511static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
NeilBrownfd01b882011-10-11 16:47:53 +1100513 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
NeilBrown8f5f02c2011-02-16 13:58:51 +1100515 if (unit && MAJOR(unit) != MD_MAJOR)
516 unit &= ~((1<<MdpMinorShift)-1);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 retry:
519 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
NeilBrownefeb53c2009-01-09 08:31:10 +1100521 if (unit) {
522 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
523 if (mddev->unit == unit) {
524 mddev_get(mddev);
525 spin_unlock(&all_mddevs_lock);
526 kfree(new);
527 return mddev;
528 }
529
530 if (new) {
531 list_add(&new->all_mddevs, &all_mddevs);
532 spin_unlock(&all_mddevs_lock);
533 new->hold_active = UNTIL_IOCTL;
534 return new;
535 }
536 } else if (new) {
537 /* find an unused unit number */
538 static int next_minor = 512;
539 int start = next_minor;
540 int is_free = 0;
541 int dev = 0;
542 while (!is_free) {
543 dev = MKDEV(MD_MAJOR, next_minor);
544 next_minor++;
545 if (next_minor > MINORMASK)
546 next_minor = 0;
547 if (next_minor == start) {
548 /* Oh dear, all in use. */
549 spin_unlock(&all_mddevs_lock);
550 kfree(new);
551 return NULL;
552 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000553
NeilBrownefeb53c2009-01-09 08:31:10 +1100554 is_free = 1;
555 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
556 if (mddev->unit == dev) {
557 is_free = 0;
558 break;
559 }
560 }
561 new->unit = dev;
562 new->md_minor = MINOR(dev);
563 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 list_add(&new->all_mddevs, &all_mddevs);
565 spin_unlock(&all_mddevs_lock);
566 return new;
567 }
568 spin_unlock(&all_mddevs_lock);
569
NeilBrown9ffae0c2006-01-06 00:20:32 -0800570 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!new)
572 return NULL;
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 new->unit = unit;
575 if (MAJOR(unit) == MD_MAJOR)
576 new->md_minor = MINOR(unit);
577 else
578 new->md_minor = MINOR(unit) >> MdpMinorShift;
579
NeilBrownfafd7fb2010-04-01 15:55:30 +1100580 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto retry;
583}
584
NeilBrownb6eb1272010-04-15 10:13:47 +1000585static struct attribute_group md_redundancy_group;
586
NeilBrown5c47daf2014-12-15 12:57:01 +1100587void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
NeilBrowna64c8762010-04-14 17:15:37 +1000589 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000590 /* These cannot be removed under reconfig_mutex as
591 * an access to the files will try to take reconfig_mutex
592 * while holding the file unremovable, which leads to
593 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000594 * So hold set sysfs_active while the remove in happeing,
595 * and anything else which might set ->to_remove or my
596 * otherwise change the sysfs namespace will fail with
597 * -EBUSY if sysfs_active is still set.
598 * We set sysfs_active under reconfig_mutex and elsewhere
599 * test it under the same mutex to ensure its correct value
600 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000601 */
NeilBrowna64c8762010-04-14 17:15:37 +1000602 struct attribute_group *to_remove = mddev->to_remove;
603 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000604 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000605 mutex_unlock(&mddev->reconfig_mutex);
606
NeilBrown00bcb4a2010-06-01 19:37:23 +1000607 if (mddev->kobj.sd) {
608 if (to_remove != &md_redundancy_group)
609 sysfs_remove_group(&mddev->kobj, to_remove);
610 if (mddev->pers == NULL ||
611 mddev->pers->sync_request == NULL) {
612 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
613 if (mddev->sysfs_action)
614 sysfs_put(mddev->sysfs_action);
615 mddev->sysfs_action = NULL;
616 }
NeilBrowna64c8762010-04-14 17:15:37 +1000617 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000618 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000619 } else
620 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Chris Dunlop751e67c2011-10-19 16:48:26 +1100622 /* As we've dropped the mutex we need a spinlock to
623 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000624 */
625 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700626 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000627 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
NeilBrown5c47daf2014-12-15 12:57:01 +1100629EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500631struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100632{
633 struct md_rdev *rdev;
634
635 rdev_for_each_rcu(rdev, mddev)
636 if (rdev->desc_nr == nr)
637 return rdev;
638
639 return NULL;
640}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500641EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100642
643static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
NeilBrown3cb03002011-10-11 16:45:26 +1100645 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
NeilBrowndafb20f2012-03-19 12:46:39 +1100647 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (rdev->bdev->bd_dev == dev)
649 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return NULL;
652}
653
NeilBrown1ca69c42012-10-11 13:37:33 +1100654static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
655{
656 struct md_rdev *rdev;
657
658 rdev_for_each_rcu(rdev, mddev)
659 if (rdev->bdev->bd_dev == dev)
660 return rdev;
661
662 return NULL;
663}
664
NeilBrown84fc4b52011-10-11 16:49:58 +1100665static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800666{
NeilBrown84fc4b52011-10-11 16:49:58 +1100667 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800668 list_for_each_entry(pers, &pers_list, list) {
669 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800670 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800671 if (strcmp(pers->name, clevel)==0)
672 return pers;
673 }
NeilBrown2604b702006-01-06 00:20:36 -0800674 return NULL;
675}
676
Andre Nollb73df2d2008-07-11 22:02:23 +1000677/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100678static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100680 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000681 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
NeilBrownf72ffdd2014-09-30 14:23:59 +1000684static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 rdev->sb_page = alloc_page(GFP_KERNEL);
687 if (!rdev->sb_page) {
688 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000689 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
692 return 0;
693}
694
NeilBrown545c8792012-05-22 13:54:30 +1000695void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800698 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 rdev->sb_loaded = 0;
700 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000701 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100702 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
NeilBrown2699b672011-07-28 11:31:47 +1000704 if (rdev->bb_page) {
705 put_page(rdev->bb_page);
706 rdev->bb_page = NULL;
707 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800708 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
NeilBrown545c8792012-05-22 13:54:30 +1000710EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200712static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700713{
NeilBrown3cb03002011-10-11 16:45:26 +1100714 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100715 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700716
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200717 if (bio->bi_error) {
718 printk("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800719 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700720 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700721
NeilBrowna9701a32005-11-08 21:39:34 -0800722 if (atomic_dec_and_test(&mddev->pending_writes))
723 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700724 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700725 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700726}
727
NeilBrownfd01b882011-10-11 16:47:53 +1100728void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700729 sector_t sector, int size, struct page *page)
730{
731 /* write first size bytes of page to sector of rdev
732 * Increment mddev->pending_writes before returning
733 * and decrement it on completion, waking up sb_wait
734 * if zero is reached.
735 * If an error occurred, call md_error
736 */
NeilBrowna167f662010-10-26 18:31:13 +1100737 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700738
Shaohua Lied3b98c2016-03-29 14:00:19 -0700739 atomic_inc(&rdev->nr_pending);
740
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100741 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700742 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743 bio_add_page(bio, page, size, 0);
744 bio->bi_private = rdev;
745 bio->bi_end_io = super_written;
Mike Christie796a5cf2016-06-05 14:32:07 -0500746 bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH_FUA);
NeilBrowna9701a32005-11-08 21:39:34 -0800747
NeilBrown7bfa19f2005-06-21 17:17:28 -0700748 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500749 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800750}
751
NeilBrownfd01b882011-10-11 16:47:53 +1100752void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800753{
Tejun Heoe9c74692010-09-03 11:56:18 +0200754 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000755 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700756}
757
NeilBrown3cb03002011-10-11 16:45:26 +1100758int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500759 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
NeilBrowna167f662010-10-26 18:31:13 +1100761 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 int ret;
763
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100764 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
765 rdev->meta_bdev : rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -0500766 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100767 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700768 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000769 else if (rdev->mddev->reshape_position != MaxSector &&
770 (rdev->mddev->reshape_backwards ==
771 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700772 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100773 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700774 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500776
777 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200779 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 bio_put(bio);
781 return ret;
782}
NeilBrowna8745db2006-01-06 00:20:34 -0800783EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
NeilBrownf72ffdd2014-09-30 14:23:59 +1000785static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (rdev->sb_loaded)
790 return 0;
791
Mike Christie796a5cf2016-06-05 14:32:07 -0500792 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto fail;
794 rdev->sb_loaded = 1;
795 return 0;
796
797fail:
798 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
799 bdevname(rdev->bdev,b));
800 return -EINVAL;
801}
802
803static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
804{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000805 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000806 sb1->set_uuid1 == sb2->set_uuid1 &&
807 sb1->set_uuid2 == sb2->set_uuid2 &&
808 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
812{
813 int ret;
814 mdp_super_t *tmp1, *tmp2;
815
816 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
817 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
818
819 if (!tmp1 || !tmp2) {
820 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100821 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 goto abort;
823 }
824
825 *tmp1 = *sb1;
826 *tmp2 = *sb2;
827
828 /*
829 * nr_disks is not constant
830 */
831 tmp1->nr_disks = 0;
832 tmp2->nr_disks = 0;
833
Andre Nollce0c8e02008-07-11 22:02:20 +1000834 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700836 kfree(tmp1);
837 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return ret;
839}
840
NeilBrown4d167f02007-05-09 02:35:37 -0700841static u32 md_csum_fold(u32 csum)
842{
843 csum = (csum & 0xffff) + (csum >> 16);
844 return (csum & 0xffff) + (csum >> 16);
845}
846
NeilBrownf72ffdd2014-09-30 14:23:59 +1000847static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
NeilBrown4d167f02007-05-09 02:35:37 -0700849 u64 newcsum = 0;
850 u32 *sb32 = (u32*)sb;
851 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned int disk_csum, csum;
853
854 disk_csum = sb->sb_csum;
855 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700856
857 for (i = 0; i < MD_SB_BYTES/4 ; i++)
858 newcsum += sb32[i];
859 csum = (newcsum & 0xffffffff) + (newcsum>>32);
860
NeilBrown4d167f02007-05-09 02:35:37 -0700861#ifdef CONFIG_ALPHA
862 /* This used to use csum_partial, which was wrong for several
863 * reasons including that different results are returned on
864 * different architectures. It isn't critical that we get exactly
865 * the same return value as before (we always csum_fold before
866 * testing, and that removes any differences). However as we
867 * know that csum_partial always returned a 16bit value on
868 * alphas, do a fold to maximise conformity to previous behaviour.
869 */
870 sb->sb_csum = md_csum_fold(disk_csum);
871#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700873#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return csum;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * Handle superblock details.
879 * We want to be able to handle multiple superblock formats
880 * so we have a common interface to them all, and an array of
881 * different handlers.
882 * We rely on user-space to write the initial superblock, and support
883 * reading and updating of superblocks.
884 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100885 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * loads and validates a superblock on dev.
887 * if refdev != NULL, compare superblocks on both devices
888 * Return:
889 * 0 - dev has a superblock that is compatible with refdev
890 * 1 - dev has a superblock that is compatible and newer than refdev
891 * so dev should be used as the refdev in future
892 * -EINVAL superblock incompatible or invalid
893 * -othererror e.g. -EIO
894 *
NeilBrownfd01b882011-10-11 16:47:53 +1100895 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * Verify that dev is acceptable into mddev.
897 * The first time, mddev->raid_disks will be 0, and data from
898 * dev should be merged in. Subsequent calls check that dev
899 * is new enough. Return 0 or -EINVAL
900 *
NeilBrownfd01b882011-10-11 16:47:53 +1100901 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 * Update the superblock for rdev with data in mddev
903 * This does not write to disc.
904 *
905 */
906
907struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000908 char *name;
909 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000910 int (*load_super)(struct md_rdev *rdev,
911 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000912 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000913 int (*validate_super)(struct mddev *mddev,
914 struct md_rdev *rdev);
915 void (*sync_super)(struct mddev *mddev,
916 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100917 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000918 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000919 int (*allow_new_offset)(struct md_rdev *rdev,
920 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921};
922
923/*
Andre Noll0894cc32009-06-18 08:49:23 +1000924 * Check that the given mddev has no bitmap.
925 *
926 * This function is called from the run method of all personalities that do not
927 * support bitmaps. It prints an error message and returns non-zero if mddev
928 * has a bitmap. Otherwise, it returns 0.
929 *
930 */
NeilBrownfd01b882011-10-11 16:47:53 +1100931int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000932{
NeilBrownc3d97142009-12-14 12:49:52 +1100933 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000934 return 0;
935 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
936 mdname(mddev), mddev->pers->name);
937 return 1;
938}
939EXPORT_SYMBOL(md_check_no_bitmap);
940
941/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000942 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
NeilBrown3cb03002011-10-11 16:45:26 +1100944static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
947 mdp_super_t *sb;
948 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /*
Andre Noll0f420352008-07-11 22:02:23 +1000951 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * it's at the end of the disk.
953 *
954 * It also happens to be a multiple of 4Kb.
955 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100956 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
NeilBrown0002b272005-09-09 16:23:53 -0700958 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (ret) return ret;
960
961 ret = -EINVAL;
962
963 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000964 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (sb->md_magic != MD_SB_MAGIC) {
967 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
968 b);
969 goto abort;
970 }
971
972 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800973 sb->minor_version < 90 ||
974 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
976 sb->major_version, sb->minor_version,
977 b);
978 goto abort;
979 }
980
981 if (sb->raid_disks <= 0)
982 goto abort;
983
NeilBrown4d167f02007-05-09 02:35:37 -0700984 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
986 b);
987 goto abort;
988 }
989
990 rdev->preferred_minor = sb->md_minor;
991 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000992 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700993 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000994 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (sb->level == LEVEL_MULTIPATH)
997 rdev->desc_nr = -1;
998 else
999 rdev->desc_nr = sb->this_disk.number;
1000
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001001 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001003 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001005 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (!uuid_equal(refsb, sb)) {
1007 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1008 b, bdevname(refdev->bdev,b2));
1009 goto abort;
1010 }
1011 if (!sb_equal(refsb, sb)) {
1012 printk(KERN_WARNING "md: %s has same UUID"
1013 " but different superblock to %s\n",
1014 b, bdevname(refdev->bdev, b2));
1015 goto abort;
1016 }
1017 ev1 = md_event(sb);
1018 ev2 = md_event(refsb);
1019 if (ev1 > ev2)
1020 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001021 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ret = 0;
1023 }
NeilBrown8190e752009-06-18 08:48:58 +10001024 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001025 /* Limit to 4TB as metadata cannot record more than that.
1026 * (not needed for Linear and RAID0 as metadata doesn't
1027 * record this size)
1028 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001029 if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1030 sb->level >= 1)
1031 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
NeilBrown27a7b262011-09-10 17:21:28 +10001033 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001034 /* "this cannot possibly happen" ... */
1035 ret = -EINVAL;
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 abort:
1038 return ret;
1039}
1040
1041/*
1042 * validate_super for 0.90.0
1043 */
NeilBrownfd01b882011-10-11 16:47:53 +11001044static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001047 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001048 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
NeilBrown41158c72005-06-21 17:17:25 -07001050 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001051 clear_bit(Faulty, &rdev->flags);
1052 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001053 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001054 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (mddev->raid_disks == 0) {
1057 mddev->major_version = 0;
1058 mddev->minor_version = sb->minor_version;
1059 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001060 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001061 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 mddev->ctime = sb->ctime;
1063 mddev->utime = sb->utime;
1064 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001065 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 mddev->layout = sb->layout;
1067 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001068 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001069 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001070 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001071 mddev->bitmap_info.space = 0;
1072 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001073 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001074 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001075 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
NeilBrownf6705572006-03-27 01:18:11 -08001077 if (mddev->minor_version >= 91) {
1078 mddev->reshape_position = sb->reshape_position;
1079 mddev->delta_disks = sb->delta_disks;
1080 mddev->new_level = sb->new_level;
1081 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001082 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001083 if (mddev->delta_disks < 0)
1084 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001085 } else {
1086 mddev->reshape_position = MaxSector;
1087 mddev->delta_disks = 0;
1088 mddev->new_level = mddev->level;
1089 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001090 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001091 }
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (sb->state & (1<<MD_SB_CLEAN))
1094 mddev->recovery_cp = MaxSector;
1095 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001096 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 sb->events_lo == sb->cp_events_lo) {
1098 mddev->recovery_cp = sb->recovery_cp;
1099 } else
1100 mddev->recovery_cp = 0;
1101 }
1102
1103 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1104 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1105 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1106 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1107
1108 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001109
1110 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001111 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001112 mddev->bitmap_info.offset =
1113 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001114 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001115 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001116 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001117
NeilBrown41158c72005-06-21 17:17:25 -07001118 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001119 /* Insist on good event counter while assembling, except
1120 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001122 if (sb->disks[rdev->desc_nr].state & (
1123 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001124 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001125 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001126 } else if (mddev->bitmap) {
1127 /* if adding to array with a bitmap, then we can accept an
1128 * older device ... but not too old.
1129 */
NeilBrown41158c72005-06-21 17:17:25 -07001130 if (ev1 < mddev->bitmap->events_cleared)
1131 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001132 if (ev1 < mddev->events)
1133 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001134 } else {
1135 if (ev1 < mddev->events)
1136 /* just a hot-add of a new device, leave raid_disk at -1 */
1137 return 0;
1138 }
NeilBrown41158c72005-06-21 17:17:25 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 desc = sb->disks + rdev->desc_nr;
1142
1143 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001144 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001145 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1146 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001147 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001149 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001150 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1151 /* active but not in sync implies recovery up to
1152 * reshape position. We don't know exactly where
1153 * that is, so set to zero for now */
1154 if (mddev->minor_version >= 91) {
1155 rdev->recovery_offset = 0;
1156 rdev->raid_disk = desc->raid_disk;
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001159 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1160 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001161 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001162 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return 0;
1164}
1165
1166/*
1167 * sync_super for 0.90.0
1168 */
NeilBrownfd01b882011-10-11 16:47:53 +11001169static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001172 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /* make rdev->sb match mddev data..
1176 *
1177 * 1/ zero out disks
1178 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1179 * 3/ any empty disks < next_spare become removed
1180 *
1181 * disks[0] gets initialised to REMOVED because
1182 * we cannot be sure from other fields if it has
1183 * been initialised or not.
1184 */
1185 int i;
1186 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1187
NeilBrown61181562005-09-09 16:24:02 -07001188 rdev->sb_size = MD_SB_BYTES;
1189
Namhyung Kim65a06f062011-07-27 11:00:36 +10001190 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 memset(sb, 0, sizeof(*sb));
1193
1194 sb->md_magic = MD_SB_MAGIC;
1195 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 sb->patch_version = mddev->patch_version;
1197 sb->gvalid_words = 0; /* ignored */
1198 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1199 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1200 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1201 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1202
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001203 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001205 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 sb->raid_disks = mddev->raid_disks;
1207 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001208 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001209 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 sb->state = 0;
1211 sb->events_hi = (mddev->events>>32);
1212 sb->events_lo = (u32)mddev->events;
1213
NeilBrownf6705572006-03-27 01:18:11 -08001214 if (mddev->reshape_position == MaxSector)
1215 sb->minor_version = 90;
1216 else {
1217 sb->minor_version = 91;
1218 sb->reshape_position = mddev->reshape_position;
1219 sb->new_level = mddev->new_level;
1220 sb->delta_disks = mddev->delta_disks;
1221 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001222 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001223 }
1224 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (mddev->in_sync)
1226 {
1227 sb->recovery_cp = mddev->recovery_cp;
1228 sb->cp_events_hi = (mddev->events>>32);
1229 sb->cp_events_lo = (u32)mddev->events;
1230 if (mddev->recovery_cp == MaxSector)
1231 sb->state = (1<< MD_SB_CLEAN);
1232 } else
1233 sb->recovery_cp = 0;
1234
1235 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001236 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
NeilBrownc3d97142009-12-14 12:49:52 +11001238 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001239 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001242 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001244 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001245 int is_active = test_bit(In_sync, &rdev2->flags);
1246
1247 if (rdev2->raid_disk >= 0 &&
1248 sb->minor_version >= 91)
1249 /* we have nowhere to store the recovery_offset,
1250 * but if it is not below the reshape_position,
1251 * we can piggy-back on that.
1252 */
1253 is_active = 1;
1254 if (rdev2->raid_disk < 0 ||
1255 test_bit(Faulty, &rdev2->flags))
1256 is_active = 0;
1257 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001258 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001260 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001261 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 d = &sb->disks[rdev2->desc_nr];
1263 nr_disks++;
1264 d->number = rdev2->desc_nr;
1265 d->major = MAJOR(rdev2->bdev->bd_dev);
1266 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001267 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 d->raid_disk = rdev2->raid_disk;
1269 else
1270 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001271 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001273 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001275 if (test_bit(In_sync, &rdev2->flags))
1276 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 active++;
1278 working++;
1279 } else {
1280 d->state = 0;
1281 spare++;
1282 working++;
1283 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001284 if (test_bit(WriteMostly, &rdev2->flags))
1285 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /* now set the "removed" and "faulty" bits on any missing devices */
1288 for (i=0 ; i < mddev->raid_disks ; i++) {
1289 mdp_disk_t *d = &sb->disks[i];
1290 if (d->state == 0 && d->number == 0) {
1291 d->number = i;
1292 d->raid_disk = i;
1293 d->state = (1<<MD_DISK_REMOVED);
1294 d->state |= (1<<MD_DISK_FAULTY);
1295 failed++;
1296 }
1297 }
1298 sb->nr_disks = nr_disks;
1299 sb->active_disks = active;
1300 sb->working_disks = working;
1301 sb->failed_disks = failed;
1302 sb->spare_disks = spare;
1303
1304 sb->this_disk = sb->disks[rdev->desc_nr];
1305 sb->sb_csum = calc_sb_csum(sb);
1306}
1307
1308/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001309 * rdev_size_change for 0.90.0
1310 */
1311static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001312super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001313{
Andre Noll58c0fed2009-03-31 14:33:13 +11001314 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001315 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001316 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001317 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001318 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001319 if (!num_sectors || num_sectors > rdev->sb_start)
1320 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001321 /* Limit to 4TB as metadata cannot record more than that.
1322 * 4TB == 2^32 KB, or 2*2^32 sectors.
1323 */
Arnd Bergmann3312c952015-12-21 10:51:01 +11001324 if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1325 rdev->mddev->level >= 1)
1326 num_sectors = (sector_t)(2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001327 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001328 rdev->sb_page);
1329 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001330 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001331}
1332
NeilBrownc6563a82012-05-21 09:27:00 +10001333static int
1334super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1335{
1336 /* non-zero offset changes not possible with v0.90 */
1337 return new_offset == 0;
1338}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001339
1340/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 * version 1 superblock
1342 */
1343
NeilBrownf72ffdd2014-09-30 14:23:59 +10001344static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001346 __le32 disk_csum;
1347 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 unsigned long long newcsum;
1349 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001350 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 disk_csum = sb->sb_csum;
1353 sb->sb_csum = 0;
1354 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001355 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 newcsum += le32_to_cpu(*isuper++);
1357
1358 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001359 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1362 sb->sb_csum = disk_csum;
1363 return cpu_to_le32(csum);
1364}
1365
NeilBrown3cb03002011-10-11 16:45:26 +11001366static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 struct mdp_superblock_1 *sb;
1369 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001370 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001371 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001373 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /*
Andre Noll0f420352008-07-11 22:02:23 +10001376 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 * It is always aligned to a 4K boundary and
1378 * depeding on minor_version, it can be:
1379 * 0: At least 8K, but less than 12K, from end of device
1380 * 1: At start of device
1381 * 2: 4K from start of device.
1382 */
1383 switch(minor_version) {
1384 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001385 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001386 sb_start -= 8*2;
1387 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001390 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 break;
1392 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001393 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 break;
1395 default:
1396 return -EINVAL;
1397 }
Andre Noll0f420352008-07-11 22:02:23 +10001398 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
NeilBrown0002b272005-09-09 16:23:53 -07001400 /* superblock is rarely larger than 1K, but it can be larger,
1401 * and it is safe to read 4k, so we do that
1402 */
1403 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (ret) return ret;
1405
Namhyung Kim65a06f062011-07-27 11:00:36 +10001406 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1409 sb->major_version != cpu_to_le32(1) ||
1410 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001411 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001412 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return -EINVAL;
1414
1415 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1416 printk("md: invalid superblock checksum on %s\n",
1417 bdevname(rdev->bdev,b));
1418 return -EINVAL;
1419 }
1420 if (le64_to_cpu(sb->data_size) < 10) {
1421 printk("md: data_size too small on %s\n",
1422 bdevname(rdev->bdev,b));
1423 return -EINVAL;
1424 }
NeilBrownc6563a82012-05-21 09:27:00 +10001425 if (sb->pad0 ||
1426 sb->pad3[0] ||
1427 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1428 /* Some padding is non-zero, might be a new feature */
1429 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 rdev->preferred_minor = 0xffff;
1432 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001433 rdev->new_data_offset = rdev->data_offset;
1434 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1435 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1436 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001437 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
NeilBrown0002b272005-09-09 16:23:53 -07001439 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001440 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001441 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001442 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1443
1444 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001445 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001446 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001447 if (minor_version
1448 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1449 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001450
NeilBrown31b65a02006-07-10 04:44:14 -07001451 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1452 rdev->desc_nr = -1;
1453 else
1454 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1455
NeilBrown2699b672011-07-28 11:31:47 +10001456 if (!rdev->bb_page) {
1457 rdev->bb_page = alloc_page(GFP_KERNEL);
1458 if (!rdev->bb_page)
1459 return -ENOMEM;
1460 }
1461 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1462 rdev->badblocks.count == 0) {
1463 /* need to load the bad block list.
1464 * Currently we limit it to one page.
1465 */
1466 s32 offset;
1467 sector_t bb_sector;
1468 u64 *bbp;
1469 int i;
1470 int sectors = le16_to_cpu(sb->bblog_size);
1471 if (sectors > (PAGE_SIZE / 512))
1472 return -EINVAL;
1473 offset = le32_to_cpu(sb->bblog_offset);
1474 if (offset == 0)
1475 return -EINVAL;
1476 bb_sector = (long long)offset;
1477 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001478 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001479 return -EIO;
1480 bbp = (u64 *)page_address(rdev->bb_page);
1481 rdev->badblocks.shift = sb->bblog_shift;
1482 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1483 u64 bb = le64_to_cpu(*bbp);
1484 int count = bb & (0x3ff);
1485 u64 sector = bb >> 10;
1486 sector <<= sb->bblog_shift;
1487 count <<= sb->bblog_shift;
1488 if (bb + 1 == 0)
1489 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001490 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001491 return -EINVAL;
1492 }
NeilBrown486adf72013-04-24 11:42:44 +10001493 } else if (sb->bblog_offset != 0)
1494 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001495
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001496 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001497 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001498 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001500 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1503 sb->level != refsb->level ||
1504 sb->layout != refsb->layout ||
1505 sb->chunksize != refsb->chunksize) {
1506 printk(KERN_WARNING "md: %s has strangely different"
1507 " superblock to %s\n",
1508 bdevname(rdev->bdev,b),
1509 bdevname(refdev->bdev,b2));
1510 return -EINVAL;
1511 }
1512 ev1 = le64_to_cpu(sb->events);
1513 ev2 = le64_to_cpu(refsb->events);
1514
1515 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001516 ret = 1;
1517 else
1518 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
NeilBrownc6563a82012-05-21 09:27:00 +10001520 if (minor_version) {
1521 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1522 sectors -= rdev->data_offset;
1523 } else
1524 sectors = rdev->sb_start;
1525 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001527 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001528 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
NeilBrownfd01b882011-10-11 16:47:53 +11001531static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001533 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001534 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
NeilBrown41158c72005-06-21 17:17:25 -07001536 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001537 clear_bit(Faulty, &rdev->flags);
1538 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001539 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001540 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (mddev->raid_disks == 0) {
1543 mddev->major_version = 1;
1544 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001545 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001546 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001547 mddev->ctime = le64_to_cpu(sb->ctime);
1548 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001550 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 mddev->layout = le32_to_cpu(sb->layout);
1552 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001553 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001554 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001555 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001556 mddev->bitmap_info.space = 0;
1557 /* Default location for bitmap is 1K after superblock
1558 * using 3K - total of 4K
1559 */
NeilBrownc3d97142009-12-14 12:49:52 +11001560 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001561 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001562 mddev->reshape_backwards = 0;
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1565 memcpy(mddev->uuid, sb->set_uuid, 16);
1566
1567 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001568
NeilBrown71c08052005-09-09 16:23:51 -07001569 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001570 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001571 mddev->bitmap_info.offset =
1572 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001573 /* Metadata doesn't record how much space is available.
1574 * For 1.0, we assume we can use up to the superblock
1575 * if before, else to 4K beyond superblock.
1576 * For others, assume no change is possible.
1577 */
1578 if (mddev->minor_version > 0)
1579 mddev->bitmap_info.space = 0;
1580 else if (mddev->bitmap_info.offset > 0)
1581 mddev->bitmap_info.space =
1582 8 - mddev->bitmap_info.offset;
1583 else
1584 mddev->bitmap_info.space =
1585 -mddev->bitmap_info.offset;
1586 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001587
NeilBrownf6705572006-03-27 01:18:11 -08001588 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1589 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1590 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1591 mddev->new_level = le32_to_cpu(sb->new_level);
1592 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001593 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001594 if (mddev->delta_disks < 0 ||
1595 (mddev->delta_disks == 0 &&
1596 (le32_to_cpu(sb->feature_map)
1597 & MD_FEATURE_RESHAPE_BACKWARDS)))
1598 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001599 } else {
1600 mddev->reshape_position = MaxSector;
1601 mddev->delta_disks = 0;
1602 mddev->new_level = mddev->level;
1603 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001604 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001605 }
1606
Song Liu486b0f72016-08-19 15:34:01 -07001607 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001608 set_bit(MD_HAS_JOURNAL, &mddev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001609 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001610 /* Insist of good event counter while assembling, except for
1611 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001613 if (rdev->desc_nr >= 0 &&
1614 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001615 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1616 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001617 if (ev1 < mddev->events)
1618 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001619 } else if (mddev->bitmap) {
1620 /* If adding to array with a bitmap, then we can accept an
1621 * older device, but not too old.
1622 */
NeilBrown41158c72005-06-21 17:17:25 -07001623 if (ev1 < mddev->bitmap->events_cleared)
1624 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001625 if (ev1 < mddev->events)
1626 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001627 } else {
1628 if (ev1 < mddev->events)
1629 /* just a hot-add of a new device, leave raid_disk at -1 */
1630 return 0;
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (mddev->level != LEVEL_MULTIPATH) {
1633 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001634 if (rdev->desc_nr < 0 ||
1635 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001636 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001637 rdev->desc_nr = -1;
1638 } else
1639 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001641 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001643 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001644 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 break;
Song Liubac624f2015-08-13 14:31:55 -07001646 case MD_DISK_ROLE_JOURNAL: /* journal device */
1647 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1648 /* journal device without journal feature */
1649 printk(KERN_WARNING
1650 "md: journal device provided without journal feature, ignoring the device\n");
1651 return -EINVAL;
1652 }
1653 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001654 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001655 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 default:
NeilBrownf4667222013-12-09 12:04:56 +11001658 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001659 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001660 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001661 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001662 if (!(le32_to_cpu(sb->feature_map) &
1663 MD_FEATURE_RECOVERY_BITMAP))
1664 rdev->saved_raid_disk = -1;
1665 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001666 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 rdev->raid_disk = role;
1668 break;
1669 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001670 if (sb->devflags & WriteMostly1)
1671 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001672 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1673 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001674 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001675 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return 0;
1678}
1679
NeilBrownfd01b882011-10-11 16:47:53 +11001680static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001683 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 int max_dev, i;
1685 /* make rdev->sb match mddev and rdev data. */
1686
Namhyung Kim65a06f062011-07-27 11:00:36 +10001687 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 sb->feature_map = 0;
1690 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001691 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 memset(sb->pad3, 0, sizeof(sb->pad3));
1693
1694 sb->utime = cpu_to_le64((__u64)mddev->utime);
1695 sb->events = cpu_to_le64(mddev->events);
1696 if (mddev->in_sync)
1697 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001698 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1699 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 else
1701 sb->resync_offset = cpu_to_le64(0);
1702
NeilBrown1c05b4b2006-10-21 10:24:08 -07001703 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001704
NeilBrownf0ca3402006-02-02 14:28:04 -08001705 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001706 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001707 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001708 sb->level = cpu_to_le32(mddev->level);
1709 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001710
NeilBrownaeb9b2112011-08-25 14:43:08 +10001711 if (test_bit(WriteMostly, &rdev->flags))
1712 sb->devflags |= WriteMostly1;
1713 else
1714 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001715 sb->data_offset = cpu_to_le64(rdev->data_offset);
1716 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001717
NeilBrownc3d97142009-12-14 12:49:52 +11001718 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1719 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001720 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001721 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001722
Shaohua Lif2076e72015-10-08 21:54:12 -07001723 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001724 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001725 sb->feature_map |=
1726 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1727 sb->recovery_offset =
1728 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001729 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1730 sb->feature_map |=
1731 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001732 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001733 /* Note: recovery_offset and journal_tail share space */
1734 if (test_bit(Journal, &rdev->flags))
1735 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001736 if (test_bit(Replacement, &rdev->flags))
1737 sb->feature_map |=
1738 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001739
NeilBrownf6705572006-03-27 01:18:11 -08001740 if (mddev->reshape_position != MaxSector) {
1741 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1742 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1743 sb->new_layout = cpu_to_le32(mddev->new_layout);
1744 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1745 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001746 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001747 if (mddev->delta_disks == 0 &&
1748 mddev->reshape_backwards)
1749 sb->feature_map
1750 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001751 if (rdev->new_data_offset != rdev->data_offset) {
1752 sb->feature_map
1753 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1754 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1755 - rdev->data_offset));
1756 }
NeilBrownf6705572006-03-27 01:18:11 -08001757 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001758
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001759 if (mddev_is_clustered(mddev))
1760 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1761
NeilBrown2699b672011-07-28 11:31:47 +10001762 if (rdev->badblocks.count == 0)
1763 /* Nothing to do for bad blocks*/ ;
1764 else if (sb->bblog_offset == 0)
1765 /* Cannot record bad blocks on this device */
1766 md_error(mddev, rdev);
1767 else {
1768 struct badblocks *bb = &rdev->badblocks;
1769 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1770 u64 *p = bb->page;
1771 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1772 if (bb->changed) {
1773 unsigned seq;
1774
1775retry:
1776 seq = read_seqbegin(&bb->lock);
1777
1778 memset(bbp, 0xff, PAGE_SIZE);
1779
1780 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001781 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001782 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1783 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001784 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001785 }
NeilBrownd0962932012-03-19 12:46:41 +11001786 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001787 if (read_seqretry(&bb->lock, seq))
1788 goto retry;
1789
1790 bb->sector = (rdev->sb_start +
1791 (int)le32_to_cpu(sb->bblog_offset));
1792 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001793 }
1794 }
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001797 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (rdev2->desc_nr+1 > max_dev)
1799 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001800
NeilBrown70471da2009-08-03 10:59:57 +10001801 if (max_dev > le32_to_cpu(sb->max_dev)) {
1802 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001803 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001804 rdev->sb_size = max_dev * 2 + 256;
1805 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1806 if (rdev->sb_size & bmask)
1807 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001808 } else
1809 max_dev = le32_to_cpu(sb->max_dev);
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001812 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001813
Song Liua97b7892015-10-08 21:54:09 -07001814 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1815 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
NeilBrowndafb20f2012-03-19 12:46:39 +11001817 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001819 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001820 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001821 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001823 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001824 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001825 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001826 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 else
Song Liuc4d4c912015-08-13 14:31:54 -07001828 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 sb->sb_csum = calc_sb_1_csum(sb);
1832}
1833
Chris Webb0cd17fe2008-06-28 08:31:46 +10001834static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001835super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001836{
1837 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001838 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001839 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001840 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001841 if (rdev->data_offset != rdev->new_data_offset)
1842 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001843 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001844 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001845 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001846 max_sectors -= rdev->data_offset;
1847 if (!num_sectors || num_sectors > max_sectors)
1848 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001849 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001850 /* minor version 0 with bitmap we can't move */
1851 return 0;
1852 } else {
1853 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001854 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001855 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001856 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001857 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001858 if (!num_sectors || num_sectors > max_sectors)
1859 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001860 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001861 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001862 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001863 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001864 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001865 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001866 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001867 rdev->sb_page);
1868 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001869 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001870
1871}
1872
1873static int
1874super_1_allow_new_offset(struct md_rdev *rdev,
1875 unsigned long long new_offset)
1876{
1877 /* All necessary checks on new >= old have been done */
1878 struct bitmap *bitmap;
1879 if (new_offset >= rdev->data_offset)
1880 return 1;
1881
1882 /* with 1.0 metadata, there is no metadata to tread on
1883 * so we can always move back */
1884 if (rdev->mddev->minor_version == 0)
1885 return 1;
1886
1887 /* otherwise we must be sure not to step on
1888 * any metadata, so stay:
1889 * 36K beyond start of superblock
1890 * beyond end of badblocks
1891 * beyond write-intent bitmap
1892 */
1893 if (rdev->sb_start + (32+4)*2 > new_offset)
1894 return 0;
1895 bitmap = rdev->mddev->bitmap;
1896 if (bitmap && !rdev->mddev->bitmap_info.file &&
1897 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001898 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001899 return 0;
1900 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1901 return 0;
1902
1903 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001904}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Adrian Bunk75c96f82005-05-05 16:16:09 -07001906static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 [0] = {
1908 .name = "0.90.0",
1909 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001910 .load_super = super_90_load,
1911 .validate_super = super_90_validate,
1912 .sync_super = super_90_sync,
1913 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001914 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 },
1916 [1] = {
1917 .name = "md-1",
1918 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001919 .load_super = super_1_load,
1920 .validate_super = super_1_validate,
1921 .sync_super = super_1_sync,
1922 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001923 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 },
1925};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
NeilBrownfd01b882011-10-11 16:47:53 +11001927static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001928{
1929 if (mddev->sync_super) {
1930 mddev->sync_super(mddev, rdev);
1931 return;
1932 }
1933
1934 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1935
1936 super_types[mddev->major_version].sync_super(mddev, rdev);
1937}
1938
NeilBrownfd01b882011-10-11 16:47:53 +11001939static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
NeilBrown3cb03002011-10-11 16:45:26 +11001941 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
NeilBrown4b809912008-07-21 17:05:25 +10001943 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001944 rdev_for_each_rcu(rdev, mddev1) {
1945 if (test_bit(Faulty, &rdev->flags) ||
1946 test_bit(Journal, &rdev->flags) ||
1947 rdev->raid_disk == -1)
1948 continue;
1949 rdev_for_each_rcu(rdev2, mddev2) {
1950 if (test_bit(Faulty, &rdev2->flags) ||
1951 test_bit(Journal, &rdev2->flags) ||
1952 rdev2->raid_disk == -1)
1953 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001954 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001955 rdev2->bdev->bd_contains) {
1956 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001957 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001958 }
Song Liu0b020e82015-09-03 23:00:35 -07001959 }
1960 }
NeilBrown4b809912008-07-21 17:05:25 +10001961 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return 0;
1963}
1964
1965static LIST_HEAD(pending_raid_disks);
1966
Andre Nollac5e7112009-08-03 10:59:47 +10001967/*
1968 * Try to register data integrity profile for an mddev
1969 *
1970 * This is called when an array is started and after a disk has been kicked
1971 * from the array. It only succeeds if all working and active component devices
1972 * are integrity capable with matching profiles.
1973 */
NeilBrownfd01b882011-10-11 16:47:53 +11001974int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001975{
NeilBrown3cb03002011-10-11 16:45:26 +11001976 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001977
Andre Nollac5e7112009-08-03 10:59:47 +10001978 if (list_empty(&mddev->disks))
1979 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001980 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1981 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001982 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001983 /* skip spares and non-functional disks */
1984 if (test_bit(Faulty, &rdev->flags))
1985 continue;
1986 if (rdev->raid_disk < 0)
1987 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001988 if (!reference) {
1989 /* Use the first rdev as the reference */
1990 reference = rdev;
1991 continue;
1992 }
1993 /* does this rdev's profile match the reference profile? */
1994 if (blk_integrity_compare(reference->bdev->bd_disk,
1995 rdev->bdev->bd_disk) < 0)
1996 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001997 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001998 if (!reference || !bdev_get_integrity(reference->bdev))
1999 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002000 /*
2001 * All component devices are integrity capable and have matching
2002 * profiles, register the common profile for the md device.
2003 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002004 blk_integrity_register(mddev->gendisk,
2005 bdev_get_integrity(reference->bdev));
2006
Martin K. Petersena91a2782011-03-17 11:11:05 +01002007 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2008 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2009 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2010 mdname(mddev));
2011 return -EINVAL;
2012 }
Andre Nollac5e7112009-08-03 10:59:47 +10002013 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002014}
Andre Nollac5e7112009-08-03 10:59:47 +10002015EXPORT_SYMBOL(md_integrity_register);
2016
Dan Williams1501efa2016-01-13 16:00:07 -08002017/*
2018 * Attempt to add an rdev, but only if it is consistent with the current
2019 * integrity profile
2020 */
2021int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002022{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002023 struct blk_integrity *bi_rdev;
2024 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002025 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002026
2027 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002028 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002029
2030 bi_rdev = bdev_get_integrity(rdev->bdev);
2031 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002032
2033 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002034 return 0;
2035
2036 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2037 printk(KERN_NOTICE "%s: incompatible integrity profile for %s\n",
2038 mdname(mddev), bdevname(rdev->bdev, name));
2039 return -ENXIO;
2040 }
2041
2042 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002043}
2044EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002045
NeilBrownf72ffdd2014-09-30 14:23:59 +10002046static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002048 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002049 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002050 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Dan Williams11e2ede2008-04-30 00:52:32 -07002052 /* prevent duplicates */
2053 if (find_rdev(mddev, rdev->bdev->bd_dev))
2054 return -EEXIST;
2055
Andre Nolldd8ac332009-03-31 14:33:13 +11002056 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002057 if (!test_bit(Journal, &rdev->flags) &&
2058 rdev->sectors &&
2059 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002060 if (mddev->pers) {
2061 /* Cannot change size, so fail
2062 * If mddev->level <= 0, then we don't care
2063 * about aligning sizes (e.g. linear)
2064 */
2065 if (mddev->level > 0)
2066 return -ENOSPC;
2067 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002068 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 /* Verify rdev->desc_nr is unique.
2072 * If it is -1, assign a free number, else
2073 * check number is not in use
2074 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002075 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (rdev->desc_nr < 0) {
2077 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002078 if (mddev->pers)
2079 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002080 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 choice++;
2082 rdev->desc_nr = choice;
2083 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002084 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002085 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002089 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002090 if (!test_bit(Journal, &rdev->flags) &&
2091 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002092 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2093 mdname(mddev), mddev->max_disks);
2094 return -EBUSY;
2095 }
NeilBrown19133a42005-11-08 21:39:35 -08002096 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002097 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002100 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002101
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002102 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002103 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002104
Tejun Heo0762b8b2008-08-25 19:56:12 +09002105 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002106 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2107 /* failure here is OK */;
2108 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002109
NeilBrown4b809912008-07-21 17:05:25 +10002110 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002111 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002112
2113 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002114 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002117
2118 fail:
2119 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2120 b, mdname(mddev));
2121 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
NeilBrown177a99b2008-02-06 01:39:56 -08002124static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002125{
NeilBrown3cb03002011-10-11 16:45:26 +11002126 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002127 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002128 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002129}
2130
NeilBrownf72ffdd2014-09-30 14:23:59 +10002131static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002134
Tejun Heo49731ba2011-01-14 18:43:57 +01002135 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002136 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2138 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002139 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002140 sysfs_put(rdev->sysfs_state);
2141 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002142 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002143 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002144 * writing to 'remove' to "dev/state". We also need
2145 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002146 */
NeilBrown4b809912008-07-21 17:05:25 +10002147 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002148 INIT_WORK(&rdev->del_work, md_delayed_delete);
2149 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002150 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
2153/*
2154 * prevent the device from being mounted, repartitioned or
2155 * otherwise reused by a RAID array (or any other kernel
2156 * subsystem), by bd_claiming the device.
2157 */
NeilBrown3cb03002011-10-11 16:45:26 +11002158static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 int err = 0;
2161 struct block_device *bdev;
2162 char b[BDEVNAME_SIZE];
2163
Tejun Heod4d77622010-11-13 11:55:18 +01002164 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002165 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (IS_ERR(bdev)) {
2167 printk(KERN_ERR "md: could not open %s.\n",
2168 __bdevname(dev, b));
2169 return PTR_ERR(bdev);
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 rdev->bdev = bdev;
2172 return err;
2173}
2174
NeilBrown3cb03002011-10-11 16:45:26 +11002175static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
2177 struct block_device *bdev = rdev->bdev;
2178 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002179 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
2182void md_autodetect_dev(dev_t dev);
2183
NeilBrownf72ffdd2014-09-30 14:23:59 +10002184static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 printk(KERN_INFO "md: export_rdev(%s)\n",
2189 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002190 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002192 if (test_bit(AutoDetected, &rdev->flags))
2193 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194#endif
2195 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002196 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002199void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 unbind_rdev_from_array(rdev);
2202 export_rdev(rdev);
2203}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002204EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
NeilBrownfd01b882011-10-11 16:47:53 +11002206static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
NeilBrown0638bb02014-09-25 17:43:47 +10002208 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
NeilBrown0638bb02014-09-25 17:43:47 +10002210 while (!list_empty(&mddev->disks)) {
2211 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2212 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002213 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 mddev->raid_disks = 0;
2216 mddev->major_version = 0;
2217}
2218
NeilBrownf72ffdd2014-09-30 14:23:59 +10002219static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
NeilBrown42543762006-06-26 00:27:57 -07002221 /* Update each superblock (in-memory image), but
2222 * if we are allowed to, skip spares which already
2223 * have the right event counter, or have one earlier
2224 * (which would mean they aren't being marked as dirty
2225 * with the rest of the array)
2226 */
NeilBrown3cb03002011-10-11 16:45:26 +11002227 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002228 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002229 if (rdev->sb_events == mddev->events ||
2230 (nospares &&
2231 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002232 rdev->sb_events+1 == mddev->events)) {
2233 /* Don't update this superblock */
2234 rdev->sb_loaded = 2;
2235 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002236 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002237 rdev->sb_loaded = 1;
2238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
2240}
2241
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002242static bool does_sb_need_changing(struct mddev *mddev)
2243{
2244 struct md_rdev *rdev;
2245 struct mdp_superblock_1 *sb;
2246 int role;
2247
2248 /* Find a good rdev */
2249 rdev_for_each(rdev, mddev)
2250 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2251 break;
2252
2253 /* No good device found. */
2254 if (!rdev)
2255 return false;
2256
2257 sb = page_address(rdev->sb_page);
2258 /* Check if a device has become faulty or a spare become active */
2259 rdev_for_each(rdev, mddev) {
2260 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2261 /* Device activated? */
2262 if (role == 0xffff && rdev->raid_disk >=0 &&
2263 !test_bit(Faulty, &rdev->flags))
2264 return true;
2265 /* Device turned faulty? */
2266 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2267 return true;
2268 }
2269
2270 /* Check if any mddev parameters have changed */
2271 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2272 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002273 (mddev->layout != le64_to_cpu(sb->layout)) ||
2274 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2275 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2276 return true;
2277
2278 return false;
2279}
2280
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002281void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
NeilBrown3cb03002011-10-11 16:45:26 +11002283 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002284 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002285 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002286 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002287 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
NeilBrownd87f0642013-04-24 11:42:40 +10002289 if (mddev->ro) {
2290 if (force_change)
2291 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2292 return;
2293 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002294
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002295repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002296 if (mddev_is_clustered(mddev)) {
2297 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2298 force_change = 1;
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002299 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2300 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002301 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002302 /* Has someone else has updated the sb */
2303 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002304 if (ret == 0)
2305 md_cluster_ops->metadata_update_cancel(mddev);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002306 bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING),
2307 BIT(MD_CHANGE_DEVS) |
2308 BIT(MD_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002309 return;
2310 }
2311 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002312
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002313 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002314 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002315 if (rdev->raid_disk >= 0 &&
2316 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002317 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002318 !test_bit(In_sync, &rdev->flags) &&
2319 mddev->curr_resync_completed > rdev->recovery_offset)
2320 rdev->recovery_offset = mddev->curr_resync_completed;
2321
NeilBrownf72ffdd2014-09-30 14:23:59 +10002322 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002323 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002324 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002325 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002326 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002327 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002328 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002329 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002330 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002331 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002332 md_error(mddev, rdev);
2333 }
2334 clear_bit(Blocked, &rdev->flags);
2335 clear_bit(BlockedBadBlocks, &rdev->flags);
2336 wake_up(&rdev->blocked_wait);
2337 }
2338 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002339 wake_up(&mddev->sb_wait);
2340 return;
2341 }
2342
NeilBrown85572d72014-12-15 12:56:56 +11002343 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002344
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002345 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002346
NeilBrown850b2b422006-10-03 01:15:46 -07002347 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2348 force_change = 1;
2349 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2350 /* just a clean<-> dirty transition, possibly leave spares alone,
2351 * though if events isn't the right even/odd, we will have to do
2352 * spares after all
2353 */
2354 nospares = 1;
2355 if (force_change)
2356 nospares = 0;
2357 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002358 /* If the array is degraded, then skipping spares is both
2359 * dangerous and fairly pointless.
2360 * Dangerous because a device that was removed from the array
2361 * might have a event_count that still looks up-to-date,
2362 * so it can be re-added without a resync.
2363 * Pointless because if there are any spares to skip,
2364 * then a recovery will happen and soon that array won't
2365 * be degraded any more and the spare can go back to sleep then.
2366 */
NeilBrown850b2b422006-10-03 01:15:46 -07002367 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002368
NeilBrown06d91a52005-06-21 17:17:12 -07002369 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002370
2371 /* If this is just a dirty<->clean transition, and the array is clean
2372 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002373 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002374 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002375 && mddev->can_decrease_events
2376 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002377 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002378 mddev->can_decrease_events = 0;
2379 } else {
NeilBrown42543762006-06-26 00:27:57 -07002380 /* otherwise we have to go forward and ... */
2381 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002382 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
NeilBrown403df472014-09-30 15:52:29 +10002385 /*
2386 * This 64-bit counter should never wrap.
2387 * Either we are in around ~1 trillion A.C., assuming
2388 * 1 reboot per second, or we have a bug...
2389 */
2390 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002391
NeilBrowndafb20f2012-03-19 12:46:39 +11002392 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002393 if (rdev->badblocks.changed)
2394 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002395 if (test_bit(Faulty, &rdev->flags))
2396 set_bit(FaultRecorded, &rdev->flags);
2397 }
NeilBrown2699b672011-07-28 11:31:47 +10002398
NeilBrowne6910632008-02-06 01:39:51 -08002399 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002400 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
NeilBrown36a4e1f2011-10-07 14:23:17 +11002402 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2403 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
NeilBrown4ad13662007-07-17 04:06:13 -07002405 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002406 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002408
NeilBrown42543762006-06-26 00:27:57 -07002409 if (rdev->sb_loaded != 1)
2410 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
NeilBrownf4667222013-12-09 12:04:56 +11002412 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002413 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002414 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002415 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002416 pr_debug("md: (write) %s's sb offset: %llu\n",
2417 bdevname(rdev->bdev, b),
2418 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002419 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002420 if (rdev->badblocks.size) {
2421 md_super_write(mddev, rdev,
2422 rdev->badblocks.sector,
2423 rdev->badblocks.size << 9,
2424 rdev->bb_page);
2425 rdev->badblocks.size = 0;
2426 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002427
NeilBrownf4667222013-12-09 12:04:56 +11002428 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002429 pr_debug("md: %s (skipping faulty)\n",
2430 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002431
NeilBrown7bfa19f2005-06-21 17:17:28 -07002432 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /* only need to write one superblock... */
2434 break;
2435 }
NeilBrowna9701a32005-11-08 21:39:34 -08002436 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002437 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002438
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002439 if (mddev_is_clustered(mddev) && ret == 0)
2440 md_cluster_ops->metadata_update_finish(mddev);
2441
NeilBrown850b2b422006-10-03 01:15:46 -07002442 if (mddev->in_sync != sync_req ||
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002443 !bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING),
2444 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002445 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002446 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002447 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002448 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2449 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002450
NeilBrowndafb20f2012-03-19 12:46:39 +11002451 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002452 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2453 clear_bit(Blocked, &rdev->flags);
2454
2455 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002456 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002457 clear_bit(BlockedBadBlocks, &rdev->flags);
2458 wake_up(&rdev->blocked_wait);
2459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002461EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002463static int add_bound_rdev(struct md_rdev *rdev)
2464{
2465 struct mddev *mddev = rdev->mddev;
2466 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002467 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002468
Shaohua Li87d4d912016-01-06 14:37:14 -08002469 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002470 /* If there is hot_add_disk but no hot_remove_disk
2471 * then added disks for geometry changes,
2472 * and should be added immediately.
2473 */
2474 super_types[mddev->major_version].
2475 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002476 if (add_journal)
2477 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002478 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002479 if (add_journal)
2480 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002481 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002482 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002483 return err;
2484 }
2485 }
2486 sysfs_notify_dirent_safe(rdev->sysfs_state);
2487
2488 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2489 if (mddev->degraded)
2490 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2491 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2492 md_new_event(mddev);
2493 md_wakeup_thread(mddev->thread);
2494 return 0;
2495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Andre Noll7f6ce762008-03-23 18:34:54 +01002497/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002498 * We want to accept with case. For this we use cmd_match.
2499 */
2500static int cmd_match(const char *cmd, const char *str)
2501{
2502 /* See if cmd, written into a sysfs file, matches
2503 * str. They must either be the same, or cmd can
2504 * have a trailing newline
2505 */
2506 while (*cmd && *str && *cmd == *str) {
2507 cmd++;
2508 str++;
2509 }
2510 if (*cmd == '\n')
2511 cmd++;
2512 if (*str || *cmd)
2513 return 0;
2514 return 1;
2515}
2516
NeilBrown86e6ffd2005-11-08 21:39:24 -08002517struct rdev_sysfs_entry {
2518 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002519 ssize_t (*show)(struct md_rdev *, char *);
2520 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002521};
2522
2523static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002524state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002525{
2526 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002527 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002528 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002529
NeilBrown758bfc82014-12-15 12:56:59 +11002530 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002531 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002532 len+= sprintf(page+len, "%sfaulty",sep);
2533 sep = ",";
2534 }
NeilBrown758bfc82014-12-15 12:56:59 +11002535 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002536 len += sprintf(page+len, "%sin_sync",sep);
2537 sep = ",";
2538 }
Shaohua Liac6096e2015-10-04 09:20:11 -07002539 if (test_bit(Journal, &flags)) {
2540 len += sprintf(page+len, "%sjournal",sep);
2541 sep = ",";
2542 }
NeilBrown758bfc82014-12-15 12:56:59 +11002543 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002544 len += sprintf(page+len, "%swrite_mostly",sep);
2545 sep = ",";
2546 }
NeilBrown758bfc82014-12-15 12:56:59 +11002547 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002548 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002549 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002550 len += sprintf(page+len, "%sblocked", sep);
2551 sep = ",";
2552 }
NeilBrown758bfc82014-12-15 12:56:59 +11002553 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002554 !test_bit(Journal, &flags) &&
NeilBrown758bfc82014-12-15 12:56:59 +11002555 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002556 len += sprintf(page+len, "%sspare", sep);
2557 sep = ",";
2558 }
NeilBrown758bfc82014-12-15 12:56:59 +11002559 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002560 len += sprintf(page+len, "%swrite_error", sep);
2561 sep = ",";
2562 }
NeilBrown758bfc82014-12-15 12:56:59 +11002563 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002564 len += sprintf(page+len, "%swant_replacement", sep);
2565 sep = ",";
2566 }
NeilBrown758bfc82014-12-15 12:56:59 +11002567 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002568 len += sprintf(page+len, "%sreplacement", sep);
2569 sep = ",";
2570 }
2571
NeilBrown86e6ffd2005-11-08 21:39:24 -08002572 return len+sprintf(page+len, "\n");
2573}
2574
NeilBrown45dc2de2006-06-26 00:27:58 -07002575static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002576state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002577{
2578 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002579 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002580 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002581 * writemostly - sets write_mostly
2582 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002583 * blocked - sets the Blocked flags
2584 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002585 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002586 * -insync - clear Insync for a device with a slot assigned,
2587 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002588 * write_error - sets WriteErrorSeen
2589 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002590 */
2591 int err = -EINVAL;
2592 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2593 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002594 if (test_bit(Faulty, &rdev->flags))
2595 err = 0;
2596 else
2597 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002598 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002599 if (rdev->mddev->pers) {
2600 clear_bit(Blocked, &rdev->flags);
2601 remove_and_add_spares(rdev->mddev, rdev);
2602 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002603 if (rdev->raid_disk >= 0)
2604 err = -EBUSY;
2605 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002606 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002607 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002608 if (mddev_is_clustered(mddev))
2609 err = md_cluster_ops->remove_disk(mddev, rdev);
2610
2611 if (err == 0) {
2612 md_kick_rdev_from_array(rdev);
2613 if (mddev->pers)
2614 md_update_sb(mddev, 1);
2615 md_new_event(mddev);
2616 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002617 }
NeilBrownf6556752006-06-26 00:28:01 -07002618 } else if (cmd_match(buf, "writemostly")) {
2619 set_bit(WriteMostly, &rdev->flags);
2620 err = 0;
2621 } else if (cmd_match(buf, "-writemostly")) {
2622 clear_bit(WriteMostly, &rdev->flags);
2623 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002624 } else if (cmd_match(buf, "blocked")) {
2625 set_bit(Blocked, &rdev->flags);
2626 err = 0;
2627 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002628 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002629 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002630 /* metadata handler doesn't understand badblocks,
2631 * so we need to fail the device
2632 */
2633 md_error(rdev->mddev, rdev);
2634 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002635 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002636 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002637 wake_up(&rdev->blocked_wait);
2638 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2639 md_wakeup_thread(rdev->mddev->thread);
2640
2641 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002642 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2643 set_bit(In_sync, &rdev->flags);
2644 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002645 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2646 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002647 if (rdev->mddev->pers == NULL) {
2648 clear_bit(In_sync, &rdev->flags);
2649 rdev->saved_raid_disk = rdev->raid_disk;
2650 rdev->raid_disk = -1;
2651 err = 0;
2652 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002653 } else if (cmd_match(buf, "write_error")) {
2654 set_bit(WriteErrorSeen, &rdev->flags);
2655 err = 0;
2656 } else if (cmd_match(buf, "-write_error")) {
2657 clear_bit(WriteErrorSeen, &rdev->flags);
2658 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002659 } else if (cmd_match(buf, "want_replacement")) {
2660 /* Any non-spare device that is not a replacement can
2661 * become want_replacement at any time, but we then need to
2662 * check if recovery is needed.
2663 */
2664 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002665 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002666 !test_bit(Replacement, &rdev->flags))
2667 set_bit(WantReplacement, &rdev->flags);
2668 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2669 md_wakeup_thread(rdev->mddev->thread);
2670 err = 0;
2671 } else if (cmd_match(buf, "-want_replacement")) {
2672 /* Clearing 'want_replacement' is always allowed.
2673 * Once replacements starts it is too late though.
2674 */
2675 err = 0;
2676 clear_bit(WantReplacement, &rdev->flags);
2677 } else if (cmd_match(buf, "replacement")) {
2678 /* Can only set a device as a replacement when array has not
2679 * yet been started. Once running, replacement is automatic
2680 * from spares, or by assigning 'slot'.
2681 */
2682 if (rdev->mddev->pers)
2683 err = -EBUSY;
2684 else {
2685 set_bit(Replacement, &rdev->flags);
2686 err = 0;
2687 }
2688 } else if (cmd_match(buf, "-replacement")) {
2689 /* Similarly, can only clear Replacement before start */
2690 if (rdev->mddev->pers)
2691 err = -EBUSY;
2692 else {
2693 clear_bit(Replacement, &rdev->flags);
2694 err = 0;
2695 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002696 } else if (cmd_match(buf, "re-add")) {
2697 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002698 /* clear_bit is performed _after_ all the devices
2699 * have their local Faulty bit cleared. If any writes
2700 * happen in the meantime in the local node, they
2701 * will land in the local bitmap, which will be synced
2702 * by this node eventually
2703 */
2704 if (!mddev_is_clustered(rdev->mddev) ||
2705 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2706 clear_bit(Faulty, &rdev->flags);
2707 err = add_bound_rdev(rdev);
2708 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002709 } else
2710 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002711 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002712 if (!err)
2713 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002714 return err ? err : len;
2715}
NeilBrown80ca3a42006-07-10 04:44:18 -07002716static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002717__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002718
2719static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002720errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002721{
2722 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2723}
2724
2725static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002726errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002727{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002728 unsigned int n;
2729 int rv;
2730
2731 rv = kstrtouint(buf, 10, &n);
2732 if (rv < 0)
2733 return rv;
2734 atomic_set(&rdev->corrected_errors, n);
2735 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002736}
2737static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002738__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002739
NeilBrown014236d2006-01-06 00:20:55 -08002740static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002741slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002742{
Shaohua Lif2076e72015-10-08 21:54:12 -07002743 if (test_bit(Journal, &rdev->flags))
2744 return sprintf(page, "journal\n");
2745 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002746 return sprintf(page, "none\n");
2747 else
2748 return sprintf(page, "%d\n", rdev->raid_disk);
2749}
2750
2751static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002752slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002753{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002754 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002755 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002756
Shaohua Lif2076e72015-10-08 21:54:12 -07002757 if (test_bit(Journal, &rdev->flags))
2758 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002759 if (strncmp(buf, "none", 4)==0)
2760 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002761 else {
2762 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2763 if (err < 0)
2764 return err;
2765 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002766 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002767 /* Setting 'slot' on an active array requires also
2768 * updating the 'rd%d' link, and communicating
2769 * with the personality with ->hot_*_disk.
2770 * For now we only support removing
2771 * failed/spare devices. This normally happens automatically,
2772 * but not when the metadata is externally managed.
2773 */
NeilBrownc303da62008-02-06 01:39:51 -08002774 if (rdev->raid_disk == -1)
2775 return -EEXIST;
2776 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002777 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002778 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002779 clear_bit(Blocked, &rdev->flags);
2780 remove_and_add_spares(rdev->mddev, rdev);
2781 if (rdev->raid_disk >= 0)
2782 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002783 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2784 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002785 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002786 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002787 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002788 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002789 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002790
2791 if (rdev->raid_disk != -1)
2792 return -EBUSY;
2793
NeilBrownc6751b22011-02-02 11:57:13 +11002794 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2795 return -EBUSY;
2796
Neil Brown6c2fce22008-06-28 08:31:31 +10002797 if (rdev->mddev->pers->hot_add_disk == NULL)
2798 return -EINVAL;
2799
NeilBrownba1b41b2011-01-14 09:14:34 +11002800 if (slot >= rdev->mddev->raid_disks &&
2801 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2802 return -ENOSPC;
2803
Neil Brown6c2fce22008-06-28 08:31:31 +10002804 rdev->raid_disk = slot;
2805 if (test_bit(In_sync, &rdev->flags))
2806 rdev->saved_raid_disk = slot;
2807 else
2808 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002809 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002810 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002811 err = rdev->mddev->pers->
2812 hot_add_disk(rdev->mddev, rdev);
2813 if (err) {
2814 rdev->raid_disk = -1;
2815 return err;
2816 } else
2817 sysfs_notify_dirent_safe(rdev->sysfs_state);
2818 if (sysfs_link_rdev(rdev->mddev, rdev))
2819 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002820 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002821 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002822 if (slot >= rdev->mddev->raid_disks &&
2823 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002824 return -ENOSPC;
2825 rdev->raid_disk = slot;
2826 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002827 clear_bit(Faulty, &rdev->flags);
2828 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002829 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002830 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002831 }
NeilBrown014236d2006-01-06 00:20:55 -08002832 return len;
2833}
2834
NeilBrown014236d2006-01-06 00:20:55 -08002835static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002836__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002837
NeilBrown93c8cad2006-01-06 00:20:56 -08002838static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002839offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002840{
Andrew Morton6961ece2006-01-06 00:20:59 -08002841 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002842}
2843
2844static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002845offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002846{
NeilBrownc6563a82012-05-21 09:27:00 +10002847 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002848 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002849 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002850 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002851 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002852 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002853 /* Must set offset before size, so overlap checks
2854 * can be sane */
2855 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002856 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002857 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002858 return len;
2859}
2860
2861static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002862__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002863
NeilBrownc6563a82012-05-21 09:27:00 +10002864static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2865{
2866 return sprintf(page, "%llu\n",
2867 (unsigned long long)rdev->new_data_offset);
2868}
2869
2870static ssize_t new_offset_store(struct md_rdev *rdev,
2871 const char *buf, size_t len)
2872{
2873 unsigned long long new_offset;
2874 struct mddev *mddev = rdev->mddev;
2875
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002876 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002877 return -EINVAL;
2878
NeilBrownf851b602014-12-11 10:02:10 +11002879 if (mddev->sync_thread ||
2880 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002881 return -EBUSY;
2882 if (new_offset == rdev->data_offset)
2883 /* reset is always permitted */
2884 ;
2885 else if (new_offset > rdev->data_offset) {
2886 /* must not push array size beyond rdev_sectors */
2887 if (new_offset - rdev->data_offset
2888 + mddev->dev_sectors > rdev->sectors)
2889 return -E2BIG;
2890 }
2891 /* Metadata worries about other space details. */
2892
2893 /* decreasing the offset is inconsistent with a backwards
2894 * reshape.
2895 */
2896 if (new_offset < rdev->data_offset &&
2897 mddev->reshape_backwards)
2898 return -EINVAL;
2899 /* Increasing offset is inconsistent with forwards
2900 * reshape. reshape_direction should be set to
2901 * 'backwards' first.
2902 */
2903 if (new_offset > rdev->data_offset &&
2904 !mddev->reshape_backwards)
2905 return -EINVAL;
2906
2907 if (mddev->pers && mddev->persistent &&
2908 !super_types[mddev->major_version]
2909 .allow_new_offset(rdev, new_offset))
2910 return -E2BIG;
2911 rdev->new_data_offset = new_offset;
2912 if (new_offset > rdev->data_offset)
2913 mddev->reshape_backwards = 1;
2914 else if (new_offset < rdev->data_offset)
2915 mddev->reshape_backwards = 0;
2916
2917 return len;
2918}
2919static struct rdev_sysfs_entry rdev_new_offset =
2920__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2921
NeilBrown83303b62006-01-06 00:21:06 -08002922static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002923rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002924{
Andre Nolldd8ac332009-03-31 14:33:13 +11002925 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002926}
2927
NeilBrownc5d79ad2008-02-06 01:39:54 -08002928static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2929{
2930 /* check if two start/length pairs overlap */
2931 if (s1+l1 <= s2)
2932 return 0;
2933 if (s2+l2 <= s1)
2934 return 0;
2935 return 1;
2936}
2937
Dan Williamsb522adc2009-03-31 15:00:31 +11002938static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2939{
2940 unsigned long long blocks;
2941 sector_t new;
2942
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002943 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002944 return -EINVAL;
2945
2946 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2947 return -EINVAL; /* sector conversion overflow */
2948
2949 new = blocks * 2;
2950 if (new != blocks * 2)
2951 return -EINVAL; /* unsigned long long to sector_t overflow */
2952
2953 *sectors = new;
2954 return 0;
2955}
2956
NeilBrown83303b62006-01-06 00:21:06 -08002957static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002958rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002959{
NeilBrownfd01b882011-10-11 16:47:53 +11002960 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002961 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002962 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002963
Shaohua Lif2076e72015-10-08 21:54:12 -07002964 if (test_bit(Journal, &rdev->flags))
2965 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002966 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002967 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002968 if (rdev->data_offset != rdev->new_data_offset)
2969 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002970 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002971 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002972 sectors = super_types[my_mddev->major_version].
2973 rdev_size_change(rdev, sectors);
2974 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002975 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002976 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002977 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002978 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002979 if (!my_mddev->pers->resize)
2980 /* Cannot change size for RAID0 or Linear etc */
2981 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002982 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002983 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002984 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002985
Andre Nolldd8ac332009-03-31 14:33:13 +11002986 rdev->sectors = sectors;
2987 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002988 /* Need to check that all other rdevs with the same
2989 * ->bdev do not overlap. 'rcu' is sufficient to walk
2990 * the rdev lists safely.
2991 * This check does not provide a hard guarantee, it
2992 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002993 */
NeilBrownfd01b882011-10-11 16:47:53 +11002994 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002995 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002996 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002997
NeilBrown8b1afc32014-09-29 15:33:20 +10002998 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002999 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003000 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003001
NeilBrowndafb20f2012-03-19 12:46:39 +11003002 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003003 if (rdev->bdev == rdev2->bdev &&
3004 rdev != rdev2 &&
3005 overlaps(rdev->data_offset, rdev->sectors,
3006 rdev2->data_offset,
3007 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003008 overlap = 1;
3009 break;
3010 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003011 if (overlap) {
3012 mddev_put(mddev);
3013 break;
3014 }
3015 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003016 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003017 if (overlap) {
3018 /* Someone else could have slipped in a size
3019 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003020 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003021 * safe, and trust userspace not to race with
3022 * itself
3023 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003024 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003025 return -EBUSY;
3026 }
3027 }
NeilBrown83303b62006-01-06 00:21:06 -08003028 return len;
3029}
3030
3031static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003032__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003033
NeilBrown3cb03002011-10-11 16:45:26 +11003034static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003035{
3036 unsigned long long recovery_start = rdev->recovery_offset;
3037
3038 if (test_bit(In_sync, &rdev->flags) ||
3039 recovery_start == MaxSector)
3040 return sprintf(page, "none\n");
3041
3042 return sprintf(page, "%llu\n", recovery_start);
3043}
3044
NeilBrown3cb03002011-10-11 16:45:26 +11003045static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003046{
3047 unsigned long long recovery_start;
3048
3049 if (cmd_match(buf, "none"))
3050 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003051 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003052 return -EINVAL;
3053
3054 if (rdev->mddev->pers &&
3055 rdev->raid_disk >= 0)
3056 return -EBUSY;
3057
3058 rdev->recovery_offset = recovery_start;
3059 if (recovery_start == MaxSector)
3060 set_bit(In_sync, &rdev->flags);
3061 else
3062 clear_bit(In_sync, &rdev->flags);
3063 return len;
3064}
3065
3066static struct rdev_sysfs_entry rdev_recovery_start =
3067__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3068
Vishal Vermafc974ee2015-12-24 19:20:34 -07003069/* sysfs access to bad-blocks list.
3070 * We present two files.
3071 * 'bad-blocks' lists sector numbers and lengths of ranges that
3072 * are recorded as bad. The list is truncated to fit within
3073 * the one-page limit of sysfs.
3074 * Writing "sector length" to this file adds an acknowledged
3075 * bad block list.
3076 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3077 * been acknowledged. Writing to this file adds bad blocks
3078 * without acknowledging them. This is largely for testing.
3079 */
NeilBrown3cb03002011-10-11 16:45:26 +11003080static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003081{
3082 return badblocks_show(&rdev->badblocks, page, 0);
3083}
NeilBrown3cb03002011-10-11 16:45:26 +11003084static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003085{
NeilBrownde393cd2011-07-28 11:31:48 +10003086 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3087 /* Maybe that ack was all we needed */
3088 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3089 wake_up(&rdev->blocked_wait);
3090 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003091}
3092static struct rdev_sysfs_entry rdev_bad_blocks =
3093__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3094
NeilBrown3cb03002011-10-11 16:45:26 +11003095static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003096{
3097 return badblocks_show(&rdev->badblocks, page, 1);
3098}
NeilBrown3cb03002011-10-11 16:45:26 +11003099static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003100{
3101 return badblocks_store(&rdev->badblocks, page, len, 1);
3102}
3103static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3104__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3105
NeilBrown86e6ffd2005-11-08 21:39:24 -08003106static struct attribute *rdev_default_attrs[] = {
3107 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003108 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003109 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003110 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003111 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003112 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003113 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003114 &rdev_bad_blocks.attr,
3115 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003116 NULL,
3117};
3118static ssize_t
3119rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3120{
3121 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003122 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003123
3124 if (!entry->show)
3125 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003126 if (!rdev->mddev)
3127 return -EBUSY;
3128 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003129}
3130
3131static ssize_t
3132rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3133 const char *page, size_t length)
3134{
3135 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003136 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003137 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003138 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003139
3140 if (!entry->store)
3141 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003142 if (!capable(CAP_SYS_ADMIN))
3143 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003144 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003145 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003146 if (rdev->mddev == NULL)
3147 rv = -EBUSY;
3148 else
3149 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003150 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003151 }
3152 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003153}
3154
3155static void rdev_free(struct kobject *ko)
3156{
NeilBrown3cb03002011-10-11 16:45:26 +11003157 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003158 kfree(rdev);
3159}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003160static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003161 .show = rdev_attr_show,
3162 .store = rdev_attr_store,
3163};
3164static struct kobj_type rdev_ktype = {
3165 .release = rdev_free,
3166 .sysfs_ops = &rdev_sysfs_ops,
3167 .default_attrs = rdev_default_attrs,
3168};
3169
NeilBrown3cb03002011-10-11 16:45:26 +11003170int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003171{
3172 rdev->desc_nr = -1;
3173 rdev->saved_raid_disk = -1;
3174 rdev->raid_disk = -1;
3175 rdev->flags = 0;
3176 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003177 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003178 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003179 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003180 rdev->sb_loaded = 0;
3181 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003182 atomic_set(&rdev->nr_pending, 0);
3183 atomic_set(&rdev->read_errors, 0);
3184 atomic_set(&rdev->corrected_errors, 0);
3185
3186 INIT_LIST_HEAD(&rdev->same_set);
3187 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003188
3189 /* Add space to store bad block list.
3190 * This reserves the space even on arrays where it cannot
3191 * be used - I wonder if that matters
3192 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003193 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003194}
3195EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196/*
3197 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3198 *
3199 * mark the device faulty if:
3200 *
3201 * - the device is nonexistent (zero size)
3202 * - the device has no valid superblock
3203 *
3204 * a faulty rdev _never_ has rdev->sb set.
3205 */
NeilBrown3cb03002011-10-11 16:45:26 +11003206static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
3208 char b[BDEVNAME_SIZE];
3209 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003210 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 sector_t size;
3212
NeilBrown9ffae0c2006-01-06 00:20:32 -08003213 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (!rdev) {
3215 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3216 return ERR_PTR(-ENOMEM);
3217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
NeilBrown2230dfe2011-07-28 11:31:46 +10003219 err = md_rdev_init(rdev);
3220 if (err)
3221 goto abort_free;
3222 err = alloc_disk_sb(rdev);
3223 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 goto abort_free;
3225
NeilBrownc5d79ad2008-02-06 01:39:54 -08003226 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (err)
3228 goto abort_free;
3229
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003230 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003231
Mike Snitzer77304d22010-11-08 14:39:12 +01003232 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003234 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 "md: %s has zero or unknown size, marking faulty!\n",
3236 bdevname(rdev->bdev,b));
3237 err = -EINVAL;
3238 goto abort_free;
3239 }
3240
3241 if (super_format >= 0) {
3242 err = super_types[super_format].
3243 load_super(rdev, NULL, super_minor);
3244 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003245 printk(KERN_WARNING
3246 "md: %s does not have a valid v%d.%d "
3247 "superblock, not importing!\n",
3248 bdevname(rdev->bdev,b),
3249 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 goto abort_free;
3251 }
3252 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003253 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 "md: could not read %s's sb, not importing!\n",
3255 bdevname(rdev->bdev,b));
3256 goto abort_free;
3257 }
3258 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return rdev;
3261
3262abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003263 if (rdev->bdev)
3264 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003265 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 kfree(rdev);
3267 return ERR_PTR(err);
3268}
3269
3270/*
3271 * Check a full RAID array for plausibility
3272 */
3273
NeilBrownf72ffdd2014-09-30 14:23:59 +10003274static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
3276 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003277 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 char b[BDEVNAME_SIZE];
3279
3280 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003281 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 switch (super_types[mddev->major_version].
3283 load_super(rdev, freshest, mddev->minor_version)) {
3284 case 1:
3285 freshest = rdev;
3286 break;
3287 case 0:
3288 break;
3289 default:
3290 printk( KERN_ERR \
3291 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003292 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003294 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
3296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 super_types[mddev->major_version].
3298 validate_super(mddev, freshest);
3299
3300 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003301 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003302 if (mddev->max_disks &&
3303 (rdev->desc_nr >= mddev->max_disks ||
3304 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003305 printk(KERN_WARNING
3306 "md: %s: %s: only %d devices permitted\n",
3307 mdname(mddev), bdevname(rdev->bdev, b),
3308 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003309 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003310 continue;
3311 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003312 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (super_types[mddev->major_version].
3314 validate_super(mddev, rdev)) {
3315 printk(KERN_WARNING "md: kicking non-fresh %s"
3316 " from array!\n",
3317 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003318 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 continue;
3320 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 if (mddev->level == LEVEL_MULTIPATH) {
3323 rdev->desc_nr = i++;
3324 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003325 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003326 } else if (rdev->raid_disk >=
3327 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3328 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003329 rdev->raid_disk = -1;
3330 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 }
3332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
NeilBrown72e02072009-12-14 12:49:55 +11003335/* Read a fixed-point number.
3336 * Numbers in sysfs attributes should be in "standard" units where
3337 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003338 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003339 * milliseconds or jiffies.
3340 * This function takes a decimal number with a possible fractional
3341 * component, and produces an integer which is the result of
3342 * multiplying that number by 10^'scale'.
3343 * all without any floating-point arithmetic.
3344 */
3345int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3346{
3347 unsigned long result = 0;
3348 long decimals = -1;
3349 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3350 if (*cp == '.')
3351 decimals = 0;
3352 else if (decimals < scale) {
3353 unsigned int value;
3354 value = *cp - '0';
3355 result = result * 10 + value;
3356 if (decimals >= 0)
3357 decimals++;
3358 }
3359 cp++;
3360 }
3361 if (*cp == '\n')
3362 cp++;
3363 if (*cp)
3364 return -EINVAL;
3365 if (decimals < 0)
3366 decimals = 0;
3367 while (decimals < scale) {
3368 result *= 10;
3369 decimals ++;
3370 }
3371 *res = result;
3372 return 0;
3373}
3374
NeilBrowneae17012005-11-08 21:39:23 -08003375static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003376safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003377{
3378 int msec = (mddev->safemode_delay*1000)/HZ;
3379 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3380}
3381static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003382safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003383{
NeilBrown16f17b32006-06-26 00:27:37 -07003384 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003385
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003386 if (mddev_is_clustered(mddev)) {
3387 pr_info("md: Safemode is disabled for clustered mode\n");
3388 return -EINVAL;
3389 }
3390
NeilBrown72e02072009-12-14 12:49:55 +11003391 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003392 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003393 if (msec == 0)
3394 mddev->safemode_delay = 0;
3395 else {
NeilBrown19052c02008-08-05 15:54:13 +10003396 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003397 unsigned long new_delay = (msec*HZ)/1000;
3398
3399 if (new_delay == 0)
3400 new_delay = 1;
3401 mddev->safemode_delay = new_delay;
3402 if (new_delay < old_delay || old_delay == 0)
3403 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003404 }
3405 return len;
3406}
3407static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003408__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003409
3410static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003411level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003412{
NeilBrown36d091f2014-12-15 12:56:58 +11003413 struct md_personality *p;
3414 int ret;
3415 spin_lock(&mddev->lock);
3416 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003417 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003418 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003419 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003420 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003421 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003422 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003423 else
NeilBrown36d091f2014-12-15 12:56:58 +11003424 ret = 0;
3425 spin_unlock(&mddev->lock);
3426 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003427}
3428
NeilBrownd9d166c2006-01-06 00:20:51 -08003429static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003430level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003431{
Dan Williamsf2859af2010-05-02 10:04:16 -07003432 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003433 ssize_t rv;
3434 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003435 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003436 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003437 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003438 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003439
NeilBrown67918752014-12-15 12:57:01 +11003440 if (slen == 0 || slen >= sizeof(clevel))
3441 return -EINVAL;
3442
3443 rv = mddev_lock(mddev);
3444 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003445 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003446
3447 if (mddev->pers == NULL) {
3448 strncpy(mddev->clevel, buf, slen);
3449 if (mddev->clevel[slen-1] == '\n')
3450 slen--;
3451 mddev->clevel[slen] = 0;
3452 mddev->level = LEVEL_NONE;
3453 rv = len;
3454 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003455 }
NeilBrown67918752014-12-15 12:57:01 +11003456 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003457 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003458 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003459
3460 /* request to change the personality. Need to ensure:
3461 * - array is not engaged in resync/recovery/reshape
3462 * - old personality can be suspended
3463 * - new personality will access other array.
3464 */
3465
NeilBrown67918752014-12-15 12:57:01 +11003466 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003467 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003468 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003469 mddev->reshape_position != MaxSector ||
3470 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003471 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003472
NeilBrown67918752014-12-15 12:57:01 +11003473 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003474 if (!mddev->pers->quiesce) {
3475 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3476 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003477 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003478 }
3479
3480 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003481 strncpy(clevel, buf, slen);
3482 if (clevel[slen-1] == '\n')
3483 slen--;
3484 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003485 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003486 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003487
Dan Williamsf2859af2010-05-02 10:04:16 -07003488 if (request_module("md-%s", clevel) != 0)
3489 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003490 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003491 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003492 if (!pers || !try_module_get(pers->owner)) {
3493 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003494 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003495 rv = -EINVAL;
3496 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003497 }
3498 spin_unlock(&pers_lock);
3499
3500 if (pers == mddev->pers) {
3501 /* Nothing to do! */
3502 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003503 rv = len;
3504 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003505 }
3506 if (!pers->takeover) {
3507 module_put(pers->owner);
3508 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003509 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003510 rv = -EINVAL;
3511 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003512 }
3513
NeilBrowndafb20f2012-03-19 12:46:39 +11003514 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003515 rdev->new_raid_disk = rdev->raid_disk;
3516
NeilBrown245f46c2009-03-31 14:39:39 +11003517 /* ->takeover must set new_* and/or delta_disks
3518 * if it succeeds, and may set them when it fails.
3519 */
3520 priv = pers->takeover(mddev);
3521 if (IS_ERR(priv)) {
3522 mddev->new_level = mddev->level;
3523 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003524 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003525 mddev->raid_disks -= mddev->delta_disks;
3526 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003527 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003528 module_put(pers->owner);
3529 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003530 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003531 rv = PTR_ERR(priv);
3532 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003533 }
3534
3535 /* Looks like we have a winner */
3536 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003537 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003538
3539 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003540 oldpers = mddev->pers;
3541 oldpriv = mddev->private;
3542 mddev->pers = pers;
3543 mddev->private = priv;
3544 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3545 mddev->level = mddev->new_level;
3546 mddev->layout = mddev->new_layout;
3547 mddev->chunk_sectors = mddev->new_chunk_sectors;
3548 mddev->delta_disks = 0;
3549 mddev->reshape_backwards = 0;
3550 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003551 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003552
NeilBrowndb721d32014-12-15 12:56:58 +11003553 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003554 mddev->external) {
3555 /* We are converting from a no-redundancy array
3556 * to a redundancy array and metadata is managed
3557 * externally so we need to be sure that writes
3558 * won't block due to a need to transition
3559 * clean->dirty
3560 * until external management is started.
3561 */
3562 mddev->in_sync = 0;
3563 mddev->safemode_delay = 0;
3564 mddev->safemode = 0;
3565 }
3566
NeilBrowndb721d32014-12-15 12:56:58 +11003567 oldpers->free(mddev, oldpriv);
3568
3569 if (oldpers->sync_request == NULL &&
3570 pers->sync_request != NULL) {
3571 /* need to add the md_redundancy_group */
3572 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3573 printk(KERN_WARNING
3574 "md: cannot register extra attributes for %s\n",
3575 mdname(mddev));
3576 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3577 }
3578 if (oldpers->sync_request != NULL &&
3579 pers->sync_request == NULL) {
3580 /* need to remove the md_redundancy_group */
3581 if (mddev->to_remove == NULL)
3582 mddev->to_remove = &md_redundancy_group;
3583 }
3584
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003585 module_put(oldpers->owner);
3586
NeilBrowndafb20f2012-03-19 12:46:39 +11003587 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003588 if (rdev->raid_disk < 0)
3589 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003590 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003591 rdev->new_raid_disk = -1;
3592 if (rdev->new_raid_disk == rdev->raid_disk)
3593 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003594 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003595 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003596 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003597 if (rdev->raid_disk < 0)
3598 continue;
3599 if (rdev->new_raid_disk == rdev->raid_disk)
3600 continue;
3601 rdev->raid_disk = rdev->new_raid_disk;
3602 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003603 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003604 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003605 if (sysfs_link_rdev(mddev, rdev))
3606 printk(KERN_WARNING "md: cannot register rd%d"
3607 " for %s after level change\n",
3608 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003609 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003610 }
3611
NeilBrowndb721d32014-12-15 12:56:58 +11003612 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003613 /* this is now an array without redundancy, so
3614 * it must always be in_sync
3615 */
3616 mddev->in_sync = 1;
3617 del_timer_sync(&mddev->safemode_timer);
3618 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003619 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003620 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003621 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003622 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003623 if (!mddev->thread)
3624 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003625 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003626 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003627 rv = len;
3628out_unlock:
3629 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003630 return rv;
3631}
3632
3633static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003634__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003635
NeilBrownd4dbd022006-06-26 00:27:59 -07003636static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003637layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003638{
3639 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003640 if (mddev->reshape_position != MaxSector &&
3641 mddev->layout != mddev->new_layout)
3642 return sprintf(page, "%d (%d)\n",
3643 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003644 return sprintf(page, "%d\n", mddev->layout);
3645}
3646
3647static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003648layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003649{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003650 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003651 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003652
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003653 err = kstrtouint(buf, 10, &n);
3654 if (err < 0)
3655 return err;
NeilBrown67918752014-12-15 12:57:01 +11003656 err = mddev_lock(mddev);
3657 if (err)
3658 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003659
NeilBrownb3546032009-03-31 14:56:41 +11003660 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003661 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003662 err = -EBUSY;
3663 else if (mddev->ro)
3664 err = -EROFS;
3665 else {
3666 mddev->new_layout = n;
3667 err = mddev->pers->check_reshape(mddev);
3668 if (err)
3669 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003670 }
NeilBrownb3546032009-03-31 14:56:41 +11003671 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003672 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003673 if (mddev->reshape_position == MaxSector)
3674 mddev->layout = n;
3675 }
NeilBrown67918752014-12-15 12:57:01 +11003676 mddev_unlock(mddev);
3677 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003678}
3679static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003680__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003681
NeilBrowneae17012005-11-08 21:39:23 -08003682static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003683raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003684{
NeilBrownbb636542005-11-08 21:39:45 -08003685 if (mddev->raid_disks == 0)
3686 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003687 if (mddev->reshape_position != MaxSector &&
3688 mddev->delta_disks != 0)
3689 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3690 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003691 return sprintf(page, "%d\n", mddev->raid_disks);
3692}
3693
NeilBrownfd01b882011-10-11 16:47:53 +11003694static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003695
3696static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003697raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003698{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003699 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003700 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003701
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003702 err = kstrtouint(buf, 10, &n);
3703 if (err < 0)
3704 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003705
NeilBrown67918752014-12-15 12:57:01 +11003706 err = mddev_lock(mddev);
3707 if (err)
3708 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003709 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003710 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003711 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003712 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003713 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003714
NeilBrown67918752014-12-15 12:57:01 +11003715 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003716 rdev_for_each(rdev, mddev) {
3717 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 if (olddisks > n &&
3721 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003722 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003723 }
NeilBrown67918752014-12-15 12:57:01 +11003724 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003725 mddev->delta_disks = n - olddisks;
3726 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003727 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003728 } else
NeilBrownda943b992006-01-06 00:20:54 -08003729 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003730out_unlock:
3731 mddev_unlock(mddev);
3732 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003733}
3734static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003735__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003736
NeilBrown24dd4692005-11-08 21:39:26 -08003737static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003738chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003739{
NeilBrown08a02ec2007-05-09 02:35:38 -07003740 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003741 mddev->chunk_sectors != mddev->new_chunk_sectors)
3742 return sprintf(page, "%d (%d)\n",
3743 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003744 mddev->chunk_sectors << 9);
3745 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003746}
3747
3748static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003749chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003750{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003751 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003752 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003753
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003754 err = kstrtoul(buf, 10, &n);
3755 if (err < 0)
3756 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003757
NeilBrown67918752014-12-15 12:57:01 +11003758 err = mddev_lock(mddev);
3759 if (err)
3760 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003761 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003762 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003763 err = -EBUSY;
3764 else if (mddev->ro)
3765 err = -EROFS;
3766 else {
3767 mddev->new_chunk_sectors = n >> 9;
3768 err = mddev->pers->check_reshape(mddev);
3769 if (err)
3770 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003771 }
NeilBrownb3546032009-03-31 14:56:41 +11003772 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003773 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003774 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003775 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003776 }
NeilBrown67918752014-12-15 12:57:01 +11003777 mddev_unlock(mddev);
3778 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003779}
3780static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003781__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003782
NeilBrowna94213b2006-06-26 00:28:00 -07003783static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003784resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003785{
NeilBrownd1a7c502009-03-31 15:24:32 +11003786 if (mddev->recovery_cp == MaxSector)
3787 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003788 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3789}
3790
3791static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003792resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003793{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003794 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003795 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003796
3797 if (cmd_match(buf, "none"))
3798 n = MaxSector;
3799 else {
3800 err = kstrtoull(buf, 10, &n);
3801 if (err < 0)
3802 return err;
3803 if (n != (sector_t)n)
3804 return -EINVAL;
3805 }
NeilBrowna94213b2006-06-26 00:28:00 -07003806
NeilBrown67918752014-12-15 12:57:01 +11003807 err = mddev_lock(mddev);
3808 if (err)
3809 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003810 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003811 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003812
NeilBrown67918752014-12-15 12:57:01 +11003813 if (!err) {
3814 mddev->recovery_cp = n;
3815 if (mddev->pers)
3816 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3817 }
3818 mddev_unlock(mddev);
3819 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003820}
3821static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003822__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3823 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003824
NeilBrown9e653b62006-06-26 00:27:58 -07003825/*
3826 * The array state can be:
3827 *
3828 * clear
3829 * No devices, no size, no level
3830 * Equivalent to STOP_ARRAY ioctl
3831 * inactive
3832 * May have some settings, but array is not active
3833 * all IO results in error
3834 * When written, doesn't tear down array, but just stops it
3835 * suspended (not supported yet)
3836 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003837 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003838 * readonly
3839 * no resync can happen. no superblocks get written.
3840 * write requests fail
3841 * read-auto
3842 * like readonly, but behaves like 'clean' on a write request.
3843 *
3844 * clean - no pending writes, but otherwise active.
3845 * When written to inactive array, starts without resync
3846 * If a write request arrives then
3847 * if metadata is known, mark 'dirty' and switch to 'active'.
3848 * if not known, block and switch to write-pending
3849 * If written to an active array that has pending writes, then fails.
3850 * active
3851 * fully active: IO and resync can be happening.
3852 * When written to inactive array, starts with resync
3853 *
3854 * write-pending
3855 * clean, but writes are blocked waiting for 'active' to be written.
3856 *
3857 * active-idle
3858 * like active, but no writes have been seen for a while (100msec).
3859 *
3860 */
3861enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3862 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003863static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003864 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3865 "write-pending", "active-idle", NULL };
3866
3867static int match_word(const char *word, char **list)
3868{
3869 int n;
3870 for (n=0; list[n]; n++)
3871 if (cmd_match(word, list[n]))
3872 break;
3873 return n;
3874}
3875
3876static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003877array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003878{
3879 enum array_state st = inactive;
3880
3881 if (mddev->pers)
3882 switch(mddev->ro) {
3883 case 1:
3884 st = readonly;
3885 break;
3886 case 2:
3887 st = read_auto;
3888 break;
3889 case 0:
3890 if (mddev->in_sync)
3891 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003892 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003893 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003894 else if (mddev->safemode)
3895 st = active_idle;
3896 else
3897 st = active;
3898 }
3899 else {
3900 if (list_empty(&mddev->disks) &&
3901 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003902 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003903 st = clear;
3904 else
3905 st = inactive;
3906 }
3907 return sprintf(page, "%s\n", array_states[st]);
3908}
3909
NeilBrownf72ffdd2014-09-30 14:23:59 +10003910static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3911static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3912static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003913static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003914
3915static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003916array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003917{
NeilBrown67918752014-12-15 12:57:01 +11003918 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003919 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003920
3921 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3922 /* don't take reconfig_mutex when toggling between
3923 * clean and active
3924 */
3925 spin_lock(&mddev->lock);
3926 if (st == active) {
3927 restart_array(mddev);
3928 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3929 wake_up(&mddev->sb_wait);
3930 err = 0;
3931 } else /* st == clean */ {
3932 restart_array(mddev);
3933 if (atomic_read(&mddev->writes_pending) == 0) {
3934 if (mddev->in_sync == 0) {
3935 mddev->in_sync = 1;
3936 if (mddev->safemode == 1)
3937 mddev->safemode = 0;
3938 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3939 }
3940 err = 0;
3941 } else
3942 err = -EBUSY;
3943 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02003944 if (!err)
3945 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11003946 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003947 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003948 }
3949 err = mddev_lock(mddev);
3950 if (err)
3951 return err;
3952 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003953 switch(st) {
3954 case bad_word:
3955 break;
3956 case clear:
3957 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003958 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003959 break;
3960 case inactive:
3961 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003962 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003963 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003964 else
NeilBrowne6910632008-02-06 01:39:51 -08003965 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003966 break;
3967 case suspended:
3968 break; /* not supported yet */
3969 case readonly:
3970 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003971 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003972 else {
3973 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003974 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003975 err = do_md_run(mddev);
3976 }
3977 break;
3978 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003979 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003980 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003981 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003982 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003983 err = restart_array(mddev);
3984 if (err == 0) {
3985 mddev->ro = 2;
3986 set_disk_ro(mddev->gendisk, 0);
3987 }
NeilBrown9e653b62006-06-26 00:27:58 -07003988 } else {
3989 mddev->ro = 2;
3990 err = do_md_run(mddev);
3991 }
3992 break;
3993 case clean:
3994 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003995 err = restart_array(mddev);
3996 if (err)
3997 break;
NeilBrown85572d72014-12-15 12:56:56 +11003998 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003999 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08004000 if (mddev->in_sync == 0) {
4001 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07004002 if (mddev->safemode == 1)
4003 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10004004 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08004005 }
4006 err = 0;
4007 } else
4008 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004009 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004010 } else
4011 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004012 break;
4013 case active:
4014 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004015 err = restart_array(mddev);
4016 if (err)
4017 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10004018 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004019 wake_up(&mddev->sb_wait);
4020 err = 0;
4021 } else {
4022 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004023 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004024 err = do_md_run(mddev);
4025 }
4026 break;
4027 case write_pending:
4028 case active_idle:
4029 /* these cannot be set */
4030 break;
4031 }
NeilBrown67918752014-12-15 12:57:01 +11004032
4033 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004034 if (mddev->hold_active == UNTIL_IOCTL)
4035 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004036 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004037 }
NeilBrown67918752014-12-15 12:57:01 +11004038 mddev_unlock(mddev);
4039 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004040}
NeilBrown80ca3a42006-07-10 04:44:18 -07004041static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004042__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004043
NeilBrown6d7ff7382006-01-06 00:21:16 -08004044static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004045max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004046 return sprintf(page, "%d\n",
4047 atomic_read(&mddev->max_corr_read_errors));
4048}
4049
4050static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004051max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004052{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004053 unsigned int n;
4054 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004055
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004056 rv = kstrtouint(buf, 10, &n);
4057 if (rv < 0)
4058 return rv;
4059 atomic_set(&mddev->max_corr_read_errors, n);
4060 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004061}
4062
4063static struct md_sysfs_entry max_corr_read_errors =
4064__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4065 max_corrected_read_errors_store);
4066
4067static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004068null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004069{
4070 return -EINVAL;
4071}
4072
4073static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004074new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004075{
4076 /* buf must be %d:%d\n? giving major and minor numbers */
4077 /* The new device is added to the array.
4078 * If the array has a persistent superblock, we read the
4079 * superblock to initialise info and check validity.
4080 * Otherwise, only checking done is that in bind_rdev_to_array,
4081 * which mainly checks size.
4082 */
4083 char *e;
4084 int major = simple_strtoul(buf, &e, 10);
4085 int minor;
4086 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004087 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004088 int err;
4089
4090 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4091 return -EINVAL;
4092 minor = simple_strtoul(e+1, &e, 10);
4093 if (*e && *e != '\n')
4094 return -EINVAL;
4095 dev = MKDEV(major, minor);
4096 if (major != MAJOR(dev) ||
4097 minor != MINOR(dev))
4098 return -EOVERFLOW;
4099
NeilBrown67918752014-12-15 12:57:01 +11004100 flush_workqueue(md_misc_wq);
4101
4102 err = mddev_lock(mddev);
4103 if (err)
4104 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004105 if (mddev->persistent) {
4106 rdev = md_import_device(dev, mddev->major_version,
4107 mddev->minor_version);
4108 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004109 struct md_rdev *rdev0
4110 = list_entry(mddev->disks.next,
4111 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004112 err = super_types[mddev->major_version]
4113 .load_super(rdev, rdev0, mddev->minor_version);
4114 if (err < 0)
4115 goto out;
4116 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004117 } else if (mddev->external)
4118 rdev = md_import_device(dev, -2, -1);
4119 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004120 rdev = md_import_device(dev, -1, -1);
4121
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004122 if (IS_ERR(rdev)) {
4123 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004124 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004125 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004126 err = bind_rdev_to_array(rdev, mddev);
4127 out:
4128 if (err)
4129 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004130 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004131 return err ? err : len;
4132}
4133
4134static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004135__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004136
4137static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004138bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004139{
4140 char *end;
4141 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004142 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004143
NeilBrown67918752014-12-15 12:57:01 +11004144 err = mddev_lock(mddev);
4145 if (err)
4146 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004147 if (!mddev->bitmap)
4148 goto out;
4149 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4150 while (*buf) {
4151 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4152 if (buf == end) break;
4153 if (*end == '-') { /* range */
4154 buf = end + 1;
4155 end_chunk = simple_strtoul(buf, &end, 0);
4156 if (buf == end) break;
4157 }
4158 if (*end && !isspace(*end)) break;
4159 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004160 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004161 }
4162 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4163out:
NeilBrown67918752014-12-15 12:57:01 +11004164 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004165 return len;
4166}
4167
4168static struct md_sysfs_entry md_bitmap =
4169__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4170
4171static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004172size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004173{
Andre Noll58c0fed2009-03-31 14:33:13 +11004174 return sprintf(page, "%llu\n",
4175 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004176}
4177
NeilBrownfd01b882011-10-11 16:47:53 +11004178static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004179
4180static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004181size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004182{
4183 /* If array is inactive, we can reduce the component size, but
4184 * not increase it (except from 0).
4185 * If array is active, we can try an on-line resize
4186 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004187 sector_t sectors;
4188 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004189
Andre Noll58c0fed2009-03-31 14:33:13 +11004190 if (err < 0)
4191 return err;
NeilBrown67918752014-12-15 12:57:01 +11004192 err = mddev_lock(mddev);
4193 if (err)
4194 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004195 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004196 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004197 if (err == 0)
4198 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004199 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004200 if (mddev->dev_sectors == 0 ||
4201 mddev->dev_sectors > sectors)
4202 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004203 else
4204 err = -ENOSPC;
4205 }
NeilBrown67918752014-12-15 12:57:01 +11004206 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004207 return err ? err : len;
4208}
4209
4210static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004211__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004212
Masanari Iida83f0d772012-10-30 00:18:08 +09004213/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004214 * This is one of
4215 * 'none' for arrays with no metadata (good luck...)
4216 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004217 * or N.M for internally known formats
4218 */
4219static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004220metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004221{
4222 if (mddev->persistent)
4223 return sprintf(page, "%d.%d\n",
4224 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004225 else if (mddev->external)
4226 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004227 else
4228 return sprintf(page, "none\n");
4229}
4230
4231static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004232metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004233{
4234 int major, minor;
4235 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004236 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004237 /* Changing the details of 'external' metadata is
4238 * always permitted. Otherwise there must be
4239 * no devices attached to the array.
4240 */
NeilBrown67918752014-12-15 12:57:01 +11004241
4242 err = mddev_lock(mddev);
4243 if (err)
4244 return err;
4245 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004246 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4247 ;
4248 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004249 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004250
NeilBrown67918752014-12-15 12:57:01 +11004251 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004252 if (cmd_match(buf, "none")) {
4253 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004254 mddev->external = 0;
4255 mddev->major_version = 0;
4256 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004257 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004258 }
4259 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004260 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004261 if (namelen >= sizeof(mddev->metadata_type))
4262 namelen = sizeof(mddev->metadata_type)-1;
4263 strncpy(mddev->metadata_type, buf+9, namelen);
4264 mddev->metadata_type[namelen] = 0;
4265 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4266 mddev->metadata_type[--namelen] = 0;
4267 mddev->persistent = 0;
4268 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004269 mddev->major_version = 0;
4270 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004271 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004272 }
4273 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004274 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004275 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004276 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004277 buf = e+1;
4278 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004279 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004280 goto out_unlock;
4281 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004282 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004283 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004284 mddev->major_version = major;
4285 mddev->minor_version = minor;
4286 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004287 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004288 err = 0;
4289out_unlock:
4290 mddev_unlock(mddev);
4291 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004292}
4293
4294static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004295__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004296
NeilBrowna35b0d62006-01-06 00:20:49 -08004297static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004298action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004299{
NeilBrown7eec3142005-11-08 21:39:44 -08004300 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004301 unsigned long recovery = mddev->recovery;
4302 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004303 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004304 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4305 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4306 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004307 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004308 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4309 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004310 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004311 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004312 type = "check";
4313 else
4314 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004315 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004316 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004317 else if (mddev->reshape_position != MaxSector)
4318 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004319 }
4320 return sprintf(page, "%s\n", type);
4321}
4322
4323static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004324action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004325{
NeilBrown7eec3142005-11-08 21:39:44 -08004326 if (!mddev->pers || !mddev->pers->sync_request)
4327 return -EINVAL;
4328
NeilBrownb6a9ce62009-05-26 09:41:17 +10004329
4330 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004331 if (cmd_match(page, "frozen"))
4332 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4333 else
4334 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004335 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4336 mddev_lock(mddev) == 0) {
4337 flush_workqueue(md_misc_wq);
4338 if (mddev->sync_thread) {
4339 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004340 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004341 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004342 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004343 }
NeilBrown312045e2015-12-21 11:01:21 +11004344 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004345 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004346 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004347 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004348 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004349 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004350 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004351 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004352 int err;
4353 if (mddev->pers->start_reshape == NULL)
4354 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004355 err = mddev_lock(mddev);
4356 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004357 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4358 err = -EBUSY;
4359 else {
4360 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4361 err = mddev->pers->start_reshape(mddev);
4362 }
NeilBrown67918752014-12-15 12:57:01 +11004363 mddev_unlock(mddev);
4364 }
NeilBrown16484bf2006-03-27 01:18:13 -08004365 if (err)
4366 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004367 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004368 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004369 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004370 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004371 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004372 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004373 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004374 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4375 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004376 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004377 if (mddev->ro == 2) {
4378 /* A write to sync_action is enough to justify
4379 * canceling read-auto mode
4380 */
4381 mddev->ro = 0;
4382 md_wakeup_thread(mddev->sync_thread);
4383 }
NeilBrown03c902e2006-01-06 00:20:46 -08004384 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004385 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004386 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004387 return len;
4388}
4389
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004390static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004391__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004392
4393static ssize_t
4394last_sync_action_show(struct mddev *mddev, char *page)
4395{
4396 return sprintf(page, "%s\n", mddev->last_sync_action);
4397}
4398
4399static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4400
NeilBrown9d888832005-11-08 21:39:26 -08004401static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004402mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004403{
4404 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004405 (unsigned long long)
4406 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004407}
4408
NeilBrown80ca3a42006-07-10 04:44:18 -07004409static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004410
NeilBrown88202a02006-01-06 00:21:36 -08004411static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004412sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004413{
4414 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4415 mddev->sync_speed_min ? "local": "system");
4416}
4417
4418static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004419sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004420{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004421 unsigned int min;
4422 int rv;
4423
NeilBrown88202a02006-01-06 00:21:36 -08004424 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004425 min = 0;
4426 } else {
4427 rv = kstrtouint(buf, 10, &min);
4428 if (rv < 0)
4429 return rv;
4430 if (min == 0)
4431 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004432 }
NeilBrown88202a02006-01-06 00:21:36 -08004433 mddev->sync_speed_min = min;
4434 return len;
4435}
4436
4437static struct md_sysfs_entry md_sync_min =
4438__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4439
4440static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004441sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004442{
4443 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4444 mddev->sync_speed_max ? "local": "system");
4445}
4446
4447static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004448sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004449{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004450 unsigned int max;
4451 int rv;
4452
NeilBrown88202a02006-01-06 00:21:36 -08004453 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004454 max = 0;
4455 } else {
4456 rv = kstrtouint(buf, 10, &max);
4457 if (rv < 0)
4458 return rv;
4459 if (max == 0)
4460 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004461 }
NeilBrown88202a02006-01-06 00:21:36 -08004462 mddev->sync_speed_max = max;
4463 return len;
4464}
4465
4466static struct md_sysfs_entry md_sync_max =
4467__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4468
Iustin Popd7f3d292007-10-16 23:30:54 -07004469static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004470degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004471{
4472 return sprintf(page, "%d\n", mddev->degraded);
4473}
4474static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004475
4476static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004477sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004478{
4479 return sprintf(page, "%d\n", mddev->parallel_resync);
4480}
4481
4482static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004483sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004484{
4485 long n;
4486
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004487 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004488 return -EINVAL;
4489
4490 if (n != 0 && n != 1)
4491 return -EINVAL;
4492
4493 mddev->parallel_resync = n;
4494
4495 if (mddev->sync_thread)
4496 wake_up(&resync_wait);
4497
4498 return len;
4499}
4500
4501/* force parallel resync, even with shared block devices */
4502static struct md_sysfs_entry md_sync_force_parallel =
4503__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4504 sync_force_parallel_show, sync_force_parallel_store);
4505
4506static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004507sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004508{
4509 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004510 if (mddev->curr_resync == 0)
4511 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004512 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4513 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004514 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004515 db = resync - mddev->resync_mark_cnt;
4516 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004517}
4518
NeilBrown80ca3a42006-07-10 04:44:18 -07004519static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004520
4521static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004522sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004523{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004524 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004525
NeilBrownacb180b2009-04-14 16:28:34 +10004526 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4527 return sprintf(page, "none\n");
4528
NeilBrown72f36d52012-10-11 14:25:57 +11004529 if (mddev->curr_resync == 1 ||
4530 mddev->curr_resync == 2)
4531 return sprintf(page, "delayed\n");
4532
NeilBrownc804cde2012-05-21 09:28:33 +10004533 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4534 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004535 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004536 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004537 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004538
NeilBrownacb180b2009-04-14 16:28:34 +10004539 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004540 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004541}
4542
NeilBrown750f1992014-09-30 08:53:05 +10004543static struct md_sysfs_entry md_sync_completed =
4544 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004545
NeilBrowne464eaf2006-03-27 01:18:14 -08004546static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004547min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004548{
4549 return sprintf(page, "%llu\n",
4550 (unsigned long long)mddev->resync_min);
4551}
4552static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004553min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004554{
4555 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004556 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004557
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004558 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004559 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004560
4561 spin_lock(&mddev->lock);
4562 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004563 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004564 goto out_unlock;
4565
4566 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004567 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004568 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004569
NeilBrown50c37b12015-03-23 17:36:38 +11004570 /* Round down to multiple of 4K for safety */
4571 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004572 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004573
NeilBrown23da4222014-12-15 12:57:01 +11004574out_unlock:
4575 spin_unlock(&mddev->lock);
4576 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004577}
4578
4579static struct md_sysfs_entry md_min_sync =
4580__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4581
4582static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004583max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004584{
4585 if (mddev->resync_max == MaxSector)
4586 return sprintf(page, "max\n");
4587 else
4588 return sprintf(page, "%llu\n",
4589 (unsigned long long)mddev->resync_max);
4590}
4591static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004592max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004593{
NeilBrown23da4222014-12-15 12:57:01 +11004594 int err;
4595 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004596 if (strncmp(buf, "max", 3) == 0)
4597 mddev->resync_max = MaxSector;
4598 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004599 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004600 int chunk;
4601
4602 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004603 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004604 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004605 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004606 goto out_unlock;
4607
4608 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004609 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004610 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004611 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004612 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004613
4614 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004615 chunk = mddev->chunk_sectors;
4616 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004617 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004618
4619 err = -EINVAL;
4620 if (sector_div(temp, chunk))
4621 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004622 }
4623 mddev->resync_max = max;
4624 }
4625 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004626 err = 0;
4627out_unlock:
4628 spin_unlock(&mddev->lock);
4629 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004630}
4631
4632static struct md_sysfs_entry md_max_sync =
4633__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4634
4635static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004636suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004637{
4638 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4639}
4640
4641static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004642suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004643{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004644 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004645 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004646
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004647 err = kstrtoull(buf, 10, &new);
4648 if (err < 0)
4649 return err;
4650 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004651 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004652
NeilBrown67918752014-12-15 12:57:01 +11004653 err = mddev_lock(mddev);
4654 if (err)
4655 return err;
4656 err = -EINVAL;
4657 if (mddev->pers == NULL ||
4658 mddev->pers->quiesce == NULL)
4659 goto unlock;
4660 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004661 mddev->suspend_lo = new;
4662 if (new >= old)
4663 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004664 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004665 else {
4666 /* Expanding suspended region - need to wait */
4667 mddev->pers->quiesce(mddev, 1);
4668 mddev->pers->quiesce(mddev, 0);
4669 }
NeilBrown67918752014-12-15 12:57:01 +11004670 err = 0;
4671unlock:
4672 mddev_unlock(mddev);
4673 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004674}
4675static struct md_sysfs_entry md_suspend_lo =
4676__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4677
NeilBrowne464eaf2006-03-27 01:18:14 -08004678static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004679suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004680{
4681 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4682}
4683
4684static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004685suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004686{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004687 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004688 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004689
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004690 err = kstrtoull(buf, 10, &new);
4691 if (err < 0)
4692 return err;
4693 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004694 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004695
NeilBrown67918752014-12-15 12:57:01 +11004696 err = mddev_lock(mddev);
4697 if (err)
4698 return err;
4699 err = -EINVAL;
4700 if (mddev->pers == NULL ||
4701 mddev->pers->quiesce == NULL)
4702 goto unlock;
4703 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004704 mddev->suspend_hi = new;
4705 if (new <= old)
4706 /* Shrinking suspended region */
4707 mddev->pers->quiesce(mddev, 2);
4708 else {
4709 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004710 mddev->pers->quiesce(mddev, 1);
4711 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004712 }
NeilBrown67918752014-12-15 12:57:01 +11004713 err = 0;
4714unlock:
4715 mddev_unlock(mddev);
4716 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004717}
4718static struct md_sysfs_entry md_suspend_hi =
4719__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4720
NeilBrown08a02ec2007-05-09 02:35:38 -07004721static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004722reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004723{
4724 if (mddev->reshape_position != MaxSector)
4725 return sprintf(page, "%llu\n",
4726 (unsigned long long)mddev->reshape_position);
4727 strcpy(page, "none\n");
4728 return 5;
4729}
4730
4731static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004732reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004733{
NeilBrownc6563a82012-05-21 09:27:00 +10004734 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004735 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004736 int err;
NeilBrown67918752014-12-15 12:57:01 +11004737
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004738 err = kstrtoull(buf, 10, &new);
4739 if (err < 0)
4740 return err;
4741 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004742 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004743 err = mddev_lock(mddev);
4744 if (err)
4745 return err;
4746 err = -EBUSY;
4747 if (mddev->pers)
4748 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004749 mddev->reshape_position = new;
4750 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004751 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004752 mddev->new_level = mddev->level;
4753 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004754 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004755 rdev_for_each(rdev, mddev)
4756 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004757 err = 0;
4758unlock:
4759 mddev_unlock(mddev);
4760 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004761}
4762
4763static struct md_sysfs_entry md_reshape_position =
4764__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4765 reshape_position_store);
4766
Dan Williamsb522adc2009-03-31 15:00:31 +11004767static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004768reshape_direction_show(struct mddev *mddev, char *page)
4769{
4770 return sprintf(page, "%s\n",
4771 mddev->reshape_backwards ? "backwards" : "forwards");
4772}
4773
4774static ssize_t
4775reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4776{
4777 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004778 int err;
4779
NeilBrown2c810cd2012-05-21 09:27:00 +10004780 if (cmd_match(buf, "forwards"))
4781 backwards = 0;
4782 else if (cmd_match(buf, "backwards"))
4783 backwards = 1;
4784 else
4785 return -EINVAL;
4786 if (mddev->reshape_backwards == backwards)
4787 return len;
4788
NeilBrown67918752014-12-15 12:57:01 +11004789 err = mddev_lock(mddev);
4790 if (err)
4791 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004792 /* check if we are allowed to change */
4793 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004794 err = -EBUSY;
4795 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004796 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004797 err = -EINVAL;
4798 else
4799 mddev->reshape_backwards = backwards;
4800 mddev_unlock(mddev);
4801 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004802}
4803
4804static struct md_sysfs_entry md_reshape_direction =
4805__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4806 reshape_direction_store);
4807
4808static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004809array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004810{
4811 if (mddev->external_size)
4812 return sprintf(page, "%llu\n",
4813 (unsigned long long)mddev->array_sectors/2);
4814 else
4815 return sprintf(page, "default\n");
4816}
4817
4818static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004819array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004820{
4821 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004822 int err;
4823
4824 err = mddev_lock(mddev);
4825 if (err)
4826 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004827
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004828 /* cluster raid doesn't support change array_sectors */
4829 if (mddev_is_clustered(mddev))
4830 return -EINVAL;
4831
Dan Williamsb522adc2009-03-31 15:00:31 +11004832 if (strncmp(buf, "default", 7) == 0) {
4833 if (mddev->pers)
4834 sectors = mddev->pers->size(mddev, 0, 0);
4835 else
4836 sectors = mddev->array_sectors;
4837
4838 mddev->external_size = 0;
4839 } else {
4840 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004841 err = -EINVAL;
4842 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4843 err = -E2BIG;
4844 else
4845 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004846 }
4847
NeilBrown67918752014-12-15 12:57:01 +11004848 if (!err) {
4849 mddev->array_sectors = sectors;
4850 if (mddev->pers) {
4851 set_capacity(mddev->gendisk, mddev->array_sectors);
4852 revalidate_disk(mddev->gendisk);
4853 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004854 }
NeilBrown67918752014-12-15 12:57:01 +11004855 mddev_unlock(mddev);
4856 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004857}
4858
4859static struct md_sysfs_entry md_array_size =
4860__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4861 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004862
NeilBrowneae17012005-11-08 21:39:23 -08004863static struct attribute *md_default_attrs[] = {
4864 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004865 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004866 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004867 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004868 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004869 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004870 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004871 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004872 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004873 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004874 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004875 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004876 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004877 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004878 NULL,
4879};
4880
4881static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004882 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004883 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004884 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004885 &md_sync_min.attr,
4886 &md_sync_max.attr,
4887 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004888 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004889 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004890 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004891 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004892 &md_suspend_lo.attr,
4893 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004894 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004895 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004896 NULL,
4897};
NeilBrown411036f2005-11-08 21:39:40 -08004898static struct attribute_group md_redundancy_group = {
4899 .name = NULL,
4900 .attrs = md_redundancy_attrs,
4901};
4902
NeilBrowneae17012005-11-08 21:39:23 -08004903static ssize_t
4904md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4905{
4906 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004907 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004908 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004909
4910 if (!entry->show)
4911 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004912 spin_lock(&all_mddevs_lock);
4913 if (list_empty(&mddev->all_mddevs)) {
4914 spin_unlock(&all_mddevs_lock);
4915 return -EBUSY;
4916 }
4917 mddev_get(mddev);
4918 spin_unlock(&all_mddevs_lock);
4919
NeilBrownb7b17c92014-12-15 12:56:59 +11004920 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004921 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004922 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004923}
4924
4925static ssize_t
4926md_attr_store(struct kobject *kobj, struct attribute *attr,
4927 const char *page, size_t length)
4928{
4929 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004930 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004931 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004932
4933 if (!entry->store)
4934 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004935 if (!capable(CAP_SYS_ADMIN))
4936 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004937 spin_lock(&all_mddevs_lock);
4938 if (list_empty(&mddev->all_mddevs)) {
4939 spin_unlock(&all_mddevs_lock);
4940 return -EBUSY;
4941 }
4942 mddev_get(mddev);
4943 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004944 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004945 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004946 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004947}
4948
4949static void md_free(struct kobject *ko)
4950{
NeilBrownfd01b882011-10-11 16:47:53 +11004951 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004952
4953 if (mddev->sysfs_state)
4954 sysfs_put(mddev->sysfs_state);
4955
NeilBrown6cd18e72015-04-27 14:12:22 +10004956 if (mddev->queue)
4957 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004958 if (mddev->gendisk) {
4959 del_gendisk(mddev->gendisk);
4960 put_disk(mddev->gendisk);
4961 }
NeilBrowna21d1502009-01-09 08:31:09 +11004962
NeilBrowneae17012005-11-08 21:39:23 -08004963 kfree(mddev);
4964}
4965
Emese Revfy52cf25d2010-01-19 02:58:23 +01004966static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004967 .show = md_attr_show,
4968 .store = md_attr_store,
4969};
4970static struct kobj_type md_ktype = {
4971 .release = md_free,
4972 .sysfs_ops = &md_sysfs_ops,
4973 .default_attrs = md_default_attrs,
4974};
4975
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976int mdp_major = 0;
4977
Dan Williams5fd3a172009-03-04 00:57:25 -07004978static void mddev_delayed_delete(struct work_struct *ws)
4979{
NeilBrownfd01b882011-10-11 16:47:53 +11004980 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004981
NeilBrown43a70502009-12-14 12:49:55 +11004982 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004983 kobject_del(&mddev->kobj);
4984 kobject_put(&mddev->kobj);
4985}
4986
NeilBrownefeb53c2009-01-09 08:31:10 +11004987static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004989 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004990 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004992 int partitioned;
4993 int shift;
4994 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004995 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
4997 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004998 return -ENODEV;
4999
5000 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5001 shift = partitioned ? MdpMinorShift : 0;
5002 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
Tejun Heoe804ac72010-10-15 15:36:08 +02005004 /* wait for any previous instance of this device to be
5005 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005006 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005007 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005008
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005009 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005010 error = -EEXIST;
5011 if (mddev->gendisk)
5012 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005013
5014 if (name) {
5015 /* Need to ensure that 'name' is not a duplicate.
5016 */
NeilBrownfd01b882011-10-11 16:47:53 +11005017 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005018 spin_lock(&all_mddevs_lock);
5019
5020 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5021 if (mddev2->gendisk &&
5022 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5023 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005024 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005025 }
5026 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 }
NeilBrown8b765392009-01-09 08:31:08 +11005028
NeilBrown0909dc42009-07-01 12:27:21 +10005029 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005030 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005031 if (!mddev->queue)
5032 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005033 mddev->queue->queuedata = mddev;
5034
NeilBrown409c57f2009-03-31 14:39:39 +11005035 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005036 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005037
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 disk = alloc_disk(1 << shift);
5039 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005040 blk_cleanup_queue(mddev->queue);
5041 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005042 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005044 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005046 if (name)
5047 strcpy(disk->disk_name, name);
5048 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005050 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 disk->fops = &md_fops;
5053 disk->private_data = mddev;
5054 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005055 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005056 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005057 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005058 * remove it now.
5059 */
5060 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005062 /* As soon as we call add_disk(), another thread could get
5063 * through to md_open, so make sure it doesn't get too far
5064 */
5065 mutex_lock(&mddev->open_mutex);
5066 add_disk(disk);
5067
Tejun Heoed9e1982008-08-25 19:56:05 +09005068 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5069 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005070 if (error) {
5071 /* This isn't possible, but as kobject_init_and_add is marked
5072 * __must_check, we must do something with the result
5073 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005074 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5075 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005076 error = 0;
5077 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005078 if (mddev->kobj.sd &&
5079 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11005080 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005081 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005082 abort:
5083 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005084 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005085 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005086 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005087 }
NeilBrownd3374822009-01-09 08:31:10 +11005088 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005089 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005090}
5091
5092static struct kobject *md_probe(dev_t dev, int *part, void *data)
5093{
5094 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 return NULL;
5096}
5097
NeilBrownefeb53c2009-01-09 08:31:10 +11005098static int add_named_array(const char *val, struct kernel_param *kp)
5099{
5100 /* val must be "md_*" where * is not all digits.
5101 * We allocate an array with a large free minor number, and
5102 * set the name to val. val must not already be an active name.
5103 */
5104 int len = strlen(val);
5105 char buf[DISK_NAME_LEN];
5106
5107 while (len && val[len-1] == '\n')
5108 len--;
5109 if (len >= DISK_NAME_LEN)
5110 return -E2BIG;
5111 strlcpy(buf, val, len+1);
5112 if (strncmp(buf, "md_", 3) != 0)
5113 return -EINVAL;
5114 return md_alloc(0, buf);
5115}
5116
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117static void md_safemode_timeout(unsigned long data)
5118{
NeilBrownfd01b882011-10-11 16:47:53 +11005119 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
Neil Brown0fd62b82008-06-28 08:31:36 +10005121 if (!atomic_read(&mddev->writes_pending)) {
5122 mddev->safemode = 1;
5123 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005124 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 md_wakeup_thread(mddev->thread);
5127}
5128
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005129static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
NeilBrownfd01b882011-10-11 16:47:53 +11005131int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132{
NeilBrown2604b702006-01-06 00:20:36 -08005133 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005134 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005135 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
NeilBrowna757e642005-04-16 15:26:42 -07005137 if (list_empty(&mddev->disks))
5138 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
5141 if (mddev->pers)
5142 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005143 /* Cannot run until previous stop completes properly */
5144 if (mddev->sysfs_active)
5145 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 /*
5148 * Analyze all RAID superblock(s)
5149 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005150 if (!mddev->raid_disks) {
5151 if (!mddev->persistent)
5152 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005153 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
NeilBrownd9d166c2006-01-06 00:20:51 -08005156 if (mddev->level != LEVEL_NONE)
5157 request_module("md-level-%d", mddev->level);
5158 else if (mddev->clevel[0])
5159 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
5161 /*
5162 * Drop all container device buffers, from now on
5163 * the only valid external interface is through the md
5164 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005166 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005167 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 continue;
5169 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005170 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005171
5172 /* perform some consistency tests on the device.
5173 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005174 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005175 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005176 if (rdev->meta_bdev) {
5177 /* Nothing to check */;
5178 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005179 if (mddev->dev_sectors &&
5180 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005181 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005182 printk("md: %s: data overlaps metadata\n",
5183 mdname(mddev));
5184 return -EINVAL;
5185 }
5186 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005187 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005188 > rdev->data_offset) {
5189 printk("md: %s: metadata overlaps data\n",
5190 mdname(mddev));
5191 return -EINVAL;
5192 }
5193 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005194 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 }
5196
NeilBrowna167f662010-10-26 18:31:13 +11005197 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005198 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005199
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005201 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005202 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005204 if (mddev->level != LEVEL_NONE)
5205 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5206 mddev->level);
5207 else
5208 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5209 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 return -EINVAL;
5211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005213 if (mddev->level != pers->level) {
5214 mddev->level = pers->level;
5215 mddev->new_level = pers->level;
5216 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005217 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
NeilBrownf6705572006-03-27 01:18:11 -08005219 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005220 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005221 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005222 module_put(pers->owner);
5223 return -EINVAL;
5224 }
5225
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005226 if (pers->sync_request) {
5227 /* Warn if this is a potentially silly
5228 * configuration.
5229 */
5230 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005231 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005232 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005233
NeilBrowndafb20f2012-03-19 12:46:39 +11005234 rdev_for_each(rdev, mddev)
5235 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005236 if (rdev < rdev2 &&
5237 rdev->bdev->bd_contains ==
5238 rdev2->bdev->bd_contains) {
5239 printk(KERN_WARNING
5240 "%s: WARNING: %s appears to be"
5241 " on the same physical disk as"
5242 " %s.\n",
5243 mdname(mddev),
5244 bdevname(rdev->bdev,b),
5245 bdevname(rdev2->bdev,b2));
5246 warned = 1;
5247 }
5248 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005249
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005250 if (warned)
5251 printk(KERN_WARNING
5252 "True protection against single-disk"
5253 " failure might be compromised.\n");
5254 }
5255
NeilBrown657390d2005-08-26 18:34:16 -07005256 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005257 /* may be over-ridden by personality */
5258 mddev->resync_max_sectors = mddev->dev_sectors;
5259
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005260 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
NeilBrown0f9552b52009-12-30 12:08:50 +11005262 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005263 mddev->ro = 2; /* read-only, but switch on first write */
5264
NeilBrown36d091f2014-12-15 12:56:58 +11005265 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005266 if (err)
5267 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005268 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005269 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5270 " but 'external_size' not in effect?\n", __func__);
5271 printk(KERN_ERR
5272 "md: invalid array_size %llu > default size %llu\n",
5273 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005274 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005275 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005276 }
NeilBrown36d091f2014-12-15 12:56:58 +11005277 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005278 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005279 struct bitmap *bitmap;
5280
5281 bitmap = bitmap_create(mddev, -1);
5282 if (IS_ERR(bitmap)) {
5283 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005284 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5285 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005286 } else
5287 mddev->bitmap = bitmap;
5288
NeilBrownb15c2e52006-01-06 00:20:16 -08005289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005291 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005292 if (mddev->private)
5293 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005294 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005295 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005296 bitmap_destroy(mddev);
5297 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 }
NeilBrown5c675f82014-12-15 12:56:56 +11005299 if (mddev->queue) {
5300 mddev->queue->backing_dev_info.congested_data = mddev;
5301 mddev->queue->backing_dev_info.congested_fn = md_congested;
5302 }
NeilBrown36d091f2014-12-15 12:56:58 +11005303 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005304 if (mddev->kobj.sd &&
5305 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005306 printk(KERN_WARNING
5307 "md: cannot register extra attributes for %s\n",
5308 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005309 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005310 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005311 mddev->ro = 0;
5312
NeilBrownf72ffdd2014-09-30 14:23:59 +10005313 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005314 atomic_set(&mddev->max_corr_read_errors,
5315 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005317 if (mddev_is_clustered(mddev))
5318 mddev->safemode_delay = 0;
5319 else
5320 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005322 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005323 spin_lock(&mddev->lock);
5324 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005325 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005326 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005327 if (rdev->raid_disk >= 0)
5328 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005329 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005330
NeilBrowna4a3d262015-07-17 11:57:30 +10005331 if (mddev->degraded && !mddev->ro)
5332 /* This ensures that recovering status is reported immediately
5333 * via sysfs - until a lack of spares is confirmed.
5334 */
5335 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005337
NeilBrown7a0a5352013-08-27 16:28:23 +10005338 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005339 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
NeilBrownd7603b72006-01-06 00:20:30 -08005341 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005342 sysfs_notify_dirent_safe(mddev->sysfs_state);
5343 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005344 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 return 0;
5346}
NeilBrown390ee602010-06-01 19:37:27 +10005347EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
NeilBrownfd01b882011-10-11 16:47:53 +11005349static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005350{
5351 int err;
5352
5353 err = md_run(mddev);
5354 if (err)
5355 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005356 err = bitmap_load(mddev);
5357 if (err) {
5358 bitmap_destroy(mddev);
5359 goto out;
5360 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005361
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005362 if (mddev_is_clustered(mddev))
5363 md_allow_write(mddev);
5364
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005365 md_wakeup_thread(mddev->thread);
5366 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5367
NeilBrownfe60b012010-03-29 11:10:42 +11005368 set_capacity(mddev->gendisk, mddev->array_sectors);
5369 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005370 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005371 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5372out:
5373 return err;
5374}
5375
NeilBrownfd01b882011-10-11 16:47:53 +11005376static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377{
5378 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
Andre Noll80fab1d2008-07-11 22:02:21 +10005380 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005382 return -ENXIO;
5383 if (!mddev->pers)
5384 return -EINVAL;
5385 if (!mddev->ro)
5386 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005387 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5388 struct md_rdev *rdev;
5389 bool has_journal = false;
5390
5391 rcu_read_lock();
5392 rdev_for_each_rcu(rdev, mddev) {
5393 if (test_bit(Journal, &rdev->flags) &&
5394 !test_bit(Faulty, &rdev->flags)) {
5395 has_journal = true;
5396 break;
5397 }
5398 }
5399 rcu_read_unlock();
5400
5401 /* Don't restart rw with journal missing/faulty */
5402 if (!has_journal)
5403 return -EINVAL;
5404 }
5405
Andre Noll80fab1d2008-07-11 22:02:21 +10005406 mddev->safemode = 0;
5407 mddev->ro = 0;
5408 set_disk_ro(disk, 0);
5409 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5410 mdname(mddev));
5411 /* Kick recovery or resync if necessary */
5412 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5413 md_wakeup_thread(mddev->thread);
5414 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005415 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417}
5418
NeilBrownfd01b882011-10-11 16:47:53 +11005419static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005420{
5421 mddev->array_sectors = 0;
5422 mddev->external_size = 0;
5423 mddev->dev_sectors = 0;
5424 mddev->raid_disks = 0;
5425 mddev->recovery_cp = 0;
5426 mddev->resync_min = 0;
5427 mddev->resync_max = MaxSector;
5428 mddev->reshape_position = MaxSector;
5429 mddev->external = 0;
5430 mddev->persistent = 0;
5431 mddev->level = LEVEL_NONE;
5432 mddev->clevel[0] = 0;
5433 mddev->flags = 0;
5434 mddev->ro = 0;
5435 mddev->metadata_type[0] = 0;
5436 mddev->chunk_sectors = 0;
5437 mddev->ctime = mddev->utime = 0;
5438 mddev->layout = 0;
5439 mddev->max_disks = 0;
5440 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005441 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005442 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005443 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005444 mddev->new_level = LEVEL_NONE;
5445 mddev->new_layout = 0;
5446 mddev->new_chunk_sectors = 0;
5447 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005448 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005449 mddev->suspend_lo = mddev->suspend_hi = 0;
5450 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5451 mddev->recovery = 0;
5452 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005453 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005454 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005455 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005456 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005457 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005458 mddev->bitmap_info.offset = 0;
5459 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005460 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005461 mddev->bitmap_info.chunksize = 0;
5462 mddev->bitmap_info.daemon_sleep = 0;
5463 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005464 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005465}
5466
NeilBrownfd01b882011-10-11 16:47:53 +11005467static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005468{
NeilBrown6b6204e2013-05-09 09:48:30 +10005469 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005470 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005471 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005472 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005473 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005474 }
5475
5476 del_timer_sync(&mddev->safemode_timer);
5477
5478 bitmap_flush(mddev);
5479 md_super_wait(mddev);
5480
NeilBrownb6d428c2013-04-24 11:42:42 +10005481 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005482 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5483 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005484 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005485 if (!mddev_is_clustered(mddev))
5486 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005487 md_update_sb(mddev, 1);
5488 }
5489}
NeilBrowndefad612011-01-14 09:14:33 +11005490
NeilBrownfd01b882011-10-11 16:47:53 +11005491void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005492{
NeilBrown29f097c2013-11-14 17:54:51 +11005493 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005494 __md_stop_writes(mddev);
5495 mddev_unlock(mddev);
5496}
NeilBrown390ee602010-06-01 19:37:27 +10005497EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005498
NeilBrown5aa61f42014-12-15 12:56:57 +11005499static void mddev_detach(struct mddev *mddev)
5500{
5501 struct bitmap *bitmap = mddev->bitmap;
5502 /* wait for behind writes to complete */
5503 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5504 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5505 mdname(mddev));
5506 /* need to kick something here to make sure I/O goes? */
5507 wait_event(bitmap->behind_wait,
5508 atomic_read(&bitmap->behind_writes) == 0);
5509 }
NeilBrown36d091f2014-12-15 12:56:58 +11005510 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005511 mddev->pers->quiesce(mddev, 1);
5512 mddev->pers->quiesce(mddev, 0);
5513 }
5514 md_unregister_thread(&mddev->thread);
5515 if (mddev->queue)
5516 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5517}
5518
NeilBrown5eff3c42012-11-19 10:47:48 +11005519static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005520{
NeilBrown36d091f2014-12-15 12:56:58 +11005521 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005522 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005523 /* Ensure ->event_work is done */
5524 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005525 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005526 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005527 spin_unlock(&mddev->lock);
5528 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005529 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005530 if (pers->sync_request && mddev->to_remove == NULL)
5531 mddev->to_remove = &md_redundancy_group;
5532 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005533 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005534}
NeilBrown5eff3c42012-11-19 10:47:48 +11005535
5536void md_stop(struct mddev *mddev)
5537{
5538 /* stop the array and free an attached data structures.
5539 * This is called from dm-raid
5540 */
5541 __md_stop(mddev);
5542 bitmap_destroy(mddev);
5543 if (mddev->bio_set)
5544 bioset_free(mddev->bio_set);
5545}
5546
NeilBrown390ee602010-06-01 19:37:27 +10005547EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005548
NeilBrowna05b7ea2012-07-19 15:59:18 +10005549static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005550{
5551 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005552 int did_freeze = 0;
5553
5554 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5555 did_freeze = 1;
5556 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5557 md_wakeup_thread(mddev->thread);
5558 }
NeilBrownf851b602014-12-11 10:02:10 +11005559 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005560 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005561 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005562 /* Thread might be blocked waiting for metadata update
5563 * which will now never happen */
5564 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005565
NeilBrown88724bf2015-09-24 14:00:51 +10005566 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5567 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005568 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005569 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5570 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005571 wait_event(mddev->sb_wait,
5572 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005573 mddev_lock_nointr(mddev);
5574
NeilBrowna4bd82d2010-03-29 13:23:10 +11005575 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005576 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005577 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005578 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005579 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005580 if (did_freeze) {
5581 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005582 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005583 md_wakeup_thread(mddev->thread);
5584 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005585 err = -EBUSY;
5586 goto out;
5587 }
5588 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005589 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005590
5591 err = -ENXIO;
5592 if (mddev->ro==1)
5593 goto out;
5594 mddev->ro = 1;
5595 set_disk_ro(mddev->gendisk, 1);
5596 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005597 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5598 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005599 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005600 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005601 }
5602out:
5603 mutex_unlock(&mddev->open_mutex);
5604 return err;
5605}
5606
NeilBrown9e653b62006-06-26 00:27:58 -07005607/* mode:
5608 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005609 * 2 - stop but do not disassemble array
5610 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005611static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005612 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005615 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005616 int did_freeze = 0;
5617
5618 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5619 did_freeze = 1;
5620 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5621 md_wakeup_thread(mddev->thread);
5622 }
NeilBrownf851b602014-12-11 10:02:10 +11005623 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005624 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005625 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005626 /* Thread might be blocked waiting for metadata update
5627 * which will now never happen */
5628 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005629
NeilBrown30b8feb2013-11-14 15:16:17 +11005630 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005631 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5632 !test_bit(MD_RECOVERY_RUNNING,
5633 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005634 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
NeilBrownc8c00a62009-08-10 12:50:52 +10005636 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005637 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005638 mddev->sysfs_active ||
5639 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08005640 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005641 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005642 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005643 if (did_freeze) {
5644 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005645 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005646 md_wakeup_thread(mddev->thread);
5647 }
NeilBrown260fa032013-08-27 16:44:13 +10005648 return -EBUSY;
5649 }
NeilBrown6e17b022010-08-07 21:41:19 +10005650 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005651 if (mddev->ro)
5652 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005653
NeilBrowndefad612011-01-14 09:14:33 +11005654 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005655 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005656 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005657
NeilBrowna4bd82d2010-03-29 13:23:10 +11005658 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005659 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005660
NeilBrowndafb20f2012-03-19 12:46:39 +11005661 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005662 if (rdev->raid_disk >= 0)
5663 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005664
NeilBrowna4bd82d2010-03-29 13:23:10 +11005665 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005666 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005667 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005668 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005669
NeilBrowna4bd82d2010-03-29 13:23:10 +11005670 if (mddev->ro)
5671 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005672 } else
5673 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 /*
5675 * Free resources if final stop
5676 */
NeilBrown9e653b62006-06-26 00:27:58 -07005677 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5679
NeilBrown978f9462006-02-02 14:28:05 -08005680 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005681 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005682 struct file *f = mddev->bitmap_info.file;
5683 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005684 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005685 spin_unlock(&mddev->lock);
5686 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005687 }
NeilBrownc3d97142009-12-14 12:49:52 +11005688 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005689
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 export_array(mddev);
5691
NeilBrown6177b472010-03-29 11:37:13 +11005692 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005693 if (mddev->hold_active == UNTIL_STOP)
5694 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005695 }
NeilBrownd7603b72006-01-06 00:20:30 -08005696 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005697 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699}
5700
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005701#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005702static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703{
NeilBrown3cb03002011-10-11 16:45:26 +11005704 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 int err;
5706
NeilBrowna757e642005-04-16 15:26:42 -07005707 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
5710 printk(KERN_INFO "md: running: ");
5711
NeilBrowndafb20f2012-03-19 12:46:39 +11005712 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 char b[BDEVNAME_SIZE];
5714 printk("<%s>", bdevname(rdev->bdev,b));
5715 }
5716 printk("\n");
5717
NeilBrownd710e132008-10-13 11:55:12 +11005718 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 if (err) {
5720 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005721 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 }
5723}
5724
5725/*
5726 * lets try to run arrays based on all disks that have arrived
5727 * until now. (those are in pending_raid_disks)
5728 *
5729 * the method: pick the first pending disk, collect all disks with
5730 * the same UUID, remove all from the pending list and put them into
5731 * the 'same_array' list. Then order this list based on superblock
5732 * update time (freshest comes first), kick out 'old' disks and
5733 * compare superblocks. If everything's fine then run it.
5734 *
5735 * If "unit" is allocated, then bump its reference count
5736 */
5737static void autorun_devices(int part)
5738{
NeilBrown3cb03002011-10-11 16:45:26 +11005739 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005740 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 char b[BDEVNAME_SIZE];
5742
5743 printk(KERN_INFO "md: autorun ...\n");
5744 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005745 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005747 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005749 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
5751 printk(KERN_INFO "md: considering %s ...\n",
5752 bdevname(rdev0->bdev,b));
5753 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005754 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 if (super_90_load(rdev, rdev0, 0) >= 0) {
5756 printk(KERN_INFO "md: adding %s ...\n",
5757 bdevname(rdev->bdev,b));
5758 list_move(&rdev->same_set, &candidates);
5759 }
5760 /*
5761 * now we have a set of devices, with all of them having
5762 * mostly sane superblocks. It's time to allocate the
5763 * mddev.
5764 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005765 if (part) {
5766 dev = MKDEV(mdp_major,
5767 rdev0->preferred_minor << MdpMinorShift);
5768 unit = MINOR(dev) >> MdpMinorShift;
5769 } else {
5770 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5771 unit = MINOR(dev);
5772 }
5773 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5775 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5776 break;
5777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
5779 md_probe(dev, NULL, NULL);
5780 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005781 if (!mddev || !mddev->gendisk) {
5782 if (mddev)
5783 mddev_put(mddev);
5784 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 "md: cannot allocate memory for md drive.\n");
5786 break;
5787 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005788 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 printk(KERN_WARNING "md: %s locked, cannot run\n",
5790 mdname(mddev));
5791 else if (mddev->raid_disks || mddev->major_version
5792 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005793 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 "md: %s already running, cannot run %s\n",
5795 mdname(mddev), bdevname(rdev0->bdev,b));
5796 mddev_unlock(mddev);
5797 } else {
5798 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005799 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005800 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 list_del_init(&rdev->same_set);
5802 if (bind_rdev_to_array(rdev, mddev))
5803 export_rdev(rdev);
5804 }
5805 autorun_array(mddev);
5806 mddev_unlock(mddev);
5807 }
5808 /* on success, candidates will be empty, on error
5809 * it won't...
5810 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005811 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005812 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 mddev_put(mddev);
5816 }
5817 printk(KERN_INFO "md: ... autorun DONE.\n");
5818}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005819#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
NeilBrownf72ffdd2014-09-30 14:23:59 +10005821static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822{
5823 mdu_version_t ver;
5824
5825 ver.major = MD_MAJOR_VERSION;
5826 ver.minor = MD_MINOR_VERSION;
5827 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5828
5829 if (copy_to_user(arg, &ver, sizeof(ver)))
5830 return -EFAULT;
5831
5832 return 0;
5833}
5834
NeilBrownf72ffdd2014-09-30 14:23:59 +10005835static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836{
5837 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005838 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005839 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
NeilBrown1ca69c42012-10-11 13:37:33 +11005841 nr = working = insync = failed = spare = 0;
5842 rcu_read_lock();
5843 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005845 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 failed++;
5847 else {
5848 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005849 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005850 insync++;
Song Liub347af82016-08-11 17:14:45 -07005851 else if (test_bit(Journal, &rdev->flags))
5852 /* TODO: add journal count to md_u.h */
5853 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 else
5855 spare++;
5856 }
5857 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005858 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
5860 info.major_version = mddev->major_version;
5861 info.minor_version = mddev->minor_version;
5862 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005863 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005865 info.size = mddev->dev_sectors / 2;
5866 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005867 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 info.nr_disks = nr;
5869 info.raid_disks = mddev->raid_disks;
5870 info.md_minor = mddev->md_minor;
5871 info.not_persistent= !mddev->persistent;
5872
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005873 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 info.state = 0;
5875 if (mddev->in_sync)
5876 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005877 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005878 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005879 if (mddev_is_clustered(mddev))
5880 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005881 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 info.working_disks = working;
5883 info.failed_disks = failed;
5884 info.spare_disks = spare;
5885
5886 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005887 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
5889 if (copy_to_user(arg, &info, sizeof(info)))
5890 return -EFAULT;
5891
5892 return 0;
5893}
5894
NeilBrownf72ffdd2014-09-30 14:23:59 +10005895static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005896{
5897 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005898 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005899 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005900
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005901 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005902 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005903 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005904
NeilBrown32a76272005-06-21 17:17:14 -07005905 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005906 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005907 /* bitmap enabled */
5908 if (mddev->bitmap_info.file) {
5909 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5910 sizeof(file->pathname));
5911 if (IS_ERR(ptr))
5912 err = PTR_ERR(ptr);
5913 else
5914 memmove(file->pathname, ptr,
5915 sizeof(file->pathname)-(ptr-file->pathname));
5916 }
NeilBrown4af1a042014-12-15 12:57:00 +11005917 spin_unlock(&mddev->lock);
5918
5919 if (err == 0 &&
5920 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005921 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005922
NeilBrown32a76272005-06-21 17:17:14 -07005923 kfree(file);
5924 return err;
5925}
5926
NeilBrownf72ffdd2014-09-30 14:23:59 +10005927static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928{
5929 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005930 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 if (copy_from_user(&info, arg, sizeof(info)))
5933 return -EFAULT;
5934
NeilBrown1ca69c42012-10-11 13:37:33 +11005935 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005936 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 if (rdev) {
5938 info.major = MAJOR(rdev->bdev->bd_dev);
5939 info.minor = MINOR(rdev->bdev->bd_dev);
5940 info.raid_disk = rdev->raid_disk;
5941 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005942 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005944 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 info.state |= (1<<MD_DISK_ACTIVE);
5946 info.state |= (1<<MD_DISK_SYNC);
5947 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005948 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005949 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005950 if (test_bit(WriteMostly, &rdev->flags))
5951 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 } else {
5953 info.major = info.minor = 0;
5954 info.raid_disk = -1;
5955 info.state = (1<<MD_DISK_REMOVED);
5956 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005957 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958
5959 if (copy_to_user(arg, &info, sizeof(info)))
5960 return -EFAULT;
5961
5962 return 0;
5963}
5964
NeilBrownf72ffdd2014-09-30 14:23:59 +10005965static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966{
5967 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005968 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 dev_t dev = MKDEV(info->major,info->minor);
5970
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005971 if (mddev_is_clustered(mddev) &&
5972 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005973 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005974 mdname(mddev));
5975 return -EINVAL;
5976 }
5977
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5979 return -EOVERFLOW;
5980
5981 if (!mddev->raid_disks) {
5982 int err;
5983 /* expecting a device which has a superblock */
5984 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5985 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005986 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 "md: md_import_device returned %ld\n",
5988 PTR_ERR(rdev));
5989 return PTR_ERR(rdev);
5990 }
5991 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005992 struct md_rdev *rdev0
5993 = list_entry(mddev->disks.next,
5994 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005995 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 .load_super(rdev, rdev0, mddev->minor_version);
5997 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005998 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006000 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 bdevname(rdev0->bdev,b2));
6002 export_rdev(rdev);
6003 return -EINVAL;
6004 }
6005 }
6006 err = bind_rdev_to_array(rdev, mddev);
6007 if (err)
6008 export_rdev(rdev);
6009 return err;
6010 }
6011
6012 /*
6013 * add_new_disk can be used once the array is assembled
6014 * to add "hot spares". They must already have a superblock
6015 * written
6016 */
6017 if (mddev->pers) {
6018 int err;
6019 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006020 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 "%s: personality does not support diskops!\n",
6022 mdname(mddev));
6023 return -EINVAL;
6024 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006025 if (mddev->persistent)
6026 rdev = md_import_device(dev, mddev->major_version,
6027 mddev->minor_version);
6028 else
6029 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006031 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 "md: md_import_device returned %ld\n",
6033 PTR_ERR(rdev));
6034 return PTR_ERR(rdev);
6035 }
NeilBrown1a855a02010-12-09 16:36:28 +11006036 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006037 if (!mddev->persistent) {
6038 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006039 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006040 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006041 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006042 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006043 } else
NeilBrown41158c72005-06-21 17:17:25 -07006044 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006045 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006046 } else
6047 super_types[mddev->major_version].
6048 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006049 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006050 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006051 /* This was a hot-add request, but events doesn't
6052 * match, so reject it.
6053 */
6054 export_rdev(rdev);
6055 return -EINVAL;
6056 }
6057
NeilBrownb2d444d2005-11-08 21:39:31 -08006058 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006059 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6060 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006061 else
6062 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006063
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006064 if (info->state & (1<<MD_DISK_JOURNAL)) {
6065 struct md_rdev *rdev2;
6066 bool has_journal = false;
6067
6068 /* make sure no existing journal disk */
6069 rdev_for_each(rdev2, mddev) {
6070 if (test_bit(Journal, &rdev2->flags)) {
6071 has_journal = true;
6072 break;
6073 }
6074 }
6075 if (has_journal) {
6076 export_rdev(rdev);
6077 return -EBUSY;
6078 }
Song Liubac624f2015-08-13 14:31:55 -07006079 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006080 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006081 /*
6082 * check whether the device shows up in other nodes
6083 */
6084 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006085 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006086 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006087 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006088 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006089 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006090 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006091 export_rdev(rdev);
6092 return err;
6093 }
6094 }
6095 }
6096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 rdev->raid_disk = -1;
6098 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006099
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 if (err)
6101 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006102
6103 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006104 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6105 if (!err) {
6106 err = md_cluster_ops->new_disk_ack(mddev,
6107 err == 0);
6108 if (err)
6109 md_kick_rdev_from_array(rdev);
6110 }
6111 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006112 if (err)
6113 md_cluster_ops->add_new_disk_cancel(mddev);
6114 else
6115 err = add_bound_rdev(rdev);
6116 }
6117
6118 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006119 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006120
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 return err;
6122 }
6123
6124 /* otherwise, add_new_disk is only allowed
6125 * for major_version==0 superblocks
6126 */
6127 if (mddev->major_version != 0) {
6128 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6129 mdname(mddev));
6130 return -EINVAL;
6131 }
6132
6133 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6134 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006135 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006137 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 "md: error, md_import_device() returned %ld\n",
6139 PTR_ERR(rdev));
6140 return PTR_ERR(rdev);
6141 }
6142 rdev->desc_nr = info->number;
6143 if (info->raid_disk < mddev->raid_disks)
6144 rdev->raid_disk = info->raid_disk;
6145 else
6146 rdev->raid_disk = -1;
6147
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006149 if (info->state & (1<<MD_DISK_SYNC))
6150 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006152 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6153 set_bit(WriteMostly, &rdev->flags);
6154
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 if (!mddev->persistent) {
6156 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006157 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6158 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006159 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006160 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
NeilBrown2bf071b2006-01-06 00:20:55 -08006162 err = bind_rdev_to_array(rdev, mddev);
6163 if (err) {
6164 export_rdev(rdev);
6165 return err;
6166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 }
6168
6169 return 0;
6170}
6171
NeilBrownf72ffdd2014-09-30 14:23:59 +10006172static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173{
6174 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006175 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 rdev = find_rdev(mddev, dev);
6178 if (!rdev)
6179 return -ENXIO;
6180
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006181 if (rdev->raid_disk < 0)
6182 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006183
NeilBrown3ea8929d2013-04-24 11:42:41 +10006184 clear_bit(Blocked, &rdev->flags);
6185 remove_and_add_spares(mddev, rdev);
6186
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 if (rdev->raid_disk >= 0)
6188 goto busy;
6189
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006190kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006191 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006192 md_cluster_ops->remove_disk(mddev, rdev);
6193
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006194 md_kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07006195 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006196 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
6198 return 0;
6199busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006200 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 bdevname(rdev->bdev,b), mdname(mddev));
6202 return -EBUSY;
6203}
6204
NeilBrownf72ffdd2014-09-30 14:23:59 +10006205static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206{
6207 char b[BDEVNAME_SIZE];
6208 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006209 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
6211 if (!mddev->pers)
6212 return -ENODEV;
6213
6214 if (mddev->major_version != 0) {
6215 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6216 " version-0 superblocks.\n",
6217 mdname(mddev));
6218 return -EINVAL;
6219 }
6220 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006221 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 "%s: personality does not support diskops!\n",
6223 mdname(mddev));
6224 return -EINVAL;
6225 }
6226
NeilBrownd710e132008-10-13 11:55:12 +11006227 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006229 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 "md: error, md_import_device() returned %ld\n",
6231 PTR_ERR(rdev));
6232 return -EINVAL;
6233 }
6234
6235 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006236 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006238 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239
NeilBrown8190e752009-06-18 08:48:58 +10006240 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241
NeilBrownb2d444d2005-11-08 21:39:31 -08006242 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006243 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 "md: can not hot-add faulty %s disk to %s!\n",
6245 bdevname(rdev->bdev,b), mdname(mddev));
6246 err = -EINVAL;
6247 goto abort_export;
6248 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006249
NeilBrownb2d444d2005-11-08 21:39:31 -08006250 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006252 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006253 err = bind_rdev_to_array(rdev, mddev);
6254 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006255 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
6257 /*
6258 * The rest should better be atomic, we can have disk failures
6259 * noticed in interrupt contexts ...
6260 */
6261
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 rdev->raid_disk = -1;
6263
NeilBrown850b2b422006-10-03 01:15:46 -07006264 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 /*
6266 * Kick recovery, maybe this spare has to be added to the
6267 * array immediately.
6268 */
6269 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6270 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006271 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 return 0;
6273
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274abort_export:
6275 export_rdev(rdev);
6276 return err;
6277}
6278
NeilBrownfd01b882011-10-11 16:47:53 +11006279static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006280{
NeilBrown035328c2014-04-09 12:25:40 +10006281 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006282
NeilBrown36fa3062005-09-09 16:23:45 -07006283 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006284 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006285 return -EBUSY;
6286 if (mddev->recovery || mddev->sync_thread)
6287 return -EBUSY;
6288 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006289 }
6290
NeilBrown36fa3062005-09-09 16:23:45 -07006291 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006292 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006293 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006294
NeilBrown1e594bb2014-12-15 12:57:00 +11006295 if (mddev->bitmap || mddev->bitmap_info.file)
6296 return -EEXIST; /* cannot add when bitmap is present */
6297 f = fget(fd);
6298
6299 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006300 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6301 mdname(mddev));
6302 return -EBADF;
6303 }
6304
NeilBrown1e594bb2014-12-15 12:57:00 +11006305 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006306 if (!S_ISREG(inode->i_mode)) {
6307 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6308 mdname(mddev));
6309 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006310 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006311 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6312 mdname(mddev));
6313 err = -EBADF;
6314 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006315 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6316 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006317 err = -EBUSY;
6318 }
6319 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006320 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006321 return err;
6322 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006323 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006324 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006325 } else if (mddev->bitmap == NULL)
6326 return -ENOENT; /* cannot remove what isn't there */
6327 err = 0;
6328 if (mddev->pers) {
6329 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006330 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006331 struct bitmap *bitmap;
6332
6333 bitmap = bitmap_create(mddev, -1);
6334 if (!IS_ERR(bitmap)) {
6335 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006336 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006337 } else
6338 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006339 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006340 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006341 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006342 fd = -1; /* make sure to put the file */
6343 }
NeilBrown36fa3062005-09-09 16:23:45 -07006344 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006345 }
6346 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006347 struct file *f = mddev->bitmap_info.file;
6348 if (f) {
6349 spin_lock(&mddev->lock);
6350 mddev->bitmap_info.file = NULL;
6351 spin_unlock(&mddev->lock);
6352 fput(f);
6353 }
NeilBrown36fa3062005-09-09 16:23:45 -07006354 }
6355
NeilBrown32a76272005-06-21 17:17:14 -07006356 return err;
6357}
6358
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359/*
6360 * set_array_info is used two different ways
6361 * The original usage is when creating a new array.
6362 * In this usage, raid_disks is > 0 and it together with
6363 * level, size, not_persistent,layout,chunksize determine the
6364 * shape of the array.
6365 * This will always create an array with a type-0.90.0 superblock.
6366 * The newer usage is when assembling an array.
6367 * In this case raid_disks will be 0, and the major_version field is
6368 * use to determine which style super-blocks are to be found on the devices.
6369 * The minor and patch _version numbers are also kept incase the
6370 * super_block handler wishes to interpret them.
6371 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006372static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373{
6374
6375 if (info->raid_disks == 0) {
6376 /* just setting version number for superblock loading */
6377 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006378 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 super_types[info->major_version].name == NULL) {
6380 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006381 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 "md: superblock version %d not known\n",
6383 info->major_version);
6384 return -EINVAL;
6385 }
6386 mddev->major_version = info->major_version;
6387 mddev->minor_version = info->minor_version;
6388 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006389 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006390 /* ensure mddev_put doesn't delete this now that there
6391 * is some minimal configuration.
6392 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006393 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 return 0;
6395 }
6396 mddev->major_version = MD_MAJOR_VERSION;
6397 mddev->minor_version = MD_MINOR_VERSION;
6398 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006399 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
6401 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006402 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006403 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 mddev->raid_disks = info->raid_disks;
6405 /* don't set md_minor, it is determined by which /dev/md* was
6406 * openned
6407 */
6408 if (info->state & (1<<MD_SB_CLEAN))
6409 mddev->recovery_cp = MaxSector;
6410 else
6411 mddev->recovery_cp = 0;
6412 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006413 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414
6415 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006416 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417
6418 mddev->max_disks = MD_SB_DISKS;
6419
NeilBrowne6910632008-02-06 01:39:51 -08006420 if (mddev->persistent)
6421 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006422 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
NeilBrownc3d97142009-12-14 12:49:52 +11006424 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006425 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006426 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006427
NeilBrownf6705572006-03-27 01:18:11 -08006428 mddev->reshape_position = MaxSector;
6429
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 /*
6431 * Generate a 128 bit UUID
6432 */
6433 get_random_bytes(mddev->uuid, 16);
6434
NeilBrownf6705572006-03-27 01:18:11 -08006435 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006436 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006437 mddev->new_layout = mddev->layout;
6438 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006439 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006440
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 return 0;
6442}
6443
NeilBrownfd01b882011-10-11 16:47:53 +11006444void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006445{
Dan Williamsb522adc2009-03-31 15:00:31 +11006446 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6447
6448 if (mddev->external_size)
6449 return;
6450
Dan Williams1f403622009-03-31 14:59:03 +11006451 mddev->array_sectors = array_sectors;
6452}
6453EXPORT_SYMBOL(md_set_array_sectors);
6454
NeilBrownfd01b882011-10-11 16:47:53 +11006455static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006456{
NeilBrown3cb03002011-10-11 16:45:26 +11006457 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006458 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006459 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006460
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006461 /* cluster raid doesn't support update size */
6462 if (mddev_is_clustered(mddev))
6463 return -EINVAL;
6464
NeilBrowna35b0d62006-01-06 00:20:49 -08006465 if (mddev->pers->resize == NULL)
6466 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006467 /* The "num_sectors" is the number of sectors of each device that
6468 * is used. This can only make sense for arrays with redundancy.
6469 * linear and raid0 always use whatever space is available. We can only
6470 * consider changing this number if no resync or reconstruction is
6471 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006472 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006473 * of each device. If num_sectors is zero, we find the largest size
6474 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006475 */
NeilBrownf851b602014-12-11 10:02:10 +11006476 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6477 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006478 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006479 if (mddev->ro)
6480 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006481
NeilBrowndafb20f2012-03-19 12:46:39 +11006482 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006483 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006484
Andre Nolld71f9f82008-07-11 22:02:22 +10006485 if (fit && (num_sectors == 0 || num_sectors > avail))
6486 num_sectors = avail;
6487 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006488 return -ENOSPC;
6489 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006490 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006491 if (!rv)
6492 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006493 return rv;
6494}
6495
NeilBrownfd01b882011-10-11 16:47:53 +11006496static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006497{
6498 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006499 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006500 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006501 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006502 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006503 if (mddev->ro)
6504 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006505 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006506 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006507 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006508 if (mddev->sync_thread ||
6509 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6510 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006511 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006512
6513 rdev_for_each(rdev, mddev) {
6514 if (mddev->raid_disks < raid_disks &&
6515 rdev->data_offset < rdev->new_data_offset)
6516 return -EINVAL;
6517 if (mddev->raid_disks > raid_disks &&
6518 rdev->data_offset > rdev->new_data_offset)
6519 return -EINVAL;
6520 }
6521
NeilBrown63c70c42006-03-27 01:18:13 -08006522 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006523 if (mddev->delta_disks < 0)
6524 mddev->reshape_backwards = 1;
6525 else if (mddev->delta_disks > 0)
6526 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006527
6528 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006529 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006530 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006531 mddev->reshape_backwards = 0;
6532 }
NeilBrownda943b992006-01-06 00:20:54 -08006533 return rv;
6534}
6535
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536/*
6537 * update_array_info is used to change the configuration of an
6538 * on-line array.
6539 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6540 * fields in the info are checked against the array.
6541 * Any differences that cannot be handled will cause an error.
6542 * Normally, only one change can be managed at a time.
6543 */
NeilBrownfd01b882011-10-11 16:47:53 +11006544static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545{
6546 int rv = 0;
6547 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006548 int state = 0;
6549
6550 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006551 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006552 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553
6554 if (mddev->major_version != info->major_version ||
6555 mddev->minor_version != info->minor_version ||
6556/* mddev->patch_version != info->patch_version || */
6557 mddev->ctime != info->ctime ||
6558 mddev->level != info->level ||
6559/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006560 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006561 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006562 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6563 ((state^info->state) & 0xfffffe00)
6564 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 return -EINVAL;
6566 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006567 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6568 cnt++;
6569 if (mddev->raid_disks != info->raid_disks)
6570 cnt++;
6571 if (mddev->layout != info->layout)
6572 cnt++;
6573 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6574 cnt++;
6575 if (cnt == 0)
6576 return 0;
6577 if (cnt > 1)
6578 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
6580 if (mddev->layout != info->layout) {
6581 /* Change layout
6582 * we don't need to do anything at the md level, the
6583 * personality will take care of it all.
6584 */
NeilBrown50ac1682009-06-18 08:47:55 +10006585 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006587 else {
6588 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006589 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006590 if (rv)
6591 mddev->new_layout = mddev->layout;
6592 return rv;
6593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006595 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006596 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597
NeilBrownda943b992006-01-06 00:20:54 -08006598 if (mddev->raid_disks != info->raid_disks)
6599 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600
NeilBrown36fa3062005-09-09 16:23:45 -07006601 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006602 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6603 rv = -EINVAL;
6604 goto err;
6605 }
6606 if (mddev->recovery || mddev->sync_thread) {
6607 rv = -EBUSY;
6608 goto err;
6609 }
NeilBrown36fa3062005-09-09 16:23:45 -07006610 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006611 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006612 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006613 if (mddev->bitmap) {
6614 rv = -EEXIST;
6615 goto err;
6616 }
6617 if (mddev->bitmap_info.default_offset == 0) {
6618 rv = -EINVAL;
6619 goto err;
6620 }
NeilBrownc3d97142009-12-14 12:49:52 +11006621 mddev->bitmap_info.offset =
6622 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006623 mddev->bitmap_info.space =
6624 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006625 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006626 bitmap = bitmap_create(mddev, -1);
6627 if (!IS_ERR(bitmap)) {
6628 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006629 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006630 } else
6631 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006632 if (rv)
6633 bitmap_destroy(mddev);
6634 mddev->pers->quiesce(mddev, 0);
6635 } else {
6636 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006637 if (!mddev->bitmap) {
6638 rv = -ENOENT;
6639 goto err;
6640 }
6641 if (mddev->bitmap->storage.file) {
6642 rv = -EINVAL;
6643 goto err;
6644 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006645 if (mddev->bitmap_info.nodes) {
6646 /* hold PW on all the bitmap lock */
6647 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6648 printk("md: can't change bitmap to none since the"
6649 " array is in use by more than one node\n");
6650 rv = -EPERM;
6651 md_cluster_ops->unlock_all_bitmaps(mddev);
6652 goto err;
6653 }
6654
6655 mddev->bitmap_info.nodes = 0;
6656 md_cluster_ops->leave(mddev);
6657 }
NeilBrown36fa3062005-09-09 16:23:45 -07006658 mddev->pers->quiesce(mddev, 1);
6659 bitmap_destroy(mddev);
6660 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006661 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006662 }
6663 }
NeilBrown850b2b422006-10-03 01:15:46 -07006664 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006665 return rv;
6666err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 return rv;
6668}
6669
NeilBrownfd01b882011-10-11 16:47:53 +11006670static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671{
NeilBrown3cb03002011-10-11 16:45:26 +11006672 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006673 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
6675 if (mddev->pers == NULL)
6676 return -ENODEV;
6677
NeilBrown1ca69c42012-10-11 13:37:33 +11006678 rcu_read_lock();
6679 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006681 err = -ENODEV;
6682 else {
6683 md_error(mddev, rdev);
6684 if (!test_bit(Faulty, &rdev->flags))
6685 err = -EBUSY;
6686 }
6687 rcu_read_unlock();
6688 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689}
6690
Andre Noll2f9618c2008-04-25 18:57:58 +02006691/*
6692 * We have a problem here : there is no easy way to give a CHS
6693 * virtual geometry. We currently pretend that we have a 2 heads
6694 * 4 sectors (with a BIG number of cylinders...). This drives
6695 * dosfs just mad... ;-)
6696 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006697static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6698{
NeilBrownfd01b882011-10-11 16:47:53 +11006699 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006700
6701 geo->heads = 2;
6702 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006703 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006704 return 0;
6705}
6706
Nicolas Schichancb335f82014-01-15 16:58:52 +01006707static inline bool md_ioctl_valid(unsigned int cmd)
6708{
6709 switch (cmd) {
6710 case ADD_NEW_DISK:
6711 case BLKROSET:
6712 case GET_ARRAY_INFO:
6713 case GET_BITMAP_FILE:
6714 case GET_DISK_INFO:
6715 case HOT_ADD_DISK:
6716 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006717 case RAID_AUTORUN:
6718 case RAID_VERSION:
6719 case RESTART_ARRAY_RW:
6720 case RUN_ARRAY:
6721 case SET_ARRAY_INFO:
6722 case SET_BITMAP_FILE:
6723 case SET_DISK_FAULTY:
6724 case STOP_ARRAY:
6725 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006726 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006727 return true;
6728 default:
6729 return false;
6730 }
6731}
6732
Al Viroa39907f2008-03-02 10:31:15 -05006733static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 unsigned int cmd, unsigned long arg)
6735{
6736 int err = 0;
6737 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006738 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006739 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740
Nicolas Schichancb335f82014-01-15 16:58:52 +01006741 if (!md_ioctl_valid(cmd))
6742 return -ENOTTY;
6743
NeilBrown506c9e42011-12-23 10:17:26 +11006744 switch (cmd) {
6745 case RAID_VERSION:
6746 case GET_ARRAY_INFO:
6747 case GET_DISK_INFO:
6748 break;
6749 default:
6750 if (!capable(CAP_SYS_ADMIN))
6751 return -EACCES;
6752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
6754 /*
6755 * Commands dealing with the RAID driver but not any
6756 * particular array:
6757 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006758 switch (cmd) {
6759 case RAID_VERSION:
6760 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006761 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006764 case RAID_AUTORUN:
6765 err = 0;
6766 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006767 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006769 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 }
6771
6772 /*
6773 * Commands creating/starting a new array:
6774 */
6775
Al Viroa39907f2008-03-02 10:31:15 -05006776 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777
6778 if (!mddev) {
6779 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006780 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 }
6782
NeilBrown1ca69c42012-10-11 13:37:33 +11006783 /* Some actions do not requires the mutex */
6784 switch (cmd) {
6785 case GET_ARRAY_INFO:
6786 if (!mddev->raid_disks && !mddev->external)
6787 err = -ENODEV;
6788 else
6789 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006790 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006791
6792 case GET_DISK_INFO:
6793 if (!mddev->raid_disks && !mddev->external)
6794 err = -ENODEV;
6795 else
6796 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006797 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006798
6799 case SET_DISK_FAULTY:
6800 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006801 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006802
6803 case GET_BITMAP_FILE:
6804 err = get_bitmap_file(mddev, argp);
6805 goto out;
6806
NeilBrown1ca69c42012-10-11 13:37:33 +11006807 }
6808
NeilBrowna7a3f082012-12-11 13:35:54 +11006809 if (cmd == ADD_NEW_DISK)
6810 /* need to ensure md_delayed_delete() has completed */
6811 flush_workqueue(md_misc_wq);
6812
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006813 if (cmd == HOT_REMOVE_DISK)
6814 /* need to ensure recovery thread has run */
6815 wait_event_interruptible_timeout(mddev->sb_wait,
6816 !test_bit(MD_RECOVERY_NEEDED,
6817 &mddev->flags),
6818 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006819 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6820 /* Need to flush page cache, and ensure no-one else opens
6821 * and writes
6822 */
6823 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006824 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006825 mutex_unlock(&mddev->open_mutex);
6826 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006827 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006828 }
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006829 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown260fa032013-08-27 16:44:13 +10006830 mutex_unlock(&mddev->open_mutex);
6831 sync_blockdev(bdev);
6832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 err = mddev_lock(mddev);
6834 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006835 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6837 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 }
6840
NeilBrownc02c0ae2012-12-11 13:39:21 +11006841 if (cmd == SET_ARRAY_INFO) {
6842 mdu_array_info_t info;
6843 if (!arg)
6844 memset(&info, 0, sizeof(info));
6845 else if (copy_from_user(&info, argp, sizeof(info))) {
6846 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006847 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006848 }
6849 if (mddev->pers) {
6850 err = update_array_info(mddev, &info);
6851 if (err) {
6852 printk(KERN_WARNING "md: couldn't update"
6853 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006854 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006856 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006857 }
6858 if (!list_empty(&mddev->disks)) {
6859 printk(KERN_WARNING
6860 "md: array %s already has disks!\n",
6861 mdname(mddev));
6862 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006863 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006864 }
6865 if (mddev->raid_disks) {
6866 printk(KERN_WARNING
6867 "md: array %s already initialised!\n",
6868 mdname(mddev));
6869 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006870 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006871 }
6872 err = set_array_info(mddev, &info);
6873 if (err) {
6874 printk(KERN_WARNING "md: couldn't set"
6875 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006876 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006877 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006878 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 }
6880
6881 /*
6882 * Commands querying/configuring an existing array:
6883 */
NeilBrown32a76272005-06-21 17:17:14 -07006884 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006885 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006886 if ((!mddev->raid_disks && !mddev->external)
6887 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6888 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6889 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006891 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 }
6893
6894 /*
6895 * Commands even a read-only array can execute:
6896 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006897 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006898 case RESTART_ARRAY_RW:
6899 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006900 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006901
6902 case STOP_ARRAY:
6903 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006904 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006905
6906 case STOP_ARRAY_RO:
6907 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006908 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006909
NeilBrown3ea8929d2013-04-24 11:42:41 +10006910 case HOT_REMOVE_DISK:
6911 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006912 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006913
NeilBrown7ceb17e2013-04-24 11:42:42 +10006914 case ADD_NEW_DISK:
6915 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08006916 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10006917 * So require mddev->pers and MD_DISK_SYNC.
6918 */
6919 if (mddev->pers) {
6920 mdu_disk_info_t info;
6921 if (copy_from_user(&info, argp, sizeof(info)))
6922 err = -EFAULT;
6923 else if (!(info.state & (1<<MD_DISK_SYNC)))
6924 /* Need to clear read-only for this */
6925 break;
6926 else
6927 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006928 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006929 }
6930 break;
6931
NeilBrownc02c0ae2012-12-11 13:39:21 +11006932 case BLKROSET:
6933 if (get_user(ro, (int __user *)(arg))) {
6934 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006935 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006936 }
6937 err = -EINVAL;
6938
6939 /* if the bdev is going readonly the value of mddev->ro
6940 * does not matter, no writes are coming
6941 */
6942 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006943 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006944
NeilBrownc02c0ae2012-12-11 13:39:21 +11006945 /* are we are already prepared for writes? */
6946 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006947 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006948
6949 /* transitioning to readauto need only happen for
6950 * arrays that call md_write_start
6951 */
6952 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006954 if (err == 0) {
6955 mddev->ro = 2;
6956 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006957 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006958 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006959 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 }
6961
6962 /*
6963 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006964 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 */
NeilBrown326eb172014-09-30 15:36:28 +10006966 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006967 if (mddev->ro == 2) {
6968 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006969 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006970 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006971 /* mddev_unlock will wake thread */
6972 /* If a device failed while we were read-only, we
6973 * need to make sure the metadata is updated now.
6974 */
6975 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6976 mddev_unlock(mddev);
6977 wait_event(mddev->sb_wait,
6978 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6979 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006980 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006981 }
NeilBrownf91de922005-11-08 21:39:36 -08006982 } else {
6983 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006984 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 }
6987
NeilBrownc02c0ae2012-12-11 13:39:21 +11006988 switch (cmd) {
6989 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006991 mdu_disk_info_t info;
6992 if (copy_from_user(&info, argp, sizeof(info)))
6993 err = -EFAULT;
6994 else
6995 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006996 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006999 case CLUSTERED_DISK_NACK:
7000 if (mddev_is_clustered(mddev))
7001 md_cluster_ops->new_disk_ack(mddev, false);
7002 else
7003 err = -EINVAL;
7004 goto unlock;
7005
NeilBrownc02c0ae2012-12-11 13:39:21 +11007006 case HOT_ADD_DISK:
7007 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007008 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009
NeilBrownc02c0ae2012-12-11 13:39:21 +11007010 case RUN_ARRAY:
7011 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007012 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013
NeilBrownc02c0ae2012-12-11 13:39:21 +11007014 case SET_BITMAP_FILE:
7015 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007016 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007017
NeilBrownc02c0ae2012-12-11 13:39:21 +11007018 default:
7019 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007020 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 }
7022
NeilBrown3adc28d2014-09-30 15:46:41 +10007023unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007024 if (mddev->hold_active == UNTIL_IOCTL &&
7025 err != -EINVAL)
7026 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007028out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 return err;
7030}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007031#ifdef CONFIG_COMPAT
7032static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7033 unsigned int cmd, unsigned long arg)
7034{
7035 switch (cmd) {
7036 case HOT_REMOVE_DISK:
7037 case HOT_ADD_DISK:
7038 case SET_DISK_FAULTY:
7039 case SET_BITMAP_FILE:
7040 /* These take in integer arg, do not convert */
7041 break;
7042 default:
7043 arg = (unsigned long)compat_ptr(arg);
7044 break;
7045 }
7046
7047 return md_ioctl(bdev, mode, cmd, arg);
7048}
7049#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050
Al Viroa39907f2008-03-02 10:31:15 -05007051static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052{
7053 /*
7054 * Succeed if we can lock the mddev, which confirms that
7055 * it isn't being stopped right now.
7056 */
NeilBrownfd01b882011-10-11 16:47:53 +11007057 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 int err;
7059
Yuanhan Liu0c098222012-05-22 13:55:32 +10007060 if (!mddev)
7061 return -ENODEV;
7062
NeilBrownd3374822009-01-09 08:31:10 +11007063 if (mddev->gendisk != bdev->bd_disk) {
7064 /* we are racing with mddev_put which is discarding this
7065 * bd_disk.
7066 */
7067 mddev_put(mddev);
7068 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007069 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007070 /* Then retry the open from the top */
7071 return -ERESTARTSYS;
7072 }
7073 BUG_ON(mddev != bdev->bd_disk->private_data);
7074
NeilBrownc8c00a62009-08-10 12:50:52 +10007075 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 goto out;
7077
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007078 if (test_bit(MD_CLOSING, &mddev->flags)) {
7079 mutex_unlock(&mddev->open_mutex);
7080 return -ENODEV;
7081 }
7082
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007084 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007085 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007087 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 out:
7089 return err;
7090}
7091
Al Virodb2a1442013-05-05 21:52:57 -04007092static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007094 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007096 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007097 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007100
7101static int md_media_changed(struct gendisk *disk)
7102{
NeilBrownfd01b882011-10-11 16:47:53 +11007103 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007104
7105 return mddev->changed;
7106}
7107
7108static int md_revalidate(struct gendisk *disk)
7109{
NeilBrownfd01b882011-10-11 16:47:53 +11007110 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007111
7112 mddev->changed = 0;
7113 return 0;
7114}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007115static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116{
7117 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007118 .open = md_open,
7119 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007120 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007121#ifdef CONFIG_COMPAT
7122 .compat_ioctl = md_compat_ioctl,
7123#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007124 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007125 .media_changed = md_media_changed,
7126 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127};
7128
NeilBrownf72ffdd2014-09-30 14:23:59 +10007129static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130{
NeilBrown2b8bf342011-10-11 16:48:23 +11007131 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 /*
7134 * md_thread is a 'system-thread', it's priority should be very
7135 * high. We avoid resource deadlocks individually in each
7136 * raid personality. (RAID5 does preallocation) We also use RR and
7137 * the very same RT priority as kswapd, thus we will never get
7138 * into a priority inversion deadlock.
7139 *
7140 * we definitely have to have equal or higher priority than
7141 * bdflush, otherwise bdflush will deadlock if there are too
7142 * many dirty RAID5 blocks.
7143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144
NeilBrown6985c432005-10-19 21:23:47 -07007145 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007146 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147
NeilBrown93588e22005-11-15 00:09:12 -08007148 /* We need to wait INTERRUPTIBLE so that
7149 * we don't add to the load-average.
7150 * That means we need to be sure no signals are
7151 * pending
7152 */
7153 if (signal_pending(current))
7154 flush_signals(current);
7155
7156 wait_event_interruptible_timeout
7157 (thread->wqueue,
7158 test_bit(THREAD_WAKEUP, &thread->flags)
7159 || kthread_should_stop(),
7160 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161
NeilBrown6c987912011-01-14 09:13:53 +11007162 clear_bit(THREAD_WAKEUP, &thread->flags);
7163 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007164 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007166
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 return 0;
7168}
7169
NeilBrown2b8bf342011-10-11 16:48:23 +11007170void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171{
7172 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007173 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 set_bit(THREAD_WAKEUP, &thread->flags);
7175 wake_up(&thread->wqueue);
7176 }
7177}
NeilBrown6c144d32014-09-30 16:15:38 +10007178EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179
Shaohua Li4ed87312012-10-11 13:34:00 +11007180struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7181 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182{
NeilBrown2b8bf342011-10-11 16:48:23 +11007183 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184
NeilBrown2b8bf342011-10-11 16:48:23 +11007185 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186 if (!thread)
7187 return NULL;
7188
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 init_waitqueue_head(&thread->wqueue);
7190
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 thread->run = run;
7192 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007193 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007194 thread->tsk = kthread_run(md_thread, thread,
7195 "%s_%s",
7196 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007197 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007198 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 kfree(thread);
7200 return NULL;
7201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 return thread;
7203}
NeilBrown6c144d32014-09-30 16:15:38 +10007204EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205
NeilBrown2b8bf342011-10-11 16:48:23 +11007206void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207{
NeilBrown2b8bf342011-10-11 16:48:23 +11007208 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007209 if (!thread)
7210 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007211 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007212 /* Locking ensures that mddev_unlock does not wake_up a
7213 * non-existent thread
7214 */
7215 spin_lock(&pers_lock);
7216 *threadp = NULL;
7217 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007218
7219 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 kfree(thread);
7221}
NeilBrown6c144d32014-09-30 16:15:38 +10007222EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223
NeilBrownfd01b882011-10-11 16:47:53 +11007224void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225{
NeilBrownb2d444d2005-11-08 21:39:31 -08007226 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007228
NeilBrownde393cd2011-07-28 11:31:48 +10007229 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 return;
7231 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007232 if (mddev->degraded)
7233 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007234 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7236 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7237 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007238 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007239 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007240 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241}
NeilBrown6c144d32014-09-30 16:15:38 +10007242EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243
7244/* seq_file implementation /proc/mdstat */
7245
7246static void status_unused(struct seq_file *seq)
7247{
7248 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007249 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
7251 seq_printf(seq, "unused devices: ");
7252
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007253 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254 char b[BDEVNAME_SIZE];
7255 i++;
7256 seq_printf(seq, "%s ",
7257 bdevname(rdev->bdev,b));
7258 }
7259 if (!i)
7260 seq_printf(seq, "<none>");
7261
7262 seq_printf(seq, "\n");
7263}
7264
NeilBrownf7851be2015-07-02 17:12:58 +10007265static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266{
NeilBrowndd71cf62009-05-07 12:49:35 +10007267 sector_t max_sectors, resync, res;
7268 unsigned long dt, db;
7269 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007270 int scale;
7271 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272
NeilBrownc804cde2012-05-21 09:28:33 +10007273 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7274 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007275 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007277 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
NeilBrownf7851be2015-07-02 17:12:58 +10007279 resync = mddev->curr_resync;
7280 if (resync <= 3) {
7281 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7282 /* Still cleaning up */
7283 resync = max_sectors;
7284 } else
7285 resync -= atomic_read(&mddev->recovery_active);
7286
7287 if (resync == 0) {
7288 if (mddev->recovery_cp < MaxSector) {
7289 seq_printf(seq, "\tresync=PENDING");
7290 return 1;
7291 }
7292 return 0;
7293 }
7294 if (resync < 3) {
7295 seq_printf(seq, "\tresync=DELAYED");
7296 return 1;
7297 }
7298
NeilBrown403df472014-09-30 15:52:29 +10007299 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007300 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007301 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007302 * u32, as those are the requirements for sector_div.
7303 * Thus 'scale' must be at least 10
7304 */
7305 scale = 10;
7306 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007307 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007308 scale++;
7309 }
7310 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007311 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007312
7313 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 {
NeilBrown4588b422006-03-27 01:18:04 -08007315 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 seq_printf(seq, "[");
7317 for (i = 0; i < x; i++)
7318 seq_printf(seq, "=");
7319 seq_printf(seq, ">");
7320 for (i = 0; i < y; i++)
7321 seq_printf(seq, ".");
7322 seq_printf(seq, "] ");
7323 }
NeilBrown4588b422006-03-27 01:18:04 -08007324 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007325 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7326 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007327 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7328 "check" :
7329 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7330 "resync" : "recovery"))),
7331 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007332 (unsigned long long) resync/2,
7333 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334
7335 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 * dt: time from mark until now
7337 * db: blocks written from mark until now
7338 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007339 *
7340 * rt is a sector_t, so could be 32bit or 64bit.
7341 * So we divide before multiply in case it is 32bit and close
7342 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007343 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007344 * near the end of resync when the number of remaining sectors
7345 * is close to 'db'.
7346 * We then divide rt by 32 after multiplying by db to compensate.
7347 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 */
7349 dt = ((jiffies - mddev->resync_mark) / HZ);
7350 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007351 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7352 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353
NeilBrowndd71cf62009-05-07 12:49:35 +10007354 rt = max_sectors - resync; /* number of remaining sectors */
7355 sector_div(rt, db/32+1);
7356 rt *= dt;
7357 rt >>= 5;
7358
7359 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7360 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361
NeilBrownff4e8d92006-07-10 04:44:16 -07007362 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007363 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364}
7365
7366static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7367{
7368 struct list_head *tmp;
7369 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007370 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371
7372 if (l >= 0x10000)
7373 return NULL;
7374 if (!l--)
7375 /* header */
7376 return (void*)1;
7377
7378 spin_lock(&all_mddevs_lock);
7379 list_for_each(tmp,&all_mddevs)
7380 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007381 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 mddev_get(mddev);
7383 spin_unlock(&all_mddevs_lock);
7384 return mddev;
7385 }
7386 spin_unlock(&all_mddevs_lock);
7387 if (!l--)
7388 return (void*)2;/* tail */
7389 return NULL;
7390}
7391
7392static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7393{
7394 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007395 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007396
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 ++*pos;
7398 if (v == (void*)2)
7399 return NULL;
7400
7401 spin_lock(&all_mddevs_lock);
7402 if (v == (void*)1)
7403 tmp = all_mddevs.next;
7404 else
7405 tmp = mddev->all_mddevs.next;
7406 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007407 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 else {
7409 next_mddev = (void*)2;
7410 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 spin_unlock(&all_mddevs_lock);
7413
7414 if (v != (void*)1)
7415 mddev_put(mddev);
7416 return next_mddev;
7417
7418}
7419
7420static void md_seq_stop(struct seq_file *seq, void *v)
7421{
NeilBrownfd01b882011-10-11 16:47:53 +11007422 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423
7424 if (mddev && v != (void*)1 && v != (void*)2)
7425 mddev_put(mddev);
7426}
7427
7428static int md_seq_show(struct seq_file *seq, void *v)
7429{
NeilBrownfd01b882011-10-11 16:47:53 +11007430 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007431 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007432 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433
7434 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007435 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 seq_printf(seq, "Personalities : ");
7437 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007438 list_for_each_entry(pers, &pers_list, list)
7439 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440
7441 spin_unlock(&pers_lock);
7442 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007443 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 return 0;
7445 }
7446 if (v == (void*)2) {
7447 status_unused(seq);
7448 return 0;
7449 }
7450
NeilBrown36d091f2014-12-15 12:56:58 +11007451 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7453 seq_printf(seq, "%s : %sactive", mdname(mddev),
7454 mddev->pers ? "" : "in");
7455 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007456 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007458 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007459 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 seq_printf(seq, " %s", mddev->pers->name);
7461 }
7462
Andre Nolldd8ac332009-03-31 14:33:13 +11007463 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007464 rcu_read_lock();
7465 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 char b[BDEVNAME_SIZE];
7467 seq_printf(seq, " %s[%d]",
7468 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007469 if (test_bit(WriteMostly, &rdev->flags))
7470 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007471 if (test_bit(Journal, &rdev->flags))
7472 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007473 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 seq_printf(seq, "(F)");
7475 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007476 }
7477 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007478 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007479 if (test_bit(Replacement, &rdev->flags))
7480 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007481 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007483 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484
7485 if (!list_empty(&mddev->disks)) {
7486 if (mddev->pers)
7487 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007488 (unsigned long long)
7489 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 else
7491 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007492 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007494 if (mddev->persistent) {
7495 if (mddev->major_version != 0 ||
7496 mddev->minor_version != 90) {
7497 seq_printf(seq," super %d.%d",
7498 mddev->major_version,
7499 mddev->minor_version);
7500 }
NeilBrowne6910632008-02-06 01:39:51 -08007501 } else if (mddev->external)
7502 seq_printf(seq, " super external:%s",
7503 mddev->metadata_type);
7504 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007505 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506
7507 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007508 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007509 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007510 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007511 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007512 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007513 }
NeilBrown32a76272005-06-21 17:17:14 -07007514 } else
7515 seq_printf(seq, "\n ");
7516
NeilBrown57148962012-03-19 12:46:40 +11007517 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518
7519 seq_printf(seq, "\n");
7520 }
NeilBrown36d091f2014-12-15 12:56:58 +11007521 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007522
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 return 0;
7524}
7525
Jan Engelhardt110518b2009-05-07 12:49:37 +10007526static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 .start = md_seq_start,
7528 .next = md_seq_next,
7529 .stop = md_seq_stop,
7530 .show = md_seq_show,
7531};
7532
7533static int md_seq_open(struct inode *inode, struct file *file)
7534{
Kay Sieversf1514632011-07-12 20:48:39 +02007535 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 int error;
7537
7538 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007539 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007540 return error;
7541
7542 seq = file->private_data;
7543 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 return error;
7545}
7546
NeilBrowne2f23b62014-04-09 14:33:51 +10007547static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007548static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7549{
Kay Sieversf1514632011-07-12 20:48:39 +02007550 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007551 int mask;
7552
NeilBrowne2f23b62014-04-09 14:33:51 +10007553 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007554 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007555 poll_wait(filp, &md_event_waiters, wait);
7556
7557 /* always allow read */
7558 mask = POLLIN | POLLRDNORM;
7559
Kay Sieversf1514632011-07-12 20:48:39 +02007560 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007561 mask |= POLLERR | POLLPRI;
7562 return mask;
7563}
7564
Arjan van de Venfa027c22007-02-12 00:55:33 -08007565static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007566 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 .open = md_seq_open,
7568 .read = seq_read,
7569 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007570 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007571 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572};
7573
NeilBrown84fc4b52011-10-11 16:49:58 +11007574int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575{
Chao Yu50bd3772014-09-25 15:28:34 +08007576 printk(KERN_INFO "md: %s personality registered for level %d\n",
7577 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007579 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 spin_unlock(&pers_lock);
7581 return 0;
7582}
NeilBrown6c144d32014-09-30 16:15:38 +10007583EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584
NeilBrown84fc4b52011-10-11 16:49:58 +11007585int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586{
NeilBrown2604b702006-01-06 00:20:36 -08007587 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007589 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 spin_unlock(&pers_lock);
7591 return 0;
7592}
NeilBrown6c144d32014-09-30 16:15:38 +10007593EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594
NeilBrown6022e752015-08-13 12:32:55 +10007595int register_md_cluster_operations(struct md_cluster_operations *ops,
7596 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007597{
NeilBrown6022e752015-08-13 12:32:55 +10007598 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007599 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007600 if (md_cluster_ops != NULL)
7601 ret = -EALREADY;
7602 else {
7603 md_cluster_ops = ops;
7604 md_cluster_mod = module;
7605 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007606 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007607 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007608}
7609EXPORT_SYMBOL(register_md_cluster_operations);
7610
7611int unregister_md_cluster_operations(void)
7612{
7613 spin_lock(&pers_lock);
7614 md_cluster_ops = NULL;
7615 spin_unlock(&pers_lock);
7616 return 0;
7617}
7618EXPORT_SYMBOL(unregister_md_cluster_operations);
7619
7620int md_setup_cluster(struct mddev *mddev, int nodes)
7621{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007622 if (!md_cluster_ops)
7623 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007624 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007625 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007626 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04007627 pr_err("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007628 spin_unlock(&pers_lock);
7629 return -ENOENT;
7630 }
7631 spin_unlock(&pers_lock);
7632
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007633 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007634}
7635
7636void md_cluster_stop(struct mddev *mddev)
7637{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007638 if (!md_cluster_ops)
7639 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007640 md_cluster_ops->leave(mddev);
7641 module_put(md_cluster_mod);
7642}
7643
NeilBrownfd01b882011-10-11 16:47:53 +11007644static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007646 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007648 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649
7650 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007651 rcu_read_lock();
7652 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007654 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7655 (int)part_stat_read(&disk->part0, sectors[1]) -
7656 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007657 /* sync IO will cause sync_io to increase before the disk_stats
7658 * as sync_io is counted when a request starts, and
7659 * disk_stats is counted when it completes.
7660 * So resync activity will cause curr_events to be smaller than
7661 * when there was no such activity.
7662 * non-sync IO will cause disk_stat to increase without
7663 * increasing sync_io so curr_events will (eventually)
7664 * be larger than it was before. Once it becomes
7665 * substantially larger, the test below will cause
7666 * the array to appear non-idle, and resync will slow
7667 * down.
7668 * If there is a lot of outstanding resync activity when
7669 * we set last_event to curr_events, then all that activity
7670 * completing might cause the array to appear non-idle
7671 * and resync will be slowed down even though there might
7672 * not have been non-resync activity. This will only
7673 * happen once though. 'last_events' will soon reflect
7674 * the state where there is little or no outstanding
7675 * resync requests, and further resync activity will
7676 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007677 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007679 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 rdev->last_events = curr_events;
7681 idle = 0;
7682 }
7683 }
NeilBrown4b809912008-07-21 17:05:25 +10007684 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 return idle;
7686}
7687
NeilBrownfd01b882011-10-11 16:47:53 +11007688void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689{
7690 /* another "blocks" (512byte) blocks have been synced */
7691 atomic_sub(blocks, &mddev->recovery_active);
7692 wake_up(&mddev->recovery_wait);
7693 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007694 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007695 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 md_wakeup_thread(mddev->thread);
7697 // stop recovery, signal do_sync ....
7698 }
7699}
NeilBrown6c144d32014-09-30 16:15:38 +10007700EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701
NeilBrown06d91a52005-06-21 17:17:12 -07007702/* md_write_start(mddev, bi)
7703 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007704 * in superblock) before writing, schedule a superblock update
7705 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007706 */
NeilBrownfd01b882011-10-11 16:47:53 +11007707void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708{
Neil Brown0fd62b82008-06-28 08:31:36 +10007709 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007710 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007711 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007712
NeilBrownf91de922005-11-08 21:39:36 -08007713 BUG_ON(mddev->ro == 1);
7714 if (mddev->ro == 2) {
7715 /* need to switch to read/write */
7716 mddev->ro = 0;
7717 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7718 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007719 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007720 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007721 }
NeilBrown06d91a52005-06-21 17:17:12 -07007722 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007723 if (mddev->safemode == 1)
7724 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007725 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007726 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007727 if (mddev->in_sync) {
7728 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007729 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007730 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007731 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007732 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007733 }
NeilBrown85572d72014-12-15 12:56:56 +11007734 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007735 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007736 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007737 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007738 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007739 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740}
NeilBrown6c144d32014-09-30 16:15:38 +10007741EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742
NeilBrownfd01b882011-10-11 16:47:53 +11007743void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744{
7745 if (atomic_dec_and_test(&mddev->writes_pending)) {
7746 if (mddev->safemode == 2)
7747 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007748 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7750 }
7751}
NeilBrown6c144d32014-09-30 16:15:38 +10007752EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753
NeilBrown2a2275d2007-01-26 00:57:11 -08007754/* md_allow_write(mddev)
7755 * Calling this ensures that the array is marked 'active' so that writes
7756 * may proceed without blocking. It is important to call this before
7757 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7758 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007759 *
Guoqing Jiangabf35082015-12-21 10:51:01 +11007760 * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007761 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007762 */
NeilBrownfd01b882011-10-11 16:47:53 +11007763int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007764{
7765 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007766 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007767 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007768 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007769 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007770 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007771
NeilBrown85572d72014-12-15 12:56:56 +11007772 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007773 if (mddev->in_sync) {
7774 mddev->in_sync = 0;
7775 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007776 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007777 if (mddev->safemode_delay &&
7778 mddev->safemode == 0)
7779 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007780 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007781 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007782 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007783 } else
NeilBrown85572d72014-12-15 12:56:56 +11007784 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007785
NeilBrown070dc6d2010-08-30 17:33:34 +10007786 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007787 return -EAGAIN;
7788 else
7789 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007790}
7791EXPORT_SYMBOL_GPL(md_allow_write);
7792
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793#define SYNC_MARKS 10
7794#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007795#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007796void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797{
Shaohua Li4ed87312012-10-11 13:34:00 +11007798 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007799 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 unsigned int currspeed = 0,
7801 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007802 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007804 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805 sector_t mark_cnt[SYNC_MARKS];
7806 int last_mark,m;
7807 struct list_head *tmp;
7808 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007809 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007810 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007811 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007812 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007813 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814
7815 /* just incase thread restarts... */
7816 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7817 return;
NeilBrown3991b312014-05-28 13:39:23 +10007818 if (mddev->ro) {/* never try to sync a read-only array */
7819 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007820 return;
NeilBrown3991b312014-05-28 13:39:23 +10007821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007823 if (mddev_is_clustered(mddev)) {
7824 ret = md_cluster_ops->resync_start(mddev);
7825 if (ret)
7826 goto skip;
7827
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04007828 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007829 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7830 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
7831 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
7832 && ((unsigned long long)mddev->curr_resync_completed
7833 < (unsigned long long)mddev->resync_max_sectors))
7834 goto skip;
7835 }
7836
NeilBrown61df9d92006-10-03 01:15:57 -07007837 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007838 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007839 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007840 action = "check";
7841 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007842 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007843 action = "repair";
7844 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007845 desc = "resync";
7846 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7847 desc = "reshape";
7848 else
7849 desc = "recovery";
7850
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007851 mddev->last_sync_action = action ?: desc;
7852
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 /* we overload curr_resync somewhat here.
7854 * 0 == not engaged in resync at all
7855 * 2 == checking that there is no conflict with another sync
7856 * 1 == like 2, but have yielded to allow conflicting resync to
7857 * commense
7858 * other == active in resync - this many blocks
7859 *
7860 * Before starting a resync we must have set curr_resync to
7861 * 2, and then checked that every "conflicting" array has curr_resync
7862 * less than ours. When we find one that is the same or higher
7863 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7864 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7865 * This will mean we have to start checking from the beginning again.
7866 *
7867 */
7868
7869 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02007870 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 mddev->curr_resync = 2;
7872
7873 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007874 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007876 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 if (mddev2 == mddev)
7878 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007879 if (!mddev->parallel_resync
7880 && mddev2->curr_resync
7881 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882 DEFINE_WAIT(wq);
7883 if (mddev < mddev2 && mddev->curr_resync == 2) {
7884 /* arbitrarily yield */
7885 mddev->curr_resync = 1;
7886 wake_up(&resync_wait);
7887 }
7888 if (mddev > mddev2 && mddev->curr_resync == 1)
7889 /* no need to wait here, we can wait the next
7890 * time 'round when curr_resync == 2
7891 */
7892 continue;
NeilBrown97441972008-09-19 11:49:54 +10007893 /* We need to wait 'interruptible' so as not to
7894 * contribute to the load average, and not to
7895 * be caught by 'softlockup'
7896 */
7897 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007898 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007899 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02007900 if (mddev2_minor != mddev2->md_minor) {
7901 mddev2_minor = mddev2->md_minor;
7902 printk(KERN_INFO "md: delaying %s of %s"
7903 " until %s has finished (they"
7904 " share one or more physical units)\n",
7905 desc, mdname(mddev),
7906 mdname(mddev2));
7907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007909 if (signal_pending(current))
7910 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 schedule();
7912 finish_wait(&resync_wait, &wq);
7913 goto try_again;
7914 }
7915 finish_wait(&resync_wait, &wq);
7916 }
7917 }
7918 } while (mddev->curr_resync < 2);
7919
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007920 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007921 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007923 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 */
7925 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007926 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007927 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007928 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7929 j = mddev->resync_min;
7930 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007931 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007932
NeilBrownccfcc3c2006-03-27 01:18:09 -08007933 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007934 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007935 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007937 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007938 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007939 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007940 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007941 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007942 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007943 !test_bit(Faulty, &rdev->flags) &&
7944 !test_bit(In_sync, &rdev->flags) &&
7945 rdev->recovery_offset < j)
7946 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007947 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007948
7949 /* If there is a bitmap, we need to make sure all
7950 * writes that started before we added a spare
7951 * complete before we start doing a recovery.
7952 * Otherwise the write might complete and (via
7953 * bitmap_endwrite) set a bit in the bitmap after the
7954 * recovery has checked that bit and skipped that
7955 * region.
7956 */
7957 if (mddev->bitmap) {
7958 mddev->pers->quiesce(mddev, 1);
7959 mddev->pers->quiesce(mddev, 0);
7960 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962
NeilBrown61df9d92006-10-03 01:15:57 -07007963 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7964 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7965 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007966 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007967 "(but not more than %d KB/sec) for %s.\n",
7968 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969
NeilBrowneea1bf32009-03-31 14:27:02 +11007970 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007971
NeilBrown57afd892005-06-21 17:17:13 -07007972 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 for (m = 0; m < SYNC_MARKS; m++) {
7974 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007975 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976 }
7977 last_mark = 0;
7978 mddev->resync_mark = mark[last_mark];
7979 mddev->resync_mark_cnt = mark_cnt[last_mark];
7980
7981 /*
7982 * Tune reconstruction:
7983 */
7984 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007985 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7986 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987
7988 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 last_check = 0;
7990
7991 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007992 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007993 "md: resuming %s of %s from checkpoint.\n",
7994 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007996 } else
7997 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007998 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007999 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8000 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008001 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002
majianpeng7c2c57c2012-07-03 12:12:26 +10008003 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008005 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006
NeilBrown57afd892005-06-21 17:17:13 -07008007 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008008
NeilBrown7a91ee12009-05-26 12:57:21 +10008009 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8010 ((mddev->curr_resync > mddev->curr_resync_completed &&
8011 (mddev->curr_resync - mddev->curr_resync_completed)
8012 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008013 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008014 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008015 >= mddev->resync_max - mddev->curr_resync_completed ||
8016 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008017 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008018 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008019 wait_event(mddev->recovery_wait,
8020 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008021 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008022 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8023 j > mddev->recovery_cp)
8024 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008025 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10008026 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008027 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008028 }
NeilBrownacb180b2009-04-14 16:28:34 +10008029
NeilBrownc91abf52013-11-19 12:02:01 +11008030 while (j >= mddev->resync_max &&
8031 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008032 /* As this condition is controlled by user-space,
8033 * we can block indefinitely, so use '_interruptible'
8034 * to avoid triggering warnings.
8035 */
8036 flush_signals(current); /* just in case */
8037 wait_event_interruptible(mddev->recovery_wait,
8038 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008039 || test_bit(MD_RECOVERY_INTR,
8040 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008041 }
NeilBrownacb180b2009-04-14 16:28:34 +10008042
NeilBrownc91abf52013-11-19 12:02:01 +11008043 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8044 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008045
NeilBrown09314792015-02-19 16:04:40 +11008046 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008047 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008048 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008049 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050 }
NeilBrown57afd892005-06-21 17:17:13 -07008051
8052 if (!skipped) { /* actual IO requested */
8053 io_sectors += sectors;
8054 atomic_add(sectors, &mddev->recovery_active);
8055 }
8056
NeilBrowne875ece2011-07-28 11:39:24 +10008057 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8058 break;
8059
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008061 if (j > max_sectors)
8062 /* when skipping, extra large numbers can be returned. */
8063 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008064 if (j > 2)
8065 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008066 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008067 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008068 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008069 * visible in /proc/mdstat
8070 */
8071 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008072
8073 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074 continue;
8075
NeilBrown57afd892005-06-21 17:17:13 -07008076 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077 repeat:
8078 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8079 /* step marks */
8080 int next = (last_mark+1) % SYNC_MARKS;
8081
8082 mddev->resync_mark = mark[next];
8083 mddev->resync_mark_cnt = mark_cnt[next];
8084 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008085 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086 last_mark = next;
8087 }
8088
NeilBrownc91abf52013-11-19 12:02:01 +11008089 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091
8092 /*
8093 * this loop exits only if either when we are slower than
8094 * the 'hard' speed limit, or the system was IO-idle for
8095 * a jiffy.
8096 * the system might be non-idle CPU-wise, but we only care
8097 * about not overloading the IO subsystem. (things like an
8098 * e2fsck being done on the RAID array should execute fast)
8099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100 cond_resched();
8101
Xiao Niac7e50a2014-08-07 09:37:41 -04008102 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8103 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008104 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105
NeilBrown88202a02006-01-06 00:21:36 -08008106 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008107 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008108 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109 goto repeat;
8110 }
NeilBrownac8fa412015-02-19 16:55:00 +11008111 if (!is_mddev_idle(mddev, 0)) {
8112 /*
8113 * Give other IO more of a chance.
8114 * The faster the devices, the less we wait.
8115 */
8116 wait_event(mddev->recovery_wait,
8117 !atomic_read(&mddev->recovery_active));
8118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119 }
8120 }
NeilBrownc91abf52013-11-19 12:02:01 +11008121 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8122 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8123 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 /*
8125 * this also signals 'finished resyncing' to md_stop
8126 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008127 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8129
NeilBrown5ed1df22015-07-24 13:27:08 +10008130 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8131 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8132 mddev->curr_resync > 2) {
8133 mddev->curr_resync_completed = mddev->curr_resync;
8134 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8135 }
NeilBrown09314792015-02-19 16:04:40 +11008136 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137
NeilBrowndfc70642008-05-23 13:04:39 -07008138 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008139 mddev->curr_resync > 2) {
8140 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8141 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8142 if (mddev->curr_resync >= mddev->recovery_cp) {
8143 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008144 "md: checkpointing %s of %s.\n",
8145 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008146 if (test_bit(MD_RECOVERY_ERROR,
8147 &mddev->recovery))
8148 mddev->recovery_cp =
8149 mddev->curr_resync_completed;
8150 else
8151 mddev->recovery_cp =
8152 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008153 }
8154 } else
8155 mddev->recovery_cp = MaxSector;
8156 } else {
8157 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8158 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008159 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008160 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008161 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008162 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008163 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008164 !test_bit(Faulty, &rdev->flags) &&
8165 !test_bit(In_sync, &rdev->flags) &&
8166 rdev->recovery_offset < mddev->curr_resync)
8167 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008168 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008170 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008171 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008172 /* set CHANGE_PENDING here since maybe another update is needed,
8173 * so other nodes are informed. It should be harmless for normal
8174 * raid */
8175 set_mask_bits(&mddev->flags, 0,
8176 BIT(MD_CHANGE_PENDING) | BIT(MD_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008177
NeilBrown23da4222014-12-15 12:57:01 +11008178 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008179 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8180 /* We completed so min/max setting can be forgotten if used. */
8181 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8182 mddev->resync_min = 0;
8183 mddev->resync_max = MaxSector;
8184 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8185 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008186 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008187 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008188 spin_unlock(&mddev->lock);
8189
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193}
NeilBrown29269552006-03-27 01:18:10 -08008194EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195
NeilBrown746d3202013-04-24 11:42:41 +10008196static int remove_and_add_spares(struct mddev *mddev,
8197 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008198{
NeilBrown3cb03002011-10-11 16:45:26 +11008199 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008200 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008201 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008202 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008203
NeilBrownd787be42016-06-02 16:19:53 +10008204 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008205 if ((this == NULL || rdev == this) &&
8206 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008207 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008208 test_bit(Faulty, &rdev->flags) &&
8209 atomic_read(&rdev->nr_pending)==0) {
8210 /* Faulty non-Blocked devices with nr_pending == 0
8211 * never get nr_pending incremented,
8212 * never get Faulty cleared, and never get Blocked set.
8213 * So we can synchronize_rcu now rather than once per device
8214 */
8215 remove_some = true;
8216 set_bit(RemoveSynchronized, &rdev->flags);
8217 }
8218 }
8219
8220 if (remove_some)
8221 synchronize_rcu();
8222 rdev_for_each(rdev, mddev) {
8223 if ((this == NULL || rdev == this) &&
8224 rdev->raid_disk >= 0 &&
8225 !test_bit(Blocked, &rdev->flags) &&
8226 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008227 (!test_bit(In_sync, &rdev->flags) &&
8228 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008229 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008230 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008231 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008232 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008233 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008234 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008235 }
8236 }
NeilBrownd787be42016-06-02 16:19:53 +10008237 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8238 clear_bit(RemoveSynchronized, &rdev->flags);
8239 }
8240
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008241 if (removed && mddev->kobj.sd)
8242 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008243
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008244 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008245 goto no_add;
8246
NeilBrowndafb20f2012-03-19 12:46:39 +11008247 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008248 if (this && this != rdev)
8249 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008250 if (test_bit(Candidate, &rdev->flags))
8251 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008252 if (rdev->raid_disk >= 0 &&
8253 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008254 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008255 !test_bit(Faulty, &rdev->flags))
8256 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008257 if (rdev->raid_disk >= 0)
8258 continue;
8259 if (test_bit(Faulty, &rdev->flags))
8260 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008261 if (!test_bit(Journal, &rdev->flags)) {
8262 if (mddev->ro &&
8263 ! (rdev->saved_raid_disk >= 0 &&
8264 !test_bit(Bitmap_sync, &rdev->flags)))
8265 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008266
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008267 rdev->recovery_offset = 0;
8268 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008269 if (mddev->pers->
8270 hot_add_disk(mddev, rdev) == 0) {
8271 if (sysfs_link_rdev(mddev, rdev))
8272 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008273 if (!test_bit(Journal, &rdev->flags))
8274 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008275 md_new_event(mddev);
8276 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008277 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008278 }
NeilBrown746d3202013-04-24 11:42:41 +10008279no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008280 if (removed)
8281 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008282 return spares;
8283}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008284
NeilBrownac05f252014-09-30 08:10:42 +10008285static void md_start_sync(struct work_struct *ws)
8286{
8287 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008288
NeilBrownac05f252014-09-30 08:10:42 +10008289 mddev->sync_thread = md_register_thread(md_do_sync,
8290 mddev,
8291 "resync");
8292 if (!mddev->sync_thread) {
Guoqing Jiang207efcd2016-08-12 13:42:40 +08008293 printk(KERN_ERR "%s: could not start resync thread...\n",
8294 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008295 /* leave the spares where they are, it shouldn't hurt */
8296 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8297 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8298 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8299 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8300 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008301 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008302 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8303 &mddev->recovery))
8304 if (mddev->sysfs_action)
8305 sysfs_notify_dirent_safe(mddev->sysfs_action);
8306 } else
8307 md_wakeup_thread(mddev->sync_thread);
8308 sysfs_notify_dirent_safe(mddev->sysfs_action);
8309 md_new_event(mddev);
8310}
8311
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312/*
8313 * This routine is regularly called by all per-raid-array threads to
8314 * deal with generic issues like resync and super-block update.
8315 * Raid personalities that don't have a thread (linear/raid0) do not
8316 * need this as they never do any recovery or update the superblock.
8317 *
8318 * It does not do any resync itself, but rather "forks" off other threads
8319 * to do that as needed.
8320 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8321 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008322 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323 * and wakeups up this thread which will reap the thread and finish up.
8324 * This thread also removes any faulty devices (with nr_pending == 0).
8325 *
8326 * The overall approach is:
8327 * 1/ if the superblock needs updating, update it.
8328 * 2/ If a recovery thread is running, don't do anything else.
8329 * 3/ If recovery has finished, clean up, possibly marking spares active.
8330 * 4/ If there are any faulty devices, remove them.
8331 * 5/ If array is degraded, try to add spares devices
8332 * 6/ If array has spares or is not in-sync, start a resync thread.
8333 */
NeilBrownfd01b882011-10-11 16:47:53 +11008334void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008336 if (mddev->suspended)
8337 return;
8338
NeilBrown5f404022005-06-21 17:17:16 -07008339 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008340 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341
NeilBrownfca4d842005-06-21 17:17:11 -07008342 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008343 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008344 printk(KERN_INFO "md: %s in immediate safe mode\n",
8345 mdname(mddev));
8346 mddev->safemode = 2;
8347 }
8348 flush_signals(current);
8349 }
8350
NeilBrownc89a8ee2008-08-05 15:54:13 +10008351 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8352 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008354 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008356 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008357 test_bit(MD_RELOAD_SB, &mddev->flags) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008358 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008359 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8360 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361 ))
8362 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008363
NeilBrowndf5b89b2006-03-27 01:18:20 -08008364 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008365 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008366
NeilBrownc89a8ee2008-08-05 15:54:13 +10008367 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008368 struct md_rdev *rdev;
8369 if (!mddev->external && mddev->in_sync)
8370 /* 'Blocked' flag not needed as failed devices
8371 * will be recorded if array switched to read/write.
8372 * Leaving it set will prevent the device
8373 * from being removed.
8374 */
8375 rdev_for_each(rdev, mddev)
8376 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008377 /* On a read-only array we can:
8378 * - remove failed devices
8379 * - add already-in_sync devices if the array itself
8380 * is in-sync.
8381 * As we only add devices that are already in-sync,
8382 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008383 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008384 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008385 /* There is no thread, but we need to call
8386 * ->spare_active and clear saved_raid_disk
8387 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008388 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008389 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008390 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008391 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008392 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008393 goto unlock;
8394 }
8395
Guoqing Jiang659b2542015-12-21 10:50:59 +11008396 if (mddev_is_clustered(mddev)) {
8397 struct md_rdev *rdev;
8398 /* kick the device if another node issued a
8399 * remove disk.
8400 */
8401 rdev_for_each(rdev, mddev) {
8402 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8403 rdev->raid_disk < 0)
8404 md_kick_rdev_from_array(rdev);
8405 }
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008406
8407 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8408 md_reload_sb(mddev, mddev->good_device_nr);
Guoqing Jiang659b2542015-12-21 10:50:59 +11008409 }
8410
NeilBrown31a59e32008-04-30 00:52:30 -07008411 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008412 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008413 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008414 if (mddev->safemode &&
8415 !atomic_read(&mddev->writes_pending) &&
8416 !mddev->in_sync &&
8417 mddev->recovery_cp == MaxSector) {
8418 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008419 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008420 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008421 }
8422 if (mddev->safemode == 1)
8423 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008424 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008425 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008426 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008427 }
NeilBrownfca4d842005-06-21 17:17:11 -07008428
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008429 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07008430 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008431
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8433 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8434 /* resync/recovery still happening */
8435 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8436 goto unlock;
8437 }
8438 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008439 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440 goto unlock;
8441 }
Neil Brown72a23c22008-06-28 08:31:41 +10008442 /* Set RUNNING before clearing NEEDED to avoid
8443 * any transients in the value of "sync_action".
8444 */
NeilBrown72f36d52012-10-11 14:25:57 +11008445 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008446 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008447 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008448 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008449 /* Clear some bits that don't mean anything, but
8450 * might be left set
8451 */
NeilBrown24dd4692005-11-08 21:39:26 -08008452 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8453 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454
NeilBrowned209582012-04-24 10:23:14 +10008455 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8456 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008457 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458 /* no recovery is running.
8459 * remove any failed drives, then
8460 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008461 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462 * the personality to fail the re-add.
8463 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008464
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008465 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008466 if (mddev->pers->check_reshape == NULL ||
8467 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008468 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008469 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008470 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008471 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008472 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008473 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8474 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008475 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008476 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008477 } else if (mddev->recovery_cp < MaxSector) {
8478 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008479 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008480 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8481 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008482 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008483
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008485 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008486 /* We are adding a device or devices to an array
8487 * which has the bitmap stored on all devices.
8488 * So make sure all bitmap pages get written
8489 */
8490 bitmap_write_all(mddev->bitmap);
8491 }
NeilBrownac05f252014-09-30 08:10:42 +10008492 INIT_WORK(&mddev->del_work, md_start_sync);
8493 queue_work(md_misc_wq, &mddev->del_work);
8494 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495 }
NeilBrownac05f252014-09-30 08:10:42 +10008496 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008497 if (!mddev->sync_thread) {
8498 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008499 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008500 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8501 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008502 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008503 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008504 }
NeilBrownac05f252014-09-30 08:10:42 +10008505 unlock:
8506 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507 mddev_unlock(mddev);
8508 }
8509}
NeilBrown6c144d32014-09-30 16:15:38 +10008510EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008512void md_reap_sync_thread(struct mddev *mddev)
8513{
8514 struct md_rdev *rdev;
8515
8516 /* resync has finished, collect result */
8517 md_unregister_thread(&mddev->sync_thread);
8518 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8519 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8520 /* success...*/
8521 /* activate any spares */
8522 if (mddev->pers->spare_active(mddev)) {
8523 sysfs_notify(&mddev->kobj, NULL,
8524 "degraded");
8525 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8526 }
8527 }
8528 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8529 mddev->pers->finish_reshape)
8530 mddev->pers->finish_reshape(mddev);
8531
8532 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008533 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008534 */
NeilBrownf4667222013-12-09 12:04:56 +11008535 if (!mddev->degraded)
8536 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008537 rdev->saved_raid_disk = -1;
8538
8539 md_update_sb(mddev, 1);
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008540 /* MD_CHANGE_PENDING should be cleared by md_update_sb, so we can
8541 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8542 * clustered raid */
8543 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8544 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008545 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008546 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008547 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8548 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8549 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8550 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008551 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008552 /* flag recovery needed just to double check */
8553 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8554 sysfs_notify_dirent_safe(mddev->sysfs_action);
8555 md_new_event(mddev);
8556 if (mddev->event_work.func)
8557 queue_work(md_misc_wq, &mddev->event_work);
8558}
NeilBrown6c144d32014-09-30 16:15:38 +10008559EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008560
NeilBrownfd01b882011-10-11 16:47:53 +11008561void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008562{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008563 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008564 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008565 !test_bit(Blocked, &rdev->flags) &&
8566 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008567 msecs_to_jiffies(5000));
8568 rdev_dec_pending(rdev, mddev);
8569}
8570EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8571
NeilBrownc6563a82012-05-21 09:27:00 +10008572void md_finish_reshape(struct mddev *mddev)
8573{
8574 /* called be personality module when reshape completes. */
8575 struct md_rdev *rdev;
8576
8577 rdev_for_each(rdev, mddev) {
8578 if (rdev->data_offset > rdev->new_data_offset)
8579 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8580 else
8581 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8582 rdev->data_offset = rdev->new_data_offset;
8583 }
8584}
8585EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008586
Vishal Vermafc974ee2015-12-24 19:20:34 -07008587/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008588
Vishal Vermafc974ee2015-12-24 19:20:34 -07008589/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008590int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008591 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008592{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008593 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008594 int rv;
8595 if (is_new)
8596 s += rdev->new_data_offset;
8597 else
8598 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008599 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8600 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008601 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008602 sysfs_notify_dirent_safe(rdev->sysfs_state);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008603 set_mask_bits(&mddev->flags, 0,
8604 BIT(MD_CHANGE_CLEAN) | BIT(MD_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008605 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008606 return 1;
8607 } else
8608 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008609}
8610EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8611
NeilBrownc6563a82012-05-21 09:27:00 +10008612int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8613 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008614{
NeilBrownc6563a82012-05-21 09:27:00 +10008615 if (is_new)
8616 s += rdev->new_data_offset;
8617 else
8618 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008619 return badblocks_clear(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008620 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008621}
8622EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8623
Adrian Bunk75c96f82005-05-05 16:16:09 -07008624static int md_notify_reboot(struct notifier_block *this,
8625 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008626{
8627 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008628 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008629 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630
NeilBrownc744a652012-03-19 12:46:37 +11008631 for_each_mddev(mddev, tmp) {
8632 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008633 if (mddev->pers)
8634 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008635 if (mddev->persistent)
8636 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008637 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008638 }
NeilBrownc744a652012-03-19 12:46:37 +11008639 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 }
NeilBrownc744a652012-03-19 12:46:37 +11008641 /*
8642 * certain more exotic SCSI devices are known to be
8643 * volatile wrt too early system reboots. While the
8644 * right place to handle this issue is the given
8645 * driver, we do want to have a safe RAID driver ...
8646 */
8647 if (need_delay)
8648 mdelay(1000*1);
8649
Linus Torvalds1da177e2005-04-16 15:20:36 -07008650 return NOTIFY_DONE;
8651}
8652
Adrian Bunk75c96f82005-05-05 16:16:09 -07008653static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008654 .notifier_call = md_notify_reboot,
8655 .next = NULL,
8656 .priority = INT_MAX, /* before any real devices */
8657};
8658
8659static void md_geninit(void)
8660{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008661 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008662
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008663 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008664}
8665
Adrian Bunk75c96f82005-05-05 16:16:09 -07008666static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667{
Tejun Heoe804ac72010-10-15 15:36:08 +02008668 int ret = -ENOMEM;
8669
Tejun Heoada609e2011-01-25 14:35:54 +01008670 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008671 if (!md_wq)
8672 goto err_wq;
8673
8674 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8675 if (!md_misc_wq)
8676 goto err_misc_wq;
8677
8678 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8679 goto err_md;
8680
8681 if ((ret = register_blkdev(0, "mdp")) < 0)
8682 goto err_mdp;
8683 mdp_major = ret;
8684
NeilBrownaf5628f2014-07-31 13:54:54 +10008685 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008686 md_probe, NULL, NULL);
8687 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688 md_probe, NULL, NULL);
8689
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008691 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692
8693 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695
Tejun Heoe804ac72010-10-15 15:36:08 +02008696err_mdp:
8697 unregister_blkdev(MD_MAJOR, "md");
8698err_md:
8699 destroy_workqueue(md_misc_wq);
8700err_misc_wq:
8701 destroy_workqueue(md_wq);
8702err_wq:
8703 return ret;
8704}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008706static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008707{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008708 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8709 struct md_rdev *rdev2;
8710 int role, ret;
8711 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008712
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008713 /* Check for change of roles in the active devices */
8714 rdev_for_each(rdev2, mddev) {
8715 if (test_bit(Faulty, &rdev2->flags))
8716 continue;
8717
8718 /* Check if the roles changed */
8719 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008720
8721 if (test_bit(Candidate, &rdev2->flags)) {
8722 if (role == 0xfffe) {
8723 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8724 md_kick_rdev_from_array(rdev2);
8725 continue;
8726 }
8727 else
8728 clear_bit(Candidate, &rdev2->flags);
8729 }
8730
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008731 if (role != rdev2->raid_disk) {
8732 /* got activated */
8733 if (rdev2->raid_disk == -1 && role != 0xffff) {
8734 rdev2->saved_raid_disk = role;
8735 ret = remove_and_add_spares(mddev, rdev2);
8736 pr_info("Activated spare: %s\n",
8737 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04008738 /* wakeup mddev->thread here, so array could
8739 * perform resync with the new activated disk */
8740 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8741 md_wakeup_thread(mddev->thread);
8742
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008743 }
8744 /* device faulty
8745 * We just want to do the minimum to mark the disk
8746 * as faulty. The recovery is performed by the
8747 * one who initiated the error.
8748 */
8749 if ((role == 0xfffe) || (role == 0xfffd)) {
8750 md_error(mddev, rdev2);
8751 clear_bit(Blocked, &rdev2->flags);
8752 }
8753 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008754 }
8755
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11008756 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8757 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008758
8759 /* Finally set the event to be up to date */
8760 mddev->events = le64_to_cpu(sb->events);
8761}
8762
8763static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8764{
8765 int err;
8766 struct page *swapout = rdev->sb_page;
8767 struct mdp_superblock_1 *sb;
8768
8769 /* Store the sb page of the rdev in the swapout temporary
8770 * variable in case we err in the future
8771 */
8772 rdev->sb_page = NULL;
8773 alloc_disk_sb(rdev);
8774 ClearPageUptodate(rdev->sb_page);
8775 rdev->sb_loaded = 0;
8776 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
8777
8778 if (err < 0) {
8779 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8780 __func__, __LINE__, rdev->desc_nr, err);
8781 put_page(rdev->sb_page);
8782 rdev->sb_page = swapout;
8783 rdev->sb_loaded = 1;
8784 return err;
8785 }
8786
8787 sb = page_address(rdev->sb_page);
8788 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8789 * is not set
8790 */
8791
8792 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8793 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8794
8795 /* The other node finished recovery, call spare_active to set
8796 * device In_sync and mddev->degraded
8797 */
8798 if (rdev->recovery_offset == MaxSector &&
8799 !test_bit(In_sync, &rdev->flags) &&
8800 mddev->pers->spare_active(mddev))
8801 sysfs_notify(&mddev->kobj, NULL, "degraded");
8802
8803 put_page(swapout);
8804 return 0;
8805}
8806
8807void md_reload_sb(struct mddev *mddev, int nr)
8808{
8809 struct md_rdev *rdev;
8810 int err;
8811
8812 /* Find the rdev */
8813 rdev_for_each_rcu(rdev, mddev) {
8814 if (rdev->desc_nr == nr)
8815 break;
8816 }
8817
8818 if (!rdev || rdev->desc_nr != nr) {
8819 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
8820 return;
8821 }
8822
8823 err = read_rdev(mddev, rdev);
8824 if (err < 0)
8825 return;
8826
8827 check_sb_changes(mddev, rdev);
8828
8829 /* Read all rdev's to update recovery_offset */
8830 rdev_for_each_rcu(rdev, mddev)
8831 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008832}
8833EXPORT_SYMBOL(md_reload_sb);
8834
Linus Torvalds1da177e2005-04-16 15:20:36 -07008835#ifndef MODULE
8836
8837/*
8838 * Searches all registered partitions for autorun RAID arrays
8839 * at boot time.
8840 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008841
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008842static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008843static LIST_HEAD(all_detected_devices);
8844struct detected_devices_node {
8845 struct list_head list;
8846 dev_t dev;
8847};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008848
8849void md_autodetect_dev(dev_t dev)
8850{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008851 struct detected_devices_node *node_detected_dev;
8852
8853 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8854 if (node_detected_dev) {
8855 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008856 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008857 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008858 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008859 } else {
8860 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8861 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863}
8864
Linus Torvalds1da177e2005-04-16 15:20:36 -07008865static void autostart_arrays(int part)
8866{
NeilBrown3cb03002011-10-11 16:45:26 +11008867 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008868 struct detected_devices_node *node_detected_dev;
8869 dev_t dev;
8870 int i_scanned, i_passed;
8871
8872 i_scanned = 0;
8873 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874
8875 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8876
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008877 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008878 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8879 i_scanned++;
8880 node_detected_dev = list_entry(all_detected_devices.next,
8881 struct detected_devices_node, list);
8882 list_del(&node_detected_dev->list);
8883 dev = node_detected_dev->dev;
8884 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07008885 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07008886 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07008887 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 if (IS_ERR(rdev))
8889 continue;
8890
NeilBrown403df472014-09-30 15:52:29 +10008891 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008892 continue;
NeilBrown403df472014-09-30 15:52:29 +10008893
NeilBrownd0fae182008-03-04 14:29:31 -08008894 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008896 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008898 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008899
8900 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8901 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902
8903 autorun_devices(part);
8904}
8905
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008906#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
8908static __exit void md_exit(void)
8909{
NeilBrownfd01b882011-10-11 16:47:53 +11008910 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008912 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008913
NeilBrownaf5628f2014-07-31 13:54:54 +10008914 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008915 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008917 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918 unregister_blkdev(mdp_major, "mdp");
8919 unregister_reboot_notifier(&md_notifier);
8920 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008921
8922 /* We cannot unload the modules while some process is
8923 * waiting for us in select() or poll() - wake them up
8924 */
8925 md_unloading = 1;
8926 while (waitqueue_active(&md_event_waiters)) {
8927 /* not safe to leave yet */
8928 wake_up(&md_event_waiters);
8929 msleep(delay);
8930 delay += delay;
8931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008932 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008933
NeilBrown29ac4aa2008-02-06 01:39:58 -08008934 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008936 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008938 destroy_workqueue(md_misc_wq);
8939 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940}
8941
Dan Williams685784a2007-07-09 11:56:42 -07008942subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943module_exit(md_exit)
8944
NeilBrownf91de922005-11-08 21:39:36 -08008945static int get_ro(char *buffer, struct kernel_param *kp)
8946{
8947 return sprintf(buffer, "%d", start_readonly);
8948}
8949static int set_ro(const char *val, struct kernel_param *kp)
8950{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03008951 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08008952}
8953
NeilBrown80ca3a42006-07-10 04:44:18 -07008954module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8955module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008956module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008957
Linus Torvalds1da177e2005-04-16 15:20:36 -07008958MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008959MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008960MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008961MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);