blob: 3592beb6931e16c653d019838a80d6e1578e4fb8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10003 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050056#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110059static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
NeilBrown01f96c02011-09-21 15:30:20 +100062/* pers_list is a list of registered personalities protected
63 * by pers_lock.
64 * pers_lock does extra service to protect accesses to
65 * mddev->thread when the mutex cannot be held.
66 */
NeilBrown2604b702006-01-06 00:20:36 -080067static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_SPINLOCK(pers_lock);
69
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050070struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050071EXPORT_SYMBOL(md_cluster_ops);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050072struct module *md_cluster_mod;
73EXPORT_SYMBOL(md_cluster_mod);
74
Bernd Schubert90b08712008-05-23 13:04:38 -070075static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020076static struct workqueue_struct *md_wq;
77static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070078
NeilBrown746d3202013-04-24 11:42:41 +100079static int remove_and_add_spares(struct mddev *mddev,
80 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110081static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Robert Becker1e509152009-12-14 12:49:58 +110084 * Default number of read corrections we'll attempt on an rdev
85 * before ejecting it from the array. We divide the read error
86 * count by 2 for every hour elapsed between read errors.
87 */
88#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
89/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
91 * is 1000 KB/sec, so the extra system load does not show up that much.
92 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070093 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * subsystem is idle. There is also an 'absolute maximum' reconstruction
95 * speed limit - in case reconstruction slows down your system despite
96 * idle IO detection.
97 *
98 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080099 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101
102static int sysctl_speed_limit_min = 1000;
103static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_min ?
107 mddev->sync_speed_min : sysctl_speed_limit_min;
108}
109
NeilBrownfd01b882011-10-11 16:47:53 +1100110static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800111{
112 return mddev->sync_speed_max ?
113 mddev->sync_speed_max : sysctl_speed_limit_max;
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct ctl_table_header *raid_table_header;
117
Joe Perches82592c32013-11-14 15:16:18 +1100118static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .procname = "speed_limit_min",
121 .data = &sysctl_speed_limit_min,
122 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700123 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800124 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 },
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .procname = "speed_limit_max",
128 .data = &sysctl_speed_limit_max,
129 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700130 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800131 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800133 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Joe Perches82592c32013-11-14 15:16:18 +1100136static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .procname = "raid",
139 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700140 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .child = raid_table,
142 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800143 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Joe Perches82592c32013-11-14 15:16:18 +1100146static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .procname = "dev",
149 .maxlen = 0,
150 .mode = 0555,
151 .child = raid_dir_table,
152 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800153 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700156static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
NeilBrownf91de922005-11-08 21:39:36 -0800158static int start_readonly;
159
NeilBrowna167f662010-10-26 18:31:13 +1100160/* bio_clone_mddev
161 * like bio_clone, but with a local bio set
162 */
163
NeilBrowna167f662010-10-26 18:31:13 +1100164struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100165 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100166{
167 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100168
169 if (!mddev || !mddev->bio_set)
170 return bio_alloc(gfp_mask, nr_iovecs);
171
Kent Overstreet395c72a2012-09-06 15:34:55 -0700172 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100173 if (!b)
174 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100175 return b;
176}
177EXPORT_SYMBOL_GPL(bio_alloc_mddev);
178
179struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100180 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100181{
NeilBrowna167f662010-10-26 18:31:13 +1100182 if (!mddev || !mddev->bio_set)
183 return bio_clone(bio, gfp_mask);
184
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700185 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100186}
187EXPORT_SYMBOL_GPL(bio_clone_mddev);
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
NeilBrownd7603b72006-01-06 00:20:30 -0800190 * We have a system wide 'event count' that is incremented
191 * on any 'interesting' event, and readers of /proc/mdstat
192 * can use 'poll' or 'select' to find out when the event
193 * count increases.
194 *
195 * Events are:
196 * start array, stop array, error, add device, remove device,
197 * start build, activate spare
198 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800199static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800200static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100201void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800202{
203 atomic_inc(&md_event_count);
204 wake_up(&md_event_waiters);
205}
NeilBrown29269552006-03-27 01:18:10 -0800206EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800207
NeilBrownc331eb02006-05-30 21:27:13 -0700208/* Alternate version that can be called from interrupts
209 * when calling sysfs_notify isn't needed.
210 */
NeilBrownfd01b882011-10-11 16:47:53 +1100211static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700212{
213 atomic_inc(&md_event_count);
214 wake_up(&md_event_waiters);
215}
216
NeilBrownd7603b72006-01-06 00:20:30 -0800217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Enables to iterate over all existing md arrays
219 * all_mddevs_lock protects this list.
220 */
221static LIST_HEAD(all_mddevs);
222static DEFINE_SPINLOCK(all_mddevs_lock);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
225 * iterates through all used mddevs in the system.
226 * We take care to grab the all_mddevs_lock whenever navigating
227 * the list, and to always hold a refcount when unlocked.
228 * Any code which breaks out of this loop while own
229 * a reference to the current mddev and must mddev_put it.
230 */
NeilBrownfd01b882011-10-11 16:47:53 +1100231#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000233 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100234 _tmp = all_mddevs.next; \
235 _mddev = NULL;}); \
236 ({ if (_tmp != &all_mddevs) \
237 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100239 if (_mddev) mddev_put(_mddev); \
240 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
241 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100243 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 )
245
NeilBrown409c57f2009-03-31 14:39:39 +1100246/* Rather than calling directly into the personality make_request function,
247 * IO requests come here first so that we can check if the device is
248 * being suspended pending a reconfiguration.
249 * We hold a refcount over the call to ->make_request. By the time that
250 * call has finished, the bio has been linked into some internal structure
251 * and so is visible to ->quiesce(), so we don't need the refcount any more.
252 */
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) &&
Song Liuc4d4c912015-08-13 14:31:54 -07001611 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX)
NeilBrownbe6800a2010-05-18 10:17:09 +10001612 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)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001631 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001632 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) {
Song Liuc4d4c912015-08-13 14:31:54 -07001636 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001638 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001639 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
Song Liubac624f2015-08-13 14:31:55 -07001641 case MD_DISK_ROLE_JOURNAL: /* journal device */
1642 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1643 /* journal device without journal feature */
1644 printk(KERN_WARNING
1645 "md: journal device provided without journal feature, ignoring the device\n");
1646 return -EINVAL;
1647 }
1648 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001649 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Song Liubac624f2015-08-13 14:31:55 -07001650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 default:
NeilBrownf4667222013-12-09 12:04:56 +11001652 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001653 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001654 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001655 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001656 if (!(le32_to_cpu(sb->feature_map) &
1657 MD_FEATURE_RECOVERY_BITMAP))
1658 rdev->saved_raid_disk = -1;
1659 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001660 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 rdev->raid_disk = role;
1662 break;
1663 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001664 if (sb->devflags & WriteMostly1)
1665 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001666 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1667 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001668 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001669 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return 0;
1672}
1673
NeilBrownfd01b882011-10-11 16:47:53 +11001674static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001677 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 int max_dev, i;
1679 /* make rdev->sb match mddev and rdev data. */
1680
Namhyung Kim65a06f062011-07-27 11:00:36 +10001681 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 sb->feature_map = 0;
1684 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001685 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 memset(sb->pad3, 0, sizeof(sb->pad3));
1687
1688 sb->utime = cpu_to_le64((__u64)mddev->utime);
1689 sb->events = cpu_to_le64(mddev->events);
1690 if (mddev->in_sync)
1691 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1692 else
1693 sb->resync_offset = cpu_to_le64(0);
1694
NeilBrown1c05b4b2006-10-21 10:24:08 -07001695 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001696
NeilBrownf0ca3402006-02-02 14:28:04 -08001697 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001698 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001699 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001700 sb->level = cpu_to_le32(mddev->level);
1701 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001702
NeilBrownaeb9b2112011-08-25 14:43:08 +10001703 if (test_bit(WriteMostly, &rdev->flags))
1704 sb->devflags |= WriteMostly1;
1705 else
1706 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001707 sb->data_offset = cpu_to_le64(rdev->data_offset);
1708 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001709
NeilBrownc3d97142009-12-14 12:49:52 +11001710 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1711 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001712 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001713 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001714
1715 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001716 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001717 sb->feature_map |=
1718 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1719 sb->recovery_offset =
1720 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001721 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1722 sb->feature_map |=
1723 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001724 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001725 /* Note: recovery_offset and journal_tail share space */
1726 if (test_bit(Journal, &rdev->flags))
1727 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001728 if (test_bit(Replacement, &rdev->flags))
1729 sb->feature_map |=
1730 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001731
NeilBrownf6705572006-03-27 01:18:11 -08001732 if (mddev->reshape_position != MaxSector) {
1733 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1734 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1735 sb->new_layout = cpu_to_le32(mddev->new_layout);
1736 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1737 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001738 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001739 if (mddev->delta_disks == 0 &&
1740 mddev->reshape_backwards)
1741 sb->feature_map
1742 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001743 if (rdev->new_data_offset != rdev->data_offset) {
1744 sb->feature_map
1745 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1746 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1747 - rdev->data_offset));
1748 }
NeilBrownf6705572006-03-27 01:18:11 -08001749 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001750
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001751 if (mddev_is_clustered(mddev))
1752 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1753
NeilBrown2699b672011-07-28 11:31:47 +10001754 if (rdev->badblocks.count == 0)
1755 /* Nothing to do for bad blocks*/ ;
1756 else if (sb->bblog_offset == 0)
1757 /* Cannot record bad blocks on this device */
1758 md_error(mddev, rdev);
1759 else {
1760 struct badblocks *bb = &rdev->badblocks;
1761 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1762 u64 *p = bb->page;
1763 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1764 if (bb->changed) {
1765 unsigned seq;
1766
1767retry:
1768 seq = read_seqbegin(&bb->lock);
1769
1770 memset(bbp, 0xff, PAGE_SIZE);
1771
1772 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001773 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001774 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1775 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001776 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001777 }
NeilBrownd0962932012-03-19 12:46:41 +11001778 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001779 if (read_seqretry(&bb->lock, seq))
1780 goto retry;
1781
1782 bb->sector = (rdev->sb_start +
1783 (int)le32_to_cpu(sb->bblog_offset));
1784 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001785 }
1786 }
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001789 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (rdev2->desc_nr+1 > max_dev)
1791 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001792
NeilBrown70471da2009-08-03 10:59:57 +10001793 if (max_dev > le32_to_cpu(sb->max_dev)) {
1794 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001795 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001796 rdev->sb_size = max_dev * 2 + 256;
1797 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1798 if (rdev->sb_size & bmask)
1799 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001800 } else
1801 max_dev = le32_to_cpu(sb->max_dev);
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 for (i=0; i<max_dev;i++)
Song Liuc4d4c912015-08-13 14:31:54 -07001804 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001805
NeilBrowndafb20f2012-03-19 12:46:39 +11001806 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001808 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07001809 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08001810 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liubac624f2015-08-13 14:31:55 -07001812 else if (test_bit(Journal, &rdev2->flags)) {
1813 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
1814 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
1815 } else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001816 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 else
Song Liuc4d4c912015-08-13 14:31:54 -07001818 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 sb->sb_csum = calc_sb_1_csum(sb);
1822}
1823
Chris Webb0cd17fe2008-06-28 08:31:46 +10001824static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001825super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001826{
1827 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001828 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001829 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001830 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001831 if (rdev->data_offset != rdev->new_data_offset)
1832 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001833 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001834 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001835 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001836 max_sectors -= rdev->data_offset;
1837 if (!num_sectors || num_sectors > max_sectors)
1838 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001839 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001840 /* minor version 0 with bitmap we can't move */
1841 return 0;
1842 } else {
1843 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001844 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001845 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001846 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001847 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001848 if (!num_sectors || num_sectors > max_sectors)
1849 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001850 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001851 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001852 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001853 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001854 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001855 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001856 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001857 rdev->sb_page);
1858 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001859 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001860
1861}
1862
1863static int
1864super_1_allow_new_offset(struct md_rdev *rdev,
1865 unsigned long long new_offset)
1866{
1867 /* All necessary checks on new >= old have been done */
1868 struct bitmap *bitmap;
1869 if (new_offset >= rdev->data_offset)
1870 return 1;
1871
1872 /* with 1.0 metadata, there is no metadata to tread on
1873 * so we can always move back */
1874 if (rdev->mddev->minor_version == 0)
1875 return 1;
1876
1877 /* otherwise we must be sure not to step on
1878 * any metadata, so stay:
1879 * 36K beyond start of superblock
1880 * beyond end of badblocks
1881 * beyond write-intent bitmap
1882 */
1883 if (rdev->sb_start + (32+4)*2 > new_offset)
1884 return 0;
1885 bitmap = rdev->mddev->bitmap;
1886 if (bitmap && !rdev->mddev->bitmap_info.file &&
1887 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001888 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001889 return 0;
1890 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1891 return 0;
1892
1893 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001894}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Adrian Bunk75c96f82005-05-05 16:16:09 -07001896static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 [0] = {
1898 .name = "0.90.0",
1899 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001900 .load_super = super_90_load,
1901 .validate_super = super_90_validate,
1902 .sync_super = super_90_sync,
1903 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001904 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 },
1906 [1] = {
1907 .name = "md-1",
1908 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001909 .load_super = super_1_load,
1910 .validate_super = super_1_validate,
1911 .sync_super = super_1_sync,
1912 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001913 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 },
1915};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
NeilBrownfd01b882011-10-11 16:47:53 +11001917static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001918{
1919 if (mddev->sync_super) {
1920 mddev->sync_super(mddev, rdev);
1921 return;
1922 }
1923
1924 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1925
1926 super_types[mddev->major_version].sync_super(mddev, rdev);
1927}
1928
NeilBrownfd01b882011-10-11 16:47:53 +11001929static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
NeilBrown3cb03002011-10-11 16:45:26 +11001931 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
NeilBrown4b809912008-07-21 17:05:25 +10001933 rcu_read_lock();
1934 rdev_for_each_rcu(rdev, mddev1)
1935 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001936 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001937 rdev2->bdev->bd_contains) {
1938 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001939 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001940 }
1941 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return 0;
1943}
1944
1945static LIST_HEAD(pending_raid_disks);
1946
Andre Nollac5e7112009-08-03 10:59:47 +10001947/*
1948 * Try to register data integrity profile for an mddev
1949 *
1950 * This is called when an array is started and after a disk has been kicked
1951 * from the array. It only succeeds if all working and active component devices
1952 * are integrity capable with matching profiles.
1953 */
NeilBrownfd01b882011-10-11 16:47:53 +11001954int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001955{
NeilBrown3cb03002011-10-11 16:45:26 +11001956 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001957
Andre Nollac5e7112009-08-03 10:59:47 +10001958 if (list_empty(&mddev->disks))
1959 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001960 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1961 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001962 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001963 /* skip spares and non-functional disks */
1964 if (test_bit(Faulty, &rdev->flags))
1965 continue;
1966 if (rdev->raid_disk < 0)
1967 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001968 if (!reference) {
1969 /* Use the first rdev as the reference */
1970 reference = rdev;
1971 continue;
1972 }
1973 /* does this rdev's profile match the reference profile? */
1974 if (blk_integrity_compare(reference->bdev->bd_disk,
1975 rdev->bdev->bd_disk) < 0)
1976 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001977 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001978 if (!reference || !bdev_get_integrity(reference->bdev))
1979 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001980 /*
1981 * All component devices are integrity capable and have matching
1982 * profiles, register the common profile for the md device.
1983 */
1984 if (blk_integrity_register(mddev->gendisk,
1985 bdev_get_integrity(reference->bdev)) != 0) {
1986 printk(KERN_ERR "md: failed to register integrity for %s\n",
1987 mdname(mddev));
1988 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001989 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001990 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1991 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1992 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1993 mdname(mddev));
1994 return -EINVAL;
1995 }
Andre Nollac5e7112009-08-03 10:59:47 +10001996 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001997}
Andre Nollac5e7112009-08-03 10:59:47 +10001998EXPORT_SYMBOL(md_integrity_register);
1999
2000/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002001void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002002{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002003 struct blk_integrity *bi_rdev;
2004 struct blk_integrity *bi_mddev;
2005
2006 if (!mddev->gendisk)
2007 return;
2008
2009 bi_rdev = bdev_get_integrity(rdev->bdev);
2010 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002011
2012 if (!bi_mddev) /* nothing to do */
2013 return;
2014 if (rdev->raid_disk < 0) /* skip spares */
2015 return;
2016 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2017 rdev->bdev->bd_disk) >= 0)
2018 return;
2019 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2020 blk_integrity_unregister(mddev->gendisk);
2021}
2022EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002023
NeilBrownf72ffdd2014-09-30 14:23:59 +10002024static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002026 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002027 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002028 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Dan Williams11e2ede2008-04-30 00:52:32 -07002030 /* prevent duplicates */
2031 if (find_rdev(mddev, rdev->bdev->bd_dev))
2032 return -EEXIST;
2033
Andre Nolldd8ac332009-03-31 14:33:13 +11002034 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2035 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2036 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002037 if (mddev->pers) {
2038 /* Cannot change size, so fail
2039 * If mddev->level <= 0, then we don't care
2040 * about aligning sizes (e.g. linear)
2041 */
2042 if (mddev->level > 0)
2043 return -ENOSPC;
2044 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002045 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* Verify rdev->desc_nr is unique.
2049 * If it is -1, assign a free number, else
2050 * check number is not in use
2051 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002052 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (rdev->desc_nr < 0) {
2054 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002055 if (mddev->pers)
2056 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002057 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 choice++;
2059 rdev->desc_nr = choice;
2060 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002061 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002062 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002066 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002067 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2068 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2069 mdname(mddev), mddev->max_disks);
2070 return -EBUSY;
2071 }
NeilBrown19133a42005-11-08 21:39:35 -08002072 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002073 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002076 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002077
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002078 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002079 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002080
Tejun Heo0762b8b2008-08-25 19:56:12 +09002081 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002082 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2083 /* failure here is OK */;
2084 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002085
NeilBrown4b809912008-07-21 17:05:25 +10002086 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002087 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002088
2089 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002090 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002093
2094 fail:
2095 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2096 b, mdname(mddev));
2097 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
NeilBrown177a99b2008-02-06 01:39:56 -08002100static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002101{
NeilBrown3cb03002011-10-11 16:45:26 +11002102 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002103 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002104 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002105}
2106
NeilBrownf72ffdd2014-09-30 14:23:59 +10002107static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002110
Tejun Heo49731ba2011-01-14 18:43:57 +01002111 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002112 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2114 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002115 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002116 sysfs_put(rdev->sysfs_state);
2117 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002118 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002119 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002120 * writing to 'remove' to "dev/state". We also need
2121 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002122 */
NeilBrown4b809912008-07-21 17:05:25 +10002123 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002124 INIT_WORK(&rdev->del_work, md_delayed_delete);
2125 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002126 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129/*
2130 * prevent the device from being mounted, repartitioned or
2131 * otherwise reused by a RAID array (or any other kernel
2132 * subsystem), by bd_claiming the device.
2133 */
NeilBrown3cb03002011-10-11 16:45:26 +11002134static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 int err = 0;
2137 struct block_device *bdev;
2138 char b[BDEVNAME_SIZE];
2139
Tejun Heod4d77622010-11-13 11:55:18 +01002140 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002141 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (IS_ERR(bdev)) {
2143 printk(KERN_ERR "md: could not open %s.\n",
2144 __bdevname(dev, b));
2145 return PTR_ERR(bdev);
2146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 rdev->bdev = bdev;
2148 return err;
2149}
2150
NeilBrown3cb03002011-10-11 16:45:26 +11002151static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 struct block_device *bdev = rdev->bdev;
2154 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002155 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
2158void md_autodetect_dev(dev_t dev);
2159
NeilBrownf72ffdd2014-09-30 14:23:59 +10002160static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 printk(KERN_INFO "md: export_rdev(%s)\n",
2165 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002166 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002168 if (test_bit(AutoDetected, &rdev->flags))
2169 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170#endif
2171 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002172 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002175void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
2177 unbind_rdev_from_array(rdev);
2178 export_rdev(rdev);
2179}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002180EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
NeilBrownfd01b882011-10-11 16:47:53 +11002182static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
NeilBrown0638bb02014-09-25 17:43:47 +10002184 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
NeilBrown0638bb02014-09-25 17:43:47 +10002186 while (!list_empty(&mddev->disks)) {
2187 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2188 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002189 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 mddev->raid_disks = 0;
2192 mddev->major_version = 0;
2193}
2194
NeilBrownf72ffdd2014-09-30 14:23:59 +10002195static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
NeilBrown42543762006-06-26 00:27:57 -07002197 /* Update each superblock (in-memory image), but
2198 * if we are allowed to, skip spares which already
2199 * have the right event counter, or have one earlier
2200 * (which would mean they aren't being marked as dirty
2201 * with the rest of the array)
2202 */
NeilBrown3cb03002011-10-11 16:45:26 +11002203 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002204 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002205 if (rdev->sb_events == mddev->events ||
2206 (nospares &&
2207 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002208 rdev->sb_events+1 == mddev->events)) {
2209 /* Don't update this superblock */
2210 rdev->sb_loaded = 2;
2211 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002212 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002213 rdev->sb_loaded = 1;
2214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
2216}
2217
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002218static bool does_sb_need_changing(struct mddev *mddev)
2219{
2220 struct md_rdev *rdev;
2221 struct mdp_superblock_1 *sb;
2222 int role;
2223
2224 /* Find a good rdev */
2225 rdev_for_each(rdev, mddev)
2226 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2227 break;
2228
2229 /* No good device found. */
2230 if (!rdev)
2231 return false;
2232
2233 sb = page_address(rdev->sb_page);
2234 /* Check if a device has become faulty or a spare become active */
2235 rdev_for_each(rdev, mddev) {
2236 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2237 /* Device activated? */
2238 if (role == 0xffff && rdev->raid_disk >=0 &&
2239 !test_bit(Faulty, &rdev->flags))
2240 return true;
2241 /* Device turned faulty? */
2242 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2243 return true;
2244 }
2245
2246 /* Check if any mddev parameters have changed */
2247 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2248 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002249 (mddev->layout != le64_to_cpu(sb->layout)) ||
2250 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2251 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2252 return true;
2253
2254 return false;
2255}
2256
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002257void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
NeilBrown3cb03002011-10-11 16:45:26 +11002259 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002260 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002261 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002262 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002263 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
NeilBrownd87f0642013-04-24 11:42:40 +10002265 if (mddev->ro) {
2266 if (force_change)
2267 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2268 return;
2269 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002270
2271 if (mddev_is_clustered(mddev)) {
2272 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2273 force_change = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002274 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002275 /* Has someone else has updated the sb */
2276 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002277 if (ret == 0)
2278 md_cluster_ops->metadata_update_cancel(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002279 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2280 return;
2281 }
2282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002284 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002285 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002286 if (rdev->raid_disk >= 0 &&
2287 mddev->delta_disks >= 0 &&
2288 !test_bit(In_sync, &rdev->flags) &&
2289 mddev->curr_resync_completed > rdev->recovery_offset)
2290 rdev->recovery_offset = mddev->curr_resync_completed;
2291
NeilBrownf72ffdd2014-09-30 14:23:59 +10002292 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002293 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002294 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002295 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002296 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002297 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002298 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002299 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002300 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002301 md_ack_all_badblocks(&rdev->badblocks);
2302 md_error(mddev, rdev);
2303 }
2304 clear_bit(Blocked, &rdev->flags);
2305 clear_bit(BlockedBadBlocks, &rdev->flags);
2306 wake_up(&rdev->blocked_wait);
2307 }
2308 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002309 wake_up(&mddev->sb_wait);
2310 return;
2311 }
2312
NeilBrown85572d72014-12-15 12:56:56 +11002313 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002314
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002315 mddev->utime = get_seconds();
2316
NeilBrown850b2b422006-10-03 01:15:46 -07002317 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2318 force_change = 1;
2319 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2320 /* just a clean<-> dirty transition, possibly leave spares alone,
2321 * though if events isn't the right even/odd, we will have to do
2322 * spares after all
2323 */
2324 nospares = 1;
2325 if (force_change)
2326 nospares = 0;
2327 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002328 /* If the array is degraded, then skipping spares is both
2329 * dangerous and fairly pointless.
2330 * Dangerous because a device that was removed from the array
2331 * might have a event_count that still looks up-to-date,
2332 * so it can be re-added without a resync.
2333 * Pointless because if there are any spares to skip,
2334 * then a recovery will happen and soon that array won't
2335 * be degraded any more and the spare can go back to sleep then.
2336 */
NeilBrown850b2b422006-10-03 01:15:46 -07002337 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002338
NeilBrown06d91a52005-06-21 17:17:12 -07002339 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002340
2341 /* If this is just a dirty<->clean transition, and the array is clean
2342 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002343 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002344 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002345 && mddev->can_decrease_events
2346 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002347 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002348 mddev->can_decrease_events = 0;
2349 } else {
NeilBrown42543762006-06-26 00:27:57 -07002350 /* otherwise we have to go forward and ... */
2351 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002352 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
NeilBrown403df472014-09-30 15:52:29 +10002355 /*
2356 * This 64-bit counter should never wrap.
2357 * Either we are in around ~1 trillion A.C., assuming
2358 * 1 reboot per second, or we have a bug...
2359 */
2360 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002361
NeilBrowndafb20f2012-03-19 12:46:39 +11002362 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002363 if (rdev->badblocks.changed)
2364 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002365 if (test_bit(Faulty, &rdev->flags))
2366 set_bit(FaultRecorded, &rdev->flags);
2367 }
NeilBrown2699b672011-07-28 11:31:47 +10002368
NeilBrowne6910632008-02-06 01:39:51 -08002369 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002370 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
NeilBrown36a4e1f2011-10-07 14:23:17 +11002372 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2373 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
NeilBrown4ad13662007-07-17 04:06:13 -07002375 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002376 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002378
NeilBrown42543762006-06-26 00:27:57 -07002379 if (rdev->sb_loaded != 1)
2380 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
NeilBrownf4667222013-12-09 12:04:56 +11002382 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002383 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002384 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002385 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002386 pr_debug("md: (write) %s's sb offset: %llu\n",
2387 bdevname(rdev->bdev, b),
2388 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002389 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002390 if (rdev->badblocks.size) {
2391 md_super_write(mddev, rdev,
2392 rdev->badblocks.sector,
2393 rdev->badblocks.size << 9,
2394 rdev->bb_page);
2395 rdev->badblocks.size = 0;
2396 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002397
NeilBrownf4667222013-12-09 12:04:56 +11002398 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002399 pr_debug("md: %s (skipping faulty)\n",
2400 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002401
NeilBrown7bfa19f2005-06-21 17:17:28 -07002402 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 /* only need to write one superblock... */
2404 break;
2405 }
NeilBrowna9701a32005-11-08 21:39:34 -08002406 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002407 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002408
NeilBrown85572d72014-12-15 12:56:56 +11002409 spin_lock(&mddev->lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002410 if (mddev->in_sync != sync_req ||
2411 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002412 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002413 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002414 goto repeat;
2415 }
NeilBrown850b2b422006-10-03 01:15:46 -07002416 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002417 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002418 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002419 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2420 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002421
NeilBrowndafb20f2012-03-19 12:46:39 +11002422 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002423 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2424 clear_bit(Blocked, &rdev->flags);
2425
2426 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002427 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002428 clear_bit(BlockedBadBlocks, &rdev->flags);
2429 wake_up(&rdev->blocked_wait);
2430 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002431
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002432 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002433 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002435EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002437static int add_bound_rdev(struct md_rdev *rdev)
2438{
2439 struct mddev *mddev = rdev->mddev;
2440 int err = 0;
2441
2442 if (!mddev->pers->hot_remove_disk) {
2443 /* If there is hot_add_disk but no hot_remove_disk
2444 * then added disks for geometry changes,
2445 * and should be added immediately.
2446 */
2447 super_types[mddev->major_version].
2448 validate_super(mddev, rdev);
2449 err = mddev->pers->hot_add_disk(mddev, rdev);
2450 if (err) {
2451 unbind_rdev_from_array(rdev);
2452 export_rdev(rdev);
2453 return err;
2454 }
2455 }
2456 sysfs_notify_dirent_safe(rdev->sysfs_state);
2457
2458 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2459 if (mddev->degraded)
2460 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2461 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2462 md_new_event(mddev);
2463 md_wakeup_thread(mddev->thread);
2464 return 0;
2465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Andre Noll7f6ce762008-03-23 18:34:54 +01002467/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002468 * We want to accept with case. For this we use cmd_match.
2469 */
2470static int cmd_match(const char *cmd, const char *str)
2471{
2472 /* See if cmd, written into a sysfs file, matches
2473 * str. They must either be the same, or cmd can
2474 * have a trailing newline
2475 */
2476 while (*cmd && *str && *cmd == *str) {
2477 cmd++;
2478 str++;
2479 }
2480 if (*cmd == '\n')
2481 cmd++;
2482 if (*str || *cmd)
2483 return 0;
2484 return 1;
2485}
2486
NeilBrown86e6ffd2005-11-08 21:39:24 -08002487struct rdev_sysfs_entry {
2488 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002489 ssize_t (*show)(struct md_rdev *, char *);
2490 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002491};
2492
2493static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002494state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002495{
2496 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002497 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002498 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002499
NeilBrown758bfc82014-12-15 12:56:59 +11002500 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002501 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002502 len+= sprintf(page+len, "%sfaulty",sep);
2503 sep = ",";
2504 }
NeilBrown758bfc82014-12-15 12:56:59 +11002505 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002506 len += sprintf(page+len, "%sin_sync",sep);
2507 sep = ",";
2508 }
NeilBrown758bfc82014-12-15 12:56:59 +11002509 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002510 len += sprintf(page+len, "%swrite_mostly",sep);
2511 sep = ",";
2512 }
NeilBrown758bfc82014-12-15 12:56:59 +11002513 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002514 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002515 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002516 len += sprintf(page+len, "%sblocked", sep);
2517 sep = ",";
2518 }
NeilBrown758bfc82014-12-15 12:56:59 +11002519 if (!test_bit(Faulty, &flags) &&
2520 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002521 len += sprintf(page+len, "%sspare", sep);
2522 sep = ",";
2523 }
NeilBrown758bfc82014-12-15 12:56:59 +11002524 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002525 len += sprintf(page+len, "%swrite_error", sep);
2526 sep = ",";
2527 }
NeilBrown758bfc82014-12-15 12:56:59 +11002528 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002529 len += sprintf(page+len, "%swant_replacement", sep);
2530 sep = ",";
2531 }
NeilBrown758bfc82014-12-15 12:56:59 +11002532 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002533 len += sprintf(page+len, "%sreplacement", sep);
2534 sep = ",";
2535 }
2536
NeilBrown86e6ffd2005-11-08 21:39:24 -08002537 return len+sprintf(page+len, "\n");
2538}
2539
NeilBrown45dc2de2006-06-26 00:27:58 -07002540static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002541state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002542{
2543 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002544 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002545 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002546 * writemostly - sets write_mostly
2547 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002548 * blocked - sets the Blocked flags
2549 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002550 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002551 * -insync - clear Insync for a device with a slot assigned,
2552 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002553 * write_error - sets WriteErrorSeen
2554 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002555 */
2556 int err = -EINVAL;
2557 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2558 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002559 if (test_bit(Faulty, &rdev->flags))
2560 err = 0;
2561 else
2562 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002563 } else if (cmd_match(buf, "remove")) {
2564 if (rdev->raid_disk >= 0)
2565 err = -EBUSY;
2566 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002567 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002568 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002569 if (mddev_is_clustered(mddev))
2570 err = md_cluster_ops->remove_disk(mddev, rdev);
2571
2572 if (err == 0) {
2573 md_kick_rdev_from_array(rdev);
2574 if (mddev->pers)
2575 md_update_sb(mddev, 1);
2576 md_new_event(mddev);
2577 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002578 }
NeilBrownf6556752006-06-26 00:28:01 -07002579 } else if (cmd_match(buf, "writemostly")) {
2580 set_bit(WriteMostly, &rdev->flags);
2581 err = 0;
2582 } else if (cmd_match(buf, "-writemostly")) {
2583 clear_bit(WriteMostly, &rdev->flags);
2584 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002585 } else if (cmd_match(buf, "blocked")) {
2586 set_bit(Blocked, &rdev->flags);
2587 err = 0;
2588 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002589 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002590 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002591 /* metadata handler doesn't understand badblocks,
2592 * so we need to fail the device
2593 */
2594 md_error(rdev->mddev, rdev);
2595 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002596 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002597 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002598 wake_up(&rdev->blocked_wait);
2599 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2600 md_wakeup_thread(rdev->mddev->thread);
2601
2602 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002603 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2604 set_bit(In_sync, &rdev->flags);
2605 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002606 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002607 if (rdev->mddev->pers == NULL) {
2608 clear_bit(In_sync, &rdev->flags);
2609 rdev->saved_raid_disk = rdev->raid_disk;
2610 rdev->raid_disk = -1;
2611 err = 0;
2612 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002613 } else if (cmd_match(buf, "write_error")) {
2614 set_bit(WriteErrorSeen, &rdev->flags);
2615 err = 0;
2616 } else if (cmd_match(buf, "-write_error")) {
2617 clear_bit(WriteErrorSeen, &rdev->flags);
2618 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002619 } else if (cmd_match(buf, "want_replacement")) {
2620 /* Any non-spare device that is not a replacement can
2621 * become want_replacement at any time, but we then need to
2622 * check if recovery is needed.
2623 */
2624 if (rdev->raid_disk >= 0 &&
2625 !test_bit(Replacement, &rdev->flags))
2626 set_bit(WantReplacement, &rdev->flags);
2627 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2628 md_wakeup_thread(rdev->mddev->thread);
2629 err = 0;
2630 } else if (cmd_match(buf, "-want_replacement")) {
2631 /* Clearing 'want_replacement' is always allowed.
2632 * Once replacements starts it is too late though.
2633 */
2634 err = 0;
2635 clear_bit(WantReplacement, &rdev->flags);
2636 } else if (cmd_match(buf, "replacement")) {
2637 /* Can only set a device as a replacement when array has not
2638 * yet been started. Once running, replacement is automatic
2639 * from spares, or by assigning 'slot'.
2640 */
2641 if (rdev->mddev->pers)
2642 err = -EBUSY;
2643 else {
2644 set_bit(Replacement, &rdev->flags);
2645 err = 0;
2646 }
2647 } else if (cmd_match(buf, "-replacement")) {
2648 /* Similarly, can only clear Replacement before start */
2649 if (rdev->mddev->pers)
2650 err = -EBUSY;
2651 else {
2652 clear_bit(Replacement, &rdev->flags);
2653 err = 0;
2654 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002655 } else if (cmd_match(buf, "re-add")) {
2656 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002657 /* clear_bit is performed _after_ all the devices
2658 * have their local Faulty bit cleared. If any writes
2659 * happen in the meantime in the local node, they
2660 * will land in the local bitmap, which will be synced
2661 * by this node eventually
2662 */
2663 if (!mddev_is_clustered(rdev->mddev) ||
2664 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2665 clear_bit(Faulty, &rdev->flags);
2666 err = add_bound_rdev(rdev);
2667 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002668 } else
2669 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002670 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002671 if (!err)
2672 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002673 return err ? err : len;
2674}
NeilBrown80ca3a42006-07-10 04:44:18 -07002675static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002676__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002677
2678static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002679errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002680{
2681 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2682}
2683
2684static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002685errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002686{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002687 unsigned int n;
2688 int rv;
2689
2690 rv = kstrtouint(buf, 10, &n);
2691 if (rv < 0)
2692 return rv;
2693 atomic_set(&rdev->corrected_errors, n);
2694 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002695}
2696static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002697__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002698
NeilBrown014236d2006-01-06 00:20:55 -08002699static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002700slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002701{
2702 if (rdev->raid_disk < 0)
2703 return sprintf(page, "none\n");
2704 else
2705 return sprintf(page, "%d\n", rdev->raid_disk);
2706}
2707
2708static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002709slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002710{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002711 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002712 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002713
NeilBrown014236d2006-01-06 00:20:55 -08002714 if (strncmp(buf, "none", 4)==0)
2715 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002716 else {
2717 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2718 if (err < 0)
2719 return err;
2720 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002721 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002722 /* Setting 'slot' on an active array requires also
2723 * updating the 'rd%d' link, and communicating
2724 * with the personality with ->hot_*_disk.
2725 * For now we only support removing
2726 * failed/spare devices. This normally happens automatically,
2727 * but not when the metadata is externally managed.
2728 */
NeilBrownc303da62008-02-06 01:39:51 -08002729 if (rdev->raid_disk == -1)
2730 return -EEXIST;
2731 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002732 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002733 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002734 clear_bit(Blocked, &rdev->flags);
2735 remove_and_add_spares(rdev->mddev, rdev);
2736 if (rdev->raid_disk >= 0)
2737 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002738 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2739 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002740 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002741 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002742 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002743 */
2744
2745 if (rdev->raid_disk != -1)
2746 return -EBUSY;
2747
NeilBrownc6751b22011-02-02 11:57:13 +11002748 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2749 return -EBUSY;
2750
Neil Brown6c2fce22008-06-28 08:31:31 +10002751 if (rdev->mddev->pers->hot_add_disk == NULL)
2752 return -EINVAL;
2753
NeilBrownba1b41b2011-01-14 09:14:34 +11002754 if (slot >= rdev->mddev->raid_disks &&
2755 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2756 return -ENOSPC;
2757
Neil Brown6c2fce22008-06-28 08:31:31 +10002758 rdev->raid_disk = slot;
2759 if (test_bit(In_sync, &rdev->flags))
2760 rdev->saved_raid_disk = slot;
2761 else
2762 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002763 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002764 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05002765 remove_and_add_spares(rdev->mddev, rdev);
2766 if (rdev->raid_disk == -1)
2767 return -EBUSY;
Neil Brown6c2fce22008-06-28 08:31:31 +10002768 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002769 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002770 if (slot >= rdev->mddev->raid_disks &&
2771 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002772 return -ENOSPC;
2773 rdev->raid_disk = slot;
2774 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002775 clear_bit(Faulty, &rdev->flags);
2776 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002777 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002778 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002779 }
NeilBrown014236d2006-01-06 00:20:55 -08002780 return len;
2781}
2782
NeilBrown014236d2006-01-06 00:20:55 -08002783static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002784__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002785
NeilBrown93c8cad2006-01-06 00:20:56 -08002786static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002787offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002788{
Andrew Morton6961ece2006-01-06 00:20:59 -08002789 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002790}
2791
2792static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002793offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002794{
NeilBrownc6563a82012-05-21 09:27:00 +10002795 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002796 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002797 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002798 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002799 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002800 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002801 /* Must set offset before size, so overlap checks
2802 * can be sane */
2803 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002804 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002805 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002806 return len;
2807}
2808
2809static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002810__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002811
NeilBrownc6563a82012-05-21 09:27:00 +10002812static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2813{
2814 return sprintf(page, "%llu\n",
2815 (unsigned long long)rdev->new_data_offset);
2816}
2817
2818static ssize_t new_offset_store(struct md_rdev *rdev,
2819 const char *buf, size_t len)
2820{
2821 unsigned long long new_offset;
2822 struct mddev *mddev = rdev->mddev;
2823
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002824 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002825 return -EINVAL;
2826
NeilBrownf851b602014-12-11 10:02:10 +11002827 if (mddev->sync_thread ||
2828 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002829 return -EBUSY;
2830 if (new_offset == rdev->data_offset)
2831 /* reset is always permitted */
2832 ;
2833 else if (new_offset > rdev->data_offset) {
2834 /* must not push array size beyond rdev_sectors */
2835 if (new_offset - rdev->data_offset
2836 + mddev->dev_sectors > rdev->sectors)
2837 return -E2BIG;
2838 }
2839 /* Metadata worries about other space details. */
2840
2841 /* decreasing the offset is inconsistent with a backwards
2842 * reshape.
2843 */
2844 if (new_offset < rdev->data_offset &&
2845 mddev->reshape_backwards)
2846 return -EINVAL;
2847 /* Increasing offset is inconsistent with forwards
2848 * reshape. reshape_direction should be set to
2849 * 'backwards' first.
2850 */
2851 if (new_offset > rdev->data_offset &&
2852 !mddev->reshape_backwards)
2853 return -EINVAL;
2854
2855 if (mddev->pers && mddev->persistent &&
2856 !super_types[mddev->major_version]
2857 .allow_new_offset(rdev, new_offset))
2858 return -E2BIG;
2859 rdev->new_data_offset = new_offset;
2860 if (new_offset > rdev->data_offset)
2861 mddev->reshape_backwards = 1;
2862 else if (new_offset < rdev->data_offset)
2863 mddev->reshape_backwards = 0;
2864
2865 return len;
2866}
2867static struct rdev_sysfs_entry rdev_new_offset =
2868__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2869
NeilBrown83303b62006-01-06 00:21:06 -08002870static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002871rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002872{
Andre Nolldd8ac332009-03-31 14:33:13 +11002873 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002874}
2875
NeilBrownc5d79ad2008-02-06 01:39:54 -08002876static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2877{
2878 /* check if two start/length pairs overlap */
2879 if (s1+l1 <= s2)
2880 return 0;
2881 if (s2+l2 <= s1)
2882 return 0;
2883 return 1;
2884}
2885
Dan Williamsb522adc2009-03-31 15:00:31 +11002886static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2887{
2888 unsigned long long blocks;
2889 sector_t new;
2890
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002891 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002892 return -EINVAL;
2893
2894 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2895 return -EINVAL; /* sector conversion overflow */
2896
2897 new = blocks * 2;
2898 if (new != blocks * 2)
2899 return -EINVAL; /* unsigned long long to sector_t overflow */
2900
2901 *sectors = new;
2902 return 0;
2903}
2904
NeilBrown83303b62006-01-06 00:21:06 -08002905static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002906rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002907{
NeilBrownfd01b882011-10-11 16:47:53 +11002908 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002909 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002910 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002911
Dan Williamsb522adc2009-03-31 15:00:31 +11002912 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002913 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002914 if (rdev->data_offset != rdev->new_data_offset)
2915 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002916 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002917 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002918 sectors = super_types[my_mddev->major_version].
2919 rdev_size_change(rdev, sectors);
2920 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002921 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002922 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002923 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002924 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002925 if (!my_mddev->pers->resize)
2926 /* Cannot change size for RAID0 or Linear etc */
2927 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002928 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002929 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002930 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002931
Andre Nolldd8ac332009-03-31 14:33:13 +11002932 rdev->sectors = sectors;
2933 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002934 /* Need to check that all other rdevs with the same
2935 * ->bdev do not overlap. 'rcu' is sufficient to walk
2936 * the rdev lists safely.
2937 * This check does not provide a hard guarantee, it
2938 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002939 */
NeilBrownfd01b882011-10-11 16:47:53 +11002940 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002941 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002942 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002943
NeilBrown8b1afc32014-09-29 15:33:20 +10002944 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002945 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002946 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002947
NeilBrowndafb20f2012-03-19 12:46:39 +11002948 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002949 if (rdev->bdev == rdev2->bdev &&
2950 rdev != rdev2 &&
2951 overlaps(rdev->data_offset, rdev->sectors,
2952 rdev2->data_offset,
2953 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002954 overlap = 1;
2955 break;
2956 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002957 if (overlap) {
2958 mddev_put(mddev);
2959 break;
2960 }
2961 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002962 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002963 if (overlap) {
2964 /* Someone else could have slipped in a size
2965 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002966 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002967 * safe, and trust userspace not to race with
2968 * itself
2969 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002970 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002971 return -EBUSY;
2972 }
2973 }
NeilBrown83303b62006-01-06 00:21:06 -08002974 return len;
2975}
2976
2977static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002978__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002979
NeilBrown3cb03002011-10-11 16:45:26 +11002980static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002981{
2982 unsigned long long recovery_start = rdev->recovery_offset;
2983
2984 if (test_bit(In_sync, &rdev->flags) ||
2985 recovery_start == MaxSector)
2986 return sprintf(page, "none\n");
2987
2988 return sprintf(page, "%llu\n", recovery_start);
2989}
2990
NeilBrown3cb03002011-10-11 16:45:26 +11002991static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002992{
2993 unsigned long long recovery_start;
2994
2995 if (cmd_match(buf, "none"))
2996 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002997 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002998 return -EINVAL;
2999
3000 if (rdev->mddev->pers &&
3001 rdev->raid_disk >= 0)
3002 return -EBUSY;
3003
3004 rdev->recovery_offset = recovery_start;
3005 if (recovery_start == MaxSector)
3006 set_bit(In_sync, &rdev->flags);
3007 else
3008 clear_bit(In_sync, &rdev->flags);
3009 return len;
3010}
3011
3012static struct rdev_sysfs_entry rdev_recovery_start =
3013__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3014
NeilBrown16c791a2011-07-28 11:31:47 +10003015static ssize_t
3016badblocks_show(struct badblocks *bb, char *page, int unack);
3017static ssize_t
3018badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3019
NeilBrown3cb03002011-10-11 16:45:26 +11003020static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003021{
3022 return badblocks_show(&rdev->badblocks, page, 0);
3023}
NeilBrown3cb03002011-10-11 16:45:26 +11003024static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003025{
NeilBrownde393cd2011-07-28 11:31:48 +10003026 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3027 /* Maybe that ack was all we needed */
3028 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3029 wake_up(&rdev->blocked_wait);
3030 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003031}
3032static struct rdev_sysfs_entry rdev_bad_blocks =
3033__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3034
NeilBrown3cb03002011-10-11 16:45:26 +11003035static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003036{
3037 return badblocks_show(&rdev->badblocks, page, 1);
3038}
NeilBrown3cb03002011-10-11 16:45:26 +11003039static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003040{
3041 return badblocks_store(&rdev->badblocks, page, len, 1);
3042}
3043static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3044__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3045
NeilBrown86e6ffd2005-11-08 21:39:24 -08003046static struct attribute *rdev_default_attrs[] = {
3047 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003048 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003049 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003050 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003051 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003052 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003053 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003054 &rdev_bad_blocks.attr,
3055 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003056 NULL,
3057};
3058static ssize_t
3059rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3060{
3061 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003062 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003063
3064 if (!entry->show)
3065 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003066 if (!rdev->mddev)
3067 return -EBUSY;
3068 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003069}
3070
3071static ssize_t
3072rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3073 const char *page, size_t length)
3074{
3075 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003076 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003077 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003078 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003079
3080 if (!entry->store)
3081 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003082 if (!capable(CAP_SYS_ADMIN))
3083 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003084 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003085 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003086 if (rdev->mddev == NULL)
3087 rv = -EBUSY;
3088 else
3089 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003090 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003091 }
3092 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003093}
3094
3095static void rdev_free(struct kobject *ko)
3096{
NeilBrown3cb03002011-10-11 16:45:26 +11003097 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003098 kfree(rdev);
3099}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003100static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003101 .show = rdev_attr_show,
3102 .store = rdev_attr_store,
3103};
3104static struct kobj_type rdev_ktype = {
3105 .release = rdev_free,
3106 .sysfs_ops = &rdev_sysfs_ops,
3107 .default_attrs = rdev_default_attrs,
3108};
3109
NeilBrown3cb03002011-10-11 16:45:26 +11003110int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003111{
3112 rdev->desc_nr = -1;
3113 rdev->saved_raid_disk = -1;
3114 rdev->raid_disk = -1;
3115 rdev->flags = 0;
3116 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003117 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003118 rdev->sb_events = 0;
3119 rdev->last_read_error.tv_sec = 0;
3120 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003121 rdev->sb_loaded = 0;
3122 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003123 atomic_set(&rdev->nr_pending, 0);
3124 atomic_set(&rdev->read_errors, 0);
3125 atomic_set(&rdev->corrected_errors, 0);
3126
3127 INIT_LIST_HEAD(&rdev->same_set);
3128 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003129
3130 /* Add space to store bad block list.
3131 * This reserves the space even on arrays where it cannot
3132 * be used - I wonder if that matters
3133 */
3134 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003135 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003136 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3137 seqlock_init(&rdev->badblocks.lock);
3138 if (rdev->badblocks.page == NULL)
3139 return -ENOMEM;
3140
3141 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003142}
3143EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144/*
3145 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3146 *
3147 * mark the device faulty if:
3148 *
3149 * - the device is nonexistent (zero size)
3150 * - the device has no valid superblock
3151 *
3152 * a faulty rdev _never_ has rdev->sb set.
3153 */
NeilBrown3cb03002011-10-11 16:45:26 +11003154static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
3156 char b[BDEVNAME_SIZE];
3157 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003158 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 sector_t size;
3160
NeilBrown9ffae0c2006-01-06 00:20:32 -08003161 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (!rdev) {
3163 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3164 return ERR_PTR(-ENOMEM);
3165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
NeilBrown2230dfe2011-07-28 11:31:46 +10003167 err = md_rdev_init(rdev);
3168 if (err)
3169 goto abort_free;
3170 err = alloc_disk_sb(rdev);
3171 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 goto abort_free;
3173
NeilBrownc5d79ad2008-02-06 01:39:54 -08003174 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 if (err)
3176 goto abort_free;
3177
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003178 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003179
Mike Snitzer77304d22010-11-08 14:39:12 +01003180 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003182 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 "md: %s has zero or unknown size, marking faulty!\n",
3184 bdevname(rdev->bdev,b));
3185 err = -EINVAL;
3186 goto abort_free;
3187 }
3188
3189 if (super_format >= 0) {
3190 err = super_types[super_format].
3191 load_super(rdev, NULL, super_minor);
3192 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003193 printk(KERN_WARNING
3194 "md: %s does not have a valid v%d.%d "
3195 "superblock, not importing!\n",
3196 bdevname(rdev->bdev,b),
3197 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 goto abort_free;
3199 }
3200 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003201 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 "md: could not read %s's sb, not importing!\n",
3203 bdevname(rdev->bdev,b));
3204 goto abort_free;
3205 }
3206 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return rdev;
3209
3210abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003211 if (rdev->bdev)
3212 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003213 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 kfree(rdev);
3215 return ERR_PTR(err);
3216}
3217
3218/*
3219 * Check a full RAID array for plausibility
3220 */
3221
NeilBrownf72ffdd2014-09-30 14:23:59 +10003222static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
3224 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003225 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 char b[BDEVNAME_SIZE];
3227
3228 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003229 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 switch (super_types[mddev->major_version].
3231 load_super(rdev, freshest, mddev->minor_version)) {
3232 case 1:
3233 freshest = rdev;
3234 break;
3235 case 0:
3236 break;
3237 default:
3238 printk( KERN_ERR \
3239 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003240 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003242 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 super_types[mddev->major_version].
3246 validate_super(mddev, freshest);
3247
3248 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003249 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003250 if (mddev->max_disks &&
3251 (rdev->desc_nr >= mddev->max_disks ||
3252 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003253 printk(KERN_WARNING
3254 "md: %s: %s: only %d devices permitted\n",
3255 mdname(mddev), bdevname(rdev->bdev, b),
3256 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003257 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003258 continue;
3259 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003260 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 if (super_types[mddev->major_version].
3262 validate_super(mddev, rdev)) {
3263 printk(KERN_WARNING "md: kicking non-fresh %s"
3264 " from array!\n",
3265 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003266 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 continue;
3268 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (mddev->level == LEVEL_MULTIPATH) {
3271 rdev->desc_nr = i++;
3272 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003273 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003274 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003275 rdev->raid_disk = -1;
3276 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
3280
NeilBrown72e02072009-12-14 12:49:55 +11003281/* Read a fixed-point number.
3282 * Numbers in sysfs attributes should be in "standard" units where
3283 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003284 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003285 * milliseconds or jiffies.
3286 * This function takes a decimal number with a possible fractional
3287 * component, and produces an integer which is the result of
3288 * multiplying that number by 10^'scale'.
3289 * all without any floating-point arithmetic.
3290 */
3291int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3292{
3293 unsigned long result = 0;
3294 long decimals = -1;
3295 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3296 if (*cp == '.')
3297 decimals = 0;
3298 else if (decimals < scale) {
3299 unsigned int value;
3300 value = *cp - '0';
3301 result = result * 10 + value;
3302 if (decimals >= 0)
3303 decimals++;
3304 }
3305 cp++;
3306 }
3307 if (*cp == '\n')
3308 cp++;
3309 if (*cp)
3310 return -EINVAL;
3311 if (decimals < 0)
3312 decimals = 0;
3313 while (decimals < scale) {
3314 result *= 10;
3315 decimals ++;
3316 }
3317 *res = result;
3318 return 0;
3319}
3320
NeilBrowneae17012005-11-08 21:39:23 -08003321static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003322safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003323{
3324 int msec = (mddev->safemode_delay*1000)/HZ;
3325 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3326}
3327static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003328safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003329{
NeilBrown16f17b32006-06-26 00:27:37 -07003330 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003331
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003332 if (mddev_is_clustered(mddev)) {
3333 pr_info("md: Safemode is disabled for clustered mode\n");
3334 return -EINVAL;
3335 }
3336
NeilBrown72e02072009-12-14 12:49:55 +11003337 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003338 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003339 if (msec == 0)
3340 mddev->safemode_delay = 0;
3341 else {
NeilBrown19052c02008-08-05 15:54:13 +10003342 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003343 unsigned long new_delay = (msec*HZ)/1000;
3344
3345 if (new_delay == 0)
3346 new_delay = 1;
3347 mddev->safemode_delay = new_delay;
3348 if (new_delay < old_delay || old_delay == 0)
3349 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003350 }
3351 return len;
3352}
3353static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003354__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003355
3356static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003357level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003358{
NeilBrown36d091f2014-12-15 12:56:58 +11003359 struct md_personality *p;
3360 int ret;
3361 spin_lock(&mddev->lock);
3362 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003363 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003364 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003365 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003366 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003367 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003368 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003369 else
NeilBrown36d091f2014-12-15 12:56:58 +11003370 ret = 0;
3371 spin_unlock(&mddev->lock);
3372 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003373}
3374
NeilBrownd9d166c2006-01-06 00:20:51 -08003375static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003376level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003377{
Dan Williamsf2859af2010-05-02 10:04:16 -07003378 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003379 ssize_t rv;
3380 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003381 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003382 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003383 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003384 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003385
NeilBrown67918752014-12-15 12:57:01 +11003386 if (slen == 0 || slen >= sizeof(clevel))
3387 return -EINVAL;
3388
3389 rv = mddev_lock(mddev);
3390 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003391 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003392
3393 if (mddev->pers == NULL) {
3394 strncpy(mddev->clevel, buf, slen);
3395 if (mddev->clevel[slen-1] == '\n')
3396 slen--;
3397 mddev->clevel[slen] = 0;
3398 mddev->level = LEVEL_NONE;
3399 rv = len;
3400 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003401 }
NeilBrown67918752014-12-15 12:57:01 +11003402 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003403 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003404 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003405
3406 /* request to change the personality. Need to ensure:
3407 * - array is not engaged in resync/recovery/reshape
3408 * - old personality can be suspended
3409 * - new personality will access other array.
3410 */
3411
NeilBrown67918752014-12-15 12:57:01 +11003412 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003413 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003414 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003415 mddev->reshape_position != MaxSector ||
3416 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003417 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003418
NeilBrown67918752014-12-15 12:57:01 +11003419 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003420 if (!mddev->pers->quiesce) {
3421 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3422 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003423 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003424 }
3425
3426 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003427 strncpy(clevel, buf, slen);
3428 if (clevel[slen-1] == '\n')
3429 slen--;
3430 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003431 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003432 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003433
Dan Williamsf2859af2010-05-02 10:04:16 -07003434 if (request_module("md-%s", clevel) != 0)
3435 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003436 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003437 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003438 if (!pers || !try_module_get(pers->owner)) {
3439 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003440 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003441 rv = -EINVAL;
3442 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003443 }
3444 spin_unlock(&pers_lock);
3445
3446 if (pers == mddev->pers) {
3447 /* Nothing to do! */
3448 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003449 rv = len;
3450 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003451 }
3452 if (!pers->takeover) {
3453 module_put(pers->owner);
3454 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003455 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003456 rv = -EINVAL;
3457 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003458 }
3459
NeilBrowndafb20f2012-03-19 12:46:39 +11003460 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003461 rdev->new_raid_disk = rdev->raid_disk;
3462
NeilBrown245f46c2009-03-31 14:39:39 +11003463 /* ->takeover must set new_* and/or delta_disks
3464 * if it succeeds, and may set them when it fails.
3465 */
3466 priv = pers->takeover(mddev);
3467 if (IS_ERR(priv)) {
3468 mddev->new_level = mddev->level;
3469 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003470 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003471 mddev->raid_disks -= mddev->delta_disks;
3472 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003473 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003474 module_put(pers->owner);
3475 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003476 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003477 rv = PTR_ERR(priv);
3478 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003479 }
3480
3481 /* Looks like we have a winner */
3482 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003483 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003484
3485 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003486 oldpers = mddev->pers;
3487 oldpriv = mddev->private;
3488 mddev->pers = pers;
3489 mddev->private = priv;
3490 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3491 mddev->level = mddev->new_level;
3492 mddev->layout = mddev->new_layout;
3493 mddev->chunk_sectors = mddev->new_chunk_sectors;
3494 mddev->delta_disks = 0;
3495 mddev->reshape_backwards = 0;
3496 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003497 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003498
NeilBrowndb721d32014-12-15 12:56:58 +11003499 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003500 mddev->external) {
3501 /* We are converting from a no-redundancy array
3502 * to a redundancy array and metadata is managed
3503 * externally so we need to be sure that writes
3504 * won't block due to a need to transition
3505 * clean->dirty
3506 * until external management is started.
3507 */
3508 mddev->in_sync = 0;
3509 mddev->safemode_delay = 0;
3510 mddev->safemode = 0;
3511 }
3512
NeilBrowndb721d32014-12-15 12:56:58 +11003513 oldpers->free(mddev, oldpriv);
3514
3515 if (oldpers->sync_request == NULL &&
3516 pers->sync_request != NULL) {
3517 /* need to add the md_redundancy_group */
3518 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3519 printk(KERN_WARNING
3520 "md: cannot register extra attributes for %s\n",
3521 mdname(mddev));
3522 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3523 }
3524 if (oldpers->sync_request != NULL &&
3525 pers->sync_request == NULL) {
3526 /* need to remove the md_redundancy_group */
3527 if (mddev->to_remove == NULL)
3528 mddev->to_remove = &md_redundancy_group;
3529 }
3530
NeilBrowndafb20f2012-03-19 12:46:39 +11003531 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003532 if (rdev->raid_disk < 0)
3533 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003534 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003535 rdev->new_raid_disk = -1;
3536 if (rdev->new_raid_disk == rdev->raid_disk)
3537 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003538 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003539 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003540 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003541 if (rdev->raid_disk < 0)
3542 continue;
3543 if (rdev->new_raid_disk == rdev->raid_disk)
3544 continue;
3545 rdev->raid_disk = rdev->new_raid_disk;
3546 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003547 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003548 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003549 if (sysfs_link_rdev(mddev, rdev))
3550 printk(KERN_WARNING "md: cannot register rd%d"
3551 " for %s after level change\n",
3552 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003553 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003554 }
3555
NeilBrowndb721d32014-12-15 12:56:58 +11003556 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003557 /* this is now an array without redundancy, so
3558 * it must always be in_sync
3559 */
3560 mddev->in_sync = 1;
3561 del_timer_sync(&mddev->safemode_timer);
3562 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003563 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003564 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003565 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003566 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003567 if (!mddev->thread)
3568 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003569 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003570 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003571 rv = len;
3572out_unlock:
3573 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003574 return rv;
3575}
3576
3577static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003578__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003579
NeilBrownd4dbd022006-06-26 00:27:59 -07003580static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003581layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003582{
3583 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003584 if (mddev->reshape_position != MaxSector &&
3585 mddev->layout != mddev->new_layout)
3586 return sprintf(page, "%d (%d)\n",
3587 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003588 return sprintf(page, "%d\n", mddev->layout);
3589}
3590
3591static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003592layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003593{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003594 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003595 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003596
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003597 err = kstrtouint(buf, 10, &n);
3598 if (err < 0)
3599 return err;
NeilBrown67918752014-12-15 12:57:01 +11003600 err = mddev_lock(mddev);
3601 if (err)
3602 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003603
NeilBrownb3546032009-03-31 14:56:41 +11003604 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003605 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003606 err = -EBUSY;
3607 else if (mddev->ro)
3608 err = -EROFS;
3609 else {
3610 mddev->new_layout = n;
3611 err = mddev->pers->check_reshape(mddev);
3612 if (err)
3613 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003614 }
NeilBrownb3546032009-03-31 14:56:41 +11003615 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003616 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003617 if (mddev->reshape_position == MaxSector)
3618 mddev->layout = n;
3619 }
NeilBrown67918752014-12-15 12:57:01 +11003620 mddev_unlock(mddev);
3621 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003622}
3623static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003624__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003625
NeilBrowneae17012005-11-08 21:39:23 -08003626static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003627raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003628{
NeilBrownbb636542005-11-08 21:39:45 -08003629 if (mddev->raid_disks == 0)
3630 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003631 if (mddev->reshape_position != MaxSector &&
3632 mddev->delta_disks != 0)
3633 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3634 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003635 return sprintf(page, "%d\n", mddev->raid_disks);
3636}
3637
NeilBrownfd01b882011-10-11 16:47:53 +11003638static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003639
3640static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003641raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003642{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003643 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003644 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003645
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003646 err = kstrtouint(buf, 10, &n);
3647 if (err < 0)
3648 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003649
NeilBrown67918752014-12-15 12:57:01 +11003650 err = mddev_lock(mddev);
3651 if (err)
3652 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003653 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003654 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003655 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003656 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003657 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003658
NeilBrown67918752014-12-15 12:57:01 +11003659 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003660 rdev_for_each(rdev, mddev) {
3661 if (olddisks < n &&
3662 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003663 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003664 if (olddisks > n &&
3665 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003666 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003667 }
NeilBrown67918752014-12-15 12:57:01 +11003668 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003669 mddev->delta_disks = n - olddisks;
3670 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003671 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003672 } else
NeilBrownda943b992006-01-06 00:20:54 -08003673 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003674out_unlock:
3675 mddev_unlock(mddev);
3676 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003677}
3678static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003679__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003680
NeilBrown24dd4692005-11-08 21:39:26 -08003681static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003682chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003683{
NeilBrown08a02ec2007-05-09 02:35:38 -07003684 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003685 mddev->chunk_sectors != mddev->new_chunk_sectors)
3686 return sprintf(page, "%d (%d)\n",
3687 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003688 mddev->chunk_sectors << 9);
3689 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003690}
3691
3692static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003693chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003694{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003695 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003696 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003697
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003698 err = kstrtoul(buf, 10, &n);
3699 if (err < 0)
3700 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003701
NeilBrown67918752014-12-15 12:57:01 +11003702 err = mddev_lock(mddev);
3703 if (err)
3704 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003705 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003706 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003707 err = -EBUSY;
3708 else if (mddev->ro)
3709 err = -EROFS;
3710 else {
3711 mddev->new_chunk_sectors = n >> 9;
3712 err = mddev->pers->check_reshape(mddev);
3713 if (err)
3714 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003715 }
NeilBrownb3546032009-03-31 14:56:41 +11003716 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003717 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003718 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003719 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003720 }
NeilBrown67918752014-12-15 12:57:01 +11003721 mddev_unlock(mddev);
3722 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003723}
3724static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003725__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003726
NeilBrowna94213b2006-06-26 00:28:00 -07003727static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003728resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003729{
NeilBrownd1a7c502009-03-31 15:24:32 +11003730 if (mddev->recovery_cp == MaxSector)
3731 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003732 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3733}
3734
3735static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003736resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003737{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003738 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003739 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003740
3741 if (cmd_match(buf, "none"))
3742 n = MaxSector;
3743 else {
3744 err = kstrtoull(buf, 10, &n);
3745 if (err < 0)
3746 return err;
3747 if (n != (sector_t)n)
3748 return -EINVAL;
3749 }
NeilBrowna94213b2006-06-26 00:28:00 -07003750
NeilBrown67918752014-12-15 12:57:01 +11003751 err = mddev_lock(mddev);
3752 if (err)
3753 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003754 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003755 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003756
NeilBrown67918752014-12-15 12:57:01 +11003757 if (!err) {
3758 mddev->recovery_cp = n;
3759 if (mddev->pers)
3760 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3761 }
3762 mddev_unlock(mddev);
3763 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003764}
3765static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003766__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3767 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003768
NeilBrown9e653b62006-06-26 00:27:58 -07003769/*
3770 * The array state can be:
3771 *
3772 * clear
3773 * No devices, no size, no level
3774 * Equivalent to STOP_ARRAY ioctl
3775 * inactive
3776 * May have some settings, but array is not active
3777 * all IO results in error
3778 * When written, doesn't tear down array, but just stops it
3779 * suspended (not supported yet)
3780 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003781 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003782 * readonly
3783 * no resync can happen. no superblocks get written.
3784 * write requests fail
3785 * read-auto
3786 * like readonly, but behaves like 'clean' on a write request.
3787 *
3788 * clean - no pending writes, but otherwise active.
3789 * When written to inactive array, starts without resync
3790 * If a write request arrives then
3791 * if metadata is known, mark 'dirty' and switch to 'active'.
3792 * if not known, block and switch to write-pending
3793 * If written to an active array that has pending writes, then fails.
3794 * active
3795 * fully active: IO and resync can be happening.
3796 * When written to inactive array, starts with resync
3797 *
3798 * write-pending
3799 * clean, but writes are blocked waiting for 'active' to be written.
3800 *
3801 * active-idle
3802 * like active, but no writes have been seen for a while (100msec).
3803 *
3804 */
3805enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3806 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003807static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003808 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3809 "write-pending", "active-idle", NULL };
3810
3811static int match_word(const char *word, char **list)
3812{
3813 int n;
3814 for (n=0; list[n]; n++)
3815 if (cmd_match(word, list[n]))
3816 break;
3817 return n;
3818}
3819
3820static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003821array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003822{
3823 enum array_state st = inactive;
3824
3825 if (mddev->pers)
3826 switch(mddev->ro) {
3827 case 1:
3828 st = readonly;
3829 break;
3830 case 2:
3831 st = read_auto;
3832 break;
3833 case 0:
3834 if (mddev->in_sync)
3835 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003836 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003837 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003838 else if (mddev->safemode)
3839 st = active_idle;
3840 else
3841 st = active;
3842 }
3843 else {
3844 if (list_empty(&mddev->disks) &&
3845 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003846 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003847 st = clear;
3848 else
3849 st = inactive;
3850 }
3851 return sprintf(page, "%s\n", array_states[st]);
3852}
3853
NeilBrownf72ffdd2014-09-30 14:23:59 +10003854static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3855static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3856static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003857static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003858
3859static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003860array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003861{
NeilBrown67918752014-12-15 12:57:01 +11003862 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003863 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003864
3865 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3866 /* don't take reconfig_mutex when toggling between
3867 * clean and active
3868 */
3869 spin_lock(&mddev->lock);
3870 if (st == active) {
3871 restart_array(mddev);
3872 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3873 wake_up(&mddev->sb_wait);
3874 err = 0;
3875 } else /* st == clean */ {
3876 restart_array(mddev);
3877 if (atomic_read(&mddev->writes_pending) == 0) {
3878 if (mddev->in_sync == 0) {
3879 mddev->in_sync = 1;
3880 if (mddev->safemode == 1)
3881 mddev->safemode = 0;
3882 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3883 }
3884 err = 0;
3885 } else
3886 err = -EBUSY;
3887 }
3888 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003889 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003890 }
3891 err = mddev_lock(mddev);
3892 if (err)
3893 return err;
3894 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003895 switch(st) {
3896 case bad_word:
3897 break;
3898 case clear:
3899 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003900 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003901 break;
3902 case inactive:
3903 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003904 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003905 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003906 else
NeilBrowne6910632008-02-06 01:39:51 -08003907 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003908 break;
3909 case suspended:
3910 break; /* not supported yet */
3911 case readonly:
3912 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003913 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003914 else {
3915 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003916 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003917 err = do_md_run(mddev);
3918 }
3919 break;
3920 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003921 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003922 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003923 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003924 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003925 err = restart_array(mddev);
3926 if (err == 0) {
3927 mddev->ro = 2;
3928 set_disk_ro(mddev->gendisk, 0);
3929 }
NeilBrown9e653b62006-06-26 00:27:58 -07003930 } else {
3931 mddev->ro = 2;
3932 err = do_md_run(mddev);
3933 }
3934 break;
3935 case clean:
3936 if (mddev->pers) {
3937 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003938 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003939 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003940 if (mddev->in_sync == 0) {
3941 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003942 if (mddev->safemode == 1)
3943 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003944 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003945 }
3946 err = 0;
3947 } else
3948 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003949 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003950 } else
3951 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003952 break;
3953 case active:
3954 if (mddev->pers) {
3955 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003956 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003957 wake_up(&mddev->sb_wait);
3958 err = 0;
3959 } else {
3960 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003961 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003962 err = do_md_run(mddev);
3963 }
3964 break;
3965 case write_pending:
3966 case active_idle:
3967 /* these cannot be set */
3968 break;
3969 }
NeilBrown67918752014-12-15 12:57:01 +11003970
3971 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11003972 if (mddev->hold_active == UNTIL_IOCTL)
3973 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003974 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003975 }
NeilBrown67918752014-12-15 12:57:01 +11003976 mddev_unlock(mddev);
3977 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07003978}
NeilBrown80ca3a42006-07-10 04:44:18 -07003979static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10003980__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003981
NeilBrown6d7ff7382006-01-06 00:21:16 -08003982static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003983max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003984 return sprintf(page, "%d\n",
3985 atomic_read(&mddev->max_corr_read_errors));
3986}
3987
3988static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003989max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003990{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003991 unsigned int n;
3992 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11003993
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003994 rv = kstrtouint(buf, 10, &n);
3995 if (rv < 0)
3996 return rv;
3997 atomic_set(&mddev->max_corr_read_errors, n);
3998 return len;
Robert Becker1e509152009-12-14 12:49:58 +11003999}
4000
4001static struct md_sysfs_entry max_corr_read_errors =
4002__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4003 max_corrected_read_errors_store);
4004
4005static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004006null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004007{
4008 return -EINVAL;
4009}
4010
4011static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004012new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004013{
4014 /* buf must be %d:%d\n? giving major and minor numbers */
4015 /* The new device is added to the array.
4016 * If the array has a persistent superblock, we read the
4017 * superblock to initialise info and check validity.
4018 * Otherwise, only checking done is that in bind_rdev_to_array,
4019 * which mainly checks size.
4020 */
4021 char *e;
4022 int major = simple_strtoul(buf, &e, 10);
4023 int minor;
4024 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004025 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004026 int err;
4027
4028 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4029 return -EINVAL;
4030 minor = simple_strtoul(e+1, &e, 10);
4031 if (*e && *e != '\n')
4032 return -EINVAL;
4033 dev = MKDEV(major, minor);
4034 if (major != MAJOR(dev) ||
4035 minor != MINOR(dev))
4036 return -EOVERFLOW;
4037
NeilBrown67918752014-12-15 12:57:01 +11004038 flush_workqueue(md_misc_wq);
4039
4040 err = mddev_lock(mddev);
4041 if (err)
4042 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004043 if (mddev->persistent) {
4044 rdev = md_import_device(dev, mddev->major_version,
4045 mddev->minor_version);
4046 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004047 struct md_rdev *rdev0
4048 = list_entry(mddev->disks.next,
4049 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004050 err = super_types[mddev->major_version]
4051 .load_super(rdev, rdev0, mddev->minor_version);
4052 if (err < 0)
4053 goto out;
4054 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004055 } else if (mddev->external)
4056 rdev = md_import_device(dev, -2, -1);
4057 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004058 rdev = md_import_device(dev, -1, -1);
4059
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004060 if (IS_ERR(rdev)) {
4061 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004062 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004063 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004064 err = bind_rdev_to_array(rdev, mddev);
4065 out:
4066 if (err)
4067 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004068 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004069 return err ? err : len;
4070}
4071
4072static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004073__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004074
4075static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004076bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004077{
4078 char *end;
4079 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004080 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004081
NeilBrown67918752014-12-15 12:57:01 +11004082 err = mddev_lock(mddev);
4083 if (err)
4084 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004085 if (!mddev->bitmap)
4086 goto out;
4087 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4088 while (*buf) {
4089 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4090 if (buf == end) break;
4091 if (*end == '-') { /* range */
4092 buf = end + 1;
4093 end_chunk = simple_strtoul(buf, &end, 0);
4094 if (buf == end) break;
4095 }
4096 if (*end && !isspace(*end)) break;
4097 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004098 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004099 }
4100 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4101out:
NeilBrown67918752014-12-15 12:57:01 +11004102 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004103 return len;
4104}
4105
4106static struct md_sysfs_entry md_bitmap =
4107__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4108
4109static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004110size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004111{
Andre Noll58c0fed2009-03-31 14:33:13 +11004112 return sprintf(page, "%llu\n",
4113 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004114}
4115
NeilBrownfd01b882011-10-11 16:47:53 +11004116static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004117
4118static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004119size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004120{
4121 /* If array is inactive, we can reduce the component size, but
4122 * not increase it (except from 0).
4123 * If array is active, we can try an on-line resize
4124 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004125 sector_t sectors;
4126 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004127
Andre Noll58c0fed2009-03-31 14:33:13 +11004128 if (err < 0)
4129 return err;
NeilBrown67918752014-12-15 12:57:01 +11004130 err = mddev_lock(mddev);
4131 if (err)
4132 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004133 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004134 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004135 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004136 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004137 if (mddev->dev_sectors == 0 ||
4138 mddev->dev_sectors > sectors)
4139 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004140 else
4141 err = -ENOSPC;
4142 }
NeilBrown67918752014-12-15 12:57:01 +11004143 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004144 return err ? err : len;
4145}
4146
4147static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004148__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004149
Masanari Iida83f0d772012-10-30 00:18:08 +09004150/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004151 * This is one of
4152 * 'none' for arrays with no metadata (good luck...)
4153 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004154 * or N.M for internally known formats
4155 */
4156static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004157metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004158{
4159 if (mddev->persistent)
4160 return sprintf(page, "%d.%d\n",
4161 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004162 else if (mddev->external)
4163 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004164 else
4165 return sprintf(page, "none\n");
4166}
4167
4168static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004169metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004170{
4171 int major, minor;
4172 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004173 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004174 /* Changing the details of 'external' metadata is
4175 * always permitted. Otherwise there must be
4176 * no devices attached to the array.
4177 */
NeilBrown67918752014-12-15 12:57:01 +11004178
4179 err = mddev_lock(mddev);
4180 if (err)
4181 return err;
4182 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004183 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4184 ;
4185 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004186 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004187
NeilBrown67918752014-12-15 12:57:01 +11004188 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004189 if (cmd_match(buf, "none")) {
4190 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004191 mddev->external = 0;
4192 mddev->major_version = 0;
4193 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004194 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004195 }
4196 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004197 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004198 if (namelen >= sizeof(mddev->metadata_type))
4199 namelen = sizeof(mddev->metadata_type)-1;
4200 strncpy(mddev->metadata_type, buf+9, namelen);
4201 mddev->metadata_type[namelen] = 0;
4202 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4203 mddev->metadata_type[--namelen] = 0;
4204 mddev->persistent = 0;
4205 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004206 mddev->major_version = 0;
4207 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004208 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004209 }
4210 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004211 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004212 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004213 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004214 buf = e+1;
4215 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004216 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004217 goto out_unlock;
4218 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004219 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004220 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004221 mddev->major_version = major;
4222 mddev->minor_version = minor;
4223 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004224 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004225 err = 0;
4226out_unlock:
4227 mddev_unlock(mddev);
4228 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004229}
4230
4231static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004232__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004233
NeilBrowna35b0d62006-01-06 00:20:49 -08004234static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004235action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004236{
NeilBrown7eec3142005-11-08 21:39:44 -08004237 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004238 unsigned long recovery = mddev->recovery;
4239 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004240 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004241 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4242 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4243 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004244 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004245 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4246 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004247 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004248 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004249 type = "check";
4250 else
4251 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004252 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004253 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004254 else if (mddev->reshape_position != MaxSector)
4255 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004256 }
4257 return sprintf(page, "%s\n", type);
4258}
4259
4260static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004261action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004262{
NeilBrown7eec3142005-11-08 21:39:44 -08004263 if (!mddev->pers || !mddev->pers->sync_request)
4264 return -EINVAL;
4265
NeilBrownb6a9ce62009-05-26 09:41:17 +10004266
4267 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004268 if (cmd_match(page, "frozen"))
4269 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4270 else
4271 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004272 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4273 mddev_lock(mddev) == 0) {
4274 flush_workqueue(md_misc_wq);
4275 if (mddev->sync_thread) {
4276 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004277 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004278 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004279 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004280 }
NeilBrown03c902e2006-01-06 00:20:46 -08004281 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4282 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004283 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004284 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004285 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004286 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004287 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004288 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004289 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004290 int err;
4291 if (mddev->pers->start_reshape == NULL)
4292 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004293 err = mddev_lock(mddev);
4294 if (!err) {
NeilBrown56ccc112015-05-28 17:53:29 +10004295 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004296 err = mddev->pers->start_reshape(mddev);
4297 mddev_unlock(mddev);
4298 }
NeilBrown16484bf2006-03-27 01:18:13 -08004299 if (err)
4300 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004301 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004302 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004303 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004304 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004305 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004306 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004307 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004308 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4309 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004310 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004311 if (mddev->ro == 2) {
4312 /* A write to sync_action is enough to justify
4313 * canceling read-auto mode
4314 */
4315 mddev->ro = 0;
4316 md_wakeup_thread(mddev->sync_thread);
4317 }
NeilBrown03c902e2006-01-06 00:20:46 -08004318 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004319 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004320 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004321 return len;
4322}
4323
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004324static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004325__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004326
4327static ssize_t
4328last_sync_action_show(struct mddev *mddev, char *page)
4329{
4330 return sprintf(page, "%s\n", mddev->last_sync_action);
4331}
4332
4333static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4334
NeilBrown9d888832005-11-08 21:39:26 -08004335static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004336mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004337{
4338 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004339 (unsigned long long)
4340 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004341}
4342
NeilBrown80ca3a42006-07-10 04:44:18 -07004343static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004344
NeilBrown88202a02006-01-06 00:21:36 -08004345static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004346sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004347{
4348 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4349 mddev->sync_speed_min ? "local": "system");
4350}
4351
4352static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004353sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004354{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004355 unsigned int min;
4356 int rv;
4357
NeilBrown88202a02006-01-06 00:21:36 -08004358 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004359 min = 0;
4360 } else {
4361 rv = kstrtouint(buf, 10, &min);
4362 if (rv < 0)
4363 return rv;
4364 if (min == 0)
4365 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004366 }
NeilBrown88202a02006-01-06 00:21:36 -08004367 mddev->sync_speed_min = min;
4368 return len;
4369}
4370
4371static struct md_sysfs_entry md_sync_min =
4372__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4373
4374static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004375sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004376{
4377 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4378 mddev->sync_speed_max ? "local": "system");
4379}
4380
4381static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004382sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004383{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004384 unsigned int max;
4385 int rv;
4386
NeilBrown88202a02006-01-06 00:21:36 -08004387 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004388 max = 0;
4389 } else {
4390 rv = kstrtouint(buf, 10, &max);
4391 if (rv < 0)
4392 return rv;
4393 if (max == 0)
4394 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004395 }
NeilBrown88202a02006-01-06 00:21:36 -08004396 mddev->sync_speed_max = max;
4397 return len;
4398}
4399
4400static struct md_sysfs_entry md_sync_max =
4401__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4402
Iustin Popd7f3d292007-10-16 23:30:54 -07004403static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004404degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004405{
4406 return sprintf(page, "%d\n", mddev->degraded);
4407}
4408static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004409
4410static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004411sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004412{
4413 return sprintf(page, "%d\n", mddev->parallel_resync);
4414}
4415
4416static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004417sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004418{
4419 long n;
4420
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004421 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004422 return -EINVAL;
4423
4424 if (n != 0 && n != 1)
4425 return -EINVAL;
4426
4427 mddev->parallel_resync = n;
4428
4429 if (mddev->sync_thread)
4430 wake_up(&resync_wait);
4431
4432 return len;
4433}
4434
4435/* force parallel resync, even with shared block devices */
4436static struct md_sysfs_entry md_sync_force_parallel =
4437__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4438 sync_force_parallel_show, sync_force_parallel_store);
4439
4440static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004441sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004442{
4443 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004444 if (mddev->curr_resync == 0)
4445 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004446 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4447 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004448 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004449 db = resync - mddev->resync_mark_cnt;
4450 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004451}
4452
NeilBrown80ca3a42006-07-10 04:44:18 -07004453static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004454
4455static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004456sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004457{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004458 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004459
NeilBrownacb180b2009-04-14 16:28:34 +10004460 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4461 return sprintf(page, "none\n");
4462
NeilBrown72f36d52012-10-11 14:25:57 +11004463 if (mddev->curr_resync == 1 ||
4464 mddev->curr_resync == 2)
4465 return sprintf(page, "delayed\n");
4466
NeilBrownc804cde2012-05-21 09:28:33 +10004467 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4468 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004469 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004470 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004471 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004472
NeilBrownacb180b2009-04-14 16:28:34 +10004473 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004474 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004475}
4476
NeilBrown750f1992014-09-30 08:53:05 +10004477static struct md_sysfs_entry md_sync_completed =
4478 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004479
NeilBrowne464eaf2006-03-27 01:18:14 -08004480static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004481min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004482{
4483 return sprintf(page, "%llu\n",
4484 (unsigned long long)mddev->resync_min);
4485}
4486static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004487min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004488{
4489 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004490 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004491
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004492 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004493 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004494
4495 spin_lock(&mddev->lock);
4496 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004497 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004498 goto out_unlock;
4499
4500 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004501 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004502 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004503
NeilBrown50c37b12015-03-23 17:36:38 +11004504 /* Round down to multiple of 4K for safety */
4505 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004506 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004507
NeilBrown23da4222014-12-15 12:57:01 +11004508out_unlock:
4509 spin_unlock(&mddev->lock);
4510 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004511}
4512
4513static struct md_sysfs_entry md_min_sync =
4514__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4515
4516static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004517max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004518{
4519 if (mddev->resync_max == MaxSector)
4520 return sprintf(page, "max\n");
4521 else
4522 return sprintf(page, "%llu\n",
4523 (unsigned long long)mddev->resync_max);
4524}
4525static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004526max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004527{
NeilBrown23da4222014-12-15 12:57:01 +11004528 int err;
4529 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004530 if (strncmp(buf, "max", 3) == 0)
4531 mddev->resync_max = MaxSector;
4532 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004533 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004534 int chunk;
4535
4536 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004537 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004538 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004539 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004540 goto out_unlock;
4541
4542 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004543 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004544 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004545 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004546 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004547
4548 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004549 chunk = mddev->chunk_sectors;
4550 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004551 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004552
4553 err = -EINVAL;
4554 if (sector_div(temp, chunk))
4555 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004556 }
4557 mddev->resync_max = max;
4558 }
4559 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004560 err = 0;
4561out_unlock:
4562 spin_unlock(&mddev->lock);
4563 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004564}
4565
4566static struct md_sysfs_entry md_max_sync =
4567__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4568
4569static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004570suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004571{
4572 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4573}
4574
4575static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004576suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004577{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004578 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004579 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004580
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004581 err = kstrtoull(buf, 10, &new);
4582 if (err < 0)
4583 return err;
4584 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004585 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004586
NeilBrown67918752014-12-15 12:57:01 +11004587 err = mddev_lock(mddev);
4588 if (err)
4589 return err;
4590 err = -EINVAL;
4591 if (mddev->pers == NULL ||
4592 mddev->pers->quiesce == NULL)
4593 goto unlock;
4594 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004595 mddev->suspend_lo = new;
4596 if (new >= old)
4597 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004598 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004599 else {
4600 /* Expanding suspended region - need to wait */
4601 mddev->pers->quiesce(mddev, 1);
4602 mddev->pers->quiesce(mddev, 0);
4603 }
NeilBrown67918752014-12-15 12:57:01 +11004604 err = 0;
4605unlock:
4606 mddev_unlock(mddev);
4607 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004608}
4609static struct md_sysfs_entry md_suspend_lo =
4610__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4611
NeilBrowne464eaf2006-03-27 01:18:14 -08004612static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004613suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004614{
4615 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4616}
4617
4618static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004619suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004620{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004621 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004622 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004623
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004624 err = kstrtoull(buf, 10, &new);
4625 if (err < 0)
4626 return err;
4627 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004628 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004629
NeilBrown67918752014-12-15 12:57:01 +11004630 err = mddev_lock(mddev);
4631 if (err)
4632 return err;
4633 err = -EINVAL;
4634 if (mddev->pers == NULL ||
4635 mddev->pers->quiesce == NULL)
4636 goto unlock;
4637 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004638 mddev->suspend_hi = new;
4639 if (new <= old)
4640 /* Shrinking suspended region */
4641 mddev->pers->quiesce(mddev, 2);
4642 else {
4643 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004644 mddev->pers->quiesce(mddev, 1);
4645 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004646 }
NeilBrown67918752014-12-15 12:57:01 +11004647 err = 0;
4648unlock:
4649 mddev_unlock(mddev);
4650 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004651}
4652static struct md_sysfs_entry md_suspend_hi =
4653__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4654
NeilBrown08a02ec2007-05-09 02:35:38 -07004655static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004656reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004657{
4658 if (mddev->reshape_position != MaxSector)
4659 return sprintf(page, "%llu\n",
4660 (unsigned long long)mddev->reshape_position);
4661 strcpy(page, "none\n");
4662 return 5;
4663}
4664
4665static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004666reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004667{
NeilBrownc6563a82012-05-21 09:27:00 +10004668 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004669 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004670 int err;
NeilBrown67918752014-12-15 12:57:01 +11004671
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004672 err = kstrtoull(buf, 10, &new);
4673 if (err < 0)
4674 return err;
4675 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004676 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004677 err = mddev_lock(mddev);
4678 if (err)
4679 return err;
4680 err = -EBUSY;
4681 if (mddev->pers)
4682 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004683 mddev->reshape_position = new;
4684 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004685 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004686 mddev->new_level = mddev->level;
4687 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004688 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004689 rdev_for_each(rdev, mddev)
4690 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004691 err = 0;
4692unlock:
4693 mddev_unlock(mddev);
4694 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004695}
4696
4697static struct md_sysfs_entry md_reshape_position =
4698__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4699 reshape_position_store);
4700
Dan Williamsb522adc2009-03-31 15:00:31 +11004701static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004702reshape_direction_show(struct mddev *mddev, char *page)
4703{
4704 return sprintf(page, "%s\n",
4705 mddev->reshape_backwards ? "backwards" : "forwards");
4706}
4707
4708static ssize_t
4709reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4710{
4711 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004712 int err;
4713
NeilBrown2c810cd2012-05-21 09:27:00 +10004714 if (cmd_match(buf, "forwards"))
4715 backwards = 0;
4716 else if (cmd_match(buf, "backwards"))
4717 backwards = 1;
4718 else
4719 return -EINVAL;
4720 if (mddev->reshape_backwards == backwards)
4721 return len;
4722
NeilBrown67918752014-12-15 12:57:01 +11004723 err = mddev_lock(mddev);
4724 if (err)
4725 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004726 /* check if we are allowed to change */
4727 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004728 err = -EBUSY;
4729 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004730 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004731 err = -EINVAL;
4732 else
4733 mddev->reshape_backwards = backwards;
4734 mddev_unlock(mddev);
4735 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004736}
4737
4738static struct md_sysfs_entry md_reshape_direction =
4739__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4740 reshape_direction_store);
4741
4742static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004743array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004744{
4745 if (mddev->external_size)
4746 return sprintf(page, "%llu\n",
4747 (unsigned long long)mddev->array_sectors/2);
4748 else
4749 return sprintf(page, "default\n");
4750}
4751
4752static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004753array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004754{
4755 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004756 int err;
4757
4758 err = mddev_lock(mddev);
4759 if (err)
4760 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004761
4762 if (strncmp(buf, "default", 7) == 0) {
4763 if (mddev->pers)
4764 sectors = mddev->pers->size(mddev, 0, 0);
4765 else
4766 sectors = mddev->array_sectors;
4767
4768 mddev->external_size = 0;
4769 } else {
4770 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004771 err = -EINVAL;
4772 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4773 err = -E2BIG;
4774 else
4775 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004776 }
4777
NeilBrown67918752014-12-15 12:57:01 +11004778 if (!err) {
4779 mddev->array_sectors = sectors;
4780 if (mddev->pers) {
4781 set_capacity(mddev->gendisk, mddev->array_sectors);
4782 revalidate_disk(mddev->gendisk);
4783 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004784 }
NeilBrown67918752014-12-15 12:57:01 +11004785 mddev_unlock(mddev);
4786 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004787}
4788
4789static struct md_sysfs_entry md_array_size =
4790__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4791 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004792
NeilBrowneae17012005-11-08 21:39:23 -08004793static struct attribute *md_default_attrs[] = {
4794 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004795 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004796 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004797 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004798 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004799 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004800 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004801 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004802 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004803 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004804 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004805 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004806 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004807 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004808 NULL,
4809};
4810
4811static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004812 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004813 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004814 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004815 &md_sync_min.attr,
4816 &md_sync_max.attr,
4817 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004818 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004819 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004820 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004821 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004822 &md_suspend_lo.attr,
4823 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004824 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004825 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004826 NULL,
4827};
NeilBrown411036f2005-11-08 21:39:40 -08004828static struct attribute_group md_redundancy_group = {
4829 .name = NULL,
4830 .attrs = md_redundancy_attrs,
4831};
4832
NeilBrowneae17012005-11-08 21:39:23 -08004833static ssize_t
4834md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4835{
4836 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004837 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004838 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004839
4840 if (!entry->show)
4841 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004842 spin_lock(&all_mddevs_lock);
4843 if (list_empty(&mddev->all_mddevs)) {
4844 spin_unlock(&all_mddevs_lock);
4845 return -EBUSY;
4846 }
4847 mddev_get(mddev);
4848 spin_unlock(&all_mddevs_lock);
4849
NeilBrownb7b17c92014-12-15 12:56:59 +11004850 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004851 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004852 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004853}
4854
4855static ssize_t
4856md_attr_store(struct kobject *kobj, struct attribute *attr,
4857 const char *page, size_t length)
4858{
4859 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004860 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004861 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004862
4863 if (!entry->store)
4864 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004865 if (!capable(CAP_SYS_ADMIN))
4866 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004867 spin_lock(&all_mddevs_lock);
4868 if (list_empty(&mddev->all_mddevs)) {
4869 spin_unlock(&all_mddevs_lock);
4870 return -EBUSY;
4871 }
4872 mddev_get(mddev);
4873 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004874 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004875 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004876 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004877}
4878
4879static void md_free(struct kobject *ko)
4880{
NeilBrownfd01b882011-10-11 16:47:53 +11004881 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004882
4883 if (mddev->sysfs_state)
4884 sysfs_put(mddev->sysfs_state);
4885
NeilBrown6cd18e72015-04-27 14:12:22 +10004886 if (mddev->queue)
4887 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004888 if (mddev->gendisk) {
4889 del_gendisk(mddev->gendisk);
4890 put_disk(mddev->gendisk);
4891 }
NeilBrowna21d1502009-01-09 08:31:09 +11004892
NeilBrowneae17012005-11-08 21:39:23 -08004893 kfree(mddev);
4894}
4895
Emese Revfy52cf25d2010-01-19 02:58:23 +01004896static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004897 .show = md_attr_show,
4898 .store = md_attr_store,
4899};
4900static struct kobj_type md_ktype = {
4901 .release = md_free,
4902 .sysfs_ops = &md_sysfs_ops,
4903 .default_attrs = md_default_attrs,
4904};
4905
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906int mdp_major = 0;
4907
Dan Williams5fd3a172009-03-04 00:57:25 -07004908static void mddev_delayed_delete(struct work_struct *ws)
4909{
NeilBrownfd01b882011-10-11 16:47:53 +11004910 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004911
NeilBrown43a70502009-12-14 12:49:55 +11004912 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004913 kobject_del(&mddev->kobj);
4914 kobject_put(&mddev->kobj);
4915}
4916
NeilBrownefeb53c2009-01-09 08:31:10 +11004917static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004919 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004920 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004922 int partitioned;
4923 int shift;
4924 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004925 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004928 return -ENODEV;
4929
4930 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4931 shift = partitioned ? MdpMinorShift : 0;
4932 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Tejun Heoe804ac72010-10-15 15:36:08 +02004934 /* wait for any previous instance of this device to be
4935 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004936 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004937 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004938
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004939 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004940 error = -EEXIST;
4941 if (mddev->gendisk)
4942 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004943
4944 if (name) {
4945 /* Need to ensure that 'name' is not a duplicate.
4946 */
NeilBrownfd01b882011-10-11 16:47:53 +11004947 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004948 spin_lock(&all_mddevs_lock);
4949
4950 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4951 if (mddev2->gendisk &&
4952 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4953 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004954 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004955 }
4956 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 }
NeilBrown8b765392009-01-09 08:31:08 +11004958
NeilBrown0909dc42009-07-01 12:27:21 +10004959 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004960 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004961 if (!mddev->queue)
4962 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004963 mddev->queue->queuedata = mddev;
4964
NeilBrown409c57f2009-03-31 14:39:39 +11004965 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004966 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 disk = alloc_disk(1 << shift);
4969 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004970 blk_cleanup_queue(mddev->queue);
4971 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004972 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004974 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004976 if (name)
4977 strcpy(disk->disk_name, name);
4978 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004980 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 disk->fops = &md_fops;
4983 disk->private_data = mddev;
4984 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004985 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004986 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004987 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004988 * remove it now.
4989 */
4990 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004992 /* As soon as we call add_disk(), another thread could get
4993 * through to md_open, so make sure it doesn't get too far
4994 */
4995 mutex_lock(&mddev->open_mutex);
4996 add_disk(disk);
4997
Tejun Heoed9e1982008-08-25 19:56:05 +09004998 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4999 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005000 if (error) {
5001 /* This isn't possible, but as kobject_init_and_add is marked
5002 * __must_check, we must do something with the result
5003 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005004 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
5005 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005006 error = 0;
5007 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005008 if (mddev->kobj.sd &&
5009 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11005010 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005011 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005012 abort:
5013 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005014 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005015 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005016 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005017 }
NeilBrownd3374822009-01-09 08:31:10 +11005018 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005019 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005020}
5021
5022static struct kobject *md_probe(dev_t dev, int *part, void *data)
5023{
5024 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 return NULL;
5026}
5027
NeilBrownefeb53c2009-01-09 08:31:10 +11005028static int add_named_array(const char *val, struct kernel_param *kp)
5029{
5030 /* val must be "md_*" where * is not all digits.
5031 * We allocate an array with a large free minor number, and
5032 * set the name to val. val must not already be an active name.
5033 */
5034 int len = strlen(val);
5035 char buf[DISK_NAME_LEN];
5036
5037 while (len && val[len-1] == '\n')
5038 len--;
5039 if (len >= DISK_NAME_LEN)
5040 return -E2BIG;
5041 strlcpy(buf, val, len+1);
5042 if (strncmp(buf, "md_", 3) != 0)
5043 return -EINVAL;
5044 return md_alloc(0, buf);
5045}
5046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047static void md_safemode_timeout(unsigned long data)
5048{
NeilBrownfd01b882011-10-11 16:47:53 +11005049 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
Neil Brown0fd62b82008-06-28 08:31:36 +10005051 if (!atomic_read(&mddev->writes_pending)) {
5052 mddev->safemode = 1;
5053 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005054 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 md_wakeup_thread(mddev->thread);
5057}
5058
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005059static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
NeilBrownfd01b882011-10-11 16:47:53 +11005061int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062{
NeilBrown2604b702006-01-06 00:20:36 -08005063 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005064 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005065 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
NeilBrowna757e642005-04-16 15:26:42 -07005067 if (list_empty(&mddev->disks))
5068 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
5071 if (mddev->pers)
5072 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005073 /* Cannot run until previous stop completes properly */
5074 if (mddev->sysfs_active)
5075 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005076
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 /*
5078 * Analyze all RAID superblock(s)
5079 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005080 if (!mddev->raid_disks) {
5081 if (!mddev->persistent)
5082 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005083 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
NeilBrownd9d166c2006-01-06 00:20:51 -08005086 if (mddev->level != LEVEL_NONE)
5087 request_module("md-level-%d", mddev->level);
5088 else if (mddev->clevel[0])
5089 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
5091 /*
5092 * Drop all container device buffers, from now on
5093 * the only valid external interface is through the md
5094 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005096 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005097 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 continue;
5099 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005100 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005101
5102 /* perform some consistency tests on the device.
5103 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005104 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005105 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005106 if (rdev->meta_bdev) {
5107 /* Nothing to check */;
5108 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005109 if (mddev->dev_sectors &&
5110 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005111 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005112 printk("md: %s: data overlaps metadata\n",
5113 mdname(mddev));
5114 return -EINVAL;
5115 }
5116 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005117 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005118 > rdev->data_offset) {
5119 printk("md: %s: metadata overlaps data\n",
5120 mdname(mddev));
5121 return -EINVAL;
5122 }
5123 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005124 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 }
5126
NeilBrowna167f662010-10-26 18:31:13 +11005127 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005128 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005129
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005131 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005132 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005134 if (mddev->level != LEVEL_NONE)
5135 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5136 mddev->level);
5137 else
5138 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5139 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 return -EINVAL;
5141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005143 if (mddev->level != pers->level) {
5144 mddev->level = pers->level;
5145 mddev->new_level = pers->level;
5146 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005147 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
NeilBrownf6705572006-03-27 01:18:11 -08005149 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005150 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005151 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005152 module_put(pers->owner);
5153 return -EINVAL;
5154 }
5155
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005156 if (pers->sync_request) {
5157 /* Warn if this is a potentially silly
5158 * configuration.
5159 */
5160 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005161 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005162 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005163
NeilBrowndafb20f2012-03-19 12:46:39 +11005164 rdev_for_each(rdev, mddev)
5165 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005166 if (rdev < rdev2 &&
5167 rdev->bdev->bd_contains ==
5168 rdev2->bdev->bd_contains) {
5169 printk(KERN_WARNING
5170 "%s: WARNING: %s appears to be"
5171 " on the same physical disk as"
5172 " %s.\n",
5173 mdname(mddev),
5174 bdevname(rdev->bdev,b),
5175 bdevname(rdev2->bdev,b2));
5176 warned = 1;
5177 }
5178 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005179
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005180 if (warned)
5181 printk(KERN_WARNING
5182 "True protection against single-disk"
5183 " failure might be compromised.\n");
5184 }
5185
NeilBrown657390d2005-08-26 18:34:16 -07005186 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005187 /* may be over-ridden by personality */
5188 mddev->resync_max_sectors = mddev->dev_sectors;
5189
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005190 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
NeilBrown0f9552b52009-12-30 12:08:50 +11005192 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005193 mddev->ro = 2; /* read-only, but switch on first write */
5194
NeilBrown36d091f2014-12-15 12:56:58 +11005195 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005196 if (err)
5197 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005198 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005199 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5200 " but 'external_size' not in effect?\n", __func__);
5201 printk(KERN_ERR
5202 "md: invalid array_size %llu > default size %llu\n",
5203 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005204 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005205 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005206 }
NeilBrown36d091f2014-12-15 12:56:58 +11005207 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005208 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005209 struct bitmap *bitmap;
5210
5211 bitmap = bitmap_create(mddev, -1);
5212 if (IS_ERR(bitmap)) {
5213 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005214 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5215 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005216 } else
5217 mddev->bitmap = bitmap;
5218
NeilBrownb15c2e52006-01-06 00:20:16 -08005219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005221 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005222 if (mddev->private)
5223 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005224 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005225 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005226 bitmap_destroy(mddev);
5227 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 }
NeilBrown5c675f82014-12-15 12:56:56 +11005229 if (mddev->queue) {
5230 mddev->queue->backing_dev_info.congested_data = mddev;
5231 mddev->queue->backing_dev_info.congested_fn = md_congested;
5232 }
NeilBrown36d091f2014-12-15 12:56:58 +11005233 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005234 if (mddev->kobj.sd &&
5235 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005236 printk(KERN_WARNING
5237 "md: cannot register extra attributes for %s\n",
5238 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005239 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005240 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005241 mddev->ro = 0;
5242
NeilBrownf72ffdd2014-09-30 14:23:59 +10005243 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005244 atomic_set(&mddev->max_corr_read_errors,
5245 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005247 if (mddev_is_clustered(mddev))
5248 mddev->safemode_delay = 0;
5249 else
5250 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005252 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005253 spin_lock(&mddev->lock);
5254 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005255 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005256 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005257 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005258 if (rdev->raid_disk >= 0)
5259 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005260 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005261
NeilBrowna4a3d262015-07-17 11:57:30 +10005262 if (mddev->degraded && !mddev->ro)
5263 /* This ensures that recovering status is reported immediately
5264 * via sysfs - until a lack of spares is confirmed.
5265 */
5266 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005268
NeilBrown7a0a5352013-08-27 16:28:23 +10005269 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005270 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
NeilBrownd7603b72006-01-06 00:20:30 -08005272 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005273 sysfs_notify_dirent_safe(mddev->sysfs_state);
5274 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005275 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 return 0;
5277}
NeilBrown390ee602010-06-01 19:37:27 +10005278EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
NeilBrownfd01b882011-10-11 16:47:53 +11005280static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005281{
5282 int err;
5283
5284 err = md_run(mddev);
5285 if (err)
5286 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005287 err = bitmap_load(mddev);
5288 if (err) {
5289 bitmap_destroy(mddev);
5290 goto out;
5291 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005292
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005293 if (mddev_is_clustered(mddev))
5294 md_allow_write(mddev);
5295
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005296 md_wakeup_thread(mddev->thread);
5297 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5298
NeilBrownfe60b012010-03-29 11:10:42 +11005299 set_capacity(mddev->gendisk, mddev->array_sectors);
5300 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005301 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005302 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5303out:
5304 return err;
5305}
5306
NeilBrownfd01b882011-10-11 16:47:53 +11005307static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308{
5309 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Andre Noll80fab1d2008-07-11 22:02:21 +10005311 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005313 return -ENXIO;
5314 if (!mddev->pers)
5315 return -EINVAL;
5316 if (!mddev->ro)
5317 return -EBUSY;
5318 mddev->safemode = 0;
5319 mddev->ro = 0;
5320 set_disk_ro(disk, 0);
5321 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5322 mdname(mddev));
5323 /* Kick recovery or resync if necessary */
5324 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5325 md_wakeup_thread(mddev->thread);
5326 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005327 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329}
5330
NeilBrownfd01b882011-10-11 16:47:53 +11005331static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005332{
5333 mddev->array_sectors = 0;
5334 mddev->external_size = 0;
5335 mddev->dev_sectors = 0;
5336 mddev->raid_disks = 0;
5337 mddev->recovery_cp = 0;
5338 mddev->resync_min = 0;
5339 mddev->resync_max = MaxSector;
5340 mddev->reshape_position = MaxSector;
5341 mddev->external = 0;
5342 mddev->persistent = 0;
5343 mddev->level = LEVEL_NONE;
5344 mddev->clevel[0] = 0;
5345 mddev->flags = 0;
5346 mddev->ro = 0;
5347 mddev->metadata_type[0] = 0;
5348 mddev->chunk_sectors = 0;
5349 mddev->ctime = mddev->utime = 0;
5350 mddev->layout = 0;
5351 mddev->max_disks = 0;
5352 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005353 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005354 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005355 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005356 mddev->new_level = LEVEL_NONE;
5357 mddev->new_layout = 0;
5358 mddev->new_chunk_sectors = 0;
5359 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005360 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005361 mddev->suspend_lo = mddev->suspend_hi = 0;
5362 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5363 mddev->recovery = 0;
5364 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005365 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005366 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005367 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005368 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005369 mddev->bitmap_info.offset = 0;
5370 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005371 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005372 mddev->bitmap_info.chunksize = 0;
5373 mddev->bitmap_info.daemon_sleep = 0;
5374 mddev->bitmap_info.max_write_behind = 0;
5375}
5376
NeilBrownfd01b882011-10-11 16:47:53 +11005377static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005378{
NeilBrown6b6204e2013-05-09 09:48:30 +10005379 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005380 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005381 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005382 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005383 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005384 }
5385
5386 del_timer_sync(&mddev->safemode_timer);
5387
5388 bitmap_flush(mddev);
5389 md_super_wait(mddev);
5390
NeilBrownb6d428c2013-04-24 11:42:42 +10005391 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005392 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
5393 (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005394 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005395 if (!mddev_is_clustered(mddev))
5396 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005397 md_update_sb(mddev, 1);
5398 }
5399}
NeilBrowndefad612011-01-14 09:14:33 +11005400
NeilBrownfd01b882011-10-11 16:47:53 +11005401void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005402{
NeilBrown29f097c2013-11-14 17:54:51 +11005403 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005404 __md_stop_writes(mddev);
5405 mddev_unlock(mddev);
5406}
NeilBrown390ee602010-06-01 19:37:27 +10005407EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005408
NeilBrown5aa61f42014-12-15 12:56:57 +11005409static void mddev_detach(struct mddev *mddev)
5410{
5411 struct bitmap *bitmap = mddev->bitmap;
5412 /* wait for behind writes to complete */
5413 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5414 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5415 mdname(mddev));
5416 /* need to kick something here to make sure I/O goes? */
5417 wait_event(bitmap->behind_wait,
5418 atomic_read(&bitmap->behind_writes) == 0);
5419 }
NeilBrown36d091f2014-12-15 12:56:58 +11005420 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005421 mddev->pers->quiesce(mddev, 1);
5422 mddev->pers->quiesce(mddev, 0);
5423 }
5424 md_unregister_thread(&mddev->thread);
5425 if (mddev->queue)
5426 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5427}
5428
NeilBrown5eff3c42012-11-19 10:47:48 +11005429static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005430{
NeilBrown36d091f2014-12-15 12:56:58 +11005431 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005432 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005433 /* Ensure ->event_work is done */
5434 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005435 spin_lock(&mddev->lock);
5436 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005437 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005438 spin_unlock(&mddev->lock);
5439 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005440 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005441 if (pers->sync_request && mddev->to_remove == NULL)
5442 mddev->to_remove = &md_redundancy_group;
5443 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005444 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005445}
NeilBrown5eff3c42012-11-19 10:47:48 +11005446
5447void md_stop(struct mddev *mddev)
5448{
5449 /* stop the array and free an attached data structures.
5450 * This is called from dm-raid
5451 */
5452 __md_stop(mddev);
5453 bitmap_destroy(mddev);
5454 if (mddev->bio_set)
5455 bioset_free(mddev->bio_set);
5456}
5457
NeilBrown390ee602010-06-01 19:37:27 +10005458EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005459
NeilBrowna05b7ea2012-07-19 15:59:18 +10005460static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005461{
5462 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005463 int did_freeze = 0;
5464
5465 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5466 did_freeze = 1;
5467 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5468 md_wakeup_thread(mddev->thread);
5469 }
NeilBrownf851b602014-12-11 10:02:10 +11005470 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005471 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005472 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005473 /* Thread might be blocked waiting for metadata update
5474 * which will now never happen */
5475 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005476
NeilBrown88724bf2015-09-24 14:00:51 +10005477 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5478 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005479 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005480 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5481 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005482 wait_event(mddev->sb_wait,
5483 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005484 mddev_lock_nointr(mddev);
5485
NeilBrowna4bd82d2010-03-29 13:23:10 +11005486 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005487 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005488 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005489 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005490 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005491 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005492 if (did_freeze) {
5493 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005494 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005495 md_wakeup_thread(mddev->thread);
5496 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005497 err = -EBUSY;
5498 goto out;
5499 }
5500 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005501 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005502
5503 err = -ENXIO;
5504 if (mddev->ro==1)
5505 goto out;
5506 mddev->ro = 1;
5507 set_disk_ro(mddev->gendisk, 1);
5508 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005509 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5510 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005511 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005512 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005513 }
5514out:
5515 mutex_unlock(&mddev->open_mutex);
5516 return err;
5517}
5518
NeilBrown9e653b62006-06-26 00:27:58 -07005519/* mode:
5520 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005521 * 2 - stop but do not disassemble array
5522 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005523static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005524 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005527 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005528 int did_freeze = 0;
5529
5530 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5531 did_freeze = 1;
5532 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5533 md_wakeup_thread(mddev->thread);
5534 }
NeilBrownf851b602014-12-11 10:02:10 +11005535 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005536 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005537 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005538 /* Thread might be blocked waiting for metadata update
5539 * which will now never happen */
5540 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005541
NeilBrown30b8feb2013-11-14 15:16:17 +11005542 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005543 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5544 !test_bit(MD_RECOVERY_RUNNING,
5545 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005546 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
NeilBrownc8c00a62009-08-10 12:50:52 +10005548 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005549 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005550 mddev->sysfs_active ||
5551 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005552 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005553 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005554 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005555 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005556 if (did_freeze) {
5557 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005558 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005559 md_wakeup_thread(mddev->thread);
5560 }
NeilBrown260fa032013-08-27 16:44:13 +10005561 return -EBUSY;
5562 }
NeilBrown6e17b022010-08-07 21:41:19 +10005563 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005564 if (mddev->ro)
5565 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005566
NeilBrowndefad612011-01-14 09:14:33 +11005567 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005568 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005569 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005570
NeilBrowna4bd82d2010-03-29 13:23:10 +11005571 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005572 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005573
NeilBrowndafb20f2012-03-19 12:46:39 +11005574 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005575 if (rdev->raid_disk >= 0)
5576 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005577
NeilBrowna4bd82d2010-03-29 13:23:10 +11005578 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005579 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005580 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005581 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005582
NeilBrowna4bd82d2010-03-29 13:23:10 +11005583 if (mddev->ro)
5584 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005585 } else
5586 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 /*
5588 * Free resources if final stop
5589 */
NeilBrown9e653b62006-06-26 00:27:58 -07005590 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5592
NeilBrown978f9462006-02-02 14:28:05 -08005593 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005594 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005595 struct file *f = mddev->bitmap_info.file;
5596 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005597 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005598 spin_unlock(&mddev->lock);
5599 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005600 }
NeilBrownc3d97142009-12-14 12:49:52 +11005601 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 export_array(mddev);
5604
NeilBrown6177b472010-03-29 11:37:13 +11005605 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005606 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005607 if (mddev->hold_active == UNTIL_STOP)
5608 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005609 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005610 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005611 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005612 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614}
5615
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005616#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005617static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618{
NeilBrown3cb03002011-10-11 16:45:26 +11005619 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 int err;
5621
NeilBrowna757e642005-04-16 15:26:42 -07005622 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
5625 printk(KERN_INFO "md: running: ");
5626
NeilBrowndafb20f2012-03-19 12:46:39 +11005627 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 char b[BDEVNAME_SIZE];
5629 printk("<%s>", bdevname(rdev->bdev,b));
5630 }
5631 printk("\n");
5632
NeilBrownd710e132008-10-13 11:55:12 +11005633 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 if (err) {
5635 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005636 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 }
5638}
5639
5640/*
5641 * lets try to run arrays based on all disks that have arrived
5642 * until now. (those are in pending_raid_disks)
5643 *
5644 * the method: pick the first pending disk, collect all disks with
5645 * the same UUID, remove all from the pending list and put them into
5646 * the 'same_array' list. Then order this list based on superblock
5647 * update time (freshest comes first), kick out 'old' disks and
5648 * compare superblocks. If everything's fine then run it.
5649 *
5650 * If "unit" is allocated, then bump its reference count
5651 */
5652static void autorun_devices(int part)
5653{
NeilBrown3cb03002011-10-11 16:45:26 +11005654 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005655 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 char b[BDEVNAME_SIZE];
5657
5658 printk(KERN_INFO "md: autorun ...\n");
5659 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005660 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005662 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005664 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
5666 printk(KERN_INFO "md: considering %s ...\n",
5667 bdevname(rdev0->bdev,b));
5668 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005669 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 if (super_90_load(rdev, rdev0, 0) >= 0) {
5671 printk(KERN_INFO "md: adding %s ...\n",
5672 bdevname(rdev->bdev,b));
5673 list_move(&rdev->same_set, &candidates);
5674 }
5675 /*
5676 * now we have a set of devices, with all of them having
5677 * mostly sane superblocks. It's time to allocate the
5678 * mddev.
5679 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005680 if (part) {
5681 dev = MKDEV(mdp_major,
5682 rdev0->preferred_minor << MdpMinorShift);
5683 unit = MINOR(dev) >> MdpMinorShift;
5684 } else {
5685 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5686 unit = MINOR(dev);
5687 }
5688 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5690 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5691 break;
5692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
5694 md_probe(dev, NULL, NULL);
5695 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005696 if (!mddev || !mddev->gendisk) {
5697 if (mddev)
5698 mddev_put(mddev);
5699 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 "md: cannot allocate memory for md drive.\n");
5701 break;
5702 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005703 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 printk(KERN_WARNING "md: %s locked, cannot run\n",
5705 mdname(mddev));
5706 else if (mddev->raid_disks || mddev->major_version
5707 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005708 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 "md: %s already running, cannot run %s\n",
5710 mdname(mddev), bdevname(rdev0->bdev,b));
5711 mddev_unlock(mddev);
5712 } else {
5713 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005714 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005715 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 list_del_init(&rdev->same_set);
5717 if (bind_rdev_to_array(rdev, mddev))
5718 export_rdev(rdev);
5719 }
5720 autorun_array(mddev);
5721 mddev_unlock(mddev);
5722 }
5723 /* on success, candidates will be empty, on error
5724 * it won't...
5725 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005726 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005727 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 mddev_put(mddev);
5731 }
5732 printk(KERN_INFO "md: ... autorun DONE.\n");
5733}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005734#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
NeilBrownf72ffdd2014-09-30 14:23:59 +10005736static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737{
5738 mdu_version_t ver;
5739
5740 ver.major = MD_MAJOR_VERSION;
5741 ver.minor = MD_MINOR_VERSION;
5742 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5743
5744 if (copy_to_user(arg, &ver, sizeof(ver)))
5745 return -EFAULT;
5746
5747 return 0;
5748}
5749
NeilBrownf72ffdd2014-09-30 14:23:59 +10005750static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751{
5752 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005753 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005754 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
NeilBrown1ca69c42012-10-11 13:37:33 +11005756 nr = working = insync = failed = spare = 0;
5757 rcu_read_lock();
5758 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005760 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 failed++;
5762 else {
5763 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005764 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005765 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 else
5767 spare++;
5768 }
5769 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005770 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771
5772 info.major_version = mddev->major_version;
5773 info.minor_version = mddev->minor_version;
5774 info.patch_version = MD_PATCHLEVEL_VERSION;
5775 info.ctime = mddev->ctime;
5776 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005777 info.size = mddev->dev_sectors / 2;
5778 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005779 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 info.nr_disks = nr;
5781 info.raid_disks = mddev->raid_disks;
5782 info.md_minor = mddev->md_minor;
5783 info.not_persistent= !mddev->persistent;
5784
5785 info.utime = mddev->utime;
5786 info.state = 0;
5787 if (mddev->in_sync)
5788 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005789 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005790 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005791 if (mddev_is_clustered(mddev))
5792 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005793 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 info.working_disks = working;
5795 info.failed_disks = failed;
5796 info.spare_disks = spare;
5797
5798 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005799 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
5801 if (copy_to_user(arg, &info, sizeof(info)))
5802 return -EFAULT;
5803
5804 return 0;
5805}
5806
NeilBrownf72ffdd2014-09-30 14:23:59 +10005807static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005808{
5809 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005810 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005811 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005812
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005813 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005814 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005815 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005816
NeilBrown32a76272005-06-21 17:17:14 -07005817 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005818 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005819 /* bitmap enabled */
5820 if (mddev->bitmap_info.file) {
5821 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5822 sizeof(file->pathname));
5823 if (IS_ERR(ptr))
5824 err = PTR_ERR(ptr);
5825 else
5826 memmove(file->pathname, ptr,
5827 sizeof(file->pathname)-(ptr-file->pathname));
5828 }
NeilBrown4af1a042014-12-15 12:57:00 +11005829 spin_unlock(&mddev->lock);
5830
5831 if (err == 0 &&
5832 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005833 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005834
NeilBrown32a76272005-06-21 17:17:14 -07005835 kfree(file);
5836 return err;
5837}
5838
NeilBrownf72ffdd2014-09-30 14:23:59 +10005839static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840{
5841 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005842 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
5844 if (copy_from_user(&info, arg, sizeof(info)))
5845 return -EFAULT;
5846
NeilBrown1ca69c42012-10-11 13:37:33 +11005847 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005848 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 if (rdev) {
5850 info.major = MAJOR(rdev->bdev->bd_dev);
5851 info.minor = MINOR(rdev->bdev->bd_dev);
5852 info.raid_disk = rdev->raid_disk;
5853 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005854 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005856 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 info.state |= (1<<MD_DISK_ACTIVE);
5858 info.state |= (1<<MD_DISK_SYNC);
Song Liubac624f2015-08-13 14:31:55 -07005859 } else if (test_bit(Journal, &rdev->flags))
5860 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005861 if (test_bit(WriteMostly, &rdev->flags))
5862 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 } else {
5864 info.major = info.minor = 0;
5865 info.raid_disk = -1;
5866 info.state = (1<<MD_DISK_REMOVED);
5867 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005868 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
5870 if (copy_to_user(arg, &info, sizeof(info)))
5871 return -EFAULT;
5872
5873 return 0;
5874}
5875
NeilBrownf72ffdd2014-09-30 14:23:59 +10005876static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877{
5878 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005879 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 dev_t dev = MKDEV(info->major,info->minor);
5881
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005882 if (mddev_is_clustered(mddev) &&
5883 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005884 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005885 mdname(mddev));
5886 return -EINVAL;
5887 }
5888
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5890 return -EOVERFLOW;
5891
5892 if (!mddev->raid_disks) {
5893 int err;
5894 /* expecting a device which has a superblock */
5895 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5896 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005897 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 "md: md_import_device returned %ld\n",
5899 PTR_ERR(rdev));
5900 return PTR_ERR(rdev);
5901 }
5902 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005903 struct md_rdev *rdev0
5904 = list_entry(mddev->disks.next,
5905 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005906 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 .load_super(rdev, rdev0, mddev->minor_version);
5908 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005909 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005911 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 bdevname(rdev0->bdev,b2));
5913 export_rdev(rdev);
5914 return -EINVAL;
5915 }
5916 }
5917 err = bind_rdev_to_array(rdev, mddev);
5918 if (err)
5919 export_rdev(rdev);
5920 return err;
5921 }
5922
5923 /*
5924 * add_new_disk can be used once the array is assembled
5925 * to add "hot spares". They must already have a superblock
5926 * written
5927 */
5928 if (mddev->pers) {
5929 int err;
5930 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005931 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 "%s: personality does not support diskops!\n",
5933 mdname(mddev));
5934 return -EINVAL;
5935 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005936 if (mddev->persistent)
5937 rdev = md_import_device(dev, mddev->major_version,
5938 mddev->minor_version);
5939 else
5940 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005942 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 "md: md_import_device returned %ld\n",
5944 PTR_ERR(rdev));
5945 return PTR_ERR(rdev);
5946 }
NeilBrown1a855a02010-12-09 16:36:28 +11005947 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005948 if (!mddev->persistent) {
5949 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005950 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005951 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005952 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005953 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005954 } else
NeilBrown41158c72005-06-21 17:17:25 -07005955 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005956 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005957 } else
5958 super_types[mddev->major_version].
5959 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005960 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005961 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005962 /* This was a hot-add request, but events doesn't
5963 * match, so reject it.
5964 */
5965 export_rdev(rdev);
5966 return -EINVAL;
5967 }
5968
NeilBrownb2d444d2005-11-08 21:39:31 -08005969 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005970 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5971 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005972 else
5973 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005974
Song Liubac624f2015-08-13 14:31:55 -07005975 if (info->state & (1<<MD_DISK_JOURNAL))
5976 set_bit(Journal, &rdev->flags);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005977 /*
5978 * check whether the device shows up in other nodes
5979 */
5980 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05005981 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005982 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05005983 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005984 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05005985 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005986 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005987 export_rdev(rdev);
5988 return err;
5989 }
5990 }
5991 }
5992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 rdev->raid_disk = -1;
5994 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05005995
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 if (err)
5997 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05005998
5999 if (mddev_is_clustered(mddev)) {
6000 if (info->state & (1 << MD_DISK_CANDIDATE))
6001 md_cluster_ops->new_disk_ack(mddev, (err == 0));
6002 else {
6003 if (err)
6004 md_cluster_ops->add_new_disk_cancel(mddev);
6005 else
6006 err = add_bound_rdev(rdev);
6007 }
6008
6009 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006010 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006011
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 return err;
6013 }
6014
6015 /* otherwise, add_new_disk is only allowed
6016 * for major_version==0 superblocks
6017 */
6018 if (mddev->major_version != 0) {
6019 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
6020 mdname(mddev));
6021 return -EINVAL;
6022 }
6023
6024 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6025 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006026 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006028 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 "md: error, md_import_device() returned %ld\n",
6030 PTR_ERR(rdev));
6031 return PTR_ERR(rdev);
6032 }
6033 rdev->desc_nr = info->number;
6034 if (info->raid_disk < mddev->raid_disks)
6035 rdev->raid_disk = info->raid_disk;
6036 else
6037 rdev->raid_disk = -1;
6038
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006040 if (info->state & (1<<MD_DISK_SYNC))
6041 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006043 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6044 set_bit(WriteMostly, &rdev->flags);
6045
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 if (!mddev->persistent) {
6047 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006048 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6049 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006050 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006051 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
NeilBrown2bf071b2006-01-06 00:20:55 -08006053 err = bind_rdev_to_array(rdev, mddev);
6054 if (err) {
6055 export_rdev(rdev);
6056 return err;
6057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 }
6059
6060 return 0;
6061}
6062
NeilBrownf72ffdd2014-09-30 14:23:59 +10006063static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064{
6065 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006066 struct md_rdev *rdev;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006067 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 rdev = find_rdev(mddev, dev);
6070 if (!rdev)
6071 return -ENXIO;
6072
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006073 if (mddev_is_clustered(mddev))
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006074 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006075
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006076 if (rdev->raid_disk < 0)
6077 goto kick_rdev;
6078
NeilBrown3ea8929d2013-04-24 11:42:41 +10006079 clear_bit(Blocked, &rdev->flags);
6080 remove_and_add_spares(mddev, rdev);
6081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 if (rdev->raid_disk >= 0)
6083 goto busy;
6084
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006085kick_rdev:
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006086 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006087 md_cluster_ops->remove_disk(mddev, rdev);
6088
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006089 md_kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07006090 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006091 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
6093 return 0;
6094busy:
Guoqing Jiang23b63f92015-10-12 17:21:30 +08006095 if (mddev_is_clustered(mddev) && ret == 0)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006096 md_cluster_ops->metadata_update_cancel(mddev);
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006097
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006098 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 bdevname(rdev->bdev,b), mdname(mddev));
6100 return -EBUSY;
6101}
6102
NeilBrownf72ffdd2014-09-30 14:23:59 +10006103static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104{
6105 char b[BDEVNAME_SIZE];
6106 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006107 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
6109 if (!mddev->pers)
6110 return -ENODEV;
6111
6112 if (mddev->major_version != 0) {
6113 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6114 " version-0 superblocks.\n",
6115 mdname(mddev));
6116 return -EINVAL;
6117 }
6118 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006119 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 "%s: personality does not support diskops!\n",
6121 mdname(mddev));
6122 return -EINVAL;
6123 }
6124
NeilBrownd710e132008-10-13 11:55:12 +11006125 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006127 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 "md: error, md_import_device() returned %ld\n",
6129 PTR_ERR(rdev));
6130 return -EINVAL;
6131 }
6132
6133 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006134 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006136 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137
NeilBrown8190e752009-06-18 08:48:58 +10006138 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
NeilBrownb2d444d2005-11-08 21:39:31 -08006140 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006141 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 "md: can not hot-add faulty %s disk to %s!\n",
6143 bdevname(rdev->bdev,b), mdname(mddev));
6144 err = -EINVAL;
6145 goto abort_export;
6146 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006147
NeilBrownb2d444d2005-11-08 21:39:31 -08006148 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006150 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006151 err = bind_rdev_to_array(rdev, mddev);
6152 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006153 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
6155 /*
6156 * The rest should better be atomic, we can have disk failures
6157 * noticed in interrupt contexts ...
6158 */
6159
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 rdev->raid_disk = -1;
6161
NeilBrown850b2b422006-10-03 01:15:46 -07006162 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 /*
6164 * Kick recovery, maybe this spare has to be added to the
6165 * array immediately.
6166 */
6167 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6168 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006169 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 return 0;
6171
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172abort_export:
6173 export_rdev(rdev);
6174 return err;
6175}
6176
NeilBrownfd01b882011-10-11 16:47:53 +11006177static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006178{
NeilBrown035328c2014-04-09 12:25:40 +10006179 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006180
NeilBrown36fa3062005-09-09 16:23:45 -07006181 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006182 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006183 return -EBUSY;
6184 if (mddev->recovery || mddev->sync_thread)
6185 return -EBUSY;
6186 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006187 }
6188
NeilBrown36fa3062005-09-09 16:23:45 -07006189 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006190 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006191 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006192
NeilBrown1e594bb2014-12-15 12:57:00 +11006193 if (mddev->bitmap || mddev->bitmap_info.file)
6194 return -EEXIST; /* cannot add when bitmap is present */
6195 f = fget(fd);
6196
6197 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006198 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6199 mdname(mddev));
6200 return -EBADF;
6201 }
6202
NeilBrown1e594bb2014-12-15 12:57:00 +11006203 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006204 if (!S_ISREG(inode->i_mode)) {
6205 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6206 mdname(mddev));
6207 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006208 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006209 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6210 mdname(mddev));
6211 err = -EBADF;
6212 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006213 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6214 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006215 err = -EBUSY;
6216 }
6217 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006218 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006219 return err;
6220 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006221 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006222 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006223 } else if (mddev->bitmap == NULL)
6224 return -ENOENT; /* cannot remove what isn't there */
6225 err = 0;
6226 if (mddev->pers) {
6227 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006228 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006229 struct bitmap *bitmap;
6230
6231 bitmap = bitmap_create(mddev, -1);
6232 if (!IS_ERR(bitmap)) {
6233 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006234 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006235 } else
6236 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006237 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006238 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006239 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006240 fd = -1; /* make sure to put the file */
6241 }
NeilBrown36fa3062005-09-09 16:23:45 -07006242 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006243 }
6244 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006245 struct file *f = mddev->bitmap_info.file;
6246 if (f) {
6247 spin_lock(&mddev->lock);
6248 mddev->bitmap_info.file = NULL;
6249 spin_unlock(&mddev->lock);
6250 fput(f);
6251 }
NeilBrown36fa3062005-09-09 16:23:45 -07006252 }
6253
NeilBrown32a76272005-06-21 17:17:14 -07006254 return err;
6255}
6256
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257/*
6258 * set_array_info is used two different ways
6259 * The original usage is when creating a new array.
6260 * In this usage, raid_disks is > 0 and it together with
6261 * level, size, not_persistent,layout,chunksize determine the
6262 * shape of the array.
6263 * This will always create an array with a type-0.90.0 superblock.
6264 * The newer usage is when assembling an array.
6265 * In this case raid_disks will be 0, and the major_version field is
6266 * use to determine which style super-blocks are to be found on the devices.
6267 * The minor and patch _version numbers are also kept incase the
6268 * super_block handler wishes to interpret them.
6269 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006270static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271{
6272
6273 if (info->raid_disks == 0) {
6274 /* just setting version number for superblock loading */
6275 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006276 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 super_types[info->major_version].name == NULL) {
6278 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006279 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 "md: superblock version %d not known\n",
6281 info->major_version);
6282 return -EINVAL;
6283 }
6284 mddev->major_version = info->major_version;
6285 mddev->minor_version = info->minor_version;
6286 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006287 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006288 /* ensure mddev_put doesn't delete this now that there
6289 * is some minimal configuration.
6290 */
6291 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 return 0;
6293 }
6294 mddev->major_version = MD_MAJOR_VERSION;
6295 mddev->minor_version = MD_MINOR_VERSION;
6296 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6297 mddev->ctime = get_seconds();
6298
6299 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006300 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006301 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 mddev->raid_disks = info->raid_disks;
6303 /* don't set md_minor, it is determined by which /dev/md* was
6304 * openned
6305 */
6306 if (info->state & (1<<MD_SB_CLEAN))
6307 mddev->recovery_cp = MaxSector;
6308 else
6309 mddev->recovery_cp = 0;
6310 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006311 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
6313 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006314 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
6316 mddev->max_disks = MD_SB_DISKS;
6317
NeilBrowne6910632008-02-06 01:39:51 -08006318 if (mddev->persistent)
6319 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006320 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
NeilBrownc3d97142009-12-14 12:49:52 +11006322 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006323 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006324 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006325
NeilBrownf6705572006-03-27 01:18:11 -08006326 mddev->reshape_position = MaxSector;
6327
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 /*
6329 * Generate a 128 bit UUID
6330 */
6331 get_random_bytes(mddev->uuid, 16);
6332
NeilBrownf6705572006-03-27 01:18:11 -08006333 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006334 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006335 mddev->new_layout = mddev->layout;
6336 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006337 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006338
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 return 0;
6340}
6341
NeilBrownfd01b882011-10-11 16:47:53 +11006342void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006343{
Dan Williamsb522adc2009-03-31 15:00:31 +11006344 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6345
6346 if (mddev->external_size)
6347 return;
6348
Dan Williams1f403622009-03-31 14:59:03 +11006349 mddev->array_sectors = array_sectors;
6350}
6351EXPORT_SYMBOL(md_set_array_sectors);
6352
NeilBrownfd01b882011-10-11 16:47:53 +11006353static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006354{
NeilBrown3cb03002011-10-11 16:45:26 +11006355 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006356 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006357 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006358
6359 if (mddev->pers->resize == NULL)
6360 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006361 /* The "num_sectors" is the number of sectors of each device that
6362 * is used. This can only make sense for arrays with redundancy.
6363 * linear and raid0 always use whatever space is available. We can only
6364 * consider changing this number if no resync or reconstruction is
6365 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006366 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006367 * of each device. If num_sectors is zero, we find the largest size
6368 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006369 */
NeilBrownf851b602014-12-11 10:02:10 +11006370 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6371 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006372 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006373 if (mddev->ro)
6374 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006375
NeilBrowndafb20f2012-03-19 12:46:39 +11006376 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006377 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006378
Andre Nolld71f9f82008-07-11 22:02:22 +10006379 if (fit && (num_sectors == 0 || num_sectors > avail))
6380 num_sectors = avail;
6381 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006382 return -ENOSPC;
6383 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006384 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006385 if (!rv)
6386 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006387 return rv;
6388}
6389
NeilBrownfd01b882011-10-11 16:47:53 +11006390static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006391{
6392 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006393 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006394 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006395 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006396 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006397 if (mddev->ro)
6398 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006399 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006400 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006401 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006402 if (mddev->sync_thread ||
6403 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6404 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006405 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006406
6407 rdev_for_each(rdev, mddev) {
6408 if (mddev->raid_disks < raid_disks &&
6409 rdev->data_offset < rdev->new_data_offset)
6410 return -EINVAL;
6411 if (mddev->raid_disks > raid_disks &&
6412 rdev->data_offset > rdev->new_data_offset)
6413 return -EINVAL;
6414 }
6415
NeilBrown63c70c42006-03-27 01:18:13 -08006416 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006417 if (mddev->delta_disks < 0)
6418 mddev->reshape_backwards = 1;
6419 else if (mddev->delta_disks > 0)
6420 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006421
6422 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006423 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006424 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006425 mddev->reshape_backwards = 0;
6426 }
NeilBrownda943b992006-01-06 00:20:54 -08006427 return rv;
6428}
6429
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430/*
6431 * update_array_info is used to change the configuration of an
6432 * on-line array.
6433 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6434 * fields in the info are checked against the array.
6435 * Any differences that cannot be handled will cause an error.
6436 * Normally, only one change can be managed at a time.
6437 */
NeilBrownfd01b882011-10-11 16:47:53 +11006438static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439{
6440 int rv = 0;
6441 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006442 int state = 0;
6443
6444 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006445 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006446 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
6448 if (mddev->major_version != info->major_version ||
6449 mddev->minor_version != info->minor_version ||
6450/* mddev->patch_version != info->patch_version || */
6451 mddev->ctime != info->ctime ||
6452 mddev->level != info->level ||
6453/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006454 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006455 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006456 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6457 ((state^info->state) & 0xfffffe00)
6458 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 return -EINVAL;
6460 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006461 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6462 cnt++;
6463 if (mddev->raid_disks != info->raid_disks)
6464 cnt++;
6465 if (mddev->layout != info->layout)
6466 cnt++;
6467 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6468 cnt++;
6469 if (cnt == 0)
6470 return 0;
6471 if (cnt > 1)
6472 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473
6474 if (mddev->layout != info->layout) {
6475 /* Change layout
6476 * we don't need to do anything at the md level, the
6477 * personality will take care of it all.
6478 */
NeilBrown50ac1682009-06-18 08:47:55 +10006479 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006481 else {
6482 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006483 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006484 if (rv)
6485 mddev->new_layout = mddev->layout;
6486 return rv;
6487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006489 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006490 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491
NeilBrownda943b992006-01-06 00:20:54 -08006492 if (mddev->raid_disks != info->raid_disks)
6493 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
NeilBrown36fa3062005-09-09 16:23:45 -07006495 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006496 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6497 rv = -EINVAL;
6498 goto err;
6499 }
6500 if (mddev->recovery || mddev->sync_thread) {
6501 rv = -EBUSY;
6502 goto err;
6503 }
NeilBrown36fa3062005-09-09 16:23:45 -07006504 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006505 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006506 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006507 if (mddev->bitmap) {
6508 rv = -EEXIST;
6509 goto err;
6510 }
6511 if (mddev->bitmap_info.default_offset == 0) {
6512 rv = -EINVAL;
6513 goto err;
6514 }
NeilBrownc3d97142009-12-14 12:49:52 +11006515 mddev->bitmap_info.offset =
6516 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006517 mddev->bitmap_info.space =
6518 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006519 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006520 bitmap = bitmap_create(mddev, -1);
6521 if (!IS_ERR(bitmap)) {
6522 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006523 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006524 } else
6525 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006526 if (rv)
6527 bitmap_destroy(mddev);
6528 mddev->pers->quiesce(mddev, 0);
6529 } else {
6530 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006531 if (!mddev->bitmap) {
6532 rv = -ENOENT;
6533 goto err;
6534 }
6535 if (mddev->bitmap->storage.file) {
6536 rv = -EINVAL;
6537 goto err;
6538 }
NeilBrown36fa3062005-09-09 16:23:45 -07006539 mddev->pers->quiesce(mddev, 1);
6540 bitmap_destroy(mddev);
6541 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006542 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006543 }
6544 }
NeilBrown850b2b422006-10-03 01:15:46 -07006545 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006546 return rv;
6547err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 return rv;
6549}
6550
NeilBrownfd01b882011-10-11 16:47:53 +11006551static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552{
NeilBrown3cb03002011-10-11 16:45:26 +11006553 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006554 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555
6556 if (mddev->pers == NULL)
6557 return -ENODEV;
6558
NeilBrown1ca69c42012-10-11 13:37:33 +11006559 rcu_read_lock();
6560 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006562 err = -ENODEV;
6563 else {
6564 md_error(mddev, rdev);
6565 if (!test_bit(Faulty, &rdev->flags))
6566 err = -EBUSY;
6567 }
6568 rcu_read_unlock();
6569 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570}
6571
Andre Noll2f9618c2008-04-25 18:57:58 +02006572/*
6573 * We have a problem here : there is no easy way to give a CHS
6574 * virtual geometry. We currently pretend that we have a 2 heads
6575 * 4 sectors (with a BIG number of cylinders...). This drives
6576 * dosfs just mad... ;-)
6577 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006578static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6579{
NeilBrownfd01b882011-10-11 16:47:53 +11006580 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006581
6582 geo->heads = 2;
6583 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006584 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006585 return 0;
6586}
6587
Nicolas Schichancb335f82014-01-15 16:58:52 +01006588static inline bool md_ioctl_valid(unsigned int cmd)
6589{
6590 switch (cmd) {
6591 case ADD_NEW_DISK:
6592 case BLKROSET:
6593 case GET_ARRAY_INFO:
6594 case GET_BITMAP_FILE:
6595 case GET_DISK_INFO:
6596 case HOT_ADD_DISK:
6597 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006598 case RAID_AUTORUN:
6599 case RAID_VERSION:
6600 case RESTART_ARRAY_RW:
6601 case RUN_ARRAY:
6602 case SET_ARRAY_INFO:
6603 case SET_BITMAP_FILE:
6604 case SET_DISK_FAULTY:
6605 case STOP_ARRAY:
6606 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006607 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006608 return true;
6609 default:
6610 return false;
6611 }
6612}
6613
Al Viroa39907f2008-03-02 10:31:15 -05006614static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 unsigned int cmd, unsigned long arg)
6616{
6617 int err = 0;
6618 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006619 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006620 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
Nicolas Schichancb335f82014-01-15 16:58:52 +01006622 if (!md_ioctl_valid(cmd))
6623 return -ENOTTY;
6624
NeilBrown506c9e42011-12-23 10:17:26 +11006625 switch (cmd) {
6626 case RAID_VERSION:
6627 case GET_ARRAY_INFO:
6628 case GET_DISK_INFO:
6629 break;
6630 default:
6631 if (!capable(CAP_SYS_ADMIN))
6632 return -EACCES;
6633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634
6635 /*
6636 * Commands dealing with the RAID driver but not any
6637 * particular array:
6638 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006639 switch (cmd) {
6640 case RAID_VERSION:
6641 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006642 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006645 case RAID_AUTORUN:
6646 err = 0;
6647 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006650 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 }
6652
6653 /*
6654 * Commands creating/starting a new array:
6655 */
6656
Al Viroa39907f2008-03-02 10:31:15 -05006657 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658
6659 if (!mddev) {
6660 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006661 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 }
6663
NeilBrown1ca69c42012-10-11 13:37:33 +11006664 /* Some actions do not requires the mutex */
6665 switch (cmd) {
6666 case GET_ARRAY_INFO:
6667 if (!mddev->raid_disks && !mddev->external)
6668 err = -ENODEV;
6669 else
6670 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006671 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006672
6673 case GET_DISK_INFO:
6674 if (!mddev->raid_disks && !mddev->external)
6675 err = -ENODEV;
6676 else
6677 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006678 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006679
6680 case SET_DISK_FAULTY:
6681 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006682 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006683
6684 case GET_BITMAP_FILE:
6685 err = get_bitmap_file(mddev, argp);
6686 goto out;
6687
NeilBrown1ca69c42012-10-11 13:37:33 +11006688 }
6689
NeilBrowna7a3f082012-12-11 13:35:54 +11006690 if (cmd == ADD_NEW_DISK)
6691 /* need to ensure md_delayed_delete() has completed */
6692 flush_workqueue(md_misc_wq);
6693
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006694 if (cmd == HOT_REMOVE_DISK)
6695 /* need to ensure recovery thread has run */
6696 wait_event_interruptible_timeout(mddev->sb_wait,
6697 !test_bit(MD_RECOVERY_NEEDED,
6698 &mddev->flags),
6699 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006700 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6701 /* Need to flush page cache, and ensure no-one else opens
6702 * and writes
6703 */
6704 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006705 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006706 mutex_unlock(&mddev->open_mutex);
6707 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006708 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006709 }
6710 set_bit(MD_STILL_CLOSED, &mddev->flags);
6711 mutex_unlock(&mddev->open_mutex);
6712 sync_blockdev(bdev);
6713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 err = mddev_lock(mddev);
6715 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006716 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6718 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006719 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 }
6721
NeilBrownc02c0ae2012-12-11 13:39:21 +11006722 if (cmd == SET_ARRAY_INFO) {
6723 mdu_array_info_t info;
6724 if (!arg)
6725 memset(&info, 0, sizeof(info));
6726 else if (copy_from_user(&info, argp, sizeof(info))) {
6727 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006728 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006729 }
6730 if (mddev->pers) {
6731 err = update_array_info(mddev, &info);
6732 if (err) {
6733 printk(KERN_WARNING "md: couldn't update"
6734 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006735 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006737 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006738 }
6739 if (!list_empty(&mddev->disks)) {
6740 printk(KERN_WARNING
6741 "md: array %s already has disks!\n",
6742 mdname(mddev));
6743 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006744 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006745 }
6746 if (mddev->raid_disks) {
6747 printk(KERN_WARNING
6748 "md: array %s already initialised!\n",
6749 mdname(mddev));
6750 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006751 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006752 }
6753 err = set_array_info(mddev, &info);
6754 if (err) {
6755 printk(KERN_WARNING "md: couldn't set"
6756 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006757 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006758 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006759 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 }
6761
6762 /*
6763 * Commands querying/configuring an existing array:
6764 */
NeilBrown32a76272005-06-21 17:17:14 -07006765 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006766 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006767 if ((!mddev->raid_disks && !mddev->external)
6768 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6769 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6770 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006772 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 }
6774
6775 /*
6776 * Commands even a read-only array can execute:
6777 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006778 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006779 case RESTART_ARRAY_RW:
6780 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006781 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006782
6783 case STOP_ARRAY:
6784 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006785 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006786
6787 case STOP_ARRAY_RO:
6788 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006789 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006790
NeilBrown3ea8929d2013-04-24 11:42:41 +10006791 case HOT_REMOVE_DISK:
6792 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006793 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006794
NeilBrown7ceb17e2013-04-24 11:42:42 +10006795 case ADD_NEW_DISK:
6796 /* We can support ADD_NEW_DISK on read-only arrays
6797 * on if we are re-adding a preexisting device.
6798 * So require mddev->pers and MD_DISK_SYNC.
6799 */
6800 if (mddev->pers) {
6801 mdu_disk_info_t info;
6802 if (copy_from_user(&info, argp, sizeof(info)))
6803 err = -EFAULT;
6804 else if (!(info.state & (1<<MD_DISK_SYNC)))
6805 /* Need to clear read-only for this */
6806 break;
6807 else
6808 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006809 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006810 }
6811 break;
6812
NeilBrownc02c0ae2012-12-11 13:39:21 +11006813 case BLKROSET:
6814 if (get_user(ro, (int __user *)(arg))) {
6815 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006816 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006817 }
6818 err = -EINVAL;
6819
6820 /* if the bdev is going readonly the value of mddev->ro
6821 * does not matter, no writes are coming
6822 */
6823 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006824 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006825
NeilBrownc02c0ae2012-12-11 13:39:21 +11006826 /* are we are already prepared for writes? */
6827 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006828 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006829
6830 /* transitioning to readauto need only happen for
6831 * arrays that call md_write_start
6832 */
6833 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006835 if (err == 0) {
6836 mddev->ro = 2;
6837 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006838 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006839 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006840 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 }
6842
6843 /*
6844 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006845 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 */
NeilBrown326eb172014-09-30 15:36:28 +10006847 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006848 if (mddev->ro == 2) {
6849 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006850 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006851 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006852 /* mddev_unlock will wake thread */
6853 /* If a device failed while we were read-only, we
6854 * need to make sure the metadata is updated now.
6855 */
6856 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6857 mddev_unlock(mddev);
6858 wait_event(mddev->sb_wait,
6859 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6860 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006861 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006862 }
NeilBrownf91de922005-11-08 21:39:36 -08006863 } else {
6864 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006865 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 }
6868
NeilBrownc02c0ae2012-12-11 13:39:21 +11006869 switch (cmd) {
6870 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006872 mdu_disk_info_t info;
6873 if (copy_from_user(&info, argp, sizeof(info)))
6874 err = -EFAULT;
6875 else
6876 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006877 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006880 case CLUSTERED_DISK_NACK:
6881 if (mddev_is_clustered(mddev))
6882 md_cluster_ops->new_disk_ack(mddev, false);
6883 else
6884 err = -EINVAL;
6885 goto unlock;
6886
NeilBrownc02c0ae2012-12-11 13:39:21 +11006887 case HOT_ADD_DISK:
6888 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006889 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890
NeilBrownc02c0ae2012-12-11 13:39:21 +11006891 case RUN_ARRAY:
6892 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006893 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894
NeilBrownc02c0ae2012-12-11 13:39:21 +11006895 case SET_BITMAP_FILE:
6896 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006897 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006898
NeilBrownc02c0ae2012-12-11 13:39:21 +11006899 default:
6900 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006901 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 }
6903
NeilBrown3adc28d2014-09-30 15:46:41 +10006904unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006905 if (mddev->hold_active == UNTIL_IOCTL &&
6906 err != -EINVAL)
6907 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006909out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 return err;
6911}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006912#ifdef CONFIG_COMPAT
6913static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6914 unsigned int cmd, unsigned long arg)
6915{
6916 switch (cmd) {
6917 case HOT_REMOVE_DISK:
6918 case HOT_ADD_DISK:
6919 case SET_DISK_FAULTY:
6920 case SET_BITMAP_FILE:
6921 /* These take in integer arg, do not convert */
6922 break;
6923 default:
6924 arg = (unsigned long)compat_ptr(arg);
6925 break;
6926 }
6927
6928 return md_ioctl(bdev, mode, cmd, arg);
6929}
6930#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931
Al Viroa39907f2008-03-02 10:31:15 -05006932static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933{
6934 /*
6935 * Succeed if we can lock the mddev, which confirms that
6936 * it isn't being stopped right now.
6937 */
NeilBrownfd01b882011-10-11 16:47:53 +11006938 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 int err;
6940
Yuanhan Liu0c098222012-05-22 13:55:32 +10006941 if (!mddev)
6942 return -ENODEV;
6943
NeilBrownd3374822009-01-09 08:31:10 +11006944 if (mddev->gendisk != bdev->bd_disk) {
6945 /* we are racing with mddev_put which is discarding this
6946 * bd_disk.
6947 */
6948 mddev_put(mddev);
6949 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006950 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006951 /* Then retry the open from the top */
6952 return -ERESTARTSYS;
6953 }
6954 BUG_ON(mddev != bdev->bd_disk->private_data);
6955
NeilBrownc8c00a62009-08-10 12:50:52 +10006956 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 goto out;
6958
6959 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006960 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006961 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006962 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006964 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 out:
6966 return err;
6967}
6968
Al Virodb2a1442013-05-05 21:52:57 -04006969static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006971 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006973 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006974 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006977
6978static int md_media_changed(struct gendisk *disk)
6979{
NeilBrownfd01b882011-10-11 16:47:53 +11006980 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006981
6982 return mddev->changed;
6983}
6984
6985static int md_revalidate(struct gendisk *disk)
6986{
NeilBrownfd01b882011-10-11 16:47:53 +11006987 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006988
6989 mddev->changed = 0;
6990 return 0;
6991}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006992static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993{
6994 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006995 .open = md_open,
6996 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006997 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006998#ifdef CONFIG_COMPAT
6999 .compat_ioctl = md_compat_ioctl,
7000#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007001 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007002 .media_changed = md_media_changed,
7003 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004};
7005
NeilBrownf72ffdd2014-09-30 14:23:59 +10007006static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007{
NeilBrown2b8bf342011-10-11 16:48:23 +11007008 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 /*
7011 * md_thread is a 'system-thread', it's priority should be very
7012 * high. We avoid resource deadlocks individually in each
7013 * raid personality. (RAID5 does preallocation) We also use RR and
7014 * the very same RT priority as kswapd, thus we will never get
7015 * into a priority inversion deadlock.
7016 *
7017 * we definitely have to have equal or higher priority than
7018 * bdflush, otherwise bdflush will deadlock if there are too
7019 * many dirty RAID5 blocks.
7020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021
NeilBrown6985c432005-10-19 21:23:47 -07007022 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007023 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
NeilBrown93588e22005-11-15 00:09:12 -08007025 /* We need to wait INTERRUPTIBLE so that
7026 * we don't add to the load-average.
7027 * That means we need to be sure no signals are
7028 * pending
7029 */
7030 if (signal_pending(current))
7031 flush_signals(current);
7032
7033 wait_event_interruptible_timeout
7034 (thread->wqueue,
7035 test_bit(THREAD_WAKEUP, &thread->flags)
7036 || kthread_should_stop(),
7037 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038
NeilBrown6c987912011-01-14 09:13:53 +11007039 clear_bit(THREAD_WAKEUP, &thread->flags);
7040 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007041 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007043
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 return 0;
7045}
7046
NeilBrown2b8bf342011-10-11 16:48:23 +11007047void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048{
7049 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007050 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 set_bit(THREAD_WAKEUP, &thread->flags);
7052 wake_up(&thread->wqueue);
7053 }
7054}
NeilBrown6c144d32014-09-30 16:15:38 +10007055EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
Shaohua Li4ed87312012-10-11 13:34:00 +11007057struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7058 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059{
NeilBrown2b8bf342011-10-11 16:48:23 +11007060 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
NeilBrown2b8bf342011-10-11 16:48:23 +11007062 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 if (!thread)
7064 return NULL;
7065
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 init_waitqueue_head(&thread->wqueue);
7067
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 thread->run = run;
7069 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007070 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007071 thread->tsk = kthread_run(md_thread, thread,
7072 "%s_%s",
7073 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007074 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007075 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 kfree(thread);
7077 return NULL;
7078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079 return thread;
7080}
NeilBrown6c144d32014-09-30 16:15:38 +10007081EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082
NeilBrown2b8bf342011-10-11 16:48:23 +11007083void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084{
NeilBrown2b8bf342011-10-11 16:48:23 +11007085 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007086 if (!thread)
7087 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007088 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007089 /* Locking ensures that mddev_unlock does not wake_up a
7090 * non-existent thread
7091 */
7092 spin_lock(&pers_lock);
7093 *threadp = NULL;
7094 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007095
7096 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 kfree(thread);
7098}
NeilBrown6c144d32014-09-30 16:15:38 +10007099EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100
NeilBrownfd01b882011-10-11 16:47:53 +11007101void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102{
NeilBrownb2d444d2005-11-08 21:39:31 -08007103 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007105
NeilBrownde393cd2011-07-28 11:31:48 +10007106 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 return;
7108 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007109 if (mddev->degraded)
7110 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007111 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7113 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7114 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007115 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007116 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07007117 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118}
NeilBrown6c144d32014-09-30 16:15:38 +10007119EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120
7121/* seq_file implementation /proc/mdstat */
7122
7123static void status_unused(struct seq_file *seq)
7124{
7125 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007126 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127
7128 seq_printf(seq, "unused devices: ");
7129
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007130 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 char b[BDEVNAME_SIZE];
7132 i++;
7133 seq_printf(seq, "%s ",
7134 bdevname(rdev->bdev,b));
7135 }
7136 if (!i)
7137 seq_printf(seq, "<none>");
7138
7139 seq_printf(seq, "\n");
7140}
7141
NeilBrownf7851be2015-07-02 17:12:58 +10007142static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143{
NeilBrowndd71cf62009-05-07 12:49:35 +10007144 sector_t max_sectors, resync, res;
7145 unsigned long dt, db;
7146 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007147 int scale;
7148 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
NeilBrownc804cde2012-05-21 09:28:33 +10007150 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7151 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007152 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007154 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155
NeilBrownf7851be2015-07-02 17:12:58 +10007156 resync = mddev->curr_resync;
7157 if (resync <= 3) {
7158 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7159 /* Still cleaning up */
7160 resync = max_sectors;
7161 } else
7162 resync -= atomic_read(&mddev->recovery_active);
7163
7164 if (resync == 0) {
7165 if (mddev->recovery_cp < MaxSector) {
7166 seq_printf(seq, "\tresync=PENDING");
7167 return 1;
7168 }
7169 return 0;
7170 }
7171 if (resync < 3) {
7172 seq_printf(seq, "\tresync=DELAYED");
7173 return 1;
7174 }
7175
NeilBrown403df472014-09-30 15:52:29 +10007176 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007177 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007178 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007179 * u32, as those are the requirements for sector_div.
7180 * Thus 'scale' must be at least 10
7181 */
7182 scale = 10;
7183 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007184 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007185 scale++;
7186 }
7187 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007188 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007189
7190 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 {
NeilBrown4588b422006-03-27 01:18:04 -08007192 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 seq_printf(seq, "[");
7194 for (i = 0; i < x; i++)
7195 seq_printf(seq, "=");
7196 seq_printf(seq, ">");
7197 for (i = 0; i < y; i++)
7198 seq_printf(seq, ".");
7199 seq_printf(seq, "] ");
7200 }
NeilBrown4588b422006-03-27 01:18:04 -08007201 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007202 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7203 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007204 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7205 "check" :
7206 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7207 "resync" : "recovery"))),
7208 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007209 (unsigned long long) resync/2,
7210 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211
7212 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 * dt: time from mark until now
7214 * db: blocks written from mark until now
7215 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007216 *
7217 * rt is a sector_t, so could be 32bit or 64bit.
7218 * So we divide before multiply in case it is 32bit and close
7219 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007220 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007221 * near the end of resync when the number of remaining sectors
7222 * is close to 'db'.
7223 * We then divide rt by 32 after multiplying by db to compensate.
7224 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 */
7226 dt = ((jiffies - mddev->resync_mark) / HZ);
7227 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007228 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7229 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
NeilBrowndd71cf62009-05-07 12:49:35 +10007231 rt = max_sectors - resync; /* number of remaining sectors */
7232 sector_div(rt, db/32+1);
7233 rt *= dt;
7234 rt >>= 5;
7235
7236 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7237 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238
NeilBrownff4e8d92006-07-10 04:44:16 -07007239 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007240 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241}
7242
7243static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7244{
7245 struct list_head *tmp;
7246 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007247 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
7249 if (l >= 0x10000)
7250 return NULL;
7251 if (!l--)
7252 /* header */
7253 return (void*)1;
7254
7255 spin_lock(&all_mddevs_lock);
7256 list_for_each(tmp,&all_mddevs)
7257 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007258 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 mddev_get(mddev);
7260 spin_unlock(&all_mddevs_lock);
7261 return mddev;
7262 }
7263 spin_unlock(&all_mddevs_lock);
7264 if (!l--)
7265 return (void*)2;/* tail */
7266 return NULL;
7267}
7268
7269static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7270{
7271 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007272 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007273
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274 ++*pos;
7275 if (v == (void*)2)
7276 return NULL;
7277
7278 spin_lock(&all_mddevs_lock);
7279 if (v == (void*)1)
7280 tmp = all_mddevs.next;
7281 else
7282 tmp = mddev->all_mddevs.next;
7283 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007284 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 else {
7286 next_mddev = (void*)2;
7287 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 spin_unlock(&all_mddevs_lock);
7290
7291 if (v != (void*)1)
7292 mddev_put(mddev);
7293 return next_mddev;
7294
7295}
7296
7297static void md_seq_stop(struct seq_file *seq, void *v)
7298{
NeilBrownfd01b882011-10-11 16:47:53 +11007299 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300
7301 if (mddev && v != (void*)1 && v != (void*)2)
7302 mddev_put(mddev);
7303}
7304
7305static int md_seq_show(struct seq_file *seq, void *v)
7306{
NeilBrownfd01b882011-10-11 16:47:53 +11007307 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007308 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007309 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310
7311 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007312 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 seq_printf(seq, "Personalities : ");
7314 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007315 list_for_each_entry(pers, &pers_list, list)
7316 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317
7318 spin_unlock(&pers_lock);
7319 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007320 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321 return 0;
7322 }
7323 if (v == (void*)2) {
7324 status_unused(seq);
7325 return 0;
7326 }
7327
NeilBrown36d091f2014-12-15 12:56:58 +11007328 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7330 seq_printf(seq, "%s : %sactive", mdname(mddev),
7331 mddev->pers ? "" : "in");
7332 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007333 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007335 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007336 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 seq_printf(seq, " %s", mddev->pers->name);
7338 }
7339
Andre Nolldd8ac332009-03-31 14:33:13 +11007340 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007341 rcu_read_lock();
7342 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 char b[BDEVNAME_SIZE];
7344 seq_printf(seq, " %s[%d]",
7345 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007346 if (test_bit(WriteMostly, &rdev->flags))
7347 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007348 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 seq_printf(seq, "(F)");
7350 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007351 }
Song Liubac624f2015-08-13 14:31:55 -07007352 if (test_bit(Journal, &rdev->flags)) {
7353 seq_printf(seq, "(J)");
7354 continue;
7355 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11007356 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007357 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007358 if (test_bit(Replacement, &rdev->flags))
7359 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007360 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007362 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363
7364 if (!list_empty(&mddev->disks)) {
7365 if (mddev->pers)
7366 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007367 (unsigned long long)
7368 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 else
7370 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007371 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007373 if (mddev->persistent) {
7374 if (mddev->major_version != 0 ||
7375 mddev->minor_version != 90) {
7376 seq_printf(seq," super %d.%d",
7377 mddev->major_version,
7378 mddev->minor_version);
7379 }
NeilBrowne6910632008-02-06 01:39:51 -08007380 } else if (mddev->external)
7381 seq_printf(seq, " super external:%s",
7382 mddev->metadata_type);
7383 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007384 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385
7386 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007387 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007388 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007389 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007390 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007391 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007392 }
NeilBrown32a76272005-06-21 17:17:14 -07007393 } else
7394 seq_printf(seq, "\n ");
7395
NeilBrown57148962012-03-19 12:46:40 +11007396 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397
7398 seq_printf(seq, "\n");
7399 }
NeilBrown36d091f2014-12-15 12:56:58 +11007400 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007401
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 return 0;
7403}
7404
Jan Engelhardt110518b2009-05-07 12:49:37 +10007405static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 .start = md_seq_start,
7407 .next = md_seq_next,
7408 .stop = md_seq_stop,
7409 .show = md_seq_show,
7410};
7411
7412static int md_seq_open(struct inode *inode, struct file *file)
7413{
Kay Sieversf1514632011-07-12 20:48:39 +02007414 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 int error;
7416
7417 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007418 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007419 return error;
7420
7421 seq = file->private_data;
7422 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 return error;
7424}
7425
NeilBrowne2f23b62014-04-09 14:33:51 +10007426static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007427static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7428{
Kay Sieversf1514632011-07-12 20:48:39 +02007429 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007430 int mask;
7431
NeilBrowne2f23b62014-04-09 14:33:51 +10007432 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007433 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007434 poll_wait(filp, &md_event_waiters, wait);
7435
7436 /* always allow read */
7437 mask = POLLIN | POLLRDNORM;
7438
Kay Sieversf1514632011-07-12 20:48:39 +02007439 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007440 mask |= POLLERR | POLLPRI;
7441 return mask;
7442}
7443
Arjan van de Venfa027c22007-02-12 00:55:33 -08007444static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007445 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 .open = md_seq_open,
7447 .read = seq_read,
7448 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007449 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007450 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451};
7452
NeilBrown84fc4b52011-10-11 16:49:58 +11007453int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454{
Chao Yu50bd3772014-09-25 15:28:34 +08007455 printk(KERN_INFO "md: %s personality registered for level %d\n",
7456 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007458 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 spin_unlock(&pers_lock);
7460 return 0;
7461}
NeilBrown6c144d32014-09-30 16:15:38 +10007462EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463
NeilBrown84fc4b52011-10-11 16:49:58 +11007464int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465{
NeilBrown2604b702006-01-06 00:20:36 -08007466 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007468 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 spin_unlock(&pers_lock);
7470 return 0;
7471}
NeilBrown6c144d32014-09-30 16:15:38 +10007472EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473
NeilBrown6022e752015-08-13 12:32:55 +10007474int register_md_cluster_operations(struct md_cluster_operations *ops,
7475 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007476{
NeilBrown6022e752015-08-13 12:32:55 +10007477 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007478 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007479 if (md_cluster_ops != NULL)
7480 ret = -EALREADY;
7481 else {
7482 md_cluster_ops = ops;
7483 md_cluster_mod = module;
7484 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007485 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007486 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007487}
7488EXPORT_SYMBOL(register_md_cluster_operations);
7489
7490int unregister_md_cluster_operations(void)
7491{
7492 spin_lock(&pers_lock);
7493 md_cluster_ops = NULL;
7494 spin_unlock(&pers_lock);
7495 return 0;
7496}
7497EXPORT_SYMBOL(unregister_md_cluster_operations);
7498
7499int md_setup_cluster(struct mddev *mddev, int nodes)
7500{
7501 int err;
7502
7503 err = request_module("md-cluster");
7504 if (err) {
7505 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007506 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007507 }
7508
7509 spin_lock(&pers_lock);
7510 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7511 spin_unlock(&pers_lock);
7512 return -ENOENT;
7513 }
7514 spin_unlock(&pers_lock);
7515
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007516 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007517}
7518
7519void md_cluster_stop(struct mddev *mddev)
7520{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007521 if (!md_cluster_ops)
7522 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007523 md_cluster_ops->leave(mddev);
7524 module_put(md_cluster_mod);
7525}
7526
NeilBrownfd01b882011-10-11 16:47:53 +11007527static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007529 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007531 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532
7533 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007534 rcu_read_lock();
7535 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007537 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7538 (int)part_stat_read(&disk->part0, sectors[1]) -
7539 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007540 /* sync IO will cause sync_io to increase before the disk_stats
7541 * as sync_io is counted when a request starts, and
7542 * disk_stats is counted when it completes.
7543 * So resync activity will cause curr_events to be smaller than
7544 * when there was no such activity.
7545 * non-sync IO will cause disk_stat to increase without
7546 * increasing sync_io so curr_events will (eventually)
7547 * be larger than it was before. Once it becomes
7548 * substantially larger, the test below will cause
7549 * the array to appear non-idle, and resync will slow
7550 * down.
7551 * If there is a lot of outstanding resync activity when
7552 * we set last_event to curr_events, then all that activity
7553 * completing might cause the array to appear non-idle
7554 * and resync will be slowed down even though there might
7555 * not have been non-resync activity. This will only
7556 * happen once though. 'last_events' will soon reflect
7557 * the state where there is little or no outstanding
7558 * resync requests, and further resync activity will
7559 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007560 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007562 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 rdev->last_events = curr_events;
7564 idle = 0;
7565 }
7566 }
NeilBrown4b809912008-07-21 17:05:25 +10007567 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 return idle;
7569}
7570
NeilBrownfd01b882011-10-11 16:47:53 +11007571void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572{
7573 /* another "blocks" (512byte) blocks have been synced */
7574 atomic_sub(blocks, &mddev->recovery_active);
7575 wake_up(&mddev->recovery_wait);
7576 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007577 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007578 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 md_wakeup_thread(mddev->thread);
7580 // stop recovery, signal do_sync ....
7581 }
7582}
NeilBrown6c144d32014-09-30 16:15:38 +10007583EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584
NeilBrown06d91a52005-06-21 17:17:12 -07007585/* md_write_start(mddev, bi)
7586 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007587 * in superblock) before writing, schedule a superblock update
7588 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007589 */
NeilBrownfd01b882011-10-11 16:47:53 +11007590void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591{
Neil Brown0fd62b82008-06-28 08:31:36 +10007592 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007593 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007594 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007595
NeilBrownf91de922005-11-08 21:39:36 -08007596 BUG_ON(mddev->ro == 1);
7597 if (mddev->ro == 2) {
7598 /* need to switch to read/write */
7599 mddev->ro = 0;
7600 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7601 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007602 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007603 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007604 }
NeilBrown06d91a52005-06-21 17:17:12 -07007605 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007606 if (mddev->safemode == 1)
7607 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007608 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007609 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007610 if (mddev->in_sync) {
7611 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007612 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007613 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007614 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007615 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007616 }
NeilBrown85572d72014-12-15 12:56:56 +11007617 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007618 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007619 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007620 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007621 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007622 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623}
NeilBrown6c144d32014-09-30 16:15:38 +10007624EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625
NeilBrownfd01b882011-10-11 16:47:53 +11007626void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627{
7628 if (atomic_dec_and_test(&mddev->writes_pending)) {
7629 if (mddev->safemode == 2)
7630 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007631 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7633 }
7634}
NeilBrown6c144d32014-09-30 16:15:38 +10007635EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636
NeilBrown2a2275d2007-01-26 00:57:11 -08007637/* md_allow_write(mddev)
7638 * Calling this ensures that the array is marked 'active' so that writes
7639 * may proceed without blocking. It is important to call this before
7640 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7641 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007642 *
7643 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7644 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007645 */
NeilBrownfd01b882011-10-11 16:47:53 +11007646int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007647{
7648 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007649 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007650 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007651 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007652 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007653 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007654
NeilBrown85572d72014-12-15 12:56:56 +11007655 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007656 if (mddev->in_sync) {
7657 mddev->in_sync = 0;
7658 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007659 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007660 if (mddev->safemode_delay &&
7661 mddev->safemode == 0)
7662 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007663 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007664 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007665 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007666 } else
NeilBrown85572d72014-12-15 12:56:56 +11007667 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007668
NeilBrown070dc6d2010-08-30 17:33:34 +10007669 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007670 return -EAGAIN;
7671 else
7672 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007673}
7674EXPORT_SYMBOL_GPL(md_allow_write);
7675
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676#define SYNC_MARKS 10
7677#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007678#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007679void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680{
Shaohua Li4ed87312012-10-11 13:34:00 +11007681 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007682 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 unsigned int currspeed = 0,
7684 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007685 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007687 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 sector_t mark_cnt[SYNC_MARKS];
7689 int last_mark,m;
7690 struct list_head *tmp;
7691 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007692 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007693 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007694 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007695 struct blk_plug plug;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05007696 bool cluster_resync_finished = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697
7698 /* just incase thread restarts... */
7699 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7700 return;
NeilBrown3991b312014-05-28 13:39:23 +10007701 if (mddev->ro) {/* never try to sync a read-only array */
7702 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007703 return;
NeilBrown3991b312014-05-28 13:39:23 +10007704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705
NeilBrown61df9d92006-10-03 01:15:57 -07007706 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007707 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007708 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007709 action = "check";
7710 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007711 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007712 action = "repair";
7713 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007714 desc = "resync";
7715 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7716 desc = "reshape";
7717 else
7718 desc = "recovery";
7719
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007720 mddev->last_sync_action = action ?: desc;
7721
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722 /* we overload curr_resync somewhat here.
7723 * 0 == not engaged in resync at all
7724 * 2 == checking that there is no conflict with another sync
7725 * 1 == like 2, but have yielded to allow conflicting resync to
7726 * commense
7727 * other == active in resync - this many blocks
7728 *
7729 * Before starting a resync we must have set curr_resync to
7730 * 2, and then checked that every "conflicting" array has curr_resync
7731 * less than ours. When we find one that is the same or higher
7732 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7733 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7734 * This will mean we have to start checking from the beginning again.
7735 *
7736 */
7737
7738 do {
7739 mddev->curr_resync = 2;
7740
7741 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007742 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007744 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 if (mddev2 == mddev)
7746 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007747 if (!mddev->parallel_resync
7748 && mddev2->curr_resync
7749 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750 DEFINE_WAIT(wq);
7751 if (mddev < mddev2 && mddev->curr_resync == 2) {
7752 /* arbitrarily yield */
7753 mddev->curr_resync = 1;
7754 wake_up(&resync_wait);
7755 }
7756 if (mddev > mddev2 && mddev->curr_resync == 1)
7757 /* no need to wait here, we can wait the next
7758 * time 'round when curr_resync == 2
7759 */
7760 continue;
NeilBrown97441972008-09-19 11:49:54 +10007761 /* We need to wait 'interruptible' so as not to
7762 * contribute to the load average, and not to
7763 * be caught by 'softlockup'
7764 */
7765 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007766 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007767 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007768 printk(KERN_INFO "md: delaying %s of %s"
7769 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007771 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007773 if (signal_pending(current))
7774 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 schedule();
7776 finish_wait(&resync_wait, &wq);
7777 goto try_again;
7778 }
7779 finish_wait(&resync_wait, &wq);
7780 }
7781 }
7782 } while (mddev->curr_resync < 2);
7783
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007784 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007785 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007787 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788 */
7789 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007790 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007791 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007792 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7793 j = mddev->resync_min;
7794 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007795 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007796
NeilBrownccfcc3c2006-03-27 01:18:09 -08007797 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007798 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007799 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007801 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007802 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007803 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007804 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007805 if (rdev->raid_disk >= 0 &&
7806 !test_bit(Faulty, &rdev->flags) &&
7807 !test_bit(In_sync, &rdev->flags) &&
7808 rdev->recovery_offset < j)
7809 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007810 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007811
7812 /* If there is a bitmap, we need to make sure all
7813 * writes that started before we added a spare
7814 * complete before we start doing a recovery.
7815 * Otherwise the write might complete and (via
7816 * bitmap_endwrite) set a bit in the bitmap after the
7817 * recovery has checked that bit and skipped that
7818 * region.
7819 */
7820 if (mddev->bitmap) {
7821 mddev->pers->quiesce(mddev, 1);
7822 mddev->pers->quiesce(mddev, 0);
7823 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825
NeilBrown61df9d92006-10-03 01:15:57 -07007826 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7827 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7828 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007829 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007830 "(but not more than %d KB/sec) for %s.\n",
7831 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
NeilBrowneea1bf32009-03-31 14:27:02 +11007833 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007834
NeilBrown57afd892005-06-21 17:17:13 -07007835 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836 for (m = 0; m < SYNC_MARKS; m++) {
7837 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007838 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839 }
7840 last_mark = 0;
7841 mddev->resync_mark = mark[last_mark];
7842 mddev->resync_mark_cnt = mark_cnt[last_mark];
7843
7844 /*
7845 * Tune reconstruction:
7846 */
7847 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007848 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7849 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850
7851 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 last_check = 0;
7853
7854 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007855 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007856 "md: resuming %s of %s from checkpoint.\n",
7857 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007859 } else
7860 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007861 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007862 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7863 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007864 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865
majianpeng7c2c57c2012-07-03 12:12:26 +10007866 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007868 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869
NeilBrown57afd892005-06-21 17:17:13 -07007870 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007871
NeilBrown7a91ee12009-05-26 12:57:21 +10007872 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7873 ((mddev->curr_resync > mddev->curr_resync_completed &&
7874 (mddev->curr_resync - mddev->curr_resync_completed)
7875 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007876 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007877 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007878 >= mddev->resync_max - mddev->curr_resync_completed ||
7879 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007880 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007881 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007882 wait_event(mddev->recovery_wait,
7883 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007884 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007885 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7886 j > mddev->recovery_cp)
7887 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007888 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007889 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007890 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007891 }
NeilBrownacb180b2009-04-14 16:28:34 +10007892
NeilBrownc91abf52013-11-19 12:02:01 +11007893 while (j >= mddev->resync_max &&
7894 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007895 /* As this condition is controlled by user-space,
7896 * we can block indefinitely, so use '_interruptible'
7897 * to avoid triggering warnings.
7898 */
7899 flush_signals(current); /* just in case */
7900 wait_event_interruptible(mddev->recovery_wait,
7901 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007902 || test_bit(MD_RECOVERY_INTR,
7903 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007904 }
NeilBrownacb180b2009-04-14 16:28:34 +10007905
NeilBrownc91abf52013-11-19 12:02:01 +11007906 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7907 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007908
NeilBrown09314792015-02-19 16:04:40 +11007909 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07007910 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007911 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007912 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 }
NeilBrown57afd892005-06-21 17:17:13 -07007914
7915 if (!skipped) { /* actual IO requested */
7916 io_sectors += sectors;
7917 atomic_add(sectors, &mddev->recovery_active);
7918 }
7919
NeilBrowne875ece2011-07-28 11:39:24 +10007920 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7921 break;
7922
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10007924 if (j > max_sectors)
7925 /* when skipping, extra large numbers can be returned. */
7926 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007927 if (j > 2)
7928 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007929 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007930 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007931 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007932 * visible in /proc/mdstat
7933 */
7934 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007935
7936 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937 continue;
7938
NeilBrown57afd892005-06-21 17:17:13 -07007939 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 repeat:
7941 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7942 /* step marks */
7943 int next = (last_mark+1) % SYNC_MARKS;
7944
7945 mddev->resync_mark = mark[next];
7946 mddev->resync_mark_cnt = mark_cnt[next];
7947 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007948 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 last_mark = next;
7950 }
7951
NeilBrownc91abf52013-11-19 12:02:01 +11007952 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7953 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007954
7955 /*
7956 * this loop exits only if either when we are slower than
7957 * the 'hard' speed limit, or the system was IO-idle for
7958 * a jiffy.
7959 * the system might be non-idle CPU-wise, but we only care
7960 * about not overloading the IO subsystem. (things like an
7961 * e2fsck being done on the RAID array should execute fast)
7962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963 cond_resched();
7964
Xiao Niac7e50a2014-08-07 09:37:41 -04007965 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7966 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007967 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968
NeilBrown88202a02006-01-06 00:21:36 -08007969 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11007970 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007971 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 goto repeat;
7973 }
NeilBrownac8fa412015-02-19 16:55:00 +11007974 if (!is_mddev_idle(mddev, 0)) {
7975 /*
7976 * Give other IO more of a chance.
7977 * The faster the devices, the less we wait.
7978 */
7979 wait_event(mddev->recovery_wait,
7980 !atomic_read(&mddev->recovery_active));
7981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 }
7983 }
NeilBrownc91abf52013-11-19 12:02:01 +11007984 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7985 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7986 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 /*
7988 * this also signals 'finished resyncing' to md_stop
7989 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007990 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7992
NeilBrown5ed1df22015-07-24 13:27:08 +10007993 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7994 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7995 mddev->curr_resync > 2) {
7996 mddev->curr_resync_completed = mddev->curr_resync;
7997 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7998 }
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05007999 /* tell personality and other nodes that we are finished */
8000 if (mddev_is_clustered(mddev)) {
8001 md_cluster_ops->resync_finish(mddev);
8002 cluster_resync_finished = true;
8003 }
NeilBrown09314792015-02-19 16:04:40 +11008004 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005
NeilBrowndfc70642008-05-23 13:04:39 -07008006 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008007 mddev->curr_resync > 2) {
8008 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8009 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8010 if (mddev->curr_resync >= mddev->recovery_cp) {
8011 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07008012 "md: checkpointing %s of %s.\n",
8013 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008014 if (test_bit(MD_RECOVERY_ERROR,
8015 &mddev->recovery))
8016 mddev->recovery_cp =
8017 mddev->curr_resync_completed;
8018 else
8019 mddev->recovery_cp =
8020 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008021 }
8022 } else
8023 mddev->recovery_cp = MaxSector;
8024 } else {
8025 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8026 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008027 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008028 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008029 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10008030 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008031 !test_bit(Faulty, &rdev->flags) &&
8032 !test_bit(In_sync, &rdev->flags) &&
8033 rdev->recovery_offset < mddev->curr_resync)
8034 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07008035 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008038 skip:
NeilBrown17571282006-12-10 02:20:52 -08008039 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008041 if (mddev_is_clustered(mddev) &&
8042 test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8043 !cluster_resync_finished)
8044 md_cluster_ops->resync_finish(mddev);
8045
NeilBrown23da4222014-12-15 12:57:01 +11008046 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008047 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8048 /* We completed so min/max setting can be forgotten if used. */
8049 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8050 mddev->resync_min = 0;
8051 mddev->resync_max = MaxSector;
8052 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8053 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008054 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008056 spin_unlock(&mddev->lock);
8057
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008061}
NeilBrown29269552006-03-27 01:18:10 -08008062EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063
NeilBrown746d3202013-04-24 11:42:41 +10008064static int remove_and_add_spares(struct mddev *mddev,
8065 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008066{
NeilBrown3cb03002011-10-11 16:45:26 +11008067 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008068 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008069 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008070
NeilBrowndafb20f2012-03-19 12:46:39 +11008071 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008072 if ((this == NULL || rdev == this) &&
8073 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008074 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008075 (test_bit(Faulty, &rdev->flags) ||
8076 ! test_bit(In_sync, &rdev->flags)) &&
8077 atomic_read(&rdev->nr_pending)==0) {
8078 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008079 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008080 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008081 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008082 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008083 }
8084 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008085 if (removed && mddev->kobj.sd)
8086 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008087
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008088 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008089 goto no_add;
8090
NeilBrowndafb20f2012-03-19 12:46:39 +11008091 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008092 if (this && this != rdev)
8093 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008094 if (test_bit(Candidate, &rdev->flags))
8095 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008096 if (rdev->raid_disk >= 0 &&
8097 !test_bit(In_sync, &rdev->flags) &&
8098 !test_bit(Faulty, &rdev->flags))
8099 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008100 if (rdev->raid_disk >= 0)
8101 continue;
8102 if (test_bit(Faulty, &rdev->flags))
8103 continue;
Shaohua Li3069aa82015-08-13 14:31:56 -07008104 if (test_bit(Journal, &rdev->flags))
8105 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008106 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11008107 ! (rdev->saved_raid_disk >= 0 &&
8108 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10008109 continue;
8110
NeilBrown7eb41882014-01-14 15:55:14 +11008111 if (rdev->saved_raid_disk < 0)
8112 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008113 if (mddev->pers->
8114 hot_add_disk(mddev, rdev) == 0) {
8115 if (sysfs_link_rdev(mddev, rdev))
8116 /* failure here is OK */;
8117 spares++;
8118 md_new_event(mddev);
8119 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008120 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008121 }
NeilBrown746d3202013-04-24 11:42:41 +10008122no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008123 if (removed)
8124 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008125 return spares;
8126}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008127
NeilBrownac05f252014-09-30 08:10:42 +10008128static void md_start_sync(struct work_struct *ws)
8129{
8130 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008131 int ret = 0;
8132
8133 if (mddev_is_clustered(mddev)) {
8134 ret = md_cluster_ops->resync_start(mddev);
8135 if (ret) {
8136 mddev->sync_thread = NULL;
8137 goto out;
8138 }
8139 }
NeilBrownac05f252014-09-30 08:10:42 +10008140
8141 mddev->sync_thread = md_register_thread(md_do_sync,
8142 mddev,
8143 "resync");
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008144out:
NeilBrownac05f252014-09-30 08:10:42 +10008145 if (!mddev->sync_thread) {
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008146 if (!(mddev_is_clustered(mddev) && ret == -EAGAIN))
8147 printk(KERN_ERR "%s: could not start resync"
8148 " thread...\n",
8149 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008150 /* leave the spares where they are, it shouldn't hurt */
8151 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8152 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8153 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8154 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8155 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008156 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008157 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8158 &mddev->recovery))
8159 if (mddev->sysfs_action)
8160 sysfs_notify_dirent_safe(mddev->sysfs_action);
8161 } else
8162 md_wakeup_thread(mddev->sync_thread);
8163 sysfs_notify_dirent_safe(mddev->sysfs_action);
8164 md_new_event(mddev);
8165}
8166
Linus Torvalds1da177e2005-04-16 15:20:36 -07008167/*
8168 * This routine is regularly called by all per-raid-array threads to
8169 * deal with generic issues like resync and super-block update.
8170 * Raid personalities that don't have a thread (linear/raid0) do not
8171 * need this as they never do any recovery or update the superblock.
8172 *
8173 * It does not do any resync itself, but rather "forks" off other threads
8174 * to do that as needed.
8175 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8176 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008177 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178 * and wakeups up this thread which will reap the thread and finish up.
8179 * This thread also removes any faulty devices (with nr_pending == 0).
8180 *
8181 * The overall approach is:
8182 * 1/ if the superblock needs updating, update it.
8183 * 2/ If a recovery thread is running, don't do anything else.
8184 * 3/ If recovery has finished, clean up, possibly marking spares active.
8185 * 4/ If there are any faulty devices, remove them.
8186 * 5/ If array is degraded, try to add spares devices
8187 * 6/ If array has spares or is not in-sync, start a resync thread.
8188 */
NeilBrownfd01b882011-10-11 16:47:53 +11008189void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008191 if (mddev->suspended)
8192 return;
8193
NeilBrown5f404022005-06-21 17:17:16 -07008194 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008195 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196
NeilBrownfca4d842005-06-21 17:17:11 -07008197 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008198 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008199 printk(KERN_INFO "md: %s in immediate safe mode\n",
8200 mdname(mddev));
8201 mddev->safemode = 2;
8202 }
8203 flush_signals(current);
8204 }
8205
NeilBrownc89a8ee2008-08-05 15:54:13 +10008206 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8207 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008209 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008211 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008212 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008213 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8214 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 ))
8216 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008217
NeilBrowndf5b89b2006-03-27 01:18:20 -08008218 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008219 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008220
NeilBrownc89a8ee2008-08-05 15:54:13 +10008221 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008222 struct md_rdev *rdev;
8223 if (!mddev->external && mddev->in_sync)
8224 /* 'Blocked' flag not needed as failed devices
8225 * will be recorded if array switched to read/write.
8226 * Leaving it set will prevent the device
8227 * from being removed.
8228 */
8229 rdev_for_each(rdev, mddev)
8230 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008231 /* On a read-only array we can:
8232 * - remove failed devices
8233 * - add already-in_sync devices if the array itself
8234 * is in-sync.
8235 * As we only add devices that are already in-sync,
8236 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008237 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008238 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008239 /* There is no thread, but we need to call
8240 * ->spare_active and clear saved_raid_disk
8241 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008242 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008243 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008244 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008245 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008246 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008247 goto unlock;
8248 }
8249
NeilBrown31a59e32008-04-30 00:52:30 -07008250 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008251 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008252 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008253 if (mddev->safemode &&
8254 !atomic_read(&mddev->writes_pending) &&
8255 !mddev->in_sync &&
8256 mddev->recovery_cp == MaxSector) {
8257 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008258 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008259 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008260 }
8261 if (mddev->safemode == 1)
8262 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008263 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008264 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008265 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008266 }
NeilBrownfca4d842005-06-21 17:17:11 -07008267
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05008268 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07008269 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008270
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8272 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8273 /* resync/recovery still happening */
8274 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8275 goto unlock;
8276 }
8277 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008278 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279 goto unlock;
8280 }
Neil Brown72a23c22008-06-28 08:31:41 +10008281 /* Set RUNNING before clearing NEEDED to avoid
8282 * any transients in the value of "sync_action".
8283 */
NeilBrown72f36d52012-10-11 14:25:57 +11008284 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008285 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008286 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008287 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008288 /* Clear some bits that don't mean anything, but
8289 * might be left set
8290 */
NeilBrown24dd4692005-11-08 21:39:26 -08008291 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8292 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293
NeilBrowned209582012-04-24 10:23:14 +10008294 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8295 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008296 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297 /* no recovery is running.
8298 * remove any failed drives, then
8299 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008300 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301 * the personality to fail the re-add.
8302 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008304 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008305 if (mddev->pers->check_reshape == NULL ||
8306 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008307 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008308 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008309 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008310 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008311 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008312 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8313 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008314 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008315 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008316 } else if (mddev->recovery_cp < MaxSector) {
8317 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008318 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008319 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8320 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008321 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008322
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008324 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008325 /* We are adding a device or devices to an array
8326 * which has the bitmap stored on all devices.
8327 * So make sure all bitmap pages get written
8328 */
8329 bitmap_write_all(mddev->bitmap);
8330 }
NeilBrownac05f252014-09-30 08:10:42 +10008331 INIT_WORK(&mddev->del_work, md_start_sync);
8332 queue_work(md_misc_wq, &mddev->del_work);
8333 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008334 }
NeilBrownac05f252014-09-30 08:10:42 +10008335 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008336 if (!mddev->sync_thread) {
8337 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008338 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008339 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8340 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008341 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008342 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008343 }
NeilBrownac05f252014-09-30 08:10:42 +10008344 unlock:
8345 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346 mddev_unlock(mddev);
8347 }
8348}
NeilBrown6c144d32014-09-30 16:15:38 +10008349EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008351void md_reap_sync_thread(struct mddev *mddev)
8352{
8353 struct md_rdev *rdev;
8354
8355 /* resync has finished, collect result */
8356 md_unregister_thread(&mddev->sync_thread);
8357 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8358 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8359 /* success...*/
8360 /* activate any spares */
8361 if (mddev->pers->spare_active(mddev)) {
8362 sysfs_notify(&mddev->kobj, NULL,
8363 "degraded");
8364 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8365 }
8366 }
8367 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8368 mddev->pers->finish_reshape)
8369 mddev->pers->finish_reshape(mddev);
8370
8371 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008372 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008373 */
NeilBrownf4667222013-12-09 12:04:56 +11008374 if (!mddev->degraded)
8375 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008376 rdev->saved_raid_disk = -1;
8377
8378 md_update_sb(mddev, 1);
8379 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008380 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008381 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8382 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8383 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8384 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008385 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008386 /* flag recovery needed just to double check */
8387 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8388 sysfs_notify_dirent_safe(mddev->sysfs_action);
8389 md_new_event(mddev);
8390 if (mddev->event_work.func)
8391 queue_work(md_misc_wq, &mddev->event_work);
8392}
NeilBrown6c144d32014-09-30 16:15:38 +10008393EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008394
NeilBrownfd01b882011-10-11 16:47:53 +11008395void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008396{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008397 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008398 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008399 !test_bit(Blocked, &rdev->flags) &&
8400 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008401 msecs_to_jiffies(5000));
8402 rdev_dec_pending(rdev, mddev);
8403}
8404EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8405
NeilBrownc6563a82012-05-21 09:27:00 +10008406void md_finish_reshape(struct mddev *mddev)
8407{
8408 /* called be personality module when reshape completes. */
8409 struct md_rdev *rdev;
8410
8411 rdev_for_each(rdev, mddev) {
8412 if (rdev->data_offset > rdev->new_data_offset)
8413 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8414 else
8415 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8416 rdev->data_offset = rdev->new_data_offset;
8417 }
8418}
8419EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008420
8421/* Bad block management.
8422 * We can record which blocks on each device are 'bad' and so just
8423 * fail those blocks, or that stripe, rather than the whole device.
8424 * Entries in the bad-block table are 64bits wide. This comprises:
8425 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8426 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8427 * A 'shift' can be set so that larger blocks are tracked and
8428 * consequently larger devices can be covered.
8429 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8430 *
8431 * Locking of the bad-block table uses a seqlock so md_is_badblock
8432 * might need to retry if it is very unlucky.
8433 * We will sometimes want to check for bad blocks in a bi_end_io function,
8434 * so we use the write_seqlock_irq variant.
8435 *
8436 * When looking for a bad block we specify a range and want to
8437 * know if any block in the range is bad. So we binary-search
8438 * to the last range that starts at-or-before the given endpoint,
8439 * (or "before the sector after the target range")
8440 * then see if it ends after the given start.
8441 * We return
8442 * 0 if there are no known bad blocks in the range
8443 * 1 if there are known bad block which are all acknowledged
8444 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8445 * plus the start/length of the first bad section we overlap.
8446 */
8447int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8448 sector_t *first_bad, int *bad_sectors)
8449{
8450 int hi;
majianpengab056132012-11-06 17:13:44 +08008451 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008452 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008453 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008454 sector_t target = s + sectors;
8455 unsigned seq;
8456
8457 if (bb->shift > 0) {
8458 /* round the start down, and the end up */
8459 s >>= bb->shift;
8460 target += (1<<bb->shift) - 1;
8461 target >>= bb->shift;
8462 sectors = target - s;
8463 }
8464 /* 'target' is now the first block after the bad range */
8465
8466retry:
8467 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008468 lo = 0;
8469 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008470 hi = bb->count;
8471
8472 /* Binary search between lo and hi for 'target'
8473 * i.e. for the last range that starts before 'target'
8474 */
8475 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8476 * are known not to be the last range before target.
8477 * VARIANT: hi-lo is the number of possible
8478 * ranges, and decreases until it reaches 1
8479 */
8480 while (hi - lo > 1) {
8481 int mid = (lo + hi) / 2;
8482 sector_t a = BB_OFFSET(p[mid]);
8483 if (a < target)
8484 /* This could still be the one, earlier ranges
8485 * could not. */
8486 lo = mid;
8487 else
8488 /* This and later ranges are definitely out. */
8489 hi = mid;
8490 }
8491 /* 'lo' might be the last that started before target, but 'hi' isn't */
8492 if (hi > lo) {
8493 /* need to check all range that end after 's' to see if
8494 * any are unacknowledged.
8495 */
8496 while (lo >= 0 &&
8497 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8498 if (BB_OFFSET(p[lo]) < target) {
8499 /* starts before the end, and finishes after
8500 * the start, so they must overlap
8501 */
8502 if (rv != -1 && BB_ACK(p[lo]))
8503 rv = 1;
8504 else
8505 rv = -1;
8506 *first_bad = BB_OFFSET(p[lo]);
8507 *bad_sectors = BB_LEN(p[lo]);
8508 }
8509 lo--;
8510 }
8511 }
8512
8513 if (read_seqretry(&bb->lock, seq))
8514 goto retry;
8515
8516 return rv;
8517}
8518EXPORT_SYMBOL_GPL(md_is_badblock);
8519
8520/*
8521 * Add a range of bad blocks to the table.
8522 * This might extend the table, or might contract it
8523 * if two adjacent ranges can be merged.
8524 * We binary-search to find the 'insertion' point, then
8525 * decide how best to handle it.
8526 */
8527static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8528 int acknowledged)
8529{
8530 u64 *p;
8531 int lo, hi;
8532 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008533 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008534
8535 if (bb->shift < 0)
8536 /* badblocks are disabled */
8537 return 0;
8538
8539 if (bb->shift) {
8540 /* round the start down, and the end up */
8541 sector_t next = s + sectors;
8542 s >>= bb->shift;
8543 next += (1<<bb->shift) - 1;
8544 next >>= bb->shift;
8545 sectors = next - s;
8546 }
8547
Bian Yu905b0292013-10-12 01:10:03 -04008548 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008549
8550 p = bb->page;
8551 lo = 0;
8552 hi = bb->count;
8553 /* Find the last range that starts at-or-before 's' */
8554 while (hi - lo > 1) {
8555 int mid = (lo + hi) / 2;
8556 sector_t a = BB_OFFSET(p[mid]);
8557 if (a <= s)
8558 lo = mid;
8559 else
8560 hi = mid;
8561 }
8562 if (hi > lo && BB_OFFSET(p[lo]) > s)
8563 hi = lo;
8564
8565 if (hi > lo) {
8566 /* we found a range that might merge with the start
8567 * of our new range
8568 */
8569 sector_t a = BB_OFFSET(p[lo]);
8570 sector_t e = a + BB_LEN(p[lo]);
8571 int ack = BB_ACK(p[lo]);
8572 if (e >= s) {
8573 /* Yes, we can merge with a previous range */
8574 if (s == a && s + sectors >= e)
8575 /* new range covers old */
8576 ack = acknowledged;
8577 else
8578 ack = ack && acknowledged;
8579
8580 if (e < s + sectors)
8581 e = s + sectors;
8582 if (e - a <= BB_MAX_LEN) {
8583 p[lo] = BB_MAKE(a, e-a, ack);
8584 s = e;
8585 } else {
8586 /* does not all fit in one range,
8587 * make p[lo] maximal
8588 */
8589 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8590 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8591 s = a + BB_MAX_LEN;
8592 }
8593 sectors = e - s;
8594 }
8595 }
8596 if (sectors && hi < bb->count) {
8597 /* 'hi' points to the first range that starts after 's'.
8598 * Maybe we can merge with the start of that range */
8599 sector_t a = BB_OFFSET(p[hi]);
8600 sector_t e = a + BB_LEN(p[hi]);
8601 int ack = BB_ACK(p[hi]);
8602 if (a <= s + sectors) {
8603 /* merging is possible */
8604 if (e <= s + sectors) {
8605 /* full overlap */
8606 e = s + sectors;
8607 ack = acknowledged;
8608 } else
8609 ack = ack && acknowledged;
8610
8611 a = s;
8612 if (e - a <= BB_MAX_LEN) {
8613 p[hi] = BB_MAKE(a, e-a, ack);
8614 s = e;
8615 } else {
8616 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8617 s = a + BB_MAX_LEN;
8618 }
8619 sectors = e - s;
8620 lo = hi;
8621 hi++;
8622 }
8623 }
8624 if (sectors == 0 && hi < bb->count) {
8625 /* we might be able to combine lo and hi */
8626 /* Note: 's' is at the end of 'lo' */
8627 sector_t a = BB_OFFSET(p[hi]);
8628 int lolen = BB_LEN(p[lo]);
8629 int hilen = BB_LEN(p[hi]);
8630 int newlen = lolen + hilen - (s - a);
8631 if (s >= a && newlen < BB_MAX_LEN) {
8632 /* yes, we can combine them */
8633 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8634 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8635 memmove(p + hi, p + hi + 1,
8636 (bb->count - hi - 1) * 8);
8637 bb->count--;
8638 }
8639 }
8640 while (sectors) {
8641 /* didn't merge (it all).
8642 * Need to add a range just before 'hi' */
8643 if (bb->count >= MD_MAX_BADBLOCKS) {
8644 /* No room for more */
8645 rv = 0;
8646 break;
8647 } else {
8648 int this_sectors = sectors;
8649 memmove(p + hi + 1, p + hi,
8650 (bb->count - hi) * 8);
8651 bb->count++;
8652
8653 if (this_sectors > BB_MAX_LEN)
8654 this_sectors = BB_MAX_LEN;
8655 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8656 sectors -= this_sectors;
8657 s += this_sectors;
8658 }
8659 }
8660
8661 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008662 if (!acknowledged)
8663 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008664 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008665
8666 return rv;
8667}
8668
NeilBrown3cb03002011-10-11 16:45:26 +11008669int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008670 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008671{
NeilBrownc6563a82012-05-21 09:27:00 +10008672 int rv;
8673 if (is_new)
8674 s += rdev->new_data_offset;
8675 else
8676 s += rdev->data_offset;
8677 rv = md_set_badblocks(&rdev->badblocks,
8678 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008679 if (rv) {
8680 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008681 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008682 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
NeilBrown55ce74d2015-08-14 11:11:10 +10008683 set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008684 md_wakeup_thread(rdev->mddev->thread);
8685 }
8686 return rv;
8687}
8688EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8689
8690/*
8691 * Remove a range of bad blocks from the table.
8692 * This may involve extending the table if we spilt a region,
8693 * but it must not fail. So if the table becomes full, we just
8694 * drop the remove request.
8695 */
8696static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8697{
8698 u64 *p;
8699 int lo, hi;
8700 sector_t target = s + sectors;
8701 int rv = 0;
8702
8703 if (bb->shift > 0) {
8704 /* When clearing we round the start up and the end down.
8705 * This should not matter as the shift should align with
8706 * the block size and no rounding should ever be needed.
8707 * However it is better the think a block is bad when it
8708 * isn't than to think a block is not bad when it is.
8709 */
8710 s += (1<<bb->shift) - 1;
8711 s >>= bb->shift;
8712 target >>= bb->shift;
8713 sectors = target - s;
8714 }
8715
8716 write_seqlock_irq(&bb->lock);
8717
8718 p = bb->page;
8719 lo = 0;
8720 hi = bb->count;
8721 /* Find the last range that starts before 'target' */
8722 while (hi - lo > 1) {
8723 int mid = (lo + hi) / 2;
8724 sector_t a = BB_OFFSET(p[mid]);
8725 if (a < target)
8726 lo = mid;
8727 else
8728 hi = mid;
8729 }
8730 if (hi > lo) {
8731 /* p[lo] is the last range that could overlap the
8732 * current range. Earlier ranges could also overlap,
8733 * but only this one can overlap the end of the range.
8734 */
8735 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8736 /* Partial overlap, leave the tail of this range */
8737 int ack = BB_ACK(p[lo]);
8738 sector_t a = BB_OFFSET(p[lo]);
8739 sector_t end = a + BB_LEN(p[lo]);
8740
8741 if (a < s) {
8742 /* we need to split this range */
8743 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008744 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008745 goto out;
8746 }
8747 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8748 bb->count++;
8749 p[lo] = BB_MAKE(a, s-a, ack);
8750 lo++;
8751 }
8752 p[lo] = BB_MAKE(target, end - target, ack);
8753 /* there is no longer an overlap */
8754 hi = lo;
8755 lo--;
8756 }
8757 while (lo >= 0 &&
8758 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8759 /* This range does overlap */
8760 if (BB_OFFSET(p[lo]) < s) {
8761 /* Keep the early parts of this range. */
8762 int ack = BB_ACK(p[lo]);
8763 sector_t start = BB_OFFSET(p[lo]);
8764 p[lo] = BB_MAKE(start, s - start, ack);
8765 /* now low doesn't overlap, so.. */
8766 break;
8767 }
8768 lo--;
8769 }
8770 /* 'lo' is strictly before, 'hi' is strictly after,
8771 * anything between needs to be discarded
8772 */
8773 if (hi - lo > 1) {
8774 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8775 bb->count -= (hi - lo - 1);
8776 }
8777 }
8778
8779 bb->changed = 1;
8780out:
8781 write_sequnlock_irq(&bb->lock);
8782 return rv;
8783}
8784
NeilBrownc6563a82012-05-21 09:27:00 +10008785int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8786 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008787{
NeilBrownc6563a82012-05-21 09:27:00 +10008788 if (is_new)
8789 s += rdev->new_data_offset;
8790 else
8791 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008792 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008793 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008794}
8795EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8796
8797/*
8798 * Acknowledge all bad blocks in a list.
8799 * This only succeeds if ->changed is clear. It is used by
8800 * in-kernel metadata updates
8801 */
8802void md_ack_all_badblocks(struct badblocks *bb)
8803{
8804 if (bb->page == NULL || bb->changed)
8805 /* no point even trying */
8806 return;
8807 write_seqlock_irq(&bb->lock);
8808
majianpengecb178b2012-03-19 12:46:42 +11008809 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008810 u64 *p = bb->page;
8811 int i;
8812 for (i = 0; i < bb->count ; i++) {
8813 if (!BB_ACK(p[i])) {
8814 sector_t start = BB_OFFSET(p[i]);
8815 int len = BB_LEN(p[i]);
8816 p[i] = BB_MAKE(start, len, 1);
8817 }
8818 }
NeilBrownde393cd2011-07-28 11:31:48 +10008819 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008820 }
8821 write_sequnlock_irq(&bb->lock);
8822}
8823EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8824
NeilBrown16c791a2011-07-28 11:31:47 +10008825/* sysfs access to bad-blocks list.
8826 * We present two files.
8827 * 'bad-blocks' lists sector numbers and lengths of ranges that
8828 * are recorded as bad. The list is truncated to fit within
8829 * the one-page limit of sysfs.
8830 * Writing "sector length" to this file adds an acknowledged
8831 * bad block list.
8832 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8833 * been acknowledged. Writing to this file adds bad blocks
8834 * without acknowledging them. This is largely for testing.
8835 */
8836
8837static ssize_t
8838badblocks_show(struct badblocks *bb, char *page, int unack)
8839{
8840 size_t len;
8841 int i;
8842 u64 *p = bb->page;
8843 unsigned seq;
8844
8845 if (bb->shift < 0)
8846 return 0;
8847
8848retry:
8849 seq = read_seqbegin(&bb->lock);
8850
8851 len = 0;
8852 i = 0;
8853
8854 while (len < PAGE_SIZE && i < bb->count) {
8855 sector_t s = BB_OFFSET(p[i]);
8856 unsigned int length = BB_LEN(p[i]);
8857 int ack = BB_ACK(p[i]);
8858 i++;
8859
8860 if (unack && ack)
8861 continue;
8862
8863 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8864 (unsigned long long)s << bb->shift,
8865 length << bb->shift);
8866 }
NeilBrownde393cd2011-07-28 11:31:48 +10008867 if (unack && len == 0)
8868 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008869
8870 if (read_seqretry(&bb->lock, seq))
8871 goto retry;
8872
8873 return len;
8874}
8875
8876#define DO_DEBUG 1
8877
8878static ssize_t
8879badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8880{
8881 unsigned long long sector;
8882 int length;
8883 char newline;
8884#ifdef DO_DEBUG
8885 /* Allow clearing via sysfs *only* for testing/debugging.
8886 * Normally only a successful write may clear a badblock
8887 */
8888 int clear = 0;
8889 if (page[0] == '-') {
8890 clear = 1;
8891 page++;
8892 }
8893#endif /* DO_DEBUG */
8894
8895 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8896 case 3:
8897 if (newline != '\n')
8898 return -EINVAL;
8899 case 2:
8900 if (length <= 0)
8901 return -EINVAL;
8902 break;
8903 default:
8904 return -EINVAL;
8905 }
8906
8907#ifdef DO_DEBUG
8908 if (clear) {
8909 md_clear_badblocks(bb, sector, length);
8910 return len;
8911 }
8912#endif /* DO_DEBUG */
8913 if (md_set_badblocks(bb, sector, length, !unack))
8914 return len;
8915 else
8916 return -ENOSPC;
8917}
8918
Adrian Bunk75c96f82005-05-05 16:16:09 -07008919static int md_notify_reboot(struct notifier_block *this,
8920 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921{
8922 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008923 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008924 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925
NeilBrownc744a652012-03-19 12:46:37 +11008926 for_each_mddev(mddev, tmp) {
8927 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008928 if (mddev->pers)
8929 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008930 if (mddev->persistent)
8931 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008932 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008933 }
NeilBrownc744a652012-03-19 12:46:37 +11008934 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935 }
NeilBrownc744a652012-03-19 12:46:37 +11008936 /*
8937 * certain more exotic SCSI devices are known to be
8938 * volatile wrt too early system reboots. While the
8939 * right place to handle this issue is the given
8940 * driver, we do want to have a safe RAID driver ...
8941 */
8942 if (need_delay)
8943 mdelay(1000*1);
8944
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 return NOTIFY_DONE;
8946}
8947
Adrian Bunk75c96f82005-05-05 16:16:09 -07008948static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949 .notifier_call = md_notify_reboot,
8950 .next = NULL,
8951 .priority = INT_MAX, /* before any real devices */
8952};
8953
8954static void md_geninit(void)
8955{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008956 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008957
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008958 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008959}
8960
Adrian Bunk75c96f82005-05-05 16:16:09 -07008961static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962{
Tejun Heoe804ac72010-10-15 15:36:08 +02008963 int ret = -ENOMEM;
8964
Tejun Heoada609e2011-01-25 14:35:54 +01008965 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008966 if (!md_wq)
8967 goto err_wq;
8968
8969 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8970 if (!md_misc_wq)
8971 goto err_misc_wq;
8972
8973 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8974 goto err_md;
8975
8976 if ((ret = register_blkdev(0, "mdp")) < 0)
8977 goto err_mdp;
8978 mdp_major = ret;
8979
NeilBrownaf5628f2014-07-31 13:54:54 +10008980 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008981 md_probe, NULL, NULL);
8982 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983 md_probe, NULL, NULL);
8984
Linus Torvalds1da177e2005-04-16 15:20:36 -07008985 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008986 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008987
8988 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990
Tejun Heoe804ac72010-10-15 15:36:08 +02008991err_mdp:
8992 unregister_blkdev(MD_MAJOR, "md");
8993err_md:
8994 destroy_workqueue(md_misc_wq);
8995err_misc_wq:
8996 destroy_workqueue(md_wq);
8997err_wq:
8998 return ret;
8999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009001static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009002{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009003 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9004 struct md_rdev *rdev2;
9005 int role, ret;
9006 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009007
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009008 /* Check for change of roles in the active devices */
9009 rdev_for_each(rdev2, mddev) {
9010 if (test_bit(Faulty, &rdev2->flags))
9011 continue;
9012
9013 /* Check if the roles changed */
9014 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009015
9016 if (test_bit(Candidate, &rdev2->flags)) {
9017 if (role == 0xfffe) {
9018 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9019 md_kick_rdev_from_array(rdev2);
9020 continue;
9021 }
9022 else
9023 clear_bit(Candidate, &rdev2->flags);
9024 }
9025
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009026 if (role != rdev2->raid_disk) {
9027 /* got activated */
9028 if (rdev2->raid_disk == -1 && role != 0xffff) {
9029 rdev2->saved_raid_disk = role;
9030 ret = remove_and_add_spares(mddev, rdev2);
9031 pr_info("Activated spare: %s\n",
9032 bdevname(rdev2->bdev,b));
9033 continue;
9034 }
9035 /* device faulty
9036 * We just want to do the minimum to mark the disk
9037 * as faulty. The recovery is performed by the
9038 * one who initiated the error.
9039 */
9040 if ((role == 0xfffe) || (role == 0xfffd)) {
9041 md_error(mddev, rdev2);
9042 clear_bit(Blocked, &rdev2->flags);
9043 }
9044 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009045 }
9046
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009047 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9048 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009049
9050 /* Finally set the event to be up to date */
9051 mddev->events = le64_to_cpu(sb->events);
9052}
9053
9054static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9055{
9056 int err;
9057 struct page *swapout = rdev->sb_page;
9058 struct mdp_superblock_1 *sb;
9059
9060 /* Store the sb page of the rdev in the swapout temporary
9061 * variable in case we err in the future
9062 */
9063 rdev->sb_page = NULL;
9064 alloc_disk_sb(rdev);
9065 ClearPageUptodate(rdev->sb_page);
9066 rdev->sb_loaded = 0;
9067 err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version);
9068
9069 if (err < 0) {
9070 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9071 __func__, __LINE__, rdev->desc_nr, err);
9072 put_page(rdev->sb_page);
9073 rdev->sb_page = swapout;
9074 rdev->sb_loaded = 1;
9075 return err;
9076 }
9077
9078 sb = page_address(rdev->sb_page);
9079 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9080 * is not set
9081 */
9082
9083 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9084 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9085
9086 /* The other node finished recovery, call spare_active to set
9087 * device In_sync and mddev->degraded
9088 */
9089 if (rdev->recovery_offset == MaxSector &&
9090 !test_bit(In_sync, &rdev->flags) &&
9091 mddev->pers->spare_active(mddev))
9092 sysfs_notify(&mddev->kobj, NULL, "degraded");
9093
9094 put_page(swapout);
9095 return 0;
9096}
9097
9098void md_reload_sb(struct mddev *mddev, int nr)
9099{
9100 struct md_rdev *rdev;
9101 int err;
9102
9103 /* Find the rdev */
9104 rdev_for_each_rcu(rdev, mddev) {
9105 if (rdev->desc_nr == nr)
9106 break;
9107 }
9108
9109 if (!rdev || rdev->desc_nr != nr) {
9110 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9111 return;
9112 }
9113
9114 err = read_rdev(mddev, rdev);
9115 if (err < 0)
9116 return;
9117
9118 check_sb_changes(mddev, rdev);
9119
9120 /* Read all rdev's to update recovery_offset */
9121 rdev_for_each_rcu(rdev, mddev)
9122 read_rdev(mddev, rdev);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009123}
9124EXPORT_SYMBOL(md_reload_sb);
9125
Linus Torvalds1da177e2005-04-16 15:20:36 -07009126#ifndef MODULE
9127
9128/*
9129 * Searches all registered partitions for autorun RAID arrays
9130 * at boot time.
9131 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009132
9133static LIST_HEAD(all_detected_devices);
9134struct detected_devices_node {
9135 struct list_head list;
9136 dev_t dev;
9137};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009138
9139void md_autodetect_dev(dev_t dev)
9140{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009141 struct detected_devices_node *node_detected_dev;
9142
9143 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9144 if (node_detected_dev) {
9145 node_detected_dev->dev = dev;
9146 list_add_tail(&node_detected_dev->list, &all_detected_devices);
9147 } else {
9148 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
9149 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
9150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009151}
9152
Linus Torvalds1da177e2005-04-16 15:20:36 -07009153static void autostart_arrays(int part)
9154{
NeilBrown3cb03002011-10-11 16:45:26 +11009155 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009156 struct detected_devices_node *node_detected_dev;
9157 dev_t dev;
9158 int i_scanned, i_passed;
9159
9160 i_scanned = 0;
9161 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009162
9163 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
9164
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009165 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9166 i_scanned++;
9167 node_detected_dev = list_entry(all_detected_devices.next,
9168 struct detected_devices_node, list);
9169 list_del(&node_detected_dev->list);
9170 dev = node_detected_dev->dev;
9171 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07009172 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009173 if (IS_ERR(rdev))
9174 continue;
9175
NeilBrown403df472014-09-30 15:52:29 +10009176 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009177 continue;
NeilBrown403df472014-09-30 15:52:29 +10009178
NeilBrownd0fae182008-03-04 14:29:31 -08009179 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009180 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009181 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009183
9184 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
9185 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009186
9187 autorun_devices(part);
9188}
9189
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009190#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191
9192static __exit void md_exit(void)
9193{
NeilBrownfd01b882011-10-11 16:47:53 +11009194 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009196 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009197
NeilBrownaf5628f2014-07-31 13:54:54 +10009198 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009199 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009201 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202 unregister_blkdev(mdp_major, "mdp");
9203 unregister_reboot_notifier(&md_notifier);
9204 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009205
9206 /* We cannot unload the modules while some process is
9207 * waiting for us in select() or poll() - wake them up
9208 */
9209 md_unloading = 1;
9210 while (waitqueue_active(&md_event_waiters)) {
9211 /* not safe to leave yet */
9212 wake_up(&md_event_waiters);
9213 msleep(delay);
9214 delay += delay;
9215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009217
NeilBrown29ac4aa2008-02-06 01:39:58 -08009218 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009220 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009222 destroy_workqueue(md_misc_wq);
9223 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224}
9225
Dan Williams685784a2007-07-09 11:56:42 -07009226subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227module_exit(md_exit)
9228
NeilBrownf91de922005-11-08 21:39:36 -08009229static int get_ro(char *buffer, struct kernel_param *kp)
9230{
9231 return sprintf(buffer, "%d", start_readonly);
9232}
9233static int set_ro(const char *val, struct kernel_param *kp)
9234{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009235 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009236}
9237
NeilBrown80ca3a42006-07-10 04:44:18 -07009238module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9239module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009240module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009241
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009243MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009244MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009245MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);