blob: d750b52376b97f871663dd2e9bb931da84932b2b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10003 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070037#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110039#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040040#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080041#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070042#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080043#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110044#include <linux/hdreg.h>
45#include <linux/proc_fs.h>
46#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040047#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110048#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070049#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110050#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110051#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/raid/md_p.h>
53#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110055#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110056#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050057#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110060static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
NeilBrown01f96c02011-09-21 15:30:20 +100063/* pers_list is a list of registered personalities protected
64 * by pers_lock.
65 * pers_lock does extra service to protect accesses to
66 * mddev->thread when the mutex cannot be held.
67 */
NeilBrown2604b702006-01-06 00:20:36 -080068static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static DEFINE_SPINLOCK(pers_lock);
70
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050071struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050072EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050073struct module *md_cluster_mod;
74EXPORT_SYMBOL(md_cluster_mod);
75
Bernd Schubert90b08712008-05-23 13:04:38 -070076static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020077static struct workqueue_struct *md_wq;
78static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070079
NeilBrown746d3202013-04-24 11:42:41 +100080static int remove_and_add_spares(struct mddev *mddev,
81 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110082static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
Robert Becker1e509152009-12-14 12:49:58 +110085 * Default number of read corrections we'll attempt on an rdev
86 * before ejecting it from the array. We divide the read error
87 * count by 2 for every hour elapsed between read errors.
88 */
89#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
90/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
92 * is 1000 KB/sec, so the extra system load does not show up that much.
93 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070094 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * subsystem is idle. There is also an 'absolute maximum' reconstruction
96 * speed limit - in case reconstruction slows down your system despite
97 * idle IO detection.
98 *
99 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800100 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
102
103static int sysctl_speed_limit_min = 1000;
104static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100105static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800106{
107 return mddev->sync_speed_min ?
108 mddev->sync_speed_min : sysctl_speed_limit_min;
109}
110
NeilBrownfd01b882011-10-11 16:47:53 +1100111static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800112{
113 return mddev->sync_speed_max ?
114 mddev->sync_speed_max : sysctl_speed_limit_max;
115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117static struct ctl_table_header *raid_table_header;
118
Joe Perches82592c32013-11-14 15:16:18 +1100119static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .procname = "speed_limit_min",
122 .data = &sysctl_speed_limit_min,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 },
127 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .procname = "speed_limit_max",
129 .data = &sysctl_speed_limit_max,
130 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700131 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800132 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800134 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Joe Perches82592c32013-11-14 15:16:18 +1100137static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .procname = "raid",
140 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700141 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .child = raid_table,
143 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800144 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Joe Perches82592c32013-11-14 15:16:18 +1100147static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .procname = "dev",
150 .maxlen = 0,
151 .mode = 0555,
152 .child = raid_dir_table,
153 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800154 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700157static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
NeilBrownf91de922005-11-08 21:39:36 -0800159static int start_readonly;
160
NeilBrowna167f662010-10-26 18:31:13 +1100161/* bio_clone_mddev
162 * like bio_clone, but with a local bio set
163 */
164
NeilBrowna167f662010-10-26 18:31:13 +1100165struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100166 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100167{
168 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100169
170 if (!mddev || !mddev->bio_set)
171 return bio_alloc(gfp_mask, nr_iovecs);
172
Kent Overstreet395c72a2012-09-06 15:34:55 -0700173 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100174 if (!b)
175 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100176 return b;
177}
178EXPORT_SYMBOL_GPL(bio_alloc_mddev);
179
180struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100181 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100182{
NeilBrowna167f662010-10-26 18:31:13 +1100183 if (!mddev || !mddev->bio_set)
184 return bio_clone(bio, gfp_mask);
185
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700186 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100187}
188EXPORT_SYMBOL_GPL(bio_clone_mddev);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
NeilBrownd7603b72006-01-06 00:20:30 -0800191 * We have a system wide 'event count' that is incremented
192 * on any 'interesting' event, and readers of /proc/mdstat
193 * can use 'poll' or 'select' to find out when the event
194 * count increases.
195 *
196 * Events are:
197 * start array, stop array, error, add device, remove device,
198 * start build, activate spare
199 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800200static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800201static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100202void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800203{
204 atomic_inc(&md_event_count);
205 wake_up(&md_event_waiters);
206}
NeilBrown29269552006-03-27 01:18:10 -0800207EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800208
209/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * Enables to iterate over all existing md arrays
211 * all_mddevs_lock protects this list.
212 */
213static LIST_HEAD(all_mddevs);
214static DEFINE_SPINLOCK(all_mddevs_lock);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
217 * iterates through all used mddevs in the system.
218 * We take care to grab the all_mddevs_lock whenever navigating
219 * the list, and to always hold a refcount when unlocked.
220 * Any code which breaks out of this loop while own
221 * a reference to the current mddev and must mddev_put it.
222 */
NeilBrownfd01b882011-10-11 16:47:53 +1100223#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000225 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100226 _tmp = all_mddevs.next; \
227 _mddev = NULL;}); \
228 ({ if (_tmp != &all_mddevs) \
229 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100231 if (_mddev) mddev_put(_mddev); \
232 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
233 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100235 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 )
237
NeilBrown409c57f2009-03-31 14:39:39 +1100238/* Rather than calling directly into the personality make_request function,
239 * IO requests come here first so that we can check if the device is
240 * being suspended pending a reconfiguration.
241 * We hold a refcount over the call to ->make_request. By the time that
242 * call has finished, the bio has been linked into some internal structure
243 * and so is visible to ->quiesce(), so we don't need the refcount any more.
244 */
Jens Axboedece1632015-11-05 10:41:16 -0700245static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
NeilBrown49077322010-03-25 16:20:56 +1100247 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100248 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500249 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800250 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100251
Kent Overstreet54efd502015-04-23 22:37:18 -0700252 blk_queue_split(q, &bio, q->bio_split);
253
NeilBrown274d8cb2016-01-04 16:16:58 +1100254 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100255 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700256 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100257 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100258 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200259 if (bio_sectors(bio) != 0)
260 bio->bi_error = -EROFS;
261 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700262 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100263 }
NeilBrown0ca69882011-01-14 09:14:33 +1100264 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100265 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200266 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100267 DEFINE_WAIT(__wait);
268 for (;;) {
269 prepare_to_wait(&mddev->sb_wait, &__wait,
270 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200271 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100272 break;
273 rcu_read_unlock();
274 schedule();
275 rcu_read_lock();
276 }
277 finish_wait(&mddev->sb_wait, &__wait);
278 }
279 atomic_inc(&mddev->active_io);
280 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100281
Chris Masone91ece52011-02-07 19:21:48 -0500282 /*
283 * save the sectors now since our bio can
284 * go away inside make_request
285 */
286 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700287 /* bio could be mergeable after passing to underlayer */
288 bio->bi_rw &= ~REQ_NOMERGE;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200289 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100290
Gu Zheng74672d02015-04-03 08:44:47 +0800291 cpu = part_stat_lock();
292 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
293 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
294 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100295
NeilBrown409c57f2009-03-31 14:39:39 +1100296 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
297 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700298
299 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
NeilBrown9e35b992010-04-06 14:23:02 +1000302/* mddev_suspend makes sure no new requests are submitted
303 * to the device, and that any requests that have been submitted
304 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100305 * Once mddev_detach() is called and completes, the module will be
306 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000307 */
NeilBrownfd01b882011-10-11 16:47:53 +1100308void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100309{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200310 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100311 if (mddev->suspended++)
312 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100313 synchronize_rcu();
314 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
315 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500316
317 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100318}
NeilBrown390ee602010-06-01 19:37:27 +1000319EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100320
NeilBrownfd01b882011-10-11 16:47:53 +1100321void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100322{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100323 if (--mddev->suspended)
324 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100325 wake_up(&mddev->sb_wait);
326 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500327
Jonathan Brassow47525e52012-05-22 13:55:29 +1000328 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500329 md_wakeup_thread(mddev->thread);
330 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100331}
NeilBrown390ee602010-06-01 19:37:27 +1000332EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100333
NeilBrownfd01b882011-10-11 16:47:53 +1100334int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000335{
NeilBrown5c675f82014-12-15 12:56:56 +1100336 struct md_personality *pers = mddev->pers;
337 int ret = 0;
338
339 rcu_read_lock();
340 if (mddev->suspended)
341 ret = 1;
342 else if (pers && pers->congested)
343 ret = pers->congested(mddev, bits);
344 rcu_read_unlock();
345 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000346}
NeilBrown5c675f82014-12-15 12:56:56 +1100347EXPORT_SYMBOL_GPL(mddev_congested);
348static int md_congested(void *data, int bits)
349{
350 struct mddev *mddev = data;
351 return mddev_congested(mddev, bits);
352}
NeilBrown3fa841d2009-09-23 18:10:29 +1000353
NeilBrowna2826aa2009-12-14 12:49:49 +1100354/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200355 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100356 */
357
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200358static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100359{
NeilBrown3cb03002011-10-11 16:45:26 +1100360 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100361 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100362
363 rdev_dec_pending(rdev, mddev);
364
365 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200366 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200367 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100368 }
369 bio_put(bio);
370}
371
NeilBrowna7a07e62010-12-09 16:04:25 +1100372static void md_submit_flush_data(struct work_struct *ws);
373
NeilBrowna035fc32010-12-09 16:17:51 +1100374static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100375{
NeilBrownfd01b882011-10-11 16:47:53 +1100376 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100377 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100378
NeilBrowna7a07e62010-12-09 16:04:25 +1100379 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
380 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100381 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100382 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100383 if (rdev->raid_disk >= 0 &&
384 !test_bit(Faulty, &rdev->flags)) {
385 /* Take two references, one is dropped
386 * when request finishes, one after
387 * we reclaim rcu_read_lock
388 */
389 struct bio *bi;
390 atomic_inc(&rdev->nr_pending);
391 atomic_inc(&rdev->nr_pending);
392 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000393 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100395 bi->bi_private = rdev;
396 bi->bi_bdev = rdev->bdev;
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 {
Mike Christie28a8f0d2016-06-05 14:32:25 -0500417 bio->bi_rw &= ~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
Shaohua Lia62ab492016-01-06 14:37:13 -08001607 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) {
1608 set_bit(MD_HAS_JOURNAL, &mddev->flags);
1609 if (mddev->recovery_cp == MaxSector)
1610 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
1611 }
NeilBrown41158c72005-06-21 17:17:25 -07001612 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001613 /* Insist of good event counter while assembling, except for
1614 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001616 if (rdev->desc_nr >= 0 &&
1617 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001618 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1619 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001620 if (ev1 < mddev->events)
1621 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001622 } else if (mddev->bitmap) {
1623 /* If adding to array with a bitmap, then we can accept an
1624 * older device, but not too old.
1625 */
NeilBrown41158c72005-06-21 17:17:25 -07001626 if (ev1 < mddev->bitmap->events_cleared)
1627 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001628 if (ev1 < mddev->events)
1629 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001630 } else {
1631 if (ev1 < mddev->events)
1632 /* just a hot-add of a new device, leave raid_disk at -1 */
1633 return 0;
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (mddev->level != LEVEL_MULTIPATH) {
1636 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001637 if (rdev->desc_nr < 0 ||
1638 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001639 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001640 rdev->desc_nr = -1;
1641 } else
1642 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001644 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001646 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001647 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 break;
Song Liubac624f2015-08-13 14:31:55 -07001649 case MD_DISK_ROLE_JOURNAL: /* journal device */
1650 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1651 /* journal device without journal feature */
1652 printk(KERN_WARNING
1653 "md: journal device provided without journal feature, ignoring the device\n");
1654 return -EINVAL;
1655 }
1656 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001657 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001658 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 default:
NeilBrownf4667222013-12-09 12:04:56 +11001661 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001662 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001663 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001664 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001665 if (!(le32_to_cpu(sb->feature_map) &
1666 MD_FEATURE_RECOVERY_BITMAP))
1667 rdev->saved_raid_disk = -1;
1668 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001669 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 rdev->raid_disk = role;
1671 break;
1672 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001673 if (sb->devflags & WriteMostly1)
1674 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001675 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1676 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001677 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001678 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return 0;
1681}
1682
NeilBrownfd01b882011-10-11 16:47:53 +11001683static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001686 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int max_dev, i;
1688 /* make rdev->sb match mddev and rdev data. */
1689
Namhyung Kim65a06f062011-07-27 11:00:36 +10001690 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 sb->feature_map = 0;
1693 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001694 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 memset(sb->pad3, 0, sizeof(sb->pad3));
1696
1697 sb->utime = cpu_to_le64((__u64)mddev->utime);
1698 sb->events = cpu_to_le64(mddev->events);
1699 if (mddev->in_sync)
1700 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001701 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1702 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 else
1704 sb->resync_offset = cpu_to_le64(0);
1705
NeilBrown1c05b4b2006-10-21 10:24:08 -07001706 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001707
NeilBrownf0ca3402006-02-02 14:28:04 -08001708 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001709 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001710 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001711 sb->level = cpu_to_le32(mddev->level);
1712 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001713
NeilBrownaeb9b2112011-08-25 14:43:08 +10001714 if (test_bit(WriteMostly, &rdev->flags))
1715 sb->devflags |= WriteMostly1;
1716 else
1717 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001718 sb->data_offset = cpu_to_le64(rdev->data_offset);
1719 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001720
NeilBrownc3d97142009-12-14 12:49:52 +11001721 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1722 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001723 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001724 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001725
Shaohua Lif2076e72015-10-08 21:54:12 -07001726 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001727 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001728 sb->feature_map |=
1729 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1730 sb->recovery_offset =
1731 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001732 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1733 sb->feature_map |=
1734 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001735 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001736 /* Note: recovery_offset and journal_tail share space */
1737 if (test_bit(Journal, &rdev->flags))
1738 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001739 if (test_bit(Replacement, &rdev->flags))
1740 sb->feature_map |=
1741 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001742
NeilBrownf6705572006-03-27 01:18:11 -08001743 if (mddev->reshape_position != MaxSector) {
1744 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1745 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1746 sb->new_layout = cpu_to_le32(mddev->new_layout);
1747 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1748 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001749 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001750 if (mddev->delta_disks == 0 &&
1751 mddev->reshape_backwards)
1752 sb->feature_map
1753 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001754 if (rdev->new_data_offset != rdev->data_offset) {
1755 sb->feature_map
1756 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1757 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1758 - rdev->data_offset));
1759 }
NeilBrownf6705572006-03-27 01:18:11 -08001760 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001761
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001762 if (mddev_is_clustered(mddev))
1763 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1764
NeilBrown2699b672011-07-28 11:31:47 +10001765 if (rdev->badblocks.count == 0)
1766 /* Nothing to do for bad blocks*/ ;
1767 else if (sb->bblog_offset == 0)
1768 /* Cannot record bad blocks on this device */
1769 md_error(mddev, rdev);
1770 else {
1771 struct badblocks *bb = &rdev->badblocks;
1772 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1773 u64 *p = bb->page;
1774 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1775 if (bb->changed) {
1776 unsigned seq;
1777
1778retry:
1779 seq = read_seqbegin(&bb->lock);
1780
1781 memset(bbp, 0xff, PAGE_SIZE);
1782
1783 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001784 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001785 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1786 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001787 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001788 }
NeilBrownd0962932012-03-19 12:46:41 +11001789 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001790 if (read_seqretry(&bb->lock, seq))
1791 goto retry;
1792
1793 bb->sector = (rdev->sb_start +
1794 (int)le32_to_cpu(sb->bblog_offset));
1795 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001796 }
1797 }
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001800 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (rdev2->desc_nr+1 > max_dev)
1802 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001803
NeilBrown70471da2009-08-03 10:59:57 +10001804 if (max_dev > le32_to_cpu(sb->max_dev)) {
1805 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001806 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001807 rdev->sb_size = max_dev * 2 + 256;
1808 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1809 if (rdev->sb_size & bmask)
1810 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001811 } else
1812 max_dev = le32_to_cpu(sb->max_dev);
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001815 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001816
Song Liua97b7892015-10-08 21:54:09 -07001817 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1818 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
NeilBrowndafb20f2012-03-19 12:46:39 +11001820 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001822 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001823 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001824 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001826 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001827 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001828 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001829 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 else
Song Liuc4d4c912015-08-13 14:31:54 -07001831 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 sb->sb_csum = calc_sb_1_csum(sb);
1835}
1836
Chris Webb0cd17fe2008-06-28 08:31:46 +10001837static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001838super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001839{
1840 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001841 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001842 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001843 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001844 if (rdev->data_offset != rdev->new_data_offset)
1845 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001846 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001847 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001848 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001849 max_sectors -= rdev->data_offset;
1850 if (!num_sectors || num_sectors > max_sectors)
1851 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001852 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001853 /* minor version 0 with bitmap we can't move */
1854 return 0;
1855 } else {
1856 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001857 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001858 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001859 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001860 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001861 if (!num_sectors || num_sectors > max_sectors)
1862 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001863 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001864 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001865 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001866 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001867 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001868 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001869 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001870 rdev->sb_page);
1871 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001872 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001873
1874}
1875
1876static int
1877super_1_allow_new_offset(struct md_rdev *rdev,
1878 unsigned long long new_offset)
1879{
1880 /* All necessary checks on new >= old have been done */
1881 struct bitmap *bitmap;
1882 if (new_offset >= rdev->data_offset)
1883 return 1;
1884
1885 /* with 1.0 metadata, there is no metadata to tread on
1886 * so we can always move back */
1887 if (rdev->mddev->minor_version == 0)
1888 return 1;
1889
1890 /* otherwise we must be sure not to step on
1891 * any metadata, so stay:
1892 * 36K beyond start of superblock
1893 * beyond end of badblocks
1894 * beyond write-intent bitmap
1895 */
1896 if (rdev->sb_start + (32+4)*2 > new_offset)
1897 return 0;
1898 bitmap = rdev->mddev->bitmap;
1899 if (bitmap && !rdev->mddev->bitmap_info.file &&
1900 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001901 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001902 return 0;
1903 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1904 return 0;
1905
1906 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001907}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Adrian Bunk75c96f82005-05-05 16:16:09 -07001909static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 [0] = {
1911 .name = "0.90.0",
1912 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001913 .load_super = super_90_load,
1914 .validate_super = super_90_validate,
1915 .sync_super = super_90_sync,
1916 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001917 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 },
1919 [1] = {
1920 .name = "md-1",
1921 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001922 .load_super = super_1_load,
1923 .validate_super = super_1_validate,
1924 .sync_super = super_1_sync,
1925 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001926 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 },
1928};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
NeilBrownfd01b882011-10-11 16:47:53 +11001930static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001931{
1932 if (mddev->sync_super) {
1933 mddev->sync_super(mddev, rdev);
1934 return;
1935 }
1936
1937 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1938
1939 super_types[mddev->major_version].sync_super(mddev, rdev);
1940}
1941
NeilBrownfd01b882011-10-11 16:47:53 +11001942static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
NeilBrown3cb03002011-10-11 16:45:26 +11001944 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
NeilBrown4b809912008-07-21 17:05:25 +10001946 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001947 rdev_for_each_rcu(rdev, mddev1) {
1948 if (test_bit(Faulty, &rdev->flags) ||
1949 test_bit(Journal, &rdev->flags) ||
1950 rdev->raid_disk == -1)
1951 continue;
1952 rdev_for_each_rcu(rdev2, mddev2) {
1953 if (test_bit(Faulty, &rdev2->flags) ||
1954 test_bit(Journal, &rdev2->flags) ||
1955 rdev2->raid_disk == -1)
1956 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001957 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001958 rdev2->bdev->bd_contains) {
1959 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001960 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001961 }
Song Liu0b020e82015-09-03 23:00:35 -07001962 }
1963 }
NeilBrown4b809912008-07-21 17:05:25 +10001964 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return 0;
1966}
1967
1968static LIST_HEAD(pending_raid_disks);
1969
Andre Nollac5e7112009-08-03 10:59:47 +10001970/*
1971 * Try to register data integrity profile for an mddev
1972 *
1973 * This is called when an array is started and after a disk has been kicked
1974 * from the array. It only succeeds if all working and active component devices
1975 * are integrity capable with matching profiles.
1976 */
NeilBrownfd01b882011-10-11 16:47:53 +11001977int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001978{
NeilBrown3cb03002011-10-11 16:45:26 +11001979 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001980
Andre Nollac5e7112009-08-03 10:59:47 +10001981 if (list_empty(&mddev->disks))
1982 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001983 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1984 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001985 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001986 /* skip spares and non-functional disks */
1987 if (test_bit(Faulty, &rdev->flags))
1988 continue;
1989 if (rdev->raid_disk < 0)
1990 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001991 if (!reference) {
1992 /* Use the first rdev as the reference */
1993 reference = rdev;
1994 continue;
1995 }
1996 /* does this rdev's profile match the reference profile? */
1997 if (blk_integrity_compare(reference->bdev->bd_disk,
1998 rdev->bdev->bd_disk) < 0)
1999 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002000 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002001 if (!reference || !bdev_get_integrity(reference->bdev))
2002 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002003 /*
2004 * All component devices are integrity capable and have matching
2005 * profiles, register the common profile for the md device.
2006 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002007 blk_integrity_register(mddev->gendisk,
2008 bdev_get_integrity(reference->bdev));
2009
Martin K. Petersena91a2782011-03-17 11:11:05 +01002010 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2011 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2012 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2013 mdname(mddev));
2014 return -EINVAL;
2015 }
Andre Nollac5e7112009-08-03 10:59:47 +10002016 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002017}
Andre Nollac5e7112009-08-03 10:59:47 +10002018EXPORT_SYMBOL(md_integrity_register);
2019
Dan Williams1501efa2016-01-13 16:00:07 -08002020/*
2021 * Attempt to add an rdev, but only if it is consistent with the current
2022 * integrity profile
2023 */
2024int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002025{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002026 struct blk_integrity *bi_rdev;
2027 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002028 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002029
2030 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002031 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002032
2033 bi_rdev = bdev_get_integrity(rdev->bdev);
2034 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002035
2036 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002037 return 0;
2038
2039 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2040 printk(KERN_NOTICE "%s: incompatible integrity profile for %s\n",
2041 mdname(mddev), bdevname(rdev->bdev, name));
2042 return -ENXIO;
2043 }
2044
2045 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002046}
2047EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002048
NeilBrownf72ffdd2014-09-30 14:23:59 +10002049static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002051 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002052 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002053 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Dan Williams11e2ede2008-04-30 00:52:32 -07002055 /* prevent duplicates */
2056 if (find_rdev(mddev, rdev->bdev->bd_dev))
2057 return -EEXIST;
2058
Andre Nolldd8ac332009-03-31 14:33:13 +11002059 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002060 if (!test_bit(Journal, &rdev->flags) &&
2061 rdev->sectors &&
2062 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002063 if (mddev->pers) {
2064 /* Cannot change size, so fail
2065 * If mddev->level <= 0, then we don't care
2066 * about aligning sizes (e.g. linear)
2067 */
2068 if (mddev->level > 0)
2069 return -ENOSPC;
2070 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002071 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 /* Verify rdev->desc_nr is unique.
2075 * If it is -1, assign a free number, else
2076 * check number is not in use
2077 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002078 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (rdev->desc_nr < 0) {
2080 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002081 if (mddev->pers)
2082 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002083 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 choice++;
2085 rdev->desc_nr = choice;
2086 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002087 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002088 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002092 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002093 if (!test_bit(Journal, &rdev->flags) &&
2094 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002095 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2096 mdname(mddev), mddev->max_disks);
2097 return -EBUSY;
2098 }
NeilBrown19133a42005-11-08 21:39:35 -08002099 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002100 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002103 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002104
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002105 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002106 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002107
Tejun Heo0762b8b2008-08-25 19:56:12 +09002108 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002109 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2110 /* failure here is OK */;
2111 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002112
NeilBrown4b809912008-07-21 17:05:25 +10002113 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002114 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002115
2116 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002117 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002120
2121 fail:
2122 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2123 b, mdname(mddev));
2124 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
NeilBrown177a99b2008-02-06 01:39:56 -08002127static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002128{
NeilBrown3cb03002011-10-11 16:45:26 +11002129 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002130 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002131 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002132}
2133
NeilBrownf72ffdd2014-09-30 14:23:59 +10002134static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002137
Tejun Heo49731ba2011-01-14 18:43:57 +01002138 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002139 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2141 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002142 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002143 sysfs_put(rdev->sysfs_state);
2144 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002145 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002146 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002147 * writing to 'remove' to "dev/state". We also need
2148 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002149 */
NeilBrown4b809912008-07-21 17:05:25 +10002150 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002151 INIT_WORK(&rdev->del_work, md_delayed_delete);
2152 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002153 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
2156/*
2157 * prevent the device from being mounted, repartitioned or
2158 * otherwise reused by a RAID array (or any other kernel
2159 * subsystem), by bd_claiming the device.
2160 */
NeilBrown3cb03002011-10-11 16:45:26 +11002161static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 int err = 0;
2164 struct block_device *bdev;
2165 char b[BDEVNAME_SIZE];
2166
Tejun Heod4d77622010-11-13 11:55:18 +01002167 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002168 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (IS_ERR(bdev)) {
2170 printk(KERN_ERR "md: could not open %s.\n",
2171 __bdevname(dev, b));
2172 return PTR_ERR(bdev);
2173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 rdev->bdev = bdev;
2175 return err;
2176}
2177
NeilBrown3cb03002011-10-11 16:45:26 +11002178static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 struct block_device *bdev = rdev->bdev;
2181 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002182 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185void md_autodetect_dev(dev_t dev);
2186
NeilBrownf72ffdd2014-09-30 14:23:59 +10002187static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 printk(KERN_INFO "md: export_rdev(%s)\n",
2192 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002193 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002195 if (test_bit(AutoDetected, &rdev->flags))
2196 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197#endif
2198 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002199 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002202void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 unbind_rdev_from_array(rdev);
2205 export_rdev(rdev);
2206}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002207EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
NeilBrownfd01b882011-10-11 16:47:53 +11002209static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
NeilBrown0638bb02014-09-25 17:43:47 +10002211 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
NeilBrown0638bb02014-09-25 17:43:47 +10002213 while (!list_empty(&mddev->disks)) {
2214 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2215 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002216 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 mddev->raid_disks = 0;
2219 mddev->major_version = 0;
2220}
2221
NeilBrownf72ffdd2014-09-30 14:23:59 +10002222static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
NeilBrown42543762006-06-26 00:27:57 -07002224 /* Update each superblock (in-memory image), but
2225 * if we are allowed to, skip spares which already
2226 * have the right event counter, or have one earlier
2227 * (which would mean they aren't being marked as dirty
2228 * with the rest of the array)
2229 */
NeilBrown3cb03002011-10-11 16:45:26 +11002230 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002231 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002232 if (rdev->sb_events == mddev->events ||
2233 (nospares &&
2234 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002235 rdev->sb_events+1 == mddev->events)) {
2236 /* Don't update this superblock */
2237 rdev->sb_loaded = 2;
2238 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002239 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002240 rdev->sb_loaded = 1;
2241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243}
2244
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002245static bool does_sb_need_changing(struct mddev *mddev)
2246{
2247 struct md_rdev *rdev;
2248 struct mdp_superblock_1 *sb;
2249 int role;
2250
2251 /* Find a good rdev */
2252 rdev_for_each(rdev, mddev)
2253 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2254 break;
2255
2256 /* No good device found. */
2257 if (!rdev)
2258 return false;
2259
2260 sb = page_address(rdev->sb_page);
2261 /* Check if a device has become faulty or a spare become active */
2262 rdev_for_each(rdev, mddev) {
2263 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2264 /* Device activated? */
2265 if (role == 0xffff && rdev->raid_disk >=0 &&
2266 !test_bit(Faulty, &rdev->flags))
2267 return true;
2268 /* Device turned faulty? */
2269 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2270 return true;
2271 }
2272
2273 /* Check if any mddev parameters have changed */
2274 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2275 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002276 (mddev->layout != le64_to_cpu(sb->layout)) ||
2277 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2278 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2279 return true;
2280
2281 return false;
2282}
2283
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002284void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
NeilBrown3cb03002011-10-11 16:45:26 +11002286 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002287 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002288 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002289 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002290 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
NeilBrownd87f0642013-04-24 11:42:40 +10002292 if (mddev->ro) {
2293 if (force_change)
2294 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2295 return;
2296 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002297
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002298repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002299 if (mddev_is_clustered(mddev)) {
2300 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2301 force_change = 1;
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002302 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2303 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002304 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002305 /* Has someone else has updated the sb */
2306 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002307 if (ret == 0)
2308 md_cluster_ops->metadata_update_cancel(mddev);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002309 bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING),
2310 BIT(MD_CHANGE_DEVS) |
2311 BIT(MD_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002312 return;
2313 }
2314 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002315
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002316 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002317 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002318 if (rdev->raid_disk >= 0 &&
2319 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002320 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002321 !test_bit(In_sync, &rdev->flags) &&
2322 mddev->curr_resync_completed > rdev->recovery_offset)
2323 rdev->recovery_offset = mddev->curr_resync_completed;
2324
NeilBrownf72ffdd2014-09-30 14:23:59 +10002325 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002326 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002327 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002328 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002329 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002330 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002331 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002332 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002333 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002334 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002335 md_error(mddev, rdev);
2336 }
2337 clear_bit(Blocked, &rdev->flags);
2338 clear_bit(BlockedBadBlocks, &rdev->flags);
2339 wake_up(&rdev->blocked_wait);
2340 }
2341 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002342 wake_up(&mddev->sb_wait);
2343 return;
2344 }
2345
NeilBrown85572d72014-12-15 12:56:56 +11002346 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002347
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002348 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002349
NeilBrown850b2b422006-10-03 01:15:46 -07002350 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2351 force_change = 1;
2352 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2353 /* just a clean<-> dirty transition, possibly leave spares alone,
2354 * though if events isn't the right even/odd, we will have to do
2355 * spares after all
2356 */
2357 nospares = 1;
2358 if (force_change)
2359 nospares = 0;
2360 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002361 /* If the array is degraded, then skipping spares is both
2362 * dangerous and fairly pointless.
2363 * Dangerous because a device that was removed from the array
2364 * might have a event_count that still looks up-to-date,
2365 * so it can be re-added without a resync.
2366 * Pointless because if there are any spares to skip,
2367 * then a recovery will happen and soon that array won't
2368 * be degraded any more and the spare can go back to sleep then.
2369 */
NeilBrown850b2b422006-10-03 01:15:46 -07002370 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002371
NeilBrown06d91a52005-06-21 17:17:12 -07002372 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002373
2374 /* If this is just a dirty<->clean transition, and the array is clean
2375 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002376 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002377 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002378 && mddev->can_decrease_events
2379 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002380 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002381 mddev->can_decrease_events = 0;
2382 } else {
NeilBrown42543762006-06-26 00:27:57 -07002383 /* otherwise we have to go forward and ... */
2384 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002385 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
NeilBrown403df472014-09-30 15:52:29 +10002388 /*
2389 * This 64-bit counter should never wrap.
2390 * Either we are in around ~1 trillion A.C., assuming
2391 * 1 reboot per second, or we have a bug...
2392 */
2393 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002394
NeilBrowndafb20f2012-03-19 12:46:39 +11002395 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002396 if (rdev->badblocks.changed)
2397 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002398 if (test_bit(Faulty, &rdev->flags))
2399 set_bit(FaultRecorded, &rdev->flags);
2400 }
NeilBrown2699b672011-07-28 11:31:47 +10002401
NeilBrowne6910632008-02-06 01:39:51 -08002402 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002403 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
NeilBrown36a4e1f2011-10-07 14:23:17 +11002405 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2406 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
NeilBrown4ad13662007-07-17 04:06:13 -07002408 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002409 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002411
NeilBrown42543762006-06-26 00:27:57 -07002412 if (rdev->sb_loaded != 1)
2413 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
NeilBrownf4667222013-12-09 12:04:56 +11002415 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002416 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002417 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002418 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002419 pr_debug("md: (write) %s's sb offset: %llu\n",
2420 bdevname(rdev->bdev, b),
2421 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002422 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002423 if (rdev->badblocks.size) {
2424 md_super_write(mddev, rdev,
2425 rdev->badblocks.sector,
2426 rdev->badblocks.size << 9,
2427 rdev->bb_page);
2428 rdev->badblocks.size = 0;
2429 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002430
NeilBrownf4667222013-12-09 12:04:56 +11002431 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002432 pr_debug("md: %s (skipping faulty)\n",
2433 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002434
NeilBrown7bfa19f2005-06-21 17:17:28 -07002435 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 /* only need to write one superblock... */
2437 break;
2438 }
NeilBrowna9701a32005-11-08 21:39:34 -08002439 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002440 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002441
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002442 if (mddev_is_clustered(mddev) && ret == 0)
2443 md_cluster_ops->metadata_update_finish(mddev);
2444
NeilBrown850b2b422006-10-03 01:15:46 -07002445 if (mddev->in_sync != sync_req ||
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002446 !bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING),
2447 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002448 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002449 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002450 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002451 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2452 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002453
NeilBrowndafb20f2012-03-19 12:46:39 +11002454 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002455 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2456 clear_bit(Blocked, &rdev->flags);
2457
2458 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002459 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002460 clear_bit(BlockedBadBlocks, &rdev->flags);
2461 wake_up(&rdev->blocked_wait);
2462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002464EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002466static int add_bound_rdev(struct md_rdev *rdev)
2467{
2468 struct mddev *mddev = rdev->mddev;
2469 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002470 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002471
Shaohua Li87d4d912016-01-06 14:37:14 -08002472 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002473 /* If there is hot_add_disk but no hot_remove_disk
2474 * then added disks for geometry changes,
2475 * and should be added immediately.
2476 */
2477 super_types[mddev->major_version].
2478 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002479 if (add_journal)
2480 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002481 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002482 if (add_journal)
2483 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002484 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002485 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002486 return err;
2487 }
2488 }
2489 sysfs_notify_dirent_safe(rdev->sysfs_state);
2490
2491 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2492 if (mddev->degraded)
2493 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2494 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2495 md_new_event(mddev);
2496 md_wakeup_thread(mddev->thread);
2497 return 0;
2498}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Andre Noll7f6ce762008-03-23 18:34:54 +01002500/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002501 * We want to accept with case. For this we use cmd_match.
2502 */
2503static int cmd_match(const char *cmd, const char *str)
2504{
2505 /* See if cmd, written into a sysfs file, matches
2506 * str. They must either be the same, or cmd can
2507 * have a trailing newline
2508 */
2509 while (*cmd && *str && *cmd == *str) {
2510 cmd++;
2511 str++;
2512 }
2513 if (*cmd == '\n')
2514 cmd++;
2515 if (*str || *cmd)
2516 return 0;
2517 return 1;
2518}
2519
NeilBrown86e6ffd2005-11-08 21:39:24 -08002520struct rdev_sysfs_entry {
2521 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002522 ssize_t (*show)(struct md_rdev *, char *);
2523 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002524};
2525
2526static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002527state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002528{
2529 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002530 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002531 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002532
NeilBrown758bfc82014-12-15 12:56:59 +11002533 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002534 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002535 len+= sprintf(page+len, "%sfaulty",sep);
2536 sep = ",";
2537 }
NeilBrown758bfc82014-12-15 12:56:59 +11002538 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002539 len += sprintf(page+len, "%sin_sync",sep);
2540 sep = ",";
2541 }
Shaohua Liac6096e2015-10-04 09:20:11 -07002542 if (test_bit(Journal, &flags)) {
2543 len += sprintf(page+len, "%sjournal",sep);
2544 sep = ",";
2545 }
NeilBrown758bfc82014-12-15 12:56:59 +11002546 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002547 len += sprintf(page+len, "%swrite_mostly",sep);
2548 sep = ",";
2549 }
NeilBrown758bfc82014-12-15 12:56:59 +11002550 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002551 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002552 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002553 len += sprintf(page+len, "%sblocked", sep);
2554 sep = ",";
2555 }
NeilBrown758bfc82014-12-15 12:56:59 +11002556 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002557 !test_bit(Journal, &flags) &&
NeilBrown758bfc82014-12-15 12:56:59 +11002558 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002559 len += sprintf(page+len, "%sspare", sep);
2560 sep = ",";
2561 }
NeilBrown758bfc82014-12-15 12:56:59 +11002562 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002563 len += sprintf(page+len, "%swrite_error", sep);
2564 sep = ",";
2565 }
NeilBrown758bfc82014-12-15 12:56:59 +11002566 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002567 len += sprintf(page+len, "%swant_replacement", sep);
2568 sep = ",";
2569 }
NeilBrown758bfc82014-12-15 12:56:59 +11002570 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002571 len += sprintf(page+len, "%sreplacement", sep);
2572 sep = ",";
2573 }
2574
NeilBrown86e6ffd2005-11-08 21:39:24 -08002575 return len+sprintf(page+len, "\n");
2576}
2577
NeilBrown45dc2de2006-06-26 00:27:58 -07002578static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002579state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002580{
2581 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002582 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002583 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002584 * writemostly - sets write_mostly
2585 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002586 * blocked - sets the Blocked flags
2587 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002588 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002589 * -insync - clear Insync for a device with a slot assigned,
2590 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002591 * write_error - sets WriteErrorSeen
2592 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002593 */
2594 int err = -EINVAL;
2595 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2596 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002597 if (test_bit(Faulty, &rdev->flags))
2598 err = 0;
2599 else
2600 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002601 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002602 if (rdev->mddev->pers) {
2603 clear_bit(Blocked, &rdev->flags);
2604 remove_and_add_spares(rdev->mddev, rdev);
2605 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002606 if (rdev->raid_disk >= 0)
2607 err = -EBUSY;
2608 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002609 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002610 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002611 if (mddev_is_clustered(mddev))
2612 err = md_cluster_ops->remove_disk(mddev, rdev);
2613
2614 if (err == 0) {
2615 md_kick_rdev_from_array(rdev);
2616 if (mddev->pers)
2617 md_update_sb(mddev, 1);
2618 md_new_event(mddev);
2619 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002620 }
NeilBrownf6556752006-06-26 00:28:01 -07002621 } else if (cmd_match(buf, "writemostly")) {
2622 set_bit(WriteMostly, &rdev->flags);
2623 err = 0;
2624 } else if (cmd_match(buf, "-writemostly")) {
2625 clear_bit(WriteMostly, &rdev->flags);
2626 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002627 } else if (cmd_match(buf, "blocked")) {
2628 set_bit(Blocked, &rdev->flags);
2629 err = 0;
2630 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002631 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002632 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002633 /* metadata handler doesn't understand badblocks,
2634 * so we need to fail the device
2635 */
2636 md_error(rdev->mddev, rdev);
2637 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002638 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002639 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002640 wake_up(&rdev->blocked_wait);
2641 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2642 md_wakeup_thread(rdev->mddev->thread);
2643
2644 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002645 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2646 set_bit(In_sync, &rdev->flags);
2647 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002648 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2649 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002650 if (rdev->mddev->pers == NULL) {
2651 clear_bit(In_sync, &rdev->flags);
2652 rdev->saved_raid_disk = rdev->raid_disk;
2653 rdev->raid_disk = -1;
2654 err = 0;
2655 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002656 } else if (cmd_match(buf, "write_error")) {
2657 set_bit(WriteErrorSeen, &rdev->flags);
2658 err = 0;
2659 } else if (cmd_match(buf, "-write_error")) {
2660 clear_bit(WriteErrorSeen, &rdev->flags);
2661 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002662 } else if (cmd_match(buf, "want_replacement")) {
2663 /* Any non-spare device that is not a replacement can
2664 * become want_replacement at any time, but we then need to
2665 * check if recovery is needed.
2666 */
2667 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002668 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002669 !test_bit(Replacement, &rdev->flags))
2670 set_bit(WantReplacement, &rdev->flags);
2671 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2672 md_wakeup_thread(rdev->mddev->thread);
2673 err = 0;
2674 } else if (cmd_match(buf, "-want_replacement")) {
2675 /* Clearing 'want_replacement' is always allowed.
2676 * Once replacements starts it is too late though.
2677 */
2678 err = 0;
2679 clear_bit(WantReplacement, &rdev->flags);
2680 } else if (cmd_match(buf, "replacement")) {
2681 /* Can only set a device as a replacement when array has not
2682 * yet been started. Once running, replacement is automatic
2683 * from spares, or by assigning 'slot'.
2684 */
2685 if (rdev->mddev->pers)
2686 err = -EBUSY;
2687 else {
2688 set_bit(Replacement, &rdev->flags);
2689 err = 0;
2690 }
2691 } else if (cmd_match(buf, "-replacement")) {
2692 /* Similarly, can only clear Replacement before start */
2693 if (rdev->mddev->pers)
2694 err = -EBUSY;
2695 else {
2696 clear_bit(Replacement, &rdev->flags);
2697 err = 0;
2698 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002699 } else if (cmd_match(buf, "re-add")) {
2700 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002701 /* clear_bit is performed _after_ all the devices
2702 * have their local Faulty bit cleared. If any writes
2703 * happen in the meantime in the local node, they
2704 * will land in the local bitmap, which will be synced
2705 * by this node eventually
2706 */
2707 if (!mddev_is_clustered(rdev->mddev) ||
2708 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2709 clear_bit(Faulty, &rdev->flags);
2710 err = add_bound_rdev(rdev);
2711 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002712 } else
2713 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002714 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002715 if (!err)
2716 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002717 return err ? err : len;
2718}
NeilBrown80ca3a42006-07-10 04:44:18 -07002719static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002720__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002721
2722static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002723errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002724{
2725 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2726}
2727
2728static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002729errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002730{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002731 unsigned int n;
2732 int rv;
2733
2734 rv = kstrtouint(buf, 10, &n);
2735 if (rv < 0)
2736 return rv;
2737 atomic_set(&rdev->corrected_errors, n);
2738 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002739}
2740static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002741__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002742
NeilBrown014236d2006-01-06 00:20:55 -08002743static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002744slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002745{
Shaohua Lif2076e72015-10-08 21:54:12 -07002746 if (test_bit(Journal, &rdev->flags))
2747 return sprintf(page, "journal\n");
2748 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002749 return sprintf(page, "none\n");
2750 else
2751 return sprintf(page, "%d\n", rdev->raid_disk);
2752}
2753
2754static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002755slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002756{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002757 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002758 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002759
Shaohua Lif2076e72015-10-08 21:54:12 -07002760 if (test_bit(Journal, &rdev->flags))
2761 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002762 if (strncmp(buf, "none", 4)==0)
2763 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002764 else {
2765 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2766 if (err < 0)
2767 return err;
2768 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002769 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002770 /* Setting 'slot' on an active array requires also
2771 * updating the 'rd%d' link, and communicating
2772 * with the personality with ->hot_*_disk.
2773 * For now we only support removing
2774 * failed/spare devices. This normally happens automatically,
2775 * but not when the metadata is externally managed.
2776 */
NeilBrownc303da62008-02-06 01:39:51 -08002777 if (rdev->raid_disk == -1)
2778 return -EEXIST;
2779 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002780 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002781 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002782 clear_bit(Blocked, &rdev->flags);
2783 remove_and_add_spares(rdev->mddev, rdev);
2784 if (rdev->raid_disk >= 0)
2785 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002786 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2787 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002788 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002789 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002790 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002791 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002792 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002793
2794 if (rdev->raid_disk != -1)
2795 return -EBUSY;
2796
NeilBrownc6751b22011-02-02 11:57:13 +11002797 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2798 return -EBUSY;
2799
Neil Brown6c2fce22008-06-28 08:31:31 +10002800 if (rdev->mddev->pers->hot_add_disk == NULL)
2801 return -EINVAL;
2802
NeilBrownba1b41b2011-01-14 09:14:34 +11002803 if (slot >= rdev->mddev->raid_disks &&
2804 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2805 return -ENOSPC;
2806
Neil Brown6c2fce22008-06-28 08:31:31 +10002807 rdev->raid_disk = slot;
2808 if (test_bit(In_sync, &rdev->flags))
2809 rdev->saved_raid_disk = slot;
2810 else
2811 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002812 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002813 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002814 err = rdev->mddev->pers->
2815 hot_add_disk(rdev->mddev, rdev);
2816 if (err) {
2817 rdev->raid_disk = -1;
2818 return err;
2819 } else
2820 sysfs_notify_dirent_safe(rdev->sysfs_state);
2821 if (sysfs_link_rdev(rdev->mddev, rdev))
2822 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002823 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002824 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002825 if (slot >= rdev->mddev->raid_disks &&
2826 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002827 return -ENOSPC;
2828 rdev->raid_disk = slot;
2829 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002830 clear_bit(Faulty, &rdev->flags);
2831 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002832 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002833 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002834 }
NeilBrown014236d2006-01-06 00:20:55 -08002835 return len;
2836}
2837
NeilBrown014236d2006-01-06 00:20:55 -08002838static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002839__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002840
NeilBrown93c8cad2006-01-06 00:20:56 -08002841static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002842offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002843{
Andrew Morton6961ece2006-01-06 00:20:59 -08002844 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002845}
2846
2847static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002848offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002849{
NeilBrownc6563a82012-05-21 09:27:00 +10002850 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002851 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002852 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002853 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002854 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002855 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002856 /* Must set offset before size, so overlap checks
2857 * can be sane */
2858 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002859 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002860 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002861 return len;
2862}
2863
2864static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002865__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002866
NeilBrownc6563a82012-05-21 09:27:00 +10002867static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2868{
2869 return sprintf(page, "%llu\n",
2870 (unsigned long long)rdev->new_data_offset);
2871}
2872
2873static ssize_t new_offset_store(struct md_rdev *rdev,
2874 const char *buf, size_t len)
2875{
2876 unsigned long long new_offset;
2877 struct mddev *mddev = rdev->mddev;
2878
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002879 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002880 return -EINVAL;
2881
NeilBrownf851b602014-12-11 10:02:10 +11002882 if (mddev->sync_thread ||
2883 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002884 return -EBUSY;
2885 if (new_offset == rdev->data_offset)
2886 /* reset is always permitted */
2887 ;
2888 else if (new_offset > rdev->data_offset) {
2889 /* must not push array size beyond rdev_sectors */
2890 if (new_offset - rdev->data_offset
2891 + mddev->dev_sectors > rdev->sectors)
2892 return -E2BIG;
2893 }
2894 /* Metadata worries about other space details. */
2895
2896 /* decreasing the offset is inconsistent with a backwards
2897 * reshape.
2898 */
2899 if (new_offset < rdev->data_offset &&
2900 mddev->reshape_backwards)
2901 return -EINVAL;
2902 /* Increasing offset is inconsistent with forwards
2903 * reshape. reshape_direction should be set to
2904 * 'backwards' first.
2905 */
2906 if (new_offset > rdev->data_offset &&
2907 !mddev->reshape_backwards)
2908 return -EINVAL;
2909
2910 if (mddev->pers && mddev->persistent &&
2911 !super_types[mddev->major_version]
2912 .allow_new_offset(rdev, new_offset))
2913 return -E2BIG;
2914 rdev->new_data_offset = new_offset;
2915 if (new_offset > rdev->data_offset)
2916 mddev->reshape_backwards = 1;
2917 else if (new_offset < rdev->data_offset)
2918 mddev->reshape_backwards = 0;
2919
2920 return len;
2921}
2922static struct rdev_sysfs_entry rdev_new_offset =
2923__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2924
NeilBrown83303b62006-01-06 00:21:06 -08002925static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002926rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002927{
Andre Nolldd8ac332009-03-31 14:33:13 +11002928 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002929}
2930
NeilBrownc5d79ad2008-02-06 01:39:54 -08002931static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2932{
2933 /* check if two start/length pairs overlap */
2934 if (s1+l1 <= s2)
2935 return 0;
2936 if (s2+l2 <= s1)
2937 return 0;
2938 return 1;
2939}
2940
Dan Williamsb522adc2009-03-31 15:00:31 +11002941static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2942{
2943 unsigned long long blocks;
2944 sector_t new;
2945
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002946 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002947 return -EINVAL;
2948
2949 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2950 return -EINVAL; /* sector conversion overflow */
2951
2952 new = blocks * 2;
2953 if (new != blocks * 2)
2954 return -EINVAL; /* unsigned long long to sector_t overflow */
2955
2956 *sectors = new;
2957 return 0;
2958}
2959
NeilBrown83303b62006-01-06 00:21:06 -08002960static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002961rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002962{
NeilBrownfd01b882011-10-11 16:47:53 +11002963 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002964 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002965 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002966
Shaohua Lif2076e72015-10-08 21:54:12 -07002967 if (test_bit(Journal, &rdev->flags))
2968 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002969 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002970 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002971 if (rdev->data_offset != rdev->new_data_offset)
2972 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002973 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002974 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002975 sectors = super_types[my_mddev->major_version].
2976 rdev_size_change(rdev, sectors);
2977 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002978 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002979 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002980 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002981 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002982 if (!my_mddev->pers->resize)
2983 /* Cannot change size for RAID0 or Linear etc */
2984 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002985 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002986 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002987 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002988
Andre Nolldd8ac332009-03-31 14:33:13 +11002989 rdev->sectors = sectors;
2990 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002991 /* Need to check that all other rdevs with the same
2992 * ->bdev do not overlap. 'rcu' is sufficient to walk
2993 * the rdev lists safely.
2994 * This check does not provide a hard guarantee, it
2995 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002996 */
NeilBrownfd01b882011-10-11 16:47:53 +11002997 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002998 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002999 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003000
NeilBrown8b1afc32014-09-29 15:33:20 +10003001 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003002 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003003 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003004
NeilBrowndafb20f2012-03-19 12:46:39 +11003005 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003006 if (rdev->bdev == rdev2->bdev &&
3007 rdev != rdev2 &&
3008 overlaps(rdev->data_offset, rdev->sectors,
3009 rdev2->data_offset,
3010 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003011 overlap = 1;
3012 break;
3013 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003014 if (overlap) {
3015 mddev_put(mddev);
3016 break;
3017 }
3018 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003019 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003020 if (overlap) {
3021 /* Someone else could have slipped in a size
3022 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003023 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003024 * safe, and trust userspace not to race with
3025 * itself
3026 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003027 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003028 return -EBUSY;
3029 }
3030 }
NeilBrown83303b62006-01-06 00:21:06 -08003031 return len;
3032}
3033
3034static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003035__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003036
NeilBrown3cb03002011-10-11 16:45:26 +11003037static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003038{
3039 unsigned long long recovery_start = rdev->recovery_offset;
3040
3041 if (test_bit(In_sync, &rdev->flags) ||
3042 recovery_start == MaxSector)
3043 return sprintf(page, "none\n");
3044
3045 return sprintf(page, "%llu\n", recovery_start);
3046}
3047
NeilBrown3cb03002011-10-11 16:45:26 +11003048static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003049{
3050 unsigned long long recovery_start;
3051
3052 if (cmd_match(buf, "none"))
3053 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003054 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003055 return -EINVAL;
3056
3057 if (rdev->mddev->pers &&
3058 rdev->raid_disk >= 0)
3059 return -EBUSY;
3060
3061 rdev->recovery_offset = recovery_start;
3062 if (recovery_start == MaxSector)
3063 set_bit(In_sync, &rdev->flags);
3064 else
3065 clear_bit(In_sync, &rdev->flags);
3066 return len;
3067}
3068
3069static struct rdev_sysfs_entry rdev_recovery_start =
3070__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3071
Vishal Vermafc974ee2015-12-24 19:20:34 -07003072/* sysfs access to bad-blocks list.
3073 * We present two files.
3074 * 'bad-blocks' lists sector numbers and lengths of ranges that
3075 * are recorded as bad. The list is truncated to fit within
3076 * the one-page limit of sysfs.
3077 * Writing "sector length" to this file adds an acknowledged
3078 * bad block list.
3079 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3080 * been acknowledged. Writing to this file adds bad blocks
3081 * without acknowledging them. This is largely for testing.
3082 */
NeilBrown3cb03002011-10-11 16:45:26 +11003083static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003084{
3085 return badblocks_show(&rdev->badblocks, page, 0);
3086}
NeilBrown3cb03002011-10-11 16:45:26 +11003087static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003088{
NeilBrownde393cd2011-07-28 11:31:48 +10003089 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3090 /* Maybe that ack was all we needed */
3091 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3092 wake_up(&rdev->blocked_wait);
3093 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003094}
3095static struct rdev_sysfs_entry rdev_bad_blocks =
3096__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3097
NeilBrown3cb03002011-10-11 16:45:26 +11003098static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003099{
3100 return badblocks_show(&rdev->badblocks, page, 1);
3101}
NeilBrown3cb03002011-10-11 16:45:26 +11003102static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003103{
3104 return badblocks_store(&rdev->badblocks, page, len, 1);
3105}
3106static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3107__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3108
NeilBrown86e6ffd2005-11-08 21:39:24 -08003109static struct attribute *rdev_default_attrs[] = {
3110 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003111 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003112 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003113 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003114 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003115 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003116 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003117 &rdev_bad_blocks.attr,
3118 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003119 NULL,
3120};
3121static ssize_t
3122rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3123{
3124 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003125 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003126
3127 if (!entry->show)
3128 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003129 if (!rdev->mddev)
3130 return -EBUSY;
3131 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003132}
3133
3134static ssize_t
3135rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3136 const char *page, size_t length)
3137{
3138 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003139 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003140 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003141 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003142
3143 if (!entry->store)
3144 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003145 if (!capable(CAP_SYS_ADMIN))
3146 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003147 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003148 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003149 if (rdev->mddev == NULL)
3150 rv = -EBUSY;
3151 else
3152 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003153 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003154 }
3155 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003156}
3157
3158static void rdev_free(struct kobject *ko)
3159{
NeilBrown3cb03002011-10-11 16:45:26 +11003160 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003161 kfree(rdev);
3162}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003163static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003164 .show = rdev_attr_show,
3165 .store = rdev_attr_store,
3166};
3167static struct kobj_type rdev_ktype = {
3168 .release = rdev_free,
3169 .sysfs_ops = &rdev_sysfs_ops,
3170 .default_attrs = rdev_default_attrs,
3171};
3172
NeilBrown3cb03002011-10-11 16:45:26 +11003173int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003174{
3175 rdev->desc_nr = -1;
3176 rdev->saved_raid_disk = -1;
3177 rdev->raid_disk = -1;
3178 rdev->flags = 0;
3179 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003180 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003181 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003182 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003183 rdev->sb_loaded = 0;
3184 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003185 atomic_set(&rdev->nr_pending, 0);
3186 atomic_set(&rdev->read_errors, 0);
3187 atomic_set(&rdev->corrected_errors, 0);
3188
3189 INIT_LIST_HEAD(&rdev->same_set);
3190 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003191
3192 /* Add space to store bad block list.
3193 * This reserves the space even on arrays where it cannot
3194 * be used - I wonder if that matters
3195 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003196 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003197}
3198EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199/*
3200 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3201 *
3202 * mark the device faulty if:
3203 *
3204 * - the device is nonexistent (zero size)
3205 * - the device has no valid superblock
3206 *
3207 * a faulty rdev _never_ has rdev->sb set.
3208 */
NeilBrown3cb03002011-10-11 16:45:26 +11003209static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
3211 char b[BDEVNAME_SIZE];
3212 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003213 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 sector_t size;
3215
NeilBrown9ffae0c2006-01-06 00:20:32 -08003216 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (!rdev) {
3218 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3219 return ERR_PTR(-ENOMEM);
3220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
NeilBrown2230dfe2011-07-28 11:31:46 +10003222 err = md_rdev_init(rdev);
3223 if (err)
3224 goto abort_free;
3225 err = alloc_disk_sb(rdev);
3226 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 goto abort_free;
3228
NeilBrownc5d79ad2008-02-06 01:39:54 -08003229 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (err)
3231 goto abort_free;
3232
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003233 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003234
Mike Snitzer77304d22010-11-08 14:39:12 +01003235 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003237 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 "md: %s has zero or unknown size, marking faulty!\n",
3239 bdevname(rdev->bdev,b));
3240 err = -EINVAL;
3241 goto abort_free;
3242 }
3243
3244 if (super_format >= 0) {
3245 err = super_types[super_format].
3246 load_super(rdev, NULL, super_minor);
3247 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003248 printk(KERN_WARNING
3249 "md: %s does not have a valid v%d.%d "
3250 "superblock, not importing!\n",
3251 bdevname(rdev->bdev,b),
3252 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 goto abort_free;
3254 }
3255 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003256 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 "md: could not read %s's sb, not importing!\n",
3258 bdevname(rdev->bdev,b));
3259 goto abort_free;
3260 }
3261 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return rdev;
3264
3265abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003266 if (rdev->bdev)
3267 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003268 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 kfree(rdev);
3270 return ERR_PTR(err);
3271}
3272
3273/*
3274 * Check a full RAID array for plausibility
3275 */
3276
NeilBrownf72ffdd2014-09-30 14:23:59 +10003277static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
3279 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003280 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 char b[BDEVNAME_SIZE];
3282
3283 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003284 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 switch (super_types[mddev->major_version].
3286 load_super(rdev, freshest, mddev->minor_version)) {
3287 case 1:
3288 freshest = rdev;
3289 break;
3290 case 0:
3291 break;
3292 default:
3293 printk( KERN_ERR \
3294 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003295 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003297 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
3299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 super_types[mddev->major_version].
3301 validate_super(mddev, freshest);
3302
3303 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003304 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003305 if (mddev->max_disks &&
3306 (rdev->desc_nr >= mddev->max_disks ||
3307 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003308 printk(KERN_WARNING
3309 "md: %s: %s: only %d devices permitted\n",
3310 mdname(mddev), bdevname(rdev->bdev, b),
3311 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003312 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003313 continue;
3314 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003315 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (super_types[mddev->major_version].
3317 validate_super(mddev, rdev)) {
3318 printk(KERN_WARNING "md: kicking non-fresh %s"
3319 " from array!\n",
3320 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003321 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 continue;
3323 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 if (mddev->level == LEVEL_MULTIPATH) {
3326 rdev->desc_nr = i++;
3327 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003328 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003329 } else if (rdev->raid_disk >=
3330 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3331 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003332 rdev->raid_disk = -1;
3333 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
3335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336}
3337
NeilBrown72e02072009-12-14 12:49:55 +11003338/* Read a fixed-point number.
3339 * Numbers in sysfs attributes should be in "standard" units where
3340 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003341 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003342 * milliseconds or jiffies.
3343 * This function takes a decimal number with a possible fractional
3344 * component, and produces an integer which is the result of
3345 * multiplying that number by 10^'scale'.
3346 * all without any floating-point arithmetic.
3347 */
3348int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3349{
3350 unsigned long result = 0;
3351 long decimals = -1;
3352 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3353 if (*cp == '.')
3354 decimals = 0;
3355 else if (decimals < scale) {
3356 unsigned int value;
3357 value = *cp - '0';
3358 result = result * 10 + value;
3359 if (decimals >= 0)
3360 decimals++;
3361 }
3362 cp++;
3363 }
3364 if (*cp == '\n')
3365 cp++;
3366 if (*cp)
3367 return -EINVAL;
3368 if (decimals < 0)
3369 decimals = 0;
3370 while (decimals < scale) {
3371 result *= 10;
3372 decimals ++;
3373 }
3374 *res = result;
3375 return 0;
3376}
3377
NeilBrowneae17012005-11-08 21:39:23 -08003378static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003379safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003380{
3381 int msec = (mddev->safemode_delay*1000)/HZ;
3382 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3383}
3384static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003385safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003386{
NeilBrown16f17b32006-06-26 00:27:37 -07003387 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003388
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003389 if (mddev_is_clustered(mddev)) {
3390 pr_info("md: Safemode is disabled for clustered mode\n");
3391 return -EINVAL;
3392 }
3393
NeilBrown72e02072009-12-14 12:49:55 +11003394 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003395 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003396 if (msec == 0)
3397 mddev->safemode_delay = 0;
3398 else {
NeilBrown19052c02008-08-05 15:54:13 +10003399 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003400 unsigned long new_delay = (msec*HZ)/1000;
3401
3402 if (new_delay == 0)
3403 new_delay = 1;
3404 mddev->safemode_delay = new_delay;
3405 if (new_delay < old_delay || old_delay == 0)
3406 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003407 }
3408 return len;
3409}
3410static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003411__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003412
3413static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003414level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003415{
NeilBrown36d091f2014-12-15 12:56:58 +11003416 struct md_personality *p;
3417 int ret;
3418 spin_lock(&mddev->lock);
3419 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003420 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003421 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003422 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003423 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003424 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003425 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003426 else
NeilBrown36d091f2014-12-15 12:56:58 +11003427 ret = 0;
3428 spin_unlock(&mddev->lock);
3429 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003430}
3431
NeilBrownd9d166c2006-01-06 00:20:51 -08003432static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003433level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003434{
Dan Williamsf2859af2010-05-02 10:04:16 -07003435 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003436 ssize_t rv;
3437 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003438 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003439 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003440 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003441 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003442
NeilBrown67918752014-12-15 12:57:01 +11003443 if (slen == 0 || slen >= sizeof(clevel))
3444 return -EINVAL;
3445
3446 rv = mddev_lock(mddev);
3447 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003448 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003449
3450 if (mddev->pers == NULL) {
3451 strncpy(mddev->clevel, buf, slen);
3452 if (mddev->clevel[slen-1] == '\n')
3453 slen--;
3454 mddev->clevel[slen] = 0;
3455 mddev->level = LEVEL_NONE;
3456 rv = len;
3457 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003458 }
NeilBrown67918752014-12-15 12:57:01 +11003459 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003460 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003461 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003462
3463 /* request to change the personality. Need to ensure:
3464 * - array is not engaged in resync/recovery/reshape
3465 * - old personality can be suspended
3466 * - new personality will access other array.
3467 */
3468
NeilBrown67918752014-12-15 12:57:01 +11003469 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003470 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003471 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003472 mddev->reshape_position != MaxSector ||
3473 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003474 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003475
NeilBrown67918752014-12-15 12:57:01 +11003476 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003477 if (!mddev->pers->quiesce) {
3478 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3479 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003480 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003481 }
3482
3483 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003484 strncpy(clevel, buf, slen);
3485 if (clevel[slen-1] == '\n')
3486 slen--;
3487 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003488 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003489 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003490
Dan Williamsf2859af2010-05-02 10:04:16 -07003491 if (request_module("md-%s", clevel) != 0)
3492 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003493 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003494 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003495 if (!pers || !try_module_get(pers->owner)) {
3496 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003497 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003498 rv = -EINVAL;
3499 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003500 }
3501 spin_unlock(&pers_lock);
3502
3503 if (pers == mddev->pers) {
3504 /* Nothing to do! */
3505 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003506 rv = len;
3507 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003508 }
3509 if (!pers->takeover) {
3510 module_put(pers->owner);
3511 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003512 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003513 rv = -EINVAL;
3514 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003515 }
3516
NeilBrowndafb20f2012-03-19 12:46:39 +11003517 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003518 rdev->new_raid_disk = rdev->raid_disk;
3519
NeilBrown245f46c2009-03-31 14:39:39 +11003520 /* ->takeover must set new_* and/or delta_disks
3521 * if it succeeds, and may set them when it fails.
3522 */
3523 priv = pers->takeover(mddev);
3524 if (IS_ERR(priv)) {
3525 mddev->new_level = mddev->level;
3526 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003527 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003528 mddev->raid_disks -= mddev->delta_disks;
3529 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003530 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003531 module_put(pers->owner);
3532 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003533 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003534 rv = PTR_ERR(priv);
3535 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003536 }
3537
3538 /* Looks like we have a winner */
3539 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003540 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003541
3542 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003543 oldpers = mddev->pers;
3544 oldpriv = mddev->private;
3545 mddev->pers = pers;
3546 mddev->private = priv;
3547 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3548 mddev->level = mddev->new_level;
3549 mddev->layout = mddev->new_layout;
3550 mddev->chunk_sectors = mddev->new_chunk_sectors;
3551 mddev->delta_disks = 0;
3552 mddev->reshape_backwards = 0;
3553 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003554 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003555
NeilBrowndb721d32014-12-15 12:56:58 +11003556 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003557 mddev->external) {
3558 /* We are converting from a no-redundancy array
3559 * to a redundancy array and metadata is managed
3560 * externally so we need to be sure that writes
3561 * won't block due to a need to transition
3562 * clean->dirty
3563 * until external management is started.
3564 */
3565 mddev->in_sync = 0;
3566 mddev->safemode_delay = 0;
3567 mddev->safemode = 0;
3568 }
3569
NeilBrowndb721d32014-12-15 12:56:58 +11003570 oldpers->free(mddev, oldpriv);
3571
3572 if (oldpers->sync_request == NULL &&
3573 pers->sync_request != NULL) {
3574 /* need to add the md_redundancy_group */
3575 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3576 printk(KERN_WARNING
3577 "md: cannot register extra attributes for %s\n",
3578 mdname(mddev));
3579 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3580 }
3581 if (oldpers->sync_request != NULL &&
3582 pers->sync_request == NULL) {
3583 /* need to remove the md_redundancy_group */
3584 if (mddev->to_remove == NULL)
3585 mddev->to_remove = &md_redundancy_group;
3586 }
3587
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003588 module_put(oldpers->owner);
3589
NeilBrowndafb20f2012-03-19 12:46:39 +11003590 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003591 if (rdev->raid_disk < 0)
3592 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003593 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003594 rdev->new_raid_disk = -1;
3595 if (rdev->new_raid_disk == rdev->raid_disk)
3596 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003597 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003598 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003599 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003600 if (rdev->raid_disk < 0)
3601 continue;
3602 if (rdev->new_raid_disk == rdev->raid_disk)
3603 continue;
3604 rdev->raid_disk = rdev->new_raid_disk;
3605 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003606 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003607 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003608 if (sysfs_link_rdev(mddev, rdev))
3609 printk(KERN_WARNING "md: cannot register rd%d"
3610 " for %s after level change\n",
3611 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003612 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003613 }
3614
NeilBrowndb721d32014-12-15 12:56:58 +11003615 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003616 /* this is now an array without redundancy, so
3617 * it must always be in_sync
3618 */
3619 mddev->in_sync = 1;
3620 del_timer_sync(&mddev->safemode_timer);
3621 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003622 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003623 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003624 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003625 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003626 if (!mddev->thread)
3627 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003628 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003629 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003630 rv = len;
3631out_unlock:
3632 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003633 return rv;
3634}
3635
3636static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003637__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003638
NeilBrownd4dbd022006-06-26 00:27:59 -07003639static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003640layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003641{
3642 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003643 if (mddev->reshape_position != MaxSector &&
3644 mddev->layout != mddev->new_layout)
3645 return sprintf(page, "%d (%d)\n",
3646 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003647 return sprintf(page, "%d\n", mddev->layout);
3648}
3649
3650static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003651layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003652{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003653 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003654 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003655
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003656 err = kstrtouint(buf, 10, &n);
3657 if (err < 0)
3658 return err;
NeilBrown67918752014-12-15 12:57:01 +11003659 err = mddev_lock(mddev);
3660 if (err)
3661 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003662
NeilBrownb3546032009-03-31 14:56:41 +11003663 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003664 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003665 err = -EBUSY;
3666 else if (mddev->ro)
3667 err = -EROFS;
3668 else {
3669 mddev->new_layout = n;
3670 err = mddev->pers->check_reshape(mddev);
3671 if (err)
3672 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003673 }
NeilBrownb3546032009-03-31 14:56:41 +11003674 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003675 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003676 if (mddev->reshape_position == MaxSector)
3677 mddev->layout = n;
3678 }
NeilBrown67918752014-12-15 12:57:01 +11003679 mddev_unlock(mddev);
3680 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003681}
3682static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003683__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003684
NeilBrowneae17012005-11-08 21:39:23 -08003685static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003686raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003687{
NeilBrownbb636542005-11-08 21:39:45 -08003688 if (mddev->raid_disks == 0)
3689 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003690 if (mddev->reshape_position != MaxSector &&
3691 mddev->delta_disks != 0)
3692 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3693 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003694 return sprintf(page, "%d\n", mddev->raid_disks);
3695}
3696
NeilBrownfd01b882011-10-11 16:47:53 +11003697static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003698
3699static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003700raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003701{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003702 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003703 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003704
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003705 err = kstrtouint(buf, 10, &n);
3706 if (err < 0)
3707 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003708
NeilBrown67918752014-12-15 12:57:01 +11003709 err = mddev_lock(mddev);
3710 if (err)
3711 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003712 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003713 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003714 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003715 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003716 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003717
NeilBrown67918752014-12-15 12:57:01 +11003718 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003719 rdev_for_each(rdev, mddev) {
3720 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 if (olddisks > n &&
3724 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003725 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003726 }
NeilBrown67918752014-12-15 12:57:01 +11003727 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003728 mddev->delta_disks = n - olddisks;
3729 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003730 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003731 } else
NeilBrownda943b992006-01-06 00:20:54 -08003732 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003733out_unlock:
3734 mddev_unlock(mddev);
3735 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003736}
3737static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003738__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003739
NeilBrown24dd4692005-11-08 21:39:26 -08003740static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003741chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003742{
NeilBrown08a02ec2007-05-09 02:35:38 -07003743 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003744 mddev->chunk_sectors != mddev->new_chunk_sectors)
3745 return sprintf(page, "%d (%d)\n",
3746 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003747 mddev->chunk_sectors << 9);
3748 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003749}
3750
3751static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003752chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003753{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003754 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003755 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003756
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003757 err = kstrtoul(buf, 10, &n);
3758 if (err < 0)
3759 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003760
NeilBrown67918752014-12-15 12:57:01 +11003761 err = mddev_lock(mddev);
3762 if (err)
3763 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003764 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003765 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003766 err = -EBUSY;
3767 else if (mddev->ro)
3768 err = -EROFS;
3769 else {
3770 mddev->new_chunk_sectors = n >> 9;
3771 err = mddev->pers->check_reshape(mddev);
3772 if (err)
3773 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003774 }
NeilBrownb3546032009-03-31 14:56:41 +11003775 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003776 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003777 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003778 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003779 }
NeilBrown67918752014-12-15 12:57:01 +11003780 mddev_unlock(mddev);
3781 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003782}
3783static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003784__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003785
NeilBrowna94213b2006-06-26 00:28:00 -07003786static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003787resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003788{
NeilBrownd1a7c502009-03-31 15:24:32 +11003789 if (mddev->recovery_cp == MaxSector)
3790 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003791 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3792}
3793
3794static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003795resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003796{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003797 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003798 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003799
3800 if (cmd_match(buf, "none"))
3801 n = MaxSector;
3802 else {
3803 err = kstrtoull(buf, 10, &n);
3804 if (err < 0)
3805 return err;
3806 if (n != (sector_t)n)
3807 return -EINVAL;
3808 }
NeilBrowna94213b2006-06-26 00:28:00 -07003809
NeilBrown67918752014-12-15 12:57:01 +11003810 err = mddev_lock(mddev);
3811 if (err)
3812 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003813 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003814 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003815
NeilBrown67918752014-12-15 12:57:01 +11003816 if (!err) {
3817 mddev->recovery_cp = n;
3818 if (mddev->pers)
3819 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3820 }
3821 mddev_unlock(mddev);
3822 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003823}
3824static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003825__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3826 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003827
NeilBrown9e653b62006-06-26 00:27:58 -07003828/*
3829 * The array state can be:
3830 *
3831 * clear
3832 * No devices, no size, no level
3833 * Equivalent to STOP_ARRAY ioctl
3834 * inactive
3835 * May have some settings, but array is not active
3836 * all IO results in error
3837 * When written, doesn't tear down array, but just stops it
3838 * suspended (not supported yet)
3839 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003840 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003841 * readonly
3842 * no resync can happen. no superblocks get written.
3843 * write requests fail
3844 * read-auto
3845 * like readonly, but behaves like 'clean' on a write request.
3846 *
3847 * clean - no pending writes, but otherwise active.
3848 * When written to inactive array, starts without resync
3849 * If a write request arrives then
3850 * if metadata is known, mark 'dirty' and switch to 'active'.
3851 * if not known, block and switch to write-pending
3852 * If written to an active array that has pending writes, then fails.
3853 * active
3854 * fully active: IO and resync can be happening.
3855 * When written to inactive array, starts with resync
3856 *
3857 * write-pending
3858 * clean, but writes are blocked waiting for 'active' to be written.
3859 *
3860 * active-idle
3861 * like active, but no writes have been seen for a while (100msec).
3862 *
3863 */
3864enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3865 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003866static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003867 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3868 "write-pending", "active-idle", NULL };
3869
3870static int match_word(const char *word, char **list)
3871{
3872 int n;
3873 for (n=0; list[n]; n++)
3874 if (cmd_match(word, list[n]))
3875 break;
3876 return n;
3877}
3878
3879static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003880array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003881{
3882 enum array_state st = inactive;
3883
3884 if (mddev->pers)
3885 switch(mddev->ro) {
3886 case 1:
3887 st = readonly;
3888 break;
3889 case 2:
3890 st = read_auto;
3891 break;
3892 case 0:
3893 if (mddev->in_sync)
3894 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003895 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003896 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003897 else if (mddev->safemode)
3898 st = active_idle;
3899 else
3900 st = active;
3901 }
3902 else {
3903 if (list_empty(&mddev->disks) &&
3904 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003905 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003906 st = clear;
3907 else
3908 st = inactive;
3909 }
3910 return sprintf(page, "%s\n", array_states[st]);
3911}
3912
NeilBrownf72ffdd2014-09-30 14:23:59 +10003913static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3914static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3915static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003916static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003917
3918static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003919array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003920{
NeilBrown67918752014-12-15 12:57:01 +11003921 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003922 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003923
3924 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3925 /* don't take reconfig_mutex when toggling between
3926 * clean and active
3927 */
3928 spin_lock(&mddev->lock);
3929 if (st == active) {
3930 restart_array(mddev);
3931 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3932 wake_up(&mddev->sb_wait);
3933 err = 0;
3934 } else /* st == clean */ {
3935 restart_array(mddev);
3936 if (atomic_read(&mddev->writes_pending) == 0) {
3937 if (mddev->in_sync == 0) {
3938 mddev->in_sync = 1;
3939 if (mddev->safemode == 1)
3940 mddev->safemode = 0;
3941 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3942 }
3943 err = 0;
3944 } else
3945 err = -EBUSY;
3946 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02003947 if (!err)
3948 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11003949 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003950 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003951 }
3952 err = mddev_lock(mddev);
3953 if (err)
3954 return err;
3955 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003956 switch(st) {
3957 case bad_word:
3958 break;
3959 case clear:
3960 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003961 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003962 break;
3963 case inactive:
3964 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003965 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003966 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003967 else
NeilBrowne6910632008-02-06 01:39:51 -08003968 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003969 break;
3970 case suspended:
3971 break; /* not supported yet */
3972 case readonly:
3973 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003974 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003975 else {
3976 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003977 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003978 err = do_md_run(mddev);
3979 }
3980 break;
3981 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003982 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003983 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003984 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003985 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003986 err = restart_array(mddev);
3987 if (err == 0) {
3988 mddev->ro = 2;
3989 set_disk_ro(mddev->gendisk, 0);
3990 }
NeilBrown9e653b62006-06-26 00:27:58 -07003991 } else {
3992 mddev->ro = 2;
3993 err = do_md_run(mddev);
3994 }
3995 break;
3996 case clean:
3997 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003998 err = restart_array(mddev);
3999 if (err)
4000 break;
NeilBrown85572d72014-12-15 12:56:56 +11004001 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004002 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08004003 if (mddev->in_sync == 0) {
4004 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07004005 if (mddev->safemode == 1)
4006 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10004007 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08004008 }
4009 err = 0;
4010 } else
4011 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004012 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004013 } else
4014 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004015 break;
4016 case active:
4017 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004018 err = restart_array(mddev);
4019 if (err)
4020 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10004021 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004022 wake_up(&mddev->sb_wait);
4023 err = 0;
4024 } else {
4025 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004026 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004027 err = do_md_run(mddev);
4028 }
4029 break;
4030 case write_pending:
4031 case active_idle:
4032 /* these cannot be set */
4033 break;
4034 }
NeilBrown67918752014-12-15 12:57:01 +11004035
4036 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004037 if (mddev->hold_active == UNTIL_IOCTL)
4038 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004039 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004040 }
NeilBrown67918752014-12-15 12:57:01 +11004041 mddev_unlock(mddev);
4042 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004043}
NeilBrown80ca3a42006-07-10 04:44:18 -07004044static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004045__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004046
NeilBrown6d7ff7382006-01-06 00:21:16 -08004047static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004048max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004049 return sprintf(page, "%d\n",
4050 atomic_read(&mddev->max_corr_read_errors));
4051}
4052
4053static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004054max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004055{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004056 unsigned int n;
4057 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004058
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004059 rv = kstrtouint(buf, 10, &n);
4060 if (rv < 0)
4061 return rv;
4062 atomic_set(&mddev->max_corr_read_errors, n);
4063 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004064}
4065
4066static struct md_sysfs_entry max_corr_read_errors =
4067__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4068 max_corrected_read_errors_store);
4069
4070static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004071null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004072{
4073 return -EINVAL;
4074}
4075
4076static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004077new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004078{
4079 /* buf must be %d:%d\n? giving major and minor numbers */
4080 /* The new device is added to the array.
4081 * If the array has a persistent superblock, we read the
4082 * superblock to initialise info and check validity.
4083 * Otherwise, only checking done is that in bind_rdev_to_array,
4084 * which mainly checks size.
4085 */
4086 char *e;
4087 int major = simple_strtoul(buf, &e, 10);
4088 int minor;
4089 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004090 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004091 int err;
4092
4093 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4094 return -EINVAL;
4095 minor = simple_strtoul(e+1, &e, 10);
4096 if (*e && *e != '\n')
4097 return -EINVAL;
4098 dev = MKDEV(major, minor);
4099 if (major != MAJOR(dev) ||
4100 minor != MINOR(dev))
4101 return -EOVERFLOW;
4102
NeilBrown67918752014-12-15 12:57:01 +11004103 flush_workqueue(md_misc_wq);
4104
4105 err = mddev_lock(mddev);
4106 if (err)
4107 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004108 if (mddev->persistent) {
4109 rdev = md_import_device(dev, mddev->major_version,
4110 mddev->minor_version);
4111 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004112 struct md_rdev *rdev0
4113 = list_entry(mddev->disks.next,
4114 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004115 err = super_types[mddev->major_version]
4116 .load_super(rdev, rdev0, mddev->minor_version);
4117 if (err < 0)
4118 goto out;
4119 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004120 } else if (mddev->external)
4121 rdev = md_import_device(dev, -2, -1);
4122 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004123 rdev = md_import_device(dev, -1, -1);
4124
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004125 if (IS_ERR(rdev)) {
4126 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004127 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004128 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004129 err = bind_rdev_to_array(rdev, mddev);
4130 out:
4131 if (err)
4132 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004133 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004134 return err ? err : len;
4135}
4136
4137static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004138__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004139
4140static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004141bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004142{
4143 char *end;
4144 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004145 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004146
NeilBrown67918752014-12-15 12:57:01 +11004147 err = mddev_lock(mddev);
4148 if (err)
4149 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004150 if (!mddev->bitmap)
4151 goto out;
4152 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4153 while (*buf) {
4154 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4155 if (buf == end) break;
4156 if (*end == '-') { /* range */
4157 buf = end + 1;
4158 end_chunk = simple_strtoul(buf, &end, 0);
4159 if (buf == end) break;
4160 }
4161 if (*end && !isspace(*end)) break;
4162 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004163 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004164 }
4165 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4166out:
NeilBrown67918752014-12-15 12:57:01 +11004167 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004168 return len;
4169}
4170
4171static struct md_sysfs_entry md_bitmap =
4172__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4173
4174static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004175size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004176{
Andre Noll58c0fed2009-03-31 14:33:13 +11004177 return sprintf(page, "%llu\n",
4178 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004179}
4180
NeilBrownfd01b882011-10-11 16:47:53 +11004181static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004182
4183static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004184size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004185{
4186 /* If array is inactive, we can reduce the component size, but
4187 * not increase it (except from 0).
4188 * If array is active, we can try an on-line resize
4189 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004190 sector_t sectors;
4191 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004192
Andre Noll58c0fed2009-03-31 14:33:13 +11004193 if (err < 0)
4194 return err;
NeilBrown67918752014-12-15 12:57:01 +11004195 err = mddev_lock(mddev);
4196 if (err)
4197 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004198 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004199 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004200 if (err == 0)
4201 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004202 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004203 if (mddev->dev_sectors == 0 ||
4204 mddev->dev_sectors > sectors)
4205 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004206 else
4207 err = -ENOSPC;
4208 }
NeilBrown67918752014-12-15 12:57:01 +11004209 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004210 return err ? err : len;
4211}
4212
4213static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004214__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004215
Masanari Iida83f0d772012-10-30 00:18:08 +09004216/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004217 * This is one of
4218 * 'none' for arrays with no metadata (good luck...)
4219 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004220 * or N.M for internally known formats
4221 */
4222static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004223metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004224{
4225 if (mddev->persistent)
4226 return sprintf(page, "%d.%d\n",
4227 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004228 else if (mddev->external)
4229 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004230 else
4231 return sprintf(page, "none\n");
4232}
4233
4234static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004235metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004236{
4237 int major, minor;
4238 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004239 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004240 /* Changing the details of 'external' metadata is
4241 * always permitted. Otherwise there must be
4242 * no devices attached to the array.
4243 */
NeilBrown67918752014-12-15 12:57:01 +11004244
4245 err = mddev_lock(mddev);
4246 if (err)
4247 return err;
4248 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004249 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4250 ;
4251 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004252 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004253
NeilBrown67918752014-12-15 12:57:01 +11004254 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004255 if (cmd_match(buf, "none")) {
4256 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004257 mddev->external = 0;
4258 mddev->major_version = 0;
4259 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004260 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004261 }
4262 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004263 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004264 if (namelen >= sizeof(mddev->metadata_type))
4265 namelen = sizeof(mddev->metadata_type)-1;
4266 strncpy(mddev->metadata_type, buf+9, namelen);
4267 mddev->metadata_type[namelen] = 0;
4268 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4269 mddev->metadata_type[--namelen] = 0;
4270 mddev->persistent = 0;
4271 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004272 mddev->major_version = 0;
4273 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004274 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004275 }
4276 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004277 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004278 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004279 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004280 buf = e+1;
4281 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004282 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004283 goto out_unlock;
4284 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004285 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004286 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004287 mddev->major_version = major;
4288 mddev->minor_version = minor;
4289 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004290 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004291 err = 0;
4292out_unlock:
4293 mddev_unlock(mddev);
4294 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004295}
4296
4297static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004298__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004299
NeilBrowna35b0d62006-01-06 00:20:49 -08004300static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004301action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004302{
NeilBrown7eec3142005-11-08 21:39:44 -08004303 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004304 unsigned long recovery = mddev->recovery;
4305 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004306 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004307 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4308 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4309 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004310 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004311 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4312 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004313 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004314 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004315 type = "check";
4316 else
4317 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004318 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004319 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004320 else if (mddev->reshape_position != MaxSector)
4321 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004322 }
4323 return sprintf(page, "%s\n", type);
4324}
4325
4326static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004327action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004328{
NeilBrown7eec3142005-11-08 21:39:44 -08004329 if (!mddev->pers || !mddev->pers->sync_request)
4330 return -EINVAL;
4331
NeilBrownb6a9ce62009-05-26 09:41:17 +10004332
4333 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004334 if (cmd_match(page, "frozen"))
4335 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4336 else
4337 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004338 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4339 mddev_lock(mddev) == 0) {
4340 flush_workqueue(md_misc_wq);
4341 if (mddev->sync_thread) {
4342 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004343 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004344 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004345 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004346 }
NeilBrown312045e2015-12-21 11:01:21 +11004347 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004348 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004349 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004350 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004351 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004352 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004353 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004354 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004355 int err;
4356 if (mddev->pers->start_reshape == NULL)
4357 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004358 err = mddev_lock(mddev);
4359 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004360 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4361 err = -EBUSY;
4362 else {
4363 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4364 err = mddev->pers->start_reshape(mddev);
4365 }
NeilBrown67918752014-12-15 12:57:01 +11004366 mddev_unlock(mddev);
4367 }
NeilBrown16484bf2006-03-27 01:18:13 -08004368 if (err)
4369 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004370 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004371 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004372 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004373 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004374 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004375 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004376 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004377 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4378 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004379 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004380 if (mddev->ro == 2) {
4381 /* A write to sync_action is enough to justify
4382 * canceling read-auto mode
4383 */
4384 mddev->ro = 0;
4385 md_wakeup_thread(mddev->sync_thread);
4386 }
NeilBrown03c902e2006-01-06 00:20:46 -08004387 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004388 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004389 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004390 return len;
4391}
4392
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004393static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004394__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004395
4396static ssize_t
4397last_sync_action_show(struct mddev *mddev, char *page)
4398{
4399 return sprintf(page, "%s\n", mddev->last_sync_action);
4400}
4401
4402static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4403
NeilBrown9d888832005-11-08 21:39:26 -08004404static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004405mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004406{
4407 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004408 (unsigned long long)
4409 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004410}
4411
NeilBrown80ca3a42006-07-10 04:44:18 -07004412static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004413
NeilBrown88202a02006-01-06 00:21:36 -08004414static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004415sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004416{
4417 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4418 mddev->sync_speed_min ? "local": "system");
4419}
4420
4421static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004422sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004423{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004424 unsigned int min;
4425 int rv;
4426
NeilBrown88202a02006-01-06 00:21:36 -08004427 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004428 min = 0;
4429 } else {
4430 rv = kstrtouint(buf, 10, &min);
4431 if (rv < 0)
4432 return rv;
4433 if (min == 0)
4434 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004435 }
NeilBrown88202a02006-01-06 00:21:36 -08004436 mddev->sync_speed_min = min;
4437 return len;
4438}
4439
4440static struct md_sysfs_entry md_sync_min =
4441__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4442
4443static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004444sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004445{
4446 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4447 mddev->sync_speed_max ? "local": "system");
4448}
4449
4450static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004451sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004452{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004453 unsigned int max;
4454 int rv;
4455
NeilBrown88202a02006-01-06 00:21:36 -08004456 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004457 max = 0;
4458 } else {
4459 rv = kstrtouint(buf, 10, &max);
4460 if (rv < 0)
4461 return rv;
4462 if (max == 0)
4463 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004464 }
NeilBrown88202a02006-01-06 00:21:36 -08004465 mddev->sync_speed_max = max;
4466 return len;
4467}
4468
4469static struct md_sysfs_entry md_sync_max =
4470__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4471
Iustin Popd7f3d292007-10-16 23:30:54 -07004472static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004473degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004474{
4475 return sprintf(page, "%d\n", mddev->degraded);
4476}
4477static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004478
4479static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004480sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004481{
4482 return sprintf(page, "%d\n", mddev->parallel_resync);
4483}
4484
4485static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004486sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004487{
4488 long n;
4489
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004490 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004491 return -EINVAL;
4492
4493 if (n != 0 && n != 1)
4494 return -EINVAL;
4495
4496 mddev->parallel_resync = n;
4497
4498 if (mddev->sync_thread)
4499 wake_up(&resync_wait);
4500
4501 return len;
4502}
4503
4504/* force parallel resync, even with shared block devices */
4505static struct md_sysfs_entry md_sync_force_parallel =
4506__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4507 sync_force_parallel_show, sync_force_parallel_store);
4508
4509static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004510sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004511{
4512 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004513 if (mddev->curr_resync == 0)
4514 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004515 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4516 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004517 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004518 db = resync - mddev->resync_mark_cnt;
4519 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004520}
4521
NeilBrown80ca3a42006-07-10 04:44:18 -07004522static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004523
4524static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004525sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004526{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004527 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004528
NeilBrownacb180b2009-04-14 16:28:34 +10004529 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4530 return sprintf(page, "none\n");
4531
NeilBrown72f36d52012-10-11 14:25:57 +11004532 if (mddev->curr_resync == 1 ||
4533 mddev->curr_resync == 2)
4534 return sprintf(page, "delayed\n");
4535
NeilBrownc804cde2012-05-21 09:28:33 +10004536 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4537 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004538 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004539 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004540 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004541
NeilBrownacb180b2009-04-14 16:28:34 +10004542 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004543 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004544}
4545
NeilBrown750f1992014-09-30 08:53:05 +10004546static struct md_sysfs_entry md_sync_completed =
4547 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004548
NeilBrowne464eaf2006-03-27 01:18:14 -08004549static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004550min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004551{
4552 return sprintf(page, "%llu\n",
4553 (unsigned long long)mddev->resync_min);
4554}
4555static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004556min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004557{
4558 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004559 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004560
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004561 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004562 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004563
4564 spin_lock(&mddev->lock);
4565 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004566 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004567 goto out_unlock;
4568
4569 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004570 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004571 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004572
NeilBrown50c37b12015-03-23 17:36:38 +11004573 /* Round down to multiple of 4K for safety */
4574 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004575 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004576
NeilBrown23da4222014-12-15 12:57:01 +11004577out_unlock:
4578 spin_unlock(&mddev->lock);
4579 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004580}
4581
4582static struct md_sysfs_entry md_min_sync =
4583__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4584
4585static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004586max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004587{
4588 if (mddev->resync_max == MaxSector)
4589 return sprintf(page, "max\n");
4590 else
4591 return sprintf(page, "%llu\n",
4592 (unsigned long long)mddev->resync_max);
4593}
4594static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004595max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004596{
NeilBrown23da4222014-12-15 12:57:01 +11004597 int err;
4598 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004599 if (strncmp(buf, "max", 3) == 0)
4600 mddev->resync_max = MaxSector;
4601 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004602 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004603 int chunk;
4604
4605 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004606 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004607 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004608 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004609 goto out_unlock;
4610
4611 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004612 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004613 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004614 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004615 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004616
4617 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004618 chunk = mddev->chunk_sectors;
4619 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004620 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004621
4622 err = -EINVAL;
4623 if (sector_div(temp, chunk))
4624 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004625 }
4626 mddev->resync_max = max;
4627 }
4628 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004629 err = 0;
4630out_unlock:
4631 spin_unlock(&mddev->lock);
4632 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004633}
4634
4635static struct md_sysfs_entry md_max_sync =
4636__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4637
4638static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004639suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004640{
4641 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4642}
4643
4644static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004645suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004646{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004647 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004648 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004649
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004650 err = kstrtoull(buf, 10, &new);
4651 if (err < 0)
4652 return err;
4653 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004654 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004655
NeilBrown67918752014-12-15 12:57:01 +11004656 err = mddev_lock(mddev);
4657 if (err)
4658 return err;
4659 err = -EINVAL;
4660 if (mddev->pers == NULL ||
4661 mddev->pers->quiesce == NULL)
4662 goto unlock;
4663 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004664 mddev->suspend_lo = new;
4665 if (new >= old)
4666 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004667 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004668 else {
4669 /* Expanding suspended region - need to wait */
4670 mddev->pers->quiesce(mddev, 1);
4671 mddev->pers->quiesce(mddev, 0);
4672 }
NeilBrown67918752014-12-15 12:57:01 +11004673 err = 0;
4674unlock:
4675 mddev_unlock(mddev);
4676 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004677}
4678static struct md_sysfs_entry md_suspend_lo =
4679__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4680
NeilBrowne464eaf2006-03-27 01:18:14 -08004681static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004682suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004683{
4684 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4685}
4686
4687static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004688suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004689{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004690 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004691 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004692
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004693 err = kstrtoull(buf, 10, &new);
4694 if (err < 0)
4695 return err;
4696 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004697 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004698
NeilBrown67918752014-12-15 12:57:01 +11004699 err = mddev_lock(mddev);
4700 if (err)
4701 return err;
4702 err = -EINVAL;
4703 if (mddev->pers == NULL ||
4704 mddev->pers->quiesce == NULL)
4705 goto unlock;
4706 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004707 mddev->suspend_hi = new;
4708 if (new <= old)
4709 /* Shrinking suspended region */
4710 mddev->pers->quiesce(mddev, 2);
4711 else {
4712 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004713 mddev->pers->quiesce(mddev, 1);
4714 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004715 }
NeilBrown67918752014-12-15 12:57:01 +11004716 err = 0;
4717unlock:
4718 mddev_unlock(mddev);
4719 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004720}
4721static struct md_sysfs_entry md_suspend_hi =
4722__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4723
NeilBrown08a02ec2007-05-09 02:35:38 -07004724static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004725reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004726{
4727 if (mddev->reshape_position != MaxSector)
4728 return sprintf(page, "%llu\n",
4729 (unsigned long long)mddev->reshape_position);
4730 strcpy(page, "none\n");
4731 return 5;
4732}
4733
4734static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004735reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004736{
NeilBrownc6563a82012-05-21 09:27:00 +10004737 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004738 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004739 int err;
NeilBrown67918752014-12-15 12:57:01 +11004740
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004741 err = kstrtoull(buf, 10, &new);
4742 if (err < 0)
4743 return err;
4744 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004745 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004746 err = mddev_lock(mddev);
4747 if (err)
4748 return err;
4749 err = -EBUSY;
4750 if (mddev->pers)
4751 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004752 mddev->reshape_position = new;
4753 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004754 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004755 mddev->new_level = mddev->level;
4756 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004757 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004758 rdev_for_each(rdev, mddev)
4759 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004760 err = 0;
4761unlock:
4762 mddev_unlock(mddev);
4763 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004764}
4765
4766static struct md_sysfs_entry md_reshape_position =
4767__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4768 reshape_position_store);
4769
Dan Williamsb522adc2009-03-31 15:00:31 +11004770static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004771reshape_direction_show(struct mddev *mddev, char *page)
4772{
4773 return sprintf(page, "%s\n",
4774 mddev->reshape_backwards ? "backwards" : "forwards");
4775}
4776
4777static ssize_t
4778reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4779{
4780 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004781 int err;
4782
NeilBrown2c810cd2012-05-21 09:27:00 +10004783 if (cmd_match(buf, "forwards"))
4784 backwards = 0;
4785 else if (cmd_match(buf, "backwards"))
4786 backwards = 1;
4787 else
4788 return -EINVAL;
4789 if (mddev->reshape_backwards == backwards)
4790 return len;
4791
NeilBrown67918752014-12-15 12:57:01 +11004792 err = mddev_lock(mddev);
4793 if (err)
4794 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004795 /* check if we are allowed to change */
4796 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004797 err = -EBUSY;
4798 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004799 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004800 err = -EINVAL;
4801 else
4802 mddev->reshape_backwards = backwards;
4803 mddev_unlock(mddev);
4804 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004805}
4806
4807static struct md_sysfs_entry md_reshape_direction =
4808__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4809 reshape_direction_store);
4810
4811static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004812array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004813{
4814 if (mddev->external_size)
4815 return sprintf(page, "%llu\n",
4816 (unsigned long long)mddev->array_sectors/2);
4817 else
4818 return sprintf(page, "default\n");
4819}
4820
4821static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004822array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004823{
4824 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004825 int err;
4826
4827 err = mddev_lock(mddev);
4828 if (err)
4829 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004830
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04004831 /* cluster raid doesn't support change array_sectors */
4832 if (mddev_is_clustered(mddev))
4833 return -EINVAL;
4834
Dan Williamsb522adc2009-03-31 15:00:31 +11004835 if (strncmp(buf, "default", 7) == 0) {
4836 if (mddev->pers)
4837 sectors = mddev->pers->size(mddev, 0, 0);
4838 else
4839 sectors = mddev->array_sectors;
4840
4841 mddev->external_size = 0;
4842 } else {
4843 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004844 err = -EINVAL;
4845 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4846 err = -E2BIG;
4847 else
4848 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004849 }
4850
NeilBrown67918752014-12-15 12:57:01 +11004851 if (!err) {
4852 mddev->array_sectors = sectors;
4853 if (mddev->pers) {
4854 set_capacity(mddev->gendisk, mddev->array_sectors);
4855 revalidate_disk(mddev->gendisk);
4856 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004857 }
NeilBrown67918752014-12-15 12:57:01 +11004858 mddev_unlock(mddev);
4859 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004860}
4861
4862static struct md_sysfs_entry md_array_size =
4863__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4864 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004865
NeilBrowneae17012005-11-08 21:39:23 -08004866static struct attribute *md_default_attrs[] = {
4867 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004868 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004869 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004870 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004871 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004872 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004873 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004874 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004875 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004876 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004877 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004878 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004879 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004880 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004881 NULL,
4882};
4883
4884static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004885 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004886 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004887 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004888 &md_sync_min.attr,
4889 &md_sync_max.attr,
4890 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004891 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004892 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004893 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004894 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004895 &md_suspend_lo.attr,
4896 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004897 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004898 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004899 NULL,
4900};
NeilBrown411036f2005-11-08 21:39:40 -08004901static struct attribute_group md_redundancy_group = {
4902 .name = NULL,
4903 .attrs = md_redundancy_attrs,
4904};
4905
NeilBrowneae17012005-11-08 21:39:23 -08004906static ssize_t
4907md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4908{
4909 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004910 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004911 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004912
4913 if (!entry->show)
4914 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004915 spin_lock(&all_mddevs_lock);
4916 if (list_empty(&mddev->all_mddevs)) {
4917 spin_unlock(&all_mddevs_lock);
4918 return -EBUSY;
4919 }
4920 mddev_get(mddev);
4921 spin_unlock(&all_mddevs_lock);
4922
NeilBrownb7b17c92014-12-15 12:56:59 +11004923 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004924 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004925 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004926}
4927
4928static ssize_t
4929md_attr_store(struct kobject *kobj, struct attribute *attr,
4930 const char *page, size_t length)
4931{
4932 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004933 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004934 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004935
4936 if (!entry->store)
4937 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004938 if (!capable(CAP_SYS_ADMIN))
4939 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004940 spin_lock(&all_mddevs_lock);
4941 if (list_empty(&mddev->all_mddevs)) {
4942 spin_unlock(&all_mddevs_lock);
4943 return -EBUSY;
4944 }
4945 mddev_get(mddev);
4946 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004947 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004948 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004949 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004950}
4951
4952static void md_free(struct kobject *ko)
4953{
NeilBrownfd01b882011-10-11 16:47:53 +11004954 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004955
4956 if (mddev->sysfs_state)
4957 sysfs_put(mddev->sysfs_state);
4958
NeilBrown6cd18e72015-04-27 14:12:22 +10004959 if (mddev->queue)
4960 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004961 if (mddev->gendisk) {
4962 del_gendisk(mddev->gendisk);
4963 put_disk(mddev->gendisk);
4964 }
NeilBrowna21d1502009-01-09 08:31:09 +11004965
NeilBrowneae17012005-11-08 21:39:23 -08004966 kfree(mddev);
4967}
4968
Emese Revfy52cf25d2010-01-19 02:58:23 +01004969static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004970 .show = md_attr_show,
4971 .store = md_attr_store,
4972};
4973static struct kobj_type md_ktype = {
4974 .release = md_free,
4975 .sysfs_ops = &md_sysfs_ops,
4976 .default_attrs = md_default_attrs,
4977};
4978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979int mdp_major = 0;
4980
Dan Williams5fd3a172009-03-04 00:57:25 -07004981static void mddev_delayed_delete(struct work_struct *ws)
4982{
NeilBrownfd01b882011-10-11 16:47:53 +11004983 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004984
NeilBrown43a70502009-12-14 12:49:55 +11004985 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004986 kobject_del(&mddev->kobj);
4987 kobject_put(&mddev->kobj);
4988}
4989
NeilBrownefeb53c2009-01-09 08:31:10 +11004990static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004992 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004993 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004995 int partitioned;
4996 int shift;
4997 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004998 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
5000 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005001 return -ENODEV;
5002
5003 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5004 shift = partitioned ? MdpMinorShift : 0;
5005 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
Tejun Heoe804ac72010-10-15 15:36:08 +02005007 /* wait for any previous instance of this device to be
5008 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005009 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005010 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005011
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005012 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005013 error = -EEXIST;
5014 if (mddev->gendisk)
5015 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005016
5017 if (name) {
5018 /* Need to ensure that 'name' is not a duplicate.
5019 */
NeilBrownfd01b882011-10-11 16:47:53 +11005020 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005021 spin_lock(&all_mddevs_lock);
5022
5023 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5024 if (mddev2->gendisk &&
5025 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5026 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005027 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005028 }
5029 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 }
NeilBrown8b765392009-01-09 08:31:08 +11005031
NeilBrown0909dc42009-07-01 12:27:21 +10005032 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005033 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005034 if (!mddev->queue)
5035 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005036 mddev->queue->queuedata = mddev;
5037
NeilBrown409c57f2009-03-31 14:39:39 +11005038 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005039 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 disk = alloc_disk(1 << shift);
5042 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005043 blk_cleanup_queue(mddev->queue);
5044 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005045 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005047 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005049 if (name)
5050 strcpy(disk->disk_name, name);
5051 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005053 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 disk->fops = &md_fops;
5056 disk->private_data = mddev;
5057 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005058 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005059 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005060 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005061 * remove it now.
5062 */
5063 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005065 /* As soon as we call add_disk(), another thread could get
5066 * through to md_open, so make sure it doesn't get too far
5067 */
5068 mutex_lock(&mddev->open_mutex);
5069 add_disk(disk);
5070
Tejun Heoed9e1982008-08-25 19:56:05 +09005071 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5072 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005073 if (error) {
5074 /* This isn't possible, but as kobject_init_and_add is marked
5075 * __must_check, we must do something with the result
5076 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005077 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5078 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005079 error = 0;
5080 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005081 if (mddev->kobj.sd &&
5082 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11005083 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005084 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005085 abort:
5086 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005087 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005088 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005089 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005090 }
NeilBrownd3374822009-01-09 08:31:10 +11005091 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005092 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005093}
5094
5095static struct kobject *md_probe(dev_t dev, int *part, void *data)
5096{
5097 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 return NULL;
5099}
5100
NeilBrownefeb53c2009-01-09 08:31:10 +11005101static int add_named_array(const char *val, struct kernel_param *kp)
5102{
5103 /* val must be "md_*" where * is not all digits.
5104 * We allocate an array with a large free minor number, and
5105 * set the name to val. val must not already be an active name.
5106 */
5107 int len = strlen(val);
5108 char buf[DISK_NAME_LEN];
5109
5110 while (len && val[len-1] == '\n')
5111 len--;
5112 if (len >= DISK_NAME_LEN)
5113 return -E2BIG;
5114 strlcpy(buf, val, len+1);
5115 if (strncmp(buf, "md_", 3) != 0)
5116 return -EINVAL;
5117 return md_alloc(0, buf);
5118}
5119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120static void md_safemode_timeout(unsigned long data)
5121{
NeilBrownfd01b882011-10-11 16:47:53 +11005122 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
Neil Brown0fd62b82008-06-28 08:31:36 +10005124 if (!atomic_read(&mddev->writes_pending)) {
5125 mddev->safemode = 1;
5126 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005127 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 md_wakeup_thread(mddev->thread);
5130}
5131
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005132static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
NeilBrownfd01b882011-10-11 16:47:53 +11005134int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135{
NeilBrown2604b702006-01-06 00:20:36 -08005136 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005137 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005138 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139
NeilBrowna757e642005-04-16 15:26:42 -07005140 if (list_empty(&mddev->disks))
5141 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
5144 if (mddev->pers)
5145 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005146 /* Cannot run until previous stop completes properly */
5147 if (mddev->sysfs_active)
5148 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005149
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 /*
5151 * Analyze all RAID superblock(s)
5152 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005153 if (!mddev->raid_disks) {
5154 if (!mddev->persistent)
5155 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005156 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
NeilBrownd9d166c2006-01-06 00:20:51 -08005159 if (mddev->level != LEVEL_NONE)
5160 request_module("md-level-%d", mddev->level);
5161 else if (mddev->clevel[0])
5162 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
5164 /*
5165 * Drop all container device buffers, from now on
5166 * the only valid external interface is through the md
5167 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005169 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005170 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 continue;
5172 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005173 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005174
5175 /* perform some consistency tests on the device.
5176 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005177 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005178 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005179 if (rdev->meta_bdev) {
5180 /* Nothing to check */;
5181 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005182 if (mddev->dev_sectors &&
5183 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005184 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005185 printk("md: %s: data overlaps metadata\n",
5186 mdname(mddev));
5187 return -EINVAL;
5188 }
5189 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005190 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005191 > rdev->data_offset) {
5192 printk("md: %s: metadata overlaps data\n",
5193 mdname(mddev));
5194 return -EINVAL;
5195 }
5196 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005197 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 }
5199
NeilBrowna167f662010-10-26 18:31:13 +11005200 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005201 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005202
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005204 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005205 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005207 if (mddev->level != LEVEL_NONE)
5208 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5209 mddev->level);
5210 else
5211 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5212 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 return -EINVAL;
5214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005216 if (mddev->level != pers->level) {
5217 mddev->level = pers->level;
5218 mddev->new_level = pers->level;
5219 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005220 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
NeilBrownf6705572006-03-27 01:18:11 -08005222 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005223 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005224 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005225 module_put(pers->owner);
5226 return -EINVAL;
5227 }
5228
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005229 if (pers->sync_request) {
5230 /* Warn if this is a potentially silly
5231 * configuration.
5232 */
5233 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005234 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005235 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005236
NeilBrowndafb20f2012-03-19 12:46:39 +11005237 rdev_for_each(rdev, mddev)
5238 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005239 if (rdev < rdev2 &&
5240 rdev->bdev->bd_contains ==
5241 rdev2->bdev->bd_contains) {
5242 printk(KERN_WARNING
5243 "%s: WARNING: %s appears to be"
5244 " on the same physical disk as"
5245 " %s.\n",
5246 mdname(mddev),
5247 bdevname(rdev->bdev,b),
5248 bdevname(rdev2->bdev,b2));
5249 warned = 1;
5250 }
5251 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005252
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005253 if (warned)
5254 printk(KERN_WARNING
5255 "True protection against single-disk"
5256 " failure might be compromised.\n");
5257 }
5258
NeilBrown657390d2005-08-26 18:34:16 -07005259 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005260 /* may be over-ridden by personality */
5261 mddev->resync_max_sectors = mddev->dev_sectors;
5262
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005263 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
NeilBrown0f9552b52009-12-30 12:08:50 +11005265 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005266 mddev->ro = 2; /* read-only, but switch on first write */
5267
NeilBrown36d091f2014-12-15 12:56:58 +11005268 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005269 if (err)
5270 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005271 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005272 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5273 " but 'external_size' not in effect?\n", __func__);
5274 printk(KERN_ERR
5275 "md: invalid array_size %llu > default size %llu\n",
5276 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005277 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005278 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005279 }
NeilBrown36d091f2014-12-15 12:56:58 +11005280 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005281 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005282 struct bitmap *bitmap;
5283
5284 bitmap = bitmap_create(mddev, -1);
5285 if (IS_ERR(bitmap)) {
5286 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005287 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5288 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005289 } else
5290 mddev->bitmap = bitmap;
5291
NeilBrownb15c2e52006-01-06 00:20:16 -08005292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005294 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005295 if (mddev->private)
5296 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005297 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005298 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005299 bitmap_destroy(mddev);
5300 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 }
NeilBrown5c675f82014-12-15 12:56:56 +11005302 if (mddev->queue) {
5303 mddev->queue->backing_dev_info.congested_data = mddev;
5304 mddev->queue->backing_dev_info.congested_fn = md_congested;
5305 }
NeilBrown36d091f2014-12-15 12:56:58 +11005306 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005307 if (mddev->kobj.sd &&
5308 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005309 printk(KERN_WARNING
5310 "md: cannot register extra attributes for %s\n",
5311 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005312 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005313 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005314 mddev->ro = 0;
5315
NeilBrownf72ffdd2014-09-30 14:23:59 +10005316 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005317 atomic_set(&mddev->max_corr_read_errors,
5318 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005320 if (mddev_is_clustered(mddev))
5321 mddev->safemode_delay = 0;
5322 else
5323 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005325 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005326 spin_lock(&mddev->lock);
5327 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005328 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005329 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005330 if (rdev->raid_disk >= 0)
5331 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005332 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005333
NeilBrowna4a3d262015-07-17 11:57:30 +10005334 if (mddev->degraded && !mddev->ro)
5335 /* This ensures that recovering status is reported immediately
5336 * via sysfs - until a lack of spares is confirmed.
5337 */
5338 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005340
NeilBrown7a0a5352013-08-27 16:28:23 +10005341 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005342 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
NeilBrownd7603b72006-01-06 00:20:30 -08005344 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005345 sysfs_notify_dirent_safe(mddev->sysfs_state);
5346 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005347 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 return 0;
5349}
NeilBrown390ee602010-06-01 19:37:27 +10005350EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
NeilBrownfd01b882011-10-11 16:47:53 +11005352static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005353{
5354 int err;
5355
5356 err = md_run(mddev);
5357 if (err)
5358 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005359 err = bitmap_load(mddev);
5360 if (err) {
5361 bitmap_destroy(mddev);
5362 goto out;
5363 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005364
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005365 if (mddev_is_clustered(mddev))
5366 md_allow_write(mddev);
5367
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005368 md_wakeup_thread(mddev->thread);
5369 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5370
NeilBrownfe60b012010-03-29 11:10:42 +11005371 set_capacity(mddev->gendisk, mddev->array_sectors);
5372 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005373 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005374 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5375out:
5376 return err;
5377}
5378
NeilBrownfd01b882011-10-11 16:47:53 +11005379static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380{
5381 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
Andre Noll80fab1d2008-07-11 22:02:21 +10005383 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005385 return -ENXIO;
5386 if (!mddev->pers)
5387 return -EINVAL;
5388 if (!mddev->ro)
5389 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005390 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5391 struct md_rdev *rdev;
5392 bool has_journal = false;
5393
5394 rcu_read_lock();
5395 rdev_for_each_rcu(rdev, mddev) {
5396 if (test_bit(Journal, &rdev->flags) &&
5397 !test_bit(Faulty, &rdev->flags)) {
5398 has_journal = true;
5399 break;
5400 }
5401 }
5402 rcu_read_unlock();
5403
5404 /* Don't restart rw with journal missing/faulty */
5405 if (!has_journal)
5406 return -EINVAL;
5407 }
5408
Andre Noll80fab1d2008-07-11 22:02:21 +10005409 mddev->safemode = 0;
5410 mddev->ro = 0;
5411 set_disk_ro(disk, 0);
5412 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5413 mdname(mddev));
5414 /* Kick recovery or resync if necessary */
5415 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5416 md_wakeup_thread(mddev->thread);
5417 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005418 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420}
5421
NeilBrownfd01b882011-10-11 16:47:53 +11005422static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005423{
5424 mddev->array_sectors = 0;
5425 mddev->external_size = 0;
5426 mddev->dev_sectors = 0;
5427 mddev->raid_disks = 0;
5428 mddev->recovery_cp = 0;
5429 mddev->resync_min = 0;
5430 mddev->resync_max = MaxSector;
5431 mddev->reshape_position = MaxSector;
5432 mddev->external = 0;
5433 mddev->persistent = 0;
5434 mddev->level = LEVEL_NONE;
5435 mddev->clevel[0] = 0;
5436 mddev->flags = 0;
5437 mddev->ro = 0;
5438 mddev->metadata_type[0] = 0;
5439 mddev->chunk_sectors = 0;
5440 mddev->ctime = mddev->utime = 0;
5441 mddev->layout = 0;
5442 mddev->max_disks = 0;
5443 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005444 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005445 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005446 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005447 mddev->new_level = LEVEL_NONE;
5448 mddev->new_layout = 0;
5449 mddev->new_chunk_sectors = 0;
5450 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005451 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005452 mddev->suspend_lo = mddev->suspend_hi = 0;
5453 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5454 mddev->recovery = 0;
5455 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005456 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005457 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005458 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005459 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005460 mddev->bitmap_info.offset = 0;
5461 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005462 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005463 mddev->bitmap_info.chunksize = 0;
5464 mddev->bitmap_info.daemon_sleep = 0;
5465 mddev->bitmap_info.max_write_behind = 0;
5466}
5467
NeilBrownfd01b882011-10-11 16:47:53 +11005468static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005469{
NeilBrown6b6204e2013-05-09 09:48:30 +10005470 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005471 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005472 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005473 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005474 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005475 }
5476
5477 del_timer_sync(&mddev->safemode_timer);
5478
5479 bitmap_flush(mddev);
5480 md_super_wait(mddev);
5481
NeilBrownb6d428c2013-04-24 11:42:42 +10005482 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005483 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5484 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005485 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005486 if (!mddev_is_clustered(mddev))
5487 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005488 md_update_sb(mddev, 1);
5489 }
5490}
NeilBrowndefad612011-01-14 09:14:33 +11005491
NeilBrownfd01b882011-10-11 16:47:53 +11005492void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005493{
NeilBrown29f097c2013-11-14 17:54:51 +11005494 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005495 __md_stop_writes(mddev);
5496 mddev_unlock(mddev);
5497}
NeilBrown390ee602010-06-01 19:37:27 +10005498EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005499
NeilBrown5aa61f42014-12-15 12:56:57 +11005500static void mddev_detach(struct mddev *mddev)
5501{
5502 struct bitmap *bitmap = mddev->bitmap;
5503 /* wait for behind writes to complete */
5504 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5505 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5506 mdname(mddev));
5507 /* need to kick something here to make sure I/O goes? */
5508 wait_event(bitmap->behind_wait,
5509 atomic_read(&bitmap->behind_writes) == 0);
5510 }
NeilBrown36d091f2014-12-15 12:56:58 +11005511 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005512 mddev->pers->quiesce(mddev, 1);
5513 mddev->pers->quiesce(mddev, 0);
5514 }
5515 md_unregister_thread(&mddev->thread);
5516 if (mddev->queue)
5517 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5518}
5519
NeilBrown5eff3c42012-11-19 10:47:48 +11005520static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005521{
NeilBrown36d091f2014-12-15 12:56:58 +11005522 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005523 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005524 /* Ensure ->event_work is done */
5525 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005526 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005527 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005528 spin_unlock(&mddev->lock);
5529 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005530 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005531 if (pers->sync_request && mddev->to_remove == NULL)
5532 mddev->to_remove = &md_redundancy_group;
5533 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005534 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005535}
NeilBrown5eff3c42012-11-19 10:47:48 +11005536
5537void md_stop(struct mddev *mddev)
5538{
5539 /* stop the array and free an attached data structures.
5540 * This is called from dm-raid
5541 */
5542 __md_stop(mddev);
5543 bitmap_destroy(mddev);
5544 if (mddev->bio_set)
5545 bioset_free(mddev->bio_set);
5546}
5547
NeilBrown390ee602010-06-01 19:37:27 +10005548EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005549
NeilBrowna05b7ea2012-07-19 15:59:18 +10005550static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005551{
5552 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005553 int did_freeze = 0;
5554
5555 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5556 did_freeze = 1;
5557 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5558 md_wakeup_thread(mddev->thread);
5559 }
NeilBrownf851b602014-12-11 10:02:10 +11005560 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005561 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005562 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005563 /* Thread might be blocked waiting for metadata update
5564 * which will now never happen */
5565 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005566
NeilBrown88724bf2015-09-24 14:00:51 +10005567 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5568 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005569 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005570 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5571 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005572 wait_event(mddev->sb_wait,
5573 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005574 mddev_lock_nointr(mddev);
5575
NeilBrowna4bd82d2010-03-29 13:23:10 +11005576 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005577 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005578 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005579 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005580 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005581 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005582 if (did_freeze) {
5583 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005584 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005585 md_wakeup_thread(mddev->thread);
5586 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005587 err = -EBUSY;
5588 goto out;
5589 }
5590 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005591 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005592
5593 err = -ENXIO;
5594 if (mddev->ro==1)
5595 goto out;
5596 mddev->ro = 1;
5597 set_disk_ro(mddev->gendisk, 1);
5598 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005599 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5600 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005601 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005602 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005603 }
5604out:
5605 mutex_unlock(&mddev->open_mutex);
5606 return err;
5607}
5608
NeilBrown9e653b62006-06-26 00:27:58 -07005609/* mode:
5610 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005611 * 2 - stop but do not disassemble array
5612 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005613static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005614 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005617 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005618 int did_freeze = 0;
5619
5620 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5621 did_freeze = 1;
5622 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5623 md_wakeup_thread(mddev->thread);
5624 }
NeilBrownf851b602014-12-11 10:02:10 +11005625 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005626 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005627 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005628 /* Thread might be blocked waiting for metadata update
5629 * which will now never happen */
5630 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005631
NeilBrown30b8feb2013-11-14 15:16:17 +11005632 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005633 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5634 !test_bit(MD_RECOVERY_RUNNING,
5635 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005636 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
NeilBrownc8c00a62009-08-10 12:50:52 +10005638 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005639 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005640 mddev->sysfs_active ||
5641 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005642 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005643 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005644 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005645 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005646 if (did_freeze) {
5647 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005648 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005649 md_wakeup_thread(mddev->thread);
5650 }
NeilBrown260fa032013-08-27 16:44:13 +10005651 return -EBUSY;
5652 }
NeilBrown6e17b022010-08-07 21:41:19 +10005653 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005654 if (mddev->ro)
5655 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005656
NeilBrowndefad612011-01-14 09:14:33 +11005657 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005658 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005659 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005660
NeilBrowna4bd82d2010-03-29 13:23:10 +11005661 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005662 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005663
NeilBrowndafb20f2012-03-19 12:46:39 +11005664 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005665 if (rdev->raid_disk >= 0)
5666 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005667
NeilBrowna4bd82d2010-03-29 13:23:10 +11005668 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005669 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005670 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005671 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005672
NeilBrowna4bd82d2010-03-29 13:23:10 +11005673 if (mddev->ro)
5674 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005675 } else
5676 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 /*
5678 * Free resources if final stop
5679 */
NeilBrown9e653b62006-06-26 00:27:58 -07005680 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5682
NeilBrown978f9462006-02-02 14:28:05 -08005683 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005684 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005685 struct file *f = mddev->bitmap_info.file;
5686 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005687 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005688 spin_unlock(&mddev->lock);
5689 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005690 }
NeilBrownc3d97142009-12-14 12:49:52 +11005691 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005692
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 export_array(mddev);
5694
NeilBrown6177b472010-03-29 11:37:13 +11005695 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11005696 if (mddev->hold_active == UNTIL_STOP)
5697 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005698 }
NeilBrownd7603b72006-01-06 00:20:30 -08005699 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005700 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702}
5703
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005704#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005705static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706{
NeilBrown3cb03002011-10-11 16:45:26 +11005707 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 int err;
5709
NeilBrowna757e642005-04-16 15:26:42 -07005710 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
5713 printk(KERN_INFO "md: running: ");
5714
NeilBrowndafb20f2012-03-19 12:46:39 +11005715 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 char b[BDEVNAME_SIZE];
5717 printk("<%s>", bdevname(rdev->bdev,b));
5718 }
5719 printk("\n");
5720
NeilBrownd710e132008-10-13 11:55:12 +11005721 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 if (err) {
5723 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005724 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 }
5726}
5727
5728/*
5729 * lets try to run arrays based on all disks that have arrived
5730 * until now. (those are in pending_raid_disks)
5731 *
5732 * the method: pick the first pending disk, collect all disks with
5733 * the same UUID, remove all from the pending list and put them into
5734 * the 'same_array' list. Then order this list based on superblock
5735 * update time (freshest comes first), kick out 'old' disks and
5736 * compare superblocks. If everything's fine then run it.
5737 *
5738 * If "unit" is allocated, then bump its reference count
5739 */
5740static void autorun_devices(int part)
5741{
NeilBrown3cb03002011-10-11 16:45:26 +11005742 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005743 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 char b[BDEVNAME_SIZE];
5745
5746 printk(KERN_INFO "md: autorun ...\n");
5747 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005748 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005750 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005752 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753
5754 printk(KERN_INFO "md: considering %s ...\n",
5755 bdevname(rdev0->bdev,b));
5756 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005757 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 if (super_90_load(rdev, rdev0, 0) >= 0) {
5759 printk(KERN_INFO "md: adding %s ...\n",
5760 bdevname(rdev->bdev,b));
5761 list_move(&rdev->same_set, &candidates);
5762 }
5763 /*
5764 * now we have a set of devices, with all of them having
5765 * mostly sane superblocks. It's time to allocate the
5766 * mddev.
5767 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005768 if (part) {
5769 dev = MKDEV(mdp_major,
5770 rdev0->preferred_minor << MdpMinorShift);
5771 unit = MINOR(dev) >> MdpMinorShift;
5772 } else {
5773 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5774 unit = MINOR(dev);
5775 }
5776 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5778 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5779 break;
5780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
5782 md_probe(dev, NULL, NULL);
5783 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005784 if (!mddev || !mddev->gendisk) {
5785 if (mddev)
5786 mddev_put(mddev);
5787 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 "md: cannot allocate memory for md drive.\n");
5789 break;
5790 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005791 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 printk(KERN_WARNING "md: %s locked, cannot run\n",
5793 mdname(mddev));
5794 else if (mddev->raid_disks || mddev->major_version
5795 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005796 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 "md: %s already running, cannot run %s\n",
5798 mdname(mddev), bdevname(rdev0->bdev,b));
5799 mddev_unlock(mddev);
5800 } else {
5801 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005802 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005803 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 list_del_init(&rdev->same_set);
5805 if (bind_rdev_to_array(rdev, mddev))
5806 export_rdev(rdev);
5807 }
5808 autorun_array(mddev);
5809 mddev_unlock(mddev);
5810 }
5811 /* on success, candidates will be empty, on error
5812 * it won't...
5813 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005814 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005815 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 mddev_put(mddev);
5819 }
5820 printk(KERN_INFO "md: ... autorun DONE.\n");
5821}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005822#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
NeilBrownf72ffdd2014-09-30 14:23:59 +10005824static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825{
5826 mdu_version_t ver;
5827
5828 ver.major = MD_MAJOR_VERSION;
5829 ver.minor = MD_MINOR_VERSION;
5830 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5831
5832 if (copy_to_user(arg, &ver, sizeof(ver)))
5833 return -EFAULT;
5834
5835 return 0;
5836}
5837
NeilBrownf72ffdd2014-09-30 14:23:59 +10005838static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839{
5840 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005841 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005842 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
NeilBrown1ca69c42012-10-11 13:37:33 +11005844 nr = working = insync = failed = spare = 0;
5845 rcu_read_lock();
5846 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005848 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 failed++;
5850 else {
5851 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005852 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005853 insync++;
Song Liub347af82016-08-11 17:14:45 -07005854 else if (test_bit(Journal, &rdev->flags))
5855 /* TODO: add journal count to md_u.h */
5856 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 else
5858 spare++;
5859 }
5860 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005861 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
5863 info.major_version = mddev->major_version;
5864 info.minor_version = mddev->minor_version;
5865 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005866 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005868 info.size = mddev->dev_sectors / 2;
5869 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005870 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 info.nr_disks = nr;
5872 info.raid_disks = mddev->raid_disks;
5873 info.md_minor = mddev->md_minor;
5874 info.not_persistent= !mddev->persistent;
5875
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005876 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 info.state = 0;
5878 if (mddev->in_sync)
5879 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005880 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005881 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005882 if (mddev_is_clustered(mddev))
5883 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005884 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 info.working_disks = working;
5886 info.failed_disks = failed;
5887 info.spare_disks = spare;
5888
5889 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005890 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
5892 if (copy_to_user(arg, &info, sizeof(info)))
5893 return -EFAULT;
5894
5895 return 0;
5896}
5897
NeilBrownf72ffdd2014-09-30 14:23:59 +10005898static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005899{
5900 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005901 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005902 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005903
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005904 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005905 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005906 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005907
NeilBrown32a76272005-06-21 17:17:14 -07005908 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005909 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005910 /* bitmap enabled */
5911 if (mddev->bitmap_info.file) {
5912 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5913 sizeof(file->pathname));
5914 if (IS_ERR(ptr))
5915 err = PTR_ERR(ptr);
5916 else
5917 memmove(file->pathname, ptr,
5918 sizeof(file->pathname)-(ptr-file->pathname));
5919 }
NeilBrown4af1a042014-12-15 12:57:00 +11005920 spin_unlock(&mddev->lock);
5921
5922 if (err == 0 &&
5923 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005924 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005925
NeilBrown32a76272005-06-21 17:17:14 -07005926 kfree(file);
5927 return err;
5928}
5929
NeilBrownf72ffdd2014-09-30 14:23:59 +10005930static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931{
5932 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005933 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
5935 if (copy_from_user(&info, arg, sizeof(info)))
5936 return -EFAULT;
5937
NeilBrown1ca69c42012-10-11 13:37:33 +11005938 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005939 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 if (rdev) {
5941 info.major = MAJOR(rdev->bdev->bd_dev);
5942 info.minor = MINOR(rdev->bdev->bd_dev);
5943 info.raid_disk = rdev->raid_disk;
5944 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005945 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005947 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 info.state |= (1<<MD_DISK_ACTIVE);
5949 info.state |= (1<<MD_DISK_SYNC);
5950 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005951 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005952 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005953 if (test_bit(WriteMostly, &rdev->flags))
5954 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 } else {
5956 info.major = info.minor = 0;
5957 info.raid_disk = -1;
5958 info.state = (1<<MD_DISK_REMOVED);
5959 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005960 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961
5962 if (copy_to_user(arg, &info, sizeof(info)))
5963 return -EFAULT;
5964
5965 return 0;
5966}
5967
NeilBrownf72ffdd2014-09-30 14:23:59 +10005968static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969{
5970 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005971 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 dev_t dev = MKDEV(info->major,info->minor);
5973
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005974 if (mddev_is_clustered(mddev) &&
5975 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005976 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005977 mdname(mddev));
5978 return -EINVAL;
5979 }
5980
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5982 return -EOVERFLOW;
5983
5984 if (!mddev->raid_disks) {
5985 int err;
5986 /* expecting a device which has a superblock */
5987 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5988 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005989 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 "md: md_import_device returned %ld\n",
5991 PTR_ERR(rdev));
5992 return PTR_ERR(rdev);
5993 }
5994 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005995 struct md_rdev *rdev0
5996 = list_entry(mddev->disks.next,
5997 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005998 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 .load_super(rdev, rdev0, mddev->minor_version);
6000 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006001 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006003 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 bdevname(rdev0->bdev,b2));
6005 export_rdev(rdev);
6006 return -EINVAL;
6007 }
6008 }
6009 err = bind_rdev_to_array(rdev, mddev);
6010 if (err)
6011 export_rdev(rdev);
6012 return err;
6013 }
6014
6015 /*
6016 * add_new_disk can be used once the array is assembled
6017 * to add "hot spares". They must already have a superblock
6018 * written
6019 */
6020 if (mddev->pers) {
6021 int err;
6022 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006023 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 "%s: personality does not support diskops!\n",
6025 mdname(mddev));
6026 return -EINVAL;
6027 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006028 if (mddev->persistent)
6029 rdev = md_import_device(dev, mddev->major_version,
6030 mddev->minor_version);
6031 else
6032 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006034 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 "md: md_import_device returned %ld\n",
6036 PTR_ERR(rdev));
6037 return PTR_ERR(rdev);
6038 }
NeilBrown1a855a02010-12-09 16:36:28 +11006039 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006040 if (!mddev->persistent) {
6041 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006042 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006043 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006044 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006045 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006046 } else
NeilBrown41158c72005-06-21 17:17:25 -07006047 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006048 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006049 } else
6050 super_types[mddev->major_version].
6051 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006052 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006053 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006054 /* This was a hot-add request, but events doesn't
6055 * match, so reject it.
6056 */
6057 export_rdev(rdev);
6058 return -EINVAL;
6059 }
6060
NeilBrownb2d444d2005-11-08 21:39:31 -08006061 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006062 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6063 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006064 else
6065 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006066
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006067 if (info->state & (1<<MD_DISK_JOURNAL)) {
6068 struct md_rdev *rdev2;
6069 bool has_journal = false;
6070
6071 /* make sure no existing journal disk */
6072 rdev_for_each(rdev2, mddev) {
6073 if (test_bit(Journal, &rdev2->flags)) {
6074 has_journal = true;
6075 break;
6076 }
6077 }
6078 if (has_journal) {
6079 export_rdev(rdev);
6080 return -EBUSY;
6081 }
Song Liubac624f2015-08-13 14:31:55 -07006082 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006083 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006084 /*
6085 * check whether the device shows up in other nodes
6086 */
6087 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006088 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006089 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006090 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006091 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006092 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006093 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006094 export_rdev(rdev);
6095 return err;
6096 }
6097 }
6098 }
6099
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 rdev->raid_disk = -1;
6101 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006102
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 if (err)
6104 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006105
6106 if (mddev_is_clustered(mddev)) {
6107 if (info->state & (1 << MD_DISK_CANDIDATE))
6108 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6109 else {
6110 if (err)
6111 md_cluster_ops->add_new_disk_cancel(mddev);
6112 else
6113 err = add_bound_rdev(rdev);
6114 }
6115
6116 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006117 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006118
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 return err;
6120 }
6121
6122 /* otherwise, add_new_disk is only allowed
6123 * for major_version==0 superblocks
6124 */
6125 if (mddev->major_version != 0) {
6126 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6127 mdname(mddev));
6128 return -EINVAL;
6129 }
6130
6131 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6132 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006133 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006135 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 "md: error, md_import_device() returned %ld\n",
6137 PTR_ERR(rdev));
6138 return PTR_ERR(rdev);
6139 }
6140 rdev->desc_nr = info->number;
6141 if (info->raid_disk < mddev->raid_disks)
6142 rdev->raid_disk = info->raid_disk;
6143 else
6144 rdev->raid_disk = -1;
6145
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006147 if (info->state & (1<<MD_DISK_SYNC))
6148 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006150 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6151 set_bit(WriteMostly, &rdev->flags);
6152
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 if (!mddev->persistent) {
6154 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006155 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6156 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006157 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006158 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
NeilBrown2bf071b2006-01-06 00:20:55 -08006160 err = bind_rdev_to_array(rdev, mddev);
6161 if (err) {
6162 export_rdev(rdev);
6163 return err;
6164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 }
6166
6167 return 0;
6168}
6169
NeilBrownf72ffdd2014-09-30 14:23:59 +10006170static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171{
6172 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006173 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 rdev = find_rdev(mddev, dev);
6176 if (!rdev)
6177 return -ENXIO;
6178
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006179 if (rdev->raid_disk < 0)
6180 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006181
NeilBrown3ea8929d2013-04-24 11:42:41 +10006182 clear_bit(Blocked, &rdev->flags);
6183 remove_and_add_spares(mddev, rdev);
6184
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 if (rdev->raid_disk >= 0)
6186 goto busy;
6187
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006188kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006189 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006190 md_cluster_ops->remove_disk(mddev, rdev);
6191
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006192 md_kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07006193 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006194 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195
6196 return 0;
6197busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006198 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 bdevname(rdev->bdev,b), mdname(mddev));
6200 return -EBUSY;
6201}
6202
NeilBrownf72ffdd2014-09-30 14:23:59 +10006203static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204{
6205 char b[BDEVNAME_SIZE];
6206 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006207 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
6209 if (!mddev->pers)
6210 return -ENODEV;
6211
6212 if (mddev->major_version != 0) {
6213 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6214 " version-0 superblocks.\n",
6215 mdname(mddev));
6216 return -EINVAL;
6217 }
6218 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006219 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 "%s: personality does not support diskops!\n",
6221 mdname(mddev));
6222 return -EINVAL;
6223 }
6224
NeilBrownd710e132008-10-13 11:55:12 +11006225 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006227 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 "md: error, md_import_device() returned %ld\n",
6229 PTR_ERR(rdev));
6230 return -EINVAL;
6231 }
6232
6233 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006234 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006236 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
NeilBrown8190e752009-06-18 08:48:58 +10006238 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239
NeilBrownb2d444d2005-11-08 21:39:31 -08006240 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006241 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 "md: can not hot-add faulty %s disk to %s!\n",
6243 bdevname(rdev->bdev,b), mdname(mddev));
6244 err = -EINVAL;
6245 goto abort_export;
6246 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006247
NeilBrownb2d444d2005-11-08 21:39:31 -08006248 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006250 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006251 err = bind_rdev_to_array(rdev, mddev);
6252 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006253 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
6255 /*
6256 * The rest should better be atomic, we can have disk failures
6257 * noticed in interrupt contexts ...
6258 */
6259
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 rdev->raid_disk = -1;
6261
NeilBrown850b2b422006-10-03 01:15:46 -07006262 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 /*
6264 * Kick recovery, maybe this spare has to be added to the
6265 * array immediately.
6266 */
6267 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6268 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006269 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 return 0;
6271
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272abort_export:
6273 export_rdev(rdev);
6274 return err;
6275}
6276
NeilBrownfd01b882011-10-11 16:47:53 +11006277static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006278{
NeilBrown035328c2014-04-09 12:25:40 +10006279 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006280
NeilBrown36fa3062005-09-09 16:23:45 -07006281 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006282 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006283 return -EBUSY;
6284 if (mddev->recovery || mddev->sync_thread)
6285 return -EBUSY;
6286 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006287 }
6288
NeilBrown36fa3062005-09-09 16:23:45 -07006289 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006290 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006291 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006292
NeilBrown1e594bb2014-12-15 12:57:00 +11006293 if (mddev->bitmap || mddev->bitmap_info.file)
6294 return -EEXIST; /* cannot add when bitmap is present */
6295 f = fget(fd);
6296
6297 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006298 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6299 mdname(mddev));
6300 return -EBADF;
6301 }
6302
NeilBrown1e594bb2014-12-15 12:57:00 +11006303 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006304 if (!S_ISREG(inode->i_mode)) {
6305 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6306 mdname(mddev));
6307 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006308 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006309 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6310 mdname(mddev));
6311 err = -EBADF;
6312 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006313 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6314 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006315 err = -EBUSY;
6316 }
6317 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006318 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006319 return err;
6320 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006321 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006322 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006323 } else if (mddev->bitmap == NULL)
6324 return -ENOENT; /* cannot remove what isn't there */
6325 err = 0;
6326 if (mddev->pers) {
6327 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006328 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006329 struct bitmap *bitmap;
6330
6331 bitmap = bitmap_create(mddev, -1);
6332 if (!IS_ERR(bitmap)) {
6333 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006334 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006335 } else
6336 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006337 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006338 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006339 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006340 fd = -1; /* make sure to put the file */
6341 }
NeilBrown36fa3062005-09-09 16:23:45 -07006342 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006343 }
6344 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006345 struct file *f = mddev->bitmap_info.file;
6346 if (f) {
6347 spin_lock(&mddev->lock);
6348 mddev->bitmap_info.file = NULL;
6349 spin_unlock(&mddev->lock);
6350 fput(f);
6351 }
NeilBrown36fa3062005-09-09 16:23:45 -07006352 }
6353
NeilBrown32a76272005-06-21 17:17:14 -07006354 return err;
6355}
6356
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357/*
6358 * set_array_info is used two different ways
6359 * The original usage is when creating a new array.
6360 * In this usage, raid_disks is > 0 and it together with
6361 * level, size, not_persistent,layout,chunksize determine the
6362 * shape of the array.
6363 * This will always create an array with a type-0.90.0 superblock.
6364 * The newer usage is when assembling an array.
6365 * In this case raid_disks will be 0, and the major_version field is
6366 * use to determine which style super-blocks are to be found on the devices.
6367 * The minor and patch _version numbers are also kept incase the
6368 * super_block handler wishes to interpret them.
6369 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006370static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371{
6372
6373 if (info->raid_disks == 0) {
6374 /* just setting version number for superblock loading */
6375 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006376 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 super_types[info->major_version].name == NULL) {
6378 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006379 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 "md: superblock version %d not known\n",
6381 info->major_version);
6382 return -EINVAL;
6383 }
6384 mddev->major_version = info->major_version;
6385 mddev->minor_version = info->minor_version;
6386 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006387 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006388 /* ensure mddev_put doesn't delete this now that there
6389 * is some minimal configuration.
6390 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006391 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 return 0;
6393 }
6394 mddev->major_version = MD_MAJOR_VERSION;
6395 mddev->minor_version = MD_MINOR_VERSION;
6396 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006397 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398
6399 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006400 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006401 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 mddev->raid_disks = info->raid_disks;
6403 /* don't set md_minor, it is determined by which /dev/md* was
6404 * openned
6405 */
6406 if (info->state & (1<<MD_SB_CLEAN))
6407 mddev->recovery_cp = MaxSector;
6408 else
6409 mddev->recovery_cp = 0;
6410 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006411 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412
6413 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006414 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415
6416 mddev->max_disks = MD_SB_DISKS;
6417
NeilBrowne6910632008-02-06 01:39:51 -08006418 if (mddev->persistent)
6419 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006420 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
NeilBrownc3d97142009-12-14 12:49:52 +11006422 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006423 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006424 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006425
NeilBrownf6705572006-03-27 01:18:11 -08006426 mddev->reshape_position = MaxSector;
6427
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 /*
6429 * Generate a 128 bit UUID
6430 */
6431 get_random_bytes(mddev->uuid, 16);
6432
NeilBrownf6705572006-03-27 01:18:11 -08006433 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006434 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006435 mddev->new_layout = mddev->layout;
6436 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006437 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006438
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 return 0;
6440}
6441
NeilBrownfd01b882011-10-11 16:47:53 +11006442void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006443{
Dan Williamsb522adc2009-03-31 15:00:31 +11006444 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6445
6446 if (mddev->external_size)
6447 return;
6448
Dan Williams1f403622009-03-31 14:59:03 +11006449 mddev->array_sectors = array_sectors;
6450}
6451EXPORT_SYMBOL(md_set_array_sectors);
6452
NeilBrownfd01b882011-10-11 16:47:53 +11006453static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006454{
NeilBrown3cb03002011-10-11 16:45:26 +11006455 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006456 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006457 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006458
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006459 /* cluster raid doesn't support update size */
6460 if (mddev_is_clustered(mddev))
6461 return -EINVAL;
6462
NeilBrowna35b0d62006-01-06 00:20:49 -08006463 if (mddev->pers->resize == NULL)
6464 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006465 /* The "num_sectors" is the number of sectors of each device that
6466 * is used. This can only make sense for arrays with redundancy.
6467 * linear and raid0 always use whatever space is available. We can only
6468 * consider changing this number if no resync or reconstruction is
6469 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006470 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006471 * of each device. If num_sectors is zero, we find the largest size
6472 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006473 */
NeilBrownf851b602014-12-11 10:02:10 +11006474 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6475 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006476 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006477 if (mddev->ro)
6478 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006479
NeilBrowndafb20f2012-03-19 12:46:39 +11006480 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006481 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006482
Andre Nolld71f9f82008-07-11 22:02:22 +10006483 if (fit && (num_sectors == 0 || num_sectors > avail))
6484 num_sectors = avail;
6485 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006486 return -ENOSPC;
6487 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006488 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006489 if (!rv)
6490 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006491 return rv;
6492}
6493
NeilBrownfd01b882011-10-11 16:47:53 +11006494static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006495{
6496 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006497 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006498 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006499 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006500 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006501 if (mddev->ro)
6502 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006503 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006504 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006505 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006506 if (mddev->sync_thread ||
6507 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6508 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006509 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006510
6511 rdev_for_each(rdev, mddev) {
6512 if (mddev->raid_disks < raid_disks &&
6513 rdev->data_offset < rdev->new_data_offset)
6514 return -EINVAL;
6515 if (mddev->raid_disks > raid_disks &&
6516 rdev->data_offset > rdev->new_data_offset)
6517 return -EINVAL;
6518 }
6519
NeilBrown63c70c42006-03-27 01:18:13 -08006520 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006521 if (mddev->delta_disks < 0)
6522 mddev->reshape_backwards = 1;
6523 else if (mddev->delta_disks > 0)
6524 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006525
6526 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006527 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006528 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006529 mddev->reshape_backwards = 0;
6530 }
NeilBrownda943b992006-01-06 00:20:54 -08006531 return rv;
6532}
6533
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534/*
6535 * update_array_info is used to change the configuration of an
6536 * on-line array.
6537 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6538 * fields in the info are checked against the array.
6539 * Any differences that cannot be handled will cause an error.
6540 * Normally, only one change can be managed at a time.
6541 */
NeilBrownfd01b882011-10-11 16:47:53 +11006542static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543{
6544 int rv = 0;
6545 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006546 int state = 0;
6547
6548 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006549 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006550 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551
6552 if (mddev->major_version != info->major_version ||
6553 mddev->minor_version != info->minor_version ||
6554/* mddev->patch_version != info->patch_version || */
6555 mddev->ctime != info->ctime ||
6556 mddev->level != info->level ||
6557/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006558 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006559 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006560 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6561 ((state^info->state) & 0xfffffe00)
6562 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 return -EINVAL;
6564 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006565 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6566 cnt++;
6567 if (mddev->raid_disks != info->raid_disks)
6568 cnt++;
6569 if (mddev->layout != info->layout)
6570 cnt++;
6571 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6572 cnt++;
6573 if (cnt == 0)
6574 return 0;
6575 if (cnt > 1)
6576 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577
6578 if (mddev->layout != info->layout) {
6579 /* Change layout
6580 * we don't need to do anything at the md level, the
6581 * personality will take care of it all.
6582 */
NeilBrown50ac1682009-06-18 08:47:55 +10006583 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006585 else {
6586 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006587 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006588 if (rv)
6589 mddev->new_layout = mddev->layout;
6590 return rv;
6591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006593 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006594 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595
NeilBrownda943b992006-01-06 00:20:54 -08006596 if (mddev->raid_disks != info->raid_disks)
6597 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
NeilBrown36fa3062005-09-09 16:23:45 -07006599 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006600 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6601 rv = -EINVAL;
6602 goto err;
6603 }
6604 if (mddev->recovery || mddev->sync_thread) {
6605 rv = -EBUSY;
6606 goto err;
6607 }
NeilBrown36fa3062005-09-09 16:23:45 -07006608 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006609 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006610 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006611 if (mddev->bitmap) {
6612 rv = -EEXIST;
6613 goto err;
6614 }
6615 if (mddev->bitmap_info.default_offset == 0) {
6616 rv = -EINVAL;
6617 goto err;
6618 }
NeilBrownc3d97142009-12-14 12:49:52 +11006619 mddev->bitmap_info.offset =
6620 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006621 mddev->bitmap_info.space =
6622 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006623 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006624 bitmap = bitmap_create(mddev, -1);
6625 if (!IS_ERR(bitmap)) {
6626 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006627 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006628 } else
6629 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006630 if (rv)
6631 bitmap_destroy(mddev);
6632 mddev->pers->quiesce(mddev, 0);
6633 } else {
6634 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006635 if (!mddev->bitmap) {
6636 rv = -ENOENT;
6637 goto err;
6638 }
6639 if (mddev->bitmap->storage.file) {
6640 rv = -EINVAL;
6641 goto err;
6642 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006643 if (mddev->bitmap_info.nodes) {
6644 /* hold PW on all the bitmap lock */
6645 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6646 printk("md: can't change bitmap to none since the"
6647 " array is in use by more than one node\n");
6648 rv = -EPERM;
6649 md_cluster_ops->unlock_all_bitmaps(mddev);
6650 goto err;
6651 }
6652
6653 mddev->bitmap_info.nodes = 0;
6654 md_cluster_ops->leave(mddev);
6655 }
NeilBrown36fa3062005-09-09 16:23:45 -07006656 mddev->pers->quiesce(mddev, 1);
6657 bitmap_destroy(mddev);
6658 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006659 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006660 }
6661 }
NeilBrown850b2b422006-10-03 01:15:46 -07006662 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006663 return rv;
6664err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 return rv;
6666}
6667
NeilBrownfd01b882011-10-11 16:47:53 +11006668static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669{
NeilBrown3cb03002011-10-11 16:45:26 +11006670 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006671 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
6673 if (mddev->pers == NULL)
6674 return -ENODEV;
6675
NeilBrown1ca69c42012-10-11 13:37:33 +11006676 rcu_read_lock();
6677 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006679 err = -ENODEV;
6680 else {
6681 md_error(mddev, rdev);
6682 if (!test_bit(Faulty, &rdev->flags))
6683 err = -EBUSY;
6684 }
6685 rcu_read_unlock();
6686 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687}
6688
Andre Noll2f9618c2008-04-25 18:57:58 +02006689/*
6690 * We have a problem here : there is no easy way to give a CHS
6691 * virtual geometry. We currently pretend that we have a 2 heads
6692 * 4 sectors (with a BIG number of cylinders...). This drives
6693 * dosfs just mad... ;-)
6694 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006695static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6696{
NeilBrownfd01b882011-10-11 16:47:53 +11006697 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006698
6699 geo->heads = 2;
6700 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006701 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006702 return 0;
6703}
6704
Nicolas Schichancb335f82014-01-15 16:58:52 +01006705static inline bool md_ioctl_valid(unsigned int cmd)
6706{
6707 switch (cmd) {
6708 case ADD_NEW_DISK:
6709 case BLKROSET:
6710 case GET_ARRAY_INFO:
6711 case GET_BITMAP_FILE:
6712 case GET_DISK_INFO:
6713 case HOT_ADD_DISK:
6714 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006715 case RAID_AUTORUN:
6716 case RAID_VERSION:
6717 case RESTART_ARRAY_RW:
6718 case RUN_ARRAY:
6719 case SET_ARRAY_INFO:
6720 case SET_BITMAP_FILE:
6721 case SET_DISK_FAULTY:
6722 case STOP_ARRAY:
6723 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006724 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006725 return true;
6726 default:
6727 return false;
6728 }
6729}
6730
Al Viroa39907f2008-03-02 10:31:15 -05006731static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 unsigned int cmd, unsigned long arg)
6733{
6734 int err = 0;
6735 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006736 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006737 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
Nicolas Schichancb335f82014-01-15 16:58:52 +01006739 if (!md_ioctl_valid(cmd))
6740 return -ENOTTY;
6741
NeilBrown506c9e42011-12-23 10:17:26 +11006742 switch (cmd) {
6743 case RAID_VERSION:
6744 case GET_ARRAY_INFO:
6745 case GET_DISK_INFO:
6746 break;
6747 default:
6748 if (!capable(CAP_SYS_ADMIN))
6749 return -EACCES;
6750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751
6752 /*
6753 * Commands dealing with the RAID driver but not any
6754 * particular array:
6755 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006756 switch (cmd) {
6757 case RAID_VERSION:
6758 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006762 case RAID_AUTORUN:
6763 err = 0;
6764 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006765 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006767 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 }
6769
6770 /*
6771 * Commands creating/starting a new array:
6772 */
6773
Al Viroa39907f2008-03-02 10:31:15 -05006774 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775
6776 if (!mddev) {
6777 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 }
6780
NeilBrown1ca69c42012-10-11 13:37:33 +11006781 /* Some actions do not requires the mutex */
6782 switch (cmd) {
6783 case GET_ARRAY_INFO:
6784 if (!mddev->raid_disks && !mddev->external)
6785 err = -ENODEV;
6786 else
6787 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006788 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006789
6790 case GET_DISK_INFO:
6791 if (!mddev->raid_disks && !mddev->external)
6792 err = -ENODEV;
6793 else
6794 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006795 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006796
6797 case SET_DISK_FAULTY:
6798 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006799 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006800
6801 case GET_BITMAP_FILE:
6802 err = get_bitmap_file(mddev, argp);
6803 goto out;
6804
NeilBrown1ca69c42012-10-11 13:37:33 +11006805 }
6806
NeilBrowna7a3f082012-12-11 13:35:54 +11006807 if (cmd == ADD_NEW_DISK)
6808 /* need to ensure md_delayed_delete() has completed */
6809 flush_workqueue(md_misc_wq);
6810
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006811 if (cmd == HOT_REMOVE_DISK)
6812 /* need to ensure recovery thread has run */
6813 wait_event_interruptible_timeout(mddev->sb_wait,
6814 !test_bit(MD_RECOVERY_NEEDED,
6815 &mddev->flags),
6816 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006817 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6818 /* Need to flush page cache, and ensure no-one else opens
6819 * and writes
6820 */
6821 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006822 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006823 mutex_unlock(&mddev->open_mutex);
6824 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006825 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006826 }
6827 set_bit(MD_STILL_CLOSED, &mddev->flags);
6828 mutex_unlock(&mddev->open_mutex);
6829 sync_blockdev(bdev);
6830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 err = mddev_lock(mddev);
6832 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006833 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6835 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 }
6838
NeilBrownc02c0ae2012-12-11 13:39:21 +11006839 if (cmd == SET_ARRAY_INFO) {
6840 mdu_array_info_t info;
6841 if (!arg)
6842 memset(&info, 0, sizeof(info));
6843 else if (copy_from_user(&info, argp, sizeof(info))) {
6844 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006845 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006846 }
6847 if (mddev->pers) {
6848 err = update_array_info(mddev, &info);
6849 if (err) {
6850 printk(KERN_WARNING "md: couldn't update"
6851 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006852 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006854 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006855 }
6856 if (!list_empty(&mddev->disks)) {
6857 printk(KERN_WARNING
6858 "md: array %s already has disks!\n",
6859 mdname(mddev));
6860 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006861 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006862 }
6863 if (mddev->raid_disks) {
6864 printk(KERN_WARNING
6865 "md: array %s already initialised!\n",
6866 mdname(mddev));
6867 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006868 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006869 }
6870 err = set_array_info(mddev, &info);
6871 if (err) {
6872 printk(KERN_WARNING "md: couldn't set"
6873 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006874 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006875 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006876 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 }
6878
6879 /*
6880 * Commands querying/configuring an existing array:
6881 */
NeilBrown32a76272005-06-21 17:17:14 -07006882 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006883 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006884 if ((!mddev->raid_disks && !mddev->external)
6885 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6886 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6887 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006889 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 }
6891
6892 /*
6893 * Commands even a read-only array can execute:
6894 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006895 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006896 case RESTART_ARRAY_RW:
6897 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006898 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006899
6900 case STOP_ARRAY:
6901 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006902 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006903
6904 case STOP_ARRAY_RO:
6905 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006906 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006907
NeilBrown3ea8929d2013-04-24 11:42:41 +10006908 case HOT_REMOVE_DISK:
6909 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006910 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006911
NeilBrown7ceb17e2013-04-24 11:42:42 +10006912 case ADD_NEW_DISK:
6913 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08006914 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10006915 * So require mddev->pers and MD_DISK_SYNC.
6916 */
6917 if (mddev->pers) {
6918 mdu_disk_info_t info;
6919 if (copy_from_user(&info, argp, sizeof(info)))
6920 err = -EFAULT;
6921 else if (!(info.state & (1<<MD_DISK_SYNC)))
6922 /* Need to clear read-only for this */
6923 break;
6924 else
6925 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006926 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006927 }
6928 break;
6929
NeilBrownc02c0ae2012-12-11 13:39:21 +11006930 case BLKROSET:
6931 if (get_user(ro, (int __user *)(arg))) {
6932 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006933 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006934 }
6935 err = -EINVAL;
6936
6937 /* if the bdev is going readonly the value of mddev->ro
6938 * does not matter, no writes are coming
6939 */
6940 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006941 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006942
NeilBrownc02c0ae2012-12-11 13:39:21 +11006943 /* are we are already prepared for writes? */
6944 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006945 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006946
6947 /* transitioning to readauto need only happen for
6948 * arrays that call md_write_start
6949 */
6950 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006952 if (err == 0) {
6953 mddev->ro = 2;
6954 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006955 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006956 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006957 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 }
6959
6960 /*
6961 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006962 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 */
NeilBrown326eb172014-09-30 15:36:28 +10006964 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006965 if (mddev->ro == 2) {
6966 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006967 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006968 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006969 /* mddev_unlock will wake thread */
6970 /* If a device failed while we were read-only, we
6971 * need to make sure the metadata is updated now.
6972 */
6973 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6974 mddev_unlock(mddev);
6975 wait_event(mddev->sb_wait,
6976 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6977 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006978 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006979 }
NeilBrownf91de922005-11-08 21:39:36 -08006980 } else {
6981 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006982 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 }
6985
NeilBrownc02c0ae2012-12-11 13:39:21 +11006986 switch (cmd) {
6987 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006989 mdu_disk_info_t info;
6990 if (copy_from_user(&info, argp, sizeof(info)))
6991 err = -EFAULT;
6992 else
6993 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006994 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006997 case CLUSTERED_DISK_NACK:
6998 if (mddev_is_clustered(mddev))
6999 md_cluster_ops->new_disk_ack(mddev, false);
7000 else
7001 err = -EINVAL;
7002 goto unlock;
7003
NeilBrownc02c0ae2012-12-11 13:39:21 +11007004 case HOT_ADD_DISK:
7005 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007006 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007
NeilBrownc02c0ae2012-12-11 13:39:21 +11007008 case RUN_ARRAY:
7009 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007010 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011
NeilBrownc02c0ae2012-12-11 13:39:21 +11007012 case SET_BITMAP_FILE:
7013 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007014 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007015
NeilBrownc02c0ae2012-12-11 13:39:21 +11007016 default:
7017 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007018 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 }
7020
NeilBrown3adc28d2014-09-30 15:46:41 +10007021unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007022 if (mddev->hold_active == UNTIL_IOCTL &&
7023 err != -EINVAL)
7024 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007026out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 return err;
7028}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007029#ifdef CONFIG_COMPAT
7030static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7031 unsigned int cmd, unsigned long arg)
7032{
7033 switch (cmd) {
7034 case HOT_REMOVE_DISK:
7035 case HOT_ADD_DISK:
7036 case SET_DISK_FAULTY:
7037 case SET_BITMAP_FILE:
7038 /* These take in integer arg, do not convert */
7039 break;
7040 default:
7041 arg = (unsigned long)compat_ptr(arg);
7042 break;
7043 }
7044
7045 return md_ioctl(bdev, mode, cmd, arg);
7046}
7047#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048
Al Viroa39907f2008-03-02 10:31:15 -05007049static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050{
7051 /*
7052 * Succeed if we can lock the mddev, which confirms that
7053 * it isn't being stopped right now.
7054 */
NeilBrownfd01b882011-10-11 16:47:53 +11007055 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 int err;
7057
Yuanhan Liu0c098222012-05-22 13:55:32 +10007058 if (!mddev)
7059 return -ENODEV;
7060
NeilBrownd3374822009-01-09 08:31:10 +11007061 if (mddev->gendisk != bdev->bd_disk) {
7062 /* we are racing with mddev_put which is discarding this
7063 * bd_disk.
7064 */
7065 mddev_put(mddev);
7066 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007067 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007068 /* Then retry the open from the top */
7069 return -ERESTARTSYS;
7070 }
7071 BUG_ON(mddev != bdev->bd_disk->private_data);
7072
NeilBrownc8c00a62009-08-10 12:50:52 +10007073 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 goto out;
7075
7076 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007077 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10007078 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10007079 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007081 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 out:
7083 return err;
7084}
7085
Al Virodb2a1442013-05-05 21:52:57 -04007086static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007088 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007090 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007091 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007094
7095static int md_media_changed(struct gendisk *disk)
7096{
NeilBrownfd01b882011-10-11 16:47:53 +11007097 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007098
7099 return mddev->changed;
7100}
7101
7102static int md_revalidate(struct gendisk *disk)
7103{
NeilBrownfd01b882011-10-11 16:47:53 +11007104 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007105
7106 mddev->changed = 0;
7107 return 0;
7108}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007109static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110{
7111 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007112 .open = md_open,
7113 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007114 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007115#ifdef CONFIG_COMPAT
7116 .compat_ioctl = md_compat_ioctl,
7117#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007118 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007119 .media_changed = md_media_changed,
7120 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121};
7122
NeilBrownf72ffdd2014-09-30 14:23:59 +10007123static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124{
NeilBrown2b8bf342011-10-11 16:48:23 +11007125 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 /*
7128 * md_thread is a 'system-thread', it's priority should be very
7129 * high. We avoid resource deadlocks individually in each
7130 * raid personality. (RAID5 does preallocation) We also use RR and
7131 * the very same RT priority as kswapd, thus we will never get
7132 * into a priority inversion deadlock.
7133 *
7134 * we definitely have to have equal or higher priority than
7135 * bdflush, otherwise bdflush will deadlock if there are too
7136 * many dirty RAID5 blocks.
7137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138
NeilBrown6985c432005-10-19 21:23:47 -07007139 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007140 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141
NeilBrown93588e22005-11-15 00:09:12 -08007142 /* We need to wait INTERRUPTIBLE so that
7143 * we don't add to the load-average.
7144 * That means we need to be sure no signals are
7145 * pending
7146 */
7147 if (signal_pending(current))
7148 flush_signals(current);
7149
7150 wait_event_interruptible_timeout
7151 (thread->wqueue,
7152 test_bit(THREAD_WAKEUP, &thread->flags)
7153 || kthread_should_stop(),
7154 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155
NeilBrown6c987912011-01-14 09:13:53 +11007156 clear_bit(THREAD_WAKEUP, &thread->flags);
7157 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007158 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007160
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 return 0;
7162}
7163
NeilBrown2b8bf342011-10-11 16:48:23 +11007164void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165{
7166 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007167 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 set_bit(THREAD_WAKEUP, &thread->flags);
7169 wake_up(&thread->wqueue);
7170 }
7171}
NeilBrown6c144d32014-09-30 16:15:38 +10007172EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173
Shaohua Li4ed87312012-10-11 13:34:00 +11007174struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7175 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176{
NeilBrown2b8bf342011-10-11 16:48:23 +11007177 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178
NeilBrown2b8bf342011-10-11 16:48:23 +11007179 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 if (!thread)
7181 return NULL;
7182
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 init_waitqueue_head(&thread->wqueue);
7184
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 thread->run = run;
7186 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007187 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007188 thread->tsk = kthread_run(md_thread, thread,
7189 "%s_%s",
7190 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007191 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007192 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 kfree(thread);
7194 return NULL;
7195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 return thread;
7197}
NeilBrown6c144d32014-09-30 16:15:38 +10007198EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199
NeilBrown2b8bf342011-10-11 16:48:23 +11007200void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201{
NeilBrown2b8bf342011-10-11 16:48:23 +11007202 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007203 if (!thread)
7204 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007205 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007206 /* Locking ensures that mddev_unlock does not wake_up a
7207 * non-existent thread
7208 */
7209 spin_lock(&pers_lock);
7210 *threadp = NULL;
7211 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007212
7213 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 kfree(thread);
7215}
NeilBrown6c144d32014-09-30 16:15:38 +10007216EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217
NeilBrownfd01b882011-10-11 16:47:53 +11007218void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219{
NeilBrownb2d444d2005-11-08 21:39:31 -08007220 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007222
NeilBrownde393cd2011-07-28 11:31:48 +10007223 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 return;
7225 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007226 if (mddev->degraded)
7227 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007228 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7230 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7231 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007232 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007233 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007234 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235}
NeilBrown6c144d32014-09-30 16:15:38 +10007236EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237
7238/* seq_file implementation /proc/mdstat */
7239
7240static void status_unused(struct seq_file *seq)
7241{
7242 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007243 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244
7245 seq_printf(seq, "unused devices: ");
7246
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007247 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 char b[BDEVNAME_SIZE];
7249 i++;
7250 seq_printf(seq, "%s ",
7251 bdevname(rdev->bdev,b));
7252 }
7253 if (!i)
7254 seq_printf(seq, "<none>");
7255
7256 seq_printf(seq, "\n");
7257}
7258
NeilBrownf7851be2015-07-02 17:12:58 +10007259static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260{
NeilBrowndd71cf62009-05-07 12:49:35 +10007261 sector_t max_sectors, resync, res;
7262 unsigned long dt, db;
7263 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007264 int scale;
7265 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266
NeilBrownc804cde2012-05-21 09:28:33 +10007267 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7268 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007269 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007271 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272
NeilBrownf7851be2015-07-02 17:12:58 +10007273 resync = mddev->curr_resync;
7274 if (resync <= 3) {
7275 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7276 /* Still cleaning up */
7277 resync = max_sectors;
7278 } else
7279 resync -= atomic_read(&mddev->recovery_active);
7280
7281 if (resync == 0) {
7282 if (mddev->recovery_cp < MaxSector) {
7283 seq_printf(seq, "\tresync=PENDING");
7284 return 1;
7285 }
7286 return 0;
7287 }
7288 if (resync < 3) {
7289 seq_printf(seq, "\tresync=DELAYED");
7290 return 1;
7291 }
7292
NeilBrown403df472014-09-30 15:52:29 +10007293 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007294 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007295 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007296 * u32, as those are the requirements for sector_div.
7297 * Thus 'scale' must be at least 10
7298 */
7299 scale = 10;
7300 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007301 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007302 scale++;
7303 }
7304 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007305 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007306
7307 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 {
NeilBrown4588b422006-03-27 01:18:04 -08007309 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 seq_printf(seq, "[");
7311 for (i = 0; i < x; i++)
7312 seq_printf(seq, "=");
7313 seq_printf(seq, ">");
7314 for (i = 0; i < y; i++)
7315 seq_printf(seq, ".");
7316 seq_printf(seq, "] ");
7317 }
NeilBrown4588b422006-03-27 01:18:04 -08007318 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007319 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7320 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007321 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7322 "check" :
7323 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7324 "resync" : "recovery"))),
7325 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007326 (unsigned long long) resync/2,
7327 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328
7329 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330 * dt: time from mark until now
7331 * db: blocks written from mark until now
7332 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007333 *
7334 * rt is a sector_t, so could be 32bit or 64bit.
7335 * So we divide before multiply in case it is 32bit and close
7336 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007337 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007338 * near the end of resync when the number of remaining sectors
7339 * is close to 'db'.
7340 * We then divide rt by 32 after multiplying by db to compensate.
7341 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 */
7343 dt = ((jiffies - mddev->resync_mark) / HZ);
7344 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007345 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7346 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347
NeilBrowndd71cf62009-05-07 12:49:35 +10007348 rt = max_sectors - resync; /* number of remaining sectors */
7349 sector_div(rt, db/32+1);
7350 rt *= dt;
7351 rt >>= 5;
7352
7353 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7354 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
NeilBrownff4e8d92006-07-10 04:44:16 -07007356 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007357 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358}
7359
7360static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7361{
7362 struct list_head *tmp;
7363 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007364 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
7366 if (l >= 0x10000)
7367 return NULL;
7368 if (!l--)
7369 /* header */
7370 return (void*)1;
7371
7372 spin_lock(&all_mddevs_lock);
7373 list_for_each(tmp,&all_mddevs)
7374 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007375 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 mddev_get(mddev);
7377 spin_unlock(&all_mddevs_lock);
7378 return mddev;
7379 }
7380 spin_unlock(&all_mddevs_lock);
7381 if (!l--)
7382 return (void*)2;/* tail */
7383 return NULL;
7384}
7385
7386static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7387{
7388 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007389 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007390
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 ++*pos;
7392 if (v == (void*)2)
7393 return NULL;
7394
7395 spin_lock(&all_mddevs_lock);
7396 if (v == (void*)1)
7397 tmp = all_mddevs.next;
7398 else
7399 tmp = mddev->all_mddevs.next;
7400 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007401 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 else {
7403 next_mddev = (void*)2;
7404 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 spin_unlock(&all_mddevs_lock);
7407
7408 if (v != (void*)1)
7409 mddev_put(mddev);
7410 return next_mddev;
7411
7412}
7413
7414static void md_seq_stop(struct seq_file *seq, void *v)
7415{
NeilBrownfd01b882011-10-11 16:47:53 +11007416 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417
7418 if (mddev && v != (void*)1 && v != (void*)2)
7419 mddev_put(mddev);
7420}
7421
7422static int md_seq_show(struct seq_file *seq, void *v)
7423{
NeilBrownfd01b882011-10-11 16:47:53 +11007424 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007425 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007426 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427
7428 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007429 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 seq_printf(seq, "Personalities : ");
7431 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007432 list_for_each_entry(pers, &pers_list, list)
7433 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434
7435 spin_unlock(&pers_lock);
7436 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007437 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 return 0;
7439 }
7440 if (v == (void*)2) {
7441 status_unused(seq);
7442 return 0;
7443 }
7444
NeilBrown36d091f2014-12-15 12:56:58 +11007445 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7447 seq_printf(seq, "%s : %sactive", mdname(mddev),
7448 mddev->pers ? "" : "in");
7449 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007450 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007452 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007453 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 seq_printf(seq, " %s", mddev->pers->name);
7455 }
7456
Andre Nolldd8ac332009-03-31 14:33:13 +11007457 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007458 rcu_read_lock();
7459 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 char b[BDEVNAME_SIZE];
7461 seq_printf(seq, " %s[%d]",
7462 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007463 if (test_bit(WriteMostly, &rdev->flags))
7464 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007465 if (test_bit(Journal, &rdev->flags))
7466 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007467 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 seq_printf(seq, "(F)");
7469 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007470 }
7471 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007472 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007473 if (test_bit(Replacement, &rdev->flags))
7474 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007475 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007477 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
7479 if (!list_empty(&mddev->disks)) {
7480 if (mddev->pers)
7481 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007482 (unsigned long long)
7483 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 else
7485 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007486 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007488 if (mddev->persistent) {
7489 if (mddev->major_version != 0 ||
7490 mddev->minor_version != 90) {
7491 seq_printf(seq," super %d.%d",
7492 mddev->major_version,
7493 mddev->minor_version);
7494 }
NeilBrowne6910632008-02-06 01:39:51 -08007495 } else if (mddev->external)
7496 seq_printf(seq, " super external:%s",
7497 mddev->metadata_type);
7498 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007499 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500
7501 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007502 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007503 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007504 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007505 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007506 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007507 }
NeilBrown32a76272005-06-21 17:17:14 -07007508 } else
7509 seq_printf(seq, "\n ");
7510
NeilBrown57148962012-03-19 12:46:40 +11007511 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512
7513 seq_printf(seq, "\n");
7514 }
NeilBrown36d091f2014-12-15 12:56:58 +11007515 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007516
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517 return 0;
7518}
7519
Jan Engelhardt110518b2009-05-07 12:49:37 +10007520static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 .start = md_seq_start,
7522 .next = md_seq_next,
7523 .stop = md_seq_stop,
7524 .show = md_seq_show,
7525};
7526
7527static int md_seq_open(struct inode *inode, struct file *file)
7528{
Kay Sieversf1514632011-07-12 20:48:39 +02007529 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 int error;
7531
7532 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007533 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007534 return error;
7535
7536 seq = file->private_data;
7537 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 return error;
7539}
7540
NeilBrowne2f23b62014-04-09 14:33:51 +10007541static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007542static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7543{
Kay Sieversf1514632011-07-12 20:48:39 +02007544 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007545 int mask;
7546
NeilBrowne2f23b62014-04-09 14:33:51 +10007547 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007548 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007549 poll_wait(filp, &md_event_waiters, wait);
7550
7551 /* always allow read */
7552 mask = POLLIN | POLLRDNORM;
7553
Kay Sieversf1514632011-07-12 20:48:39 +02007554 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007555 mask |= POLLERR | POLLPRI;
7556 return mask;
7557}
7558
Arjan van de Venfa027c22007-02-12 00:55:33 -08007559static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007560 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 .open = md_seq_open,
7562 .read = seq_read,
7563 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007564 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007565 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566};
7567
NeilBrown84fc4b52011-10-11 16:49:58 +11007568int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569{
Chao Yu50bd3772014-09-25 15:28:34 +08007570 printk(KERN_INFO "md: %s personality registered for level %d\n",
7571 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007573 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 spin_unlock(&pers_lock);
7575 return 0;
7576}
NeilBrown6c144d32014-09-30 16:15:38 +10007577EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578
NeilBrown84fc4b52011-10-11 16:49:58 +11007579int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580{
NeilBrown2604b702006-01-06 00:20:36 -08007581 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007583 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 spin_unlock(&pers_lock);
7585 return 0;
7586}
NeilBrown6c144d32014-09-30 16:15:38 +10007587EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588
NeilBrown6022e752015-08-13 12:32:55 +10007589int register_md_cluster_operations(struct md_cluster_operations *ops,
7590 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007591{
NeilBrown6022e752015-08-13 12:32:55 +10007592 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007593 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007594 if (md_cluster_ops != NULL)
7595 ret = -EALREADY;
7596 else {
7597 md_cluster_ops = ops;
7598 md_cluster_mod = module;
7599 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007600 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007601 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007602}
7603EXPORT_SYMBOL(register_md_cluster_operations);
7604
7605int unregister_md_cluster_operations(void)
7606{
7607 spin_lock(&pers_lock);
7608 md_cluster_ops = NULL;
7609 spin_unlock(&pers_lock);
7610 return 0;
7611}
7612EXPORT_SYMBOL(unregister_md_cluster_operations);
7613
7614int md_setup_cluster(struct mddev *mddev, int nodes)
7615{
7616 int err;
7617
7618 err = request_module("md-cluster");
7619 if (err) {
7620 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007621 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007622 }
7623
7624 spin_lock(&pers_lock);
7625 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7626 spin_unlock(&pers_lock);
7627 return -ENOENT;
7628 }
7629 spin_unlock(&pers_lock);
7630
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007631 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007632}
7633
7634void md_cluster_stop(struct mddev *mddev)
7635{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007636 if (!md_cluster_ops)
7637 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007638 md_cluster_ops->leave(mddev);
7639 module_put(md_cluster_mod);
7640}
7641
NeilBrownfd01b882011-10-11 16:47:53 +11007642static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007644 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007646 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647
7648 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007649 rcu_read_lock();
7650 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007652 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7653 (int)part_stat_read(&disk->part0, sectors[1]) -
7654 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007655 /* sync IO will cause sync_io to increase before the disk_stats
7656 * as sync_io is counted when a request starts, and
7657 * disk_stats is counted when it completes.
7658 * So resync activity will cause curr_events to be smaller than
7659 * when there was no such activity.
7660 * non-sync IO will cause disk_stat to increase without
7661 * increasing sync_io so curr_events will (eventually)
7662 * be larger than it was before. Once it becomes
7663 * substantially larger, the test below will cause
7664 * the array to appear non-idle, and resync will slow
7665 * down.
7666 * If there is a lot of outstanding resync activity when
7667 * we set last_event to curr_events, then all that activity
7668 * completing might cause the array to appear non-idle
7669 * and resync will be slowed down even though there might
7670 * not have been non-resync activity. This will only
7671 * happen once though. 'last_events' will soon reflect
7672 * the state where there is little or no outstanding
7673 * resync requests, and further resync activity will
7674 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007675 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007677 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 rdev->last_events = curr_events;
7679 idle = 0;
7680 }
7681 }
NeilBrown4b809912008-07-21 17:05:25 +10007682 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 return idle;
7684}
7685
NeilBrownfd01b882011-10-11 16:47:53 +11007686void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687{
7688 /* another "blocks" (512byte) blocks have been synced */
7689 atomic_sub(blocks, &mddev->recovery_active);
7690 wake_up(&mddev->recovery_wait);
7691 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007692 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007693 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 md_wakeup_thread(mddev->thread);
7695 // stop recovery, signal do_sync ....
7696 }
7697}
NeilBrown6c144d32014-09-30 16:15:38 +10007698EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699
NeilBrown06d91a52005-06-21 17:17:12 -07007700/* md_write_start(mddev, bi)
7701 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007702 * in superblock) before writing, schedule a superblock update
7703 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007704 */
NeilBrownfd01b882011-10-11 16:47:53 +11007705void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706{
Neil Brown0fd62b82008-06-28 08:31:36 +10007707 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007708 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007709 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007710
NeilBrownf91de922005-11-08 21:39:36 -08007711 BUG_ON(mddev->ro == 1);
7712 if (mddev->ro == 2) {
7713 /* need to switch to read/write */
7714 mddev->ro = 0;
7715 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7716 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007717 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007718 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007719 }
NeilBrown06d91a52005-06-21 17:17:12 -07007720 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007721 if (mddev->safemode == 1)
7722 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007723 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007724 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007725 if (mddev->in_sync) {
7726 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007727 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007728 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007729 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007730 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007731 }
NeilBrown85572d72014-12-15 12:56:56 +11007732 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007733 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007734 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007735 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007736 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007737 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738}
NeilBrown6c144d32014-09-30 16:15:38 +10007739EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740
NeilBrownfd01b882011-10-11 16:47:53 +11007741void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742{
7743 if (atomic_dec_and_test(&mddev->writes_pending)) {
7744 if (mddev->safemode == 2)
7745 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007746 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7748 }
7749}
NeilBrown6c144d32014-09-30 16:15:38 +10007750EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751
NeilBrown2a2275d2007-01-26 00:57:11 -08007752/* md_allow_write(mddev)
7753 * Calling this ensures that the array is marked 'active' so that writes
7754 * may proceed without blocking. It is important to call this before
7755 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7756 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007757 *
Guoqing Jiangabf35082015-12-21 10:51:01 +11007758 * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007759 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007760 */
NeilBrownfd01b882011-10-11 16:47:53 +11007761int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007762{
7763 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007764 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007765 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007766 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007767 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007768 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007769
NeilBrown85572d72014-12-15 12:56:56 +11007770 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007771 if (mddev->in_sync) {
7772 mddev->in_sync = 0;
7773 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007774 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007775 if (mddev->safemode_delay &&
7776 mddev->safemode == 0)
7777 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007778 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007779 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007780 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007781 } else
NeilBrown85572d72014-12-15 12:56:56 +11007782 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007783
NeilBrown070dc6d2010-08-30 17:33:34 +10007784 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007785 return -EAGAIN;
7786 else
7787 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007788}
7789EXPORT_SYMBOL_GPL(md_allow_write);
7790
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791#define SYNC_MARKS 10
7792#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007793#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007794void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795{
Shaohua Li4ed87312012-10-11 13:34:00 +11007796 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007797 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798 unsigned int currspeed = 0,
7799 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007800 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007802 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 sector_t mark_cnt[SYNC_MARKS];
7804 int last_mark,m;
7805 struct list_head *tmp;
7806 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007807 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007808 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007809 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007810 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007811 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812
7813 /* just incase thread restarts... */
7814 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7815 return;
NeilBrown3991b312014-05-28 13:39:23 +10007816 if (mddev->ro) {/* never try to sync a read-only array */
7817 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007818 return;
NeilBrown3991b312014-05-28 13:39:23 +10007819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007821 if (mddev_is_clustered(mddev)) {
7822 ret = md_cluster_ops->resync_start(mddev);
7823 if (ret)
7824 goto skip;
7825
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04007826 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04007827 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7828 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
7829 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
7830 && ((unsigned long long)mddev->curr_resync_completed
7831 < (unsigned long long)mddev->resync_max_sectors))
7832 goto skip;
7833 }
7834
NeilBrown61df9d92006-10-03 01:15:57 -07007835 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007836 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007837 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007838 action = "check";
7839 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007840 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007841 action = "repair";
7842 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007843 desc = "resync";
7844 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7845 desc = "reshape";
7846 else
7847 desc = "recovery";
7848
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007849 mddev->last_sync_action = action ?: desc;
7850
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 /* we overload curr_resync somewhat here.
7852 * 0 == not engaged in resync at all
7853 * 2 == checking that there is no conflict with another sync
7854 * 1 == like 2, but have yielded to allow conflicting resync to
7855 * commense
7856 * other == active in resync - this many blocks
7857 *
7858 * Before starting a resync we must have set curr_resync to
7859 * 2, and then checked that every "conflicting" array has curr_resync
7860 * less than ours. When we find one that is the same or higher
7861 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7862 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7863 * This will mean we have to start checking from the beginning again.
7864 *
7865 */
7866
7867 do {
7868 mddev->curr_resync = 2;
7869
7870 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007871 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007873 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 if (mddev2 == mddev)
7875 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007876 if (!mddev->parallel_resync
7877 && mddev2->curr_resync
7878 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 DEFINE_WAIT(wq);
7880 if (mddev < mddev2 && mddev->curr_resync == 2) {
7881 /* arbitrarily yield */
7882 mddev->curr_resync = 1;
7883 wake_up(&resync_wait);
7884 }
7885 if (mddev > mddev2 && mddev->curr_resync == 1)
7886 /* no need to wait here, we can wait the next
7887 * time 'round when curr_resync == 2
7888 */
7889 continue;
NeilBrown97441972008-09-19 11:49:54 +10007890 /* We need to wait 'interruptible' so as not to
7891 * contribute to the load average, and not to
7892 * be caught by 'softlockup'
7893 */
7894 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007895 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007896 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007897 printk(KERN_INFO "md: delaying %s of %s"
7898 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007900 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007902 if (signal_pending(current))
7903 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 schedule();
7905 finish_wait(&resync_wait, &wq);
7906 goto try_again;
7907 }
7908 finish_wait(&resync_wait, &wq);
7909 }
7910 }
7911 } while (mddev->curr_resync < 2);
7912
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007913 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007914 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007916 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 */
7918 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007919 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007920 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007921 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7922 j = mddev->resync_min;
7923 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007924 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007925
NeilBrownccfcc3c2006-03-27 01:18:09 -08007926 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007927 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007928 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007930 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007931 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007932 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007933 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007934 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007935 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007936 !test_bit(Faulty, &rdev->flags) &&
7937 !test_bit(In_sync, &rdev->flags) &&
7938 rdev->recovery_offset < j)
7939 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007940 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007941
7942 /* If there is a bitmap, we need to make sure all
7943 * writes that started before we added a spare
7944 * complete before we start doing a recovery.
7945 * Otherwise the write might complete and (via
7946 * bitmap_endwrite) set a bit in the bitmap after the
7947 * recovery has checked that bit and skipped that
7948 * region.
7949 */
7950 if (mddev->bitmap) {
7951 mddev->pers->quiesce(mddev, 1);
7952 mddev->pers->quiesce(mddev, 0);
7953 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955
NeilBrown61df9d92006-10-03 01:15:57 -07007956 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7957 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7958 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007959 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007960 "(but not more than %d KB/sec) for %s.\n",
7961 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962
NeilBrowneea1bf32009-03-31 14:27:02 +11007963 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007964
NeilBrown57afd892005-06-21 17:17:13 -07007965 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966 for (m = 0; m < SYNC_MARKS; m++) {
7967 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007968 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969 }
7970 last_mark = 0;
7971 mddev->resync_mark = mark[last_mark];
7972 mddev->resync_mark_cnt = mark_cnt[last_mark];
7973
7974 /*
7975 * Tune reconstruction:
7976 */
7977 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007978 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7979 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980
7981 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 last_check = 0;
7983
7984 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007985 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007986 "md: resuming %s of %s from checkpoint.\n",
7987 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007989 } else
7990 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007991 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007992 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7993 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007994 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995
majianpeng7c2c57c2012-07-03 12:12:26 +10007996 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007998 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999
NeilBrown57afd892005-06-21 17:17:13 -07008000 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008001
NeilBrown7a91ee12009-05-26 12:57:21 +10008002 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8003 ((mddev->curr_resync > mddev->curr_resync_completed &&
8004 (mddev->curr_resync - mddev->curr_resync_completed)
8005 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008006 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008007 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008008 >= mddev->resync_max - mddev->curr_resync_completed ||
8009 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008010 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008011 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008012 wait_event(mddev->recovery_wait,
8013 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008014 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008015 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8016 j > mddev->recovery_cp)
8017 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008018 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10008019 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008020 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008021 }
NeilBrownacb180b2009-04-14 16:28:34 +10008022
NeilBrownc91abf52013-11-19 12:02:01 +11008023 while (j >= mddev->resync_max &&
8024 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008025 /* As this condition is controlled by user-space,
8026 * we can block indefinitely, so use '_interruptible'
8027 * to avoid triggering warnings.
8028 */
8029 flush_signals(current); /* just in case */
8030 wait_event_interruptible(mddev->recovery_wait,
8031 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008032 || test_bit(MD_RECOVERY_INTR,
8033 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008034 }
NeilBrownacb180b2009-04-14 16:28:34 +10008035
NeilBrownc91abf52013-11-19 12:02:01 +11008036 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8037 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008038
NeilBrown09314792015-02-19 16:04:40 +11008039 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008040 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008041 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008042 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043 }
NeilBrown57afd892005-06-21 17:17:13 -07008044
8045 if (!skipped) { /* actual IO requested */
8046 io_sectors += sectors;
8047 atomic_add(sectors, &mddev->recovery_active);
8048 }
8049
NeilBrowne875ece2011-07-28 11:39:24 +10008050 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8051 break;
8052
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008054 if (j > max_sectors)
8055 /* when skipping, extra large numbers can be returned. */
8056 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008057 if (j > 2)
8058 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008059 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008060 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008061 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008062 * visible in /proc/mdstat
8063 */
8064 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008065
8066 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067 continue;
8068
NeilBrown57afd892005-06-21 17:17:13 -07008069 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070 repeat:
8071 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8072 /* step marks */
8073 int next = (last_mark+1) % SYNC_MARKS;
8074
8075 mddev->resync_mark = mark[next];
8076 mddev->resync_mark_cnt = mark_cnt[next];
8077 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008078 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079 last_mark = next;
8080 }
8081
NeilBrownc91abf52013-11-19 12:02:01 +11008082 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008084
8085 /*
8086 * this loop exits only if either when we are slower than
8087 * the 'hard' speed limit, or the system was IO-idle for
8088 * a jiffy.
8089 * the system might be non-idle CPU-wise, but we only care
8090 * about not overloading the IO subsystem. (things like an
8091 * e2fsck being done on the RAID array should execute fast)
8092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093 cond_resched();
8094
Xiao Niac7e50a2014-08-07 09:37:41 -04008095 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8096 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008097 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098
NeilBrown88202a02006-01-06 00:21:36 -08008099 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008100 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008101 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008102 goto repeat;
8103 }
NeilBrownac8fa412015-02-19 16:55:00 +11008104 if (!is_mddev_idle(mddev, 0)) {
8105 /*
8106 * Give other IO more of a chance.
8107 * The faster the devices, the less we wait.
8108 */
8109 wait_event(mddev->recovery_wait,
8110 !atomic_read(&mddev->recovery_active));
8111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112 }
8113 }
NeilBrownc91abf52013-11-19 12:02:01 +11008114 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8115 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8116 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117 /*
8118 * this also signals 'finished resyncing' to md_stop
8119 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008120 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8122
NeilBrown5ed1df22015-07-24 13:27:08 +10008123 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8124 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8125 mddev->curr_resync > 2) {
8126 mddev->curr_resync_completed = mddev->curr_resync;
8127 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8128 }
NeilBrown09314792015-02-19 16:04:40 +11008129 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008130
NeilBrowndfc70642008-05-23 13:04:39 -07008131 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008132 mddev->curr_resync > 2) {
8133 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8134 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8135 if (mddev->curr_resync >= mddev->recovery_cp) {
8136 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008137 "md: checkpointing %s of %s.\n",
8138 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008139 if (test_bit(MD_RECOVERY_ERROR,
8140 &mddev->recovery))
8141 mddev->recovery_cp =
8142 mddev->curr_resync_completed;
8143 else
8144 mddev->recovery_cp =
8145 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008146 }
8147 } else
8148 mddev->recovery_cp = MaxSector;
8149 } else {
8150 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8151 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008152 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008153 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008154 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008155 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008156 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008157 !test_bit(Faulty, &rdev->flags) &&
8158 !test_bit(In_sync, &rdev->flags) &&
8159 rdev->recovery_offset < mddev->curr_resync)
8160 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008161 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008164 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008165 /* set CHANGE_PENDING here since maybe another update is needed,
8166 * so other nodes are informed. It should be harmless for normal
8167 * raid */
8168 set_mask_bits(&mddev->flags, 0,
8169 BIT(MD_CHANGE_PENDING) | BIT(MD_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008170
NeilBrown23da4222014-12-15 12:57:01 +11008171 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008172 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8173 /* We completed so min/max setting can be forgotten if used. */
8174 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8175 mddev->resync_min = 0;
8176 mddev->resync_max = MaxSector;
8177 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8178 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008179 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008180 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008181 spin_unlock(&mddev->lock);
8182
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008185 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186}
NeilBrown29269552006-03-27 01:18:10 -08008187EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188
NeilBrown746d3202013-04-24 11:42:41 +10008189static int remove_and_add_spares(struct mddev *mddev,
8190 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008191{
NeilBrown3cb03002011-10-11 16:45:26 +11008192 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008193 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008194 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008195 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008196
NeilBrownd787be42016-06-02 16:19:53 +10008197 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008198 if ((this == NULL || rdev == this) &&
8199 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008200 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008201 test_bit(Faulty, &rdev->flags) &&
8202 atomic_read(&rdev->nr_pending)==0) {
8203 /* Faulty non-Blocked devices with nr_pending == 0
8204 * never get nr_pending incremented,
8205 * never get Faulty cleared, and never get Blocked set.
8206 * So we can synchronize_rcu now rather than once per device
8207 */
8208 remove_some = true;
8209 set_bit(RemoveSynchronized, &rdev->flags);
8210 }
8211 }
8212
8213 if (remove_some)
8214 synchronize_rcu();
8215 rdev_for_each(rdev, mddev) {
8216 if ((this == NULL || rdev == this) &&
8217 rdev->raid_disk >= 0 &&
8218 !test_bit(Blocked, &rdev->flags) &&
8219 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008220 (!test_bit(In_sync, &rdev->flags) &&
8221 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008222 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008223 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008224 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008225 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008226 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008227 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008228 }
8229 }
NeilBrownd787be42016-06-02 16:19:53 +10008230 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8231 clear_bit(RemoveSynchronized, &rdev->flags);
8232 }
8233
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008234 if (removed && mddev->kobj.sd)
8235 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008236
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008237 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008238 goto no_add;
8239
NeilBrowndafb20f2012-03-19 12:46:39 +11008240 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008241 if (this && this != rdev)
8242 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008243 if (test_bit(Candidate, &rdev->flags))
8244 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008245 if (rdev->raid_disk >= 0 &&
8246 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008247 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008248 !test_bit(Faulty, &rdev->flags))
8249 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008250 if (rdev->raid_disk >= 0)
8251 continue;
8252 if (test_bit(Faulty, &rdev->flags))
8253 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008254 if (!test_bit(Journal, &rdev->flags)) {
8255 if (mddev->ro &&
8256 ! (rdev->saved_raid_disk >= 0 &&
8257 !test_bit(Bitmap_sync, &rdev->flags)))
8258 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008259
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008260 rdev->recovery_offset = 0;
8261 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008262 if (mddev->pers->
8263 hot_add_disk(mddev, rdev) == 0) {
8264 if (sysfs_link_rdev(mddev, rdev))
8265 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008266 if (!test_bit(Journal, &rdev->flags))
8267 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008268 md_new_event(mddev);
8269 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008270 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008271 }
NeilBrown746d3202013-04-24 11:42:41 +10008272no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008273 if (removed)
8274 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008275 return spares;
8276}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008277
NeilBrownac05f252014-09-30 08:10:42 +10008278static void md_start_sync(struct work_struct *ws)
8279{
8280 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008281 int ret = 0;
8282
NeilBrownac05f252014-09-30 08:10:42 +10008283 mddev->sync_thread = md_register_thread(md_do_sync,
8284 mddev,
8285 "resync");
8286 if (!mddev->sync_thread) {
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008287 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8288 printk(KERN_ERR "%s: could not start resync"
8289 " thread...\n",
8290 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008291 /* leave the spares where they are, it shouldn't hurt */
8292 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8293 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8294 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8295 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8296 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008297 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008298 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8299 &mddev->recovery))
8300 if (mddev->sysfs_action)
8301 sysfs_notify_dirent_safe(mddev->sysfs_action);
8302 } else
8303 md_wakeup_thread(mddev->sync_thread);
8304 sysfs_notify_dirent_safe(mddev->sysfs_action);
8305 md_new_event(mddev);
8306}
8307
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308/*
8309 * This routine is regularly called by all per-raid-array threads to
8310 * deal with generic issues like resync and super-block update.
8311 * Raid personalities that don't have a thread (linear/raid0) do not
8312 * need this as they never do any recovery or update the superblock.
8313 *
8314 * It does not do any resync itself, but rather "forks" off other threads
8315 * to do that as needed.
8316 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8317 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008318 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 * and wakeups up this thread which will reap the thread and finish up.
8320 * This thread also removes any faulty devices (with nr_pending == 0).
8321 *
8322 * The overall approach is:
8323 * 1/ if the superblock needs updating, update it.
8324 * 2/ If a recovery thread is running, don't do anything else.
8325 * 3/ If recovery has finished, clean up, possibly marking spares active.
8326 * 4/ If there are any faulty devices, remove them.
8327 * 5/ If array is degraded, try to add spares devices
8328 * 6/ If array has spares or is not in-sync, start a resync thread.
8329 */
NeilBrownfd01b882011-10-11 16:47:53 +11008330void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008332 if (mddev->suspended)
8333 return;
8334
NeilBrown5f404022005-06-21 17:17:16 -07008335 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008336 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337
NeilBrownfca4d842005-06-21 17:17:11 -07008338 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008339 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008340 printk(KERN_INFO "md: %s in immediate safe mode\n",
8341 mdname(mddev));
8342 mddev->safemode = 2;
8343 }
8344 flush_signals(current);
8345 }
8346
NeilBrownc89a8ee2008-08-05 15:54:13 +10008347 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8348 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008350 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008352 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008353 test_bit(MD_RELOAD_SB, &mddev->flags) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008354 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008355 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8356 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357 ))
8358 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008359
NeilBrowndf5b89b2006-03-27 01:18:20 -08008360 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008361 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008362
NeilBrownc89a8ee2008-08-05 15:54:13 +10008363 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008364 struct md_rdev *rdev;
8365 if (!mddev->external && mddev->in_sync)
8366 /* 'Blocked' flag not needed as failed devices
8367 * will be recorded if array switched to read/write.
8368 * Leaving it set will prevent the device
8369 * from being removed.
8370 */
8371 rdev_for_each(rdev, mddev)
8372 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008373 /* On a read-only array we can:
8374 * - remove failed devices
8375 * - add already-in_sync devices if the array itself
8376 * is in-sync.
8377 * As we only add devices that are already in-sync,
8378 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008379 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008380 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008381 /* There is no thread, but we need to call
8382 * ->spare_active and clear saved_raid_disk
8383 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008384 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008385 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008386 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008387 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008388 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008389 goto unlock;
8390 }
8391
Guoqing Jiang659b2542015-12-21 10:50:59 +11008392 if (mddev_is_clustered(mddev)) {
8393 struct md_rdev *rdev;
8394 /* kick the device if another node issued a
8395 * remove disk.
8396 */
8397 rdev_for_each(rdev, mddev) {
8398 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8399 rdev->raid_disk < 0)
8400 md_kick_rdev_from_array(rdev);
8401 }
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008402
8403 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8404 md_reload_sb(mddev, mddev->good_device_nr);
Guoqing Jiang659b2542015-12-21 10:50:59 +11008405 }
8406
NeilBrown31a59e32008-04-30 00:52:30 -07008407 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008408 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008409 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008410 if (mddev->safemode &&
8411 !atomic_read(&mddev->writes_pending) &&
8412 !mddev->in_sync &&
8413 mddev->recovery_cp == MaxSector) {
8414 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008415 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008416 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008417 }
8418 if (mddev->safemode == 1)
8419 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008420 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008421 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008422 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008423 }
NeilBrownfca4d842005-06-21 17:17:11 -07008424
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008425 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07008426 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008427
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8429 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8430 /* resync/recovery still happening */
8431 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8432 goto unlock;
8433 }
8434 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008435 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436 goto unlock;
8437 }
Neil Brown72a23c22008-06-28 08:31:41 +10008438 /* Set RUNNING before clearing NEEDED to avoid
8439 * any transients in the value of "sync_action".
8440 */
NeilBrown72f36d52012-10-11 14:25:57 +11008441 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008442 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008443 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008444 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008445 /* Clear some bits that don't mean anything, but
8446 * might be left set
8447 */
NeilBrown24dd4692005-11-08 21:39:26 -08008448 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8449 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008450
NeilBrowned209582012-04-24 10:23:14 +10008451 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8452 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008453 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454 /* no recovery is running.
8455 * remove any failed drives, then
8456 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008457 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458 * the personality to fail the re-add.
8459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008461 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008462 if (mddev->pers->check_reshape == NULL ||
8463 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008464 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008465 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008466 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008467 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008468 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008469 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8470 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008471 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008472 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008473 } else if (mddev->recovery_cp < MaxSector) {
8474 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008475 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008476 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8477 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008478 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008479
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008481 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008482 /* We are adding a device or devices to an array
8483 * which has the bitmap stored on all devices.
8484 * So make sure all bitmap pages get written
8485 */
8486 bitmap_write_all(mddev->bitmap);
8487 }
NeilBrownac05f252014-09-30 08:10:42 +10008488 INIT_WORK(&mddev->del_work, md_start_sync);
8489 queue_work(md_misc_wq, &mddev->del_work);
8490 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491 }
NeilBrownac05f252014-09-30 08:10:42 +10008492 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008493 if (!mddev->sync_thread) {
8494 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008495 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008496 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8497 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008498 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008499 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008500 }
NeilBrownac05f252014-09-30 08:10:42 +10008501 unlock:
8502 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 mddev_unlock(mddev);
8504 }
8505}
NeilBrown6c144d32014-09-30 16:15:38 +10008506EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008508void md_reap_sync_thread(struct mddev *mddev)
8509{
8510 struct md_rdev *rdev;
8511
8512 /* resync has finished, collect result */
8513 md_unregister_thread(&mddev->sync_thread);
8514 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8515 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8516 /* success...*/
8517 /* activate any spares */
8518 if (mddev->pers->spare_active(mddev)) {
8519 sysfs_notify(&mddev->kobj, NULL,
8520 "degraded");
8521 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8522 }
8523 }
8524 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8525 mddev->pers->finish_reshape)
8526 mddev->pers->finish_reshape(mddev);
8527
8528 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008529 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008530 */
NeilBrownf4667222013-12-09 12:04:56 +11008531 if (!mddev->degraded)
8532 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008533 rdev->saved_raid_disk = -1;
8534
8535 md_update_sb(mddev, 1);
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008536 /* MD_CHANGE_PENDING should be cleared by md_update_sb, so we can
8537 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
8538 * clustered raid */
8539 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
8540 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008541 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008542 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008543 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8544 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8545 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8546 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008547 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008548 /* flag recovery needed just to double check */
8549 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8550 sysfs_notify_dirent_safe(mddev->sysfs_action);
8551 md_new_event(mddev);
8552 if (mddev->event_work.func)
8553 queue_work(md_misc_wq, &mddev->event_work);
8554}
NeilBrown6c144d32014-09-30 16:15:38 +10008555EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008556
NeilBrownfd01b882011-10-11 16:47:53 +11008557void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008558{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008559 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008560 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008561 !test_bit(Blocked, &rdev->flags) &&
8562 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008563 msecs_to_jiffies(5000));
8564 rdev_dec_pending(rdev, mddev);
8565}
8566EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8567
NeilBrownc6563a82012-05-21 09:27:00 +10008568void md_finish_reshape(struct mddev *mddev)
8569{
8570 /* called be personality module when reshape completes. */
8571 struct md_rdev *rdev;
8572
8573 rdev_for_each(rdev, mddev) {
8574 if (rdev->data_offset > rdev->new_data_offset)
8575 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8576 else
8577 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8578 rdev->data_offset = rdev->new_data_offset;
8579 }
8580}
8581EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008582
Vishal Vermafc974ee2015-12-24 19:20:34 -07008583/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10008584
Vishal Vermafc974ee2015-12-24 19:20:34 -07008585/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11008586int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008587 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008588{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008589 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10008590 int rv;
8591 if (is_new)
8592 s += rdev->new_data_offset;
8593 else
8594 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008595 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
8596 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008597 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008598 sysfs_notify_dirent_safe(rdev->sysfs_state);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04008599 set_mask_bits(&mddev->flags, 0,
8600 BIT(MD_CHANGE_CLEAN) | BIT(MD_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10008601 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07008602 return 1;
8603 } else
8604 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008605}
8606EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8607
NeilBrownc6563a82012-05-21 09:27:00 +10008608int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8609 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008610{
NeilBrownc6563a82012-05-21 09:27:00 +10008611 if (is_new)
8612 s += rdev->new_data_offset;
8613 else
8614 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07008615 return badblocks_clear(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008616 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008617}
8618EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8619
Adrian Bunk75c96f82005-05-05 16:16:09 -07008620static int md_notify_reboot(struct notifier_block *this,
8621 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622{
8623 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008624 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008625 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008626
NeilBrownc744a652012-03-19 12:46:37 +11008627 for_each_mddev(mddev, tmp) {
8628 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008629 if (mddev->pers)
8630 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008631 if (mddev->persistent)
8632 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008633 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008634 }
NeilBrownc744a652012-03-19 12:46:37 +11008635 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636 }
NeilBrownc744a652012-03-19 12:46:37 +11008637 /*
8638 * certain more exotic SCSI devices are known to be
8639 * volatile wrt too early system reboots. While the
8640 * right place to handle this issue is the given
8641 * driver, we do want to have a safe RAID driver ...
8642 */
8643 if (need_delay)
8644 mdelay(1000*1);
8645
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 return NOTIFY_DONE;
8647}
8648
Adrian Bunk75c96f82005-05-05 16:16:09 -07008649static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008650 .notifier_call = md_notify_reboot,
8651 .next = NULL,
8652 .priority = INT_MAX, /* before any real devices */
8653};
8654
8655static void md_geninit(void)
8656{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008657 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008659 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660}
8661
Adrian Bunk75c96f82005-05-05 16:16:09 -07008662static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663{
Tejun Heoe804ac72010-10-15 15:36:08 +02008664 int ret = -ENOMEM;
8665
Tejun Heoada609e2011-01-25 14:35:54 +01008666 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008667 if (!md_wq)
8668 goto err_wq;
8669
8670 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8671 if (!md_misc_wq)
8672 goto err_misc_wq;
8673
8674 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8675 goto err_md;
8676
8677 if ((ret = register_blkdev(0, "mdp")) < 0)
8678 goto err_mdp;
8679 mdp_major = ret;
8680
NeilBrownaf5628f2014-07-31 13:54:54 +10008681 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008682 md_probe, NULL, NULL);
8683 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008684 md_probe, NULL, NULL);
8685
Linus Torvalds1da177e2005-04-16 15:20:36 -07008686 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008687 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688
8689 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691
Tejun Heoe804ac72010-10-15 15:36:08 +02008692err_mdp:
8693 unregister_blkdev(MD_MAJOR, "md");
8694err_md:
8695 destroy_workqueue(md_misc_wq);
8696err_misc_wq:
8697 destroy_workqueue(md_wq);
8698err_wq:
8699 return ret;
8700}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008701
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008702static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008703{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008704 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8705 struct md_rdev *rdev2;
8706 int role, ret;
8707 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008708
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008709 /* Check for change of roles in the active devices */
8710 rdev_for_each(rdev2, mddev) {
8711 if (test_bit(Faulty, &rdev2->flags))
8712 continue;
8713
8714 /* Check if the roles changed */
8715 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008716
8717 if (test_bit(Candidate, &rdev2->flags)) {
8718 if (role == 0xfffe) {
8719 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
8720 md_kick_rdev_from_array(rdev2);
8721 continue;
8722 }
8723 else
8724 clear_bit(Candidate, &rdev2->flags);
8725 }
8726
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008727 if (role != rdev2->raid_disk) {
8728 /* got activated */
8729 if (rdev2->raid_disk == -1 && role != 0xffff) {
8730 rdev2->saved_raid_disk = role;
8731 ret = remove_and_add_spares(mddev, rdev2);
8732 pr_info("Activated spare: %s\n",
8733 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04008734 /* wakeup mddev->thread here, so array could
8735 * perform resync with the new activated disk */
8736 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8737 md_wakeup_thread(mddev->thread);
8738
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008739 }
8740 /* device faulty
8741 * We just want to do the minimum to mark the disk
8742 * as faulty. The recovery is performed by the
8743 * one who initiated the error.
8744 */
8745 if ((role == 0xfffe) || (role == 0xfffd)) {
8746 md_error(mddev, rdev2);
8747 clear_bit(Blocked, &rdev2->flags);
8748 }
8749 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008750 }
8751
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11008752 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
8753 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05008754
8755 /* Finally set the event to be up to date */
8756 mddev->events = le64_to_cpu(sb->events);
8757}
8758
8759static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
8760{
8761 int err;
8762 struct page *swapout = rdev->sb_page;
8763 struct mdp_superblock_1 *sb;
8764
8765 /* Store the sb page of the rdev in the swapout temporary
8766 * variable in case we err in the future
8767 */
8768 rdev->sb_page = NULL;
8769 alloc_disk_sb(rdev);
8770 ClearPageUptodate(rdev->sb_page);
8771 rdev->sb_loaded = 0;
8772 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
8773
8774 if (err < 0) {
8775 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
8776 __func__, __LINE__, rdev->desc_nr, err);
8777 put_page(rdev->sb_page);
8778 rdev->sb_page = swapout;
8779 rdev->sb_loaded = 1;
8780 return err;
8781 }
8782
8783 sb = page_address(rdev->sb_page);
8784 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
8785 * is not set
8786 */
8787
8788 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
8789 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
8790
8791 /* The other node finished recovery, call spare_active to set
8792 * device In_sync and mddev->degraded
8793 */
8794 if (rdev->recovery_offset == MaxSector &&
8795 !test_bit(In_sync, &rdev->flags) &&
8796 mddev->pers->spare_active(mddev))
8797 sysfs_notify(&mddev->kobj, NULL, "degraded");
8798
8799 put_page(swapout);
8800 return 0;
8801}
8802
8803void md_reload_sb(struct mddev *mddev, int nr)
8804{
8805 struct md_rdev *rdev;
8806 int err;
8807
8808 /* Find the rdev */
8809 rdev_for_each_rcu(rdev, mddev) {
8810 if (rdev->desc_nr == nr)
8811 break;
8812 }
8813
8814 if (!rdev || rdev->desc_nr != nr) {
8815 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
8816 return;
8817 }
8818
8819 err = read_rdev(mddev, rdev);
8820 if (err < 0)
8821 return;
8822
8823 check_sb_changes(mddev, rdev);
8824
8825 /* Read all rdev's to update recovery_offset */
8826 rdev_for_each_rcu(rdev, mddev)
8827 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008828}
8829EXPORT_SYMBOL(md_reload_sb);
8830
Linus Torvalds1da177e2005-04-16 15:20:36 -07008831#ifndef MODULE
8832
8833/*
8834 * Searches all registered partitions for autorun RAID arrays
8835 * at boot time.
8836 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008837
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008838static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008839static LIST_HEAD(all_detected_devices);
8840struct detected_devices_node {
8841 struct list_head list;
8842 dev_t dev;
8843};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844
8845void md_autodetect_dev(dev_t dev)
8846{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008847 struct detected_devices_node *node_detected_dev;
8848
8849 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8850 if (node_detected_dev) {
8851 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008852 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008853 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008854 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008855 } else {
8856 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8857 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859}
8860
Linus Torvalds1da177e2005-04-16 15:20:36 -07008861static void autostart_arrays(int part)
8862{
NeilBrown3cb03002011-10-11 16:45:26 +11008863 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008864 struct detected_devices_node *node_detected_dev;
8865 dev_t dev;
8866 int i_scanned, i_passed;
8867
8868 i_scanned = 0;
8869 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870
8871 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8872
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008873 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008874 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8875 i_scanned++;
8876 node_detected_dev = list_entry(all_detected_devices.next,
8877 struct detected_devices_node, list);
8878 list_del(&node_detected_dev->list);
8879 dev = node_detected_dev->dev;
8880 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008881 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882 if (IS_ERR(rdev))
8883 continue;
8884
NeilBrown403df472014-09-30 15:52:29 +10008885 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008886 continue;
NeilBrown403df472014-09-30 15:52:29 +10008887
NeilBrownd0fae182008-03-04 14:29:31 -08008888 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008890 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07008892 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008893
8894 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8895 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896
8897 autorun_devices(part);
8898}
8899
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008900#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901
8902static __exit void md_exit(void)
8903{
NeilBrownfd01b882011-10-11 16:47:53 +11008904 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008905 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008906 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008907
NeilBrownaf5628f2014-07-31 13:54:54 +10008908 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008909 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008911 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008912 unregister_blkdev(mdp_major, "mdp");
8913 unregister_reboot_notifier(&md_notifier);
8914 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008915
8916 /* We cannot unload the modules while some process is
8917 * waiting for us in select() or poll() - wake them up
8918 */
8919 md_unloading = 1;
8920 while (waitqueue_active(&md_event_waiters)) {
8921 /* not safe to leave yet */
8922 wake_up(&md_event_waiters);
8923 msleep(delay);
8924 delay += delay;
8925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008927
NeilBrown29ac4aa2008-02-06 01:39:58 -08008928 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008930 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008932 destroy_workqueue(md_misc_wq);
8933 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934}
8935
Dan Williams685784a2007-07-09 11:56:42 -07008936subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937module_exit(md_exit)
8938
NeilBrownf91de922005-11-08 21:39:36 -08008939static int get_ro(char *buffer, struct kernel_param *kp)
8940{
8941 return sprintf(buffer, "%d", start_readonly);
8942}
8943static int set_ro(const char *val, struct kernel_param *kp)
8944{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03008945 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08008946}
8947
NeilBrown80ca3a42006-07-10 04:44:18 -07008948module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8949module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008950module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008951
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008953MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008954MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008955MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);