blob: c0c3e6dec2484e0a15736a7c494e9fd95bbe310a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050056#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110059static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
NeilBrown01f96c02011-09-21 15:30:20 +100062/* pers_list is a list of registered personalities protected
63 * by pers_lock.
64 * pers_lock does extra service to protect accesses to
65 * mddev->thread when the mutex cannot be held.
66 */
NeilBrown2604b702006-01-06 00:20:36 -080067static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_SPINLOCK(pers_lock);
69
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050070struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050071EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050072struct module *md_cluster_mod;
73EXPORT_SYMBOL(md_cluster_mod);
74
Bernd Schubert90b08712008-05-23 13:04:38 -070075static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020076static struct workqueue_struct *md_wq;
77static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070078
NeilBrown746d3202013-04-24 11:42:41 +100079static int remove_and_add_spares(struct mddev *mddev,
80 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110081static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Robert Becker1e509152009-12-14 12:49:58 +110084 * Default number of read corrections we'll attempt on an rdev
85 * before ejecting it from the array. We divide the read error
86 * count by 2 for every hour elapsed between read errors.
87 */
88#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
89/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
91 * is 1000 KB/sec, so the extra system load does not show up that much.
92 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070093 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * subsystem is idle. There is also an 'absolute maximum' reconstruction
95 * speed limit - in case reconstruction slows down your system despite
96 * idle IO detection.
97 *
98 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080099 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101
102static int sysctl_speed_limit_min = 1000;
103static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_min ?
107 mddev->sync_speed_min : sysctl_speed_limit_min;
108}
109
NeilBrownfd01b882011-10-11 16:47:53 +1100110static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800111{
112 return mddev->sync_speed_max ?
113 mddev->sync_speed_max : sysctl_speed_limit_max;
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct ctl_table_header *raid_table_header;
117
Joe Perches82592c32013-11-14 15:16:18 +1100118static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .procname = "speed_limit_min",
121 .data = &sysctl_speed_limit_min,
122 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700123 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800124 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 },
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .procname = "speed_limit_max",
128 .data = &sysctl_speed_limit_max,
129 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700130 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800131 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800133 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Joe Perches82592c32013-11-14 15:16:18 +1100136static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .procname = "raid",
139 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .child = raid_table,
142 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800143 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Joe Perches82592c32013-11-14 15:16:18 +1100146static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .procname = "dev",
149 .maxlen = 0,
150 .mode = 0555,
151 .child = raid_dir_table,
152 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800153 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700156static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
NeilBrownf91de922005-11-08 21:39:36 -0800158static int start_readonly;
159
NeilBrowna167f662010-10-26 18:31:13 +1100160/* bio_clone_mddev
161 * like bio_clone, but with a local bio set
162 */
163
NeilBrowna167f662010-10-26 18:31:13 +1100164struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100165 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100166{
167 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100168
169 if (!mddev || !mddev->bio_set)
170 return bio_alloc(gfp_mask, nr_iovecs);
171
Kent Overstreet395c72a2012-09-06 15:34:55 -0700172 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100173 if (!b)
174 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100175 return b;
176}
177EXPORT_SYMBOL_GPL(bio_alloc_mddev);
178
179struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100180 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100181{
NeilBrowna167f662010-10-26 18:31:13 +1100182 if (!mddev || !mddev->bio_set)
183 return bio_clone(bio, gfp_mask);
184
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700185 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100186}
187EXPORT_SYMBOL_GPL(bio_clone_mddev);
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
NeilBrownd7603b72006-01-06 00:20:30 -0800190 * We have a system wide 'event count' that is incremented
191 * on any 'interesting' event, and readers of /proc/mdstat
192 * can use 'poll' or 'select' to find out when the event
193 * count increases.
194 *
195 * Events are:
196 * start array, stop array, error, add device, remove device,
197 * start build, activate spare
198 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800199static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800200static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100201void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800202{
203 atomic_inc(&md_event_count);
204 wake_up(&md_event_waiters);
205}
NeilBrown29269552006-03-27 01:18:10 -0800206EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800207
NeilBrownc331eb02006-05-30 21:27:13 -0700208/* Alternate version that can be called from interrupts
209 * when calling sysfs_notify isn't needed.
210 */
NeilBrownfd01b882011-10-11 16:47:53 +1100211static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700212{
213 atomic_inc(&md_event_count);
214 wake_up(&md_event_waiters);
215}
216
NeilBrownd7603b72006-01-06 00:20:30 -0800217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Enables to iterate over all existing md arrays
219 * all_mddevs_lock protects this list.
220 */
221static LIST_HEAD(all_mddevs);
222static DEFINE_SPINLOCK(all_mddevs_lock);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
225 * iterates through all used mddevs in the system.
226 * We take care to grab the all_mddevs_lock whenever navigating
227 * the list, and to always hold a refcount when unlocked.
228 * Any code which breaks out of this loop while own
229 * a reference to the current mddev and must mddev_put it.
230 */
NeilBrownfd01b882011-10-11 16:47:53 +1100231#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000233 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100234 _tmp = all_mddevs.next; \
235 _mddev = NULL;}); \
236 ({ if (_tmp != &all_mddevs) \
237 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100239 if (_mddev) mddev_put(_mddev); \
240 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
241 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100243 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 )
245
NeilBrown409c57f2009-03-31 14:39:39 +1100246/* Rather than calling directly into the personality make_request function,
247 * IO requests come here first so that we can check if the device is
248 * being suspended pending a reconfiguration.
249 * We hold a refcount over the call to ->make_request. By the time that
250 * call has finished, the bio has been linked into some internal structure
251 * and so is visible to ->quiesce(), so we don't need the refcount any more.
252 */
Jens Axboedece1632015-11-05 10:41:16 -0700253static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
NeilBrown49077322010-03-25 16:20:56 +1100255 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100256 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500257 unsigned int sectors;
Gu Zheng74672d02015-04-03 08:44:47 +0800258 int cpu;
NeilBrown49077322010-03-25 16:20:56 +1100259
Kent Overstreet54efd502015-04-23 22:37:18 -0700260 blk_queue_split(q, &bio, q->bio_split);
261
NeilBrown0ca69882011-01-14 09:14:33 +1100262 if (mddev == NULL || mddev->pers == NULL
263 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100264 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700265 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100266 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100267 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200268 if (bio_sectors(bio) != 0)
269 bio->bi_error = -EROFS;
270 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700271 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100272 }
NeilBrown0ca69882011-01-14 09:14:33 +1100273 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100274 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200275 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100276 DEFINE_WAIT(__wait);
277 for (;;) {
278 prepare_to_wait(&mddev->sb_wait, &__wait,
279 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200280 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100281 break;
282 rcu_read_unlock();
283 schedule();
284 rcu_read_lock();
285 }
286 finish_wait(&mddev->sb_wait, &__wait);
287 }
288 atomic_inc(&mddev->active_io);
289 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100290
Chris Masone91ece52011-02-07 19:21:48 -0500291 /*
292 * save the sectors now since our bio can
293 * go away inside make_request
294 */
295 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200296 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100297
Gu Zheng74672d02015-04-03 08:44:47 +0800298 cpu = part_stat_lock();
299 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
300 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
301 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100302
NeilBrown409c57f2009-03-31 14:39:39 +1100303 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
304 wake_up(&mddev->sb_wait);
Jens Axboedece1632015-11-05 10:41:16 -0700305
306 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
NeilBrown9e35b992010-04-06 14:23:02 +1000309/* mddev_suspend makes sure no new requests are submitted
310 * to the device, and that any requests that have been submitted
311 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100312 * Once mddev_detach() is called and completes, the module will be
313 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000314 */
NeilBrownfd01b882011-10-11 16:47:53 +1100315void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100316{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100317 if (mddev->suspended++)
318 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100319 synchronize_rcu();
320 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
321 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500322
323 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100324}
NeilBrown390ee602010-06-01 19:37:27 +1000325EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100326
NeilBrownfd01b882011-10-11 16:47:53 +1100327void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100328{
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100329 if (--mddev->suspended)
330 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100331 wake_up(&mddev->sb_wait);
332 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500333
Jonathan Brassow47525e52012-05-22 13:55:29 +1000334 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500335 md_wakeup_thread(mddev->thread);
336 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100337}
NeilBrown390ee602010-06-01 19:37:27 +1000338EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100339
NeilBrownfd01b882011-10-11 16:47:53 +1100340int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000341{
NeilBrown5c675f82014-12-15 12:56:56 +1100342 struct md_personality *pers = mddev->pers;
343 int ret = 0;
344
345 rcu_read_lock();
346 if (mddev->suspended)
347 ret = 1;
348 else if (pers && pers->congested)
349 ret = pers->congested(mddev, bits);
350 rcu_read_unlock();
351 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000352}
NeilBrown5c675f82014-12-15 12:56:56 +1100353EXPORT_SYMBOL_GPL(mddev_congested);
354static int md_congested(void *data, int bits)
355{
356 struct mddev *mddev = data;
357 return mddev_congested(mddev, bits);
358}
NeilBrown3fa841d2009-09-23 18:10:29 +1000359
NeilBrowna2826aa2009-12-14 12:49:49 +1100360/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200361 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100362 */
363
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200364static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100365{
NeilBrown3cb03002011-10-11 16:45:26 +1100366 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100367 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100368
369 rdev_dec_pending(rdev, mddev);
370
371 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200372 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200373 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100374 }
375 bio_put(bio);
376}
377
NeilBrowna7a07e62010-12-09 16:04:25 +1100378static void md_submit_flush_data(struct work_struct *ws);
379
NeilBrowna035fc32010-12-09 16:17:51 +1100380static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100381{
NeilBrownfd01b882011-10-11 16:47:53 +1100382 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100383 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100384
NeilBrowna7a07e62010-12-09 16:04:25 +1100385 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
386 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100387 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100388 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100389 if (rdev->raid_disk >= 0 &&
390 !test_bit(Faulty, &rdev->flags)) {
391 /* Take two references, one is dropped
392 * when request finishes, one after
393 * we reclaim rcu_read_lock
394 */
395 struct bio *bi;
396 atomic_inc(&rdev->nr_pending);
397 atomic_inc(&rdev->nr_pending);
398 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000399 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200400 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100401 bi->bi_private = rdev;
402 bi->bi_bdev = rdev->bdev;
403 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200404 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100405 rcu_read_lock();
406 rdev_dec_pending(rdev, mddev);
407 }
408 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100409 if (atomic_dec_and_test(&mddev->flush_pending))
410 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100411}
412
Tejun Heoe9c74692010-09-03 11:56:18 +0200413static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100414{
NeilBrownfd01b882011-10-11 16:47:53 +1100415 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200416 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100417
Kent Overstreet4f024f32013-10-11 15:44:27 -0700418 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100419 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200420 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100421 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200422 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200423 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100424 }
NeilBrown2b74e122010-12-09 15:59:01 +1100425
426 mddev->flush_bio = NULL;
427 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100428}
429
NeilBrownfd01b882011-10-11 16:47:53 +1100430void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100431{
NeilBrown85572d72014-12-15 12:56:56 +1100432 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100433 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200434 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100435 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200436 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100437 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100438
NeilBrowna035fc32010-12-09 16:17:51 +1100439 INIT_WORK(&mddev->flush_work, submit_flushes);
440 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100441}
Tejun Heoe9c74692010-09-03 11:56:18 +0200442EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100443
NeilBrown74018dc2012-07-31 09:08:15 +0200444void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000445{
NeilBrown9cbb1752012-07-31 09:08:14 +0200446 struct mddev *mddev = cb->data;
447 md_wakeup_thread(mddev->thread);
448 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000449}
NeilBrown9cbb1752012-07-31 09:08:14 +0200450EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000451
NeilBrownfd01b882011-10-11 16:47:53 +1100452static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 atomic_inc(&mddev->active);
455 return mddev;
456}
457
Dan Williams5fd3a172009-03-04 00:57:25 -0700458static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100459
NeilBrownfd01b882011-10-11 16:47:53 +1100460static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
NeilBrowna167f662010-10-26 18:31:13 +1100462 struct bio_set *bs = NULL;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
465 return;
NeilBrownd3374822009-01-09 08:31:10 +1100466 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100467 mddev->ctime == 0 && !mddev->hold_active) {
468 /* Array is not configured at all, and not held active,
469 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100470 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100471 bs = mddev->bio_set;
472 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100473 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200474 /* We did a probe so need to clean up. Call
475 * queue_work inside the spinlock so that
476 * flush_workqueue() after mddev_find will
477 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100478 */
479 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200480 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100481 } else
482 kfree(mddev);
483 }
484 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100485 if (bs)
486 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Sasha Levin25b2edf2015-07-24 18:19:58 -0400489static void md_safemode_timeout(unsigned long data);
490
NeilBrownfd01b882011-10-11 16:47:53 +1100491void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100492{
493 mutex_init(&mddev->open_mutex);
494 mutex_init(&mddev->reconfig_mutex);
495 mutex_init(&mddev->bitmap_info.mutex);
496 INIT_LIST_HEAD(&mddev->disks);
497 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400498 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
499 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100500 atomic_set(&mddev->active, 1);
501 atomic_set(&mddev->openers, 0);
502 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100503 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100504 atomic_set(&mddev->flush_pending, 0);
505 init_waitqueue_head(&mddev->sb_wait);
506 init_waitqueue_head(&mddev->recovery_wait);
507 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000508 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500509 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100510 mddev->resync_min = 0;
511 mddev->resync_max = MaxSector;
512 mddev->level = LEVEL_NONE;
513}
NeilBrown390ee602010-06-01 19:37:27 +1000514EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100515
NeilBrownf72ffdd2014-09-30 14:23:59 +1000516static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
NeilBrownfd01b882011-10-11 16:47:53 +1100518 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
NeilBrown8f5f02c2011-02-16 13:58:51 +1100520 if (unit && MAJOR(unit) != MD_MAJOR)
521 unit &= ~((1<<MdpMinorShift)-1);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 retry:
524 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
NeilBrownefeb53c2009-01-09 08:31:10 +1100526 if (unit) {
527 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
528 if (mddev->unit == unit) {
529 mddev_get(mddev);
530 spin_unlock(&all_mddevs_lock);
531 kfree(new);
532 return mddev;
533 }
534
535 if (new) {
536 list_add(&new->all_mddevs, &all_mddevs);
537 spin_unlock(&all_mddevs_lock);
538 new->hold_active = UNTIL_IOCTL;
539 return new;
540 }
541 } else if (new) {
542 /* find an unused unit number */
543 static int next_minor = 512;
544 int start = next_minor;
545 int is_free = 0;
546 int dev = 0;
547 while (!is_free) {
548 dev = MKDEV(MD_MAJOR, next_minor);
549 next_minor++;
550 if (next_minor > MINORMASK)
551 next_minor = 0;
552 if (next_minor == start) {
553 /* Oh dear, all in use. */
554 spin_unlock(&all_mddevs_lock);
555 kfree(new);
556 return NULL;
557 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000558
NeilBrownefeb53c2009-01-09 08:31:10 +1100559 is_free = 1;
560 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
561 if (mddev->unit == dev) {
562 is_free = 0;
563 break;
564 }
565 }
566 new->unit = dev;
567 new->md_minor = MINOR(dev);
568 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 list_add(&new->all_mddevs, &all_mddevs);
570 spin_unlock(&all_mddevs_lock);
571 return new;
572 }
573 spin_unlock(&all_mddevs_lock);
574
NeilBrown9ffae0c2006-01-06 00:20:32 -0800575 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (!new)
577 return NULL;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 new->unit = unit;
580 if (MAJOR(unit) == MD_MAJOR)
581 new->md_minor = MINOR(unit);
582 else
583 new->md_minor = MINOR(unit) >> MdpMinorShift;
584
NeilBrownfafd7fb2010-04-01 15:55:30 +1100585 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto retry;
588}
589
NeilBrownb6eb1272010-04-15 10:13:47 +1000590static struct attribute_group md_redundancy_group;
591
NeilBrown5c47daf2014-12-15 12:57:01 +1100592void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
NeilBrowna64c8762010-04-14 17:15:37 +1000594 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000595 /* These cannot be removed under reconfig_mutex as
596 * an access to the files will try to take reconfig_mutex
597 * while holding the file unremovable, which leads to
598 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000599 * So hold set sysfs_active while the remove in happeing,
600 * and anything else which might set ->to_remove or my
601 * otherwise change the sysfs namespace will fail with
602 * -EBUSY if sysfs_active is still set.
603 * We set sysfs_active under reconfig_mutex and elsewhere
604 * test it under the same mutex to ensure its correct value
605 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000606 */
NeilBrowna64c8762010-04-14 17:15:37 +1000607 struct attribute_group *to_remove = mddev->to_remove;
608 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000609 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000610 mutex_unlock(&mddev->reconfig_mutex);
611
NeilBrown00bcb4a2010-06-01 19:37:23 +1000612 if (mddev->kobj.sd) {
613 if (to_remove != &md_redundancy_group)
614 sysfs_remove_group(&mddev->kobj, to_remove);
615 if (mddev->pers == NULL ||
616 mddev->pers->sync_request == NULL) {
617 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
618 if (mddev->sysfs_action)
619 sysfs_put(mddev->sysfs_action);
620 mddev->sysfs_action = NULL;
621 }
NeilBrowna64c8762010-04-14 17:15:37 +1000622 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000623 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000624 } else
625 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Chris Dunlop751e67c2011-10-19 16:48:26 +1100627 /* As we've dropped the mutex we need a spinlock to
628 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000629 */
630 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700631 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000632 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
NeilBrown5c47daf2014-12-15 12:57:01 +1100634EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500636struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100637{
638 struct md_rdev *rdev;
639
640 rdev_for_each_rcu(rdev, mddev)
641 if (rdev->desc_nr == nr)
642 return rdev;
643
644 return NULL;
645}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500646EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100647
648static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
NeilBrown3cb03002011-10-11 16:45:26 +1100650 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
NeilBrowndafb20f2012-03-19 12:46:39 +1100652 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (rdev->bdev->bd_dev == dev)
654 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return NULL;
657}
658
NeilBrown1ca69c42012-10-11 13:37:33 +1100659static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
660{
661 struct md_rdev *rdev;
662
663 rdev_for_each_rcu(rdev, mddev)
664 if (rdev->bdev->bd_dev == dev)
665 return rdev;
666
667 return NULL;
668}
669
NeilBrown84fc4b52011-10-11 16:49:58 +1100670static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800671{
NeilBrown84fc4b52011-10-11 16:49:58 +1100672 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800673 list_for_each_entry(pers, &pers_list, list) {
674 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800675 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800676 if (strcmp(pers->name, clevel)==0)
677 return pers;
678 }
NeilBrown2604b702006-01-06 00:20:36 -0800679 return NULL;
680}
681
Andre Nollb73df2d2008-07-11 22:02:23 +1000682/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100683static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100685 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000686 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
NeilBrownf72ffdd2014-09-30 14:23:59 +1000689static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 rdev->sb_page = alloc_page(GFP_KERNEL);
692 if (!rdev->sb_page) {
693 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000694 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 return 0;
698}
699
NeilBrown545c8792012-05-22 13:54:30 +1000700void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800703 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rdev->sb_loaded = 0;
705 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000706 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100707 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
NeilBrown2699b672011-07-28 11:31:47 +1000709 if (rdev->bb_page) {
710 put_page(rdev->bb_page);
711 rdev->bb_page = NULL;
712 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000713 kfree(rdev->badblocks.page);
714 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
NeilBrown545c8792012-05-22 13:54:30 +1000716EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200718static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700719{
NeilBrown3cb03002011-10-11 16:45:26 +1100720 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100721 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700722
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200723 if (bio->bi_error) {
724 printk("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800725 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700726 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700727
NeilBrowna9701a32005-11-08 21:39:34 -0800728 if (atomic_dec_and_test(&mddev->pending_writes))
729 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700730 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700731}
732
NeilBrownfd01b882011-10-11 16:47:53 +1100733void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700734 sector_t sector, int size, struct page *page)
735{
736 /* write first size bytes of page to sector of rdev
737 * Increment mddev->pending_writes before returning
738 * and decrement it on completion, waking up sb_wait
739 * if zero is reached.
740 * If an error occurred, call md_error
741 */
NeilBrowna167f662010-10-26 18:31:13 +1100742 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100744 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700745 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746 bio_add_page(bio, page, size, 0);
747 bio->bi_private = rdev;
748 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800749
NeilBrown7bfa19f2005-06-21 17:17:28 -0700750 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000751 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800752}
753
NeilBrownfd01b882011-10-11 16:47:53 +1100754void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800755{
Tejun Heoe9c74692010-09-03 11:56:18 +0200756 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000757 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700758}
759
NeilBrown3cb03002011-10-11 16:45:26 +1100760int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100761 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
NeilBrowna167f662010-10-26 18:31:13 +1100763 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int ret;
765
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100766 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
767 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100768 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700769 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000770 else if (rdev->mddev->reshape_position != MaxSector &&
771 (rdev->mddev->reshape_backwards ==
772 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700773 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100774 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700775 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700777 submit_bio_wait(rw, 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
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100792 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 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
NeilBrown2699b672011-07-28 11:31:47 +10001366static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1367 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001368static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct mdp_superblock_1 *sb;
1371 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001372 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001373 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001375 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /*
Andre Noll0f420352008-07-11 22:02:23 +10001378 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 * It is always aligned to a 4K boundary and
1380 * depeding on minor_version, it can be:
1381 * 0: At least 8K, but less than 12K, from end of device
1382 * 1: At start of device
1383 * 2: 4K from start of device.
1384 */
1385 switch(minor_version) {
1386 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001387 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001388 sb_start -= 8*2;
1389 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
1391 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001392 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 break;
1394 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001395 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 break;
1397 default:
1398 return -EINVAL;
1399 }
Andre Noll0f420352008-07-11 22:02:23 +10001400 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
NeilBrown0002b272005-09-09 16:23:53 -07001402 /* superblock is rarely larger than 1K, but it can be larger,
1403 * and it is safe to read 4k, so we do that
1404 */
1405 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (ret) return ret;
1407
Namhyung Kim65a06f062011-07-27 11:00:36 +10001408 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1411 sb->major_version != cpu_to_le32(1) ||
1412 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001413 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001414 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return -EINVAL;
1416
1417 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1418 printk("md: invalid superblock checksum on %s\n",
1419 bdevname(rdev->bdev,b));
1420 return -EINVAL;
1421 }
1422 if (le64_to_cpu(sb->data_size) < 10) {
1423 printk("md: data_size too small on %s\n",
1424 bdevname(rdev->bdev,b));
1425 return -EINVAL;
1426 }
NeilBrownc6563a82012-05-21 09:27:00 +10001427 if (sb->pad0 ||
1428 sb->pad3[0] ||
1429 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1430 /* Some padding is non-zero, might be a new feature */
1431 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 rdev->preferred_minor = 0xffff;
1434 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001435 rdev->new_data_offset = rdev->data_offset;
1436 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1437 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1438 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001439 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
NeilBrown0002b272005-09-09 16:23:53 -07001441 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001442 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001443 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001444 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1445
1446 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001447 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001448 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001449 if (minor_version
1450 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1451 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001452
NeilBrown31b65a02006-07-10 04:44:14 -07001453 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1454 rdev->desc_nr = -1;
1455 else
1456 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1457
NeilBrown2699b672011-07-28 11:31:47 +10001458 if (!rdev->bb_page) {
1459 rdev->bb_page = alloc_page(GFP_KERNEL);
1460 if (!rdev->bb_page)
1461 return -ENOMEM;
1462 }
1463 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1464 rdev->badblocks.count == 0) {
1465 /* need to load the bad block list.
1466 * Currently we limit it to one page.
1467 */
1468 s32 offset;
1469 sector_t bb_sector;
1470 u64 *bbp;
1471 int i;
1472 int sectors = le16_to_cpu(sb->bblog_size);
1473 if (sectors > (PAGE_SIZE / 512))
1474 return -EINVAL;
1475 offset = le32_to_cpu(sb->bblog_offset);
1476 if (offset == 0)
1477 return -EINVAL;
1478 bb_sector = (long long)offset;
1479 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1480 rdev->bb_page, READ, true))
1481 return -EIO;
1482 bbp = (u64 *)page_address(rdev->bb_page);
1483 rdev->badblocks.shift = sb->bblog_shift;
1484 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1485 u64 bb = le64_to_cpu(*bbp);
1486 int count = bb & (0x3ff);
1487 u64 sector = bb >> 10;
1488 sector <<= sb->bblog_shift;
1489 count <<= sb->bblog_shift;
1490 if (bb + 1 == 0)
1491 break;
1492 if (md_set_badblocks(&rdev->badblocks,
1493 sector, count, 1) == 0)
1494 return -EINVAL;
1495 }
NeilBrown486adf72013-04-24 11:42:44 +10001496 } else if (sb->bblog_offset != 0)
1497 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001498
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001499 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001500 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001501 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001503 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1506 sb->level != refsb->level ||
1507 sb->layout != refsb->layout ||
1508 sb->chunksize != refsb->chunksize) {
1509 printk(KERN_WARNING "md: %s has strangely different"
1510 " superblock to %s\n",
1511 bdevname(rdev->bdev,b),
1512 bdevname(refdev->bdev,b2));
1513 return -EINVAL;
1514 }
1515 ev1 = le64_to_cpu(sb->events);
1516 ev2 = le64_to_cpu(refsb->events);
1517
1518 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001519 ret = 1;
1520 else
1521 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
NeilBrownc6563a82012-05-21 09:27:00 +10001523 if (minor_version) {
1524 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1525 sectors -= rdev->data_offset;
1526 } else
1527 sectors = rdev->sb_start;
1528 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001530 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001531 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
NeilBrownfd01b882011-10-11 16:47:53 +11001534static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001536 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001537 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
NeilBrown41158c72005-06-21 17:17:25 -07001539 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001540 clear_bit(Faulty, &rdev->flags);
1541 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001542 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001543 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (mddev->raid_disks == 0) {
1546 mddev->major_version = 1;
1547 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001548 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001549 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001550 mddev->ctime = le64_to_cpu(sb->ctime);
1551 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001553 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 mddev->layout = le32_to_cpu(sb->layout);
1555 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001556 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001557 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001558 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001559 mddev->bitmap_info.space = 0;
1560 /* Default location for bitmap is 1K after superblock
1561 * using 3K - total of 4K
1562 */
NeilBrownc3d97142009-12-14 12:49:52 +11001563 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001564 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001565 mddev->reshape_backwards = 0;
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1568 memcpy(mddev->uuid, sb->set_uuid, 16);
1569
1570 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001571
NeilBrown71c08052005-09-09 16:23:51 -07001572 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001573 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001574 mddev->bitmap_info.offset =
1575 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001576 /* Metadata doesn't record how much space is available.
1577 * For 1.0, we assume we can use up to the superblock
1578 * if before, else to 4K beyond superblock.
1579 * For others, assume no change is possible.
1580 */
1581 if (mddev->minor_version > 0)
1582 mddev->bitmap_info.space = 0;
1583 else if (mddev->bitmap_info.offset > 0)
1584 mddev->bitmap_info.space =
1585 8 - mddev->bitmap_info.offset;
1586 else
1587 mddev->bitmap_info.space =
1588 -mddev->bitmap_info.offset;
1589 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001590
NeilBrownf6705572006-03-27 01:18:11 -08001591 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1592 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1593 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1594 mddev->new_level = le32_to_cpu(sb->new_level);
1595 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001596 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001597 if (mddev->delta_disks < 0 ||
1598 (mddev->delta_disks == 0 &&
1599 (le32_to_cpu(sb->feature_map)
1600 & MD_FEATURE_RESHAPE_BACKWARDS)))
1601 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001602 } else {
1603 mddev->reshape_position = MaxSector;
1604 mddev->delta_disks = 0;
1605 mddev->new_level = mddev->level;
1606 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001607 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001608 }
1609
NeilBrown41158c72005-06-21 17:17:25 -07001610 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001611 /* Insist of good event counter while assembling, except for
1612 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001614 if (rdev->desc_nr >= 0 &&
1615 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001616 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1617 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001618 if (ev1 < mddev->events)
1619 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001620 } else if (mddev->bitmap) {
1621 /* If adding to array with a bitmap, then we can accept an
1622 * older device, but not too old.
1623 */
NeilBrown41158c72005-06-21 17:17:25 -07001624 if (ev1 < mddev->bitmap->events_cleared)
1625 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001626 if (ev1 < mddev->events)
1627 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001628 } else {
1629 if (ev1 < mddev->events)
1630 /* just a hot-add of a new device, leave raid_disk at -1 */
1631 return 0;
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (mddev->level != LEVEL_MULTIPATH) {
1634 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001635 if (rdev->desc_nr < 0 ||
1636 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001637 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001638 rdev->desc_nr = -1;
1639 } else
1640 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001642 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001644 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001645 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 break;
Song Liubac624f2015-08-13 14:31:55 -07001647 case MD_DISK_ROLE_JOURNAL: /* journal device */
1648 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1649 /* journal device without journal feature */
1650 printk(KERN_WARNING
1651 "md: journal device provided without journal feature, ignoring the device\n");
1652 return -EINVAL;
1653 }
1654 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001655 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Libd18f642015-09-02 13:49:50 -07001656 if (mddev->recovery_cp == MaxSector)
1657 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
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);
Song Liua97b7892015-10-08 21:54:09 -07001677 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1678 set_bit(MD_HAS_JOURNAL, &mddev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001679 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001680 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return 0;
1683}
1684
NeilBrownfd01b882011-10-11 16:47:53 +11001685static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001688 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 int max_dev, i;
1690 /* make rdev->sb match mddev and rdev data. */
1691
Namhyung Kim65a06f062011-07-27 11:00:36 +10001692 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 sb->feature_map = 0;
1695 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001696 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 memset(sb->pad3, 0, sizeof(sb->pad3));
1698
1699 sb->utime = cpu_to_le64((__u64)mddev->utime);
1700 sb->events = cpu_to_le64(mddev->events);
1701 if (mddev->in_sync)
1702 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001703 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1704 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 else
1706 sb->resync_offset = cpu_to_le64(0);
1707
NeilBrown1c05b4b2006-10-21 10:24:08 -07001708 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001709
NeilBrownf0ca3402006-02-02 14:28:04 -08001710 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001711 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001712 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001713 sb->level = cpu_to_le32(mddev->level);
1714 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001715
NeilBrownaeb9b2112011-08-25 14:43:08 +10001716 if (test_bit(WriteMostly, &rdev->flags))
1717 sb->devflags |= WriteMostly1;
1718 else
1719 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001720 sb->data_offset = cpu_to_le64(rdev->data_offset);
1721 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001722
NeilBrownc3d97142009-12-14 12:49:52 +11001723 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1724 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001725 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001726 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001727
Shaohua Lif2076e72015-10-08 21:54:12 -07001728 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001729 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001730 sb->feature_map |=
1731 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1732 sb->recovery_offset =
1733 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001734 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1735 sb->feature_map |=
1736 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001737 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001738 /* Note: recovery_offset and journal_tail share space */
1739 if (test_bit(Journal, &rdev->flags))
1740 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001741 if (test_bit(Replacement, &rdev->flags))
1742 sb->feature_map |=
1743 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001744
NeilBrownf6705572006-03-27 01:18:11 -08001745 if (mddev->reshape_position != MaxSector) {
1746 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1747 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1748 sb->new_layout = cpu_to_le32(mddev->new_layout);
1749 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1750 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001751 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001752 if (mddev->delta_disks == 0 &&
1753 mddev->reshape_backwards)
1754 sb->feature_map
1755 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001756 if (rdev->new_data_offset != rdev->data_offset) {
1757 sb->feature_map
1758 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1759 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1760 - rdev->data_offset));
1761 }
NeilBrownf6705572006-03-27 01:18:11 -08001762 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001763
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001764 if (mddev_is_clustered(mddev))
1765 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1766
NeilBrown2699b672011-07-28 11:31:47 +10001767 if (rdev->badblocks.count == 0)
1768 /* Nothing to do for bad blocks*/ ;
1769 else if (sb->bblog_offset == 0)
1770 /* Cannot record bad blocks on this device */
1771 md_error(mddev, rdev);
1772 else {
1773 struct badblocks *bb = &rdev->badblocks;
1774 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1775 u64 *p = bb->page;
1776 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1777 if (bb->changed) {
1778 unsigned seq;
1779
1780retry:
1781 seq = read_seqbegin(&bb->lock);
1782
1783 memset(bbp, 0xff, PAGE_SIZE);
1784
1785 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001786 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001787 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1788 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001789 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001790 }
NeilBrownd0962932012-03-19 12:46:41 +11001791 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001792 if (read_seqretry(&bb->lock, seq))
1793 goto retry;
1794
1795 bb->sector = (rdev->sb_start +
1796 (int)le32_to_cpu(sb->bblog_offset));
1797 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001798 }
1799 }
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001802 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (rdev2->desc_nr+1 > max_dev)
1804 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001805
NeilBrown70471da2009-08-03 10:59:57 +10001806 if (max_dev > le32_to_cpu(sb->max_dev)) {
1807 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001808 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001809 rdev->sb_size = max_dev * 2 + 256;
1810 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1811 if (rdev->sb_size & bmask)
1812 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001813 } else
1814 max_dev = le32_to_cpu(sb->max_dev);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001817 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001818
Song Liua97b7892015-10-08 21:54:09 -07001819 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1820 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
NeilBrowndafb20f2012-03-19 12:46:39 +11001822 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001824 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001825 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001826 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07001828 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07001829 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11001830 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001831 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 else
Song Liuc4d4c912015-08-13 14:31:54 -07001833 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 sb->sb_csum = calc_sb_1_csum(sb);
1837}
1838
Chris Webb0cd17fe2008-06-28 08:31:46 +10001839static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001840super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001841{
1842 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001843 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001844 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001845 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001846 if (rdev->data_offset != rdev->new_data_offset)
1847 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001848 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001849 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001850 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001851 max_sectors -= rdev->data_offset;
1852 if (!num_sectors || num_sectors > max_sectors)
1853 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001854 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001855 /* minor version 0 with bitmap we can't move */
1856 return 0;
1857 } else {
1858 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001859 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001860 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001861 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001862 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001863 if (!num_sectors || num_sectors > max_sectors)
1864 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001865 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001866 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001867 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001868 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001869 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001870 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001871 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001872 rdev->sb_page);
1873 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001874 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001875
1876}
1877
1878static int
1879super_1_allow_new_offset(struct md_rdev *rdev,
1880 unsigned long long new_offset)
1881{
1882 /* All necessary checks on new >= old have been done */
1883 struct bitmap *bitmap;
1884 if (new_offset >= rdev->data_offset)
1885 return 1;
1886
1887 /* with 1.0 metadata, there is no metadata to tread on
1888 * so we can always move back */
1889 if (rdev->mddev->minor_version == 0)
1890 return 1;
1891
1892 /* otherwise we must be sure not to step on
1893 * any metadata, so stay:
1894 * 36K beyond start of superblock
1895 * beyond end of badblocks
1896 * beyond write-intent bitmap
1897 */
1898 if (rdev->sb_start + (32+4)*2 > new_offset)
1899 return 0;
1900 bitmap = rdev->mddev->bitmap;
1901 if (bitmap && !rdev->mddev->bitmap_info.file &&
1902 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001903 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001904 return 0;
1905 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1906 return 0;
1907
1908 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001909}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Adrian Bunk75c96f82005-05-05 16:16:09 -07001911static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 [0] = {
1913 .name = "0.90.0",
1914 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001915 .load_super = super_90_load,
1916 .validate_super = super_90_validate,
1917 .sync_super = super_90_sync,
1918 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001919 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 },
1921 [1] = {
1922 .name = "md-1",
1923 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001924 .load_super = super_1_load,
1925 .validate_super = super_1_validate,
1926 .sync_super = super_1_sync,
1927 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001928 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 },
1930};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
NeilBrownfd01b882011-10-11 16:47:53 +11001932static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001933{
1934 if (mddev->sync_super) {
1935 mddev->sync_super(mddev, rdev);
1936 return;
1937 }
1938
1939 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1940
1941 super_types[mddev->major_version].sync_super(mddev, rdev);
1942}
1943
NeilBrownfd01b882011-10-11 16:47:53 +11001944static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
NeilBrown3cb03002011-10-11 16:45:26 +11001946 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
NeilBrown4b809912008-07-21 17:05:25 +10001948 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07001949 rdev_for_each_rcu(rdev, mddev1) {
1950 if (test_bit(Faulty, &rdev->flags) ||
1951 test_bit(Journal, &rdev->flags) ||
1952 rdev->raid_disk == -1)
1953 continue;
1954 rdev_for_each_rcu(rdev2, mddev2) {
1955 if (test_bit(Faulty, &rdev2->flags) ||
1956 test_bit(Journal, &rdev2->flags) ||
1957 rdev2->raid_disk == -1)
1958 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001959 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001960 rdev2->bdev->bd_contains) {
1961 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001962 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001963 }
Song Liu0b020e82015-09-03 23:00:35 -07001964 }
1965 }
NeilBrown4b809912008-07-21 17:05:25 +10001966 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return 0;
1968}
1969
1970static LIST_HEAD(pending_raid_disks);
1971
Andre Nollac5e7112009-08-03 10:59:47 +10001972/*
1973 * Try to register data integrity profile for an mddev
1974 *
1975 * This is called when an array is started and after a disk has been kicked
1976 * from the array. It only succeeds if all working and active component devices
1977 * are integrity capable with matching profiles.
1978 */
NeilBrownfd01b882011-10-11 16:47:53 +11001979int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001980{
NeilBrown3cb03002011-10-11 16:45:26 +11001981 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001982
Andre Nollac5e7112009-08-03 10:59:47 +10001983 if (list_empty(&mddev->disks))
1984 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001985 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1986 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001987 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001988 /* skip spares and non-functional disks */
1989 if (test_bit(Faulty, &rdev->flags))
1990 continue;
1991 if (rdev->raid_disk < 0)
1992 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001993 if (!reference) {
1994 /* Use the first rdev as the reference */
1995 reference = rdev;
1996 continue;
1997 }
1998 /* does this rdev's profile match the reference profile? */
1999 if (blk_integrity_compare(reference->bdev->bd_disk,
2000 rdev->bdev->bd_disk) < 0)
2001 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002002 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002003 if (!reference || !bdev_get_integrity(reference->bdev))
2004 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002005 /*
2006 * All component devices are integrity capable and have matching
2007 * profiles, register the common profile for the md device.
2008 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002009 blk_integrity_register(mddev->gendisk,
2010 bdev_get_integrity(reference->bdev));
2011
Martin K. Petersena91a2782011-03-17 11:11:05 +01002012 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2013 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2014 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2015 mdname(mddev));
2016 return -EINVAL;
2017 }
Andre Nollac5e7112009-08-03 10:59:47 +10002018 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002019}
Andre Nollac5e7112009-08-03 10:59:47 +10002020EXPORT_SYMBOL(md_integrity_register);
2021
2022/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002023void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002024{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002025 struct blk_integrity *bi_rdev;
2026 struct blk_integrity *bi_mddev;
2027
2028 if (!mddev->gendisk)
2029 return;
2030
2031 bi_rdev = bdev_get_integrity(rdev->bdev);
2032 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002033
2034 if (!bi_mddev) /* nothing to do */
2035 return;
2036 if (rdev->raid_disk < 0) /* skip spares */
2037 return;
2038 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2039 rdev->bdev->bd_disk) >= 0)
2040 return;
Dan Williamsc7bfced2015-10-21 13:20:02 -04002041 WARN_ON_ONCE(!mddev->suspended);
Andre Nollac5e7112009-08-03 10:59:47 +10002042 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2043 blk_integrity_unregister(mddev->gendisk);
2044}
2045EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002046
NeilBrownf72ffdd2014-09-30 14:23:59 +10002047static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002049 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002050 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002051 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Dan Williams11e2ede2008-04-30 00:52:32 -07002053 /* prevent duplicates */
2054 if (find_rdev(mddev, rdev->bdev->bd_dev))
2055 return -EEXIST;
2056
Andre Nolldd8ac332009-03-31 14:33:13 +11002057 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002058 if (!test_bit(Journal, &rdev->flags) &&
2059 rdev->sectors &&
2060 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002061 if (mddev->pers) {
2062 /* Cannot change size, so fail
2063 * If mddev->level <= 0, then we don't care
2064 * about aligning sizes (e.g. linear)
2065 */
2066 if (mddev->level > 0)
2067 return -ENOSPC;
2068 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002069 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 /* Verify rdev->desc_nr is unique.
2073 * If it is -1, assign a free number, else
2074 * check number is not in use
2075 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002076 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (rdev->desc_nr < 0) {
2078 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002079 if (mddev->pers)
2080 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002081 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 choice++;
2083 rdev->desc_nr = choice;
2084 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002085 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002086 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002090 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002091 if (!test_bit(Journal, &rdev->flags) &&
2092 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002093 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2094 mdname(mddev), mddev->max_disks);
2095 return -EBUSY;
2096 }
NeilBrown19133a42005-11-08 21:39:35 -08002097 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002098 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002101 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002102
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002103 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002104 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002105
Tejun Heo0762b8b2008-08-25 19:56:12 +09002106 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002107 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2108 /* failure here is OK */;
2109 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002110
NeilBrown4b809912008-07-21 17:05:25 +10002111 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002112 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002113
2114 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002115 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002118
2119 fail:
2120 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2121 b, mdname(mddev));
2122 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
NeilBrown177a99b2008-02-06 01:39:56 -08002125static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002126{
NeilBrown3cb03002011-10-11 16:45:26 +11002127 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002128 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002129 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002130}
2131
NeilBrownf72ffdd2014-09-30 14:23:59 +10002132static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002135
Tejun Heo49731ba2011-01-14 18:43:57 +01002136 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002137 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2139 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002140 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002141 sysfs_put(rdev->sysfs_state);
2142 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002143 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002144 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002145 * writing to 'remove' to "dev/state". We also need
2146 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002147 */
NeilBrown4b809912008-07-21 17:05:25 +10002148 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002149 INIT_WORK(&rdev->del_work, md_delayed_delete);
2150 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002151 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
2154/*
2155 * prevent the device from being mounted, repartitioned or
2156 * otherwise reused by a RAID array (or any other kernel
2157 * subsystem), by bd_claiming the device.
2158 */
NeilBrown3cb03002011-10-11 16:45:26 +11002159static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 int err = 0;
2162 struct block_device *bdev;
2163 char b[BDEVNAME_SIZE];
2164
Tejun Heod4d77622010-11-13 11:55:18 +01002165 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002166 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (IS_ERR(bdev)) {
2168 printk(KERN_ERR "md: could not open %s.\n",
2169 __bdevname(dev, b));
2170 return PTR_ERR(bdev);
2171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 rdev->bdev = bdev;
2173 return err;
2174}
2175
NeilBrown3cb03002011-10-11 16:45:26 +11002176static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 struct block_device *bdev = rdev->bdev;
2179 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002180 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183void md_autodetect_dev(dev_t dev);
2184
NeilBrownf72ffdd2014-09-30 14:23:59 +10002185static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 printk(KERN_INFO "md: export_rdev(%s)\n",
2190 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002191 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002193 if (test_bit(AutoDetected, &rdev->flags))
2194 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195#endif
2196 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002197 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002200void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 unbind_rdev_from_array(rdev);
2203 export_rdev(rdev);
2204}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002205EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
NeilBrownfd01b882011-10-11 16:47:53 +11002207static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
NeilBrown0638bb02014-09-25 17:43:47 +10002209 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
NeilBrown0638bb02014-09-25 17:43:47 +10002211 while (!list_empty(&mddev->disks)) {
2212 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2213 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002214 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 mddev->raid_disks = 0;
2217 mddev->major_version = 0;
2218}
2219
NeilBrownf72ffdd2014-09-30 14:23:59 +10002220static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
NeilBrown42543762006-06-26 00:27:57 -07002222 /* Update each superblock (in-memory image), but
2223 * if we are allowed to, skip spares which already
2224 * have the right event counter, or have one earlier
2225 * (which would mean they aren't being marked as dirty
2226 * with the rest of the array)
2227 */
NeilBrown3cb03002011-10-11 16:45:26 +11002228 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002229 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002230 if (rdev->sb_events == mddev->events ||
2231 (nospares &&
2232 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002233 rdev->sb_events+1 == mddev->events)) {
2234 /* Don't update this superblock */
2235 rdev->sb_loaded = 2;
2236 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002237 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002238 rdev->sb_loaded = 1;
2239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
2241}
2242
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002243static bool does_sb_need_changing(struct mddev *mddev)
2244{
2245 struct md_rdev *rdev;
2246 struct mdp_superblock_1 *sb;
2247 int role;
2248
2249 /* Find a good rdev */
2250 rdev_for_each(rdev, mddev)
2251 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2252 break;
2253
2254 /* No good device found. */
2255 if (!rdev)
2256 return false;
2257
2258 sb = page_address(rdev->sb_page);
2259 /* Check if a device has become faulty or a spare become active */
2260 rdev_for_each(rdev, mddev) {
2261 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2262 /* Device activated? */
2263 if (role == 0xffff && rdev->raid_disk >=0 &&
2264 !test_bit(Faulty, &rdev->flags))
2265 return true;
2266 /* Device turned faulty? */
2267 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2268 return true;
2269 }
2270
2271 /* Check if any mddev parameters have changed */
2272 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2273 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002274 (mddev->layout != le64_to_cpu(sb->layout)) ||
2275 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2276 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2277 return true;
2278
2279 return false;
2280}
2281
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002282void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
NeilBrown3cb03002011-10-11 16:45:26 +11002284 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002285 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002286 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002287 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002288 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
NeilBrownd87f0642013-04-24 11:42:40 +10002290 if (mddev->ro) {
2291 if (force_change)
2292 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2293 return;
2294 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002295
2296 if (mddev_is_clustered(mddev)) {
2297 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2298 force_change = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002299 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002300 /* Has someone else has updated the sb */
2301 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002302 if (ret == 0)
2303 md_cluster_ops->metadata_update_cancel(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002304 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2305 return;
2306 }
2307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002309 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002310 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002311 if (rdev->raid_disk >= 0 &&
2312 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002313 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002314 !test_bit(In_sync, &rdev->flags) &&
2315 mddev->curr_resync_completed > rdev->recovery_offset)
2316 rdev->recovery_offset = mddev->curr_resync_completed;
2317
NeilBrownf72ffdd2014-09-30 14:23:59 +10002318 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002319 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002320 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002321 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002322 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002323 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002324 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002325 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002326 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002327 md_ack_all_badblocks(&rdev->badblocks);
2328 md_error(mddev, rdev);
2329 }
2330 clear_bit(Blocked, &rdev->flags);
2331 clear_bit(BlockedBadBlocks, &rdev->flags);
2332 wake_up(&rdev->blocked_wait);
2333 }
2334 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002335 wake_up(&mddev->sb_wait);
2336 return;
2337 }
2338
NeilBrown85572d72014-12-15 12:56:56 +11002339 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002340
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002341 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002342
NeilBrown850b2b422006-10-03 01:15:46 -07002343 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2344 force_change = 1;
2345 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2346 /* just a clean<-> dirty transition, possibly leave spares alone,
2347 * though if events isn't the right even/odd, we will have to do
2348 * spares after all
2349 */
2350 nospares = 1;
2351 if (force_change)
2352 nospares = 0;
2353 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002354 /* If the array is degraded, then skipping spares is both
2355 * dangerous and fairly pointless.
2356 * Dangerous because a device that was removed from the array
2357 * might have a event_count that still looks up-to-date,
2358 * so it can be re-added without a resync.
2359 * Pointless because if there are any spares to skip,
2360 * then a recovery will happen and soon that array won't
2361 * be degraded any more and the spare can go back to sleep then.
2362 */
NeilBrown850b2b422006-10-03 01:15:46 -07002363 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002364
NeilBrown06d91a52005-06-21 17:17:12 -07002365 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002366
2367 /* If this is just a dirty<->clean transition, and the array is clean
2368 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002369 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002370 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002371 && mddev->can_decrease_events
2372 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002373 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002374 mddev->can_decrease_events = 0;
2375 } else {
NeilBrown42543762006-06-26 00:27:57 -07002376 /* otherwise we have to go forward and ... */
2377 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002378 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
NeilBrown403df472014-09-30 15:52:29 +10002381 /*
2382 * This 64-bit counter should never wrap.
2383 * Either we are in around ~1 trillion A.C., assuming
2384 * 1 reboot per second, or we have a bug...
2385 */
2386 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002387
NeilBrowndafb20f2012-03-19 12:46:39 +11002388 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002389 if (rdev->badblocks.changed)
2390 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002391 if (test_bit(Faulty, &rdev->flags))
2392 set_bit(FaultRecorded, &rdev->flags);
2393 }
NeilBrown2699b672011-07-28 11:31:47 +10002394
NeilBrowne6910632008-02-06 01:39:51 -08002395 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002396 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
NeilBrown36a4e1f2011-10-07 14:23:17 +11002398 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2399 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
NeilBrown4ad13662007-07-17 04:06:13 -07002401 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002402 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002404
NeilBrown42543762006-06-26 00:27:57 -07002405 if (rdev->sb_loaded != 1)
2406 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
NeilBrownf4667222013-12-09 12:04:56 +11002408 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002409 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002410 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002411 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002412 pr_debug("md: (write) %s's sb offset: %llu\n",
2413 bdevname(rdev->bdev, b),
2414 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002415 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002416 if (rdev->badblocks.size) {
2417 md_super_write(mddev, rdev,
2418 rdev->badblocks.sector,
2419 rdev->badblocks.size << 9,
2420 rdev->bb_page);
2421 rdev->badblocks.size = 0;
2422 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002423
NeilBrownf4667222013-12-09 12:04:56 +11002424 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002425 pr_debug("md: %s (skipping faulty)\n",
2426 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002427
NeilBrown7bfa19f2005-06-21 17:17:28 -07002428 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 /* only need to write one superblock... */
2430 break;
2431 }
NeilBrowna9701a32005-11-08 21:39:34 -08002432 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002433 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002434
NeilBrown85572d72014-12-15 12:56:56 +11002435 spin_lock(&mddev->lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002436 if (mddev->in_sync != sync_req ||
2437 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002438 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002439 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002440 goto repeat;
2441 }
NeilBrown850b2b422006-10-03 01:15:46 -07002442 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002443 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002444 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002445 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2446 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002447
NeilBrowndafb20f2012-03-19 12:46:39 +11002448 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002449 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2450 clear_bit(Blocked, &rdev->flags);
2451
2452 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002453 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002454 clear_bit(BlockedBadBlocks, &rdev->flags);
2455 wake_up(&rdev->blocked_wait);
2456 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002457
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002458 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002459 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002461EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002463static int add_bound_rdev(struct md_rdev *rdev)
2464{
2465 struct mddev *mddev = rdev->mddev;
2466 int err = 0;
2467
2468 if (!mddev->pers->hot_remove_disk) {
2469 /* If there is hot_add_disk but no hot_remove_disk
2470 * then added disks for geometry changes,
2471 * and should be added immediately.
2472 */
2473 super_types[mddev->major_version].
2474 validate_super(mddev, rdev);
2475 err = mddev->pers->hot_add_disk(mddev, rdev);
2476 if (err) {
2477 unbind_rdev_from_array(rdev);
2478 export_rdev(rdev);
2479 return err;
2480 }
2481 }
2482 sysfs_notify_dirent_safe(rdev->sysfs_state);
2483
2484 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2485 if (mddev->degraded)
2486 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2487 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2488 md_new_event(mddev);
2489 md_wakeup_thread(mddev->thread);
2490 return 0;
2491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Andre Noll7f6ce762008-03-23 18:34:54 +01002493/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002494 * We want to accept with case. For this we use cmd_match.
2495 */
2496static int cmd_match(const char *cmd, const char *str)
2497{
2498 /* See if cmd, written into a sysfs file, matches
2499 * str. They must either be the same, or cmd can
2500 * have a trailing newline
2501 */
2502 while (*cmd && *str && *cmd == *str) {
2503 cmd++;
2504 str++;
2505 }
2506 if (*cmd == '\n')
2507 cmd++;
2508 if (*str || *cmd)
2509 return 0;
2510 return 1;
2511}
2512
NeilBrown86e6ffd2005-11-08 21:39:24 -08002513struct rdev_sysfs_entry {
2514 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002515 ssize_t (*show)(struct md_rdev *, char *);
2516 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002517};
2518
2519static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002520state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002521{
2522 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002523 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002524 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002525
NeilBrown758bfc82014-12-15 12:56:59 +11002526 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002527 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002528 len+= sprintf(page+len, "%sfaulty",sep);
2529 sep = ",";
2530 }
NeilBrown758bfc82014-12-15 12:56:59 +11002531 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002532 len += sprintf(page+len, "%sin_sync",sep);
2533 sep = ",";
2534 }
Shaohua Liac6096e2015-10-04 09:20:11 -07002535 if (test_bit(Journal, &flags)) {
2536 len += sprintf(page+len, "%sjournal",sep);
2537 sep = ",";
2538 }
NeilBrown758bfc82014-12-15 12:56:59 +11002539 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002540 len += sprintf(page+len, "%swrite_mostly",sep);
2541 sep = ",";
2542 }
NeilBrown758bfc82014-12-15 12:56:59 +11002543 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002544 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002545 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002546 len += sprintf(page+len, "%sblocked", sep);
2547 sep = ",";
2548 }
NeilBrown758bfc82014-12-15 12:56:59 +11002549 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002550 !test_bit(Journal, &flags) &&
NeilBrown758bfc82014-12-15 12:56:59 +11002551 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002552 len += sprintf(page+len, "%sspare", sep);
2553 sep = ",";
2554 }
NeilBrown758bfc82014-12-15 12:56:59 +11002555 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002556 len += sprintf(page+len, "%swrite_error", sep);
2557 sep = ",";
2558 }
NeilBrown758bfc82014-12-15 12:56:59 +11002559 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002560 len += sprintf(page+len, "%swant_replacement", sep);
2561 sep = ",";
2562 }
NeilBrown758bfc82014-12-15 12:56:59 +11002563 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002564 len += sprintf(page+len, "%sreplacement", sep);
2565 sep = ",";
2566 }
2567
NeilBrown86e6ffd2005-11-08 21:39:24 -08002568 return len+sprintf(page+len, "\n");
2569}
2570
NeilBrown45dc2de2006-06-26 00:27:58 -07002571static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002572state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002573{
2574 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002575 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002576 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002577 * writemostly - sets write_mostly
2578 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002579 * blocked - sets the Blocked flags
2580 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002581 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002582 * -insync - clear Insync for a device with a slot assigned,
2583 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002584 * write_error - sets WriteErrorSeen
2585 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002586 */
2587 int err = -EINVAL;
2588 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2589 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002590 if (test_bit(Faulty, &rdev->flags))
2591 err = 0;
2592 else
2593 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002594 } else if (cmd_match(buf, "remove")) {
2595 if (rdev->raid_disk >= 0)
2596 err = -EBUSY;
2597 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002598 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002599 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002600 if (mddev_is_clustered(mddev))
2601 err = md_cluster_ops->remove_disk(mddev, rdev);
2602
2603 if (err == 0) {
2604 md_kick_rdev_from_array(rdev);
2605 if (mddev->pers)
2606 md_update_sb(mddev, 1);
2607 md_new_event(mddev);
2608 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002609 }
NeilBrownf6556752006-06-26 00:28:01 -07002610 } else if (cmd_match(buf, "writemostly")) {
2611 set_bit(WriteMostly, &rdev->flags);
2612 err = 0;
2613 } else if (cmd_match(buf, "-writemostly")) {
2614 clear_bit(WriteMostly, &rdev->flags);
2615 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002616 } else if (cmd_match(buf, "blocked")) {
2617 set_bit(Blocked, &rdev->flags);
2618 err = 0;
2619 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002620 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002621 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002622 /* metadata handler doesn't understand badblocks,
2623 * so we need to fail the device
2624 */
2625 md_error(rdev->mddev, rdev);
2626 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002627 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002628 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002629 wake_up(&rdev->blocked_wait);
2630 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2631 md_wakeup_thread(rdev->mddev->thread);
2632
2633 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002634 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2635 set_bit(In_sync, &rdev->flags);
2636 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002637 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2638 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002639 if (rdev->mddev->pers == NULL) {
2640 clear_bit(In_sync, &rdev->flags);
2641 rdev->saved_raid_disk = rdev->raid_disk;
2642 rdev->raid_disk = -1;
2643 err = 0;
2644 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002645 } else if (cmd_match(buf, "write_error")) {
2646 set_bit(WriteErrorSeen, &rdev->flags);
2647 err = 0;
2648 } else if (cmd_match(buf, "-write_error")) {
2649 clear_bit(WriteErrorSeen, &rdev->flags);
2650 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002651 } else if (cmd_match(buf, "want_replacement")) {
2652 /* Any non-spare device that is not a replacement can
2653 * become want_replacement at any time, but we then need to
2654 * check if recovery is needed.
2655 */
2656 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002657 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002658 !test_bit(Replacement, &rdev->flags))
2659 set_bit(WantReplacement, &rdev->flags);
2660 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2661 md_wakeup_thread(rdev->mddev->thread);
2662 err = 0;
2663 } else if (cmd_match(buf, "-want_replacement")) {
2664 /* Clearing 'want_replacement' is always allowed.
2665 * Once replacements starts it is too late though.
2666 */
2667 err = 0;
2668 clear_bit(WantReplacement, &rdev->flags);
2669 } else if (cmd_match(buf, "replacement")) {
2670 /* Can only set a device as a replacement when array has not
2671 * yet been started. Once running, replacement is automatic
2672 * from spares, or by assigning 'slot'.
2673 */
2674 if (rdev->mddev->pers)
2675 err = -EBUSY;
2676 else {
2677 set_bit(Replacement, &rdev->flags);
2678 err = 0;
2679 }
2680 } else if (cmd_match(buf, "-replacement")) {
2681 /* Similarly, can only clear Replacement before start */
2682 if (rdev->mddev->pers)
2683 err = -EBUSY;
2684 else {
2685 clear_bit(Replacement, &rdev->flags);
2686 err = 0;
2687 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002688 } else if (cmd_match(buf, "re-add")) {
2689 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002690 /* clear_bit is performed _after_ all the devices
2691 * have their local Faulty bit cleared. If any writes
2692 * happen in the meantime in the local node, they
2693 * will land in the local bitmap, which will be synced
2694 * by this node eventually
2695 */
2696 if (!mddev_is_clustered(rdev->mddev) ||
2697 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2698 clear_bit(Faulty, &rdev->flags);
2699 err = add_bound_rdev(rdev);
2700 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002701 } else
2702 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002703 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002704 if (!err)
2705 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002706 return err ? err : len;
2707}
NeilBrown80ca3a42006-07-10 04:44:18 -07002708static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002709__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002710
2711static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002712errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002713{
2714 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2715}
2716
2717static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002718errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002719{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002720 unsigned int n;
2721 int rv;
2722
2723 rv = kstrtouint(buf, 10, &n);
2724 if (rv < 0)
2725 return rv;
2726 atomic_set(&rdev->corrected_errors, n);
2727 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002728}
2729static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002730__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002731
NeilBrown014236d2006-01-06 00:20:55 -08002732static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002733slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002734{
Shaohua Lif2076e72015-10-08 21:54:12 -07002735 if (test_bit(Journal, &rdev->flags))
2736 return sprintf(page, "journal\n");
2737 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002738 return sprintf(page, "none\n");
2739 else
2740 return sprintf(page, "%d\n", rdev->raid_disk);
2741}
2742
2743static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002744slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002745{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002746 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002747 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002748
Shaohua Lif2076e72015-10-08 21:54:12 -07002749 if (test_bit(Journal, &rdev->flags))
2750 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08002751 if (strncmp(buf, "none", 4)==0)
2752 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002753 else {
2754 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2755 if (err < 0)
2756 return err;
2757 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002758 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002759 /* Setting 'slot' on an active array requires also
2760 * updating the 'rd%d' link, and communicating
2761 * with the personality with ->hot_*_disk.
2762 * For now we only support removing
2763 * failed/spare devices. This normally happens automatically,
2764 * but not when the metadata is externally managed.
2765 */
NeilBrownc303da62008-02-06 01:39:51 -08002766 if (rdev->raid_disk == -1)
2767 return -EEXIST;
2768 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002769 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002770 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002771 clear_bit(Blocked, &rdev->flags);
2772 remove_and_add_spares(rdev->mddev, rdev);
2773 if (rdev->raid_disk >= 0)
2774 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002775 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2776 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002777 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002778 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002779 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002780 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002781 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10002782
2783 if (rdev->raid_disk != -1)
2784 return -EBUSY;
2785
NeilBrownc6751b22011-02-02 11:57:13 +11002786 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2787 return -EBUSY;
2788
Neil Brown6c2fce22008-06-28 08:31:31 +10002789 if (rdev->mddev->pers->hot_add_disk == NULL)
2790 return -EINVAL;
2791
NeilBrownba1b41b2011-01-14 09:14:34 +11002792 if (slot >= rdev->mddev->raid_disks &&
2793 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2794 return -ENOSPC;
2795
Neil Brown6c2fce22008-06-28 08:31:31 +10002796 rdev->raid_disk = slot;
2797 if (test_bit(In_sync, &rdev->flags))
2798 rdev->saved_raid_disk = slot;
2799 else
2800 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002801 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002802 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11002803 err = rdev->mddev->pers->
2804 hot_add_disk(rdev->mddev, rdev);
2805 if (err) {
2806 rdev->raid_disk = -1;
2807 return err;
2808 } else
2809 sysfs_notify_dirent_safe(rdev->sysfs_state);
2810 if (sysfs_link_rdev(rdev->mddev, rdev))
2811 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002812 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002813 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002814 if (slot >= rdev->mddev->raid_disks &&
2815 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002816 return -ENOSPC;
2817 rdev->raid_disk = slot;
2818 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002819 clear_bit(Faulty, &rdev->flags);
2820 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002821 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002822 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002823 }
NeilBrown014236d2006-01-06 00:20:55 -08002824 return len;
2825}
2826
NeilBrown014236d2006-01-06 00:20:55 -08002827static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002828__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002829
NeilBrown93c8cad2006-01-06 00:20:56 -08002830static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002831offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002832{
Andrew Morton6961ece2006-01-06 00:20:59 -08002833 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002834}
2835
2836static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002837offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002838{
NeilBrownc6563a82012-05-21 09:27:00 +10002839 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002840 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002841 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002842 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002843 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002844 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002845 /* Must set offset before size, so overlap checks
2846 * can be sane */
2847 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002848 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002849 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002850 return len;
2851}
2852
2853static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002854__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002855
NeilBrownc6563a82012-05-21 09:27:00 +10002856static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2857{
2858 return sprintf(page, "%llu\n",
2859 (unsigned long long)rdev->new_data_offset);
2860}
2861
2862static ssize_t new_offset_store(struct md_rdev *rdev,
2863 const char *buf, size_t len)
2864{
2865 unsigned long long new_offset;
2866 struct mddev *mddev = rdev->mddev;
2867
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002868 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002869 return -EINVAL;
2870
NeilBrownf851b602014-12-11 10:02:10 +11002871 if (mddev->sync_thread ||
2872 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002873 return -EBUSY;
2874 if (new_offset == rdev->data_offset)
2875 /* reset is always permitted */
2876 ;
2877 else if (new_offset > rdev->data_offset) {
2878 /* must not push array size beyond rdev_sectors */
2879 if (new_offset - rdev->data_offset
2880 + mddev->dev_sectors > rdev->sectors)
2881 return -E2BIG;
2882 }
2883 /* Metadata worries about other space details. */
2884
2885 /* decreasing the offset is inconsistent with a backwards
2886 * reshape.
2887 */
2888 if (new_offset < rdev->data_offset &&
2889 mddev->reshape_backwards)
2890 return -EINVAL;
2891 /* Increasing offset is inconsistent with forwards
2892 * reshape. reshape_direction should be set to
2893 * 'backwards' first.
2894 */
2895 if (new_offset > rdev->data_offset &&
2896 !mddev->reshape_backwards)
2897 return -EINVAL;
2898
2899 if (mddev->pers && mddev->persistent &&
2900 !super_types[mddev->major_version]
2901 .allow_new_offset(rdev, new_offset))
2902 return -E2BIG;
2903 rdev->new_data_offset = new_offset;
2904 if (new_offset > rdev->data_offset)
2905 mddev->reshape_backwards = 1;
2906 else if (new_offset < rdev->data_offset)
2907 mddev->reshape_backwards = 0;
2908
2909 return len;
2910}
2911static struct rdev_sysfs_entry rdev_new_offset =
2912__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2913
NeilBrown83303b62006-01-06 00:21:06 -08002914static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002915rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002916{
Andre Nolldd8ac332009-03-31 14:33:13 +11002917 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002918}
2919
NeilBrownc5d79ad2008-02-06 01:39:54 -08002920static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2921{
2922 /* check if two start/length pairs overlap */
2923 if (s1+l1 <= s2)
2924 return 0;
2925 if (s2+l2 <= s1)
2926 return 0;
2927 return 1;
2928}
2929
Dan Williamsb522adc2009-03-31 15:00:31 +11002930static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2931{
2932 unsigned long long blocks;
2933 sector_t new;
2934
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002935 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002936 return -EINVAL;
2937
2938 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2939 return -EINVAL; /* sector conversion overflow */
2940
2941 new = blocks * 2;
2942 if (new != blocks * 2)
2943 return -EINVAL; /* unsigned long long to sector_t overflow */
2944
2945 *sectors = new;
2946 return 0;
2947}
2948
NeilBrown83303b62006-01-06 00:21:06 -08002949static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002950rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002951{
NeilBrownfd01b882011-10-11 16:47:53 +11002952 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002953 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002954 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002955
Shaohua Lif2076e72015-10-08 21:54:12 -07002956 if (test_bit(Journal, &rdev->flags))
2957 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11002958 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002959 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002960 if (rdev->data_offset != rdev->new_data_offset)
2961 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002962 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002963 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002964 sectors = super_types[my_mddev->major_version].
2965 rdev_size_change(rdev, sectors);
2966 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002967 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002968 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002969 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002970 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002971 if (!my_mddev->pers->resize)
2972 /* Cannot change size for RAID0 or Linear etc */
2973 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002974 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002975 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002976 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002977
Andre Nolldd8ac332009-03-31 14:33:13 +11002978 rdev->sectors = sectors;
2979 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002980 /* Need to check that all other rdevs with the same
2981 * ->bdev do not overlap. 'rcu' is sufficient to walk
2982 * the rdev lists safely.
2983 * This check does not provide a hard guarantee, it
2984 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002985 */
NeilBrownfd01b882011-10-11 16:47:53 +11002986 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002987 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002988 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002989
NeilBrown8b1afc32014-09-29 15:33:20 +10002990 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002991 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002992 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002993
NeilBrowndafb20f2012-03-19 12:46:39 +11002994 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002995 if (rdev->bdev == rdev2->bdev &&
2996 rdev != rdev2 &&
2997 overlaps(rdev->data_offset, rdev->sectors,
2998 rdev2->data_offset,
2999 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003000 overlap = 1;
3001 break;
3002 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003003 if (overlap) {
3004 mddev_put(mddev);
3005 break;
3006 }
3007 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003008 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003009 if (overlap) {
3010 /* Someone else could have slipped in a size
3011 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003012 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003013 * safe, and trust userspace not to race with
3014 * itself
3015 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003016 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003017 return -EBUSY;
3018 }
3019 }
NeilBrown83303b62006-01-06 00:21:06 -08003020 return len;
3021}
3022
3023static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003024__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003025
NeilBrown3cb03002011-10-11 16:45:26 +11003026static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003027{
3028 unsigned long long recovery_start = rdev->recovery_offset;
3029
3030 if (test_bit(In_sync, &rdev->flags) ||
3031 recovery_start == MaxSector)
3032 return sprintf(page, "none\n");
3033
3034 return sprintf(page, "%llu\n", recovery_start);
3035}
3036
NeilBrown3cb03002011-10-11 16:45:26 +11003037static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003038{
3039 unsigned long long recovery_start;
3040
3041 if (cmd_match(buf, "none"))
3042 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003043 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003044 return -EINVAL;
3045
3046 if (rdev->mddev->pers &&
3047 rdev->raid_disk >= 0)
3048 return -EBUSY;
3049
3050 rdev->recovery_offset = recovery_start;
3051 if (recovery_start == MaxSector)
3052 set_bit(In_sync, &rdev->flags);
3053 else
3054 clear_bit(In_sync, &rdev->flags);
3055 return len;
3056}
3057
3058static struct rdev_sysfs_entry rdev_recovery_start =
3059__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3060
NeilBrown16c791a2011-07-28 11:31:47 +10003061static ssize_t
3062badblocks_show(struct badblocks *bb, char *page, int unack);
3063static ssize_t
3064badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3065
NeilBrown3cb03002011-10-11 16:45:26 +11003066static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003067{
3068 return badblocks_show(&rdev->badblocks, page, 0);
3069}
NeilBrown3cb03002011-10-11 16:45:26 +11003070static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003071{
NeilBrownde393cd2011-07-28 11:31:48 +10003072 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3073 /* Maybe that ack was all we needed */
3074 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3075 wake_up(&rdev->blocked_wait);
3076 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003077}
3078static struct rdev_sysfs_entry rdev_bad_blocks =
3079__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3080
NeilBrown3cb03002011-10-11 16:45:26 +11003081static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003082{
3083 return badblocks_show(&rdev->badblocks, page, 1);
3084}
NeilBrown3cb03002011-10-11 16:45:26 +11003085static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003086{
3087 return badblocks_store(&rdev->badblocks, page, len, 1);
3088}
3089static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3090__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3091
NeilBrown86e6ffd2005-11-08 21:39:24 -08003092static struct attribute *rdev_default_attrs[] = {
3093 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003094 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003095 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003096 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003097 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003098 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003099 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003100 &rdev_bad_blocks.attr,
3101 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003102 NULL,
3103};
3104static ssize_t
3105rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3106{
3107 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003108 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003109
3110 if (!entry->show)
3111 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003112 if (!rdev->mddev)
3113 return -EBUSY;
3114 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003115}
3116
3117static ssize_t
3118rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3119 const char *page, size_t length)
3120{
3121 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003122 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003123 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003124 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003125
3126 if (!entry->store)
3127 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003128 if (!capable(CAP_SYS_ADMIN))
3129 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003130 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003131 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003132 if (rdev->mddev == NULL)
3133 rv = -EBUSY;
3134 else
3135 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003136 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003137 }
3138 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003139}
3140
3141static void rdev_free(struct kobject *ko)
3142{
NeilBrown3cb03002011-10-11 16:45:26 +11003143 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003144 kfree(rdev);
3145}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003146static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003147 .show = rdev_attr_show,
3148 .store = rdev_attr_store,
3149};
3150static struct kobj_type rdev_ktype = {
3151 .release = rdev_free,
3152 .sysfs_ops = &rdev_sysfs_ops,
3153 .default_attrs = rdev_default_attrs,
3154};
3155
NeilBrown3cb03002011-10-11 16:45:26 +11003156int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003157{
3158 rdev->desc_nr = -1;
3159 rdev->saved_raid_disk = -1;
3160 rdev->raid_disk = -1;
3161 rdev->flags = 0;
3162 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003163 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003164 rdev->sb_events = 0;
3165 rdev->last_read_error.tv_sec = 0;
3166 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003167 rdev->sb_loaded = 0;
3168 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003169 atomic_set(&rdev->nr_pending, 0);
3170 atomic_set(&rdev->read_errors, 0);
3171 atomic_set(&rdev->corrected_errors, 0);
3172
3173 INIT_LIST_HEAD(&rdev->same_set);
3174 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003175
3176 /* Add space to store bad block list.
3177 * This reserves the space even on arrays where it cannot
3178 * be used - I wonder if that matters
3179 */
3180 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003181 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003182 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3183 seqlock_init(&rdev->badblocks.lock);
3184 if (rdev->badblocks.page == NULL)
3185 return -ENOMEM;
3186
3187 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003188}
3189EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190/*
3191 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3192 *
3193 * mark the device faulty if:
3194 *
3195 * - the device is nonexistent (zero size)
3196 * - the device has no valid superblock
3197 *
3198 * a faulty rdev _never_ has rdev->sb set.
3199 */
NeilBrown3cb03002011-10-11 16:45:26 +11003200static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 char b[BDEVNAME_SIZE];
3203 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003204 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 sector_t size;
3206
NeilBrown9ffae0c2006-01-06 00:20:32 -08003207 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (!rdev) {
3209 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3210 return ERR_PTR(-ENOMEM);
3211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
NeilBrown2230dfe2011-07-28 11:31:46 +10003213 err = md_rdev_init(rdev);
3214 if (err)
3215 goto abort_free;
3216 err = alloc_disk_sb(rdev);
3217 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 goto abort_free;
3219
NeilBrownc5d79ad2008-02-06 01:39:54 -08003220 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if (err)
3222 goto abort_free;
3223
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003224 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003225
Mike Snitzer77304d22010-11-08 14:39:12 +01003226 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003228 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 "md: %s has zero or unknown size, marking faulty!\n",
3230 bdevname(rdev->bdev,b));
3231 err = -EINVAL;
3232 goto abort_free;
3233 }
3234
3235 if (super_format >= 0) {
3236 err = super_types[super_format].
3237 load_super(rdev, NULL, super_minor);
3238 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003239 printk(KERN_WARNING
3240 "md: %s does not have a valid v%d.%d "
3241 "superblock, not importing!\n",
3242 bdevname(rdev->bdev,b),
3243 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 goto abort_free;
3245 }
3246 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003247 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 "md: could not read %s's sb, not importing!\n",
3249 bdevname(rdev->bdev,b));
3250 goto abort_free;
3251 }
3252 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return rdev;
3255
3256abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003257 if (rdev->bdev)
3258 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003259 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 kfree(rdev);
3261 return ERR_PTR(err);
3262}
3263
3264/*
3265 * Check a full RAID array for plausibility
3266 */
3267
NeilBrownf72ffdd2014-09-30 14:23:59 +10003268static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003271 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 char b[BDEVNAME_SIZE];
3273
3274 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003275 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 switch (super_types[mddev->major_version].
3277 load_super(rdev, freshest, mddev->minor_version)) {
3278 case 1:
3279 freshest = rdev;
3280 break;
3281 case 0:
3282 break;
3283 default:
3284 printk( KERN_ERR \
3285 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003286 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003288 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 }
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 super_types[mddev->major_version].
3292 validate_super(mddev, freshest);
3293
3294 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003295 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003296 if (mddev->max_disks &&
3297 (rdev->desc_nr >= mddev->max_disks ||
3298 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003299 printk(KERN_WARNING
3300 "md: %s: %s: only %d devices permitted\n",
3301 mdname(mddev), bdevname(rdev->bdev, b),
3302 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003303 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003304 continue;
3305 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003306 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 if (super_types[mddev->major_version].
3308 validate_super(mddev, rdev)) {
3309 printk(KERN_WARNING "md: kicking non-fresh %s"
3310 " from array!\n",
3311 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003312 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 continue;
3314 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (mddev->level == LEVEL_MULTIPATH) {
3317 rdev->desc_nr = i++;
3318 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003319 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003320 } else if (rdev->raid_disk >=
3321 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3322 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003323 rdev->raid_disk = -1;
3324 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
3326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
NeilBrown72e02072009-12-14 12:49:55 +11003329/* Read a fixed-point number.
3330 * Numbers in sysfs attributes should be in "standard" units where
3331 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003332 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003333 * milliseconds or jiffies.
3334 * This function takes a decimal number with a possible fractional
3335 * component, and produces an integer which is the result of
3336 * multiplying that number by 10^'scale'.
3337 * all without any floating-point arithmetic.
3338 */
3339int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3340{
3341 unsigned long result = 0;
3342 long decimals = -1;
3343 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3344 if (*cp == '.')
3345 decimals = 0;
3346 else if (decimals < scale) {
3347 unsigned int value;
3348 value = *cp - '0';
3349 result = result * 10 + value;
3350 if (decimals >= 0)
3351 decimals++;
3352 }
3353 cp++;
3354 }
3355 if (*cp == '\n')
3356 cp++;
3357 if (*cp)
3358 return -EINVAL;
3359 if (decimals < 0)
3360 decimals = 0;
3361 while (decimals < scale) {
3362 result *= 10;
3363 decimals ++;
3364 }
3365 *res = result;
3366 return 0;
3367}
3368
NeilBrowneae17012005-11-08 21:39:23 -08003369static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003370safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003371{
3372 int msec = (mddev->safemode_delay*1000)/HZ;
3373 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3374}
3375static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003376safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003377{
NeilBrown16f17b32006-06-26 00:27:37 -07003378 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003379
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003380 if (mddev_is_clustered(mddev)) {
3381 pr_info("md: Safemode is disabled for clustered mode\n");
3382 return -EINVAL;
3383 }
3384
NeilBrown72e02072009-12-14 12:49:55 +11003385 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003386 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003387 if (msec == 0)
3388 mddev->safemode_delay = 0;
3389 else {
NeilBrown19052c02008-08-05 15:54:13 +10003390 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003391 unsigned long new_delay = (msec*HZ)/1000;
3392
3393 if (new_delay == 0)
3394 new_delay = 1;
3395 mddev->safemode_delay = new_delay;
3396 if (new_delay < old_delay || old_delay == 0)
3397 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003398 }
3399 return len;
3400}
3401static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003402__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003403
3404static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003405level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003406{
NeilBrown36d091f2014-12-15 12:56:58 +11003407 struct md_personality *p;
3408 int ret;
3409 spin_lock(&mddev->lock);
3410 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003411 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003412 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003413 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003414 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003415 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003416 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003417 else
NeilBrown36d091f2014-12-15 12:56:58 +11003418 ret = 0;
3419 spin_unlock(&mddev->lock);
3420 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003421}
3422
NeilBrownd9d166c2006-01-06 00:20:51 -08003423static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003424level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003425{
Dan Williamsf2859af2010-05-02 10:04:16 -07003426 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003427 ssize_t rv;
3428 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003429 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003430 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003431 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003432 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003433
NeilBrown67918752014-12-15 12:57:01 +11003434 if (slen == 0 || slen >= sizeof(clevel))
3435 return -EINVAL;
3436
3437 rv = mddev_lock(mddev);
3438 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003439 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003440
3441 if (mddev->pers == NULL) {
3442 strncpy(mddev->clevel, buf, slen);
3443 if (mddev->clevel[slen-1] == '\n')
3444 slen--;
3445 mddev->clevel[slen] = 0;
3446 mddev->level = LEVEL_NONE;
3447 rv = len;
3448 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003449 }
NeilBrown67918752014-12-15 12:57:01 +11003450 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003451 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003452 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003453
3454 /* request to change the personality. Need to ensure:
3455 * - array is not engaged in resync/recovery/reshape
3456 * - old personality can be suspended
3457 * - new personality will access other array.
3458 */
3459
NeilBrown67918752014-12-15 12:57:01 +11003460 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003461 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003462 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003463 mddev->reshape_position != MaxSector ||
3464 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003465 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003466
NeilBrown67918752014-12-15 12:57:01 +11003467 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003468 if (!mddev->pers->quiesce) {
3469 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3470 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003471 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003472 }
3473
3474 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003475 strncpy(clevel, buf, slen);
3476 if (clevel[slen-1] == '\n')
3477 slen--;
3478 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003479 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003480 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003481
Dan Williamsf2859af2010-05-02 10:04:16 -07003482 if (request_module("md-%s", clevel) != 0)
3483 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003484 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003485 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003486 if (!pers || !try_module_get(pers->owner)) {
3487 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003488 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003489 rv = -EINVAL;
3490 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003491 }
3492 spin_unlock(&pers_lock);
3493
3494 if (pers == mddev->pers) {
3495 /* Nothing to do! */
3496 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003497 rv = len;
3498 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003499 }
3500 if (!pers->takeover) {
3501 module_put(pers->owner);
3502 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003503 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003504 rv = -EINVAL;
3505 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003506 }
3507
NeilBrowndafb20f2012-03-19 12:46:39 +11003508 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003509 rdev->new_raid_disk = rdev->raid_disk;
3510
NeilBrown245f46c2009-03-31 14:39:39 +11003511 /* ->takeover must set new_* and/or delta_disks
3512 * if it succeeds, and may set them when it fails.
3513 */
3514 priv = pers->takeover(mddev);
3515 if (IS_ERR(priv)) {
3516 mddev->new_level = mddev->level;
3517 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003518 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003519 mddev->raid_disks -= mddev->delta_disks;
3520 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003521 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003522 module_put(pers->owner);
3523 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003524 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003525 rv = PTR_ERR(priv);
3526 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003527 }
3528
3529 /* Looks like we have a winner */
3530 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003531 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003532
3533 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003534 oldpers = mddev->pers;
3535 oldpriv = mddev->private;
3536 mddev->pers = pers;
3537 mddev->private = priv;
3538 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3539 mddev->level = mddev->new_level;
3540 mddev->layout = mddev->new_layout;
3541 mddev->chunk_sectors = mddev->new_chunk_sectors;
3542 mddev->delta_disks = 0;
3543 mddev->reshape_backwards = 0;
3544 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003545 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003546
NeilBrowndb721d32014-12-15 12:56:58 +11003547 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003548 mddev->external) {
3549 /* We are converting from a no-redundancy array
3550 * to a redundancy array and metadata is managed
3551 * externally so we need to be sure that writes
3552 * won't block due to a need to transition
3553 * clean->dirty
3554 * until external management is started.
3555 */
3556 mddev->in_sync = 0;
3557 mddev->safemode_delay = 0;
3558 mddev->safemode = 0;
3559 }
3560
NeilBrowndb721d32014-12-15 12:56:58 +11003561 oldpers->free(mddev, oldpriv);
3562
3563 if (oldpers->sync_request == NULL &&
3564 pers->sync_request != NULL) {
3565 /* need to add the md_redundancy_group */
3566 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3567 printk(KERN_WARNING
3568 "md: cannot register extra attributes for %s\n",
3569 mdname(mddev));
3570 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3571 }
3572 if (oldpers->sync_request != NULL &&
3573 pers->sync_request == NULL) {
3574 /* need to remove the md_redundancy_group */
3575 if (mddev->to_remove == NULL)
3576 mddev->to_remove = &md_redundancy_group;
3577 }
3578
NeilBrowndafb20f2012-03-19 12:46:39 +11003579 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003580 if (rdev->raid_disk < 0)
3581 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003582 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003583 rdev->new_raid_disk = -1;
3584 if (rdev->new_raid_disk == rdev->raid_disk)
3585 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003586 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003587 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003588 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003589 if (rdev->raid_disk < 0)
3590 continue;
3591 if (rdev->new_raid_disk == rdev->raid_disk)
3592 continue;
3593 rdev->raid_disk = rdev->new_raid_disk;
3594 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003595 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003596 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003597 if (sysfs_link_rdev(mddev, rdev))
3598 printk(KERN_WARNING "md: cannot register rd%d"
3599 " for %s after level change\n",
3600 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003601 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003602 }
3603
NeilBrowndb721d32014-12-15 12:56:58 +11003604 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003605 /* this is now an array without redundancy, so
3606 * it must always be in_sync
3607 */
3608 mddev->in_sync = 1;
3609 del_timer_sync(&mddev->safemode_timer);
3610 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003611 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003612 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003613 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003614 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003615 if (!mddev->thread)
3616 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003617 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003618 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003619 rv = len;
3620out_unlock:
3621 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003622 return rv;
3623}
3624
3625static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003626__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003627
NeilBrownd4dbd022006-06-26 00:27:59 -07003628static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003629layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003630{
3631 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003632 if (mddev->reshape_position != MaxSector &&
3633 mddev->layout != mddev->new_layout)
3634 return sprintf(page, "%d (%d)\n",
3635 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003636 return sprintf(page, "%d\n", mddev->layout);
3637}
3638
3639static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003640layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003641{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003642 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003643 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003644
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003645 err = kstrtouint(buf, 10, &n);
3646 if (err < 0)
3647 return err;
NeilBrown67918752014-12-15 12:57:01 +11003648 err = mddev_lock(mddev);
3649 if (err)
3650 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003651
NeilBrownb3546032009-03-31 14:56:41 +11003652 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003653 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003654 err = -EBUSY;
3655 else if (mddev->ro)
3656 err = -EROFS;
3657 else {
3658 mddev->new_layout = n;
3659 err = mddev->pers->check_reshape(mddev);
3660 if (err)
3661 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003662 }
NeilBrownb3546032009-03-31 14:56:41 +11003663 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003664 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003665 if (mddev->reshape_position == MaxSector)
3666 mddev->layout = n;
3667 }
NeilBrown67918752014-12-15 12:57:01 +11003668 mddev_unlock(mddev);
3669 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003670}
3671static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003672__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003673
NeilBrowneae17012005-11-08 21:39:23 -08003674static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003675raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003676{
NeilBrownbb636542005-11-08 21:39:45 -08003677 if (mddev->raid_disks == 0)
3678 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003679 if (mddev->reshape_position != MaxSector &&
3680 mddev->delta_disks != 0)
3681 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3682 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003683 return sprintf(page, "%d\n", mddev->raid_disks);
3684}
3685
NeilBrownfd01b882011-10-11 16:47:53 +11003686static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003687
3688static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003689raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003690{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003691 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003692 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003693
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003694 err = kstrtouint(buf, 10, &n);
3695 if (err < 0)
3696 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003697
NeilBrown67918752014-12-15 12:57:01 +11003698 err = mddev_lock(mddev);
3699 if (err)
3700 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003701 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003702 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003703 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003704 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003705 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003706
NeilBrown67918752014-12-15 12:57:01 +11003707 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003708 rdev_for_each(rdev, mddev) {
3709 if (olddisks < n &&
3710 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003711 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003712 if (olddisks > n &&
3713 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003714 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003715 }
NeilBrown67918752014-12-15 12:57:01 +11003716 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003717 mddev->delta_disks = n - olddisks;
3718 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003719 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003720 } else
NeilBrownda943b992006-01-06 00:20:54 -08003721 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003722out_unlock:
3723 mddev_unlock(mddev);
3724 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003725}
3726static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003727__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003728
NeilBrown24dd4692005-11-08 21:39:26 -08003729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003730chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003731{
NeilBrown08a02ec2007-05-09 02:35:38 -07003732 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003733 mddev->chunk_sectors != mddev->new_chunk_sectors)
3734 return sprintf(page, "%d (%d)\n",
3735 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003736 mddev->chunk_sectors << 9);
3737 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003738}
3739
3740static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003741chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003742{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003743 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003744 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003745
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003746 err = kstrtoul(buf, 10, &n);
3747 if (err < 0)
3748 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003749
NeilBrown67918752014-12-15 12:57:01 +11003750 err = mddev_lock(mddev);
3751 if (err)
3752 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003753 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003754 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003755 err = -EBUSY;
3756 else if (mddev->ro)
3757 err = -EROFS;
3758 else {
3759 mddev->new_chunk_sectors = n >> 9;
3760 err = mddev->pers->check_reshape(mddev);
3761 if (err)
3762 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003763 }
NeilBrownb3546032009-03-31 14:56:41 +11003764 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003765 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003766 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003767 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003768 }
NeilBrown67918752014-12-15 12:57:01 +11003769 mddev_unlock(mddev);
3770 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003771}
3772static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003773__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003774
NeilBrowna94213b2006-06-26 00:28:00 -07003775static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003776resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003777{
NeilBrownd1a7c502009-03-31 15:24:32 +11003778 if (mddev->recovery_cp == MaxSector)
3779 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003780 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3781}
3782
3783static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003784resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003785{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003786 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003787 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003788
3789 if (cmd_match(buf, "none"))
3790 n = MaxSector;
3791 else {
3792 err = kstrtoull(buf, 10, &n);
3793 if (err < 0)
3794 return err;
3795 if (n != (sector_t)n)
3796 return -EINVAL;
3797 }
NeilBrowna94213b2006-06-26 00:28:00 -07003798
NeilBrown67918752014-12-15 12:57:01 +11003799 err = mddev_lock(mddev);
3800 if (err)
3801 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003802 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003803 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003804
NeilBrown67918752014-12-15 12:57:01 +11003805 if (!err) {
3806 mddev->recovery_cp = n;
3807 if (mddev->pers)
3808 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3809 }
3810 mddev_unlock(mddev);
3811 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003812}
3813static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003814__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3815 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003816
NeilBrown9e653b62006-06-26 00:27:58 -07003817/*
3818 * The array state can be:
3819 *
3820 * clear
3821 * No devices, no size, no level
3822 * Equivalent to STOP_ARRAY ioctl
3823 * inactive
3824 * May have some settings, but array is not active
3825 * all IO results in error
3826 * When written, doesn't tear down array, but just stops it
3827 * suspended (not supported yet)
3828 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003829 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003830 * readonly
3831 * no resync can happen. no superblocks get written.
3832 * write requests fail
3833 * read-auto
3834 * like readonly, but behaves like 'clean' on a write request.
3835 *
3836 * clean - no pending writes, but otherwise active.
3837 * When written to inactive array, starts without resync
3838 * If a write request arrives then
3839 * if metadata is known, mark 'dirty' and switch to 'active'.
3840 * if not known, block and switch to write-pending
3841 * If written to an active array that has pending writes, then fails.
3842 * active
3843 * fully active: IO and resync can be happening.
3844 * When written to inactive array, starts with resync
3845 *
3846 * write-pending
3847 * clean, but writes are blocked waiting for 'active' to be written.
3848 *
3849 * active-idle
3850 * like active, but no writes have been seen for a while (100msec).
3851 *
3852 */
3853enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3854 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003855static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003856 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3857 "write-pending", "active-idle", NULL };
3858
3859static int match_word(const char *word, char **list)
3860{
3861 int n;
3862 for (n=0; list[n]; n++)
3863 if (cmd_match(word, list[n]))
3864 break;
3865 return n;
3866}
3867
3868static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003869array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003870{
3871 enum array_state st = inactive;
3872
3873 if (mddev->pers)
3874 switch(mddev->ro) {
3875 case 1:
3876 st = readonly;
3877 break;
3878 case 2:
3879 st = read_auto;
3880 break;
3881 case 0:
3882 if (mddev->in_sync)
3883 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003884 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003885 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003886 else if (mddev->safemode)
3887 st = active_idle;
3888 else
3889 st = active;
3890 }
3891 else {
3892 if (list_empty(&mddev->disks) &&
3893 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003894 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003895 st = clear;
3896 else
3897 st = inactive;
3898 }
3899 return sprintf(page, "%s\n", array_states[st]);
3900}
3901
NeilBrownf72ffdd2014-09-30 14:23:59 +10003902static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3903static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3904static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003905static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003906
3907static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003908array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003909{
NeilBrown67918752014-12-15 12:57:01 +11003910 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003911 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003912
3913 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3914 /* don't take reconfig_mutex when toggling between
3915 * clean and active
3916 */
3917 spin_lock(&mddev->lock);
3918 if (st == active) {
3919 restart_array(mddev);
3920 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3921 wake_up(&mddev->sb_wait);
3922 err = 0;
3923 } else /* st == clean */ {
3924 restart_array(mddev);
3925 if (atomic_read(&mddev->writes_pending) == 0) {
3926 if (mddev->in_sync == 0) {
3927 mddev->in_sync = 1;
3928 if (mddev->safemode == 1)
3929 mddev->safemode = 0;
3930 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3931 }
3932 err = 0;
3933 } else
3934 err = -EBUSY;
3935 }
3936 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003937 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003938 }
3939 err = mddev_lock(mddev);
3940 if (err)
3941 return err;
3942 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003943 switch(st) {
3944 case bad_word:
3945 break;
3946 case clear:
3947 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003948 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003949 break;
3950 case inactive:
3951 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003952 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003953 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003954 else
NeilBrowne6910632008-02-06 01:39:51 -08003955 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003956 break;
3957 case suspended:
3958 break; /* not supported yet */
3959 case readonly:
3960 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003961 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003962 else {
3963 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003964 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003965 err = do_md_run(mddev);
3966 }
3967 break;
3968 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003969 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003970 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003971 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003972 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003973 err = restart_array(mddev);
3974 if (err == 0) {
3975 mddev->ro = 2;
3976 set_disk_ro(mddev->gendisk, 0);
3977 }
NeilBrown9e653b62006-06-26 00:27:58 -07003978 } else {
3979 mddev->ro = 2;
3980 err = do_md_run(mddev);
3981 }
3982 break;
3983 case clean:
3984 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07003985 err = restart_array(mddev);
3986 if (err)
3987 break;
NeilBrown85572d72014-12-15 12:56:56 +11003988 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003989 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003990 if (mddev->in_sync == 0) {
3991 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003992 if (mddev->safemode == 1)
3993 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003994 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003995 }
3996 err = 0;
3997 } else
3998 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003999 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004000 } else
4001 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004002 break;
4003 case active:
4004 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004005 err = restart_array(mddev);
4006 if (err)
4007 break;
NeilBrown070dc6d2010-08-30 17:33:34 +10004008 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004009 wake_up(&mddev->sb_wait);
4010 err = 0;
4011 } else {
4012 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004013 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004014 err = do_md_run(mddev);
4015 }
4016 break;
4017 case write_pending:
4018 case active_idle:
4019 /* these cannot be set */
4020 break;
4021 }
NeilBrown67918752014-12-15 12:57:01 +11004022
4023 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004024 if (mddev->hold_active == UNTIL_IOCTL)
4025 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004026 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004027 }
NeilBrown67918752014-12-15 12:57:01 +11004028 mddev_unlock(mddev);
4029 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004030}
NeilBrown80ca3a42006-07-10 04:44:18 -07004031static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004032__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004033
NeilBrown6d7ff7382006-01-06 00:21:16 -08004034static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004035max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004036 return sprintf(page, "%d\n",
4037 atomic_read(&mddev->max_corr_read_errors));
4038}
4039
4040static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004041max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004042{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004043 unsigned int n;
4044 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004045
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004046 rv = kstrtouint(buf, 10, &n);
4047 if (rv < 0)
4048 return rv;
4049 atomic_set(&mddev->max_corr_read_errors, n);
4050 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004051}
4052
4053static struct md_sysfs_entry max_corr_read_errors =
4054__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4055 max_corrected_read_errors_store);
4056
4057static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004058null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004059{
4060 return -EINVAL;
4061}
4062
4063static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004064new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004065{
4066 /* buf must be %d:%d\n? giving major and minor numbers */
4067 /* The new device is added to the array.
4068 * If the array has a persistent superblock, we read the
4069 * superblock to initialise info and check validity.
4070 * Otherwise, only checking done is that in bind_rdev_to_array,
4071 * which mainly checks size.
4072 */
4073 char *e;
4074 int major = simple_strtoul(buf, &e, 10);
4075 int minor;
4076 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004077 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004078 int err;
4079
4080 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4081 return -EINVAL;
4082 minor = simple_strtoul(e+1, &e, 10);
4083 if (*e && *e != '\n')
4084 return -EINVAL;
4085 dev = MKDEV(major, minor);
4086 if (major != MAJOR(dev) ||
4087 minor != MINOR(dev))
4088 return -EOVERFLOW;
4089
NeilBrown67918752014-12-15 12:57:01 +11004090 flush_workqueue(md_misc_wq);
4091
4092 err = mddev_lock(mddev);
4093 if (err)
4094 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004095 if (mddev->persistent) {
4096 rdev = md_import_device(dev, mddev->major_version,
4097 mddev->minor_version);
4098 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004099 struct md_rdev *rdev0
4100 = list_entry(mddev->disks.next,
4101 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004102 err = super_types[mddev->major_version]
4103 .load_super(rdev, rdev0, mddev->minor_version);
4104 if (err < 0)
4105 goto out;
4106 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004107 } else if (mddev->external)
4108 rdev = md_import_device(dev, -2, -1);
4109 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004110 rdev = md_import_device(dev, -1, -1);
4111
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004112 if (IS_ERR(rdev)) {
4113 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004114 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004115 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004116 err = bind_rdev_to_array(rdev, mddev);
4117 out:
4118 if (err)
4119 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004120 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004121 return err ? err : len;
4122}
4123
4124static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004125__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004126
4127static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004128bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004129{
4130 char *end;
4131 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004132 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004133
NeilBrown67918752014-12-15 12:57:01 +11004134 err = mddev_lock(mddev);
4135 if (err)
4136 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004137 if (!mddev->bitmap)
4138 goto out;
4139 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4140 while (*buf) {
4141 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4142 if (buf == end) break;
4143 if (*end == '-') { /* range */
4144 buf = end + 1;
4145 end_chunk = simple_strtoul(buf, &end, 0);
4146 if (buf == end) break;
4147 }
4148 if (*end && !isspace(*end)) break;
4149 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004150 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004151 }
4152 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4153out:
NeilBrown67918752014-12-15 12:57:01 +11004154 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004155 return len;
4156}
4157
4158static struct md_sysfs_entry md_bitmap =
4159__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4160
4161static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004162size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004163{
Andre Noll58c0fed2009-03-31 14:33:13 +11004164 return sprintf(page, "%llu\n",
4165 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004166}
4167
NeilBrownfd01b882011-10-11 16:47:53 +11004168static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004169
4170static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004171size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004172{
4173 /* If array is inactive, we can reduce the component size, but
4174 * not increase it (except from 0).
4175 * If array is active, we can try an on-line resize
4176 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004177 sector_t sectors;
4178 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004179
Andre Noll58c0fed2009-03-31 14:33:13 +11004180 if (err < 0)
4181 return err;
NeilBrown67918752014-12-15 12:57:01 +11004182 err = mddev_lock(mddev);
4183 if (err)
4184 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004185 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004186 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004187 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004188 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004189 if (mddev->dev_sectors == 0 ||
4190 mddev->dev_sectors > sectors)
4191 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004192 else
4193 err = -ENOSPC;
4194 }
NeilBrown67918752014-12-15 12:57:01 +11004195 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004196 return err ? err : len;
4197}
4198
4199static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004200__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004201
Masanari Iida83f0d772012-10-30 00:18:08 +09004202/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004203 * This is one of
4204 * 'none' for arrays with no metadata (good luck...)
4205 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004206 * or N.M for internally known formats
4207 */
4208static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004209metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004210{
4211 if (mddev->persistent)
4212 return sprintf(page, "%d.%d\n",
4213 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004214 else if (mddev->external)
4215 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004216 else
4217 return sprintf(page, "none\n");
4218}
4219
4220static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004221metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004222{
4223 int major, minor;
4224 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004225 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004226 /* Changing the details of 'external' metadata is
4227 * always permitted. Otherwise there must be
4228 * no devices attached to the array.
4229 */
NeilBrown67918752014-12-15 12:57:01 +11004230
4231 err = mddev_lock(mddev);
4232 if (err)
4233 return err;
4234 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004235 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4236 ;
4237 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004238 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004239
NeilBrown67918752014-12-15 12:57:01 +11004240 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004241 if (cmd_match(buf, "none")) {
4242 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004243 mddev->external = 0;
4244 mddev->major_version = 0;
4245 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004246 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004247 }
4248 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004249 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004250 if (namelen >= sizeof(mddev->metadata_type))
4251 namelen = sizeof(mddev->metadata_type)-1;
4252 strncpy(mddev->metadata_type, buf+9, namelen);
4253 mddev->metadata_type[namelen] = 0;
4254 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4255 mddev->metadata_type[--namelen] = 0;
4256 mddev->persistent = 0;
4257 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004258 mddev->major_version = 0;
4259 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004260 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004261 }
4262 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004263 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004264 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004265 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004266 buf = e+1;
4267 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004268 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004269 goto out_unlock;
4270 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004271 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004272 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004273 mddev->major_version = major;
4274 mddev->minor_version = minor;
4275 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004276 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004277 err = 0;
4278out_unlock:
4279 mddev_unlock(mddev);
4280 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004281}
4282
4283static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004284__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004285
NeilBrowna35b0d62006-01-06 00:20:49 -08004286static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004287action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004288{
NeilBrown7eec3142005-11-08 21:39:44 -08004289 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004290 unsigned long recovery = mddev->recovery;
4291 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004292 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004293 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4294 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4295 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004296 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004297 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4298 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004299 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004300 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004301 type = "check";
4302 else
4303 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004304 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004305 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004306 else if (mddev->reshape_position != MaxSector)
4307 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004308 }
4309 return sprintf(page, "%s\n", type);
4310}
4311
4312static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004313action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004314{
NeilBrown7eec3142005-11-08 21:39:44 -08004315 if (!mddev->pers || !mddev->pers->sync_request)
4316 return -EINVAL;
4317
NeilBrownb6a9ce62009-05-26 09:41:17 +10004318
4319 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004320 if (cmd_match(page, "frozen"))
4321 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4322 else
4323 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004324 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4325 mddev_lock(mddev) == 0) {
4326 flush_workqueue(md_misc_wq);
4327 if (mddev->sync_thread) {
4328 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004329 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004330 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004331 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004332 }
NeilBrown312045e2015-12-21 11:01:21 +11004333 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004334 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004335 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004336 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004337 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004338 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004339 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004340 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004341 int err;
4342 if (mddev->pers->start_reshape == NULL)
4343 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004344 err = mddev_lock(mddev);
4345 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004346 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4347 err = -EBUSY;
4348 else {
4349 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4350 err = mddev->pers->start_reshape(mddev);
4351 }
NeilBrown67918752014-12-15 12:57:01 +11004352 mddev_unlock(mddev);
4353 }
NeilBrown16484bf2006-03-27 01:18:13 -08004354 if (err)
4355 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004356 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004357 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004358 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004359 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004360 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004361 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004362 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004363 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4364 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004365 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004366 if (mddev->ro == 2) {
4367 /* A write to sync_action is enough to justify
4368 * canceling read-auto mode
4369 */
4370 mddev->ro = 0;
4371 md_wakeup_thread(mddev->sync_thread);
4372 }
NeilBrown03c902e2006-01-06 00:20:46 -08004373 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004374 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004375 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004376 return len;
4377}
4378
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004379static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004380__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004381
4382static ssize_t
4383last_sync_action_show(struct mddev *mddev, char *page)
4384{
4385 return sprintf(page, "%s\n", mddev->last_sync_action);
4386}
4387
4388static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4389
NeilBrown9d888832005-11-08 21:39:26 -08004390static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004391mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004392{
4393 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004394 (unsigned long long)
4395 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004396}
4397
NeilBrown80ca3a42006-07-10 04:44:18 -07004398static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004399
NeilBrown88202a02006-01-06 00:21:36 -08004400static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004401sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004402{
4403 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4404 mddev->sync_speed_min ? "local": "system");
4405}
4406
4407static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004408sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004409{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004410 unsigned int min;
4411 int rv;
4412
NeilBrown88202a02006-01-06 00:21:36 -08004413 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004414 min = 0;
4415 } else {
4416 rv = kstrtouint(buf, 10, &min);
4417 if (rv < 0)
4418 return rv;
4419 if (min == 0)
4420 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004421 }
NeilBrown88202a02006-01-06 00:21:36 -08004422 mddev->sync_speed_min = min;
4423 return len;
4424}
4425
4426static struct md_sysfs_entry md_sync_min =
4427__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4428
4429static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004430sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004431{
4432 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4433 mddev->sync_speed_max ? "local": "system");
4434}
4435
4436static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004437sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004438{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004439 unsigned int max;
4440 int rv;
4441
NeilBrown88202a02006-01-06 00:21:36 -08004442 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004443 max = 0;
4444 } else {
4445 rv = kstrtouint(buf, 10, &max);
4446 if (rv < 0)
4447 return rv;
4448 if (max == 0)
4449 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004450 }
NeilBrown88202a02006-01-06 00:21:36 -08004451 mddev->sync_speed_max = max;
4452 return len;
4453}
4454
4455static struct md_sysfs_entry md_sync_max =
4456__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4457
Iustin Popd7f3d292007-10-16 23:30:54 -07004458static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004459degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004460{
4461 return sprintf(page, "%d\n", mddev->degraded);
4462}
4463static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004464
4465static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004466sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004467{
4468 return sprintf(page, "%d\n", mddev->parallel_resync);
4469}
4470
4471static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004472sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004473{
4474 long n;
4475
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004476 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004477 return -EINVAL;
4478
4479 if (n != 0 && n != 1)
4480 return -EINVAL;
4481
4482 mddev->parallel_resync = n;
4483
4484 if (mddev->sync_thread)
4485 wake_up(&resync_wait);
4486
4487 return len;
4488}
4489
4490/* force parallel resync, even with shared block devices */
4491static struct md_sysfs_entry md_sync_force_parallel =
4492__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4493 sync_force_parallel_show, sync_force_parallel_store);
4494
4495static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004496sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004497{
4498 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004499 if (mddev->curr_resync == 0)
4500 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004501 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4502 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004503 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004504 db = resync - mddev->resync_mark_cnt;
4505 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004506}
4507
NeilBrown80ca3a42006-07-10 04:44:18 -07004508static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004509
4510static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004511sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004512{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004513 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004514
NeilBrownacb180b2009-04-14 16:28:34 +10004515 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4516 return sprintf(page, "none\n");
4517
NeilBrown72f36d52012-10-11 14:25:57 +11004518 if (mddev->curr_resync == 1 ||
4519 mddev->curr_resync == 2)
4520 return sprintf(page, "delayed\n");
4521
NeilBrownc804cde2012-05-21 09:28:33 +10004522 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4523 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004524 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004525 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004526 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004527
NeilBrownacb180b2009-04-14 16:28:34 +10004528 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004529 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004530}
4531
NeilBrown750f1992014-09-30 08:53:05 +10004532static struct md_sysfs_entry md_sync_completed =
4533 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004534
NeilBrowne464eaf2006-03-27 01:18:14 -08004535static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004536min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004537{
4538 return sprintf(page, "%llu\n",
4539 (unsigned long long)mddev->resync_min);
4540}
4541static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004542min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004543{
4544 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004545 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004546
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004547 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004548 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004549
4550 spin_lock(&mddev->lock);
4551 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004552 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004553 goto out_unlock;
4554
4555 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004556 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004557 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004558
NeilBrown50c37b12015-03-23 17:36:38 +11004559 /* Round down to multiple of 4K for safety */
4560 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004561 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004562
NeilBrown23da4222014-12-15 12:57:01 +11004563out_unlock:
4564 spin_unlock(&mddev->lock);
4565 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004566}
4567
4568static struct md_sysfs_entry md_min_sync =
4569__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4570
4571static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004572max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004573{
4574 if (mddev->resync_max == MaxSector)
4575 return sprintf(page, "max\n");
4576 else
4577 return sprintf(page, "%llu\n",
4578 (unsigned long long)mddev->resync_max);
4579}
4580static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004581max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004582{
NeilBrown23da4222014-12-15 12:57:01 +11004583 int err;
4584 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004585 if (strncmp(buf, "max", 3) == 0)
4586 mddev->resync_max = MaxSector;
4587 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004588 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004589 int chunk;
4590
4591 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004592 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004593 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004594 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004595 goto out_unlock;
4596
4597 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004598 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004599 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004600 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004601 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004602
4603 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004604 chunk = mddev->chunk_sectors;
4605 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004606 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004607
4608 err = -EINVAL;
4609 if (sector_div(temp, chunk))
4610 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004611 }
4612 mddev->resync_max = max;
4613 }
4614 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004615 err = 0;
4616out_unlock:
4617 spin_unlock(&mddev->lock);
4618 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004619}
4620
4621static struct md_sysfs_entry md_max_sync =
4622__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4623
4624static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004625suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004626{
4627 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4628}
4629
4630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004631suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004632{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004633 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004634 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004635
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004636 err = kstrtoull(buf, 10, &new);
4637 if (err < 0)
4638 return err;
4639 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004640 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004641
NeilBrown67918752014-12-15 12:57:01 +11004642 err = mddev_lock(mddev);
4643 if (err)
4644 return err;
4645 err = -EINVAL;
4646 if (mddev->pers == NULL ||
4647 mddev->pers->quiesce == NULL)
4648 goto unlock;
4649 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004650 mddev->suspend_lo = new;
4651 if (new >= old)
4652 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004653 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004654 else {
4655 /* Expanding suspended region - need to wait */
4656 mddev->pers->quiesce(mddev, 1);
4657 mddev->pers->quiesce(mddev, 0);
4658 }
NeilBrown67918752014-12-15 12:57:01 +11004659 err = 0;
4660unlock:
4661 mddev_unlock(mddev);
4662 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004663}
4664static struct md_sysfs_entry md_suspend_lo =
4665__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4666
NeilBrowne464eaf2006-03-27 01:18:14 -08004667static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004668suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004669{
4670 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4671}
4672
4673static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004674suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004675{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004676 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004677 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004678
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004679 err = kstrtoull(buf, 10, &new);
4680 if (err < 0)
4681 return err;
4682 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004683 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004684
NeilBrown67918752014-12-15 12:57:01 +11004685 err = mddev_lock(mddev);
4686 if (err)
4687 return err;
4688 err = -EINVAL;
4689 if (mddev->pers == NULL ||
4690 mddev->pers->quiesce == NULL)
4691 goto unlock;
4692 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004693 mddev->suspend_hi = new;
4694 if (new <= old)
4695 /* Shrinking suspended region */
4696 mddev->pers->quiesce(mddev, 2);
4697 else {
4698 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004699 mddev->pers->quiesce(mddev, 1);
4700 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004701 }
NeilBrown67918752014-12-15 12:57:01 +11004702 err = 0;
4703unlock:
4704 mddev_unlock(mddev);
4705 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004706}
4707static struct md_sysfs_entry md_suspend_hi =
4708__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4709
NeilBrown08a02ec2007-05-09 02:35:38 -07004710static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004711reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004712{
4713 if (mddev->reshape_position != MaxSector)
4714 return sprintf(page, "%llu\n",
4715 (unsigned long long)mddev->reshape_position);
4716 strcpy(page, "none\n");
4717 return 5;
4718}
4719
4720static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004721reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004722{
NeilBrownc6563a82012-05-21 09:27:00 +10004723 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004724 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004725 int err;
NeilBrown67918752014-12-15 12:57:01 +11004726
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004727 err = kstrtoull(buf, 10, &new);
4728 if (err < 0)
4729 return err;
4730 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004731 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004732 err = mddev_lock(mddev);
4733 if (err)
4734 return err;
4735 err = -EBUSY;
4736 if (mddev->pers)
4737 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004738 mddev->reshape_position = new;
4739 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004740 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004741 mddev->new_level = mddev->level;
4742 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004743 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004744 rdev_for_each(rdev, mddev)
4745 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004746 err = 0;
4747unlock:
4748 mddev_unlock(mddev);
4749 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004750}
4751
4752static struct md_sysfs_entry md_reshape_position =
4753__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4754 reshape_position_store);
4755
Dan Williamsb522adc2009-03-31 15:00:31 +11004756static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004757reshape_direction_show(struct mddev *mddev, char *page)
4758{
4759 return sprintf(page, "%s\n",
4760 mddev->reshape_backwards ? "backwards" : "forwards");
4761}
4762
4763static ssize_t
4764reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4765{
4766 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004767 int err;
4768
NeilBrown2c810cd2012-05-21 09:27:00 +10004769 if (cmd_match(buf, "forwards"))
4770 backwards = 0;
4771 else if (cmd_match(buf, "backwards"))
4772 backwards = 1;
4773 else
4774 return -EINVAL;
4775 if (mddev->reshape_backwards == backwards)
4776 return len;
4777
NeilBrown67918752014-12-15 12:57:01 +11004778 err = mddev_lock(mddev);
4779 if (err)
4780 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004781 /* check if we are allowed to change */
4782 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004783 err = -EBUSY;
4784 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004785 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004786 err = -EINVAL;
4787 else
4788 mddev->reshape_backwards = backwards;
4789 mddev_unlock(mddev);
4790 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004791}
4792
4793static struct md_sysfs_entry md_reshape_direction =
4794__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4795 reshape_direction_store);
4796
4797static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004798array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004799{
4800 if (mddev->external_size)
4801 return sprintf(page, "%llu\n",
4802 (unsigned long long)mddev->array_sectors/2);
4803 else
4804 return sprintf(page, "default\n");
4805}
4806
4807static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004808array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004809{
4810 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004811 int err;
4812
4813 err = mddev_lock(mddev);
4814 if (err)
4815 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004816
4817 if (strncmp(buf, "default", 7) == 0) {
4818 if (mddev->pers)
4819 sectors = mddev->pers->size(mddev, 0, 0);
4820 else
4821 sectors = mddev->array_sectors;
4822
4823 mddev->external_size = 0;
4824 } else {
4825 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004826 err = -EINVAL;
4827 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4828 err = -E2BIG;
4829 else
4830 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004831 }
4832
NeilBrown67918752014-12-15 12:57:01 +11004833 if (!err) {
4834 mddev->array_sectors = sectors;
4835 if (mddev->pers) {
4836 set_capacity(mddev->gendisk, mddev->array_sectors);
4837 revalidate_disk(mddev->gendisk);
4838 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004839 }
NeilBrown67918752014-12-15 12:57:01 +11004840 mddev_unlock(mddev);
4841 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004842}
4843
4844static struct md_sysfs_entry md_array_size =
4845__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4846 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004847
NeilBrowneae17012005-11-08 21:39:23 -08004848static struct attribute *md_default_attrs[] = {
4849 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004850 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004851 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004852 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004853 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004854 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004855 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004856 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004857 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004858 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004859 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004860 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004861 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004862 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004863 NULL,
4864};
4865
4866static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004867 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004868 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004869 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004870 &md_sync_min.attr,
4871 &md_sync_max.attr,
4872 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004873 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004874 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004875 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004876 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004877 &md_suspend_lo.attr,
4878 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004879 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004880 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004881 NULL,
4882};
NeilBrown411036f2005-11-08 21:39:40 -08004883static struct attribute_group md_redundancy_group = {
4884 .name = NULL,
4885 .attrs = md_redundancy_attrs,
4886};
4887
NeilBrowneae17012005-11-08 21:39:23 -08004888static ssize_t
4889md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4890{
4891 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004892 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004893 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004894
4895 if (!entry->show)
4896 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004897 spin_lock(&all_mddevs_lock);
4898 if (list_empty(&mddev->all_mddevs)) {
4899 spin_unlock(&all_mddevs_lock);
4900 return -EBUSY;
4901 }
4902 mddev_get(mddev);
4903 spin_unlock(&all_mddevs_lock);
4904
NeilBrownb7b17c92014-12-15 12:56:59 +11004905 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004906 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004907 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004908}
4909
4910static ssize_t
4911md_attr_store(struct kobject *kobj, struct attribute *attr,
4912 const char *page, size_t length)
4913{
4914 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004915 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004916 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004917
4918 if (!entry->store)
4919 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004920 if (!capable(CAP_SYS_ADMIN))
4921 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004922 spin_lock(&all_mddevs_lock);
4923 if (list_empty(&mddev->all_mddevs)) {
4924 spin_unlock(&all_mddevs_lock);
4925 return -EBUSY;
4926 }
4927 mddev_get(mddev);
4928 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004929 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004930 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004931 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004932}
4933
4934static void md_free(struct kobject *ko)
4935{
NeilBrownfd01b882011-10-11 16:47:53 +11004936 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004937
4938 if (mddev->sysfs_state)
4939 sysfs_put(mddev->sysfs_state);
4940
NeilBrown6cd18e72015-04-27 14:12:22 +10004941 if (mddev->queue)
4942 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004943 if (mddev->gendisk) {
4944 del_gendisk(mddev->gendisk);
4945 put_disk(mddev->gendisk);
4946 }
NeilBrowna21d1502009-01-09 08:31:09 +11004947
NeilBrowneae17012005-11-08 21:39:23 -08004948 kfree(mddev);
4949}
4950
Emese Revfy52cf25d2010-01-19 02:58:23 +01004951static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004952 .show = md_attr_show,
4953 .store = md_attr_store,
4954};
4955static struct kobj_type md_ktype = {
4956 .release = md_free,
4957 .sysfs_ops = &md_sysfs_ops,
4958 .default_attrs = md_default_attrs,
4959};
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961int mdp_major = 0;
4962
Dan Williams5fd3a172009-03-04 00:57:25 -07004963static void mddev_delayed_delete(struct work_struct *ws)
4964{
NeilBrownfd01b882011-10-11 16:47:53 +11004965 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004966
NeilBrown43a70502009-12-14 12:49:55 +11004967 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004968 kobject_del(&mddev->kobj);
4969 kobject_put(&mddev->kobj);
4970}
4971
NeilBrownefeb53c2009-01-09 08:31:10 +11004972static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004974 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004975 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004977 int partitioned;
4978 int shift;
4979 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004980 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
4982 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004983 return -ENODEV;
4984
4985 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4986 shift = partitioned ? MdpMinorShift : 0;
4987 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
Tejun Heoe804ac72010-10-15 15:36:08 +02004989 /* wait for any previous instance of this device to be
4990 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004991 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004992 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004993
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004994 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004995 error = -EEXIST;
4996 if (mddev->gendisk)
4997 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004998
4999 if (name) {
5000 /* Need to ensure that 'name' is not a duplicate.
5001 */
NeilBrownfd01b882011-10-11 16:47:53 +11005002 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005003 spin_lock(&all_mddevs_lock);
5004
5005 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5006 if (mddev2->gendisk &&
5007 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5008 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005009 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005010 }
5011 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 }
NeilBrown8b765392009-01-09 08:31:08 +11005013
NeilBrown0909dc42009-07-01 12:27:21 +10005014 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005015 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005016 if (!mddev->queue)
5017 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005018 mddev->queue->queuedata = mddev;
5019
NeilBrown409c57f2009-03-31 14:39:39 +11005020 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005021 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 disk = alloc_disk(1 << shift);
5024 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005025 blk_cleanup_queue(mddev->queue);
5026 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005027 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005029 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005031 if (name)
5032 strcpy(disk->disk_name, name);
5033 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005035 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 disk->fops = &md_fops;
5038 disk->private_data = mddev;
5039 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10005040 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11005041 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005042 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005043 * remove it now.
5044 */
5045 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005047 /* As soon as we call add_disk(), another thread could get
5048 * through to md_open, so make sure it doesn't get too far
5049 */
5050 mutex_lock(&mddev->open_mutex);
5051 add_disk(disk);
5052
Tejun Heoed9e1982008-08-25 19:56:05 +09005053 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
5054 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005055 if (error) {
5056 /* This isn't possible, but as kobject_init_and_add is marked
5057 * __must_check, we must do something with the result
5058 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005059 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5060 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005061 error = 0;
5062 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005063 if (mddev->kobj.sd &&
5064 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11005065 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005066 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005067 abort:
5068 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005069 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005070 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005071 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005072 }
NeilBrownd3374822009-01-09 08:31:10 +11005073 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005074 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005075}
5076
5077static struct kobject *md_probe(dev_t dev, int *part, void *data)
5078{
5079 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 return NULL;
5081}
5082
NeilBrownefeb53c2009-01-09 08:31:10 +11005083static int add_named_array(const char *val, struct kernel_param *kp)
5084{
5085 /* val must be "md_*" where * is not all digits.
5086 * We allocate an array with a large free minor number, and
5087 * set the name to val. val must not already be an active name.
5088 */
5089 int len = strlen(val);
5090 char buf[DISK_NAME_LEN];
5091
5092 while (len && val[len-1] == '\n')
5093 len--;
5094 if (len >= DISK_NAME_LEN)
5095 return -E2BIG;
5096 strlcpy(buf, val, len+1);
5097 if (strncmp(buf, "md_", 3) != 0)
5098 return -EINVAL;
5099 return md_alloc(0, buf);
5100}
5101
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102static void md_safemode_timeout(unsigned long data)
5103{
NeilBrownfd01b882011-10-11 16:47:53 +11005104 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
Neil Brown0fd62b82008-06-28 08:31:36 +10005106 if (!atomic_read(&mddev->writes_pending)) {
5107 mddev->safemode = 1;
5108 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005109 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 md_wakeup_thread(mddev->thread);
5112}
5113
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005114static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
NeilBrownfd01b882011-10-11 16:47:53 +11005116int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117{
NeilBrown2604b702006-01-06 00:20:36 -08005118 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005119 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005120 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
NeilBrowna757e642005-04-16 15:26:42 -07005122 if (list_empty(&mddev->disks))
5123 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
5126 if (mddev->pers)
5127 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005128 /* Cannot run until previous stop completes properly */
5129 if (mddev->sysfs_active)
5130 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005131
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 /*
5133 * Analyze all RAID superblock(s)
5134 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005135 if (!mddev->raid_disks) {
5136 if (!mddev->persistent)
5137 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005138 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
NeilBrownd9d166c2006-01-06 00:20:51 -08005141 if (mddev->level != LEVEL_NONE)
5142 request_module("md-level-%d", mddev->level);
5143 else if (mddev->clevel[0])
5144 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
5146 /*
5147 * Drop all container device buffers, from now on
5148 * the only valid external interface is through the md
5149 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005151 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005152 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 continue;
5154 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005155 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005156
5157 /* perform some consistency tests on the device.
5158 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005159 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005160 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005161 if (rdev->meta_bdev) {
5162 /* Nothing to check */;
5163 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005164 if (mddev->dev_sectors &&
5165 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005166 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005167 printk("md: %s: data overlaps metadata\n",
5168 mdname(mddev));
5169 return -EINVAL;
5170 }
5171 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005172 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005173 > rdev->data_offset) {
5174 printk("md: %s: metadata overlaps data\n",
5175 mdname(mddev));
5176 return -EINVAL;
5177 }
5178 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005179 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 }
5181
NeilBrowna167f662010-10-26 18:31:13 +11005182 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005183 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005184
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005186 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005187 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005189 if (mddev->level != LEVEL_NONE)
5190 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5191 mddev->level);
5192 else
5193 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5194 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 return -EINVAL;
5196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005198 if (mddev->level != pers->level) {
5199 mddev->level = pers->level;
5200 mddev->new_level = pers->level;
5201 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005202 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
NeilBrownf6705572006-03-27 01:18:11 -08005204 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005205 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005206 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005207 module_put(pers->owner);
5208 return -EINVAL;
5209 }
5210
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005211 if (pers->sync_request) {
5212 /* Warn if this is a potentially silly
5213 * configuration.
5214 */
5215 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005216 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005217 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005218
NeilBrowndafb20f2012-03-19 12:46:39 +11005219 rdev_for_each(rdev, mddev)
5220 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005221 if (rdev < rdev2 &&
5222 rdev->bdev->bd_contains ==
5223 rdev2->bdev->bd_contains) {
5224 printk(KERN_WARNING
5225 "%s: WARNING: %s appears to be"
5226 " on the same physical disk as"
5227 " %s.\n",
5228 mdname(mddev),
5229 bdevname(rdev->bdev,b),
5230 bdevname(rdev2->bdev,b2));
5231 warned = 1;
5232 }
5233 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005234
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005235 if (warned)
5236 printk(KERN_WARNING
5237 "True protection against single-disk"
5238 " failure might be compromised.\n");
5239 }
5240
NeilBrown657390d2005-08-26 18:34:16 -07005241 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005242 /* may be over-ridden by personality */
5243 mddev->resync_max_sectors = mddev->dev_sectors;
5244
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005245 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
NeilBrown0f9552b52009-12-30 12:08:50 +11005247 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005248 mddev->ro = 2; /* read-only, but switch on first write */
5249
NeilBrown36d091f2014-12-15 12:56:58 +11005250 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005251 if (err)
5252 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005253 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005254 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5255 " but 'external_size' not in effect?\n", __func__);
5256 printk(KERN_ERR
5257 "md: invalid array_size %llu > default size %llu\n",
5258 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005259 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005260 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005261 }
NeilBrown36d091f2014-12-15 12:56:58 +11005262 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005263 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005264 struct bitmap *bitmap;
5265
5266 bitmap = bitmap_create(mddev, -1);
5267 if (IS_ERR(bitmap)) {
5268 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005269 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5270 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005271 } else
5272 mddev->bitmap = bitmap;
5273
NeilBrownb15c2e52006-01-06 00:20:16 -08005274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005276 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005277 if (mddev->private)
5278 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005279 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005280 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005281 bitmap_destroy(mddev);
5282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 }
NeilBrown5c675f82014-12-15 12:56:56 +11005284 if (mddev->queue) {
5285 mddev->queue->backing_dev_info.congested_data = mddev;
5286 mddev->queue->backing_dev_info.congested_fn = md_congested;
5287 }
NeilBrown36d091f2014-12-15 12:56:58 +11005288 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005289 if (mddev->kobj.sd &&
5290 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005291 printk(KERN_WARNING
5292 "md: cannot register extra attributes for %s\n",
5293 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005294 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005295 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005296 mddev->ro = 0;
5297
NeilBrownf72ffdd2014-09-30 14:23:59 +10005298 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005299 atomic_set(&mddev->max_corr_read_errors,
5300 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005302 if (mddev_is_clustered(mddev))
5303 mddev->safemode_delay = 0;
5304 else
5305 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005307 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005308 spin_lock(&mddev->lock);
5309 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005310 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005311 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005312 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005313 if (rdev->raid_disk >= 0)
5314 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005315 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005316
NeilBrowna4a3d262015-07-17 11:57:30 +10005317 if (mddev->degraded && !mddev->ro)
5318 /* This ensures that recovering status is reported immediately
5319 * via sysfs - until a lack of spares is confirmed.
5320 */
5321 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005323
NeilBrown7a0a5352013-08-27 16:28:23 +10005324 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005325 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
NeilBrownd7603b72006-01-06 00:20:30 -08005327 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005328 sysfs_notify_dirent_safe(mddev->sysfs_state);
5329 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005330 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 return 0;
5332}
NeilBrown390ee602010-06-01 19:37:27 +10005333EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
NeilBrownfd01b882011-10-11 16:47:53 +11005335static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005336{
5337 int err;
5338
5339 err = md_run(mddev);
5340 if (err)
5341 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005342 err = bitmap_load(mddev);
5343 if (err) {
5344 bitmap_destroy(mddev);
5345 goto out;
5346 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005347
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005348 if (mddev_is_clustered(mddev))
5349 md_allow_write(mddev);
5350
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005351 md_wakeup_thread(mddev->thread);
5352 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5353
NeilBrownfe60b012010-03-29 11:10:42 +11005354 set_capacity(mddev->gendisk, mddev->array_sectors);
5355 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005356 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005357 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5358out:
5359 return err;
5360}
5361
NeilBrownfd01b882011-10-11 16:47:53 +11005362static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363{
5364 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Andre Noll80fab1d2008-07-11 22:02:21 +10005366 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005368 return -ENXIO;
5369 if (!mddev->pers)
5370 return -EINVAL;
5371 if (!mddev->ro)
5372 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005373 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5374 struct md_rdev *rdev;
5375 bool has_journal = false;
5376
5377 rcu_read_lock();
5378 rdev_for_each_rcu(rdev, mddev) {
5379 if (test_bit(Journal, &rdev->flags) &&
5380 !test_bit(Faulty, &rdev->flags)) {
5381 has_journal = true;
5382 break;
5383 }
5384 }
5385 rcu_read_unlock();
5386
5387 /* Don't restart rw with journal missing/faulty */
5388 if (!has_journal)
5389 return -EINVAL;
5390 }
5391
Andre Noll80fab1d2008-07-11 22:02:21 +10005392 mddev->safemode = 0;
5393 mddev->ro = 0;
5394 set_disk_ro(disk, 0);
5395 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5396 mdname(mddev));
5397 /* Kick recovery or resync if necessary */
5398 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5399 md_wakeup_thread(mddev->thread);
5400 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005401 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403}
5404
NeilBrownfd01b882011-10-11 16:47:53 +11005405static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005406{
5407 mddev->array_sectors = 0;
5408 mddev->external_size = 0;
5409 mddev->dev_sectors = 0;
5410 mddev->raid_disks = 0;
5411 mddev->recovery_cp = 0;
5412 mddev->resync_min = 0;
5413 mddev->resync_max = MaxSector;
5414 mddev->reshape_position = MaxSector;
5415 mddev->external = 0;
5416 mddev->persistent = 0;
5417 mddev->level = LEVEL_NONE;
5418 mddev->clevel[0] = 0;
5419 mddev->flags = 0;
5420 mddev->ro = 0;
5421 mddev->metadata_type[0] = 0;
5422 mddev->chunk_sectors = 0;
5423 mddev->ctime = mddev->utime = 0;
5424 mddev->layout = 0;
5425 mddev->max_disks = 0;
5426 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005427 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005428 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005429 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005430 mddev->new_level = LEVEL_NONE;
5431 mddev->new_layout = 0;
5432 mddev->new_chunk_sectors = 0;
5433 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005434 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005435 mddev->suspend_lo = mddev->suspend_hi = 0;
5436 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5437 mddev->recovery = 0;
5438 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005439 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005440 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005441 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005442 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005443 mddev->bitmap_info.offset = 0;
5444 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005445 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005446 mddev->bitmap_info.chunksize = 0;
5447 mddev->bitmap_info.daemon_sleep = 0;
5448 mddev->bitmap_info.max_write_behind = 0;
5449}
5450
NeilBrownfd01b882011-10-11 16:47:53 +11005451static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005452{
NeilBrown6b6204e2013-05-09 09:48:30 +10005453 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005454 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005455 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005456 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005457 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005458 }
5459
5460 del_timer_sync(&mddev->safemode_timer);
5461
5462 bitmap_flush(mddev);
5463 md_super_wait(mddev);
5464
NeilBrownb6d428c2013-04-24 11:42:42 +10005465 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005466 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5467 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005468 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005469 if (!mddev_is_clustered(mddev))
5470 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005471 md_update_sb(mddev, 1);
5472 }
5473}
NeilBrowndefad612011-01-14 09:14:33 +11005474
NeilBrownfd01b882011-10-11 16:47:53 +11005475void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005476{
NeilBrown29f097c2013-11-14 17:54:51 +11005477 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005478 __md_stop_writes(mddev);
5479 mddev_unlock(mddev);
5480}
NeilBrown390ee602010-06-01 19:37:27 +10005481EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005482
NeilBrown5aa61f42014-12-15 12:56:57 +11005483static void mddev_detach(struct mddev *mddev)
5484{
5485 struct bitmap *bitmap = mddev->bitmap;
5486 /* wait for behind writes to complete */
5487 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5488 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5489 mdname(mddev));
5490 /* need to kick something here to make sure I/O goes? */
5491 wait_event(bitmap->behind_wait,
5492 atomic_read(&bitmap->behind_writes) == 0);
5493 }
NeilBrown36d091f2014-12-15 12:56:58 +11005494 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005495 mddev->pers->quiesce(mddev, 1);
5496 mddev->pers->quiesce(mddev, 0);
5497 }
5498 md_unregister_thread(&mddev->thread);
5499 if (mddev->queue)
5500 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5501}
5502
NeilBrown5eff3c42012-11-19 10:47:48 +11005503static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005504{
NeilBrown36d091f2014-12-15 12:56:58 +11005505 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005506 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005507 /* Ensure ->event_work is done */
5508 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005509 spin_lock(&mddev->lock);
5510 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005511 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005512 spin_unlock(&mddev->lock);
5513 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005514 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005515 if (pers->sync_request && mddev->to_remove == NULL)
5516 mddev->to_remove = &md_redundancy_group;
5517 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005518 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005519}
NeilBrown5eff3c42012-11-19 10:47:48 +11005520
5521void md_stop(struct mddev *mddev)
5522{
5523 /* stop the array and free an attached data structures.
5524 * This is called from dm-raid
5525 */
5526 __md_stop(mddev);
5527 bitmap_destroy(mddev);
5528 if (mddev->bio_set)
5529 bioset_free(mddev->bio_set);
5530}
5531
NeilBrown390ee602010-06-01 19:37:27 +10005532EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005533
NeilBrowna05b7ea2012-07-19 15:59:18 +10005534static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005535{
5536 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005537 int did_freeze = 0;
5538
5539 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5540 did_freeze = 1;
5541 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5542 md_wakeup_thread(mddev->thread);
5543 }
NeilBrownf851b602014-12-11 10:02:10 +11005544 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005545 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005546 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005547 /* Thread might be blocked waiting for metadata update
5548 * which will now never happen */
5549 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005550
NeilBrown88724bf2015-09-24 14:00:51 +10005551 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5552 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005553 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005554 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5555 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005556 wait_event(mddev->sb_wait,
5557 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005558 mddev_lock_nointr(mddev);
5559
NeilBrowna4bd82d2010-03-29 13:23:10 +11005560 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005561 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005562 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005563 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005564 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005565 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005566 if (did_freeze) {
5567 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005568 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005569 md_wakeup_thread(mddev->thread);
5570 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005571 err = -EBUSY;
5572 goto out;
5573 }
5574 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005575 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005576
5577 err = -ENXIO;
5578 if (mddev->ro==1)
5579 goto out;
5580 mddev->ro = 1;
5581 set_disk_ro(mddev->gendisk, 1);
5582 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005583 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5584 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005585 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005586 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005587 }
5588out:
5589 mutex_unlock(&mddev->open_mutex);
5590 return err;
5591}
5592
NeilBrown9e653b62006-06-26 00:27:58 -07005593/* mode:
5594 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005595 * 2 - stop but do not disassemble array
5596 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005597static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005598 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005601 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005602 int did_freeze = 0;
5603
5604 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5605 did_freeze = 1;
5606 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5607 md_wakeup_thread(mddev->thread);
5608 }
NeilBrownf851b602014-12-11 10:02:10 +11005609 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005610 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005611 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005612 /* Thread might be blocked waiting for metadata update
5613 * which will now never happen */
5614 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005615
NeilBrown30b8feb2013-11-14 15:16:17 +11005616 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005617 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5618 !test_bit(MD_RECOVERY_RUNNING,
5619 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005620 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
NeilBrownc8c00a62009-08-10 12:50:52 +10005622 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005623 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005624 mddev->sysfs_active ||
5625 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005626 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005627 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005628 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005629 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005630 if (did_freeze) {
5631 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005632 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005633 md_wakeup_thread(mddev->thread);
5634 }
NeilBrown260fa032013-08-27 16:44:13 +10005635 return -EBUSY;
5636 }
NeilBrown6e17b022010-08-07 21:41:19 +10005637 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005638 if (mddev->ro)
5639 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005640
NeilBrowndefad612011-01-14 09:14:33 +11005641 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005642 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005643 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005644
NeilBrowna4bd82d2010-03-29 13:23:10 +11005645 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005646 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005647
NeilBrowndafb20f2012-03-19 12:46:39 +11005648 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005649 if (rdev->raid_disk >= 0)
5650 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005651
NeilBrowna4bd82d2010-03-29 13:23:10 +11005652 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005653 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005654 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005655 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005656
NeilBrowna4bd82d2010-03-29 13:23:10 +11005657 if (mddev->ro)
5658 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005659 } else
5660 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 /*
5662 * Free resources if final stop
5663 */
NeilBrown9e653b62006-06-26 00:27:58 -07005664 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5666
NeilBrown978f9462006-02-02 14:28:05 -08005667 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005668 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005669 struct file *f = mddev->bitmap_info.file;
5670 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005671 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005672 spin_unlock(&mddev->lock);
5673 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005674 }
NeilBrownc3d97142009-12-14 12:49:52 +11005675 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005676
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 export_array(mddev);
5678
NeilBrown6177b472010-03-29 11:37:13 +11005679 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005680 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005681 if (mddev->hold_active == UNTIL_STOP)
5682 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005683 }
NeilBrownd7603b72006-01-06 00:20:30 -08005684 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005685 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687}
5688
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005689#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005690static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691{
NeilBrown3cb03002011-10-11 16:45:26 +11005692 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 int err;
5694
NeilBrowna757e642005-04-16 15:26:42 -07005695 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 printk(KERN_INFO "md: running: ");
5699
NeilBrowndafb20f2012-03-19 12:46:39 +11005700 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 char b[BDEVNAME_SIZE];
5702 printk("<%s>", bdevname(rdev->bdev,b));
5703 }
5704 printk("\n");
5705
NeilBrownd710e132008-10-13 11:55:12 +11005706 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 if (err) {
5708 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005709 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 }
5711}
5712
5713/*
5714 * lets try to run arrays based on all disks that have arrived
5715 * until now. (those are in pending_raid_disks)
5716 *
5717 * the method: pick the first pending disk, collect all disks with
5718 * the same UUID, remove all from the pending list and put them into
5719 * the 'same_array' list. Then order this list based on superblock
5720 * update time (freshest comes first), kick out 'old' disks and
5721 * compare superblocks. If everything's fine then run it.
5722 *
5723 * If "unit" is allocated, then bump its reference count
5724 */
5725static void autorun_devices(int part)
5726{
NeilBrown3cb03002011-10-11 16:45:26 +11005727 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005728 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 char b[BDEVNAME_SIZE];
5730
5731 printk(KERN_INFO "md: autorun ...\n");
5732 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005733 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005735 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005737 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
5739 printk(KERN_INFO "md: considering %s ...\n",
5740 bdevname(rdev0->bdev,b));
5741 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005742 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 if (super_90_load(rdev, rdev0, 0) >= 0) {
5744 printk(KERN_INFO "md: adding %s ...\n",
5745 bdevname(rdev->bdev,b));
5746 list_move(&rdev->same_set, &candidates);
5747 }
5748 /*
5749 * now we have a set of devices, with all of them having
5750 * mostly sane superblocks. It's time to allocate the
5751 * mddev.
5752 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005753 if (part) {
5754 dev = MKDEV(mdp_major,
5755 rdev0->preferred_minor << MdpMinorShift);
5756 unit = MINOR(dev) >> MdpMinorShift;
5757 } else {
5758 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5759 unit = MINOR(dev);
5760 }
5761 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5763 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5764 break;
5765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767 md_probe(dev, NULL, NULL);
5768 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005769 if (!mddev || !mddev->gendisk) {
5770 if (mddev)
5771 mddev_put(mddev);
5772 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 "md: cannot allocate memory for md drive.\n");
5774 break;
5775 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005776 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 printk(KERN_WARNING "md: %s locked, cannot run\n",
5778 mdname(mddev));
5779 else if (mddev->raid_disks || mddev->major_version
5780 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005781 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 "md: %s already running, cannot run %s\n",
5783 mdname(mddev), bdevname(rdev0->bdev,b));
5784 mddev_unlock(mddev);
5785 } else {
5786 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005787 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005788 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 list_del_init(&rdev->same_set);
5790 if (bind_rdev_to_array(rdev, mddev))
5791 export_rdev(rdev);
5792 }
5793 autorun_array(mddev);
5794 mddev_unlock(mddev);
5795 }
5796 /* on success, candidates will be empty, on error
5797 * it won't...
5798 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005799 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005800 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 mddev_put(mddev);
5804 }
5805 printk(KERN_INFO "md: ... autorun DONE.\n");
5806}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005807#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808
NeilBrownf72ffdd2014-09-30 14:23:59 +10005809static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810{
5811 mdu_version_t ver;
5812
5813 ver.major = MD_MAJOR_VERSION;
5814 ver.minor = MD_MINOR_VERSION;
5815 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5816
5817 if (copy_to_user(arg, &ver, sizeof(ver)))
5818 return -EFAULT;
5819
5820 return 0;
5821}
5822
NeilBrownf72ffdd2014-09-30 14:23:59 +10005823static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824{
5825 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005826 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005827 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
NeilBrown1ca69c42012-10-11 13:37:33 +11005829 nr = working = insync = failed = spare = 0;
5830 rcu_read_lock();
5831 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005833 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 failed++;
5835 else {
5836 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005837 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005838 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 else
5840 spare++;
5841 }
5842 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005843 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844
5845 info.major_version = mddev->major_version;
5846 info.minor_version = mddev->minor_version;
5847 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005848 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005850 info.size = mddev->dev_sectors / 2;
5851 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005852 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 info.nr_disks = nr;
5854 info.raid_disks = mddev->raid_disks;
5855 info.md_minor = mddev->md_minor;
5856 info.not_persistent= !mddev->persistent;
5857
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11005858 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 info.state = 0;
5860 if (mddev->in_sync)
5861 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005862 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005863 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005864 if (mddev_is_clustered(mddev))
5865 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005866 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 info.working_disks = working;
5868 info.failed_disks = failed;
5869 info.spare_disks = spare;
5870
5871 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005872 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
5874 if (copy_to_user(arg, &info, sizeof(info)))
5875 return -EFAULT;
5876
5877 return 0;
5878}
5879
NeilBrownf72ffdd2014-09-30 14:23:59 +10005880static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005881{
5882 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005883 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005884 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005885
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005886 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005887 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005888 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005889
NeilBrown32a76272005-06-21 17:17:14 -07005890 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005891 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005892 /* bitmap enabled */
5893 if (mddev->bitmap_info.file) {
5894 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5895 sizeof(file->pathname));
5896 if (IS_ERR(ptr))
5897 err = PTR_ERR(ptr);
5898 else
5899 memmove(file->pathname, ptr,
5900 sizeof(file->pathname)-(ptr-file->pathname));
5901 }
NeilBrown4af1a042014-12-15 12:57:00 +11005902 spin_unlock(&mddev->lock);
5903
5904 if (err == 0 &&
5905 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005906 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005907
NeilBrown32a76272005-06-21 17:17:14 -07005908 kfree(file);
5909 return err;
5910}
5911
NeilBrownf72ffdd2014-09-30 14:23:59 +10005912static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913{
5914 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005915 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
5917 if (copy_from_user(&info, arg, sizeof(info)))
5918 return -EFAULT;
5919
NeilBrown1ca69c42012-10-11 13:37:33 +11005920 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005921 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 if (rdev) {
5923 info.major = MAJOR(rdev->bdev->bd_dev);
5924 info.minor = MINOR(rdev->bdev->bd_dev);
5925 info.raid_disk = rdev->raid_disk;
5926 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005927 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005929 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 info.state |= (1<<MD_DISK_ACTIVE);
5931 info.state |= (1<<MD_DISK_SYNC);
5932 }
Shaohua Li9efdca12015-10-12 16:59:50 -07005933 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07005934 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005935 if (test_bit(WriteMostly, &rdev->flags))
5936 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 } else {
5938 info.major = info.minor = 0;
5939 info.raid_disk = -1;
5940 info.state = (1<<MD_DISK_REMOVED);
5941 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005942 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943
5944 if (copy_to_user(arg, &info, sizeof(info)))
5945 return -EFAULT;
5946
5947 return 0;
5948}
5949
NeilBrownf72ffdd2014-09-30 14:23:59 +10005950static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951{
5952 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005953 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 dev_t dev = MKDEV(info->major,info->minor);
5955
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005956 if (mddev_is_clustered(mddev) &&
5957 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005958 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005959 mdname(mddev));
5960 return -EINVAL;
5961 }
5962
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5964 return -EOVERFLOW;
5965
5966 if (!mddev->raid_disks) {
5967 int err;
5968 /* expecting a device which has a superblock */
5969 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5970 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005971 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 "md: md_import_device returned %ld\n",
5973 PTR_ERR(rdev));
5974 return PTR_ERR(rdev);
5975 }
5976 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005977 struct md_rdev *rdev0
5978 = list_entry(mddev->disks.next,
5979 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005980 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 .load_super(rdev, rdev0, mddev->minor_version);
5982 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005983 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005985 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 bdevname(rdev0->bdev,b2));
5987 export_rdev(rdev);
5988 return -EINVAL;
5989 }
5990 }
5991 err = bind_rdev_to_array(rdev, mddev);
5992 if (err)
5993 export_rdev(rdev);
5994 return err;
5995 }
5996
5997 /*
5998 * add_new_disk can be used once the array is assembled
5999 * to add "hot spares". They must already have a superblock
6000 * written
6001 */
6002 if (mddev->pers) {
6003 int err;
6004 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006005 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 "%s: personality does not support diskops!\n",
6007 mdname(mddev));
6008 return -EINVAL;
6009 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006010 if (mddev->persistent)
6011 rdev = md_import_device(dev, mddev->major_version,
6012 mddev->minor_version);
6013 else
6014 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006016 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 "md: md_import_device returned %ld\n",
6018 PTR_ERR(rdev));
6019 return PTR_ERR(rdev);
6020 }
NeilBrown1a855a02010-12-09 16:36:28 +11006021 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006022 if (!mddev->persistent) {
6023 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006024 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006025 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006026 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006027 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006028 } else
NeilBrown41158c72005-06-21 17:17:25 -07006029 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006030 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006031 } else
6032 super_types[mddev->major_version].
6033 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006034 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006035 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006036 /* This was a hot-add request, but events doesn't
6037 * match, so reject it.
6038 */
6039 export_rdev(rdev);
6040 return -EINVAL;
6041 }
6042
NeilBrownb2d444d2005-11-08 21:39:31 -08006043 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006044 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6045 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006046 else
6047 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006048
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006049 if (info->state & (1<<MD_DISK_JOURNAL)) {
6050 struct md_rdev *rdev2;
6051 bool has_journal = false;
6052
6053 /* make sure no existing journal disk */
6054 rdev_for_each(rdev2, mddev) {
6055 if (test_bit(Journal, &rdev2->flags)) {
6056 has_journal = true;
6057 break;
6058 }
6059 }
6060 if (has_journal) {
6061 export_rdev(rdev);
6062 return -EBUSY;
6063 }
Song Liubac624f2015-08-13 14:31:55 -07006064 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006065 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006066 /*
6067 * check whether the device shows up in other nodes
6068 */
6069 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006070 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006071 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006072 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006073 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006074 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006075 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006076 export_rdev(rdev);
6077 return err;
6078 }
6079 }
6080 }
6081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 rdev->raid_disk = -1;
6083 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 if (err)
6086 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006087
6088 if (mddev_is_clustered(mddev)) {
6089 if (info->state & (1 << MD_DISK_CANDIDATE))
6090 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6091 else {
6092 if (err)
6093 md_cluster_ops->add_new_disk_cancel(mddev);
6094 else
6095 err = add_bound_rdev(rdev);
6096 }
6097
6098 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006099 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006100
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 return err;
6102 }
6103
6104 /* otherwise, add_new_disk is only allowed
6105 * for major_version==0 superblocks
6106 */
6107 if (mddev->major_version != 0) {
6108 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6109 mdname(mddev));
6110 return -EINVAL;
6111 }
6112
6113 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6114 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006115 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006117 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 "md: error, md_import_device() returned %ld\n",
6119 PTR_ERR(rdev));
6120 return PTR_ERR(rdev);
6121 }
6122 rdev->desc_nr = info->number;
6123 if (info->raid_disk < mddev->raid_disks)
6124 rdev->raid_disk = info->raid_disk;
6125 else
6126 rdev->raid_disk = -1;
6127
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006129 if (info->state & (1<<MD_DISK_SYNC))
6130 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006132 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6133 set_bit(WriteMostly, &rdev->flags);
6134
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 if (!mddev->persistent) {
6136 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006137 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6138 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006139 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006140 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
NeilBrown2bf071b2006-01-06 00:20:55 -08006142 err = bind_rdev_to_array(rdev, mddev);
6143 if (err) {
6144 export_rdev(rdev);
6145 return err;
6146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 }
6148
6149 return 0;
6150}
6151
NeilBrownf72ffdd2014-09-30 14:23:59 +10006152static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153{
6154 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006155 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 rdev = find_rdev(mddev, dev);
6158 if (!rdev)
6159 return -ENXIO;
6160
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006161 if (rdev->raid_disk < 0)
6162 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006163
NeilBrown3ea8929d2013-04-24 11:42:41 +10006164 clear_bit(Blocked, &rdev->flags);
6165 remove_and_add_spares(mddev, rdev);
6166
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 if (rdev->raid_disk >= 0)
6168 goto busy;
6169
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006170kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006171 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006172 md_cluster_ops->remove_disk(mddev, rdev);
6173
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006174 md_kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07006175 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006176 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177
6178 return 0;
6179busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006180 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 bdevname(rdev->bdev,b), mdname(mddev));
6182 return -EBUSY;
6183}
6184
NeilBrownf72ffdd2014-09-30 14:23:59 +10006185static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186{
6187 char b[BDEVNAME_SIZE];
6188 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006189 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
6191 if (!mddev->pers)
6192 return -ENODEV;
6193
6194 if (mddev->major_version != 0) {
6195 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6196 " version-0 superblocks.\n",
6197 mdname(mddev));
6198 return -EINVAL;
6199 }
6200 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006201 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 "%s: personality does not support diskops!\n",
6203 mdname(mddev));
6204 return -EINVAL;
6205 }
6206
NeilBrownd710e132008-10-13 11:55:12 +11006207 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006209 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 "md: error, md_import_device() returned %ld\n",
6211 PTR_ERR(rdev));
6212 return -EINVAL;
6213 }
6214
6215 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006216 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006218 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
NeilBrown8190e752009-06-18 08:48:58 +10006220 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
NeilBrownb2d444d2005-11-08 21:39:31 -08006222 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006223 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 "md: can not hot-add faulty %s disk to %s!\n",
6225 bdevname(rdev->bdev,b), mdname(mddev));
6226 err = -EINVAL;
6227 goto abort_export;
6228 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006229
NeilBrownb2d444d2005-11-08 21:39:31 -08006230 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006232 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006233 err = bind_rdev_to_array(rdev, mddev);
6234 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006235 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
6237 /*
6238 * The rest should better be atomic, we can have disk failures
6239 * noticed in interrupt contexts ...
6240 */
6241
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 rdev->raid_disk = -1;
6243
NeilBrown850b2b422006-10-03 01:15:46 -07006244 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 /*
6246 * Kick recovery, maybe this spare has to be added to the
6247 * array immediately.
6248 */
6249 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6250 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006251 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 return 0;
6253
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254abort_export:
6255 export_rdev(rdev);
6256 return err;
6257}
6258
NeilBrownfd01b882011-10-11 16:47:53 +11006259static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006260{
NeilBrown035328c2014-04-09 12:25:40 +10006261 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006262
NeilBrown36fa3062005-09-09 16:23:45 -07006263 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006264 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006265 return -EBUSY;
6266 if (mddev->recovery || mddev->sync_thread)
6267 return -EBUSY;
6268 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006269 }
6270
NeilBrown36fa3062005-09-09 16:23:45 -07006271 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006272 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006273 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006274
NeilBrown1e594bb2014-12-15 12:57:00 +11006275 if (mddev->bitmap || mddev->bitmap_info.file)
6276 return -EEXIST; /* cannot add when bitmap is present */
6277 f = fget(fd);
6278
6279 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006280 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6281 mdname(mddev));
6282 return -EBADF;
6283 }
6284
NeilBrown1e594bb2014-12-15 12:57:00 +11006285 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006286 if (!S_ISREG(inode->i_mode)) {
6287 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6288 mdname(mddev));
6289 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006290 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006291 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6292 mdname(mddev));
6293 err = -EBADF;
6294 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006295 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6296 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006297 err = -EBUSY;
6298 }
6299 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006300 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006301 return err;
6302 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006303 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006304 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006305 } else if (mddev->bitmap == NULL)
6306 return -ENOENT; /* cannot remove what isn't there */
6307 err = 0;
6308 if (mddev->pers) {
6309 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006310 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006311 struct bitmap *bitmap;
6312
6313 bitmap = bitmap_create(mddev, -1);
6314 if (!IS_ERR(bitmap)) {
6315 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006316 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006317 } else
6318 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006319 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006320 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006321 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006322 fd = -1; /* make sure to put the file */
6323 }
NeilBrown36fa3062005-09-09 16:23:45 -07006324 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006325 }
6326 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006327 struct file *f = mddev->bitmap_info.file;
6328 if (f) {
6329 spin_lock(&mddev->lock);
6330 mddev->bitmap_info.file = NULL;
6331 spin_unlock(&mddev->lock);
6332 fput(f);
6333 }
NeilBrown36fa3062005-09-09 16:23:45 -07006334 }
6335
NeilBrown32a76272005-06-21 17:17:14 -07006336 return err;
6337}
6338
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339/*
6340 * set_array_info is used two different ways
6341 * The original usage is when creating a new array.
6342 * In this usage, raid_disks is > 0 and it together with
6343 * level, size, not_persistent,layout,chunksize determine the
6344 * shape of the array.
6345 * This will always create an array with a type-0.90.0 superblock.
6346 * The newer usage is when assembling an array.
6347 * In this case raid_disks will be 0, and the major_version field is
6348 * use to determine which style super-blocks are to be found on the devices.
6349 * The minor and patch _version numbers are also kept incase the
6350 * super_block handler wishes to interpret them.
6351 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006352static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353{
6354
6355 if (info->raid_disks == 0) {
6356 /* just setting version number for superblock loading */
6357 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006358 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 super_types[info->major_version].name == NULL) {
6360 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006361 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 "md: superblock version %d not known\n",
6363 info->major_version);
6364 return -EINVAL;
6365 }
6366 mddev->major_version = info->major_version;
6367 mddev->minor_version = info->minor_version;
6368 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006369 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006370 /* ensure mddev_put doesn't delete this now that there
6371 * is some minimal configuration.
6372 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006373 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 return 0;
6375 }
6376 mddev->major_version = MD_MAJOR_VERSION;
6377 mddev->minor_version = MD_MINOR_VERSION;
6378 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006379 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380
6381 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006382 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006383 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 mddev->raid_disks = info->raid_disks;
6385 /* don't set md_minor, it is determined by which /dev/md* was
6386 * openned
6387 */
6388 if (info->state & (1<<MD_SB_CLEAN))
6389 mddev->recovery_cp = MaxSector;
6390 else
6391 mddev->recovery_cp = 0;
6392 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006393 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394
6395 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006396 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
6398 mddev->max_disks = MD_SB_DISKS;
6399
NeilBrowne6910632008-02-06 01:39:51 -08006400 if (mddev->persistent)
6401 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006402 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
NeilBrownc3d97142009-12-14 12:49:52 +11006404 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006405 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006406 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006407
NeilBrownf6705572006-03-27 01:18:11 -08006408 mddev->reshape_position = MaxSector;
6409
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 /*
6411 * Generate a 128 bit UUID
6412 */
6413 get_random_bytes(mddev->uuid, 16);
6414
NeilBrownf6705572006-03-27 01:18:11 -08006415 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006416 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006417 mddev->new_layout = mddev->layout;
6418 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006419 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006420
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 return 0;
6422}
6423
NeilBrownfd01b882011-10-11 16:47:53 +11006424void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006425{
Dan Williamsb522adc2009-03-31 15:00:31 +11006426 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6427
6428 if (mddev->external_size)
6429 return;
6430
Dan Williams1f403622009-03-31 14:59:03 +11006431 mddev->array_sectors = array_sectors;
6432}
6433EXPORT_SYMBOL(md_set_array_sectors);
6434
NeilBrownfd01b882011-10-11 16:47:53 +11006435static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006436{
NeilBrown3cb03002011-10-11 16:45:26 +11006437 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006438 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006439 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006440
6441 if (mddev->pers->resize == NULL)
6442 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006443 /* The "num_sectors" is the number of sectors of each device that
6444 * is used. This can only make sense for arrays with redundancy.
6445 * linear and raid0 always use whatever space is available. We can only
6446 * consider changing this number if no resync or reconstruction is
6447 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006448 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006449 * of each device. If num_sectors is zero, we find the largest size
6450 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006451 */
NeilBrownf851b602014-12-11 10:02:10 +11006452 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6453 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006454 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006455 if (mddev->ro)
6456 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006457
NeilBrowndafb20f2012-03-19 12:46:39 +11006458 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006459 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006460
Andre Nolld71f9f82008-07-11 22:02:22 +10006461 if (fit && (num_sectors == 0 || num_sectors > avail))
6462 num_sectors = avail;
6463 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006464 return -ENOSPC;
6465 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006466 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006467 if (!rv)
6468 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006469 return rv;
6470}
6471
NeilBrownfd01b882011-10-11 16:47:53 +11006472static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006473{
6474 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006475 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006476 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006477 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006478 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006479 if (mddev->ro)
6480 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006481 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006482 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006483 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006484 if (mddev->sync_thread ||
6485 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6486 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006487 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006488
6489 rdev_for_each(rdev, mddev) {
6490 if (mddev->raid_disks < raid_disks &&
6491 rdev->data_offset < rdev->new_data_offset)
6492 return -EINVAL;
6493 if (mddev->raid_disks > raid_disks &&
6494 rdev->data_offset > rdev->new_data_offset)
6495 return -EINVAL;
6496 }
6497
NeilBrown63c70c42006-03-27 01:18:13 -08006498 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006499 if (mddev->delta_disks < 0)
6500 mddev->reshape_backwards = 1;
6501 else if (mddev->delta_disks > 0)
6502 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006503
6504 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006505 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006506 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006507 mddev->reshape_backwards = 0;
6508 }
NeilBrownda943b992006-01-06 00:20:54 -08006509 return rv;
6510}
6511
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512/*
6513 * update_array_info is used to change the configuration of an
6514 * on-line array.
6515 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6516 * fields in the info are checked against the array.
6517 * Any differences that cannot be handled will cause an error.
6518 * Normally, only one change can be managed at a time.
6519 */
NeilBrownfd01b882011-10-11 16:47:53 +11006520static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521{
6522 int rv = 0;
6523 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006524 int state = 0;
6525
6526 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006527 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006528 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
6530 if (mddev->major_version != info->major_version ||
6531 mddev->minor_version != info->minor_version ||
6532/* mddev->patch_version != info->patch_version || */
6533 mddev->ctime != info->ctime ||
6534 mddev->level != info->level ||
6535/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006536 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006537 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006538 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6539 ((state^info->state) & 0xfffffe00)
6540 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 return -EINVAL;
6542 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006543 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6544 cnt++;
6545 if (mddev->raid_disks != info->raid_disks)
6546 cnt++;
6547 if (mddev->layout != info->layout)
6548 cnt++;
6549 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6550 cnt++;
6551 if (cnt == 0)
6552 return 0;
6553 if (cnt > 1)
6554 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555
6556 if (mddev->layout != info->layout) {
6557 /* Change layout
6558 * we don't need to do anything at the md level, the
6559 * personality will take care of it all.
6560 */
NeilBrown50ac1682009-06-18 08:47:55 +10006561 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006563 else {
6564 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006565 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006566 if (rv)
6567 mddev->new_layout = mddev->layout;
6568 return rv;
6569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006571 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006572 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573
NeilBrownda943b992006-01-06 00:20:54 -08006574 if (mddev->raid_disks != info->raid_disks)
6575 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576
NeilBrown36fa3062005-09-09 16:23:45 -07006577 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006578 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6579 rv = -EINVAL;
6580 goto err;
6581 }
6582 if (mddev->recovery || mddev->sync_thread) {
6583 rv = -EBUSY;
6584 goto err;
6585 }
NeilBrown36fa3062005-09-09 16:23:45 -07006586 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006587 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006588 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006589 if (mddev->bitmap) {
6590 rv = -EEXIST;
6591 goto err;
6592 }
6593 if (mddev->bitmap_info.default_offset == 0) {
6594 rv = -EINVAL;
6595 goto err;
6596 }
NeilBrownc3d97142009-12-14 12:49:52 +11006597 mddev->bitmap_info.offset =
6598 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006599 mddev->bitmap_info.space =
6600 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006601 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006602 bitmap = bitmap_create(mddev, -1);
6603 if (!IS_ERR(bitmap)) {
6604 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006605 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006606 } else
6607 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006608 if (rv)
6609 bitmap_destroy(mddev);
6610 mddev->pers->quiesce(mddev, 0);
6611 } else {
6612 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006613 if (!mddev->bitmap) {
6614 rv = -ENOENT;
6615 goto err;
6616 }
6617 if (mddev->bitmap->storage.file) {
6618 rv = -EINVAL;
6619 goto err;
6620 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11006621 if (mddev->bitmap_info.nodes) {
6622 /* hold PW on all the bitmap lock */
6623 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
6624 printk("md: can't change bitmap to none since the"
6625 " array is in use by more than one node\n");
6626 rv = -EPERM;
6627 md_cluster_ops->unlock_all_bitmaps(mddev);
6628 goto err;
6629 }
6630
6631 mddev->bitmap_info.nodes = 0;
6632 md_cluster_ops->leave(mddev);
6633 }
NeilBrown36fa3062005-09-09 16:23:45 -07006634 mddev->pers->quiesce(mddev, 1);
6635 bitmap_destroy(mddev);
6636 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006637 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006638 }
6639 }
NeilBrown850b2b422006-10-03 01:15:46 -07006640 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006641 return rv;
6642err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 return rv;
6644}
6645
NeilBrownfd01b882011-10-11 16:47:53 +11006646static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647{
NeilBrown3cb03002011-10-11 16:45:26 +11006648 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006649 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650
6651 if (mddev->pers == NULL)
6652 return -ENODEV;
6653
NeilBrown1ca69c42012-10-11 13:37:33 +11006654 rcu_read_lock();
6655 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006657 err = -ENODEV;
6658 else {
6659 md_error(mddev, rdev);
6660 if (!test_bit(Faulty, &rdev->flags))
6661 err = -EBUSY;
6662 }
6663 rcu_read_unlock();
6664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665}
6666
Andre Noll2f9618c2008-04-25 18:57:58 +02006667/*
6668 * We have a problem here : there is no easy way to give a CHS
6669 * virtual geometry. We currently pretend that we have a 2 heads
6670 * 4 sectors (with a BIG number of cylinders...). This drives
6671 * dosfs just mad... ;-)
6672 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006673static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6674{
NeilBrownfd01b882011-10-11 16:47:53 +11006675 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006676
6677 geo->heads = 2;
6678 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006679 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006680 return 0;
6681}
6682
Nicolas Schichancb335f82014-01-15 16:58:52 +01006683static inline bool md_ioctl_valid(unsigned int cmd)
6684{
6685 switch (cmd) {
6686 case ADD_NEW_DISK:
6687 case BLKROSET:
6688 case GET_ARRAY_INFO:
6689 case GET_BITMAP_FILE:
6690 case GET_DISK_INFO:
6691 case HOT_ADD_DISK:
6692 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006693 case RAID_AUTORUN:
6694 case RAID_VERSION:
6695 case RESTART_ARRAY_RW:
6696 case RUN_ARRAY:
6697 case SET_ARRAY_INFO:
6698 case SET_BITMAP_FILE:
6699 case SET_DISK_FAULTY:
6700 case STOP_ARRAY:
6701 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006702 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006703 return true;
6704 default:
6705 return false;
6706 }
6707}
6708
Al Viroa39907f2008-03-02 10:31:15 -05006709static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 unsigned int cmd, unsigned long arg)
6711{
6712 int err = 0;
6713 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006714 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006715 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
Nicolas Schichancb335f82014-01-15 16:58:52 +01006717 if (!md_ioctl_valid(cmd))
6718 return -ENOTTY;
6719
NeilBrown506c9e42011-12-23 10:17:26 +11006720 switch (cmd) {
6721 case RAID_VERSION:
6722 case GET_ARRAY_INFO:
6723 case GET_DISK_INFO:
6724 break;
6725 default:
6726 if (!capable(CAP_SYS_ADMIN))
6727 return -EACCES;
6728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
6730 /*
6731 * Commands dealing with the RAID driver but not any
6732 * particular array:
6733 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006734 switch (cmd) {
6735 case RAID_VERSION:
6736 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006737 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006740 case RAID_AUTORUN:
6741 err = 0;
6742 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006743 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006745 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 }
6747
6748 /*
6749 * Commands creating/starting a new array:
6750 */
6751
Al Viroa39907f2008-03-02 10:31:15 -05006752 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
6754 if (!mddev) {
6755 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006756 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 }
6758
NeilBrown1ca69c42012-10-11 13:37:33 +11006759 /* Some actions do not requires the mutex */
6760 switch (cmd) {
6761 case GET_ARRAY_INFO:
6762 if (!mddev->raid_disks && !mddev->external)
6763 err = -ENODEV;
6764 else
6765 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006766 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006767
6768 case GET_DISK_INFO:
6769 if (!mddev->raid_disks && !mddev->external)
6770 err = -ENODEV;
6771 else
6772 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006773 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006774
6775 case SET_DISK_FAULTY:
6776 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006777 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006778
6779 case GET_BITMAP_FILE:
6780 err = get_bitmap_file(mddev, argp);
6781 goto out;
6782
NeilBrown1ca69c42012-10-11 13:37:33 +11006783 }
6784
NeilBrowna7a3f082012-12-11 13:35:54 +11006785 if (cmd == ADD_NEW_DISK)
6786 /* need to ensure md_delayed_delete() has completed */
6787 flush_workqueue(md_misc_wq);
6788
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006789 if (cmd == HOT_REMOVE_DISK)
6790 /* need to ensure recovery thread has run */
6791 wait_event_interruptible_timeout(mddev->sb_wait,
6792 !test_bit(MD_RECOVERY_NEEDED,
6793 &mddev->flags),
6794 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006795 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6796 /* Need to flush page cache, and ensure no-one else opens
6797 * and writes
6798 */
6799 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006800 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006801 mutex_unlock(&mddev->open_mutex);
6802 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006803 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006804 }
6805 set_bit(MD_STILL_CLOSED, &mddev->flags);
6806 mutex_unlock(&mddev->open_mutex);
6807 sync_blockdev(bdev);
6808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 err = mddev_lock(mddev);
6810 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006811 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6813 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 }
6816
NeilBrownc02c0ae2012-12-11 13:39:21 +11006817 if (cmd == SET_ARRAY_INFO) {
6818 mdu_array_info_t info;
6819 if (!arg)
6820 memset(&info, 0, sizeof(info));
6821 else if (copy_from_user(&info, argp, sizeof(info))) {
6822 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006823 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006824 }
6825 if (mddev->pers) {
6826 err = update_array_info(mddev, &info);
6827 if (err) {
6828 printk(KERN_WARNING "md: couldn't update"
6829 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006830 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006832 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006833 }
6834 if (!list_empty(&mddev->disks)) {
6835 printk(KERN_WARNING
6836 "md: array %s already has disks!\n",
6837 mdname(mddev));
6838 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006839 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006840 }
6841 if (mddev->raid_disks) {
6842 printk(KERN_WARNING
6843 "md: array %s already initialised!\n",
6844 mdname(mddev));
6845 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006846 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006847 }
6848 err = set_array_info(mddev, &info);
6849 if (err) {
6850 printk(KERN_WARNING "md: couldn't set"
6851 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006852 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006853 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006854 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 }
6856
6857 /*
6858 * Commands querying/configuring an existing array:
6859 */
NeilBrown32a76272005-06-21 17:17:14 -07006860 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006861 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006862 if ((!mddev->raid_disks && !mddev->external)
6863 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6864 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6865 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006867 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 }
6869
6870 /*
6871 * Commands even a read-only array can execute:
6872 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006873 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006874 case RESTART_ARRAY_RW:
6875 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006876 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006877
6878 case STOP_ARRAY:
6879 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006880 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006881
6882 case STOP_ARRAY_RO:
6883 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006884 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006885
NeilBrown3ea8929d2013-04-24 11:42:41 +10006886 case HOT_REMOVE_DISK:
6887 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006888 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006889
NeilBrown7ceb17e2013-04-24 11:42:42 +10006890 case ADD_NEW_DISK:
6891 /* We can support ADD_NEW_DISK on read-only arrays
6892 * on if we are re-adding a preexisting device.
6893 * So require mddev->pers and MD_DISK_SYNC.
6894 */
6895 if (mddev->pers) {
6896 mdu_disk_info_t info;
6897 if (copy_from_user(&info, argp, sizeof(info)))
6898 err = -EFAULT;
6899 else if (!(info.state & (1<<MD_DISK_SYNC)))
6900 /* Need to clear read-only for this */
6901 break;
6902 else
6903 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006904 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006905 }
6906 break;
6907
NeilBrownc02c0ae2012-12-11 13:39:21 +11006908 case BLKROSET:
6909 if (get_user(ro, (int __user *)(arg))) {
6910 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006911 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006912 }
6913 err = -EINVAL;
6914
6915 /* if the bdev is going readonly the value of mddev->ro
6916 * does not matter, no writes are coming
6917 */
6918 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006919 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006920
NeilBrownc02c0ae2012-12-11 13:39:21 +11006921 /* are we are already prepared for writes? */
6922 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006923 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006924
6925 /* transitioning to readauto need only happen for
6926 * arrays that call md_write_start
6927 */
6928 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006930 if (err == 0) {
6931 mddev->ro = 2;
6932 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006933 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006934 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006935 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 }
6937
6938 /*
6939 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006940 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 */
NeilBrown326eb172014-09-30 15:36:28 +10006942 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006943 if (mddev->ro == 2) {
6944 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006945 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006946 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006947 /* mddev_unlock will wake thread */
6948 /* If a device failed while we were read-only, we
6949 * need to make sure the metadata is updated now.
6950 */
6951 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6952 mddev_unlock(mddev);
6953 wait_event(mddev->sb_wait,
6954 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6955 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006956 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006957 }
NeilBrownf91de922005-11-08 21:39:36 -08006958 } else {
6959 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006960 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 }
6963
NeilBrownc02c0ae2012-12-11 13:39:21 +11006964 switch (cmd) {
6965 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006967 mdu_disk_info_t info;
6968 if (copy_from_user(&info, argp, sizeof(info)))
6969 err = -EFAULT;
6970 else
6971 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006972 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006975 case CLUSTERED_DISK_NACK:
6976 if (mddev_is_clustered(mddev))
6977 md_cluster_ops->new_disk_ack(mddev, false);
6978 else
6979 err = -EINVAL;
6980 goto unlock;
6981
NeilBrownc02c0ae2012-12-11 13:39:21 +11006982 case HOT_ADD_DISK:
6983 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006984 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
NeilBrownc02c0ae2012-12-11 13:39:21 +11006986 case RUN_ARRAY:
6987 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006988 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989
NeilBrownc02c0ae2012-12-11 13:39:21 +11006990 case SET_BITMAP_FILE:
6991 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006992 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006993
NeilBrownc02c0ae2012-12-11 13:39:21 +11006994 default:
6995 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006996 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 }
6998
NeilBrown3adc28d2014-09-30 15:46:41 +10006999unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007000 if (mddev->hold_active == UNTIL_IOCTL &&
7001 err != -EINVAL)
7002 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007004out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 return err;
7006}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007007#ifdef CONFIG_COMPAT
7008static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7009 unsigned int cmd, unsigned long arg)
7010{
7011 switch (cmd) {
7012 case HOT_REMOVE_DISK:
7013 case HOT_ADD_DISK:
7014 case SET_DISK_FAULTY:
7015 case SET_BITMAP_FILE:
7016 /* These take in integer arg, do not convert */
7017 break;
7018 default:
7019 arg = (unsigned long)compat_ptr(arg);
7020 break;
7021 }
7022
7023 return md_ioctl(bdev, mode, cmd, arg);
7024}
7025#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026
Al Viroa39907f2008-03-02 10:31:15 -05007027static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028{
7029 /*
7030 * Succeed if we can lock the mddev, which confirms that
7031 * it isn't being stopped right now.
7032 */
NeilBrownfd01b882011-10-11 16:47:53 +11007033 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 int err;
7035
Yuanhan Liu0c098222012-05-22 13:55:32 +10007036 if (!mddev)
7037 return -ENODEV;
7038
NeilBrownd3374822009-01-09 08:31:10 +11007039 if (mddev->gendisk != bdev->bd_disk) {
7040 /* we are racing with mddev_put which is discarding this
7041 * bd_disk.
7042 */
7043 mddev_put(mddev);
7044 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007045 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007046 /* Then retry the open from the top */
7047 return -ERESTARTSYS;
7048 }
7049 BUG_ON(mddev != bdev->bd_disk->private_data);
7050
NeilBrownc8c00a62009-08-10 12:50:52 +10007051 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 goto out;
7053
7054 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007055 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10007056 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10007057 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007059 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 out:
7061 return err;
7062}
7063
Al Virodb2a1442013-05-05 21:52:57 -04007064static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007066 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007068 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007069 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007072
7073static int md_media_changed(struct gendisk *disk)
7074{
NeilBrownfd01b882011-10-11 16:47:53 +11007075 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007076
7077 return mddev->changed;
7078}
7079
7080static int md_revalidate(struct gendisk *disk)
7081{
NeilBrownfd01b882011-10-11 16:47:53 +11007082 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007083
7084 mddev->changed = 0;
7085 return 0;
7086}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007087static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088{
7089 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007090 .open = md_open,
7091 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007092 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007093#ifdef CONFIG_COMPAT
7094 .compat_ioctl = md_compat_ioctl,
7095#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007096 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007097 .media_changed = md_media_changed,
7098 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099};
7100
NeilBrownf72ffdd2014-09-30 14:23:59 +10007101static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102{
NeilBrown2b8bf342011-10-11 16:48:23 +11007103 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 /*
7106 * md_thread is a 'system-thread', it's priority should be very
7107 * high. We avoid resource deadlocks individually in each
7108 * raid personality. (RAID5 does preallocation) We also use RR and
7109 * the very same RT priority as kswapd, thus we will never get
7110 * into a priority inversion deadlock.
7111 *
7112 * we definitely have to have equal or higher priority than
7113 * bdflush, otherwise bdflush will deadlock if there are too
7114 * many dirty RAID5 blocks.
7115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116
NeilBrown6985c432005-10-19 21:23:47 -07007117 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007118 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119
NeilBrown93588e22005-11-15 00:09:12 -08007120 /* We need to wait INTERRUPTIBLE so that
7121 * we don't add to the load-average.
7122 * That means we need to be sure no signals are
7123 * pending
7124 */
7125 if (signal_pending(current))
7126 flush_signals(current);
7127
7128 wait_event_interruptible_timeout
7129 (thread->wqueue,
7130 test_bit(THREAD_WAKEUP, &thread->flags)
7131 || kthread_should_stop(),
7132 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133
NeilBrown6c987912011-01-14 09:13:53 +11007134 clear_bit(THREAD_WAKEUP, &thread->flags);
7135 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007136 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007138
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 return 0;
7140}
7141
NeilBrown2b8bf342011-10-11 16:48:23 +11007142void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143{
7144 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007145 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 set_bit(THREAD_WAKEUP, &thread->flags);
7147 wake_up(&thread->wqueue);
7148 }
7149}
NeilBrown6c144d32014-09-30 16:15:38 +10007150EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
Shaohua Li4ed87312012-10-11 13:34:00 +11007152struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7153 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154{
NeilBrown2b8bf342011-10-11 16:48:23 +11007155 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156
NeilBrown2b8bf342011-10-11 16:48:23 +11007157 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 if (!thread)
7159 return NULL;
7160
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 init_waitqueue_head(&thread->wqueue);
7162
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 thread->run = run;
7164 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007165 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007166 thread->tsk = kthread_run(md_thread, thread,
7167 "%s_%s",
7168 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007169 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007170 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 kfree(thread);
7172 return NULL;
7173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 return thread;
7175}
NeilBrown6c144d32014-09-30 16:15:38 +10007176EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177
NeilBrown2b8bf342011-10-11 16:48:23 +11007178void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179{
NeilBrown2b8bf342011-10-11 16:48:23 +11007180 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007181 if (!thread)
7182 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007183 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007184 /* Locking ensures that mddev_unlock does not wake_up a
7185 * non-existent thread
7186 */
7187 spin_lock(&pers_lock);
7188 *threadp = NULL;
7189 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007190
7191 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 kfree(thread);
7193}
NeilBrown6c144d32014-09-30 16:15:38 +10007194EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195
NeilBrownfd01b882011-10-11 16:47:53 +11007196void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197{
NeilBrownb2d444d2005-11-08 21:39:31 -08007198 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007200
NeilBrownde393cd2011-07-28 11:31:48 +10007201 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 return;
7203 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007204 if (mddev->degraded)
7205 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007206 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7208 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7209 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007210 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007211 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07007212 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213}
NeilBrown6c144d32014-09-30 16:15:38 +10007214EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215
7216/* seq_file implementation /proc/mdstat */
7217
7218static void status_unused(struct seq_file *seq)
7219{
7220 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007221 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222
7223 seq_printf(seq, "unused devices: ");
7224
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007225 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 char b[BDEVNAME_SIZE];
7227 i++;
7228 seq_printf(seq, "%s ",
7229 bdevname(rdev->bdev,b));
7230 }
7231 if (!i)
7232 seq_printf(seq, "<none>");
7233
7234 seq_printf(seq, "\n");
7235}
7236
NeilBrownf7851be2015-07-02 17:12:58 +10007237static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238{
NeilBrowndd71cf62009-05-07 12:49:35 +10007239 sector_t max_sectors, resync, res;
7240 unsigned long dt, db;
7241 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007242 int scale;
7243 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244
NeilBrownc804cde2012-05-21 09:28:33 +10007245 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7246 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007247 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007249 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
NeilBrownf7851be2015-07-02 17:12:58 +10007251 resync = mddev->curr_resync;
7252 if (resync <= 3) {
7253 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7254 /* Still cleaning up */
7255 resync = max_sectors;
7256 } else
7257 resync -= atomic_read(&mddev->recovery_active);
7258
7259 if (resync == 0) {
7260 if (mddev->recovery_cp < MaxSector) {
7261 seq_printf(seq, "\tresync=PENDING");
7262 return 1;
7263 }
7264 return 0;
7265 }
7266 if (resync < 3) {
7267 seq_printf(seq, "\tresync=DELAYED");
7268 return 1;
7269 }
7270
NeilBrown403df472014-09-30 15:52:29 +10007271 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007272 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007273 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007274 * u32, as those are the requirements for sector_div.
7275 * Thus 'scale' must be at least 10
7276 */
7277 scale = 10;
7278 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007279 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007280 scale++;
7281 }
7282 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007283 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007284
7285 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 {
NeilBrown4588b422006-03-27 01:18:04 -08007287 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 seq_printf(seq, "[");
7289 for (i = 0; i < x; i++)
7290 seq_printf(seq, "=");
7291 seq_printf(seq, ">");
7292 for (i = 0; i < y; i++)
7293 seq_printf(seq, ".");
7294 seq_printf(seq, "] ");
7295 }
NeilBrown4588b422006-03-27 01:18:04 -08007296 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007297 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7298 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007299 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7300 "check" :
7301 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7302 "resync" : "recovery"))),
7303 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007304 (unsigned long long) resync/2,
7305 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306
7307 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 * dt: time from mark until now
7309 * db: blocks written from mark until now
7310 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007311 *
7312 * rt is a sector_t, so could be 32bit or 64bit.
7313 * So we divide before multiply in case it is 32bit and close
7314 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007315 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007316 * near the end of resync when the number of remaining sectors
7317 * is close to 'db'.
7318 * We then divide rt by 32 after multiplying by db to compensate.
7319 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 */
7321 dt = ((jiffies - mddev->resync_mark) / HZ);
7322 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007323 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7324 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
NeilBrowndd71cf62009-05-07 12:49:35 +10007326 rt = max_sectors - resync; /* number of remaining sectors */
7327 sector_div(rt, db/32+1);
7328 rt *= dt;
7329 rt >>= 5;
7330
7331 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7332 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333
NeilBrownff4e8d92006-07-10 04:44:16 -07007334 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007335 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336}
7337
7338static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7339{
7340 struct list_head *tmp;
7341 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007342 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343
7344 if (l >= 0x10000)
7345 return NULL;
7346 if (!l--)
7347 /* header */
7348 return (void*)1;
7349
7350 spin_lock(&all_mddevs_lock);
7351 list_for_each(tmp,&all_mddevs)
7352 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007353 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 mddev_get(mddev);
7355 spin_unlock(&all_mddevs_lock);
7356 return mddev;
7357 }
7358 spin_unlock(&all_mddevs_lock);
7359 if (!l--)
7360 return (void*)2;/* tail */
7361 return NULL;
7362}
7363
7364static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7365{
7366 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007367 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007368
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 ++*pos;
7370 if (v == (void*)2)
7371 return NULL;
7372
7373 spin_lock(&all_mddevs_lock);
7374 if (v == (void*)1)
7375 tmp = all_mddevs.next;
7376 else
7377 tmp = mddev->all_mddevs.next;
7378 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007379 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 else {
7381 next_mddev = (void*)2;
7382 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 spin_unlock(&all_mddevs_lock);
7385
7386 if (v != (void*)1)
7387 mddev_put(mddev);
7388 return next_mddev;
7389
7390}
7391
7392static void md_seq_stop(struct seq_file *seq, void *v)
7393{
NeilBrownfd01b882011-10-11 16:47:53 +11007394 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395
7396 if (mddev && v != (void*)1 && v != (void*)2)
7397 mddev_put(mddev);
7398}
7399
7400static int md_seq_show(struct seq_file *seq, void *v)
7401{
NeilBrownfd01b882011-10-11 16:47:53 +11007402 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007403 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007404 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405
7406 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007407 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 seq_printf(seq, "Personalities : ");
7409 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007410 list_for_each_entry(pers, &pers_list, list)
7411 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
7413 spin_unlock(&pers_lock);
7414 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007415 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 return 0;
7417 }
7418 if (v == (void*)2) {
7419 status_unused(seq);
7420 return 0;
7421 }
7422
NeilBrown36d091f2014-12-15 12:56:58 +11007423 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7425 seq_printf(seq, "%s : %sactive", mdname(mddev),
7426 mddev->pers ? "" : "in");
7427 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007428 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007430 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007431 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 seq_printf(seq, " %s", mddev->pers->name);
7433 }
7434
Andre Nolldd8ac332009-03-31 14:33:13 +11007435 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007436 rcu_read_lock();
7437 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 char b[BDEVNAME_SIZE];
7439 seq_printf(seq, " %s[%d]",
7440 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007441 if (test_bit(WriteMostly, &rdev->flags))
7442 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007443 if (test_bit(Journal, &rdev->flags))
7444 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007445 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 seq_printf(seq, "(F)");
7447 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007448 }
7449 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007450 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007451 if (test_bit(Replacement, &rdev->flags))
7452 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007453 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007455 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456
7457 if (!list_empty(&mddev->disks)) {
7458 if (mddev->pers)
7459 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007460 (unsigned long long)
7461 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 else
7463 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007464 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007466 if (mddev->persistent) {
7467 if (mddev->major_version != 0 ||
7468 mddev->minor_version != 90) {
7469 seq_printf(seq," super %d.%d",
7470 mddev->major_version,
7471 mddev->minor_version);
7472 }
NeilBrowne6910632008-02-06 01:39:51 -08007473 } else if (mddev->external)
7474 seq_printf(seq, " super external:%s",
7475 mddev->metadata_type);
7476 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007477 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
7479 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007480 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007481 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007482 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007483 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007484 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007485 }
NeilBrown32a76272005-06-21 17:17:14 -07007486 } else
7487 seq_printf(seq, "\n ");
7488
NeilBrown57148962012-03-19 12:46:40 +11007489 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490
7491 seq_printf(seq, "\n");
7492 }
NeilBrown36d091f2014-12-15 12:56:58 +11007493 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007494
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495 return 0;
7496}
7497
Jan Engelhardt110518b2009-05-07 12:49:37 +10007498static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 .start = md_seq_start,
7500 .next = md_seq_next,
7501 .stop = md_seq_stop,
7502 .show = md_seq_show,
7503};
7504
7505static int md_seq_open(struct inode *inode, struct file *file)
7506{
Kay Sieversf1514632011-07-12 20:48:39 +02007507 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 int error;
7509
7510 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007511 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007512 return error;
7513
7514 seq = file->private_data;
7515 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 return error;
7517}
7518
NeilBrowne2f23b62014-04-09 14:33:51 +10007519static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007520static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7521{
Kay Sieversf1514632011-07-12 20:48:39 +02007522 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007523 int mask;
7524
NeilBrowne2f23b62014-04-09 14:33:51 +10007525 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007526 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007527 poll_wait(filp, &md_event_waiters, wait);
7528
7529 /* always allow read */
7530 mask = POLLIN | POLLRDNORM;
7531
Kay Sieversf1514632011-07-12 20:48:39 +02007532 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007533 mask |= POLLERR | POLLPRI;
7534 return mask;
7535}
7536
Arjan van de Venfa027c22007-02-12 00:55:33 -08007537static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007538 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539 .open = md_seq_open,
7540 .read = seq_read,
7541 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007542 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007543 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544};
7545
NeilBrown84fc4b52011-10-11 16:49:58 +11007546int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547{
Chao Yu50bd3772014-09-25 15:28:34 +08007548 printk(KERN_INFO "md: %s personality registered for level %d\n",
7549 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007551 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 spin_unlock(&pers_lock);
7553 return 0;
7554}
NeilBrown6c144d32014-09-30 16:15:38 +10007555EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556
NeilBrown84fc4b52011-10-11 16:49:58 +11007557int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558{
NeilBrown2604b702006-01-06 00:20:36 -08007559 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007561 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 spin_unlock(&pers_lock);
7563 return 0;
7564}
NeilBrown6c144d32014-09-30 16:15:38 +10007565EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566
NeilBrown6022e752015-08-13 12:32:55 +10007567int register_md_cluster_operations(struct md_cluster_operations *ops,
7568 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007569{
NeilBrown6022e752015-08-13 12:32:55 +10007570 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007571 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007572 if (md_cluster_ops != NULL)
7573 ret = -EALREADY;
7574 else {
7575 md_cluster_ops = ops;
7576 md_cluster_mod = module;
7577 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007578 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007579 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007580}
7581EXPORT_SYMBOL(register_md_cluster_operations);
7582
7583int unregister_md_cluster_operations(void)
7584{
7585 spin_lock(&pers_lock);
7586 md_cluster_ops = NULL;
7587 spin_unlock(&pers_lock);
7588 return 0;
7589}
7590EXPORT_SYMBOL(unregister_md_cluster_operations);
7591
7592int md_setup_cluster(struct mddev *mddev, int nodes)
7593{
7594 int err;
7595
7596 err = request_module("md-cluster");
7597 if (err) {
7598 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007599 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007600 }
7601
7602 spin_lock(&pers_lock);
7603 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7604 spin_unlock(&pers_lock);
7605 return -ENOENT;
7606 }
7607 spin_unlock(&pers_lock);
7608
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007609 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007610}
7611
7612void md_cluster_stop(struct mddev *mddev)
7613{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007614 if (!md_cluster_ops)
7615 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007616 md_cluster_ops->leave(mddev);
7617 module_put(md_cluster_mod);
7618}
7619
NeilBrownfd01b882011-10-11 16:47:53 +11007620static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007622 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007624 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625
7626 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007627 rcu_read_lock();
7628 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007630 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7631 (int)part_stat_read(&disk->part0, sectors[1]) -
7632 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007633 /* sync IO will cause sync_io to increase before the disk_stats
7634 * as sync_io is counted when a request starts, and
7635 * disk_stats is counted when it completes.
7636 * So resync activity will cause curr_events to be smaller than
7637 * when there was no such activity.
7638 * non-sync IO will cause disk_stat to increase without
7639 * increasing sync_io so curr_events will (eventually)
7640 * be larger than it was before. Once it becomes
7641 * substantially larger, the test below will cause
7642 * the array to appear non-idle, and resync will slow
7643 * down.
7644 * If there is a lot of outstanding resync activity when
7645 * we set last_event to curr_events, then all that activity
7646 * completing might cause the array to appear non-idle
7647 * and resync will be slowed down even though there might
7648 * not have been non-resync activity. This will only
7649 * happen once though. 'last_events' will soon reflect
7650 * the state where there is little or no outstanding
7651 * resync requests, and further resync activity will
7652 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007653 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007655 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 rdev->last_events = curr_events;
7657 idle = 0;
7658 }
7659 }
NeilBrown4b809912008-07-21 17:05:25 +10007660 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 return idle;
7662}
7663
NeilBrownfd01b882011-10-11 16:47:53 +11007664void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665{
7666 /* another "blocks" (512byte) blocks have been synced */
7667 atomic_sub(blocks, &mddev->recovery_active);
7668 wake_up(&mddev->recovery_wait);
7669 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007670 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007671 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 md_wakeup_thread(mddev->thread);
7673 // stop recovery, signal do_sync ....
7674 }
7675}
NeilBrown6c144d32014-09-30 16:15:38 +10007676EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677
NeilBrown06d91a52005-06-21 17:17:12 -07007678/* md_write_start(mddev, bi)
7679 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007680 * in superblock) before writing, schedule a superblock update
7681 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007682 */
NeilBrownfd01b882011-10-11 16:47:53 +11007683void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684{
Neil Brown0fd62b82008-06-28 08:31:36 +10007685 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007686 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007687 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007688
NeilBrownf91de922005-11-08 21:39:36 -08007689 BUG_ON(mddev->ro == 1);
7690 if (mddev->ro == 2) {
7691 /* need to switch to read/write */
7692 mddev->ro = 0;
7693 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7694 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007695 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007696 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007697 }
NeilBrown06d91a52005-06-21 17:17:12 -07007698 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007699 if (mddev->safemode == 1)
7700 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007701 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007702 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007703 if (mddev->in_sync) {
7704 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007705 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007706 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007707 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007708 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007709 }
NeilBrown85572d72014-12-15 12:56:56 +11007710 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007711 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007712 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007713 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007714 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007715 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716}
NeilBrown6c144d32014-09-30 16:15:38 +10007717EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718
NeilBrownfd01b882011-10-11 16:47:53 +11007719void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720{
7721 if (atomic_dec_and_test(&mddev->writes_pending)) {
7722 if (mddev->safemode == 2)
7723 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007724 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7726 }
7727}
NeilBrown6c144d32014-09-30 16:15:38 +10007728EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729
NeilBrown2a2275d2007-01-26 00:57:11 -08007730/* md_allow_write(mddev)
7731 * Calling this ensures that the array is marked 'active' so that writes
7732 * may proceed without blocking. It is important to call this before
7733 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7734 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007735 *
Guoqing Jiangabf35082015-12-21 10:51:01 +11007736 * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock
Dan Williamsb5470dc2008-06-27 21:44:04 -07007737 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007738 */
NeilBrownfd01b882011-10-11 16:47:53 +11007739int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007740{
7741 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007742 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007743 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007744 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007745 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007746 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007747
NeilBrown85572d72014-12-15 12:56:56 +11007748 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007749 if (mddev->in_sync) {
7750 mddev->in_sync = 0;
7751 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007752 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007753 if (mddev->safemode_delay &&
7754 mddev->safemode == 0)
7755 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007756 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007757 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007758 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007759 } else
NeilBrown85572d72014-12-15 12:56:56 +11007760 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007761
NeilBrown070dc6d2010-08-30 17:33:34 +10007762 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007763 return -EAGAIN;
7764 else
7765 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007766}
7767EXPORT_SYMBOL_GPL(md_allow_write);
7768
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769#define SYNC_MARKS 10
7770#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007771#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007772void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773{
Shaohua Li4ed87312012-10-11 13:34:00 +11007774 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007775 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776 unsigned int currspeed = 0,
7777 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007778 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007780 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 sector_t mark_cnt[SYNC_MARKS];
7782 int last_mark,m;
7783 struct list_head *tmp;
7784 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007785 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007786 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007787 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007788 struct blk_plug plug;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05007789 bool cluster_resync_finished = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790
7791 /* just incase thread restarts... */
7792 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7793 return;
NeilBrown3991b312014-05-28 13:39:23 +10007794 if (mddev->ro) {/* never try to sync a read-only array */
7795 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007796 return;
NeilBrown3991b312014-05-28 13:39:23 +10007797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798
NeilBrown61df9d92006-10-03 01:15:57 -07007799 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007800 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007801 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007802 action = "check";
7803 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007804 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007805 action = "repair";
7806 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007807 desc = "resync";
7808 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7809 desc = "reshape";
7810 else
7811 desc = "recovery";
7812
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007813 mddev->last_sync_action = action ?: desc;
7814
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815 /* we overload curr_resync somewhat here.
7816 * 0 == not engaged in resync at all
7817 * 2 == checking that there is no conflict with another sync
7818 * 1 == like 2, but have yielded to allow conflicting resync to
7819 * commense
7820 * other == active in resync - this many blocks
7821 *
7822 * Before starting a resync we must have set curr_resync to
7823 * 2, and then checked that every "conflicting" array has curr_resync
7824 * less than ours. When we find one that is the same or higher
7825 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7826 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7827 * This will mean we have to start checking from the beginning again.
7828 *
7829 */
7830
7831 do {
7832 mddev->curr_resync = 2;
7833
7834 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007835 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007837 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 if (mddev2 == mddev)
7839 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007840 if (!mddev->parallel_resync
7841 && mddev2->curr_resync
7842 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 DEFINE_WAIT(wq);
7844 if (mddev < mddev2 && mddev->curr_resync == 2) {
7845 /* arbitrarily yield */
7846 mddev->curr_resync = 1;
7847 wake_up(&resync_wait);
7848 }
7849 if (mddev > mddev2 && mddev->curr_resync == 1)
7850 /* no need to wait here, we can wait the next
7851 * time 'round when curr_resync == 2
7852 */
7853 continue;
NeilBrown97441972008-09-19 11:49:54 +10007854 /* We need to wait 'interruptible' so as not to
7855 * contribute to the load average, and not to
7856 * be caught by 'softlockup'
7857 */
7858 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007859 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007860 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007861 printk(KERN_INFO "md: delaying %s of %s"
7862 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007864 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007866 if (signal_pending(current))
7867 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 schedule();
7869 finish_wait(&resync_wait, &wq);
7870 goto try_again;
7871 }
7872 finish_wait(&resync_wait, &wq);
7873 }
7874 }
7875 } while (mddev->curr_resync < 2);
7876
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007877 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007878 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007880 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 */
7882 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007883 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007884 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007885 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7886 j = mddev->resync_min;
7887 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007888 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007889
NeilBrownccfcc3c2006-03-27 01:18:09 -08007890 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007891 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007892 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007894 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007895 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007896 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007897 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007898 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07007899 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007900 !test_bit(Faulty, &rdev->flags) &&
7901 !test_bit(In_sync, &rdev->flags) &&
7902 rdev->recovery_offset < j)
7903 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007904 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007905
7906 /* If there is a bitmap, we need to make sure all
7907 * writes that started before we added a spare
7908 * complete before we start doing a recovery.
7909 * Otherwise the write might complete and (via
7910 * bitmap_endwrite) set a bit in the bitmap after the
7911 * recovery has checked that bit and skipped that
7912 * region.
7913 */
7914 if (mddev->bitmap) {
7915 mddev->pers->quiesce(mddev, 1);
7916 mddev->pers->quiesce(mddev, 0);
7917 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919
NeilBrown61df9d92006-10-03 01:15:57 -07007920 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7921 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7922 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007923 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007924 "(but not more than %d KB/sec) for %s.\n",
7925 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926
NeilBrowneea1bf32009-03-31 14:27:02 +11007927 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007928
NeilBrown57afd892005-06-21 17:17:13 -07007929 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930 for (m = 0; m < SYNC_MARKS; m++) {
7931 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007932 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933 }
7934 last_mark = 0;
7935 mddev->resync_mark = mark[last_mark];
7936 mddev->resync_mark_cnt = mark_cnt[last_mark];
7937
7938 /*
7939 * Tune reconstruction:
7940 */
7941 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007942 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7943 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944
7945 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946 last_check = 0;
7947
7948 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007949 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007950 "md: resuming %s of %s from checkpoint.\n",
7951 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007953 } else
7954 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007955 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007956 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7957 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007958 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959
majianpeng7c2c57c2012-07-03 12:12:26 +10007960 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007962 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963
NeilBrown57afd892005-06-21 17:17:13 -07007964 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007965
NeilBrown7a91ee12009-05-26 12:57:21 +10007966 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7967 ((mddev->curr_resync > mddev->curr_resync_completed &&
7968 (mddev->curr_resync - mddev->curr_resync_completed)
7969 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007970 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007971 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007972 >= mddev->resync_max - mddev->curr_resync_completed ||
7973 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007974 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007975 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007976 wait_event(mddev->recovery_wait,
7977 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007978 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007979 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7980 j > mddev->recovery_cp)
7981 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007982 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007983 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007984 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007985 }
NeilBrownacb180b2009-04-14 16:28:34 +10007986
NeilBrownc91abf52013-11-19 12:02:01 +11007987 while (j >= mddev->resync_max &&
7988 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007989 /* As this condition is controlled by user-space,
7990 * we can block indefinitely, so use '_interruptible'
7991 * to avoid triggering warnings.
7992 */
7993 flush_signals(current); /* just in case */
7994 wait_event_interruptible(mddev->recovery_wait,
7995 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007996 || test_bit(MD_RECOVERY_INTR,
7997 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007998 }
NeilBrownacb180b2009-04-14 16:28:34 +10007999
NeilBrownc91abf52013-11-19 12:02:01 +11008000 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8001 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008002
NeilBrown09314792015-02-19 16:04:40 +11008003 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008004 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008005 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008006 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 }
NeilBrown57afd892005-06-21 17:17:13 -07008008
8009 if (!skipped) { /* actual IO requested */
8010 io_sectors += sectors;
8011 atomic_add(sectors, &mddev->recovery_active);
8012 }
8013
NeilBrowne875ece2011-07-28 11:39:24 +10008014 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8015 break;
8016
Linus Torvalds1da177e2005-04-16 15:20:36 -07008017 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008018 if (j > max_sectors)
8019 /* when skipping, extra large numbers can be returned. */
8020 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008021 if (j > 2)
8022 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008023 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008024 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008025 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008026 * visible in /proc/mdstat
8027 */
8028 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008029
8030 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 continue;
8032
NeilBrown57afd892005-06-21 17:17:13 -07008033 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 repeat:
8035 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8036 /* step marks */
8037 int next = (last_mark+1) % SYNC_MARKS;
8038
8039 mddev->resync_mark = mark[next];
8040 mddev->resync_mark_cnt = mark_cnt[next];
8041 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008042 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043 last_mark = next;
8044 }
8045
NeilBrownc91abf52013-11-19 12:02:01 +11008046 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8047 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048
8049 /*
8050 * this loop exits only if either when we are slower than
8051 * the 'hard' speed limit, or the system was IO-idle for
8052 * a jiffy.
8053 * the system might be non-idle CPU-wise, but we only care
8054 * about not overloading the IO subsystem. (things like an
8055 * e2fsck being done on the RAID array should execute fast)
8056 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008057 cond_resched();
8058
Xiao Niac7e50a2014-08-07 09:37:41 -04008059 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8060 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008061 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062
NeilBrown88202a02006-01-06 00:21:36 -08008063 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008064 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008065 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066 goto repeat;
8067 }
NeilBrownac8fa412015-02-19 16:55:00 +11008068 if (!is_mddev_idle(mddev, 0)) {
8069 /*
8070 * Give other IO more of a chance.
8071 * The faster the devices, the less we wait.
8072 */
8073 wait_event(mddev->recovery_wait,
8074 !atomic_read(&mddev->recovery_active));
8075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076 }
8077 }
NeilBrownc91abf52013-11-19 12:02:01 +11008078 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
8079 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8080 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081 /*
8082 * this also signals 'finished resyncing' to md_stop
8083 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008084 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8086
NeilBrown5ed1df22015-07-24 13:27:08 +10008087 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8088 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8089 mddev->curr_resync > 2) {
8090 mddev->curr_resync_completed = mddev->curr_resync;
8091 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8092 }
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008093 /* tell personality and other nodes that we are finished */
8094 if (mddev_is_clustered(mddev)) {
8095 md_cluster_ops->resync_finish(mddev);
8096 cluster_resync_finished = true;
8097 }
NeilBrown09314792015-02-19 16:04:40 +11008098 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099
NeilBrowndfc70642008-05-23 13:04:39 -07008100 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008101 mddev->curr_resync > 2) {
8102 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8103 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8104 if (mddev->curr_resync >= mddev->recovery_cp) {
8105 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008106 "md: checkpointing %s of %s.\n",
8107 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008108 if (test_bit(MD_RECOVERY_ERROR,
8109 &mddev->recovery))
8110 mddev->recovery_cp =
8111 mddev->curr_resync_completed;
8112 else
8113 mddev->recovery_cp =
8114 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008115 }
8116 } else
8117 mddev->recovery_cp = MaxSector;
8118 } else {
8119 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8120 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008121 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008122 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008123 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008124 mddev->delta_disks >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008125 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008126 !test_bit(Faulty, &rdev->flags) &&
8127 !test_bit(In_sync, &rdev->flags) &&
8128 rdev->recovery_offset < mddev->curr_resync)
8129 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008130 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008133 skip:
NeilBrown17571282006-12-10 02:20:52 -08008134 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008136 if (mddev_is_clustered(mddev) &&
8137 test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8138 !cluster_resync_finished)
8139 md_cluster_ops->resync_finish(mddev);
8140
NeilBrown23da4222014-12-15 12:57:01 +11008141 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008142 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8143 /* We completed so min/max setting can be forgotten if used. */
8144 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8145 mddev->resync_min = 0;
8146 mddev->resync_max = MaxSector;
8147 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8148 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008149 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008151 spin_unlock(&mddev->lock);
8152
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008155 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156}
NeilBrown29269552006-03-27 01:18:10 -08008157EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158
NeilBrown746d3202013-04-24 11:42:41 +10008159static int remove_and_add_spares(struct mddev *mddev,
8160 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008161{
NeilBrown3cb03002011-10-11 16:45:26 +11008162 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008163 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008164 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008165
NeilBrowndafb20f2012-03-19 12:46:39 +11008166 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008167 if ((this == NULL || rdev == this) &&
8168 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008169 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008170 (test_bit(Faulty, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008171 (!test_bit(In_sync, &rdev->flags) &&
8172 !test_bit(Journal, &rdev->flags))) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008173 atomic_read(&rdev->nr_pending)==0) {
8174 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008175 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008176 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008177 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008178 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008179 }
8180 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008181 if (removed && mddev->kobj.sd)
8182 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008183
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008184 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008185 goto no_add;
8186
NeilBrowndafb20f2012-03-19 12:46:39 +11008187 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008188 if (this && this != rdev)
8189 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008190 if (test_bit(Candidate, &rdev->flags))
8191 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008192 if (rdev->raid_disk >= 0 &&
8193 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008194 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008195 !test_bit(Faulty, &rdev->flags))
8196 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008197 if (rdev->raid_disk >= 0)
8198 continue;
8199 if (test_bit(Faulty, &rdev->flags))
8200 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008201 if (!test_bit(Journal, &rdev->flags)) {
8202 if (mddev->ro &&
8203 ! (rdev->saved_raid_disk >= 0 &&
8204 !test_bit(Bitmap_sync, &rdev->flags)))
8205 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008206
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008207 rdev->recovery_offset = 0;
8208 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008209 if (mddev->pers->
8210 hot_add_disk(mddev, rdev) == 0) {
8211 if (sysfs_link_rdev(mddev, rdev))
8212 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008213 if (!test_bit(Journal, &rdev->flags))
8214 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008215 md_new_event(mddev);
8216 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008217 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008218 }
NeilBrown746d3202013-04-24 11:42:41 +10008219no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008220 if (removed)
8221 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008222 return spares;
8223}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008224
NeilBrownac05f252014-09-30 08:10:42 +10008225static void md_start_sync(struct work_struct *ws)
8226{
8227 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008228 int ret = 0;
8229
8230 if (mddev_is_clustered(mddev)) {
8231 ret = md_cluster_ops->resync_start(mddev);
8232 if (ret) {
8233 mddev->sync_thread = NULL;
8234 goto out;
8235 }
8236 }
NeilBrownac05f252014-09-30 08:10:42 +10008237
8238 mddev->sync_thread = md_register_thread(md_do_sync,
8239 mddev,
8240 "resync");
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008241out:
NeilBrownac05f252014-09-30 08:10:42 +10008242 if (!mddev->sync_thread) {
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008243 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8244 printk(KERN_ERR "%s: could not start resync"
8245 " thread...\n",
8246 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008247 /* leave the spares where they are, it shouldn't hurt */
8248 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8249 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8250 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8251 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8252 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008253 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008254 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8255 &mddev->recovery))
8256 if (mddev->sysfs_action)
8257 sysfs_notify_dirent_safe(mddev->sysfs_action);
8258 } else
8259 md_wakeup_thread(mddev->sync_thread);
8260 sysfs_notify_dirent_safe(mddev->sysfs_action);
8261 md_new_event(mddev);
8262}
8263
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264/*
8265 * This routine is regularly called by all per-raid-array threads to
8266 * deal with generic issues like resync and super-block update.
8267 * Raid personalities that don't have a thread (linear/raid0) do not
8268 * need this as they never do any recovery or update the superblock.
8269 *
8270 * It does not do any resync itself, but rather "forks" off other threads
8271 * to do that as needed.
8272 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8273 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008274 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275 * and wakeups up this thread which will reap the thread and finish up.
8276 * This thread also removes any faulty devices (with nr_pending == 0).
8277 *
8278 * The overall approach is:
8279 * 1/ if the superblock needs updating, update it.
8280 * 2/ If a recovery thread is running, don't do anything else.
8281 * 3/ If recovery has finished, clean up, possibly marking spares active.
8282 * 4/ If there are any faulty devices, remove them.
8283 * 5/ If array is degraded, try to add spares devices
8284 * 6/ If array has spares or is not in-sync, start a resync thread.
8285 */
NeilBrownfd01b882011-10-11 16:47:53 +11008286void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008287{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008288 if (mddev->suspended)
8289 return;
8290
NeilBrown5f404022005-06-21 17:17:16 -07008291 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008292 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293
NeilBrownfca4d842005-06-21 17:17:11 -07008294 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008295 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008296 printk(KERN_INFO "md: %s in immediate safe mode\n",
8297 mdname(mddev));
8298 mddev->safemode = 2;
8299 }
8300 flush_signals(current);
8301 }
8302
NeilBrownc89a8ee2008-08-05 15:54:13 +10008303 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8304 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008306 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008308 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008309 test_bit(MD_RELOAD_SB, &mddev->flags) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008310 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008311 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8312 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313 ))
8314 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008315
NeilBrowndf5b89b2006-03-27 01:18:20 -08008316 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008317 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008318
NeilBrownc89a8ee2008-08-05 15:54:13 +10008319 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008320 struct md_rdev *rdev;
8321 if (!mddev->external && mddev->in_sync)
8322 /* 'Blocked' flag not needed as failed devices
8323 * will be recorded if array switched to read/write.
8324 * Leaving it set will prevent the device
8325 * from being removed.
8326 */
8327 rdev_for_each(rdev, mddev)
8328 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008329 /* On a read-only array we can:
8330 * - remove failed devices
8331 * - add already-in_sync devices if the array itself
8332 * is in-sync.
8333 * As we only add devices that are already in-sync,
8334 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008335 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008336 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008337 /* There is no thread, but we need to call
8338 * ->spare_active and clear saved_raid_disk
8339 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008340 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008341 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008342 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008343 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008344 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008345 goto unlock;
8346 }
8347
Guoqing Jiang659b2542015-12-21 10:50:59 +11008348 if (mddev_is_clustered(mddev)) {
8349 struct md_rdev *rdev;
8350 /* kick the device if another node issued a
8351 * remove disk.
8352 */
8353 rdev_for_each(rdev, mddev) {
8354 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8355 rdev->raid_disk < 0)
8356 md_kick_rdev_from_array(rdev);
8357 }
Guoqing Jiang15858fa2015-12-21 10:51:00 +11008358
8359 if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags))
8360 md_reload_sb(mddev, mddev->good_device_nr);
Guoqing Jiang659b2542015-12-21 10:50:59 +11008361 }
8362
NeilBrown31a59e32008-04-30 00:52:30 -07008363 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008364 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008365 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008366 if (mddev->safemode &&
8367 !atomic_read(&mddev->writes_pending) &&
8368 !mddev->in_sync &&
8369 mddev->recovery_cp == MaxSector) {
8370 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008371 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008372 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008373 }
8374 if (mddev->safemode == 1)
8375 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008376 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008377 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008378 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008379 }
NeilBrownfca4d842005-06-21 17:17:11 -07008380
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008381 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07008382 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008383
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8385 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8386 /* resync/recovery still happening */
8387 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8388 goto unlock;
8389 }
8390 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008391 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392 goto unlock;
8393 }
Neil Brown72a23c22008-06-28 08:31:41 +10008394 /* Set RUNNING before clearing NEEDED to avoid
8395 * any transients in the value of "sync_action".
8396 */
NeilBrown72f36d52012-10-11 14:25:57 +11008397 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008398 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008399 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008400 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008401 /* Clear some bits that don't mean anything, but
8402 * might be left set
8403 */
NeilBrown24dd4692005-11-08 21:39:26 -08008404 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8405 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406
NeilBrowned209582012-04-24 10:23:14 +10008407 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8408 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008409 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410 /* no recovery is running.
8411 * remove any failed drives, then
8412 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008413 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 * the personality to fail the re-add.
8415 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008417 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008418 if (mddev->pers->check_reshape == NULL ||
8419 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008420 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008421 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008422 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008423 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008424 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008425 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8426 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008427 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008428 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008429 } else if (mddev->recovery_cp < MaxSector) {
8430 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008431 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008432 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8433 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008434 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008435
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008437 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008438 /* We are adding a device or devices to an array
8439 * which has the bitmap stored on all devices.
8440 * So make sure all bitmap pages get written
8441 */
8442 bitmap_write_all(mddev->bitmap);
8443 }
NeilBrownac05f252014-09-30 08:10:42 +10008444 INIT_WORK(&mddev->del_work, md_start_sync);
8445 queue_work(md_misc_wq, &mddev->del_work);
8446 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447 }
NeilBrownac05f252014-09-30 08:10:42 +10008448 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008449 if (!mddev->sync_thread) {
8450 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008451 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008452 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8453 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008454 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008455 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008456 }
NeilBrownac05f252014-09-30 08:10:42 +10008457 unlock:
8458 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459 mddev_unlock(mddev);
8460 }
8461}
NeilBrown6c144d32014-09-30 16:15:38 +10008462EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008464void md_reap_sync_thread(struct mddev *mddev)
8465{
8466 struct md_rdev *rdev;
8467
8468 /* resync has finished, collect result */
8469 md_unregister_thread(&mddev->sync_thread);
8470 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8471 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8472 /* success...*/
8473 /* activate any spares */
8474 if (mddev->pers->spare_active(mddev)) {
8475 sysfs_notify(&mddev->kobj, NULL,
8476 "degraded");
8477 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8478 }
8479 }
8480 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8481 mddev->pers->finish_reshape)
8482 mddev->pers->finish_reshape(mddev);
8483
8484 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008485 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008486 */
NeilBrownf4667222013-12-09 12:04:56 +11008487 if (!mddev->degraded)
8488 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008489 rdev->saved_raid_disk = -1;
8490
8491 md_update_sb(mddev, 1);
8492 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008493 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008494 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8495 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8496 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8497 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008498 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008499 /* flag recovery needed just to double check */
8500 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8501 sysfs_notify_dirent_safe(mddev->sysfs_action);
8502 md_new_event(mddev);
8503 if (mddev->event_work.func)
8504 queue_work(md_misc_wq, &mddev->event_work);
8505}
NeilBrown6c144d32014-09-30 16:15:38 +10008506EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008507
NeilBrownfd01b882011-10-11 16:47:53 +11008508void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008509{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008510 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008511 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008512 !test_bit(Blocked, &rdev->flags) &&
8513 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008514 msecs_to_jiffies(5000));
8515 rdev_dec_pending(rdev, mddev);
8516}
8517EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8518
NeilBrownc6563a82012-05-21 09:27:00 +10008519void md_finish_reshape(struct mddev *mddev)
8520{
8521 /* called be personality module when reshape completes. */
8522 struct md_rdev *rdev;
8523
8524 rdev_for_each(rdev, mddev) {
8525 if (rdev->data_offset > rdev->new_data_offset)
8526 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8527 else
8528 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8529 rdev->data_offset = rdev->new_data_offset;
8530 }
8531}
8532EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008533
8534/* Bad block management.
8535 * We can record which blocks on each device are 'bad' and so just
8536 * fail those blocks, or that stripe, rather than the whole device.
8537 * Entries in the bad-block table are 64bits wide. This comprises:
8538 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8539 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8540 * A 'shift' can be set so that larger blocks are tracked and
8541 * consequently larger devices can be covered.
8542 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8543 *
8544 * Locking of the bad-block table uses a seqlock so md_is_badblock
8545 * might need to retry if it is very unlucky.
8546 * We will sometimes want to check for bad blocks in a bi_end_io function,
8547 * so we use the write_seqlock_irq variant.
8548 *
8549 * When looking for a bad block we specify a range and want to
8550 * know if any block in the range is bad. So we binary-search
8551 * to the last range that starts at-or-before the given endpoint,
8552 * (or "before the sector after the target range")
8553 * then see if it ends after the given start.
8554 * We return
8555 * 0 if there are no known bad blocks in the range
8556 * 1 if there are known bad block which are all acknowledged
8557 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8558 * plus the start/length of the first bad section we overlap.
8559 */
8560int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8561 sector_t *first_bad, int *bad_sectors)
8562{
8563 int hi;
majianpengab056132012-11-06 17:13:44 +08008564 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008565 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008566 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008567 sector_t target = s + sectors;
8568 unsigned seq;
8569
8570 if (bb->shift > 0) {
8571 /* round the start down, and the end up */
8572 s >>= bb->shift;
8573 target += (1<<bb->shift) - 1;
8574 target >>= bb->shift;
8575 sectors = target - s;
8576 }
8577 /* 'target' is now the first block after the bad range */
8578
8579retry:
8580 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008581 lo = 0;
8582 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008583 hi = bb->count;
8584
8585 /* Binary search between lo and hi for 'target'
8586 * i.e. for the last range that starts before 'target'
8587 */
8588 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8589 * are known not to be the last range before target.
8590 * VARIANT: hi-lo is the number of possible
8591 * ranges, and decreases until it reaches 1
8592 */
8593 while (hi - lo > 1) {
8594 int mid = (lo + hi) / 2;
8595 sector_t a = BB_OFFSET(p[mid]);
8596 if (a < target)
8597 /* This could still be the one, earlier ranges
8598 * could not. */
8599 lo = mid;
8600 else
8601 /* This and later ranges are definitely out. */
8602 hi = mid;
8603 }
8604 /* 'lo' might be the last that started before target, but 'hi' isn't */
8605 if (hi > lo) {
8606 /* need to check all range that end after 's' to see if
8607 * any are unacknowledged.
8608 */
8609 while (lo >= 0 &&
8610 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8611 if (BB_OFFSET(p[lo]) < target) {
8612 /* starts before the end, and finishes after
8613 * the start, so they must overlap
8614 */
8615 if (rv != -1 && BB_ACK(p[lo]))
8616 rv = 1;
8617 else
8618 rv = -1;
8619 *first_bad = BB_OFFSET(p[lo]);
8620 *bad_sectors = BB_LEN(p[lo]);
8621 }
8622 lo--;
8623 }
8624 }
8625
8626 if (read_seqretry(&bb->lock, seq))
8627 goto retry;
8628
8629 return rv;
8630}
8631EXPORT_SYMBOL_GPL(md_is_badblock);
8632
8633/*
8634 * Add a range of bad blocks to the table.
8635 * This might extend the table, or might contract it
8636 * if two adjacent ranges can be merged.
8637 * We binary-search to find the 'insertion' point, then
8638 * decide how best to handle it.
8639 */
8640static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8641 int acknowledged)
8642{
8643 u64 *p;
8644 int lo, hi;
8645 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008646 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008647
8648 if (bb->shift < 0)
8649 /* badblocks are disabled */
8650 return 0;
8651
8652 if (bb->shift) {
8653 /* round the start down, and the end up */
8654 sector_t next = s + sectors;
8655 s >>= bb->shift;
8656 next += (1<<bb->shift) - 1;
8657 next >>= bb->shift;
8658 sectors = next - s;
8659 }
8660
Bian Yu905b0292013-10-12 01:10:03 -04008661 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008662
8663 p = bb->page;
8664 lo = 0;
8665 hi = bb->count;
8666 /* Find the last range that starts at-or-before 's' */
8667 while (hi - lo > 1) {
8668 int mid = (lo + hi) / 2;
8669 sector_t a = BB_OFFSET(p[mid]);
8670 if (a <= s)
8671 lo = mid;
8672 else
8673 hi = mid;
8674 }
8675 if (hi > lo && BB_OFFSET(p[lo]) > s)
8676 hi = lo;
8677
8678 if (hi > lo) {
8679 /* we found a range that might merge with the start
8680 * of our new range
8681 */
8682 sector_t a = BB_OFFSET(p[lo]);
8683 sector_t e = a + BB_LEN(p[lo]);
8684 int ack = BB_ACK(p[lo]);
8685 if (e >= s) {
8686 /* Yes, we can merge with a previous range */
8687 if (s == a && s + sectors >= e)
8688 /* new range covers old */
8689 ack = acknowledged;
8690 else
8691 ack = ack && acknowledged;
8692
8693 if (e < s + sectors)
8694 e = s + sectors;
8695 if (e - a <= BB_MAX_LEN) {
8696 p[lo] = BB_MAKE(a, e-a, ack);
8697 s = e;
8698 } else {
8699 /* does not all fit in one range,
8700 * make p[lo] maximal
8701 */
8702 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8703 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8704 s = a + BB_MAX_LEN;
8705 }
8706 sectors = e - s;
8707 }
8708 }
8709 if (sectors && hi < bb->count) {
8710 /* 'hi' points to the first range that starts after 's'.
8711 * Maybe we can merge with the start of that range */
8712 sector_t a = BB_OFFSET(p[hi]);
8713 sector_t e = a + BB_LEN(p[hi]);
8714 int ack = BB_ACK(p[hi]);
8715 if (a <= s + sectors) {
8716 /* merging is possible */
8717 if (e <= s + sectors) {
8718 /* full overlap */
8719 e = s + sectors;
8720 ack = acknowledged;
8721 } else
8722 ack = ack && acknowledged;
8723
8724 a = s;
8725 if (e - a <= BB_MAX_LEN) {
8726 p[hi] = BB_MAKE(a, e-a, ack);
8727 s = e;
8728 } else {
8729 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8730 s = a + BB_MAX_LEN;
8731 }
8732 sectors = e - s;
8733 lo = hi;
8734 hi++;
8735 }
8736 }
8737 if (sectors == 0 && hi < bb->count) {
8738 /* we might be able to combine lo and hi */
8739 /* Note: 's' is at the end of 'lo' */
8740 sector_t a = BB_OFFSET(p[hi]);
8741 int lolen = BB_LEN(p[lo]);
8742 int hilen = BB_LEN(p[hi]);
8743 int newlen = lolen + hilen - (s - a);
8744 if (s >= a && newlen < BB_MAX_LEN) {
8745 /* yes, we can combine them */
8746 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8747 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8748 memmove(p + hi, p + hi + 1,
8749 (bb->count - hi - 1) * 8);
8750 bb->count--;
8751 }
8752 }
8753 while (sectors) {
8754 /* didn't merge (it all).
8755 * Need to add a range just before 'hi' */
8756 if (bb->count >= MD_MAX_BADBLOCKS) {
8757 /* No room for more */
8758 rv = 0;
8759 break;
8760 } else {
8761 int this_sectors = sectors;
8762 memmove(p + hi + 1, p + hi,
8763 (bb->count - hi) * 8);
8764 bb->count++;
8765
8766 if (this_sectors > BB_MAX_LEN)
8767 this_sectors = BB_MAX_LEN;
8768 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8769 sectors -= this_sectors;
8770 s += this_sectors;
8771 }
8772 }
8773
8774 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008775 if (!acknowledged)
8776 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008777 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008778
8779 return rv;
8780}
8781
NeilBrown3cb03002011-10-11 16:45:26 +11008782int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008783 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008784{
NeilBrownc6563a82012-05-21 09:27:00 +10008785 int rv;
8786 if (is_new)
8787 s += rdev->new_data_offset;
8788 else
8789 s += rdev->data_offset;
8790 rv = md_set_badblocks(&rdev->badblocks,
8791 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008792 if (rv) {
8793 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008794 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008795 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
NeilBrown55ce74d2015-08-14 11:11:10 +10008796 set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008797 md_wakeup_thread(rdev->mddev->thread);
8798 }
8799 return rv;
8800}
8801EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8802
8803/*
8804 * Remove a range of bad blocks from the table.
8805 * This may involve extending the table if we spilt a region,
8806 * but it must not fail. So if the table becomes full, we just
8807 * drop the remove request.
8808 */
8809static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8810{
8811 u64 *p;
8812 int lo, hi;
8813 sector_t target = s + sectors;
8814 int rv = 0;
8815
8816 if (bb->shift > 0) {
8817 /* When clearing we round the start up and the end down.
8818 * This should not matter as the shift should align with
8819 * the block size and no rounding should ever be needed.
8820 * However it is better the think a block is bad when it
8821 * isn't than to think a block is not bad when it is.
8822 */
8823 s += (1<<bb->shift) - 1;
8824 s >>= bb->shift;
8825 target >>= bb->shift;
8826 sectors = target - s;
8827 }
8828
8829 write_seqlock_irq(&bb->lock);
8830
8831 p = bb->page;
8832 lo = 0;
8833 hi = bb->count;
8834 /* Find the last range that starts before 'target' */
8835 while (hi - lo > 1) {
8836 int mid = (lo + hi) / 2;
8837 sector_t a = BB_OFFSET(p[mid]);
8838 if (a < target)
8839 lo = mid;
8840 else
8841 hi = mid;
8842 }
8843 if (hi > lo) {
8844 /* p[lo] is the last range that could overlap the
8845 * current range. Earlier ranges could also overlap,
8846 * but only this one can overlap the end of the range.
8847 */
8848 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8849 /* Partial overlap, leave the tail of this range */
8850 int ack = BB_ACK(p[lo]);
8851 sector_t a = BB_OFFSET(p[lo]);
8852 sector_t end = a + BB_LEN(p[lo]);
8853
8854 if (a < s) {
8855 /* we need to split this range */
8856 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008857 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008858 goto out;
8859 }
8860 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8861 bb->count++;
8862 p[lo] = BB_MAKE(a, s-a, ack);
8863 lo++;
8864 }
8865 p[lo] = BB_MAKE(target, end - target, ack);
8866 /* there is no longer an overlap */
8867 hi = lo;
8868 lo--;
8869 }
8870 while (lo >= 0 &&
8871 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8872 /* This range does overlap */
8873 if (BB_OFFSET(p[lo]) < s) {
8874 /* Keep the early parts of this range. */
8875 int ack = BB_ACK(p[lo]);
8876 sector_t start = BB_OFFSET(p[lo]);
8877 p[lo] = BB_MAKE(start, s - start, ack);
8878 /* now low doesn't overlap, so.. */
8879 break;
8880 }
8881 lo--;
8882 }
8883 /* 'lo' is strictly before, 'hi' is strictly after,
8884 * anything between needs to be discarded
8885 */
8886 if (hi - lo > 1) {
8887 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8888 bb->count -= (hi - lo - 1);
8889 }
8890 }
8891
8892 bb->changed = 1;
8893out:
8894 write_sequnlock_irq(&bb->lock);
8895 return rv;
8896}
8897
NeilBrownc6563a82012-05-21 09:27:00 +10008898int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8899 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008900{
NeilBrownc6563a82012-05-21 09:27:00 +10008901 if (is_new)
8902 s += rdev->new_data_offset;
8903 else
8904 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008905 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008906 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008907}
8908EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8909
8910/*
8911 * Acknowledge all bad blocks in a list.
8912 * This only succeeds if ->changed is clear. It is used by
8913 * in-kernel metadata updates
8914 */
8915void md_ack_all_badblocks(struct badblocks *bb)
8916{
8917 if (bb->page == NULL || bb->changed)
8918 /* no point even trying */
8919 return;
8920 write_seqlock_irq(&bb->lock);
8921
majianpengecb178b2012-03-19 12:46:42 +11008922 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008923 u64 *p = bb->page;
8924 int i;
8925 for (i = 0; i < bb->count ; i++) {
8926 if (!BB_ACK(p[i])) {
8927 sector_t start = BB_OFFSET(p[i]);
8928 int len = BB_LEN(p[i]);
8929 p[i] = BB_MAKE(start, len, 1);
8930 }
8931 }
NeilBrownde393cd2011-07-28 11:31:48 +10008932 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008933 }
8934 write_sequnlock_irq(&bb->lock);
8935}
8936EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8937
NeilBrown16c791a2011-07-28 11:31:47 +10008938/* sysfs access to bad-blocks list.
8939 * We present two files.
8940 * 'bad-blocks' lists sector numbers and lengths of ranges that
8941 * are recorded as bad. The list is truncated to fit within
8942 * the one-page limit of sysfs.
8943 * Writing "sector length" to this file adds an acknowledged
8944 * bad block list.
8945 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8946 * been acknowledged. Writing to this file adds bad blocks
8947 * without acknowledging them. This is largely for testing.
8948 */
8949
8950static ssize_t
8951badblocks_show(struct badblocks *bb, char *page, int unack)
8952{
8953 size_t len;
8954 int i;
8955 u64 *p = bb->page;
8956 unsigned seq;
8957
8958 if (bb->shift < 0)
8959 return 0;
8960
8961retry:
8962 seq = read_seqbegin(&bb->lock);
8963
8964 len = 0;
8965 i = 0;
8966
8967 while (len < PAGE_SIZE && i < bb->count) {
8968 sector_t s = BB_OFFSET(p[i]);
8969 unsigned int length = BB_LEN(p[i]);
8970 int ack = BB_ACK(p[i]);
8971 i++;
8972
8973 if (unack && ack)
8974 continue;
8975
8976 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8977 (unsigned long long)s << bb->shift,
8978 length << bb->shift);
8979 }
NeilBrownde393cd2011-07-28 11:31:48 +10008980 if (unack && len == 0)
8981 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008982
8983 if (read_seqretry(&bb->lock, seq))
8984 goto retry;
8985
8986 return len;
8987}
8988
8989#define DO_DEBUG 1
8990
8991static ssize_t
8992badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8993{
8994 unsigned long long sector;
8995 int length;
8996 char newline;
8997#ifdef DO_DEBUG
8998 /* Allow clearing via sysfs *only* for testing/debugging.
8999 * Normally only a successful write may clear a badblock
9000 */
9001 int clear = 0;
9002 if (page[0] == '-') {
9003 clear = 1;
9004 page++;
9005 }
9006#endif /* DO_DEBUG */
9007
9008 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
9009 case 3:
9010 if (newline != '\n')
9011 return -EINVAL;
9012 case 2:
9013 if (length <= 0)
9014 return -EINVAL;
9015 break;
9016 default:
9017 return -EINVAL;
9018 }
9019
9020#ifdef DO_DEBUG
9021 if (clear) {
9022 md_clear_badblocks(bb, sector, length);
9023 return len;
9024 }
9025#endif /* DO_DEBUG */
9026 if (md_set_badblocks(bb, sector, length, !unack))
9027 return len;
9028 else
9029 return -ENOSPC;
9030}
9031
Adrian Bunk75c96f82005-05-05 16:16:09 -07009032static int md_notify_reboot(struct notifier_block *this,
9033 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009034{
9035 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009036 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009037 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038
NeilBrownc744a652012-03-19 12:46:37 +11009039 for_each_mddev(mddev, tmp) {
9040 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009041 if (mddev->pers)
9042 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009043 if (mddev->persistent)
9044 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009045 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009046 }
NeilBrownc744a652012-03-19 12:46:37 +11009047 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009048 }
NeilBrownc744a652012-03-19 12:46:37 +11009049 /*
9050 * certain more exotic SCSI devices are known to be
9051 * volatile wrt too early system reboots. While the
9052 * right place to handle this issue is the given
9053 * driver, we do want to have a safe RAID driver ...
9054 */
9055 if (need_delay)
9056 mdelay(1000*1);
9057
Linus Torvalds1da177e2005-04-16 15:20:36 -07009058 return NOTIFY_DONE;
9059}
9060
Adrian Bunk75c96f82005-05-05 16:16:09 -07009061static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009062 .notifier_call = md_notify_reboot,
9063 .next = NULL,
9064 .priority = INT_MAX, /* before any real devices */
9065};
9066
9067static void md_geninit(void)
9068{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009069 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009070
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009071 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072}
9073
Adrian Bunk75c96f82005-05-05 16:16:09 -07009074static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009075{
Tejun Heoe804ac72010-10-15 15:36:08 +02009076 int ret = -ENOMEM;
9077
Tejun Heoada609e2011-01-25 14:35:54 +01009078 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009079 if (!md_wq)
9080 goto err_wq;
9081
9082 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9083 if (!md_misc_wq)
9084 goto err_misc_wq;
9085
9086 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9087 goto err_md;
9088
9089 if ((ret = register_blkdev(0, "mdp")) < 0)
9090 goto err_mdp;
9091 mdp_major = ret;
9092
NeilBrownaf5628f2014-07-31 13:54:54 +10009093 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009094 md_probe, NULL, NULL);
9095 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096 md_probe, NULL, NULL);
9097
Linus Torvalds1da177e2005-04-16 15:20:36 -07009098 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009099 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100
9101 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103
Tejun Heoe804ac72010-10-15 15:36:08 +02009104err_mdp:
9105 unregister_blkdev(MD_MAJOR, "md");
9106err_md:
9107 destroy_workqueue(md_misc_wq);
9108err_misc_wq:
9109 destroy_workqueue(md_wq);
9110err_wq:
9111 return ret;
9112}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009113
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009114static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009115{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009116 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9117 struct md_rdev *rdev2;
9118 int role, ret;
9119 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009120
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009121 /* Check for change of roles in the active devices */
9122 rdev_for_each(rdev2, mddev) {
9123 if (test_bit(Faulty, &rdev2->flags))
9124 continue;
9125
9126 /* Check if the roles changed */
9127 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009128
9129 if (test_bit(Candidate, &rdev2->flags)) {
9130 if (role == 0xfffe) {
9131 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9132 md_kick_rdev_from_array(rdev2);
9133 continue;
9134 }
9135 else
9136 clear_bit(Candidate, &rdev2->flags);
9137 }
9138
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009139 if (role != rdev2->raid_disk) {
9140 /* got activated */
9141 if (rdev2->raid_disk == -1 && role != 0xffff) {
9142 rdev2->saved_raid_disk = role;
9143 ret = remove_and_add_spares(mddev, rdev2);
9144 pr_info("Activated spare: %s\n",
9145 bdevname(rdev2->bdev,b));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009146 }
9147 /* device faulty
9148 * We just want to do the minimum to mark the disk
9149 * as faulty. The recovery is performed by the
9150 * one who initiated the error.
9151 */
9152 if ((role == 0xfffe) || (role == 0xfffd)) {
9153 md_error(mddev, rdev2);
9154 clear_bit(Blocked, &rdev2->flags);
9155 }
9156 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009157 }
9158
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009159 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9160 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009161
9162 /* Finally set the event to be up to date */
9163 mddev->events = le64_to_cpu(sb->events);
9164}
9165
9166static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9167{
9168 int err;
9169 struct page *swapout = rdev->sb_page;
9170 struct mdp_superblock_1 *sb;
9171
9172 /* Store the sb page of the rdev in the swapout temporary
9173 * variable in case we err in the future
9174 */
9175 rdev->sb_page = NULL;
9176 alloc_disk_sb(rdev);
9177 ClearPageUptodate(rdev->sb_page);
9178 rdev->sb_loaded = 0;
9179 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
9180
9181 if (err < 0) {
9182 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9183 __func__, __LINE__, rdev->desc_nr, err);
9184 put_page(rdev->sb_page);
9185 rdev->sb_page = swapout;
9186 rdev->sb_loaded = 1;
9187 return err;
9188 }
9189
9190 sb = page_address(rdev->sb_page);
9191 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9192 * is not set
9193 */
9194
9195 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9196 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9197
9198 /* The other node finished recovery, call spare_active to set
9199 * device In_sync and mddev->degraded
9200 */
9201 if (rdev->recovery_offset == MaxSector &&
9202 !test_bit(In_sync, &rdev->flags) &&
9203 mddev->pers->spare_active(mddev))
9204 sysfs_notify(&mddev->kobj, NULL, "degraded");
9205
9206 put_page(swapout);
9207 return 0;
9208}
9209
9210void md_reload_sb(struct mddev *mddev, int nr)
9211{
9212 struct md_rdev *rdev;
9213 int err;
9214
9215 /* Find the rdev */
9216 rdev_for_each_rcu(rdev, mddev) {
9217 if (rdev->desc_nr == nr)
9218 break;
9219 }
9220
9221 if (!rdev || rdev->desc_nr != nr) {
9222 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9223 return;
9224 }
9225
9226 err = read_rdev(mddev, rdev);
9227 if (err < 0)
9228 return;
9229
9230 check_sb_changes(mddev, rdev);
9231
9232 /* Read all rdev's to update recovery_offset */
9233 rdev_for_each_rcu(rdev, mddev)
9234 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009235}
9236EXPORT_SYMBOL(md_reload_sb);
9237
Linus Torvalds1da177e2005-04-16 15:20:36 -07009238#ifndef MODULE
9239
9240/*
9241 * Searches all registered partitions for autorun RAID arrays
9242 * at boot time.
9243 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009244
9245static LIST_HEAD(all_detected_devices);
9246struct detected_devices_node {
9247 struct list_head list;
9248 dev_t dev;
9249};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009250
9251void md_autodetect_dev(dev_t dev)
9252{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009253 struct detected_devices_node *node_detected_dev;
9254
9255 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9256 if (node_detected_dev) {
9257 node_detected_dev->dev = dev;
9258 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9259 } else {
9260 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
9261 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
9262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263}
9264
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265static void autostart_arrays(int part)
9266{
NeilBrown3cb03002011-10-11 16:45:26 +11009267 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009268 struct detected_devices_node *node_detected_dev;
9269 dev_t dev;
9270 int i_scanned, i_passed;
9271
9272 i_scanned = 0;
9273 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009274
9275 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
9276
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009277 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9278 i_scanned++;
9279 node_detected_dev = list_entry(all_detected_devices.next,
9280 struct detected_devices_node, list);
9281 list_del(&node_detected_dev->list);
9282 dev = node_detected_dev->dev;
9283 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07009284 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285 if (IS_ERR(rdev))
9286 continue;
9287
NeilBrown403df472014-09-30 15:52:29 +10009288 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009289 continue;
NeilBrown403df472014-09-30 15:52:29 +10009290
NeilBrownd0fae182008-03-04 14:29:31 -08009291 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009293 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009295
9296 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
9297 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298
9299 autorun_devices(part);
9300}
9301
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009302#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303
9304static __exit void md_exit(void)
9305{
NeilBrownfd01b882011-10-11 16:47:53 +11009306 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009308 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009309
NeilBrownaf5628f2014-07-31 13:54:54 +10009310 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009311 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009313 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314 unregister_blkdev(mdp_major, "mdp");
9315 unregister_reboot_notifier(&md_notifier);
9316 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009317
9318 /* We cannot unload the modules while some process is
9319 * waiting for us in select() or poll() - wake them up
9320 */
9321 md_unloading = 1;
9322 while (waitqueue_active(&md_event_waiters)) {
9323 /* not safe to leave yet */
9324 wake_up(&md_event_waiters);
9325 msleep(delay);
9326 delay += delay;
9327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009329
NeilBrown29ac4aa2008-02-06 01:39:58 -08009330 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009331 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009332 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009333 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009334 destroy_workqueue(md_misc_wq);
9335 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009336}
9337
Dan Williams685784a2007-07-09 11:56:42 -07009338subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339module_exit(md_exit)
9340
NeilBrownf91de922005-11-08 21:39:36 -08009341static int get_ro(char *buffer, struct kernel_param *kp)
9342{
9343 return sprintf(buffer, "%d", start_readonly);
9344}
9345static int set_ro(const char *val, struct kernel_param *kp)
9346{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009347 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009348}
9349
NeilBrown80ca3a42006-07-10 04:44:18 -07009350module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9351module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009352module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009353
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009355MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009356MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009357MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);