blob: 4f5ecbe94ccbf97c562d96930635c6aaff0550d3 [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
NeilBrowna2826aa2009-12-14 12:49:49 +1100357/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200358 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100359 */
360
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200361static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100362{
NeilBrown3cb03002011-10-11 16:45:26 +1100363 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100364 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100365
366 rdev_dec_pending(rdev, mddev);
367
368 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200369 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200370 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100371 }
372 bio_put(bio);
373}
374
NeilBrowna7a07e62010-12-09 16:04:25 +1100375static void md_submit_flush_data(struct work_struct *ws);
376
NeilBrowna035fc32010-12-09 16:17:51 +1100377static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100378{
NeilBrownfd01b882011-10-11 16:47:53 +1100379 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100380 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100381
NeilBrowna7a07e62010-12-09 16:04:25 +1100382 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
383 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100384 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100385 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100386 if (rdev->raid_disk >= 0 &&
387 !test_bit(Faulty, &rdev->flags)) {
388 /* Take two references, one is dropped
389 * when request finishes, one after
390 * we reclaim rcu_read_lock
391 */
392 struct bio *bi;
393 atomic_inc(&rdev->nr_pending);
394 atomic_inc(&rdev->nr_pending);
395 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000396 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200397 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100398 bi->bi_private = rdev;
399 bi->bi_bdev = rdev->bdev;
400 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200401 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100402 rcu_read_lock();
403 rdev_dec_pending(rdev, mddev);
404 }
405 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100406 if (atomic_dec_and_test(&mddev->flush_pending))
407 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100408}
409
Tejun Heoe9c74692010-09-03 11:56:18 +0200410static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100411{
NeilBrownfd01b882011-10-11 16:47:53 +1100412 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200413 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100414
Kent Overstreet4f024f32013-10-11 15:44:27 -0700415 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100416 /* an empty barrier - all done */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200417 bio_endio(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100418 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200419 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200420 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100421 }
NeilBrown2b74e122010-12-09 15:59:01 +1100422
423 mddev->flush_bio = NULL;
424 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100425}
426
NeilBrownfd01b882011-10-11 16:47:53 +1100427void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100428{
NeilBrown85572d72014-12-15 12:56:56 +1100429 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100430 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200431 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100432 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200433 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100434 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100435
NeilBrowna035fc32010-12-09 16:17:51 +1100436 INIT_WORK(&mddev->flush_work, submit_flushes);
437 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100438}
Tejun Heoe9c74692010-09-03 11:56:18 +0200439EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100440
NeilBrown74018dc2012-07-31 09:08:15 +0200441void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000442{
NeilBrown9cbb1752012-07-31 09:08:14 +0200443 struct mddev *mddev = cb->data;
444 md_wakeup_thread(mddev->thread);
445 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000446}
NeilBrown9cbb1752012-07-31 09:08:14 +0200447EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000448
NeilBrownfd01b882011-10-11 16:47:53 +1100449static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 atomic_inc(&mddev->active);
452 return mddev;
453}
454
Dan Williams5fd3a172009-03-04 00:57:25 -0700455static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100456
NeilBrownfd01b882011-10-11 16:47:53 +1100457static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
NeilBrowna167f662010-10-26 18:31:13 +1100459 struct bio_set *bs = NULL;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
462 return;
NeilBrownd3374822009-01-09 08:31:10 +1100463 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100464 mddev->ctime == 0 && !mddev->hold_active) {
465 /* Array is not configured at all, and not held active,
466 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100467 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100468 bs = mddev->bio_set;
469 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100470 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200471 /* We did a probe so need to clean up. Call
472 * queue_work inside the spinlock so that
473 * flush_workqueue() after mddev_find will
474 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100475 */
476 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200477 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100478 } else
479 kfree(mddev);
480 }
481 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100482 if (bs)
483 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Sasha Levin25b2edf2015-07-24 18:19:58 -0400486static void md_safemode_timeout(unsigned long data);
487
NeilBrownfd01b882011-10-11 16:47:53 +1100488void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100489{
490 mutex_init(&mddev->open_mutex);
491 mutex_init(&mddev->reconfig_mutex);
492 mutex_init(&mddev->bitmap_info.mutex);
493 INIT_LIST_HEAD(&mddev->disks);
494 INIT_LIST_HEAD(&mddev->all_mddevs);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400495 setup_timer(&mddev->safemode_timer, md_safemode_timeout,
496 (unsigned long) mddev);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100497 atomic_set(&mddev->active, 1);
498 atomic_set(&mddev->openers, 0);
499 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100500 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100501 atomic_set(&mddev->flush_pending, 0);
502 init_waitqueue_head(&mddev->sb_wait);
503 init_waitqueue_head(&mddev->recovery_wait);
504 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000505 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500506 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100507 mddev->resync_min = 0;
508 mddev->resync_max = MaxSector;
509 mddev->level = LEVEL_NONE;
510}
NeilBrown390ee602010-06-01 19:37:27 +1000511EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100512
NeilBrownf72ffdd2014-09-30 14:23:59 +1000513static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
NeilBrownfd01b882011-10-11 16:47:53 +1100515 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
NeilBrown8f5f02c2011-02-16 13:58:51 +1100517 if (unit && MAJOR(unit) != MD_MAJOR)
518 unit &= ~((1<<MdpMinorShift)-1);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 retry:
521 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
NeilBrownefeb53c2009-01-09 08:31:10 +1100523 if (unit) {
524 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
525 if (mddev->unit == unit) {
526 mddev_get(mddev);
527 spin_unlock(&all_mddevs_lock);
528 kfree(new);
529 return mddev;
530 }
531
532 if (new) {
533 list_add(&new->all_mddevs, &all_mddevs);
534 spin_unlock(&all_mddevs_lock);
535 new->hold_active = UNTIL_IOCTL;
536 return new;
537 }
538 } else if (new) {
539 /* find an unused unit number */
540 static int next_minor = 512;
541 int start = next_minor;
542 int is_free = 0;
543 int dev = 0;
544 while (!is_free) {
545 dev = MKDEV(MD_MAJOR, next_minor);
546 next_minor++;
547 if (next_minor > MINORMASK)
548 next_minor = 0;
549 if (next_minor == start) {
550 /* Oh dear, all in use. */
551 spin_unlock(&all_mddevs_lock);
552 kfree(new);
553 return NULL;
554 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000555
NeilBrownefeb53c2009-01-09 08:31:10 +1100556 is_free = 1;
557 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
558 if (mddev->unit == dev) {
559 is_free = 0;
560 break;
561 }
562 }
563 new->unit = dev;
564 new->md_minor = MINOR(dev);
565 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 list_add(&new->all_mddevs, &all_mddevs);
567 spin_unlock(&all_mddevs_lock);
568 return new;
569 }
570 spin_unlock(&all_mddevs_lock);
571
NeilBrown9ffae0c2006-01-06 00:20:32 -0800572 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!new)
574 return NULL;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 new->unit = unit;
577 if (MAJOR(unit) == MD_MAJOR)
578 new->md_minor = MINOR(unit);
579 else
580 new->md_minor = MINOR(unit) >> MdpMinorShift;
581
NeilBrownfafd7fb2010-04-01 15:55:30 +1100582 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 goto retry;
585}
586
NeilBrownb6eb1272010-04-15 10:13:47 +1000587static struct attribute_group md_redundancy_group;
588
NeilBrown5c47daf2014-12-15 12:57:01 +1100589void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
NeilBrowna64c8762010-04-14 17:15:37 +1000591 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000592 /* These cannot be removed under reconfig_mutex as
593 * an access to the files will try to take reconfig_mutex
594 * while holding the file unremovable, which leads to
595 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000596 * So hold set sysfs_active while the remove in happeing,
597 * and anything else which might set ->to_remove or my
598 * otherwise change the sysfs namespace will fail with
599 * -EBUSY if sysfs_active is still set.
600 * We set sysfs_active under reconfig_mutex and elsewhere
601 * test it under the same mutex to ensure its correct value
602 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000603 */
NeilBrowna64c8762010-04-14 17:15:37 +1000604 struct attribute_group *to_remove = mddev->to_remove;
605 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000606 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000607 mutex_unlock(&mddev->reconfig_mutex);
608
NeilBrown00bcb4a2010-06-01 19:37:23 +1000609 if (mddev->kobj.sd) {
610 if (to_remove != &md_redundancy_group)
611 sysfs_remove_group(&mddev->kobj, to_remove);
612 if (mddev->pers == NULL ||
613 mddev->pers->sync_request == NULL) {
614 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
615 if (mddev->sysfs_action)
616 sysfs_put(mddev->sysfs_action);
617 mddev->sysfs_action = NULL;
618 }
NeilBrowna64c8762010-04-14 17:15:37 +1000619 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000620 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000621 } else
622 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Chris Dunlop751e67c2011-10-19 16:48:26 +1100624 /* As we've dropped the mutex we need a spinlock to
625 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000626 */
627 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700628 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000629 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
NeilBrown5c47daf2014-12-15 12:57:01 +1100631EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500633struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100634{
635 struct md_rdev *rdev;
636
637 rdev_for_each_rcu(rdev, mddev)
638 if (rdev->desc_nr == nr)
639 return rdev;
640
641 return NULL;
642}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500643EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100644
645static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
NeilBrown3cb03002011-10-11 16:45:26 +1100647 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
NeilBrowndafb20f2012-03-19 12:46:39 +1100649 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (rdev->bdev->bd_dev == dev)
651 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return NULL;
654}
655
NeilBrown1ca69c42012-10-11 13:37:33 +1100656static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
657{
658 struct md_rdev *rdev;
659
660 rdev_for_each_rcu(rdev, mddev)
661 if (rdev->bdev->bd_dev == dev)
662 return rdev;
663
664 return NULL;
665}
666
NeilBrown84fc4b52011-10-11 16:49:58 +1100667static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800668{
NeilBrown84fc4b52011-10-11 16:49:58 +1100669 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800670 list_for_each_entry(pers, &pers_list, list) {
671 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800672 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800673 if (strcmp(pers->name, clevel)==0)
674 return pers;
675 }
NeilBrown2604b702006-01-06 00:20:36 -0800676 return NULL;
677}
678
Andre Nollb73df2d2008-07-11 22:02:23 +1000679/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100680static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100682 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000683 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
NeilBrownf72ffdd2014-09-30 14:23:59 +1000686static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 rdev->sb_page = alloc_page(GFP_KERNEL);
689 if (!rdev->sb_page) {
690 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000691 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
694 return 0;
695}
696
NeilBrown545c8792012-05-22 13:54:30 +1000697void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800700 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 rdev->sb_loaded = 0;
702 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000703 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100704 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
NeilBrown2699b672011-07-28 11:31:47 +1000706 if (rdev->bb_page) {
707 put_page(rdev->bb_page);
708 rdev->bb_page = NULL;
709 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000710 kfree(rdev->badblocks.page);
711 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
NeilBrown545c8792012-05-22 13:54:30 +1000713EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200715static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700716{
NeilBrown3cb03002011-10-11 16:45:26 +1100717 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100718 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700719
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200720 if (bio->bi_error) {
721 printk("md: super_written gets error=%d\n", bio->bi_error);
NeilBrowna9701a32005-11-08 21:39:34 -0800722 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700723 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700724
NeilBrowna9701a32005-11-08 21:39:34 -0800725 if (atomic_dec_and_test(&mddev->pending_writes))
726 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700727 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700728}
729
NeilBrownfd01b882011-10-11 16:47:53 +1100730void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700731 sector_t sector, int size, struct page *page)
732{
733 /* write first size bytes of page to sector of rdev
734 * Increment mddev->pending_writes before returning
735 * and decrement it on completion, waking up sb_wait
736 * if zero is reached.
737 * If an error occurred, call md_error
738 */
NeilBrowna167f662010-10-26 18:31:13 +1100739 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700740
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100741 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700742 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743 bio_add_page(bio, page, size, 0);
744 bio->bi_private = rdev;
745 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800746
NeilBrown7bfa19f2005-06-21 17:17:28 -0700747 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000748 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800749}
750
NeilBrownfd01b882011-10-11 16:47:53 +1100751void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800752{
Tejun Heoe9c74692010-09-03 11:56:18 +0200753 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000754 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700755}
756
NeilBrown3cb03002011-10-11 16:45:26 +1100757int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100758 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
NeilBrowna167f662010-10-26 18:31:13 +1100760 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int ret;
762
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100763 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
764 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100765 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700766 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000767 else if (rdev->mddev->reshape_position != MaxSector &&
768 (rdev->mddev->reshape_backwards ==
769 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700770 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100771 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700772 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700774 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200776 ret = !bio->bi_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 bio_put(bio);
778 return ret;
779}
NeilBrowna8745db2006-01-06 00:20:34 -0800780EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
NeilBrownf72ffdd2014-09-30 14:23:59 +1000782static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (rdev->sb_loaded)
787 return 0;
788
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100789 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto fail;
791 rdev->sb_loaded = 1;
792 return 0;
793
794fail:
795 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
796 bdevname(rdev->bdev,b));
797 return -EINVAL;
798}
799
800static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
801{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000802 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000803 sb1->set_uuid1 == sb2->set_uuid1 &&
804 sb1->set_uuid2 == sb2->set_uuid2 &&
805 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
809{
810 int ret;
811 mdp_super_t *tmp1, *tmp2;
812
813 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
814 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
815
816 if (!tmp1 || !tmp2) {
817 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100818 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto abort;
820 }
821
822 *tmp1 = *sb1;
823 *tmp2 = *sb2;
824
825 /*
826 * nr_disks is not constant
827 */
828 tmp1->nr_disks = 0;
829 tmp2->nr_disks = 0;
830
Andre Nollce0c8e02008-07-11 22:02:20 +1000831 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700833 kfree(tmp1);
834 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return ret;
836}
837
NeilBrown4d167f02007-05-09 02:35:37 -0700838static u32 md_csum_fold(u32 csum)
839{
840 csum = (csum & 0xffff) + (csum >> 16);
841 return (csum & 0xffff) + (csum >> 16);
842}
843
NeilBrownf72ffdd2014-09-30 14:23:59 +1000844static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
NeilBrown4d167f02007-05-09 02:35:37 -0700846 u64 newcsum = 0;
847 u32 *sb32 = (u32*)sb;
848 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 unsigned int disk_csum, csum;
850
851 disk_csum = sb->sb_csum;
852 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700853
854 for (i = 0; i < MD_SB_BYTES/4 ; i++)
855 newcsum += sb32[i];
856 csum = (newcsum & 0xffffffff) + (newcsum>>32);
857
NeilBrown4d167f02007-05-09 02:35:37 -0700858#ifdef CONFIG_ALPHA
859 /* This used to use csum_partial, which was wrong for several
860 * reasons including that different results are returned on
861 * different architectures. It isn't critical that we get exactly
862 * the same return value as before (we always csum_fold before
863 * testing, and that removes any differences). However as we
864 * know that csum_partial always returned a 16bit value on
865 * alphas, do a fold to maximise conformity to previous behaviour.
866 */
867 sb->sb_csum = md_csum_fold(disk_csum);
868#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700870#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return csum;
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * Handle superblock details.
876 * We want to be able to handle multiple superblock formats
877 * so we have a common interface to them all, and an array of
878 * different handlers.
879 * We rely on user-space to write the initial superblock, and support
880 * reading and updating of superblocks.
881 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100882 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * loads and validates a superblock on dev.
884 * if refdev != NULL, compare superblocks on both devices
885 * Return:
886 * 0 - dev has a superblock that is compatible with refdev
887 * 1 - dev has a superblock that is compatible and newer than refdev
888 * so dev should be used as the refdev in future
889 * -EINVAL superblock incompatible or invalid
890 * -othererror e.g. -EIO
891 *
NeilBrownfd01b882011-10-11 16:47:53 +1100892 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * Verify that dev is acceptable into mddev.
894 * The first time, mddev->raid_disks will be 0, and data from
895 * dev should be merged in. Subsequent calls check that dev
896 * is new enough. Return 0 or -EINVAL
897 *
NeilBrownfd01b882011-10-11 16:47:53 +1100898 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * Update the superblock for rdev with data in mddev
900 * This does not write to disc.
901 *
902 */
903
904struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000905 char *name;
906 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000907 int (*load_super)(struct md_rdev *rdev,
908 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000909 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000910 int (*validate_super)(struct mddev *mddev,
911 struct md_rdev *rdev);
912 void (*sync_super)(struct mddev *mddev,
913 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100914 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000915 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000916 int (*allow_new_offset)(struct md_rdev *rdev,
917 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918};
919
920/*
Andre Noll0894cc32009-06-18 08:49:23 +1000921 * Check that the given mddev has no bitmap.
922 *
923 * This function is called from the run method of all personalities that do not
924 * support bitmaps. It prints an error message and returns non-zero if mddev
925 * has a bitmap. Otherwise, it returns 0.
926 *
927 */
NeilBrownfd01b882011-10-11 16:47:53 +1100928int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000929{
NeilBrownc3d97142009-12-14 12:49:52 +1100930 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000931 return 0;
932 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
933 mdname(mddev), mddev->pers->name);
934 return 1;
935}
936EXPORT_SYMBOL(md_check_no_bitmap);
937
938/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000939 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 */
NeilBrown3cb03002011-10-11 16:45:26 +1100941static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
944 mdp_super_t *sb;
945 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /*
Andre Noll0f420352008-07-11 22:02:23 +1000948 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * it's at the end of the disk.
950 *
951 * It also happens to be a multiple of 4Kb.
952 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100953 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
NeilBrown0002b272005-09-09 16:23:53 -0700955 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (ret) return ret;
957
958 ret = -EINVAL;
959
960 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000961 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (sb->md_magic != MD_SB_MAGIC) {
964 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
965 b);
966 goto abort;
967 }
968
969 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800970 sb->minor_version < 90 ||
971 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
973 sb->major_version, sb->minor_version,
974 b);
975 goto abort;
976 }
977
978 if (sb->raid_disks <= 0)
979 goto abort;
980
NeilBrown4d167f02007-05-09 02:35:37 -0700981 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
983 b);
984 goto abort;
985 }
986
987 rdev->preferred_minor = sb->md_minor;
988 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000989 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700990 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000991 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (sb->level == LEVEL_MULTIPATH)
994 rdev->desc_nr = -1;
995 else
996 rdev->desc_nr = sb->this_disk.number;
997
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700998 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001000 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001002 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (!uuid_equal(refsb, sb)) {
1004 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1005 b, bdevname(refdev->bdev,b2));
1006 goto abort;
1007 }
1008 if (!sb_equal(refsb, sb)) {
1009 printk(KERN_WARNING "md: %s has same UUID"
1010 " but different superblock to %s\n",
1011 b, bdevname(refdev->bdev, b2));
1012 goto abort;
1013 }
1014 ev1 = md_event(sb);
1015 ev2 = md_event(refsb);
1016 if (ev1 > ev2)
1017 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001018 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ret = 0;
1020 }
NeilBrown8190e752009-06-18 08:48:58 +10001021 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001022 /* Limit to 4TB as metadata cannot record more than that.
1023 * (not needed for Linear and RAID0 as metadata doesn't
1024 * record this size)
1025 */
1026 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001027 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
NeilBrown27a7b262011-09-10 17:21:28 +10001029 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001030 /* "this cannot possibly happen" ... */
1031 ret = -EINVAL;
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 abort:
1034 return ret;
1035}
1036
1037/*
1038 * validate_super for 0.90.0
1039 */
NeilBrownfd01b882011-10-11 16:47:53 +11001040static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001043 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001044 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
NeilBrown41158c72005-06-21 17:17:25 -07001046 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001047 clear_bit(Faulty, &rdev->flags);
1048 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001049 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001050 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (mddev->raid_disks == 0) {
1053 mddev->major_version = 0;
1054 mddev->minor_version = sb->minor_version;
1055 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001056 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001057 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 mddev->ctime = sb->ctime;
1059 mddev->utime = sb->utime;
1060 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001061 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 mddev->layout = sb->layout;
1063 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001064 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001065 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001066 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001067 mddev->bitmap_info.space = 0;
1068 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001069 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001070 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001071 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
NeilBrownf6705572006-03-27 01:18:11 -08001073 if (mddev->minor_version >= 91) {
1074 mddev->reshape_position = sb->reshape_position;
1075 mddev->delta_disks = sb->delta_disks;
1076 mddev->new_level = sb->new_level;
1077 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001078 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001079 if (mddev->delta_disks < 0)
1080 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001081 } else {
1082 mddev->reshape_position = MaxSector;
1083 mddev->delta_disks = 0;
1084 mddev->new_level = mddev->level;
1085 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001086 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001087 }
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (sb->state & (1<<MD_SB_CLEAN))
1090 mddev->recovery_cp = MaxSector;
1091 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001092 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 sb->events_lo == sb->cp_events_lo) {
1094 mddev->recovery_cp = sb->recovery_cp;
1095 } else
1096 mddev->recovery_cp = 0;
1097 }
1098
1099 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1100 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1101 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1102 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1103
1104 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001105
1106 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001107 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001108 mddev->bitmap_info.offset =
1109 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001110 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001111 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001112 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001113
NeilBrown41158c72005-06-21 17:17:25 -07001114 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001115 /* Insist on good event counter while assembling, except
1116 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001118 if (sb->disks[rdev->desc_nr].state & (
1119 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001120 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001121 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001122 } else if (mddev->bitmap) {
1123 /* if adding to array with a bitmap, then we can accept an
1124 * older device ... but not too old.
1125 */
NeilBrown41158c72005-06-21 17:17:25 -07001126 if (ev1 < mddev->bitmap->events_cleared)
1127 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001128 if (ev1 < mddev->events)
1129 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001130 } else {
1131 if (ev1 < mddev->events)
1132 /* just a hot-add of a new device, leave raid_disk at -1 */
1133 return 0;
1134 }
NeilBrown41158c72005-06-21 17:17:25 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 desc = sb->disks + rdev->desc_nr;
1138
1139 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001140 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001141 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1142 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001143 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001145 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001146 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1147 /* active but not in sync implies recovery up to
1148 * reshape position. We don't know exactly where
1149 * that is, so set to zero for now */
1150 if (mddev->minor_version >= 91) {
1151 rdev->recovery_offset = 0;
1152 rdev->raid_disk = desc->raid_disk;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001155 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1156 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001157 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001158 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return 0;
1160}
1161
1162/*
1163 * sync_super for 0.90.0
1164 */
NeilBrownfd01b882011-10-11 16:47:53 +11001165static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001168 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /* make rdev->sb match mddev data..
1172 *
1173 * 1/ zero out disks
1174 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1175 * 3/ any empty disks < next_spare become removed
1176 *
1177 * disks[0] gets initialised to REMOVED because
1178 * we cannot be sure from other fields if it has
1179 * been initialised or not.
1180 */
1181 int i;
1182 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1183
NeilBrown61181562005-09-09 16:24:02 -07001184 rdev->sb_size = MD_SB_BYTES;
1185
Namhyung Kim65a06f062011-07-27 11:00:36 +10001186 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 memset(sb, 0, sizeof(*sb));
1189
1190 sb->md_magic = MD_SB_MAGIC;
1191 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 sb->patch_version = mddev->patch_version;
1193 sb->gvalid_words = 0; /* ignored */
1194 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1195 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1196 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1197 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1198
1199 sb->ctime = mddev->ctime;
1200 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001201 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 sb->raid_disks = mddev->raid_disks;
1203 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001204 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 sb->utime = mddev->utime;
1206 sb->state = 0;
1207 sb->events_hi = (mddev->events>>32);
1208 sb->events_lo = (u32)mddev->events;
1209
NeilBrownf6705572006-03-27 01:18:11 -08001210 if (mddev->reshape_position == MaxSector)
1211 sb->minor_version = 90;
1212 else {
1213 sb->minor_version = 91;
1214 sb->reshape_position = mddev->reshape_position;
1215 sb->new_level = mddev->new_level;
1216 sb->delta_disks = mddev->delta_disks;
1217 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001218 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001219 }
1220 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (mddev->in_sync)
1222 {
1223 sb->recovery_cp = mddev->recovery_cp;
1224 sb->cp_events_hi = (mddev->events>>32);
1225 sb->cp_events_lo = (u32)mddev->events;
1226 if (mddev->recovery_cp == MaxSector)
1227 sb->state = (1<< MD_SB_CLEAN);
1228 } else
1229 sb->recovery_cp = 0;
1230
1231 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001232 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
NeilBrownc3d97142009-12-14 12:49:52 +11001234 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001235 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001238 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001240 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001241 int is_active = test_bit(In_sync, &rdev2->flags);
1242
1243 if (rdev2->raid_disk >= 0 &&
1244 sb->minor_version >= 91)
1245 /* we have nowhere to store the recovery_offset,
1246 * but if it is not below the reshape_position,
1247 * we can piggy-back on that.
1248 */
1249 is_active = 1;
1250 if (rdev2->raid_disk < 0 ||
1251 test_bit(Faulty, &rdev2->flags))
1252 is_active = 0;
1253 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001254 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001256 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001257 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 d = &sb->disks[rdev2->desc_nr];
1259 nr_disks++;
1260 d->number = rdev2->desc_nr;
1261 d->major = MAJOR(rdev2->bdev->bd_dev);
1262 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001263 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 d->raid_disk = rdev2->raid_disk;
1265 else
1266 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001267 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001269 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001271 if (test_bit(In_sync, &rdev2->flags))
1272 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 active++;
1274 working++;
1275 } else {
1276 d->state = 0;
1277 spare++;
1278 working++;
1279 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001280 if (test_bit(WriteMostly, &rdev2->flags))
1281 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* now set the "removed" and "faulty" bits on any missing devices */
1284 for (i=0 ; i < mddev->raid_disks ; i++) {
1285 mdp_disk_t *d = &sb->disks[i];
1286 if (d->state == 0 && d->number == 0) {
1287 d->number = i;
1288 d->raid_disk = i;
1289 d->state = (1<<MD_DISK_REMOVED);
1290 d->state |= (1<<MD_DISK_FAULTY);
1291 failed++;
1292 }
1293 }
1294 sb->nr_disks = nr_disks;
1295 sb->active_disks = active;
1296 sb->working_disks = working;
1297 sb->failed_disks = failed;
1298 sb->spare_disks = spare;
1299
1300 sb->this_disk = sb->disks[rdev->desc_nr];
1301 sb->sb_csum = calc_sb_csum(sb);
1302}
1303
1304/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001305 * rdev_size_change for 0.90.0
1306 */
1307static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001308super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001309{
Andre Noll58c0fed2009-03-31 14:33:13 +11001310 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001311 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001312 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001313 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001314 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001315 if (!num_sectors || num_sectors > rdev->sb_start)
1316 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001317 /* Limit to 4TB as metadata cannot record more than that.
1318 * 4TB == 2^32 KB, or 2*2^32 sectors.
1319 */
NeilBrown667a5312012-08-16 16:46:12 +10001320 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001321 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001322 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001323 rdev->sb_page);
1324 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001325 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001326}
1327
NeilBrownc6563a82012-05-21 09:27:00 +10001328static int
1329super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1330{
1331 /* non-zero offset changes not possible with v0.90 */
1332 return new_offset == 0;
1333}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001334
1335/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * version 1 superblock
1337 */
1338
NeilBrownf72ffdd2014-09-30 14:23:59 +10001339static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001341 __le32 disk_csum;
1342 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 unsigned long long newcsum;
1344 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001345 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 disk_csum = sb->sb_csum;
1348 sb->sb_csum = 0;
1349 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001350 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 newcsum += le32_to_cpu(*isuper++);
1352
1353 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001354 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1357 sb->sb_csum = disk_csum;
1358 return cpu_to_le32(csum);
1359}
1360
NeilBrown2699b672011-07-28 11:31:47 +10001361static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1362 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001363static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct mdp_superblock_1 *sb;
1366 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001367 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001368 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001370 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /*
Andre Noll0f420352008-07-11 22:02:23 +10001373 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * It is always aligned to a 4K boundary and
1375 * depeding on minor_version, it can be:
1376 * 0: At least 8K, but less than 12K, from end of device
1377 * 1: At start of device
1378 * 2: 4K from start of device.
1379 */
1380 switch(minor_version) {
1381 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001382 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001383 sb_start -= 8*2;
1384 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 break;
1386 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001387 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001390 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 break;
1392 default:
1393 return -EINVAL;
1394 }
Andre Noll0f420352008-07-11 22:02:23 +10001395 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
NeilBrown0002b272005-09-09 16:23:53 -07001397 /* superblock is rarely larger than 1K, but it can be larger,
1398 * and it is safe to read 4k, so we do that
1399 */
1400 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (ret) return ret;
1402
Namhyung Kim65a06f062011-07-27 11:00:36 +10001403 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1406 sb->major_version != cpu_to_le32(1) ||
1407 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001408 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001409 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -EINVAL;
1411
1412 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1413 printk("md: invalid superblock checksum on %s\n",
1414 bdevname(rdev->bdev,b));
1415 return -EINVAL;
1416 }
1417 if (le64_to_cpu(sb->data_size) < 10) {
1418 printk("md: data_size too small on %s\n",
1419 bdevname(rdev->bdev,b));
1420 return -EINVAL;
1421 }
NeilBrownc6563a82012-05-21 09:27:00 +10001422 if (sb->pad0 ||
1423 sb->pad3[0] ||
1424 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1425 /* Some padding is non-zero, might be a new feature */
1426 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 rdev->preferred_minor = 0xffff;
1429 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001430 rdev->new_data_offset = rdev->data_offset;
1431 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1432 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1433 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001434 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
NeilBrown0002b272005-09-09 16:23:53 -07001436 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001437 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001438 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001439 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1440
1441 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001442 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001443 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001444 if (minor_version
1445 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1446 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001447
NeilBrown31b65a02006-07-10 04:44:14 -07001448 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1449 rdev->desc_nr = -1;
1450 else
1451 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1452
NeilBrown2699b672011-07-28 11:31:47 +10001453 if (!rdev->bb_page) {
1454 rdev->bb_page = alloc_page(GFP_KERNEL);
1455 if (!rdev->bb_page)
1456 return -ENOMEM;
1457 }
1458 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1459 rdev->badblocks.count == 0) {
1460 /* need to load the bad block list.
1461 * Currently we limit it to one page.
1462 */
1463 s32 offset;
1464 sector_t bb_sector;
1465 u64 *bbp;
1466 int i;
1467 int sectors = le16_to_cpu(sb->bblog_size);
1468 if (sectors > (PAGE_SIZE / 512))
1469 return -EINVAL;
1470 offset = le32_to_cpu(sb->bblog_offset);
1471 if (offset == 0)
1472 return -EINVAL;
1473 bb_sector = (long long)offset;
1474 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1475 rdev->bb_page, READ, true))
1476 return -EIO;
1477 bbp = (u64 *)page_address(rdev->bb_page);
1478 rdev->badblocks.shift = sb->bblog_shift;
1479 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1480 u64 bb = le64_to_cpu(*bbp);
1481 int count = bb & (0x3ff);
1482 u64 sector = bb >> 10;
1483 sector <<= sb->bblog_shift;
1484 count <<= sb->bblog_shift;
1485 if (bb + 1 == 0)
1486 break;
1487 if (md_set_badblocks(&rdev->badblocks,
1488 sector, count, 1) == 0)
1489 return -EINVAL;
1490 }
NeilBrown486adf72013-04-24 11:42:44 +10001491 } else if (sb->bblog_offset != 0)
1492 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001493
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001494 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001495 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001496 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001498 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1501 sb->level != refsb->level ||
1502 sb->layout != refsb->layout ||
1503 sb->chunksize != refsb->chunksize) {
1504 printk(KERN_WARNING "md: %s has strangely different"
1505 " superblock to %s\n",
1506 bdevname(rdev->bdev,b),
1507 bdevname(refdev->bdev,b2));
1508 return -EINVAL;
1509 }
1510 ev1 = le64_to_cpu(sb->events);
1511 ev2 = le64_to_cpu(refsb->events);
1512
1513 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001514 ret = 1;
1515 else
1516 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
NeilBrownc6563a82012-05-21 09:27:00 +10001518 if (minor_version) {
1519 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1520 sectors -= rdev->data_offset;
1521 } else
1522 sectors = rdev->sb_start;
1523 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001525 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001526 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
NeilBrownfd01b882011-10-11 16:47:53 +11001529static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001531 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001532 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
NeilBrown41158c72005-06-21 17:17:25 -07001534 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001535 clear_bit(Faulty, &rdev->flags);
1536 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001537 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001538 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (mddev->raid_disks == 0) {
1541 mddev->major_version = 1;
1542 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001543 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001544 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1546 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1547 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001548 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 mddev->layout = le32_to_cpu(sb->layout);
1550 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001551 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001552 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001553 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001554 mddev->bitmap_info.space = 0;
1555 /* Default location for bitmap is 1K after superblock
1556 * using 3K - total of 4K
1557 */
NeilBrownc3d97142009-12-14 12:49:52 +11001558 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001559 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001560 mddev->reshape_backwards = 0;
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1563 memcpy(mddev->uuid, sb->set_uuid, 16);
1564
1565 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001566
NeilBrown71c08052005-09-09 16:23:51 -07001567 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001568 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001569 mddev->bitmap_info.offset =
1570 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001571 /* Metadata doesn't record how much space is available.
1572 * For 1.0, we assume we can use up to the superblock
1573 * if before, else to 4K beyond superblock.
1574 * For others, assume no change is possible.
1575 */
1576 if (mddev->minor_version > 0)
1577 mddev->bitmap_info.space = 0;
1578 else if (mddev->bitmap_info.offset > 0)
1579 mddev->bitmap_info.space =
1580 8 - mddev->bitmap_info.offset;
1581 else
1582 mddev->bitmap_info.space =
1583 -mddev->bitmap_info.offset;
1584 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001585
NeilBrownf6705572006-03-27 01:18:11 -08001586 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1587 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1588 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1589 mddev->new_level = le32_to_cpu(sb->new_level);
1590 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001591 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001592 if (mddev->delta_disks < 0 ||
1593 (mddev->delta_disks == 0 &&
1594 (le32_to_cpu(sb->feature_map)
1595 & MD_FEATURE_RESHAPE_BACKWARDS)))
1596 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001597 } else {
1598 mddev->reshape_position = MaxSector;
1599 mddev->delta_disks = 0;
1600 mddev->new_level = mddev->level;
1601 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001602 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001603 }
1604
NeilBrown41158c72005-06-21 17:17:25 -07001605 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001606 /* Insist of good event counter while assembling, except for
1607 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001609 if (rdev->desc_nr >= 0 &&
1610 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1611 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1612 if (ev1 < mddev->events)
1613 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001614 } else if (mddev->bitmap) {
1615 /* If adding to array with a bitmap, then we can accept an
1616 * older device, but not too old.
1617 */
NeilBrown41158c72005-06-21 17:17:25 -07001618 if (ev1 < mddev->bitmap->events_cleared)
1619 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001620 if (ev1 < mddev->events)
1621 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001622 } else {
1623 if (ev1 < mddev->events)
1624 /* just a hot-add of a new device, leave raid_disk at -1 */
1625 return 0;
1626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (mddev->level != LEVEL_MULTIPATH) {
1628 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001629 if (rdev->desc_nr < 0 ||
1630 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1631 role = 0xffff;
1632 rdev->desc_nr = -1;
1633 } else
1634 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 switch(role) {
1636 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 break;
1638 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001639 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641 default:
NeilBrownf4667222013-12-09 12:04:56 +11001642 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001643 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001644 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001645 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001646 if (!(le32_to_cpu(sb->feature_map) &
1647 MD_FEATURE_RECOVERY_BITMAP))
1648 rdev->saved_raid_disk = -1;
1649 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001650 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 rdev->raid_disk = role;
1652 break;
1653 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001654 if (sb->devflags & WriteMostly1)
1655 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001656 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1657 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001658 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001659 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return 0;
1662}
1663
NeilBrownfd01b882011-10-11 16:47:53 +11001664static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001667 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int max_dev, i;
1669 /* make rdev->sb match mddev and rdev data. */
1670
Namhyung Kim65a06f062011-07-27 11:00:36 +10001671 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 sb->feature_map = 0;
1674 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001675 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 memset(sb->pad3, 0, sizeof(sb->pad3));
1677
1678 sb->utime = cpu_to_le64((__u64)mddev->utime);
1679 sb->events = cpu_to_le64(mddev->events);
1680 if (mddev->in_sync)
1681 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1682 else
1683 sb->resync_offset = cpu_to_le64(0);
1684
NeilBrown1c05b4b2006-10-21 10:24:08 -07001685 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001686
NeilBrownf0ca3402006-02-02 14:28:04 -08001687 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001688 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001689 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001690 sb->level = cpu_to_le32(mddev->level);
1691 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001692
NeilBrownaeb9b212011-08-25 14:43:08 +10001693 if (test_bit(WriteMostly, &rdev->flags))
1694 sb->devflags |= WriteMostly1;
1695 else
1696 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001697 sb->data_offset = cpu_to_le64(rdev->data_offset);
1698 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001699
NeilBrownc3d97142009-12-14 12:49:52 +11001700 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1701 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001702 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001703 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001704
1705 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001706 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001707 sb->feature_map |=
1708 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1709 sb->recovery_offset =
1710 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001711 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1712 sb->feature_map |=
1713 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001714 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001715 if (test_bit(Replacement, &rdev->flags))
1716 sb->feature_map |=
1717 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001718
NeilBrownf6705572006-03-27 01:18:11 -08001719 if (mddev->reshape_position != MaxSector) {
1720 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1721 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1722 sb->new_layout = cpu_to_le32(mddev->new_layout);
1723 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1724 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001725 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001726 if (mddev->delta_disks == 0 &&
1727 mddev->reshape_backwards)
1728 sb->feature_map
1729 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001730 if (rdev->new_data_offset != rdev->data_offset) {
1731 sb->feature_map
1732 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1733 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1734 - rdev->data_offset));
1735 }
NeilBrownf6705572006-03-27 01:18:11 -08001736 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001737
NeilBrown2699b672011-07-28 11:31:47 +10001738 if (rdev->badblocks.count == 0)
1739 /* Nothing to do for bad blocks*/ ;
1740 else if (sb->bblog_offset == 0)
1741 /* Cannot record bad blocks on this device */
1742 md_error(mddev, rdev);
1743 else {
1744 struct badblocks *bb = &rdev->badblocks;
1745 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1746 u64 *p = bb->page;
1747 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1748 if (bb->changed) {
1749 unsigned seq;
1750
1751retry:
1752 seq = read_seqbegin(&bb->lock);
1753
1754 memset(bbp, 0xff, PAGE_SIZE);
1755
1756 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001757 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001758 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1759 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001760 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001761 }
NeilBrownd0962932012-03-19 12:46:41 +11001762 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001763 if (read_seqretry(&bb->lock, seq))
1764 goto retry;
1765
1766 bb->sector = (rdev->sb_start +
1767 (int)le32_to_cpu(sb->bblog_offset));
1768 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001769 }
1770 }
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001773 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (rdev2->desc_nr+1 > max_dev)
1775 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001776
NeilBrown70471da2009-08-03 10:59:57 +10001777 if (max_dev > le32_to_cpu(sb->max_dev)) {
1778 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001779 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001780 rdev->sb_size = max_dev * 2 + 256;
1781 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1782 if (rdev->sb_size & bmask)
1783 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001784 } else
1785 max_dev = le32_to_cpu(sb->max_dev);
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 for (i=0; i<max_dev;i++)
1788 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001789
NeilBrowndafb20f2012-03-19 12:46:39 +11001790 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001792 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001794 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001796 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001797 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 else
1799 sb->dev_roles[i] = cpu_to_le16(0xffff);
1800 }
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 sb->sb_csum = calc_sb_1_csum(sb);
1803}
1804
Chris Webb0cd17fe2008-06-28 08:31:46 +10001805static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001806super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001807{
1808 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001809 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001810 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001811 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001812 if (rdev->data_offset != rdev->new_data_offset)
1813 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001814 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001815 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001816 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001817 max_sectors -= rdev->data_offset;
1818 if (!num_sectors || num_sectors > max_sectors)
1819 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001820 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001821 /* minor version 0 with bitmap we can't move */
1822 return 0;
1823 } else {
1824 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001825 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001826 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001827 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001828 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001829 if (!num_sectors || num_sectors > max_sectors)
1830 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001831 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001832 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001833 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001834 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001835 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001836 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001837 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001838 rdev->sb_page);
1839 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001840 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001841
1842}
1843
1844static int
1845super_1_allow_new_offset(struct md_rdev *rdev,
1846 unsigned long long new_offset)
1847{
1848 /* All necessary checks on new >= old have been done */
1849 struct bitmap *bitmap;
1850 if (new_offset >= rdev->data_offset)
1851 return 1;
1852
1853 /* with 1.0 metadata, there is no metadata to tread on
1854 * so we can always move back */
1855 if (rdev->mddev->minor_version == 0)
1856 return 1;
1857
1858 /* otherwise we must be sure not to step on
1859 * any metadata, so stay:
1860 * 36K beyond start of superblock
1861 * beyond end of badblocks
1862 * beyond write-intent bitmap
1863 */
1864 if (rdev->sb_start + (32+4)*2 > new_offset)
1865 return 0;
1866 bitmap = rdev->mddev->bitmap;
1867 if (bitmap && !rdev->mddev->bitmap_info.file &&
1868 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001869 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001870 return 0;
1871 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1872 return 0;
1873
1874 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001875}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Adrian Bunk75c96f82005-05-05 16:16:09 -07001877static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 [0] = {
1879 .name = "0.90.0",
1880 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881 .load_super = super_90_load,
1882 .validate_super = super_90_validate,
1883 .sync_super = super_90_sync,
1884 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001885 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 },
1887 [1] = {
1888 .name = "md-1",
1889 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001890 .load_super = super_1_load,
1891 .validate_super = super_1_validate,
1892 .sync_super = super_1_sync,
1893 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001894 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 },
1896};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
NeilBrownfd01b882011-10-11 16:47:53 +11001898static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001899{
1900 if (mddev->sync_super) {
1901 mddev->sync_super(mddev, rdev);
1902 return;
1903 }
1904
1905 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1906
1907 super_types[mddev->major_version].sync_super(mddev, rdev);
1908}
1909
NeilBrownfd01b882011-10-11 16:47:53 +11001910static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
NeilBrown3cb03002011-10-11 16:45:26 +11001912 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
NeilBrown4b809912008-07-21 17:05:25 +10001914 rcu_read_lock();
1915 rdev_for_each_rcu(rdev, mddev1)
1916 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001917 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001918 rdev2->bdev->bd_contains) {
1919 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001920 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001921 }
1922 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return 0;
1924}
1925
1926static LIST_HEAD(pending_raid_disks);
1927
Andre Nollac5e7112009-08-03 10:59:47 +10001928/*
1929 * Try to register data integrity profile for an mddev
1930 *
1931 * This is called when an array is started and after a disk has been kicked
1932 * from the array. It only succeeds if all working and active component devices
1933 * are integrity capable with matching profiles.
1934 */
NeilBrownfd01b882011-10-11 16:47:53 +11001935int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001936{
NeilBrown3cb03002011-10-11 16:45:26 +11001937 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001938
Andre Nollac5e7112009-08-03 10:59:47 +10001939 if (list_empty(&mddev->disks))
1940 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001941 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1942 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001943 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001944 /* skip spares and non-functional disks */
1945 if (test_bit(Faulty, &rdev->flags))
1946 continue;
1947 if (rdev->raid_disk < 0)
1948 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001949 if (!reference) {
1950 /* Use the first rdev as the reference */
1951 reference = rdev;
1952 continue;
1953 }
1954 /* does this rdev's profile match the reference profile? */
1955 if (blk_integrity_compare(reference->bdev->bd_disk,
1956 rdev->bdev->bd_disk) < 0)
1957 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001958 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001959 if (!reference || !bdev_get_integrity(reference->bdev))
1960 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001961 /*
1962 * All component devices are integrity capable and have matching
1963 * profiles, register the common profile for the md device.
1964 */
1965 if (blk_integrity_register(mddev->gendisk,
1966 bdev_get_integrity(reference->bdev)) != 0) {
1967 printk(KERN_ERR "md: failed to register integrity for %s\n",
1968 mdname(mddev));
1969 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001970 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001971 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1972 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1973 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1974 mdname(mddev));
1975 return -EINVAL;
1976 }
Andre Nollac5e7112009-08-03 10:59:47 +10001977 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001978}
Andre Nollac5e7112009-08-03 10:59:47 +10001979EXPORT_SYMBOL(md_integrity_register);
1980
1981/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001982void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001983{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001984 struct blk_integrity *bi_rdev;
1985 struct blk_integrity *bi_mddev;
1986
1987 if (!mddev->gendisk)
1988 return;
1989
1990 bi_rdev = bdev_get_integrity(rdev->bdev);
1991 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10001992
1993 if (!bi_mddev) /* nothing to do */
1994 return;
1995 if (rdev->raid_disk < 0) /* skip spares */
1996 return;
1997 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1998 rdev->bdev->bd_disk) >= 0)
1999 return;
2000 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2001 blk_integrity_unregister(mddev->gendisk);
2002}
2003EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002004
NeilBrownf72ffdd2014-09-30 14:23:59 +10002005static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002007 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002008 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002009 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Dan Williams11e2ede2008-04-30 00:52:32 -07002011 /* prevent duplicates */
2012 if (find_rdev(mddev, rdev->bdev->bd_dev))
2013 return -EEXIST;
2014
Andre Nolldd8ac332009-03-31 14:33:13 +11002015 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2016 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2017 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002018 if (mddev->pers) {
2019 /* Cannot change size, so fail
2020 * If mddev->level <= 0, then we don't care
2021 * about aligning sizes (e.g. linear)
2022 */
2023 if (mddev->level > 0)
2024 return -ENOSPC;
2025 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002026 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 /* Verify rdev->desc_nr is unique.
2030 * If it is -1, assign a free number, else
2031 * check number is not in use
2032 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002033 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (rdev->desc_nr < 0) {
2035 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002036 if (mddev->pers)
2037 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002038 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 choice++;
2040 rdev->desc_nr = choice;
2041 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002042 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002043 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002047 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002048 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2049 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2050 mdname(mddev), mddev->max_disks);
2051 return -EBUSY;
2052 }
NeilBrown19133a42005-11-08 21:39:35 -08002053 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002054 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002057 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002058
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002059 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002060 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002061
Tejun Heo0762b8b2008-08-25 19:56:12 +09002062 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002063 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2064 /* failure here is OK */;
2065 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002066
NeilBrown4b809912008-07-21 17:05:25 +10002067 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002068 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002069
2070 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002071 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002074
2075 fail:
2076 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2077 b, mdname(mddev));
2078 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
NeilBrown177a99b2008-02-06 01:39:56 -08002081static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002082{
NeilBrown3cb03002011-10-11 16:45:26 +11002083 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002084 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002085 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002086}
2087
NeilBrownf72ffdd2014-09-30 14:23:59 +10002088static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
2090 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002091
Tejun Heo49731ba2011-01-14 18:43:57 +01002092 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002093 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2095 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002096 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002097 sysfs_put(rdev->sysfs_state);
2098 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002099 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002100 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002101 * writing to 'remove' to "dev/state". We also need
2102 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002103 */
NeilBrown4b809912008-07-21 17:05:25 +10002104 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002105 INIT_WORK(&rdev->del_work, md_delayed_delete);
2106 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002107 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
2110/*
2111 * prevent the device from being mounted, repartitioned or
2112 * otherwise reused by a RAID array (or any other kernel
2113 * subsystem), by bd_claiming the device.
2114 */
NeilBrown3cb03002011-10-11 16:45:26 +11002115static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
2117 int err = 0;
2118 struct block_device *bdev;
2119 char b[BDEVNAME_SIZE];
2120
Tejun Heod4d77622010-11-13 11:55:18 +01002121 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002122 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (IS_ERR(bdev)) {
2124 printk(KERN_ERR "md: could not open %s.\n",
2125 __bdevname(dev, b));
2126 return PTR_ERR(bdev);
2127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 rdev->bdev = bdev;
2129 return err;
2130}
2131
NeilBrown3cb03002011-10-11 16:45:26 +11002132static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 struct block_device *bdev = rdev->bdev;
2135 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002136 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
2139void md_autodetect_dev(dev_t dev);
2140
NeilBrownf72ffdd2014-09-30 14:23:59 +10002141static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 printk(KERN_INFO "md: export_rdev(%s)\n",
2146 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002147 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002149 if (test_bit(AutoDetected, &rdev->flags))
2150 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151#endif
2152 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002153 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002156void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 unbind_rdev_from_array(rdev);
2159 export_rdev(rdev);
2160}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002161EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
NeilBrownfd01b882011-10-11 16:47:53 +11002163static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
NeilBrown0638bb02014-09-25 17:43:47 +10002165 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
NeilBrown0638bb02014-09-25 17:43:47 +10002167 while (!list_empty(&mddev->disks)) {
2168 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2169 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002170 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 mddev->raid_disks = 0;
2173 mddev->major_version = 0;
2174}
2175
NeilBrownf72ffdd2014-09-30 14:23:59 +10002176static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
NeilBrown42543762006-06-26 00:27:57 -07002178 /* Update each superblock (in-memory image), but
2179 * if we are allowed to, skip spares which already
2180 * have the right event counter, or have one earlier
2181 * (which would mean they aren't being marked as dirty
2182 * with the rest of the array)
2183 */
NeilBrown3cb03002011-10-11 16:45:26 +11002184 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002185 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002186 if (rdev->sb_events == mddev->events ||
2187 (nospares &&
2188 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002189 rdev->sb_events+1 == mddev->events)) {
2190 /* Don't update this superblock */
2191 rdev->sb_loaded = 2;
2192 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002193 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002194 rdev->sb_loaded = 1;
2195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197}
2198
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002199void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
NeilBrown3cb03002011-10-11 16:45:26 +11002201 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002202 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002203 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002204 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
NeilBrownd87f0642013-04-24 11:42:40 +10002206 if (mddev->ro) {
2207 if (force_change)
2208 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2209 return;
2210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002212 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002213 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002214 if (rdev->raid_disk >= 0 &&
2215 mddev->delta_disks >= 0 &&
2216 !test_bit(In_sync, &rdev->flags) &&
2217 mddev->curr_resync_completed > rdev->recovery_offset)
2218 rdev->recovery_offset = mddev->curr_resync_completed;
2219
NeilBrownf72ffdd2014-09-30 14:23:59 +10002220 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002221 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002222 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002223 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002224 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002225 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002226 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002227 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002228 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002229 md_ack_all_badblocks(&rdev->badblocks);
2230 md_error(mddev, rdev);
2231 }
2232 clear_bit(Blocked, &rdev->flags);
2233 clear_bit(BlockedBadBlocks, &rdev->flags);
2234 wake_up(&rdev->blocked_wait);
2235 }
2236 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002237 wake_up(&mddev->sb_wait);
2238 return;
2239 }
2240
NeilBrown85572d72014-12-15 12:56:56 +11002241 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002242
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002243 mddev->utime = get_seconds();
2244
NeilBrown850b2b42006-10-03 01:15:46 -07002245 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2246 force_change = 1;
2247 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2248 /* just a clean<-> dirty transition, possibly leave spares alone,
2249 * though if events isn't the right even/odd, we will have to do
2250 * spares after all
2251 */
2252 nospares = 1;
2253 if (force_change)
2254 nospares = 0;
2255 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002256 /* If the array is degraded, then skipping spares is both
2257 * dangerous and fairly pointless.
2258 * Dangerous because a device that was removed from the array
2259 * might have a event_count that still looks up-to-date,
2260 * so it can be re-added without a resync.
2261 * Pointless because if there are any spares to skip,
2262 * then a recovery will happen and soon that array won't
2263 * be degraded any more and the spare can go back to sleep then.
2264 */
NeilBrown850b2b42006-10-03 01:15:46 -07002265 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002266
NeilBrown06d91a52005-06-21 17:17:12 -07002267 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002268
2269 /* If this is just a dirty<->clean transition, and the array is clean
2270 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002271 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002272 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002273 && mddev->can_decrease_events
2274 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002275 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002276 mddev->can_decrease_events = 0;
2277 } else {
NeilBrown42543762006-06-26 00:27:57 -07002278 /* otherwise we have to go forward and ... */
2279 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002280 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
NeilBrown403df472014-09-30 15:52:29 +10002283 /*
2284 * This 64-bit counter should never wrap.
2285 * Either we are in around ~1 trillion A.C., assuming
2286 * 1 reboot per second, or we have a bug...
2287 */
2288 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002289
NeilBrowndafb20f2012-03-19 12:46:39 +11002290 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002291 if (rdev->badblocks.changed)
2292 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002293 if (test_bit(Faulty, &rdev->flags))
2294 set_bit(FaultRecorded, &rdev->flags);
2295 }
NeilBrown2699b672011-07-28 11:31:47 +10002296
NeilBrowne6910632008-02-06 01:39:51 -08002297 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002298 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
NeilBrown36a4e1f2011-10-07 14:23:17 +11002300 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2301 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
NeilBrown4ad13662007-07-17 04:06:13 -07002303 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002304 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002306
NeilBrown42543762006-06-26 00:27:57 -07002307 if (rdev->sb_loaded != 1)
2308 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
NeilBrownf4667222013-12-09 12:04:56 +11002310 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002311 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002312 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002313 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002314 pr_debug("md: (write) %s's sb offset: %llu\n",
2315 bdevname(rdev->bdev, b),
2316 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002317 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002318 if (rdev->badblocks.size) {
2319 md_super_write(mddev, rdev,
2320 rdev->badblocks.sector,
2321 rdev->badblocks.size << 9,
2322 rdev->bb_page);
2323 rdev->badblocks.size = 0;
2324 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002325
NeilBrownf4667222013-12-09 12:04:56 +11002326 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002327 pr_debug("md: %s (skipping faulty)\n",
2328 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002329
NeilBrown7bfa19f2005-06-21 17:17:28 -07002330 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 /* only need to write one superblock... */
2332 break;
2333 }
NeilBrowna9701a32005-11-08 21:39:34 -08002334 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002335 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002336
NeilBrown85572d72014-12-15 12:56:56 +11002337 spin_lock(&mddev->lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002338 if (mddev->in_sync != sync_req ||
2339 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002340 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002341 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002342 goto repeat;
2343 }
NeilBrown850b2b42006-10-03 01:15:46 -07002344 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002345 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002346 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002347 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2348 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002349
NeilBrowndafb20f2012-03-19 12:46:39 +11002350 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002351 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2352 clear_bit(Blocked, &rdev->flags);
2353
2354 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002355 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002356 clear_bit(BlockedBadBlocks, &rdev->flags);
2357 wake_up(&rdev->blocked_wait);
2358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002360EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002362static int add_bound_rdev(struct md_rdev *rdev)
2363{
2364 struct mddev *mddev = rdev->mddev;
2365 int err = 0;
2366
2367 if (!mddev->pers->hot_remove_disk) {
2368 /* If there is hot_add_disk but no hot_remove_disk
2369 * then added disks for geometry changes,
2370 * and should be added immediately.
2371 */
2372 super_types[mddev->major_version].
2373 validate_super(mddev, rdev);
2374 err = mddev->pers->hot_add_disk(mddev, rdev);
2375 if (err) {
2376 unbind_rdev_from_array(rdev);
2377 export_rdev(rdev);
2378 return err;
2379 }
2380 }
2381 sysfs_notify_dirent_safe(rdev->sysfs_state);
2382
2383 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2384 if (mddev->degraded)
2385 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2386 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2387 md_new_event(mddev);
2388 md_wakeup_thread(mddev->thread);
2389 return 0;
2390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Andre Noll7f6ce762008-03-23 18:34:54 +01002392/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002393 * We want to accept with case. For this we use cmd_match.
2394 */
2395static int cmd_match(const char *cmd, const char *str)
2396{
2397 /* See if cmd, written into a sysfs file, matches
2398 * str. They must either be the same, or cmd can
2399 * have a trailing newline
2400 */
2401 while (*cmd && *str && *cmd == *str) {
2402 cmd++;
2403 str++;
2404 }
2405 if (*cmd == '\n')
2406 cmd++;
2407 if (*str || *cmd)
2408 return 0;
2409 return 1;
2410}
2411
NeilBrown86e6ffd2005-11-08 21:39:24 -08002412struct rdev_sysfs_entry {
2413 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002414 ssize_t (*show)(struct md_rdev *, char *);
2415 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002416};
2417
2418static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002419state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002420{
2421 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002422 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002423 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002424
NeilBrown758bfc82014-12-15 12:56:59 +11002425 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002426 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002427 len+= sprintf(page+len, "%sfaulty",sep);
2428 sep = ",";
2429 }
NeilBrown758bfc82014-12-15 12:56:59 +11002430 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002431 len += sprintf(page+len, "%sin_sync",sep);
2432 sep = ",";
2433 }
NeilBrown758bfc82014-12-15 12:56:59 +11002434 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002435 len += sprintf(page+len, "%swrite_mostly",sep);
2436 sep = ",";
2437 }
NeilBrown758bfc82014-12-15 12:56:59 +11002438 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002439 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002440 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002441 len += sprintf(page+len, "%sblocked", sep);
2442 sep = ",";
2443 }
NeilBrown758bfc82014-12-15 12:56:59 +11002444 if (!test_bit(Faulty, &flags) &&
2445 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002446 len += sprintf(page+len, "%sspare", sep);
2447 sep = ",";
2448 }
NeilBrown758bfc82014-12-15 12:56:59 +11002449 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002450 len += sprintf(page+len, "%swrite_error", sep);
2451 sep = ",";
2452 }
NeilBrown758bfc82014-12-15 12:56:59 +11002453 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002454 len += sprintf(page+len, "%swant_replacement", sep);
2455 sep = ",";
2456 }
NeilBrown758bfc82014-12-15 12:56:59 +11002457 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002458 len += sprintf(page+len, "%sreplacement", sep);
2459 sep = ",";
2460 }
2461
NeilBrown86e6ffd2005-11-08 21:39:24 -08002462 return len+sprintf(page+len, "\n");
2463}
2464
NeilBrown45dc2de2006-06-26 00:27:58 -07002465static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002466state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002467{
2468 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002469 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002470 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002471 * writemostly - sets write_mostly
2472 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002473 * blocked - sets the Blocked flags
2474 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002475 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002476 * -insync - clear Insync for a device with a slot assigned,
2477 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002478 * write_error - sets WriteErrorSeen
2479 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002480 */
2481 int err = -EINVAL;
2482 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2483 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002484 if (test_bit(Faulty, &rdev->flags))
2485 err = 0;
2486 else
2487 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002488 } else if (cmd_match(buf, "remove")) {
2489 if (rdev->raid_disk >= 0)
2490 err = -EBUSY;
2491 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002492 struct mddev *mddev = rdev->mddev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002493 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002494 md_cluster_ops->remove_disk(mddev, rdev);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002495 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002496 if (mddev_is_clustered(mddev))
2497 md_cluster_ops->metadata_update_start(mddev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002498 if (mddev->pers)
2499 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002500 md_new_event(mddev);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002501 if (mddev_is_clustered(mddev))
2502 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown45dc2de2006-06-26 00:27:58 -07002503 err = 0;
2504 }
NeilBrownf6556752006-06-26 00:28:01 -07002505 } else if (cmd_match(buf, "writemostly")) {
2506 set_bit(WriteMostly, &rdev->flags);
2507 err = 0;
2508 } else if (cmd_match(buf, "-writemostly")) {
2509 clear_bit(WriteMostly, &rdev->flags);
2510 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002511 } else if (cmd_match(buf, "blocked")) {
2512 set_bit(Blocked, &rdev->flags);
2513 err = 0;
2514 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002515 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002516 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002517 /* metadata handler doesn't understand badblocks,
2518 * so we need to fail the device
2519 */
2520 md_error(rdev->mddev, rdev);
2521 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002522 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002523 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002524 wake_up(&rdev->blocked_wait);
2525 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2526 md_wakeup_thread(rdev->mddev->thread);
2527
2528 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002529 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2530 set_bit(In_sync, &rdev->flags);
2531 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002532 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002533 if (rdev->mddev->pers == NULL) {
2534 clear_bit(In_sync, &rdev->flags);
2535 rdev->saved_raid_disk = rdev->raid_disk;
2536 rdev->raid_disk = -1;
2537 err = 0;
2538 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002539 } else if (cmd_match(buf, "write_error")) {
2540 set_bit(WriteErrorSeen, &rdev->flags);
2541 err = 0;
2542 } else if (cmd_match(buf, "-write_error")) {
2543 clear_bit(WriteErrorSeen, &rdev->flags);
2544 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002545 } else if (cmd_match(buf, "want_replacement")) {
2546 /* Any non-spare device that is not a replacement can
2547 * become want_replacement at any time, but we then need to
2548 * check if recovery is needed.
2549 */
2550 if (rdev->raid_disk >= 0 &&
2551 !test_bit(Replacement, &rdev->flags))
2552 set_bit(WantReplacement, &rdev->flags);
2553 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2554 md_wakeup_thread(rdev->mddev->thread);
2555 err = 0;
2556 } else if (cmd_match(buf, "-want_replacement")) {
2557 /* Clearing 'want_replacement' is always allowed.
2558 * Once replacements starts it is too late though.
2559 */
2560 err = 0;
2561 clear_bit(WantReplacement, &rdev->flags);
2562 } else if (cmd_match(buf, "replacement")) {
2563 /* Can only set a device as a replacement when array has not
2564 * yet been started. Once running, replacement is automatic
2565 * from spares, or by assigning 'slot'.
2566 */
2567 if (rdev->mddev->pers)
2568 err = -EBUSY;
2569 else {
2570 set_bit(Replacement, &rdev->flags);
2571 err = 0;
2572 }
2573 } else if (cmd_match(buf, "-replacement")) {
2574 /* Similarly, can only clear Replacement before start */
2575 if (rdev->mddev->pers)
2576 err = -EBUSY;
2577 else {
2578 clear_bit(Replacement, &rdev->flags);
2579 err = 0;
2580 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002581 } else if (cmd_match(buf, "re-add")) {
2582 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002583 /* clear_bit is performed _after_ all the devices
2584 * have their local Faulty bit cleared. If any writes
2585 * happen in the meantime in the local node, they
2586 * will land in the local bitmap, which will be synced
2587 * by this node eventually
2588 */
2589 if (!mddev_is_clustered(rdev->mddev) ||
2590 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2591 clear_bit(Faulty, &rdev->flags);
2592 err = add_bound_rdev(rdev);
2593 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002594 } else
2595 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002596 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002597 if (!err)
2598 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002599 return err ? err : len;
2600}
NeilBrown80ca3a42006-07-10 04:44:18 -07002601static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002602__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002603
2604static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002605errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002606{
2607 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2608}
2609
2610static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002611errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002612{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002613 unsigned int n;
2614 int rv;
2615
2616 rv = kstrtouint(buf, 10, &n);
2617 if (rv < 0)
2618 return rv;
2619 atomic_set(&rdev->corrected_errors, n);
2620 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002621}
2622static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002623__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002624
NeilBrown014236d2006-01-06 00:20:55 -08002625static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002626slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002627{
2628 if (rdev->raid_disk < 0)
2629 return sprintf(page, "none\n");
2630 else
2631 return sprintf(page, "%d\n", rdev->raid_disk);
2632}
2633
2634static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002635slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002636{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002637 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002638 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002639
NeilBrown014236d2006-01-06 00:20:55 -08002640 if (strncmp(buf, "none", 4)==0)
2641 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002642 else {
2643 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2644 if (err < 0)
2645 return err;
2646 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002647 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002648 /* Setting 'slot' on an active array requires also
2649 * updating the 'rd%d' link, and communicating
2650 * with the personality with ->hot_*_disk.
2651 * For now we only support removing
2652 * failed/spare devices. This normally happens automatically,
2653 * but not when the metadata is externally managed.
2654 */
NeilBrownc303da62008-02-06 01:39:51 -08002655 if (rdev->raid_disk == -1)
2656 return -EEXIST;
2657 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002658 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002659 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002660 clear_bit(Blocked, &rdev->flags);
2661 remove_and_add_spares(rdev->mddev, rdev);
2662 if (rdev->raid_disk >= 0)
2663 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002664 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2665 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002666 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002667 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002668 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002669 */
2670
2671 if (rdev->raid_disk != -1)
2672 return -EBUSY;
2673
NeilBrownc6751b22011-02-02 11:57:13 +11002674 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2675 return -EBUSY;
2676
Neil Brown6c2fce22008-06-28 08:31:31 +10002677 if (rdev->mddev->pers->hot_add_disk == NULL)
2678 return -EINVAL;
2679
NeilBrownba1b41b2011-01-14 09:14:34 +11002680 if (slot >= rdev->mddev->raid_disks &&
2681 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2682 return -ENOSPC;
2683
Neil Brown6c2fce22008-06-28 08:31:31 +10002684 rdev->raid_disk = slot;
2685 if (test_bit(In_sync, &rdev->flags))
2686 rdev->saved_raid_disk = slot;
2687 else
2688 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002689 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002690 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002691 err = rdev->mddev->pers->
2692 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002693 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002694 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002695 return err;
Neil Brown52664732008-06-28 08:31:44 +10002696 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002697 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002698 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002699 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002700 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002701 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002702 if (slot >= rdev->mddev->raid_disks &&
2703 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002704 return -ENOSPC;
2705 rdev->raid_disk = slot;
2706 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002707 clear_bit(Faulty, &rdev->flags);
2708 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002709 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002710 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002711 }
NeilBrown014236d2006-01-06 00:20:55 -08002712 return len;
2713}
2714
NeilBrown014236d2006-01-06 00:20:55 -08002715static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002716__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002717
NeilBrown93c8cad2006-01-06 00:20:56 -08002718static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002719offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002720{
Andrew Morton6961ece2006-01-06 00:20:59 -08002721 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002722}
2723
2724static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002725offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002726{
NeilBrownc6563a82012-05-21 09:27:00 +10002727 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002728 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002729 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002730 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002731 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002732 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002733 /* Must set offset before size, so overlap checks
2734 * can be sane */
2735 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002736 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002737 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002738 return len;
2739}
2740
2741static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002742__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002743
NeilBrownc6563a82012-05-21 09:27:00 +10002744static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2745{
2746 return sprintf(page, "%llu\n",
2747 (unsigned long long)rdev->new_data_offset);
2748}
2749
2750static ssize_t new_offset_store(struct md_rdev *rdev,
2751 const char *buf, size_t len)
2752{
2753 unsigned long long new_offset;
2754 struct mddev *mddev = rdev->mddev;
2755
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002756 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002757 return -EINVAL;
2758
NeilBrownf851b602014-12-11 10:02:10 +11002759 if (mddev->sync_thread ||
2760 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002761 return -EBUSY;
2762 if (new_offset == rdev->data_offset)
2763 /* reset is always permitted */
2764 ;
2765 else if (new_offset > rdev->data_offset) {
2766 /* must not push array size beyond rdev_sectors */
2767 if (new_offset - rdev->data_offset
2768 + mddev->dev_sectors > rdev->sectors)
2769 return -E2BIG;
2770 }
2771 /* Metadata worries about other space details. */
2772
2773 /* decreasing the offset is inconsistent with a backwards
2774 * reshape.
2775 */
2776 if (new_offset < rdev->data_offset &&
2777 mddev->reshape_backwards)
2778 return -EINVAL;
2779 /* Increasing offset is inconsistent with forwards
2780 * reshape. reshape_direction should be set to
2781 * 'backwards' first.
2782 */
2783 if (new_offset > rdev->data_offset &&
2784 !mddev->reshape_backwards)
2785 return -EINVAL;
2786
2787 if (mddev->pers && mddev->persistent &&
2788 !super_types[mddev->major_version]
2789 .allow_new_offset(rdev, new_offset))
2790 return -E2BIG;
2791 rdev->new_data_offset = new_offset;
2792 if (new_offset > rdev->data_offset)
2793 mddev->reshape_backwards = 1;
2794 else if (new_offset < rdev->data_offset)
2795 mddev->reshape_backwards = 0;
2796
2797 return len;
2798}
2799static struct rdev_sysfs_entry rdev_new_offset =
2800__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2801
NeilBrown83303b62006-01-06 00:21:06 -08002802static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002803rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002804{
Andre Nolldd8ac332009-03-31 14:33:13 +11002805 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002806}
2807
NeilBrownc5d79ad2008-02-06 01:39:54 -08002808static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2809{
2810 /* check if two start/length pairs overlap */
2811 if (s1+l1 <= s2)
2812 return 0;
2813 if (s2+l2 <= s1)
2814 return 0;
2815 return 1;
2816}
2817
Dan Williamsb522adc2009-03-31 15:00:31 +11002818static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2819{
2820 unsigned long long blocks;
2821 sector_t new;
2822
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002823 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002824 return -EINVAL;
2825
2826 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2827 return -EINVAL; /* sector conversion overflow */
2828
2829 new = blocks * 2;
2830 if (new != blocks * 2)
2831 return -EINVAL; /* unsigned long long to sector_t overflow */
2832
2833 *sectors = new;
2834 return 0;
2835}
2836
NeilBrown83303b62006-01-06 00:21:06 -08002837static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002838rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002839{
NeilBrownfd01b882011-10-11 16:47:53 +11002840 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002841 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002842 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002843
Dan Williamsb522adc2009-03-31 15:00:31 +11002844 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002845 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002846 if (rdev->data_offset != rdev->new_data_offset)
2847 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002848 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002849 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002850 sectors = super_types[my_mddev->major_version].
2851 rdev_size_change(rdev, sectors);
2852 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002853 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002854 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002855 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002856 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002857 if (!my_mddev->pers->resize)
2858 /* Cannot change size for RAID0 or Linear etc */
2859 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002860 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002861 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002862 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002863
Andre Nolldd8ac332009-03-31 14:33:13 +11002864 rdev->sectors = sectors;
2865 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002866 /* Need to check that all other rdevs with the same
2867 * ->bdev do not overlap. 'rcu' is sufficient to walk
2868 * the rdev lists safely.
2869 * This check does not provide a hard guarantee, it
2870 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002871 */
NeilBrownfd01b882011-10-11 16:47:53 +11002872 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002873 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002874 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002875
NeilBrown8b1afc32014-09-29 15:33:20 +10002876 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002877 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002878 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002879
NeilBrowndafb20f2012-03-19 12:46:39 +11002880 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002881 if (rdev->bdev == rdev2->bdev &&
2882 rdev != rdev2 &&
2883 overlaps(rdev->data_offset, rdev->sectors,
2884 rdev2->data_offset,
2885 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002886 overlap = 1;
2887 break;
2888 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002889 if (overlap) {
2890 mddev_put(mddev);
2891 break;
2892 }
2893 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002894 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002895 if (overlap) {
2896 /* Someone else could have slipped in a size
2897 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002898 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002899 * safe, and trust userspace not to race with
2900 * itself
2901 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002902 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002903 return -EBUSY;
2904 }
2905 }
NeilBrown83303b62006-01-06 00:21:06 -08002906 return len;
2907}
2908
2909static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002910__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002911
NeilBrown3cb03002011-10-11 16:45:26 +11002912static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002913{
2914 unsigned long long recovery_start = rdev->recovery_offset;
2915
2916 if (test_bit(In_sync, &rdev->flags) ||
2917 recovery_start == MaxSector)
2918 return sprintf(page, "none\n");
2919
2920 return sprintf(page, "%llu\n", recovery_start);
2921}
2922
NeilBrown3cb03002011-10-11 16:45:26 +11002923static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002924{
2925 unsigned long long recovery_start;
2926
2927 if (cmd_match(buf, "none"))
2928 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002929 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002930 return -EINVAL;
2931
2932 if (rdev->mddev->pers &&
2933 rdev->raid_disk >= 0)
2934 return -EBUSY;
2935
2936 rdev->recovery_offset = recovery_start;
2937 if (recovery_start == MaxSector)
2938 set_bit(In_sync, &rdev->flags);
2939 else
2940 clear_bit(In_sync, &rdev->flags);
2941 return len;
2942}
2943
2944static struct rdev_sysfs_entry rdev_recovery_start =
2945__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2946
NeilBrown16c791a2011-07-28 11:31:47 +10002947static ssize_t
2948badblocks_show(struct badblocks *bb, char *page, int unack);
2949static ssize_t
2950badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2951
NeilBrown3cb03002011-10-11 16:45:26 +11002952static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002953{
2954 return badblocks_show(&rdev->badblocks, page, 0);
2955}
NeilBrown3cb03002011-10-11 16:45:26 +11002956static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002957{
NeilBrownde393cd2011-07-28 11:31:48 +10002958 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2959 /* Maybe that ack was all we needed */
2960 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2961 wake_up(&rdev->blocked_wait);
2962 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002963}
2964static struct rdev_sysfs_entry rdev_bad_blocks =
2965__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2966
NeilBrown3cb03002011-10-11 16:45:26 +11002967static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002968{
2969 return badblocks_show(&rdev->badblocks, page, 1);
2970}
NeilBrown3cb03002011-10-11 16:45:26 +11002971static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002972{
2973 return badblocks_store(&rdev->badblocks, page, len, 1);
2974}
2975static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2976__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2977
NeilBrown86e6ffd2005-11-08 21:39:24 -08002978static struct attribute *rdev_default_attrs[] = {
2979 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002980 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002981 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002982 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10002983 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002984 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002985 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002986 &rdev_bad_blocks.attr,
2987 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002988 NULL,
2989};
2990static ssize_t
2991rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2992{
2993 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002994 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002995
2996 if (!entry->show)
2997 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11002998 if (!rdev->mddev)
2999 return -EBUSY;
3000 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003001}
3002
3003static ssize_t
3004rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3005 const char *page, size_t length)
3006{
3007 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003008 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003009 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003010 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003011
3012 if (!entry->store)
3013 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003014 if (!capable(CAP_SYS_ADMIN))
3015 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003016 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003017 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003018 if (rdev->mddev == NULL)
3019 rv = -EBUSY;
3020 else
3021 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003022 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003023 }
3024 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003025}
3026
3027static void rdev_free(struct kobject *ko)
3028{
NeilBrown3cb03002011-10-11 16:45:26 +11003029 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003030 kfree(rdev);
3031}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003032static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003033 .show = rdev_attr_show,
3034 .store = rdev_attr_store,
3035};
3036static struct kobj_type rdev_ktype = {
3037 .release = rdev_free,
3038 .sysfs_ops = &rdev_sysfs_ops,
3039 .default_attrs = rdev_default_attrs,
3040};
3041
NeilBrown3cb03002011-10-11 16:45:26 +11003042int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003043{
3044 rdev->desc_nr = -1;
3045 rdev->saved_raid_disk = -1;
3046 rdev->raid_disk = -1;
3047 rdev->flags = 0;
3048 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003049 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003050 rdev->sb_events = 0;
3051 rdev->last_read_error.tv_sec = 0;
3052 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003053 rdev->sb_loaded = 0;
3054 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003055 atomic_set(&rdev->nr_pending, 0);
3056 atomic_set(&rdev->read_errors, 0);
3057 atomic_set(&rdev->corrected_errors, 0);
3058
3059 INIT_LIST_HEAD(&rdev->same_set);
3060 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003061
3062 /* Add space to store bad block list.
3063 * This reserves the space even on arrays where it cannot
3064 * be used - I wonder if that matters
3065 */
3066 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003067 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003068 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3069 seqlock_init(&rdev->badblocks.lock);
3070 if (rdev->badblocks.page == NULL)
3071 return -ENOMEM;
3072
3073 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003074}
3075EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076/*
3077 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3078 *
3079 * mark the device faulty if:
3080 *
3081 * - the device is nonexistent (zero size)
3082 * - the device has no valid superblock
3083 *
3084 * a faulty rdev _never_ has rdev->sb set.
3085 */
NeilBrown3cb03002011-10-11 16:45:26 +11003086static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
3088 char b[BDEVNAME_SIZE];
3089 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003090 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 sector_t size;
3092
NeilBrown9ffae0c2006-01-06 00:20:32 -08003093 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (!rdev) {
3095 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3096 return ERR_PTR(-ENOMEM);
3097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
NeilBrown2230dfe2011-07-28 11:31:46 +10003099 err = md_rdev_init(rdev);
3100 if (err)
3101 goto abort_free;
3102 err = alloc_disk_sb(rdev);
3103 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 goto abort_free;
3105
NeilBrownc5d79ad2008-02-06 01:39:54 -08003106 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 if (err)
3108 goto abort_free;
3109
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003110 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003111
Mike Snitzer77304d22010-11-08 14:39:12 +01003112 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003114 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 "md: %s has zero or unknown size, marking faulty!\n",
3116 bdevname(rdev->bdev,b));
3117 err = -EINVAL;
3118 goto abort_free;
3119 }
3120
3121 if (super_format >= 0) {
3122 err = super_types[super_format].
3123 load_super(rdev, NULL, super_minor);
3124 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003125 printk(KERN_WARNING
3126 "md: %s does not have a valid v%d.%d "
3127 "superblock, not importing!\n",
3128 bdevname(rdev->bdev,b),
3129 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 goto abort_free;
3131 }
3132 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003133 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 "md: could not read %s's sb, not importing!\n",
3135 bdevname(rdev->bdev,b));
3136 goto abort_free;
3137 }
3138 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return rdev;
3141
3142abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003143 if (rdev->bdev)
3144 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003145 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 kfree(rdev);
3147 return ERR_PTR(err);
3148}
3149
3150/*
3151 * Check a full RAID array for plausibility
3152 */
3153
NeilBrownf72ffdd2014-09-30 14:23:59 +10003154static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
3156 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003157 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 char b[BDEVNAME_SIZE];
3159
3160 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003161 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 switch (super_types[mddev->major_version].
3163 load_super(rdev, freshest, mddev->minor_version)) {
3164 case 1:
3165 freshest = rdev;
3166 break;
3167 case 0:
3168 break;
3169 default:
3170 printk( KERN_ERR \
3171 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003172 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003174 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 super_types[mddev->major_version].
3178 validate_super(mddev, freshest);
3179
3180 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003181 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003182 if (mddev->max_disks &&
3183 (rdev->desc_nr >= mddev->max_disks ||
3184 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003185 printk(KERN_WARNING
3186 "md: %s: %s: only %d devices permitted\n",
3187 mdname(mddev), bdevname(rdev->bdev, b),
3188 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003189 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003190 continue;
3191 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003192 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (super_types[mddev->major_version].
3194 validate_super(mddev, rdev)) {
3195 printk(KERN_WARNING "md: kicking non-fresh %s"
3196 " from array!\n",
3197 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003198 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 continue;
3200 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003201 /* No device should have a Candidate flag
3202 * when reading devices
3203 */
3204 if (test_bit(Candidate, &rdev->flags)) {
3205 pr_info("md: kicking Cluster Candidate %s from array!\n",
3206 bdevname(rdev->bdev, b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003207 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003208 }
3209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 if (mddev->level == LEVEL_MULTIPATH) {
3211 rdev->desc_nr = i++;
3212 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003213 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003214 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003215 rdev->raid_disk = -1;
3216 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
3218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219}
3220
NeilBrown72e02072009-12-14 12:49:55 +11003221/* Read a fixed-point number.
3222 * Numbers in sysfs attributes should be in "standard" units where
3223 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003224 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003225 * milliseconds or jiffies.
3226 * This function takes a decimal number with a possible fractional
3227 * component, and produces an integer which is the result of
3228 * multiplying that number by 10^'scale'.
3229 * all without any floating-point arithmetic.
3230 */
3231int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3232{
3233 unsigned long result = 0;
3234 long decimals = -1;
3235 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3236 if (*cp == '.')
3237 decimals = 0;
3238 else if (decimals < scale) {
3239 unsigned int value;
3240 value = *cp - '0';
3241 result = result * 10 + value;
3242 if (decimals >= 0)
3243 decimals++;
3244 }
3245 cp++;
3246 }
3247 if (*cp == '\n')
3248 cp++;
3249 if (*cp)
3250 return -EINVAL;
3251 if (decimals < 0)
3252 decimals = 0;
3253 while (decimals < scale) {
3254 result *= 10;
3255 decimals ++;
3256 }
3257 *res = result;
3258 return 0;
3259}
3260
NeilBrowneae17012005-11-08 21:39:23 -08003261static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003262safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003263{
3264 int msec = (mddev->safemode_delay*1000)/HZ;
3265 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3266}
3267static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003268safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003269{
NeilBrown16f17b32006-06-26 00:27:37 -07003270 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003271
NeilBrown72e02072009-12-14 12:49:55 +11003272 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003273 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003274 if (msec == 0)
3275 mddev->safemode_delay = 0;
3276 else {
NeilBrown19052c02008-08-05 15:54:13 +10003277 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003278 unsigned long new_delay = (msec*HZ)/1000;
3279
3280 if (new_delay == 0)
3281 new_delay = 1;
3282 mddev->safemode_delay = new_delay;
3283 if (new_delay < old_delay || old_delay == 0)
3284 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003285 }
3286 return len;
3287}
3288static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003289__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003290
3291static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003292level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003293{
NeilBrown36d091f2014-12-15 12:56:58 +11003294 struct md_personality *p;
3295 int ret;
3296 spin_lock(&mddev->lock);
3297 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003298 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003299 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003300 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003301 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003302 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003303 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003304 else
NeilBrown36d091f2014-12-15 12:56:58 +11003305 ret = 0;
3306 spin_unlock(&mddev->lock);
3307 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003308}
3309
NeilBrownd9d166c2006-01-06 00:20:51 -08003310static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003311level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003312{
Dan Williamsf2859af2010-05-02 10:04:16 -07003313 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003314 ssize_t rv;
3315 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003316 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003317 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003318 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003319 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003320
NeilBrown67918752014-12-15 12:57:01 +11003321 if (slen == 0 || slen >= sizeof(clevel))
3322 return -EINVAL;
3323
3324 rv = mddev_lock(mddev);
3325 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003326 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003327
3328 if (mddev->pers == NULL) {
3329 strncpy(mddev->clevel, buf, slen);
3330 if (mddev->clevel[slen-1] == '\n')
3331 slen--;
3332 mddev->clevel[slen] = 0;
3333 mddev->level = LEVEL_NONE;
3334 rv = len;
3335 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003336 }
NeilBrown67918752014-12-15 12:57:01 +11003337 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003338 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003339 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003340
3341 /* request to change the personality. Need to ensure:
3342 * - array is not engaged in resync/recovery/reshape
3343 * - old personality can be suspended
3344 * - new personality will access other array.
3345 */
3346
NeilBrown67918752014-12-15 12:57:01 +11003347 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003348 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003349 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003350 mddev->reshape_position != MaxSector ||
3351 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003352 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003353
NeilBrown67918752014-12-15 12:57:01 +11003354 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003355 if (!mddev->pers->quiesce) {
3356 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3357 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003358 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003359 }
3360
3361 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003362 strncpy(clevel, buf, slen);
3363 if (clevel[slen-1] == '\n')
3364 slen--;
3365 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003366 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003367 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003368
Dan Williamsf2859af2010-05-02 10:04:16 -07003369 if (request_module("md-%s", clevel) != 0)
3370 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003371 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003372 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003373 if (!pers || !try_module_get(pers->owner)) {
3374 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003375 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003376 rv = -EINVAL;
3377 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003378 }
3379 spin_unlock(&pers_lock);
3380
3381 if (pers == mddev->pers) {
3382 /* Nothing to do! */
3383 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003384 rv = len;
3385 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003386 }
3387 if (!pers->takeover) {
3388 module_put(pers->owner);
3389 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003390 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003391 rv = -EINVAL;
3392 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003393 }
3394
NeilBrowndafb20f2012-03-19 12:46:39 +11003395 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003396 rdev->new_raid_disk = rdev->raid_disk;
3397
NeilBrown245f46c2009-03-31 14:39:39 +11003398 /* ->takeover must set new_* and/or delta_disks
3399 * if it succeeds, and may set them when it fails.
3400 */
3401 priv = pers->takeover(mddev);
3402 if (IS_ERR(priv)) {
3403 mddev->new_level = mddev->level;
3404 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003405 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003406 mddev->raid_disks -= mddev->delta_disks;
3407 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003408 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003409 module_put(pers->owner);
3410 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003411 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003412 rv = PTR_ERR(priv);
3413 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003414 }
3415
3416 /* Looks like we have a winner */
3417 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003418 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003419
3420 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003421 oldpers = mddev->pers;
3422 oldpriv = mddev->private;
3423 mddev->pers = pers;
3424 mddev->private = priv;
3425 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3426 mddev->level = mddev->new_level;
3427 mddev->layout = mddev->new_layout;
3428 mddev->chunk_sectors = mddev->new_chunk_sectors;
3429 mddev->delta_disks = 0;
3430 mddev->reshape_backwards = 0;
3431 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003432 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003433
NeilBrowndb721d32014-12-15 12:56:58 +11003434 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003435 mddev->external) {
3436 /* We are converting from a no-redundancy array
3437 * to a redundancy array and metadata is managed
3438 * externally so we need to be sure that writes
3439 * won't block due to a need to transition
3440 * clean->dirty
3441 * until external management is started.
3442 */
3443 mddev->in_sync = 0;
3444 mddev->safemode_delay = 0;
3445 mddev->safemode = 0;
3446 }
3447
NeilBrowndb721d32014-12-15 12:56:58 +11003448 oldpers->free(mddev, oldpriv);
3449
3450 if (oldpers->sync_request == NULL &&
3451 pers->sync_request != NULL) {
3452 /* need to add the md_redundancy_group */
3453 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3454 printk(KERN_WARNING
3455 "md: cannot register extra attributes for %s\n",
3456 mdname(mddev));
3457 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3458 }
3459 if (oldpers->sync_request != NULL &&
3460 pers->sync_request == NULL) {
3461 /* need to remove the md_redundancy_group */
3462 if (mddev->to_remove == NULL)
3463 mddev->to_remove = &md_redundancy_group;
3464 }
3465
NeilBrowndafb20f2012-03-19 12:46:39 +11003466 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003467 if (rdev->raid_disk < 0)
3468 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003469 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003470 rdev->new_raid_disk = -1;
3471 if (rdev->new_raid_disk == rdev->raid_disk)
3472 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003473 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003474 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003475 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003476 if (rdev->raid_disk < 0)
3477 continue;
3478 if (rdev->new_raid_disk == rdev->raid_disk)
3479 continue;
3480 rdev->raid_disk = rdev->new_raid_disk;
3481 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003482 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003483 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003484 if (sysfs_link_rdev(mddev, rdev))
3485 printk(KERN_WARNING "md: cannot register rd%d"
3486 " for %s after level change\n",
3487 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003488 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003489 }
3490
NeilBrowndb721d32014-12-15 12:56:58 +11003491 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003492 /* this is now an array without redundancy, so
3493 * it must always be in_sync
3494 */
3495 mddev->in_sync = 1;
3496 del_timer_sync(&mddev->safemode_timer);
3497 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003498 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003499 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003500 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003501 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003502 if (!mddev->thread)
3503 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003504 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003505 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003506 rv = len;
3507out_unlock:
3508 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003509 return rv;
3510}
3511
3512static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003513__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003514
NeilBrownd4dbd022006-06-26 00:27:59 -07003515static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003516layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003517{
3518 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003519 if (mddev->reshape_position != MaxSector &&
3520 mddev->layout != mddev->new_layout)
3521 return sprintf(page, "%d (%d)\n",
3522 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003523 return sprintf(page, "%d\n", mddev->layout);
3524}
3525
3526static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003527layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003528{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003529 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003530 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003531
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003532 err = kstrtouint(buf, 10, &n);
3533 if (err < 0)
3534 return err;
NeilBrown67918752014-12-15 12:57:01 +11003535 err = mddev_lock(mddev);
3536 if (err)
3537 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003538
NeilBrownb3546032009-03-31 14:56:41 +11003539 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003540 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003541 err = -EBUSY;
3542 else if (mddev->ro)
3543 err = -EROFS;
3544 else {
3545 mddev->new_layout = n;
3546 err = mddev->pers->check_reshape(mddev);
3547 if (err)
3548 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003549 }
NeilBrownb3546032009-03-31 14:56:41 +11003550 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003551 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003552 if (mddev->reshape_position == MaxSector)
3553 mddev->layout = n;
3554 }
NeilBrown67918752014-12-15 12:57:01 +11003555 mddev_unlock(mddev);
3556 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003557}
3558static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003559__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003560
NeilBrowneae17012005-11-08 21:39:23 -08003561static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003562raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003563{
NeilBrownbb636542005-11-08 21:39:45 -08003564 if (mddev->raid_disks == 0)
3565 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003566 if (mddev->reshape_position != MaxSector &&
3567 mddev->delta_disks != 0)
3568 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3569 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003570 return sprintf(page, "%d\n", mddev->raid_disks);
3571}
3572
NeilBrownfd01b882011-10-11 16:47:53 +11003573static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003574
3575static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003576raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003577{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003578 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003579 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003580
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003581 err = kstrtouint(buf, 10, &n);
3582 if (err < 0)
3583 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003584
NeilBrown67918752014-12-15 12:57:01 +11003585 err = mddev_lock(mddev);
3586 if (err)
3587 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003588 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003589 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003590 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003591 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003592 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003593
NeilBrown67918752014-12-15 12:57:01 +11003594 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003595 rdev_for_each(rdev, mddev) {
3596 if (olddisks < n &&
3597 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003598 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003599 if (olddisks > n &&
3600 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003601 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003602 }
NeilBrown67918752014-12-15 12:57:01 +11003603 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003604 mddev->delta_disks = n - olddisks;
3605 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003606 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003607 } else
NeilBrownda943b992006-01-06 00:20:54 -08003608 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003609out_unlock:
3610 mddev_unlock(mddev);
3611 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003612}
3613static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003614__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003615
NeilBrown24dd4692005-11-08 21:39:26 -08003616static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003617chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003618{
NeilBrown08a02ec2007-05-09 02:35:38 -07003619 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003620 mddev->chunk_sectors != mddev->new_chunk_sectors)
3621 return sprintf(page, "%d (%d)\n",
3622 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003623 mddev->chunk_sectors << 9);
3624 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003625}
3626
3627static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003628chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003629{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003630 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003631 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003632
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003633 err = kstrtoul(buf, 10, &n);
3634 if (err < 0)
3635 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003636
NeilBrown67918752014-12-15 12:57:01 +11003637 err = mddev_lock(mddev);
3638 if (err)
3639 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003640 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003641 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003642 err = -EBUSY;
3643 else if (mddev->ro)
3644 err = -EROFS;
3645 else {
3646 mddev->new_chunk_sectors = n >> 9;
3647 err = mddev->pers->check_reshape(mddev);
3648 if (err)
3649 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003650 }
NeilBrownb3546032009-03-31 14:56:41 +11003651 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003652 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003653 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003654 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003655 }
NeilBrown67918752014-12-15 12:57:01 +11003656 mddev_unlock(mddev);
3657 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003658}
3659static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003660__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003661
NeilBrowna94213b2006-06-26 00:28:00 -07003662static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003663resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003664{
NeilBrownd1a7c502009-03-31 15:24:32 +11003665 if (mddev->recovery_cp == MaxSector)
3666 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003667 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3668}
3669
3670static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003671resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003672{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003673 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003674 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003675
3676 if (cmd_match(buf, "none"))
3677 n = MaxSector;
3678 else {
3679 err = kstrtoull(buf, 10, &n);
3680 if (err < 0)
3681 return err;
3682 if (n != (sector_t)n)
3683 return -EINVAL;
3684 }
NeilBrowna94213b2006-06-26 00:28:00 -07003685
NeilBrown67918752014-12-15 12:57:01 +11003686 err = mddev_lock(mddev);
3687 if (err)
3688 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003689 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003690 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003691
NeilBrown67918752014-12-15 12:57:01 +11003692 if (!err) {
3693 mddev->recovery_cp = n;
3694 if (mddev->pers)
3695 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3696 }
3697 mddev_unlock(mddev);
3698 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003699}
3700static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003701__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3702 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003703
NeilBrown9e653b62006-06-26 00:27:58 -07003704/*
3705 * The array state can be:
3706 *
3707 * clear
3708 * No devices, no size, no level
3709 * Equivalent to STOP_ARRAY ioctl
3710 * inactive
3711 * May have some settings, but array is not active
3712 * all IO results in error
3713 * When written, doesn't tear down array, but just stops it
3714 * suspended (not supported yet)
3715 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003716 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003717 * readonly
3718 * no resync can happen. no superblocks get written.
3719 * write requests fail
3720 * read-auto
3721 * like readonly, but behaves like 'clean' on a write request.
3722 *
3723 * clean - no pending writes, but otherwise active.
3724 * When written to inactive array, starts without resync
3725 * If a write request arrives then
3726 * if metadata is known, mark 'dirty' and switch to 'active'.
3727 * if not known, block and switch to write-pending
3728 * If written to an active array that has pending writes, then fails.
3729 * active
3730 * fully active: IO and resync can be happening.
3731 * When written to inactive array, starts with resync
3732 *
3733 * write-pending
3734 * clean, but writes are blocked waiting for 'active' to be written.
3735 *
3736 * active-idle
3737 * like active, but no writes have been seen for a while (100msec).
3738 *
3739 */
3740enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3741 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003742static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003743 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3744 "write-pending", "active-idle", NULL };
3745
3746static int match_word(const char *word, char **list)
3747{
3748 int n;
3749 for (n=0; list[n]; n++)
3750 if (cmd_match(word, list[n]))
3751 break;
3752 return n;
3753}
3754
3755static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003756array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003757{
3758 enum array_state st = inactive;
3759
3760 if (mddev->pers)
3761 switch(mddev->ro) {
3762 case 1:
3763 st = readonly;
3764 break;
3765 case 2:
3766 st = read_auto;
3767 break;
3768 case 0:
3769 if (mddev->in_sync)
3770 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003771 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003772 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003773 else if (mddev->safemode)
3774 st = active_idle;
3775 else
3776 st = active;
3777 }
3778 else {
3779 if (list_empty(&mddev->disks) &&
3780 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003781 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003782 st = clear;
3783 else
3784 st = inactive;
3785 }
3786 return sprintf(page, "%s\n", array_states[st]);
3787}
3788
NeilBrownf72ffdd2014-09-30 14:23:59 +10003789static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3790static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3791static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003792static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003793
3794static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003795array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003796{
NeilBrown67918752014-12-15 12:57:01 +11003797 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003798 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003799
3800 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3801 /* don't take reconfig_mutex when toggling between
3802 * clean and active
3803 */
3804 spin_lock(&mddev->lock);
3805 if (st == active) {
3806 restart_array(mddev);
3807 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3808 wake_up(&mddev->sb_wait);
3809 err = 0;
3810 } else /* st == clean */ {
3811 restart_array(mddev);
3812 if (atomic_read(&mddev->writes_pending) == 0) {
3813 if (mddev->in_sync == 0) {
3814 mddev->in_sync = 1;
3815 if (mddev->safemode == 1)
3816 mddev->safemode = 0;
3817 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3818 }
3819 err = 0;
3820 } else
3821 err = -EBUSY;
3822 }
3823 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003824 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003825 }
3826 err = mddev_lock(mddev);
3827 if (err)
3828 return err;
3829 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003830 switch(st) {
3831 case bad_word:
3832 break;
3833 case clear:
3834 /* stopping an active array */
NeilBrowna05b7ea02012-07-19 15:59:18 +10003835 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003836 break;
3837 case inactive:
3838 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003839 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003840 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003841 else
NeilBrowne6910632008-02-06 01:39:51 -08003842 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003843 break;
3844 case suspended:
3845 break; /* not supported yet */
3846 case readonly:
3847 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003848 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003849 else {
3850 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003851 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003852 err = do_md_run(mddev);
3853 }
3854 break;
3855 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003856 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003857 if (mddev->ro == 0)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003858 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003859 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003860 err = restart_array(mddev);
3861 if (err == 0) {
3862 mddev->ro = 2;
3863 set_disk_ro(mddev->gendisk, 0);
3864 }
NeilBrown9e653b62006-06-26 00:27:58 -07003865 } else {
3866 mddev->ro = 2;
3867 err = do_md_run(mddev);
3868 }
3869 break;
3870 case clean:
3871 if (mddev->pers) {
3872 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003873 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003874 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003875 if (mddev->in_sync == 0) {
3876 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003877 if (mddev->safemode == 1)
3878 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003879 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003880 }
3881 err = 0;
3882 } else
3883 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003884 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003885 } else
3886 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003887 break;
3888 case active:
3889 if (mddev->pers) {
3890 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003891 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003892 wake_up(&mddev->sb_wait);
3893 err = 0;
3894 } else {
3895 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003896 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003897 err = do_md_run(mddev);
3898 }
3899 break;
3900 case write_pending:
3901 case active_idle:
3902 /* these cannot be set */
3903 break;
3904 }
NeilBrown67918752014-12-15 12:57:01 +11003905
3906 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11003907 if (mddev->hold_active == UNTIL_IOCTL)
3908 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003909 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003910 }
NeilBrown67918752014-12-15 12:57:01 +11003911 mddev_unlock(mddev);
3912 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07003913}
NeilBrown80ca3a42006-07-10 04:44:18 -07003914static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10003915__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003916
NeilBrown6d7ff732006-01-06 00:21:16 -08003917static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003918max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003919 return sprintf(page, "%d\n",
3920 atomic_read(&mddev->max_corr_read_errors));
3921}
3922
3923static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003924max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003925{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003926 unsigned int n;
3927 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11003928
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003929 rv = kstrtouint(buf, 10, &n);
3930 if (rv < 0)
3931 return rv;
3932 atomic_set(&mddev->max_corr_read_errors, n);
3933 return len;
Robert Becker1e509152009-12-14 12:49:58 +11003934}
3935
3936static struct md_sysfs_entry max_corr_read_errors =
3937__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3938 max_corrected_read_errors_store);
3939
3940static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003941null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003942{
3943 return -EINVAL;
3944}
3945
3946static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003947new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08003948{
3949 /* buf must be %d:%d\n? giving major and minor numbers */
3950 /* The new device is added to the array.
3951 * If the array has a persistent superblock, we read the
3952 * superblock to initialise info and check validity.
3953 * Otherwise, only checking done is that in bind_rdev_to_array,
3954 * which mainly checks size.
3955 */
3956 char *e;
3957 int major = simple_strtoul(buf, &e, 10);
3958 int minor;
3959 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003960 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08003961 int err;
3962
3963 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3964 return -EINVAL;
3965 minor = simple_strtoul(e+1, &e, 10);
3966 if (*e && *e != '\n')
3967 return -EINVAL;
3968 dev = MKDEV(major, minor);
3969 if (major != MAJOR(dev) ||
3970 minor != MINOR(dev))
3971 return -EOVERFLOW;
3972
NeilBrown67918752014-12-15 12:57:01 +11003973 flush_workqueue(md_misc_wq);
3974
3975 err = mddev_lock(mddev);
3976 if (err)
3977 return err;
NeilBrown6d7ff732006-01-06 00:21:16 -08003978 if (mddev->persistent) {
3979 rdev = md_import_device(dev, mddev->major_version,
3980 mddev->minor_version);
3981 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003982 struct md_rdev *rdev0
3983 = list_entry(mddev->disks.next,
3984 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08003985 err = super_types[mddev->major_version]
3986 .load_super(rdev, rdev0, mddev->minor_version);
3987 if (err < 0)
3988 goto out;
3989 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003990 } else if (mddev->external)
3991 rdev = md_import_device(dev, -2, -1);
3992 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003993 rdev = md_import_device(dev, -1, -1);
3994
NeilBrown9a8c0fa2015-06-25 17:06:40 +10003995 if (IS_ERR(rdev)) {
3996 mddev_unlock(mddev);
NeilBrown6d7ff732006-01-06 00:21:16 -08003997 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10003998 }
NeilBrown6d7ff732006-01-06 00:21:16 -08003999 err = bind_rdev_to_array(rdev, mddev);
4000 out:
4001 if (err)
4002 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004003 mddev_unlock(mddev);
NeilBrown6d7ff732006-01-06 00:21:16 -08004004 return err ? err : len;
4005}
4006
4007static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004008__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004009
4010static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004011bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004012{
4013 char *end;
4014 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004015 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004016
NeilBrown67918752014-12-15 12:57:01 +11004017 err = mddev_lock(mddev);
4018 if (err)
4019 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004020 if (!mddev->bitmap)
4021 goto out;
4022 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4023 while (*buf) {
4024 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4025 if (buf == end) break;
4026 if (*end == '-') { /* range */
4027 buf = end + 1;
4028 end_chunk = simple_strtoul(buf, &end, 0);
4029 if (buf == end) break;
4030 }
4031 if (*end && !isspace(*end)) break;
4032 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004033 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004034 }
4035 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4036out:
NeilBrown67918752014-12-15 12:57:01 +11004037 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004038 return len;
4039}
4040
4041static struct md_sysfs_entry md_bitmap =
4042__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4043
4044static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004045size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004046{
Andre Noll58c0fed2009-03-31 14:33:13 +11004047 return sprintf(page, "%llu\n",
4048 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004049}
4050
NeilBrownfd01b882011-10-11 16:47:53 +11004051static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004052
4053static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004054size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004055{
4056 /* If array is inactive, we can reduce the component size, but
4057 * not increase it (except from 0).
4058 * If array is active, we can try an on-line resize
4059 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004060 sector_t sectors;
4061 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004062
Andre Noll58c0fed2009-03-31 14:33:13 +11004063 if (err < 0)
4064 return err;
NeilBrown67918752014-12-15 12:57:01 +11004065 err = mddev_lock(mddev);
4066 if (err)
4067 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004068 if (mddev->pers) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004069 if (mddev_is_clustered(mddev))
4070 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11004071 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004072 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004073 if (mddev_is_clustered(mddev))
4074 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004075 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004076 if (mddev->dev_sectors == 0 ||
4077 mddev->dev_sectors > sectors)
4078 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004079 else
4080 err = -ENOSPC;
4081 }
NeilBrown67918752014-12-15 12:57:01 +11004082 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004083 return err ? err : len;
4084}
4085
4086static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004087__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004088
Masanari Iida83f0d772012-10-30 00:18:08 +09004089/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004090 * This is one of
4091 * 'none' for arrays with no metadata (good luck...)
4092 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004093 * or N.M for internally known formats
4094 */
4095static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004096metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004097{
4098 if (mddev->persistent)
4099 return sprintf(page, "%d.%d\n",
4100 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004101 else if (mddev->external)
4102 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004103 else
4104 return sprintf(page, "none\n");
4105}
4106
4107static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004108metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004109{
4110 int major, minor;
4111 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004112 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004113 /* Changing the details of 'external' metadata is
4114 * always permitted. Otherwise there must be
4115 * no devices attached to the array.
4116 */
NeilBrown67918752014-12-15 12:57:01 +11004117
4118 err = mddev_lock(mddev);
4119 if (err)
4120 return err;
4121 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004122 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4123 ;
4124 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004125 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004126
NeilBrown67918752014-12-15 12:57:01 +11004127 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004128 if (cmd_match(buf, "none")) {
4129 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004130 mddev->external = 0;
4131 mddev->major_version = 0;
4132 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004133 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004134 }
4135 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004136 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004137 if (namelen >= sizeof(mddev->metadata_type))
4138 namelen = sizeof(mddev->metadata_type)-1;
4139 strncpy(mddev->metadata_type, buf+9, namelen);
4140 mddev->metadata_type[namelen] = 0;
4141 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4142 mddev->metadata_type[--namelen] = 0;
4143 mddev->persistent = 0;
4144 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004145 mddev->major_version = 0;
4146 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004147 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004148 }
4149 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004150 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004151 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004152 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004153 buf = e+1;
4154 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004155 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004156 goto out_unlock;
4157 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004158 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004159 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004160 mddev->major_version = major;
4161 mddev->minor_version = minor;
4162 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004163 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004164 err = 0;
4165out_unlock:
4166 mddev_unlock(mddev);
4167 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004168}
4169
4170static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004171__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004172
NeilBrowna35b0d62006-01-06 00:20:49 -08004173static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004174action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004175{
NeilBrown7eec3142005-11-08 21:39:44 -08004176 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004177 unsigned long recovery = mddev->recovery;
4178 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004179 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004180 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4181 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4182 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004183 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004184 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4185 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004186 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004187 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004188 type = "check";
4189 else
4190 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004191 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004192 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004193 else if (mddev->reshape_position != MaxSector)
4194 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004195 }
4196 return sprintf(page, "%s\n", type);
4197}
4198
4199static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004200action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004201{
NeilBrown7eec3142005-11-08 21:39:44 -08004202 if (!mddev->pers || !mddev->pers->sync_request)
4203 return -EINVAL;
4204
NeilBrownb6a9ce62009-05-26 09:41:17 +10004205
4206 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004207 if (cmd_match(page, "frozen"))
4208 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4209 else
4210 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004211 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4212 mddev_lock(mddev) == 0) {
4213 flush_workqueue(md_misc_wq);
4214 if (mddev->sync_thread) {
4215 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004216 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004217 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004218 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004219 }
NeilBrown03c902e2006-01-06 00:20:46 -08004220 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4221 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004222 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004223 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004224 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004225 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004226 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004227 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004228 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004229 int err;
4230 if (mddev->pers->start_reshape == NULL)
4231 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004232 err = mddev_lock(mddev);
4233 if (!err) {
NeilBrown56ccc112015-05-28 17:53:29 +10004234 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004235 err = mddev->pers->start_reshape(mddev);
4236 mddev_unlock(mddev);
4237 }
NeilBrown16484bf2006-03-27 01:18:13 -08004238 if (err)
4239 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004240 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004241 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004242 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004243 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004244 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004245 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004246 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004247 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4248 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004249 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004250 if (mddev->ro == 2) {
4251 /* A write to sync_action is enough to justify
4252 * canceling read-auto mode
4253 */
4254 mddev->ro = 0;
4255 md_wakeup_thread(mddev->sync_thread);
4256 }
NeilBrown03c902e2006-01-06 00:20:46 -08004257 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004258 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004259 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004260 return len;
4261}
4262
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004263static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004264__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004265
4266static ssize_t
4267last_sync_action_show(struct mddev *mddev, char *page)
4268{
4269 return sprintf(page, "%s\n", mddev->last_sync_action);
4270}
4271
4272static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4273
NeilBrown9d888832005-11-08 21:39:26 -08004274static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004275mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004276{
4277 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004278 (unsigned long long)
4279 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004280}
4281
NeilBrown80ca3a42006-07-10 04:44:18 -07004282static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004283
NeilBrown88202a02006-01-06 00:21:36 -08004284static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004285sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004286{
4287 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4288 mddev->sync_speed_min ? "local": "system");
4289}
4290
4291static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004292sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004293{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004294 unsigned int min;
4295 int rv;
4296
NeilBrown88202a02006-01-06 00:21:36 -08004297 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004298 min = 0;
4299 } else {
4300 rv = kstrtouint(buf, 10, &min);
4301 if (rv < 0)
4302 return rv;
4303 if (min == 0)
4304 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004305 }
NeilBrown88202a02006-01-06 00:21:36 -08004306 mddev->sync_speed_min = min;
4307 return len;
4308}
4309
4310static struct md_sysfs_entry md_sync_min =
4311__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4312
4313static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004314sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004315{
4316 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4317 mddev->sync_speed_max ? "local": "system");
4318}
4319
4320static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004321sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004322{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004323 unsigned int max;
4324 int rv;
4325
NeilBrown88202a02006-01-06 00:21:36 -08004326 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004327 max = 0;
4328 } else {
4329 rv = kstrtouint(buf, 10, &max);
4330 if (rv < 0)
4331 return rv;
4332 if (max == 0)
4333 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004334 }
NeilBrown88202a02006-01-06 00:21:36 -08004335 mddev->sync_speed_max = max;
4336 return len;
4337}
4338
4339static struct md_sysfs_entry md_sync_max =
4340__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4341
Iustin Popd7f3d292007-10-16 23:30:54 -07004342static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004343degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004344{
4345 return sprintf(page, "%d\n", mddev->degraded);
4346}
4347static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004348
4349static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004350sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004351{
4352 return sprintf(page, "%d\n", mddev->parallel_resync);
4353}
4354
4355static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004356sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004357{
4358 long n;
4359
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004360 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004361 return -EINVAL;
4362
4363 if (n != 0 && n != 1)
4364 return -EINVAL;
4365
4366 mddev->parallel_resync = n;
4367
4368 if (mddev->sync_thread)
4369 wake_up(&resync_wait);
4370
4371 return len;
4372}
4373
4374/* force parallel resync, even with shared block devices */
4375static struct md_sysfs_entry md_sync_force_parallel =
4376__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4377 sync_force_parallel_show, sync_force_parallel_store);
4378
4379static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004380sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004381{
4382 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004383 if (mddev->curr_resync == 0)
4384 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004385 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4386 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004387 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004388 db = resync - mddev->resync_mark_cnt;
4389 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004390}
4391
NeilBrown80ca3a42006-07-10 04:44:18 -07004392static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004393
4394static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004395sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004396{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004397 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004398
NeilBrownacb180b2009-04-14 16:28:34 +10004399 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4400 return sprintf(page, "none\n");
4401
NeilBrown72f36d52012-10-11 14:25:57 +11004402 if (mddev->curr_resync == 1 ||
4403 mddev->curr_resync == 2)
4404 return sprintf(page, "delayed\n");
4405
NeilBrownc804cde2012-05-21 09:28:33 +10004406 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4407 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004408 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004409 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004410 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004411
NeilBrownacb180b2009-04-14 16:28:34 +10004412 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004413 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004414}
4415
NeilBrown750f1992014-09-30 08:53:05 +10004416static struct md_sysfs_entry md_sync_completed =
4417 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004418
NeilBrowne464eaf2006-03-27 01:18:14 -08004419static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004420min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004421{
4422 return sprintf(page, "%llu\n",
4423 (unsigned long long)mddev->resync_min);
4424}
4425static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004426min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004427{
4428 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004429 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004430
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004431 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004432 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004433
4434 spin_lock(&mddev->lock);
4435 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004436 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004437 goto out_unlock;
4438
4439 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004440 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004441 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004442
NeilBrown50c37b12015-03-23 17:36:38 +11004443 /* Round down to multiple of 4K for safety */
4444 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004445 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004446
NeilBrown23da4222014-12-15 12:57:01 +11004447out_unlock:
4448 spin_unlock(&mddev->lock);
4449 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004450}
4451
4452static struct md_sysfs_entry md_min_sync =
4453__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4454
4455static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004456max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004457{
4458 if (mddev->resync_max == MaxSector)
4459 return sprintf(page, "max\n");
4460 else
4461 return sprintf(page, "%llu\n",
4462 (unsigned long long)mddev->resync_max);
4463}
4464static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004465max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004466{
NeilBrown23da4222014-12-15 12:57:01 +11004467 int err;
4468 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004469 if (strncmp(buf, "max", 3) == 0)
4470 mddev->resync_max = MaxSector;
4471 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004472 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004473 int chunk;
4474
4475 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004476 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004477 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004478 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004479 goto out_unlock;
4480
4481 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004482 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004483 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004484 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004485 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004486
4487 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004488 chunk = mddev->chunk_sectors;
4489 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004490 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004491
4492 err = -EINVAL;
4493 if (sector_div(temp, chunk))
4494 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004495 }
4496 mddev->resync_max = max;
4497 }
4498 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004499 err = 0;
4500out_unlock:
4501 spin_unlock(&mddev->lock);
4502 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004503}
4504
4505static struct md_sysfs_entry md_max_sync =
4506__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4507
4508static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004509suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004510{
4511 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4512}
4513
4514static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004515suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004516{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004517 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004518 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004519
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004520 err = kstrtoull(buf, 10, &new);
4521 if (err < 0)
4522 return err;
4523 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004524 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004525
NeilBrown67918752014-12-15 12:57:01 +11004526 err = mddev_lock(mddev);
4527 if (err)
4528 return err;
4529 err = -EINVAL;
4530 if (mddev->pers == NULL ||
4531 mddev->pers->quiesce == NULL)
4532 goto unlock;
4533 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004534 mddev->suspend_lo = new;
4535 if (new >= old)
4536 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004537 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004538 else {
4539 /* Expanding suspended region - need to wait */
4540 mddev->pers->quiesce(mddev, 1);
4541 mddev->pers->quiesce(mddev, 0);
4542 }
NeilBrown67918752014-12-15 12:57:01 +11004543 err = 0;
4544unlock:
4545 mddev_unlock(mddev);
4546 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004547}
4548static struct md_sysfs_entry md_suspend_lo =
4549__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4550
NeilBrowne464eaf2006-03-27 01:18:14 -08004551static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004552suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004553{
4554 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4555}
4556
4557static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004558suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004559{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004560 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004561 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004562
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004563 err = kstrtoull(buf, 10, &new);
4564 if (err < 0)
4565 return err;
4566 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004567 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004568
NeilBrown67918752014-12-15 12:57:01 +11004569 err = mddev_lock(mddev);
4570 if (err)
4571 return err;
4572 err = -EINVAL;
4573 if (mddev->pers == NULL ||
4574 mddev->pers->quiesce == NULL)
4575 goto unlock;
4576 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004577 mddev->suspend_hi = new;
4578 if (new <= old)
4579 /* Shrinking suspended region */
4580 mddev->pers->quiesce(mddev, 2);
4581 else {
4582 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004583 mddev->pers->quiesce(mddev, 1);
4584 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004585 }
NeilBrown67918752014-12-15 12:57:01 +11004586 err = 0;
4587unlock:
4588 mddev_unlock(mddev);
4589 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004590}
4591static struct md_sysfs_entry md_suspend_hi =
4592__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4593
NeilBrown08a02ec2007-05-09 02:35:38 -07004594static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004595reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004596{
4597 if (mddev->reshape_position != MaxSector)
4598 return sprintf(page, "%llu\n",
4599 (unsigned long long)mddev->reshape_position);
4600 strcpy(page, "none\n");
4601 return 5;
4602}
4603
4604static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004605reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004606{
NeilBrownc6563a82012-05-21 09:27:00 +10004607 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004608 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004609 int err;
NeilBrown67918752014-12-15 12:57:01 +11004610
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004611 err = kstrtoull(buf, 10, &new);
4612 if (err < 0)
4613 return err;
4614 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004615 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004616 err = mddev_lock(mddev);
4617 if (err)
4618 return err;
4619 err = -EBUSY;
4620 if (mddev->pers)
4621 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004622 mddev->reshape_position = new;
4623 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004624 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004625 mddev->new_level = mddev->level;
4626 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004627 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004628 rdev_for_each(rdev, mddev)
4629 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004630 err = 0;
4631unlock:
4632 mddev_unlock(mddev);
4633 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004634}
4635
4636static struct md_sysfs_entry md_reshape_position =
4637__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4638 reshape_position_store);
4639
Dan Williamsb522adc2009-03-31 15:00:31 +11004640static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004641reshape_direction_show(struct mddev *mddev, char *page)
4642{
4643 return sprintf(page, "%s\n",
4644 mddev->reshape_backwards ? "backwards" : "forwards");
4645}
4646
4647static ssize_t
4648reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4649{
4650 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004651 int err;
4652
NeilBrown2c810cd2012-05-21 09:27:00 +10004653 if (cmd_match(buf, "forwards"))
4654 backwards = 0;
4655 else if (cmd_match(buf, "backwards"))
4656 backwards = 1;
4657 else
4658 return -EINVAL;
4659 if (mddev->reshape_backwards == backwards)
4660 return len;
4661
NeilBrown67918752014-12-15 12:57:01 +11004662 err = mddev_lock(mddev);
4663 if (err)
4664 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004665 /* check if we are allowed to change */
4666 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004667 err = -EBUSY;
4668 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004669 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004670 err = -EINVAL;
4671 else
4672 mddev->reshape_backwards = backwards;
4673 mddev_unlock(mddev);
4674 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004675}
4676
4677static struct md_sysfs_entry md_reshape_direction =
4678__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4679 reshape_direction_store);
4680
4681static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004682array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004683{
4684 if (mddev->external_size)
4685 return sprintf(page, "%llu\n",
4686 (unsigned long long)mddev->array_sectors/2);
4687 else
4688 return sprintf(page, "default\n");
4689}
4690
4691static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004692array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004693{
4694 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004695 int err;
4696
4697 err = mddev_lock(mddev);
4698 if (err)
4699 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004700
4701 if (strncmp(buf, "default", 7) == 0) {
4702 if (mddev->pers)
4703 sectors = mddev->pers->size(mddev, 0, 0);
4704 else
4705 sectors = mddev->array_sectors;
4706
4707 mddev->external_size = 0;
4708 } else {
4709 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004710 err = -EINVAL;
4711 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4712 err = -E2BIG;
4713 else
4714 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004715 }
4716
NeilBrown67918752014-12-15 12:57:01 +11004717 if (!err) {
4718 mddev->array_sectors = sectors;
4719 if (mddev->pers) {
4720 set_capacity(mddev->gendisk, mddev->array_sectors);
4721 revalidate_disk(mddev->gendisk);
4722 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004723 }
NeilBrown67918752014-12-15 12:57:01 +11004724 mddev_unlock(mddev);
4725 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004726}
4727
4728static struct md_sysfs_entry md_array_size =
4729__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4730 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004731
NeilBrowneae17012005-11-08 21:39:23 -08004732static struct attribute *md_default_attrs[] = {
4733 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004734 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004735 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004736 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004737 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004738 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004739 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004740 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004741 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004742 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004743 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004744 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004745 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004746 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004747 NULL,
4748};
4749
4750static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004751 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004752 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004753 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004754 &md_sync_min.attr,
4755 &md_sync_max.attr,
4756 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004757 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004758 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004759 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004760 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004761 &md_suspend_lo.attr,
4762 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004763 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004764 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004765 NULL,
4766};
NeilBrown411036f2005-11-08 21:39:40 -08004767static struct attribute_group md_redundancy_group = {
4768 .name = NULL,
4769 .attrs = md_redundancy_attrs,
4770};
4771
NeilBrowneae17012005-11-08 21:39:23 -08004772static ssize_t
4773md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4774{
4775 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004776 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004777 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004778
4779 if (!entry->show)
4780 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004781 spin_lock(&all_mddevs_lock);
4782 if (list_empty(&mddev->all_mddevs)) {
4783 spin_unlock(&all_mddevs_lock);
4784 return -EBUSY;
4785 }
4786 mddev_get(mddev);
4787 spin_unlock(&all_mddevs_lock);
4788
NeilBrownb7b17c92014-12-15 12:56:59 +11004789 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004790 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004791 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004792}
4793
4794static ssize_t
4795md_attr_store(struct kobject *kobj, struct attribute *attr,
4796 const char *page, size_t length)
4797{
4798 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004799 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004800 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004801
4802 if (!entry->store)
4803 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004804 if (!capable(CAP_SYS_ADMIN))
4805 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004806 spin_lock(&all_mddevs_lock);
4807 if (list_empty(&mddev->all_mddevs)) {
4808 spin_unlock(&all_mddevs_lock);
4809 return -EBUSY;
4810 }
4811 mddev_get(mddev);
4812 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004813 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004814 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004815 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004816}
4817
4818static void md_free(struct kobject *ko)
4819{
NeilBrownfd01b882011-10-11 16:47:53 +11004820 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004821
4822 if (mddev->sysfs_state)
4823 sysfs_put(mddev->sysfs_state);
4824
NeilBrown6cd18e72015-04-27 14:12:22 +10004825 if (mddev->queue)
4826 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004827 if (mddev->gendisk) {
4828 del_gendisk(mddev->gendisk);
4829 put_disk(mddev->gendisk);
4830 }
NeilBrowna21d1502009-01-09 08:31:09 +11004831
NeilBrowneae17012005-11-08 21:39:23 -08004832 kfree(mddev);
4833}
4834
Emese Revfy52cf25d2010-01-19 02:58:23 +01004835static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004836 .show = md_attr_show,
4837 .store = md_attr_store,
4838};
4839static struct kobj_type md_ktype = {
4840 .release = md_free,
4841 .sysfs_ops = &md_sysfs_ops,
4842 .default_attrs = md_default_attrs,
4843};
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845int mdp_major = 0;
4846
Dan Williams5fd3a172009-03-04 00:57:25 -07004847static void mddev_delayed_delete(struct work_struct *ws)
4848{
NeilBrownfd01b882011-10-11 16:47:53 +11004849 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004850
NeilBrown43a70502009-12-14 12:49:55 +11004851 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004852 kobject_del(&mddev->kobj);
4853 kobject_put(&mddev->kobj);
4854}
4855
NeilBrownefeb53c2009-01-09 08:31:10 +11004856static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004858 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004859 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004861 int partitioned;
4862 int shift;
4863 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004864 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004867 return -ENODEV;
4868
4869 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4870 shift = partitioned ? MdpMinorShift : 0;
4871 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Tejun Heoe804ac72010-10-15 15:36:08 +02004873 /* wait for any previous instance of this device to be
4874 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004875 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004876 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004877
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004878 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004879 error = -EEXIST;
4880 if (mddev->gendisk)
4881 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004882
4883 if (name) {
4884 /* Need to ensure that 'name' is not a duplicate.
4885 */
NeilBrownfd01b882011-10-11 16:47:53 +11004886 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004887 spin_lock(&all_mddevs_lock);
4888
4889 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4890 if (mddev2->gendisk &&
4891 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4892 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004893 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004894 }
4895 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 }
NeilBrown8b765392009-01-09 08:31:08 +11004897
NeilBrown0909dc42009-07-01 12:27:21 +10004898 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004899 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004900 if (!mddev->queue)
4901 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004902 mddev->queue->queuedata = mddev;
4903
NeilBrown409c57f2009-03-31 14:39:39 +11004904 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004905 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 disk = alloc_disk(1 << shift);
4908 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004909 blk_cleanup_queue(mddev->queue);
4910 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004911 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004913 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004915 if (name)
4916 strcpy(disk->disk_name, name);
4917 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004919 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 disk->fops = &md_fops;
4922 disk->private_data = mddev;
4923 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004924 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004925 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004926 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004927 * remove it now.
4928 */
4929 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004931 /* As soon as we call add_disk(), another thread could get
4932 * through to md_open, so make sure it doesn't get too far
4933 */
4934 mutex_lock(&mddev->open_mutex);
4935 add_disk(disk);
4936
Tejun Heoed9e1982008-08-25 19:56:05 +09004937 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4938 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004939 if (error) {
4940 /* This isn't possible, but as kobject_init_and_add is marked
4941 * __must_check, we must do something with the result
4942 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004943 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4944 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004945 error = 0;
4946 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004947 if (mddev->kobj.sd &&
4948 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004949 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004950 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004951 abort:
4952 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004953 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004954 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004955 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004956 }
NeilBrownd3374822009-01-09 08:31:10 +11004957 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004958 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004959}
4960
4961static struct kobject *md_probe(dev_t dev, int *part, void *data)
4962{
4963 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 return NULL;
4965}
4966
NeilBrownefeb53c2009-01-09 08:31:10 +11004967static int add_named_array(const char *val, struct kernel_param *kp)
4968{
4969 /* val must be "md_*" where * is not all digits.
4970 * We allocate an array with a large free minor number, and
4971 * set the name to val. val must not already be an active name.
4972 */
4973 int len = strlen(val);
4974 char buf[DISK_NAME_LEN];
4975
4976 while (len && val[len-1] == '\n')
4977 len--;
4978 if (len >= DISK_NAME_LEN)
4979 return -E2BIG;
4980 strlcpy(buf, val, len+1);
4981 if (strncmp(buf, "md_", 3) != 0)
4982 return -EINVAL;
4983 return md_alloc(0, buf);
4984}
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986static void md_safemode_timeout(unsigned long data)
4987{
NeilBrownfd01b882011-10-11 16:47:53 +11004988 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Neil Brown0fd62b82008-06-28 08:31:36 +10004990 if (!atomic_read(&mddev->writes_pending)) {
4991 mddev->safemode = 1;
4992 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004993 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 md_wakeup_thread(mddev->thread);
4996}
4997
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004998static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
NeilBrownfd01b882011-10-11 16:47:53 +11005000int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001{
NeilBrown2604b702006-01-06 00:20:36 -08005002 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005003 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005004 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
NeilBrowna757e642005-04-16 15:26:42 -07005006 if (list_empty(&mddev->disks))
5007 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
5010 if (mddev->pers)
5011 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005012 /* Cannot run until previous stop completes properly */
5013 if (mddev->sysfs_active)
5014 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 /*
5017 * Analyze all RAID superblock(s)
5018 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005019 if (!mddev->raid_disks) {
5020 if (!mddev->persistent)
5021 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005022 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
NeilBrownd9d166c2006-01-06 00:20:51 -08005025 if (mddev->level != LEVEL_NONE)
5026 request_module("md-level-%d", mddev->level);
5027 else if (mddev->clevel[0])
5028 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 /*
5031 * Drop all container device buffers, from now on
5032 * the only valid external interface is through the md
5033 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005035 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005036 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 continue;
5038 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005039 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005040
5041 /* perform some consistency tests on the device.
5042 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005043 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005044 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005045 if (rdev->meta_bdev) {
5046 /* Nothing to check */;
5047 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005048 if (mddev->dev_sectors &&
5049 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005050 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005051 printk("md: %s: data overlaps metadata\n",
5052 mdname(mddev));
5053 return -EINVAL;
5054 }
5055 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005056 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005057 > rdev->data_offset) {
5058 printk("md: %s: metadata overlaps data\n",
5059 mdname(mddev));
5060 return -EINVAL;
5061 }
5062 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005063 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 }
5065
NeilBrowna167f662010-10-26 18:31:13 +11005066 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005067 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005068
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005070 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005071 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005073 if (mddev->level != LEVEL_NONE)
5074 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5075 mddev->level);
5076 else
5077 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5078 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 return -EINVAL;
5080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005082 if (mddev->level != pers->level) {
5083 mddev->level = pers->level;
5084 mddev->new_level = pers->level;
5085 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005086 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
NeilBrownf6705572006-03-27 01:18:11 -08005088 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005089 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005090 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005091 module_put(pers->owner);
5092 return -EINVAL;
5093 }
5094
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005095 if (pers->sync_request) {
5096 /* Warn if this is a potentially silly
5097 * configuration.
5098 */
5099 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005100 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005101 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005102
NeilBrowndafb20f2012-03-19 12:46:39 +11005103 rdev_for_each(rdev, mddev)
5104 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005105 if (rdev < rdev2 &&
5106 rdev->bdev->bd_contains ==
5107 rdev2->bdev->bd_contains) {
5108 printk(KERN_WARNING
5109 "%s: WARNING: %s appears to be"
5110 " on the same physical disk as"
5111 " %s.\n",
5112 mdname(mddev),
5113 bdevname(rdev->bdev,b),
5114 bdevname(rdev2->bdev,b2));
5115 warned = 1;
5116 }
5117 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005118
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005119 if (warned)
5120 printk(KERN_WARNING
5121 "True protection against single-disk"
5122 " failure might be compromised.\n");
5123 }
5124
NeilBrown657390d2005-08-26 18:34:16 -07005125 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005126 /* may be over-ridden by personality */
5127 mddev->resync_max_sectors = mddev->dev_sectors;
5128
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005129 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
NeilBrown0f9552b2009-12-30 12:08:50 +11005131 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005132 mddev->ro = 2; /* read-only, but switch on first write */
5133
NeilBrown36d091f2014-12-15 12:56:58 +11005134 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005135 if (err)
5136 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005137 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005138 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5139 " but 'external_size' not in effect?\n", __func__);
5140 printk(KERN_ERR
5141 "md: invalid array_size %llu > default size %llu\n",
5142 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005143 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005144 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005145 }
NeilBrown36d091f2014-12-15 12:56:58 +11005146 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005147 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005148 struct bitmap *bitmap;
5149
5150 bitmap = bitmap_create(mddev, -1);
5151 if (IS_ERR(bitmap)) {
5152 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005153 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5154 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005155 } else
5156 mddev->bitmap = bitmap;
5157
NeilBrownb15c2e52006-01-06 00:20:16 -08005158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005160 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005161 if (mddev->private)
5162 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005163 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005164 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005165 bitmap_destroy(mddev);
5166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 }
NeilBrown5c675f82014-12-15 12:56:56 +11005168 if (mddev->queue) {
5169 mddev->queue->backing_dev_info.congested_data = mddev;
5170 mddev->queue->backing_dev_info.congested_fn = md_congested;
5171 }
NeilBrown36d091f2014-12-15 12:56:58 +11005172 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005173 if (mddev->kobj.sd &&
5174 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005175 printk(KERN_WARNING
5176 "md: cannot register extra attributes for %s\n",
5177 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005178 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005179 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005180 mddev->ro = 0;
5181
NeilBrownf72ffdd2014-09-30 14:23:59 +10005182 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005183 atomic_set(&mddev->max_corr_read_errors,
5184 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 mddev->safemode = 0;
NeilBrown16f17b32006-06-26 00:27:37 -07005186 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005188 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005189 spin_lock(&mddev->lock);
5190 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005191 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005192 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005193 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005194 if (rdev->raid_disk >= 0)
5195 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005196 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005197
NeilBrowna4a3d262015-07-17 11:57:30 +10005198 if (mddev->degraded && !mddev->ro)
5199 /* This ensures that recovering status is reported immediately
5200 * via sysfs - until a lack of spares is confirmed.
5201 */
5202 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005204
NeilBrown7a0a5352013-08-27 16:28:23 +10005205 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005206 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
NeilBrownd7603b72006-01-06 00:20:30 -08005208 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005209 sysfs_notify_dirent_safe(mddev->sysfs_state);
5210 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005211 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 return 0;
5213}
NeilBrown390ee602010-06-01 19:37:27 +10005214EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
NeilBrownfd01b882011-10-11 16:47:53 +11005216static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005217{
5218 int err;
5219
5220 err = md_run(mddev);
5221 if (err)
5222 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005223 err = bitmap_load(mddev);
5224 if (err) {
5225 bitmap_destroy(mddev);
5226 goto out;
5227 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005228
5229 md_wakeup_thread(mddev->thread);
5230 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5231
NeilBrownfe60b012010-03-29 11:10:42 +11005232 set_capacity(mddev->gendisk, mddev->array_sectors);
5233 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005234 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005235 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5236out:
5237 return err;
5238}
5239
NeilBrownfd01b882011-10-11 16:47:53 +11005240static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241{
5242 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
Andre Noll80fab1d2008-07-11 22:02:21 +10005244 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005246 return -ENXIO;
5247 if (!mddev->pers)
5248 return -EINVAL;
5249 if (!mddev->ro)
5250 return -EBUSY;
5251 mddev->safemode = 0;
5252 mddev->ro = 0;
5253 set_disk_ro(disk, 0);
5254 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5255 mdname(mddev));
5256 /* Kick recovery or resync if necessary */
5257 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5258 md_wakeup_thread(mddev->thread);
5259 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005260 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262}
5263
NeilBrownfd01b882011-10-11 16:47:53 +11005264static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005265{
5266 mddev->array_sectors = 0;
5267 mddev->external_size = 0;
5268 mddev->dev_sectors = 0;
5269 mddev->raid_disks = 0;
5270 mddev->recovery_cp = 0;
5271 mddev->resync_min = 0;
5272 mddev->resync_max = MaxSector;
5273 mddev->reshape_position = MaxSector;
5274 mddev->external = 0;
5275 mddev->persistent = 0;
5276 mddev->level = LEVEL_NONE;
5277 mddev->clevel[0] = 0;
5278 mddev->flags = 0;
5279 mddev->ro = 0;
5280 mddev->metadata_type[0] = 0;
5281 mddev->chunk_sectors = 0;
5282 mddev->ctime = mddev->utime = 0;
5283 mddev->layout = 0;
5284 mddev->max_disks = 0;
5285 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005286 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005287 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005288 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005289 mddev->new_level = LEVEL_NONE;
5290 mddev->new_layout = 0;
5291 mddev->new_chunk_sectors = 0;
5292 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005293 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005294 mddev->suspend_lo = mddev->suspend_hi = 0;
5295 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5296 mddev->recovery = 0;
5297 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005298 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005299 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005300 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005301 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005302 mddev->bitmap_info.offset = 0;
5303 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005304 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005305 mddev->bitmap_info.chunksize = 0;
5306 mddev->bitmap_info.daemon_sleep = 0;
5307 mddev->bitmap_info.max_write_behind = 0;
5308}
5309
NeilBrownfd01b882011-10-11 16:47:53 +11005310static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005311{
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005312 if (mddev_is_clustered(mddev))
5313 md_cluster_ops->metadata_update_start(mddev);
NeilBrown6b6204e2013-05-09 09:48:30 +10005314 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005315 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005316 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005317 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005318 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005319 }
5320
5321 del_timer_sync(&mddev->safemode_timer);
5322
5323 bitmap_flush(mddev);
5324 md_super_wait(mddev);
5325
NeilBrownb6d428c2013-04-24 11:42:42 +10005326 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005327 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005328 /* mark array as shutdown cleanly */
5329 mddev->in_sync = 1;
5330 md_update_sb(mddev, 1);
5331 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005332 if (mddev_is_clustered(mddev))
5333 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005334}
NeilBrowndefad612011-01-14 09:14:33 +11005335
NeilBrownfd01b882011-10-11 16:47:53 +11005336void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005337{
NeilBrown29f097c2013-11-14 17:54:51 +11005338 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005339 __md_stop_writes(mddev);
5340 mddev_unlock(mddev);
5341}
NeilBrown390ee602010-06-01 19:37:27 +10005342EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005343
NeilBrown5aa61f42014-12-15 12:56:57 +11005344static void mddev_detach(struct mddev *mddev)
5345{
5346 struct bitmap *bitmap = mddev->bitmap;
5347 /* wait for behind writes to complete */
5348 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5349 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5350 mdname(mddev));
5351 /* need to kick something here to make sure I/O goes? */
5352 wait_event(bitmap->behind_wait,
5353 atomic_read(&bitmap->behind_writes) == 0);
5354 }
NeilBrown36d091f2014-12-15 12:56:58 +11005355 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005356 mddev->pers->quiesce(mddev, 1);
5357 mddev->pers->quiesce(mddev, 0);
5358 }
5359 md_unregister_thread(&mddev->thread);
5360 if (mddev->queue)
5361 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5362}
5363
NeilBrown5eff3c42012-11-19 10:47:48 +11005364static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005365{
NeilBrown36d091f2014-12-15 12:56:58 +11005366 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005367 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005368 /* Ensure ->event_work is done */
5369 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005370 spin_lock(&mddev->lock);
5371 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005372 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005373 spin_unlock(&mddev->lock);
5374 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005375 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005376 if (pers->sync_request && mddev->to_remove == NULL)
5377 mddev->to_remove = &md_redundancy_group;
5378 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005379 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005380}
NeilBrown5eff3c42012-11-19 10:47:48 +11005381
5382void md_stop(struct mddev *mddev)
5383{
5384 /* stop the array and free an attached data structures.
5385 * This is called from dm-raid
5386 */
5387 __md_stop(mddev);
5388 bitmap_destroy(mddev);
5389 if (mddev->bio_set)
5390 bioset_free(mddev->bio_set);
5391}
5392
NeilBrown390ee602010-06-01 19:37:27 +10005393EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005394
NeilBrowna05b7ea02012-07-19 15:59:18 +10005395static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005396{
5397 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005398 int did_freeze = 0;
5399
5400 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5401 did_freeze = 1;
5402 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5403 md_wakeup_thread(mddev->thread);
5404 }
NeilBrownf851b602014-12-11 10:02:10 +11005405 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005406 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005407 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005408 /* Thread might be blocked waiting for metadata update
5409 * which will now never happen */
5410 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005411
NeilBrown30b8feb2013-11-14 15:16:17 +11005412 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005413 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5414 &mddev->recovery));
NeilBrown30b8feb2013-11-14 15:16:17 +11005415 mddev_lock_nointr(mddev);
5416
NeilBrowna4bd82d2010-03-29 13:23:10 +11005417 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005418 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005419 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005420 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005421 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005422 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005423 if (did_freeze) {
5424 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005425 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005426 md_wakeup_thread(mddev->thread);
5427 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005428 err = -EBUSY;
5429 goto out;
5430 }
5431 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005432 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005433
5434 err = -ENXIO;
5435 if (mddev->ro==1)
5436 goto out;
5437 mddev->ro = 1;
5438 set_disk_ro(mddev->gendisk, 1);
5439 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005440 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5441 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005442 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005443 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005444 }
5445out:
5446 mutex_unlock(&mddev->open_mutex);
5447 return err;
5448}
5449
NeilBrown9e653b62006-06-26 00:27:58 -07005450/* mode:
5451 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005452 * 2 - stop but do not disassemble array
5453 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005454static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea02012-07-19 15:59:18 +10005455 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005458 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005459 int did_freeze = 0;
5460
5461 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5462 did_freeze = 1;
5463 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5464 md_wakeup_thread(mddev->thread);
5465 }
NeilBrownf851b602014-12-11 10:02:10 +11005466 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005467 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005468 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005469 /* Thread might be blocked waiting for metadata update
5470 * which will now never happen */
5471 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005472
NeilBrown30b8feb2013-11-14 15:16:17 +11005473 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005474 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5475 !test_bit(MD_RECOVERY_RUNNING,
5476 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005477 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
NeilBrownc8c00a62009-08-10 12:50:52 +10005479 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005480 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005481 mddev->sysfs_active ||
5482 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005483 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005484 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005485 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005486 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005487 if (did_freeze) {
5488 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005489 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005490 md_wakeup_thread(mddev->thread);
5491 }
NeilBrown260fa032013-08-27 16:44:13 +10005492 return -EBUSY;
5493 }
NeilBrown6e17b022010-08-07 21:41:19 +10005494 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005495 if (mddev->ro)
5496 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005497
NeilBrowndefad612011-01-14 09:14:33 +11005498 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005499 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005500 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005501
NeilBrowna4bd82d2010-03-29 13:23:10 +11005502 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005503 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005504
NeilBrowndafb20f2012-03-19 12:46:39 +11005505 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005506 if (rdev->raid_disk >= 0)
5507 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005508
NeilBrowna4bd82d2010-03-29 13:23:10 +11005509 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005510 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005511 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005512 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005513
NeilBrowna4bd82d2010-03-29 13:23:10 +11005514 if (mddev->ro)
5515 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005516 } else
5517 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 /*
5519 * Free resources if final stop
5520 */
NeilBrown9e653b62006-06-26 00:27:58 -07005521 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5523
NeilBrown978f9462006-02-02 14:28:05 -08005524 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005525 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005526 struct file *f = mddev->bitmap_info.file;
5527 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005528 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005529 spin_unlock(&mddev->lock);
5530 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005531 }
NeilBrownc3d97142009-12-14 12:49:52 +11005532 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005533
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 export_array(mddev);
5535
NeilBrown6177b472010-03-29 11:37:13 +11005536 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005537 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005538 if (mddev->hold_active == UNTIL_STOP)
5539 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005540 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005541 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005542 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005543 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545}
5546
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005547#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005548static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549{
NeilBrown3cb03002011-10-11 16:45:26 +11005550 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 int err;
5552
NeilBrowna757e642005-04-16 15:26:42 -07005553 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
5556 printk(KERN_INFO "md: running: ");
5557
NeilBrowndafb20f2012-03-19 12:46:39 +11005558 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 char b[BDEVNAME_SIZE];
5560 printk("<%s>", bdevname(rdev->bdev,b));
5561 }
5562 printk("\n");
5563
NeilBrownd710e132008-10-13 11:55:12 +11005564 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 if (err) {
5566 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005567 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 }
5569}
5570
5571/*
5572 * lets try to run arrays based on all disks that have arrived
5573 * until now. (those are in pending_raid_disks)
5574 *
5575 * the method: pick the first pending disk, collect all disks with
5576 * the same UUID, remove all from the pending list and put them into
5577 * the 'same_array' list. Then order this list based on superblock
5578 * update time (freshest comes first), kick out 'old' disks and
5579 * compare superblocks. If everything's fine then run it.
5580 *
5581 * If "unit" is allocated, then bump its reference count
5582 */
5583static void autorun_devices(int part)
5584{
NeilBrown3cb03002011-10-11 16:45:26 +11005585 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005586 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 char b[BDEVNAME_SIZE];
5588
5589 printk(KERN_INFO "md: autorun ...\n");
5590 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005591 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005593 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005595 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596
5597 printk(KERN_INFO "md: considering %s ...\n",
5598 bdevname(rdev0->bdev,b));
5599 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005600 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 if (super_90_load(rdev, rdev0, 0) >= 0) {
5602 printk(KERN_INFO "md: adding %s ...\n",
5603 bdevname(rdev->bdev,b));
5604 list_move(&rdev->same_set, &candidates);
5605 }
5606 /*
5607 * now we have a set of devices, with all of them having
5608 * mostly sane superblocks. It's time to allocate the
5609 * mddev.
5610 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005611 if (part) {
5612 dev = MKDEV(mdp_major,
5613 rdev0->preferred_minor << MdpMinorShift);
5614 unit = MINOR(dev) >> MdpMinorShift;
5615 } else {
5616 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5617 unit = MINOR(dev);
5618 }
5619 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5621 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5622 break;
5623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
5625 md_probe(dev, NULL, NULL);
5626 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005627 if (!mddev || !mddev->gendisk) {
5628 if (mddev)
5629 mddev_put(mddev);
5630 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 "md: cannot allocate memory for md drive.\n");
5632 break;
5633 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005634 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 printk(KERN_WARNING "md: %s locked, cannot run\n",
5636 mdname(mddev));
5637 else if (mddev->raid_disks || mddev->major_version
5638 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005639 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 "md: %s already running, cannot run %s\n",
5641 mdname(mddev), bdevname(rdev0->bdev,b));
5642 mddev_unlock(mddev);
5643 } else {
5644 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005645 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005646 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 list_del_init(&rdev->same_set);
5648 if (bind_rdev_to_array(rdev, mddev))
5649 export_rdev(rdev);
5650 }
5651 autorun_array(mddev);
5652 mddev_unlock(mddev);
5653 }
5654 /* on success, candidates will be empty, on error
5655 * it won't...
5656 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005657 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005658 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 mddev_put(mddev);
5662 }
5663 printk(KERN_INFO "md: ... autorun DONE.\n");
5664}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005665#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
NeilBrownf72ffdd2014-09-30 14:23:59 +10005667static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668{
5669 mdu_version_t ver;
5670
5671 ver.major = MD_MAJOR_VERSION;
5672 ver.minor = MD_MINOR_VERSION;
5673 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5674
5675 if (copy_to_user(arg, &ver, sizeof(ver)))
5676 return -EFAULT;
5677
5678 return 0;
5679}
5680
NeilBrownf72ffdd2014-09-30 14:23:59 +10005681static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682{
5683 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005684 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005685 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
NeilBrown1ca69c42012-10-11 13:37:33 +11005687 nr = working = insync = failed = spare = 0;
5688 rcu_read_lock();
5689 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005691 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 failed++;
5693 else {
5694 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005695 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005696 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 else
5698 spare++;
5699 }
5700 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005701 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
5703 info.major_version = mddev->major_version;
5704 info.minor_version = mddev->minor_version;
5705 info.patch_version = MD_PATCHLEVEL_VERSION;
5706 info.ctime = mddev->ctime;
5707 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005708 info.size = mddev->dev_sectors / 2;
5709 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005710 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 info.nr_disks = nr;
5712 info.raid_disks = mddev->raid_disks;
5713 info.md_minor = mddev->md_minor;
5714 info.not_persistent= !mddev->persistent;
5715
5716 info.utime = mddev->utime;
5717 info.state = 0;
5718 if (mddev->in_sync)
5719 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005720 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005721 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005722 if (mddev_is_clustered(mddev))
5723 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005724 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 info.working_disks = working;
5726 info.failed_disks = failed;
5727 info.spare_disks = spare;
5728
5729 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005730 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
5732 if (copy_to_user(arg, &info, sizeof(info)))
5733 return -EFAULT;
5734
5735 return 0;
5736}
5737
NeilBrownf72ffdd2014-09-30 14:23:59 +10005738static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005739{
5740 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005741 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005742 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005743
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005744 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005745 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005746 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005747
NeilBrown32a76272005-06-21 17:17:14 -07005748 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005749 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005750 /* bitmap enabled */
5751 if (mddev->bitmap_info.file) {
5752 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5753 sizeof(file->pathname));
5754 if (IS_ERR(ptr))
5755 err = PTR_ERR(ptr);
5756 else
5757 memmove(file->pathname, ptr,
5758 sizeof(file->pathname)-(ptr-file->pathname));
5759 }
NeilBrown4af1a042014-12-15 12:57:00 +11005760 spin_unlock(&mddev->lock);
5761
5762 if (err == 0 &&
5763 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005764 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005765
NeilBrown32a76272005-06-21 17:17:14 -07005766 kfree(file);
5767 return err;
5768}
5769
NeilBrownf72ffdd2014-09-30 14:23:59 +10005770static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771{
5772 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005773 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774
5775 if (copy_from_user(&info, arg, sizeof(info)))
5776 return -EFAULT;
5777
NeilBrown1ca69c42012-10-11 13:37:33 +11005778 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005779 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 if (rdev) {
5781 info.major = MAJOR(rdev->bdev->bd_dev);
5782 info.minor = MINOR(rdev->bdev->bd_dev);
5783 info.raid_disk = rdev->raid_disk;
5784 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005785 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005787 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 info.state |= (1<<MD_DISK_ACTIVE);
5789 info.state |= (1<<MD_DISK_SYNC);
5790 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005791 if (test_bit(WriteMostly, &rdev->flags))
5792 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 } else {
5794 info.major = info.minor = 0;
5795 info.raid_disk = -1;
5796 info.state = (1<<MD_DISK_REMOVED);
5797 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005798 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
5800 if (copy_to_user(arg, &info, sizeof(info)))
5801 return -EFAULT;
5802
5803 return 0;
5804}
5805
NeilBrownf72ffdd2014-09-30 14:23:59 +10005806static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807{
5808 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005809 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 dev_t dev = MKDEV(info->major,info->minor);
5811
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005812 if (mddev_is_clustered(mddev) &&
5813 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005814 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005815 mdname(mddev));
5816 return -EINVAL;
5817 }
5818
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5820 return -EOVERFLOW;
5821
5822 if (!mddev->raid_disks) {
5823 int err;
5824 /* expecting a device which has a superblock */
5825 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5826 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005827 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 "md: md_import_device returned %ld\n",
5829 PTR_ERR(rdev));
5830 return PTR_ERR(rdev);
5831 }
5832 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005833 struct md_rdev *rdev0
5834 = list_entry(mddev->disks.next,
5835 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005836 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 .load_super(rdev, rdev0, mddev->minor_version);
5838 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005839 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005841 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 bdevname(rdev0->bdev,b2));
5843 export_rdev(rdev);
5844 return -EINVAL;
5845 }
5846 }
5847 err = bind_rdev_to_array(rdev, mddev);
5848 if (err)
5849 export_rdev(rdev);
5850 return err;
5851 }
5852
5853 /*
5854 * add_new_disk can be used once the array is assembled
5855 * to add "hot spares". They must already have a superblock
5856 * written
5857 */
5858 if (mddev->pers) {
5859 int err;
5860 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005861 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 "%s: personality does not support diskops!\n",
5863 mdname(mddev));
5864 return -EINVAL;
5865 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005866 if (mddev->persistent)
5867 rdev = md_import_device(dev, mddev->major_version,
5868 mddev->minor_version);
5869 else
5870 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005872 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 "md: md_import_device returned %ld\n",
5874 PTR_ERR(rdev));
5875 return PTR_ERR(rdev);
5876 }
NeilBrown1a855a02010-12-09 16:36:28 +11005877 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005878 if (!mddev->persistent) {
5879 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005880 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005881 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005882 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005883 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005884 } else
NeilBrown41158c72005-06-21 17:17:25 -07005885 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005886 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005887 } else
5888 super_types[mddev->major_version].
5889 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005890 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005891 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005892 /* This was a hot-add request, but events doesn't
5893 * match, so reject it.
5894 */
5895 export_rdev(rdev);
5896 return -EINVAL;
5897 }
5898
NeilBrownb2d444d2005-11-08 21:39:31 -08005899 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005900 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5901 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005902 else
5903 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005904
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005905 /*
5906 * check whether the device shows up in other nodes
5907 */
5908 if (mddev_is_clustered(mddev)) {
5909 if (info->state & (1 << MD_DISK_CANDIDATE)) {
5910 /* Through --cluster-confirm */
5911 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005912 err = md_cluster_ops->new_disk_ack(mddev, true);
5913 if (err) {
5914 export_rdev(rdev);
5915 return err;
5916 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005917 } else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
5918 /* --add initiated by this node */
5919 err = md_cluster_ops->add_new_disk_start(mddev, rdev);
5920 if (err) {
5921 md_cluster_ops->add_new_disk_finish(mddev);
5922 export_rdev(rdev);
5923 return err;
5924 }
5925 }
5926 }
5927
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 rdev->raid_disk = -1;
5929 err = bind_rdev_to_array(rdev, mddev);
5930 if (err)
5931 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005932 else
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05005933 err = add_bound_rdev(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005934 if (mddev_is_clustered(mddev) &&
5935 (info->state & (1 << MD_DISK_CLUSTER_ADD)))
5936 md_cluster_ops->add_new_disk_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 return err;
5938 }
5939
5940 /* otherwise, add_new_disk is only allowed
5941 * for major_version==0 superblocks
5942 */
5943 if (mddev->major_version != 0) {
5944 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5945 mdname(mddev));
5946 return -EINVAL;
5947 }
5948
5949 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5950 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005951 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005953 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 "md: error, md_import_device() returned %ld\n",
5955 PTR_ERR(rdev));
5956 return PTR_ERR(rdev);
5957 }
5958 rdev->desc_nr = info->number;
5959 if (info->raid_disk < mddev->raid_disks)
5960 rdev->raid_disk = info->raid_disk;
5961 else
5962 rdev->raid_disk = -1;
5963
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005965 if (info->state & (1<<MD_DISK_SYNC))
5966 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005968 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5969 set_bit(WriteMostly, &rdev->flags);
5970
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 if (!mddev->persistent) {
5972 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005973 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5974 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005975 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005976 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
NeilBrown2bf071b2006-01-06 00:20:55 -08005978 err = bind_rdev_to_array(rdev, mddev);
5979 if (err) {
5980 export_rdev(rdev);
5981 return err;
5982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 }
5984
5985 return 0;
5986}
5987
NeilBrownf72ffdd2014-09-30 14:23:59 +10005988static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989{
5990 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005991 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 rdev = find_rdev(mddev, dev);
5994 if (!rdev)
5995 return -ENXIO;
5996
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005997 if (mddev_is_clustered(mddev))
5998 md_cluster_ops->metadata_update_start(mddev);
5999
NeilBrown3ea8929d2013-04-24 11:42:41 +10006000 clear_bit(Blocked, &rdev->flags);
6001 remove_and_add_spares(mddev, rdev);
6002
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 if (rdev->raid_disk >= 0)
6004 goto busy;
6005
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006006 if (mddev_is_clustered(mddev))
6007 md_cluster_ops->remove_disk(mddev, rdev);
6008
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006009 md_kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07006010 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006011 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006013 if (mddev_is_clustered(mddev))
6014 md_cluster_ops->metadata_update_finish(mddev);
6015
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 return 0;
6017busy:
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006018 if (mddev_is_clustered(mddev))
6019 md_cluster_ops->metadata_update_cancel(mddev);
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006020 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 bdevname(rdev->bdev,b), mdname(mddev));
6022 return -EBUSY;
6023}
6024
NeilBrownf72ffdd2014-09-30 14:23:59 +10006025static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026{
6027 char b[BDEVNAME_SIZE];
6028 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006029 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
6031 if (!mddev->pers)
6032 return -ENODEV;
6033
6034 if (mddev->major_version != 0) {
6035 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6036 " version-0 superblocks.\n",
6037 mdname(mddev));
6038 return -EINVAL;
6039 }
6040 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006041 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 "%s: personality does not support diskops!\n",
6043 mdname(mddev));
6044 return -EINVAL;
6045 }
6046
NeilBrownd710e132008-10-13 11:55:12 +11006047 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006049 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 "md: error, md_import_device() returned %ld\n",
6051 PTR_ERR(rdev));
6052 return -EINVAL;
6053 }
6054
6055 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006056 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006058 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
NeilBrown8190e752009-06-18 08:48:58 +10006060 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
NeilBrownb2d444d2005-11-08 21:39:31 -08006062 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006063 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 "md: can not hot-add faulty %s disk to %s!\n",
6065 bdevname(rdev->bdev,b), mdname(mddev));
6066 err = -EINVAL;
6067 goto abort_export;
6068 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006069
6070 if (mddev_is_clustered(mddev))
6071 md_cluster_ops->metadata_update_start(mddev);
NeilBrownb2d444d2005-11-08 21:39:31 -08006072 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006074 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006075 err = bind_rdev_to_array(rdev, mddev);
6076 if (err)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006077 goto abort_clustered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
6079 /*
6080 * The rest should better be atomic, we can have disk failures
6081 * noticed in interrupt contexts ...
6082 */
6083
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 rdev->raid_disk = -1;
6085
NeilBrown850b2b42006-10-03 01:15:46 -07006086 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006088 if (mddev_is_clustered(mddev))
6089 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 /*
6091 * Kick recovery, maybe this spare has to be added to the
6092 * array immediately.
6093 */
6094 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6095 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006096 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 return 0;
6098
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006099abort_clustered:
6100 if (mddev_is_clustered(mddev))
6101 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102abort_export:
6103 export_rdev(rdev);
6104 return err;
6105}
6106
NeilBrownfd01b882011-10-11 16:47:53 +11006107static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006108{
NeilBrown035328c2014-04-09 12:25:40 +10006109 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006110
NeilBrown36fa3062005-09-09 16:23:45 -07006111 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006112 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006113 return -EBUSY;
6114 if (mddev->recovery || mddev->sync_thread)
6115 return -EBUSY;
6116 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006117 }
6118
NeilBrown36fa3062005-09-09 16:23:45 -07006119 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006120 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006121 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006122
NeilBrown1e594bb2014-12-15 12:57:00 +11006123 if (mddev->bitmap || mddev->bitmap_info.file)
6124 return -EEXIST; /* cannot add when bitmap is present */
6125 f = fget(fd);
6126
6127 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006128 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6129 mdname(mddev));
6130 return -EBADF;
6131 }
6132
NeilBrown1e594bb2014-12-15 12:57:00 +11006133 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006134 if (!S_ISREG(inode->i_mode)) {
6135 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6136 mdname(mddev));
6137 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006138 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006139 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6140 mdname(mddev));
6141 err = -EBADF;
6142 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006143 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6144 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006145 err = -EBUSY;
6146 }
6147 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006148 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006149 return err;
6150 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006151 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006152 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006153 } else if (mddev->bitmap == NULL)
6154 return -ENOENT; /* cannot remove what isn't there */
6155 err = 0;
6156 if (mddev->pers) {
6157 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006158 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006159 struct bitmap *bitmap;
6160
6161 bitmap = bitmap_create(mddev, -1);
6162 if (!IS_ERR(bitmap)) {
6163 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006164 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006165 } else
6166 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006167 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006168 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006169 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006170 fd = -1; /* make sure to put the file */
6171 }
NeilBrown36fa3062005-09-09 16:23:45 -07006172 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006173 }
6174 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006175 struct file *f = mddev->bitmap_info.file;
6176 if (f) {
6177 spin_lock(&mddev->lock);
6178 mddev->bitmap_info.file = NULL;
6179 spin_unlock(&mddev->lock);
6180 fput(f);
6181 }
NeilBrown36fa3062005-09-09 16:23:45 -07006182 }
6183
NeilBrown32a76272005-06-21 17:17:14 -07006184 return err;
6185}
6186
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187/*
6188 * set_array_info is used two different ways
6189 * The original usage is when creating a new array.
6190 * In this usage, raid_disks is > 0 and it together with
6191 * level, size, not_persistent,layout,chunksize determine the
6192 * shape of the array.
6193 * This will always create an array with a type-0.90.0 superblock.
6194 * The newer usage is when assembling an array.
6195 * In this case raid_disks will be 0, and the major_version field is
6196 * use to determine which style super-blocks are to be found on the devices.
6197 * The minor and patch _version numbers are also kept incase the
6198 * super_block handler wishes to interpret them.
6199 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006200static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201{
6202
6203 if (info->raid_disks == 0) {
6204 /* just setting version number for superblock loading */
6205 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006206 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 super_types[info->major_version].name == NULL) {
6208 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006209 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 "md: superblock version %d not known\n",
6211 info->major_version);
6212 return -EINVAL;
6213 }
6214 mddev->major_version = info->major_version;
6215 mddev->minor_version = info->minor_version;
6216 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006217 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006218 /* ensure mddev_put doesn't delete this now that there
6219 * is some minimal configuration.
6220 */
6221 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 return 0;
6223 }
6224 mddev->major_version = MD_MAJOR_VERSION;
6225 mddev->minor_version = MD_MINOR_VERSION;
6226 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6227 mddev->ctime = get_seconds();
6228
6229 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006230 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006231 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 mddev->raid_disks = info->raid_disks;
6233 /* don't set md_minor, it is determined by which /dev/md* was
6234 * openned
6235 */
6236 if (info->state & (1<<MD_SB_CLEAN))
6237 mddev->recovery_cp = MaxSector;
6238 else
6239 mddev->recovery_cp = 0;
6240 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006241 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
6243 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006244 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
6246 mddev->max_disks = MD_SB_DISKS;
6247
NeilBrowne6910632008-02-06 01:39:51 -08006248 if (mddev->persistent)
6249 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006250 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251
NeilBrownc3d97142009-12-14 12:49:52 +11006252 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006253 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006254 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006255
NeilBrownf6705572006-03-27 01:18:11 -08006256 mddev->reshape_position = MaxSector;
6257
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 /*
6259 * Generate a 128 bit UUID
6260 */
6261 get_random_bytes(mddev->uuid, 16);
6262
NeilBrownf6705572006-03-27 01:18:11 -08006263 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006264 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006265 mddev->new_layout = mddev->layout;
6266 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006267 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006268
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 return 0;
6270}
6271
NeilBrownfd01b882011-10-11 16:47:53 +11006272void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006273{
Dan Williamsb522adc2009-03-31 15:00:31 +11006274 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6275
6276 if (mddev->external_size)
6277 return;
6278
Dan Williams1f403622009-03-31 14:59:03 +11006279 mddev->array_sectors = array_sectors;
6280}
6281EXPORT_SYMBOL(md_set_array_sectors);
6282
NeilBrownfd01b882011-10-11 16:47:53 +11006283static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006284{
NeilBrown3cb03002011-10-11 16:45:26 +11006285 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006286 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006287 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006288
6289 if (mddev->pers->resize == NULL)
6290 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006291 /* The "num_sectors" is the number of sectors of each device that
6292 * is used. This can only make sense for arrays with redundancy.
6293 * linear and raid0 always use whatever space is available. We can only
6294 * consider changing this number if no resync or reconstruction is
6295 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006296 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006297 * of each device. If num_sectors is zero, we find the largest size
6298 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006299 */
NeilBrownf851b602014-12-11 10:02:10 +11006300 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6301 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006302 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006303 if (mddev->ro)
6304 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006305
NeilBrowndafb20f2012-03-19 12:46:39 +11006306 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006307 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006308
Andre Nolld71f9f82008-07-11 22:02:22 +10006309 if (fit && (num_sectors == 0 || num_sectors > avail))
6310 num_sectors = avail;
6311 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006312 return -ENOSPC;
6313 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006314 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006315 if (!rv)
6316 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006317 return rv;
6318}
6319
NeilBrownfd01b882011-10-11 16:47:53 +11006320static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006321{
6322 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006323 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006324 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006325 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006326 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006327 if (mddev->ro)
6328 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006329 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006330 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006331 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006332 if (mddev->sync_thread ||
6333 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6334 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006335 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006336
6337 rdev_for_each(rdev, mddev) {
6338 if (mddev->raid_disks < raid_disks &&
6339 rdev->data_offset < rdev->new_data_offset)
6340 return -EINVAL;
6341 if (mddev->raid_disks > raid_disks &&
6342 rdev->data_offset > rdev->new_data_offset)
6343 return -EINVAL;
6344 }
6345
NeilBrown63c70c42006-03-27 01:18:13 -08006346 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006347 if (mddev->delta_disks < 0)
6348 mddev->reshape_backwards = 1;
6349 else if (mddev->delta_disks > 0)
6350 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006351
6352 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006353 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006354 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006355 mddev->reshape_backwards = 0;
6356 }
NeilBrownda943b992006-01-06 00:20:54 -08006357 return rv;
6358}
6359
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360/*
6361 * update_array_info is used to change the configuration of an
6362 * on-line array.
6363 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6364 * fields in the info are checked against the array.
6365 * Any differences that cannot be handled will cause an error.
6366 * Normally, only one change can be managed at a time.
6367 */
NeilBrownfd01b882011-10-11 16:47:53 +11006368static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369{
6370 int rv = 0;
6371 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006372 int state = 0;
6373
6374 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006375 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006376 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
6378 if (mddev->major_version != info->major_version ||
6379 mddev->minor_version != info->minor_version ||
6380/* mddev->patch_version != info->patch_version || */
6381 mddev->ctime != info->ctime ||
6382 mddev->level != info->level ||
6383/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006384 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006385 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006386 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6387 ((state^info->state) & 0xfffffe00)
6388 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 return -EINVAL;
6390 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006391 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6392 cnt++;
6393 if (mddev->raid_disks != info->raid_disks)
6394 cnt++;
6395 if (mddev->layout != info->layout)
6396 cnt++;
6397 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6398 cnt++;
6399 if (cnt == 0)
6400 return 0;
6401 if (cnt > 1)
6402 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
6404 if (mddev->layout != info->layout) {
6405 /* Change layout
6406 * we don't need to do anything at the md level, the
6407 * personality will take care of it all.
6408 */
NeilBrown50ac1682009-06-18 08:47:55 +10006409 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006411 else {
6412 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006413 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006414 if (rv)
6415 mddev->new_layout = mddev->layout;
6416 return rv;
6417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006419 if (mddev_is_clustered(mddev))
6420 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11006421 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006422 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
NeilBrownda943b992006-01-06 00:20:54 -08006424 if (mddev->raid_disks != info->raid_disks)
6425 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
NeilBrown36fa3062005-09-09 16:23:45 -07006427 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006428 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6429 rv = -EINVAL;
6430 goto err;
6431 }
6432 if (mddev->recovery || mddev->sync_thread) {
6433 rv = -EBUSY;
6434 goto err;
6435 }
NeilBrown36fa3062005-09-09 16:23:45 -07006436 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006437 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006438 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006439 if (mddev->bitmap) {
6440 rv = -EEXIST;
6441 goto err;
6442 }
6443 if (mddev->bitmap_info.default_offset == 0) {
6444 rv = -EINVAL;
6445 goto err;
6446 }
NeilBrownc3d97142009-12-14 12:49:52 +11006447 mddev->bitmap_info.offset =
6448 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006449 mddev->bitmap_info.space =
6450 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006451 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006452 bitmap = bitmap_create(mddev, -1);
6453 if (!IS_ERR(bitmap)) {
6454 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006455 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006456 } else
6457 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006458 if (rv)
6459 bitmap_destroy(mddev);
6460 mddev->pers->quiesce(mddev, 0);
6461 } else {
6462 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006463 if (!mddev->bitmap) {
6464 rv = -ENOENT;
6465 goto err;
6466 }
6467 if (mddev->bitmap->storage.file) {
6468 rv = -EINVAL;
6469 goto err;
6470 }
NeilBrown36fa3062005-09-09 16:23:45 -07006471 mddev->pers->quiesce(mddev, 1);
6472 bitmap_destroy(mddev);
6473 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006474 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006475 }
6476 }
NeilBrown850b2b42006-10-03 01:15:46 -07006477 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006478 if (mddev_is_clustered(mddev))
6479 md_cluster_ops->metadata_update_finish(mddev);
6480 return rv;
6481err:
6482 if (mddev_is_clustered(mddev))
6483 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 return rv;
6485}
6486
NeilBrownfd01b882011-10-11 16:47:53 +11006487static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488{
NeilBrown3cb03002011-10-11 16:45:26 +11006489 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006490 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491
6492 if (mddev->pers == NULL)
6493 return -ENODEV;
6494
NeilBrown1ca69c42012-10-11 13:37:33 +11006495 rcu_read_lock();
6496 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006498 err = -ENODEV;
6499 else {
6500 md_error(mddev, rdev);
6501 if (!test_bit(Faulty, &rdev->flags))
6502 err = -EBUSY;
6503 }
6504 rcu_read_unlock();
6505 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506}
6507
Andre Noll2f9618c2008-04-25 18:57:58 +02006508/*
6509 * We have a problem here : there is no easy way to give a CHS
6510 * virtual geometry. We currently pretend that we have a 2 heads
6511 * 4 sectors (with a BIG number of cylinders...). This drives
6512 * dosfs just mad... ;-)
6513 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006514static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6515{
NeilBrownfd01b882011-10-11 16:47:53 +11006516 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006517
6518 geo->heads = 2;
6519 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006520 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006521 return 0;
6522}
6523
Nicolas Schichancb335f82014-01-15 16:58:52 +01006524static inline bool md_ioctl_valid(unsigned int cmd)
6525{
6526 switch (cmd) {
6527 case ADD_NEW_DISK:
6528 case BLKROSET:
6529 case GET_ARRAY_INFO:
6530 case GET_BITMAP_FILE:
6531 case GET_DISK_INFO:
6532 case HOT_ADD_DISK:
6533 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006534 case RAID_AUTORUN:
6535 case RAID_VERSION:
6536 case RESTART_ARRAY_RW:
6537 case RUN_ARRAY:
6538 case SET_ARRAY_INFO:
6539 case SET_BITMAP_FILE:
6540 case SET_DISK_FAULTY:
6541 case STOP_ARRAY:
6542 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006543 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006544 return true;
6545 default:
6546 return false;
6547 }
6548}
6549
Al Viroa39907f2008-03-02 10:31:15 -05006550static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 unsigned int cmd, unsigned long arg)
6552{
6553 int err = 0;
6554 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006555 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006556 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557
Nicolas Schichancb335f82014-01-15 16:58:52 +01006558 if (!md_ioctl_valid(cmd))
6559 return -ENOTTY;
6560
NeilBrown506c9e42011-12-23 10:17:26 +11006561 switch (cmd) {
6562 case RAID_VERSION:
6563 case GET_ARRAY_INFO:
6564 case GET_DISK_INFO:
6565 break;
6566 default:
6567 if (!capable(CAP_SYS_ADMIN))
6568 return -EACCES;
6569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
6571 /*
6572 * Commands dealing with the RAID driver but not any
6573 * particular array:
6574 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006575 switch (cmd) {
6576 case RAID_VERSION:
6577 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006581 case RAID_AUTORUN:
6582 err = 0;
6583 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006584 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006586 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 }
6588
6589 /*
6590 * Commands creating/starting a new array:
6591 */
6592
Al Viroa39907f2008-03-02 10:31:15 -05006593 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594
6595 if (!mddev) {
6596 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006597 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 }
6599
NeilBrown1ca69c42012-10-11 13:37:33 +11006600 /* Some actions do not requires the mutex */
6601 switch (cmd) {
6602 case GET_ARRAY_INFO:
6603 if (!mddev->raid_disks && !mddev->external)
6604 err = -ENODEV;
6605 else
6606 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006607 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006608
6609 case GET_DISK_INFO:
6610 if (!mddev->raid_disks && !mddev->external)
6611 err = -ENODEV;
6612 else
6613 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006614 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006615
6616 case SET_DISK_FAULTY:
6617 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006618 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006619
6620 case GET_BITMAP_FILE:
6621 err = get_bitmap_file(mddev, argp);
6622 goto out;
6623
NeilBrown1ca69c42012-10-11 13:37:33 +11006624 }
6625
NeilBrowna7a3f082012-12-11 13:35:54 +11006626 if (cmd == ADD_NEW_DISK)
6627 /* need to ensure md_delayed_delete() has completed */
6628 flush_workqueue(md_misc_wq);
6629
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006630 if (cmd == HOT_REMOVE_DISK)
6631 /* need to ensure recovery thread has run */
6632 wait_event_interruptible_timeout(mddev->sb_wait,
6633 !test_bit(MD_RECOVERY_NEEDED,
6634 &mddev->flags),
6635 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006636 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6637 /* Need to flush page cache, and ensure no-one else opens
6638 * and writes
6639 */
6640 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006641 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006642 mutex_unlock(&mddev->open_mutex);
6643 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006644 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006645 }
6646 set_bit(MD_STILL_CLOSED, &mddev->flags);
6647 mutex_unlock(&mddev->open_mutex);
6648 sync_blockdev(bdev);
6649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 err = mddev_lock(mddev);
6651 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006652 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6654 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006655 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 }
6657
NeilBrownc02c0ae2012-12-11 13:39:21 +11006658 if (cmd == SET_ARRAY_INFO) {
6659 mdu_array_info_t info;
6660 if (!arg)
6661 memset(&info, 0, sizeof(info));
6662 else if (copy_from_user(&info, argp, sizeof(info))) {
6663 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006664 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006665 }
6666 if (mddev->pers) {
6667 err = update_array_info(mddev, &info);
6668 if (err) {
6669 printk(KERN_WARNING "md: couldn't update"
6670 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006671 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006673 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006674 }
6675 if (!list_empty(&mddev->disks)) {
6676 printk(KERN_WARNING
6677 "md: array %s already has disks!\n",
6678 mdname(mddev));
6679 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006680 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006681 }
6682 if (mddev->raid_disks) {
6683 printk(KERN_WARNING
6684 "md: array %s already initialised!\n",
6685 mdname(mddev));
6686 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006687 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006688 }
6689 err = set_array_info(mddev, &info);
6690 if (err) {
6691 printk(KERN_WARNING "md: couldn't set"
6692 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006693 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006694 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006695 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 }
6697
6698 /*
6699 * Commands querying/configuring an existing array:
6700 */
NeilBrown32a76272005-06-21 17:17:14 -07006701 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006702 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006703 if ((!mddev->raid_disks && !mddev->external)
6704 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6705 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6706 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006708 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 }
6710
6711 /*
6712 * Commands even a read-only array can execute:
6713 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006714 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006715 case RESTART_ARRAY_RW:
6716 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006717 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006718
6719 case STOP_ARRAY:
6720 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006721 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006722
6723 case STOP_ARRAY_RO:
6724 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006725 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006726
NeilBrown3ea8929d2013-04-24 11:42:41 +10006727 case HOT_REMOVE_DISK:
6728 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006729 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006730
NeilBrown7ceb17e2013-04-24 11:42:42 +10006731 case ADD_NEW_DISK:
6732 /* We can support ADD_NEW_DISK on read-only arrays
6733 * on if we are re-adding a preexisting device.
6734 * So require mddev->pers and MD_DISK_SYNC.
6735 */
6736 if (mddev->pers) {
6737 mdu_disk_info_t info;
6738 if (copy_from_user(&info, argp, sizeof(info)))
6739 err = -EFAULT;
6740 else if (!(info.state & (1<<MD_DISK_SYNC)))
6741 /* Need to clear read-only for this */
6742 break;
6743 else
6744 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006745 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006746 }
6747 break;
6748
NeilBrownc02c0ae2012-12-11 13:39:21 +11006749 case BLKROSET:
6750 if (get_user(ro, (int __user *)(arg))) {
6751 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006752 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006753 }
6754 err = -EINVAL;
6755
6756 /* if the bdev is going readonly the value of mddev->ro
6757 * does not matter, no writes are coming
6758 */
6759 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006760 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006761
NeilBrownc02c0ae2012-12-11 13:39:21 +11006762 /* are we are already prepared for writes? */
6763 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006764 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006765
6766 /* transitioning to readauto need only happen for
6767 * arrays that call md_write_start
6768 */
6769 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006771 if (err == 0) {
6772 mddev->ro = 2;
6773 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006774 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006775 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006776 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 }
6778
6779 /*
6780 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006781 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 */
NeilBrown326eb172014-09-30 15:36:28 +10006783 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006784 if (mddev->ro == 2) {
6785 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006786 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006787 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006788 /* mddev_unlock will wake thread */
6789 /* If a device failed while we were read-only, we
6790 * need to make sure the metadata is updated now.
6791 */
6792 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6793 mddev_unlock(mddev);
6794 wait_event(mddev->sb_wait,
6795 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6796 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006797 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006798 }
NeilBrownf91de922005-11-08 21:39:36 -08006799 } else {
6800 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006801 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 }
6804
NeilBrownc02c0ae2012-12-11 13:39:21 +11006805 switch (cmd) {
6806 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006808 mdu_disk_info_t info;
6809 if (copy_from_user(&info, argp, sizeof(info)))
6810 err = -EFAULT;
6811 else
6812 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006813 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006816 case CLUSTERED_DISK_NACK:
6817 if (mddev_is_clustered(mddev))
6818 md_cluster_ops->new_disk_ack(mddev, false);
6819 else
6820 err = -EINVAL;
6821 goto unlock;
6822
NeilBrownc02c0ae2012-12-11 13:39:21 +11006823 case HOT_ADD_DISK:
6824 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006825 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826
NeilBrownc02c0ae2012-12-11 13:39:21 +11006827 case RUN_ARRAY:
6828 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006829 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830
NeilBrownc02c0ae2012-12-11 13:39:21 +11006831 case SET_BITMAP_FILE:
6832 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006833 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006834
NeilBrownc02c0ae2012-12-11 13:39:21 +11006835 default:
6836 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006837 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 }
6839
NeilBrown3adc28d2014-09-30 15:46:41 +10006840unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006841 if (mddev->hold_active == UNTIL_IOCTL &&
6842 err != -EINVAL)
6843 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006845out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 return err;
6847}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006848#ifdef CONFIG_COMPAT
6849static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6850 unsigned int cmd, unsigned long arg)
6851{
6852 switch (cmd) {
6853 case HOT_REMOVE_DISK:
6854 case HOT_ADD_DISK:
6855 case SET_DISK_FAULTY:
6856 case SET_BITMAP_FILE:
6857 /* These take in integer arg, do not convert */
6858 break;
6859 default:
6860 arg = (unsigned long)compat_ptr(arg);
6861 break;
6862 }
6863
6864 return md_ioctl(bdev, mode, cmd, arg);
6865}
6866#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
Al Viroa39907f2008-03-02 10:31:15 -05006868static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869{
6870 /*
6871 * Succeed if we can lock the mddev, which confirms that
6872 * it isn't being stopped right now.
6873 */
NeilBrownfd01b882011-10-11 16:47:53 +11006874 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 int err;
6876
Yuanhan Liu0c098222012-05-22 13:55:32 +10006877 if (!mddev)
6878 return -ENODEV;
6879
NeilBrownd3374822009-01-09 08:31:10 +11006880 if (mddev->gendisk != bdev->bd_disk) {
6881 /* we are racing with mddev_put which is discarding this
6882 * bd_disk.
6883 */
6884 mddev_put(mddev);
6885 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006886 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006887 /* Then retry the open from the top */
6888 return -ERESTARTSYS;
6889 }
6890 BUG_ON(mddev != bdev->bd_disk->private_data);
6891
NeilBrownc8c00a62009-08-10 12:50:52 +10006892 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 goto out;
6894
6895 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006896 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006897 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006898 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006900 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 out:
6902 return err;
6903}
6904
Al Virodb2a1442013-05-05 21:52:57 -04006905static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006907 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006909 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006910 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006913
6914static int md_media_changed(struct gendisk *disk)
6915{
NeilBrownfd01b882011-10-11 16:47:53 +11006916 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006917
6918 return mddev->changed;
6919}
6920
6921static int md_revalidate(struct gendisk *disk)
6922{
NeilBrownfd01b882011-10-11 16:47:53 +11006923 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006924
6925 mddev->changed = 0;
6926 return 0;
6927}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006928static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929{
6930 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006931 .open = md_open,
6932 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006933 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006934#ifdef CONFIG_COMPAT
6935 .compat_ioctl = md_compat_ioctl,
6936#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006937 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006938 .media_changed = md_media_changed,
6939 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940};
6941
NeilBrownf72ffdd2014-09-30 14:23:59 +10006942static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943{
NeilBrown2b8bf342011-10-11 16:48:23 +11006944 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 /*
6947 * md_thread is a 'system-thread', it's priority should be very
6948 * high. We avoid resource deadlocks individually in each
6949 * raid personality. (RAID5 does preallocation) We also use RR and
6950 * the very same RT priority as kswapd, thus we will never get
6951 * into a priority inversion deadlock.
6952 *
6953 * we definitely have to have equal or higher priority than
6954 * bdflush, otherwise bdflush will deadlock if there are too
6955 * many dirty RAID5 blocks.
6956 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957
NeilBrown6985c432005-10-19 21:23:47 -07006958 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006959 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960
NeilBrown93588e22005-11-15 00:09:12 -08006961 /* We need to wait INTERRUPTIBLE so that
6962 * we don't add to the load-average.
6963 * That means we need to be sure no signals are
6964 * pending
6965 */
6966 if (signal_pending(current))
6967 flush_signals(current);
6968
6969 wait_event_interruptible_timeout
6970 (thread->wqueue,
6971 test_bit(THREAD_WAKEUP, &thread->flags)
6972 || kthread_should_stop(),
6973 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974
NeilBrown6c987912011-01-14 09:13:53 +11006975 clear_bit(THREAD_WAKEUP, &thread->flags);
6976 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006977 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006979
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 return 0;
6981}
6982
NeilBrown2b8bf342011-10-11 16:48:23 +11006983void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984{
6985 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006986 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987 set_bit(THREAD_WAKEUP, &thread->flags);
6988 wake_up(&thread->wqueue);
6989 }
6990}
NeilBrown6c144d32014-09-30 16:15:38 +10006991EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
Shaohua Li4ed87312012-10-11 13:34:00 +11006993struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6994 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995{
NeilBrown2b8bf342011-10-11 16:48:23 +11006996 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997
NeilBrown2b8bf342011-10-11 16:48:23 +11006998 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 if (!thread)
7000 return NULL;
7001
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 init_waitqueue_head(&thread->wqueue);
7003
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 thread->run = run;
7005 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007006 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007007 thread->tsk = kthread_run(md_thread, thread,
7008 "%s_%s",
7009 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007010 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007011 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 kfree(thread);
7013 return NULL;
7014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 return thread;
7016}
NeilBrown6c144d32014-09-30 16:15:38 +10007017EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018
NeilBrown2b8bf342011-10-11 16:48:23 +11007019void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020{
NeilBrown2b8bf342011-10-11 16:48:23 +11007021 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007022 if (!thread)
7023 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007024 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007025 /* Locking ensures that mddev_unlock does not wake_up a
7026 * non-existent thread
7027 */
7028 spin_lock(&pers_lock);
7029 *threadp = NULL;
7030 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007031
7032 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 kfree(thread);
7034}
NeilBrown6c144d32014-09-30 16:15:38 +10007035EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
NeilBrownfd01b882011-10-11 16:47:53 +11007037void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038{
NeilBrownb2d444d2005-11-08 21:39:31 -08007039 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007041
NeilBrownde393cd2011-07-28 11:31:48 +10007042 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 return;
7044 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007045 if (mddev->degraded)
7046 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007047 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7049 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7050 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007051 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007052 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07007053 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054}
NeilBrown6c144d32014-09-30 16:15:38 +10007055EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
7057/* seq_file implementation /proc/mdstat */
7058
7059static void status_unused(struct seq_file *seq)
7060{
7061 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007062 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
7064 seq_printf(seq, "unused devices: ");
7065
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007066 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 char b[BDEVNAME_SIZE];
7068 i++;
7069 seq_printf(seq, "%s ",
7070 bdevname(rdev->bdev,b));
7071 }
7072 if (!i)
7073 seq_printf(seq, "<none>");
7074
7075 seq_printf(seq, "\n");
7076}
7077
NeilBrownf7851be2015-07-02 17:12:58 +10007078static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079{
NeilBrowndd71cf62009-05-07 12:49:35 +10007080 sector_t max_sectors, resync, res;
7081 unsigned long dt, db;
7082 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007083 int scale;
7084 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085
NeilBrownc804cde2012-05-21 09:28:33 +10007086 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7087 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007088 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007090 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091
NeilBrownf7851be2015-07-02 17:12:58 +10007092 resync = mddev->curr_resync;
7093 if (resync <= 3) {
7094 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7095 /* Still cleaning up */
7096 resync = max_sectors;
7097 } else
7098 resync -= atomic_read(&mddev->recovery_active);
7099
7100 if (resync == 0) {
7101 if (mddev->recovery_cp < MaxSector) {
7102 seq_printf(seq, "\tresync=PENDING");
7103 return 1;
7104 }
7105 return 0;
7106 }
7107 if (resync < 3) {
7108 seq_printf(seq, "\tresync=DELAYED");
7109 return 1;
7110 }
7111
NeilBrown403df472014-09-30 15:52:29 +10007112 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007113 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007114 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007115 * u32, as those are the requirements for sector_div.
7116 * Thus 'scale' must be at least 10
7117 */
7118 scale = 10;
7119 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007120 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007121 scale++;
7122 }
7123 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007124 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007125
7126 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 {
NeilBrown4588b422006-03-27 01:18:04 -08007128 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 seq_printf(seq, "[");
7130 for (i = 0; i < x; i++)
7131 seq_printf(seq, "=");
7132 seq_printf(seq, ">");
7133 for (i = 0; i < y; i++)
7134 seq_printf(seq, ".");
7135 seq_printf(seq, "] ");
7136 }
NeilBrown4588b422006-03-27 01:18:04 -08007137 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007138 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7139 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007140 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7141 "check" :
7142 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7143 "resync" : "recovery"))),
7144 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007145 (unsigned long long) resync/2,
7146 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147
7148 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149 * dt: time from mark until now
7150 * db: blocks written from mark until now
7151 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007152 *
7153 * rt is a sector_t, so could be 32bit or 64bit.
7154 * So we divide before multiply in case it is 32bit and close
7155 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007156 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007157 * near the end of resync when the number of remaining sectors
7158 * is close to 'db'.
7159 * We then divide rt by 32 after multiplying by db to compensate.
7160 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 */
7162 dt = ((jiffies - mddev->resync_mark) / HZ);
7163 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007164 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7165 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
NeilBrowndd71cf62009-05-07 12:49:35 +10007167 rt = max_sectors - resync; /* number of remaining sectors */
7168 sector_div(rt, db/32+1);
7169 rt *= dt;
7170 rt >>= 5;
7171
7172 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7173 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174
NeilBrownff4e8d92006-07-10 04:44:16 -07007175 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007176 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177}
7178
7179static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7180{
7181 struct list_head *tmp;
7182 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007183 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184
7185 if (l >= 0x10000)
7186 return NULL;
7187 if (!l--)
7188 /* header */
7189 return (void*)1;
7190
7191 spin_lock(&all_mddevs_lock);
7192 list_for_each(tmp,&all_mddevs)
7193 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007194 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 mddev_get(mddev);
7196 spin_unlock(&all_mddevs_lock);
7197 return mddev;
7198 }
7199 spin_unlock(&all_mddevs_lock);
7200 if (!l--)
7201 return (void*)2;/* tail */
7202 return NULL;
7203}
7204
7205static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7206{
7207 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007208 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007209
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 ++*pos;
7211 if (v == (void*)2)
7212 return NULL;
7213
7214 spin_lock(&all_mddevs_lock);
7215 if (v == (void*)1)
7216 tmp = all_mddevs.next;
7217 else
7218 tmp = mddev->all_mddevs.next;
7219 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007220 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 else {
7222 next_mddev = (void*)2;
7223 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 spin_unlock(&all_mddevs_lock);
7226
7227 if (v != (void*)1)
7228 mddev_put(mddev);
7229 return next_mddev;
7230
7231}
7232
7233static void md_seq_stop(struct seq_file *seq, void *v)
7234{
NeilBrownfd01b882011-10-11 16:47:53 +11007235 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236
7237 if (mddev && v != (void*)1 && v != (void*)2)
7238 mddev_put(mddev);
7239}
7240
7241static int md_seq_show(struct seq_file *seq, void *v)
7242{
NeilBrownfd01b882011-10-11 16:47:53 +11007243 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007244 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007245 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246
7247 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007248 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 seq_printf(seq, "Personalities : ");
7250 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007251 list_for_each_entry(pers, &pers_list, list)
7252 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
7254 spin_unlock(&pers_lock);
7255 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007256 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 return 0;
7258 }
7259 if (v == (void*)2) {
7260 status_unused(seq);
7261 return 0;
7262 }
7263
NeilBrown36d091f2014-12-15 12:56:58 +11007264 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7266 seq_printf(seq, "%s : %sactive", mdname(mddev),
7267 mddev->pers ? "" : "in");
7268 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007269 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007271 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007272 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 seq_printf(seq, " %s", mddev->pers->name);
7274 }
7275
Andre Nolldd8ac332009-03-31 14:33:13 +11007276 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007277 rcu_read_lock();
7278 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 char b[BDEVNAME_SIZE];
7280 seq_printf(seq, " %s[%d]",
7281 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007282 if (test_bit(WriteMostly, &rdev->flags))
7283 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007284 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 seq_printf(seq, "(F)");
7286 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007287 }
7288 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007289 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007290 if (test_bit(Replacement, &rdev->flags))
7291 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007292 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007294 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295
7296 if (!list_empty(&mddev->disks)) {
7297 if (mddev->pers)
7298 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007299 (unsigned long long)
7300 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 else
7302 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007303 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007305 if (mddev->persistent) {
7306 if (mddev->major_version != 0 ||
7307 mddev->minor_version != 90) {
7308 seq_printf(seq," super %d.%d",
7309 mddev->major_version,
7310 mddev->minor_version);
7311 }
NeilBrowne6910632008-02-06 01:39:51 -08007312 } else if (mddev->external)
7313 seq_printf(seq, " super external:%s",
7314 mddev->metadata_type);
7315 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007316 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317
7318 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007319 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007320 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007321 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007322 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007323 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007324 }
NeilBrown32a76272005-06-21 17:17:14 -07007325 } else
7326 seq_printf(seq, "\n ");
7327
NeilBrown57148962012-03-19 12:46:40 +11007328 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329
7330 seq_printf(seq, "\n");
7331 }
NeilBrown36d091f2014-12-15 12:56:58 +11007332 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007333
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 return 0;
7335}
7336
Jan Engelhardt110518b2009-05-07 12:49:37 +10007337static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338 .start = md_seq_start,
7339 .next = md_seq_next,
7340 .stop = md_seq_stop,
7341 .show = md_seq_show,
7342};
7343
7344static int md_seq_open(struct inode *inode, struct file *file)
7345{
Kay Sieversf1514632011-07-12 20:48:39 +02007346 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 int error;
7348
7349 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007350 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007351 return error;
7352
7353 seq = file->private_data;
7354 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 return error;
7356}
7357
NeilBrowne2f23b62014-04-09 14:33:51 +10007358static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007359static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7360{
Kay Sieversf1514632011-07-12 20:48:39 +02007361 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007362 int mask;
7363
NeilBrowne2f23b62014-04-09 14:33:51 +10007364 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007365 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007366 poll_wait(filp, &md_event_waiters, wait);
7367
7368 /* always allow read */
7369 mask = POLLIN | POLLRDNORM;
7370
Kay Sieversf1514632011-07-12 20:48:39 +02007371 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007372 mask |= POLLERR | POLLPRI;
7373 return mask;
7374}
7375
Arjan van de Venfa027c22007-02-12 00:55:33 -08007376static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007377 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 .open = md_seq_open,
7379 .read = seq_read,
7380 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007381 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007382 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383};
7384
NeilBrown84fc4b52011-10-11 16:49:58 +11007385int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386{
Chao Yu50bd3772014-09-25 15:28:34 +08007387 printk(KERN_INFO "md: %s personality registered for level %d\n",
7388 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007390 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 spin_unlock(&pers_lock);
7392 return 0;
7393}
NeilBrown6c144d32014-09-30 16:15:38 +10007394EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395
NeilBrown84fc4b52011-10-11 16:49:58 +11007396int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397{
NeilBrown2604b702006-01-06 00:20:36 -08007398 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007400 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 spin_unlock(&pers_lock);
7402 return 0;
7403}
NeilBrown6c144d32014-09-30 16:15:38 +10007404EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405
NeilBrown6022e752015-08-13 12:32:55 +10007406int register_md_cluster_operations(struct md_cluster_operations *ops,
7407 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007408{
NeilBrown6022e752015-08-13 12:32:55 +10007409 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007410 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007411 if (md_cluster_ops != NULL)
7412 ret = -EALREADY;
7413 else {
7414 md_cluster_ops = ops;
7415 md_cluster_mod = module;
7416 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007417 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007418 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007419}
7420EXPORT_SYMBOL(register_md_cluster_operations);
7421
7422int unregister_md_cluster_operations(void)
7423{
7424 spin_lock(&pers_lock);
7425 md_cluster_ops = NULL;
7426 spin_unlock(&pers_lock);
7427 return 0;
7428}
7429EXPORT_SYMBOL(unregister_md_cluster_operations);
7430
7431int md_setup_cluster(struct mddev *mddev, int nodes)
7432{
7433 int err;
7434
7435 err = request_module("md-cluster");
7436 if (err) {
7437 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007438 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007439 }
7440
7441 spin_lock(&pers_lock);
7442 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7443 spin_unlock(&pers_lock);
7444 return -ENOENT;
7445 }
7446 spin_unlock(&pers_lock);
7447
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007448 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007449}
7450
7451void md_cluster_stop(struct mddev *mddev)
7452{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007453 if (!md_cluster_ops)
7454 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007455 md_cluster_ops->leave(mddev);
7456 module_put(md_cluster_mod);
7457}
7458
NeilBrownfd01b882011-10-11 16:47:53 +11007459static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007461 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007463 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464
7465 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007466 rcu_read_lock();
7467 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007469 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7470 (int)part_stat_read(&disk->part0, sectors[1]) -
7471 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007472 /* sync IO will cause sync_io to increase before the disk_stats
7473 * as sync_io is counted when a request starts, and
7474 * disk_stats is counted when it completes.
7475 * So resync activity will cause curr_events to be smaller than
7476 * when there was no such activity.
7477 * non-sync IO will cause disk_stat to increase without
7478 * increasing sync_io so curr_events will (eventually)
7479 * be larger than it was before. Once it becomes
7480 * substantially larger, the test below will cause
7481 * the array to appear non-idle, and resync will slow
7482 * down.
7483 * If there is a lot of outstanding resync activity when
7484 * we set last_event to curr_events, then all that activity
7485 * completing might cause the array to appear non-idle
7486 * and resync will be slowed down even though there might
7487 * not have been non-resync activity. This will only
7488 * happen once though. 'last_events' will soon reflect
7489 * the state where there is little or no outstanding
7490 * resync requests, and further resync activity will
7491 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007492 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007494 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495 rdev->last_events = curr_events;
7496 idle = 0;
7497 }
7498 }
NeilBrown4b809912008-07-21 17:05:25 +10007499 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 return idle;
7501}
7502
NeilBrownfd01b882011-10-11 16:47:53 +11007503void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504{
7505 /* another "blocks" (512byte) blocks have been synced */
7506 atomic_sub(blocks, &mddev->recovery_active);
7507 wake_up(&mddev->recovery_wait);
7508 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007509 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007510 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 md_wakeup_thread(mddev->thread);
7512 // stop recovery, signal do_sync ....
7513 }
7514}
NeilBrown6c144d32014-09-30 16:15:38 +10007515EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516
NeilBrown06d91a52005-06-21 17:17:12 -07007517/* md_write_start(mddev, bi)
7518 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007519 * in superblock) before writing, schedule a superblock update
7520 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007521 */
NeilBrownfd01b882011-10-11 16:47:53 +11007522void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523{
Neil Brown0fd62b82008-06-28 08:31:36 +10007524 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007525 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007526 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007527
NeilBrownf91de922005-11-08 21:39:36 -08007528 BUG_ON(mddev->ro == 1);
7529 if (mddev->ro == 2) {
7530 /* need to switch to read/write */
7531 mddev->ro = 0;
7532 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7533 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007534 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007535 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007536 }
NeilBrown06d91a52005-06-21 17:17:12 -07007537 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007538 if (mddev->safemode == 1)
7539 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007540 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007541 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007542 if (mddev->in_sync) {
7543 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007544 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007545 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007546 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007547 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007548 }
NeilBrown85572d72014-12-15 12:56:56 +11007549 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007550 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007551 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007552 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007553 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007554 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555}
NeilBrown6c144d32014-09-30 16:15:38 +10007556EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557
NeilBrownfd01b882011-10-11 16:47:53 +11007558void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559{
7560 if (atomic_dec_and_test(&mddev->writes_pending)) {
7561 if (mddev->safemode == 2)
7562 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007563 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7565 }
7566}
NeilBrown6c144d32014-09-30 16:15:38 +10007567EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568
NeilBrown2a2275d2007-01-26 00:57:11 -08007569/* md_allow_write(mddev)
7570 * Calling this ensures that the array is marked 'active' so that writes
7571 * may proceed without blocking. It is important to call this before
7572 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7573 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007574 *
7575 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7576 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007577 */
NeilBrownfd01b882011-10-11 16:47:53 +11007578int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007579{
7580 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007581 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007582 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007583 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007584 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007585 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007586
NeilBrown85572d72014-12-15 12:56:56 +11007587 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007588 if (mddev->in_sync) {
7589 mddev->in_sync = 0;
7590 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007591 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007592 if (mddev->safemode_delay &&
7593 mddev->safemode == 0)
7594 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007595 spin_unlock(&mddev->lock);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007596 if (mddev_is_clustered(mddev))
7597 md_cluster_ops->metadata_update_start(mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08007598 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007599 if (mddev_is_clustered(mddev))
7600 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007601 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007602 } else
NeilBrown85572d72014-12-15 12:56:56 +11007603 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007604
NeilBrown070dc6d2010-08-30 17:33:34 +10007605 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007606 return -EAGAIN;
7607 else
7608 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007609}
7610EXPORT_SYMBOL_GPL(md_allow_write);
7611
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612#define SYNC_MARKS 10
7613#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007614#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007615void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616{
Shaohua Li4ed87312012-10-11 13:34:00 +11007617 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007618 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 unsigned int currspeed = 0,
7620 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007621 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007623 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624 sector_t mark_cnt[SYNC_MARKS];
7625 int last_mark,m;
7626 struct list_head *tmp;
7627 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007628 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007629 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007630 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007631 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632
7633 /* just incase thread restarts... */
7634 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7635 return;
NeilBrown3991b312014-05-28 13:39:23 +10007636 if (mddev->ro) {/* never try to sync a read-only array */
7637 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007638 return;
NeilBrown3991b312014-05-28 13:39:23 +10007639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640
NeilBrown61df9d92006-10-03 01:15:57 -07007641 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007642 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007643 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007644 action = "check";
7645 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007646 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007647 action = "repair";
7648 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007649 desc = "resync";
7650 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7651 desc = "reshape";
7652 else
7653 desc = "recovery";
7654
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007655 mddev->last_sync_action = action ?: desc;
7656
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 /* we overload curr_resync somewhat here.
7658 * 0 == not engaged in resync at all
7659 * 2 == checking that there is no conflict with another sync
7660 * 1 == like 2, but have yielded to allow conflicting resync to
7661 * commense
7662 * other == active in resync - this many blocks
7663 *
7664 * Before starting a resync we must have set curr_resync to
7665 * 2, and then checked that every "conflicting" array has curr_resync
7666 * less than ours. When we find one that is the same or higher
7667 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7668 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7669 * This will mean we have to start checking from the beginning again.
7670 *
7671 */
7672
7673 do {
7674 mddev->curr_resync = 2;
7675
7676 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007677 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007679 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 if (mddev2 == mddev)
7681 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007682 if (!mddev->parallel_resync
7683 && mddev2->curr_resync
7684 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 DEFINE_WAIT(wq);
7686 if (mddev < mddev2 && mddev->curr_resync == 2) {
7687 /* arbitrarily yield */
7688 mddev->curr_resync = 1;
7689 wake_up(&resync_wait);
7690 }
7691 if (mddev > mddev2 && mddev->curr_resync == 1)
7692 /* no need to wait here, we can wait the next
7693 * time 'round when curr_resync == 2
7694 */
7695 continue;
NeilBrown97441972008-09-19 11:49:54 +10007696 /* We need to wait 'interruptible' so as not to
7697 * contribute to the load average, and not to
7698 * be caught by 'softlockup'
7699 */
7700 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007701 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007702 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007703 printk(KERN_INFO "md: delaying %s of %s"
7704 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007706 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007708 if (signal_pending(current))
7709 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710 schedule();
7711 finish_wait(&resync_wait, &wq);
7712 goto try_again;
7713 }
7714 finish_wait(&resync_wait, &wq);
7715 }
7716 }
7717 } while (mddev->curr_resync < 2);
7718
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007719 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007720 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007722 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 */
7724 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007725 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007726 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007727 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7728 j = mddev->resync_min;
7729 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007730 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007731
NeilBrownccfcc3c2006-03-27 01:18:09 -08007732 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007733 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007734 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007735 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007736 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007737 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007738 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007739 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007740 if (rdev->raid_disk >= 0 &&
7741 !test_bit(Faulty, &rdev->flags) &&
7742 !test_bit(In_sync, &rdev->flags) &&
7743 rdev->recovery_offset < j)
7744 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007745 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007746
7747 /* If there is a bitmap, we need to make sure all
7748 * writes that started before we added a spare
7749 * complete before we start doing a recovery.
7750 * Otherwise the write might complete and (via
7751 * bitmap_endwrite) set a bit in the bitmap after the
7752 * recovery has checked that bit and skipped that
7753 * region.
7754 */
7755 if (mddev->bitmap) {
7756 mddev->pers->quiesce(mddev, 1);
7757 mddev->pers->quiesce(mddev, 0);
7758 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760
NeilBrown61df9d92006-10-03 01:15:57 -07007761 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7762 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7763 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007764 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007765 "(but not more than %d KB/sec) for %s.\n",
7766 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767
NeilBrowneea1bf32009-03-31 14:27:02 +11007768 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007769
NeilBrown57afd892005-06-21 17:17:13 -07007770 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 for (m = 0; m < SYNC_MARKS; m++) {
7772 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007773 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 }
7775 last_mark = 0;
7776 mddev->resync_mark = mark[last_mark];
7777 mddev->resync_mark_cnt = mark_cnt[last_mark];
7778
7779 /*
7780 * Tune reconstruction:
7781 */
7782 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007783 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7784 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785
7786 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787 last_check = 0;
7788
7789 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007790 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007791 "md: resuming %s of %s from checkpoint.\n",
7792 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007794 } else
7795 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007796 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007797 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7798 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007799 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007801 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007802 md_cluster_ops->resync_start(mddev, j, max_sectors);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007803
majianpeng7c2c57c2012-07-03 12:12:26 +10007804 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007806 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807
NeilBrown57afd892005-06-21 17:17:13 -07007808 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007809
NeilBrown7a91ee12009-05-26 12:57:21 +10007810 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7811 ((mddev->curr_resync > mddev->curr_resync_completed &&
7812 (mddev->curr_resync - mddev->curr_resync_completed)
7813 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007814 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007815 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007816 >= mddev->resync_max - mddev->curr_resync_completed ||
7817 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007818 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007819 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007820 wait_event(mddev->recovery_wait,
7821 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007822 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007823 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7824 j > mddev->recovery_cp)
7825 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007826 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007827 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007828 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007829 }
NeilBrownacb180b2009-04-14 16:28:34 +10007830
NeilBrownc91abf52013-11-19 12:02:01 +11007831 while (j >= mddev->resync_max &&
7832 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007833 /* As this condition is controlled by user-space,
7834 * we can block indefinitely, so use '_interruptible'
7835 * to avoid triggering warnings.
7836 */
7837 flush_signals(current); /* just in case */
7838 wait_event_interruptible(mddev->recovery_wait,
7839 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007840 || test_bit(MD_RECOVERY_INTR,
7841 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007842 }
NeilBrownacb180b2009-04-14 16:28:34 +10007843
NeilBrownc91abf52013-11-19 12:02:01 +11007844 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7845 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007846
NeilBrown09314792015-02-19 16:04:40 +11007847 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07007848 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007849 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007850 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 }
NeilBrown57afd892005-06-21 17:17:13 -07007852
7853 if (!skipped) { /* actual IO requested */
7854 io_sectors += sectors;
7855 atomic_add(sectors, &mddev->recovery_active);
7856 }
7857
NeilBrowne875ece2011-07-28 11:39:24 +10007858 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7859 break;
7860
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10007862 if (j > max_sectors)
7863 /* when skipping, extra large numbers can be returned. */
7864 j = max_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
NeilBrown5ed1df22015-07-24 13:27:08 +10007933 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7934 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7935 mddev->curr_resync > 2) {
7936 mddev->curr_resync_completed = mddev->curr_resync;
7937 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939 /* tell personality that we are finished */
NeilBrown09314792015-02-19 16:04:40 +11007940 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941
NeilBrowndfc70642008-05-23 13:04:39 -07007942 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007943 mddev->curr_resync > 2) {
7944 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7945 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7946 if (mddev->curr_resync >= mddev->recovery_cp) {
7947 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007948 "md: checkpointing %s of %s.\n",
7949 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007950 if (test_bit(MD_RECOVERY_ERROR,
7951 &mddev->recovery))
7952 mddev->recovery_cp =
7953 mddev->curr_resync_completed;
7954 else
7955 mddev->recovery_cp =
7956 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007957 }
7958 } else
7959 mddev->recovery_cp = MaxSector;
7960 } else {
7961 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7962 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007963 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007964 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007965 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007966 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007967 !test_bit(Faulty, &rdev->flags) &&
7968 !test_bit(In_sync, &rdev->flags) &&
7969 rdev->recovery_offset < mddev->curr_resync)
7970 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007971 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007974 skip:
Guoqing Jiangdc737d72015-07-10 16:54:04 +08007975 if (mddev_is_clustered(mddev))
7976 md_cluster_ops->resync_finish(mddev);
7977
NeilBrown17571282006-12-10 02:20:52 -08007978 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979
NeilBrown23da4222014-12-15 12:57:01 +11007980 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11007981 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7982 /* We completed so min/max setting can be forgotten if used. */
7983 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7984 mddev->resync_min = 0;
7985 mddev->resync_max = MaxSector;
7986 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7987 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10007988 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007990 spin_unlock(&mddev->lock);
7991
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007994 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995}
NeilBrown29269552006-03-27 01:18:10 -08007996EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997
NeilBrown746d3202013-04-24 11:42:41 +10007998static int remove_and_add_spares(struct mddev *mddev,
7999 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008000{
NeilBrown3cb03002011-10-11 16:45:26 +11008001 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008002 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008003 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008004
NeilBrowndafb20f2012-03-19 12:46:39 +11008005 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008006 if ((this == NULL || rdev == this) &&
8007 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008008 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008009 (test_bit(Faulty, &rdev->flags) ||
8010 ! test_bit(In_sync, &rdev->flags)) &&
8011 atomic_read(&rdev->nr_pending)==0) {
8012 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008013 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008014 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008015 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008016 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008017 }
8018 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008019 if (removed && mddev->kobj.sd)
8020 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008021
NeilBrown746d3202013-04-24 11:42:41 +10008022 if (this)
8023 goto no_add;
8024
NeilBrowndafb20f2012-03-19 12:46:39 +11008025 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11008026 if (rdev->raid_disk >= 0 &&
8027 !test_bit(In_sync, &rdev->flags) &&
8028 !test_bit(Faulty, &rdev->flags))
8029 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008030 if (rdev->raid_disk >= 0)
8031 continue;
8032 if (test_bit(Faulty, &rdev->flags))
8033 continue;
8034 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11008035 ! (rdev->saved_raid_disk >= 0 &&
8036 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10008037 continue;
8038
NeilBrown7eb41882014-01-14 15:55:14 +11008039 if (rdev->saved_raid_disk < 0)
8040 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008041 if (mddev->pers->
8042 hot_add_disk(mddev, rdev) == 0) {
8043 if (sysfs_link_rdev(mddev, rdev))
8044 /* failure here is OK */;
8045 spares++;
8046 md_new_event(mddev);
8047 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008048 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008049 }
NeilBrown746d3202013-04-24 11:42:41 +10008050no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008051 if (removed)
8052 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008053 return spares;
8054}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008055
NeilBrownac05f252014-09-30 08:10:42 +10008056static void md_start_sync(struct work_struct *ws)
8057{
8058 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8059
8060 mddev->sync_thread = md_register_thread(md_do_sync,
8061 mddev,
8062 "resync");
8063 if (!mddev->sync_thread) {
8064 printk(KERN_ERR "%s: could not start resync"
8065 " thread...\n",
8066 mdname(mddev));
8067 /* leave the spares where they are, it shouldn't hurt */
8068 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8069 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8070 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8071 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8072 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008073 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008074 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8075 &mddev->recovery))
8076 if (mddev->sysfs_action)
8077 sysfs_notify_dirent_safe(mddev->sysfs_action);
8078 } else
8079 md_wakeup_thread(mddev->sync_thread);
8080 sysfs_notify_dirent_safe(mddev->sysfs_action);
8081 md_new_event(mddev);
8082}
8083
Linus Torvalds1da177e2005-04-16 15:20:36 -07008084/*
8085 * This routine is regularly called by all per-raid-array threads to
8086 * deal with generic issues like resync and super-block update.
8087 * Raid personalities that don't have a thread (linear/raid0) do not
8088 * need this as they never do any recovery or update the superblock.
8089 *
8090 * It does not do any resync itself, but rather "forks" off other threads
8091 * to do that as needed.
8092 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8093 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008094 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095 * and wakeups up this thread which will reap the thread and finish up.
8096 * This thread also removes any faulty devices (with nr_pending == 0).
8097 *
8098 * The overall approach is:
8099 * 1/ if the superblock needs updating, update it.
8100 * 2/ If a recovery thread is running, don't do anything else.
8101 * 3/ If recovery has finished, clean up, possibly marking spares active.
8102 * 4/ If there are any faulty devices, remove them.
8103 * 5/ If array is degraded, try to add spares devices
8104 * 6/ If array has spares or is not in-sync, start a resync thread.
8105 */
NeilBrownfd01b882011-10-11 16:47:53 +11008106void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008108 if (mddev->suspended)
8109 return;
8110
NeilBrown5f404022005-06-21 17:17:16 -07008111 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008112 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113
NeilBrownfca4d842005-06-21 17:17:11 -07008114 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008115 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008116 printk(KERN_INFO "md: %s in immediate safe mode\n",
8117 mdname(mddev));
8118 mddev->safemode = 2;
8119 }
8120 flush_signals(current);
8121 }
8122
NeilBrownc89a8ee2008-08-05 15:54:13 +10008123 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8124 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008126 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008128 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008129 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008130 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8131 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132 ))
8133 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008134
NeilBrowndf5b89b2006-03-27 01:18:20 -08008135 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008136 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008137
NeilBrownc89a8ee2008-08-05 15:54:13 +10008138 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008139 struct md_rdev *rdev;
8140 if (!mddev->external && mddev->in_sync)
8141 /* 'Blocked' flag not needed as failed devices
8142 * will be recorded if array switched to read/write.
8143 * Leaving it set will prevent the device
8144 * from being removed.
8145 */
8146 rdev_for_each(rdev, mddev)
8147 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008148 /* On a read-only array we can:
8149 * - remove failed devices
8150 * - add already-in_sync devices if the array itself
8151 * is in-sync.
8152 * As we only add devices that are already in-sync,
8153 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008154 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008155 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008156 /* There is no thread, but we need to call
8157 * ->spare_active and clear saved_raid_disk
8158 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008159 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008160 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008161 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008162 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008163 goto unlock;
8164 }
8165
NeilBrown31a59e32008-04-30 00:52:30 -07008166 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008167 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008168 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008169 if (mddev->safemode &&
8170 !atomic_read(&mddev->writes_pending) &&
8171 !mddev->in_sync &&
8172 mddev->recovery_cp == MaxSector) {
8173 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008174 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008175 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008176 }
8177 if (mddev->safemode == 1)
8178 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008179 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008180 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008181 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008182 }
NeilBrownfca4d842005-06-21 17:17:11 -07008183
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008184 if (mddev->flags & MD_UPDATE_SB_FLAGS) {
8185 if (mddev_is_clustered(mddev))
8186 md_cluster_ops->metadata_update_start(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07008187 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008188 if (mddev_is_clustered(mddev))
8189 md_cluster_ops->metadata_update_finish(mddev);
8190 }
NeilBrown06d91a52005-06-21 17:17:12 -07008191
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8193 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8194 /* resync/recovery still happening */
8195 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8196 goto unlock;
8197 }
8198 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008199 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200 goto unlock;
8201 }
Neil Brown72a23c22008-06-28 08:31:41 +10008202 /* Set RUNNING before clearing NEEDED to avoid
8203 * any transients in the value of "sync_action".
8204 */
NeilBrown72f36d52012-10-11 14:25:57 +11008205 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008206 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008207 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008208 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008209 /* Clear some bits that don't mean anything, but
8210 * might be left set
8211 */
NeilBrown24dd4692005-11-08 21:39:26 -08008212 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8213 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214
NeilBrowned209582012-04-24 10:23:14 +10008215 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8216 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008217 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218 /* no recovery is running.
8219 * remove any failed drives, then
8220 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008221 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222 * the personality to fail the re-add.
8223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008225 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008226 if (mddev->pers->check_reshape == NULL ||
8227 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008228 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008229 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008230 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008231 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008232 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008233 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8234 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008235 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008236 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008237 } else if (mddev->recovery_cp < MaxSector) {
8238 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008239 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008240 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8241 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008242 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008243
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008245 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008246 /* We are adding a device or devices to an array
8247 * which has the bitmap stored on all devices.
8248 * So make sure all bitmap pages get written
8249 */
8250 bitmap_write_all(mddev->bitmap);
8251 }
NeilBrownac05f252014-09-30 08:10:42 +10008252 INIT_WORK(&mddev->del_work, md_start_sync);
8253 queue_work(md_misc_wq, &mddev->del_work);
8254 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255 }
NeilBrownac05f252014-09-30 08:10:42 +10008256 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008257 if (!mddev->sync_thread) {
8258 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008259 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008260 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8261 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008262 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008263 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008264 }
NeilBrownac05f252014-09-30 08:10:42 +10008265 unlock:
8266 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267 mddev_unlock(mddev);
8268 }
8269}
NeilBrown6c144d32014-09-30 16:15:38 +10008270EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008272void md_reap_sync_thread(struct mddev *mddev)
8273{
8274 struct md_rdev *rdev;
8275
8276 /* resync has finished, collect result */
8277 md_unregister_thread(&mddev->sync_thread);
8278 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8279 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8280 /* success...*/
8281 /* activate any spares */
8282 if (mddev->pers->spare_active(mddev)) {
8283 sysfs_notify(&mddev->kobj, NULL,
8284 "degraded");
8285 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8286 }
8287 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008288 if (mddev_is_clustered(mddev))
8289 md_cluster_ops->metadata_update_start(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008290 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8291 mddev->pers->finish_reshape)
8292 mddev->pers->finish_reshape(mddev);
8293
8294 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008295 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008296 */
NeilBrownf4667222013-12-09 12:04:56 +11008297 if (!mddev->degraded)
8298 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008299 rdev->saved_raid_disk = -1;
8300
8301 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008302 if (mddev_is_clustered(mddev))
8303 md_cluster_ops->metadata_update_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008304 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008305 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008306 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8307 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8308 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8309 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008310 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008311 /* flag recovery needed just to double check */
8312 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8313 sysfs_notify_dirent_safe(mddev->sysfs_action);
8314 md_new_event(mddev);
8315 if (mddev->event_work.func)
8316 queue_work(md_misc_wq, &mddev->event_work);
8317}
NeilBrown6c144d32014-09-30 16:15:38 +10008318EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008319
NeilBrownfd01b882011-10-11 16:47:53 +11008320void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008321{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008322 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008323 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008324 !test_bit(Blocked, &rdev->flags) &&
8325 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008326 msecs_to_jiffies(5000));
8327 rdev_dec_pending(rdev, mddev);
8328}
8329EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8330
NeilBrownc6563a82012-05-21 09:27:00 +10008331void md_finish_reshape(struct mddev *mddev)
8332{
8333 /* called be personality module when reshape completes. */
8334 struct md_rdev *rdev;
8335
8336 rdev_for_each(rdev, mddev) {
8337 if (rdev->data_offset > rdev->new_data_offset)
8338 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8339 else
8340 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8341 rdev->data_offset = rdev->new_data_offset;
8342 }
8343}
8344EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008345
8346/* Bad block management.
8347 * We can record which blocks on each device are 'bad' and so just
8348 * fail those blocks, or that stripe, rather than the whole device.
8349 * Entries in the bad-block table are 64bits wide. This comprises:
8350 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8351 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8352 * A 'shift' can be set so that larger blocks are tracked and
8353 * consequently larger devices can be covered.
8354 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8355 *
8356 * Locking of the bad-block table uses a seqlock so md_is_badblock
8357 * might need to retry if it is very unlucky.
8358 * We will sometimes want to check for bad blocks in a bi_end_io function,
8359 * so we use the write_seqlock_irq variant.
8360 *
8361 * When looking for a bad block we specify a range and want to
8362 * know if any block in the range is bad. So we binary-search
8363 * to the last range that starts at-or-before the given endpoint,
8364 * (or "before the sector after the target range")
8365 * then see if it ends after the given start.
8366 * We return
8367 * 0 if there are no known bad blocks in the range
8368 * 1 if there are known bad block which are all acknowledged
8369 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8370 * plus the start/length of the first bad section we overlap.
8371 */
8372int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8373 sector_t *first_bad, int *bad_sectors)
8374{
8375 int hi;
majianpengab056132012-11-06 17:13:44 +08008376 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008377 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008378 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008379 sector_t target = s + sectors;
8380 unsigned seq;
8381
8382 if (bb->shift > 0) {
8383 /* round the start down, and the end up */
8384 s >>= bb->shift;
8385 target += (1<<bb->shift) - 1;
8386 target >>= bb->shift;
8387 sectors = target - s;
8388 }
8389 /* 'target' is now the first block after the bad range */
8390
8391retry:
8392 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008393 lo = 0;
8394 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008395 hi = bb->count;
8396
8397 /* Binary search between lo and hi for 'target'
8398 * i.e. for the last range that starts before 'target'
8399 */
8400 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8401 * are known not to be the last range before target.
8402 * VARIANT: hi-lo is the number of possible
8403 * ranges, and decreases until it reaches 1
8404 */
8405 while (hi - lo > 1) {
8406 int mid = (lo + hi) / 2;
8407 sector_t a = BB_OFFSET(p[mid]);
8408 if (a < target)
8409 /* This could still be the one, earlier ranges
8410 * could not. */
8411 lo = mid;
8412 else
8413 /* This and later ranges are definitely out. */
8414 hi = mid;
8415 }
8416 /* 'lo' might be the last that started before target, but 'hi' isn't */
8417 if (hi > lo) {
8418 /* need to check all range that end after 's' to see if
8419 * any are unacknowledged.
8420 */
8421 while (lo >= 0 &&
8422 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8423 if (BB_OFFSET(p[lo]) < target) {
8424 /* starts before the end, and finishes after
8425 * the start, so they must overlap
8426 */
8427 if (rv != -1 && BB_ACK(p[lo]))
8428 rv = 1;
8429 else
8430 rv = -1;
8431 *first_bad = BB_OFFSET(p[lo]);
8432 *bad_sectors = BB_LEN(p[lo]);
8433 }
8434 lo--;
8435 }
8436 }
8437
8438 if (read_seqretry(&bb->lock, seq))
8439 goto retry;
8440
8441 return rv;
8442}
8443EXPORT_SYMBOL_GPL(md_is_badblock);
8444
8445/*
8446 * Add a range of bad blocks to the table.
8447 * This might extend the table, or might contract it
8448 * if two adjacent ranges can be merged.
8449 * We binary-search to find the 'insertion' point, then
8450 * decide how best to handle it.
8451 */
8452static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8453 int acknowledged)
8454{
8455 u64 *p;
8456 int lo, hi;
8457 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008458 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008459
8460 if (bb->shift < 0)
8461 /* badblocks are disabled */
8462 return 0;
8463
8464 if (bb->shift) {
8465 /* round the start down, and the end up */
8466 sector_t next = s + sectors;
8467 s >>= bb->shift;
8468 next += (1<<bb->shift) - 1;
8469 next >>= bb->shift;
8470 sectors = next - s;
8471 }
8472
Bian Yu905b0292013-10-12 01:10:03 -04008473 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008474
8475 p = bb->page;
8476 lo = 0;
8477 hi = bb->count;
8478 /* Find the last range that starts at-or-before 's' */
8479 while (hi - lo > 1) {
8480 int mid = (lo + hi) / 2;
8481 sector_t a = BB_OFFSET(p[mid]);
8482 if (a <= s)
8483 lo = mid;
8484 else
8485 hi = mid;
8486 }
8487 if (hi > lo && BB_OFFSET(p[lo]) > s)
8488 hi = lo;
8489
8490 if (hi > lo) {
8491 /* we found a range that might merge with the start
8492 * of our new range
8493 */
8494 sector_t a = BB_OFFSET(p[lo]);
8495 sector_t e = a + BB_LEN(p[lo]);
8496 int ack = BB_ACK(p[lo]);
8497 if (e >= s) {
8498 /* Yes, we can merge with a previous range */
8499 if (s == a && s + sectors >= e)
8500 /* new range covers old */
8501 ack = acknowledged;
8502 else
8503 ack = ack && acknowledged;
8504
8505 if (e < s + sectors)
8506 e = s + sectors;
8507 if (e - a <= BB_MAX_LEN) {
8508 p[lo] = BB_MAKE(a, e-a, ack);
8509 s = e;
8510 } else {
8511 /* does not all fit in one range,
8512 * make p[lo] maximal
8513 */
8514 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8515 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8516 s = a + BB_MAX_LEN;
8517 }
8518 sectors = e - s;
8519 }
8520 }
8521 if (sectors && hi < bb->count) {
8522 /* 'hi' points to the first range that starts after 's'.
8523 * Maybe we can merge with the start of that range */
8524 sector_t a = BB_OFFSET(p[hi]);
8525 sector_t e = a + BB_LEN(p[hi]);
8526 int ack = BB_ACK(p[hi]);
8527 if (a <= s + sectors) {
8528 /* merging is possible */
8529 if (e <= s + sectors) {
8530 /* full overlap */
8531 e = s + sectors;
8532 ack = acknowledged;
8533 } else
8534 ack = ack && acknowledged;
8535
8536 a = s;
8537 if (e - a <= BB_MAX_LEN) {
8538 p[hi] = BB_MAKE(a, e-a, ack);
8539 s = e;
8540 } else {
8541 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8542 s = a + BB_MAX_LEN;
8543 }
8544 sectors = e - s;
8545 lo = hi;
8546 hi++;
8547 }
8548 }
8549 if (sectors == 0 && hi < bb->count) {
8550 /* we might be able to combine lo and hi */
8551 /* Note: 's' is at the end of 'lo' */
8552 sector_t a = BB_OFFSET(p[hi]);
8553 int lolen = BB_LEN(p[lo]);
8554 int hilen = BB_LEN(p[hi]);
8555 int newlen = lolen + hilen - (s - a);
8556 if (s >= a && newlen < BB_MAX_LEN) {
8557 /* yes, we can combine them */
8558 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8559 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8560 memmove(p + hi, p + hi + 1,
8561 (bb->count - hi - 1) * 8);
8562 bb->count--;
8563 }
8564 }
8565 while (sectors) {
8566 /* didn't merge (it all).
8567 * Need to add a range just before 'hi' */
8568 if (bb->count >= MD_MAX_BADBLOCKS) {
8569 /* No room for more */
8570 rv = 0;
8571 break;
8572 } else {
8573 int this_sectors = sectors;
8574 memmove(p + hi + 1, p + hi,
8575 (bb->count - hi) * 8);
8576 bb->count++;
8577
8578 if (this_sectors > BB_MAX_LEN)
8579 this_sectors = BB_MAX_LEN;
8580 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8581 sectors -= this_sectors;
8582 s += this_sectors;
8583 }
8584 }
8585
8586 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008587 if (!acknowledged)
8588 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008589 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008590
8591 return rv;
8592}
8593
NeilBrown3cb03002011-10-11 16:45:26 +11008594int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008595 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008596{
NeilBrownc6563a82012-05-21 09:27:00 +10008597 int rv;
8598 if (is_new)
8599 s += rdev->new_data_offset;
8600 else
8601 s += rdev->data_offset;
8602 rv = md_set_badblocks(&rdev->badblocks,
8603 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008604 if (rv) {
8605 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008606 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008607 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
NeilBrown55ce74d2015-08-14 11:11:10 +10008608 set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008609 md_wakeup_thread(rdev->mddev->thread);
8610 }
8611 return rv;
8612}
8613EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8614
8615/*
8616 * Remove a range of bad blocks from the table.
8617 * This may involve extending the table if we spilt a region,
8618 * but it must not fail. So if the table becomes full, we just
8619 * drop the remove request.
8620 */
8621static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8622{
8623 u64 *p;
8624 int lo, hi;
8625 sector_t target = s + sectors;
8626 int rv = 0;
8627
8628 if (bb->shift > 0) {
8629 /* When clearing we round the start up and the end down.
8630 * This should not matter as the shift should align with
8631 * the block size and no rounding should ever be needed.
8632 * However it is better the think a block is bad when it
8633 * isn't than to think a block is not bad when it is.
8634 */
8635 s += (1<<bb->shift) - 1;
8636 s >>= bb->shift;
8637 target >>= bb->shift;
8638 sectors = target - s;
8639 }
8640
8641 write_seqlock_irq(&bb->lock);
8642
8643 p = bb->page;
8644 lo = 0;
8645 hi = bb->count;
8646 /* Find the last range that starts before 'target' */
8647 while (hi - lo > 1) {
8648 int mid = (lo + hi) / 2;
8649 sector_t a = BB_OFFSET(p[mid]);
8650 if (a < target)
8651 lo = mid;
8652 else
8653 hi = mid;
8654 }
8655 if (hi > lo) {
8656 /* p[lo] is the last range that could overlap the
8657 * current range. Earlier ranges could also overlap,
8658 * but only this one can overlap the end of the range.
8659 */
8660 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8661 /* Partial overlap, leave the tail of this range */
8662 int ack = BB_ACK(p[lo]);
8663 sector_t a = BB_OFFSET(p[lo]);
8664 sector_t end = a + BB_LEN(p[lo]);
8665
8666 if (a < s) {
8667 /* we need to split this range */
8668 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008669 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008670 goto out;
8671 }
8672 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8673 bb->count++;
8674 p[lo] = BB_MAKE(a, s-a, ack);
8675 lo++;
8676 }
8677 p[lo] = BB_MAKE(target, end - target, ack);
8678 /* there is no longer an overlap */
8679 hi = lo;
8680 lo--;
8681 }
8682 while (lo >= 0 &&
8683 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8684 /* This range does overlap */
8685 if (BB_OFFSET(p[lo]) < s) {
8686 /* Keep the early parts of this range. */
8687 int ack = BB_ACK(p[lo]);
8688 sector_t start = BB_OFFSET(p[lo]);
8689 p[lo] = BB_MAKE(start, s - start, ack);
8690 /* now low doesn't overlap, so.. */
8691 break;
8692 }
8693 lo--;
8694 }
8695 /* 'lo' is strictly before, 'hi' is strictly after,
8696 * anything between needs to be discarded
8697 */
8698 if (hi - lo > 1) {
8699 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8700 bb->count -= (hi - lo - 1);
8701 }
8702 }
8703
8704 bb->changed = 1;
8705out:
8706 write_sequnlock_irq(&bb->lock);
8707 return rv;
8708}
8709
NeilBrownc6563a82012-05-21 09:27:00 +10008710int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8711 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008712{
NeilBrownc6563a82012-05-21 09:27:00 +10008713 if (is_new)
8714 s += rdev->new_data_offset;
8715 else
8716 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008717 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008718 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008719}
8720EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8721
8722/*
8723 * Acknowledge all bad blocks in a list.
8724 * This only succeeds if ->changed is clear. It is used by
8725 * in-kernel metadata updates
8726 */
8727void md_ack_all_badblocks(struct badblocks *bb)
8728{
8729 if (bb->page == NULL || bb->changed)
8730 /* no point even trying */
8731 return;
8732 write_seqlock_irq(&bb->lock);
8733
majianpengecb178b2012-03-19 12:46:42 +11008734 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008735 u64 *p = bb->page;
8736 int i;
8737 for (i = 0; i < bb->count ; i++) {
8738 if (!BB_ACK(p[i])) {
8739 sector_t start = BB_OFFSET(p[i]);
8740 int len = BB_LEN(p[i]);
8741 p[i] = BB_MAKE(start, len, 1);
8742 }
8743 }
NeilBrownde393cd2011-07-28 11:31:48 +10008744 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008745 }
8746 write_sequnlock_irq(&bb->lock);
8747}
8748EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8749
NeilBrown16c791a2011-07-28 11:31:47 +10008750/* sysfs access to bad-blocks list.
8751 * We present two files.
8752 * 'bad-blocks' lists sector numbers and lengths of ranges that
8753 * are recorded as bad. The list is truncated to fit within
8754 * the one-page limit of sysfs.
8755 * Writing "sector length" to this file adds an acknowledged
8756 * bad block list.
8757 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8758 * been acknowledged. Writing to this file adds bad blocks
8759 * without acknowledging them. This is largely for testing.
8760 */
8761
8762static ssize_t
8763badblocks_show(struct badblocks *bb, char *page, int unack)
8764{
8765 size_t len;
8766 int i;
8767 u64 *p = bb->page;
8768 unsigned seq;
8769
8770 if (bb->shift < 0)
8771 return 0;
8772
8773retry:
8774 seq = read_seqbegin(&bb->lock);
8775
8776 len = 0;
8777 i = 0;
8778
8779 while (len < PAGE_SIZE && i < bb->count) {
8780 sector_t s = BB_OFFSET(p[i]);
8781 unsigned int length = BB_LEN(p[i]);
8782 int ack = BB_ACK(p[i]);
8783 i++;
8784
8785 if (unack && ack)
8786 continue;
8787
8788 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8789 (unsigned long long)s << bb->shift,
8790 length << bb->shift);
8791 }
NeilBrownde393cd2011-07-28 11:31:48 +10008792 if (unack && len == 0)
8793 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008794
8795 if (read_seqretry(&bb->lock, seq))
8796 goto retry;
8797
8798 return len;
8799}
8800
8801#define DO_DEBUG 1
8802
8803static ssize_t
8804badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8805{
8806 unsigned long long sector;
8807 int length;
8808 char newline;
8809#ifdef DO_DEBUG
8810 /* Allow clearing via sysfs *only* for testing/debugging.
8811 * Normally only a successful write may clear a badblock
8812 */
8813 int clear = 0;
8814 if (page[0] == '-') {
8815 clear = 1;
8816 page++;
8817 }
8818#endif /* DO_DEBUG */
8819
8820 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8821 case 3:
8822 if (newline != '\n')
8823 return -EINVAL;
8824 case 2:
8825 if (length <= 0)
8826 return -EINVAL;
8827 break;
8828 default:
8829 return -EINVAL;
8830 }
8831
8832#ifdef DO_DEBUG
8833 if (clear) {
8834 md_clear_badblocks(bb, sector, length);
8835 return len;
8836 }
8837#endif /* DO_DEBUG */
8838 if (md_set_badblocks(bb, sector, length, !unack))
8839 return len;
8840 else
8841 return -ENOSPC;
8842}
8843
Adrian Bunk75c96f82005-05-05 16:16:09 -07008844static int md_notify_reboot(struct notifier_block *this,
8845 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846{
8847 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008848 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008849 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850
NeilBrownc744a652012-03-19 12:46:37 +11008851 for_each_mddev(mddev, tmp) {
8852 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008853 if (mddev->pers)
8854 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008855 if (mddev->persistent)
8856 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008857 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008858 }
NeilBrownc744a652012-03-19 12:46:37 +11008859 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 }
NeilBrownc744a652012-03-19 12:46:37 +11008861 /*
8862 * certain more exotic SCSI devices are known to be
8863 * volatile wrt too early system reboots. While the
8864 * right place to handle this issue is the given
8865 * driver, we do want to have a safe RAID driver ...
8866 */
8867 if (need_delay)
8868 mdelay(1000*1);
8869
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870 return NOTIFY_DONE;
8871}
8872
Adrian Bunk75c96f82005-05-05 16:16:09 -07008873static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874 .notifier_call = md_notify_reboot,
8875 .next = NULL,
8876 .priority = INT_MAX, /* before any real devices */
8877};
8878
8879static void md_geninit(void)
8880{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008881 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008883 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884}
8885
Adrian Bunk75c96f82005-05-05 16:16:09 -07008886static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008887{
Tejun Heoe804ac72010-10-15 15:36:08 +02008888 int ret = -ENOMEM;
8889
Tejun Heoada609e2011-01-25 14:35:54 +01008890 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008891 if (!md_wq)
8892 goto err_wq;
8893
8894 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8895 if (!md_misc_wq)
8896 goto err_misc_wq;
8897
8898 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8899 goto err_md;
8900
8901 if ((ret = register_blkdev(0, "mdp")) < 0)
8902 goto err_mdp;
8903 mdp_major = ret;
8904
NeilBrownaf5628f2014-07-31 13:54:54 +10008905 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008906 md_probe, NULL, NULL);
8907 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908 md_probe, NULL, NULL);
8909
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008911 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008912
8913 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008914 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915
Tejun Heoe804ac72010-10-15 15:36:08 +02008916err_mdp:
8917 unregister_blkdev(MD_MAJOR, "md");
8918err_md:
8919 destroy_workqueue(md_misc_wq);
8920err_misc_wq:
8921 destroy_workqueue(md_wq);
8922err_wq:
8923 return ret;
8924}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008926void md_reload_sb(struct mddev *mddev)
8927{
8928 struct md_rdev *rdev, *tmp;
8929
8930 rdev_for_each_safe(rdev, tmp, mddev) {
8931 rdev->sb_loaded = 0;
8932 ClearPageUptodate(rdev->sb_page);
8933 }
8934 mddev->raid_disks = 0;
8935 analyze_sbs(mddev);
8936 rdev_for_each_safe(rdev, tmp, mddev) {
8937 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8938 /* since we don't write to faulty devices, we figure out if the
8939 * disk is faulty by comparing events
8940 */
8941 if (mddev->events > sb->events)
8942 set_bit(Faulty, &rdev->flags);
8943 }
8944
8945}
8946EXPORT_SYMBOL(md_reload_sb);
8947
Linus Torvalds1da177e2005-04-16 15:20:36 -07008948#ifndef MODULE
8949
8950/*
8951 * Searches all registered partitions for autorun RAID arrays
8952 * at boot time.
8953 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008954
8955static LIST_HEAD(all_detected_devices);
8956struct detected_devices_node {
8957 struct list_head list;
8958 dev_t dev;
8959};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960
8961void md_autodetect_dev(dev_t dev)
8962{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008963 struct detected_devices_node *node_detected_dev;
8964
8965 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8966 if (node_detected_dev) {
8967 node_detected_dev->dev = dev;
8968 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8969 } else {
8970 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8971 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973}
8974
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975static void autostart_arrays(int part)
8976{
NeilBrown3cb03002011-10-11 16:45:26 +11008977 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008978 struct detected_devices_node *node_detected_dev;
8979 dev_t dev;
8980 int i_scanned, i_passed;
8981
8982 i_scanned = 0;
8983 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008984
8985 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8986
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008987 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8988 i_scanned++;
8989 node_detected_dev = list_entry(all_detected_devices.next,
8990 struct detected_devices_node, list);
8991 list_del(&node_detected_dev->list);
8992 dev = node_detected_dev->dev;
8993 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008994 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995 if (IS_ERR(rdev))
8996 continue;
8997
NeilBrown403df472014-09-30 15:52:29 +10008998 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999 continue;
NeilBrown403df472014-09-30 15:52:29 +10009000
NeilBrownd0fae182008-03-04 14:29:31 -08009001 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009002 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009003 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009005
9006 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
9007 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009008
9009 autorun_devices(part);
9010}
9011
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009012#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009013
9014static __exit void md_exit(void)
9015{
NeilBrownfd01b882011-10-11 16:47:53 +11009016 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009017 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009018 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009019
NeilBrownaf5628f2014-07-31 13:54:54 +10009020 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009021 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009022
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009023 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024 unregister_blkdev(mdp_major, "mdp");
9025 unregister_reboot_notifier(&md_notifier);
9026 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009027
9028 /* We cannot unload the modules while some process is
9029 * waiting for us in select() or poll() - wake them up
9030 */
9031 md_unloading = 1;
9032 while (waitqueue_active(&md_event_waiters)) {
9033 /* not safe to leave yet */
9034 wake_up(&md_event_waiters);
9035 msleep(delay);
9036 delay += delay;
9037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009039
NeilBrown29ac4aa2008-02-06 01:39:58 -08009040 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009042 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009043 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009044 destroy_workqueue(md_misc_wq);
9045 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046}
9047
Dan Williams685784a2007-07-09 11:56:42 -07009048subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009049module_exit(md_exit)
9050
NeilBrownf91de922005-11-08 21:39:36 -08009051static int get_ro(char *buffer, struct kernel_param *kp)
9052{
9053 return sprintf(buffer, "%d", start_readonly);
9054}
9055static int set_ro(const char *val, struct kernel_param *kp)
9056{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009057 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009058}
9059
NeilBrown80ca3a42006-07-10 04:44:18 -07009060module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9061module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009062module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009063
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009065MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009066MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009067MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);