blob: 1e1bdd86f40c7a7d1c011b0d74e1662656bef14f [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) &&
1611 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1612 if (ev1 < mddev->events)
1613 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001614 } else if (mddev->bitmap) {
1615 /* If adding to array with a bitmap, then we can accept an
1616 * older device, but not too old.
1617 */
NeilBrown41158c72005-06-21 17:17:25 -07001618 if (ev1 < mddev->bitmap->events_cleared)
1619 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001620 if (ev1 < mddev->events)
1621 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001622 } else {
1623 if (ev1 < mddev->events)
1624 /* just a hot-add of a new device, leave raid_disk at -1 */
1625 return 0;
1626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (mddev->level != LEVEL_MULTIPATH) {
1628 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001629 if (rdev->desc_nr < 0 ||
1630 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1631 role = 0xffff;
1632 rdev->desc_nr = -1;
1633 } else
1634 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 switch(role) {
1636 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 break;
1638 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001639 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641 default:
NeilBrownf4667222013-12-09 12:04:56 +11001642 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001643 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001644 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001645 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001646 if (!(le32_to_cpu(sb->feature_map) &
1647 MD_FEATURE_RECOVERY_BITMAP))
1648 rdev->saved_raid_disk = -1;
1649 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001650 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 rdev->raid_disk = role;
1652 break;
1653 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001654 if (sb->devflags & WriteMostly1)
1655 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001656 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1657 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001658 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001659 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return 0;
1662}
1663
NeilBrownfd01b882011-10-11 16:47:53 +11001664static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001667 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int max_dev, i;
1669 /* make rdev->sb match mddev and rdev data. */
1670
Namhyung Kim65a06f062011-07-27 11:00:36 +10001671 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 sb->feature_map = 0;
1674 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001675 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 memset(sb->pad3, 0, sizeof(sb->pad3));
1677
1678 sb->utime = cpu_to_le64((__u64)mddev->utime);
1679 sb->events = cpu_to_le64(mddev->events);
1680 if (mddev->in_sync)
1681 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1682 else
1683 sb->resync_offset = cpu_to_le64(0);
1684
NeilBrown1c05b4b2006-10-21 10:24:08 -07001685 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001686
NeilBrownf0ca3402006-02-02 14:28:04 -08001687 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001688 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001689 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001690 sb->level = cpu_to_le32(mddev->level);
1691 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001692
NeilBrownaeb9b2112011-08-25 14:43:08 +10001693 if (test_bit(WriteMostly, &rdev->flags))
1694 sb->devflags |= WriteMostly1;
1695 else
1696 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001697 sb->data_offset = cpu_to_le64(rdev->data_offset);
1698 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001699
NeilBrownc3d97142009-12-14 12:49:52 +11001700 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1701 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001702 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001703 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001704
1705 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001706 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001707 sb->feature_map |=
1708 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1709 sb->recovery_offset =
1710 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001711 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1712 sb->feature_map |=
1713 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001714 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001715 if (test_bit(Replacement, &rdev->flags))
1716 sb->feature_map |=
1717 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001718
NeilBrownf6705572006-03-27 01:18:11 -08001719 if (mddev->reshape_position != MaxSector) {
1720 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1721 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1722 sb->new_layout = cpu_to_le32(mddev->new_layout);
1723 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1724 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001725 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001726 if (mddev->delta_disks == 0 &&
1727 mddev->reshape_backwards)
1728 sb->feature_map
1729 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001730 if (rdev->new_data_offset != rdev->data_offset) {
1731 sb->feature_map
1732 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1733 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1734 - rdev->data_offset));
1735 }
NeilBrownf6705572006-03-27 01:18:11 -08001736 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001737
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001738 if (mddev_is_clustered(mddev))
1739 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1740
NeilBrown2699b672011-07-28 11:31:47 +10001741 if (rdev->badblocks.count == 0)
1742 /* Nothing to do for bad blocks*/ ;
1743 else if (sb->bblog_offset == 0)
1744 /* Cannot record bad blocks on this device */
1745 md_error(mddev, rdev);
1746 else {
1747 struct badblocks *bb = &rdev->badblocks;
1748 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1749 u64 *p = bb->page;
1750 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1751 if (bb->changed) {
1752 unsigned seq;
1753
1754retry:
1755 seq = read_seqbegin(&bb->lock);
1756
1757 memset(bbp, 0xff, PAGE_SIZE);
1758
1759 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001760 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001761 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1762 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001763 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001764 }
NeilBrownd0962932012-03-19 12:46:41 +11001765 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001766 if (read_seqretry(&bb->lock, seq))
1767 goto retry;
1768
1769 bb->sector = (rdev->sb_start +
1770 (int)le32_to_cpu(sb->bblog_offset));
1771 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001772 }
1773 }
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001776 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (rdev2->desc_nr+1 > max_dev)
1778 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001779
NeilBrown70471da2009-08-03 10:59:57 +10001780 if (max_dev > le32_to_cpu(sb->max_dev)) {
1781 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001782 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001783 rdev->sb_size = max_dev * 2 + 256;
1784 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1785 if (rdev->sb_size & bmask)
1786 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001787 } else
1788 max_dev = le32_to_cpu(sb->max_dev);
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 for (i=0; i<max_dev;i++)
1791 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001792
NeilBrowndafb20f2012-03-19 12:46:39 +11001793 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001795 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001797 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001799 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001800 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 else
1802 sb->dev_roles[i] = cpu_to_le16(0xffff);
1803 }
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 sb->sb_csum = calc_sb_1_csum(sb);
1806}
1807
Chris Webb0cd17fe2008-06-28 08:31:46 +10001808static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001809super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001810{
1811 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001812 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001813 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001814 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001815 if (rdev->data_offset != rdev->new_data_offset)
1816 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001817 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001818 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001819 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001820 max_sectors -= rdev->data_offset;
1821 if (!num_sectors || num_sectors > max_sectors)
1822 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001823 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001824 /* minor version 0 with bitmap we can't move */
1825 return 0;
1826 } else {
1827 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001828 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001829 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001830 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001831 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001832 if (!num_sectors || num_sectors > max_sectors)
1833 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001834 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001835 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001836 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001837 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001838 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001839 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001840 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001841 rdev->sb_page);
1842 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001843 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001844
1845}
1846
1847static int
1848super_1_allow_new_offset(struct md_rdev *rdev,
1849 unsigned long long new_offset)
1850{
1851 /* All necessary checks on new >= old have been done */
1852 struct bitmap *bitmap;
1853 if (new_offset >= rdev->data_offset)
1854 return 1;
1855
1856 /* with 1.0 metadata, there is no metadata to tread on
1857 * so we can always move back */
1858 if (rdev->mddev->minor_version == 0)
1859 return 1;
1860
1861 /* otherwise we must be sure not to step on
1862 * any metadata, so stay:
1863 * 36K beyond start of superblock
1864 * beyond end of badblocks
1865 * beyond write-intent bitmap
1866 */
1867 if (rdev->sb_start + (32+4)*2 > new_offset)
1868 return 0;
1869 bitmap = rdev->mddev->bitmap;
1870 if (bitmap && !rdev->mddev->bitmap_info.file &&
1871 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001872 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001873 return 0;
1874 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1875 return 0;
1876
1877 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001878}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Adrian Bunk75c96f82005-05-05 16:16:09 -07001880static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 [0] = {
1882 .name = "0.90.0",
1883 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001884 .load_super = super_90_load,
1885 .validate_super = super_90_validate,
1886 .sync_super = super_90_sync,
1887 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001888 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 },
1890 [1] = {
1891 .name = "md-1",
1892 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001893 .load_super = super_1_load,
1894 .validate_super = super_1_validate,
1895 .sync_super = super_1_sync,
1896 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001897 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 },
1899};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
NeilBrownfd01b882011-10-11 16:47:53 +11001901static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001902{
1903 if (mddev->sync_super) {
1904 mddev->sync_super(mddev, rdev);
1905 return;
1906 }
1907
1908 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1909
1910 super_types[mddev->major_version].sync_super(mddev, rdev);
1911}
1912
NeilBrownfd01b882011-10-11 16:47:53 +11001913static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
NeilBrown3cb03002011-10-11 16:45:26 +11001915 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
NeilBrown4b809912008-07-21 17:05:25 +10001917 rcu_read_lock();
1918 rdev_for_each_rcu(rdev, mddev1)
1919 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001920 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001921 rdev2->bdev->bd_contains) {
1922 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001923 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001924 }
1925 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return 0;
1927}
1928
1929static LIST_HEAD(pending_raid_disks);
1930
Andre Nollac5e7112009-08-03 10:59:47 +10001931/*
1932 * Try to register data integrity profile for an mddev
1933 *
1934 * This is called when an array is started and after a disk has been kicked
1935 * from the array. It only succeeds if all working and active component devices
1936 * are integrity capable with matching profiles.
1937 */
NeilBrownfd01b882011-10-11 16:47:53 +11001938int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001939{
NeilBrown3cb03002011-10-11 16:45:26 +11001940 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001941
Andre Nollac5e7112009-08-03 10:59:47 +10001942 if (list_empty(&mddev->disks))
1943 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001944 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1945 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001946 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001947 /* skip spares and non-functional disks */
1948 if (test_bit(Faulty, &rdev->flags))
1949 continue;
1950 if (rdev->raid_disk < 0)
1951 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001952 if (!reference) {
1953 /* Use the first rdev as the reference */
1954 reference = rdev;
1955 continue;
1956 }
1957 /* does this rdev's profile match the reference profile? */
1958 if (blk_integrity_compare(reference->bdev->bd_disk,
1959 rdev->bdev->bd_disk) < 0)
1960 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001961 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001962 if (!reference || !bdev_get_integrity(reference->bdev))
1963 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001964 /*
1965 * All component devices are integrity capable and have matching
1966 * profiles, register the common profile for the md device.
1967 */
1968 if (blk_integrity_register(mddev->gendisk,
1969 bdev_get_integrity(reference->bdev)) != 0) {
1970 printk(KERN_ERR "md: failed to register integrity for %s\n",
1971 mdname(mddev));
1972 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001973 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001974 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1975 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1976 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1977 mdname(mddev));
1978 return -EINVAL;
1979 }
Andre Nollac5e7112009-08-03 10:59:47 +10001980 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001981}
Andre Nollac5e7112009-08-03 10:59:47 +10001982EXPORT_SYMBOL(md_integrity_register);
1983
1984/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001985void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001986{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001987 struct blk_integrity *bi_rdev;
1988 struct blk_integrity *bi_mddev;
1989
1990 if (!mddev->gendisk)
1991 return;
1992
1993 bi_rdev = bdev_get_integrity(rdev->bdev);
1994 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10001995
1996 if (!bi_mddev) /* nothing to do */
1997 return;
1998 if (rdev->raid_disk < 0) /* skip spares */
1999 return;
2000 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2001 rdev->bdev->bd_disk) >= 0)
2002 return;
2003 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2004 blk_integrity_unregister(mddev->gendisk);
2005}
2006EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002007
NeilBrownf72ffdd2014-09-30 14:23:59 +10002008static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002010 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002011 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002012 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Dan Williams11e2ede2008-04-30 00:52:32 -07002014 /* prevent duplicates */
2015 if (find_rdev(mddev, rdev->bdev->bd_dev))
2016 return -EEXIST;
2017
Andre Nolldd8ac332009-03-31 14:33:13 +11002018 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2019 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2020 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002021 if (mddev->pers) {
2022 /* Cannot change size, so fail
2023 * If mddev->level <= 0, then we don't care
2024 * about aligning sizes (e.g. linear)
2025 */
2026 if (mddev->level > 0)
2027 return -ENOSPC;
2028 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002029 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 /* Verify rdev->desc_nr is unique.
2033 * If it is -1, assign a free number, else
2034 * check number is not in use
2035 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002036 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (rdev->desc_nr < 0) {
2038 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002039 if (mddev->pers)
2040 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002041 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 choice++;
2043 rdev->desc_nr = choice;
2044 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002045 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002046 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002050 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002051 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2052 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2053 mdname(mddev), mddev->max_disks);
2054 return -EBUSY;
2055 }
NeilBrown19133a42005-11-08 21:39:35 -08002056 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002057 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002060 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002061
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002062 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002063 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002064
Tejun Heo0762b8b2008-08-25 19:56:12 +09002065 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002066 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2067 /* failure here is OK */;
2068 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002069
NeilBrown4b809912008-07-21 17:05:25 +10002070 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002071 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002072
2073 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002074 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002077
2078 fail:
2079 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2080 b, mdname(mddev));
2081 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
NeilBrown177a99b2008-02-06 01:39:56 -08002084static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002085{
NeilBrown3cb03002011-10-11 16:45:26 +11002086 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002087 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002088 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002089}
2090
NeilBrownf72ffdd2014-09-30 14:23:59 +10002091static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002094
Tejun Heo49731ba2011-01-14 18:43:57 +01002095 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002096 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2098 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002099 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002100 sysfs_put(rdev->sysfs_state);
2101 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002102 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002103 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002104 * writing to 'remove' to "dev/state". We also need
2105 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002106 */
NeilBrown4b809912008-07-21 17:05:25 +10002107 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002108 INIT_WORK(&rdev->del_work, md_delayed_delete);
2109 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002110 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
2113/*
2114 * prevent the device from being mounted, repartitioned or
2115 * otherwise reused by a RAID array (or any other kernel
2116 * subsystem), by bd_claiming the device.
2117 */
NeilBrown3cb03002011-10-11 16:45:26 +11002118static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
2120 int err = 0;
2121 struct block_device *bdev;
2122 char b[BDEVNAME_SIZE];
2123
Tejun Heod4d77622010-11-13 11:55:18 +01002124 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002125 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (IS_ERR(bdev)) {
2127 printk(KERN_ERR "md: could not open %s.\n",
2128 __bdevname(dev, b));
2129 return PTR_ERR(bdev);
2130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 rdev->bdev = bdev;
2132 return err;
2133}
2134
NeilBrown3cb03002011-10-11 16:45:26 +11002135static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 struct block_device *bdev = rdev->bdev;
2138 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002139 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
2142void md_autodetect_dev(dev_t dev);
2143
NeilBrownf72ffdd2014-09-30 14:23:59 +10002144static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 printk(KERN_INFO "md: export_rdev(%s)\n",
2149 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002150 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002152 if (test_bit(AutoDetected, &rdev->flags))
2153 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154#endif
2155 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002156 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002159void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 unbind_rdev_from_array(rdev);
2162 export_rdev(rdev);
2163}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002164EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
NeilBrownfd01b882011-10-11 16:47:53 +11002166static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
NeilBrown0638bb02014-09-25 17:43:47 +10002168 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
NeilBrown0638bb02014-09-25 17:43:47 +10002170 while (!list_empty(&mddev->disks)) {
2171 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2172 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002173 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 mddev->raid_disks = 0;
2176 mddev->major_version = 0;
2177}
2178
NeilBrownf72ffdd2014-09-30 14:23:59 +10002179static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
NeilBrown42543762006-06-26 00:27:57 -07002181 /* Update each superblock (in-memory image), but
2182 * if we are allowed to, skip spares which already
2183 * have the right event counter, or have one earlier
2184 * (which would mean they aren't being marked as dirty
2185 * with the rest of the array)
2186 */
NeilBrown3cb03002011-10-11 16:45:26 +11002187 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002188 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002189 if (rdev->sb_events == mddev->events ||
2190 (nospares &&
2191 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002192 rdev->sb_events+1 == mddev->events)) {
2193 /* Don't update this superblock */
2194 rdev->sb_loaded = 2;
2195 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002196 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002197 rdev->sb_loaded = 1;
2198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200}
2201
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002202void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
NeilBrown3cb03002011-10-11 16:45:26 +11002204 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002205 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002206 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002207 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
NeilBrownd87f0642013-04-24 11:42:40 +10002209 if (mddev->ro) {
2210 if (force_change)
2211 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2212 return;
2213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002215 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002216 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002217 if (rdev->raid_disk >= 0 &&
2218 mddev->delta_disks >= 0 &&
2219 !test_bit(In_sync, &rdev->flags) &&
2220 mddev->curr_resync_completed > rdev->recovery_offset)
2221 rdev->recovery_offset = mddev->curr_resync_completed;
2222
NeilBrownf72ffdd2014-09-30 14:23:59 +10002223 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002224 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002225 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002226 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002227 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002228 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002229 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002230 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002231 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002232 md_ack_all_badblocks(&rdev->badblocks);
2233 md_error(mddev, rdev);
2234 }
2235 clear_bit(Blocked, &rdev->flags);
2236 clear_bit(BlockedBadBlocks, &rdev->flags);
2237 wake_up(&rdev->blocked_wait);
2238 }
2239 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002240 wake_up(&mddev->sb_wait);
2241 return;
2242 }
2243
NeilBrown85572d72014-12-15 12:56:56 +11002244 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002245
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002246 mddev->utime = get_seconds();
2247
NeilBrown850b2b42006-10-03 01:15:46 -07002248 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2249 force_change = 1;
2250 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2251 /* just a clean<-> dirty transition, possibly leave spares alone,
2252 * though if events isn't the right even/odd, we will have to do
2253 * spares after all
2254 */
2255 nospares = 1;
2256 if (force_change)
2257 nospares = 0;
2258 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002259 /* If the array is degraded, then skipping spares is both
2260 * dangerous and fairly pointless.
2261 * Dangerous because a device that was removed from the array
2262 * might have a event_count that still looks up-to-date,
2263 * so it can be re-added without a resync.
2264 * Pointless because if there are any spares to skip,
2265 * then a recovery will happen and soon that array won't
2266 * be degraded any more and the spare can go back to sleep then.
2267 */
NeilBrown850b2b42006-10-03 01:15:46 -07002268 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002269
NeilBrown06d91a52005-06-21 17:17:12 -07002270 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002271
2272 /* If this is just a dirty<->clean transition, and the array is clean
2273 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002274 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002275 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002276 && mddev->can_decrease_events
2277 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002278 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002279 mddev->can_decrease_events = 0;
2280 } else {
NeilBrown42543762006-06-26 00:27:57 -07002281 /* otherwise we have to go forward and ... */
2282 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002283 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
NeilBrown403df472014-09-30 15:52:29 +10002286 /*
2287 * This 64-bit counter should never wrap.
2288 * Either we are in around ~1 trillion A.C., assuming
2289 * 1 reboot per second, or we have a bug...
2290 */
2291 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002292
NeilBrowndafb20f2012-03-19 12:46:39 +11002293 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002294 if (rdev->badblocks.changed)
2295 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002296 if (test_bit(Faulty, &rdev->flags))
2297 set_bit(FaultRecorded, &rdev->flags);
2298 }
NeilBrown2699b672011-07-28 11:31:47 +10002299
NeilBrowne6910632008-02-06 01:39:51 -08002300 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002301 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
NeilBrown36a4e1f2011-10-07 14:23:17 +11002303 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2304 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
NeilBrown4ad13662007-07-17 04:06:13 -07002306 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002307 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002309
NeilBrown42543762006-06-26 00:27:57 -07002310 if (rdev->sb_loaded != 1)
2311 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
NeilBrownf4667222013-12-09 12:04:56 +11002313 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002314 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002315 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002316 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002317 pr_debug("md: (write) %s's sb offset: %llu\n",
2318 bdevname(rdev->bdev, b),
2319 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002320 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002321 if (rdev->badblocks.size) {
2322 md_super_write(mddev, rdev,
2323 rdev->badblocks.sector,
2324 rdev->badblocks.size << 9,
2325 rdev->bb_page);
2326 rdev->badblocks.size = 0;
2327 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002328
NeilBrownf4667222013-12-09 12:04:56 +11002329 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002330 pr_debug("md: %s (skipping faulty)\n",
2331 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002332
NeilBrown7bfa19f2005-06-21 17:17:28 -07002333 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /* only need to write one superblock... */
2335 break;
2336 }
NeilBrowna9701a32005-11-08 21:39:34 -08002337 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002338 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002339
NeilBrown85572d72014-12-15 12:56:56 +11002340 spin_lock(&mddev->lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002341 if (mddev->in_sync != sync_req ||
2342 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002343 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002344 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002345 goto repeat;
2346 }
NeilBrown850b2b42006-10-03 01:15:46 -07002347 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002348 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002349 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002350 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2351 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002352
NeilBrowndafb20f2012-03-19 12:46:39 +11002353 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002354 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2355 clear_bit(Blocked, &rdev->flags);
2356
2357 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002358 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002359 clear_bit(BlockedBadBlocks, &rdev->flags);
2360 wake_up(&rdev->blocked_wait);
2361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002363EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002365static int add_bound_rdev(struct md_rdev *rdev)
2366{
2367 struct mddev *mddev = rdev->mddev;
2368 int err = 0;
2369
2370 if (!mddev->pers->hot_remove_disk) {
2371 /* If there is hot_add_disk but no hot_remove_disk
2372 * then added disks for geometry changes,
2373 * and should be added immediately.
2374 */
2375 super_types[mddev->major_version].
2376 validate_super(mddev, rdev);
2377 err = mddev->pers->hot_add_disk(mddev, rdev);
2378 if (err) {
2379 unbind_rdev_from_array(rdev);
2380 export_rdev(rdev);
2381 return err;
2382 }
2383 }
2384 sysfs_notify_dirent_safe(rdev->sysfs_state);
2385
2386 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2387 if (mddev->degraded)
2388 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2389 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2390 md_new_event(mddev);
2391 md_wakeup_thread(mddev->thread);
2392 return 0;
2393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Andre Noll7f6ce762008-03-23 18:34:54 +01002395/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002396 * We want to accept with case. For this we use cmd_match.
2397 */
2398static int cmd_match(const char *cmd, const char *str)
2399{
2400 /* See if cmd, written into a sysfs file, matches
2401 * str. They must either be the same, or cmd can
2402 * have a trailing newline
2403 */
2404 while (*cmd && *str && *cmd == *str) {
2405 cmd++;
2406 str++;
2407 }
2408 if (*cmd == '\n')
2409 cmd++;
2410 if (*str || *cmd)
2411 return 0;
2412 return 1;
2413}
2414
NeilBrown86e6ffd2005-11-08 21:39:24 -08002415struct rdev_sysfs_entry {
2416 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002417 ssize_t (*show)(struct md_rdev *, char *);
2418 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002419};
2420
2421static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002422state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002423{
2424 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002425 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002426 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002427
NeilBrown758bfc82014-12-15 12:56:59 +11002428 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002429 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002430 len+= sprintf(page+len, "%sfaulty",sep);
2431 sep = ",";
2432 }
NeilBrown758bfc82014-12-15 12:56:59 +11002433 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002434 len += sprintf(page+len, "%sin_sync",sep);
2435 sep = ",";
2436 }
NeilBrown758bfc82014-12-15 12:56:59 +11002437 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002438 len += sprintf(page+len, "%swrite_mostly",sep);
2439 sep = ",";
2440 }
NeilBrown758bfc82014-12-15 12:56:59 +11002441 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002442 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002443 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002444 len += sprintf(page+len, "%sblocked", sep);
2445 sep = ",";
2446 }
NeilBrown758bfc82014-12-15 12:56:59 +11002447 if (!test_bit(Faulty, &flags) &&
2448 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002449 len += sprintf(page+len, "%sspare", sep);
2450 sep = ",";
2451 }
NeilBrown758bfc82014-12-15 12:56:59 +11002452 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002453 len += sprintf(page+len, "%swrite_error", sep);
2454 sep = ",";
2455 }
NeilBrown758bfc82014-12-15 12:56:59 +11002456 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002457 len += sprintf(page+len, "%swant_replacement", sep);
2458 sep = ",";
2459 }
NeilBrown758bfc82014-12-15 12:56:59 +11002460 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002461 len += sprintf(page+len, "%sreplacement", sep);
2462 sep = ",";
2463 }
2464
NeilBrown86e6ffd2005-11-08 21:39:24 -08002465 return len+sprintf(page+len, "\n");
2466}
2467
NeilBrown45dc2de2006-06-26 00:27:58 -07002468static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002469state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002470{
2471 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002472 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002473 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002474 * writemostly - sets write_mostly
2475 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002476 * blocked - sets the Blocked flags
2477 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002478 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002479 * -insync - clear Insync for a device with a slot assigned,
2480 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002481 * write_error - sets WriteErrorSeen
2482 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002483 */
2484 int err = -EINVAL;
2485 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2486 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002487 if (test_bit(Faulty, &rdev->flags))
2488 err = 0;
2489 else
2490 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002491 } else if (cmd_match(buf, "remove")) {
2492 if (rdev->raid_disk >= 0)
2493 err = -EBUSY;
2494 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002495 struct mddev *mddev = rdev->mddev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002496 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002497 md_cluster_ops->remove_disk(mddev, rdev);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002498 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002499 if (mddev_is_clustered(mddev))
2500 md_cluster_ops->metadata_update_start(mddev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002501 if (mddev->pers)
2502 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002503 md_new_event(mddev);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002504 if (mddev_is_clustered(mddev))
2505 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown45dc2de2006-06-26 00:27:58 -07002506 err = 0;
2507 }
NeilBrownf6556752006-06-26 00:28:01 -07002508 } else if (cmd_match(buf, "writemostly")) {
2509 set_bit(WriteMostly, &rdev->flags);
2510 err = 0;
2511 } else if (cmd_match(buf, "-writemostly")) {
2512 clear_bit(WriteMostly, &rdev->flags);
2513 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002514 } else if (cmd_match(buf, "blocked")) {
2515 set_bit(Blocked, &rdev->flags);
2516 err = 0;
2517 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002518 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002519 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002520 /* metadata handler doesn't understand badblocks,
2521 * so we need to fail the device
2522 */
2523 md_error(rdev->mddev, rdev);
2524 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002525 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002526 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002527 wake_up(&rdev->blocked_wait);
2528 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2529 md_wakeup_thread(rdev->mddev->thread);
2530
2531 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002532 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2533 set_bit(In_sync, &rdev->flags);
2534 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002535 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002536 if (rdev->mddev->pers == NULL) {
2537 clear_bit(In_sync, &rdev->flags);
2538 rdev->saved_raid_disk = rdev->raid_disk;
2539 rdev->raid_disk = -1;
2540 err = 0;
2541 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002542 } else if (cmd_match(buf, "write_error")) {
2543 set_bit(WriteErrorSeen, &rdev->flags);
2544 err = 0;
2545 } else if (cmd_match(buf, "-write_error")) {
2546 clear_bit(WriteErrorSeen, &rdev->flags);
2547 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002548 } else if (cmd_match(buf, "want_replacement")) {
2549 /* Any non-spare device that is not a replacement can
2550 * become want_replacement at any time, but we then need to
2551 * check if recovery is needed.
2552 */
2553 if (rdev->raid_disk >= 0 &&
2554 !test_bit(Replacement, &rdev->flags))
2555 set_bit(WantReplacement, &rdev->flags);
2556 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2557 md_wakeup_thread(rdev->mddev->thread);
2558 err = 0;
2559 } else if (cmd_match(buf, "-want_replacement")) {
2560 /* Clearing 'want_replacement' is always allowed.
2561 * Once replacements starts it is too late though.
2562 */
2563 err = 0;
2564 clear_bit(WantReplacement, &rdev->flags);
2565 } else if (cmd_match(buf, "replacement")) {
2566 /* Can only set a device as a replacement when array has not
2567 * yet been started. Once running, replacement is automatic
2568 * from spares, or by assigning 'slot'.
2569 */
2570 if (rdev->mddev->pers)
2571 err = -EBUSY;
2572 else {
2573 set_bit(Replacement, &rdev->flags);
2574 err = 0;
2575 }
2576 } else if (cmd_match(buf, "-replacement")) {
2577 /* Similarly, can only clear Replacement before start */
2578 if (rdev->mddev->pers)
2579 err = -EBUSY;
2580 else {
2581 clear_bit(Replacement, &rdev->flags);
2582 err = 0;
2583 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002584 } else if (cmd_match(buf, "re-add")) {
2585 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002586 /* clear_bit is performed _after_ all the devices
2587 * have their local Faulty bit cleared. If any writes
2588 * happen in the meantime in the local node, they
2589 * will land in the local bitmap, which will be synced
2590 * by this node eventually
2591 */
2592 if (!mddev_is_clustered(rdev->mddev) ||
2593 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2594 clear_bit(Faulty, &rdev->flags);
2595 err = add_bound_rdev(rdev);
2596 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002597 } else
2598 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002599 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002600 if (!err)
2601 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002602 return err ? err : len;
2603}
NeilBrown80ca3a42006-07-10 04:44:18 -07002604static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002605__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002606
2607static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002608errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002609{
2610 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2611}
2612
2613static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002614errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002615{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002616 unsigned int n;
2617 int rv;
2618
2619 rv = kstrtouint(buf, 10, &n);
2620 if (rv < 0)
2621 return rv;
2622 atomic_set(&rdev->corrected_errors, n);
2623 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002624}
2625static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002626__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002627
NeilBrown014236d2006-01-06 00:20:55 -08002628static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002629slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002630{
2631 if (rdev->raid_disk < 0)
2632 return sprintf(page, "none\n");
2633 else
2634 return sprintf(page, "%d\n", rdev->raid_disk);
2635}
2636
2637static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002638slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002639{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002640 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002641 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002642
NeilBrown014236d2006-01-06 00:20:55 -08002643 if (strncmp(buf, "none", 4)==0)
2644 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002645 else {
2646 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2647 if (err < 0)
2648 return err;
2649 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002650 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002651 /* Setting 'slot' on an active array requires also
2652 * updating the 'rd%d' link, and communicating
2653 * with the personality with ->hot_*_disk.
2654 * For now we only support removing
2655 * failed/spare devices. This normally happens automatically,
2656 * but not when the metadata is externally managed.
2657 */
NeilBrownc303da62008-02-06 01:39:51 -08002658 if (rdev->raid_disk == -1)
2659 return -EEXIST;
2660 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002661 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002662 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002663 clear_bit(Blocked, &rdev->flags);
2664 remove_and_add_spares(rdev->mddev, rdev);
2665 if (rdev->raid_disk >= 0)
2666 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002667 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2668 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002669 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002670 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002671 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002672 */
2673
2674 if (rdev->raid_disk != -1)
2675 return -EBUSY;
2676
NeilBrownc6751b22011-02-02 11:57:13 +11002677 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2678 return -EBUSY;
2679
Neil Brown6c2fce22008-06-28 08:31:31 +10002680 if (rdev->mddev->pers->hot_add_disk == NULL)
2681 return -EINVAL;
2682
NeilBrownba1b41b2011-01-14 09:14:34 +11002683 if (slot >= rdev->mddev->raid_disks &&
2684 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2685 return -ENOSPC;
2686
Neil Brown6c2fce22008-06-28 08:31:31 +10002687 rdev->raid_disk = slot;
2688 if (test_bit(In_sync, &rdev->flags))
2689 rdev->saved_raid_disk = slot;
2690 else
2691 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002692 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002693 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002694 err = rdev->mddev->pers->
2695 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002696 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002697 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002698 return err;
Neil Brown52664732008-06-28 08:31:44 +10002699 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002700 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002701 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002702 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002703 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002704 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002705 if (slot >= rdev->mddev->raid_disks &&
2706 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002707 return -ENOSPC;
2708 rdev->raid_disk = slot;
2709 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002710 clear_bit(Faulty, &rdev->flags);
2711 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002712 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002713 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002714 }
NeilBrown014236d2006-01-06 00:20:55 -08002715 return len;
2716}
2717
NeilBrown014236d2006-01-06 00:20:55 -08002718static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002719__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002720
NeilBrown93c8cad2006-01-06 00:20:56 -08002721static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002722offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002723{
Andrew Morton6961ece2006-01-06 00:20:59 -08002724 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002725}
2726
2727static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002728offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002729{
NeilBrownc6563a82012-05-21 09:27:00 +10002730 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002731 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002732 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002733 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002734 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002735 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002736 /* Must set offset before size, so overlap checks
2737 * can be sane */
2738 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002739 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002740 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002741 return len;
2742}
2743
2744static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002745__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002746
NeilBrownc6563a82012-05-21 09:27:00 +10002747static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2748{
2749 return sprintf(page, "%llu\n",
2750 (unsigned long long)rdev->new_data_offset);
2751}
2752
2753static ssize_t new_offset_store(struct md_rdev *rdev,
2754 const char *buf, size_t len)
2755{
2756 unsigned long long new_offset;
2757 struct mddev *mddev = rdev->mddev;
2758
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002759 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002760 return -EINVAL;
2761
NeilBrownf851b602014-12-11 10:02:10 +11002762 if (mddev->sync_thread ||
2763 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002764 return -EBUSY;
2765 if (new_offset == rdev->data_offset)
2766 /* reset is always permitted */
2767 ;
2768 else if (new_offset > rdev->data_offset) {
2769 /* must not push array size beyond rdev_sectors */
2770 if (new_offset - rdev->data_offset
2771 + mddev->dev_sectors > rdev->sectors)
2772 return -E2BIG;
2773 }
2774 /* Metadata worries about other space details. */
2775
2776 /* decreasing the offset is inconsistent with a backwards
2777 * reshape.
2778 */
2779 if (new_offset < rdev->data_offset &&
2780 mddev->reshape_backwards)
2781 return -EINVAL;
2782 /* Increasing offset is inconsistent with forwards
2783 * reshape. reshape_direction should be set to
2784 * 'backwards' first.
2785 */
2786 if (new_offset > rdev->data_offset &&
2787 !mddev->reshape_backwards)
2788 return -EINVAL;
2789
2790 if (mddev->pers && mddev->persistent &&
2791 !super_types[mddev->major_version]
2792 .allow_new_offset(rdev, new_offset))
2793 return -E2BIG;
2794 rdev->new_data_offset = new_offset;
2795 if (new_offset > rdev->data_offset)
2796 mddev->reshape_backwards = 1;
2797 else if (new_offset < rdev->data_offset)
2798 mddev->reshape_backwards = 0;
2799
2800 return len;
2801}
2802static struct rdev_sysfs_entry rdev_new_offset =
2803__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2804
NeilBrown83303b62006-01-06 00:21:06 -08002805static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002806rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002807{
Andre Nolldd8ac332009-03-31 14:33:13 +11002808 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002809}
2810
NeilBrownc5d79ad2008-02-06 01:39:54 -08002811static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2812{
2813 /* check if two start/length pairs overlap */
2814 if (s1+l1 <= s2)
2815 return 0;
2816 if (s2+l2 <= s1)
2817 return 0;
2818 return 1;
2819}
2820
Dan Williamsb522adc2009-03-31 15:00:31 +11002821static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2822{
2823 unsigned long long blocks;
2824 sector_t new;
2825
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002826 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002827 return -EINVAL;
2828
2829 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2830 return -EINVAL; /* sector conversion overflow */
2831
2832 new = blocks * 2;
2833 if (new != blocks * 2)
2834 return -EINVAL; /* unsigned long long to sector_t overflow */
2835
2836 *sectors = new;
2837 return 0;
2838}
2839
NeilBrown83303b62006-01-06 00:21:06 -08002840static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002841rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002842{
NeilBrownfd01b882011-10-11 16:47:53 +11002843 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002844 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002845 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002846
Dan Williamsb522adc2009-03-31 15:00:31 +11002847 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002848 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002849 if (rdev->data_offset != rdev->new_data_offset)
2850 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002851 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002852 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002853 sectors = super_types[my_mddev->major_version].
2854 rdev_size_change(rdev, sectors);
2855 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002856 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002857 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002858 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002859 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002860 if (!my_mddev->pers->resize)
2861 /* Cannot change size for RAID0 or Linear etc */
2862 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002863 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002864 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002865 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002866
Andre Nolldd8ac332009-03-31 14:33:13 +11002867 rdev->sectors = sectors;
2868 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002869 /* Need to check that all other rdevs with the same
2870 * ->bdev do not overlap. 'rcu' is sufficient to walk
2871 * the rdev lists safely.
2872 * This check does not provide a hard guarantee, it
2873 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002874 */
NeilBrownfd01b882011-10-11 16:47:53 +11002875 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002876 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002877 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002878
NeilBrown8b1afc32014-09-29 15:33:20 +10002879 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002880 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002881 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002882
NeilBrowndafb20f2012-03-19 12:46:39 +11002883 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002884 if (rdev->bdev == rdev2->bdev &&
2885 rdev != rdev2 &&
2886 overlaps(rdev->data_offset, rdev->sectors,
2887 rdev2->data_offset,
2888 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002889 overlap = 1;
2890 break;
2891 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002892 if (overlap) {
2893 mddev_put(mddev);
2894 break;
2895 }
2896 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002897 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002898 if (overlap) {
2899 /* Someone else could have slipped in a size
2900 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002901 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002902 * safe, and trust userspace not to race with
2903 * itself
2904 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002905 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002906 return -EBUSY;
2907 }
2908 }
NeilBrown83303b62006-01-06 00:21:06 -08002909 return len;
2910}
2911
2912static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002913__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002914
NeilBrown3cb03002011-10-11 16:45:26 +11002915static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002916{
2917 unsigned long long recovery_start = rdev->recovery_offset;
2918
2919 if (test_bit(In_sync, &rdev->flags) ||
2920 recovery_start == MaxSector)
2921 return sprintf(page, "none\n");
2922
2923 return sprintf(page, "%llu\n", recovery_start);
2924}
2925
NeilBrown3cb03002011-10-11 16:45:26 +11002926static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002927{
2928 unsigned long long recovery_start;
2929
2930 if (cmd_match(buf, "none"))
2931 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002932 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002933 return -EINVAL;
2934
2935 if (rdev->mddev->pers &&
2936 rdev->raid_disk >= 0)
2937 return -EBUSY;
2938
2939 rdev->recovery_offset = recovery_start;
2940 if (recovery_start == MaxSector)
2941 set_bit(In_sync, &rdev->flags);
2942 else
2943 clear_bit(In_sync, &rdev->flags);
2944 return len;
2945}
2946
2947static struct rdev_sysfs_entry rdev_recovery_start =
2948__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2949
NeilBrown16c791a2011-07-28 11:31:47 +10002950static ssize_t
2951badblocks_show(struct badblocks *bb, char *page, int unack);
2952static ssize_t
2953badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2954
NeilBrown3cb03002011-10-11 16:45:26 +11002955static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002956{
2957 return badblocks_show(&rdev->badblocks, page, 0);
2958}
NeilBrown3cb03002011-10-11 16:45:26 +11002959static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002960{
NeilBrownde393cd2011-07-28 11:31:48 +10002961 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2962 /* Maybe that ack was all we needed */
2963 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2964 wake_up(&rdev->blocked_wait);
2965 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002966}
2967static struct rdev_sysfs_entry rdev_bad_blocks =
2968__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2969
NeilBrown3cb03002011-10-11 16:45:26 +11002970static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002971{
2972 return badblocks_show(&rdev->badblocks, page, 1);
2973}
NeilBrown3cb03002011-10-11 16:45:26 +11002974static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002975{
2976 return badblocks_store(&rdev->badblocks, page, len, 1);
2977}
2978static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2979__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2980
NeilBrown86e6ffd2005-11-08 21:39:24 -08002981static struct attribute *rdev_default_attrs[] = {
2982 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002983 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002984 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002985 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10002986 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002987 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002988 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002989 &rdev_bad_blocks.attr,
2990 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002991 NULL,
2992};
2993static ssize_t
2994rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2995{
2996 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002997 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002998
2999 if (!entry->show)
3000 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003001 if (!rdev->mddev)
3002 return -EBUSY;
3003 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003004}
3005
3006static ssize_t
3007rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3008 const char *page, size_t length)
3009{
3010 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003011 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003012 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003013 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003014
3015 if (!entry->store)
3016 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003017 if (!capable(CAP_SYS_ADMIN))
3018 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003019 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003020 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003021 if (rdev->mddev == NULL)
3022 rv = -EBUSY;
3023 else
3024 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003025 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003026 }
3027 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003028}
3029
3030static void rdev_free(struct kobject *ko)
3031{
NeilBrown3cb03002011-10-11 16:45:26 +11003032 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003033 kfree(rdev);
3034}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003035static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003036 .show = rdev_attr_show,
3037 .store = rdev_attr_store,
3038};
3039static struct kobj_type rdev_ktype = {
3040 .release = rdev_free,
3041 .sysfs_ops = &rdev_sysfs_ops,
3042 .default_attrs = rdev_default_attrs,
3043};
3044
NeilBrown3cb03002011-10-11 16:45:26 +11003045int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003046{
3047 rdev->desc_nr = -1;
3048 rdev->saved_raid_disk = -1;
3049 rdev->raid_disk = -1;
3050 rdev->flags = 0;
3051 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003052 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003053 rdev->sb_events = 0;
3054 rdev->last_read_error.tv_sec = 0;
3055 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003056 rdev->sb_loaded = 0;
3057 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003058 atomic_set(&rdev->nr_pending, 0);
3059 atomic_set(&rdev->read_errors, 0);
3060 atomic_set(&rdev->corrected_errors, 0);
3061
3062 INIT_LIST_HEAD(&rdev->same_set);
3063 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003064
3065 /* Add space to store bad block list.
3066 * This reserves the space even on arrays where it cannot
3067 * be used - I wonder if that matters
3068 */
3069 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003070 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003071 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3072 seqlock_init(&rdev->badblocks.lock);
3073 if (rdev->badblocks.page == NULL)
3074 return -ENOMEM;
3075
3076 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003077}
3078EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079/*
3080 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3081 *
3082 * mark the device faulty if:
3083 *
3084 * - the device is nonexistent (zero size)
3085 * - the device has no valid superblock
3086 *
3087 * a faulty rdev _never_ has rdev->sb set.
3088 */
NeilBrown3cb03002011-10-11 16:45:26 +11003089static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
3091 char b[BDEVNAME_SIZE];
3092 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003093 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 sector_t size;
3095
NeilBrown9ffae0c2006-01-06 00:20:32 -08003096 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (!rdev) {
3098 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3099 return ERR_PTR(-ENOMEM);
3100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
NeilBrown2230dfe2011-07-28 11:31:46 +10003102 err = md_rdev_init(rdev);
3103 if (err)
3104 goto abort_free;
3105 err = alloc_disk_sb(rdev);
3106 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 goto abort_free;
3108
NeilBrownc5d79ad2008-02-06 01:39:54 -08003109 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (err)
3111 goto abort_free;
3112
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003113 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003114
Mike Snitzer77304d22010-11-08 14:39:12 +01003115 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003117 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 "md: %s has zero or unknown size, marking faulty!\n",
3119 bdevname(rdev->bdev,b));
3120 err = -EINVAL;
3121 goto abort_free;
3122 }
3123
3124 if (super_format >= 0) {
3125 err = super_types[super_format].
3126 load_super(rdev, NULL, super_minor);
3127 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003128 printk(KERN_WARNING
3129 "md: %s does not have a valid v%d.%d "
3130 "superblock, not importing!\n",
3131 bdevname(rdev->bdev,b),
3132 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 goto abort_free;
3134 }
3135 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003136 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 "md: could not read %s's sb, not importing!\n",
3138 bdevname(rdev->bdev,b));
3139 goto abort_free;
3140 }
3141 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return rdev;
3144
3145abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003146 if (rdev->bdev)
3147 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003148 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 kfree(rdev);
3150 return ERR_PTR(err);
3151}
3152
3153/*
3154 * Check a full RAID array for plausibility
3155 */
3156
NeilBrownf72ffdd2014-09-30 14:23:59 +10003157static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
3159 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003160 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 char b[BDEVNAME_SIZE];
3162
3163 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003164 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 switch (super_types[mddev->major_version].
3166 load_super(rdev, freshest, mddev->minor_version)) {
3167 case 1:
3168 freshest = rdev;
3169 break;
3170 case 0:
3171 break;
3172 default:
3173 printk( KERN_ERR \
3174 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003175 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003177 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 }
3179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 super_types[mddev->major_version].
3181 validate_super(mddev, freshest);
3182
3183 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003184 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003185 if (mddev->max_disks &&
3186 (rdev->desc_nr >= mddev->max_disks ||
3187 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003188 printk(KERN_WARNING
3189 "md: %s: %s: only %d devices permitted\n",
3190 mdname(mddev), bdevname(rdev->bdev, b),
3191 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003192 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003193 continue;
3194 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003195 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 if (super_types[mddev->major_version].
3197 validate_super(mddev, rdev)) {
3198 printk(KERN_WARNING "md: kicking non-fresh %s"
3199 " from array!\n",
3200 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003201 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 continue;
3203 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003204 /* No device should have a Candidate flag
3205 * when reading devices
3206 */
3207 if (test_bit(Candidate, &rdev->flags)) {
3208 pr_info("md: kicking Cluster Candidate %s from array!\n",
3209 bdevname(rdev->bdev, b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003210 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003211 }
3212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 if (mddev->level == LEVEL_MULTIPATH) {
3214 rdev->desc_nr = i++;
3215 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003216 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003217 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003218 rdev->raid_disk = -1;
3219 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 }
3221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
NeilBrown72e02072009-12-14 12:49:55 +11003224/* Read a fixed-point number.
3225 * Numbers in sysfs attributes should be in "standard" units where
3226 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003227 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003228 * milliseconds or jiffies.
3229 * This function takes a decimal number with a possible fractional
3230 * component, and produces an integer which is the result of
3231 * multiplying that number by 10^'scale'.
3232 * all without any floating-point arithmetic.
3233 */
3234int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3235{
3236 unsigned long result = 0;
3237 long decimals = -1;
3238 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3239 if (*cp == '.')
3240 decimals = 0;
3241 else if (decimals < scale) {
3242 unsigned int value;
3243 value = *cp - '0';
3244 result = result * 10 + value;
3245 if (decimals >= 0)
3246 decimals++;
3247 }
3248 cp++;
3249 }
3250 if (*cp == '\n')
3251 cp++;
3252 if (*cp)
3253 return -EINVAL;
3254 if (decimals < 0)
3255 decimals = 0;
3256 while (decimals < scale) {
3257 result *= 10;
3258 decimals ++;
3259 }
3260 *res = result;
3261 return 0;
3262}
3263
NeilBrowneae17012005-11-08 21:39:23 -08003264static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003265safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003266{
3267 int msec = (mddev->safemode_delay*1000)/HZ;
3268 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3269}
3270static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003271safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003272{
NeilBrown16f17b32006-06-26 00:27:37 -07003273 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003274
NeilBrown72e02072009-12-14 12:49:55 +11003275 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003276 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003277 if (msec == 0)
3278 mddev->safemode_delay = 0;
3279 else {
NeilBrown19052c02008-08-05 15:54:13 +10003280 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003281 unsigned long new_delay = (msec*HZ)/1000;
3282
3283 if (new_delay == 0)
3284 new_delay = 1;
3285 mddev->safemode_delay = new_delay;
3286 if (new_delay < old_delay || old_delay == 0)
3287 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003288 }
3289 return len;
3290}
3291static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003292__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003293
3294static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003295level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003296{
NeilBrown36d091f2014-12-15 12:56:58 +11003297 struct md_personality *p;
3298 int ret;
3299 spin_lock(&mddev->lock);
3300 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003301 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003302 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003303 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003304 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003305 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003306 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003307 else
NeilBrown36d091f2014-12-15 12:56:58 +11003308 ret = 0;
3309 spin_unlock(&mddev->lock);
3310 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003311}
3312
NeilBrownd9d166c2006-01-06 00:20:51 -08003313static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003314level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003315{
Dan Williamsf2859af2010-05-02 10:04:16 -07003316 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003317 ssize_t rv;
3318 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003319 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003320 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003321 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003322 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003323
NeilBrown67918752014-12-15 12:57:01 +11003324 if (slen == 0 || slen >= sizeof(clevel))
3325 return -EINVAL;
3326
3327 rv = mddev_lock(mddev);
3328 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003329 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003330
3331 if (mddev->pers == NULL) {
3332 strncpy(mddev->clevel, buf, slen);
3333 if (mddev->clevel[slen-1] == '\n')
3334 slen--;
3335 mddev->clevel[slen] = 0;
3336 mddev->level = LEVEL_NONE;
3337 rv = len;
3338 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003339 }
NeilBrown67918752014-12-15 12:57:01 +11003340 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003341 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003342 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003343
3344 /* request to change the personality. Need to ensure:
3345 * - array is not engaged in resync/recovery/reshape
3346 * - old personality can be suspended
3347 * - new personality will access other array.
3348 */
3349
NeilBrown67918752014-12-15 12:57:01 +11003350 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003351 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003352 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003353 mddev->reshape_position != MaxSector ||
3354 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003355 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003356
NeilBrown67918752014-12-15 12:57:01 +11003357 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003358 if (!mddev->pers->quiesce) {
3359 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3360 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003361 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003362 }
3363
3364 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003365 strncpy(clevel, buf, slen);
3366 if (clevel[slen-1] == '\n')
3367 slen--;
3368 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003369 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003370 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003371
Dan Williamsf2859af2010-05-02 10:04:16 -07003372 if (request_module("md-%s", clevel) != 0)
3373 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003374 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003375 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003376 if (!pers || !try_module_get(pers->owner)) {
3377 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003378 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003379 rv = -EINVAL;
3380 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003381 }
3382 spin_unlock(&pers_lock);
3383
3384 if (pers == mddev->pers) {
3385 /* Nothing to do! */
3386 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003387 rv = len;
3388 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003389 }
3390 if (!pers->takeover) {
3391 module_put(pers->owner);
3392 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003393 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003394 rv = -EINVAL;
3395 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003396 }
3397
NeilBrowndafb20f2012-03-19 12:46:39 +11003398 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003399 rdev->new_raid_disk = rdev->raid_disk;
3400
NeilBrown245f46c2009-03-31 14:39:39 +11003401 /* ->takeover must set new_* and/or delta_disks
3402 * if it succeeds, and may set them when it fails.
3403 */
3404 priv = pers->takeover(mddev);
3405 if (IS_ERR(priv)) {
3406 mddev->new_level = mddev->level;
3407 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003408 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003409 mddev->raid_disks -= mddev->delta_disks;
3410 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003411 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003412 module_put(pers->owner);
3413 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003414 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003415 rv = PTR_ERR(priv);
3416 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003417 }
3418
3419 /* Looks like we have a winner */
3420 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003421 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003422
3423 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003424 oldpers = mddev->pers;
3425 oldpriv = mddev->private;
3426 mddev->pers = pers;
3427 mddev->private = priv;
3428 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3429 mddev->level = mddev->new_level;
3430 mddev->layout = mddev->new_layout;
3431 mddev->chunk_sectors = mddev->new_chunk_sectors;
3432 mddev->delta_disks = 0;
3433 mddev->reshape_backwards = 0;
3434 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003435 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003436
NeilBrowndb721d32014-12-15 12:56:58 +11003437 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003438 mddev->external) {
3439 /* We are converting from a no-redundancy array
3440 * to a redundancy array and metadata is managed
3441 * externally so we need to be sure that writes
3442 * won't block due to a need to transition
3443 * clean->dirty
3444 * until external management is started.
3445 */
3446 mddev->in_sync = 0;
3447 mddev->safemode_delay = 0;
3448 mddev->safemode = 0;
3449 }
3450
NeilBrowndb721d32014-12-15 12:56:58 +11003451 oldpers->free(mddev, oldpriv);
3452
3453 if (oldpers->sync_request == NULL &&
3454 pers->sync_request != NULL) {
3455 /* need to add the md_redundancy_group */
3456 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3457 printk(KERN_WARNING
3458 "md: cannot register extra attributes for %s\n",
3459 mdname(mddev));
3460 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3461 }
3462 if (oldpers->sync_request != NULL &&
3463 pers->sync_request == NULL) {
3464 /* need to remove the md_redundancy_group */
3465 if (mddev->to_remove == NULL)
3466 mddev->to_remove = &md_redundancy_group;
3467 }
3468
NeilBrowndafb20f2012-03-19 12:46:39 +11003469 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003470 if (rdev->raid_disk < 0)
3471 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003472 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003473 rdev->new_raid_disk = -1;
3474 if (rdev->new_raid_disk == rdev->raid_disk)
3475 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003476 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003477 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003478 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003479 if (rdev->raid_disk < 0)
3480 continue;
3481 if (rdev->new_raid_disk == rdev->raid_disk)
3482 continue;
3483 rdev->raid_disk = rdev->new_raid_disk;
3484 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003485 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003486 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003487 if (sysfs_link_rdev(mddev, rdev))
3488 printk(KERN_WARNING "md: cannot register rd%d"
3489 " for %s after level change\n",
3490 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003491 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003492 }
3493
NeilBrowndb721d32014-12-15 12:56:58 +11003494 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003495 /* this is now an array without redundancy, so
3496 * it must always be in_sync
3497 */
3498 mddev->in_sync = 1;
3499 del_timer_sync(&mddev->safemode_timer);
3500 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003501 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003502 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003503 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003504 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003505 if (!mddev->thread)
3506 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003507 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003508 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003509 rv = len;
3510out_unlock:
3511 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003512 return rv;
3513}
3514
3515static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003516__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003517
NeilBrownd4dbd022006-06-26 00:27:59 -07003518static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003519layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003520{
3521 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003522 if (mddev->reshape_position != MaxSector &&
3523 mddev->layout != mddev->new_layout)
3524 return sprintf(page, "%d (%d)\n",
3525 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003526 return sprintf(page, "%d\n", mddev->layout);
3527}
3528
3529static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003530layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003531{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003532 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003533 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003534
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003535 err = kstrtouint(buf, 10, &n);
3536 if (err < 0)
3537 return err;
NeilBrown67918752014-12-15 12:57:01 +11003538 err = mddev_lock(mddev);
3539 if (err)
3540 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003541
NeilBrownb3546032009-03-31 14:56:41 +11003542 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003543 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003544 err = -EBUSY;
3545 else if (mddev->ro)
3546 err = -EROFS;
3547 else {
3548 mddev->new_layout = n;
3549 err = mddev->pers->check_reshape(mddev);
3550 if (err)
3551 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003552 }
NeilBrownb3546032009-03-31 14:56:41 +11003553 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003554 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003555 if (mddev->reshape_position == MaxSector)
3556 mddev->layout = n;
3557 }
NeilBrown67918752014-12-15 12:57:01 +11003558 mddev_unlock(mddev);
3559 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003560}
3561static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003562__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003563
NeilBrowneae17012005-11-08 21:39:23 -08003564static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003565raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003566{
NeilBrownbb636542005-11-08 21:39:45 -08003567 if (mddev->raid_disks == 0)
3568 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003569 if (mddev->reshape_position != MaxSector &&
3570 mddev->delta_disks != 0)
3571 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3572 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003573 return sprintf(page, "%d\n", mddev->raid_disks);
3574}
3575
NeilBrownfd01b882011-10-11 16:47:53 +11003576static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003577
3578static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003579raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003580{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003581 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003582 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003583
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003584 err = kstrtouint(buf, 10, &n);
3585 if (err < 0)
3586 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003587
NeilBrown67918752014-12-15 12:57:01 +11003588 err = mddev_lock(mddev);
3589 if (err)
3590 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003591 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003592 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003593 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003594 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003595 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003596
NeilBrown67918752014-12-15 12:57:01 +11003597 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003598 rdev_for_each(rdev, mddev) {
3599 if (olddisks < n &&
3600 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003601 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003602 if (olddisks > n &&
3603 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003604 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003605 }
NeilBrown67918752014-12-15 12:57:01 +11003606 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003607 mddev->delta_disks = n - olddisks;
3608 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003609 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003610 } else
NeilBrownda943b992006-01-06 00:20:54 -08003611 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003612out_unlock:
3613 mddev_unlock(mddev);
3614 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003615}
3616static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003617__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003618
NeilBrown24dd4692005-11-08 21:39:26 -08003619static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003620chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003621{
NeilBrown08a02ec2007-05-09 02:35:38 -07003622 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003623 mddev->chunk_sectors != mddev->new_chunk_sectors)
3624 return sprintf(page, "%d (%d)\n",
3625 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003626 mddev->chunk_sectors << 9);
3627 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003628}
3629
3630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003631chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003632{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003633 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003634 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003635
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003636 err = kstrtoul(buf, 10, &n);
3637 if (err < 0)
3638 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003639
NeilBrown67918752014-12-15 12:57:01 +11003640 err = mddev_lock(mddev);
3641 if (err)
3642 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003643 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003644 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003645 err = -EBUSY;
3646 else if (mddev->ro)
3647 err = -EROFS;
3648 else {
3649 mddev->new_chunk_sectors = n >> 9;
3650 err = mddev->pers->check_reshape(mddev);
3651 if (err)
3652 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003653 }
NeilBrownb3546032009-03-31 14:56:41 +11003654 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003655 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003656 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003657 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003658 }
NeilBrown67918752014-12-15 12:57:01 +11003659 mddev_unlock(mddev);
3660 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003661}
3662static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003663__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003664
NeilBrowna94213b2006-06-26 00:28:00 -07003665static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003666resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003667{
NeilBrownd1a7c502009-03-31 15:24:32 +11003668 if (mddev->recovery_cp == MaxSector)
3669 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003670 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3671}
3672
3673static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003674resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003675{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003676 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003677 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003678
3679 if (cmd_match(buf, "none"))
3680 n = MaxSector;
3681 else {
3682 err = kstrtoull(buf, 10, &n);
3683 if (err < 0)
3684 return err;
3685 if (n != (sector_t)n)
3686 return -EINVAL;
3687 }
NeilBrowna94213b2006-06-26 00:28:00 -07003688
NeilBrown67918752014-12-15 12:57:01 +11003689 err = mddev_lock(mddev);
3690 if (err)
3691 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003692 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003693 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003694
NeilBrown67918752014-12-15 12:57:01 +11003695 if (!err) {
3696 mddev->recovery_cp = n;
3697 if (mddev->pers)
3698 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3699 }
3700 mddev_unlock(mddev);
3701 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003702}
3703static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003704__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3705 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003706
NeilBrown9e653b62006-06-26 00:27:58 -07003707/*
3708 * The array state can be:
3709 *
3710 * clear
3711 * No devices, no size, no level
3712 * Equivalent to STOP_ARRAY ioctl
3713 * inactive
3714 * May have some settings, but array is not active
3715 * all IO results in error
3716 * When written, doesn't tear down array, but just stops it
3717 * suspended (not supported yet)
3718 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003719 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003720 * readonly
3721 * no resync can happen. no superblocks get written.
3722 * write requests fail
3723 * read-auto
3724 * like readonly, but behaves like 'clean' on a write request.
3725 *
3726 * clean - no pending writes, but otherwise active.
3727 * When written to inactive array, starts without resync
3728 * If a write request arrives then
3729 * if metadata is known, mark 'dirty' and switch to 'active'.
3730 * if not known, block and switch to write-pending
3731 * If written to an active array that has pending writes, then fails.
3732 * active
3733 * fully active: IO and resync can be happening.
3734 * When written to inactive array, starts with resync
3735 *
3736 * write-pending
3737 * clean, but writes are blocked waiting for 'active' to be written.
3738 *
3739 * active-idle
3740 * like active, but no writes have been seen for a while (100msec).
3741 *
3742 */
3743enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3744 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003745static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003746 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3747 "write-pending", "active-idle", NULL };
3748
3749static int match_word(const char *word, char **list)
3750{
3751 int n;
3752 for (n=0; list[n]; n++)
3753 if (cmd_match(word, list[n]))
3754 break;
3755 return n;
3756}
3757
3758static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003759array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003760{
3761 enum array_state st = inactive;
3762
3763 if (mddev->pers)
3764 switch(mddev->ro) {
3765 case 1:
3766 st = readonly;
3767 break;
3768 case 2:
3769 st = read_auto;
3770 break;
3771 case 0:
3772 if (mddev->in_sync)
3773 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003774 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003775 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003776 else if (mddev->safemode)
3777 st = active_idle;
3778 else
3779 st = active;
3780 }
3781 else {
3782 if (list_empty(&mddev->disks) &&
3783 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003784 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003785 st = clear;
3786 else
3787 st = inactive;
3788 }
3789 return sprintf(page, "%s\n", array_states[st]);
3790}
3791
NeilBrownf72ffdd2014-09-30 14:23:59 +10003792static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3793static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3794static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003795static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003796
3797static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003798array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003799{
NeilBrown67918752014-12-15 12:57:01 +11003800 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003801 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003802
3803 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3804 /* don't take reconfig_mutex when toggling between
3805 * clean and active
3806 */
3807 spin_lock(&mddev->lock);
3808 if (st == active) {
3809 restart_array(mddev);
3810 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3811 wake_up(&mddev->sb_wait);
3812 err = 0;
3813 } else /* st == clean */ {
3814 restart_array(mddev);
3815 if (atomic_read(&mddev->writes_pending) == 0) {
3816 if (mddev->in_sync == 0) {
3817 mddev->in_sync = 1;
3818 if (mddev->safemode == 1)
3819 mddev->safemode = 0;
3820 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3821 }
3822 err = 0;
3823 } else
3824 err = -EBUSY;
3825 }
3826 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003827 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003828 }
3829 err = mddev_lock(mddev);
3830 if (err)
3831 return err;
3832 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003833 switch(st) {
3834 case bad_word:
3835 break;
3836 case clear:
3837 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003838 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003839 break;
3840 case inactive:
3841 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003842 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003843 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003844 else
NeilBrowne6910632008-02-06 01:39:51 -08003845 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003846 break;
3847 case suspended:
3848 break; /* not supported yet */
3849 case readonly:
3850 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003851 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003852 else {
3853 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003854 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003855 err = do_md_run(mddev);
3856 }
3857 break;
3858 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003859 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003860 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003861 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003862 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003863 err = restart_array(mddev);
3864 if (err == 0) {
3865 mddev->ro = 2;
3866 set_disk_ro(mddev->gendisk, 0);
3867 }
NeilBrown9e653b62006-06-26 00:27:58 -07003868 } else {
3869 mddev->ro = 2;
3870 err = do_md_run(mddev);
3871 }
3872 break;
3873 case clean:
3874 if (mddev->pers) {
3875 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003876 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003877 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003878 if (mddev->in_sync == 0) {
3879 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003880 if (mddev->safemode == 1)
3881 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003882 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003883 }
3884 err = 0;
3885 } else
3886 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003887 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003888 } else
3889 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003890 break;
3891 case active:
3892 if (mddev->pers) {
3893 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003894 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003895 wake_up(&mddev->sb_wait);
3896 err = 0;
3897 } else {
3898 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003899 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003900 err = do_md_run(mddev);
3901 }
3902 break;
3903 case write_pending:
3904 case active_idle:
3905 /* these cannot be set */
3906 break;
3907 }
NeilBrown67918752014-12-15 12:57:01 +11003908
3909 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11003910 if (mddev->hold_active == UNTIL_IOCTL)
3911 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003912 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003913 }
NeilBrown67918752014-12-15 12:57:01 +11003914 mddev_unlock(mddev);
3915 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07003916}
NeilBrown80ca3a42006-07-10 04:44:18 -07003917static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10003918__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003919
NeilBrown6d7ff7382006-01-06 00:21:16 -08003920static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003921max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003922 return sprintf(page, "%d\n",
3923 atomic_read(&mddev->max_corr_read_errors));
3924}
3925
3926static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003927max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003928{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003929 unsigned int n;
3930 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11003931
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003932 rv = kstrtouint(buf, 10, &n);
3933 if (rv < 0)
3934 return rv;
3935 atomic_set(&mddev->max_corr_read_errors, n);
3936 return len;
Robert Becker1e509152009-12-14 12:49:58 +11003937}
3938
3939static struct md_sysfs_entry max_corr_read_errors =
3940__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3941 max_corrected_read_errors_store);
3942
3943static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003944null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003945{
3946 return -EINVAL;
3947}
3948
3949static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003950new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003951{
3952 /* buf must be %d:%d\n? giving major and minor numbers */
3953 /* The new device is added to the array.
3954 * If the array has a persistent superblock, we read the
3955 * superblock to initialise info and check validity.
3956 * Otherwise, only checking done is that in bind_rdev_to_array,
3957 * which mainly checks size.
3958 */
3959 char *e;
3960 int major = simple_strtoul(buf, &e, 10);
3961 int minor;
3962 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003963 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003964 int err;
3965
3966 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3967 return -EINVAL;
3968 minor = simple_strtoul(e+1, &e, 10);
3969 if (*e && *e != '\n')
3970 return -EINVAL;
3971 dev = MKDEV(major, minor);
3972 if (major != MAJOR(dev) ||
3973 minor != MINOR(dev))
3974 return -EOVERFLOW;
3975
NeilBrown67918752014-12-15 12:57:01 +11003976 flush_workqueue(md_misc_wq);
3977
3978 err = mddev_lock(mddev);
3979 if (err)
3980 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003981 if (mddev->persistent) {
3982 rdev = md_import_device(dev, mddev->major_version,
3983 mddev->minor_version);
3984 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003985 struct md_rdev *rdev0
3986 = list_entry(mddev->disks.next,
3987 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003988 err = super_types[mddev->major_version]
3989 .load_super(rdev, rdev0, mddev->minor_version);
3990 if (err < 0)
3991 goto out;
3992 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003993 } else if (mddev->external)
3994 rdev = md_import_device(dev, -2, -1);
3995 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003996 rdev = md_import_device(dev, -1, -1);
3997
NeilBrown9a8c0fa2015-06-25 17:06:40 +10003998 if (IS_ERR(rdev)) {
3999 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004000 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004001 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004002 err = bind_rdev_to_array(rdev, mddev);
4003 out:
4004 if (err)
4005 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004006 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004007 return err ? err : len;
4008}
4009
4010static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004011__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004012
4013static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004014bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004015{
4016 char *end;
4017 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004018 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004019
NeilBrown67918752014-12-15 12:57:01 +11004020 err = mddev_lock(mddev);
4021 if (err)
4022 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004023 if (!mddev->bitmap)
4024 goto out;
4025 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4026 while (*buf) {
4027 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4028 if (buf == end) break;
4029 if (*end == '-') { /* range */
4030 buf = end + 1;
4031 end_chunk = simple_strtoul(buf, &end, 0);
4032 if (buf == end) break;
4033 }
4034 if (*end && !isspace(*end)) break;
4035 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004036 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004037 }
4038 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4039out:
NeilBrown67918752014-12-15 12:57:01 +11004040 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004041 return len;
4042}
4043
4044static struct md_sysfs_entry md_bitmap =
4045__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4046
4047static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004048size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004049{
Andre Noll58c0fed2009-03-31 14:33:13 +11004050 return sprintf(page, "%llu\n",
4051 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004052}
4053
NeilBrownfd01b882011-10-11 16:47:53 +11004054static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004055
4056static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004057size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004058{
4059 /* If array is inactive, we can reduce the component size, but
4060 * not increase it (except from 0).
4061 * If array is active, we can try an on-line resize
4062 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004063 sector_t sectors;
4064 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004065
Andre Noll58c0fed2009-03-31 14:33:13 +11004066 if (err < 0)
4067 return err;
NeilBrown67918752014-12-15 12:57:01 +11004068 err = mddev_lock(mddev);
4069 if (err)
4070 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004071 if (mddev->pers) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004072 if (mddev_is_clustered(mddev))
4073 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11004074 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004075 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004076 if (mddev_is_clustered(mddev))
4077 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004078 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004079 if (mddev->dev_sectors == 0 ||
4080 mddev->dev_sectors > sectors)
4081 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004082 else
4083 err = -ENOSPC;
4084 }
NeilBrown67918752014-12-15 12:57:01 +11004085 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004086 return err ? err : len;
4087}
4088
4089static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004090__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004091
Masanari Iida83f0d772012-10-30 00:18:08 +09004092/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004093 * This is one of
4094 * 'none' for arrays with no metadata (good luck...)
4095 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004096 * or N.M for internally known formats
4097 */
4098static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004099metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004100{
4101 if (mddev->persistent)
4102 return sprintf(page, "%d.%d\n",
4103 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004104 else if (mddev->external)
4105 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004106 else
4107 return sprintf(page, "none\n");
4108}
4109
4110static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004111metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004112{
4113 int major, minor;
4114 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004115 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004116 /* Changing the details of 'external' metadata is
4117 * always permitted. Otherwise there must be
4118 * no devices attached to the array.
4119 */
NeilBrown67918752014-12-15 12:57:01 +11004120
4121 err = mddev_lock(mddev);
4122 if (err)
4123 return err;
4124 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004125 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4126 ;
4127 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004128 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004129
NeilBrown67918752014-12-15 12:57:01 +11004130 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004131 if (cmd_match(buf, "none")) {
4132 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004133 mddev->external = 0;
4134 mddev->major_version = 0;
4135 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004136 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004137 }
4138 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004139 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004140 if (namelen >= sizeof(mddev->metadata_type))
4141 namelen = sizeof(mddev->metadata_type)-1;
4142 strncpy(mddev->metadata_type, buf+9, namelen);
4143 mddev->metadata_type[namelen] = 0;
4144 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4145 mddev->metadata_type[--namelen] = 0;
4146 mddev->persistent = 0;
4147 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004148 mddev->major_version = 0;
4149 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004150 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004151 }
4152 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004153 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004154 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004155 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004156 buf = e+1;
4157 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004158 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004159 goto out_unlock;
4160 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004161 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004162 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004163 mddev->major_version = major;
4164 mddev->minor_version = minor;
4165 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004166 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004167 err = 0;
4168out_unlock:
4169 mddev_unlock(mddev);
4170 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004171}
4172
4173static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004174__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004175
NeilBrowna35b0d62006-01-06 00:20:49 -08004176static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004177action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004178{
NeilBrown7eec3142005-11-08 21:39:44 -08004179 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004180 unsigned long recovery = mddev->recovery;
4181 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004182 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004183 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4184 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4185 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004186 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004187 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4188 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004189 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004190 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004191 type = "check";
4192 else
4193 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004194 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004195 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004196 else if (mddev->reshape_position != MaxSector)
4197 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004198 }
4199 return sprintf(page, "%s\n", type);
4200}
4201
4202static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004203action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004204{
NeilBrown7eec3142005-11-08 21:39:44 -08004205 if (!mddev->pers || !mddev->pers->sync_request)
4206 return -EINVAL;
4207
NeilBrownb6a9ce62009-05-26 09:41:17 +10004208
4209 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004210 if (cmd_match(page, "frozen"))
4211 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4212 else
4213 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004214 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4215 mddev_lock(mddev) == 0) {
4216 flush_workqueue(md_misc_wq);
4217 if (mddev->sync_thread) {
4218 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004219 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004220 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004221 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004222 }
NeilBrown03c902e2006-01-06 00:20:46 -08004223 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4224 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004225 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004226 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004227 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004228 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004229 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004230 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004231 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004232 int err;
4233 if (mddev->pers->start_reshape == NULL)
4234 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004235 err = mddev_lock(mddev);
4236 if (!err) {
NeilBrown56ccc112015-05-28 17:53:29 +10004237 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004238 err = mddev->pers->start_reshape(mddev);
4239 mddev_unlock(mddev);
4240 }
NeilBrown16484bf2006-03-27 01:18:13 -08004241 if (err)
4242 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004243 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004244 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004245 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004246 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004247 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004248 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004249 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004250 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4251 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004252 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004253 if (mddev->ro == 2) {
4254 /* A write to sync_action is enough to justify
4255 * canceling read-auto mode
4256 */
4257 mddev->ro = 0;
4258 md_wakeup_thread(mddev->sync_thread);
4259 }
NeilBrown03c902e2006-01-06 00:20:46 -08004260 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004261 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004262 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004263 return len;
4264}
4265
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004266static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004267__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004268
4269static ssize_t
4270last_sync_action_show(struct mddev *mddev, char *page)
4271{
4272 return sprintf(page, "%s\n", mddev->last_sync_action);
4273}
4274
4275static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4276
NeilBrown9d888832005-11-08 21:39:26 -08004277static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004278mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004279{
4280 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004281 (unsigned long long)
4282 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004283}
4284
NeilBrown80ca3a42006-07-10 04:44:18 -07004285static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004286
NeilBrown88202a02006-01-06 00:21:36 -08004287static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004288sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004289{
4290 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4291 mddev->sync_speed_min ? "local": "system");
4292}
4293
4294static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004295sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004296{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004297 unsigned int min;
4298 int rv;
4299
NeilBrown88202a02006-01-06 00:21:36 -08004300 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004301 min = 0;
4302 } else {
4303 rv = kstrtouint(buf, 10, &min);
4304 if (rv < 0)
4305 return rv;
4306 if (min == 0)
4307 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004308 }
NeilBrown88202a02006-01-06 00:21:36 -08004309 mddev->sync_speed_min = min;
4310 return len;
4311}
4312
4313static struct md_sysfs_entry md_sync_min =
4314__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4315
4316static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004317sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004318{
4319 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4320 mddev->sync_speed_max ? "local": "system");
4321}
4322
4323static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004324sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004325{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004326 unsigned int max;
4327 int rv;
4328
NeilBrown88202a02006-01-06 00:21:36 -08004329 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004330 max = 0;
4331 } else {
4332 rv = kstrtouint(buf, 10, &max);
4333 if (rv < 0)
4334 return rv;
4335 if (max == 0)
4336 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004337 }
NeilBrown88202a02006-01-06 00:21:36 -08004338 mddev->sync_speed_max = max;
4339 return len;
4340}
4341
4342static struct md_sysfs_entry md_sync_max =
4343__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4344
Iustin Popd7f3d292007-10-16 23:30:54 -07004345static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004346degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004347{
4348 return sprintf(page, "%d\n", mddev->degraded);
4349}
4350static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004351
4352static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004353sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004354{
4355 return sprintf(page, "%d\n", mddev->parallel_resync);
4356}
4357
4358static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004359sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004360{
4361 long n;
4362
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004363 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004364 return -EINVAL;
4365
4366 if (n != 0 && n != 1)
4367 return -EINVAL;
4368
4369 mddev->parallel_resync = n;
4370
4371 if (mddev->sync_thread)
4372 wake_up(&resync_wait);
4373
4374 return len;
4375}
4376
4377/* force parallel resync, even with shared block devices */
4378static struct md_sysfs_entry md_sync_force_parallel =
4379__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4380 sync_force_parallel_show, sync_force_parallel_store);
4381
4382static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004383sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004384{
4385 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004386 if (mddev->curr_resync == 0)
4387 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004388 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4389 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004390 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004391 db = resync - mddev->resync_mark_cnt;
4392 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004393}
4394
NeilBrown80ca3a42006-07-10 04:44:18 -07004395static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004396
4397static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004398sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004399{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004400 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004401
NeilBrownacb180b2009-04-14 16:28:34 +10004402 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4403 return sprintf(page, "none\n");
4404
NeilBrown72f36d52012-10-11 14:25:57 +11004405 if (mddev->curr_resync == 1 ||
4406 mddev->curr_resync == 2)
4407 return sprintf(page, "delayed\n");
4408
NeilBrownc804cde2012-05-21 09:28:33 +10004409 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4410 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004411 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004412 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004413 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004414
NeilBrownacb180b2009-04-14 16:28:34 +10004415 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004416 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004417}
4418
NeilBrown750f1992014-09-30 08:53:05 +10004419static struct md_sysfs_entry md_sync_completed =
4420 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004421
NeilBrowne464eaf2006-03-27 01:18:14 -08004422static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004423min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004424{
4425 return sprintf(page, "%llu\n",
4426 (unsigned long long)mddev->resync_min);
4427}
4428static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004429min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004430{
4431 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004432 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004433
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004434 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004435 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004436
4437 spin_lock(&mddev->lock);
4438 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004439 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004440 goto out_unlock;
4441
4442 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004443 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004444 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004445
NeilBrown50c37b12015-03-23 17:36:38 +11004446 /* Round down to multiple of 4K for safety */
4447 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004448 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004449
NeilBrown23da4222014-12-15 12:57:01 +11004450out_unlock:
4451 spin_unlock(&mddev->lock);
4452 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004453}
4454
4455static struct md_sysfs_entry md_min_sync =
4456__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4457
4458static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004459max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004460{
4461 if (mddev->resync_max == MaxSector)
4462 return sprintf(page, "max\n");
4463 else
4464 return sprintf(page, "%llu\n",
4465 (unsigned long long)mddev->resync_max);
4466}
4467static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004468max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004469{
NeilBrown23da4222014-12-15 12:57:01 +11004470 int err;
4471 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004472 if (strncmp(buf, "max", 3) == 0)
4473 mddev->resync_max = MaxSector;
4474 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004475 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004476 int chunk;
4477
4478 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004479 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004480 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004481 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004482 goto out_unlock;
4483
4484 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004485 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004486 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004487 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004488 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004489
4490 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004491 chunk = mddev->chunk_sectors;
4492 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004493 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004494
4495 err = -EINVAL;
4496 if (sector_div(temp, chunk))
4497 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004498 }
4499 mddev->resync_max = max;
4500 }
4501 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004502 err = 0;
4503out_unlock:
4504 spin_unlock(&mddev->lock);
4505 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004506}
4507
4508static struct md_sysfs_entry md_max_sync =
4509__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4510
4511static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004512suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004513{
4514 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4515}
4516
4517static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004518suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004519{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004520 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004521 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004522
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004523 err = kstrtoull(buf, 10, &new);
4524 if (err < 0)
4525 return err;
4526 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004527 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004528
NeilBrown67918752014-12-15 12:57:01 +11004529 err = mddev_lock(mddev);
4530 if (err)
4531 return err;
4532 err = -EINVAL;
4533 if (mddev->pers == NULL ||
4534 mddev->pers->quiesce == NULL)
4535 goto unlock;
4536 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004537 mddev->suspend_lo = new;
4538 if (new >= old)
4539 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004540 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004541 else {
4542 /* Expanding suspended region - need to wait */
4543 mddev->pers->quiesce(mddev, 1);
4544 mddev->pers->quiesce(mddev, 0);
4545 }
NeilBrown67918752014-12-15 12:57:01 +11004546 err = 0;
4547unlock:
4548 mddev_unlock(mddev);
4549 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004550}
4551static struct md_sysfs_entry md_suspend_lo =
4552__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4553
NeilBrowne464eaf2006-03-27 01:18:14 -08004554static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004555suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004556{
4557 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4558}
4559
4560static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004561suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004562{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004563 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004564 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004565
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004566 err = kstrtoull(buf, 10, &new);
4567 if (err < 0)
4568 return err;
4569 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004570 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004571
NeilBrown67918752014-12-15 12:57:01 +11004572 err = mddev_lock(mddev);
4573 if (err)
4574 return err;
4575 err = -EINVAL;
4576 if (mddev->pers == NULL ||
4577 mddev->pers->quiesce == NULL)
4578 goto unlock;
4579 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004580 mddev->suspend_hi = new;
4581 if (new <= old)
4582 /* Shrinking suspended region */
4583 mddev->pers->quiesce(mddev, 2);
4584 else {
4585 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004586 mddev->pers->quiesce(mddev, 1);
4587 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004588 }
NeilBrown67918752014-12-15 12:57:01 +11004589 err = 0;
4590unlock:
4591 mddev_unlock(mddev);
4592 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004593}
4594static struct md_sysfs_entry md_suspend_hi =
4595__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4596
NeilBrown08a02ec2007-05-09 02:35:38 -07004597static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004598reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004599{
4600 if (mddev->reshape_position != MaxSector)
4601 return sprintf(page, "%llu\n",
4602 (unsigned long long)mddev->reshape_position);
4603 strcpy(page, "none\n");
4604 return 5;
4605}
4606
4607static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004608reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004609{
NeilBrownc6563a82012-05-21 09:27:00 +10004610 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004611 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004612 int err;
NeilBrown67918752014-12-15 12:57:01 +11004613
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004614 err = kstrtoull(buf, 10, &new);
4615 if (err < 0)
4616 return err;
4617 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004618 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004619 err = mddev_lock(mddev);
4620 if (err)
4621 return err;
4622 err = -EBUSY;
4623 if (mddev->pers)
4624 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004625 mddev->reshape_position = new;
4626 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004627 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004628 mddev->new_level = mddev->level;
4629 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004630 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004631 rdev_for_each(rdev, mddev)
4632 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004633 err = 0;
4634unlock:
4635 mddev_unlock(mddev);
4636 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004637}
4638
4639static struct md_sysfs_entry md_reshape_position =
4640__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4641 reshape_position_store);
4642
Dan Williamsb522adc2009-03-31 15:00:31 +11004643static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004644reshape_direction_show(struct mddev *mddev, char *page)
4645{
4646 return sprintf(page, "%s\n",
4647 mddev->reshape_backwards ? "backwards" : "forwards");
4648}
4649
4650static ssize_t
4651reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4652{
4653 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004654 int err;
4655
NeilBrown2c810cd2012-05-21 09:27:00 +10004656 if (cmd_match(buf, "forwards"))
4657 backwards = 0;
4658 else if (cmd_match(buf, "backwards"))
4659 backwards = 1;
4660 else
4661 return -EINVAL;
4662 if (mddev->reshape_backwards == backwards)
4663 return len;
4664
NeilBrown67918752014-12-15 12:57:01 +11004665 err = mddev_lock(mddev);
4666 if (err)
4667 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004668 /* check if we are allowed to change */
4669 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004670 err = -EBUSY;
4671 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004672 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004673 err = -EINVAL;
4674 else
4675 mddev->reshape_backwards = backwards;
4676 mddev_unlock(mddev);
4677 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004678}
4679
4680static struct md_sysfs_entry md_reshape_direction =
4681__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4682 reshape_direction_store);
4683
4684static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004685array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004686{
4687 if (mddev->external_size)
4688 return sprintf(page, "%llu\n",
4689 (unsigned long long)mddev->array_sectors/2);
4690 else
4691 return sprintf(page, "default\n");
4692}
4693
4694static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004695array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004696{
4697 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004698 int err;
4699
4700 err = mddev_lock(mddev);
4701 if (err)
4702 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004703
4704 if (strncmp(buf, "default", 7) == 0) {
4705 if (mddev->pers)
4706 sectors = mddev->pers->size(mddev, 0, 0);
4707 else
4708 sectors = mddev->array_sectors;
4709
4710 mddev->external_size = 0;
4711 } else {
4712 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004713 err = -EINVAL;
4714 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4715 err = -E2BIG;
4716 else
4717 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004718 }
4719
NeilBrown67918752014-12-15 12:57:01 +11004720 if (!err) {
4721 mddev->array_sectors = sectors;
4722 if (mddev->pers) {
4723 set_capacity(mddev->gendisk, mddev->array_sectors);
4724 revalidate_disk(mddev->gendisk);
4725 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004726 }
NeilBrown67918752014-12-15 12:57:01 +11004727 mddev_unlock(mddev);
4728 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004729}
4730
4731static struct md_sysfs_entry md_array_size =
4732__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4733 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004734
NeilBrowneae17012005-11-08 21:39:23 -08004735static struct attribute *md_default_attrs[] = {
4736 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004737 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004738 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004739 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004740 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004741 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004742 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004743 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004744 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004745 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004746 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004747 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004748 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004749 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004750 NULL,
4751};
4752
4753static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004754 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004755 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004756 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004757 &md_sync_min.attr,
4758 &md_sync_max.attr,
4759 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004760 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004761 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004762 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004763 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004764 &md_suspend_lo.attr,
4765 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004766 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004767 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004768 NULL,
4769};
NeilBrown411036f2005-11-08 21:39:40 -08004770static struct attribute_group md_redundancy_group = {
4771 .name = NULL,
4772 .attrs = md_redundancy_attrs,
4773};
4774
NeilBrowneae17012005-11-08 21:39:23 -08004775static ssize_t
4776md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4777{
4778 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004779 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004780 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004781
4782 if (!entry->show)
4783 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004784 spin_lock(&all_mddevs_lock);
4785 if (list_empty(&mddev->all_mddevs)) {
4786 spin_unlock(&all_mddevs_lock);
4787 return -EBUSY;
4788 }
4789 mddev_get(mddev);
4790 spin_unlock(&all_mddevs_lock);
4791
NeilBrownb7b17c92014-12-15 12:56:59 +11004792 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004793 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004794 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004795}
4796
4797static ssize_t
4798md_attr_store(struct kobject *kobj, struct attribute *attr,
4799 const char *page, size_t length)
4800{
4801 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004802 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004803 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004804
4805 if (!entry->store)
4806 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004807 if (!capable(CAP_SYS_ADMIN))
4808 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004809 spin_lock(&all_mddevs_lock);
4810 if (list_empty(&mddev->all_mddevs)) {
4811 spin_unlock(&all_mddevs_lock);
4812 return -EBUSY;
4813 }
4814 mddev_get(mddev);
4815 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004816 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004817 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004818 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004819}
4820
4821static void md_free(struct kobject *ko)
4822{
NeilBrownfd01b882011-10-11 16:47:53 +11004823 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004824
4825 if (mddev->sysfs_state)
4826 sysfs_put(mddev->sysfs_state);
4827
NeilBrown6cd18e72015-04-27 14:12:22 +10004828 if (mddev->queue)
4829 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004830 if (mddev->gendisk) {
4831 del_gendisk(mddev->gendisk);
4832 put_disk(mddev->gendisk);
4833 }
NeilBrowna21d1502009-01-09 08:31:09 +11004834
NeilBrowneae17012005-11-08 21:39:23 -08004835 kfree(mddev);
4836}
4837
Emese Revfy52cf25d2010-01-19 02:58:23 +01004838static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004839 .show = md_attr_show,
4840 .store = md_attr_store,
4841};
4842static struct kobj_type md_ktype = {
4843 .release = md_free,
4844 .sysfs_ops = &md_sysfs_ops,
4845 .default_attrs = md_default_attrs,
4846};
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848int mdp_major = 0;
4849
Dan Williams5fd3a172009-03-04 00:57:25 -07004850static void mddev_delayed_delete(struct work_struct *ws)
4851{
NeilBrownfd01b882011-10-11 16:47:53 +11004852 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004853
NeilBrown43a70502009-12-14 12:49:55 +11004854 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004855 kobject_del(&mddev->kobj);
4856 kobject_put(&mddev->kobj);
4857}
4858
NeilBrownefeb53c2009-01-09 08:31:10 +11004859static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004861 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004862 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004864 int partitioned;
4865 int shift;
4866 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004867 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
4869 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004870 return -ENODEV;
4871
4872 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4873 shift = partitioned ? MdpMinorShift : 0;
4874 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Tejun Heoe804ac72010-10-15 15:36:08 +02004876 /* wait for any previous instance of this device to be
4877 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004878 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004879 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004880
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004881 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004882 error = -EEXIST;
4883 if (mddev->gendisk)
4884 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004885
4886 if (name) {
4887 /* Need to ensure that 'name' is not a duplicate.
4888 */
NeilBrownfd01b882011-10-11 16:47:53 +11004889 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004890 spin_lock(&all_mddevs_lock);
4891
4892 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4893 if (mddev2->gendisk &&
4894 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4895 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004896 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004897 }
4898 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 }
NeilBrown8b765392009-01-09 08:31:08 +11004900
NeilBrown0909dc42009-07-01 12:27:21 +10004901 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004902 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004903 if (!mddev->queue)
4904 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004905 mddev->queue->queuedata = mddev;
4906
NeilBrown409c57f2009-03-31 14:39:39 +11004907 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004908 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004909
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 disk = alloc_disk(1 << shift);
4911 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004912 blk_cleanup_queue(mddev->queue);
4913 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004914 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004916 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004918 if (name)
4919 strcpy(disk->disk_name, name);
4920 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004922 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 disk->fops = &md_fops;
4925 disk->private_data = mddev;
4926 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004927 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004928 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004929 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004930 * remove it now.
4931 */
4932 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004934 /* As soon as we call add_disk(), another thread could get
4935 * through to md_open, so make sure it doesn't get too far
4936 */
4937 mutex_lock(&mddev->open_mutex);
4938 add_disk(disk);
4939
Tejun Heoed9e1982008-08-25 19:56:05 +09004940 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4941 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004942 if (error) {
4943 /* This isn't possible, but as kobject_init_and_add is marked
4944 * __must_check, we must do something with the result
4945 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004946 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4947 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004948 error = 0;
4949 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004950 if (mddev->kobj.sd &&
4951 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004952 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004953 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004954 abort:
4955 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004956 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004957 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004958 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004959 }
NeilBrownd3374822009-01-09 08:31:10 +11004960 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004961 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004962}
4963
4964static struct kobject *md_probe(dev_t dev, int *part, void *data)
4965{
4966 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 return NULL;
4968}
4969
NeilBrownefeb53c2009-01-09 08:31:10 +11004970static int add_named_array(const char *val, struct kernel_param *kp)
4971{
4972 /* val must be "md_*" where * is not all digits.
4973 * We allocate an array with a large free minor number, and
4974 * set the name to val. val must not already be an active name.
4975 */
4976 int len = strlen(val);
4977 char buf[DISK_NAME_LEN];
4978
4979 while (len && val[len-1] == '\n')
4980 len--;
4981 if (len >= DISK_NAME_LEN)
4982 return -E2BIG;
4983 strlcpy(buf, val, len+1);
4984 if (strncmp(buf, "md_", 3) != 0)
4985 return -EINVAL;
4986 return md_alloc(0, buf);
4987}
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989static void md_safemode_timeout(unsigned long data)
4990{
NeilBrownfd01b882011-10-11 16:47:53 +11004991 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Neil Brown0fd62b82008-06-28 08:31:36 +10004993 if (!atomic_read(&mddev->writes_pending)) {
4994 mddev->safemode = 1;
4995 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004996 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 md_wakeup_thread(mddev->thread);
4999}
5000
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005001static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
NeilBrownfd01b882011-10-11 16:47:53 +11005003int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004{
NeilBrown2604b702006-01-06 00:20:36 -08005005 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005006 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005007 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
NeilBrowna757e642005-04-16 15:26:42 -07005009 if (list_empty(&mddev->disks))
5010 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
5013 if (mddev->pers)
5014 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005015 /* Cannot run until previous stop completes properly */
5016 if (mddev->sysfs_active)
5017 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 /*
5020 * Analyze all RAID superblock(s)
5021 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005022 if (!mddev->raid_disks) {
5023 if (!mddev->persistent)
5024 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005025 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
NeilBrownd9d166c2006-01-06 00:20:51 -08005028 if (mddev->level != LEVEL_NONE)
5029 request_module("md-level-%d", mddev->level);
5030 else if (mddev->clevel[0])
5031 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
5033 /*
5034 * Drop all container device buffers, from now on
5035 * the only valid external interface is through the md
5036 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005038 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005039 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 continue;
5041 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005042 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005043
5044 /* perform some consistency tests on the device.
5045 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005046 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005047 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005048 if (rdev->meta_bdev) {
5049 /* Nothing to check */;
5050 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005051 if (mddev->dev_sectors &&
5052 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005053 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005054 printk("md: %s: data overlaps metadata\n",
5055 mdname(mddev));
5056 return -EINVAL;
5057 }
5058 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005059 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005060 > rdev->data_offset) {
5061 printk("md: %s: metadata overlaps data\n",
5062 mdname(mddev));
5063 return -EINVAL;
5064 }
5065 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005066 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 }
5068
NeilBrowna167f662010-10-26 18:31:13 +11005069 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005070 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005073 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005074 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005076 if (mddev->level != LEVEL_NONE)
5077 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5078 mddev->level);
5079 else
5080 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5081 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 return -EINVAL;
5083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005085 if (mddev->level != pers->level) {
5086 mddev->level = pers->level;
5087 mddev->new_level = pers->level;
5088 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005089 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
NeilBrownf6705572006-03-27 01:18:11 -08005091 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005092 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005093 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005094 module_put(pers->owner);
5095 return -EINVAL;
5096 }
5097
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005098 if (pers->sync_request) {
5099 /* Warn if this is a potentially silly
5100 * configuration.
5101 */
5102 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005103 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005104 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005105
NeilBrowndafb20f2012-03-19 12:46:39 +11005106 rdev_for_each(rdev, mddev)
5107 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005108 if (rdev < rdev2 &&
5109 rdev->bdev->bd_contains ==
5110 rdev2->bdev->bd_contains) {
5111 printk(KERN_WARNING
5112 "%s: WARNING: %s appears to be"
5113 " on the same physical disk as"
5114 " %s.\n",
5115 mdname(mddev),
5116 bdevname(rdev->bdev,b),
5117 bdevname(rdev2->bdev,b2));
5118 warned = 1;
5119 }
5120 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005121
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005122 if (warned)
5123 printk(KERN_WARNING
5124 "True protection against single-disk"
5125 " failure might be compromised.\n");
5126 }
5127
NeilBrown657390d2005-08-26 18:34:16 -07005128 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005129 /* may be over-ridden by personality */
5130 mddev->resync_max_sectors = mddev->dev_sectors;
5131
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005132 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
NeilBrown0f9552b52009-12-30 12:08:50 +11005134 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005135 mddev->ro = 2; /* read-only, but switch on first write */
5136
NeilBrown36d091f2014-12-15 12:56:58 +11005137 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005138 if (err)
5139 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005140 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005141 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5142 " but 'external_size' not in effect?\n", __func__);
5143 printk(KERN_ERR
5144 "md: invalid array_size %llu > default size %llu\n",
5145 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005146 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005147 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005148 }
NeilBrown36d091f2014-12-15 12:56:58 +11005149 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005150 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005151 struct bitmap *bitmap;
5152
5153 bitmap = bitmap_create(mddev, -1);
5154 if (IS_ERR(bitmap)) {
5155 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005156 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5157 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005158 } else
5159 mddev->bitmap = bitmap;
5160
NeilBrownb15c2e52006-01-06 00:20:16 -08005161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005163 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005164 if (mddev->private)
5165 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005166 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005167 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005168 bitmap_destroy(mddev);
5169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 }
NeilBrown5c675f82014-12-15 12:56:56 +11005171 if (mddev->queue) {
5172 mddev->queue->backing_dev_info.congested_data = mddev;
5173 mddev->queue->backing_dev_info.congested_fn = md_congested;
5174 }
NeilBrown36d091f2014-12-15 12:56:58 +11005175 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005176 if (mddev->kobj.sd &&
5177 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005178 printk(KERN_WARNING
5179 "md: cannot register extra attributes for %s\n",
5180 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005181 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005182 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005183 mddev->ro = 0;
5184
NeilBrownf72ffdd2014-09-30 14:23:59 +10005185 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005186 atomic_set(&mddev->max_corr_read_errors,
5187 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 mddev->safemode = 0;
NeilBrown16f17b32006-06-26 00:27:37 -07005189 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005191 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005192 spin_lock(&mddev->lock);
5193 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005194 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005195 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005196 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005197 if (rdev->raid_disk >= 0)
5198 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005199 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005200
NeilBrowna4a3d262015-07-17 11:57:30 +10005201 if (mddev->degraded && !mddev->ro)
5202 /* This ensures that recovering status is reported immediately
5203 * via sysfs - until a lack of spares is confirmed.
5204 */
5205 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005207
NeilBrown7a0a5352013-08-27 16:28:23 +10005208 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005209 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
NeilBrownd7603b72006-01-06 00:20:30 -08005211 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005212 sysfs_notify_dirent_safe(mddev->sysfs_state);
5213 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005214 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 return 0;
5216}
NeilBrown390ee602010-06-01 19:37:27 +10005217EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
NeilBrownfd01b882011-10-11 16:47:53 +11005219static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005220{
5221 int err;
5222
5223 err = md_run(mddev);
5224 if (err)
5225 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005226 err = bitmap_load(mddev);
5227 if (err) {
5228 bitmap_destroy(mddev);
5229 goto out;
5230 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005231
5232 md_wakeup_thread(mddev->thread);
5233 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5234
NeilBrownfe60b012010-03-29 11:10:42 +11005235 set_capacity(mddev->gendisk, mddev->array_sectors);
5236 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005237 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005238 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5239out:
5240 return err;
5241}
5242
NeilBrownfd01b882011-10-11 16:47:53 +11005243static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244{
5245 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
Andre Noll80fab1d2008-07-11 22:02:21 +10005247 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005249 return -ENXIO;
5250 if (!mddev->pers)
5251 return -EINVAL;
5252 if (!mddev->ro)
5253 return -EBUSY;
5254 mddev->safemode = 0;
5255 mddev->ro = 0;
5256 set_disk_ro(disk, 0);
5257 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5258 mdname(mddev));
5259 /* Kick recovery or resync if necessary */
5260 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5261 md_wakeup_thread(mddev->thread);
5262 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005263 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265}
5266
NeilBrownfd01b882011-10-11 16:47:53 +11005267static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005268{
5269 mddev->array_sectors = 0;
5270 mddev->external_size = 0;
5271 mddev->dev_sectors = 0;
5272 mddev->raid_disks = 0;
5273 mddev->recovery_cp = 0;
5274 mddev->resync_min = 0;
5275 mddev->resync_max = MaxSector;
5276 mddev->reshape_position = MaxSector;
5277 mddev->external = 0;
5278 mddev->persistent = 0;
5279 mddev->level = LEVEL_NONE;
5280 mddev->clevel[0] = 0;
5281 mddev->flags = 0;
5282 mddev->ro = 0;
5283 mddev->metadata_type[0] = 0;
5284 mddev->chunk_sectors = 0;
5285 mddev->ctime = mddev->utime = 0;
5286 mddev->layout = 0;
5287 mddev->max_disks = 0;
5288 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005289 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005290 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005291 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005292 mddev->new_level = LEVEL_NONE;
5293 mddev->new_layout = 0;
5294 mddev->new_chunk_sectors = 0;
5295 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005296 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005297 mddev->suspend_lo = mddev->suspend_hi = 0;
5298 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5299 mddev->recovery = 0;
5300 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005301 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005302 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005303 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005304 mddev->private = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005305 mddev->bitmap_info.offset = 0;
5306 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005307 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005308 mddev->bitmap_info.chunksize = 0;
5309 mddev->bitmap_info.daemon_sleep = 0;
5310 mddev->bitmap_info.max_write_behind = 0;
5311}
5312
NeilBrownfd01b882011-10-11 16:47:53 +11005313static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005314{
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005315 if (mddev_is_clustered(mddev))
5316 md_cluster_ops->metadata_update_start(mddev);
NeilBrown6b6204e2013-05-09 09:48:30 +10005317 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005318 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005319 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005320 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005321 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005322 }
5323
5324 del_timer_sync(&mddev->safemode_timer);
5325
5326 bitmap_flush(mddev);
5327 md_super_wait(mddev);
5328
NeilBrownb6d428c2013-04-24 11:42:42 +10005329 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005330 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005331 /* mark array as shutdown cleanly */
5332 mddev->in_sync = 1;
5333 md_update_sb(mddev, 1);
5334 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005335 if (mddev_is_clustered(mddev))
5336 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005337}
NeilBrowndefad612011-01-14 09:14:33 +11005338
NeilBrownfd01b882011-10-11 16:47:53 +11005339void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005340{
NeilBrown29f097c2013-11-14 17:54:51 +11005341 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005342 __md_stop_writes(mddev);
5343 mddev_unlock(mddev);
5344}
NeilBrown390ee602010-06-01 19:37:27 +10005345EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005346
NeilBrown5aa61f42014-12-15 12:56:57 +11005347static void mddev_detach(struct mddev *mddev)
5348{
5349 struct bitmap *bitmap = mddev->bitmap;
5350 /* wait for behind writes to complete */
5351 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5352 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5353 mdname(mddev));
5354 /* need to kick something here to make sure I/O goes? */
5355 wait_event(bitmap->behind_wait,
5356 atomic_read(&bitmap->behind_writes) == 0);
5357 }
NeilBrown36d091f2014-12-15 12:56:58 +11005358 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005359 mddev->pers->quiesce(mddev, 1);
5360 mddev->pers->quiesce(mddev, 0);
5361 }
5362 md_unregister_thread(&mddev->thread);
5363 if (mddev->queue)
5364 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5365}
5366
NeilBrown5eff3c42012-11-19 10:47:48 +11005367static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005368{
NeilBrown36d091f2014-12-15 12:56:58 +11005369 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005370 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005371 /* Ensure ->event_work is done */
5372 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005373 spin_lock(&mddev->lock);
5374 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005375 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005376 spin_unlock(&mddev->lock);
5377 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005378 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005379 if (pers->sync_request && mddev->to_remove == NULL)
5380 mddev->to_remove = &md_redundancy_group;
5381 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005382 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005383}
NeilBrown5eff3c42012-11-19 10:47:48 +11005384
5385void md_stop(struct mddev *mddev)
5386{
5387 /* stop the array and free an attached data structures.
5388 * This is called from dm-raid
5389 */
5390 __md_stop(mddev);
5391 bitmap_destroy(mddev);
5392 if (mddev->bio_set)
5393 bioset_free(mddev->bio_set);
5394}
5395
NeilBrown390ee602010-06-01 19:37:27 +10005396EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005397
NeilBrowna05b7ea2012-07-19 15:59:18 +10005398static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005399{
5400 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005401 int did_freeze = 0;
5402
5403 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5404 did_freeze = 1;
5405 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5406 md_wakeup_thread(mddev->thread);
5407 }
NeilBrownf851b602014-12-11 10:02:10 +11005408 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005409 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005410 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005411 /* Thread might be blocked waiting for metadata update
5412 * which will now never happen */
5413 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005414
NeilBrown88724bf2015-09-24 14:00:51 +10005415 if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags))
5416 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11005417 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005418 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5419 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10005420 wait_event(mddev->sb_wait,
5421 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11005422 mddev_lock_nointr(mddev);
5423
NeilBrowna4bd82d2010-03-29 13:23:10 +11005424 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005425 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005426 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005427 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005428 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005429 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005430 if (did_freeze) {
5431 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005432 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005433 md_wakeup_thread(mddev->thread);
5434 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005435 err = -EBUSY;
5436 goto out;
5437 }
5438 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005439 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005440
5441 err = -ENXIO;
5442 if (mddev->ro==1)
5443 goto out;
5444 mddev->ro = 1;
5445 set_disk_ro(mddev->gendisk, 1);
5446 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005447 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5448 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005449 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005450 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005451 }
5452out:
5453 mutex_unlock(&mddev->open_mutex);
5454 return err;
5455}
5456
NeilBrown9e653b62006-06-26 00:27:58 -07005457/* mode:
5458 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005459 * 2 - stop but do not disassemble array
5460 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005461static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005462 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005465 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005466 int did_freeze = 0;
5467
5468 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5469 did_freeze = 1;
5470 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5471 md_wakeup_thread(mddev->thread);
5472 }
NeilBrownf851b602014-12-11 10:02:10 +11005473 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005474 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005475 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005476 /* Thread might be blocked waiting for metadata update
5477 * which will now never happen */
5478 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005479
NeilBrown30b8feb2013-11-14 15:16:17 +11005480 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005481 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5482 !test_bit(MD_RECOVERY_RUNNING,
5483 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005484 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
NeilBrownc8c00a62009-08-10 12:50:52 +10005486 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->sysfs_active ||
5489 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005490 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005491 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005492 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005493 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005494 if (did_freeze) {
5495 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005496 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005497 md_wakeup_thread(mddev->thread);
5498 }
NeilBrown260fa032013-08-27 16:44:13 +10005499 return -EBUSY;
5500 }
NeilBrown6e17b022010-08-07 21:41:19 +10005501 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005502 if (mddev->ro)
5503 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005504
NeilBrowndefad612011-01-14 09:14:33 +11005505 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005506 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005507 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005508
NeilBrowna4bd82d2010-03-29 13:23:10 +11005509 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005510 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005511
NeilBrowndafb20f2012-03-19 12:46:39 +11005512 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005513 if (rdev->raid_disk >= 0)
5514 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005515
NeilBrowna4bd82d2010-03-29 13:23:10 +11005516 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005517 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005518 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005519 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005520
NeilBrowna4bd82d2010-03-29 13:23:10 +11005521 if (mddev->ro)
5522 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005523 } else
5524 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 /*
5526 * Free resources if final stop
5527 */
NeilBrown9e653b62006-06-26 00:27:58 -07005528 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5530
NeilBrown978f9462006-02-02 14:28:05 -08005531 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005532 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005533 struct file *f = mddev->bitmap_info.file;
5534 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005535 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005536 spin_unlock(&mddev->lock);
5537 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005538 }
NeilBrownc3d97142009-12-14 12:49:52 +11005539 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 export_array(mddev);
5542
NeilBrown6177b472010-03-29 11:37:13 +11005543 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005544 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005545 if (mddev->hold_active == UNTIL_STOP)
5546 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005547 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005548 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005549 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005550 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552}
5553
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005554#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005555static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556{
NeilBrown3cb03002011-10-11 16:45:26 +11005557 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 int err;
5559
NeilBrowna757e642005-04-16 15:26:42 -07005560 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562
5563 printk(KERN_INFO "md: running: ");
5564
NeilBrowndafb20f2012-03-19 12:46:39 +11005565 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 char b[BDEVNAME_SIZE];
5567 printk("<%s>", bdevname(rdev->bdev,b));
5568 }
5569 printk("\n");
5570
NeilBrownd710e132008-10-13 11:55:12 +11005571 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 if (err) {
5573 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005574 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 }
5576}
5577
5578/*
5579 * lets try to run arrays based on all disks that have arrived
5580 * until now. (those are in pending_raid_disks)
5581 *
5582 * the method: pick the first pending disk, collect all disks with
5583 * the same UUID, remove all from the pending list and put them into
5584 * the 'same_array' list. Then order this list based on superblock
5585 * update time (freshest comes first), kick out 'old' disks and
5586 * compare superblocks. If everything's fine then run it.
5587 *
5588 * If "unit" is allocated, then bump its reference count
5589 */
5590static void autorun_devices(int part)
5591{
NeilBrown3cb03002011-10-11 16:45:26 +11005592 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005593 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 char b[BDEVNAME_SIZE];
5595
5596 printk(KERN_INFO "md: autorun ...\n");
5597 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005598 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005600 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005602 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
5604 printk(KERN_INFO "md: considering %s ...\n",
5605 bdevname(rdev0->bdev,b));
5606 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005607 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 if (super_90_load(rdev, rdev0, 0) >= 0) {
5609 printk(KERN_INFO "md: adding %s ...\n",
5610 bdevname(rdev->bdev,b));
5611 list_move(&rdev->same_set, &candidates);
5612 }
5613 /*
5614 * now we have a set of devices, with all of them having
5615 * mostly sane superblocks. It's time to allocate the
5616 * mddev.
5617 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005618 if (part) {
5619 dev = MKDEV(mdp_major,
5620 rdev0->preferred_minor << MdpMinorShift);
5621 unit = MINOR(dev) >> MdpMinorShift;
5622 } else {
5623 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5624 unit = MINOR(dev);
5625 }
5626 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5628 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5629 break;
5630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
5632 md_probe(dev, NULL, NULL);
5633 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005634 if (!mddev || !mddev->gendisk) {
5635 if (mddev)
5636 mddev_put(mddev);
5637 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 "md: cannot allocate memory for md drive.\n");
5639 break;
5640 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005641 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 printk(KERN_WARNING "md: %s locked, cannot run\n",
5643 mdname(mddev));
5644 else if (mddev->raid_disks || mddev->major_version
5645 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005646 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 "md: %s already running, cannot run %s\n",
5648 mdname(mddev), bdevname(rdev0->bdev,b));
5649 mddev_unlock(mddev);
5650 } else {
5651 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005652 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005653 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 list_del_init(&rdev->same_set);
5655 if (bind_rdev_to_array(rdev, mddev))
5656 export_rdev(rdev);
5657 }
5658 autorun_array(mddev);
5659 mddev_unlock(mddev);
5660 }
5661 /* on success, candidates will be empty, on error
5662 * it won't...
5663 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005664 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005665 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 mddev_put(mddev);
5669 }
5670 printk(KERN_INFO "md: ... autorun DONE.\n");
5671}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005672#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
NeilBrownf72ffdd2014-09-30 14:23:59 +10005674static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
5676 mdu_version_t ver;
5677
5678 ver.major = MD_MAJOR_VERSION;
5679 ver.minor = MD_MINOR_VERSION;
5680 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5681
5682 if (copy_to_user(arg, &ver, sizeof(ver)))
5683 return -EFAULT;
5684
5685 return 0;
5686}
5687
NeilBrownf72ffdd2014-09-30 14:23:59 +10005688static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689{
5690 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005691 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005692 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
NeilBrown1ca69c42012-10-11 13:37:33 +11005694 nr = working = insync = failed = spare = 0;
5695 rcu_read_lock();
5696 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005698 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 failed++;
5700 else {
5701 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005702 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005703 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 else
5705 spare++;
5706 }
5707 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005708 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
5710 info.major_version = mddev->major_version;
5711 info.minor_version = mddev->minor_version;
5712 info.patch_version = MD_PATCHLEVEL_VERSION;
5713 info.ctime = mddev->ctime;
5714 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005715 info.size = mddev->dev_sectors / 2;
5716 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005717 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 info.nr_disks = nr;
5719 info.raid_disks = mddev->raid_disks;
5720 info.md_minor = mddev->md_minor;
5721 info.not_persistent= !mddev->persistent;
5722
5723 info.utime = mddev->utime;
5724 info.state = 0;
5725 if (mddev->in_sync)
5726 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005727 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005728 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005729 if (mddev_is_clustered(mddev))
5730 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005731 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 info.working_disks = working;
5733 info.failed_disks = failed;
5734 info.spare_disks = spare;
5735
5736 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005737 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
5739 if (copy_to_user(arg, &info, sizeof(info)))
5740 return -EFAULT;
5741
5742 return 0;
5743}
5744
NeilBrownf72ffdd2014-09-30 14:23:59 +10005745static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005746{
5747 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005748 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005749 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005750
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005751 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005752 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005753 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005754
NeilBrown32a76272005-06-21 17:17:14 -07005755 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005756 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005757 /* bitmap enabled */
5758 if (mddev->bitmap_info.file) {
5759 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5760 sizeof(file->pathname));
5761 if (IS_ERR(ptr))
5762 err = PTR_ERR(ptr);
5763 else
5764 memmove(file->pathname, ptr,
5765 sizeof(file->pathname)-(ptr-file->pathname));
5766 }
NeilBrown4af1a042014-12-15 12:57:00 +11005767 spin_unlock(&mddev->lock);
5768
5769 if (err == 0 &&
5770 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005771 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005772
NeilBrown32a76272005-06-21 17:17:14 -07005773 kfree(file);
5774 return err;
5775}
5776
NeilBrownf72ffdd2014-09-30 14:23:59 +10005777static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
5779 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005780 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
5782 if (copy_from_user(&info, arg, sizeof(info)))
5783 return -EFAULT;
5784
NeilBrown1ca69c42012-10-11 13:37:33 +11005785 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005786 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 if (rdev) {
5788 info.major = MAJOR(rdev->bdev->bd_dev);
5789 info.minor = MINOR(rdev->bdev->bd_dev);
5790 info.raid_disk = rdev->raid_disk;
5791 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005792 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005794 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 info.state |= (1<<MD_DISK_ACTIVE);
5796 info.state |= (1<<MD_DISK_SYNC);
5797 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005798 if (test_bit(WriteMostly, &rdev->flags))
5799 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 } else {
5801 info.major = info.minor = 0;
5802 info.raid_disk = -1;
5803 info.state = (1<<MD_DISK_REMOVED);
5804 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005805 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
5807 if (copy_to_user(arg, &info, sizeof(info)))
5808 return -EFAULT;
5809
5810 return 0;
5811}
5812
NeilBrownf72ffdd2014-09-30 14:23:59 +10005813static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814{
5815 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005816 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 dev_t dev = MKDEV(info->major,info->minor);
5818
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005819 if (mddev_is_clustered(mddev) &&
5820 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005821 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005822 mdname(mddev));
5823 return -EINVAL;
5824 }
5825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5827 return -EOVERFLOW;
5828
5829 if (!mddev->raid_disks) {
5830 int err;
5831 /* expecting a device which has a superblock */
5832 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5833 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005834 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 "md: md_import_device returned %ld\n",
5836 PTR_ERR(rdev));
5837 return PTR_ERR(rdev);
5838 }
5839 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005840 struct md_rdev *rdev0
5841 = list_entry(mddev->disks.next,
5842 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005843 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 .load_super(rdev, rdev0, mddev->minor_version);
5845 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005846 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005848 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 bdevname(rdev0->bdev,b2));
5850 export_rdev(rdev);
5851 return -EINVAL;
5852 }
5853 }
5854 err = bind_rdev_to_array(rdev, mddev);
5855 if (err)
5856 export_rdev(rdev);
5857 return err;
5858 }
5859
5860 /*
5861 * add_new_disk can be used once the array is assembled
5862 * to add "hot spares". They must already have a superblock
5863 * written
5864 */
5865 if (mddev->pers) {
5866 int err;
5867 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005868 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 "%s: personality does not support diskops!\n",
5870 mdname(mddev));
5871 return -EINVAL;
5872 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005873 if (mddev->persistent)
5874 rdev = md_import_device(dev, mddev->major_version,
5875 mddev->minor_version);
5876 else
5877 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005879 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 "md: md_import_device returned %ld\n",
5881 PTR_ERR(rdev));
5882 return PTR_ERR(rdev);
5883 }
NeilBrown1a855a02010-12-09 16:36:28 +11005884 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005885 if (!mddev->persistent) {
5886 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005887 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005888 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005889 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005890 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005891 } else
NeilBrown41158c72005-06-21 17:17:25 -07005892 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005893 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005894 } else
5895 super_types[mddev->major_version].
5896 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005897 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005898 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005899 /* This was a hot-add request, but events doesn't
5900 * match, so reject it.
5901 */
5902 export_rdev(rdev);
5903 return -EINVAL;
5904 }
5905
NeilBrownb2d444d2005-11-08 21:39:31 -08005906 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005907 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5908 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005909 else
5910 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005911
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005912 /*
5913 * check whether the device shows up in other nodes
5914 */
5915 if (mddev_is_clustered(mddev)) {
5916 if (info->state & (1 << MD_DISK_CANDIDATE)) {
5917 /* Through --cluster-confirm */
5918 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005919 err = md_cluster_ops->new_disk_ack(mddev, true);
5920 if (err) {
5921 export_rdev(rdev);
5922 return err;
5923 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005924 } else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
5925 /* --add initiated by this node */
5926 err = md_cluster_ops->add_new_disk_start(mddev, rdev);
5927 if (err) {
5928 md_cluster_ops->add_new_disk_finish(mddev);
5929 export_rdev(rdev);
5930 return err;
5931 }
5932 }
5933 }
5934
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 rdev->raid_disk = -1;
5936 err = bind_rdev_to_array(rdev, mddev);
5937 if (err)
5938 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005939 else
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05005940 err = add_bound_rdev(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005941 if (mddev_is_clustered(mddev) &&
5942 (info->state & (1 << MD_DISK_CLUSTER_ADD)))
5943 md_cluster_ops->add_new_disk_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 return err;
5945 }
5946
5947 /* otherwise, add_new_disk is only allowed
5948 * for major_version==0 superblocks
5949 */
5950 if (mddev->major_version != 0) {
5951 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5952 mdname(mddev));
5953 return -EINVAL;
5954 }
5955
5956 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5957 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005958 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005960 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 "md: error, md_import_device() returned %ld\n",
5962 PTR_ERR(rdev));
5963 return PTR_ERR(rdev);
5964 }
5965 rdev->desc_nr = info->number;
5966 if (info->raid_disk < mddev->raid_disks)
5967 rdev->raid_disk = info->raid_disk;
5968 else
5969 rdev->raid_disk = -1;
5970
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005972 if (info->state & (1<<MD_DISK_SYNC))
5973 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005975 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5976 set_bit(WriteMostly, &rdev->flags);
5977
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 if (!mddev->persistent) {
5979 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005980 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5981 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005982 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005983 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
NeilBrown2bf071b2006-01-06 00:20:55 -08005985 err = bind_rdev_to_array(rdev, mddev);
5986 if (err) {
5987 export_rdev(rdev);
5988 return err;
5989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 }
5991
5992 return 0;
5993}
5994
NeilBrownf72ffdd2014-09-30 14:23:59 +10005995static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996{
5997 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005998 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 rdev = find_rdev(mddev, dev);
6001 if (!rdev)
6002 return -ENXIO;
6003
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006004 if (mddev_is_clustered(mddev))
6005 md_cluster_ops->metadata_update_start(mddev);
6006
NeilBrown3ea8929d2013-04-24 11:42:41 +10006007 clear_bit(Blocked, &rdev->flags);
6008 remove_and_add_spares(mddev, rdev);
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 if (rdev->raid_disk >= 0)
6011 goto busy;
6012
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006013 if (mddev_is_clustered(mddev))
6014 md_cluster_ops->remove_disk(mddev, rdev);
6015
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006016 md_kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07006017 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006018 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006020 if (mddev_is_clustered(mddev))
6021 md_cluster_ops->metadata_update_finish(mddev);
6022
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 return 0;
6024busy:
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006025 if (mddev_is_clustered(mddev))
6026 md_cluster_ops->metadata_update_cancel(mddev);
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006027 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 bdevname(rdev->bdev,b), mdname(mddev));
6029 return -EBUSY;
6030}
6031
NeilBrownf72ffdd2014-09-30 14:23:59 +10006032static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
6034 char b[BDEVNAME_SIZE];
6035 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006036 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
6038 if (!mddev->pers)
6039 return -ENODEV;
6040
6041 if (mddev->major_version != 0) {
6042 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6043 " version-0 superblocks.\n",
6044 mdname(mddev));
6045 return -EINVAL;
6046 }
6047 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006048 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 "%s: personality does not support diskops!\n",
6050 mdname(mddev));
6051 return -EINVAL;
6052 }
6053
NeilBrownd710e132008-10-13 11:55:12 +11006054 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006056 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 "md: error, md_import_device() returned %ld\n",
6058 PTR_ERR(rdev));
6059 return -EINVAL;
6060 }
6061
6062 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006063 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006065 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
NeilBrown8190e752009-06-18 08:48:58 +10006067 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
NeilBrownb2d444d2005-11-08 21:39:31 -08006069 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006070 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 "md: can not hot-add faulty %s disk to %s!\n",
6072 bdevname(rdev->bdev,b), mdname(mddev));
6073 err = -EINVAL;
6074 goto abort_export;
6075 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006076
6077 if (mddev_is_clustered(mddev))
6078 md_cluster_ops->metadata_update_start(mddev);
NeilBrownb2d444d2005-11-08 21:39:31 -08006079 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006081 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006082 err = bind_rdev_to_array(rdev, mddev);
6083 if (err)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006084 goto abort_clustered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
6086 /*
6087 * The rest should better be atomic, we can have disk failures
6088 * noticed in interrupt contexts ...
6089 */
6090
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 rdev->raid_disk = -1;
6092
NeilBrown850b2b42006-10-03 01:15:46 -07006093 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006095 if (mddev_is_clustered(mddev))
6096 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 /*
6098 * Kick recovery, maybe this spare has to be added to the
6099 * array immediately.
6100 */
6101 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6102 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006103 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 return 0;
6105
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006106abort_clustered:
6107 if (mddev_is_clustered(mddev))
6108 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109abort_export:
6110 export_rdev(rdev);
6111 return err;
6112}
6113
NeilBrownfd01b882011-10-11 16:47:53 +11006114static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006115{
NeilBrown035328c2014-04-09 12:25:40 +10006116 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006117
NeilBrown36fa3062005-09-09 16:23:45 -07006118 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006119 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006120 return -EBUSY;
6121 if (mddev->recovery || mddev->sync_thread)
6122 return -EBUSY;
6123 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006124 }
6125
NeilBrown36fa3062005-09-09 16:23:45 -07006126 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006127 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006128 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006129
NeilBrown1e594bb2014-12-15 12:57:00 +11006130 if (mddev->bitmap || mddev->bitmap_info.file)
6131 return -EEXIST; /* cannot add when bitmap is present */
6132 f = fget(fd);
6133
6134 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006135 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6136 mdname(mddev));
6137 return -EBADF;
6138 }
6139
NeilBrown1e594bb2014-12-15 12:57:00 +11006140 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006141 if (!S_ISREG(inode->i_mode)) {
6142 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6143 mdname(mddev));
6144 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006145 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006146 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6147 mdname(mddev));
6148 err = -EBADF;
6149 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006150 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6151 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006152 err = -EBUSY;
6153 }
6154 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006155 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006156 return err;
6157 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006158 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006159 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006160 } else if (mddev->bitmap == NULL)
6161 return -ENOENT; /* cannot remove what isn't there */
6162 err = 0;
6163 if (mddev->pers) {
6164 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006165 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006166 struct bitmap *bitmap;
6167
6168 bitmap = bitmap_create(mddev, -1);
6169 if (!IS_ERR(bitmap)) {
6170 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006171 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006172 } else
6173 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006174 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006175 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006176 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006177 fd = -1; /* make sure to put the file */
6178 }
NeilBrown36fa3062005-09-09 16:23:45 -07006179 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006180 }
6181 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006182 struct file *f = mddev->bitmap_info.file;
6183 if (f) {
6184 spin_lock(&mddev->lock);
6185 mddev->bitmap_info.file = NULL;
6186 spin_unlock(&mddev->lock);
6187 fput(f);
6188 }
NeilBrown36fa3062005-09-09 16:23:45 -07006189 }
6190
NeilBrown32a76272005-06-21 17:17:14 -07006191 return err;
6192}
6193
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194/*
6195 * set_array_info is used two different ways
6196 * The original usage is when creating a new array.
6197 * In this usage, raid_disks is > 0 and it together with
6198 * level, size, not_persistent,layout,chunksize determine the
6199 * shape of the array.
6200 * This will always create an array with a type-0.90.0 superblock.
6201 * The newer usage is when assembling an array.
6202 * In this case raid_disks will be 0, and the major_version field is
6203 * use to determine which style super-blocks are to be found on the devices.
6204 * The minor and patch _version numbers are also kept incase the
6205 * super_block handler wishes to interpret them.
6206 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006207static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208{
6209
6210 if (info->raid_disks == 0) {
6211 /* just setting version number for superblock loading */
6212 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006213 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 super_types[info->major_version].name == NULL) {
6215 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006216 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 "md: superblock version %d not known\n",
6218 info->major_version);
6219 return -EINVAL;
6220 }
6221 mddev->major_version = info->major_version;
6222 mddev->minor_version = info->minor_version;
6223 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006224 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006225 /* ensure mddev_put doesn't delete this now that there
6226 * is some minimal configuration.
6227 */
6228 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 return 0;
6230 }
6231 mddev->major_version = MD_MAJOR_VERSION;
6232 mddev->minor_version = MD_MINOR_VERSION;
6233 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6234 mddev->ctime = get_seconds();
6235
6236 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006237 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006238 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 mddev->raid_disks = info->raid_disks;
6240 /* don't set md_minor, it is determined by which /dev/md* was
6241 * openned
6242 */
6243 if (info->state & (1<<MD_SB_CLEAN))
6244 mddev->recovery_cp = MaxSector;
6245 else
6246 mddev->recovery_cp = 0;
6247 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006248 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249
6250 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006251 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252
6253 mddev->max_disks = MD_SB_DISKS;
6254
NeilBrowne6910632008-02-06 01:39:51 -08006255 if (mddev->persistent)
6256 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006257 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
NeilBrownc3d97142009-12-14 12:49:52 +11006259 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006260 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006261 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006262
NeilBrownf6705572006-03-27 01:18:11 -08006263 mddev->reshape_position = MaxSector;
6264
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 /*
6266 * Generate a 128 bit UUID
6267 */
6268 get_random_bytes(mddev->uuid, 16);
6269
NeilBrownf6705572006-03-27 01:18:11 -08006270 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006271 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006272 mddev->new_layout = mddev->layout;
6273 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006274 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006275
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 return 0;
6277}
6278
NeilBrownfd01b882011-10-11 16:47:53 +11006279void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006280{
Dan Williamsb522adc2009-03-31 15:00:31 +11006281 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6282
6283 if (mddev->external_size)
6284 return;
6285
Dan Williams1f403622009-03-31 14:59:03 +11006286 mddev->array_sectors = array_sectors;
6287}
6288EXPORT_SYMBOL(md_set_array_sectors);
6289
NeilBrownfd01b882011-10-11 16:47:53 +11006290static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006291{
NeilBrown3cb03002011-10-11 16:45:26 +11006292 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006293 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006294 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006295
6296 if (mddev->pers->resize == NULL)
6297 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006298 /* The "num_sectors" is the number of sectors of each device that
6299 * is used. This can only make sense for arrays with redundancy.
6300 * linear and raid0 always use whatever space is available. We can only
6301 * consider changing this number if no resync or reconstruction is
6302 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006303 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006304 * of each device. If num_sectors is zero, we find the largest size
6305 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006306 */
NeilBrownf851b602014-12-11 10:02:10 +11006307 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6308 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006309 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006310 if (mddev->ro)
6311 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006312
NeilBrowndafb20f2012-03-19 12:46:39 +11006313 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006314 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006315
Andre Nolld71f9f82008-07-11 22:02:22 +10006316 if (fit && (num_sectors == 0 || num_sectors > avail))
6317 num_sectors = avail;
6318 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006319 return -ENOSPC;
6320 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006321 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006322 if (!rv)
6323 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006324 return rv;
6325}
6326
NeilBrownfd01b882011-10-11 16:47:53 +11006327static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006328{
6329 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006330 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006331 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006332 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006333 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006334 if (mddev->ro)
6335 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006336 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006337 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006338 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006339 if (mddev->sync_thread ||
6340 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6341 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006342 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006343
6344 rdev_for_each(rdev, mddev) {
6345 if (mddev->raid_disks < raid_disks &&
6346 rdev->data_offset < rdev->new_data_offset)
6347 return -EINVAL;
6348 if (mddev->raid_disks > raid_disks &&
6349 rdev->data_offset > rdev->new_data_offset)
6350 return -EINVAL;
6351 }
6352
NeilBrown63c70c42006-03-27 01:18:13 -08006353 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006354 if (mddev->delta_disks < 0)
6355 mddev->reshape_backwards = 1;
6356 else if (mddev->delta_disks > 0)
6357 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006358
6359 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006360 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006361 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006362 mddev->reshape_backwards = 0;
6363 }
NeilBrownda943b992006-01-06 00:20:54 -08006364 return rv;
6365}
6366
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367/*
6368 * update_array_info is used to change the configuration of an
6369 * on-line array.
6370 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6371 * fields in the info are checked against the array.
6372 * Any differences that cannot be handled will cause an error.
6373 * Normally, only one change can be managed at a time.
6374 */
NeilBrownfd01b882011-10-11 16:47:53 +11006375static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376{
6377 int rv = 0;
6378 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006379 int state = 0;
6380
6381 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006382 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006383 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384
6385 if (mddev->major_version != info->major_version ||
6386 mddev->minor_version != info->minor_version ||
6387/* mddev->patch_version != info->patch_version || */
6388 mddev->ctime != info->ctime ||
6389 mddev->level != info->level ||
6390/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006391 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006392 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006393 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6394 ((state^info->state) & 0xfffffe00)
6395 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 return -EINVAL;
6397 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006398 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6399 cnt++;
6400 if (mddev->raid_disks != info->raid_disks)
6401 cnt++;
6402 if (mddev->layout != info->layout)
6403 cnt++;
6404 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6405 cnt++;
6406 if (cnt == 0)
6407 return 0;
6408 if (cnt > 1)
6409 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410
6411 if (mddev->layout != info->layout) {
6412 /* Change layout
6413 * we don't need to do anything at the md level, the
6414 * personality will take care of it all.
6415 */
NeilBrown50ac1682009-06-18 08:47:55 +10006416 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006418 else {
6419 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006420 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006421 if (rv)
6422 mddev->new_layout = mddev->layout;
6423 return rv;
6424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006426 if (mddev_is_clustered(mddev))
6427 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11006428 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006429 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430
NeilBrownda943b992006-01-06 00:20:54 -08006431 if (mddev->raid_disks != info->raid_disks)
6432 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433
NeilBrown36fa3062005-09-09 16:23:45 -07006434 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006435 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6436 rv = -EINVAL;
6437 goto err;
6438 }
6439 if (mddev->recovery || mddev->sync_thread) {
6440 rv = -EBUSY;
6441 goto err;
6442 }
NeilBrown36fa3062005-09-09 16:23:45 -07006443 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006444 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006445 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006446 if (mddev->bitmap) {
6447 rv = -EEXIST;
6448 goto err;
6449 }
6450 if (mddev->bitmap_info.default_offset == 0) {
6451 rv = -EINVAL;
6452 goto err;
6453 }
NeilBrownc3d97142009-12-14 12:49:52 +11006454 mddev->bitmap_info.offset =
6455 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006456 mddev->bitmap_info.space =
6457 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006458 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006459 bitmap = bitmap_create(mddev, -1);
6460 if (!IS_ERR(bitmap)) {
6461 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006462 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006463 } else
6464 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006465 if (rv)
6466 bitmap_destroy(mddev);
6467 mddev->pers->quiesce(mddev, 0);
6468 } else {
6469 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006470 if (!mddev->bitmap) {
6471 rv = -ENOENT;
6472 goto err;
6473 }
6474 if (mddev->bitmap->storage.file) {
6475 rv = -EINVAL;
6476 goto err;
6477 }
NeilBrown36fa3062005-09-09 16:23:45 -07006478 mddev->pers->quiesce(mddev, 1);
6479 bitmap_destroy(mddev);
6480 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006481 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006482 }
6483 }
NeilBrown850b2b42006-10-03 01:15:46 -07006484 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006485 if (mddev_is_clustered(mddev))
6486 md_cluster_ops->metadata_update_finish(mddev);
6487 return rv;
6488err:
6489 if (mddev_is_clustered(mddev))
6490 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 return rv;
6492}
6493
NeilBrownfd01b882011-10-11 16:47:53 +11006494static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495{
NeilBrown3cb03002011-10-11 16:45:26 +11006496 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006497 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498
6499 if (mddev->pers == NULL)
6500 return -ENODEV;
6501
NeilBrown1ca69c42012-10-11 13:37:33 +11006502 rcu_read_lock();
6503 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006505 err = -ENODEV;
6506 else {
6507 md_error(mddev, rdev);
6508 if (!test_bit(Faulty, &rdev->flags))
6509 err = -EBUSY;
6510 }
6511 rcu_read_unlock();
6512 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513}
6514
Andre Noll2f9618c2008-04-25 18:57:58 +02006515/*
6516 * We have a problem here : there is no easy way to give a CHS
6517 * virtual geometry. We currently pretend that we have a 2 heads
6518 * 4 sectors (with a BIG number of cylinders...). This drives
6519 * dosfs just mad... ;-)
6520 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006521static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6522{
NeilBrownfd01b882011-10-11 16:47:53 +11006523 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006524
6525 geo->heads = 2;
6526 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006527 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006528 return 0;
6529}
6530
Nicolas Schichancb335f82014-01-15 16:58:52 +01006531static inline bool md_ioctl_valid(unsigned int cmd)
6532{
6533 switch (cmd) {
6534 case ADD_NEW_DISK:
6535 case BLKROSET:
6536 case GET_ARRAY_INFO:
6537 case GET_BITMAP_FILE:
6538 case GET_DISK_INFO:
6539 case HOT_ADD_DISK:
6540 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006541 case RAID_AUTORUN:
6542 case RAID_VERSION:
6543 case RESTART_ARRAY_RW:
6544 case RUN_ARRAY:
6545 case SET_ARRAY_INFO:
6546 case SET_BITMAP_FILE:
6547 case SET_DISK_FAULTY:
6548 case STOP_ARRAY:
6549 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006550 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006551 return true;
6552 default:
6553 return false;
6554 }
6555}
6556
Al Viroa39907f2008-03-02 10:31:15 -05006557static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 unsigned int cmd, unsigned long arg)
6559{
6560 int err = 0;
6561 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006562 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006563 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
Nicolas Schichancb335f82014-01-15 16:58:52 +01006565 if (!md_ioctl_valid(cmd))
6566 return -ENOTTY;
6567
NeilBrown506c9e42011-12-23 10:17:26 +11006568 switch (cmd) {
6569 case RAID_VERSION:
6570 case GET_ARRAY_INFO:
6571 case GET_DISK_INFO:
6572 break;
6573 default:
6574 if (!capable(CAP_SYS_ADMIN))
6575 return -EACCES;
6576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577
6578 /*
6579 * Commands dealing with the RAID driver but not any
6580 * particular array:
6581 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006582 switch (cmd) {
6583 case RAID_VERSION:
6584 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006585 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006588 case RAID_AUTORUN:
6589 err = 0;
6590 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006593 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 }
6595
6596 /*
6597 * Commands creating/starting a new array:
6598 */
6599
Al Viroa39907f2008-03-02 10:31:15 -05006600 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601
6602 if (!mddev) {
6603 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006604 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 }
6606
NeilBrown1ca69c42012-10-11 13:37:33 +11006607 /* Some actions do not requires the mutex */
6608 switch (cmd) {
6609 case GET_ARRAY_INFO:
6610 if (!mddev->raid_disks && !mddev->external)
6611 err = -ENODEV;
6612 else
6613 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006614 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006615
6616 case GET_DISK_INFO:
6617 if (!mddev->raid_disks && !mddev->external)
6618 err = -ENODEV;
6619 else
6620 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006621 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006622
6623 case SET_DISK_FAULTY:
6624 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006625 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006626
6627 case GET_BITMAP_FILE:
6628 err = get_bitmap_file(mddev, argp);
6629 goto out;
6630
NeilBrown1ca69c42012-10-11 13:37:33 +11006631 }
6632
NeilBrowna7a3f082012-12-11 13:35:54 +11006633 if (cmd == ADD_NEW_DISK)
6634 /* need to ensure md_delayed_delete() has completed */
6635 flush_workqueue(md_misc_wq);
6636
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006637 if (cmd == HOT_REMOVE_DISK)
6638 /* need to ensure recovery thread has run */
6639 wait_event_interruptible_timeout(mddev->sb_wait,
6640 !test_bit(MD_RECOVERY_NEEDED,
6641 &mddev->flags),
6642 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006643 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6644 /* Need to flush page cache, and ensure no-one else opens
6645 * and writes
6646 */
6647 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006648 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006649 mutex_unlock(&mddev->open_mutex);
6650 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006651 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006652 }
6653 set_bit(MD_STILL_CLOSED, &mddev->flags);
6654 mutex_unlock(&mddev->open_mutex);
6655 sync_blockdev(bdev);
6656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 err = mddev_lock(mddev);
6658 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006659 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6661 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006662 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 }
6664
NeilBrownc02c0ae2012-12-11 13:39:21 +11006665 if (cmd == SET_ARRAY_INFO) {
6666 mdu_array_info_t info;
6667 if (!arg)
6668 memset(&info, 0, sizeof(info));
6669 else if (copy_from_user(&info, argp, sizeof(info))) {
6670 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006671 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006672 }
6673 if (mddev->pers) {
6674 err = update_array_info(mddev, &info);
6675 if (err) {
6676 printk(KERN_WARNING "md: couldn't update"
6677 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006678 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006680 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006681 }
6682 if (!list_empty(&mddev->disks)) {
6683 printk(KERN_WARNING
6684 "md: array %s already has disks!\n",
6685 mdname(mddev));
6686 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006687 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006688 }
6689 if (mddev->raid_disks) {
6690 printk(KERN_WARNING
6691 "md: array %s already initialised!\n",
6692 mdname(mddev));
6693 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006694 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006695 }
6696 err = set_array_info(mddev, &info);
6697 if (err) {
6698 printk(KERN_WARNING "md: couldn't set"
6699 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006700 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006701 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006702 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 }
6704
6705 /*
6706 * Commands querying/configuring an existing array:
6707 */
NeilBrown32a76272005-06-21 17:17:14 -07006708 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006709 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006710 if ((!mddev->raid_disks && !mddev->external)
6711 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6712 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6713 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006715 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 }
6717
6718 /*
6719 * Commands even a read-only array can execute:
6720 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006721 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006722 case RESTART_ARRAY_RW:
6723 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006724 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006725
6726 case STOP_ARRAY:
6727 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006728 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006729
6730 case STOP_ARRAY_RO:
6731 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006732 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006733
NeilBrown3ea8929d2013-04-24 11:42:41 +10006734 case HOT_REMOVE_DISK:
6735 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006736 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006737
NeilBrown7ceb17e2013-04-24 11:42:42 +10006738 case ADD_NEW_DISK:
6739 /* We can support ADD_NEW_DISK on read-only arrays
6740 * on if we are re-adding a preexisting device.
6741 * So require mddev->pers and MD_DISK_SYNC.
6742 */
6743 if (mddev->pers) {
6744 mdu_disk_info_t info;
6745 if (copy_from_user(&info, argp, sizeof(info)))
6746 err = -EFAULT;
6747 else if (!(info.state & (1<<MD_DISK_SYNC)))
6748 /* Need to clear read-only for this */
6749 break;
6750 else
6751 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006752 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006753 }
6754 break;
6755
NeilBrownc02c0ae2012-12-11 13:39:21 +11006756 case BLKROSET:
6757 if (get_user(ro, (int __user *)(arg))) {
6758 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006759 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006760 }
6761 err = -EINVAL;
6762
6763 /* if the bdev is going readonly the value of mddev->ro
6764 * does not matter, no writes are coming
6765 */
6766 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006767 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006768
NeilBrownc02c0ae2012-12-11 13:39:21 +11006769 /* are we are already prepared for writes? */
6770 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006771 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006772
6773 /* transitioning to readauto need only happen for
6774 * arrays that call md_write_start
6775 */
6776 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006778 if (err == 0) {
6779 mddev->ro = 2;
6780 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006781 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006782 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006783 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 }
6785
6786 /*
6787 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006788 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 */
NeilBrown326eb172014-09-30 15:36:28 +10006790 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006791 if (mddev->ro == 2) {
6792 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006793 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006794 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006795 /* mddev_unlock will wake thread */
6796 /* If a device failed while we were read-only, we
6797 * need to make sure the metadata is updated now.
6798 */
6799 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6800 mddev_unlock(mddev);
6801 wait_event(mddev->sb_wait,
6802 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6803 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006804 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006805 }
NeilBrownf91de922005-11-08 21:39:36 -08006806 } else {
6807 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006808 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 }
6811
NeilBrownc02c0ae2012-12-11 13:39:21 +11006812 switch (cmd) {
6813 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006815 mdu_disk_info_t info;
6816 if (copy_from_user(&info, argp, sizeof(info)))
6817 err = -EFAULT;
6818 else
6819 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006820 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006823 case CLUSTERED_DISK_NACK:
6824 if (mddev_is_clustered(mddev))
6825 md_cluster_ops->new_disk_ack(mddev, false);
6826 else
6827 err = -EINVAL;
6828 goto unlock;
6829
NeilBrownc02c0ae2012-12-11 13:39:21 +11006830 case HOT_ADD_DISK:
6831 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006832 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
NeilBrownc02c0ae2012-12-11 13:39:21 +11006834 case RUN_ARRAY:
6835 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006836 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
NeilBrownc02c0ae2012-12-11 13:39:21 +11006838 case SET_BITMAP_FILE:
6839 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006840 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006841
NeilBrownc02c0ae2012-12-11 13:39:21 +11006842 default:
6843 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006844 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 }
6846
NeilBrown3adc28d2014-09-30 15:46:41 +10006847unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006848 if (mddev->hold_active == UNTIL_IOCTL &&
6849 err != -EINVAL)
6850 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006852out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 return err;
6854}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006855#ifdef CONFIG_COMPAT
6856static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6857 unsigned int cmd, unsigned long arg)
6858{
6859 switch (cmd) {
6860 case HOT_REMOVE_DISK:
6861 case HOT_ADD_DISK:
6862 case SET_DISK_FAULTY:
6863 case SET_BITMAP_FILE:
6864 /* These take in integer arg, do not convert */
6865 break;
6866 default:
6867 arg = (unsigned long)compat_ptr(arg);
6868 break;
6869 }
6870
6871 return md_ioctl(bdev, mode, cmd, arg);
6872}
6873#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874
Al Viroa39907f2008-03-02 10:31:15 -05006875static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876{
6877 /*
6878 * Succeed if we can lock the mddev, which confirms that
6879 * it isn't being stopped right now.
6880 */
NeilBrownfd01b882011-10-11 16:47:53 +11006881 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 int err;
6883
Yuanhan Liu0c098222012-05-22 13:55:32 +10006884 if (!mddev)
6885 return -ENODEV;
6886
NeilBrownd3374822009-01-09 08:31:10 +11006887 if (mddev->gendisk != bdev->bd_disk) {
6888 /* we are racing with mddev_put which is discarding this
6889 * bd_disk.
6890 */
6891 mddev_put(mddev);
6892 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006893 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006894 /* Then retry the open from the top */
6895 return -ERESTARTSYS;
6896 }
6897 BUG_ON(mddev != bdev->bd_disk->private_data);
6898
NeilBrownc8c00a62009-08-10 12:50:52 +10006899 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 goto out;
6901
6902 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006903 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006904 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006905 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006907 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 out:
6909 return err;
6910}
6911
Al Virodb2a1442013-05-05 21:52:57 -04006912static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006914 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006916 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006917 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006920
6921static int md_media_changed(struct gendisk *disk)
6922{
NeilBrownfd01b882011-10-11 16:47:53 +11006923 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006924
6925 return mddev->changed;
6926}
6927
6928static int md_revalidate(struct gendisk *disk)
6929{
NeilBrownfd01b882011-10-11 16:47:53 +11006930 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006931
6932 mddev->changed = 0;
6933 return 0;
6934}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006935static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936{
6937 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006938 .open = md_open,
6939 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006940 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006941#ifdef CONFIG_COMPAT
6942 .compat_ioctl = md_compat_ioctl,
6943#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006944 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006945 .media_changed = md_media_changed,
6946 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947};
6948
NeilBrownf72ffdd2014-09-30 14:23:59 +10006949static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950{
NeilBrown2b8bf342011-10-11 16:48:23 +11006951 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 /*
6954 * md_thread is a 'system-thread', it's priority should be very
6955 * high. We avoid resource deadlocks individually in each
6956 * raid personality. (RAID5 does preallocation) We also use RR and
6957 * the very same RT priority as kswapd, thus we will never get
6958 * into a priority inversion deadlock.
6959 *
6960 * we definitely have to have equal or higher priority than
6961 * bdflush, otherwise bdflush will deadlock if there are too
6962 * many dirty RAID5 blocks.
6963 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964
NeilBrown6985c432005-10-19 21:23:47 -07006965 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006966 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967
NeilBrown93588e22005-11-15 00:09:12 -08006968 /* We need to wait INTERRUPTIBLE so that
6969 * we don't add to the load-average.
6970 * That means we need to be sure no signals are
6971 * pending
6972 */
6973 if (signal_pending(current))
6974 flush_signals(current);
6975
6976 wait_event_interruptible_timeout
6977 (thread->wqueue,
6978 test_bit(THREAD_WAKEUP, &thread->flags)
6979 || kthread_should_stop(),
6980 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
NeilBrown6c987912011-01-14 09:13:53 +11006982 clear_bit(THREAD_WAKEUP, &thread->flags);
6983 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006984 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006986
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987 return 0;
6988}
6989
NeilBrown2b8bf342011-10-11 16:48:23 +11006990void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991{
6992 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006993 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 set_bit(THREAD_WAKEUP, &thread->flags);
6995 wake_up(&thread->wqueue);
6996 }
6997}
NeilBrown6c144d32014-09-30 16:15:38 +10006998EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999
Shaohua Li4ed87312012-10-11 13:34:00 +11007000struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7001 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002{
NeilBrown2b8bf342011-10-11 16:48:23 +11007003 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004
NeilBrown2b8bf342011-10-11 16:48:23 +11007005 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 if (!thread)
7007 return NULL;
7008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 init_waitqueue_head(&thread->wqueue);
7010
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 thread->run = run;
7012 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007013 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007014 thread->tsk = kthread_run(md_thread, thread,
7015 "%s_%s",
7016 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007017 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007018 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 kfree(thread);
7020 return NULL;
7021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 return thread;
7023}
NeilBrown6c144d32014-09-30 16:15:38 +10007024EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025
NeilBrown2b8bf342011-10-11 16:48:23 +11007026void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027{
NeilBrown2b8bf342011-10-11 16:48:23 +11007028 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007029 if (!thread)
7030 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007031 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007032 /* Locking ensures that mddev_unlock does not wake_up a
7033 * non-existent thread
7034 */
7035 spin_lock(&pers_lock);
7036 *threadp = NULL;
7037 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007038
7039 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 kfree(thread);
7041}
NeilBrown6c144d32014-09-30 16:15:38 +10007042EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
NeilBrownfd01b882011-10-11 16:47:53 +11007044void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045{
NeilBrownb2d444d2005-11-08 21:39:31 -08007046 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007048
NeilBrownde393cd2011-07-28 11:31:48 +10007049 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050 return;
7051 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007052 if (mddev->degraded)
7053 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007054 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7056 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7057 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007058 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007059 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07007060 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061}
NeilBrown6c144d32014-09-30 16:15:38 +10007062EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
7064/* seq_file implementation /proc/mdstat */
7065
7066static void status_unused(struct seq_file *seq)
7067{
7068 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007069 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
7071 seq_printf(seq, "unused devices: ");
7072
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007073 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 char b[BDEVNAME_SIZE];
7075 i++;
7076 seq_printf(seq, "%s ",
7077 bdevname(rdev->bdev,b));
7078 }
7079 if (!i)
7080 seq_printf(seq, "<none>");
7081
7082 seq_printf(seq, "\n");
7083}
7084
NeilBrownf7851be2015-07-02 17:12:58 +10007085static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086{
NeilBrowndd71cf62009-05-07 12:49:35 +10007087 sector_t max_sectors, resync, res;
7088 unsigned long dt, db;
7089 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007090 int scale;
7091 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
NeilBrownc804cde2012-05-21 09:28:33 +10007093 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7094 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007095 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007097 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098
NeilBrownf7851be2015-07-02 17:12:58 +10007099 resync = mddev->curr_resync;
7100 if (resync <= 3) {
7101 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7102 /* Still cleaning up */
7103 resync = max_sectors;
7104 } else
7105 resync -= atomic_read(&mddev->recovery_active);
7106
7107 if (resync == 0) {
7108 if (mddev->recovery_cp < MaxSector) {
7109 seq_printf(seq, "\tresync=PENDING");
7110 return 1;
7111 }
7112 return 0;
7113 }
7114 if (resync < 3) {
7115 seq_printf(seq, "\tresync=DELAYED");
7116 return 1;
7117 }
7118
NeilBrown403df472014-09-30 15:52:29 +10007119 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007120 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007121 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007122 * u32, as those are the requirements for sector_div.
7123 * Thus 'scale' must be at least 10
7124 */
7125 scale = 10;
7126 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007127 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007128 scale++;
7129 }
7130 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007131 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007132
7133 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 {
NeilBrown4588b422006-03-27 01:18:04 -08007135 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 seq_printf(seq, "[");
7137 for (i = 0; i < x; i++)
7138 seq_printf(seq, "=");
7139 seq_printf(seq, ">");
7140 for (i = 0; i < y; i++)
7141 seq_printf(seq, ".");
7142 seq_printf(seq, "] ");
7143 }
NeilBrown4588b422006-03-27 01:18:04 -08007144 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007145 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7146 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007147 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7148 "check" :
7149 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7150 "resync" : "recovery"))),
7151 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007152 (unsigned long long) resync/2,
7153 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
7155 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 * dt: time from mark until now
7157 * db: blocks written from mark until now
7158 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007159 *
7160 * rt is a sector_t, so could be 32bit or 64bit.
7161 * So we divide before multiply in case it is 32bit and close
7162 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007163 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007164 * near the end of resync when the number of remaining sectors
7165 * is close to 'db'.
7166 * We then divide rt by 32 after multiplying by db to compensate.
7167 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 */
7169 dt = ((jiffies - mddev->resync_mark) / HZ);
7170 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007171 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7172 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173
NeilBrowndd71cf62009-05-07 12:49:35 +10007174 rt = max_sectors - resync; /* number of remaining sectors */
7175 sector_div(rt, db/32+1);
7176 rt *= dt;
7177 rt >>= 5;
7178
7179 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7180 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181
NeilBrownff4e8d92006-07-10 04:44:16 -07007182 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007183 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184}
7185
7186static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7187{
7188 struct list_head *tmp;
7189 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007190 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191
7192 if (l >= 0x10000)
7193 return NULL;
7194 if (!l--)
7195 /* header */
7196 return (void*)1;
7197
7198 spin_lock(&all_mddevs_lock);
7199 list_for_each(tmp,&all_mddevs)
7200 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007201 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 mddev_get(mddev);
7203 spin_unlock(&all_mddevs_lock);
7204 return mddev;
7205 }
7206 spin_unlock(&all_mddevs_lock);
7207 if (!l--)
7208 return (void*)2;/* tail */
7209 return NULL;
7210}
7211
7212static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7213{
7214 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007215 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007216
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 ++*pos;
7218 if (v == (void*)2)
7219 return NULL;
7220
7221 spin_lock(&all_mddevs_lock);
7222 if (v == (void*)1)
7223 tmp = all_mddevs.next;
7224 else
7225 tmp = mddev->all_mddevs.next;
7226 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007227 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 else {
7229 next_mddev = (void*)2;
7230 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 spin_unlock(&all_mddevs_lock);
7233
7234 if (v != (void*)1)
7235 mddev_put(mddev);
7236 return next_mddev;
7237
7238}
7239
7240static void md_seq_stop(struct seq_file *seq, void *v)
7241{
NeilBrownfd01b882011-10-11 16:47:53 +11007242 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243
7244 if (mddev && v != (void*)1 && v != (void*)2)
7245 mddev_put(mddev);
7246}
7247
7248static int md_seq_show(struct seq_file *seq, void *v)
7249{
NeilBrownfd01b882011-10-11 16:47:53 +11007250 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007251 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007252 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
7254 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007255 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 seq_printf(seq, "Personalities : ");
7257 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007258 list_for_each_entry(pers, &pers_list, list)
7259 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260
7261 spin_unlock(&pers_lock);
7262 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007263 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 return 0;
7265 }
7266 if (v == (void*)2) {
7267 status_unused(seq);
7268 return 0;
7269 }
7270
NeilBrown36d091f2014-12-15 12:56:58 +11007271 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7273 seq_printf(seq, "%s : %sactive", mdname(mddev),
7274 mddev->pers ? "" : "in");
7275 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007276 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007278 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007279 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 seq_printf(seq, " %s", mddev->pers->name);
7281 }
7282
Andre Nolldd8ac332009-03-31 14:33:13 +11007283 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007284 rcu_read_lock();
7285 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 char b[BDEVNAME_SIZE];
7287 seq_printf(seq, " %s[%d]",
7288 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007289 if (test_bit(WriteMostly, &rdev->flags))
7290 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007291 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 seq_printf(seq, "(F)");
7293 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007294 }
7295 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007296 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007297 if (test_bit(Replacement, &rdev->flags))
7298 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007299 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007301 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302
7303 if (!list_empty(&mddev->disks)) {
7304 if (mddev->pers)
7305 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007306 (unsigned long long)
7307 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 else
7309 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007310 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007312 if (mddev->persistent) {
7313 if (mddev->major_version != 0 ||
7314 mddev->minor_version != 90) {
7315 seq_printf(seq," super %d.%d",
7316 mddev->major_version,
7317 mddev->minor_version);
7318 }
NeilBrowne6910632008-02-06 01:39:51 -08007319 } else if (mddev->external)
7320 seq_printf(seq, " super external:%s",
7321 mddev->metadata_type);
7322 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007323 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324
7325 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007326 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007327 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007328 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10007329 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08007330 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007331 }
NeilBrown32a76272005-06-21 17:17:14 -07007332 } else
7333 seq_printf(seq, "\n ");
7334
NeilBrown57148962012-03-19 12:46:40 +11007335 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336
7337 seq_printf(seq, "\n");
7338 }
NeilBrown36d091f2014-12-15 12:56:58 +11007339 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007340
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 return 0;
7342}
7343
Jan Engelhardt110518b2009-05-07 12:49:37 +10007344static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 .start = md_seq_start,
7346 .next = md_seq_next,
7347 .stop = md_seq_stop,
7348 .show = md_seq_show,
7349};
7350
7351static int md_seq_open(struct inode *inode, struct file *file)
7352{
Kay Sieversf1514632011-07-12 20:48:39 +02007353 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 int error;
7355
7356 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007357 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007358 return error;
7359
7360 seq = file->private_data;
7361 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 return error;
7363}
7364
NeilBrowne2f23b62014-04-09 14:33:51 +10007365static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007366static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7367{
Kay Sieversf1514632011-07-12 20:48:39 +02007368 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007369 int mask;
7370
NeilBrowne2f23b62014-04-09 14:33:51 +10007371 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007372 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007373 poll_wait(filp, &md_event_waiters, wait);
7374
7375 /* always allow read */
7376 mask = POLLIN | POLLRDNORM;
7377
Kay Sieversf1514632011-07-12 20:48:39 +02007378 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007379 mask |= POLLERR | POLLPRI;
7380 return mask;
7381}
7382
Arjan van de Venfa027c22007-02-12 00:55:33 -08007383static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007384 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 .open = md_seq_open,
7386 .read = seq_read,
7387 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007388 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007389 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390};
7391
NeilBrown84fc4b52011-10-11 16:49:58 +11007392int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393{
Chao Yu50bd3772014-09-25 15:28:34 +08007394 printk(KERN_INFO "md: %s personality registered for level %d\n",
7395 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007397 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 spin_unlock(&pers_lock);
7399 return 0;
7400}
NeilBrown6c144d32014-09-30 16:15:38 +10007401EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402
NeilBrown84fc4b52011-10-11 16:49:58 +11007403int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404{
NeilBrown2604b702006-01-06 00:20:36 -08007405 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007407 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 spin_unlock(&pers_lock);
7409 return 0;
7410}
NeilBrown6c144d32014-09-30 16:15:38 +10007411EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
NeilBrown6022e752015-08-13 12:32:55 +10007413int register_md_cluster_operations(struct md_cluster_operations *ops,
7414 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007415{
NeilBrown6022e752015-08-13 12:32:55 +10007416 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007417 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007418 if (md_cluster_ops != NULL)
7419 ret = -EALREADY;
7420 else {
7421 md_cluster_ops = ops;
7422 md_cluster_mod = module;
7423 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007424 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10007425 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007426}
7427EXPORT_SYMBOL(register_md_cluster_operations);
7428
7429int unregister_md_cluster_operations(void)
7430{
7431 spin_lock(&pers_lock);
7432 md_cluster_ops = NULL;
7433 spin_unlock(&pers_lock);
7434 return 0;
7435}
7436EXPORT_SYMBOL(unregister_md_cluster_operations);
7437
7438int md_setup_cluster(struct mddev *mddev, int nodes)
7439{
7440 int err;
7441
7442 err = request_module("md-cluster");
7443 if (err) {
7444 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007445 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007446 }
7447
7448 spin_lock(&pers_lock);
7449 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7450 spin_unlock(&pers_lock);
7451 return -ENOENT;
7452 }
7453 spin_unlock(&pers_lock);
7454
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007455 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007456}
7457
7458void md_cluster_stop(struct mddev *mddev)
7459{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007460 if (!md_cluster_ops)
7461 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007462 md_cluster_ops->leave(mddev);
7463 module_put(md_cluster_mod);
7464}
7465
NeilBrownfd01b882011-10-11 16:47:53 +11007466static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007468 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007470 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471
7472 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007473 rcu_read_lock();
7474 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007476 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7477 (int)part_stat_read(&disk->part0, sectors[1]) -
7478 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007479 /* sync IO will cause sync_io to increase before the disk_stats
7480 * as sync_io is counted when a request starts, and
7481 * disk_stats is counted when it completes.
7482 * So resync activity will cause curr_events to be smaller than
7483 * when there was no such activity.
7484 * non-sync IO will cause disk_stat to increase without
7485 * increasing sync_io so curr_events will (eventually)
7486 * be larger than it was before. Once it becomes
7487 * substantially larger, the test below will cause
7488 * the array to appear non-idle, and resync will slow
7489 * down.
7490 * If there is a lot of outstanding resync activity when
7491 * we set last_event to curr_events, then all that activity
7492 * completing might cause the array to appear non-idle
7493 * and resync will be slowed down even though there might
7494 * not have been non-resync activity. This will only
7495 * happen once though. 'last_events' will soon reflect
7496 * the state where there is little or no outstanding
7497 * resync requests, and further resync activity will
7498 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007499 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007501 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 rdev->last_events = curr_events;
7503 idle = 0;
7504 }
7505 }
NeilBrown4b809912008-07-21 17:05:25 +10007506 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 return idle;
7508}
7509
NeilBrownfd01b882011-10-11 16:47:53 +11007510void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511{
7512 /* another "blocks" (512byte) blocks have been synced */
7513 atomic_sub(blocks, &mddev->recovery_active);
7514 wake_up(&mddev->recovery_wait);
7515 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007516 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007517 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 md_wakeup_thread(mddev->thread);
7519 // stop recovery, signal do_sync ....
7520 }
7521}
NeilBrown6c144d32014-09-30 16:15:38 +10007522EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523
NeilBrown06d91a52005-06-21 17:17:12 -07007524/* md_write_start(mddev, bi)
7525 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007526 * in superblock) before writing, schedule a superblock update
7527 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007528 */
NeilBrownfd01b882011-10-11 16:47:53 +11007529void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530{
Neil Brown0fd62b82008-06-28 08:31:36 +10007531 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007532 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007533 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007534
NeilBrownf91de922005-11-08 21:39:36 -08007535 BUG_ON(mddev->ro == 1);
7536 if (mddev->ro == 2) {
7537 /* need to switch to read/write */
7538 mddev->ro = 0;
7539 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7540 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007541 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007542 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007543 }
NeilBrown06d91a52005-06-21 17:17:12 -07007544 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007545 if (mddev->safemode == 1)
7546 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007547 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007548 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007549 if (mddev->in_sync) {
7550 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007551 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007552 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007553 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007554 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007555 }
NeilBrown85572d72014-12-15 12:56:56 +11007556 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007557 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007558 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007559 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007560 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007561 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562}
NeilBrown6c144d32014-09-30 16:15:38 +10007563EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564
NeilBrownfd01b882011-10-11 16:47:53 +11007565void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566{
7567 if (atomic_dec_and_test(&mddev->writes_pending)) {
7568 if (mddev->safemode == 2)
7569 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007570 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7572 }
7573}
NeilBrown6c144d32014-09-30 16:15:38 +10007574EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575
NeilBrown2a2275d2007-01-26 00:57:11 -08007576/* md_allow_write(mddev)
7577 * Calling this ensures that the array is marked 'active' so that writes
7578 * may proceed without blocking. It is important to call this before
7579 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7580 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007581 *
7582 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7583 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007584 */
NeilBrownfd01b882011-10-11 16:47:53 +11007585int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007586{
7587 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007588 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007589 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007590 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007591 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007592 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007593
NeilBrown85572d72014-12-15 12:56:56 +11007594 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007595 if (mddev->in_sync) {
7596 mddev->in_sync = 0;
7597 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007598 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007599 if (mddev->safemode_delay &&
7600 mddev->safemode == 0)
7601 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007602 spin_unlock(&mddev->lock);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007603 if (mddev_is_clustered(mddev))
7604 md_cluster_ops->metadata_update_start(mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08007605 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007606 if (mddev_is_clustered(mddev))
7607 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007608 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007609 } else
NeilBrown85572d72014-12-15 12:56:56 +11007610 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007611
NeilBrown070dc6d2010-08-30 17:33:34 +10007612 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007613 return -EAGAIN;
7614 else
7615 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007616}
7617EXPORT_SYMBOL_GPL(md_allow_write);
7618
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619#define SYNC_MARKS 10
7620#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007621#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007622void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623{
Shaohua Li4ed87312012-10-11 13:34:00 +11007624 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007625 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626 unsigned int currspeed = 0,
7627 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007628 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007630 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631 sector_t mark_cnt[SYNC_MARKS];
7632 int last_mark,m;
7633 struct list_head *tmp;
7634 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007635 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007636 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007637 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007638 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639
7640 /* just incase thread restarts... */
7641 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7642 return;
NeilBrown3991b312014-05-28 13:39:23 +10007643 if (mddev->ro) {/* never try to sync a read-only array */
7644 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007645 return;
NeilBrown3991b312014-05-28 13:39:23 +10007646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647
NeilBrown61df9d92006-10-03 01:15:57 -07007648 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007649 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007650 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007651 action = "check";
7652 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007653 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007654 action = "repair";
7655 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007656 desc = "resync";
7657 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7658 desc = "reshape";
7659 else
7660 desc = "recovery";
7661
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007662 mddev->last_sync_action = action ?: desc;
7663
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664 /* we overload curr_resync somewhat here.
7665 * 0 == not engaged in resync at all
7666 * 2 == checking that there is no conflict with another sync
7667 * 1 == like 2, but have yielded to allow conflicting resync to
7668 * commense
7669 * other == active in resync - this many blocks
7670 *
7671 * Before starting a resync we must have set curr_resync to
7672 * 2, and then checked that every "conflicting" array has curr_resync
7673 * less than ours. When we find one that is the same or higher
7674 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7675 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7676 * This will mean we have to start checking from the beginning again.
7677 *
7678 */
7679
7680 do {
7681 mddev->curr_resync = 2;
7682
7683 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007684 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007686 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687 if (mddev2 == mddev)
7688 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007689 if (!mddev->parallel_resync
7690 && mddev2->curr_resync
7691 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 DEFINE_WAIT(wq);
7693 if (mddev < mddev2 && mddev->curr_resync == 2) {
7694 /* arbitrarily yield */
7695 mddev->curr_resync = 1;
7696 wake_up(&resync_wait);
7697 }
7698 if (mddev > mddev2 && mddev->curr_resync == 1)
7699 /* no need to wait here, we can wait the next
7700 * time 'round when curr_resync == 2
7701 */
7702 continue;
NeilBrown97441972008-09-19 11:49:54 +10007703 /* We need to wait 'interruptible' so as not to
7704 * contribute to the load average, and not to
7705 * be caught by 'softlockup'
7706 */
7707 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007708 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007709 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007710 printk(KERN_INFO "md: delaying %s of %s"
7711 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007713 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007715 if (signal_pending(current))
7716 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 schedule();
7718 finish_wait(&resync_wait, &wq);
7719 goto try_again;
7720 }
7721 finish_wait(&resync_wait, &wq);
7722 }
7723 }
7724 } while (mddev->curr_resync < 2);
7725
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007726 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007727 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007729 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730 */
7731 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007732 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007733 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007734 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7735 j = mddev->resync_min;
7736 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007737 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007738
NeilBrownccfcc3c2006-03-27 01:18:09 -08007739 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007740 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007741 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007743 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007744 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007745 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007746 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007747 if (rdev->raid_disk >= 0 &&
7748 !test_bit(Faulty, &rdev->flags) &&
7749 !test_bit(In_sync, &rdev->flags) &&
7750 rdev->recovery_offset < j)
7751 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007752 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007753
7754 /* If there is a bitmap, we need to make sure all
7755 * writes that started before we added a spare
7756 * complete before we start doing a recovery.
7757 * Otherwise the write might complete and (via
7758 * bitmap_endwrite) set a bit in the bitmap after the
7759 * recovery has checked that bit and skipped that
7760 * region.
7761 */
7762 if (mddev->bitmap) {
7763 mddev->pers->quiesce(mddev, 1);
7764 mddev->pers->quiesce(mddev, 0);
7765 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767
NeilBrown61df9d92006-10-03 01:15:57 -07007768 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7769 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7770 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007771 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007772 "(but not more than %d KB/sec) for %s.\n",
7773 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774
NeilBrowneea1bf32009-03-31 14:27:02 +11007775 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007776
NeilBrown57afd892005-06-21 17:17:13 -07007777 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007778 for (m = 0; m < SYNC_MARKS; m++) {
7779 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007780 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 }
7782 last_mark = 0;
7783 mddev->resync_mark = mark[last_mark];
7784 mddev->resync_mark_cnt = mark_cnt[last_mark];
7785
7786 /*
7787 * Tune reconstruction:
7788 */
7789 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007790 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7791 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792
7793 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794 last_check = 0;
7795
7796 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007797 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007798 "md: resuming %s of %s from checkpoint.\n",
7799 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007801 } else
7802 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007803 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007804 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7805 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007806 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007808 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007809 md_cluster_ops->resync_start(mddev, j, max_sectors);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007810
majianpeng7c2c57c2012-07-03 12:12:26 +10007811 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007813 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814
NeilBrown57afd892005-06-21 17:17:13 -07007815 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007816
NeilBrown7a91ee12009-05-26 12:57:21 +10007817 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7818 ((mddev->curr_resync > mddev->curr_resync_completed &&
7819 (mddev->curr_resync - mddev->curr_resync_completed)
7820 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007821 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007822 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10007823 >= mddev->resync_max - mddev->curr_resync_completed ||
7824 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10007825 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007826 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007827 wait_event(mddev->recovery_wait,
7828 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007829 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007830 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7831 j > mddev->recovery_cp)
7832 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007833 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007834 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007835 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007836 }
NeilBrownacb180b2009-04-14 16:28:34 +10007837
NeilBrownc91abf52013-11-19 12:02:01 +11007838 while (j >= mddev->resync_max &&
7839 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007840 /* As this condition is controlled by user-space,
7841 * we can block indefinitely, so use '_interruptible'
7842 * to avoid triggering warnings.
7843 */
7844 flush_signals(current); /* just in case */
7845 wait_event_interruptible(mddev->recovery_wait,
7846 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007847 || test_bit(MD_RECOVERY_INTR,
7848 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007849 }
NeilBrownacb180b2009-04-14 16:28:34 +10007850
NeilBrownc91abf52013-11-19 12:02:01 +11007851 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7852 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007853
NeilBrown09314792015-02-19 16:04:40 +11007854 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07007855 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007856 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858 }
NeilBrown57afd892005-06-21 17:17:13 -07007859
7860 if (!skipped) { /* actual IO requested */
7861 io_sectors += sectors;
7862 atomic_add(sectors, &mddev->recovery_active);
7863 }
7864
NeilBrowne875ece2011-07-28 11:39:24 +10007865 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7866 break;
7867
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10007869 if (j > max_sectors)
7870 /* when skipping, extra large numbers can be returned. */
7871 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007872 if (j > 2)
7873 mddev->curr_resync = j;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007874 if (mddev_is_clustered(mddev))
7875 md_cluster_ops->resync_info_update(mddev, j, max_sectors);
NeilBrownff4e8d92006-07-10 04:44:16 -07007876 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007877 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007878 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007879 * visible in /proc/mdstat
7880 */
7881 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007882
7883 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884 continue;
7885
NeilBrown57afd892005-06-21 17:17:13 -07007886 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 repeat:
7888 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7889 /* step marks */
7890 int next = (last_mark+1) % SYNC_MARKS;
7891
7892 mddev->resync_mark = mark[next];
7893 mddev->resync_mark_cnt = mark_cnt[next];
7894 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007895 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896 last_mark = next;
7897 }
7898
NeilBrownc91abf52013-11-19 12:02:01 +11007899 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7900 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901
7902 /*
7903 * this loop exits only if either when we are slower than
7904 * the 'hard' speed limit, or the system was IO-idle for
7905 * a jiffy.
7906 * the system might be non-idle CPU-wise, but we only care
7907 * about not overloading the IO subsystem. (things like an
7908 * e2fsck being done on the RAID array should execute fast)
7909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 cond_resched();
7911
Xiao Niac7e50a2014-08-07 09:37:41 -04007912 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7913 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007914 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915
NeilBrown88202a02006-01-06 00:21:36 -08007916 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11007917 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007918 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 goto repeat;
7920 }
NeilBrownac8fa412015-02-19 16:55:00 +11007921 if (!is_mddev_idle(mddev, 0)) {
7922 /*
7923 * Give other IO more of a chance.
7924 * The faster the devices, the less we wait.
7925 */
7926 wait_event(mddev->recovery_wait,
7927 !atomic_read(&mddev->recovery_active));
7928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 }
7930 }
NeilBrownc91abf52013-11-19 12:02:01 +11007931 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7932 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7933 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934 /*
7935 * this also signals 'finished resyncing' to md_stop
7936 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007937 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7939
NeilBrown5ed1df22015-07-24 13:27:08 +10007940 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7941 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7942 mddev->curr_resync > 2) {
7943 mddev->curr_resync_completed = mddev->curr_resync;
7944 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946 /* tell personality that we are finished */
NeilBrown09314792015-02-19 16:04:40 +11007947 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948
NeilBrowndfc70642008-05-23 13:04:39 -07007949 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007950 mddev->curr_resync > 2) {
7951 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7952 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7953 if (mddev->curr_resync >= mddev->recovery_cp) {
7954 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007955 "md: checkpointing %s of %s.\n",
7956 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007957 if (test_bit(MD_RECOVERY_ERROR,
7958 &mddev->recovery))
7959 mddev->recovery_cp =
7960 mddev->curr_resync_completed;
7961 else
7962 mddev->recovery_cp =
7963 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007964 }
7965 } else
7966 mddev->recovery_cp = MaxSector;
7967 } else {
7968 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7969 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007970 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007971 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007972 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007973 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007974 !test_bit(Faulty, &rdev->flags) &&
7975 !test_bit(In_sync, &rdev->flags) &&
7976 rdev->recovery_offset < mddev->curr_resync)
7977 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007978 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007981 skip:
Guoqing Jiangdc737d72015-07-10 16:54:04 +08007982 if (mddev_is_clustered(mddev))
7983 md_cluster_ops->resync_finish(mddev);
7984
NeilBrown17571282006-12-10 02:20:52 -08007985 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986
NeilBrown23da4222014-12-15 12:57:01 +11007987 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11007988 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7989 /* We completed so min/max setting can be forgotten if used. */
7990 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7991 mddev->resync_min = 0;
7992 mddev->resync_max = MaxSector;
7993 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7994 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10007995 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007996 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007997 spin_unlock(&mddev->lock);
7998
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008000 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008001 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002}
NeilBrown29269552006-03-27 01:18:10 -08008003EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004
NeilBrown746d3202013-04-24 11:42:41 +10008005static int remove_and_add_spares(struct mddev *mddev,
8006 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008007{
NeilBrown3cb03002011-10-11 16:45:26 +11008008 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008009 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008010 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008011
NeilBrowndafb20f2012-03-19 12:46:39 +11008012 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008013 if ((this == NULL || rdev == this) &&
8014 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008015 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008016 (test_bit(Faulty, &rdev->flags) ||
8017 ! test_bit(In_sync, &rdev->flags)) &&
8018 atomic_read(&rdev->nr_pending)==0) {
8019 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008020 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008021 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008022 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008023 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008024 }
8025 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008026 if (removed && mddev->kobj.sd)
8027 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008028
NeilBrown746d3202013-04-24 11:42:41 +10008029 if (this)
8030 goto no_add;
8031
NeilBrowndafb20f2012-03-19 12:46:39 +11008032 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11008033 if (rdev->raid_disk >= 0 &&
8034 !test_bit(In_sync, &rdev->flags) &&
8035 !test_bit(Faulty, &rdev->flags))
8036 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008037 if (rdev->raid_disk >= 0)
8038 continue;
8039 if (test_bit(Faulty, &rdev->flags))
8040 continue;
8041 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11008042 ! (rdev->saved_raid_disk >= 0 &&
8043 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10008044 continue;
8045
NeilBrown7eb41882014-01-14 15:55:14 +11008046 if (rdev->saved_raid_disk < 0)
8047 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008048 if (mddev->pers->
8049 hot_add_disk(mddev, rdev) == 0) {
8050 if (sysfs_link_rdev(mddev, rdev))
8051 /* failure here is OK */;
8052 spares++;
8053 md_new_event(mddev);
8054 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008055 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008056 }
NeilBrown746d3202013-04-24 11:42:41 +10008057no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008058 if (removed)
8059 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008060 return spares;
8061}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008062
NeilBrownac05f252014-09-30 08:10:42 +10008063static void md_start_sync(struct work_struct *ws)
8064{
8065 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8066
8067 mddev->sync_thread = md_register_thread(md_do_sync,
8068 mddev,
8069 "resync");
8070 if (!mddev->sync_thread) {
8071 printk(KERN_ERR "%s: could not start resync"
8072 " thread...\n",
8073 mdname(mddev));
8074 /* leave the spares where they are, it shouldn't hurt */
8075 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8076 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8077 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8078 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8079 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008080 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008081 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8082 &mddev->recovery))
8083 if (mddev->sysfs_action)
8084 sysfs_notify_dirent_safe(mddev->sysfs_action);
8085 } else
8086 md_wakeup_thread(mddev->sync_thread);
8087 sysfs_notify_dirent_safe(mddev->sysfs_action);
8088 md_new_event(mddev);
8089}
8090
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091/*
8092 * This routine is regularly called by all per-raid-array threads to
8093 * deal with generic issues like resync and super-block update.
8094 * Raid personalities that don't have a thread (linear/raid0) do not
8095 * need this as they never do any recovery or update the superblock.
8096 *
8097 * It does not do any resync itself, but rather "forks" off other threads
8098 * to do that as needed.
8099 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8100 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008101 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008102 * and wakeups up this thread which will reap the thread and finish up.
8103 * This thread also removes any faulty devices (with nr_pending == 0).
8104 *
8105 * The overall approach is:
8106 * 1/ if the superblock needs updating, update it.
8107 * 2/ If a recovery thread is running, don't do anything else.
8108 * 3/ If recovery has finished, clean up, possibly marking spares active.
8109 * 4/ If there are any faulty devices, remove them.
8110 * 5/ If array is degraded, try to add spares devices
8111 * 6/ If array has spares or is not in-sync, start a resync thread.
8112 */
NeilBrownfd01b882011-10-11 16:47:53 +11008113void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008115 if (mddev->suspended)
8116 return;
8117
NeilBrown5f404022005-06-21 17:17:16 -07008118 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008119 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120
NeilBrownfca4d842005-06-21 17:17:11 -07008121 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008122 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008123 printk(KERN_INFO "md: %s in immediate safe mode\n",
8124 mdname(mddev));
8125 mddev->safemode = 2;
8126 }
8127 flush_signals(current);
8128 }
8129
NeilBrownc89a8ee2008-08-05 15:54:13 +10008130 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8131 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008133 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008134 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008135 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008136 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008137 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8138 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139 ))
8140 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008141
NeilBrowndf5b89b2006-03-27 01:18:20 -08008142 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008143 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008144
NeilBrownc89a8ee2008-08-05 15:54:13 +10008145 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008146 struct md_rdev *rdev;
8147 if (!mddev->external && mddev->in_sync)
8148 /* 'Blocked' flag not needed as failed devices
8149 * will be recorded if array switched to read/write.
8150 * Leaving it set will prevent the device
8151 * from being removed.
8152 */
8153 rdev_for_each(rdev, mddev)
8154 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008155 /* On a read-only array we can:
8156 * - remove failed devices
8157 * - add already-in_sync devices if the array itself
8158 * is in-sync.
8159 * As we only add devices that are already in-sync,
8160 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008161 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008162 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008163 /* There is no thread, but we need to call
8164 * ->spare_active and clear saved_raid_disk
8165 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008166 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008167 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008168 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008169 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Lid4929ad2015-09-18 10:20:12 -07008170 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008171 goto unlock;
8172 }
8173
NeilBrown31a59e32008-04-30 00:52:30 -07008174 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008175 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008176 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008177 if (mddev->safemode &&
8178 !atomic_read(&mddev->writes_pending) &&
8179 !mddev->in_sync &&
8180 mddev->recovery_cp == MaxSector) {
8181 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008182 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008183 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008184 }
8185 if (mddev->safemode == 1)
8186 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008187 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008188 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008189 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008190 }
NeilBrownfca4d842005-06-21 17:17:11 -07008191
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008192 if (mddev->flags & MD_UPDATE_SB_FLAGS) {
8193 if (mddev_is_clustered(mddev))
8194 md_cluster_ops->metadata_update_start(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07008195 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008196 if (mddev_is_clustered(mddev))
8197 md_cluster_ops->metadata_update_finish(mddev);
8198 }
NeilBrown06d91a52005-06-21 17:17:12 -07008199
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8201 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8202 /* resync/recovery still happening */
8203 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8204 goto unlock;
8205 }
8206 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008207 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208 goto unlock;
8209 }
Neil Brown72a23c22008-06-28 08:31:41 +10008210 /* Set RUNNING before clearing NEEDED to avoid
8211 * any transients in the value of "sync_action".
8212 */
NeilBrown72f36d52012-10-11 14:25:57 +11008213 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008214 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008215 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008216 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008217 /* Clear some bits that don't mean anything, but
8218 * might be left set
8219 */
NeilBrown24dd4692005-11-08 21:39:26 -08008220 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8221 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222
NeilBrowned209582012-04-24 10:23:14 +10008223 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8224 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008225 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008226 /* no recovery is running.
8227 * remove any failed drives, then
8228 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008229 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008230 * the personality to fail the re-add.
8231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008233 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008234 if (mddev->pers->check_reshape == NULL ||
8235 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008236 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008237 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008238 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008239 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008240 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008241 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8242 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008243 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008244 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008245 } else if (mddev->recovery_cp < MaxSector) {
8246 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008247 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008248 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8249 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008250 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008251
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008253 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008254 /* We are adding a device or devices to an array
8255 * which has the bitmap stored on all devices.
8256 * So make sure all bitmap pages get written
8257 */
8258 bitmap_write_all(mddev->bitmap);
8259 }
NeilBrownac05f252014-09-30 08:10:42 +10008260 INIT_WORK(&mddev->del_work, md_start_sync);
8261 queue_work(md_misc_wq, &mddev->del_work);
8262 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263 }
NeilBrownac05f252014-09-30 08:10:42 +10008264 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008265 if (!mddev->sync_thread) {
8266 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008267 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008268 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8269 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008270 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008271 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008272 }
NeilBrownac05f252014-09-30 08:10:42 +10008273 unlock:
8274 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275 mddev_unlock(mddev);
8276 }
8277}
NeilBrown6c144d32014-09-30 16:15:38 +10008278EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008280void md_reap_sync_thread(struct mddev *mddev)
8281{
8282 struct md_rdev *rdev;
8283
8284 /* resync has finished, collect result */
8285 md_unregister_thread(&mddev->sync_thread);
8286 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8287 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8288 /* success...*/
8289 /* activate any spares */
8290 if (mddev->pers->spare_active(mddev)) {
8291 sysfs_notify(&mddev->kobj, NULL,
8292 "degraded");
8293 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8294 }
8295 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008296 if (mddev_is_clustered(mddev))
8297 md_cluster_ops->metadata_update_start(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008298 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8299 mddev->pers->finish_reshape)
8300 mddev->pers->finish_reshape(mddev);
8301
8302 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008303 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008304 */
NeilBrownf4667222013-12-09 12:04:56 +11008305 if (!mddev->degraded)
8306 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008307 rdev->saved_raid_disk = -1;
8308
8309 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008310 if (mddev_is_clustered(mddev))
8311 md_cluster_ops->metadata_update_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008312 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008313 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008314 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8315 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8316 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8317 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008318 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008319 /* flag recovery needed just to double check */
8320 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8321 sysfs_notify_dirent_safe(mddev->sysfs_action);
8322 md_new_event(mddev);
8323 if (mddev->event_work.func)
8324 queue_work(md_misc_wq, &mddev->event_work);
8325}
NeilBrown6c144d32014-09-30 16:15:38 +10008326EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008327
NeilBrownfd01b882011-10-11 16:47:53 +11008328void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008329{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008330 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008331 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008332 !test_bit(Blocked, &rdev->flags) &&
8333 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008334 msecs_to_jiffies(5000));
8335 rdev_dec_pending(rdev, mddev);
8336}
8337EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8338
NeilBrownc6563a82012-05-21 09:27:00 +10008339void md_finish_reshape(struct mddev *mddev)
8340{
8341 /* called be personality module when reshape completes. */
8342 struct md_rdev *rdev;
8343
8344 rdev_for_each(rdev, mddev) {
8345 if (rdev->data_offset > rdev->new_data_offset)
8346 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8347 else
8348 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8349 rdev->data_offset = rdev->new_data_offset;
8350 }
8351}
8352EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008353
8354/* Bad block management.
8355 * We can record which blocks on each device are 'bad' and so just
8356 * fail those blocks, or that stripe, rather than the whole device.
8357 * Entries in the bad-block table are 64bits wide. This comprises:
8358 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8359 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8360 * A 'shift' can be set so that larger blocks are tracked and
8361 * consequently larger devices can be covered.
8362 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8363 *
8364 * Locking of the bad-block table uses a seqlock so md_is_badblock
8365 * might need to retry if it is very unlucky.
8366 * We will sometimes want to check for bad blocks in a bi_end_io function,
8367 * so we use the write_seqlock_irq variant.
8368 *
8369 * When looking for a bad block we specify a range and want to
8370 * know if any block in the range is bad. So we binary-search
8371 * to the last range that starts at-or-before the given endpoint,
8372 * (or "before the sector after the target range")
8373 * then see if it ends after the given start.
8374 * We return
8375 * 0 if there are no known bad blocks in the range
8376 * 1 if there are known bad block which are all acknowledged
8377 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8378 * plus the start/length of the first bad section we overlap.
8379 */
8380int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8381 sector_t *first_bad, int *bad_sectors)
8382{
8383 int hi;
majianpengab056132012-11-06 17:13:44 +08008384 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008385 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008386 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008387 sector_t target = s + sectors;
8388 unsigned seq;
8389
8390 if (bb->shift > 0) {
8391 /* round the start down, and the end up */
8392 s >>= bb->shift;
8393 target += (1<<bb->shift) - 1;
8394 target >>= bb->shift;
8395 sectors = target - s;
8396 }
8397 /* 'target' is now the first block after the bad range */
8398
8399retry:
8400 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008401 lo = 0;
8402 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008403 hi = bb->count;
8404
8405 /* Binary search between lo and hi for 'target'
8406 * i.e. for the last range that starts before 'target'
8407 */
8408 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8409 * are known not to be the last range before target.
8410 * VARIANT: hi-lo is the number of possible
8411 * ranges, and decreases until it reaches 1
8412 */
8413 while (hi - lo > 1) {
8414 int mid = (lo + hi) / 2;
8415 sector_t a = BB_OFFSET(p[mid]);
8416 if (a < target)
8417 /* This could still be the one, earlier ranges
8418 * could not. */
8419 lo = mid;
8420 else
8421 /* This and later ranges are definitely out. */
8422 hi = mid;
8423 }
8424 /* 'lo' might be the last that started before target, but 'hi' isn't */
8425 if (hi > lo) {
8426 /* need to check all range that end after 's' to see if
8427 * any are unacknowledged.
8428 */
8429 while (lo >= 0 &&
8430 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8431 if (BB_OFFSET(p[lo]) < target) {
8432 /* starts before the end, and finishes after
8433 * the start, so they must overlap
8434 */
8435 if (rv != -1 && BB_ACK(p[lo]))
8436 rv = 1;
8437 else
8438 rv = -1;
8439 *first_bad = BB_OFFSET(p[lo]);
8440 *bad_sectors = BB_LEN(p[lo]);
8441 }
8442 lo--;
8443 }
8444 }
8445
8446 if (read_seqretry(&bb->lock, seq))
8447 goto retry;
8448
8449 return rv;
8450}
8451EXPORT_SYMBOL_GPL(md_is_badblock);
8452
8453/*
8454 * Add a range of bad blocks to the table.
8455 * This might extend the table, or might contract it
8456 * if two adjacent ranges can be merged.
8457 * We binary-search to find the 'insertion' point, then
8458 * decide how best to handle it.
8459 */
8460static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8461 int acknowledged)
8462{
8463 u64 *p;
8464 int lo, hi;
8465 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008466 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008467
8468 if (bb->shift < 0)
8469 /* badblocks are disabled */
8470 return 0;
8471
8472 if (bb->shift) {
8473 /* round the start down, and the end up */
8474 sector_t next = s + sectors;
8475 s >>= bb->shift;
8476 next += (1<<bb->shift) - 1;
8477 next >>= bb->shift;
8478 sectors = next - s;
8479 }
8480
Bian Yu905b0292013-10-12 01:10:03 -04008481 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008482
8483 p = bb->page;
8484 lo = 0;
8485 hi = bb->count;
8486 /* Find the last range that starts at-or-before 's' */
8487 while (hi - lo > 1) {
8488 int mid = (lo + hi) / 2;
8489 sector_t a = BB_OFFSET(p[mid]);
8490 if (a <= s)
8491 lo = mid;
8492 else
8493 hi = mid;
8494 }
8495 if (hi > lo && BB_OFFSET(p[lo]) > s)
8496 hi = lo;
8497
8498 if (hi > lo) {
8499 /* we found a range that might merge with the start
8500 * of our new range
8501 */
8502 sector_t a = BB_OFFSET(p[lo]);
8503 sector_t e = a + BB_LEN(p[lo]);
8504 int ack = BB_ACK(p[lo]);
8505 if (e >= s) {
8506 /* Yes, we can merge with a previous range */
8507 if (s == a && s + sectors >= e)
8508 /* new range covers old */
8509 ack = acknowledged;
8510 else
8511 ack = ack && acknowledged;
8512
8513 if (e < s + sectors)
8514 e = s + sectors;
8515 if (e - a <= BB_MAX_LEN) {
8516 p[lo] = BB_MAKE(a, e-a, ack);
8517 s = e;
8518 } else {
8519 /* does not all fit in one range,
8520 * make p[lo] maximal
8521 */
8522 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8523 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8524 s = a + BB_MAX_LEN;
8525 }
8526 sectors = e - s;
8527 }
8528 }
8529 if (sectors && hi < bb->count) {
8530 /* 'hi' points to the first range that starts after 's'.
8531 * Maybe we can merge with the start of that range */
8532 sector_t a = BB_OFFSET(p[hi]);
8533 sector_t e = a + BB_LEN(p[hi]);
8534 int ack = BB_ACK(p[hi]);
8535 if (a <= s + sectors) {
8536 /* merging is possible */
8537 if (e <= s + sectors) {
8538 /* full overlap */
8539 e = s + sectors;
8540 ack = acknowledged;
8541 } else
8542 ack = ack && acknowledged;
8543
8544 a = s;
8545 if (e - a <= BB_MAX_LEN) {
8546 p[hi] = BB_MAKE(a, e-a, ack);
8547 s = e;
8548 } else {
8549 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8550 s = a + BB_MAX_LEN;
8551 }
8552 sectors = e - s;
8553 lo = hi;
8554 hi++;
8555 }
8556 }
8557 if (sectors == 0 && hi < bb->count) {
8558 /* we might be able to combine lo and hi */
8559 /* Note: 's' is at the end of 'lo' */
8560 sector_t a = BB_OFFSET(p[hi]);
8561 int lolen = BB_LEN(p[lo]);
8562 int hilen = BB_LEN(p[hi]);
8563 int newlen = lolen + hilen - (s - a);
8564 if (s >= a && newlen < BB_MAX_LEN) {
8565 /* yes, we can combine them */
8566 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8567 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8568 memmove(p + hi, p + hi + 1,
8569 (bb->count - hi - 1) * 8);
8570 bb->count--;
8571 }
8572 }
8573 while (sectors) {
8574 /* didn't merge (it all).
8575 * Need to add a range just before 'hi' */
8576 if (bb->count >= MD_MAX_BADBLOCKS) {
8577 /* No room for more */
8578 rv = 0;
8579 break;
8580 } else {
8581 int this_sectors = sectors;
8582 memmove(p + hi + 1, p + hi,
8583 (bb->count - hi) * 8);
8584 bb->count++;
8585
8586 if (this_sectors > BB_MAX_LEN)
8587 this_sectors = BB_MAX_LEN;
8588 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8589 sectors -= this_sectors;
8590 s += this_sectors;
8591 }
8592 }
8593
8594 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008595 if (!acknowledged)
8596 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008597 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008598
8599 return rv;
8600}
8601
NeilBrown3cb03002011-10-11 16:45:26 +11008602int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008603 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008604{
NeilBrownc6563a82012-05-21 09:27:00 +10008605 int rv;
8606 if (is_new)
8607 s += rdev->new_data_offset;
8608 else
8609 s += rdev->data_offset;
8610 rv = md_set_badblocks(&rdev->badblocks,
8611 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008612 if (rv) {
8613 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008614 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008615 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
NeilBrown55ce74d2015-08-14 11:11:10 +10008616 set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008617 md_wakeup_thread(rdev->mddev->thread);
8618 }
8619 return rv;
8620}
8621EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8622
8623/*
8624 * Remove a range of bad blocks from the table.
8625 * This may involve extending the table if we spilt a region,
8626 * but it must not fail. So if the table becomes full, we just
8627 * drop the remove request.
8628 */
8629static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8630{
8631 u64 *p;
8632 int lo, hi;
8633 sector_t target = s + sectors;
8634 int rv = 0;
8635
8636 if (bb->shift > 0) {
8637 /* When clearing we round the start up and the end down.
8638 * This should not matter as the shift should align with
8639 * the block size and no rounding should ever be needed.
8640 * However it is better the think a block is bad when it
8641 * isn't than to think a block is not bad when it is.
8642 */
8643 s += (1<<bb->shift) - 1;
8644 s >>= bb->shift;
8645 target >>= bb->shift;
8646 sectors = target - s;
8647 }
8648
8649 write_seqlock_irq(&bb->lock);
8650
8651 p = bb->page;
8652 lo = 0;
8653 hi = bb->count;
8654 /* Find the last range that starts before 'target' */
8655 while (hi - lo > 1) {
8656 int mid = (lo + hi) / 2;
8657 sector_t a = BB_OFFSET(p[mid]);
8658 if (a < target)
8659 lo = mid;
8660 else
8661 hi = mid;
8662 }
8663 if (hi > lo) {
8664 /* p[lo] is the last range that could overlap the
8665 * current range. Earlier ranges could also overlap,
8666 * but only this one can overlap the end of the range.
8667 */
8668 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8669 /* Partial overlap, leave the tail of this range */
8670 int ack = BB_ACK(p[lo]);
8671 sector_t a = BB_OFFSET(p[lo]);
8672 sector_t end = a + BB_LEN(p[lo]);
8673
8674 if (a < s) {
8675 /* we need to split this range */
8676 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008677 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008678 goto out;
8679 }
8680 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8681 bb->count++;
8682 p[lo] = BB_MAKE(a, s-a, ack);
8683 lo++;
8684 }
8685 p[lo] = BB_MAKE(target, end - target, ack);
8686 /* there is no longer an overlap */
8687 hi = lo;
8688 lo--;
8689 }
8690 while (lo >= 0 &&
8691 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8692 /* This range does overlap */
8693 if (BB_OFFSET(p[lo]) < s) {
8694 /* Keep the early parts of this range. */
8695 int ack = BB_ACK(p[lo]);
8696 sector_t start = BB_OFFSET(p[lo]);
8697 p[lo] = BB_MAKE(start, s - start, ack);
8698 /* now low doesn't overlap, so.. */
8699 break;
8700 }
8701 lo--;
8702 }
8703 /* 'lo' is strictly before, 'hi' is strictly after,
8704 * anything between needs to be discarded
8705 */
8706 if (hi - lo > 1) {
8707 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8708 bb->count -= (hi - lo - 1);
8709 }
8710 }
8711
8712 bb->changed = 1;
8713out:
8714 write_sequnlock_irq(&bb->lock);
8715 return rv;
8716}
8717
NeilBrownc6563a82012-05-21 09:27:00 +10008718int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8719 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008720{
NeilBrownc6563a82012-05-21 09:27:00 +10008721 if (is_new)
8722 s += rdev->new_data_offset;
8723 else
8724 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008725 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008726 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008727}
8728EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8729
8730/*
8731 * Acknowledge all bad blocks in a list.
8732 * This only succeeds if ->changed is clear. It is used by
8733 * in-kernel metadata updates
8734 */
8735void md_ack_all_badblocks(struct badblocks *bb)
8736{
8737 if (bb->page == NULL || bb->changed)
8738 /* no point even trying */
8739 return;
8740 write_seqlock_irq(&bb->lock);
8741
majianpengecb178b2012-03-19 12:46:42 +11008742 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008743 u64 *p = bb->page;
8744 int i;
8745 for (i = 0; i < bb->count ; i++) {
8746 if (!BB_ACK(p[i])) {
8747 sector_t start = BB_OFFSET(p[i]);
8748 int len = BB_LEN(p[i]);
8749 p[i] = BB_MAKE(start, len, 1);
8750 }
8751 }
NeilBrownde393cd2011-07-28 11:31:48 +10008752 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008753 }
8754 write_sequnlock_irq(&bb->lock);
8755}
8756EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8757
NeilBrown16c791a2011-07-28 11:31:47 +10008758/* sysfs access to bad-blocks list.
8759 * We present two files.
8760 * 'bad-blocks' lists sector numbers and lengths of ranges that
8761 * are recorded as bad. The list is truncated to fit within
8762 * the one-page limit of sysfs.
8763 * Writing "sector length" to this file adds an acknowledged
8764 * bad block list.
8765 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8766 * been acknowledged. Writing to this file adds bad blocks
8767 * without acknowledging them. This is largely for testing.
8768 */
8769
8770static ssize_t
8771badblocks_show(struct badblocks *bb, char *page, int unack)
8772{
8773 size_t len;
8774 int i;
8775 u64 *p = bb->page;
8776 unsigned seq;
8777
8778 if (bb->shift < 0)
8779 return 0;
8780
8781retry:
8782 seq = read_seqbegin(&bb->lock);
8783
8784 len = 0;
8785 i = 0;
8786
8787 while (len < PAGE_SIZE && i < bb->count) {
8788 sector_t s = BB_OFFSET(p[i]);
8789 unsigned int length = BB_LEN(p[i]);
8790 int ack = BB_ACK(p[i]);
8791 i++;
8792
8793 if (unack && ack)
8794 continue;
8795
8796 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8797 (unsigned long long)s << bb->shift,
8798 length << bb->shift);
8799 }
NeilBrownde393cd2011-07-28 11:31:48 +10008800 if (unack && len == 0)
8801 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008802
8803 if (read_seqretry(&bb->lock, seq))
8804 goto retry;
8805
8806 return len;
8807}
8808
8809#define DO_DEBUG 1
8810
8811static ssize_t
8812badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8813{
8814 unsigned long long sector;
8815 int length;
8816 char newline;
8817#ifdef DO_DEBUG
8818 /* Allow clearing via sysfs *only* for testing/debugging.
8819 * Normally only a successful write may clear a badblock
8820 */
8821 int clear = 0;
8822 if (page[0] == '-') {
8823 clear = 1;
8824 page++;
8825 }
8826#endif /* DO_DEBUG */
8827
8828 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8829 case 3:
8830 if (newline != '\n')
8831 return -EINVAL;
8832 case 2:
8833 if (length <= 0)
8834 return -EINVAL;
8835 break;
8836 default:
8837 return -EINVAL;
8838 }
8839
8840#ifdef DO_DEBUG
8841 if (clear) {
8842 md_clear_badblocks(bb, sector, length);
8843 return len;
8844 }
8845#endif /* DO_DEBUG */
8846 if (md_set_badblocks(bb, sector, length, !unack))
8847 return len;
8848 else
8849 return -ENOSPC;
8850}
8851
Adrian Bunk75c96f82005-05-05 16:16:09 -07008852static int md_notify_reboot(struct notifier_block *this,
8853 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854{
8855 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008856 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008857 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858
NeilBrownc744a652012-03-19 12:46:37 +11008859 for_each_mddev(mddev, tmp) {
8860 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008861 if (mddev->pers)
8862 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008863 if (mddev->persistent)
8864 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008865 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008866 }
NeilBrownc744a652012-03-19 12:46:37 +11008867 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868 }
NeilBrownc744a652012-03-19 12:46:37 +11008869 /*
8870 * certain more exotic SCSI devices are known to be
8871 * volatile wrt too early system reboots. While the
8872 * right place to handle this issue is the given
8873 * driver, we do want to have a safe RAID driver ...
8874 */
8875 if (need_delay)
8876 mdelay(1000*1);
8877
Linus Torvalds1da177e2005-04-16 15:20:36 -07008878 return NOTIFY_DONE;
8879}
8880
Adrian Bunk75c96f82005-05-05 16:16:09 -07008881static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882 .notifier_call = md_notify_reboot,
8883 .next = NULL,
8884 .priority = INT_MAX, /* before any real devices */
8885};
8886
8887static void md_geninit(void)
8888{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008889 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008891 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008892}
8893
Adrian Bunk75c96f82005-05-05 16:16:09 -07008894static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895{
Tejun Heoe804ac72010-10-15 15:36:08 +02008896 int ret = -ENOMEM;
8897
Tejun Heoada609e2011-01-25 14:35:54 +01008898 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008899 if (!md_wq)
8900 goto err_wq;
8901
8902 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8903 if (!md_misc_wq)
8904 goto err_misc_wq;
8905
8906 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8907 goto err_md;
8908
8909 if ((ret = register_blkdev(0, "mdp")) < 0)
8910 goto err_mdp;
8911 mdp_major = ret;
8912
NeilBrownaf5628f2014-07-31 13:54:54 +10008913 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008914 md_probe, NULL, NULL);
8915 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916 md_probe, NULL, NULL);
8917
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008919 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008920
8921 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923
Tejun Heoe804ac72010-10-15 15:36:08 +02008924err_mdp:
8925 unregister_blkdev(MD_MAJOR, "md");
8926err_md:
8927 destroy_workqueue(md_misc_wq);
8928err_misc_wq:
8929 destroy_workqueue(md_wq);
8930err_wq:
8931 return ret;
8932}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008934void md_reload_sb(struct mddev *mddev)
8935{
8936 struct md_rdev *rdev, *tmp;
8937
8938 rdev_for_each_safe(rdev, tmp, mddev) {
8939 rdev->sb_loaded = 0;
8940 ClearPageUptodate(rdev->sb_page);
8941 }
8942 mddev->raid_disks = 0;
8943 analyze_sbs(mddev);
8944 rdev_for_each_safe(rdev, tmp, mddev) {
8945 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8946 /* since we don't write to faulty devices, we figure out if the
8947 * disk is faulty by comparing events
8948 */
8949 if (mddev->events > sb->events)
8950 set_bit(Faulty, &rdev->flags);
8951 }
8952
8953}
8954EXPORT_SYMBOL(md_reload_sb);
8955
Linus Torvalds1da177e2005-04-16 15:20:36 -07008956#ifndef MODULE
8957
8958/*
8959 * Searches all registered partitions for autorun RAID arrays
8960 * at boot time.
8961 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008962
8963static LIST_HEAD(all_detected_devices);
8964struct detected_devices_node {
8965 struct list_head list;
8966 dev_t dev;
8967};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968
8969void md_autodetect_dev(dev_t dev)
8970{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008971 struct detected_devices_node *node_detected_dev;
8972
8973 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8974 if (node_detected_dev) {
8975 node_detected_dev->dev = dev;
8976 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8977 } else {
8978 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8979 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981}
8982
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983static void autostart_arrays(int part)
8984{
NeilBrown3cb03002011-10-11 16:45:26 +11008985 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008986 struct detected_devices_node *node_detected_dev;
8987 dev_t dev;
8988 int i_scanned, i_passed;
8989
8990 i_scanned = 0;
8991 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008992
8993 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8994
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008995 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8996 i_scanned++;
8997 node_detected_dev = list_entry(all_detected_devices.next,
8998 struct detected_devices_node, list);
8999 list_del(&node_detected_dev->list);
9000 dev = node_detected_dev->dev;
9001 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07009002 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009003 if (IS_ERR(rdev))
9004 continue;
9005
NeilBrown403df472014-09-30 15:52:29 +10009006 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009007 continue;
NeilBrown403df472014-09-30 15:52:29 +10009008
NeilBrownd0fae182008-03-04 14:29:31 -08009009 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009010 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009011 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009013
9014 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
9015 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009016
9017 autorun_devices(part);
9018}
9019
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009020#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021
9022static __exit void md_exit(void)
9023{
NeilBrownfd01b882011-10-11 16:47:53 +11009024 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009026 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009027
NeilBrownaf5628f2014-07-31 13:54:54 +10009028 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009029 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009031 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009032 unregister_blkdev(mdp_major, "mdp");
9033 unregister_reboot_notifier(&md_notifier);
9034 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009035
9036 /* We cannot unload the modules while some process is
9037 * waiting for us in select() or poll() - wake them up
9038 */
9039 md_unloading = 1;
9040 while (waitqueue_active(&md_event_waiters)) {
9041 /* not safe to leave yet */
9042 wake_up(&md_event_waiters);
9043 msleep(delay);
9044 delay += delay;
9045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009047
NeilBrown29ac4aa2008-02-06 01:39:58 -08009048 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009049 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009050 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009052 destroy_workqueue(md_misc_wq);
9053 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009054}
9055
Dan Williams685784a2007-07-09 11:56:42 -07009056subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057module_exit(md_exit)
9058
NeilBrownf91de922005-11-08 21:39:36 -08009059static int get_ro(char *buffer, struct kernel_param *kp)
9060{
9061 return sprintf(buffer, "%d", start_readonly);
9062}
9063static int set_ro(const char *val, struct kernel_param *kp)
9064{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009065 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009066}
9067
NeilBrown80ca3a42006-07-10 04:44:18 -07009068module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9069module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009070module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009071
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009073MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009074MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009075MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);