blob: e1d8723720ccbcd72a51239927e85342d29ab4e5 [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 Viroff01bb482011-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 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200253static void 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);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200265 return;
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);
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100271 return;
272 }
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
NeilBrown9e35b992010-04-06 14:23:02 +1000307/* mddev_suspend makes sure no new requests are submitted
308 * to the device, and that any requests that have been submitted
309 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100310 * Once mddev_detach() is called and completes, the module will be
311 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000312 */
NeilBrownfd01b882011-10-11 16:47:53 +1100313void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100314{
315 BUG_ON(mddev->suspended);
316 mddev->suspended = 1;
317 synchronize_rcu();
318 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
319 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500320
321 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100322}
NeilBrown390ee602010-06-01 19:37:27 +1000323EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100324
NeilBrownfd01b882011-10-11 16:47:53 +1100325void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100326{
327 mddev->suspended = 0;
328 wake_up(&mddev->sb_wait);
329 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500330
Jonathan Brassow47525e52012-05-22 13:55:29 +1000331 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500332 md_wakeup_thread(mddev->thread);
333 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100334}
NeilBrown390ee602010-06-01 19:37:27 +1000335EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100336
NeilBrownfd01b882011-10-11 16:47:53 +1100337int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000338{
NeilBrown5c675f82014-12-15 12:56:56 +1100339 struct md_personality *pers = mddev->pers;
340 int ret = 0;
341
342 rcu_read_lock();
343 if (mddev->suspended)
344 ret = 1;
345 else if (pers && pers->congested)
346 ret = pers->congested(mddev, bits);
347 rcu_read_unlock();
348 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000349}
NeilBrown5c675f82014-12-15 12:56:56 +1100350EXPORT_SYMBOL_GPL(mddev_congested);
351static int md_congested(void *data, int bits)
352{
353 struct mddev *mddev = data;
354 return mddev_congested(mddev, bits);
355}
NeilBrown3fa841d2009-09-23 18:10:29 +1000356
NeilBrown64590f42014-12-15 12:56:57 +1100357static int md_mergeable_bvec(struct request_queue *q,
358 struct bvec_merge_data *bvm,
359 struct bio_vec *biovec)
360{
361 struct mddev *mddev = q->queuedata;
362 int ret;
363 rcu_read_lock();
364 if (mddev->suspended) {
365 /* Must always allow one vec */
366 if (bvm->bi_size == 0)
367 ret = biovec->bv_len;
368 else
369 ret = 0;
370 } else {
371 struct md_personality *pers = mddev->pers;
372 if (pers && pers->mergeable_bvec)
373 ret = pers->mergeable_bvec(mddev, bvm, biovec);
374 else
375 ret = biovec->bv_len;
376 }
377 rcu_read_unlock();
378 return ret;
379}
NeilBrowna2826aa2009-12-14 12:49:49 +1100380/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200381 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100382 */
383
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200384static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100385{
NeilBrown3cb03002011-10-11 16:45:26 +1100386 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100387 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100388
389 rdev_dec_pending(rdev, mddev);
390
391 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200392 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200393 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100394 }
395 bio_put(bio);
396}
397
NeilBrowna7a07e62010-12-09 16:04:25 +1100398static void md_submit_flush_data(struct work_struct *ws);
399
NeilBrowna035fc32010-12-09 16:17:51 +1100400static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100401{
NeilBrownfd01b882011-10-11 16:47:53 +1100402 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100403 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100404
NeilBrowna7a07e62010-12-09 16:04:25 +1100405 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
406 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100407 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100408 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409 if (rdev->raid_disk >= 0 &&
410 !test_bit(Faulty, &rdev->flags)) {
411 /* Take two references, one is dropped
412 * when request finishes, one after
413 * we reclaim rcu_read_lock
414 */
415 struct bio *bi;
416 atomic_inc(&rdev->nr_pending);
417 atomic_inc(&rdev->nr_pending);
418 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000419 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200420 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100421 bi->bi_private = rdev;
422 bi->bi_bdev = rdev->bdev;
423 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 rcu_read_lock();
426 rdev_dec_pending(rdev, mddev);
427 }
428 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100429 if (atomic_dec_and_test(&mddev->flush_pending))
430 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100431}
432
Tejun Heoe9c74692010-09-03 11:56:18 +0200433static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100434{
NeilBrownfd01b882011-10-11 16:47:53 +1100435 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200436 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100437
Kent Overstreet4f024f32013-10-11 15:44:27 -0700438 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100439 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200440 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100441 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200442 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200443 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 }
NeilBrown2b74e122010-12-09 15:59:01 +1100445
446 mddev->flush_bio = NULL;
447 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100448}
449
NeilBrownfd01b882011-10-11 16:47:53 +1100450void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100451{
NeilBrown85572d72014-12-15 12:56:56 +1100452 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100453 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200454 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100455 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200456 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100457 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100458
NeilBrowna035fc32010-12-09 16:17:51 +1100459 INIT_WORK(&mddev->flush_work, submit_flushes);
460 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100461}
Tejun Heoe9c74692010-09-03 11:56:18 +0200462EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100463
NeilBrown74018dc2012-07-31 09:08:15 +0200464void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000465{
NeilBrown9cbb1752012-07-31 09:08:14 +0200466 struct mddev *mddev = cb->data;
467 md_wakeup_thread(mddev->thread);
468 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000469}
NeilBrown9cbb1752012-07-31 09:08:14 +0200470EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000471
NeilBrownfd01b882011-10-11 16:47:53 +1100472static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 atomic_inc(&mddev->active);
475 return mddev;
476}
477
Dan Williams5fd3a172009-03-04 00:57:25 -0700478static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100479
NeilBrownfd01b882011-10-11 16:47:53 +1100480static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
NeilBrowna167f662010-10-26 18:31:13 +1100482 struct bio_set *bs = NULL;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
485 return;
NeilBrownd3374822009-01-09 08:31:10 +1100486 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100487 mddev->ctime == 0 && !mddev->hold_active) {
488 /* Array is not configured at all, and not held active,
489 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100490 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100491 bs = mddev->bio_set;
492 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100493 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200494 /* We did a probe so need to clean up. Call
495 * queue_work inside the spinlock so that
496 * flush_workqueue() after mddev_find will
497 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100498 */
499 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200500 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100501 } else
502 kfree(mddev);
503 }
504 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100505 if (bs)
506 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
NeilBrownfd01b882011-10-11 16:47:53 +1100509void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100510{
511 mutex_init(&mddev->open_mutex);
512 mutex_init(&mddev->reconfig_mutex);
513 mutex_init(&mddev->bitmap_info.mutex);
514 INIT_LIST_HEAD(&mddev->disks);
515 INIT_LIST_HEAD(&mddev->all_mddevs);
516 init_timer(&mddev->safemode_timer);
517 atomic_set(&mddev->active, 1);
518 atomic_set(&mddev->openers, 0);
519 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100520 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100521 atomic_set(&mddev->flush_pending, 0);
522 init_waitqueue_head(&mddev->sb_wait);
523 init_waitqueue_head(&mddev->recovery_wait);
524 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000525 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500526 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100527 mddev->resync_min = 0;
528 mddev->resync_max = MaxSector;
529 mddev->level = LEVEL_NONE;
530}
NeilBrown390ee602010-06-01 19:37:27 +1000531EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100532
NeilBrownf72ffdd2014-09-30 14:23:59 +1000533static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
NeilBrownfd01b882011-10-11 16:47:53 +1100535 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
NeilBrown8f5f02c2011-02-16 13:58:51 +1100537 if (unit && MAJOR(unit) != MD_MAJOR)
538 unit &= ~((1<<MdpMinorShift)-1);
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 retry:
541 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
NeilBrownefeb53c2009-01-09 08:31:10 +1100543 if (unit) {
544 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
545 if (mddev->unit == unit) {
546 mddev_get(mddev);
547 spin_unlock(&all_mddevs_lock);
548 kfree(new);
549 return mddev;
550 }
551
552 if (new) {
553 list_add(&new->all_mddevs, &all_mddevs);
554 spin_unlock(&all_mddevs_lock);
555 new->hold_active = UNTIL_IOCTL;
556 return new;
557 }
558 } else if (new) {
559 /* find an unused unit number */
560 static int next_minor = 512;
561 int start = next_minor;
562 int is_free = 0;
563 int dev = 0;
564 while (!is_free) {
565 dev = MKDEV(MD_MAJOR, next_minor);
566 next_minor++;
567 if (next_minor > MINORMASK)
568 next_minor = 0;
569 if (next_minor == start) {
570 /* Oh dear, all in use. */
571 spin_unlock(&all_mddevs_lock);
572 kfree(new);
573 return NULL;
574 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000575
NeilBrownefeb53c2009-01-09 08:31:10 +1100576 is_free = 1;
577 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
578 if (mddev->unit == dev) {
579 is_free = 0;
580 break;
581 }
582 }
583 new->unit = dev;
584 new->md_minor = MINOR(dev);
585 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 list_add(&new->all_mddevs, &all_mddevs);
587 spin_unlock(&all_mddevs_lock);
588 return new;
589 }
590 spin_unlock(&all_mddevs_lock);
591
NeilBrown9ffae0c2006-01-06 00:20:32 -0800592 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!new)
594 return NULL;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 new->unit = unit;
597 if (MAJOR(unit) == MD_MAJOR)
598 new->md_minor = MINOR(unit);
599 else
600 new->md_minor = MINOR(unit) >> MdpMinorShift;
601
NeilBrownfafd7fb2010-04-01 15:55:30 +1100602 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto retry;
605}
606
NeilBrownb6eb1272010-04-15 10:13:47 +1000607static struct attribute_group md_redundancy_group;
608
NeilBrown5c47daf2014-12-15 12:57:01 +1100609void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
NeilBrowna64c8762010-04-14 17:15:37 +1000611 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000612 /* These cannot be removed under reconfig_mutex as
613 * an access to the files will try to take reconfig_mutex
614 * while holding the file unremovable, which leads to
615 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000616 * So hold set sysfs_active while the remove in happeing,
617 * and anything else which might set ->to_remove or my
618 * otherwise change the sysfs namespace will fail with
619 * -EBUSY if sysfs_active is still set.
620 * We set sysfs_active under reconfig_mutex and elsewhere
621 * test it under the same mutex to ensure its correct value
622 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000623 */
NeilBrowna64c8762010-04-14 17:15:37 +1000624 struct attribute_group *to_remove = mddev->to_remove;
625 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000626 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000627 mutex_unlock(&mddev->reconfig_mutex);
628
NeilBrown00bcb4a2010-06-01 19:37:23 +1000629 if (mddev->kobj.sd) {
630 if (to_remove != &md_redundancy_group)
631 sysfs_remove_group(&mddev->kobj, to_remove);
632 if (mddev->pers == NULL ||
633 mddev->pers->sync_request == NULL) {
634 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
635 if (mddev->sysfs_action)
636 sysfs_put(mddev->sysfs_action);
637 mddev->sysfs_action = NULL;
638 }
NeilBrowna64c8762010-04-14 17:15:37 +1000639 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000640 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000641 } else
642 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Chris Dunlop751e67c2011-10-19 16:48:26 +1100644 /* As we've dropped the mutex we need a spinlock to
645 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000646 */
647 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700648 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000649 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
NeilBrown5c47daf2014-12-15 12:57:01 +1100651EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500653struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100654{
655 struct md_rdev *rdev;
656
657 rdev_for_each_rcu(rdev, mddev)
658 if (rdev->desc_nr == nr)
659 return rdev;
660
661 return NULL;
662}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500663EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100664
665static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
NeilBrown3cb03002011-10-11 16:45:26 +1100667 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
NeilBrowndafb20f2012-03-19 12:46:39 +1100669 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (rdev->bdev->bd_dev == dev)
671 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return NULL;
674}
675
NeilBrown1ca69c42012-10-11 13:37:33 +1100676static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
677{
678 struct md_rdev *rdev;
679
680 rdev_for_each_rcu(rdev, mddev)
681 if (rdev->bdev->bd_dev == dev)
682 return rdev;
683
684 return NULL;
685}
686
NeilBrown84fc4b52011-10-11 16:49:58 +1100687static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800688{
NeilBrown84fc4b52011-10-11 16:49:58 +1100689 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800690 list_for_each_entry(pers, &pers_list, list) {
691 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800692 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800693 if (strcmp(pers->name, clevel)==0)
694 return pers;
695 }
NeilBrown2604b702006-01-06 00:20:36 -0800696 return NULL;
697}
698
Andre Nollb73df2d2008-07-11 22:02:23 +1000699/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100700static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100702 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000703 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
NeilBrownf72ffdd2014-09-30 14:23:59 +1000706static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 rdev->sb_page = alloc_page(GFP_KERNEL);
709 if (!rdev->sb_page) {
710 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000711 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
714 return 0;
715}
716
NeilBrown545c8792012-05-22 13:54:30 +1000717void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800720 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 rdev->sb_loaded = 0;
722 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000723 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100724 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
NeilBrown2699b672011-07-28 11:31:47 +1000726 if (rdev->bb_page) {
727 put_page(rdev->bb_page);
728 rdev->bb_page = NULL;
729 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000730 kfree(rdev->badblocks.page);
731 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
NeilBrown545c8792012-05-22 13:54:30 +1000733EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200735static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700736{
NeilBrown3cb03002011-10-11 16:45:26 +1100737 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100738 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700739
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200740 if (bio->bi_error) {
741 printk("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800742 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700743 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700744
NeilBrowna9701a32005-11-08 21:39:34 -0800745 if (atomic_dec_and_test(&mddev->pending_writes))
746 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700747 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700748}
749
NeilBrownfd01b882011-10-11 16:47:53 +1100750void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700751 sector_t sector, int size, struct page *page)
752{
753 /* write first size bytes of page to sector of rdev
754 * Increment mddev->pending_writes before returning
755 * and decrement it on completion, waking up sb_wait
756 * if zero is reached.
757 * If an error occurred, call md_error
758 */
NeilBrowna167f662010-10-26 18:31:13 +1100759 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700760
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100761 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700762 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700763 bio_add_page(bio, page, size, 0);
764 bio->bi_private = rdev;
765 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800766
NeilBrown7bfa19f2005-06-21 17:17:28 -0700767 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000768 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800769}
770
NeilBrownfd01b882011-10-11 16:47:53 +1100771void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800772{
Tejun Heoe9c74692010-09-03 11:56:18 +0200773 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000774 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700775}
776
NeilBrown3cb03002011-10-11 16:45:26 +1100777int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100778 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
NeilBrowna167f662010-10-26 18:31:13 +1100780 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int ret;
782
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100783 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
784 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100785 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700786 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000787 else if (rdev->mddev->reshape_position != MaxSector &&
788 (rdev->mddev->reshape_backwards ==
789 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700790 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100791 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700792 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700794 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200796 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 bio_put(bio);
798 return ret;
799}
NeilBrowna8745db2006-01-06 00:20:34 -0800800EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
NeilBrownf72ffdd2014-09-30 14:23:59 +1000802static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (rdev->sb_loaded)
807 return 0;
808
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100809 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto fail;
811 rdev->sb_loaded = 1;
812 return 0;
813
814fail:
815 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
816 bdevname(rdev->bdev,b));
817 return -EINVAL;
818}
819
820static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
821{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000822 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000823 sb1->set_uuid1 == sb2->set_uuid1 &&
824 sb1->set_uuid2 == sb2->set_uuid2 &&
825 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
829{
830 int ret;
831 mdp_super_t *tmp1, *tmp2;
832
833 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
834 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
835
836 if (!tmp1 || !tmp2) {
837 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100838 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 goto abort;
840 }
841
842 *tmp1 = *sb1;
843 *tmp2 = *sb2;
844
845 /*
846 * nr_disks is not constant
847 */
848 tmp1->nr_disks = 0;
849 tmp2->nr_disks = 0;
850
Andre Nollce0c8e02008-07-11 22:02:20 +1000851 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700853 kfree(tmp1);
854 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return ret;
856}
857
NeilBrown4d167f02007-05-09 02:35:37 -0700858static u32 md_csum_fold(u32 csum)
859{
860 csum = (csum & 0xffff) + (csum >> 16);
861 return (csum & 0xffff) + (csum >> 16);
862}
863
NeilBrownf72ffdd2014-09-30 14:23:59 +1000864static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
NeilBrown4d167f02007-05-09 02:35:37 -0700866 u64 newcsum = 0;
867 u32 *sb32 = (u32*)sb;
868 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 unsigned int disk_csum, csum;
870
871 disk_csum = sb->sb_csum;
872 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700873
874 for (i = 0; i < MD_SB_BYTES/4 ; i++)
875 newcsum += sb32[i];
876 csum = (newcsum & 0xffffffff) + (newcsum>>32);
877
NeilBrown4d167f02007-05-09 02:35:37 -0700878#ifdef CONFIG_ALPHA
879 /* This used to use csum_partial, which was wrong for several
880 * reasons including that different results are returned on
881 * different architectures. It isn't critical that we get exactly
882 * the same return value as before (we always csum_fold before
883 * testing, and that removes any differences). However as we
884 * know that csum_partial always returned a 16bit value on
885 * alphas, do a fold to maximise conformity to previous behaviour.
886 */
887 sb->sb_csum = md_csum_fold(disk_csum);
888#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700890#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return csum;
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/*
895 * Handle superblock details.
896 * We want to be able to handle multiple superblock formats
897 * so we have a common interface to them all, and an array of
898 * different handlers.
899 * We rely on user-space to write the initial superblock, and support
900 * reading and updating of superblocks.
901 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100902 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 * loads and validates a superblock on dev.
904 * if refdev != NULL, compare superblocks on both devices
905 * Return:
906 * 0 - dev has a superblock that is compatible with refdev
907 * 1 - dev has a superblock that is compatible and newer than refdev
908 * so dev should be used as the refdev in future
909 * -EINVAL superblock incompatible or invalid
910 * -othererror e.g. -EIO
911 *
NeilBrownfd01b882011-10-11 16:47:53 +1100912 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 * Verify that dev is acceptable into mddev.
914 * The first time, mddev->raid_disks will be 0, and data from
915 * dev should be merged in. Subsequent calls check that dev
916 * is new enough. Return 0 or -EINVAL
917 *
NeilBrownfd01b882011-10-11 16:47:53 +1100918 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * Update the superblock for rdev with data in mddev
920 * This does not write to disc.
921 *
922 */
923
924struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000925 char *name;
926 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000927 int (*load_super)(struct md_rdev *rdev,
928 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000929 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000930 int (*validate_super)(struct mddev *mddev,
931 struct md_rdev *rdev);
932 void (*sync_super)(struct mddev *mddev,
933 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100934 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000935 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000936 int (*allow_new_offset)(struct md_rdev *rdev,
937 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938};
939
940/*
Andre Noll0894cc32009-06-18 08:49:23 +1000941 * Check that the given mddev has no bitmap.
942 *
943 * This function is called from the run method of all personalities that do not
944 * support bitmaps. It prints an error message and returns non-zero if mddev
945 * has a bitmap. Otherwise, it returns 0.
946 *
947 */
NeilBrownfd01b882011-10-11 16:47:53 +1100948int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000949{
NeilBrownc3d97142009-12-14 12:49:52 +1100950 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000951 return 0;
952 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
953 mdname(mddev), mddev->pers->name);
954 return 1;
955}
956EXPORT_SYMBOL(md_check_no_bitmap);
957
958/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000959 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 */
NeilBrown3cb03002011-10-11 16:45:26 +1100961static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
964 mdp_super_t *sb;
965 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 /*
Andre Noll0f420352008-07-11 22:02:23 +1000968 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * it's at the end of the disk.
970 *
971 * It also happens to be a multiple of 4Kb.
972 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100973 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
NeilBrown0002b272005-09-09 16:23:53 -0700975 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (ret) return ret;
977
978 ret = -EINVAL;
979
980 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000981 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 if (sb->md_magic != MD_SB_MAGIC) {
984 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
985 b);
986 goto abort;
987 }
988
989 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800990 sb->minor_version < 90 ||
991 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
993 sb->major_version, sb->minor_version,
994 b);
995 goto abort;
996 }
997
998 if (sb->raid_disks <= 0)
999 goto abort;
1000
NeilBrown4d167f02007-05-09 02:35:37 -07001001 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1003 b);
1004 goto abort;
1005 }
1006
1007 rdev->preferred_minor = sb->md_minor;
1008 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001009 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001010 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001011 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (sb->level == LEVEL_MULTIPATH)
1014 rdev->desc_nr = -1;
1015 else
1016 rdev->desc_nr = sb->this_disk.number;
1017
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001018 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001020 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001022 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (!uuid_equal(refsb, sb)) {
1024 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1025 b, bdevname(refdev->bdev,b2));
1026 goto abort;
1027 }
1028 if (!sb_equal(refsb, sb)) {
1029 printk(KERN_WARNING "md: %s has same UUID"
1030 " but different superblock to %s\n",
1031 b, bdevname(refdev->bdev, b2));
1032 goto abort;
1033 }
1034 ev1 = md_event(sb);
1035 ev2 = md_event(refsb);
1036 if (ev1 > ev2)
1037 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001038 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 ret = 0;
1040 }
NeilBrown8190e752009-06-18 08:48:58 +10001041 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001042 /* Limit to 4TB as metadata cannot record more than that.
1043 * (not needed for Linear and RAID0 as metadata doesn't
1044 * record this size)
1045 */
1046 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001047 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
NeilBrown27a7b262011-09-10 17:21:28 +10001049 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001050 /* "this cannot possibly happen" ... */
1051 ret = -EINVAL;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 abort:
1054 return ret;
1055}
1056
1057/*
1058 * validate_super for 0.90.0
1059 */
NeilBrownfd01b882011-10-11 16:47:53 +11001060static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001063 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001064 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
NeilBrown41158c72005-06-21 17:17:25 -07001066 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001067 clear_bit(Faulty, &rdev->flags);
1068 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001069 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001070 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (mddev->raid_disks == 0) {
1073 mddev->major_version = 0;
1074 mddev->minor_version = sb->minor_version;
1075 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001076 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001077 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 mddev->ctime = sb->ctime;
1079 mddev->utime = sb->utime;
1080 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001081 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 mddev->layout = sb->layout;
1083 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001084 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001085 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001086 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001087 mddev->bitmap_info.space = 0;
1088 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001089 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001090 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001091 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
NeilBrownf6705572006-03-27 01:18:11 -08001093 if (mddev->minor_version >= 91) {
1094 mddev->reshape_position = sb->reshape_position;
1095 mddev->delta_disks = sb->delta_disks;
1096 mddev->new_level = sb->new_level;
1097 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001098 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001099 if (mddev->delta_disks < 0)
1100 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001101 } else {
1102 mddev->reshape_position = MaxSector;
1103 mddev->delta_disks = 0;
1104 mddev->new_level = mddev->level;
1105 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001106 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001107 }
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (sb->state & (1<<MD_SB_CLEAN))
1110 mddev->recovery_cp = MaxSector;
1111 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001112 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 sb->events_lo == sb->cp_events_lo) {
1114 mddev->recovery_cp = sb->recovery_cp;
1115 } else
1116 mddev->recovery_cp = 0;
1117 }
1118
1119 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1120 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1121 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1122 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1123
1124 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001125
1126 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001127 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001128 mddev->bitmap_info.offset =
1129 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001130 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001131 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001132 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001133
NeilBrown41158c72005-06-21 17:17:25 -07001134 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001135 /* Insist on good event counter while assembling, except
1136 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001138 if (sb->disks[rdev->desc_nr].state & (
1139 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001140 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001141 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001142 } else if (mddev->bitmap) {
1143 /* if adding to array with a bitmap, then we can accept an
1144 * older device ... but not too old.
1145 */
NeilBrown41158c72005-06-21 17:17:25 -07001146 if (ev1 < mddev->bitmap->events_cleared)
1147 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001148 if (ev1 < mddev->events)
1149 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001150 } else {
1151 if (ev1 < mddev->events)
1152 /* just a hot-add of a new device, leave raid_disk at -1 */
1153 return 0;
1154 }
NeilBrown41158c72005-06-21 17:17:25 -07001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 desc = sb->disks + rdev->desc_nr;
1158
1159 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001160 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001161 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1162 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001163 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001165 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001166 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1167 /* active but not in sync implies recovery up to
1168 * reshape position. We don't know exactly where
1169 * that is, so set to zero for now */
1170 if (mddev->minor_version >= 91) {
1171 rdev->recovery_offset = 0;
1172 rdev->raid_disk = desc->raid_disk;
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001175 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1176 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001177 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001178 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return 0;
1180}
1181
1182/*
1183 * sync_super for 0.90.0
1184 */
NeilBrownfd01b882011-10-11 16:47:53 +11001185static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001188 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /* make rdev->sb match mddev data..
1192 *
1193 * 1/ zero out disks
1194 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1195 * 3/ any empty disks < next_spare become removed
1196 *
1197 * disks[0] gets initialised to REMOVED because
1198 * we cannot be sure from other fields if it has
1199 * been initialised or not.
1200 */
1201 int i;
1202 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1203
NeilBrown61181562005-09-09 16:24:02 -07001204 rdev->sb_size = MD_SB_BYTES;
1205
Namhyung Kim65a06f062011-07-27 11:00:36 +10001206 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 memset(sb, 0, sizeof(*sb));
1209
1210 sb->md_magic = MD_SB_MAGIC;
1211 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 sb->patch_version = mddev->patch_version;
1213 sb->gvalid_words = 0; /* ignored */
1214 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1215 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1216 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1217 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1218
1219 sb->ctime = mddev->ctime;
1220 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001221 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 sb->raid_disks = mddev->raid_disks;
1223 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001224 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 sb->utime = mddev->utime;
1226 sb->state = 0;
1227 sb->events_hi = (mddev->events>>32);
1228 sb->events_lo = (u32)mddev->events;
1229
NeilBrownf6705572006-03-27 01:18:11 -08001230 if (mddev->reshape_position == MaxSector)
1231 sb->minor_version = 90;
1232 else {
1233 sb->minor_version = 91;
1234 sb->reshape_position = mddev->reshape_position;
1235 sb->new_level = mddev->new_level;
1236 sb->delta_disks = mddev->delta_disks;
1237 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001238 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001239 }
1240 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (mddev->in_sync)
1242 {
1243 sb->recovery_cp = mddev->recovery_cp;
1244 sb->cp_events_hi = (mddev->events>>32);
1245 sb->cp_events_lo = (u32)mddev->events;
1246 if (mddev->recovery_cp == MaxSector)
1247 sb->state = (1<< MD_SB_CLEAN);
1248 } else
1249 sb->recovery_cp = 0;
1250
1251 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001252 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
NeilBrownc3d97142009-12-14 12:49:52 +11001254 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001255 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001258 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001260 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001261 int is_active = test_bit(In_sync, &rdev2->flags);
1262
1263 if (rdev2->raid_disk >= 0 &&
1264 sb->minor_version >= 91)
1265 /* we have nowhere to store the recovery_offset,
1266 * but if it is not below the reshape_position,
1267 * we can piggy-back on that.
1268 */
1269 is_active = 1;
1270 if (rdev2->raid_disk < 0 ||
1271 test_bit(Faulty, &rdev2->flags))
1272 is_active = 0;
1273 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001274 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001276 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001277 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 d = &sb->disks[rdev2->desc_nr];
1279 nr_disks++;
1280 d->number = rdev2->desc_nr;
1281 d->major = MAJOR(rdev2->bdev->bd_dev);
1282 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001283 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 d->raid_disk = rdev2->raid_disk;
1285 else
1286 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001287 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001289 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001291 if (test_bit(In_sync, &rdev2->flags))
1292 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 active++;
1294 working++;
1295 } else {
1296 d->state = 0;
1297 spare++;
1298 working++;
1299 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001300 if (test_bit(WriteMostly, &rdev2->flags))
1301 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /* now set the "removed" and "faulty" bits on any missing devices */
1304 for (i=0 ; i < mddev->raid_disks ; i++) {
1305 mdp_disk_t *d = &sb->disks[i];
1306 if (d->state == 0 && d->number == 0) {
1307 d->number = i;
1308 d->raid_disk = i;
1309 d->state = (1<<MD_DISK_REMOVED);
1310 d->state |= (1<<MD_DISK_FAULTY);
1311 failed++;
1312 }
1313 }
1314 sb->nr_disks = nr_disks;
1315 sb->active_disks = active;
1316 sb->working_disks = working;
1317 sb->failed_disks = failed;
1318 sb->spare_disks = spare;
1319
1320 sb->this_disk = sb->disks[rdev->desc_nr];
1321 sb->sb_csum = calc_sb_csum(sb);
1322}
1323
1324/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325 * rdev_size_change for 0.90.0
1326 */
1327static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001328super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001329{
Andre Noll58c0fed2009-03-31 14:33:13 +11001330 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001331 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001332 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001333 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001334 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001335 if (!num_sectors || num_sectors > rdev->sb_start)
1336 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001337 /* Limit to 4TB as metadata cannot record more than that.
1338 * 4TB == 2^32 KB, or 2*2^32 sectors.
1339 */
NeilBrown667a5312012-08-16 16:46:12 +10001340 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001341 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001342 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001343 rdev->sb_page);
1344 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001345 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001346}
1347
NeilBrownc6563a82012-05-21 09:27:00 +10001348static int
1349super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1350{
1351 /* non-zero offset changes not possible with v0.90 */
1352 return new_offset == 0;
1353}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001354
1355/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 * version 1 superblock
1357 */
1358
NeilBrownf72ffdd2014-09-30 14:23:59 +10001359static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001361 __le32 disk_csum;
1362 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 unsigned long long newcsum;
1364 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001365 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 disk_csum = sb->sb_csum;
1368 sb->sb_csum = 0;
1369 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001370 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 newcsum += le32_to_cpu(*isuper++);
1372
1373 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001374 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1377 sb->sb_csum = disk_csum;
1378 return cpu_to_le32(csum);
1379}
1380
NeilBrown2699b672011-07-28 11:31:47 +10001381static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1382 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001383static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 struct mdp_superblock_1 *sb;
1386 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001387 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001388 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001390 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /*
Andre Noll0f420352008-07-11 22:02:23 +10001393 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * It is always aligned to a 4K boundary and
1395 * depeding on minor_version, it can be:
1396 * 0: At least 8K, but less than 12K, from end of device
1397 * 1: At start of device
1398 * 2: 4K from start of device.
1399 */
1400 switch(minor_version) {
1401 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001402 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001403 sb_start -= 8*2;
1404 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 break;
1406 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001407 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 break;
1409 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001410 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 break;
1412 default:
1413 return -EINVAL;
1414 }
Andre Noll0f420352008-07-11 22:02:23 +10001415 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
NeilBrown0002b272005-09-09 16:23:53 -07001417 /* superblock is rarely larger than 1K, but it can be larger,
1418 * and it is safe to read 4k, so we do that
1419 */
1420 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (ret) return ret;
1422
Namhyung Kim65a06f062011-07-27 11:00:36 +10001423 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1426 sb->major_version != cpu_to_le32(1) ||
1427 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001428 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001429 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return -EINVAL;
1431
1432 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1433 printk("md: invalid superblock checksum on %s\n",
1434 bdevname(rdev->bdev,b));
1435 return -EINVAL;
1436 }
1437 if (le64_to_cpu(sb->data_size) < 10) {
1438 printk("md: data_size too small on %s\n",
1439 bdevname(rdev->bdev,b));
1440 return -EINVAL;
1441 }
NeilBrownc6563a82012-05-21 09:27:00 +10001442 if (sb->pad0 ||
1443 sb->pad3[0] ||
1444 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1445 /* Some padding is non-zero, might be a new feature */
1446 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 rdev->preferred_minor = 0xffff;
1449 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001450 rdev->new_data_offset = rdev->data_offset;
1451 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1452 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1453 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001454 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
NeilBrown0002b272005-09-09 16:23:53 -07001456 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001457 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001458 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001459 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1460
1461 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001462 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001463 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001464 if (minor_version
1465 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1466 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001467
NeilBrown31b65a02006-07-10 04:44:14 -07001468 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1469 rdev->desc_nr = -1;
1470 else
1471 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1472
NeilBrown2699b672011-07-28 11:31:47 +10001473 if (!rdev->bb_page) {
1474 rdev->bb_page = alloc_page(GFP_KERNEL);
1475 if (!rdev->bb_page)
1476 return -ENOMEM;
1477 }
1478 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1479 rdev->badblocks.count == 0) {
1480 /* need to load the bad block list.
1481 * Currently we limit it to one page.
1482 */
1483 s32 offset;
1484 sector_t bb_sector;
1485 u64 *bbp;
1486 int i;
1487 int sectors = le16_to_cpu(sb->bblog_size);
1488 if (sectors > (PAGE_SIZE / 512))
1489 return -EINVAL;
1490 offset = le32_to_cpu(sb->bblog_offset);
1491 if (offset == 0)
1492 return -EINVAL;
1493 bb_sector = (long long)offset;
1494 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1495 rdev->bb_page, READ, true))
1496 return -EIO;
1497 bbp = (u64 *)page_address(rdev->bb_page);
1498 rdev->badblocks.shift = sb->bblog_shift;
1499 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1500 u64 bb = le64_to_cpu(*bbp);
1501 int count = bb & (0x3ff);
1502 u64 sector = bb >> 10;
1503 sector <<= sb->bblog_shift;
1504 count <<= sb->bblog_shift;
1505 if (bb + 1 == 0)
1506 break;
1507 if (md_set_badblocks(&rdev->badblocks,
1508 sector, count, 1) == 0)
1509 return -EINVAL;
1510 }
NeilBrown486adf72013-04-24 11:42:44 +10001511 } else if (sb->bblog_offset != 0)
1512 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001513
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001514 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001515 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001516 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001518 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1521 sb->level != refsb->level ||
1522 sb->layout != refsb->layout ||
1523 sb->chunksize != refsb->chunksize) {
1524 printk(KERN_WARNING "md: %s has strangely different"
1525 " superblock to %s\n",
1526 bdevname(rdev->bdev,b),
1527 bdevname(refdev->bdev,b2));
1528 return -EINVAL;
1529 }
1530 ev1 = le64_to_cpu(sb->events);
1531 ev2 = le64_to_cpu(refsb->events);
1532
1533 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001534 ret = 1;
1535 else
1536 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
NeilBrownc6563a82012-05-21 09:27:00 +10001538 if (minor_version) {
1539 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1540 sectors -= rdev->data_offset;
1541 } else
1542 sectors = rdev->sb_start;
1543 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001545 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001546 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
NeilBrownfd01b882011-10-11 16:47:53 +11001549static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001551 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001552 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
NeilBrown41158c72005-06-21 17:17:25 -07001554 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001555 clear_bit(Faulty, &rdev->flags);
1556 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001557 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001558 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (mddev->raid_disks == 0) {
1561 mddev->major_version = 1;
1562 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001563 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001564 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1566 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1567 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001568 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 mddev->layout = le32_to_cpu(sb->layout);
1570 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001571 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001572 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001573 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001574 mddev->bitmap_info.space = 0;
1575 /* Default location for bitmap is 1K after superblock
1576 * using 3K - total of 4K
1577 */
NeilBrownc3d97142009-12-14 12:49:52 +11001578 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001579 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001580 mddev->reshape_backwards = 0;
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1583 memcpy(mddev->uuid, sb->set_uuid, 16);
1584
1585 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001586
NeilBrown71c08052005-09-09 16:23:51 -07001587 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001588 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001589 mddev->bitmap_info.offset =
1590 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001591 /* Metadata doesn't record how much space is available.
1592 * For 1.0, we assume we can use up to the superblock
1593 * if before, else to 4K beyond superblock.
1594 * For others, assume no change is possible.
1595 */
1596 if (mddev->minor_version > 0)
1597 mddev->bitmap_info.space = 0;
1598 else if (mddev->bitmap_info.offset > 0)
1599 mddev->bitmap_info.space =
1600 8 - mddev->bitmap_info.offset;
1601 else
1602 mddev->bitmap_info.space =
1603 -mddev->bitmap_info.offset;
1604 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001605
NeilBrownf6705572006-03-27 01:18:11 -08001606 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1607 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1608 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1609 mddev->new_level = le32_to_cpu(sb->new_level);
1610 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001611 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001612 if (mddev->delta_disks < 0 ||
1613 (mddev->delta_disks == 0 &&
1614 (le32_to_cpu(sb->feature_map)
1615 & MD_FEATURE_RESHAPE_BACKWARDS)))
1616 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001617 } else {
1618 mddev->reshape_position = MaxSector;
1619 mddev->delta_disks = 0;
1620 mddev->new_level = mddev->level;
1621 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001622 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001623 }
1624
NeilBrown41158c72005-06-21 17:17:25 -07001625 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001626 /* Insist of good event counter while assembling, except for
1627 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001629 if (rdev->desc_nr >= 0 &&
1630 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1631 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1632 if (ev1 < mddev->events)
1633 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001634 } else if (mddev->bitmap) {
1635 /* If adding to array with a bitmap, then we can accept an
1636 * older device, but not too old.
1637 */
NeilBrown41158c72005-06-21 17:17:25 -07001638 if (ev1 < mddev->bitmap->events_cleared)
1639 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001640 if (ev1 < mddev->events)
1641 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001642 } else {
1643 if (ev1 < mddev->events)
1644 /* just a hot-add of a new device, leave raid_disk at -1 */
1645 return 0;
1646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (mddev->level != LEVEL_MULTIPATH) {
1648 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001649 if (rdev->desc_nr < 0 ||
1650 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1651 role = 0xffff;
1652 rdev->desc_nr = -1;
1653 } else
1654 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 switch(role) {
1656 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 break;
1658 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001659 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661 default:
NeilBrownf4667222013-12-09 12:04:56 +11001662 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001663 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001664 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001665 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001666 if (!(le32_to_cpu(sb->feature_map) &
1667 MD_FEATURE_RECOVERY_BITMAP))
1668 rdev->saved_raid_disk = -1;
1669 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001670 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 rdev->raid_disk = role;
1672 break;
1673 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001674 if (sb->devflags & WriteMostly1)
1675 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001676 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1677 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001678 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001679 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return 0;
1682}
1683
NeilBrownfd01b882011-10-11 16:47:53 +11001684static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001687 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 int max_dev, i;
1689 /* make rdev->sb match mddev and rdev data. */
1690
Namhyung Kim65a06f062011-07-27 11:00:36 +10001691 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 sb->feature_map = 0;
1694 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001695 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 memset(sb->pad3, 0, sizeof(sb->pad3));
1697
1698 sb->utime = cpu_to_le64((__u64)mddev->utime);
1699 sb->events = cpu_to_le64(mddev->events);
1700 if (mddev->in_sync)
1701 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1702 else
1703 sb->resync_offset = cpu_to_le64(0);
1704
NeilBrown1c05b4b2006-10-21 10:24:08 -07001705 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001706
NeilBrownf0ca3402006-02-02 14:28:04 -08001707 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001708 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001709 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001710 sb->level = cpu_to_le32(mddev->level);
1711 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001712
NeilBrownaeb9b212011-08-25 14:43:08 +10001713 if (test_bit(WriteMostly, &rdev->flags))
1714 sb->devflags |= WriteMostly1;
1715 else
1716 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001717 sb->data_offset = cpu_to_le64(rdev->data_offset);
1718 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001719
NeilBrownc3d97142009-12-14 12:49:52 +11001720 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1721 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001722 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001723 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001724
1725 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001726 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001727 sb->feature_map |=
1728 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1729 sb->recovery_offset =
1730 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001731 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1732 sb->feature_map |=
1733 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001734 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001735 if (test_bit(Replacement, &rdev->flags))
1736 sb->feature_map |=
1737 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001738
NeilBrownf6705572006-03-27 01:18:11 -08001739 if (mddev->reshape_position != MaxSector) {
1740 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1741 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1742 sb->new_layout = cpu_to_le32(mddev->new_layout);
1743 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1744 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001745 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001746 if (mddev->delta_disks == 0 &&
1747 mddev->reshape_backwards)
1748 sb->feature_map
1749 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001750 if (rdev->new_data_offset != rdev->data_offset) {
1751 sb->feature_map
1752 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1753 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1754 - rdev->data_offset));
1755 }
NeilBrownf6705572006-03-27 01:18:11 -08001756 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001757
NeilBrown2699b672011-07-28 11:31:47 +10001758 if (rdev->badblocks.count == 0)
1759 /* Nothing to do for bad blocks*/ ;
1760 else if (sb->bblog_offset == 0)
1761 /* Cannot record bad blocks on this device */
1762 md_error(mddev, rdev);
1763 else {
1764 struct badblocks *bb = &rdev->badblocks;
1765 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1766 u64 *p = bb->page;
1767 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1768 if (bb->changed) {
1769 unsigned seq;
1770
1771retry:
1772 seq = read_seqbegin(&bb->lock);
1773
1774 memset(bbp, 0xff, PAGE_SIZE);
1775
1776 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001777 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001778 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1779 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001780 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001781 }
NeilBrownd0962932012-03-19 12:46:41 +11001782 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001783 if (read_seqretry(&bb->lock, seq))
1784 goto retry;
1785
1786 bb->sector = (rdev->sb_start +
1787 (int)le32_to_cpu(sb->bblog_offset));
1788 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001789 }
1790 }
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001793 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (rdev2->desc_nr+1 > max_dev)
1795 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001796
NeilBrown70471da2009-08-03 10:59:57 +10001797 if (max_dev > le32_to_cpu(sb->max_dev)) {
1798 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001799 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001800 rdev->sb_size = max_dev * 2 + 256;
1801 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1802 if (rdev->sb_size & bmask)
1803 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001804 } else
1805 max_dev = le32_to_cpu(sb->max_dev);
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 for (i=0; i<max_dev;i++)
1808 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001809
NeilBrowndafb20f2012-03-19 12:46:39 +11001810 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001812 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001814 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001816 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001817 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 else
1819 sb->dev_roles[i] = cpu_to_le16(0xffff);
1820 }
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 sb->sb_csum = calc_sb_1_csum(sb);
1823}
1824
Chris Webb0cd17fe2008-06-28 08:31:46 +10001825static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001826super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001827{
1828 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001829 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001830 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001831 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001832 if (rdev->data_offset != rdev->new_data_offset)
1833 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001834 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001835 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001836 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001837 max_sectors -= rdev->data_offset;
1838 if (!num_sectors || num_sectors > max_sectors)
1839 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001840 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001841 /* minor version 0 with bitmap we can't move */
1842 return 0;
1843 } else {
1844 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001845 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001846 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001847 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001848 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001849 if (!num_sectors || num_sectors > max_sectors)
1850 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001851 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001852 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001853 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001854 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001855 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001856 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001857 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001858 rdev->sb_page);
1859 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001860 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001861
1862}
1863
1864static int
1865super_1_allow_new_offset(struct md_rdev *rdev,
1866 unsigned long long new_offset)
1867{
1868 /* All necessary checks on new >= old have been done */
1869 struct bitmap *bitmap;
1870 if (new_offset >= rdev->data_offset)
1871 return 1;
1872
1873 /* with 1.0 metadata, there is no metadata to tread on
1874 * so we can always move back */
1875 if (rdev->mddev->minor_version == 0)
1876 return 1;
1877
1878 /* otherwise we must be sure not to step on
1879 * any metadata, so stay:
1880 * 36K beyond start of superblock
1881 * beyond end of badblocks
1882 * beyond write-intent bitmap
1883 */
1884 if (rdev->sb_start + (32+4)*2 > new_offset)
1885 return 0;
1886 bitmap = rdev->mddev->bitmap;
1887 if (bitmap && !rdev->mddev->bitmap_info.file &&
1888 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001889 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001890 return 0;
1891 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1892 return 0;
1893
1894 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001895}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Adrian Bunk75c96f82005-05-05 16:16:09 -07001897static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 [0] = {
1899 .name = "0.90.0",
1900 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001901 .load_super = super_90_load,
1902 .validate_super = super_90_validate,
1903 .sync_super = super_90_sync,
1904 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001905 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 },
1907 [1] = {
1908 .name = "md-1",
1909 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001910 .load_super = super_1_load,
1911 .validate_super = super_1_validate,
1912 .sync_super = super_1_sync,
1913 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001914 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 },
1916};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
NeilBrownfd01b882011-10-11 16:47:53 +11001918static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001919{
1920 if (mddev->sync_super) {
1921 mddev->sync_super(mddev, rdev);
1922 return;
1923 }
1924
1925 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1926
1927 super_types[mddev->major_version].sync_super(mddev, rdev);
1928}
1929
NeilBrownfd01b882011-10-11 16:47:53 +11001930static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
NeilBrown3cb03002011-10-11 16:45:26 +11001932 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
NeilBrown4b809912008-07-21 17:05:25 +10001934 rcu_read_lock();
1935 rdev_for_each_rcu(rdev, mddev1)
1936 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001937 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001938 rdev2->bdev->bd_contains) {
1939 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001940 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001941 }
1942 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return 0;
1944}
1945
1946static LIST_HEAD(pending_raid_disks);
1947
Andre Nollac5e7112009-08-03 10:59:47 +10001948/*
1949 * Try to register data integrity profile for an mddev
1950 *
1951 * This is called when an array is started and after a disk has been kicked
1952 * from the array. It only succeeds if all working and active component devices
1953 * are integrity capable with matching profiles.
1954 */
NeilBrownfd01b882011-10-11 16:47:53 +11001955int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001956{
NeilBrown3cb03002011-10-11 16:45:26 +11001957 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001958
Andre Nollac5e7112009-08-03 10:59:47 +10001959 if (list_empty(&mddev->disks))
1960 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001961 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1962 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001963 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001964 /* skip spares and non-functional disks */
1965 if (test_bit(Faulty, &rdev->flags))
1966 continue;
1967 if (rdev->raid_disk < 0)
1968 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001969 if (!reference) {
1970 /* Use the first rdev as the reference */
1971 reference = rdev;
1972 continue;
1973 }
1974 /* does this rdev's profile match the reference profile? */
1975 if (blk_integrity_compare(reference->bdev->bd_disk,
1976 rdev->bdev->bd_disk) < 0)
1977 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001978 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001979 if (!reference || !bdev_get_integrity(reference->bdev))
1980 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001981 /*
1982 * All component devices are integrity capable and have matching
1983 * profiles, register the common profile for the md device.
1984 */
1985 if (blk_integrity_register(mddev->gendisk,
1986 bdev_get_integrity(reference->bdev)) != 0) {
1987 printk(KERN_ERR "md: failed to register integrity for %s\n",
1988 mdname(mddev));
1989 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001990 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001991 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1992 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1993 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1994 mdname(mddev));
1995 return -EINVAL;
1996 }
Andre Nollac5e7112009-08-03 10:59:47 +10001997 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001998}
Andre Nollac5e7112009-08-03 10:59:47 +10001999EXPORT_SYMBOL(md_integrity_register);
2000
2001/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002002void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002003{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002004 struct blk_integrity *bi_rdev;
2005 struct blk_integrity *bi_mddev;
2006
2007 if (!mddev->gendisk)
2008 return;
2009
2010 bi_rdev = bdev_get_integrity(rdev->bdev);
2011 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002012
2013 if (!bi_mddev) /* nothing to do */
2014 return;
2015 if (rdev->raid_disk < 0) /* skip spares */
2016 return;
2017 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2018 rdev->bdev->bd_disk) >= 0)
2019 return;
2020 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2021 blk_integrity_unregister(mddev->gendisk);
2022}
2023EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002024
NeilBrownf72ffdd2014-09-30 14:23:59 +10002025static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002027 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002028 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002029 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Dan Williams11e2ede2008-04-30 00:52:32 -07002031 /* prevent duplicates */
2032 if (find_rdev(mddev, rdev->bdev->bd_dev))
2033 return -EEXIST;
2034
Andre Nolldd8ac332009-03-31 14:33:13 +11002035 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2036 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2037 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002038 if (mddev->pers) {
2039 /* Cannot change size, so fail
2040 * If mddev->level <= 0, then we don't care
2041 * about aligning sizes (e.g. linear)
2042 */
2043 if (mddev->level > 0)
2044 return -ENOSPC;
2045 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002046 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 /* Verify rdev->desc_nr is unique.
2050 * If it is -1, assign a free number, else
2051 * check number is not in use
2052 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002053 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (rdev->desc_nr < 0) {
2055 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002056 if (mddev->pers)
2057 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002058 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 choice++;
2060 rdev->desc_nr = choice;
2061 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002062 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002063 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002067 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002068 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2069 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2070 mdname(mddev), mddev->max_disks);
2071 return -EBUSY;
2072 }
NeilBrown19133a42005-11-08 21:39:35 -08002073 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002074 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002077 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002078
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002079 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002080 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002081
Tejun Heo0762b8b2008-08-25 19:56:12 +09002082 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002083 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2084 /* failure here is OK */;
2085 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002086
NeilBrown4b809912008-07-21 17:05:25 +10002087 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002088 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002089
2090 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002091 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002094
2095 fail:
2096 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2097 b, mdname(mddev));
2098 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
NeilBrown177a99b2008-02-06 01:39:56 -08002101static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002102{
NeilBrown3cb03002011-10-11 16:45:26 +11002103 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002104 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002105 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002106}
2107
NeilBrownf72ffdd2014-09-30 14:23:59 +10002108static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002111
Tejun Heo49731ba2011-01-14 18:43:57 +01002112 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002113 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2115 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002116 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002117 sysfs_put(rdev->sysfs_state);
2118 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002119 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002120 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002121 * writing to 'remove' to "dev/state". We also need
2122 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002123 */
NeilBrown4b809912008-07-21 17:05:25 +10002124 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002125 INIT_WORK(&rdev->del_work, md_delayed_delete);
2126 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002127 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130/*
2131 * prevent the device from being mounted, repartitioned or
2132 * otherwise reused by a RAID array (or any other kernel
2133 * subsystem), by bd_claiming the device.
2134 */
NeilBrown3cb03002011-10-11 16:45:26 +11002135static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 int err = 0;
2138 struct block_device *bdev;
2139 char b[BDEVNAME_SIZE];
2140
Tejun Heod4d77622010-11-13 11:55:18 +01002141 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002142 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (IS_ERR(bdev)) {
2144 printk(KERN_ERR "md: could not open %s.\n",
2145 __bdevname(dev, b));
2146 return PTR_ERR(bdev);
2147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 rdev->bdev = bdev;
2149 return err;
2150}
2151
NeilBrown3cb03002011-10-11 16:45:26 +11002152static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 struct block_device *bdev = rdev->bdev;
2155 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002156 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
2159void md_autodetect_dev(dev_t dev);
2160
NeilBrownf72ffdd2014-09-30 14:23:59 +10002161static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 printk(KERN_INFO "md: export_rdev(%s)\n",
2166 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002167 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002169 if (test_bit(AutoDetected, &rdev->flags))
2170 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171#endif
2172 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002173 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002176void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 unbind_rdev_from_array(rdev);
2179 export_rdev(rdev);
2180}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002181EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
NeilBrownfd01b882011-10-11 16:47:53 +11002183static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
NeilBrown0638bb02014-09-25 17:43:47 +10002185 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
NeilBrown0638bb02014-09-25 17:43:47 +10002187 while (!list_empty(&mddev->disks)) {
2188 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2189 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002190 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 mddev->raid_disks = 0;
2193 mddev->major_version = 0;
2194}
2195
NeilBrownf72ffdd2014-09-30 14:23:59 +10002196static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
NeilBrown42543762006-06-26 00:27:57 -07002198 /* Update each superblock (in-memory image), but
2199 * if we are allowed to, skip spares which already
2200 * have the right event counter, or have one earlier
2201 * (which would mean they aren't being marked as dirty
2202 * with the rest of the array)
2203 */
NeilBrown3cb03002011-10-11 16:45:26 +11002204 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002205 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002206 if (rdev->sb_events == mddev->events ||
2207 (nospares &&
2208 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002209 rdev->sb_events+1 == mddev->events)) {
2210 /* Don't update this superblock */
2211 rdev->sb_loaded = 2;
2212 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002213 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002214 rdev->sb_loaded = 1;
2215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217}
2218
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002219void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
NeilBrown3cb03002011-10-11 16:45:26 +11002221 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002222 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002223 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002224 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
NeilBrownd87f0642013-04-24 11:42:40 +10002226 if (mddev->ro) {
2227 if (force_change)
2228 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2229 return;
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002232 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002233 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002234 if (rdev->raid_disk >= 0 &&
2235 mddev->delta_disks >= 0 &&
2236 !test_bit(In_sync, &rdev->flags) &&
2237 mddev->curr_resync_completed > rdev->recovery_offset)
2238 rdev->recovery_offset = mddev->curr_resync_completed;
2239
NeilBrownf72ffdd2014-09-30 14:23:59 +10002240 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002241 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002242 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002243 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002244 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002245 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002246 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002247 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002248 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002249 md_ack_all_badblocks(&rdev->badblocks);
2250 md_error(mddev, rdev);
2251 }
2252 clear_bit(Blocked, &rdev->flags);
2253 clear_bit(BlockedBadBlocks, &rdev->flags);
2254 wake_up(&rdev->blocked_wait);
2255 }
2256 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002257 wake_up(&mddev->sb_wait);
2258 return;
2259 }
2260
NeilBrown85572d72014-12-15 12:56:56 +11002261 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002262
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002263 mddev->utime = get_seconds();
2264
NeilBrown850b2b42006-10-03 01:15:46 -07002265 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2266 force_change = 1;
2267 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2268 /* just a clean<-> dirty transition, possibly leave spares alone,
2269 * though if events isn't the right even/odd, we will have to do
2270 * spares after all
2271 */
2272 nospares = 1;
2273 if (force_change)
2274 nospares = 0;
2275 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002276 /* If the array is degraded, then skipping spares is both
2277 * dangerous and fairly pointless.
2278 * Dangerous because a device that was removed from the array
2279 * might have a event_count that still looks up-to-date,
2280 * so it can be re-added without a resync.
2281 * Pointless because if there are any spares to skip,
2282 * then a recovery will happen and soon that array won't
2283 * be degraded any more and the spare can go back to sleep then.
2284 */
NeilBrown850b2b42006-10-03 01:15:46 -07002285 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002286
NeilBrown06d91a52005-06-21 17:17:12 -07002287 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002288
2289 /* If this is just a dirty<->clean transition, and the array is clean
2290 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002291 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002292 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002293 && mddev->can_decrease_events
2294 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002295 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002296 mddev->can_decrease_events = 0;
2297 } else {
NeilBrown42543762006-06-26 00:27:57 -07002298 /* otherwise we have to go forward and ... */
2299 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002300 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
NeilBrown403df472014-09-30 15:52:29 +10002303 /*
2304 * This 64-bit counter should never wrap.
2305 * Either we are in around ~1 trillion A.C., assuming
2306 * 1 reboot per second, or we have a bug...
2307 */
2308 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002309
NeilBrowndafb20f2012-03-19 12:46:39 +11002310 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002311 if (rdev->badblocks.changed)
2312 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002313 if (test_bit(Faulty, &rdev->flags))
2314 set_bit(FaultRecorded, &rdev->flags);
2315 }
NeilBrown2699b672011-07-28 11:31:47 +10002316
NeilBrowne6910632008-02-06 01:39:51 -08002317 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002318 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
NeilBrown36a4e1f2011-10-07 14:23:17 +11002320 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2321 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
NeilBrown4ad13662007-07-17 04:06:13 -07002323 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002324 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002326
NeilBrown42543762006-06-26 00:27:57 -07002327 if (rdev->sb_loaded != 1)
2328 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
NeilBrownf4667222013-12-09 12:04:56 +11002330 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002331 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002332 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002333 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002334 pr_debug("md: (write) %s's sb offset: %llu\n",
2335 bdevname(rdev->bdev, b),
2336 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002337 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002338 if (rdev->badblocks.size) {
2339 md_super_write(mddev, rdev,
2340 rdev->badblocks.sector,
2341 rdev->badblocks.size << 9,
2342 rdev->bb_page);
2343 rdev->badblocks.size = 0;
2344 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002345
NeilBrownf4667222013-12-09 12:04:56 +11002346 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002347 pr_debug("md: %s (skipping faulty)\n",
2348 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002349
NeilBrown7bfa19f2005-06-21 17:17:28 -07002350 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 /* only need to write one superblock... */
2352 break;
2353 }
NeilBrowna9701a32005-11-08 21:39:34 -08002354 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002355 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002356
NeilBrown85572d72014-12-15 12:56:56 +11002357 spin_lock(&mddev->lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002358 if (mddev->in_sync != sync_req ||
2359 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002360 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002361 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002362 goto repeat;
2363 }
NeilBrown850b2b42006-10-03 01:15:46 -07002364 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002365 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002366 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002367 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2368 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002369
NeilBrowndafb20f2012-03-19 12:46:39 +11002370 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002371 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2372 clear_bit(Blocked, &rdev->flags);
2373
2374 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002375 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002376 clear_bit(BlockedBadBlocks, &rdev->flags);
2377 wake_up(&rdev->blocked_wait);
2378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002380EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002382static int add_bound_rdev(struct md_rdev *rdev)
2383{
2384 struct mddev *mddev = rdev->mddev;
2385 int err = 0;
2386
2387 if (!mddev->pers->hot_remove_disk) {
2388 /* If there is hot_add_disk but no hot_remove_disk
2389 * then added disks for geometry changes,
2390 * and should be added immediately.
2391 */
2392 super_types[mddev->major_version].
2393 validate_super(mddev, rdev);
2394 err = mddev->pers->hot_add_disk(mddev, rdev);
2395 if (err) {
2396 unbind_rdev_from_array(rdev);
2397 export_rdev(rdev);
2398 return err;
2399 }
2400 }
2401 sysfs_notify_dirent_safe(rdev->sysfs_state);
2402
2403 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2404 if (mddev->degraded)
2405 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2406 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2407 md_new_event(mddev);
2408 md_wakeup_thread(mddev->thread);
2409 return 0;
2410}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Andre Noll7f6ce762008-03-23 18:34:54 +01002412/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002413 * We want to accept with case. For this we use cmd_match.
2414 */
2415static int cmd_match(const char *cmd, const char *str)
2416{
2417 /* See if cmd, written into a sysfs file, matches
2418 * str. They must either be the same, or cmd can
2419 * have a trailing newline
2420 */
2421 while (*cmd && *str && *cmd == *str) {
2422 cmd++;
2423 str++;
2424 }
2425 if (*cmd == '\n')
2426 cmd++;
2427 if (*str || *cmd)
2428 return 0;
2429 return 1;
2430}
2431
NeilBrown86e6ffd2005-11-08 21:39:24 -08002432struct rdev_sysfs_entry {
2433 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002434 ssize_t (*show)(struct md_rdev *, char *);
2435 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002436};
2437
2438static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002439state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002440{
2441 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002442 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002443 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002444
NeilBrown758bfc82014-12-15 12:56:59 +11002445 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002446 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002447 len+= sprintf(page+len, "%sfaulty",sep);
2448 sep = ",";
2449 }
NeilBrown758bfc82014-12-15 12:56:59 +11002450 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002451 len += sprintf(page+len, "%sin_sync",sep);
2452 sep = ",";
2453 }
NeilBrown758bfc82014-12-15 12:56:59 +11002454 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002455 len += sprintf(page+len, "%swrite_mostly",sep);
2456 sep = ",";
2457 }
NeilBrown758bfc82014-12-15 12:56:59 +11002458 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002459 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002460 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002461 len += sprintf(page+len, "%sblocked", sep);
2462 sep = ",";
2463 }
NeilBrown758bfc82014-12-15 12:56:59 +11002464 if (!test_bit(Faulty, &flags) &&
2465 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002466 len += sprintf(page+len, "%sspare", sep);
2467 sep = ",";
2468 }
NeilBrown758bfc82014-12-15 12:56:59 +11002469 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002470 len += sprintf(page+len, "%swrite_error", sep);
2471 sep = ",";
2472 }
NeilBrown758bfc82014-12-15 12:56:59 +11002473 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002474 len += sprintf(page+len, "%swant_replacement", sep);
2475 sep = ",";
2476 }
NeilBrown758bfc82014-12-15 12:56:59 +11002477 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002478 len += sprintf(page+len, "%sreplacement", sep);
2479 sep = ",";
2480 }
2481
NeilBrown86e6ffd2005-11-08 21:39:24 -08002482 return len+sprintf(page+len, "\n");
2483}
2484
NeilBrown45dc2de2006-06-26 00:27:58 -07002485static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002486state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002487{
2488 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002489 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002490 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002491 * writemostly - sets write_mostly
2492 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002493 * blocked - sets the Blocked flags
2494 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002495 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002496 * -insync - clear Insync for a device with a slot assigned,
2497 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002498 * write_error - sets WriteErrorSeen
2499 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002500 */
2501 int err = -EINVAL;
2502 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2503 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002504 if (test_bit(Faulty, &rdev->flags))
2505 err = 0;
2506 else
2507 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002508 } else if (cmd_match(buf, "remove")) {
2509 if (rdev->raid_disk >= 0)
2510 err = -EBUSY;
2511 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002512 struct mddev *mddev = rdev->mddev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002513 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002514 md_cluster_ops->remove_disk(mddev, rdev);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002515 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002516 if (mddev_is_clustered(mddev))
2517 md_cluster_ops->metadata_update_start(mddev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002518 if (mddev->pers)
2519 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002520 md_new_event(mddev);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002521 if (mddev_is_clustered(mddev))
2522 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown45dc2de2006-06-26 00:27:58 -07002523 err = 0;
2524 }
NeilBrownf6556752006-06-26 00:28:01 -07002525 } else if (cmd_match(buf, "writemostly")) {
2526 set_bit(WriteMostly, &rdev->flags);
2527 err = 0;
2528 } else if (cmd_match(buf, "-writemostly")) {
2529 clear_bit(WriteMostly, &rdev->flags);
2530 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002531 } else if (cmd_match(buf, "blocked")) {
2532 set_bit(Blocked, &rdev->flags);
2533 err = 0;
2534 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002535 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002536 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002537 /* metadata handler doesn't understand badblocks,
2538 * so we need to fail the device
2539 */
2540 md_error(rdev->mddev, rdev);
2541 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002542 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002543 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002544 wake_up(&rdev->blocked_wait);
2545 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2546 md_wakeup_thread(rdev->mddev->thread);
2547
2548 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002549 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2550 set_bit(In_sync, &rdev->flags);
2551 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002552 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002553 if (rdev->mddev->pers == NULL) {
2554 clear_bit(In_sync, &rdev->flags);
2555 rdev->saved_raid_disk = rdev->raid_disk;
2556 rdev->raid_disk = -1;
2557 err = 0;
2558 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002559 } else if (cmd_match(buf, "write_error")) {
2560 set_bit(WriteErrorSeen, &rdev->flags);
2561 err = 0;
2562 } else if (cmd_match(buf, "-write_error")) {
2563 clear_bit(WriteErrorSeen, &rdev->flags);
2564 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002565 } else if (cmd_match(buf, "want_replacement")) {
2566 /* Any non-spare device that is not a replacement can
2567 * become want_replacement at any time, but we then need to
2568 * check if recovery is needed.
2569 */
2570 if (rdev->raid_disk >= 0 &&
2571 !test_bit(Replacement, &rdev->flags))
2572 set_bit(WantReplacement, &rdev->flags);
2573 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2574 md_wakeup_thread(rdev->mddev->thread);
2575 err = 0;
2576 } else if (cmd_match(buf, "-want_replacement")) {
2577 /* Clearing 'want_replacement' is always allowed.
2578 * Once replacements starts it is too late though.
2579 */
2580 err = 0;
2581 clear_bit(WantReplacement, &rdev->flags);
2582 } else if (cmd_match(buf, "replacement")) {
2583 /* Can only set a device as a replacement when array has not
2584 * yet been started. Once running, replacement is automatic
2585 * from spares, or by assigning 'slot'.
2586 */
2587 if (rdev->mddev->pers)
2588 err = -EBUSY;
2589 else {
2590 set_bit(Replacement, &rdev->flags);
2591 err = 0;
2592 }
2593 } else if (cmd_match(buf, "-replacement")) {
2594 /* Similarly, can only clear Replacement before start */
2595 if (rdev->mddev->pers)
2596 err = -EBUSY;
2597 else {
2598 clear_bit(Replacement, &rdev->flags);
2599 err = 0;
2600 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002601 } else if (cmd_match(buf, "re-add")) {
2602 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002603 /* clear_bit is performed _after_ all the devices
2604 * have their local Faulty bit cleared. If any writes
2605 * happen in the meantime in the local node, they
2606 * will land in the local bitmap, which will be synced
2607 * by this node eventually
2608 */
2609 if (!mddev_is_clustered(rdev->mddev) ||
2610 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2611 clear_bit(Faulty, &rdev->flags);
2612 err = add_bound_rdev(rdev);
2613 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002614 } else
2615 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002616 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002617 if (!err)
2618 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002619 return err ? err : len;
2620}
NeilBrown80ca3a42006-07-10 04:44:18 -07002621static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002622__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002623
2624static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002625errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002626{
2627 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2628}
2629
2630static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002631errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002632{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002633 unsigned int n;
2634 int rv;
2635
2636 rv = kstrtouint(buf, 10, &n);
2637 if (rv < 0)
2638 return rv;
2639 atomic_set(&rdev->corrected_errors, n);
2640 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002641}
2642static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002643__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002644
NeilBrown014236d2006-01-06 00:20:55 -08002645static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002646slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002647{
2648 if (rdev->raid_disk < 0)
2649 return sprintf(page, "none\n");
2650 else
2651 return sprintf(page, "%d\n", rdev->raid_disk);
2652}
2653
2654static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002655slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002656{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002657 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002658 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002659
NeilBrown014236d2006-01-06 00:20:55 -08002660 if (strncmp(buf, "none", 4)==0)
2661 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002662 else {
2663 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2664 if (err < 0)
2665 return err;
2666 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002667 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002668 /* Setting 'slot' on an active array requires also
2669 * updating the 'rd%d' link, and communicating
2670 * with the personality with ->hot_*_disk.
2671 * For now we only support removing
2672 * failed/spare devices. This normally happens automatically,
2673 * but not when the metadata is externally managed.
2674 */
NeilBrownc303da62008-02-06 01:39:51 -08002675 if (rdev->raid_disk == -1)
2676 return -EEXIST;
2677 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002678 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002679 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002680 clear_bit(Blocked, &rdev->flags);
2681 remove_and_add_spares(rdev->mddev, rdev);
2682 if (rdev->raid_disk >= 0)
2683 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002684 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2685 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002686 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002687 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002688 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002689 */
2690
2691 if (rdev->raid_disk != -1)
2692 return -EBUSY;
2693
NeilBrownc6751b22011-02-02 11:57:13 +11002694 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2695 return -EBUSY;
2696
Neil Brown6c2fce22008-06-28 08:31:31 +10002697 if (rdev->mddev->pers->hot_add_disk == NULL)
2698 return -EINVAL;
2699
NeilBrownba1b41b2011-01-14 09:14:34 +11002700 if (slot >= rdev->mddev->raid_disks &&
2701 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2702 return -ENOSPC;
2703
Neil Brown6c2fce22008-06-28 08:31:31 +10002704 rdev->raid_disk = slot;
2705 if (test_bit(In_sync, &rdev->flags))
2706 rdev->saved_raid_disk = slot;
2707 else
2708 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002709 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002710 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002711 err = rdev->mddev->pers->
2712 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002713 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002714 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002715 return err;
Neil Brown52664732008-06-28 08:31:44 +10002716 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002717 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002718 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002719 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002720 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002721 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002722 if (slot >= rdev->mddev->raid_disks &&
2723 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002724 return -ENOSPC;
2725 rdev->raid_disk = slot;
2726 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002727 clear_bit(Faulty, &rdev->flags);
2728 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002729 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002730 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002731 }
NeilBrown014236d2006-01-06 00:20:55 -08002732 return len;
2733}
2734
NeilBrown014236d2006-01-06 00:20:55 -08002735static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002736__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002737
NeilBrown93c8cad2006-01-06 00:20:56 -08002738static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002739offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002740{
Andrew Morton6961ece2006-01-06 00:20:59 -08002741 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002742}
2743
2744static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002745offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002746{
NeilBrownc6563a82012-05-21 09:27:00 +10002747 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002748 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002749 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002750 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002751 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002752 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002753 /* Must set offset before size, so overlap checks
2754 * can be sane */
2755 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002756 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002757 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002758 return len;
2759}
2760
2761static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002762__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002763
NeilBrownc6563a82012-05-21 09:27:00 +10002764static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2765{
2766 return sprintf(page, "%llu\n",
2767 (unsigned long long)rdev->new_data_offset);
2768}
2769
2770static ssize_t new_offset_store(struct md_rdev *rdev,
2771 const char *buf, size_t len)
2772{
2773 unsigned long long new_offset;
2774 struct mddev *mddev = rdev->mddev;
2775
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002776 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002777 return -EINVAL;
2778
NeilBrownf851b602014-12-11 10:02:10 +11002779 if (mddev->sync_thread ||
2780 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002781 return -EBUSY;
2782 if (new_offset == rdev->data_offset)
2783 /* reset is always permitted */
2784 ;
2785 else if (new_offset > rdev->data_offset) {
2786 /* must not push array size beyond rdev_sectors */
2787 if (new_offset - rdev->data_offset
2788 + mddev->dev_sectors > rdev->sectors)
2789 return -E2BIG;
2790 }
2791 /* Metadata worries about other space details. */
2792
2793 /* decreasing the offset is inconsistent with a backwards
2794 * reshape.
2795 */
2796 if (new_offset < rdev->data_offset &&
2797 mddev->reshape_backwards)
2798 return -EINVAL;
2799 /* Increasing offset is inconsistent with forwards
2800 * reshape. reshape_direction should be set to
2801 * 'backwards' first.
2802 */
2803 if (new_offset > rdev->data_offset &&
2804 !mddev->reshape_backwards)
2805 return -EINVAL;
2806
2807 if (mddev->pers && mddev->persistent &&
2808 !super_types[mddev->major_version]
2809 .allow_new_offset(rdev, new_offset))
2810 return -E2BIG;
2811 rdev->new_data_offset = new_offset;
2812 if (new_offset > rdev->data_offset)
2813 mddev->reshape_backwards = 1;
2814 else if (new_offset < rdev->data_offset)
2815 mddev->reshape_backwards = 0;
2816
2817 return len;
2818}
2819static struct rdev_sysfs_entry rdev_new_offset =
2820__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2821
NeilBrown83303b62006-01-06 00:21:06 -08002822static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002823rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002824{
Andre Nolldd8ac332009-03-31 14:33:13 +11002825 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002826}
2827
NeilBrownc5d79ad2008-02-06 01:39:54 -08002828static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2829{
2830 /* check if two start/length pairs overlap */
2831 if (s1+l1 <= s2)
2832 return 0;
2833 if (s2+l2 <= s1)
2834 return 0;
2835 return 1;
2836}
2837
Dan Williamsb522adc2009-03-31 15:00:31 +11002838static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2839{
2840 unsigned long long blocks;
2841 sector_t new;
2842
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002843 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002844 return -EINVAL;
2845
2846 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2847 return -EINVAL; /* sector conversion overflow */
2848
2849 new = blocks * 2;
2850 if (new != blocks * 2)
2851 return -EINVAL; /* unsigned long long to sector_t overflow */
2852
2853 *sectors = new;
2854 return 0;
2855}
2856
NeilBrown83303b62006-01-06 00:21:06 -08002857static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002858rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002859{
NeilBrownfd01b882011-10-11 16:47:53 +11002860 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002861 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002862 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002863
Dan Williamsb522adc2009-03-31 15:00:31 +11002864 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002865 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002866 if (rdev->data_offset != rdev->new_data_offset)
2867 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002868 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002869 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002870 sectors = super_types[my_mddev->major_version].
2871 rdev_size_change(rdev, sectors);
2872 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002873 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002874 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002875 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002876 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002877 if (!my_mddev->pers->resize)
2878 /* Cannot change size for RAID0 or Linear etc */
2879 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002880 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002881 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002882 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002883
Andre Nolldd8ac332009-03-31 14:33:13 +11002884 rdev->sectors = sectors;
2885 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002886 /* Need to check that all other rdevs with the same
2887 * ->bdev do not overlap. 'rcu' is sufficient to walk
2888 * the rdev lists safely.
2889 * This check does not provide a hard guarantee, it
2890 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002891 */
NeilBrownfd01b882011-10-11 16:47:53 +11002892 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002893 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002894 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002895
NeilBrown8b1afc32014-09-29 15:33:20 +10002896 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002897 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002898 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002899
NeilBrowndafb20f2012-03-19 12:46:39 +11002900 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002901 if (rdev->bdev == rdev2->bdev &&
2902 rdev != rdev2 &&
2903 overlaps(rdev->data_offset, rdev->sectors,
2904 rdev2->data_offset,
2905 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002906 overlap = 1;
2907 break;
2908 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002909 if (overlap) {
2910 mddev_put(mddev);
2911 break;
2912 }
2913 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002914 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002915 if (overlap) {
2916 /* Someone else could have slipped in a size
2917 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002918 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002919 * safe, and trust userspace not to race with
2920 * itself
2921 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002922 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002923 return -EBUSY;
2924 }
2925 }
NeilBrown83303b62006-01-06 00:21:06 -08002926 return len;
2927}
2928
2929static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002930__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002931
NeilBrown3cb03002011-10-11 16:45:26 +11002932static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002933{
2934 unsigned long long recovery_start = rdev->recovery_offset;
2935
2936 if (test_bit(In_sync, &rdev->flags) ||
2937 recovery_start == MaxSector)
2938 return sprintf(page, "none\n");
2939
2940 return sprintf(page, "%llu\n", recovery_start);
2941}
2942
NeilBrown3cb03002011-10-11 16:45:26 +11002943static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002944{
2945 unsigned long long recovery_start;
2946
2947 if (cmd_match(buf, "none"))
2948 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002949 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002950 return -EINVAL;
2951
2952 if (rdev->mddev->pers &&
2953 rdev->raid_disk >= 0)
2954 return -EBUSY;
2955
2956 rdev->recovery_offset = recovery_start;
2957 if (recovery_start == MaxSector)
2958 set_bit(In_sync, &rdev->flags);
2959 else
2960 clear_bit(In_sync, &rdev->flags);
2961 return len;
2962}
2963
2964static struct rdev_sysfs_entry rdev_recovery_start =
2965__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2966
NeilBrown16c791a2011-07-28 11:31:47 +10002967static ssize_t
2968badblocks_show(struct badblocks *bb, char *page, int unack);
2969static ssize_t
2970badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2971
NeilBrown3cb03002011-10-11 16:45:26 +11002972static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002973{
2974 return badblocks_show(&rdev->badblocks, page, 0);
2975}
NeilBrown3cb03002011-10-11 16:45:26 +11002976static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002977{
NeilBrownde393cd2011-07-28 11:31:48 +10002978 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2979 /* Maybe that ack was all we needed */
2980 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2981 wake_up(&rdev->blocked_wait);
2982 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002983}
2984static struct rdev_sysfs_entry rdev_bad_blocks =
2985__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2986
NeilBrown3cb03002011-10-11 16:45:26 +11002987static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002988{
2989 return badblocks_show(&rdev->badblocks, page, 1);
2990}
NeilBrown3cb03002011-10-11 16:45:26 +11002991static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002992{
2993 return badblocks_store(&rdev->badblocks, page, len, 1);
2994}
2995static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2996__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2997
NeilBrown86e6ffd2005-11-08 21:39:24 -08002998static struct attribute *rdev_default_attrs[] = {
2999 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003000 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003001 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003002 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003003 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003004 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003005 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003006 &rdev_bad_blocks.attr,
3007 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003008 NULL,
3009};
3010static ssize_t
3011rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3012{
3013 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003014 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003015
3016 if (!entry->show)
3017 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003018 if (!rdev->mddev)
3019 return -EBUSY;
3020 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003021}
3022
3023static ssize_t
3024rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3025 const char *page, size_t length)
3026{
3027 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003028 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003029 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003030 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003031
3032 if (!entry->store)
3033 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003034 if (!capable(CAP_SYS_ADMIN))
3035 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003036 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003037 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003038 if (rdev->mddev == NULL)
3039 rv = -EBUSY;
3040 else
3041 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003042 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003043 }
3044 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003045}
3046
3047static void rdev_free(struct kobject *ko)
3048{
NeilBrown3cb03002011-10-11 16:45:26 +11003049 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003050 kfree(rdev);
3051}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003052static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003053 .show = rdev_attr_show,
3054 .store = rdev_attr_store,
3055};
3056static struct kobj_type rdev_ktype = {
3057 .release = rdev_free,
3058 .sysfs_ops = &rdev_sysfs_ops,
3059 .default_attrs = rdev_default_attrs,
3060};
3061
NeilBrown3cb03002011-10-11 16:45:26 +11003062int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003063{
3064 rdev->desc_nr = -1;
3065 rdev->saved_raid_disk = -1;
3066 rdev->raid_disk = -1;
3067 rdev->flags = 0;
3068 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003069 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003070 rdev->sb_events = 0;
3071 rdev->last_read_error.tv_sec = 0;
3072 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003073 rdev->sb_loaded = 0;
3074 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003075 atomic_set(&rdev->nr_pending, 0);
3076 atomic_set(&rdev->read_errors, 0);
3077 atomic_set(&rdev->corrected_errors, 0);
3078
3079 INIT_LIST_HEAD(&rdev->same_set);
3080 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003081
3082 /* Add space to store bad block list.
3083 * This reserves the space even on arrays where it cannot
3084 * be used - I wonder if that matters
3085 */
3086 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003087 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003088 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3089 seqlock_init(&rdev->badblocks.lock);
3090 if (rdev->badblocks.page == NULL)
3091 return -ENOMEM;
3092
3093 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003094}
3095EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096/*
3097 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3098 *
3099 * mark the device faulty if:
3100 *
3101 * - the device is nonexistent (zero size)
3102 * - the device has no valid superblock
3103 *
3104 * a faulty rdev _never_ has rdev->sb set.
3105 */
NeilBrown3cb03002011-10-11 16:45:26 +11003106static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108 char b[BDEVNAME_SIZE];
3109 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003110 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 sector_t size;
3112
NeilBrown9ffae0c2006-01-06 00:20:32 -08003113 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 if (!rdev) {
3115 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3116 return ERR_PTR(-ENOMEM);
3117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
NeilBrown2230dfe2011-07-28 11:31:46 +10003119 err = md_rdev_init(rdev);
3120 if (err)
3121 goto abort_free;
3122 err = alloc_disk_sb(rdev);
3123 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 goto abort_free;
3125
NeilBrownc5d79ad2008-02-06 01:39:54 -08003126 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (err)
3128 goto abort_free;
3129
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003130 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003131
Mike Snitzer77304d22010-11-08 14:39:12 +01003132 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003134 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 "md: %s has zero or unknown size, marking faulty!\n",
3136 bdevname(rdev->bdev,b));
3137 err = -EINVAL;
3138 goto abort_free;
3139 }
3140
3141 if (super_format >= 0) {
3142 err = super_types[super_format].
3143 load_super(rdev, NULL, super_minor);
3144 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003145 printk(KERN_WARNING
3146 "md: %s does not have a valid v%d.%d "
3147 "superblock, not importing!\n",
3148 bdevname(rdev->bdev,b),
3149 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 goto abort_free;
3151 }
3152 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003153 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 "md: could not read %s's sb, not importing!\n",
3155 bdevname(rdev->bdev,b));
3156 goto abort_free;
3157 }
3158 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 return rdev;
3161
3162abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003163 if (rdev->bdev)
3164 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003165 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 kfree(rdev);
3167 return ERR_PTR(err);
3168}
3169
3170/*
3171 * Check a full RAID array for plausibility
3172 */
3173
NeilBrownf72ffdd2014-09-30 14:23:59 +10003174static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
3176 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003177 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 char b[BDEVNAME_SIZE];
3179
3180 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003181 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 switch (super_types[mddev->major_version].
3183 load_super(rdev, freshest, mddev->minor_version)) {
3184 case 1:
3185 freshest = rdev;
3186 break;
3187 case 0:
3188 break;
3189 default:
3190 printk( KERN_ERR \
3191 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003192 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003194 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 super_types[mddev->major_version].
3198 validate_super(mddev, freshest);
3199
3200 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003201 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003202 if (mddev->max_disks &&
3203 (rdev->desc_nr >= mddev->max_disks ||
3204 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003205 printk(KERN_WARNING
3206 "md: %s: %s: only %d devices permitted\n",
3207 mdname(mddev), bdevname(rdev->bdev, b),
3208 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003209 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003210 continue;
3211 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003212 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 if (super_types[mddev->major_version].
3214 validate_super(mddev, rdev)) {
3215 printk(KERN_WARNING "md: kicking non-fresh %s"
3216 " from array!\n",
3217 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003218 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 continue;
3220 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003221 /* No device should have a Candidate flag
3222 * when reading devices
3223 */
3224 if (test_bit(Candidate, &rdev->flags)) {
3225 pr_info("md: kicking Cluster Candidate %s from array!\n",
3226 bdevname(rdev->bdev, b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003227 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003228 }
3229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (mddev->level == LEVEL_MULTIPATH) {
3231 rdev->desc_nr = i++;
3232 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003233 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003234 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003235 rdev->raid_disk = -1;
3236 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
3238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
NeilBrown72e02072009-12-14 12:49:55 +11003241/* Read a fixed-point number.
3242 * Numbers in sysfs attributes should be in "standard" units where
3243 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003244 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003245 * milliseconds or jiffies.
3246 * This function takes a decimal number with a possible fractional
3247 * component, and produces an integer which is the result of
3248 * multiplying that number by 10^'scale'.
3249 * all without any floating-point arithmetic.
3250 */
3251int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3252{
3253 unsigned long result = 0;
3254 long decimals = -1;
3255 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3256 if (*cp == '.')
3257 decimals = 0;
3258 else if (decimals < scale) {
3259 unsigned int value;
3260 value = *cp - '0';
3261 result = result * 10 + value;
3262 if (decimals >= 0)
3263 decimals++;
3264 }
3265 cp++;
3266 }
3267 if (*cp == '\n')
3268 cp++;
3269 if (*cp)
3270 return -EINVAL;
3271 if (decimals < 0)
3272 decimals = 0;
3273 while (decimals < scale) {
3274 result *= 10;
3275 decimals ++;
3276 }
3277 *res = result;
3278 return 0;
3279}
3280
NeilBrown19052c02008-08-05 15:54:13 +10003281static void md_safemode_timeout(unsigned long data);
3282
NeilBrowneae17012005-11-08 21:39:23 -08003283static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003284safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003285{
3286 int msec = (mddev->safemode_delay*1000)/HZ;
3287 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3288}
3289static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003290safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003291{
NeilBrown16f17b32006-06-26 00:27:37 -07003292 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003293
NeilBrown72e02072009-12-14 12:49:55 +11003294 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003295 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003296 if (msec == 0)
3297 mddev->safemode_delay = 0;
3298 else {
NeilBrown19052c02008-08-05 15:54:13 +10003299 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003300 unsigned long new_delay = (msec*HZ)/1000;
3301
3302 if (new_delay == 0)
3303 new_delay = 1;
3304 mddev->safemode_delay = new_delay;
3305 if (new_delay < old_delay || old_delay == 0)
3306 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003307 }
3308 return len;
3309}
3310static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003311__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003312
3313static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003314level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003315{
NeilBrown36d091f2014-12-15 12:56:58 +11003316 struct md_personality *p;
3317 int ret;
3318 spin_lock(&mddev->lock);
3319 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003320 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003321 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003322 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003323 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003324 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003325 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003326 else
NeilBrown36d091f2014-12-15 12:56:58 +11003327 ret = 0;
3328 spin_unlock(&mddev->lock);
3329 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003330}
3331
NeilBrownd9d166c2006-01-06 00:20:51 -08003332static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003333level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003334{
Dan Williamsf2859af2010-05-02 10:04:16 -07003335 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003336 ssize_t rv;
3337 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003338 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003339 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003340 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003341 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003342
NeilBrown67918752014-12-15 12:57:01 +11003343 if (slen == 0 || slen >= sizeof(clevel))
3344 return -EINVAL;
3345
3346 rv = mddev_lock(mddev);
3347 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003348 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003349
3350 if (mddev->pers == NULL) {
3351 strncpy(mddev->clevel, buf, slen);
3352 if (mddev->clevel[slen-1] == '\n')
3353 slen--;
3354 mddev->clevel[slen] = 0;
3355 mddev->level = LEVEL_NONE;
3356 rv = len;
3357 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003358 }
NeilBrown67918752014-12-15 12:57:01 +11003359 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003360 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003361 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003362
3363 /* request to change the personality. Need to ensure:
3364 * - array is not engaged in resync/recovery/reshape
3365 * - old personality can be suspended
3366 * - new personality will access other array.
3367 */
3368
NeilBrown67918752014-12-15 12:57:01 +11003369 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003370 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003371 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003372 mddev->reshape_position != MaxSector ||
3373 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003374 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003375
NeilBrown67918752014-12-15 12:57:01 +11003376 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003377 if (!mddev->pers->quiesce) {
3378 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3379 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003380 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003381 }
3382
3383 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003384 strncpy(clevel, buf, slen);
3385 if (clevel[slen-1] == '\n')
3386 slen--;
3387 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003388 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003389 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003390
Dan Williamsf2859af2010-05-02 10:04:16 -07003391 if (request_module("md-%s", clevel) != 0)
3392 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003393 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003394 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003395 if (!pers || !try_module_get(pers->owner)) {
3396 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003397 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003398 rv = -EINVAL;
3399 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003400 }
3401 spin_unlock(&pers_lock);
3402
3403 if (pers == mddev->pers) {
3404 /* Nothing to do! */
3405 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003406 rv = len;
3407 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003408 }
3409 if (!pers->takeover) {
3410 module_put(pers->owner);
3411 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003412 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003413 rv = -EINVAL;
3414 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003415 }
3416
NeilBrowndafb20f2012-03-19 12:46:39 +11003417 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003418 rdev->new_raid_disk = rdev->raid_disk;
3419
NeilBrown245f46c2009-03-31 14:39:39 +11003420 /* ->takeover must set new_* and/or delta_disks
3421 * if it succeeds, and may set them when it fails.
3422 */
3423 priv = pers->takeover(mddev);
3424 if (IS_ERR(priv)) {
3425 mddev->new_level = mddev->level;
3426 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003427 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003428 mddev->raid_disks -= mddev->delta_disks;
3429 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003430 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003431 module_put(pers->owner);
3432 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003433 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003434 rv = PTR_ERR(priv);
3435 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003436 }
3437
3438 /* Looks like we have a winner */
3439 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003440 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003441
3442 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003443 oldpers = mddev->pers;
3444 oldpriv = mddev->private;
3445 mddev->pers = pers;
3446 mddev->private = priv;
3447 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3448 mddev->level = mddev->new_level;
3449 mddev->layout = mddev->new_layout;
3450 mddev->chunk_sectors = mddev->new_chunk_sectors;
3451 mddev->delta_disks = 0;
3452 mddev->reshape_backwards = 0;
3453 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003454 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003455
NeilBrowndb721d32014-12-15 12:56:58 +11003456 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003457 mddev->external) {
3458 /* We are converting from a no-redundancy array
3459 * to a redundancy array and metadata is managed
3460 * externally so we need to be sure that writes
3461 * won't block due to a need to transition
3462 * clean->dirty
3463 * until external management is started.
3464 */
3465 mddev->in_sync = 0;
3466 mddev->safemode_delay = 0;
3467 mddev->safemode = 0;
3468 }
3469
NeilBrowndb721d32014-12-15 12:56:58 +11003470 oldpers->free(mddev, oldpriv);
3471
3472 if (oldpers->sync_request == NULL &&
3473 pers->sync_request != NULL) {
3474 /* need to add the md_redundancy_group */
3475 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3476 printk(KERN_WARNING
3477 "md: cannot register extra attributes for %s\n",
3478 mdname(mddev));
3479 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3480 }
3481 if (oldpers->sync_request != NULL &&
3482 pers->sync_request == NULL) {
3483 /* need to remove the md_redundancy_group */
3484 if (mddev->to_remove == NULL)
3485 mddev->to_remove = &md_redundancy_group;
3486 }
3487
NeilBrowndafb20f2012-03-19 12:46:39 +11003488 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003489 if (rdev->raid_disk < 0)
3490 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003491 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003492 rdev->new_raid_disk = -1;
3493 if (rdev->new_raid_disk == rdev->raid_disk)
3494 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003495 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003496 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003497 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003498 if (rdev->raid_disk < 0)
3499 continue;
3500 if (rdev->new_raid_disk == rdev->raid_disk)
3501 continue;
3502 rdev->raid_disk = rdev->new_raid_disk;
3503 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003504 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003505 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003506 if (sysfs_link_rdev(mddev, rdev))
3507 printk(KERN_WARNING "md: cannot register rd%d"
3508 " for %s after level change\n",
3509 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003510 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003511 }
3512
NeilBrowndb721d32014-12-15 12:56:58 +11003513 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003514 /* this is now an array without redundancy, so
3515 * it must always be in_sync
3516 */
3517 mddev->in_sync = 1;
3518 del_timer_sync(&mddev->safemode_timer);
3519 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003520 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003521 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003522 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003523 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003524 if (!mddev->thread)
3525 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003526 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003527 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003528 rv = len;
3529out_unlock:
3530 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003531 return rv;
3532}
3533
3534static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003535__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003536
NeilBrownd4dbd022006-06-26 00:27:59 -07003537static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003538layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003539{
3540 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003541 if (mddev->reshape_position != MaxSector &&
3542 mddev->layout != mddev->new_layout)
3543 return sprintf(page, "%d (%d)\n",
3544 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003545 return sprintf(page, "%d\n", mddev->layout);
3546}
3547
3548static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003549layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003550{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003551 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003552 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003553
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003554 err = kstrtouint(buf, 10, &n);
3555 if (err < 0)
3556 return err;
NeilBrown67918752014-12-15 12:57:01 +11003557 err = mddev_lock(mddev);
3558 if (err)
3559 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003560
NeilBrownb3546032009-03-31 14:56:41 +11003561 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003562 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003563 err = -EBUSY;
3564 else if (mddev->ro)
3565 err = -EROFS;
3566 else {
3567 mddev->new_layout = n;
3568 err = mddev->pers->check_reshape(mddev);
3569 if (err)
3570 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003571 }
NeilBrownb3546032009-03-31 14:56:41 +11003572 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003573 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003574 if (mddev->reshape_position == MaxSector)
3575 mddev->layout = n;
3576 }
NeilBrown67918752014-12-15 12:57:01 +11003577 mddev_unlock(mddev);
3578 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003579}
3580static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003581__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003582
NeilBrowneae17012005-11-08 21:39:23 -08003583static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003584raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003585{
NeilBrownbb636542005-11-08 21:39:45 -08003586 if (mddev->raid_disks == 0)
3587 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003588 if (mddev->reshape_position != MaxSector &&
3589 mddev->delta_disks != 0)
3590 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3591 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003592 return sprintf(page, "%d\n", mddev->raid_disks);
3593}
3594
NeilBrownfd01b882011-10-11 16:47:53 +11003595static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003596
3597static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003598raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003599{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003600 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003601 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003602
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003603 err = kstrtouint(buf, 10, &n);
3604 if (err < 0)
3605 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003606
NeilBrown67918752014-12-15 12:57:01 +11003607 err = mddev_lock(mddev);
3608 if (err)
3609 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003610 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003611 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003612 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003613 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003614 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003615
NeilBrown67918752014-12-15 12:57:01 +11003616 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003617 rdev_for_each(rdev, mddev) {
3618 if (olddisks < n &&
3619 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003620 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003621 if (olddisks > n &&
3622 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003623 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003624 }
NeilBrown67918752014-12-15 12:57:01 +11003625 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003626 mddev->delta_disks = n - olddisks;
3627 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003628 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003629 } else
NeilBrownda943b992006-01-06 00:20:54 -08003630 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003631out_unlock:
3632 mddev_unlock(mddev);
3633 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003634}
3635static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003636__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003637
NeilBrown24dd4692005-11-08 21:39:26 -08003638static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003639chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003640{
NeilBrown08a02ec2007-05-09 02:35:38 -07003641 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003642 mddev->chunk_sectors != mddev->new_chunk_sectors)
3643 return sprintf(page, "%d (%d)\n",
3644 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003645 mddev->chunk_sectors << 9);
3646 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003647}
3648
3649static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003650chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003651{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003652 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003653 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003654
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003655 err = kstrtoul(buf, 10, &n);
3656 if (err < 0)
3657 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003658
NeilBrown67918752014-12-15 12:57:01 +11003659 err = mddev_lock(mddev);
3660 if (err)
3661 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003662 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003663 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003664 err = -EBUSY;
3665 else if (mddev->ro)
3666 err = -EROFS;
3667 else {
3668 mddev->new_chunk_sectors = n >> 9;
3669 err = mddev->pers->check_reshape(mddev);
3670 if (err)
3671 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003672 }
NeilBrownb3546032009-03-31 14:56:41 +11003673 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003674 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003675 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003676 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003677 }
NeilBrown67918752014-12-15 12:57:01 +11003678 mddev_unlock(mddev);
3679 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003680}
3681static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003682__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003683
NeilBrowna94213b2006-06-26 00:28:00 -07003684static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003685resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003686{
NeilBrownd1a7c502009-03-31 15:24:32 +11003687 if (mddev->recovery_cp == MaxSector)
3688 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003689 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3690}
3691
3692static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003693resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003694{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003695 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003696 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003697
3698 if (cmd_match(buf, "none"))
3699 n = MaxSector;
3700 else {
3701 err = kstrtoull(buf, 10, &n);
3702 if (err < 0)
3703 return err;
3704 if (n != (sector_t)n)
3705 return -EINVAL;
3706 }
NeilBrowna94213b2006-06-26 00:28:00 -07003707
NeilBrown67918752014-12-15 12:57:01 +11003708 err = mddev_lock(mddev);
3709 if (err)
3710 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003711 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003712 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003713
NeilBrown67918752014-12-15 12:57:01 +11003714 if (!err) {
3715 mddev->recovery_cp = n;
3716 if (mddev->pers)
3717 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3718 }
3719 mddev_unlock(mddev);
3720 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003721}
3722static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003723__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3724 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003725
NeilBrown9e653b62006-06-26 00:27:58 -07003726/*
3727 * The array state can be:
3728 *
3729 * clear
3730 * No devices, no size, no level
3731 * Equivalent to STOP_ARRAY ioctl
3732 * inactive
3733 * May have some settings, but array is not active
3734 * all IO results in error
3735 * When written, doesn't tear down array, but just stops it
3736 * suspended (not supported yet)
3737 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003738 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003739 * readonly
3740 * no resync can happen. no superblocks get written.
3741 * write requests fail
3742 * read-auto
3743 * like readonly, but behaves like 'clean' on a write request.
3744 *
3745 * clean - no pending writes, but otherwise active.
3746 * When written to inactive array, starts without resync
3747 * If a write request arrives then
3748 * if metadata is known, mark 'dirty' and switch to 'active'.
3749 * if not known, block and switch to write-pending
3750 * If written to an active array that has pending writes, then fails.
3751 * active
3752 * fully active: IO and resync can be happening.
3753 * When written to inactive array, starts with resync
3754 *
3755 * write-pending
3756 * clean, but writes are blocked waiting for 'active' to be written.
3757 *
3758 * active-idle
3759 * like active, but no writes have been seen for a while (100msec).
3760 *
3761 */
3762enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3763 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003764static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003765 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3766 "write-pending", "active-idle", NULL };
3767
3768static int match_word(const char *word, char **list)
3769{
3770 int n;
3771 for (n=0; list[n]; n++)
3772 if (cmd_match(word, list[n]))
3773 break;
3774 return n;
3775}
3776
3777static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003778array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003779{
3780 enum array_state st = inactive;
3781
3782 if (mddev->pers)
3783 switch(mddev->ro) {
3784 case 1:
3785 st = readonly;
3786 break;
3787 case 2:
3788 st = read_auto;
3789 break;
3790 case 0:
3791 if (mddev->in_sync)
3792 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003793 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003794 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003795 else if (mddev->safemode)
3796 st = active_idle;
3797 else
3798 st = active;
3799 }
3800 else {
3801 if (list_empty(&mddev->disks) &&
3802 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003803 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003804 st = clear;
3805 else
3806 st = inactive;
3807 }
3808 return sprintf(page, "%s\n", array_states[st]);
3809}
3810
NeilBrownf72ffdd2014-09-30 14:23:59 +10003811static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3812static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3813static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003814static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003815
3816static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003817array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003818{
NeilBrown67918752014-12-15 12:57:01 +11003819 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003820 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003821
3822 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3823 /* don't take reconfig_mutex when toggling between
3824 * clean and active
3825 */
3826 spin_lock(&mddev->lock);
3827 if (st == active) {
3828 restart_array(mddev);
3829 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3830 wake_up(&mddev->sb_wait);
3831 err = 0;
3832 } else /* st == clean */ {
3833 restart_array(mddev);
3834 if (atomic_read(&mddev->writes_pending) == 0) {
3835 if (mddev->in_sync == 0) {
3836 mddev->in_sync = 1;
3837 if (mddev->safemode == 1)
3838 mddev->safemode = 0;
3839 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3840 }
3841 err = 0;
3842 } else
3843 err = -EBUSY;
3844 }
3845 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003846 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003847 }
3848 err = mddev_lock(mddev);
3849 if (err)
3850 return err;
3851 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003852 switch(st) {
3853 case bad_word:
3854 break;
3855 case clear:
3856 /* stopping an active array */
NeilBrowna05b7ea02012-07-19 15:59:18 +10003857 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003858 break;
3859 case inactive:
3860 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003861 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003862 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003863 else
NeilBrowne6910632008-02-06 01:39:51 -08003864 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003865 break;
3866 case suspended:
3867 break; /* not supported yet */
3868 case readonly:
3869 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003870 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003871 else {
3872 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003873 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003874 err = do_md_run(mddev);
3875 }
3876 break;
3877 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003878 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003879 if (mddev->ro == 0)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003880 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003881 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003882 err = restart_array(mddev);
3883 if (err == 0) {
3884 mddev->ro = 2;
3885 set_disk_ro(mddev->gendisk, 0);
3886 }
NeilBrown9e653b62006-06-26 00:27:58 -07003887 } else {
3888 mddev->ro = 2;
3889 err = do_md_run(mddev);
3890 }
3891 break;
3892 case clean:
3893 if (mddev->pers) {
3894 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003895 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003896 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003897 if (mddev->in_sync == 0) {
3898 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003899 if (mddev->safemode == 1)
3900 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003901 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003902 }
3903 err = 0;
3904 } else
3905 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003906 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003907 } else
3908 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003909 break;
3910 case active:
3911 if (mddev->pers) {
3912 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003913 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003914 wake_up(&mddev->sb_wait);
3915 err = 0;
3916 } else {
3917 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003918 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003919 err = do_md_run(mddev);
3920 }
3921 break;
3922 case write_pending:
3923 case active_idle:
3924 /* these cannot be set */
3925 break;
3926 }
NeilBrown67918752014-12-15 12:57:01 +11003927
3928 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11003929 if (mddev->hold_active == UNTIL_IOCTL)
3930 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003931 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003932 }
NeilBrown67918752014-12-15 12:57:01 +11003933 mddev_unlock(mddev);
3934 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07003935}
NeilBrown80ca3a42006-07-10 04:44:18 -07003936static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10003937__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003938
NeilBrown6d7ff732006-01-06 00:21:16 -08003939static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003940max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003941 return sprintf(page, "%d\n",
3942 atomic_read(&mddev->max_corr_read_errors));
3943}
3944
3945static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003946max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003947{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003948 unsigned int n;
3949 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11003950
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003951 rv = kstrtouint(buf, 10, &n);
3952 if (rv < 0)
3953 return rv;
3954 atomic_set(&mddev->max_corr_read_errors, n);
3955 return len;
Robert Becker1e509152009-12-14 12:49:58 +11003956}
3957
3958static struct md_sysfs_entry max_corr_read_errors =
3959__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3960 max_corrected_read_errors_store);
3961
3962static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003963null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003964{
3965 return -EINVAL;
3966}
3967
3968static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003969new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08003970{
3971 /* buf must be %d:%d\n? giving major and minor numbers */
3972 /* The new device is added to the array.
3973 * If the array has a persistent superblock, we read the
3974 * superblock to initialise info and check validity.
3975 * Otherwise, only checking done is that in bind_rdev_to_array,
3976 * which mainly checks size.
3977 */
3978 char *e;
3979 int major = simple_strtoul(buf, &e, 10);
3980 int minor;
3981 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003982 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08003983 int err;
3984
3985 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3986 return -EINVAL;
3987 minor = simple_strtoul(e+1, &e, 10);
3988 if (*e && *e != '\n')
3989 return -EINVAL;
3990 dev = MKDEV(major, minor);
3991 if (major != MAJOR(dev) ||
3992 minor != MINOR(dev))
3993 return -EOVERFLOW;
3994
NeilBrown67918752014-12-15 12:57:01 +11003995 flush_workqueue(md_misc_wq);
3996
3997 err = mddev_lock(mddev);
3998 if (err)
3999 return err;
NeilBrown6d7ff732006-01-06 00:21:16 -08004000 if (mddev->persistent) {
4001 rdev = md_import_device(dev, mddev->major_version,
4002 mddev->minor_version);
4003 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004004 struct md_rdev *rdev0
4005 = list_entry(mddev->disks.next,
4006 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08004007 err = super_types[mddev->major_version]
4008 .load_super(rdev, rdev0, mddev->minor_version);
4009 if (err < 0)
4010 goto out;
4011 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004012 } else if (mddev->external)
4013 rdev = md_import_device(dev, -2, -1);
4014 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004015 rdev = md_import_device(dev, -1, -1);
4016
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004017 if (IS_ERR(rdev)) {
4018 mddev_unlock(mddev);
NeilBrown6d7ff732006-01-06 00:21:16 -08004019 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004020 }
NeilBrown6d7ff732006-01-06 00:21:16 -08004021 err = bind_rdev_to_array(rdev, mddev);
4022 out:
4023 if (err)
4024 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004025 mddev_unlock(mddev);
NeilBrown6d7ff732006-01-06 00:21:16 -08004026 return err ? err : len;
4027}
4028
4029static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004030__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004031
4032static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004033bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004034{
4035 char *end;
4036 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004037 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004038
NeilBrown67918752014-12-15 12:57:01 +11004039 err = mddev_lock(mddev);
4040 if (err)
4041 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004042 if (!mddev->bitmap)
4043 goto out;
4044 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4045 while (*buf) {
4046 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4047 if (buf == end) break;
4048 if (*end == '-') { /* range */
4049 buf = end + 1;
4050 end_chunk = simple_strtoul(buf, &end, 0);
4051 if (buf == end) break;
4052 }
4053 if (*end && !isspace(*end)) break;
4054 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004055 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004056 }
4057 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4058out:
NeilBrown67918752014-12-15 12:57:01 +11004059 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004060 return len;
4061}
4062
4063static struct md_sysfs_entry md_bitmap =
4064__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4065
4066static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004067size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004068{
Andre Noll58c0fed2009-03-31 14:33:13 +11004069 return sprintf(page, "%llu\n",
4070 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004071}
4072
NeilBrownfd01b882011-10-11 16:47:53 +11004073static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004074
4075static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004076size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004077{
4078 /* If array is inactive, we can reduce the component size, but
4079 * not increase it (except from 0).
4080 * If array is active, we can try an on-line resize
4081 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004082 sector_t sectors;
4083 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004084
Andre Noll58c0fed2009-03-31 14:33:13 +11004085 if (err < 0)
4086 return err;
NeilBrown67918752014-12-15 12:57:01 +11004087 err = mddev_lock(mddev);
4088 if (err)
4089 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004090 if (mddev->pers) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004091 if (mddev_is_clustered(mddev))
4092 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11004093 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004094 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004095 if (mddev_is_clustered(mddev))
4096 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004097 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004098 if (mddev->dev_sectors == 0 ||
4099 mddev->dev_sectors > sectors)
4100 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004101 else
4102 err = -ENOSPC;
4103 }
NeilBrown67918752014-12-15 12:57:01 +11004104 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004105 return err ? err : len;
4106}
4107
4108static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004109__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004110
Masanari Iida83f0d772012-10-30 00:18:08 +09004111/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004112 * This is one of
4113 * 'none' for arrays with no metadata (good luck...)
4114 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004115 * or N.M for internally known formats
4116 */
4117static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004118metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004119{
4120 if (mddev->persistent)
4121 return sprintf(page, "%d.%d\n",
4122 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004123 else if (mddev->external)
4124 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004125 else
4126 return sprintf(page, "none\n");
4127}
4128
4129static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004130metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004131{
4132 int major, minor;
4133 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004134 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004135 /* Changing the details of 'external' metadata is
4136 * always permitted. Otherwise there must be
4137 * no devices attached to the array.
4138 */
NeilBrown67918752014-12-15 12:57:01 +11004139
4140 err = mddev_lock(mddev);
4141 if (err)
4142 return err;
4143 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004144 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4145 ;
4146 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004147 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004148
NeilBrown67918752014-12-15 12:57:01 +11004149 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004150 if (cmd_match(buf, "none")) {
4151 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004152 mddev->external = 0;
4153 mddev->major_version = 0;
4154 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004155 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004156 }
4157 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004158 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004159 if (namelen >= sizeof(mddev->metadata_type))
4160 namelen = sizeof(mddev->metadata_type)-1;
4161 strncpy(mddev->metadata_type, buf+9, namelen);
4162 mddev->metadata_type[namelen] = 0;
4163 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4164 mddev->metadata_type[--namelen] = 0;
4165 mddev->persistent = 0;
4166 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004167 mddev->major_version = 0;
4168 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004169 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004170 }
4171 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004172 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004173 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004174 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004175 buf = e+1;
4176 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004177 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004178 goto out_unlock;
4179 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004180 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004181 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004182 mddev->major_version = major;
4183 mddev->minor_version = minor;
4184 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004185 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004186 err = 0;
4187out_unlock:
4188 mddev_unlock(mddev);
4189 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004190}
4191
4192static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004193__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004194
NeilBrowna35b0d62006-01-06 00:20:49 -08004195static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004196action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004197{
NeilBrown7eec3142005-11-08 21:39:44 -08004198 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004199 unsigned long recovery = mddev->recovery;
4200 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004201 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004202 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4203 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4204 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004205 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004206 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4207 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004208 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004209 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004210 type = "check";
4211 else
4212 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004213 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004214 type = "recover";
4215 }
4216 return sprintf(page, "%s\n", type);
4217}
4218
4219static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004220action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004221{
NeilBrown7eec3142005-11-08 21:39:44 -08004222 if (!mddev->pers || !mddev->pers->sync_request)
4223 return -EINVAL;
4224
NeilBrownb6a9ce62009-05-26 09:41:17 +10004225
4226 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004227 if (cmd_match(page, "frozen"))
4228 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4229 else
4230 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004231 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4232 mddev_lock(mddev) == 0) {
4233 flush_workqueue(md_misc_wq);
4234 if (mddev->sync_thread) {
4235 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004236 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004237 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004238 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004239 }
NeilBrown03c902e2006-01-06 00:20:46 -08004240 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4241 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004242 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004243 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004244 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004245 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004246 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004247 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004248 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004249 int err;
4250 if (mddev->pers->start_reshape == NULL)
4251 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004252 err = mddev_lock(mddev);
4253 if (!err) {
NeilBrown56ccc112015-05-28 17:53:29 +10004254 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004255 err = mddev->pers->start_reshape(mddev);
4256 mddev_unlock(mddev);
4257 }
NeilBrown16484bf2006-03-27 01:18:13 -08004258 if (err)
4259 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004260 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004261 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004262 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004263 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004264 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004265 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004266 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004267 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4268 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004269 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004270 if (mddev->ro == 2) {
4271 /* A write to sync_action is enough to justify
4272 * canceling read-auto mode
4273 */
4274 mddev->ro = 0;
4275 md_wakeup_thread(mddev->sync_thread);
4276 }
NeilBrown03c902e2006-01-06 00:20:46 -08004277 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004278 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004279 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004280 return len;
4281}
4282
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004283static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004284__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004285
4286static ssize_t
4287last_sync_action_show(struct mddev *mddev, char *page)
4288{
4289 return sprintf(page, "%s\n", mddev->last_sync_action);
4290}
4291
4292static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4293
NeilBrown9d888832005-11-08 21:39:26 -08004294static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004295mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004296{
4297 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004298 (unsigned long long)
4299 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004300}
4301
NeilBrown80ca3a42006-07-10 04:44:18 -07004302static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004303
NeilBrown88202a02006-01-06 00:21:36 -08004304static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004305sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004306{
4307 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4308 mddev->sync_speed_min ? "local": "system");
4309}
4310
4311static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004312sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004313{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004314 unsigned int min;
4315 int rv;
4316
NeilBrown88202a02006-01-06 00:21:36 -08004317 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004318 min = 0;
4319 } else {
4320 rv = kstrtouint(buf, 10, &min);
4321 if (rv < 0)
4322 return rv;
4323 if (min == 0)
4324 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004325 }
NeilBrown88202a02006-01-06 00:21:36 -08004326 mddev->sync_speed_min = min;
4327 return len;
4328}
4329
4330static struct md_sysfs_entry md_sync_min =
4331__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4332
4333static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004334sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004335{
4336 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4337 mddev->sync_speed_max ? "local": "system");
4338}
4339
4340static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004341sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004342{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004343 unsigned int max;
4344 int rv;
4345
NeilBrown88202a02006-01-06 00:21:36 -08004346 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004347 max = 0;
4348 } else {
4349 rv = kstrtouint(buf, 10, &max);
4350 if (rv < 0)
4351 return rv;
4352 if (max == 0)
4353 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004354 }
NeilBrown88202a02006-01-06 00:21:36 -08004355 mddev->sync_speed_max = max;
4356 return len;
4357}
4358
4359static struct md_sysfs_entry md_sync_max =
4360__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4361
Iustin Popd7f3d292007-10-16 23:30:54 -07004362static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004363degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004364{
4365 return sprintf(page, "%d\n", mddev->degraded);
4366}
4367static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004368
4369static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004370sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004371{
4372 return sprintf(page, "%d\n", mddev->parallel_resync);
4373}
4374
4375static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004376sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004377{
4378 long n;
4379
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004380 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004381 return -EINVAL;
4382
4383 if (n != 0 && n != 1)
4384 return -EINVAL;
4385
4386 mddev->parallel_resync = n;
4387
4388 if (mddev->sync_thread)
4389 wake_up(&resync_wait);
4390
4391 return len;
4392}
4393
4394/* force parallel resync, even with shared block devices */
4395static struct md_sysfs_entry md_sync_force_parallel =
4396__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4397 sync_force_parallel_show, sync_force_parallel_store);
4398
4399static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004400sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004401{
4402 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004403 if (mddev->curr_resync == 0)
4404 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004405 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4406 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004407 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004408 db = resync - mddev->resync_mark_cnt;
4409 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004410}
4411
NeilBrown80ca3a42006-07-10 04:44:18 -07004412static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004413
4414static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004415sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004416{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004417 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004418
NeilBrownacb180b2009-04-14 16:28:34 +10004419 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4420 return sprintf(page, "none\n");
4421
NeilBrown72f36d52012-10-11 14:25:57 +11004422 if (mddev->curr_resync == 1 ||
4423 mddev->curr_resync == 2)
4424 return sprintf(page, "delayed\n");
4425
NeilBrownc804cde2012-05-21 09:28:33 +10004426 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4427 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004428 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004429 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004430 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004431
NeilBrownacb180b2009-04-14 16:28:34 +10004432 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004433 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004434}
4435
NeilBrown750f1992014-09-30 08:53:05 +10004436static struct md_sysfs_entry md_sync_completed =
4437 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004438
NeilBrowne464eaf2006-03-27 01:18:14 -08004439static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004440min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004441{
4442 return sprintf(page, "%llu\n",
4443 (unsigned long long)mddev->resync_min);
4444}
4445static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004446min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004447{
4448 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004449 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004450
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004451 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004452 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004453
4454 spin_lock(&mddev->lock);
4455 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004456 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004457 goto out_unlock;
4458
4459 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004460 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004461 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004462
NeilBrown50c37b12015-03-23 17:36:38 +11004463 /* Round down to multiple of 4K for safety */
4464 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004465 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004466
NeilBrown23da4222014-12-15 12:57:01 +11004467out_unlock:
4468 spin_unlock(&mddev->lock);
4469 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004470}
4471
4472static struct md_sysfs_entry md_min_sync =
4473__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4474
4475static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004476max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004477{
4478 if (mddev->resync_max == MaxSector)
4479 return sprintf(page, "max\n");
4480 else
4481 return sprintf(page, "%llu\n",
4482 (unsigned long long)mddev->resync_max);
4483}
4484static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004485max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004486{
NeilBrown23da4222014-12-15 12:57:01 +11004487 int err;
4488 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004489 if (strncmp(buf, "max", 3) == 0)
4490 mddev->resync_max = MaxSector;
4491 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004492 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004493 int chunk;
4494
4495 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004496 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004497 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004498 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004499 goto out_unlock;
4500
4501 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004502 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004503 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004504 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004505 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004506
4507 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004508 chunk = mddev->chunk_sectors;
4509 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004510 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004511
4512 err = -EINVAL;
4513 if (sector_div(temp, chunk))
4514 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004515 }
4516 mddev->resync_max = max;
4517 }
4518 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004519 err = 0;
4520out_unlock:
4521 spin_unlock(&mddev->lock);
4522 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004523}
4524
4525static struct md_sysfs_entry md_max_sync =
4526__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4527
4528static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004529suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004530{
4531 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4532}
4533
4534static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004535suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004536{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004537 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004538 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004539
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004540 err = kstrtoull(buf, 10, &new);
4541 if (err < 0)
4542 return err;
4543 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004544 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004545
NeilBrown67918752014-12-15 12:57:01 +11004546 err = mddev_lock(mddev);
4547 if (err)
4548 return err;
4549 err = -EINVAL;
4550 if (mddev->pers == NULL ||
4551 mddev->pers->quiesce == NULL)
4552 goto unlock;
4553 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004554 mddev->suspend_lo = new;
4555 if (new >= old)
4556 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004557 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004558 else {
4559 /* Expanding suspended region - need to wait */
4560 mddev->pers->quiesce(mddev, 1);
4561 mddev->pers->quiesce(mddev, 0);
4562 }
NeilBrown67918752014-12-15 12:57:01 +11004563 err = 0;
4564unlock:
4565 mddev_unlock(mddev);
4566 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004567}
4568static struct md_sysfs_entry md_suspend_lo =
4569__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4570
NeilBrowne464eaf2006-03-27 01:18:14 -08004571static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004572suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004573{
4574 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4575}
4576
4577static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004578suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004579{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004580 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004581 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004582
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004583 err = kstrtoull(buf, 10, &new);
4584 if (err < 0)
4585 return err;
4586 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004587 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004588
NeilBrown67918752014-12-15 12:57:01 +11004589 err = mddev_lock(mddev);
4590 if (err)
4591 return err;
4592 err = -EINVAL;
4593 if (mddev->pers == NULL ||
4594 mddev->pers->quiesce == NULL)
4595 goto unlock;
4596 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004597 mddev->suspend_hi = new;
4598 if (new <= old)
4599 /* Shrinking suspended region */
4600 mddev->pers->quiesce(mddev, 2);
4601 else {
4602 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004603 mddev->pers->quiesce(mddev, 1);
4604 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004605 }
NeilBrown67918752014-12-15 12:57:01 +11004606 err = 0;
4607unlock:
4608 mddev_unlock(mddev);
4609 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004610}
4611static struct md_sysfs_entry md_suspend_hi =
4612__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4613
NeilBrown08a02ec2007-05-09 02:35:38 -07004614static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004615reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004616{
4617 if (mddev->reshape_position != MaxSector)
4618 return sprintf(page, "%llu\n",
4619 (unsigned long long)mddev->reshape_position);
4620 strcpy(page, "none\n");
4621 return 5;
4622}
4623
4624static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004625reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004626{
NeilBrownc6563a82012-05-21 09:27:00 +10004627 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004628 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004629 int err;
NeilBrown67918752014-12-15 12:57:01 +11004630
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004631 err = kstrtoull(buf, 10, &new);
4632 if (err < 0)
4633 return err;
4634 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004635 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004636 err = mddev_lock(mddev);
4637 if (err)
4638 return err;
4639 err = -EBUSY;
4640 if (mddev->pers)
4641 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004642 mddev->reshape_position = new;
4643 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004644 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004645 mddev->new_level = mddev->level;
4646 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004647 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004648 rdev_for_each(rdev, mddev)
4649 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004650 err = 0;
4651unlock:
4652 mddev_unlock(mddev);
4653 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004654}
4655
4656static struct md_sysfs_entry md_reshape_position =
4657__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4658 reshape_position_store);
4659
Dan Williamsb522adc2009-03-31 15:00:31 +11004660static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004661reshape_direction_show(struct mddev *mddev, char *page)
4662{
4663 return sprintf(page, "%s\n",
4664 mddev->reshape_backwards ? "backwards" : "forwards");
4665}
4666
4667static ssize_t
4668reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4669{
4670 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004671 int err;
4672
NeilBrown2c810cd2012-05-21 09:27:00 +10004673 if (cmd_match(buf, "forwards"))
4674 backwards = 0;
4675 else if (cmd_match(buf, "backwards"))
4676 backwards = 1;
4677 else
4678 return -EINVAL;
4679 if (mddev->reshape_backwards == backwards)
4680 return len;
4681
NeilBrown67918752014-12-15 12:57:01 +11004682 err = mddev_lock(mddev);
4683 if (err)
4684 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004685 /* check if we are allowed to change */
4686 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004687 err = -EBUSY;
4688 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004689 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004690 err = -EINVAL;
4691 else
4692 mddev->reshape_backwards = backwards;
4693 mddev_unlock(mddev);
4694 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004695}
4696
4697static struct md_sysfs_entry md_reshape_direction =
4698__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4699 reshape_direction_store);
4700
4701static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004702array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004703{
4704 if (mddev->external_size)
4705 return sprintf(page, "%llu\n",
4706 (unsigned long long)mddev->array_sectors/2);
4707 else
4708 return sprintf(page, "default\n");
4709}
4710
4711static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004712array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004713{
4714 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004715 int err;
4716
4717 err = mddev_lock(mddev);
4718 if (err)
4719 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004720
4721 if (strncmp(buf, "default", 7) == 0) {
4722 if (mddev->pers)
4723 sectors = mddev->pers->size(mddev, 0, 0);
4724 else
4725 sectors = mddev->array_sectors;
4726
4727 mddev->external_size = 0;
4728 } else {
4729 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004730 err = -EINVAL;
4731 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4732 err = -E2BIG;
4733 else
4734 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004735 }
4736
NeilBrown67918752014-12-15 12:57:01 +11004737 if (!err) {
4738 mddev->array_sectors = sectors;
4739 if (mddev->pers) {
4740 set_capacity(mddev->gendisk, mddev->array_sectors);
4741 revalidate_disk(mddev->gendisk);
4742 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004743 }
NeilBrown67918752014-12-15 12:57:01 +11004744 mddev_unlock(mddev);
4745 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004746}
4747
4748static struct md_sysfs_entry md_array_size =
4749__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4750 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004751
NeilBrowneae17012005-11-08 21:39:23 -08004752static struct attribute *md_default_attrs[] = {
4753 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004754 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004755 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004756 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004757 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004758 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004759 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004760 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004761 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004762 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004763 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004764 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004765 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004766 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004767 NULL,
4768};
4769
4770static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004771 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004772 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004773 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004774 &md_sync_min.attr,
4775 &md_sync_max.attr,
4776 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004777 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004778 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004779 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004780 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004781 &md_suspend_lo.attr,
4782 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004783 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004784 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004785 NULL,
4786};
NeilBrown411036f2005-11-08 21:39:40 -08004787static struct attribute_group md_redundancy_group = {
4788 .name = NULL,
4789 .attrs = md_redundancy_attrs,
4790};
4791
NeilBrowneae17012005-11-08 21:39:23 -08004792static ssize_t
4793md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4794{
4795 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004796 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004797 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004798
4799 if (!entry->show)
4800 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004801 spin_lock(&all_mddevs_lock);
4802 if (list_empty(&mddev->all_mddevs)) {
4803 spin_unlock(&all_mddevs_lock);
4804 return -EBUSY;
4805 }
4806 mddev_get(mddev);
4807 spin_unlock(&all_mddevs_lock);
4808
NeilBrownb7b17c92014-12-15 12:56:59 +11004809 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004810 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004811 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004812}
4813
4814static ssize_t
4815md_attr_store(struct kobject *kobj, struct attribute *attr,
4816 const char *page, size_t length)
4817{
4818 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004819 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004820 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004821
4822 if (!entry->store)
4823 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004824 if (!capable(CAP_SYS_ADMIN))
4825 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004826 spin_lock(&all_mddevs_lock);
4827 if (list_empty(&mddev->all_mddevs)) {
4828 spin_unlock(&all_mddevs_lock);
4829 return -EBUSY;
4830 }
4831 mddev_get(mddev);
4832 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004833 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004834 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004835 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004836}
4837
4838static void md_free(struct kobject *ko)
4839{
NeilBrownfd01b882011-10-11 16:47:53 +11004840 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004841
4842 if (mddev->sysfs_state)
4843 sysfs_put(mddev->sysfs_state);
4844
NeilBrown6cd18e72015-04-27 14:12:22 +10004845 if (mddev->queue)
4846 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004847 if (mddev->gendisk) {
4848 del_gendisk(mddev->gendisk);
4849 put_disk(mddev->gendisk);
4850 }
NeilBrowna21d1502009-01-09 08:31:09 +11004851
NeilBrowneae17012005-11-08 21:39:23 -08004852 kfree(mddev);
4853}
4854
Emese Revfy52cf25d2010-01-19 02:58:23 +01004855static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004856 .show = md_attr_show,
4857 .store = md_attr_store,
4858};
4859static struct kobj_type md_ktype = {
4860 .release = md_free,
4861 .sysfs_ops = &md_sysfs_ops,
4862 .default_attrs = md_default_attrs,
4863};
4864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865int mdp_major = 0;
4866
Dan Williams5fd3a172009-03-04 00:57:25 -07004867static void mddev_delayed_delete(struct work_struct *ws)
4868{
NeilBrownfd01b882011-10-11 16:47:53 +11004869 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004870
NeilBrown43a70502009-12-14 12:49:55 +11004871 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004872 kobject_del(&mddev->kobj);
4873 kobject_put(&mddev->kobj);
4874}
4875
NeilBrownefeb53c2009-01-09 08:31:10 +11004876static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004878 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004879 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004881 int partitioned;
4882 int shift;
4883 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004884 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
4886 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004887 return -ENODEV;
4888
4889 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4890 shift = partitioned ? MdpMinorShift : 0;
4891 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
Tejun Heoe804ac72010-10-15 15:36:08 +02004893 /* wait for any previous instance of this device to be
4894 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004895 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004896 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004897
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004898 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004899 error = -EEXIST;
4900 if (mddev->gendisk)
4901 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004902
4903 if (name) {
4904 /* Need to ensure that 'name' is not a duplicate.
4905 */
NeilBrownfd01b882011-10-11 16:47:53 +11004906 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004907 spin_lock(&all_mddevs_lock);
4908
4909 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4910 if (mddev2->gendisk &&
4911 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4912 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004913 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004914 }
4915 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 }
NeilBrown8b765392009-01-09 08:31:08 +11004917
NeilBrown0909dc42009-07-01 12:27:21 +10004918 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004919 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004920 if (!mddev->queue)
4921 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004922 mddev->queue->queuedata = mddev;
4923
NeilBrown409c57f2009-03-31 14:39:39 +11004924 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004925 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 disk = alloc_disk(1 << shift);
4928 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004929 blk_cleanup_queue(mddev->queue);
4930 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004931 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004933 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004935 if (name)
4936 strcpy(disk->disk_name, name);
4937 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004939 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 disk->fops = &md_fops;
4942 disk->private_data = mddev;
4943 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004944 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004945 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004946 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004947 * remove it now.
4948 */
4949 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004951 /* As soon as we call add_disk(), another thread could get
4952 * through to md_open, so make sure it doesn't get too far
4953 */
4954 mutex_lock(&mddev->open_mutex);
4955 add_disk(disk);
4956
Tejun Heoed9e1982008-08-25 19:56:05 +09004957 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4958 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004959 if (error) {
4960 /* This isn't possible, but as kobject_init_and_add is marked
4961 * __must_check, we must do something with the result
4962 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004963 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4964 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004965 error = 0;
4966 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004967 if (mddev->kobj.sd &&
4968 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004969 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004970 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004971 abort:
4972 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004973 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004974 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004975 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004976 }
NeilBrownd3374822009-01-09 08:31:10 +11004977 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004978 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004979}
4980
4981static struct kobject *md_probe(dev_t dev, int *part, void *data)
4982{
4983 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 return NULL;
4985}
4986
NeilBrownefeb53c2009-01-09 08:31:10 +11004987static int add_named_array(const char *val, struct kernel_param *kp)
4988{
4989 /* val must be "md_*" where * is not all digits.
4990 * We allocate an array with a large free minor number, and
4991 * set the name to val. val must not already be an active name.
4992 */
4993 int len = strlen(val);
4994 char buf[DISK_NAME_LEN];
4995
4996 while (len && val[len-1] == '\n')
4997 len--;
4998 if (len >= DISK_NAME_LEN)
4999 return -E2BIG;
5000 strlcpy(buf, val, len+1);
5001 if (strncmp(buf, "md_", 3) != 0)
5002 return -EINVAL;
5003 return md_alloc(0, buf);
5004}
5005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006static void md_safemode_timeout(unsigned long data)
5007{
NeilBrownfd01b882011-10-11 16:47:53 +11005008 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
Neil Brown0fd62b82008-06-28 08:31:36 +10005010 if (!atomic_read(&mddev->writes_pending)) {
5011 mddev->safemode = 1;
5012 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005013 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 md_wakeup_thread(mddev->thread);
5016}
5017
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005018static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
NeilBrownfd01b882011-10-11 16:47:53 +11005020int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021{
NeilBrown2604b702006-01-06 00:20:36 -08005022 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005023 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005024 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
NeilBrowna757e642005-04-16 15:26:42 -07005026 if (list_empty(&mddev->disks))
5027 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 if (mddev->pers)
5031 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005032 /* Cannot run until previous stop completes properly */
5033 if (mddev->sysfs_active)
5034 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 /*
5037 * Analyze all RAID superblock(s)
5038 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005039 if (!mddev->raid_disks) {
5040 if (!mddev->persistent)
5041 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005042 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
NeilBrownd9d166c2006-01-06 00:20:51 -08005045 if (mddev->level != LEVEL_NONE)
5046 request_module("md-level-%d", mddev->level);
5047 else if (mddev->clevel[0])
5048 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049
5050 /*
5051 * Drop all container device buffers, from now on
5052 * the only valid external interface is through the md
5053 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005055 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005056 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 continue;
5058 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005059 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005060
5061 /* perform some consistency tests on the device.
5062 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005063 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005064 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005065 if (rdev->meta_bdev) {
5066 /* Nothing to check */;
5067 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005068 if (mddev->dev_sectors &&
5069 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005070 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005071 printk("md: %s: data overlaps metadata\n",
5072 mdname(mddev));
5073 return -EINVAL;
5074 }
5075 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005076 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005077 > rdev->data_offset) {
5078 printk("md: %s: metadata overlaps data\n",
5079 mdname(mddev));
5080 return -EINVAL;
5081 }
5082 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005083 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 }
5085
NeilBrowna167f662010-10-26 18:31:13 +11005086 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005087 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005088
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005090 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005091 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005093 if (mddev->level != LEVEL_NONE)
5094 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5095 mddev->level);
5096 else
5097 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5098 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 return -EINVAL;
5100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005102 if (mddev->level != pers->level) {
5103 mddev->level = pers->level;
5104 mddev->new_level = pers->level;
5105 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005106 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
NeilBrownf6705572006-03-27 01:18:11 -08005108 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005109 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005110 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005111 module_put(pers->owner);
5112 return -EINVAL;
5113 }
5114
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005115 if (pers->sync_request) {
5116 /* Warn if this is a potentially silly
5117 * configuration.
5118 */
5119 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005120 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005121 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005122
NeilBrowndafb20f2012-03-19 12:46:39 +11005123 rdev_for_each(rdev, mddev)
5124 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005125 if (rdev < rdev2 &&
5126 rdev->bdev->bd_contains ==
5127 rdev2->bdev->bd_contains) {
5128 printk(KERN_WARNING
5129 "%s: WARNING: %s appears to be"
5130 " on the same physical disk as"
5131 " %s.\n",
5132 mdname(mddev),
5133 bdevname(rdev->bdev,b),
5134 bdevname(rdev2->bdev,b2));
5135 warned = 1;
5136 }
5137 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005138
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005139 if (warned)
5140 printk(KERN_WARNING
5141 "True protection against single-disk"
5142 " failure might be compromised.\n");
5143 }
5144
NeilBrown657390d2005-08-26 18:34:16 -07005145 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005146 /* may be over-ridden by personality */
5147 mddev->resync_max_sectors = mddev->dev_sectors;
5148
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005149 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
NeilBrown0f9552b2009-12-30 12:08:50 +11005151 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005152 mddev->ro = 2; /* read-only, but switch on first write */
5153
NeilBrown36d091f2014-12-15 12:56:58 +11005154 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005155 if (err)
5156 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005157 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005158 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5159 " but 'external_size' not in effect?\n", __func__);
5160 printk(KERN_ERR
5161 "md: invalid array_size %llu > default size %llu\n",
5162 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005163 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005164 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005165 }
NeilBrown36d091f2014-12-15 12:56:58 +11005166 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005167 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005168 struct bitmap *bitmap;
5169
5170 bitmap = bitmap_create(mddev, -1);
5171 if (IS_ERR(bitmap)) {
5172 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005173 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5174 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005175 } else
5176 mddev->bitmap = bitmap;
5177
NeilBrownb15c2e52006-01-06 00:20:16 -08005178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005180 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005181 if (mddev->private)
5182 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005183 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005184 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005185 bitmap_destroy(mddev);
5186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 }
NeilBrown5c675f82014-12-15 12:56:56 +11005188 if (mddev->queue) {
5189 mddev->queue->backing_dev_info.congested_data = mddev;
5190 mddev->queue->backing_dev_info.congested_fn = md_congested;
NeilBrown64590f42014-12-15 12:56:57 +11005191 blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec);
NeilBrown5c675f82014-12-15 12:56:56 +11005192 }
NeilBrown36d091f2014-12-15 12:56:58 +11005193 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005194 if (mddev->kobj.sd &&
5195 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005196 printk(KERN_WARNING
5197 "md: cannot register extra attributes for %s\n",
5198 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005199 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005200 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005201 mddev->ro = 0;
5202
NeilBrownf72ffdd2014-09-30 14:23:59 +10005203 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005204 atomic_set(&mddev->max_corr_read_errors,
5205 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 mddev->safemode = 0;
5207 mddev->safemode_timer.function = md_safemode_timeout;
5208 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005209 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005211 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005212 spin_lock(&mddev->lock);
5213 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005214 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005215 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005216 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005217 if (rdev->raid_disk >= 0)
5218 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005219 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005220
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005222
NeilBrown7a0a5352013-08-27 16:28:23 +10005223 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005224 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
NeilBrownd7603b72006-01-06 00:20:30 -08005226 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005227 sysfs_notify_dirent_safe(mddev->sysfs_state);
5228 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005229 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 return 0;
5231}
NeilBrown390ee602010-06-01 19:37:27 +10005232EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
NeilBrownfd01b882011-10-11 16:47:53 +11005234static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005235{
5236 int err;
5237
5238 err = md_run(mddev);
5239 if (err)
5240 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005241 err = bitmap_load(mddev);
5242 if (err) {
5243 bitmap_destroy(mddev);
5244 goto out;
5245 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005246
5247 md_wakeup_thread(mddev->thread);
5248 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5249
NeilBrownfe60b012010-03-29 11:10:42 +11005250 set_capacity(mddev->gendisk, mddev->array_sectors);
5251 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005252 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005253 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5254out:
5255 return err;
5256}
5257
NeilBrownfd01b882011-10-11 16:47:53 +11005258static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259{
5260 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
Andre Noll80fab1d2008-07-11 22:02:21 +10005262 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005264 return -ENXIO;
5265 if (!mddev->pers)
5266 return -EINVAL;
5267 if (!mddev->ro)
5268 return -EBUSY;
5269 mddev->safemode = 0;
5270 mddev->ro = 0;
5271 set_disk_ro(disk, 0);
5272 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5273 mdname(mddev));
5274 /* Kick recovery or resync if necessary */
5275 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5276 md_wakeup_thread(mddev->thread);
5277 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005278 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280}
5281
NeilBrownfd01b882011-10-11 16:47:53 +11005282static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005283{
5284 mddev->array_sectors = 0;
5285 mddev->external_size = 0;
5286 mddev->dev_sectors = 0;
5287 mddev->raid_disks = 0;
5288 mddev->recovery_cp = 0;
5289 mddev->resync_min = 0;
5290 mddev->resync_max = MaxSector;
5291 mddev->reshape_position = MaxSector;
5292 mddev->external = 0;
5293 mddev->persistent = 0;
5294 mddev->level = LEVEL_NONE;
5295 mddev->clevel[0] = 0;
5296 mddev->flags = 0;
5297 mddev->ro = 0;
5298 mddev->metadata_type[0] = 0;
5299 mddev->chunk_sectors = 0;
5300 mddev->ctime = mddev->utime = 0;
5301 mddev->layout = 0;
5302 mddev->max_disks = 0;
5303 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005304 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005305 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005306 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005307 mddev->new_level = LEVEL_NONE;
5308 mddev->new_layout = 0;
5309 mddev->new_chunk_sectors = 0;
5310 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005311 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005312 mddev->suspend_lo = mddev->suspend_hi = 0;
5313 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5314 mddev->recovery = 0;
5315 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005316 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005317 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005318 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005319 mddev->private = NULL;
NeilBrown050b6612012-03-19 12:46:39 +11005320 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005321 mddev->bitmap_info.offset = 0;
5322 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005323 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005324 mddev->bitmap_info.chunksize = 0;
5325 mddev->bitmap_info.daemon_sleep = 0;
5326 mddev->bitmap_info.max_write_behind = 0;
5327}
5328
NeilBrownfd01b882011-10-11 16:47:53 +11005329static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005330{
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005331 if (mddev_is_clustered(mddev))
5332 md_cluster_ops->metadata_update_start(mddev);
NeilBrown6b6204e2013-05-09 09:48:30 +10005333 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005334 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005335 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005336 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005337 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005338 }
5339
5340 del_timer_sync(&mddev->safemode_timer);
5341
5342 bitmap_flush(mddev);
5343 md_super_wait(mddev);
5344
NeilBrownb6d428c2013-04-24 11:42:42 +10005345 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005346 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005347 /* mark array as shutdown cleanly */
5348 mddev->in_sync = 1;
5349 md_update_sb(mddev, 1);
5350 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005351 if (mddev_is_clustered(mddev))
5352 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005353}
NeilBrowndefad612011-01-14 09:14:33 +11005354
NeilBrownfd01b882011-10-11 16:47:53 +11005355void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005356{
NeilBrown29f097c2013-11-14 17:54:51 +11005357 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005358 __md_stop_writes(mddev);
5359 mddev_unlock(mddev);
5360}
NeilBrown390ee602010-06-01 19:37:27 +10005361EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005362
NeilBrown5aa61f42014-12-15 12:56:57 +11005363static void mddev_detach(struct mddev *mddev)
5364{
5365 struct bitmap *bitmap = mddev->bitmap;
5366 /* wait for behind writes to complete */
5367 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5368 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5369 mdname(mddev));
5370 /* need to kick something here to make sure I/O goes? */
5371 wait_event(bitmap->behind_wait,
5372 atomic_read(&bitmap->behind_writes) == 0);
5373 }
NeilBrown36d091f2014-12-15 12:56:58 +11005374 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005375 mddev->pers->quiesce(mddev, 1);
5376 mddev->pers->quiesce(mddev, 0);
5377 }
5378 md_unregister_thread(&mddev->thread);
5379 if (mddev->queue)
5380 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5381}
5382
NeilBrown5eff3c42012-11-19 10:47:48 +11005383static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005384{
NeilBrown36d091f2014-12-15 12:56:58 +11005385 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005386 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005387 spin_lock(&mddev->lock);
5388 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005389 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005390 spin_unlock(&mddev->lock);
5391 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005392 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005393 if (pers->sync_request && mddev->to_remove == NULL)
5394 mddev->to_remove = &md_redundancy_group;
5395 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005396 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005397}
NeilBrown5eff3c42012-11-19 10:47:48 +11005398
5399void md_stop(struct mddev *mddev)
5400{
5401 /* stop the array and free an attached data structures.
5402 * This is called from dm-raid
5403 */
5404 __md_stop(mddev);
5405 bitmap_destroy(mddev);
5406 if (mddev->bio_set)
5407 bioset_free(mddev->bio_set);
5408}
5409
NeilBrown390ee602010-06-01 19:37:27 +10005410EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005411
NeilBrowna05b7ea02012-07-19 15:59:18 +10005412static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005413{
5414 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005415 int did_freeze = 0;
5416
5417 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5418 did_freeze = 1;
5419 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5420 md_wakeup_thread(mddev->thread);
5421 }
NeilBrownf851b602014-12-11 10:02:10 +11005422 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005423 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005424 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005425 /* Thread might be blocked waiting for metadata update
5426 * which will now never happen */
5427 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005428
NeilBrown30b8feb2013-11-14 15:16:17 +11005429 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005430 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5431 &mddev->recovery));
NeilBrown30b8feb2013-11-14 15:16:17 +11005432 mddev_lock_nointr(mddev);
5433
NeilBrowna4bd82d2010-03-29 13:23:10 +11005434 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005435 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005436 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005437 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005438 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005439 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005440 if (did_freeze) {
5441 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005442 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005443 md_wakeup_thread(mddev->thread);
5444 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005445 err = -EBUSY;
5446 goto out;
5447 }
5448 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005449 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005450
5451 err = -ENXIO;
5452 if (mddev->ro==1)
5453 goto out;
5454 mddev->ro = 1;
5455 set_disk_ro(mddev->gendisk, 1);
5456 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005457 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5458 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005459 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005460 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005461 }
5462out:
5463 mutex_unlock(&mddev->open_mutex);
5464 return err;
5465}
5466
NeilBrown9e653b62006-06-26 00:27:58 -07005467/* mode:
5468 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005469 * 2 - stop but do not disassemble array
5470 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005471static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea02012-07-19 15:59:18 +10005472 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005475 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005476 int did_freeze = 0;
5477
5478 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5479 did_freeze = 1;
5480 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5481 md_wakeup_thread(mddev->thread);
5482 }
NeilBrownf851b602014-12-11 10:02:10 +11005483 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005484 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005485 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005486 /* Thread might be blocked waiting for metadata update
5487 * which will now never happen */
5488 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005489
NeilBrown30b8feb2013-11-14 15:16:17 +11005490 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005491 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5492 !test_bit(MD_RECOVERY_RUNNING,
5493 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005494 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
NeilBrownc8c00a62009-08-10 12:50:52 +10005496 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005497 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005498 mddev->sysfs_active ||
5499 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005500 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005501 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005502 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005503 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005504 if (did_freeze) {
5505 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005506 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005507 md_wakeup_thread(mddev->thread);
5508 }
NeilBrown260fa032013-08-27 16:44:13 +10005509 return -EBUSY;
5510 }
NeilBrown6e17b022010-08-07 21:41:19 +10005511 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005512 if (mddev->ro)
5513 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005514
NeilBrowndefad612011-01-14 09:14:33 +11005515 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005516 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005517 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005518 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005519
NeilBrowna4bd82d2010-03-29 13:23:10 +11005520 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005521 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005522
NeilBrowndafb20f2012-03-19 12:46:39 +11005523 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005524 if (rdev->raid_disk >= 0)
5525 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005526
NeilBrowna4bd82d2010-03-29 13:23:10 +11005527 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005528 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005529 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005530 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005531
NeilBrowna4bd82d2010-03-29 13:23:10 +11005532 if (mddev->ro)
5533 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005534 } else
5535 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 /*
5537 * Free resources if final stop
5538 */
NeilBrown9e653b62006-06-26 00:27:58 -07005539 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5541
NeilBrown978f9462006-02-02 14:28:05 -08005542 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005543 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005544 struct file *f = mddev->bitmap_info.file;
5545 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005546 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005547 spin_unlock(&mddev->lock);
5548 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005549 }
NeilBrownc3d97142009-12-14 12:49:52 +11005550 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005551
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 export_array(mddev);
5553
NeilBrown6177b472010-03-29 11:37:13 +11005554 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005555 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005556 if (mddev->hold_active == UNTIL_STOP)
5557 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005558 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005559 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005560 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005561 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563}
5564
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005565#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005566static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567{
NeilBrown3cb03002011-10-11 16:45:26 +11005568 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 int err;
5570
NeilBrowna757e642005-04-16 15:26:42 -07005571 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
5574 printk(KERN_INFO "md: running: ");
5575
NeilBrowndafb20f2012-03-19 12:46:39 +11005576 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 char b[BDEVNAME_SIZE];
5578 printk("<%s>", bdevname(rdev->bdev,b));
5579 }
5580 printk("\n");
5581
NeilBrownd710e132008-10-13 11:55:12 +11005582 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 if (err) {
5584 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005585 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 }
5587}
5588
5589/*
5590 * lets try to run arrays based on all disks that have arrived
5591 * until now. (those are in pending_raid_disks)
5592 *
5593 * the method: pick the first pending disk, collect all disks with
5594 * the same UUID, remove all from the pending list and put them into
5595 * the 'same_array' list. Then order this list based on superblock
5596 * update time (freshest comes first), kick out 'old' disks and
5597 * compare superblocks. If everything's fine then run it.
5598 *
5599 * If "unit" is allocated, then bump its reference count
5600 */
5601static void autorun_devices(int part)
5602{
NeilBrown3cb03002011-10-11 16:45:26 +11005603 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005604 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 char b[BDEVNAME_SIZE];
5606
5607 printk(KERN_INFO "md: autorun ...\n");
5608 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005609 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005611 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005613 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
5615 printk(KERN_INFO "md: considering %s ...\n",
5616 bdevname(rdev0->bdev,b));
5617 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005618 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 if (super_90_load(rdev, rdev0, 0) >= 0) {
5620 printk(KERN_INFO "md: adding %s ...\n",
5621 bdevname(rdev->bdev,b));
5622 list_move(&rdev->same_set, &candidates);
5623 }
5624 /*
5625 * now we have a set of devices, with all of them having
5626 * mostly sane superblocks. It's time to allocate the
5627 * mddev.
5628 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005629 if (part) {
5630 dev = MKDEV(mdp_major,
5631 rdev0->preferred_minor << MdpMinorShift);
5632 unit = MINOR(dev) >> MdpMinorShift;
5633 } else {
5634 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5635 unit = MINOR(dev);
5636 }
5637 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5639 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5640 break;
5641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
5643 md_probe(dev, NULL, NULL);
5644 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005645 if (!mddev || !mddev->gendisk) {
5646 if (mddev)
5647 mddev_put(mddev);
5648 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 "md: cannot allocate memory for md drive.\n");
5650 break;
5651 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005652 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 printk(KERN_WARNING "md: %s locked, cannot run\n",
5654 mdname(mddev));
5655 else if (mddev->raid_disks || mddev->major_version
5656 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005657 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 "md: %s already running, cannot run %s\n",
5659 mdname(mddev), bdevname(rdev0->bdev,b));
5660 mddev_unlock(mddev);
5661 } else {
5662 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005663 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005664 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 list_del_init(&rdev->same_set);
5666 if (bind_rdev_to_array(rdev, mddev))
5667 export_rdev(rdev);
5668 }
5669 autorun_array(mddev);
5670 mddev_unlock(mddev);
5671 }
5672 /* on success, candidates will be empty, on error
5673 * it won't...
5674 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005675 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005676 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 mddev_put(mddev);
5680 }
5681 printk(KERN_INFO "md: ... autorun DONE.\n");
5682}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005683#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
NeilBrownf72ffdd2014-09-30 14:23:59 +10005685static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686{
5687 mdu_version_t ver;
5688
5689 ver.major = MD_MAJOR_VERSION;
5690 ver.minor = MD_MINOR_VERSION;
5691 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5692
5693 if (copy_to_user(arg, &ver, sizeof(ver)))
5694 return -EFAULT;
5695
5696 return 0;
5697}
5698
NeilBrownf72ffdd2014-09-30 14:23:59 +10005699static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
5701 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005702 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005703 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
NeilBrown1ca69c42012-10-11 13:37:33 +11005705 nr = working = insync = failed = spare = 0;
5706 rcu_read_lock();
5707 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005709 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 failed++;
5711 else {
5712 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005713 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005714 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 else
5716 spare++;
5717 }
5718 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005719 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 info.major_version = mddev->major_version;
5722 info.minor_version = mddev->minor_version;
5723 info.patch_version = MD_PATCHLEVEL_VERSION;
5724 info.ctime = mddev->ctime;
5725 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005726 info.size = mddev->dev_sectors / 2;
5727 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005728 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 info.nr_disks = nr;
5730 info.raid_disks = mddev->raid_disks;
5731 info.md_minor = mddev->md_minor;
5732 info.not_persistent= !mddev->persistent;
5733
5734 info.utime = mddev->utime;
5735 info.state = 0;
5736 if (mddev->in_sync)
5737 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005738 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005739 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005740 if (mddev_is_clustered(mddev))
5741 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005742 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 info.working_disks = working;
5744 info.failed_disks = failed;
5745 info.spare_disks = spare;
5746
5747 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005748 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749
5750 if (copy_to_user(arg, &info, sizeof(info)))
5751 return -EFAULT;
5752
5753 return 0;
5754}
5755
NeilBrownf72ffdd2014-09-30 14:23:59 +10005756static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005757{
5758 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005759 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005760 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005761
NeilBrown60559da2013-07-16 16:44:29 +10005762 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005763 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005764 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005765
NeilBrown32a76272005-06-21 17:17:14 -07005766 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005767 spin_lock(&mddev->lock);
5768 /* bitmap disabled, zero the first byte and copy out */
5769 if (!mddev->bitmap_info.file)
5770 file->pathname[0] = '\0';
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02005771 else if ((ptr = file_path(mddev->bitmap_info.file,
NeilBrown4af1a042014-12-15 12:57:00 +11005772 file->pathname, sizeof(file->pathname))),
5773 IS_ERR(ptr))
5774 err = PTR_ERR(ptr);
5775 else
5776 memmove(file->pathname, ptr,
5777 sizeof(file->pathname)-(ptr-file->pathname));
5778 spin_unlock(&mddev->lock);
5779
5780 if (err == 0 &&
5781 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005782 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005783
NeilBrown32a76272005-06-21 17:17:14 -07005784 kfree(file);
5785 return err;
5786}
5787
NeilBrownf72ffdd2014-09-30 14:23:59 +10005788static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789{
5790 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005791 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
5793 if (copy_from_user(&info, arg, sizeof(info)))
5794 return -EFAULT;
5795
NeilBrown1ca69c42012-10-11 13:37:33 +11005796 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005797 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 if (rdev) {
5799 info.major = MAJOR(rdev->bdev->bd_dev);
5800 info.minor = MINOR(rdev->bdev->bd_dev);
5801 info.raid_disk = rdev->raid_disk;
5802 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005803 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005805 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 info.state |= (1<<MD_DISK_ACTIVE);
5807 info.state |= (1<<MD_DISK_SYNC);
5808 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005809 if (test_bit(WriteMostly, &rdev->flags))
5810 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 } else {
5812 info.major = info.minor = 0;
5813 info.raid_disk = -1;
5814 info.state = (1<<MD_DISK_REMOVED);
5815 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005816 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 if (copy_to_user(arg, &info, sizeof(info)))
5819 return -EFAULT;
5820
5821 return 0;
5822}
5823
NeilBrownf72ffdd2014-09-30 14:23:59 +10005824static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825{
5826 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005827 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 dev_t dev = MKDEV(info->major,info->minor);
5829
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005830 if (mddev_is_clustered(mddev) &&
5831 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005832 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005833 mdname(mddev));
5834 return -EINVAL;
5835 }
5836
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5838 return -EOVERFLOW;
5839
5840 if (!mddev->raid_disks) {
5841 int err;
5842 /* expecting a device which has a superblock */
5843 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5844 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005845 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 "md: md_import_device returned %ld\n",
5847 PTR_ERR(rdev));
5848 return PTR_ERR(rdev);
5849 }
5850 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005851 struct md_rdev *rdev0
5852 = list_entry(mddev->disks.next,
5853 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005854 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 .load_super(rdev, rdev0, mddev->minor_version);
5856 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005857 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005859 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 bdevname(rdev0->bdev,b2));
5861 export_rdev(rdev);
5862 return -EINVAL;
5863 }
5864 }
5865 err = bind_rdev_to_array(rdev, mddev);
5866 if (err)
5867 export_rdev(rdev);
5868 return err;
5869 }
5870
5871 /*
5872 * add_new_disk can be used once the array is assembled
5873 * to add "hot spares". They must already have a superblock
5874 * written
5875 */
5876 if (mddev->pers) {
5877 int err;
5878 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005879 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 "%s: personality does not support diskops!\n",
5881 mdname(mddev));
5882 return -EINVAL;
5883 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005884 if (mddev->persistent)
5885 rdev = md_import_device(dev, mddev->major_version,
5886 mddev->minor_version);
5887 else
5888 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005890 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 "md: md_import_device returned %ld\n",
5892 PTR_ERR(rdev));
5893 return PTR_ERR(rdev);
5894 }
NeilBrown1a855a02010-12-09 16:36:28 +11005895 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005896 if (!mddev->persistent) {
5897 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005898 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005899 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005900 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005901 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005902 } else
NeilBrown41158c72005-06-21 17:17:25 -07005903 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005904 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005905 } else
5906 super_types[mddev->major_version].
5907 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005908 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005909 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005910 /* This was a hot-add request, but events doesn't
5911 * match, so reject it.
5912 */
5913 export_rdev(rdev);
5914 return -EINVAL;
5915 }
5916
NeilBrownb2d444d2005-11-08 21:39:31 -08005917 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005918 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5919 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005920 else
5921 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005922
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005923 /*
5924 * check whether the device shows up in other nodes
5925 */
5926 if (mddev_is_clustered(mddev)) {
5927 if (info->state & (1 << MD_DISK_CANDIDATE)) {
5928 /* Through --cluster-confirm */
5929 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005930 err = md_cluster_ops->new_disk_ack(mddev, true);
5931 if (err) {
5932 export_rdev(rdev);
5933 return err;
5934 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005935 } else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
5936 /* --add initiated by this node */
5937 err = md_cluster_ops->add_new_disk_start(mddev, rdev);
5938 if (err) {
5939 md_cluster_ops->add_new_disk_finish(mddev);
5940 export_rdev(rdev);
5941 return err;
5942 }
5943 }
5944 }
5945
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 rdev->raid_disk = -1;
5947 err = bind_rdev_to_array(rdev, mddev);
5948 if (err)
5949 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005950 else
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05005951 err = add_bound_rdev(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005952 if (mddev_is_clustered(mddev) &&
5953 (info->state & (1 << MD_DISK_CLUSTER_ADD)))
5954 md_cluster_ops->add_new_disk_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 return err;
5956 }
5957
5958 /* otherwise, add_new_disk is only allowed
5959 * for major_version==0 superblocks
5960 */
5961 if (mddev->major_version != 0) {
5962 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5963 mdname(mddev));
5964 return -EINVAL;
5965 }
5966
5967 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5968 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005969 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005971 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 "md: error, md_import_device() returned %ld\n",
5973 PTR_ERR(rdev));
5974 return PTR_ERR(rdev);
5975 }
5976 rdev->desc_nr = info->number;
5977 if (info->raid_disk < mddev->raid_disks)
5978 rdev->raid_disk = info->raid_disk;
5979 else
5980 rdev->raid_disk = -1;
5981
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005983 if (info->state & (1<<MD_DISK_SYNC))
5984 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005986 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5987 set_bit(WriteMostly, &rdev->flags);
5988
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 if (!mddev->persistent) {
5990 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005991 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5992 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005993 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005994 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
NeilBrown2bf071b2006-01-06 00:20:55 -08005996 err = bind_rdev_to_array(rdev, mddev);
5997 if (err) {
5998 export_rdev(rdev);
5999 return err;
6000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 }
6002
6003 return 0;
6004}
6005
NeilBrownf72ffdd2014-09-30 14:23:59 +10006006static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007{
6008 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006009 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 rdev = find_rdev(mddev, dev);
6012 if (!rdev)
6013 return -ENXIO;
6014
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006015 if (mddev_is_clustered(mddev))
6016 md_cluster_ops->metadata_update_start(mddev);
6017
NeilBrown3ea8929d2013-04-24 11:42:41 +10006018 clear_bit(Blocked, &rdev->flags);
6019 remove_and_add_spares(mddev, rdev);
6020
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 if (rdev->raid_disk >= 0)
6022 goto busy;
6023
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006024 if (mddev_is_clustered(mddev))
6025 md_cluster_ops->remove_disk(mddev, rdev);
6026
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006027 md_kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07006028 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006029 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006031 if (mddev_is_clustered(mddev))
6032 md_cluster_ops->metadata_update_finish(mddev);
6033
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 return 0;
6035busy:
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006036 if (mddev_is_clustered(mddev))
6037 md_cluster_ops->metadata_update_cancel(mddev);
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006038 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 bdevname(rdev->bdev,b), mdname(mddev));
6040 return -EBUSY;
6041}
6042
NeilBrownf72ffdd2014-09-30 14:23:59 +10006043static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044{
6045 char b[BDEVNAME_SIZE];
6046 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006047 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
6049 if (!mddev->pers)
6050 return -ENODEV;
6051
6052 if (mddev->major_version != 0) {
6053 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6054 " version-0 superblocks.\n",
6055 mdname(mddev));
6056 return -EINVAL;
6057 }
6058 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006059 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 "%s: personality does not support diskops!\n",
6061 mdname(mddev));
6062 return -EINVAL;
6063 }
6064
NeilBrownd710e132008-10-13 11:55:12 +11006065 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006067 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 "md: error, md_import_device() returned %ld\n",
6069 PTR_ERR(rdev));
6070 return -EINVAL;
6071 }
6072
6073 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006074 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006076 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
NeilBrown8190e752009-06-18 08:48:58 +10006078 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
NeilBrownb2d444d2005-11-08 21:39:31 -08006080 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006081 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 "md: can not hot-add faulty %s disk to %s!\n",
6083 bdevname(rdev->bdev,b), mdname(mddev));
6084 err = -EINVAL;
6085 goto abort_export;
6086 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006087
6088 if (mddev_is_clustered(mddev))
6089 md_cluster_ops->metadata_update_start(mddev);
NeilBrownb2d444d2005-11-08 21:39:31 -08006090 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006092 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006093 err = bind_rdev_to_array(rdev, mddev);
6094 if (err)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006095 goto abort_clustered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
6097 /*
6098 * The rest should better be atomic, we can have disk failures
6099 * noticed in interrupt contexts ...
6100 */
6101
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 rdev->raid_disk = -1;
6103
NeilBrown850b2b42006-10-03 01:15:46 -07006104 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006106 if (mddev_is_clustered(mddev))
6107 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 /*
6109 * Kick recovery, maybe this spare has to be added to the
6110 * array immediately.
6111 */
6112 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6113 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006114 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 return 0;
6116
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006117abort_clustered:
6118 if (mddev_is_clustered(mddev))
6119 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120abort_export:
6121 export_rdev(rdev);
6122 return err;
6123}
6124
NeilBrownfd01b882011-10-11 16:47:53 +11006125static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006126{
NeilBrown035328c2014-04-09 12:25:40 +10006127 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006128
NeilBrown36fa3062005-09-09 16:23:45 -07006129 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006130 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006131 return -EBUSY;
6132 if (mddev->recovery || mddev->sync_thread)
6133 return -EBUSY;
6134 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006135 }
6136
NeilBrown36fa3062005-09-09 16:23:45 -07006137 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006138 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006139 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006140
NeilBrown1e594bb2014-12-15 12:57:00 +11006141 if (mddev->bitmap || mddev->bitmap_info.file)
6142 return -EEXIST; /* cannot add when bitmap is present */
6143 f = fget(fd);
6144
6145 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006146 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6147 mdname(mddev));
6148 return -EBADF;
6149 }
6150
NeilBrown1e594bb2014-12-15 12:57:00 +11006151 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006152 if (!S_ISREG(inode->i_mode)) {
6153 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6154 mdname(mddev));
6155 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006156 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006157 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6158 mdname(mddev));
6159 err = -EBADF;
6160 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006161 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6162 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006163 err = -EBUSY;
6164 }
6165 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006166 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006167 return err;
6168 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006169 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006170 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006171 } else if (mddev->bitmap == NULL)
6172 return -ENOENT; /* cannot remove what isn't there */
6173 err = 0;
6174 if (mddev->pers) {
6175 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006176 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006177 struct bitmap *bitmap;
6178
6179 bitmap = bitmap_create(mddev, -1);
6180 if (!IS_ERR(bitmap)) {
6181 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006182 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006183 } else
6184 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006185 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006186 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006187 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006188 fd = -1; /* make sure to put the file */
6189 }
NeilBrown36fa3062005-09-09 16:23:45 -07006190 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006191 }
6192 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006193 struct file *f = mddev->bitmap_info.file;
6194 if (f) {
6195 spin_lock(&mddev->lock);
6196 mddev->bitmap_info.file = NULL;
6197 spin_unlock(&mddev->lock);
6198 fput(f);
6199 }
NeilBrown36fa3062005-09-09 16:23:45 -07006200 }
6201
NeilBrown32a76272005-06-21 17:17:14 -07006202 return err;
6203}
6204
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205/*
6206 * set_array_info is used two different ways
6207 * The original usage is when creating a new array.
6208 * In this usage, raid_disks is > 0 and it together with
6209 * level, size, not_persistent,layout,chunksize determine the
6210 * shape of the array.
6211 * This will always create an array with a type-0.90.0 superblock.
6212 * The newer usage is when assembling an array.
6213 * In this case raid_disks will be 0, and the major_version field is
6214 * use to determine which style super-blocks are to be found on the devices.
6215 * The minor and patch _version numbers are also kept incase the
6216 * super_block handler wishes to interpret them.
6217 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006218static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219{
6220
6221 if (info->raid_disks == 0) {
6222 /* just setting version number for superblock loading */
6223 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006224 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 super_types[info->major_version].name == NULL) {
6226 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006227 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 "md: superblock version %d not known\n",
6229 info->major_version);
6230 return -EINVAL;
6231 }
6232 mddev->major_version = info->major_version;
6233 mddev->minor_version = info->minor_version;
6234 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006235 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006236 /* ensure mddev_put doesn't delete this now that there
6237 * is some minimal configuration.
6238 */
6239 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 return 0;
6241 }
6242 mddev->major_version = MD_MAJOR_VERSION;
6243 mddev->minor_version = MD_MINOR_VERSION;
6244 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6245 mddev->ctime = get_seconds();
6246
6247 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006248 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006249 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 mddev->raid_disks = info->raid_disks;
6251 /* don't set md_minor, it is determined by which /dev/md* was
6252 * openned
6253 */
6254 if (info->state & (1<<MD_SB_CLEAN))
6255 mddev->recovery_cp = MaxSector;
6256 else
6257 mddev->recovery_cp = 0;
6258 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006259 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
6261 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006262 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263
6264 mddev->max_disks = MD_SB_DISKS;
6265
NeilBrowne6910632008-02-06 01:39:51 -08006266 if (mddev->persistent)
6267 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006268 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
NeilBrownc3d97142009-12-14 12:49:52 +11006270 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006271 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006272 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006273
NeilBrownf6705572006-03-27 01:18:11 -08006274 mddev->reshape_position = MaxSector;
6275
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 /*
6277 * Generate a 128 bit UUID
6278 */
6279 get_random_bytes(mddev->uuid, 16);
6280
NeilBrownf6705572006-03-27 01:18:11 -08006281 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006282 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006283 mddev->new_layout = mddev->layout;
6284 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006285 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006286
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 return 0;
6288}
6289
NeilBrownfd01b882011-10-11 16:47:53 +11006290void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006291{
Dan Williamsb522adc2009-03-31 15:00:31 +11006292 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6293
6294 if (mddev->external_size)
6295 return;
6296
Dan Williams1f403622009-03-31 14:59:03 +11006297 mddev->array_sectors = array_sectors;
6298}
6299EXPORT_SYMBOL(md_set_array_sectors);
6300
NeilBrownfd01b882011-10-11 16:47:53 +11006301static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006302{
NeilBrown3cb03002011-10-11 16:45:26 +11006303 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006304 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006305 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006306
6307 if (mddev->pers->resize == NULL)
6308 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006309 /* The "num_sectors" is the number of sectors of each device that
6310 * is used. This can only make sense for arrays with redundancy.
6311 * linear and raid0 always use whatever space is available. We can only
6312 * consider changing this number if no resync or reconstruction is
6313 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006314 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006315 * of each device. If num_sectors is zero, we find the largest size
6316 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006317 */
NeilBrownf851b602014-12-11 10:02:10 +11006318 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6319 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006320 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006321 if (mddev->ro)
6322 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006323
NeilBrowndafb20f2012-03-19 12:46:39 +11006324 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006325 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006326
Andre Nolld71f9f82008-07-11 22:02:22 +10006327 if (fit && (num_sectors == 0 || num_sectors > avail))
6328 num_sectors = avail;
6329 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006330 return -ENOSPC;
6331 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006332 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006333 if (!rv)
6334 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006335 return rv;
6336}
6337
NeilBrownfd01b882011-10-11 16:47:53 +11006338static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006339{
6340 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006341 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006342 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006343 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006344 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006345 if (mddev->ro)
6346 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006347 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006348 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006349 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006350 if (mddev->sync_thread ||
6351 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6352 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006353 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006354
6355 rdev_for_each(rdev, mddev) {
6356 if (mddev->raid_disks < raid_disks &&
6357 rdev->data_offset < rdev->new_data_offset)
6358 return -EINVAL;
6359 if (mddev->raid_disks > raid_disks &&
6360 rdev->data_offset > rdev->new_data_offset)
6361 return -EINVAL;
6362 }
6363
NeilBrown63c70c42006-03-27 01:18:13 -08006364 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006365 if (mddev->delta_disks < 0)
6366 mddev->reshape_backwards = 1;
6367 else if (mddev->delta_disks > 0)
6368 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006369
6370 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006371 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006372 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006373 mddev->reshape_backwards = 0;
6374 }
NeilBrownda943b992006-01-06 00:20:54 -08006375 return rv;
6376}
6377
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378/*
6379 * update_array_info is used to change the configuration of an
6380 * on-line array.
6381 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6382 * fields in the info are checked against the array.
6383 * Any differences that cannot be handled will cause an error.
6384 * Normally, only one change can be managed at a time.
6385 */
NeilBrownfd01b882011-10-11 16:47:53 +11006386static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387{
6388 int rv = 0;
6389 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006390 int state = 0;
6391
6392 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006393 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006394 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
6396 if (mddev->major_version != info->major_version ||
6397 mddev->minor_version != info->minor_version ||
6398/* mddev->patch_version != info->patch_version || */
6399 mddev->ctime != info->ctime ||
6400 mddev->level != info->level ||
6401/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006402 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006403 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006404 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6405 ((state^info->state) & 0xfffffe00)
6406 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 return -EINVAL;
6408 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006409 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6410 cnt++;
6411 if (mddev->raid_disks != info->raid_disks)
6412 cnt++;
6413 if (mddev->layout != info->layout)
6414 cnt++;
6415 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6416 cnt++;
6417 if (cnt == 0)
6418 return 0;
6419 if (cnt > 1)
6420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
6422 if (mddev->layout != info->layout) {
6423 /* Change layout
6424 * we don't need to do anything at the md level, the
6425 * personality will take care of it all.
6426 */
NeilBrown50ac1682009-06-18 08:47:55 +10006427 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006429 else {
6430 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006431 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006432 if (rv)
6433 mddev->new_layout = mddev->layout;
6434 return rv;
6435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006437 if (mddev_is_clustered(mddev))
6438 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11006439 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006440 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441
NeilBrownda943b992006-01-06 00:20:54 -08006442 if (mddev->raid_disks != info->raid_disks)
6443 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444
NeilBrown36fa3062005-09-09 16:23:45 -07006445 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006446 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6447 rv = -EINVAL;
6448 goto err;
6449 }
6450 if (mddev->recovery || mddev->sync_thread) {
6451 rv = -EBUSY;
6452 goto err;
6453 }
NeilBrown36fa3062005-09-09 16:23:45 -07006454 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006455 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006456 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006457 if (mddev->bitmap) {
6458 rv = -EEXIST;
6459 goto err;
6460 }
6461 if (mddev->bitmap_info.default_offset == 0) {
6462 rv = -EINVAL;
6463 goto err;
6464 }
NeilBrownc3d97142009-12-14 12:49:52 +11006465 mddev->bitmap_info.offset =
6466 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006467 mddev->bitmap_info.space =
6468 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006469 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006470 bitmap = bitmap_create(mddev, -1);
6471 if (!IS_ERR(bitmap)) {
6472 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006473 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006474 } else
6475 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006476 if (rv)
6477 bitmap_destroy(mddev);
6478 mddev->pers->quiesce(mddev, 0);
6479 } else {
6480 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006481 if (!mddev->bitmap) {
6482 rv = -ENOENT;
6483 goto err;
6484 }
6485 if (mddev->bitmap->storage.file) {
6486 rv = -EINVAL;
6487 goto err;
6488 }
NeilBrown36fa3062005-09-09 16:23:45 -07006489 mddev->pers->quiesce(mddev, 1);
6490 bitmap_destroy(mddev);
6491 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006492 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006493 }
6494 }
NeilBrown850b2b42006-10-03 01:15:46 -07006495 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006496 if (mddev_is_clustered(mddev))
6497 md_cluster_ops->metadata_update_finish(mddev);
6498 return rv;
6499err:
6500 if (mddev_is_clustered(mddev))
6501 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 return rv;
6503}
6504
NeilBrownfd01b882011-10-11 16:47:53 +11006505static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506{
NeilBrown3cb03002011-10-11 16:45:26 +11006507 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006508 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509
6510 if (mddev->pers == NULL)
6511 return -ENODEV;
6512
NeilBrown1ca69c42012-10-11 13:37:33 +11006513 rcu_read_lock();
6514 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006516 err = -ENODEV;
6517 else {
6518 md_error(mddev, rdev);
6519 if (!test_bit(Faulty, &rdev->flags))
6520 err = -EBUSY;
6521 }
6522 rcu_read_unlock();
6523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524}
6525
Andre Noll2f9618c2008-04-25 18:57:58 +02006526/*
6527 * We have a problem here : there is no easy way to give a CHS
6528 * virtual geometry. We currently pretend that we have a 2 heads
6529 * 4 sectors (with a BIG number of cylinders...). This drives
6530 * dosfs just mad... ;-)
6531 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006532static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6533{
NeilBrownfd01b882011-10-11 16:47:53 +11006534 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006535
6536 geo->heads = 2;
6537 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006538 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006539 return 0;
6540}
6541
Nicolas Schichancb335f82014-01-15 16:58:52 +01006542static inline bool md_ioctl_valid(unsigned int cmd)
6543{
6544 switch (cmd) {
6545 case ADD_NEW_DISK:
6546 case BLKROSET:
6547 case GET_ARRAY_INFO:
6548 case GET_BITMAP_FILE:
6549 case GET_DISK_INFO:
6550 case HOT_ADD_DISK:
6551 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006552 case RAID_AUTORUN:
6553 case RAID_VERSION:
6554 case RESTART_ARRAY_RW:
6555 case RUN_ARRAY:
6556 case SET_ARRAY_INFO:
6557 case SET_BITMAP_FILE:
6558 case SET_DISK_FAULTY:
6559 case STOP_ARRAY:
6560 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006561 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006562 return true;
6563 default:
6564 return false;
6565 }
6566}
6567
Al Viroa39907f2008-03-02 10:31:15 -05006568static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 unsigned int cmd, unsigned long arg)
6570{
6571 int err = 0;
6572 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006573 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006574 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575
Nicolas Schichancb335f82014-01-15 16:58:52 +01006576 if (!md_ioctl_valid(cmd))
6577 return -ENOTTY;
6578
NeilBrown506c9e42011-12-23 10:17:26 +11006579 switch (cmd) {
6580 case RAID_VERSION:
6581 case GET_ARRAY_INFO:
6582 case GET_DISK_INFO:
6583 break;
6584 default:
6585 if (!capable(CAP_SYS_ADMIN))
6586 return -EACCES;
6587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588
6589 /*
6590 * Commands dealing with the RAID driver but not any
6591 * particular array:
6592 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006593 switch (cmd) {
6594 case RAID_VERSION:
6595 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006596 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006599 case RAID_AUTORUN:
6600 err = 0;
6601 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006604 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 }
6606
6607 /*
6608 * Commands creating/starting a new array:
6609 */
6610
Al Viroa39907f2008-03-02 10:31:15 -05006611 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
6613 if (!mddev) {
6614 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 }
6617
NeilBrown1ca69c42012-10-11 13:37:33 +11006618 /* Some actions do not requires the mutex */
6619 switch (cmd) {
6620 case GET_ARRAY_INFO:
6621 if (!mddev->raid_disks && !mddev->external)
6622 err = -ENODEV;
6623 else
6624 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006625 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006626
6627 case GET_DISK_INFO:
6628 if (!mddev->raid_disks && !mddev->external)
6629 err = -ENODEV;
6630 else
6631 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006632 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006633
6634 case SET_DISK_FAULTY:
6635 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006636 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006637
6638 case GET_BITMAP_FILE:
6639 err = get_bitmap_file(mddev, argp);
6640 goto out;
6641
NeilBrown1ca69c42012-10-11 13:37:33 +11006642 }
6643
NeilBrowna7a3f082012-12-11 13:35:54 +11006644 if (cmd == ADD_NEW_DISK)
6645 /* need to ensure md_delayed_delete() has completed */
6646 flush_workqueue(md_misc_wq);
6647
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006648 if (cmd == HOT_REMOVE_DISK)
6649 /* need to ensure recovery thread has run */
6650 wait_event_interruptible_timeout(mddev->sb_wait,
6651 !test_bit(MD_RECOVERY_NEEDED,
6652 &mddev->flags),
6653 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006654 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6655 /* Need to flush page cache, and ensure no-one else opens
6656 * and writes
6657 */
6658 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006659 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006660 mutex_unlock(&mddev->open_mutex);
6661 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006662 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006663 }
6664 set_bit(MD_STILL_CLOSED, &mddev->flags);
6665 mutex_unlock(&mddev->open_mutex);
6666 sync_blockdev(bdev);
6667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 err = mddev_lock(mddev);
6669 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006670 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6672 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006673 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 }
6675
NeilBrownc02c0ae2012-12-11 13:39:21 +11006676 if (cmd == SET_ARRAY_INFO) {
6677 mdu_array_info_t info;
6678 if (!arg)
6679 memset(&info, 0, sizeof(info));
6680 else if (copy_from_user(&info, argp, sizeof(info))) {
6681 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006682 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006683 }
6684 if (mddev->pers) {
6685 err = update_array_info(mddev, &info);
6686 if (err) {
6687 printk(KERN_WARNING "md: couldn't update"
6688 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006689 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006691 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006692 }
6693 if (!list_empty(&mddev->disks)) {
6694 printk(KERN_WARNING
6695 "md: array %s already has disks!\n",
6696 mdname(mddev));
6697 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006698 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006699 }
6700 if (mddev->raid_disks) {
6701 printk(KERN_WARNING
6702 "md: array %s already initialised!\n",
6703 mdname(mddev));
6704 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006705 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006706 }
6707 err = set_array_info(mddev, &info);
6708 if (err) {
6709 printk(KERN_WARNING "md: couldn't set"
6710 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006711 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006712 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006713 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 }
6715
6716 /*
6717 * Commands querying/configuring an existing array:
6718 */
NeilBrown32a76272005-06-21 17:17:14 -07006719 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006720 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006721 if ((!mddev->raid_disks && !mddev->external)
6722 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6723 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6724 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006726 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 }
6728
6729 /*
6730 * Commands even a read-only array can execute:
6731 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006732 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006733 case RESTART_ARRAY_RW:
6734 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006735 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006736
6737 case STOP_ARRAY:
6738 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006739 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006740
6741 case STOP_ARRAY_RO:
6742 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006743 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006744
NeilBrown3ea8929d2013-04-24 11:42:41 +10006745 case HOT_REMOVE_DISK:
6746 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006747 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006748
NeilBrown7ceb17e2013-04-24 11:42:42 +10006749 case ADD_NEW_DISK:
6750 /* We can support ADD_NEW_DISK on read-only arrays
6751 * on if we are re-adding a preexisting device.
6752 * So require mddev->pers and MD_DISK_SYNC.
6753 */
6754 if (mddev->pers) {
6755 mdu_disk_info_t info;
6756 if (copy_from_user(&info, argp, sizeof(info)))
6757 err = -EFAULT;
6758 else if (!(info.state & (1<<MD_DISK_SYNC)))
6759 /* Need to clear read-only for this */
6760 break;
6761 else
6762 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006763 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006764 }
6765 break;
6766
NeilBrownc02c0ae2012-12-11 13:39:21 +11006767 case BLKROSET:
6768 if (get_user(ro, (int __user *)(arg))) {
6769 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006770 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006771 }
6772 err = -EINVAL;
6773
6774 /* if the bdev is going readonly the value of mddev->ro
6775 * does not matter, no writes are coming
6776 */
6777 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006778 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006779
NeilBrownc02c0ae2012-12-11 13:39:21 +11006780 /* are we are already prepared for writes? */
6781 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006782 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006783
6784 /* transitioning to readauto need only happen for
6785 * arrays that call md_write_start
6786 */
6787 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006789 if (err == 0) {
6790 mddev->ro = 2;
6791 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006792 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006793 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006794 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 }
6796
6797 /*
6798 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006799 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 */
NeilBrown326eb172014-09-30 15:36:28 +10006801 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006802 if (mddev->ro == 2) {
6803 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006804 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006805 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006806 /* mddev_unlock will wake thread */
6807 /* If a device failed while we were read-only, we
6808 * need to make sure the metadata is updated now.
6809 */
6810 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6811 mddev_unlock(mddev);
6812 wait_event(mddev->sb_wait,
6813 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6814 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006815 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006816 }
NeilBrownf91de922005-11-08 21:39:36 -08006817 } else {
6818 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006819 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 }
6822
NeilBrownc02c0ae2012-12-11 13:39:21 +11006823 switch (cmd) {
6824 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006826 mdu_disk_info_t info;
6827 if (copy_from_user(&info, argp, sizeof(info)))
6828 err = -EFAULT;
6829 else
6830 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006831 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006834 case CLUSTERED_DISK_NACK:
6835 if (mddev_is_clustered(mddev))
6836 md_cluster_ops->new_disk_ack(mddev, false);
6837 else
6838 err = -EINVAL;
6839 goto unlock;
6840
NeilBrownc02c0ae2012-12-11 13:39:21 +11006841 case HOT_ADD_DISK:
6842 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006843 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844
NeilBrownc02c0ae2012-12-11 13:39:21 +11006845 case RUN_ARRAY:
6846 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006847 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
NeilBrownc02c0ae2012-12-11 13:39:21 +11006849 case SET_BITMAP_FILE:
6850 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006851 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006852
NeilBrownc02c0ae2012-12-11 13:39:21 +11006853 default:
6854 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006855 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 }
6857
NeilBrown3adc28d2014-09-30 15:46:41 +10006858unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006859 if (mddev->hold_active == UNTIL_IOCTL &&
6860 err != -EINVAL)
6861 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006863out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 return err;
6865}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006866#ifdef CONFIG_COMPAT
6867static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6868 unsigned int cmd, unsigned long arg)
6869{
6870 switch (cmd) {
6871 case HOT_REMOVE_DISK:
6872 case HOT_ADD_DISK:
6873 case SET_DISK_FAULTY:
6874 case SET_BITMAP_FILE:
6875 /* These take in integer arg, do not convert */
6876 break;
6877 default:
6878 arg = (unsigned long)compat_ptr(arg);
6879 break;
6880 }
6881
6882 return md_ioctl(bdev, mode, cmd, arg);
6883}
6884#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
Al Viroa39907f2008-03-02 10:31:15 -05006886static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887{
6888 /*
6889 * Succeed if we can lock the mddev, which confirms that
6890 * it isn't being stopped right now.
6891 */
NeilBrownfd01b882011-10-11 16:47:53 +11006892 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 int err;
6894
Yuanhan Liu0c098222012-05-22 13:55:32 +10006895 if (!mddev)
6896 return -ENODEV;
6897
NeilBrownd3374822009-01-09 08:31:10 +11006898 if (mddev->gendisk != bdev->bd_disk) {
6899 /* we are racing with mddev_put which is discarding this
6900 * bd_disk.
6901 */
6902 mddev_put(mddev);
6903 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006904 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006905 /* Then retry the open from the top */
6906 return -ERESTARTSYS;
6907 }
6908 BUG_ON(mddev != bdev->bd_disk->private_data);
6909
NeilBrownc8c00a62009-08-10 12:50:52 +10006910 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 goto out;
6912
6913 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006914 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006915 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006916 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006918 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 out:
6920 return err;
6921}
6922
Al Virodb2a1442013-05-05 21:52:57 -04006923static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006925 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006927 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006928 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006931
6932static int md_media_changed(struct gendisk *disk)
6933{
NeilBrownfd01b882011-10-11 16:47:53 +11006934 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006935
6936 return mddev->changed;
6937}
6938
6939static int md_revalidate(struct gendisk *disk)
6940{
NeilBrownfd01b882011-10-11 16:47:53 +11006941 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006942
6943 mddev->changed = 0;
6944 return 0;
6945}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006946static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947{
6948 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006949 .open = md_open,
6950 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006951 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006952#ifdef CONFIG_COMPAT
6953 .compat_ioctl = md_compat_ioctl,
6954#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006955 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006956 .media_changed = md_media_changed,
6957 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958};
6959
NeilBrownf72ffdd2014-09-30 14:23:59 +10006960static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961{
NeilBrown2b8bf342011-10-11 16:48:23 +11006962 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 /*
6965 * md_thread is a 'system-thread', it's priority should be very
6966 * high. We avoid resource deadlocks individually in each
6967 * raid personality. (RAID5 does preallocation) We also use RR and
6968 * the very same RT priority as kswapd, thus we will never get
6969 * into a priority inversion deadlock.
6970 *
6971 * we definitely have to have equal or higher priority than
6972 * bdflush, otherwise bdflush will deadlock if there are too
6973 * many dirty RAID5 blocks.
6974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975
NeilBrown6985c432005-10-19 21:23:47 -07006976 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006977 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
NeilBrown93588e22005-11-15 00:09:12 -08006979 /* We need to wait INTERRUPTIBLE so that
6980 * we don't add to the load-average.
6981 * That means we need to be sure no signals are
6982 * pending
6983 */
6984 if (signal_pending(current))
6985 flush_signals(current);
6986
6987 wait_event_interruptible_timeout
6988 (thread->wqueue,
6989 test_bit(THREAD_WAKEUP, &thread->flags)
6990 || kthread_should_stop(),
6991 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
NeilBrown6c987912011-01-14 09:13:53 +11006993 clear_bit(THREAD_WAKEUP, &thread->flags);
6994 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006995 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006997
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 return 0;
6999}
7000
NeilBrown2b8bf342011-10-11 16:48:23 +11007001void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002{
7003 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007004 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 set_bit(THREAD_WAKEUP, &thread->flags);
7006 wake_up(&thread->wqueue);
7007 }
7008}
NeilBrown6c144d32014-09-30 16:15:38 +10007009EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010
Shaohua Li4ed87312012-10-11 13:34:00 +11007011struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7012 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013{
NeilBrown2b8bf342011-10-11 16:48:23 +11007014 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015
NeilBrown2b8bf342011-10-11 16:48:23 +11007016 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 if (!thread)
7018 return NULL;
7019
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 init_waitqueue_head(&thread->wqueue);
7021
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 thread->run = run;
7023 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007024 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007025 thread->tsk = kthread_run(md_thread, thread,
7026 "%s_%s",
7027 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007028 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007029 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 kfree(thread);
7031 return NULL;
7032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 return thread;
7034}
NeilBrown6c144d32014-09-30 16:15:38 +10007035EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
NeilBrown2b8bf342011-10-11 16:48:23 +11007037void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038{
NeilBrown2b8bf342011-10-11 16:48:23 +11007039 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007040 if (!thread)
7041 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007042 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007043 /* Locking ensures that mddev_unlock does not wake_up a
7044 * non-existent thread
7045 */
7046 spin_lock(&pers_lock);
7047 *threadp = NULL;
7048 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007049
7050 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 kfree(thread);
7052}
NeilBrown6c144d32014-09-30 16:15:38 +10007053EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054
NeilBrownfd01b882011-10-11 16:47:53 +11007055void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056{
NeilBrownb2d444d2005-11-08 21:39:31 -08007057 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007059
NeilBrownde393cd2011-07-28 11:31:48 +10007060 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 return;
7062 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007063 if (mddev->degraded)
7064 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007065 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7067 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7068 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007069 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007070 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07007071 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072}
NeilBrown6c144d32014-09-30 16:15:38 +10007073EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074
7075/* seq_file implementation /proc/mdstat */
7076
7077static void status_unused(struct seq_file *seq)
7078{
7079 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007080 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081
7082 seq_printf(seq, "unused devices: ");
7083
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007084 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 char b[BDEVNAME_SIZE];
7086 i++;
7087 seq_printf(seq, "%s ",
7088 bdevname(rdev->bdev,b));
7089 }
7090 if (!i)
7091 seq_printf(seq, "<none>");
7092
7093 seq_printf(seq, "\n");
7094}
7095
NeilBrownf72ffdd2014-09-30 14:23:59 +10007096static void status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097{
NeilBrowndd71cf62009-05-07 12:49:35 +10007098 sector_t max_sectors, resync, res;
7099 unsigned long dt, db;
7100 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007101 int scale;
7102 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103
NeilBrown72f36d52012-10-11 14:25:57 +11007104 if (mddev->curr_resync <= 3)
7105 resync = 0;
7106 else
7107 resync = mddev->curr_resync
7108 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109
NeilBrownc804cde2012-05-21 09:28:33 +10007110 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7111 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007112 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007114 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
NeilBrown403df472014-09-30 15:52:29 +10007116 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007117 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007118 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007119 * u32, as those are the requirements for sector_div.
7120 * Thus 'scale' must be at least 10
7121 */
7122 scale = 10;
7123 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007124 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007125 scale++;
7126 }
7127 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007128 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007129
7130 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 {
NeilBrown4588b422006-03-27 01:18:04 -08007132 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 seq_printf(seq, "[");
7134 for (i = 0; i < x; i++)
7135 seq_printf(seq, "=");
7136 seq_printf(seq, ">");
7137 for (i = 0; i < y; i++)
7138 seq_printf(seq, ".");
7139 seq_printf(seq, "] ");
7140 }
NeilBrown4588b422006-03-27 01:18:04 -08007141 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007142 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7143 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007144 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7145 "check" :
7146 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7147 "resync" : "recovery"))),
7148 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007149 (unsigned long long) resync/2,
7150 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
7152 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 * dt: time from mark until now
7154 * db: blocks written from mark until now
7155 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007156 *
7157 * rt is a sector_t, so could be 32bit or 64bit.
7158 * So we divide before multiply in case it is 32bit and close
7159 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007160 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007161 * near the end of resync when the number of remaining sectors
7162 * is close to 'db'.
7163 * We then divide rt by 32 after multiplying by db to compensate.
7164 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 */
7166 dt = ((jiffies - mddev->resync_mark) / HZ);
7167 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007168 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7169 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
NeilBrowndd71cf62009-05-07 12:49:35 +10007171 rt = max_sectors - resync; /* number of remaining sectors */
7172 sector_div(rt, db/32+1);
7173 rt *= dt;
7174 rt >>= 5;
7175
7176 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7177 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178
NeilBrownff4e8d92006-07-10 04:44:16 -07007179 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180}
7181
7182static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7183{
7184 struct list_head *tmp;
7185 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007186 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
7188 if (l >= 0x10000)
7189 return NULL;
7190 if (!l--)
7191 /* header */
7192 return (void*)1;
7193
7194 spin_lock(&all_mddevs_lock);
7195 list_for_each(tmp,&all_mddevs)
7196 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007197 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 mddev_get(mddev);
7199 spin_unlock(&all_mddevs_lock);
7200 return mddev;
7201 }
7202 spin_unlock(&all_mddevs_lock);
7203 if (!l--)
7204 return (void*)2;/* tail */
7205 return NULL;
7206}
7207
7208static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7209{
7210 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007211 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007212
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 ++*pos;
7214 if (v == (void*)2)
7215 return NULL;
7216
7217 spin_lock(&all_mddevs_lock);
7218 if (v == (void*)1)
7219 tmp = all_mddevs.next;
7220 else
7221 tmp = mddev->all_mddevs.next;
7222 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007223 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 else {
7225 next_mddev = (void*)2;
7226 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 spin_unlock(&all_mddevs_lock);
7229
7230 if (v != (void*)1)
7231 mddev_put(mddev);
7232 return next_mddev;
7233
7234}
7235
7236static void md_seq_stop(struct seq_file *seq, void *v)
7237{
NeilBrownfd01b882011-10-11 16:47:53 +11007238 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
7240 if (mddev && v != (void*)1 && v != (void*)2)
7241 mddev_put(mddev);
7242}
7243
7244static int md_seq_show(struct seq_file *seq, void *v)
7245{
NeilBrownfd01b882011-10-11 16:47:53 +11007246 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007247 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007248 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
7250 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007251 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 seq_printf(seq, "Personalities : ");
7253 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007254 list_for_each_entry(pers, &pers_list, list)
7255 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256
7257 spin_unlock(&pers_lock);
7258 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007259 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 return 0;
7261 }
7262 if (v == (void*)2) {
7263 status_unused(seq);
7264 return 0;
7265 }
7266
NeilBrown36d091f2014-12-15 12:56:58 +11007267 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7269 seq_printf(seq, "%s : %sactive", mdname(mddev),
7270 mddev->pers ? "" : "in");
7271 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007272 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007274 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007275 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 seq_printf(seq, " %s", mddev->pers->name);
7277 }
7278
Andre Nolldd8ac332009-03-31 14:33:13 +11007279 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007280 rcu_read_lock();
7281 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 char b[BDEVNAME_SIZE];
7283 seq_printf(seq, " %s[%d]",
7284 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007285 if (test_bit(WriteMostly, &rdev->flags))
7286 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007287 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 seq_printf(seq, "(F)");
7289 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007290 }
7291 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007292 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007293 if (test_bit(Replacement, &rdev->flags))
7294 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007295 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007297 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298
7299 if (!list_empty(&mddev->disks)) {
7300 if (mddev->pers)
7301 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007302 (unsigned long long)
7303 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 else
7305 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007306 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007308 if (mddev->persistent) {
7309 if (mddev->major_version != 0 ||
7310 mddev->minor_version != 90) {
7311 seq_printf(seq," super %d.%d",
7312 mddev->major_version,
7313 mddev->minor_version);
7314 }
NeilBrowne6910632008-02-06 01:39:51 -08007315 } else if (mddev->external)
7316 seq_printf(seq, " super external:%s",
7317 mddev->metadata_type);
7318 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007319 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320
7321 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007322 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007323 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007324 if (mddev->pers->sync_request) {
7325 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007326 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007327 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007328 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007329 seq_printf(seq, "\tresync=DELAYED\n ");
7330 else if (mddev->recovery_cp < MaxSector)
7331 seq_printf(seq, "\tresync=PENDING\n ");
7332 }
NeilBrown32a76272005-06-21 17:17:14 -07007333 } else
7334 seq_printf(seq, "\n ");
7335
NeilBrown57148962012-03-19 12:46:40 +11007336 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337
7338 seq_printf(seq, "\n");
7339 }
NeilBrown36d091f2014-12-15 12:56:58 +11007340 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007341
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 return 0;
7343}
7344
Jan Engelhardt110518b2009-05-07 12:49:37 +10007345static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 .start = md_seq_start,
7347 .next = md_seq_next,
7348 .stop = md_seq_stop,
7349 .show = md_seq_show,
7350};
7351
7352static int md_seq_open(struct inode *inode, struct file *file)
7353{
Kay Sieversf1514632011-07-12 20:48:39 +02007354 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 int error;
7356
7357 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007358 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007359 return error;
7360
7361 seq = file->private_data;
7362 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363 return error;
7364}
7365
NeilBrowne2f23b62014-04-09 14:33:51 +10007366static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007367static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7368{
Kay Sieversf1514632011-07-12 20:48:39 +02007369 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007370 int mask;
7371
NeilBrowne2f23b62014-04-09 14:33:51 +10007372 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007373 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007374 poll_wait(filp, &md_event_waiters, wait);
7375
7376 /* always allow read */
7377 mask = POLLIN | POLLRDNORM;
7378
Kay Sieversf1514632011-07-12 20:48:39 +02007379 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007380 mask |= POLLERR | POLLPRI;
7381 return mask;
7382}
7383
Arjan van de Venfa027c22007-02-12 00:55:33 -08007384static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007385 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 .open = md_seq_open,
7387 .read = seq_read,
7388 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007389 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007390 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391};
7392
NeilBrown84fc4b52011-10-11 16:49:58 +11007393int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394{
Chao Yu50bd3772014-09-25 15:28:34 +08007395 printk(KERN_INFO "md: %s personality registered for level %d\n",
7396 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007398 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 spin_unlock(&pers_lock);
7400 return 0;
7401}
NeilBrown6c144d32014-09-30 16:15:38 +10007402EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403
NeilBrown84fc4b52011-10-11 16:49:58 +11007404int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405{
NeilBrown2604b702006-01-06 00:20:36 -08007406 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007408 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 spin_unlock(&pers_lock);
7410 return 0;
7411}
NeilBrown6c144d32014-09-30 16:15:38 +10007412EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007414int register_md_cluster_operations(struct md_cluster_operations *ops, struct module *module)
7415{
7416 if (md_cluster_ops != NULL)
7417 return -EALREADY;
7418 spin_lock(&pers_lock);
7419 md_cluster_ops = ops;
7420 md_cluster_mod = module;
7421 spin_unlock(&pers_lock);
7422 return 0;
7423}
7424EXPORT_SYMBOL(register_md_cluster_operations);
7425
7426int unregister_md_cluster_operations(void)
7427{
7428 spin_lock(&pers_lock);
7429 md_cluster_ops = NULL;
7430 spin_unlock(&pers_lock);
7431 return 0;
7432}
7433EXPORT_SYMBOL(unregister_md_cluster_operations);
7434
7435int md_setup_cluster(struct mddev *mddev, int nodes)
7436{
7437 int err;
7438
7439 err = request_module("md-cluster");
7440 if (err) {
7441 pr_err("md-cluster module not found.\n");
7442 return err;
7443 }
7444
7445 spin_lock(&pers_lock);
7446 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7447 spin_unlock(&pers_lock);
7448 return -ENOENT;
7449 }
7450 spin_unlock(&pers_lock);
7451
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007452 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007453}
7454
7455void md_cluster_stop(struct mddev *mddev)
7456{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007457 if (!md_cluster_ops)
7458 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007459 md_cluster_ops->leave(mddev);
7460 module_put(md_cluster_mod);
7461}
7462
NeilBrownfd01b882011-10-11 16:47:53 +11007463static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007465 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007467 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468
7469 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007470 rcu_read_lock();
7471 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007473 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7474 (int)part_stat_read(&disk->part0, sectors[1]) -
7475 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007476 /* sync IO will cause sync_io to increase before the disk_stats
7477 * as sync_io is counted when a request starts, and
7478 * disk_stats is counted when it completes.
7479 * So resync activity will cause curr_events to be smaller than
7480 * when there was no such activity.
7481 * non-sync IO will cause disk_stat to increase without
7482 * increasing sync_io so curr_events will (eventually)
7483 * be larger than it was before. Once it becomes
7484 * substantially larger, the test below will cause
7485 * the array to appear non-idle, and resync will slow
7486 * down.
7487 * If there is a lot of outstanding resync activity when
7488 * we set last_event to curr_events, then all that activity
7489 * completing might cause the array to appear non-idle
7490 * and resync will be slowed down even though there might
7491 * not have been non-resync activity. This will only
7492 * happen once though. 'last_events' will soon reflect
7493 * the state where there is little or no outstanding
7494 * resync requests, and further resync activity will
7495 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007496 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007498 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 rdev->last_events = curr_events;
7500 idle = 0;
7501 }
7502 }
NeilBrown4b809912008-07-21 17:05:25 +10007503 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 return idle;
7505}
7506
NeilBrownfd01b882011-10-11 16:47:53 +11007507void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508{
7509 /* another "blocks" (512byte) blocks have been synced */
7510 atomic_sub(blocks, &mddev->recovery_active);
7511 wake_up(&mddev->recovery_wait);
7512 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007513 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007514 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 md_wakeup_thread(mddev->thread);
7516 // stop recovery, signal do_sync ....
7517 }
7518}
NeilBrown6c144d32014-09-30 16:15:38 +10007519EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520
NeilBrown06d91a52005-06-21 17:17:12 -07007521/* md_write_start(mddev, bi)
7522 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007523 * in superblock) before writing, schedule a superblock update
7524 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007525 */
NeilBrownfd01b882011-10-11 16:47:53 +11007526void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527{
Neil Brown0fd62b82008-06-28 08:31:36 +10007528 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007529 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007530 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007531
NeilBrownf91de922005-11-08 21:39:36 -08007532 BUG_ON(mddev->ro == 1);
7533 if (mddev->ro == 2) {
7534 /* need to switch to read/write */
7535 mddev->ro = 0;
7536 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7537 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007538 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007539 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007540 }
NeilBrown06d91a52005-06-21 17:17:12 -07007541 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007542 if (mddev->safemode == 1)
7543 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007544 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007545 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007546 if (mddev->in_sync) {
7547 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007548 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007549 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007550 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007551 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007552 }
NeilBrown85572d72014-12-15 12:56:56 +11007553 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007554 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007555 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007556 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007557 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007558 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559}
NeilBrown6c144d32014-09-30 16:15:38 +10007560EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561
NeilBrownfd01b882011-10-11 16:47:53 +11007562void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563{
7564 if (atomic_dec_and_test(&mddev->writes_pending)) {
7565 if (mddev->safemode == 2)
7566 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007567 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7569 }
7570}
NeilBrown6c144d32014-09-30 16:15:38 +10007571EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572
NeilBrown2a2275d2007-01-26 00:57:11 -08007573/* md_allow_write(mddev)
7574 * Calling this ensures that the array is marked 'active' so that writes
7575 * may proceed without blocking. It is important to call this before
7576 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7577 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007578 *
7579 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7580 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007581 */
NeilBrownfd01b882011-10-11 16:47:53 +11007582int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007583{
7584 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007585 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007586 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007587 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007588 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007589 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007590
NeilBrown85572d72014-12-15 12:56:56 +11007591 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007592 if (mddev->in_sync) {
7593 mddev->in_sync = 0;
7594 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007595 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007596 if (mddev->safemode_delay &&
7597 mddev->safemode == 0)
7598 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007599 spin_unlock(&mddev->lock);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007600 if (mddev_is_clustered(mddev))
7601 md_cluster_ops->metadata_update_start(mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08007602 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007603 if (mddev_is_clustered(mddev))
7604 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007605 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007606 } else
NeilBrown85572d72014-12-15 12:56:56 +11007607 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007608
NeilBrown070dc6d2010-08-30 17:33:34 +10007609 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007610 return -EAGAIN;
7611 else
7612 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007613}
7614EXPORT_SYMBOL_GPL(md_allow_write);
7615
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616#define SYNC_MARKS 10
7617#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007618#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007619void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620{
Shaohua Li4ed87312012-10-11 13:34:00 +11007621 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007622 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 unsigned int currspeed = 0,
7624 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007625 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007627 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 sector_t mark_cnt[SYNC_MARKS];
7629 int last_mark,m;
7630 struct list_head *tmp;
7631 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007632 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007633 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007634 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007635 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636
7637 /* just incase thread restarts... */
7638 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7639 return;
NeilBrown3991b312014-05-28 13:39:23 +10007640 if (mddev->ro) {/* never try to sync a read-only array */
7641 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007642 return;
NeilBrown3991b312014-05-28 13:39:23 +10007643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644
NeilBrown61df9d92006-10-03 01:15:57 -07007645 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007646 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007647 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007648 action = "check";
7649 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007650 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007651 action = "repair";
7652 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007653 desc = "resync";
7654 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7655 desc = "reshape";
7656 else
7657 desc = "recovery";
7658
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007659 mddev->last_sync_action = action ?: desc;
7660
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 /* we overload curr_resync somewhat here.
7662 * 0 == not engaged in resync at all
7663 * 2 == checking that there is no conflict with another sync
7664 * 1 == like 2, but have yielded to allow conflicting resync to
7665 * commense
7666 * other == active in resync - this many blocks
7667 *
7668 * Before starting a resync we must have set curr_resync to
7669 * 2, and then checked that every "conflicting" array has curr_resync
7670 * less than ours. When we find one that is the same or higher
7671 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7672 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7673 * This will mean we have to start checking from the beginning again.
7674 *
7675 */
7676
7677 do {
7678 mddev->curr_resync = 2;
7679
7680 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007681 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007683 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684 if (mddev2 == mddev)
7685 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007686 if (!mddev->parallel_resync
7687 && mddev2->curr_resync
7688 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 DEFINE_WAIT(wq);
7690 if (mddev < mddev2 && mddev->curr_resync == 2) {
7691 /* arbitrarily yield */
7692 mddev->curr_resync = 1;
7693 wake_up(&resync_wait);
7694 }
7695 if (mddev > mddev2 && mddev->curr_resync == 1)
7696 /* no need to wait here, we can wait the next
7697 * time 'round when curr_resync == 2
7698 */
7699 continue;
NeilBrown97441972008-09-19 11:49:54 +10007700 /* We need to wait 'interruptible' so as not to
7701 * contribute to the load average, and not to
7702 * be caught by 'softlockup'
7703 */
7704 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007705 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007706 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007707 printk(KERN_INFO "md: delaying %s of %s"
7708 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007710 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007712 if (signal_pending(current))
7713 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714 schedule();
7715 finish_wait(&resync_wait, &wq);
7716 goto try_again;
7717 }
7718 finish_wait(&resync_wait, &wq);
7719 }
7720 }
7721 } while (mddev->curr_resync < 2);
7722
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007723 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007724 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007726 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 */
7728 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007729 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007730 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007731 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7732 j = mddev->resync_min;
7733 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007734 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007735
NeilBrownccfcc3c2006-03-27 01:18:09 -08007736 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007737 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007738 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007740 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007741 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007742 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007743 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007744 if (rdev->raid_disk >= 0 &&
7745 !test_bit(Faulty, &rdev->flags) &&
7746 !test_bit(In_sync, &rdev->flags) &&
7747 rdev->recovery_offset < j)
7748 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007749 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007750
7751 /* If there is a bitmap, we need to make sure all
7752 * writes that started before we added a spare
7753 * complete before we start doing a recovery.
7754 * Otherwise the write might complete and (via
7755 * bitmap_endwrite) set a bit in the bitmap after the
7756 * recovery has checked that bit and skipped that
7757 * region.
7758 */
7759 if (mddev->bitmap) {
7760 mddev->pers->quiesce(mddev, 1);
7761 mddev->pers->quiesce(mddev, 0);
7762 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764
NeilBrown61df9d92006-10-03 01:15:57 -07007765 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7766 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7767 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007768 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007769 "(but not more than %d KB/sec) for %s.\n",
7770 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771
NeilBrowneea1bf32009-03-31 14:27:02 +11007772 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007773
NeilBrown57afd892005-06-21 17:17:13 -07007774 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 for (m = 0; m < SYNC_MARKS; m++) {
7776 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007777 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007778 }
7779 last_mark = 0;
7780 mddev->resync_mark = mark[last_mark];
7781 mddev->resync_mark_cnt = mark_cnt[last_mark];
7782
7783 /*
7784 * Tune reconstruction:
7785 */
7786 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007787 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7788 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789
7790 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 last_check = 0;
7792
7793 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007794 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007795 "md: resuming %s of %s from checkpoint.\n",
7796 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007798 } else
7799 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007800 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007801 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7802 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007803 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007805 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007806 md_cluster_ops->resync_start(mddev, j, max_sectors);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007807
majianpeng7c2c57c2012-07-03 12:12:26 +10007808 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007810 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811
NeilBrown57afd892005-06-21 17:17:13 -07007812 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007813
NeilBrown7a91ee12009-05-26 12:57:21 +10007814 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7815 ((mddev->curr_resync > mddev->curr_resync_completed &&
7816 (mddev->curr_resync - mddev->curr_resync_completed)
7817 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007818 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007819 (j - mddev->curr_resync_completed)*2
7820 >= mddev->resync_max - mddev->curr_resync_completed
7821 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007822 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007823 wait_event(mddev->recovery_wait,
7824 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007825 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007826 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7827 j > mddev->recovery_cp)
7828 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007829 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007830 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007831 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007832 }
NeilBrownacb180b2009-04-14 16:28:34 +10007833
NeilBrownc91abf52013-11-19 12:02:01 +11007834 while (j >= mddev->resync_max &&
7835 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007836 /* As this condition is controlled by user-space,
7837 * we can block indefinitely, so use '_interruptible'
7838 * to avoid triggering warnings.
7839 */
7840 flush_signals(current); /* just in case */
7841 wait_event_interruptible(mddev->recovery_wait,
7842 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007843 || test_bit(MD_RECOVERY_INTR,
7844 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007845 }
NeilBrownacb180b2009-04-14 16:28:34 +10007846
NeilBrownc91abf52013-11-19 12:02:01 +11007847 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7848 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007849
NeilBrown09314792015-02-19 16:04:40 +11007850 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07007851 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007852 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007853 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 }
NeilBrown57afd892005-06-21 17:17:13 -07007855
7856 if (!skipped) { /* actual IO requested */
7857 io_sectors += sectors;
7858 atomic_add(sectors, &mddev->recovery_active);
7859 }
7860
NeilBrowne875ece2011-07-28 11:39:24 +10007861 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7862 break;
7863
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007865 if (j > 2)
7866 mddev->curr_resync = j;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007867 if (mddev_is_clustered(mddev))
7868 md_cluster_ops->resync_info_update(mddev, j, max_sectors);
NeilBrownff4e8d92006-07-10 04:44:16 -07007869 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007870 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007871 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007872 * visible in /proc/mdstat
7873 */
7874 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007875
7876 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 continue;
7878
NeilBrown57afd892005-06-21 17:17:13 -07007879 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880 repeat:
7881 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7882 /* step marks */
7883 int next = (last_mark+1) % SYNC_MARKS;
7884
7885 mddev->resync_mark = mark[next];
7886 mddev->resync_mark_cnt = mark_cnt[next];
7887 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007888 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 last_mark = next;
7890 }
7891
NeilBrownc91abf52013-11-19 12:02:01 +11007892 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
7895 /*
7896 * this loop exits only if either when we are slower than
7897 * the 'hard' speed limit, or the system was IO-idle for
7898 * a jiffy.
7899 * the system might be non-idle CPU-wise, but we only care
7900 * about not overloading the IO subsystem. (things like an
7901 * e2fsck being done on the RAID array should execute fast)
7902 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 cond_resched();
7904
Xiao Niac7e50a2014-08-07 09:37:41 -04007905 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7906 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007907 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908
NeilBrown88202a02006-01-06 00:21:36 -08007909 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11007910 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007911 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 goto repeat;
7913 }
NeilBrownac8fa412015-02-19 16:55:00 +11007914 if (!is_mddev_idle(mddev, 0)) {
7915 /*
7916 * Give other IO more of a chance.
7917 * The faster the devices, the less we wait.
7918 */
7919 wait_event(mddev->recovery_wait,
7920 !atomic_read(&mddev->recovery_active));
7921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 }
7923 }
NeilBrownc91abf52013-11-19 12:02:01 +11007924 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7925 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7926 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 /*
7928 * this also signals 'finished resyncing' to md_stop
7929 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007930 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7932
7933 /* tell personality that we are finished */
NeilBrown09314792015-02-19 16:04:40 +11007934 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007936 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007937 md_cluster_ops->resync_finish(mddev);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007938
NeilBrowndfc70642008-05-23 13:04:39 -07007939 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007940 mddev->curr_resync > 2) {
7941 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7942 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7943 if (mddev->curr_resync >= mddev->recovery_cp) {
7944 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007945 "md: checkpointing %s of %s.\n",
7946 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007947 if (test_bit(MD_RECOVERY_ERROR,
7948 &mddev->recovery))
7949 mddev->recovery_cp =
7950 mddev->curr_resync_completed;
7951 else
7952 mddev->recovery_cp =
7953 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007954 }
7955 } else
7956 mddev->recovery_cp = MaxSector;
7957 } else {
7958 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7959 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007960 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007961 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007962 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007963 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007964 !test_bit(Faulty, &rdev->flags) &&
7965 !test_bit(In_sync, &rdev->flags) &&
7966 rdev->recovery_offset < mddev->curr_resync)
7967 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007968 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007971 skip:
NeilBrown17571282006-12-10 02:20:52 -08007972 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973
NeilBrown23da4222014-12-15 12:57:01 +11007974 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11007975 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7976 /* We completed so min/max setting can be forgotten if used. */
7977 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7978 mddev->resync_min = 0;
7979 mddev->resync_max = MaxSector;
7980 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7981 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007983 spin_unlock(&mddev->lock);
7984
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 wake_up(&resync_wait);
7986 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7987 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007988 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989}
NeilBrown29269552006-03-27 01:18:10 -08007990EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991
NeilBrown746d3202013-04-24 11:42:41 +10007992static int remove_and_add_spares(struct mddev *mddev,
7993 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007994{
NeilBrown3cb03002011-10-11 16:45:26 +11007995 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007996 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007997 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007998
NeilBrowndafb20f2012-03-19 12:46:39 +11007999 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008000 if ((this == NULL || rdev == this) &&
8001 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008002 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008003 (test_bit(Faulty, &rdev->flags) ||
8004 ! test_bit(In_sync, &rdev->flags)) &&
8005 atomic_read(&rdev->nr_pending)==0) {
8006 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008007 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008008 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008009 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008010 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008011 }
8012 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008013 if (removed && mddev->kobj.sd)
8014 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008015
NeilBrown746d3202013-04-24 11:42:41 +10008016 if (this)
8017 goto no_add;
8018
NeilBrowndafb20f2012-03-19 12:46:39 +11008019 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11008020 if (rdev->raid_disk >= 0 &&
8021 !test_bit(In_sync, &rdev->flags) &&
8022 !test_bit(Faulty, &rdev->flags))
8023 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008024 if (rdev->raid_disk >= 0)
8025 continue;
8026 if (test_bit(Faulty, &rdev->flags))
8027 continue;
8028 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11008029 ! (rdev->saved_raid_disk >= 0 &&
8030 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10008031 continue;
8032
NeilBrown7eb41882014-01-14 15:55:14 +11008033 if (rdev->saved_raid_disk < 0)
8034 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008035 if (mddev->pers->
8036 hot_add_disk(mddev, rdev) == 0) {
8037 if (sysfs_link_rdev(mddev, rdev))
8038 /* failure here is OK */;
8039 spares++;
8040 md_new_event(mddev);
8041 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008042 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008043 }
NeilBrown746d3202013-04-24 11:42:41 +10008044no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008045 if (removed)
8046 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008047 return spares;
8048}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008049
NeilBrownac05f252014-09-30 08:10:42 +10008050static void md_start_sync(struct work_struct *ws)
8051{
8052 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8053
8054 mddev->sync_thread = md_register_thread(md_do_sync,
8055 mddev,
8056 "resync");
8057 if (!mddev->sync_thread) {
8058 printk(KERN_ERR "%s: could not start resync"
8059 " thread...\n",
8060 mdname(mddev));
8061 /* leave the spares where they are, it shouldn't hurt */
8062 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8063 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8064 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8065 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8066 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008067 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008068 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8069 &mddev->recovery))
8070 if (mddev->sysfs_action)
8071 sysfs_notify_dirent_safe(mddev->sysfs_action);
8072 } else
8073 md_wakeup_thread(mddev->sync_thread);
8074 sysfs_notify_dirent_safe(mddev->sysfs_action);
8075 md_new_event(mddev);
8076}
8077
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078/*
8079 * This routine is regularly called by all per-raid-array threads to
8080 * deal with generic issues like resync and super-block update.
8081 * Raid personalities that don't have a thread (linear/raid0) do not
8082 * need this as they never do any recovery or update the superblock.
8083 *
8084 * It does not do any resync itself, but rather "forks" off other threads
8085 * to do that as needed.
8086 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8087 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008088 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089 * and wakeups up this thread which will reap the thread and finish up.
8090 * This thread also removes any faulty devices (with nr_pending == 0).
8091 *
8092 * The overall approach is:
8093 * 1/ if the superblock needs updating, update it.
8094 * 2/ If a recovery thread is running, don't do anything else.
8095 * 3/ If recovery has finished, clean up, possibly marking spares active.
8096 * 4/ If there are any faulty devices, remove them.
8097 * 5/ If array is degraded, try to add spares devices
8098 * 6/ If array has spares or is not in-sync, start a resync thread.
8099 */
NeilBrownfd01b882011-10-11 16:47:53 +11008100void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008102 if (mddev->suspended)
8103 return;
8104
NeilBrown5f404022005-06-21 17:17:16 -07008105 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008106 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107
NeilBrownfca4d842005-06-21 17:17:11 -07008108 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008109 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008110 printk(KERN_INFO "md: %s in immediate safe mode\n",
8111 mdname(mddev));
8112 mddev->safemode = 2;
8113 }
8114 flush_signals(current);
8115 }
8116
NeilBrownc89a8ee2008-08-05 15:54:13 +10008117 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008120 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008122 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008123 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008124 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8125 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126 ))
8127 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008128
NeilBrowndf5b89b2006-03-27 01:18:20 -08008129 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008130 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008131
NeilBrownc89a8ee2008-08-05 15:54:13 +10008132 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008133 struct md_rdev *rdev;
8134 if (!mddev->external && mddev->in_sync)
8135 /* 'Blocked' flag not needed as failed devices
8136 * will be recorded if array switched to read/write.
8137 * Leaving it set will prevent the device
8138 * from being removed.
8139 */
8140 rdev_for_each(rdev, mddev)
8141 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008142 /* On a read-only array we can:
8143 * - remove failed devices
8144 * - add already-in_sync devices if the array itself
8145 * is in-sync.
8146 * As we only add devices that are already in-sync,
8147 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008148 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008149 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008150 /* There is no thread, but we need to call
8151 * ->spare_active and clear saved_raid_disk
8152 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008153 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008154 md_reap_sync_thread(mddev);
8155 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008156 goto unlock;
8157 }
8158
NeilBrown31a59e32008-04-30 00:52:30 -07008159 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008160 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008161 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008162 if (mddev->safemode &&
8163 !atomic_read(&mddev->writes_pending) &&
8164 !mddev->in_sync &&
8165 mddev->recovery_cp == MaxSector) {
8166 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008167 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008168 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008169 }
8170 if (mddev->safemode == 1)
8171 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008172 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008173 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008174 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008175 }
NeilBrownfca4d842005-06-21 17:17:11 -07008176
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008177 if (mddev->flags & MD_UPDATE_SB_FLAGS) {
8178 if (mddev_is_clustered(mddev))
8179 md_cluster_ops->metadata_update_start(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07008180 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008181 if (mddev_is_clustered(mddev))
8182 md_cluster_ops->metadata_update_finish(mddev);
8183 }
NeilBrown06d91a52005-06-21 17:17:12 -07008184
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8186 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8187 /* resync/recovery still happening */
8188 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8189 goto unlock;
8190 }
8191 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008192 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193 goto unlock;
8194 }
Neil Brown72a23c22008-06-28 08:31:41 +10008195 /* Set RUNNING before clearing NEEDED to avoid
8196 * any transients in the value of "sync_action".
8197 */
NeilBrown72f36d52012-10-11 14:25:57 +11008198 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008199 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008200 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008201 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008202 /* Clear some bits that don't mean anything, but
8203 * might be left set
8204 */
NeilBrown24dd4692005-11-08 21:39:26 -08008205 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8206 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207
NeilBrowned209582012-04-24 10:23:14 +10008208 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8209 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008210 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008211 /* no recovery is running.
8212 * remove any failed drives, then
8213 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008214 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 * the personality to fail the re-add.
8216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008218 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008219 if (mddev->pers->check_reshape == NULL ||
8220 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008221 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008222 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008223 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008224 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008225 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008226 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8227 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008228 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008229 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008230 } else if (mddev->recovery_cp < MaxSector) {
8231 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008232 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008233 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8234 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008235 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008236
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008238 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008239 /* We are adding a device or devices to an array
8240 * which has the bitmap stored on all devices.
8241 * So make sure all bitmap pages get written
8242 */
8243 bitmap_write_all(mddev->bitmap);
8244 }
NeilBrownac05f252014-09-30 08:10:42 +10008245 INIT_WORK(&mddev->del_work, md_start_sync);
8246 queue_work(md_misc_wq, &mddev->del_work);
8247 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 }
NeilBrownac05f252014-09-30 08:10:42 +10008249 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008250 if (!mddev->sync_thread) {
8251 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008252 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008253 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8254 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008255 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008256 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008257 }
NeilBrownac05f252014-09-30 08:10:42 +10008258 unlock:
8259 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260 mddev_unlock(mddev);
8261 }
8262}
NeilBrown6c144d32014-09-30 16:15:38 +10008263EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008265void md_reap_sync_thread(struct mddev *mddev)
8266{
8267 struct md_rdev *rdev;
8268
8269 /* resync has finished, collect result */
8270 md_unregister_thread(&mddev->sync_thread);
8271 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8272 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8273 /* success...*/
8274 /* activate any spares */
8275 if (mddev->pers->spare_active(mddev)) {
8276 sysfs_notify(&mddev->kobj, NULL,
8277 "degraded");
8278 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8279 }
8280 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008281 if (mddev_is_clustered(mddev))
8282 md_cluster_ops->metadata_update_start(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008283 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8284 mddev->pers->finish_reshape)
8285 mddev->pers->finish_reshape(mddev);
8286
8287 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008288 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008289 */
NeilBrownf4667222013-12-09 12:04:56 +11008290 if (!mddev->degraded)
8291 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008292 rdev->saved_raid_disk = -1;
8293
8294 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008295 if (mddev_is_clustered(mddev))
8296 md_cluster_ops->metadata_update_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008297 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008298 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008299 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8300 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8301 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8302 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008303 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008304 /* flag recovery needed just to double check */
8305 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8306 sysfs_notify_dirent_safe(mddev->sysfs_action);
8307 md_new_event(mddev);
8308 if (mddev->event_work.func)
8309 queue_work(md_misc_wq, &mddev->event_work);
8310}
NeilBrown6c144d32014-09-30 16:15:38 +10008311EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008312
NeilBrownfd01b882011-10-11 16:47:53 +11008313void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008314{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008315 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008316 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008317 !test_bit(Blocked, &rdev->flags) &&
8318 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008319 msecs_to_jiffies(5000));
8320 rdev_dec_pending(rdev, mddev);
8321}
8322EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8323
NeilBrownc6563a82012-05-21 09:27:00 +10008324void md_finish_reshape(struct mddev *mddev)
8325{
8326 /* called be personality module when reshape completes. */
8327 struct md_rdev *rdev;
8328
8329 rdev_for_each(rdev, mddev) {
8330 if (rdev->data_offset > rdev->new_data_offset)
8331 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8332 else
8333 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8334 rdev->data_offset = rdev->new_data_offset;
8335 }
8336}
8337EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008338
8339/* Bad block management.
8340 * We can record which blocks on each device are 'bad' and so just
8341 * fail those blocks, or that stripe, rather than the whole device.
8342 * Entries in the bad-block table are 64bits wide. This comprises:
8343 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8344 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8345 * A 'shift' can be set so that larger blocks are tracked and
8346 * consequently larger devices can be covered.
8347 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8348 *
8349 * Locking of the bad-block table uses a seqlock so md_is_badblock
8350 * might need to retry if it is very unlucky.
8351 * We will sometimes want to check for bad blocks in a bi_end_io function,
8352 * so we use the write_seqlock_irq variant.
8353 *
8354 * When looking for a bad block we specify a range and want to
8355 * know if any block in the range is bad. So we binary-search
8356 * to the last range that starts at-or-before the given endpoint,
8357 * (or "before the sector after the target range")
8358 * then see if it ends after the given start.
8359 * We return
8360 * 0 if there are no known bad blocks in the range
8361 * 1 if there are known bad block which are all acknowledged
8362 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8363 * plus the start/length of the first bad section we overlap.
8364 */
8365int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8366 sector_t *first_bad, int *bad_sectors)
8367{
8368 int hi;
majianpengab056132012-11-06 17:13:44 +08008369 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008370 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008371 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008372 sector_t target = s + sectors;
8373 unsigned seq;
8374
8375 if (bb->shift > 0) {
8376 /* round the start down, and the end up */
8377 s >>= bb->shift;
8378 target += (1<<bb->shift) - 1;
8379 target >>= bb->shift;
8380 sectors = target - s;
8381 }
8382 /* 'target' is now the first block after the bad range */
8383
8384retry:
8385 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008386 lo = 0;
8387 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008388 hi = bb->count;
8389
8390 /* Binary search between lo and hi for 'target'
8391 * i.e. for the last range that starts before 'target'
8392 */
8393 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8394 * are known not to be the last range before target.
8395 * VARIANT: hi-lo is the number of possible
8396 * ranges, and decreases until it reaches 1
8397 */
8398 while (hi - lo > 1) {
8399 int mid = (lo + hi) / 2;
8400 sector_t a = BB_OFFSET(p[mid]);
8401 if (a < target)
8402 /* This could still be the one, earlier ranges
8403 * could not. */
8404 lo = mid;
8405 else
8406 /* This and later ranges are definitely out. */
8407 hi = mid;
8408 }
8409 /* 'lo' might be the last that started before target, but 'hi' isn't */
8410 if (hi > lo) {
8411 /* need to check all range that end after 's' to see if
8412 * any are unacknowledged.
8413 */
8414 while (lo >= 0 &&
8415 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8416 if (BB_OFFSET(p[lo]) < target) {
8417 /* starts before the end, and finishes after
8418 * the start, so they must overlap
8419 */
8420 if (rv != -1 && BB_ACK(p[lo]))
8421 rv = 1;
8422 else
8423 rv = -1;
8424 *first_bad = BB_OFFSET(p[lo]);
8425 *bad_sectors = BB_LEN(p[lo]);
8426 }
8427 lo--;
8428 }
8429 }
8430
8431 if (read_seqretry(&bb->lock, seq))
8432 goto retry;
8433
8434 return rv;
8435}
8436EXPORT_SYMBOL_GPL(md_is_badblock);
8437
8438/*
8439 * Add a range of bad blocks to the table.
8440 * This might extend the table, or might contract it
8441 * if two adjacent ranges can be merged.
8442 * We binary-search to find the 'insertion' point, then
8443 * decide how best to handle it.
8444 */
8445static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8446 int acknowledged)
8447{
8448 u64 *p;
8449 int lo, hi;
8450 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008451 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008452
8453 if (bb->shift < 0)
8454 /* badblocks are disabled */
8455 return 0;
8456
8457 if (bb->shift) {
8458 /* round the start down, and the end up */
8459 sector_t next = s + sectors;
8460 s >>= bb->shift;
8461 next += (1<<bb->shift) - 1;
8462 next >>= bb->shift;
8463 sectors = next - s;
8464 }
8465
Bian Yu905b0292013-10-12 01:10:03 -04008466 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008467
8468 p = bb->page;
8469 lo = 0;
8470 hi = bb->count;
8471 /* Find the last range that starts at-or-before 's' */
8472 while (hi - lo > 1) {
8473 int mid = (lo + hi) / 2;
8474 sector_t a = BB_OFFSET(p[mid]);
8475 if (a <= s)
8476 lo = mid;
8477 else
8478 hi = mid;
8479 }
8480 if (hi > lo && BB_OFFSET(p[lo]) > s)
8481 hi = lo;
8482
8483 if (hi > lo) {
8484 /* we found a range that might merge with the start
8485 * of our new range
8486 */
8487 sector_t a = BB_OFFSET(p[lo]);
8488 sector_t e = a + BB_LEN(p[lo]);
8489 int ack = BB_ACK(p[lo]);
8490 if (e >= s) {
8491 /* Yes, we can merge with a previous range */
8492 if (s == a && s + sectors >= e)
8493 /* new range covers old */
8494 ack = acknowledged;
8495 else
8496 ack = ack && acknowledged;
8497
8498 if (e < s + sectors)
8499 e = s + sectors;
8500 if (e - a <= BB_MAX_LEN) {
8501 p[lo] = BB_MAKE(a, e-a, ack);
8502 s = e;
8503 } else {
8504 /* does not all fit in one range,
8505 * make p[lo] maximal
8506 */
8507 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8508 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8509 s = a + BB_MAX_LEN;
8510 }
8511 sectors = e - s;
8512 }
8513 }
8514 if (sectors && hi < bb->count) {
8515 /* 'hi' points to the first range that starts after 's'.
8516 * Maybe we can merge with the start of that range */
8517 sector_t a = BB_OFFSET(p[hi]);
8518 sector_t e = a + BB_LEN(p[hi]);
8519 int ack = BB_ACK(p[hi]);
8520 if (a <= s + sectors) {
8521 /* merging is possible */
8522 if (e <= s + sectors) {
8523 /* full overlap */
8524 e = s + sectors;
8525 ack = acknowledged;
8526 } else
8527 ack = ack && acknowledged;
8528
8529 a = s;
8530 if (e - a <= BB_MAX_LEN) {
8531 p[hi] = BB_MAKE(a, e-a, ack);
8532 s = e;
8533 } else {
8534 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8535 s = a + BB_MAX_LEN;
8536 }
8537 sectors = e - s;
8538 lo = hi;
8539 hi++;
8540 }
8541 }
8542 if (sectors == 0 && hi < bb->count) {
8543 /* we might be able to combine lo and hi */
8544 /* Note: 's' is at the end of 'lo' */
8545 sector_t a = BB_OFFSET(p[hi]);
8546 int lolen = BB_LEN(p[lo]);
8547 int hilen = BB_LEN(p[hi]);
8548 int newlen = lolen + hilen - (s - a);
8549 if (s >= a && newlen < BB_MAX_LEN) {
8550 /* yes, we can combine them */
8551 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8552 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8553 memmove(p + hi, p + hi + 1,
8554 (bb->count - hi - 1) * 8);
8555 bb->count--;
8556 }
8557 }
8558 while (sectors) {
8559 /* didn't merge (it all).
8560 * Need to add a range just before 'hi' */
8561 if (bb->count >= MD_MAX_BADBLOCKS) {
8562 /* No room for more */
8563 rv = 0;
8564 break;
8565 } else {
8566 int this_sectors = sectors;
8567 memmove(p + hi + 1, p + hi,
8568 (bb->count - hi) * 8);
8569 bb->count++;
8570
8571 if (this_sectors > BB_MAX_LEN)
8572 this_sectors = BB_MAX_LEN;
8573 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8574 sectors -= this_sectors;
8575 s += this_sectors;
8576 }
8577 }
8578
8579 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008580 if (!acknowledged)
8581 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008582 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008583
8584 return rv;
8585}
8586
NeilBrown3cb03002011-10-11 16:45:26 +11008587int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008588 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008589{
NeilBrownc6563a82012-05-21 09:27:00 +10008590 int rv;
8591 if (is_new)
8592 s += rdev->new_data_offset;
8593 else
8594 s += rdev->data_offset;
8595 rv = md_set_badblocks(&rdev->badblocks,
8596 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008597 if (rv) {
8598 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008599 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008600 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8601 md_wakeup_thread(rdev->mddev->thread);
8602 }
8603 return rv;
8604}
8605EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8606
8607/*
8608 * Remove a range of bad blocks from the table.
8609 * This may involve extending the table if we spilt a region,
8610 * but it must not fail. So if the table becomes full, we just
8611 * drop the remove request.
8612 */
8613static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8614{
8615 u64 *p;
8616 int lo, hi;
8617 sector_t target = s + sectors;
8618 int rv = 0;
8619
8620 if (bb->shift > 0) {
8621 /* When clearing we round the start up and the end down.
8622 * This should not matter as the shift should align with
8623 * the block size and no rounding should ever be needed.
8624 * However it is better the think a block is bad when it
8625 * isn't than to think a block is not bad when it is.
8626 */
8627 s += (1<<bb->shift) - 1;
8628 s >>= bb->shift;
8629 target >>= bb->shift;
8630 sectors = target - s;
8631 }
8632
8633 write_seqlock_irq(&bb->lock);
8634
8635 p = bb->page;
8636 lo = 0;
8637 hi = bb->count;
8638 /* Find the last range that starts before 'target' */
8639 while (hi - lo > 1) {
8640 int mid = (lo + hi) / 2;
8641 sector_t a = BB_OFFSET(p[mid]);
8642 if (a < target)
8643 lo = mid;
8644 else
8645 hi = mid;
8646 }
8647 if (hi > lo) {
8648 /* p[lo] is the last range that could overlap the
8649 * current range. Earlier ranges could also overlap,
8650 * but only this one can overlap the end of the range.
8651 */
8652 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8653 /* Partial overlap, leave the tail of this range */
8654 int ack = BB_ACK(p[lo]);
8655 sector_t a = BB_OFFSET(p[lo]);
8656 sector_t end = a + BB_LEN(p[lo]);
8657
8658 if (a < s) {
8659 /* we need to split this range */
8660 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008661 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008662 goto out;
8663 }
8664 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8665 bb->count++;
8666 p[lo] = BB_MAKE(a, s-a, ack);
8667 lo++;
8668 }
8669 p[lo] = BB_MAKE(target, end - target, ack);
8670 /* there is no longer an overlap */
8671 hi = lo;
8672 lo--;
8673 }
8674 while (lo >= 0 &&
8675 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8676 /* This range does overlap */
8677 if (BB_OFFSET(p[lo]) < s) {
8678 /* Keep the early parts of this range. */
8679 int ack = BB_ACK(p[lo]);
8680 sector_t start = BB_OFFSET(p[lo]);
8681 p[lo] = BB_MAKE(start, s - start, ack);
8682 /* now low doesn't overlap, so.. */
8683 break;
8684 }
8685 lo--;
8686 }
8687 /* 'lo' is strictly before, 'hi' is strictly after,
8688 * anything between needs to be discarded
8689 */
8690 if (hi - lo > 1) {
8691 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8692 bb->count -= (hi - lo - 1);
8693 }
8694 }
8695
8696 bb->changed = 1;
8697out:
8698 write_sequnlock_irq(&bb->lock);
8699 return rv;
8700}
8701
NeilBrownc6563a82012-05-21 09:27:00 +10008702int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8703 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008704{
NeilBrownc6563a82012-05-21 09:27:00 +10008705 if (is_new)
8706 s += rdev->new_data_offset;
8707 else
8708 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008709 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008710 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008711}
8712EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8713
8714/*
8715 * Acknowledge all bad blocks in a list.
8716 * This only succeeds if ->changed is clear. It is used by
8717 * in-kernel metadata updates
8718 */
8719void md_ack_all_badblocks(struct badblocks *bb)
8720{
8721 if (bb->page == NULL || bb->changed)
8722 /* no point even trying */
8723 return;
8724 write_seqlock_irq(&bb->lock);
8725
majianpengecb178b2012-03-19 12:46:42 +11008726 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008727 u64 *p = bb->page;
8728 int i;
8729 for (i = 0; i < bb->count ; i++) {
8730 if (!BB_ACK(p[i])) {
8731 sector_t start = BB_OFFSET(p[i]);
8732 int len = BB_LEN(p[i]);
8733 p[i] = BB_MAKE(start, len, 1);
8734 }
8735 }
NeilBrownde393cd2011-07-28 11:31:48 +10008736 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008737 }
8738 write_sequnlock_irq(&bb->lock);
8739}
8740EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8741
NeilBrown16c791a2011-07-28 11:31:47 +10008742/* sysfs access to bad-blocks list.
8743 * We present two files.
8744 * 'bad-blocks' lists sector numbers and lengths of ranges that
8745 * are recorded as bad. The list is truncated to fit within
8746 * the one-page limit of sysfs.
8747 * Writing "sector length" to this file adds an acknowledged
8748 * bad block list.
8749 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8750 * been acknowledged. Writing to this file adds bad blocks
8751 * without acknowledging them. This is largely for testing.
8752 */
8753
8754static ssize_t
8755badblocks_show(struct badblocks *bb, char *page, int unack)
8756{
8757 size_t len;
8758 int i;
8759 u64 *p = bb->page;
8760 unsigned seq;
8761
8762 if (bb->shift < 0)
8763 return 0;
8764
8765retry:
8766 seq = read_seqbegin(&bb->lock);
8767
8768 len = 0;
8769 i = 0;
8770
8771 while (len < PAGE_SIZE && i < bb->count) {
8772 sector_t s = BB_OFFSET(p[i]);
8773 unsigned int length = BB_LEN(p[i]);
8774 int ack = BB_ACK(p[i]);
8775 i++;
8776
8777 if (unack && ack)
8778 continue;
8779
8780 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8781 (unsigned long long)s << bb->shift,
8782 length << bb->shift);
8783 }
NeilBrownde393cd2011-07-28 11:31:48 +10008784 if (unack && len == 0)
8785 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008786
8787 if (read_seqretry(&bb->lock, seq))
8788 goto retry;
8789
8790 return len;
8791}
8792
8793#define DO_DEBUG 1
8794
8795static ssize_t
8796badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8797{
8798 unsigned long long sector;
8799 int length;
8800 char newline;
8801#ifdef DO_DEBUG
8802 /* Allow clearing via sysfs *only* for testing/debugging.
8803 * Normally only a successful write may clear a badblock
8804 */
8805 int clear = 0;
8806 if (page[0] == '-') {
8807 clear = 1;
8808 page++;
8809 }
8810#endif /* DO_DEBUG */
8811
8812 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8813 case 3:
8814 if (newline != '\n')
8815 return -EINVAL;
8816 case 2:
8817 if (length <= 0)
8818 return -EINVAL;
8819 break;
8820 default:
8821 return -EINVAL;
8822 }
8823
8824#ifdef DO_DEBUG
8825 if (clear) {
8826 md_clear_badblocks(bb, sector, length);
8827 return len;
8828 }
8829#endif /* DO_DEBUG */
8830 if (md_set_badblocks(bb, sector, length, !unack))
8831 return len;
8832 else
8833 return -ENOSPC;
8834}
8835
Adrian Bunk75c96f82005-05-05 16:16:09 -07008836static int md_notify_reboot(struct notifier_block *this,
8837 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838{
8839 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008840 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008841 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842
NeilBrownc744a652012-03-19 12:46:37 +11008843 for_each_mddev(mddev, tmp) {
8844 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008845 if (mddev->pers)
8846 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008847 if (mddev->persistent)
8848 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008849 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008850 }
NeilBrownc744a652012-03-19 12:46:37 +11008851 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852 }
NeilBrownc744a652012-03-19 12:46:37 +11008853 /*
8854 * certain more exotic SCSI devices are known to be
8855 * volatile wrt too early system reboots. While the
8856 * right place to handle this issue is the given
8857 * driver, we do want to have a safe RAID driver ...
8858 */
8859 if (need_delay)
8860 mdelay(1000*1);
8861
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862 return NOTIFY_DONE;
8863}
8864
Adrian Bunk75c96f82005-05-05 16:16:09 -07008865static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866 .notifier_call = md_notify_reboot,
8867 .next = NULL,
8868 .priority = INT_MAX, /* before any real devices */
8869};
8870
8871static void md_geninit(void)
8872{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008873 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008875 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876}
8877
Adrian Bunk75c96f82005-05-05 16:16:09 -07008878static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879{
Tejun Heoe804ac72010-10-15 15:36:08 +02008880 int ret = -ENOMEM;
8881
Tejun Heoada609e2011-01-25 14:35:54 +01008882 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008883 if (!md_wq)
8884 goto err_wq;
8885
8886 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8887 if (!md_misc_wq)
8888 goto err_misc_wq;
8889
8890 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8891 goto err_md;
8892
8893 if ((ret = register_blkdev(0, "mdp")) < 0)
8894 goto err_mdp;
8895 mdp_major = ret;
8896
NeilBrownaf5628f2014-07-31 13:54:54 +10008897 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008898 md_probe, NULL, NULL);
8899 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 md_probe, NULL, NULL);
8901
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008903 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904
8905 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
Tejun Heoe804ac72010-10-15 15:36:08 +02008908err_mdp:
8909 unregister_blkdev(MD_MAJOR, "md");
8910err_md:
8911 destroy_workqueue(md_misc_wq);
8912err_misc_wq:
8913 destroy_workqueue(md_wq);
8914err_wq:
8915 return ret;
8916}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008918void md_reload_sb(struct mddev *mddev)
8919{
8920 struct md_rdev *rdev, *tmp;
8921
8922 rdev_for_each_safe(rdev, tmp, mddev) {
8923 rdev->sb_loaded = 0;
8924 ClearPageUptodate(rdev->sb_page);
8925 }
8926 mddev->raid_disks = 0;
8927 analyze_sbs(mddev);
8928 rdev_for_each_safe(rdev, tmp, mddev) {
8929 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8930 /* since we don't write to faulty devices, we figure out if the
8931 * disk is faulty by comparing events
8932 */
8933 if (mddev->events > sb->events)
8934 set_bit(Faulty, &rdev->flags);
8935 }
8936
8937}
8938EXPORT_SYMBOL(md_reload_sb);
8939
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940#ifndef MODULE
8941
8942/*
8943 * Searches all registered partitions for autorun RAID arrays
8944 * at boot time.
8945 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008946
8947static LIST_HEAD(all_detected_devices);
8948struct detected_devices_node {
8949 struct list_head list;
8950 dev_t dev;
8951};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952
8953void md_autodetect_dev(dev_t dev)
8954{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008955 struct detected_devices_node *node_detected_dev;
8956
8957 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8958 if (node_detected_dev) {
8959 node_detected_dev->dev = dev;
8960 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8961 } else {
8962 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8963 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965}
8966
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967static void autostart_arrays(int part)
8968{
NeilBrown3cb03002011-10-11 16:45:26 +11008969 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008970 struct detected_devices_node *node_detected_dev;
8971 dev_t dev;
8972 int i_scanned, i_passed;
8973
8974 i_scanned = 0;
8975 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008976
8977 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8978
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008979 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8980 i_scanned++;
8981 node_detected_dev = list_entry(all_detected_devices.next,
8982 struct detected_devices_node, list);
8983 list_del(&node_detected_dev->list);
8984 dev = node_detected_dev->dev;
8985 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008986 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008987 if (IS_ERR(rdev))
8988 continue;
8989
NeilBrown403df472014-09-30 15:52:29 +10008990 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008991 continue;
NeilBrown403df472014-09-30 15:52:29 +10008992
NeilBrownd0fae182008-03-04 14:29:31 -08008993 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008995 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008997
8998 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8999 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000
9001 autorun_devices(part);
9002}
9003
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009004#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005
9006static __exit void md_exit(void)
9007{
NeilBrownfd01b882011-10-11 16:47:53 +11009008 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009010 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009011
NeilBrownaf5628f2014-07-31 13:54:54 +10009012 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009013 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009015 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009016 unregister_blkdev(mdp_major, "mdp");
9017 unregister_reboot_notifier(&md_notifier);
9018 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009019
9020 /* We cannot unload the modules while some process is
9021 * waiting for us in select() or poll() - wake them up
9022 */
9023 md_unloading = 1;
9024 while (waitqueue_active(&md_event_waiters)) {
9025 /* not safe to leave yet */
9026 wake_up(&md_event_waiters);
9027 msleep(delay);
9028 delay += delay;
9029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009031
NeilBrown29ac4aa2008-02-06 01:39:58 -08009032 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009033 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009034 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009035 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009036 destroy_workqueue(md_misc_wq);
9037 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038}
9039
Dan Williams685784a2007-07-09 11:56:42 -07009040subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041module_exit(md_exit)
9042
NeilBrownf91de922005-11-08 21:39:36 -08009043static int get_ro(char *buffer, struct kernel_param *kp)
9044{
9045 return sprintf(buffer, "%d", start_readonly);
9046}
9047static int set_ro(const char *val, struct kernel_param *kp)
9048{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009049 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009050}
9051
NeilBrown80ca3a42006-07-10 04:44:18 -07009052module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9053module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009054module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009055
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009057MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009058MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009059MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);