blob: 4d47a9ab8228f4b11e76945868f1dff6553a1b1f [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
NeilBrown0ca69882011-01-14 09:14:33 +1100260 if (mddev == NULL || mddev->pers == NULL
261 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100262 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200263 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100264 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100265 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
266 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
267 return;
268 }
NeilBrown0ca69882011-01-14 09:14:33 +1100269 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100270 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200271 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100272 DEFINE_WAIT(__wait);
273 for (;;) {
274 prepare_to_wait(&mddev->sb_wait, &__wait,
275 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200276 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100277 break;
278 rcu_read_unlock();
279 schedule();
280 rcu_read_lock();
281 }
282 finish_wait(&mddev->sb_wait, &__wait);
283 }
284 atomic_inc(&mddev->active_io);
285 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100286
Chris Masone91ece52011-02-07 19:21:48 -0500287 /*
288 * save the sectors now since our bio can
289 * go away inside make_request
290 */
291 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200292 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100293
Gu Zheng74672d02015-04-03 08:44:47 +0800294 cpu = part_stat_lock();
295 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
296 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
297 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100298
NeilBrown409c57f2009-03-31 14:39:39 +1100299 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
300 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
NeilBrown9e35b992010-04-06 14:23:02 +1000303/* mddev_suspend makes sure no new requests are submitted
304 * to the device, and that any requests that have been submitted
305 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100306 * Once mddev_detach() is called and completes, the module will be
307 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000308 */
NeilBrownfd01b882011-10-11 16:47:53 +1100309void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100310{
311 BUG_ON(mddev->suspended);
312 mddev->suspended = 1;
313 synchronize_rcu();
314 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
315 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500316
317 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100318}
NeilBrown390ee602010-06-01 19:37:27 +1000319EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100320
NeilBrownfd01b882011-10-11 16:47:53 +1100321void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100322{
323 mddev->suspended = 0;
324 wake_up(&mddev->sb_wait);
325 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500326
Jonathan Brassow47525e52012-05-22 13:55:29 +1000327 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500328 md_wakeup_thread(mddev->thread);
329 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100330}
NeilBrown390ee602010-06-01 19:37:27 +1000331EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100332
NeilBrownfd01b882011-10-11 16:47:53 +1100333int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000334{
NeilBrown5c675f82014-12-15 12:56:56 +1100335 struct md_personality *pers = mddev->pers;
336 int ret = 0;
337
338 rcu_read_lock();
339 if (mddev->suspended)
340 ret = 1;
341 else if (pers && pers->congested)
342 ret = pers->congested(mddev, bits);
343 rcu_read_unlock();
344 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000345}
NeilBrown5c675f82014-12-15 12:56:56 +1100346EXPORT_SYMBOL_GPL(mddev_congested);
347static int md_congested(void *data, int bits)
348{
349 struct mddev *mddev = data;
350 return mddev_congested(mddev, bits);
351}
NeilBrown3fa841d2009-09-23 18:10:29 +1000352
NeilBrown64590f42014-12-15 12:56:57 +1100353static int md_mergeable_bvec(struct request_queue *q,
354 struct bvec_merge_data *bvm,
355 struct bio_vec *biovec)
356{
357 struct mddev *mddev = q->queuedata;
358 int ret;
359 rcu_read_lock();
360 if (mddev->suspended) {
361 /* Must always allow one vec */
362 if (bvm->bi_size == 0)
363 ret = biovec->bv_len;
364 else
365 ret = 0;
366 } else {
367 struct md_personality *pers = mddev->pers;
368 if (pers && pers->mergeable_bvec)
369 ret = pers->mergeable_bvec(mddev, bvm, biovec);
370 else
371 ret = biovec->bv_len;
372 }
373 rcu_read_unlock();
374 return ret;
375}
NeilBrowna2826aa2009-12-14 12:49:49 +1100376/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200377 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100378 */
379
Tejun Heoe9c74692010-09-03 11:56:18 +0200380static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100381{
NeilBrown3cb03002011-10-11 16:45:26 +1100382 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100383 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100384
385 rdev_dec_pending(rdev, mddev);
386
387 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200388 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200389 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100390 }
391 bio_put(bio);
392}
393
NeilBrowna7a07e62010-12-09 16:04:25 +1100394static void md_submit_flush_data(struct work_struct *ws);
395
NeilBrowna035fc32010-12-09 16:17:51 +1100396static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100397{
NeilBrownfd01b882011-10-11 16:47:53 +1100398 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100399 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100400
NeilBrowna7a07e62010-12-09 16:04:25 +1100401 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
402 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100403 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100404 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100405 if (rdev->raid_disk >= 0 &&
406 !test_bit(Faulty, &rdev->flags)) {
407 /* Take two references, one is dropped
408 * when request finishes, one after
409 * we reclaim rcu_read_lock
410 */
411 struct bio *bi;
412 atomic_inc(&rdev->nr_pending);
413 atomic_inc(&rdev->nr_pending);
414 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000415 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200416 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100417 bi->bi_private = rdev;
418 bi->bi_bdev = rdev->bdev;
419 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200420 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100421 rcu_read_lock();
422 rdev_dec_pending(rdev, mddev);
423 }
424 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100425 if (atomic_dec_and_test(&mddev->flush_pending))
426 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100427}
428
Tejun Heoe9c74692010-09-03 11:56:18 +0200429static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100430{
NeilBrownfd01b882011-10-11 16:47:53 +1100431 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200432 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100433
Kent Overstreet4f024f32013-10-11 15:44:27 -0700434 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100435 /* an empty barrier - all done */
436 bio_endio(bio, 0);
437 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200438 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200439 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100440 }
NeilBrown2b74e122010-12-09 15:59:01 +1100441
442 mddev->flush_bio = NULL;
443 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444}
445
NeilBrownfd01b882011-10-11 16:47:53 +1100446void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100447{
NeilBrown85572d72014-12-15 12:56:56 +1100448 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100449 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200450 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100451 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200452 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100453 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100454
NeilBrowna035fc32010-12-09 16:17:51 +1100455 INIT_WORK(&mddev->flush_work, submit_flushes);
456 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100457}
Tejun Heoe9c74692010-09-03 11:56:18 +0200458EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100459
NeilBrown74018dc2012-07-31 09:08:15 +0200460void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000461{
NeilBrown9cbb1752012-07-31 09:08:14 +0200462 struct mddev *mddev = cb->data;
463 md_wakeup_thread(mddev->thread);
464 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000465}
NeilBrown9cbb1752012-07-31 09:08:14 +0200466EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000467
NeilBrownfd01b882011-10-11 16:47:53 +1100468static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 atomic_inc(&mddev->active);
471 return mddev;
472}
473
Dan Williams5fd3a172009-03-04 00:57:25 -0700474static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100475
NeilBrownfd01b882011-10-11 16:47:53 +1100476static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
NeilBrowna167f662010-10-26 18:31:13 +1100478 struct bio_set *bs = NULL;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
481 return;
NeilBrownd3374822009-01-09 08:31:10 +1100482 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100483 mddev->ctime == 0 && !mddev->hold_active) {
484 /* Array is not configured at all, and not held active,
485 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100486 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100487 bs = mddev->bio_set;
488 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100489 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200490 /* We did a probe so need to clean up. Call
491 * queue_work inside the spinlock so that
492 * flush_workqueue() after mddev_find will
493 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100494 */
495 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200496 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100497 } else
498 kfree(mddev);
499 }
500 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100501 if (bs)
502 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
NeilBrownfd01b882011-10-11 16:47:53 +1100505void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100506{
507 mutex_init(&mddev->open_mutex);
508 mutex_init(&mddev->reconfig_mutex);
509 mutex_init(&mddev->bitmap_info.mutex);
510 INIT_LIST_HEAD(&mddev->disks);
511 INIT_LIST_HEAD(&mddev->all_mddevs);
512 init_timer(&mddev->safemode_timer);
513 atomic_set(&mddev->active, 1);
514 atomic_set(&mddev->openers, 0);
515 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100516 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100517 atomic_set(&mddev->flush_pending, 0);
518 init_waitqueue_head(&mddev->sb_wait);
519 init_waitqueue_head(&mddev->recovery_wait);
520 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000521 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500522 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100523 mddev->resync_min = 0;
524 mddev->resync_max = MaxSector;
525 mddev->level = LEVEL_NONE;
526}
NeilBrown390ee602010-06-01 19:37:27 +1000527EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100528
NeilBrownf72ffdd2014-09-30 14:23:59 +1000529static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
NeilBrownfd01b882011-10-11 16:47:53 +1100531 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
NeilBrown8f5f02c2011-02-16 13:58:51 +1100533 if (unit && MAJOR(unit) != MD_MAJOR)
534 unit &= ~((1<<MdpMinorShift)-1);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 retry:
537 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
NeilBrownefeb53c2009-01-09 08:31:10 +1100539 if (unit) {
540 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
541 if (mddev->unit == unit) {
542 mddev_get(mddev);
543 spin_unlock(&all_mddevs_lock);
544 kfree(new);
545 return mddev;
546 }
547
548 if (new) {
549 list_add(&new->all_mddevs, &all_mddevs);
550 spin_unlock(&all_mddevs_lock);
551 new->hold_active = UNTIL_IOCTL;
552 return new;
553 }
554 } else if (new) {
555 /* find an unused unit number */
556 static int next_minor = 512;
557 int start = next_minor;
558 int is_free = 0;
559 int dev = 0;
560 while (!is_free) {
561 dev = MKDEV(MD_MAJOR, next_minor);
562 next_minor++;
563 if (next_minor > MINORMASK)
564 next_minor = 0;
565 if (next_minor == start) {
566 /* Oh dear, all in use. */
567 spin_unlock(&all_mddevs_lock);
568 kfree(new);
569 return NULL;
570 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000571
NeilBrownefeb53c2009-01-09 08:31:10 +1100572 is_free = 1;
573 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
574 if (mddev->unit == dev) {
575 is_free = 0;
576 break;
577 }
578 }
579 new->unit = dev;
580 new->md_minor = MINOR(dev);
581 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 list_add(&new->all_mddevs, &all_mddevs);
583 spin_unlock(&all_mddevs_lock);
584 return new;
585 }
586 spin_unlock(&all_mddevs_lock);
587
NeilBrown9ffae0c2006-01-06 00:20:32 -0800588 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!new)
590 return NULL;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 new->unit = unit;
593 if (MAJOR(unit) == MD_MAJOR)
594 new->md_minor = MINOR(unit);
595 else
596 new->md_minor = MINOR(unit) >> MdpMinorShift;
597
NeilBrownfafd7fb2010-04-01 15:55:30 +1100598 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto retry;
601}
602
NeilBrownb6eb1272010-04-15 10:13:47 +1000603static struct attribute_group md_redundancy_group;
604
NeilBrown5c47daf2014-12-15 12:57:01 +1100605void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
NeilBrowna64c8762010-04-14 17:15:37 +1000607 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000608 /* These cannot be removed under reconfig_mutex as
609 * an access to the files will try to take reconfig_mutex
610 * while holding the file unremovable, which leads to
611 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000612 * So hold set sysfs_active while the remove in happeing,
613 * and anything else which might set ->to_remove or my
614 * otherwise change the sysfs namespace will fail with
615 * -EBUSY if sysfs_active is still set.
616 * We set sysfs_active under reconfig_mutex and elsewhere
617 * test it under the same mutex to ensure its correct value
618 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000619 */
NeilBrowna64c8762010-04-14 17:15:37 +1000620 struct attribute_group *to_remove = mddev->to_remove;
621 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000622 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000623 mutex_unlock(&mddev->reconfig_mutex);
624
NeilBrown00bcb4a2010-06-01 19:37:23 +1000625 if (mddev->kobj.sd) {
626 if (to_remove != &md_redundancy_group)
627 sysfs_remove_group(&mddev->kobj, to_remove);
628 if (mddev->pers == NULL ||
629 mddev->pers->sync_request == NULL) {
630 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
631 if (mddev->sysfs_action)
632 sysfs_put(mddev->sysfs_action);
633 mddev->sysfs_action = NULL;
634 }
NeilBrowna64c8762010-04-14 17:15:37 +1000635 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000636 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000637 } else
638 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Chris Dunlop751e67c2011-10-19 16:48:26 +1100640 /* As we've dropped the mutex we need a spinlock to
641 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000642 */
643 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700644 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000645 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
NeilBrown5c47daf2014-12-15 12:57:01 +1100647EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500649struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100650{
651 struct md_rdev *rdev;
652
653 rdev_for_each_rcu(rdev, mddev)
654 if (rdev->desc_nr == nr)
655 return rdev;
656
657 return NULL;
658}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500659EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100660
661static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
NeilBrown3cb03002011-10-11 16:45:26 +1100663 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
NeilBrowndafb20f2012-03-19 12:46:39 +1100665 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (rdev->bdev->bd_dev == dev)
667 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return NULL;
670}
671
NeilBrown1ca69c42012-10-11 13:37:33 +1100672static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
673{
674 struct md_rdev *rdev;
675
676 rdev_for_each_rcu(rdev, mddev)
677 if (rdev->bdev->bd_dev == dev)
678 return rdev;
679
680 return NULL;
681}
682
NeilBrown84fc4b52011-10-11 16:49:58 +1100683static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800684{
NeilBrown84fc4b52011-10-11 16:49:58 +1100685 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800686 list_for_each_entry(pers, &pers_list, list) {
687 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800688 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800689 if (strcmp(pers->name, clevel)==0)
690 return pers;
691 }
NeilBrown2604b702006-01-06 00:20:36 -0800692 return NULL;
693}
694
Andre Nollb73df2d2008-07-11 22:02:23 +1000695/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100696static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100698 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000699 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
NeilBrownf72ffdd2014-09-30 14:23:59 +1000702static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rdev->sb_page = alloc_page(GFP_KERNEL);
705 if (!rdev->sb_page) {
706 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000707 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
710 return 0;
711}
712
NeilBrown545c8792012-05-22 13:54:30 +1000713void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800716 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 rdev->sb_loaded = 0;
718 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000719 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100720 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
NeilBrown2699b672011-07-28 11:31:47 +1000722 if (rdev->bb_page) {
723 put_page(rdev->bb_page);
724 rdev->bb_page = NULL;
725 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000726 kfree(rdev->badblocks.page);
727 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
NeilBrown545c8792012-05-22 13:54:30 +1000729EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
NeilBrown6712ecf2007-09-27 12:47:43 +0200731static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700732{
NeilBrown3cb03002011-10-11 16:45:26 +1100733 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100734 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700735
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700736 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
737 printk("md: super_written gets error=%d, uptodate=%d\n",
738 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
739 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800740 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700741 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700742
NeilBrowna9701a32005-11-08 21:39:34 -0800743 if (atomic_dec_and_test(&mddev->pending_writes))
744 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700745 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746}
747
NeilBrownfd01b882011-10-11 16:47:53 +1100748void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700749 sector_t sector, int size, struct page *page)
750{
751 /* write first size bytes of page to sector of rdev
752 * Increment mddev->pending_writes before returning
753 * and decrement it on completion, waking up sb_wait
754 * if zero is reached.
755 * If an error occurred, call md_error
756 */
NeilBrowna167f662010-10-26 18:31:13 +1100757 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700758
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100759 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700760 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700761 bio_add_page(bio, page, size, 0);
762 bio->bi_private = rdev;
763 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800764
NeilBrown7bfa19f2005-06-21 17:17:28 -0700765 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000766 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800767}
768
NeilBrownfd01b882011-10-11 16:47:53 +1100769void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800770{
Tejun Heoe9c74692010-09-03 11:56:18 +0200771 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000772 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700773}
774
NeilBrown3cb03002011-10-11 16:45:26 +1100775int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100776 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
NeilBrowna167f662010-10-26 18:31:13 +1100778 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int ret;
780
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100781 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
782 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100783 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700784 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000785 else if (rdev->mddev->reshape_position != MaxSector &&
786 (rdev->mddev->reshape_backwards ==
787 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700788 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100789 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700790 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700792 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
795 bio_put(bio);
796 return ret;
797}
NeilBrowna8745db2006-01-06 00:20:34 -0800798EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
NeilBrownf72ffdd2014-09-30 14:23:59 +1000800static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (rdev->sb_loaded)
805 return 0;
806
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100807 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto fail;
809 rdev->sb_loaded = 1;
810 return 0;
811
812fail:
813 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
814 bdevname(rdev->bdev,b));
815 return -EINVAL;
816}
817
818static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
819{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000820 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000821 sb1->set_uuid1 == sb2->set_uuid1 &&
822 sb1->set_uuid2 == sb2->set_uuid2 &&
823 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
827{
828 int ret;
829 mdp_super_t *tmp1, *tmp2;
830
831 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
832 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
833
834 if (!tmp1 || !tmp2) {
835 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100836 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto abort;
838 }
839
840 *tmp1 = *sb1;
841 *tmp2 = *sb2;
842
843 /*
844 * nr_disks is not constant
845 */
846 tmp1->nr_disks = 0;
847 tmp2->nr_disks = 0;
848
Andre Nollce0c8e02008-07-11 22:02:20 +1000849 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700851 kfree(tmp1);
852 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return ret;
854}
855
NeilBrown4d167f02007-05-09 02:35:37 -0700856static u32 md_csum_fold(u32 csum)
857{
858 csum = (csum & 0xffff) + (csum >> 16);
859 return (csum & 0xffff) + (csum >> 16);
860}
861
NeilBrownf72ffdd2014-09-30 14:23:59 +1000862static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
NeilBrown4d167f02007-05-09 02:35:37 -0700864 u64 newcsum = 0;
865 u32 *sb32 = (u32*)sb;
866 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 unsigned int disk_csum, csum;
868
869 disk_csum = sb->sb_csum;
870 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700871
872 for (i = 0; i < MD_SB_BYTES/4 ; i++)
873 newcsum += sb32[i];
874 csum = (newcsum & 0xffffffff) + (newcsum>>32);
875
NeilBrown4d167f02007-05-09 02:35:37 -0700876#ifdef CONFIG_ALPHA
877 /* This used to use csum_partial, which was wrong for several
878 * reasons including that different results are returned on
879 * different architectures. It isn't critical that we get exactly
880 * the same return value as before (we always csum_fold before
881 * testing, and that removes any differences). However as we
882 * know that csum_partial always returned a 16bit value on
883 * alphas, do a fold to maximise conformity to previous behaviour.
884 */
885 sb->sb_csum = md_csum_fold(disk_csum);
886#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return csum;
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
893 * Handle superblock details.
894 * We want to be able to handle multiple superblock formats
895 * so we have a common interface to them all, and an array of
896 * different handlers.
897 * We rely on user-space to write the initial superblock, and support
898 * reading and updating of superblocks.
899 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100900 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 * loads and validates a superblock on dev.
902 * if refdev != NULL, compare superblocks on both devices
903 * Return:
904 * 0 - dev has a superblock that is compatible with refdev
905 * 1 - dev has a superblock that is compatible and newer than refdev
906 * so dev should be used as the refdev in future
907 * -EINVAL superblock incompatible or invalid
908 * -othererror e.g. -EIO
909 *
NeilBrownfd01b882011-10-11 16:47:53 +1100910 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * Verify that dev is acceptable into mddev.
912 * The first time, mddev->raid_disks will be 0, and data from
913 * dev should be merged in. Subsequent calls check that dev
914 * is new enough. Return 0 or -EINVAL
915 *
NeilBrownfd01b882011-10-11 16:47:53 +1100916 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 * Update the superblock for rdev with data in mddev
918 * This does not write to disc.
919 *
920 */
921
922struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000923 char *name;
924 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000925 int (*load_super)(struct md_rdev *rdev,
926 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000927 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000928 int (*validate_super)(struct mddev *mddev,
929 struct md_rdev *rdev);
930 void (*sync_super)(struct mddev *mddev,
931 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100932 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000933 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000934 int (*allow_new_offset)(struct md_rdev *rdev,
935 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936};
937
938/*
Andre Noll0894cc32009-06-18 08:49:23 +1000939 * Check that the given mddev has no bitmap.
940 *
941 * This function is called from the run method of all personalities that do not
942 * support bitmaps. It prints an error message and returns non-zero if mddev
943 * has a bitmap. Otherwise, it returns 0.
944 *
945 */
NeilBrownfd01b882011-10-11 16:47:53 +1100946int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000947{
NeilBrownc3d97142009-12-14 12:49:52 +1100948 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000949 return 0;
950 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
951 mdname(mddev), mddev->pers->name);
952 return 1;
953}
954EXPORT_SYMBOL(md_check_no_bitmap);
955
956/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000957 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
NeilBrown3cb03002011-10-11 16:45:26 +1100959static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
962 mdp_super_t *sb;
963 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /*
Andre Noll0f420352008-07-11 22:02:23 +1000966 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * it's at the end of the disk.
968 *
969 * It also happens to be a multiple of 4Kb.
970 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100971 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
NeilBrown0002b272005-09-09 16:23:53 -0700973 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (ret) return ret;
975
976 ret = -EINVAL;
977
978 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000979 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (sb->md_magic != MD_SB_MAGIC) {
982 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
983 b);
984 goto abort;
985 }
986
987 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800988 sb->minor_version < 90 ||
989 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
991 sb->major_version, sb->minor_version,
992 b);
993 goto abort;
994 }
995
996 if (sb->raid_disks <= 0)
997 goto abort;
998
NeilBrown4d167f02007-05-09 02:35:37 -0700999 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1001 b);
1002 goto abort;
1003 }
1004
1005 rdev->preferred_minor = sb->md_minor;
1006 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001007 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001008 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001009 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 if (sb->level == LEVEL_MULTIPATH)
1012 rdev->desc_nr = -1;
1013 else
1014 rdev->desc_nr = sb->this_disk.number;
1015
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001016 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001018 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001020 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!uuid_equal(refsb, sb)) {
1022 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1023 b, bdevname(refdev->bdev,b2));
1024 goto abort;
1025 }
1026 if (!sb_equal(refsb, sb)) {
1027 printk(KERN_WARNING "md: %s has same UUID"
1028 " but different superblock to %s\n",
1029 b, bdevname(refdev->bdev, b2));
1030 goto abort;
1031 }
1032 ev1 = md_event(sb);
1033 ev2 = md_event(refsb);
1034 if (ev1 > ev2)
1035 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001036 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 ret = 0;
1038 }
NeilBrown8190e752009-06-18 08:48:58 +10001039 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001040 /* Limit to 4TB as metadata cannot record more than that.
1041 * (not needed for Linear and RAID0 as metadata doesn't
1042 * record this size)
1043 */
1044 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001045 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
NeilBrown27a7b262011-09-10 17:21:28 +10001047 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001048 /* "this cannot possibly happen" ... */
1049 ret = -EINVAL;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 abort:
1052 return ret;
1053}
1054
1055/*
1056 * validate_super for 0.90.0
1057 */
NeilBrownfd01b882011-10-11 16:47:53 +11001058static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001061 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001062 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
NeilBrown41158c72005-06-21 17:17:25 -07001064 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001065 clear_bit(Faulty, &rdev->flags);
1066 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001067 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001068 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (mddev->raid_disks == 0) {
1071 mddev->major_version = 0;
1072 mddev->minor_version = sb->minor_version;
1073 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001074 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001075 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 mddev->ctime = sb->ctime;
1077 mddev->utime = sb->utime;
1078 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001079 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 mddev->layout = sb->layout;
1081 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001082 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001083 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001084 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001085 mddev->bitmap_info.space = 0;
1086 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001087 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001088 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001089 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
NeilBrownf6705572006-03-27 01:18:11 -08001091 if (mddev->minor_version >= 91) {
1092 mddev->reshape_position = sb->reshape_position;
1093 mddev->delta_disks = sb->delta_disks;
1094 mddev->new_level = sb->new_level;
1095 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001096 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001097 if (mddev->delta_disks < 0)
1098 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001099 } else {
1100 mddev->reshape_position = MaxSector;
1101 mddev->delta_disks = 0;
1102 mddev->new_level = mddev->level;
1103 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001104 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001105 }
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (sb->state & (1<<MD_SB_CLEAN))
1108 mddev->recovery_cp = MaxSector;
1109 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001110 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 sb->events_lo == sb->cp_events_lo) {
1112 mddev->recovery_cp = sb->recovery_cp;
1113 } else
1114 mddev->recovery_cp = 0;
1115 }
1116
1117 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1118 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1119 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1120 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1121
1122 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001123
1124 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001125 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001126 mddev->bitmap_info.offset =
1127 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001128 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001129 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001130 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001131
NeilBrown41158c72005-06-21 17:17:25 -07001132 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001133 /* Insist on good event counter while assembling, except
1134 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001136 if (sb->disks[rdev->desc_nr].state & (
1137 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001138 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001139 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001140 } else if (mddev->bitmap) {
1141 /* if adding to array with a bitmap, then we can accept an
1142 * older device ... but not too old.
1143 */
NeilBrown41158c72005-06-21 17:17:25 -07001144 if (ev1 < mddev->bitmap->events_cleared)
1145 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001146 if (ev1 < mddev->events)
1147 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001148 } else {
1149 if (ev1 < mddev->events)
1150 /* just a hot-add of a new device, leave raid_disk at -1 */
1151 return 0;
1152 }
NeilBrown41158c72005-06-21 17:17:25 -07001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 desc = sb->disks + rdev->desc_nr;
1156
1157 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001158 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001159 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1160 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001161 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001163 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001164 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1165 /* active but not in sync implies recovery up to
1166 * reshape position. We don't know exactly where
1167 * that is, so set to zero for now */
1168 if (mddev->minor_version >= 91) {
1169 rdev->recovery_offset = 0;
1170 rdev->raid_disk = desc->raid_disk;
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001173 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1174 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001175 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001176 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return 0;
1178}
1179
1180/*
1181 * sync_super for 0.90.0
1182 */
NeilBrownfd01b882011-10-11 16:47:53 +11001183static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001186 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /* make rdev->sb match mddev data..
1190 *
1191 * 1/ zero out disks
1192 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1193 * 3/ any empty disks < next_spare become removed
1194 *
1195 * disks[0] gets initialised to REMOVED because
1196 * we cannot be sure from other fields if it has
1197 * been initialised or not.
1198 */
1199 int i;
1200 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1201
NeilBrown61181562005-09-09 16:24:02 -07001202 rdev->sb_size = MD_SB_BYTES;
1203
Namhyung Kim65a06f062011-07-27 11:00:36 +10001204 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 memset(sb, 0, sizeof(*sb));
1207
1208 sb->md_magic = MD_SB_MAGIC;
1209 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 sb->patch_version = mddev->patch_version;
1211 sb->gvalid_words = 0; /* ignored */
1212 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1213 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1214 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1215 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1216
1217 sb->ctime = mddev->ctime;
1218 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001219 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 sb->raid_disks = mddev->raid_disks;
1221 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001222 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 sb->utime = mddev->utime;
1224 sb->state = 0;
1225 sb->events_hi = (mddev->events>>32);
1226 sb->events_lo = (u32)mddev->events;
1227
NeilBrownf6705572006-03-27 01:18:11 -08001228 if (mddev->reshape_position == MaxSector)
1229 sb->minor_version = 90;
1230 else {
1231 sb->minor_version = 91;
1232 sb->reshape_position = mddev->reshape_position;
1233 sb->new_level = mddev->new_level;
1234 sb->delta_disks = mddev->delta_disks;
1235 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001236 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001237 }
1238 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (mddev->in_sync)
1240 {
1241 sb->recovery_cp = mddev->recovery_cp;
1242 sb->cp_events_hi = (mddev->events>>32);
1243 sb->cp_events_lo = (u32)mddev->events;
1244 if (mddev->recovery_cp == MaxSector)
1245 sb->state = (1<< MD_SB_CLEAN);
1246 } else
1247 sb->recovery_cp = 0;
1248
1249 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001250 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
NeilBrownc3d97142009-12-14 12:49:52 +11001252 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001253 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001256 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001258 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001259 int is_active = test_bit(In_sync, &rdev2->flags);
1260
1261 if (rdev2->raid_disk >= 0 &&
1262 sb->minor_version >= 91)
1263 /* we have nowhere to store the recovery_offset,
1264 * but if it is not below the reshape_position,
1265 * we can piggy-back on that.
1266 */
1267 is_active = 1;
1268 if (rdev2->raid_disk < 0 ||
1269 test_bit(Faulty, &rdev2->flags))
1270 is_active = 0;
1271 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001272 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001274 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001275 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 d = &sb->disks[rdev2->desc_nr];
1277 nr_disks++;
1278 d->number = rdev2->desc_nr;
1279 d->major = MAJOR(rdev2->bdev->bd_dev);
1280 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001281 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 d->raid_disk = rdev2->raid_disk;
1283 else
1284 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001285 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001287 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001289 if (test_bit(In_sync, &rdev2->flags))
1290 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 active++;
1292 working++;
1293 } else {
1294 d->state = 0;
1295 spare++;
1296 working++;
1297 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001298 if (test_bit(WriteMostly, &rdev2->flags))
1299 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /* now set the "removed" and "faulty" bits on any missing devices */
1302 for (i=0 ; i < mddev->raid_disks ; i++) {
1303 mdp_disk_t *d = &sb->disks[i];
1304 if (d->state == 0 && d->number == 0) {
1305 d->number = i;
1306 d->raid_disk = i;
1307 d->state = (1<<MD_DISK_REMOVED);
1308 d->state |= (1<<MD_DISK_FAULTY);
1309 failed++;
1310 }
1311 }
1312 sb->nr_disks = nr_disks;
1313 sb->active_disks = active;
1314 sb->working_disks = working;
1315 sb->failed_disks = failed;
1316 sb->spare_disks = spare;
1317
1318 sb->this_disk = sb->disks[rdev->desc_nr];
1319 sb->sb_csum = calc_sb_csum(sb);
1320}
1321
1322/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001323 * rdev_size_change for 0.90.0
1324 */
1325static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001326super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001327{
Andre Noll58c0fed2009-03-31 14:33:13 +11001328 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001329 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001330 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001331 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001332 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001333 if (!num_sectors || num_sectors > rdev->sb_start)
1334 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001335 /* Limit to 4TB as metadata cannot record more than that.
1336 * 4TB == 2^32 KB, or 2*2^32 sectors.
1337 */
NeilBrown667a5312012-08-16 16:46:12 +10001338 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001339 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001340 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001341 rdev->sb_page);
1342 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001343 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001344}
1345
NeilBrownc6563a82012-05-21 09:27:00 +10001346static int
1347super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1348{
1349 /* non-zero offset changes not possible with v0.90 */
1350 return new_offset == 0;
1351}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001352
1353/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 * version 1 superblock
1355 */
1356
NeilBrownf72ffdd2014-09-30 14:23:59 +10001357static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001359 __le32 disk_csum;
1360 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 unsigned long long newcsum;
1362 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001363 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 disk_csum = sb->sb_csum;
1366 sb->sb_csum = 0;
1367 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001368 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 newcsum += le32_to_cpu(*isuper++);
1370
1371 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001372 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1375 sb->sb_csum = disk_csum;
1376 return cpu_to_le32(csum);
1377}
1378
NeilBrown2699b672011-07-28 11:31:47 +10001379static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1380 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001381static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 struct mdp_superblock_1 *sb;
1384 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001385 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001386 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001388 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 /*
Andre Noll0f420352008-07-11 22:02:23 +10001391 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * It is always aligned to a 4K boundary and
1393 * depeding on minor_version, it can be:
1394 * 0: At least 8K, but less than 12K, from end of device
1395 * 1: At start of device
1396 * 2: 4K from start of device.
1397 */
1398 switch(minor_version) {
1399 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001400 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001401 sb_start -= 8*2;
1402 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 break;
1404 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001405 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 break;
1407 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001408 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 break;
1410 default:
1411 return -EINVAL;
1412 }
Andre Noll0f420352008-07-11 22:02:23 +10001413 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
NeilBrown0002b272005-09-09 16:23:53 -07001415 /* superblock is rarely larger than 1K, but it can be larger,
1416 * and it is safe to read 4k, so we do that
1417 */
1418 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (ret) return ret;
1420
Namhyung Kim65a06f062011-07-27 11:00:36 +10001421 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1424 sb->major_version != cpu_to_le32(1) ||
1425 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001426 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001427 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return -EINVAL;
1429
1430 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1431 printk("md: invalid superblock checksum on %s\n",
1432 bdevname(rdev->bdev,b));
1433 return -EINVAL;
1434 }
1435 if (le64_to_cpu(sb->data_size) < 10) {
1436 printk("md: data_size too small on %s\n",
1437 bdevname(rdev->bdev,b));
1438 return -EINVAL;
1439 }
NeilBrownc6563a82012-05-21 09:27:00 +10001440 if (sb->pad0 ||
1441 sb->pad3[0] ||
1442 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1443 /* Some padding is non-zero, might be a new feature */
1444 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 rdev->preferred_minor = 0xffff;
1447 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001448 rdev->new_data_offset = rdev->data_offset;
1449 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1450 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1451 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001452 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
NeilBrown0002b272005-09-09 16:23:53 -07001454 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001455 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001456 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001457 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1458
1459 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001460 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001461 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001462 if (minor_version
1463 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1464 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001465
NeilBrown31b65a02006-07-10 04:44:14 -07001466 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1467 rdev->desc_nr = -1;
1468 else
1469 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1470
NeilBrown2699b672011-07-28 11:31:47 +10001471 if (!rdev->bb_page) {
1472 rdev->bb_page = alloc_page(GFP_KERNEL);
1473 if (!rdev->bb_page)
1474 return -ENOMEM;
1475 }
1476 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1477 rdev->badblocks.count == 0) {
1478 /* need to load the bad block list.
1479 * Currently we limit it to one page.
1480 */
1481 s32 offset;
1482 sector_t bb_sector;
1483 u64 *bbp;
1484 int i;
1485 int sectors = le16_to_cpu(sb->bblog_size);
1486 if (sectors > (PAGE_SIZE / 512))
1487 return -EINVAL;
1488 offset = le32_to_cpu(sb->bblog_offset);
1489 if (offset == 0)
1490 return -EINVAL;
1491 bb_sector = (long long)offset;
1492 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1493 rdev->bb_page, READ, true))
1494 return -EIO;
1495 bbp = (u64 *)page_address(rdev->bb_page);
1496 rdev->badblocks.shift = sb->bblog_shift;
1497 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1498 u64 bb = le64_to_cpu(*bbp);
1499 int count = bb & (0x3ff);
1500 u64 sector = bb >> 10;
1501 sector <<= sb->bblog_shift;
1502 count <<= sb->bblog_shift;
1503 if (bb + 1 == 0)
1504 break;
1505 if (md_set_badblocks(&rdev->badblocks,
1506 sector, count, 1) == 0)
1507 return -EINVAL;
1508 }
NeilBrown486adf72013-04-24 11:42:44 +10001509 } else if (sb->bblog_offset != 0)
1510 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001511
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001512 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001513 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001514 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001516 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1519 sb->level != refsb->level ||
1520 sb->layout != refsb->layout ||
1521 sb->chunksize != refsb->chunksize) {
1522 printk(KERN_WARNING "md: %s has strangely different"
1523 " superblock to %s\n",
1524 bdevname(rdev->bdev,b),
1525 bdevname(refdev->bdev,b2));
1526 return -EINVAL;
1527 }
1528 ev1 = le64_to_cpu(sb->events);
1529 ev2 = le64_to_cpu(refsb->events);
1530
1531 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001532 ret = 1;
1533 else
1534 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
NeilBrownc6563a82012-05-21 09:27:00 +10001536 if (minor_version) {
1537 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1538 sectors -= rdev->data_offset;
1539 } else
1540 sectors = rdev->sb_start;
1541 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001543 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001544 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
NeilBrownfd01b882011-10-11 16:47:53 +11001547static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001549 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001550 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
NeilBrown41158c72005-06-21 17:17:25 -07001552 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001553 clear_bit(Faulty, &rdev->flags);
1554 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001555 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001556 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (mddev->raid_disks == 0) {
1559 mddev->major_version = 1;
1560 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001561 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001562 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1564 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1565 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001566 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 mddev->layout = le32_to_cpu(sb->layout);
1568 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001569 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001570 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001571 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001572 mddev->bitmap_info.space = 0;
1573 /* Default location for bitmap is 1K after superblock
1574 * using 3K - total of 4K
1575 */
NeilBrownc3d97142009-12-14 12:49:52 +11001576 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001577 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001578 mddev->reshape_backwards = 0;
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1581 memcpy(mddev->uuid, sb->set_uuid, 16);
1582
1583 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001584
NeilBrown71c08052005-09-09 16:23:51 -07001585 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001586 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001587 mddev->bitmap_info.offset =
1588 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001589 /* Metadata doesn't record how much space is available.
1590 * For 1.0, we assume we can use up to the superblock
1591 * if before, else to 4K beyond superblock.
1592 * For others, assume no change is possible.
1593 */
1594 if (mddev->minor_version > 0)
1595 mddev->bitmap_info.space = 0;
1596 else if (mddev->bitmap_info.offset > 0)
1597 mddev->bitmap_info.space =
1598 8 - mddev->bitmap_info.offset;
1599 else
1600 mddev->bitmap_info.space =
1601 -mddev->bitmap_info.offset;
1602 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001603
NeilBrownf6705572006-03-27 01:18:11 -08001604 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1605 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1606 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1607 mddev->new_level = le32_to_cpu(sb->new_level);
1608 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001609 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001610 if (mddev->delta_disks < 0 ||
1611 (mddev->delta_disks == 0 &&
1612 (le32_to_cpu(sb->feature_map)
1613 & MD_FEATURE_RESHAPE_BACKWARDS)))
1614 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001615 } else {
1616 mddev->reshape_position = MaxSector;
1617 mddev->delta_disks = 0;
1618 mddev->new_level = mddev->level;
1619 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001620 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001621 }
1622
NeilBrown41158c72005-06-21 17:17:25 -07001623 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001624 /* Insist of good event counter while assembling, except for
1625 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001627 if (rdev->desc_nr >= 0 &&
1628 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1629 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1630 if (ev1 < mddev->events)
1631 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001632 } else if (mddev->bitmap) {
1633 /* If adding to array with a bitmap, then we can accept an
1634 * older device, but not too old.
1635 */
NeilBrown41158c72005-06-21 17:17:25 -07001636 if (ev1 < mddev->bitmap->events_cleared)
1637 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001638 if (ev1 < mddev->events)
1639 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001640 } else {
1641 if (ev1 < mddev->events)
1642 /* just a hot-add of a new device, leave raid_disk at -1 */
1643 return 0;
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (mddev->level != LEVEL_MULTIPATH) {
1646 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001647 if (rdev->desc_nr < 0 ||
1648 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1649 role = 0xffff;
1650 rdev->desc_nr = -1;
1651 } else
1652 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 switch(role) {
1654 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 break;
1656 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001657 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 break;
1659 default:
NeilBrownf4667222013-12-09 12:04:56 +11001660 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001661 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001662 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001663 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001664 if (!(le32_to_cpu(sb->feature_map) &
1665 MD_FEATURE_RECOVERY_BITMAP))
1666 rdev->saved_raid_disk = -1;
1667 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001668 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 rdev->raid_disk = role;
1670 break;
1671 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001672 if (sb->devflags & WriteMostly1)
1673 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001674 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1675 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001676 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001677 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return 0;
1680}
1681
NeilBrownfd01b882011-10-11 16:47:53 +11001682static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001685 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 int max_dev, i;
1687 /* make rdev->sb match mddev and rdev data. */
1688
Namhyung Kim65a06f062011-07-27 11:00:36 +10001689 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 sb->feature_map = 0;
1692 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001693 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 memset(sb->pad3, 0, sizeof(sb->pad3));
1695
1696 sb->utime = cpu_to_le64((__u64)mddev->utime);
1697 sb->events = cpu_to_le64(mddev->events);
1698 if (mddev->in_sync)
1699 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1700 else
1701 sb->resync_offset = cpu_to_le64(0);
1702
NeilBrown1c05b4b2006-10-21 10:24:08 -07001703 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001704
NeilBrownf0ca3402006-02-02 14:28:04 -08001705 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001706 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001707 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001708 sb->level = cpu_to_le32(mddev->level);
1709 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001710
NeilBrownaeb9b2112011-08-25 14:43:08 +10001711 if (test_bit(WriteMostly, &rdev->flags))
1712 sb->devflags |= WriteMostly1;
1713 else
1714 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001715 sb->data_offset = cpu_to_le64(rdev->data_offset);
1716 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001717
NeilBrownc3d97142009-12-14 12:49:52 +11001718 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1719 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001720 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001721 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001722
1723 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001724 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001725 sb->feature_map |=
1726 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1727 sb->recovery_offset =
1728 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001729 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1730 sb->feature_map |=
1731 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001732 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001733 if (test_bit(Replacement, &rdev->flags))
1734 sb->feature_map |=
1735 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001736
NeilBrownf6705572006-03-27 01:18:11 -08001737 if (mddev->reshape_position != MaxSector) {
1738 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1739 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1740 sb->new_layout = cpu_to_le32(mddev->new_layout);
1741 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1742 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001743 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001744 if (mddev->delta_disks == 0 &&
1745 mddev->reshape_backwards)
1746 sb->feature_map
1747 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001748 if (rdev->new_data_offset != rdev->data_offset) {
1749 sb->feature_map
1750 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1751 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1752 - rdev->data_offset));
1753 }
NeilBrownf6705572006-03-27 01:18:11 -08001754 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001755
NeilBrown2699b672011-07-28 11:31:47 +10001756 if (rdev->badblocks.count == 0)
1757 /* Nothing to do for bad blocks*/ ;
1758 else if (sb->bblog_offset == 0)
1759 /* Cannot record bad blocks on this device */
1760 md_error(mddev, rdev);
1761 else {
1762 struct badblocks *bb = &rdev->badblocks;
1763 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1764 u64 *p = bb->page;
1765 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1766 if (bb->changed) {
1767 unsigned seq;
1768
1769retry:
1770 seq = read_seqbegin(&bb->lock);
1771
1772 memset(bbp, 0xff, PAGE_SIZE);
1773
1774 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001775 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001776 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1777 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001778 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001779 }
NeilBrownd0962932012-03-19 12:46:41 +11001780 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001781 if (read_seqretry(&bb->lock, seq))
1782 goto retry;
1783
1784 bb->sector = (rdev->sb_start +
1785 (int)le32_to_cpu(sb->bblog_offset));
1786 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001787 }
1788 }
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001791 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (rdev2->desc_nr+1 > max_dev)
1793 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001794
NeilBrown70471da2009-08-03 10:59:57 +10001795 if (max_dev > le32_to_cpu(sb->max_dev)) {
1796 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001797 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001798 rdev->sb_size = max_dev * 2 + 256;
1799 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1800 if (rdev->sb_size & bmask)
1801 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001802 } else
1803 max_dev = le32_to_cpu(sb->max_dev);
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 for (i=0; i<max_dev;i++)
1806 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001807
NeilBrowndafb20f2012-03-19 12:46:39 +11001808 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001810 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001812 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001814 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001815 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 else
1817 sb->dev_roles[i] = cpu_to_le16(0xffff);
1818 }
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 sb->sb_csum = calc_sb_1_csum(sb);
1821}
1822
Chris Webb0cd17fe2008-06-28 08:31:46 +10001823static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001824super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001825{
1826 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001827 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001828 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001829 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001830 if (rdev->data_offset != rdev->new_data_offset)
1831 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001832 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001833 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001834 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001835 max_sectors -= rdev->data_offset;
1836 if (!num_sectors || num_sectors > max_sectors)
1837 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001838 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001839 /* minor version 0 with bitmap we can't move */
1840 return 0;
1841 } else {
1842 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001843 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001844 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001845 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001846 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001847 if (!num_sectors || num_sectors > max_sectors)
1848 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001849 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001850 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001851 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001852 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001853 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001854 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001855 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001856 rdev->sb_page);
1857 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001858 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001859
1860}
1861
1862static int
1863super_1_allow_new_offset(struct md_rdev *rdev,
1864 unsigned long long new_offset)
1865{
1866 /* All necessary checks on new >= old have been done */
1867 struct bitmap *bitmap;
1868 if (new_offset >= rdev->data_offset)
1869 return 1;
1870
1871 /* with 1.0 metadata, there is no metadata to tread on
1872 * so we can always move back */
1873 if (rdev->mddev->minor_version == 0)
1874 return 1;
1875
1876 /* otherwise we must be sure not to step on
1877 * any metadata, so stay:
1878 * 36K beyond start of superblock
1879 * beyond end of badblocks
1880 * beyond write-intent bitmap
1881 */
1882 if (rdev->sb_start + (32+4)*2 > new_offset)
1883 return 0;
1884 bitmap = rdev->mddev->bitmap;
1885 if (bitmap && !rdev->mddev->bitmap_info.file &&
1886 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001887 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001888 return 0;
1889 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1890 return 0;
1891
1892 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001893}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Adrian Bunk75c96f82005-05-05 16:16:09 -07001895static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 [0] = {
1897 .name = "0.90.0",
1898 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001899 .load_super = super_90_load,
1900 .validate_super = super_90_validate,
1901 .sync_super = super_90_sync,
1902 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001903 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 },
1905 [1] = {
1906 .name = "md-1",
1907 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001908 .load_super = super_1_load,
1909 .validate_super = super_1_validate,
1910 .sync_super = super_1_sync,
1911 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001912 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 },
1914};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
NeilBrownfd01b882011-10-11 16:47:53 +11001916static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001917{
1918 if (mddev->sync_super) {
1919 mddev->sync_super(mddev, rdev);
1920 return;
1921 }
1922
1923 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1924
1925 super_types[mddev->major_version].sync_super(mddev, rdev);
1926}
1927
NeilBrownfd01b882011-10-11 16:47:53 +11001928static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
NeilBrown3cb03002011-10-11 16:45:26 +11001930 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
NeilBrown4b809912008-07-21 17:05:25 +10001932 rcu_read_lock();
1933 rdev_for_each_rcu(rdev, mddev1)
1934 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001935 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001936 rdev2->bdev->bd_contains) {
1937 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001938 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001939 }
1940 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return 0;
1942}
1943
1944static LIST_HEAD(pending_raid_disks);
1945
Andre Nollac5e7112009-08-03 10:59:47 +10001946/*
1947 * Try to register data integrity profile for an mddev
1948 *
1949 * This is called when an array is started and after a disk has been kicked
1950 * from the array. It only succeeds if all working and active component devices
1951 * are integrity capable with matching profiles.
1952 */
NeilBrownfd01b882011-10-11 16:47:53 +11001953int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001954{
NeilBrown3cb03002011-10-11 16:45:26 +11001955 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001956
Andre Nollac5e7112009-08-03 10:59:47 +10001957 if (list_empty(&mddev->disks))
1958 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001959 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1960 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001961 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001962 /* skip spares and non-functional disks */
1963 if (test_bit(Faulty, &rdev->flags))
1964 continue;
1965 if (rdev->raid_disk < 0)
1966 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001967 if (!reference) {
1968 /* Use the first rdev as the reference */
1969 reference = rdev;
1970 continue;
1971 }
1972 /* does this rdev's profile match the reference profile? */
1973 if (blk_integrity_compare(reference->bdev->bd_disk,
1974 rdev->bdev->bd_disk) < 0)
1975 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001976 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001977 if (!reference || !bdev_get_integrity(reference->bdev))
1978 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001979 /*
1980 * All component devices are integrity capable and have matching
1981 * profiles, register the common profile for the md device.
1982 */
1983 if (blk_integrity_register(mddev->gendisk,
1984 bdev_get_integrity(reference->bdev)) != 0) {
1985 printk(KERN_ERR "md: failed to register integrity for %s\n",
1986 mdname(mddev));
1987 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001988 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001989 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1990 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1991 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1992 mdname(mddev));
1993 return -EINVAL;
1994 }
Andre Nollac5e7112009-08-03 10:59:47 +10001995 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001996}
Andre Nollac5e7112009-08-03 10:59:47 +10001997EXPORT_SYMBOL(md_integrity_register);
1998
1999/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002000void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002001{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002002 struct blk_integrity *bi_rdev;
2003 struct blk_integrity *bi_mddev;
2004
2005 if (!mddev->gendisk)
2006 return;
2007
2008 bi_rdev = bdev_get_integrity(rdev->bdev);
2009 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002010
2011 if (!bi_mddev) /* nothing to do */
2012 return;
2013 if (rdev->raid_disk < 0) /* skip spares */
2014 return;
2015 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2016 rdev->bdev->bd_disk) >= 0)
2017 return;
2018 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2019 blk_integrity_unregister(mddev->gendisk);
2020}
2021EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002022
NeilBrownf72ffdd2014-09-30 14:23:59 +10002023static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002025 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002026 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002027 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Dan Williams11e2ede2008-04-30 00:52:32 -07002029 /* prevent duplicates */
2030 if (find_rdev(mddev, rdev->bdev->bd_dev))
2031 return -EEXIST;
2032
Andre Nolldd8ac332009-03-31 14:33:13 +11002033 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2034 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2035 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002036 if (mddev->pers) {
2037 /* Cannot change size, so fail
2038 * If mddev->level <= 0, then we don't care
2039 * about aligning sizes (e.g. linear)
2040 */
2041 if (mddev->level > 0)
2042 return -ENOSPC;
2043 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002044 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 /* Verify rdev->desc_nr is unique.
2048 * If it is -1, assign a free number, else
2049 * check number is not in use
2050 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002051 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (rdev->desc_nr < 0) {
2053 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002054 if (mddev->pers)
2055 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002056 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 choice++;
2058 rdev->desc_nr = choice;
2059 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002060 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002061 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002065 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002066 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2067 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2068 mdname(mddev), mddev->max_disks);
2069 return -EBUSY;
2070 }
NeilBrown19133a42005-11-08 21:39:35 -08002071 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002072 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002075 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002076
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002077 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002078 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002079
Tejun Heo0762b8b2008-08-25 19:56:12 +09002080 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002081 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2082 /* failure here is OK */;
2083 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002084
NeilBrown4b809912008-07-21 17:05:25 +10002085 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002086 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002087
2088 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002089 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002092
2093 fail:
2094 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2095 b, mdname(mddev));
2096 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
NeilBrown177a99b2008-02-06 01:39:56 -08002099static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002100{
NeilBrown3cb03002011-10-11 16:45:26 +11002101 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002102 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002103 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002104}
2105
NeilBrownf72ffdd2014-09-30 14:23:59 +10002106static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002109
Tejun Heo49731ba2011-01-14 18:43:57 +01002110 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002111 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2113 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002114 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002115 sysfs_put(rdev->sysfs_state);
2116 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002117 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002118 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002119 * writing to 'remove' to "dev/state". We also need
2120 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002121 */
NeilBrown4b809912008-07-21 17:05:25 +10002122 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002123 INIT_WORK(&rdev->del_work, md_delayed_delete);
2124 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002125 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
2128/*
2129 * prevent the device from being mounted, repartitioned or
2130 * otherwise reused by a RAID array (or any other kernel
2131 * subsystem), by bd_claiming the device.
2132 */
NeilBrown3cb03002011-10-11 16:45:26 +11002133static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 int err = 0;
2136 struct block_device *bdev;
2137 char b[BDEVNAME_SIZE];
2138
Tejun Heod4d77622010-11-13 11:55:18 +01002139 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002140 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (IS_ERR(bdev)) {
2142 printk(KERN_ERR "md: could not open %s.\n",
2143 __bdevname(dev, b));
2144 return PTR_ERR(bdev);
2145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 rdev->bdev = bdev;
2147 return err;
2148}
2149
NeilBrown3cb03002011-10-11 16:45:26 +11002150static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 struct block_device *bdev = rdev->bdev;
2153 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002154 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
2157void md_autodetect_dev(dev_t dev);
2158
NeilBrownf72ffdd2014-09-30 14:23:59 +10002159static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 printk(KERN_INFO "md: export_rdev(%s)\n",
2164 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002165 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002167 if (test_bit(AutoDetected, &rdev->flags))
2168 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169#endif
2170 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002171 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002174void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 unbind_rdev_from_array(rdev);
2177 export_rdev(rdev);
2178}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002179EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
NeilBrownfd01b882011-10-11 16:47:53 +11002181static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
NeilBrown0638bb02014-09-25 17:43:47 +10002183 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
NeilBrown0638bb02014-09-25 17:43:47 +10002185 while (!list_empty(&mddev->disks)) {
2186 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2187 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002188 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 mddev->raid_disks = 0;
2191 mddev->major_version = 0;
2192}
2193
NeilBrownf72ffdd2014-09-30 14:23:59 +10002194static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
NeilBrown42543762006-06-26 00:27:57 -07002196 /* Update each superblock (in-memory image), but
2197 * if we are allowed to, skip spares which already
2198 * have the right event counter, or have one earlier
2199 * (which would mean they aren't being marked as dirty
2200 * with the rest of the array)
2201 */
NeilBrown3cb03002011-10-11 16:45:26 +11002202 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002203 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002204 if (rdev->sb_events == mddev->events ||
2205 (nospares &&
2206 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002207 rdev->sb_events+1 == mddev->events)) {
2208 /* Don't update this superblock */
2209 rdev->sb_loaded = 2;
2210 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002211 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002212 rdev->sb_loaded = 1;
2213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215}
2216
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002217void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
NeilBrown3cb03002011-10-11 16:45:26 +11002219 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002220 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002221 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002222 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
NeilBrownd87f0642013-04-24 11:42:40 +10002224 if (mddev->ro) {
2225 if (force_change)
2226 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2227 return;
2228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002230 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002231 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002232 if (rdev->raid_disk >= 0 &&
2233 mddev->delta_disks >= 0 &&
2234 !test_bit(In_sync, &rdev->flags) &&
2235 mddev->curr_resync_completed > rdev->recovery_offset)
2236 rdev->recovery_offset = mddev->curr_resync_completed;
2237
NeilBrownf72ffdd2014-09-30 14:23:59 +10002238 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002239 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002240 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002241 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002242 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002243 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002244 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002245 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002246 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002247 md_ack_all_badblocks(&rdev->badblocks);
2248 md_error(mddev, rdev);
2249 }
2250 clear_bit(Blocked, &rdev->flags);
2251 clear_bit(BlockedBadBlocks, &rdev->flags);
2252 wake_up(&rdev->blocked_wait);
2253 }
2254 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002255 wake_up(&mddev->sb_wait);
2256 return;
2257 }
2258
NeilBrown85572d72014-12-15 12:56:56 +11002259 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002260
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002261 mddev->utime = get_seconds();
2262
NeilBrown850b2b422006-10-03 01:15:46 -07002263 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2264 force_change = 1;
2265 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2266 /* just a clean<-> dirty transition, possibly leave spares alone,
2267 * though if events isn't the right even/odd, we will have to do
2268 * spares after all
2269 */
2270 nospares = 1;
2271 if (force_change)
2272 nospares = 0;
2273 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002274 /* If the array is degraded, then skipping spares is both
2275 * dangerous and fairly pointless.
2276 * Dangerous because a device that was removed from the array
2277 * might have a event_count that still looks up-to-date,
2278 * so it can be re-added without a resync.
2279 * Pointless because if there are any spares to skip,
2280 * then a recovery will happen and soon that array won't
2281 * be degraded any more and the spare can go back to sleep then.
2282 */
NeilBrown850b2b422006-10-03 01:15:46 -07002283 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002284
NeilBrown06d91a52005-06-21 17:17:12 -07002285 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002286
2287 /* If this is just a dirty<->clean transition, and the array is clean
2288 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002289 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002290 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002291 && mddev->can_decrease_events
2292 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002293 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002294 mddev->can_decrease_events = 0;
2295 } else {
NeilBrown42543762006-06-26 00:27:57 -07002296 /* otherwise we have to go forward and ... */
2297 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002298 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
NeilBrown403df472014-09-30 15:52:29 +10002301 /*
2302 * This 64-bit counter should never wrap.
2303 * Either we are in around ~1 trillion A.C., assuming
2304 * 1 reboot per second, or we have a bug...
2305 */
2306 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002307
NeilBrowndafb20f2012-03-19 12:46:39 +11002308 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002309 if (rdev->badblocks.changed)
2310 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002311 if (test_bit(Faulty, &rdev->flags))
2312 set_bit(FaultRecorded, &rdev->flags);
2313 }
NeilBrown2699b672011-07-28 11:31:47 +10002314
NeilBrowne6910632008-02-06 01:39:51 -08002315 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002316 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
NeilBrown36a4e1f2011-10-07 14:23:17 +11002318 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2319 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
NeilBrown4ad13662007-07-17 04:06:13 -07002321 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002322 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002324
NeilBrown42543762006-06-26 00:27:57 -07002325 if (rdev->sb_loaded != 1)
2326 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
NeilBrownf4667222013-12-09 12:04:56 +11002328 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002329 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002330 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002331 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002332 pr_debug("md: (write) %s's sb offset: %llu\n",
2333 bdevname(rdev->bdev, b),
2334 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002335 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002336 if (rdev->badblocks.size) {
2337 md_super_write(mddev, rdev,
2338 rdev->badblocks.sector,
2339 rdev->badblocks.size << 9,
2340 rdev->bb_page);
2341 rdev->badblocks.size = 0;
2342 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002343
NeilBrownf4667222013-12-09 12:04:56 +11002344 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002345 pr_debug("md: %s (skipping faulty)\n",
2346 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002347
NeilBrown7bfa19f2005-06-21 17:17:28 -07002348 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /* only need to write one superblock... */
2350 break;
2351 }
NeilBrowna9701a32005-11-08 21:39:34 -08002352 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002353 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002354
NeilBrown85572d72014-12-15 12:56:56 +11002355 spin_lock(&mddev->lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002356 if (mddev->in_sync != sync_req ||
2357 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002358 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002359 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002360 goto repeat;
2361 }
NeilBrown850b2b422006-10-03 01:15:46 -07002362 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002363 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002364 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002365 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2366 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002367
NeilBrowndafb20f2012-03-19 12:46:39 +11002368 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002369 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2370 clear_bit(Blocked, &rdev->flags);
2371
2372 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002373 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002374 clear_bit(BlockedBadBlocks, &rdev->flags);
2375 wake_up(&rdev->blocked_wait);
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002378EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002380static int add_bound_rdev(struct md_rdev *rdev)
2381{
2382 struct mddev *mddev = rdev->mddev;
2383 int err = 0;
2384
2385 if (!mddev->pers->hot_remove_disk) {
2386 /* If there is hot_add_disk but no hot_remove_disk
2387 * then added disks for geometry changes,
2388 * and should be added immediately.
2389 */
2390 super_types[mddev->major_version].
2391 validate_super(mddev, rdev);
2392 err = mddev->pers->hot_add_disk(mddev, rdev);
2393 if (err) {
2394 unbind_rdev_from_array(rdev);
2395 export_rdev(rdev);
2396 return err;
2397 }
2398 }
2399 sysfs_notify_dirent_safe(rdev->sysfs_state);
2400
2401 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2402 if (mddev->degraded)
2403 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2404 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2405 md_new_event(mddev);
2406 md_wakeup_thread(mddev->thread);
2407 return 0;
2408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Andre Noll7f6ce762008-03-23 18:34:54 +01002410/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002411 * We want to accept with case. For this we use cmd_match.
2412 */
2413static int cmd_match(const char *cmd, const char *str)
2414{
2415 /* See if cmd, written into a sysfs file, matches
2416 * str. They must either be the same, or cmd can
2417 * have a trailing newline
2418 */
2419 while (*cmd && *str && *cmd == *str) {
2420 cmd++;
2421 str++;
2422 }
2423 if (*cmd == '\n')
2424 cmd++;
2425 if (*str || *cmd)
2426 return 0;
2427 return 1;
2428}
2429
NeilBrown86e6ffd2005-11-08 21:39:24 -08002430struct rdev_sysfs_entry {
2431 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002432 ssize_t (*show)(struct md_rdev *, char *);
2433 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002434};
2435
2436static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002437state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002438{
2439 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002440 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002441 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002442
NeilBrown758bfc82014-12-15 12:56:59 +11002443 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002444 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002445 len+= sprintf(page+len, "%sfaulty",sep);
2446 sep = ",";
2447 }
NeilBrown758bfc82014-12-15 12:56:59 +11002448 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002449 len += sprintf(page+len, "%sin_sync",sep);
2450 sep = ",";
2451 }
NeilBrown758bfc82014-12-15 12:56:59 +11002452 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002453 len += sprintf(page+len, "%swrite_mostly",sep);
2454 sep = ",";
2455 }
NeilBrown758bfc82014-12-15 12:56:59 +11002456 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002457 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002458 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002459 len += sprintf(page+len, "%sblocked", sep);
2460 sep = ",";
2461 }
NeilBrown758bfc82014-12-15 12:56:59 +11002462 if (!test_bit(Faulty, &flags) &&
2463 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002464 len += sprintf(page+len, "%sspare", sep);
2465 sep = ",";
2466 }
NeilBrown758bfc82014-12-15 12:56:59 +11002467 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002468 len += sprintf(page+len, "%swrite_error", sep);
2469 sep = ",";
2470 }
NeilBrown758bfc82014-12-15 12:56:59 +11002471 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002472 len += sprintf(page+len, "%swant_replacement", sep);
2473 sep = ",";
2474 }
NeilBrown758bfc82014-12-15 12:56:59 +11002475 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002476 len += sprintf(page+len, "%sreplacement", sep);
2477 sep = ",";
2478 }
2479
NeilBrown86e6ffd2005-11-08 21:39:24 -08002480 return len+sprintf(page+len, "\n");
2481}
2482
NeilBrown45dc2de2006-06-26 00:27:58 -07002483static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002484state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002485{
2486 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002487 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002488 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002489 * writemostly - sets write_mostly
2490 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002491 * blocked - sets the Blocked flags
2492 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002493 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002494 * -insync - clear Insync for a device with a slot assigned,
2495 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002496 * write_error - sets WriteErrorSeen
2497 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002498 */
2499 int err = -EINVAL;
2500 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2501 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002502 if (test_bit(Faulty, &rdev->flags))
2503 err = 0;
2504 else
2505 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002506 } else if (cmd_match(buf, "remove")) {
2507 if (rdev->raid_disk >= 0)
2508 err = -EBUSY;
2509 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002510 struct mddev *mddev = rdev->mddev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002511 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002512 md_cluster_ops->remove_disk(mddev, rdev);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002513 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05002514 if (mddev_is_clustered(mddev))
2515 md_cluster_ops->metadata_update_start(mddev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002516 if (mddev->pers)
2517 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002518 md_new_event(mddev);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05002519 if (mddev_is_clustered(mddev))
2520 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown45dc2de2006-06-26 00:27:58 -07002521 err = 0;
2522 }
NeilBrownf6556752006-06-26 00:28:01 -07002523 } else if (cmd_match(buf, "writemostly")) {
2524 set_bit(WriteMostly, &rdev->flags);
2525 err = 0;
2526 } else if (cmd_match(buf, "-writemostly")) {
2527 clear_bit(WriteMostly, &rdev->flags);
2528 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002529 } else if (cmd_match(buf, "blocked")) {
2530 set_bit(Blocked, &rdev->flags);
2531 err = 0;
2532 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002533 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002534 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002535 /* metadata handler doesn't understand badblocks,
2536 * so we need to fail the device
2537 */
2538 md_error(rdev->mddev, rdev);
2539 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002540 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002541 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002542 wake_up(&rdev->blocked_wait);
2543 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2544 md_wakeup_thread(rdev->mddev->thread);
2545
2546 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002547 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2548 set_bit(In_sync, &rdev->flags);
2549 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002550 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002551 if (rdev->mddev->pers == NULL) {
2552 clear_bit(In_sync, &rdev->flags);
2553 rdev->saved_raid_disk = rdev->raid_disk;
2554 rdev->raid_disk = -1;
2555 err = 0;
2556 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002557 } else if (cmd_match(buf, "write_error")) {
2558 set_bit(WriteErrorSeen, &rdev->flags);
2559 err = 0;
2560 } else if (cmd_match(buf, "-write_error")) {
2561 clear_bit(WriteErrorSeen, &rdev->flags);
2562 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002563 } else if (cmd_match(buf, "want_replacement")) {
2564 /* Any non-spare device that is not a replacement can
2565 * become want_replacement at any time, but we then need to
2566 * check if recovery is needed.
2567 */
2568 if (rdev->raid_disk >= 0 &&
2569 !test_bit(Replacement, &rdev->flags))
2570 set_bit(WantReplacement, &rdev->flags);
2571 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2572 md_wakeup_thread(rdev->mddev->thread);
2573 err = 0;
2574 } else if (cmd_match(buf, "-want_replacement")) {
2575 /* Clearing 'want_replacement' is always allowed.
2576 * Once replacements starts it is too late though.
2577 */
2578 err = 0;
2579 clear_bit(WantReplacement, &rdev->flags);
2580 } else if (cmd_match(buf, "replacement")) {
2581 /* Can only set a device as a replacement when array has not
2582 * yet been started. Once running, replacement is automatic
2583 * from spares, or by assigning 'slot'.
2584 */
2585 if (rdev->mddev->pers)
2586 err = -EBUSY;
2587 else {
2588 set_bit(Replacement, &rdev->flags);
2589 err = 0;
2590 }
2591 } else if (cmd_match(buf, "-replacement")) {
2592 /* Similarly, can only clear Replacement before start */
2593 if (rdev->mddev->pers)
2594 err = -EBUSY;
2595 else {
2596 clear_bit(Replacement, &rdev->flags);
2597 err = 0;
2598 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002599 } else if (cmd_match(buf, "re-add")) {
2600 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002601 /* clear_bit is performed _after_ all the devices
2602 * have their local Faulty bit cleared. If any writes
2603 * happen in the meantime in the local node, they
2604 * will land in the local bitmap, which will be synced
2605 * by this node eventually
2606 */
2607 if (!mddev_is_clustered(rdev->mddev) ||
2608 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2609 clear_bit(Faulty, &rdev->flags);
2610 err = add_bound_rdev(rdev);
2611 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002612 } else
2613 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002614 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002615 if (!err)
2616 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002617 return err ? err : len;
2618}
NeilBrown80ca3a42006-07-10 04:44:18 -07002619static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002620__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002621
2622static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002623errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002624{
2625 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2626}
2627
2628static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002629errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002630{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002631 unsigned int n;
2632 int rv;
2633
2634 rv = kstrtouint(buf, 10, &n);
2635 if (rv < 0)
2636 return rv;
2637 atomic_set(&rdev->corrected_errors, n);
2638 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002639}
2640static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002641__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002642
NeilBrown014236d2006-01-06 00:20:55 -08002643static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002644slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002645{
2646 if (rdev->raid_disk < 0)
2647 return sprintf(page, "none\n");
2648 else
2649 return sprintf(page, "%d\n", rdev->raid_disk);
2650}
2651
2652static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002653slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002654{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002655 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002656 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002657
NeilBrown014236d2006-01-06 00:20:55 -08002658 if (strncmp(buf, "none", 4)==0)
2659 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002660 else {
2661 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2662 if (err < 0)
2663 return err;
2664 }
Neil Brown6c2fce22008-06-28 08:31:31 +10002665 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002666 /* Setting 'slot' on an active array requires also
2667 * updating the 'rd%d' link, and communicating
2668 * with the personality with ->hot_*_disk.
2669 * For now we only support removing
2670 * failed/spare devices. This normally happens automatically,
2671 * but not when the metadata is externally managed.
2672 */
NeilBrownc303da62008-02-06 01:39:51 -08002673 if (rdev->raid_disk == -1)
2674 return -EEXIST;
2675 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002676 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002677 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002678 clear_bit(Blocked, &rdev->flags);
2679 remove_and_add_spares(rdev->mddev, rdev);
2680 if (rdev->raid_disk >= 0)
2681 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002682 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2683 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002684 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002685 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002686 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002687 */
2688
2689 if (rdev->raid_disk != -1)
2690 return -EBUSY;
2691
NeilBrownc6751b22011-02-02 11:57:13 +11002692 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2693 return -EBUSY;
2694
Neil Brown6c2fce22008-06-28 08:31:31 +10002695 if (rdev->mddev->pers->hot_add_disk == NULL)
2696 return -EINVAL;
2697
NeilBrownba1b41b2011-01-14 09:14:34 +11002698 if (slot >= rdev->mddev->raid_disks &&
2699 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2700 return -ENOSPC;
2701
Neil Brown6c2fce22008-06-28 08:31:31 +10002702 rdev->raid_disk = slot;
2703 if (test_bit(In_sync, &rdev->flags))
2704 rdev->saved_raid_disk = slot;
2705 else
2706 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002707 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002708 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002709 err = rdev->mddev->pers->
2710 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002711 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002712 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002713 return err;
Neil Brown52664732008-06-28 08:31:44 +10002714 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002715 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002716 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002717 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002718 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002719 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002720 if (slot >= rdev->mddev->raid_disks &&
2721 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002722 return -ENOSPC;
2723 rdev->raid_disk = slot;
2724 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002725 clear_bit(Faulty, &rdev->flags);
2726 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002727 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002728 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002729 }
NeilBrown014236d2006-01-06 00:20:55 -08002730 return len;
2731}
2732
NeilBrown014236d2006-01-06 00:20:55 -08002733static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002734__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002735
NeilBrown93c8cad2006-01-06 00:20:56 -08002736static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002737offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002738{
Andrew Morton6961ece2006-01-06 00:20:59 -08002739 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002740}
2741
2742static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002743offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002744{
NeilBrownc6563a82012-05-21 09:27:00 +10002745 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002746 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002747 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002748 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002749 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002750 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002751 /* Must set offset before size, so overlap checks
2752 * can be sane */
2753 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002754 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002755 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002756 return len;
2757}
2758
2759static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002760__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002761
NeilBrownc6563a82012-05-21 09:27:00 +10002762static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2763{
2764 return sprintf(page, "%llu\n",
2765 (unsigned long long)rdev->new_data_offset);
2766}
2767
2768static ssize_t new_offset_store(struct md_rdev *rdev,
2769 const char *buf, size_t len)
2770{
2771 unsigned long long new_offset;
2772 struct mddev *mddev = rdev->mddev;
2773
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002774 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002775 return -EINVAL;
2776
NeilBrownf851b602014-12-11 10:02:10 +11002777 if (mddev->sync_thread ||
2778 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002779 return -EBUSY;
2780 if (new_offset == rdev->data_offset)
2781 /* reset is always permitted */
2782 ;
2783 else if (new_offset > rdev->data_offset) {
2784 /* must not push array size beyond rdev_sectors */
2785 if (new_offset - rdev->data_offset
2786 + mddev->dev_sectors > rdev->sectors)
2787 return -E2BIG;
2788 }
2789 /* Metadata worries about other space details. */
2790
2791 /* decreasing the offset is inconsistent with a backwards
2792 * reshape.
2793 */
2794 if (new_offset < rdev->data_offset &&
2795 mddev->reshape_backwards)
2796 return -EINVAL;
2797 /* Increasing offset is inconsistent with forwards
2798 * reshape. reshape_direction should be set to
2799 * 'backwards' first.
2800 */
2801 if (new_offset > rdev->data_offset &&
2802 !mddev->reshape_backwards)
2803 return -EINVAL;
2804
2805 if (mddev->pers && mddev->persistent &&
2806 !super_types[mddev->major_version]
2807 .allow_new_offset(rdev, new_offset))
2808 return -E2BIG;
2809 rdev->new_data_offset = new_offset;
2810 if (new_offset > rdev->data_offset)
2811 mddev->reshape_backwards = 1;
2812 else if (new_offset < rdev->data_offset)
2813 mddev->reshape_backwards = 0;
2814
2815 return len;
2816}
2817static struct rdev_sysfs_entry rdev_new_offset =
2818__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2819
NeilBrown83303b62006-01-06 00:21:06 -08002820static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002821rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002822{
Andre Nolldd8ac332009-03-31 14:33:13 +11002823 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002824}
2825
NeilBrownc5d79ad2008-02-06 01:39:54 -08002826static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2827{
2828 /* check if two start/length pairs overlap */
2829 if (s1+l1 <= s2)
2830 return 0;
2831 if (s2+l2 <= s1)
2832 return 0;
2833 return 1;
2834}
2835
Dan Williamsb522adc2009-03-31 15:00:31 +11002836static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2837{
2838 unsigned long long blocks;
2839 sector_t new;
2840
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002841 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002842 return -EINVAL;
2843
2844 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2845 return -EINVAL; /* sector conversion overflow */
2846
2847 new = blocks * 2;
2848 if (new != blocks * 2)
2849 return -EINVAL; /* unsigned long long to sector_t overflow */
2850
2851 *sectors = new;
2852 return 0;
2853}
2854
NeilBrown83303b62006-01-06 00:21:06 -08002855static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002856rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002857{
NeilBrownfd01b882011-10-11 16:47:53 +11002858 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002859 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002860 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002861
Dan Williamsb522adc2009-03-31 15:00:31 +11002862 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002863 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002864 if (rdev->data_offset != rdev->new_data_offset)
2865 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002866 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002867 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002868 sectors = super_types[my_mddev->major_version].
2869 rdev_size_change(rdev, sectors);
2870 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002871 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002872 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002873 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002874 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002875 if (!my_mddev->pers->resize)
2876 /* Cannot change size for RAID0 or Linear etc */
2877 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002878 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002879 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002880 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002881
Andre Nolldd8ac332009-03-31 14:33:13 +11002882 rdev->sectors = sectors;
2883 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002884 /* Need to check that all other rdevs with the same
2885 * ->bdev do not overlap. 'rcu' is sufficient to walk
2886 * the rdev lists safely.
2887 * This check does not provide a hard guarantee, it
2888 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002889 */
NeilBrownfd01b882011-10-11 16:47:53 +11002890 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002891 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002892 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002893
NeilBrown8b1afc32014-09-29 15:33:20 +10002894 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002895 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002896 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002897
NeilBrowndafb20f2012-03-19 12:46:39 +11002898 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002899 if (rdev->bdev == rdev2->bdev &&
2900 rdev != rdev2 &&
2901 overlaps(rdev->data_offset, rdev->sectors,
2902 rdev2->data_offset,
2903 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002904 overlap = 1;
2905 break;
2906 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002907 if (overlap) {
2908 mddev_put(mddev);
2909 break;
2910 }
2911 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002912 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002913 if (overlap) {
2914 /* Someone else could have slipped in a size
2915 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002916 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002917 * safe, and trust userspace not to race with
2918 * itself
2919 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002920 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002921 return -EBUSY;
2922 }
2923 }
NeilBrown83303b62006-01-06 00:21:06 -08002924 return len;
2925}
2926
2927static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002928__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002929
NeilBrown3cb03002011-10-11 16:45:26 +11002930static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002931{
2932 unsigned long long recovery_start = rdev->recovery_offset;
2933
2934 if (test_bit(In_sync, &rdev->flags) ||
2935 recovery_start == MaxSector)
2936 return sprintf(page, "none\n");
2937
2938 return sprintf(page, "%llu\n", recovery_start);
2939}
2940
NeilBrown3cb03002011-10-11 16:45:26 +11002941static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002942{
2943 unsigned long long recovery_start;
2944
2945 if (cmd_match(buf, "none"))
2946 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002947 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002948 return -EINVAL;
2949
2950 if (rdev->mddev->pers &&
2951 rdev->raid_disk >= 0)
2952 return -EBUSY;
2953
2954 rdev->recovery_offset = recovery_start;
2955 if (recovery_start == MaxSector)
2956 set_bit(In_sync, &rdev->flags);
2957 else
2958 clear_bit(In_sync, &rdev->flags);
2959 return len;
2960}
2961
2962static struct rdev_sysfs_entry rdev_recovery_start =
2963__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2964
NeilBrown16c791a2011-07-28 11:31:47 +10002965static ssize_t
2966badblocks_show(struct badblocks *bb, char *page, int unack);
2967static ssize_t
2968badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2969
NeilBrown3cb03002011-10-11 16:45:26 +11002970static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002971{
2972 return badblocks_show(&rdev->badblocks, page, 0);
2973}
NeilBrown3cb03002011-10-11 16:45:26 +11002974static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002975{
NeilBrownde393cd2011-07-28 11:31:48 +10002976 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2977 /* Maybe that ack was all we needed */
2978 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2979 wake_up(&rdev->blocked_wait);
2980 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002981}
2982static struct rdev_sysfs_entry rdev_bad_blocks =
2983__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2984
NeilBrown3cb03002011-10-11 16:45:26 +11002985static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002986{
2987 return badblocks_show(&rdev->badblocks, page, 1);
2988}
NeilBrown3cb03002011-10-11 16:45:26 +11002989static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002990{
2991 return badblocks_store(&rdev->badblocks, page, len, 1);
2992}
2993static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2994__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2995
NeilBrown86e6ffd2005-11-08 21:39:24 -08002996static struct attribute *rdev_default_attrs[] = {
2997 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002998 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002999 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003000 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003001 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003002 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003003 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003004 &rdev_bad_blocks.attr,
3005 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003006 NULL,
3007};
3008static ssize_t
3009rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3010{
3011 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003012 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003013
3014 if (!entry->show)
3015 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003016 if (!rdev->mddev)
3017 return -EBUSY;
3018 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003019}
3020
3021static ssize_t
3022rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3023 const char *page, size_t length)
3024{
3025 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003026 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003027 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003028 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003029
3030 if (!entry->store)
3031 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003032 if (!capable(CAP_SYS_ADMIN))
3033 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003034 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003035 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003036 if (rdev->mddev == NULL)
3037 rv = -EBUSY;
3038 else
3039 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003040 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003041 }
3042 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003043}
3044
3045static void rdev_free(struct kobject *ko)
3046{
NeilBrown3cb03002011-10-11 16:45:26 +11003047 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003048 kfree(rdev);
3049}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003050static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003051 .show = rdev_attr_show,
3052 .store = rdev_attr_store,
3053};
3054static struct kobj_type rdev_ktype = {
3055 .release = rdev_free,
3056 .sysfs_ops = &rdev_sysfs_ops,
3057 .default_attrs = rdev_default_attrs,
3058};
3059
NeilBrown3cb03002011-10-11 16:45:26 +11003060int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003061{
3062 rdev->desc_nr = -1;
3063 rdev->saved_raid_disk = -1;
3064 rdev->raid_disk = -1;
3065 rdev->flags = 0;
3066 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003067 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003068 rdev->sb_events = 0;
3069 rdev->last_read_error.tv_sec = 0;
3070 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003071 rdev->sb_loaded = 0;
3072 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003073 atomic_set(&rdev->nr_pending, 0);
3074 atomic_set(&rdev->read_errors, 0);
3075 atomic_set(&rdev->corrected_errors, 0);
3076
3077 INIT_LIST_HEAD(&rdev->same_set);
3078 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003079
3080 /* Add space to store bad block list.
3081 * This reserves the space even on arrays where it cannot
3082 * be used - I wonder if that matters
3083 */
3084 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003085 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003086 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3087 seqlock_init(&rdev->badblocks.lock);
3088 if (rdev->badblocks.page == NULL)
3089 return -ENOMEM;
3090
3091 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003092}
3093EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094/*
3095 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3096 *
3097 * mark the device faulty if:
3098 *
3099 * - the device is nonexistent (zero size)
3100 * - the device has no valid superblock
3101 *
3102 * a faulty rdev _never_ has rdev->sb set.
3103 */
NeilBrown3cb03002011-10-11 16:45:26 +11003104static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106 char b[BDEVNAME_SIZE];
3107 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003108 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 sector_t size;
3110
NeilBrown9ffae0c2006-01-06 00:20:32 -08003111 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 if (!rdev) {
3113 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3114 return ERR_PTR(-ENOMEM);
3115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
NeilBrown2230dfe2011-07-28 11:31:46 +10003117 err = md_rdev_init(rdev);
3118 if (err)
3119 goto abort_free;
3120 err = alloc_disk_sb(rdev);
3121 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 goto abort_free;
3123
NeilBrownc5d79ad2008-02-06 01:39:54 -08003124 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 if (err)
3126 goto abort_free;
3127
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003128 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003129
Mike Snitzer77304d22010-11-08 14:39:12 +01003130 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003132 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 "md: %s has zero or unknown size, marking faulty!\n",
3134 bdevname(rdev->bdev,b));
3135 err = -EINVAL;
3136 goto abort_free;
3137 }
3138
3139 if (super_format >= 0) {
3140 err = super_types[super_format].
3141 load_super(rdev, NULL, super_minor);
3142 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003143 printk(KERN_WARNING
3144 "md: %s does not have a valid v%d.%d "
3145 "superblock, not importing!\n",
3146 bdevname(rdev->bdev,b),
3147 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 goto abort_free;
3149 }
3150 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003151 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 "md: could not read %s's sb, not importing!\n",
3153 bdevname(rdev->bdev,b));
3154 goto abort_free;
3155 }
3156 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 return rdev;
3159
3160abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003161 if (rdev->bdev)
3162 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003163 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 kfree(rdev);
3165 return ERR_PTR(err);
3166}
3167
3168/*
3169 * Check a full RAID array for plausibility
3170 */
3171
NeilBrownf72ffdd2014-09-30 14:23:59 +10003172static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
3174 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003175 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 char b[BDEVNAME_SIZE];
3177
3178 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003179 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 switch (super_types[mddev->major_version].
3181 load_super(rdev, freshest, mddev->minor_version)) {
3182 case 1:
3183 freshest = rdev;
3184 break;
3185 case 0:
3186 break;
3187 default:
3188 printk( KERN_ERR \
3189 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003190 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003192 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 super_types[mddev->major_version].
3196 validate_super(mddev, freshest);
3197
3198 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003199 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003200 if (mddev->max_disks &&
3201 (rdev->desc_nr >= mddev->max_disks ||
3202 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003203 printk(KERN_WARNING
3204 "md: %s: %s: only %d devices permitted\n",
3205 mdname(mddev), bdevname(rdev->bdev, b),
3206 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003207 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003208 continue;
3209 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003210 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (super_types[mddev->major_version].
3212 validate_super(mddev, rdev)) {
3213 printk(KERN_WARNING "md: kicking non-fresh %s"
3214 " from array!\n",
3215 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003216 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 continue;
3218 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003219 /* No device should have a Candidate flag
3220 * when reading devices
3221 */
3222 if (test_bit(Candidate, &rdev->flags)) {
3223 pr_info("md: kicking Cluster Candidate %s from array!\n",
3224 bdevname(rdev->bdev, b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003225 md_kick_rdev_from_array(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003226 }
3227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 if (mddev->level == LEVEL_MULTIPATH) {
3229 rdev->desc_nr = i++;
3230 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003231 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003232 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003233 rdev->raid_disk = -1;
3234 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 }
3236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
NeilBrown72e02072009-12-14 12:49:55 +11003239/* Read a fixed-point number.
3240 * Numbers in sysfs attributes should be in "standard" units where
3241 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003242 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003243 * milliseconds or jiffies.
3244 * This function takes a decimal number with a possible fractional
3245 * component, and produces an integer which is the result of
3246 * multiplying that number by 10^'scale'.
3247 * all without any floating-point arithmetic.
3248 */
3249int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3250{
3251 unsigned long result = 0;
3252 long decimals = -1;
3253 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3254 if (*cp == '.')
3255 decimals = 0;
3256 else if (decimals < scale) {
3257 unsigned int value;
3258 value = *cp - '0';
3259 result = result * 10 + value;
3260 if (decimals >= 0)
3261 decimals++;
3262 }
3263 cp++;
3264 }
3265 if (*cp == '\n')
3266 cp++;
3267 if (*cp)
3268 return -EINVAL;
3269 if (decimals < 0)
3270 decimals = 0;
3271 while (decimals < scale) {
3272 result *= 10;
3273 decimals ++;
3274 }
3275 *res = result;
3276 return 0;
3277}
3278
NeilBrown19052c02008-08-05 15:54:13 +10003279static void md_safemode_timeout(unsigned long data);
3280
NeilBrowneae17012005-11-08 21:39:23 -08003281static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003282safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003283{
3284 int msec = (mddev->safemode_delay*1000)/HZ;
3285 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3286}
3287static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003288safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003289{
NeilBrown16f17b32006-06-26 00:27:37 -07003290 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003291
NeilBrown72e02072009-12-14 12:49:55 +11003292 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003293 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003294 if (msec == 0)
3295 mddev->safemode_delay = 0;
3296 else {
NeilBrown19052c02008-08-05 15:54:13 +10003297 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003298 unsigned long new_delay = (msec*HZ)/1000;
3299
3300 if (new_delay == 0)
3301 new_delay = 1;
3302 mddev->safemode_delay = new_delay;
3303 if (new_delay < old_delay || old_delay == 0)
3304 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003305 }
3306 return len;
3307}
3308static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003309__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003310
3311static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003312level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003313{
NeilBrown36d091f2014-12-15 12:56:58 +11003314 struct md_personality *p;
3315 int ret;
3316 spin_lock(&mddev->lock);
3317 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003318 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003319 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003320 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003321 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003322 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003323 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003324 else
NeilBrown36d091f2014-12-15 12:56:58 +11003325 ret = 0;
3326 spin_unlock(&mddev->lock);
3327 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003328}
3329
NeilBrownd9d166c2006-01-06 00:20:51 -08003330static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003331level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003332{
Dan Williamsf2859af2010-05-02 10:04:16 -07003333 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003334 ssize_t rv;
3335 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003336 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003337 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003338 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003339 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003340
NeilBrown67918752014-12-15 12:57:01 +11003341 if (slen == 0 || slen >= sizeof(clevel))
3342 return -EINVAL;
3343
3344 rv = mddev_lock(mddev);
3345 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003346 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003347
3348 if (mddev->pers == NULL) {
3349 strncpy(mddev->clevel, buf, slen);
3350 if (mddev->clevel[slen-1] == '\n')
3351 slen--;
3352 mddev->clevel[slen] = 0;
3353 mddev->level = LEVEL_NONE;
3354 rv = len;
3355 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003356 }
NeilBrown67918752014-12-15 12:57:01 +11003357 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003358 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003359 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003360
3361 /* request to change the personality. Need to ensure:
3362 * - array is not engaged in resync/recovery/reshape
3363 * - old personality can be suspended
3364 * - new personality will access other array.
3365 */
3366
NeilBrown67918752014-12-15 12:57:01 +11003367 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003368 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003369 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003370 mddev->reshape_position != MaxSector ||
3371 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003372 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003373
NeilBrown67918752014-12-15 12:57:01 +11003374 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003375 if (!mddev->pers->quiesce) {
3376 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3377 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003378 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003379 }
3380
3381 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003382 strncpy(clevel, buf, slen);
3383 if (clevel[slen-1] == '\n')
3384 slen--;
3385 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003386 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003387 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003388
Dan Williamsf2859af2010-05-02 10:04:16 -07003389 if (request_module("md-%s", clevel) != 0)
3390 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003391 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003392 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003393 if (!pers || !try_module_get(pers->owner)) {
3394 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003395 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003396 rv = -EINVAL;
3397 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003398 }
3399 spin_unlock(&pers_lock);
3400
3401 if (pers == mddev->pers) {
3402 /* Nothing to do! */
3403 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003404 rv = len;
3405 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003406 }
3407 if (!pers->takeover) {
3408 module_put(pers->owner);
3409 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003410 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003411 rv = -EINVAL;
3412 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003413 }
3414
NeilBrowndafb20f2012-03-19 12:46:39 +11003415 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003416 rdev->new_raid_disk = rdev->raid_disk;
3417
NeilBrown245f46c2009-03-31 14:39:39 +11003418 /* ->takeover must set new_* and/or delta_disks
3419 * if it succeeds, and may set them when it fails.
3420 */
3421 priv = pers->takeover(mddev);
3422 if (IS_ERR(priv)) {
3423 mddev->new_level = mddev->level;
3424 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003425 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003426 mddev->raid_disks -= mddev->delta_disks;
3427 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003428 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003429 module_put(pers->owner);
3430 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003431 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003432 rv = PTR_ERR(priv);
3433 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003434 }
3435
3436 /* Looks like we have a winner */
3437 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003438 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003439
3440 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003441 oldpers = mddev->pers;
3442 oldpriv = mddev->private;
3443 mddev->pers = pers;
3444 mddev->private = priv;
3445 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3446 mddev->level = mddev->new_level;
3447 mddev->layout = mddev->new_layout;
3448 mddev->chunk_sectors = mddev->new_chunk_sectors;
3449 mddev->delta_disks = 0;
3450 mddev->reshape_backwards = 0;
3451 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003452 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003453
NeilBrowndb721d32014-12-15 12:56:58 +11003454 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003455 mddev->external) {
3456 /* We are converting from a no-redundancy array
3457 * to a redundancy array and metadata is managed
3458 * externally so we need to be sure that writes
3459 * won't block due to a need to transition
3460 * clean->dirty
3461 * until external management is started.
3462 */
3463 mddev->in_sync = 0;
3464 mddev->safemode_delay = 0;
3465 mddev->safemode = 0;
3466 }
3467
NeilBrowndb721d32014-12-15 12:56:58 +11003468 oldpers->free(mddev, oldpriv);
3469
3470 if (oldpers->sync_request == NULL &&
3471 pers->sync_request != NULL) {
3472 /* need to add the md_redundancy_group */
3473 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3474 printk(KERN_WARNING
3475 "md: cannot register extra attributes for %s\n",
3476 mdname(mddev));
3477 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3478 }
3479 if (oldpers->sync_request != NULL &&
3480 pers->sync_request == NULL) {
3481 /* need to remove the md_redundancy_group */
3482 if (mddev->to_remove == NULL)
3483 mddev->to_remove = &md_redundancy_group;
3484 }
3485
NeilBrowndafb20f2012-03-19 12:46:39 +11003486 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003487 if (rdev->raid_disk < 0)
3488 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003489 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003490 rdev->new_raid_disk = -1;
3491 if (rdev->new_raid_disk == rdev->raid_disk)
3492 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003493 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003494 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003495 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003496 if (rdev->raid_disk < 0)
3497 continue;
3498 if (rdev->new_raid_disk == rdev->raid_disk)
3499 continue;
3500 rdev->raid_disk = rdev->new_raid_disk;
3501 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003502 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003503 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003504 if (sysfs_link_rdev(mddev, rdev))
3505 printk(KERN_WARNING "md: cannot register rd%d"
3506 " for %s after level change\n",
3507 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003508 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003509 }
3510
NeilBrowndb721d32014-12-15 12:56:58 +11003511 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003512 /* this is now an array without redundancy, so
3513 * it must always be in_sync
3514 */
3515 mddev->in_sync = 1;
3516 del_timer_sync(&mddev->safemode_timer);
3517 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003518 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003519 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003520 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003521 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003522 if (!mddev->thread)
3523 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003524 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003525 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003526 rv = len;
3527out_unlock:
3528 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003529 return rv;
3530}
3531
3532static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003533__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003534
NeilBrownd4dbd022006-06-26 00:27:59 -07003535static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003536layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003537{
3538 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003539 if (mddev->reshape_position != MaxSector &&
3540 mddev->layout != mddev->new_layout)
3541 return sprintf(page, "%d (%d)\n",
3542 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003543 return sprintf(page, "%d\n", mddev->layout);
3544}
3545
3546static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003547layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003548{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003549 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003550 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003551
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003552 err = kstrtouint(buf, 10, &n);
3553 if (err < 0)
3554 return err;
NeilBrown67918752014-12-15 12:57:01 +11003555 err = mddev_lock(mddev);
3556 if (err)
3557 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003558
NeilBrownb3546032009-03-31 14:56:41 +11003559 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003560 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003561 err = -EBUSY;
3562 else if (mddev->ro)
3563 err = -EROFS;
3564 else {
3565 mddev->new_layout = n;
3566 err = mddev->pers->check_reshape(mddev);
3567 if (err)
3568 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003569 }
NeilBrownb3546032009-03-31 14:56:41 +11003570 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003571 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003572 if (mddev->reshape_position == MaxSector)
3573 mddev->layout = n;
3574 }
NeilBrown67918752014-12-15 12:57:01 +11003575 mddev_unlock(mddev);
3576 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003577}
3578static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003579__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003580
NeilBrowneae17012005-11-08 21:39:23 -08003581static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003582raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003583{
NeilBrownbb636542005-11-08 21:39:45 -08003584 if (mddev->raid_disks == 0)
3585 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003586 if (mddev->reshape_position != MaxSector &&
3587 mddev->delta_disks != 0)
3588 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3589 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003590 return sprintf(page, "%d\n", mddev->raid_disks);
3591}
3592
NeilBrownfd01b882011-10-11 16:47:53 +11003593static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003594
3595static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003596raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003597{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003598 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003599 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003600
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003601 err = kstrtouint(buf, 10, &n);
3602 if (err < 0)
3603 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003604
NeilBrown67918752014-12-15 12:57:01 +11003605 err = mddev_lock(mddev);
3606 if (err)
3607 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003608 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003609 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003610 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003611 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003612 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003613
NeilBrown67918752014-12-15 12:57:01 +11003614 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003615 rdev_for_each(rdev, mddev) {
3616 if (olddisks < n &&
3617 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003618 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003619 if (olddisks > n &&
3620 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003621 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003622 }
NeilBrown67918752014-12-15 12:57:01 +11003623 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003624 mddev->delta_disks = n - olddisks;
3625 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003626 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003627 } else
NeilBrownda943b992006-01-06 00:20:54 -08003628 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003629out_unlock:
3630 mddev_unlock(mddev);
3631 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003632}
3633static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003634__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003635
NeilBrown24dd4692005-11-08 21:39:26 -08003636static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003637chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003638{
NeilBrown08a02ec2007-05-09 02:35:38 -07003639 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003640 mddev->chunk_sectors != mddev->new_chunk_sectors)
3641 return sprintf(page, "%d (%d)\n",
3642 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003643 mddev->chunk_sectors << 9);
3644 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003645}
3646
3647static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003648chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003649{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003650 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11003651 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003652
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003653 err = kstrtoul(buf, 10, &n);
3654 if (err < 0)
3655 return err;
NeilBrown3b343802006-01-06 00:20:47 -08003656
NeilBrown67918752014-12-15 12:57:01 +11003657 err = mddev_lock(mddev);
3658 if (err)
3659 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003660 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003661 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003662 err = -EBUSY;
3663 else if (mddev->ro)
3664 err = -EROFS;
3665 else {
3666 mddev->new_chunk_sectors = n >> 9;
3667 err = mddev->pers->check_reshape(mddev);
3668 if (err)
3669 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003670 }
NeilBrownb3546032009-03-31 14:56:41 +11003671 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003672 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003673 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003674 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003675 }
NeilBrown67918752014-12-15 12:57:01 +11003676 mddev_unlock(mddev);
3677 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003678}
3679static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003680__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003681
NeilBrowna94213b2006-06-26 00:28:00 -07003682static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003683resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003684{
NeilBrownd1a7c502009-03-31 15:24:32 +11003685 if (mddev->recovery_cp == MaxSector)
3686 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003687 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3688}
3689
3690static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003691resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003692{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003693 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11003694 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003695
3696 if (cmd_match(buf, "none"))
3697 n = MaxSector;
3698 else {
3699 err = kstrtoull(buf, 10, &n);
3700 if (err < 0)
3701 return err;
3702 if (n != (sector_t)n)
3703 return -EINVAL;
3704 }
NeilBrowna94213b2006-06-26 00:28:00 -07003705
NeilBrown67918752014-12-15 12:57:01 +11003706 err = mddev_lock(mddev);
3707 if (err)
3708 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003709 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003710 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07003711
NeilBrown67918752014-12-15 12:57:01 +11003712 if (!err) {
3713 mddev->recovery_cp = n;
3714 if (mddev->pers)
3715 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3716 }
3717 mddev_unlock(mddev);
3718 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003719}
3720static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10003721__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
3722 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003723
NeilBrown9e653b62006-06-26 00:27:58 -07003724/*
3725 * The array state can be:
3726 *
3727 * clear
3728 * No devices, no size, no level
3729 * Equivalent to STOP_ARRAY ioctl
3730 * inactive
3731 * May have some settings, but array is not active
3732 * all IO results in error
3733 * When written, doesn't tear down array, but just stops it
3734 * suspended (not supported yet)
3735 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003736 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003737 * readonly
3738 * no resync can happen. no superblocks get written.
3739 * write requests fail
3740 * read-auto
3741 * like readonly, but behaves like 'clean' on a write request.
3742 *
3743 * clean - no pending writes, but otherwise active.
3744 * When written to inactive array, starts without resync
3745 * If a write request arrives then
3746 * if metadata is known, mark 'dirty' and switch to 'active'.
3747 * if not known, block and switch to write-pending
3748 * If written to an active array that has pending writes, then fails.
3749 * active
3750 * fully active: IO and resync can be happening.
3751 * When written to inactive array, starts with resync
3752 *
3753 * write-pending
3754 * clean, but writes are blocked waiting for 'active' to be written.
3755 *
3756 * active-idle
3757 * like active, but no writes have been seen for a while (100msec).
3758 *
3759 */
3760enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3761 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003762static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003763 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3764 "write-pending", "active-idle", NULL };
3765
3766static int match_word(const char *word, char **list)
3767{
3768 int n;
3769 for (n=0; list[n]; n++)
3770 if (cmd_match(word, list[n]))
3771 break;
3772 return n;
3773}
3774
3775static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003776array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003777{
3778 enum array_state st = inactive;
3779
3780 if (mddev->pers)
3781 switch(mddev->ro) {
3782 case 1:
3783 st = readonly;
3784 break;
3785 case 2:
3786 st = read_auto;
3787 break;
3788 case 0:
3789 if (mddev->in_sync)
3790 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003791 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003792 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003793 else if (mddev->safemode)
3794 st = active_idle;
3795 else
3796 st = active;
3797 }
3798 else {
3799 if (list_empty(&mddev->disks) &&
3800 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003801 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003802 st = clear;
3803 else
3804 st = inactive;
3805 }
3806 return sprintf(page, "%s\n", array_states[st]);
3807}
3808
NeilBrownf72ffdd2014-09-30 14:23:59 +10003809static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3810static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3811static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003812static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003813
3814static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003815array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003816{
NeilBrown67918752014-12-15 12:57:01 +11003817 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003818 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003819
3820 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3821 /* don't take reconfig_mutex when toggling between
3822 * clean and active
3823 */
3824 spin_lock(&mddev->lock);
3825 if (st == active) {
3826 restart_array(mddev);
3827 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3828 wake_up(&mddev->sb_wait);
3829 err = 0;
3830 } else /* st == clean */ {
3831 restart_array(mddev);
3832 if (atomic_read(&mddev->writes_pending) == 0) {
3833 if (mddev->in_sync == 0) {
3834 mddev->in_sync = 1;
3835 if (mddev->safemode == 1)
3836 mddev->safemode = 0;
3837 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3838 }
3839 err = 0;
3840 } else
3841 err = -EBUSY;
3842 }
3843 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10003844 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11003845 }
3846 err = mddev_lock(mddev);
3847 if (err)
3848 return err;
3849 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003850 switch(st) {
3851 case bad_word:
3852 break;
3853 case clear:
3854 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003855 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003856 break;
3857 case inactive:
3858 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003859 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003860 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003861 else
NeilBrowne6910632008-02-06 01:39:51 -08003862 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003863 break;
3864 case suspended:
3865 break; /* not supported yet */
3866 case readonly:
3867 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003868 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003869 else {
3870 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003871 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003872 err = do_md_run(mddev);
3873 }
3874 break;
3875 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003876 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003877 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003878 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003879 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003880 err = restart_array(mddev);
3881 if (err == 0) {
3882 mddev->ro = 2;
3883 set_disk_ro(mddev->gendisk, 0);
3884 }
NeilBrown9e653b62006-06-26 00:27:58 -07003885 } else {
3886 mddev->ro = 2;
3887 err = do_md_run(mddev);
3888 }
3889 break;
3890 case clean:
3891 if (mddev->pers) {
3892 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003893 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003894 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003895 if (mddev->in_sync == 0) {
3896 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003897 if (mddev->safemode == 1)
3898 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003899 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003900 }
3901 err = 0;
3902 } else
3903 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003904 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003905 } else
3906 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003907 break;
3908 case active:
3909 if (mddev->pers) {
3910 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003911 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003912 wake_up(&mddev->sb_wait);
3913 err = 0;
3914 } else {
3915 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003916 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003917 err = do_md_run(mddev);
3918 }
3919 break;
3920 case write_pending:
3921 case active_idle:
3922 /* these cannot be set */
3923 break;
3924 }
NeilBrown67918752014-12-15 12:57:01 +11003925
3926 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11003927 if (mddev->hold_active == UNTIL_IOCTL)
3928 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003929 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003930 }
NeilBrown67918752014-12-15 12:57:01 +11003931 mddev_unlock(mddev);
3932 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07003933}
NeilBrown80ca3a42006-07-10 04:44:18 -07003934static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10003935__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003936
NeilBrown6d7ff7382006-01-06 00:21:16 -08003937static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003938max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003939 return sprintf(page, "%d\n",
3940 atomic_read(&mddev->max_corr_read_errors));
3941}
3942
3943static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003944max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003945{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003946 unsigned int n;
3947 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11003948
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003949 rv = kstrtouint(buf, 10, &n);
3950 if (rv < 0)
3951 return rv;
3952 atomic_set(&mddev->max_corr_read_errors, n);
3953 return len;
Robert Becker1e509152009-12-14 12:49:58 +11003954}
3955
3956static struct md_sysfs_entry max_corr_read_errors =
3957__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3958 max_corrected_read_errors_store);
3959
3960static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003961null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003962{
3963 return -EINVAL;
3964}
3965
3966static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003967new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003968{
3969 /* buf must be %d:%d\n? giving major and minor numbers */
3970 /* The new device is added to the array.
3971 * If the array has a persistent superblock, we read the
3972 * superblock to initialise info and check validity.
3973 * Otherwise, only checking done is that in bind_rdev_to_array,
3974 * which mainly checks size.
3975 */
3976 char *e;
3977 int major = simple_strtoul(buf, &e, 10);
3978 int minor;
3979 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003980 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003981 int err;
3982
3983 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3984 return -EINVAL;
3985 minor = simple_strtoul(e+1, &e, 10);
3986 if (*e && *e != '\n')
3987 return -EINVAL;
3988 dev = MKDEV(major, minor);
3989 if (major != MAJOR(dev) ||
3990 minor != MINOR(dev))
3991 return -EOVERFLOW;
3992
NeilBrown67918752014-12-15 12:57:01 +11003993 flush_workqueue(md_misc_wq);
3994
3995 err = mddev_lock(mddev);
3996 if (err)
3997 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003998 if (mddev->persistent) {
3999 rdev = md_import_device(dev, mddev->major_version,
4000 mddev->minor_version);
4001 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004002 struct md_rdev *rdev0
4003 = list_entry(mddev->disks.next,
4004 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004005 err = super_types[mddev->major_version]
4006 .load_super(rdev, rdev0, mddev->minor_version);
4007 if (err < 0)
4008 goto out;
4009 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004010 } else if (mddev->external)
4011 rdev = md_import_device(dev, -2, -1);
4012 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004013 rdev = md_import_device(dev, -1, -1);
4014
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004015 if (IS_ERR(rdev)) {
4016 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004017 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004018 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004019 err = bind_rdev_to_array(rdev, mddev);
4020 out:
4021 if (err)
4022 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004023 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004024 return err ? err : len;
4025}
4026
4027static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004028__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004029
4030static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004031bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004032{
4033 char *end;
4034 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004035 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004036
NeilBrown67918752014-12-15 12:57:01 +11004037 err = mddev_lock(mddev);
4038 if (err)
4039 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004040 if (!mddev->bitmap)
4041 goto out;
4042 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4043 while (*buf) {
4044 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4045 if (buf == end) break;
4046 if (*end == '-') { /* range */
4047 buf = end + 1;
4048 end_chunk = simple_strtoul(buf, &end, 0);
4049 if (buf == end) break;
4050 }
4051 if (*end && !isspace(*end)) break;
4052 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004053 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004054 }
4055 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4056out:
NeilBrown67918752014-12-15 12:57:01 +11004057 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004058 return len;
4059}
4060
4061static struct md_sysfs_entry md_bitmap =
4062__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4063
4064static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004065size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004066{
Andre Noll58c0fed2009-03-31 14:33:13 +11004067 return sprintf(page, "%llu\n",
4068 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004069}
4070
NeilBrownfd01b882011-10-11 16:47:53 +11004071static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004072
4073static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004074size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004075{
4076 /* If array is inactive, we can reduce the component size, but
4077 * not increase it (except from 0).
4078 * If array is active, we can try an on-line resize
4079 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004080 sector_t sectors;
4081 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004082
Andre Noll58c0fed2009-03-31 14:33:13 +11004083 if (err < 0)
4084 return err;
NeilBrown67918752014-12-15 12:57:01 +11004085 err = mddev_lock(mddev);
4086 if (err)
4087 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004088 if (mddev->pers) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004089 if (mddev_is_clustered(mddev))
4090 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11004091 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004092 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05004093 if (mddev_is_clustered(mddev))
4094 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004095 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004096 if (mddev->dev_sectors == 0 ||
4097 mddev->dev_sectors > sectors)
4098 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004099 else
4100 err = -ENOSPC;
4101 }
NeilBrown67918752014-12-15 12:57:01 +11004102 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004103 return err ? err : len;
4104}
4105
4106static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004107__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004108
Masanari Iida83f0d772012-10-30 00:18:08 +09004109/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004110 * This is one of
4111 * 'none' for arrays with no metadata (good luck...)
4112 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004113 * or N.M for internally known formats
4114 */
4115static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004116metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004117{
4118 if (mddev->persistent)
4119 return sprintf(page, "%d.%d\n",
4120 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004121 else if (mddev->external)
4122 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004123 else
4124 return sprintf(page, "none\n");
4125}
4126
4127static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004128metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004129{
4130 int major, minor;
4131 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004132 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004133 /* Changing the details of 'external' metadata is
4134 * always permitted. Otherwise there must be
4135 * no devices attached to the array.
4136 */
NeilBrown67918752014-12-15 12:57:01 +11004137
4138 err = mddev_lock(mddev);
4139 if (err)
4140 return err;
4141 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004142 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4143 ;
4144 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004145 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004146
NeilBrown67918752014-12-15 12:57:01 +11004147 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004148 if (cmd_match(buf, "none")) {
4149 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004150 mddev->external = 0;
4151 mddev->major_version = 0;
4152 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004153 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004154 }
4155 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004156 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004157 if (namelen >= sizeof(mddev->metadata_type))
4158 namelen = sizeof(mddev->metadata_type)-1;
4159 strncpy(mddev->metadata_type, buf+9, namelen);
4160 mddev->metadata_type[namelen] = 0;
4161 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4162 mddev->metadata_type[--namelen] = 0;
4163 mddev->persistent = 0;
4164 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004165 mddev->major_version = 0;
4166 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004167 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004168 }
4169 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004170 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004171 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004172 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004173 buf = e+1;
4174 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004175 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004176 goto out_unlock;
4177 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004178 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004179 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004180 mddev->major_version = major;
4181 mddev->minor_version = minor;
4182 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004183 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004184 err = 0;
4185out_unlock:
4186 mddev_unlock(mddev);
4187 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004188}
4189
4190static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004191__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004192
NeilBrowna35b0d62006-01-06 00:20:49 -08004193static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004194action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004195{
NeilBrown7eec3142005-11-08 21:39:44 -08004196 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004197 unsigned long recovery = mddev->recovery;
4198 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004199 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004200 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4201 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4202 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004203 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004204 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4205 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004206 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004207 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004208 type = "check";
4209 else
4210 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004211 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004212 type = "recover";
4213 }
4214 return sprintf(page, "%s\n", type);
4215}
4216
4217static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004218action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004219{
NeilBrown7eec3142005-11-08 21:39:44 -08004220 if (!mddev->pers || !mddev->pers->sync_request)
4221 return -EINVAL;
4222
NeilBrownb6a9ce62009-05-26 09:41:17 +10004223
4224 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004225 if (cmd_match(page, "frozen"))
4226 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4227 else
4228 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004229 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4230 mddev_lock(mddev) == 0) {
4231 flush_workqueue(md_misc_wq);
4232 if (mddev->sync_thread) {
4233 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004234 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004235 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004236 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004237 }
NeilBrown03c902e2006-01-06 00:20:46 -08004238 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4239 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004240 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004241 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004242 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004243 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004244 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004245 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004246 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004247 int err;
4248 if (mddev->pers->start_reshape == NULL)
4249 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004250 err = mddev_lock(mddev);
4251 if (!err) {
NeilBrown56ccc112015-05-28 17:53:29 +10004252 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004253 err = mddev->pers->start_reshape(mddev);
4254 mddev_unlock(mddev);
4255 }
NeilBrown16484bf2006-03-27 01:18:13 -08004256 if (err)
4257 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004258 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004259 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004260 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004261 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004262 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004263 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004264 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004265 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4266 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004267 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004268 if (mddev->ro == 2) {
4269 /* A write to sync_action is enough to justify
4270 * canceling read-auto mode
4271 */
4272 mddev->ro = 0;
4273 md_wakeup_thread(mddev->sync_thread);
4274 }
NeilBrown03c902e2006-01-06 00:20:46 -08004275 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004276 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004277 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004278 return len;
4279}
4280
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004281static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004282__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004283
4284static ssize_t
4285last_sync_action_show(struct mddev *mddev, char *page)
4286{
4287 return sprintf(page, "%s\n", mddev->last_sync_action);
4288}
4289
4290static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4291
NeilBrown9d888832005-11-08 21:39:26 -08004292static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004293mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004294{
4295 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004296 (unsigned long long)
4297 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004298}
4299
NeilBrown80ca3a42006-07-10 04:44:18 -07004300static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004301
NeilBrown88202a02006-01-06 00:21:36 -08004302static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004303sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004304{
4305 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4306 mddev->sync_speed_min ? "local": "system");
4307}
4308
4309static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004310sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004311{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004312 unsigned int min;
4313 int rv;
4314
NeilBrown88202a02006-01-06 00:21:36 -08004315 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004316 min = 0;
4317 } else {
4318 rv = kstrtouint(buf, 10, &min);
4319 if (rv < 0)
4320 return rv;
4321 if (min == 0)
4322 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004323 }
NeilBrown88202a02006-01-06 00:21:36 -08004324 mddev->sync_speed_min = min;
4325 return len;
4326}
4327
4328static struct md_sysfs_entry md_sync_min =
4329__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4330
4331static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004332sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004333{
4334 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4335 mddev->sync_speed_max ? "local": "system");
4336}
4337
4338static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004339sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004340{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004341 unsigned int max;
4342 int rv;
4343
NeilBrown88202a02006-01-06 00:21:36 -08004344 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004345 max = 0;
4346 } else {
4347 rv = kstrtouint(buf, 10, &max);
4348 if (rv < 0)
4349 return rv;
4350 if (max == 0)
4351 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004352 }
NeilBrown88202a02006-01-06 00:21:36 -08004353 mddev->sync_speed_max = max;
4354 return len;
4355}
4356
4357static struct md_sysfs_entry md_sync_max =
4358__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4359
Iustin Popd7f3d292007-10-16 23:30:54 -07004360static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004361degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004362{
4363 return sprintf(page, "%d\n", mddev->degraded);
4364}
4365static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004366
4367static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004368sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004369{
4370 return sprintf(page, "%d\n", mddev->parallel_resync);
4371}
4372
4373static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004374sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004375{
4376 long n;
4377
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004378 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004379 return -EINVAL;
4380
4381 if (n != 0 && n != 1)
4382 return -EINVAL;
4383
4384 mddev->parallel_resync = n;
4385
4386 if (mddev->sync_thread)
4387 wake_up(&resync_wait);
4388
4389 return len;
4390}
4391
4392/* force parallel resync, even with shared block devices */
4393static struct md_sysfs_entry md_sync_force_parallel =
4394__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4395 sync_force_parallel_show, sync_force_parallel_store);
4396
4397static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004398sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004399{
4400 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004401 if (mddev->curr_resync == 0)
4402 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004403 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4404 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004405 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004406 db = resync - mddev->resync_mark_cnt;
4407 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004408}
4409
NeilBrown80ca3a42006-07-10 04:44:18 -07004410static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004411
4412static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004413sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004414{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004415 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004416
NeilBrownacb180b2009-04-14 16:28:34 +10004417 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4418 return sprintf(page, "none\n");
4419
NeilBrown72f36d52012-10-11 14:25:57 +11004420 if (mddev->curr_resync == 1 ||
4421 mddev->curr_resync == 2)
4422 return sprintf(page, "delayed\n");
4423
NeilBrownc804cde2012-05-21 09:28:33 +10004424 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4425 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004426 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004427 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004428 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004429
NeilBrownacb180b2009-04-14 16:28:34 +10004430 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004431 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004432}
4433
NeilBrown750f1992014-09-30 08:53:05 +10004434static struct md_sysfs_entry md_sync_completed =
4435 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004436
NeilBrowne464eaf2006-03-27 01:18:14 -08004437static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004438min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004439{
4440 return sprintf(page, "%llu\n",
4441 (unsigned long long)mddev->resync_min);
4442}
4443static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004444min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004445{
4446 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004447 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004448
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004449 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004450 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004451
4452 spin_lock(&mddev->lock);
4453 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004454 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004455 goto out_unlock;
4456
4457 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004458 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004459 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004460
NeilBrown50c37b12015-03-23 17:36:38 +11004461 /* Round down to multiple of 4K for safety */
4462 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004463 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004464
NeilBrown23da4222014-12-15 12:57:01 +11004465out_unlock:
4466 spin_unlock(&mddev->lock);
4467 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004468}
4469
4470static struct md_sysfs_entry md_min_sync =
4471__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4472
4473static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004474max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004475{
4476 if (mddev->resync_max == MaxSector)
4477 return sprintf(page, "max\n");
4478 else
4479 return sprintf(page, "%llu\n",
4480 (unsigned long long)mddev->resync_max);
4481}
4482static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004483max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004484{
NeilBrown23da4222014-12-15 12:57:01 +11004485 int err;
4486 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004487 if (strncmp(buf, "max", 3) == 0)
4488 mddev->resync_max = MaxSector;
4489 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004490 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004491 int chunk;
4492
4493 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004494 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004495 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004496 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004497 goto out_unlock;
4498
4499 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004500 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004501 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004502 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004503 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004504
4505 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004506 chunk = mddev->chunk_sectors;
4507 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004508 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004509
4510 err = -EINVAL;
4511 if (sector_div(temp, chunk))
4512 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004513 }
4514 mddev->resync_max = max;
4515 }
4516 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004517 err = 0;
4518out_unlock:
4519 spin_unlock(&mddev->lock);
4520 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004521}
4522
4523static struct md_sysfs_entry md_max_sync =
4524__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4525
4526static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004527suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004528{
4529 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4530}
4531
4532static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004533suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004534{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004535 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004536 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004537
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004538 err = kstrtoull(buf, 10, &new);
4539 if (err < 0)
4540 return err;
4541 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004542 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004543
NeilBrown67918752014-12-15 12:57:01 +11004544 err = mddev_lock(mddev);
4545 if (err)
4546 return err;
4547 err = -EINVAL;
4548 if (mddev->pers == NULL ||
4549 mddev->pers->quiesce == NULL)
4550 goto unlock;
4551 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004552 mddev->suspend_lo = new;
4553 if (new >= old)
4554 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004555 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004556 else {
4557 /* Expanding suspended region - need to wait */
4558 mddev->pers->quiesce(mddev, 1);
4559 mddev->pers->quiesce(mddev, 0);
4560 }
NeilBrown67918752014-12-15 12:57:01 +11004561 err = 0;
4562unlock:
4563 mddev_unlock(mddev);
4564 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004565}
4566static struct md_sysfs_entry md_suspend_lo =
4567__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4568
NeilBrowne464eaf2006-03-27 01:18:14 -08004569static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004570suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004571{
4572 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4573}
4574
4575static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004576suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004577{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004578 unsigned long long old, new;
NeilBrown67918752014-12-15 12:57:01 +11004579 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004580
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004581 err = kstrtoull(buf, 10, &new);
4582 if (err < 0)
4583 return err;
4584 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004585 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004586
NeilBrown67918752014-12-15 12:57:01 +11004587 err = mddev_lock(mddev);
4588 if (err)
4589 return err;
4590 err = -EINVAL;
4591 if (mddev->pers == NULL ||
4592 mddev->pers->quiesce == NULL)
4593 goto unlock;
4594 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004595 mddev->suspend_hi = new;
4596 if (new <= old)
4597 /* Shrinking suspended region */
4598 mddev->pers->quiesce(mddev, 2);
4599 else {
4600 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004601 mddev->pers->quiesce(mddev, 1);
4602 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004603 }
NeilBrown67918752014-12-15 12:57:01 +11004604 err = 0;
4605unlock:
4606 mddev_unlock(mddev);
4607 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004608}
4609static struct md_sysfs_entry md_suspend_hi =
4610__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4611
NeilBrown08a02ec2007-05-09 02:35:38 -07004612static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004613reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004614{
4615 if (mddev->reshape_position != MaxSector)
4616 return sprintf(page, "%llu\n",
4617 (unsigned long long)mddev->reshape_position);
4618 strcpy(page, "none\n");
4619 return 5;
4620}
4621
4622static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004623reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004624{
NeilBrownc6563a82012-05-21 09:27:00 +10004625 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004626 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004627 int err;
NeilBrown67918752014-12-15 12:57:01 +11004628
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004629 err = kstrtoull(buf, 10, &new);
4630 if (err < 0)
4631 return err;
4632 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07004633 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004634 err = mddev_lock(mddev);
4635 if (err)
4636 return err;
4637 err = -EBUSY;
4638 if (mddev->pers)
4639 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004640 mddev->reshape_position = new;
4641 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004642 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004643 mddev->new_level = mddev->level;
4644 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004645 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004646 rdev_for_each(rdev, mddev)
4647 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004648 err = 0;
4649unlock:
4650 mddev_unlock(mddev);
4651 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004652}
4653
4654static struct md_sysfs_entry md_reshape_position =
4655__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4656 reshape_position_store);
4657
Dan Williamsb522adc2009-03-31 15:00:31 +11004658static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004659reshape_direction_show(struct mddev *mddev, char *page)
4660{
4661 return sprintf(page, "%s\n",
4662 mddev->reshape_backwards ? "backwards" : "forwards");
4663}
4664
4665static ssize_t
4666reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4667{
4668 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004669 int err;
4670
NeilBrown2c810cd2012-05-21 09:27:00 +10004671 if (cmd_match(buf, "forwards"))
4672 backwards = 0;
4673 else if (cmd_match(buf, "backwards"))
4674 backwards = 1;
4675 else
4676 return -EINVAL;
4677 if (mddev->reshape_backwards == backwards)
4678 return len;
4679
NeilBrown67918752014-12-15 12:57:01 +11004680 err = mddev_lock(mddev);
4681 if (err)
4682 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004683 /* check if we are allowed to change */
4684 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004685 err = -EBUSY;
4686 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004687 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004688 err = -EINVAL;
4689 else
4690 mddev->reshape_backwards = backwards;
4691 mddev_unlock(mddev);
4692 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004693}
4694
4695static struct md_sysfs_entry md_reshape_direction =
4696__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4697 reshape_direction_store);
4698
4699static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004700array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004701{
4702 if (mddev->external_size)
4703 return sprintf(page, "%llu\n",
4704 (unsigned long long)mddev->array_sectors/2);
4705 else
4706 return sprintf(page, "default\n");
4707}
4708
4709static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004710array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004711{
4712 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004713 int err;
4714
4715 err = mddev_lock(mddev);
4716 if (err)
4717 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004718
4719 if (strncmp(buf, "default", 7) == 0) {
4720 if (mddev->pers)
4721 sectors = mddev->pers->size(mddev, 0, 0);
4722 else
4723 sectors = mddev->array_sectors;
4724
4725 mddev->external_size = 0;
4726 } else {
4727 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004728 err = -EINVAL;
4729 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4730 err = -E2BIG;
4731 else
4732 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004733 }
4734
NeilBrown67918752014-12-15 12:57:01 +11004735 if (!err) {
4736 mddev->array_sectors = sectors;
4737 if (mddev->pers) {
4738 set_capacity(mddev->gendisk, mddev->array_sectors);
4739 revalidate_disk(mddev->gendisk);
4740 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004741 }
NeilBrown67918752014-12-15 12:57:01 +11004742 mddev_unlock(mddev);
4743 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004744}
4745
4746static struct md_sysfs_entry md_array_size =
4747__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4748 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004749
NeilBrowneae17012005-11-08 21:39:23 -08004750static struct attribute *md_default_attrs[] = {
4751 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004752 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004753 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004754 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004755 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004756 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004757 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004758 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004759 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004760 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004761 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004762 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004763 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004764 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004765 NULL,
4766};
4767
4768static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004769 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004770 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004771 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004772 &md_sync_min.attr,
4773 &md_sync_max.attr,
4774 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004775 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004776 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004777 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004778 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004779 &md_suspend_lo.attr,
4780 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004781 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004782 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004783 NULL,
4784};
NeilBrown411036f2005-11-08 21:39:40 -08004785static struct attribute_group md_redundancy_group = {
4786 .name = NULL,
4787 .attrs = md_redundancy_attrs,
4788};
4789
NeilBrowneae17012005-11-08 21:39:23 -08004790static ssize_t
4791md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4792{
4793 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004794 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004795 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004796
4797 if (!entry->show)
4798 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004799 spin_lock(&all_mddevs_lock);
4800 if (list_empty(&mddev->all_mddevs)) {
4801 spin_unlock(&all_mddevs_lock);
4802 return -EBUSY;
4803 }
4804 mddev_get(mddev);
4805 spin_unlock(&all_mddevs_lock);
4806
NeilBrownb7b17c92014-12-15 12:56:59 +11004807 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004808 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004809 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004810}
4811
4812static ssize_t
4813md_attr_store(struct kobject *kobj, struct attribute *attr,
4814 const char *page, size_t length)
4815{
4816 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004817 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004818 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004819
4820 if (!entry->store)
4821 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004822 if (!capable(CAP_SYS_ADMIN))
4823 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004824 spin_lock(&all_mddevs_lock);
4825 if (list_empty(&mddev->all_mddevs)) {
4826 spin_unlock(&all_mddevs_lock);
4827 return -EBUSY;
4828 }
4829 mddev_get(mddev);
4830 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004831 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004832 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004833 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004834}
4835
4836static void md_free(struct kobject *ko)
4837{
NeilBrownfd01b882011-10-11 16:47:53 +11004838 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004839
4840 if (mddev->sysfs_state)
4841 sysfs_put(mddev->sysfs_state);
4842
NeilBrown6cd18e72015-04-27 14:12:22 +10004843 if (mddev->queue)
4844 blk_cleanup_queue(mddev->queue);
NeilBrowna21d1502009-01-09 08:31:09 +11004845 if (mddev->gendisk) {
4846 del_gendisk(mddev->gendisk);
4847 put_disk(mddev->gendisk);
4848 }
NeilBrowna21d1502009-01-09 08:31:09 +11004849
NeilBrowneae17012005-11-08 21:39:23 -08004850 kfree(mddev);
4851}
4852
Emese Revfy52cf25d2010-01-19 02:58:23 +01004853static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004854 .show = md_attr_show,
4855 .store = md_attr_store,
4856};
4857static struct kobj_type md_ktype = {
4858 .release = md_free,
4859 .sysfs_ops = &md_sysfs_ops,
4860 .default_attrs = md_default_attrs,
4861};
4862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863int mdp_major = 0;
4864
Dan Williams5fd3a172009-03-04 00:57:25 -07004865static void mddev_delayed_delete(struct work_struct *ws)
4866{
NeilBrownfd01b882011-10-11 16:47:53 +11004867 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004868
NeilBrown43a70502009-12-14 12:49:55 +11004869 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004870 kobject_del(&mddev->kobj);
4871 kobject_put(&mddev->kobj);
4872}
4873
NeilBrownefeb53c2009-01-09 08:31:10 +11004874static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004876 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004877 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004879 int partitioned;
4880 int shift;
4881 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004882 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
4884 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004885 return -ENODEV;
4886
4887 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4888 shift = partitioned ? MdpMinorShift : 0;
4889 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Tejun Heoe804ac72010-10-15 15:36:08 +02004891 /* wait for any previous instance of this device to be
4892 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004893 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004894 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004895
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004896 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004897 error = -EEXIST;
4898 if (mddev->gendisk)
4899 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004900
4901 if (name) {
4902 /* Need to ensure that 'name' is not a duplicate.
4903 */
NeilBrownfd01b882011-10-11 16:47:53 +11004904 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004905 spin_lock(&all_mddevs_lock);
4906
4907 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4908 if (mddev2->gendisk &&
4909 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4910 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004911 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004912 }
4913 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 }
NeilBrown8b765392009-01-09 08:31:08 +11004915
NeilBrown0909dc42009-07-01 12:27:21 +10004916 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004917 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004918 if (!mddev->queue)
4919 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004920 mddev->queue->queuedata = mddev;
4921
NeilBrown409c57f2009-03-31 14:39:39 +11004922 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004923 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 disk = alloc_disk(1 << shift);
4926 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004927 blk_cleanup_queue(mddev->queue);
4928 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004929 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004931 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004933 if (name)
4934 strcpy(disk->disk_name, name);
4935 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004937 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 disk->fops = &md_fops;
4940 disk->private_data = mddev;
4941 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004942 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004943 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004944 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004945 * remove it now.
4946 */
4947 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004949 /* As soon as we call add_disk(), another thread could get
4950 * through to md_open, so make sure it doesn't get too far
4951 */
4952 mutex_lock(&mddev->open_mutex);
4953 add_disk(disk);
4954
Tejun Heoed9e1982008-08-25 19:56:05 +09004955 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4956 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004957 if (error) {
4958 /* This isn't possible, but as kobject_init_and_add is marked
4959 * __must_check, we must do something with the result
4960 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004961 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4962 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004963 error = 0;
4964 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004965 if (mddev->kobj.sd &&
4966 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004967 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004968 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004969 abort:
4970 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004971 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004972 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004973 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004974 }
NeilBrownd3374822009-01-09 08:31:10 +11004975 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004976 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004977}
4978
4979static struct kobject *md_probe(dev_t dev, int *part, void *data)
4980{
4981 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 return NULL;
4983}
4984
NeilBrownefeb53c2009-01-09 08:31:10 +11004985static int add_named_array(const char *val, struct kernel_param *kp)
4986{
4987 /* val must be "md_*" where * is not all digits.
4988 * We allocate an array with a large free minor number, and
4989 * set the name to val. val must not already be an active name.
4990 */
4991 int len = strlen(val);
4992 char buf[DISK_NAME_LEN];
4993
4994 while (len && val[len-1] == '\n')
4995 len--;
4996 if (len >= DISK_NAME_LEN)
4997 return -E2BIG;
4998 strlcpy(buf, val, len+1);
4999 if (strncmp(buf, "md_", 3) != 0)
5000 return -EINVAL;
5001 return md_alloc(0, buf);
5002}
5003
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004static void md_safemode_timeout(unsigned long data)
5005{
NeilBrownfd01b882011-10-11 16:47:53 +11005006 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Neil Brown0fd62b82008-06-28 08:31:36 +10005008 if (!atomic_read(&mddev->writes_pending)) {
5009 mddev->safemode = 1;
5010 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10005011 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 md_wakeup_thread(mddev->thread);
5014}
5015
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005016static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
NeilBrownfd01b882011-10-11 16:47:53 +11005018int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019{
NeilBrown2604b702006-01-06 00:20:36 -08005020 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005021 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005022 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
NeilBrowna757e642005-04-16 15:26:42 -07005024 if (list_empty(&mddev->disks))
5025 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
5028 if (mddev->pers)
5029 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005030 /* Cannot run until previous stop completes properly */
5031 if (mddev->sysfs_active)
5032 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005033
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 /*
5035 * Analyze all RAID superblock(s)
5036 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005037 if (!mddev->raid_disks) {
5038 if (!mddev->persistent)
5039 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005040 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
NeilBrownd9d166c2006-01-06 00:20:51 -08005043 if (mddev->level != LEVEL_NONE)
5044 request_module("md-level-%d", mddev->level);
5045 else if (mddev->clevel[0])
5046 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
5048 /*
5049 * Drop all container device buffers, from now on
5050 * the only valid external interface is through the md
5051 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005053 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005054 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 continue;
5056 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005057 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005058
5059 /* perform some consistency tests on the device.
5060 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005061 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005062 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005063 if (rdev->meta_bdev) {
5064 /* Nothing to check */;
5065 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005066 if (mddev->dev_sectors &&
5067 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005068 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005069 printk("md: %s: data overlaps metadata\n",
5070 mdname(mddev));
5071 return -EINVAL;
5072 }
5073 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005074 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005075 > rdev->data_offset) {
5076 printk("md: %s: metadata overlaps data\n",
5077 mdname(mddev));
5078 return -EINVAL;
5079 }
5080 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005081 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 }
5083
NeilBrowna167f662010-10-26 18:31:13 +11005084 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005085 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005086
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005088 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005089 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005091 if (mddev->level != LEVEL_NONE)
5092 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5093 mddev->level);
5094 else
5095 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5096 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 return -EINVAL;
5098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005100 if (mddev->level != pers->level) {
5101 mddev->level = pers->level;
5102 mddev->new_level = pers->level;
5103 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005104 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
NeilBrownf6705572006-03-27 01:18:11 -08005106 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005107 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005108 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005109 module_put(pers->owner);
5110 return -EINVAL;
5111 }
5112
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005113 if (pers->sync_request) {
5114 /* Warn if this is a potentially silly
5115 * configuration.
5116 */
5117 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005118 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005119 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005120
NeilBrowndafb20f2012-03-19 12:46:39 +11005121 rdev_for_each(rdev, mddev)
5122 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005123 if (rdev < rdev2 &&
5124 rdev->bdev->bd_contains ==
5125 rdev2->bdev->bd_contains) {
5126 printk(KERN_WARNING
5127 "%s: WARNING: %s appears to be"
5128 " on the same physical disk as"
5129 " %s.\n",
5130 mdname(mddev),
5131 bdevname(rdev->bdev,b),
5132 bdevname(rdev2->bdev,b2));
5133 warned = 1;
5134 }
5135 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005136
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005137 if (warned)
5138 printk(KERN_WARNING
5139 "True protection against single-disk"
5140 " failure might be compromised.\n");
5141 }
5142
NeilBrown657390d2005-08-26 18:34:16 -07005143 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005144 /* may be over-ridden by personality */
5145 mddev->resync_max_sectors = mddev->dev_sectors;
5146
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005147 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
NeilBrown0f9552b52009-12-30 12:08:50 +11005149 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005150 mddev->ro = 2; /* read-only, but switch on first write */
5151
NeilBrown36d091f2014-12-15 12:56:58 +11005152 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005153 if (err)
5154 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005155 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005156 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5157 " but 'external_size' not in effect?\n", __func__);
5158 printk(KERN_ERR
5159 "md: invalid array_size %llu > default size %llu\n",
5160 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005161 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005162 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005163 }
NeilBrown36d091f2014-12-15 12:56:58 +11005164 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005165 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005166 struct bitmap *bitmap;
5167
5168 bitmap = bitmap_create(mddev, -1);
5169 if (IS_ERR(bitmap)) {
5170 err = PTR_ERR(bitmap);
NeilBrownb15c2e52006-01-06 00:20:16 -08005171 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5172 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005173 } else
5174 mddev->bitmap = bitmap;
5175
NeilBrownb15c2e52006-01-06 00:20:16 -08005176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005178 mddev_detach(mddev);
NeilBrown0c35bd42015-03-13 11:51:18 +11005179 if (mddev->private)
5180 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005181 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005182 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005183 bitmap_destroy(mddev);
5184 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 }
NeilBrown5c675f82014-12-15 12:56:56 +11005186 if (mddev->queue) {
5187 mddev->queue->backing_dev_info.congested_data = mddev;
5188 mddev->queue->backing_dev_info.congested_fn = md_congested;
NeilBrown64590f42014-12-15 12:56:57 +11005189 blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec);
NeilBrown5c675f82014-12-15 12:56:56 +11005190 }
NeilBrown36d091f2014-12-15 12:56:58 +11005191 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005192 if (mddev->kobj.sd &&
5193 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005194 printk(KERN_WARNING
5195 "md: cannot register extra attributes for %s\n",
5196 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005197 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005198 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005199 mddev->ro = 0;
5200
NeilBrownf72ffdd2014-09-30 14:23:59 +10005201 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005202 atomic_set(&mddev->max_corr_read_errors,
5203 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 mddev->safemode = 0;
5205 mddev->safemode_timer.function = md_safemode_timeout;
5206 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005207 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005209 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005210 spin_lock(&mddev->lock);
5211 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005212 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005213 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005214 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005215 if (rdev->raid_disk >= 0)
5216 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005217 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005220
NeilBrown7a0a5352013-08-27 16:28:23 +10005221 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005222 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
NeilBrownd7603b72006-01-06 00:20:30 -08005224 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005225 sysfs_notify_dirent_safe(mddev->sysfs_state);
5226 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005227 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 return 0;
5229}
NeilBrown390ee602010-06-01 19:37:27 +10005230EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
NeilBrownfd01b882011-10-11 16:47:53 +11005232static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005233{
5234 int err;
5235
5236 err = md_run(mddev);
5237 if (err)
5238 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005239 err = bitmap_load(mddev);
5240 if (err) {
5241 bitmap_destroy(mddev);
5242 goto out;
5243 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005244
5245 md_wakeup_thread(mddev->thread);
5246 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5247
NeilBrownfe60b012010-03-29 11:10:42 +11005248 set_capacity(mddev->gendisk, mddev->array_sectors);
5249 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005250 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005251 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5252out:
5253 return err;
5254}
5255
NeilBrownfd01b882011-10-11 16:47:53 +11005256static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
5258 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
Andre Noll80fab1d2008-07-11 22:02:21 +10005260 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005262 return -ENXIO;
5263 if (!mddev->pers)
5264 return -EINVAL;
5265 if (!mddev->ro)
5266 return -EBUSY;
5267 mddev->safemode = 0;
5268 mddev->ro = 0;
5269 set_disk_ro(disk, 0);
5270 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5271 mdname(mddev));
5272 /* Kick recovery or resync if necessary */
5273 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5274 md_wakeup_thread(mddev->thread);
5275 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005276 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278}
5279
NeilBrownfd01b882011-10-11 16:47:53 +11005280static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005281{
5282 mddev->array_sectors = 0;
5283 mddev->external_size = 0;
5284 mddev->dev_sectors = 0;
5285 mddev->raid_disks = 0;
5286 mddev->recovery_cp = 0;
5287 mddev->resync_min = 0;
5288 mddev->resync_max = MaxSector;
5289 mddev->reshape_position = MaxSector;
5290 mddev->external = 0;
5291 mddev->persistent = 0;
5292 mddev->level = LEVEL_NONE;
5293 mddev->clevel[0] = 0;
5294 mddev->flags = 0;
5295 mddev->ro = 0;
5296 mddev->metadata_type[0] = 0;
5297 mddev->chunk_sectors = 0;
5298 mddev->ctime = mddev->utime = 0;
5299 mddev->layout = 0;
5300 mddev->max_disks = 0;
5301 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005302 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005303 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005304 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005305 mddev->new_level = LEVEL_NONE;
5306 mddev->new_layout = 0;
5307 mddev->new_chunk_sectors = 0;
5308 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005309 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005310 mddev->suspend_lo = mddev->suspend_hi = 0;
5311 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5312 mddev->recovery = 0;
5313 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005314 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005315 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005316 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005317 mddev->private = NULL;
NeilBrown050b6612012-03-19 12:46:39 +11005318 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005319 mddev->bitmap_info.offset = 0;
5320 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005321 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005322 mddev->bitmap_info.chunksize = 0;
5323 mddev->bitmap_info.daemon_sleep = 0;
5324 mddev->bitmap_info.max_write_behind = 0;
5325}
5326
NeilBrownfd01b882011-10-11 16:47:53 +11005327static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005328{
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005329 if (mddev_is_clustered(mddev))
5330 md_cluster_ops->metadata_update_start(mddev);
NeilBrown6b6204e2013-05-09 09:48:30 +10005331 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005332 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005333 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005334 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005335 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005336 }
5337
5338 del_timer_sync(&mddev->safemode_timer);
5339
5340 bitmap_flush(mddev);
5341 md_super_wait(mddev);
5342
NeilBrownb6d428c2013-04-24 11:42:42 +10005343 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005344 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005345 /* mark array as shutdown cleanly */
5346 mddev->in_sync = 1;
5347 md_update_sb(mddev, 1);
5348 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05005349 if (mddev_is_clustered(mddev))
5350 md_cluster_ops->metadata_update_finish(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005351}
NeilBrowndefad612011-01-14 09:14:33 +11005352
NeilBrownfd01b882011-10-11 16:47:53 +11005353void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005354{
NeilBrown29f097c2013-11-14 17:54:51 +11005355 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005356 __md_stop_writes(mddev);
5357 mddev_unlock(mddev);
5358}
NeilBrown390ee602010-06-01 19:37:27 +10005359EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005360
NeilBrown5aa61f42014-12-15 12:56:57 +11005361static void mddev_detach(struct mddev *mddev)
5362{
5363 struct bitmap *bitmap = mddev->bitmap;
5364 /* wait for behind writes to complete */
5365 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5366 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5367 mdname(mddev));
5368 /* need to kick something here to make sure I/O goes? */
5369 wait_event(bitmap->behind_wait,
5370 atomic_read(&bitmap->behind_writes) == 0);
5371 }
NeilBrown36d091f2014-12-15 12:56:58 +11005372 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005373 mddev->pers->quiesce(mddev, 1);
5374 mddev->pers->quiesce(mddev, 0);
5375 }
5376 md_unregister_thread(&mddev->thread);
5377 if (mddev->queue)
5378 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5379}
5380
NeilBrown5eff3c42012-11-19 10:47:48 +11005381static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005382{
NeilBrown36d091f2014-12-15 12:56:58 +11005383 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005384 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005385 /* Ensure ->event_work is done */
5386 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005387 spin_lock(&mddev->lock);
5388 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005389 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005390 spin_unlock(&mddev->lock);
5391 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005392 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005393 if (pers->sync_request && mddev->to_remove == NULL)
5394 mddev->to_remove = &md_redundancy_group;
5395 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005396 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005397}
NeilBrown5eff3c42012-11-19 10:47:48 +11005398
5399void md_stop(struct mddev *mddev)
5400{
5401 /* stop the array and free an attached data structures.
5402 * This is called from dm-raid
5403 */
5404 __md_stop(mddev);
5405 bitmap_destroy(mddev);
5406 if (mddev->bio_set)
5407 bioset_free(mddev->bio_set);
5408}
5409
NeilBrown390ee602010-06-01 19:37:27 +10005410EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005411
NeilBrowna05b7ea2012-07-19 15:59:18 +10005412static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005413{
5414 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005415 int did_freeze = 0;
5416
5417 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5418 did_freeze = 1;
5419 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5420 md_wakeup_thread(mddev->thread);
5421 }
NeilBrownf851b602014-12-11 10:02:10 +11005422 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005423 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005424 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005425 /* Thread might be blocked waiting for metadata update
5426 * which will now never happen */
5427 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005428
NeilBrown30b8feb2013-11-14 15:16:17 +11005429 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005430 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5431 &mddev->recovery));
NeilBrown30b8feb2013-11-14 15:16:17 +11005432 mddev_lock_nointr(mddev);
5433
NeilBrowna4bd82d2010-03-29 13:23:10 +11005434 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005435 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005436 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005437 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005438 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005439 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005440 if (did_freeze) {
5441 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005442 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005443 md_wakeup_thread(mddev->thread);
5444 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005445 err = -EBUSY;
5446 goto out;
5447 }
5448 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005449 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005450
5451 err = -ENXIO;
5452 if (mddev->ro==1)
5453 goto out;
5454 mddev->ro = 1;
5455 set_disk_ro(mddev->gendisk, 1);
5456 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005457 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5458 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005459 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005460 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005461 }
5462out:
5463 mutex_unlock(&mddev->open_mutex);
5464 return err;
5465}
5466
NeilBrown9e653b62006-06-26 00:27:58 -07005467/* mode:
5468 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005469 * 2 - stop but do not disassemble array
5470 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005471static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005472 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005475 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005476 int did_freeze = 0;
5477
5478 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5479 did_freeze = 1;
5480 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5481 md_wakeup_thread(mddev->thread);
5482 }
NeilBrownf851b602014-12-11 10:02:10 +11005483 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005484 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005485 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005486 /* Thread might be blocked waiting for metadata update
5487 * which will now never happen */
5488 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005489
NeilBrown30b8feb2013-11-14 15:16:17 +11005490 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005491 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5492 !test_bit(MD_RECOVERY_RUNNING,
5493 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005494 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
NeilBrownc8c00a62009-08-10 12:50:52 +10005496 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005497 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005498 mddev->sysfs_active ||
5499 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005500 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005501 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005502 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005503 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005504 if (did_freeze) {
5505 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005506 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005507 md_wakeup_thread(mddev->thread);
5508 }
NeilBrown260fa032013-08-27 16:44:13 +10005509 return -EBUSY;
5510 }
NeilBrown6e17b022010-08-07 21:41:19 +10005511 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005512 if (mddev->ro)
5513 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005514
NeilBrowndefad612011-01-14 09:14:33 +11005515 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005516 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005517 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005518 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005519
NeilBrowna4bd82d2010-03-29 13:23:10 +11005520 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005521 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005522
NeilBrowndafb20f2012-03-19 12:46:39 +11005523 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005524 if (rdev->raid_disk >= 0)
5525 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005526
NeilBrowna4bd82d2010-03-29 13:23:10 +11005527 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005528 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005529 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005530 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005531
NeilBrowna4bd82d2010-03-29 13:23:10 +11005532 if (mddev->ro)
5533 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005534 } else
5535 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 /*
5537 * Free resources if final stop
5538 */
NeilBrown9e653b62006-06-26 00:27:58 -07005539 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5541
NeilBrown978f9462006-02-02 14:28:05 -08005542 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005543 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005544 struct file *f = mddev->bitmap_info.file;
5545 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005546 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005547 spin_unlock(&mddev->lock);
5548 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005549 }
NeilBrownc3d97142009-12-14 12:49:52 +11005550 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005551
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 export_array(mddev);
5553
NeilBrown6177b472010-03-29 11:37:13 +11005554 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005555 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005556 if (mddev->hold_active == UNTIL_STOP)
5557 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005558 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005559 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005560 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005561 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563}
5564
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005565#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005566static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567{
NeilBrown3cb03002011-10-11 16:45:26 +11005568 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 int err;
5570
NeilBrowna757e642005-04-16 15:26:42 -07005571 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
5574 printk(KERN_INFO "md: running: ");
5575
NeilBrowndafb20f2012-03-19 12:46:39 +11005576 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 char b[BDEVNAME_SIZE];
5578 printk("<%s>", bdevname(rdev->bdev,b));
5579 }
5580 printk("\n");
5581
NeilBrownd710e132008-10-13 11:55:12 +11005582 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 if (err) {
5584 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005585 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 }
5587}
5588
5589/*
5590 * lets try to run arrays based on all disks that have arrived
5591 * until now. (those are in pending_raid_disks)
5592 *
5593 * the method: pick the first pending disk, collect all disks with
5594 * the same UUID, remove all from the pending list and put them into
5595 * the 'same_array' list. Then order this list based on superblock
5596 * update time (freshest comes first), kick out 'old' disks and
5597 * compare superblocks. If everything's fine then run it.
5598 *
5599 * If "unit" is allocated, then bump its reference count
5600 */
5601static void autorun_devices(int part)
5602{
NeilBrown3cb03002011-10-11 16:45:26 +11005603 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005604 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 char b[BDEVNAME_SIZE];
5606
5607 printk(KERN_INFO "md: autorun ...\n");
5608 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005609 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005611 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005613 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
5615 printk(KERN_INFO "md: considering %s ...\n",
5616 bdevname(rdev0->bdev,b));
5617 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005618 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 if (super_90_load(rdev, rdev0, 0) >= 0) {
5620 printk(KERN_INFO "md: adding %s ...\n",
5621 bdevname(rdev->bdev,b));
5622 list_move(&rdev->same_set, &candidates);
5623 }
5624 /*
5625 * now we have a set of devices, with all of them having
5626 * mostly sane superblocks. It's time to allocate the
5627 * mddev.
5628 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005629 if (part) {
5630 dev = MKDEV(mdp_major,
5631 rdev0->preferred_minor << MdpMinorShift);
5632 unit = MINOR(dev) >> MdpMinorShift;
5633 } else {
5634 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5635 unit = MINOR(dev);
5636 }
5637 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5639 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5640 break;
5641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
5643 md_probe(dev, NULL, NULL);
5644 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005645 if (!mddev || !mddev->gendisk) {
5646 if (mddev)
5647 mddev_put(mddev);
5648 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 "md: cannot allocate memory for md drive.\n");
5650 break;
5651 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005652 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 printk(KERN_WARNING "md: %s locked, cannot run\n",
5654 mdname(mddev));
5655 else if (mddev->raid_disks || mddev->major_version
5656 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005657 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 "md: %s already running, cannot run %s\n",
5659 mdname(mddev), bdevname(rdev0->bdev,b));
5660 mddev_unlock(mddev);
5661 } else {
5662 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005663 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005664 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 list_del_init(&rdev->same_set);
5666 if (bind_rdev_to_array(rdev, mddev))
5667 export_rdev(rdev);
5668 }
5669 autorun_array(mddev);
5670 mddev_unlock(mddev);
5671 }
5672 /* on success, candidates will be empty, on error
5673 * it won't...
5674 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005675 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005676 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 mddev_put(mddev);
5680 }
5681 printk(KERN_INFO "md: ... autorun DONE.\n");
5682}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005683#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
NeilBrownf72ffdd2014-09-30 14:23:59 +10005685static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686{
5687 mdu_version_t ver;
5688
5689 ver.major = MD_MAJOR_VERSION;
5690 ver.minor = MD_MINOR_VERSION;
5691 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5692
5693 if (copy_to_user(arg, &ver, sizeof(ver)))
5694 return -EFAULT;
5695
5696 return 0;
5697}
5698
NeilBrownf72ffdd2014-09-30 14:23:59 +10005699static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
5701 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005702 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005703 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
NeilBrown1ca69c42012-10-11 13:37:33 +11005705 nr = working = insync = failed = spare = 0;
5706 rcu_read_lock();
5707 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005709 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 failed++;
5711 else {
5712 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005713 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005714 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 else
5716 spare++;
5717 }
5718 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005719 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 info.major_version = mddev->major_version;
5722 info.minor_version = mddev->minor_version;
5723 info.patch_version = MD_PATCHLEVEL_VERSION;
5724 info.ctime = mddev->ctime;
5725 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005726 info.size = mddev->dev_sectors / 2;
5727 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005728 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 info.nr_disks = nr;
5730 info.raid_disks = mddev->raid_disks;
5731 info.md_minor = mddev->md_minor;
5732 info.not_persistent= !mddev->persistent;
5733
5734 info.utime = mddev->utime;
5735 info.state = 0;
5736 if (mddev->in_sync)
5737 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005738 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005739 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005740 if (mddev_is_clustered(mddev))
5741 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10005742 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 info.working_disks = working;
5744 info.failed_disks = failed;
5745 info.spare_disks = spare;
5746
5747 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005748 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749
5750 if (copy_to_user(arg, &info, sizeof(info)))
5751 return -EFAULT;
5752
5753 return 0;
5754}
5755
NeilBrownf72ffdd2014-09-30 14:23:59 +10005756static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005757{
5758 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005759 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005760 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005761
Benjamin Randazzob6878d92015-07-25 16:36:50 +02005762 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005763 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005764 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005765
NeilBrown32a76272005-06-21 17:17:14 -07005766 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005767 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02005768 /* bitmap enabled */
5769 if (mddev->bitmap_info.file) {
5770 ptr = file_path(mddev->bitmap_info.file, file->pathname,
5771 sizeof(file->pathname));
5772 if (IS_ERR(ptr))
5773 err = PTR_ERR(ptr);
5774 else
5775 memmove(file->pathname, ptr,
5776 sizeof(file->pathname)-(ptr-file->pathname));
5777 }
NeilBrown4af1a042014-12-15 12:57:00 +11005778 spin_unlock(&mddev->lock);
5779
5780 if (err == 0 &&
5781 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005782 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005783
NeilBrown32a76272005-06-21 17:17:14 -07005784 kfree(file);
5785 return err;
5786}
5787
NeilBrownf72ffdd2014-09-30 14:23:59 +10005788static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789{
5790 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005791 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
5793 if (copy_from_user(&info, arg, sizeof(info)))
5794 return -EFAULT;
5795
NeilBrown1ca69c42012-10-11 13:37:33 +11005796 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05005797 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 if (rdev) {
5799 info.major = MAJOR(rdev->bdev->bd_dev);
5800 info.minor = MINOR(rdev->bdev->bd_dev);
5801 info.raid_disk = rdev->raid_disk;
5802 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005803 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005805 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 info.state |= (1<<MD_DISK_ACTIVE);
5807 info.state |= (1<<MD_DISK_SYNC);
5808 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005809 if (test_bit(WriteMostly, &rdev->flags))
5810 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 } else {
5812 info.major = info.minor = 0;
5813 info.raid_disk = -1;
5814 info.state = (1<<MD_DISK_REMOVED);
5815 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005816 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 if (copy_to_user(arg, &info, sizeof(info)))
5819 return -EFAULT;
5820
5821 return 0;
5822}
5823
NeilBrownf72ffdd2014-09-30 14:23:59 +10005824static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825{
5826 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005827 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 dev_t dev = MKDEV(info->major,info->minor);
5829
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005830 if (mddev_is_clustered(mddev) &&
5831 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005832 pr_err("%s: Cannot add to clustered mddev.\n",
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005833 mdname(mddev));
5834 return -EINVAL;
5835 }
5836
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5838 return -EOVERFLOW;
5839
5840 if (!mddev->raid_disks) {
5841 int err;
5842 /* expecting a device which has a superblock */
5843 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5844 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005845 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 "md: md_import_device returned %ld\n",
5847 PTR_ERR(rdev));
5848 return PTR_ERR(rdev);
5849 }
5850 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005851 struct md_rdev *rdev0
5852 = list_entry(mddev->disks.next,
5853 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005854 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 .load_super(rdev, rdev0, mddev->minor_version);
5856 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005857 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005859 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 bdevname(rdev0->bdev,b2));
5861 export_rdev(rdev);
5862 return -EINVAL;
5863 }
5864 }
5865 err = bind_rdev_to_array(rdev, mddev);
5866 if (err)
5867 export_rdev(rdev);
5868 return err;
5869 }
5870
5871 /*
5872 * add_new_disk can be used once the array is assembled
5873 * to add "hot spares". They must already have a superblock
5874 * written
5875 */
5876 if (mddev->pers) {
5877 int err;
5878 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005879 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 "%s: personality does not support diskops!\n",
5881 mdname(mddev));
5882 return -EINVAL;
5883 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005884 if (mddev->persistent)
5885 rdev = md_import_device(dev, mddev->major_version,
5886 mddev->minor_version);
5887 else
5888 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005890 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 "md: md_import_device returned %ld\n",
5892 PTR_ERR(rdev));
5893 return PTR_ERR(rdev);
5894 }
NeilBrown1a855a02010-12-09 16:36:28 +11005895 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005896 if (!mddev->persistent) {
5897 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005898 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005899 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005900 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005901 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005902 } else
NeilBrown41158c72005-06-21 17:17:25 -07005903 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005904 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005905 } else
5906 super_types[mddev->major_version].
5907 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005908 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005909 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005910 /* This was a hot-add request, but events doesn't
5911 * match, so reject it.
5912 */
5913 export_rdev(rdev);
5914 return -EINVAL;
5915 }
5916
NeilBrownb2d444d2005-11-08 21:39:31 -08005917 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005918 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5919 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005920 else
5921 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005922
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005923 /*
5924 * check whether the device shows up in other nodes
5925 */
5926 if (mddev_is_clustered(mddev)) {
5927 if (info->state & (1 << MD_DISK_CANDIDATE)) {
5928 /* Through --cluster-confirm */
5929 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -06005930 err = md_cluster_ops->new_disk_ack(mddev, true);
5931 if (err) {
5932 export_rdev(rdev);
5933 return err;
5934 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005935 } else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
5936 /* --add initiated by this node */
5937 err = md_cluster_ops->add_new_disk_start(mddev, rdev);
5938 if (err) {
5939 md_cluster_ops->add_new_disk_finish(mddev);
5940 export_rdev(rdev);
5941 return err;
5942 }
5943 }
5944 }
5945
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 rdev->raid_disk = -1;
5947 err = bind_rdev_to_array(rdev, mddev);
5948 if (err)
5949 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005950 else
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05005951 err = add_bound_rdev(rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05005952 if (mddev_is_clustered(mddev) &&
5953 (info->state & (1 << MD_DISK_CLUSTER_ADD)))
5954 md_cluster_ops->add_new_disk_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 return err;
5956 }
5957
5958 /* otherwise, add_new_disk is only allowed
5959 * for major_version==0 superblocks
5960 */
5961 if (mddev->major_version != 0) {
5962 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5963 mdname(mddev));
5964 return -EINVAL;
5965 }
5966
5967 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5968 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005969 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005971 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 "md: error, md_import_device() returned %ld\n",
5973 PTR_ERR(rdev));
5974 return PTR_ERR(rdev);
5975 }
5976 rdev->desc_nr = info->number;
5977 if (info->raid_disk < mddev->raid_disks)
5978 rdev->raid_disk = info->raid_disk;
5979 else
5980 rdev->raid_disk = -1;
5981
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005983 if (info->state & (1<<MD_DISK_SYNC))
5984 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005986 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5987 set_bit(WriteMostly, &rdev->flags);
5988
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 if (!mddev->persistent) {
5990 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005991 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5992 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005993 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005994 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
NeilBrown2bf071b2006-01-06 00:20:55 -08005996 err = bind_rdev_to_array(rdev, mddev);
5997 if (err) {
5998 export_rdev(rdev);
5999 return err;
6000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 }
6002
6003 return 0;
6004}
6005
NeilBrownf72ffdd2014-09-30 14:23:59 +10006006static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007{
6008 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006009 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 rdev = find_rdev(mddev, dev);
6012 if (!rdev)
6013 return -ENXIO;
6014
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006015 if (mddev_is_clustered(mddev))
6016 md_cluster_ops->metadata_update_start(mddev);
6017
NeilBrown3ea8929d2013-04-24 11:42:41 +10006018 clear_bit(Blocked, &rdev->flags);
6019 remove_and_add_spares(mddev, rdev);
6020
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 if (rdev->raid_disk >= 0)
6022 goto busy;
6023
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006024 if (mddev_is_clustered(mddev))
6025 md_cluster_ops->remove_disk(mddev, rdev);
6026
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006027 md_kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07006028 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006029 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006031 if (mddev_is_clustered(mddev))
6032 md_cluster_ops->metadata_update_finish(mddev);
6033
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 return 0;
6035busy:
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006036 if (mddev_is_clustered(mddev))
6037 md_cluster_ops->metadata_update_cancel(mddev);
Nick Andrewfdefa4d2008-04-21 22:42:58 +00006038 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 bdevname(rdev->bdev,b), mdname(mddev));
6040 return -EBUSY;
6041}
6042
NeilBrownf72ffdd2014-09-30 14:23:59 +10006043static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044{
6045 char b[BDEVNAME_SIZE];
6046 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006047 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
6049 if (!mddev->pers)
6050 return -ENODEV;
6051
6052 if (mddev->major_version != 0) {
6053 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
6054 " version-0 superblocks.\n",
6055 mdname(mddev));
6056 return -EINVAL;
6057 }
6058 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006059 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 "%s: personality does not support diskops!\n",
6061 mdname(mddev));
6062 return -EINVAL;
6063 }
6064
NeilBrownd710e132008-10-13 11:55:12 +11006065 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006067 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 "md: error, md_import_device() returned %ld\n",
6069 PTR_ERR(rdev));
6070 return -EINVAL;
6071 }
6072
6073 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006074 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006076 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
NeilBrown8190e752009-06-18 08:48:58 +10006078 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
NeilBrownb2d444d2005-11-08 21:39:31 -08006080 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006081 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 "md: can not hot-add faulty %s disk to %s!\n",
6083 bdevname(rdev->bdev,b), mdname(mddev));
6084 err = -EINVAL;
6085 goto abort_export;
6086 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006087
6088 if (mddev_is_clustered(mddev))
6089 md_cluster_ops->metadata_update_start(mddev);
NeilBrownb2d444d2005-11-08 21:39:31 -08006090 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006092 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006093 err = bind_rdev_to_array(rdev, mddev);
6094 if (err)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006095 goto abort_clustered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
6097 /*
6098 * The rest should better be atomic, we can have disk failures
6099 * noticed in interrupt contexts ...
6100 */
6101
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 rdev->raid_disk = -1;
6103
NeilBrown850b2b422006-10-03 01:15:46 -07006104 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006106 if (mddev_is_clustered(mddev))
6107 md_cluster_ops->metadata_update_finish(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 /*
6109 * Kick recovery, maybe this spare has to be added to the
6110 * array immediately.
6111 */
6112 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6113 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006114 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 return 0;
6116
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006117abort_clustered:
6118 if (mddev_is_clustered(mddev))
6119 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120abort_export:
6121 export_rdev(rdev);
6122 return err;
6123}
6124
NeilBrownfd01b882011-10-11 16:47:53 +11006125static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006126{
NeilBrown035328c2014-04-09 12:25:40 +10006127 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006128
NeilBrown36fa3062005-09-09 16:23:45 -07006129 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006130 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006131 return -EBUSY;
6132 if (mddev->recovery || mddev->sync_thread)
6133 return -EBUSY;
6134 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006135 }
6136
NeilBrown36fa3062005-09-09 16:23:45 -07006137 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006138 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006139 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006140
NeilBrown1e594bb2014-12-15 12:57:00 +11006141 if (mddev->bitmap || mddev->bitmap_info.file)
6142 return -EEXIST; /* cannot add when bitmap is present */
6143 f = fget(fd);
6144
6145 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006146 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6147 mdname(mddev));
6148 return -EBADF;
6149 }
6150
NeilBrown1e594bb2014-12-15 12:57:00 +11006151 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006152 if (!S_ISREG(inode->i_mode)) {
6153 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6154 mdname(mddev));
6155 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006156 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006157 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6158 mdname(mddev));
6159 err = -EBADF;
6160 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006161 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6162 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006163 err = -EBUSY;
6164 }
6165 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006166 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006167 return err;
6168 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006169 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006170 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006171 } else if (mddev->bitmap == NULL)
6172 return -ENOENT; /* cannot remove what isn't there */
6173 err = 0;
6174 if (mddev->pers) {
6175 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006176 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006177 struct bitmap *bitmap;
6178
6179 bitmap = bitmap_create(mddev, -1);
6180 if (!IS_ERR(bitmap)) {
6181 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006182 err = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006183 } else
6184 err = PTR_ERR(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10006185 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006186 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006187 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006188 fd = -1; /* make sure to put the file */
6189 }
NeilBrown36fa3062005-09-09 16:23:45 -07006190 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006191 }
6192 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006193 struct file *f = mddev->bitmap_info.file;
6194 if (f) {
6195 spin_lock(&mddev->lock);
6196 mddev->bitmap_info.file = NULL;
6197 spin_unlock(&mddev->lock);
6198 fput(f);
6199 }
NeilBrown36fa3062005-09-09 16:23:45 -07006200 }
6201
NeilBrown32a76272005-06-21 17:17:14 -07006202 return err;
6203}
6204
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205/*
6206 * set_array_info is used two different ways
6207 * The original usage is when creating a new array.
6208 * In this usage, raid_disks is > 0 and it together with
6209 * level, size, not_persistent,layout,chunksize determine the
6210 * shape of the array.
6211 * This will always create an array with a type-0.90.0 superblock.
6212 * The newer usage is when assembling an array.
6213 * In this case raid_disks will be 0, and the major_version field is
6214 * use to determine which style super-blocks are to be found on the devices.
6215 * The minor and patch _version numbers are also kept incase the
6216 * super_block handler wishes to interpret them.
6217 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006218static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219{
6220
6221 if (info->raid_disks == 0) {
6222 /* just setting version number for superblock loading */
6223 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006224 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 super_types[info->major_version].name == NULL) {
6226 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006227 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 "md: superblock version %d not known\n",
6229 info->major_version);
6230 return -EINVAL;
6231 }
6232 mddev->major_version = info->major_version;
6233 mddev->minor_version = info->minor_version;
6234 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006235 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006236 /* ensure mddev_put doesn't delete this now that there
6237 * is some minimal configuration.
6238 */
6239 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 return 0;
6241 }
6242 mddev->major_version = MD_MAJOR_VERSION;
6243 mddev->minor_version = MD_MINOR_VERSION;
6244 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6245 mddev->ctime = get_seconds();
6246
6247 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006248 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006249 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 mddev->raid_disks = info->raid_disks;
6251 /* don't set md_minor, it is determined by which /dev/md* was
6252 * openned
6253 */
6254 if (info->state & (1<<MD_SB_CLEAN))
6255 mddev->recovery_cp = MaxSector;
6256 else
6257 mddev->recovery_cp = 0;
6258 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006259 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
6261 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006262 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263
6264 mddev->max_disks = MD_SB_DISKS;
6265
NeilBrowne6910632008-02-06 01:39:51 -08006266 if (mddev->persistent)
6267 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006268 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
NeilBrownc3d97142009-12-14 12:49:52 +11006270 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006271 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006272 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006273
NeilBrownf6705572006-03-27 01:18:11 -08006274 mddev->reshape_position = MaxSector;
6275
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 /*
6277 * Generate a 128 bit UUID
6278 */
6279 get_random_bytes(mddev->uuid, 16);
6280
NeilBrownf6705572006-03-27 01:18:11 -08006281 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006282 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006283 mddev->new_layout = mddev->layout;
6284 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006285 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006286
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 return 0;
6288}
6289
NeilBrownfd01b882011-10-11 16:47:53 +11006290void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006291{
Dan Williamsb522adc2009-03-31 15:00:31 +11006292 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6293
6294 if (mddev->external_size)
6295 return;
6296
Dan Williams1f403622009-03-31 14:59:03 +11006297 mddev->array_sectors = array_sectors;
6298}
6299EXPORT_SYMBOL(md_set_array_sectors);
6300
NeilBrownfd01b882011-10-11 16:47:53 +11006301static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006302{
NeilBrown3cb03002011-10-11 16:45:26 +11006303 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006304 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006305 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006306
6307 if (mddev->pers->resize == NULL)
6308 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006309 /* The "num_sectors" is the number of sectors of each device that
6310 * is used. This can only make sense for arrays with redundancy.
6311 * linear and raid0 always use whatever space is available. We can only
6312 * consider changing this number if no resync or reconstruction is
6313 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006314 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006315 * of each device. If num_sectors is zero, we find the largest size
6316 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006317 */
NeilBrownf851b602014-12-11 10:02:10 +11006318 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6319 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006320 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006321 if (mddev->ro)
6322 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006323
NeilBrowndafb20f2012-03-19 12:46:39 +11006324 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006325 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006326
Andre Nolld71f9f82008-07-11 22:02:22 +10006327 if (fit && (num_sectors == 0 || num_sectors > avail))
6328 num_sectors = avail;
6329 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006330 return -ENOSPC;
6331 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006332 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006333 if (!rv)
6334 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006335 return rv;
6336}
6337
NeilBrownfd01b882011-10-11 16:47:53 +11006338static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006339{
6340 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006341 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006342 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006343 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006344 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006345 if (mddev->ro)
6346 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006347 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006348 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006349 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006350 if (mddev->sync_thread ||
6351 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6352 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006353 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006354
6355 rdev_for_each(rdev, mddev) {
6356 if (mddev->raid_disks < raid_disks &&
6357 rdev->data_offset < rdev->new_data_offset)
6358 return -EINVAL;
6359 if (mddev->raid_disks > raid_disks &&
6360 rdev->data_offset > rdev->new_data_offset)
6361 return -EINVAL;
6362 }
6363
NeilBrown63c70c42006-03-27 01:18:13 -08006364 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006365 if (mddev->delta_disks < 0)
6366 mddev->reshape_backwards = 1;
6367 else if (mddev->delta_disks > 0)
6368 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006369
6370 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006371 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006372 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006373 mddev->reshape_backwards = 0;
6374 }
NeilBrownda943b992006-01-06 00:20:54 -08006375 return rv;
6376}
6377
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378/*
6379 * update_array_info is used to change the configuration of an
6380 * on-line array.
6381 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6382 * fields in the info are checked against the array.
6383 * Any differences that cannot be handled will cause an error.
6384 * Normally, only one change can be managed at a time.
6385 */
NeilBrownfd01b882011-10-11 16:47:53 +11006386static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387{
6388 int rv = 0;
6389 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006390 int state = 0;
6391
6392 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006393 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006394 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
6396 if (mddev->major_version != info->major_version ||
6397 mddev->minor_version != info->minor_version ||
6398/* mddev->patch_version != info->patch_version || */
6399 mddev->ctime != info->ctime ||
6400 mddev->level != info->level ||
6401/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08006402 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10006403 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006404 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6405 ((state^info->state) & 0xfffffe00)
6406 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 return -EINVAL;
6408 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006409 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6410 cnt++;
6411 if (mddev->raid_disks != info->raid_disks)
6412 cnt++;
6413 if (mddev->layout != info->layout)
6414 cnt++;
6415 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6416 cnt++;
6417 if (cnt == 0)
6418 return 0;
6419 if (cnt > 1)
6420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
6422 if (mddev->layout != info->layout) {
6423 /* Change layout
6424 * we don't need to do anything at the md level, the
6425 * personality will take care of it all.
6426 */
NeilBrown50ac1682009-06-18 08:47:55 +10006427 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006429 else {
6430 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006431 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006432 if (rv)
6433 mddev->new_layout = mddev->layout;
6434 return rv;
6435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006437 if (mddev_is_clustered(mddev))
6438 md_cluster_ops->metadata_update_start(mddev);
Andre Noll58c0fed2009-03-31 14:33:13 +11006439 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006440 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441
NeilBrownda943b992006-01-06 00:20:54 -08006442 if (mddev->raid_disks != info->raid_disks)
6443 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444
NeilBrown36fa3062005-09-09 16:23:45 -07006445 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006446 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
6447 rv = -EINVAL;
6448 goto err;
6449 }
6450 if (mddev->recovery || mddev->sync_thread) {
6451 rv = -EBUSY;
6452 goto err;
6453 }
NeilBrown36fa3062005-09-09 16:23:45 -07006454 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006455 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07006456 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006457 if (mddev->bitmap) {
6458 rv = -EEXIST;
6459 goto err;
6460 }
6461 if (mddev->bitmap_info.default_offset == 0) {
6462 rv = -EINVAL;
6463 goto err;
6464 }
NeilBrownc3d97142009-12-14 12:49:52 +11006465 mddev->bitmap_info.offset =
6466 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006467 mddev->bitmap_info.space =
6468 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006469 mddev->pers->quiesce(mddev, 1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006470 bitmap = bitmap_create(mddev, -1);
6471 if (!IS_ERR(bitmap)) {
6472 mddev->bitmap = bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10006473 rv = bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006474 } else
6475 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07006476 if (rv)
6477 bitmap_destroy(mddev);
6478 mddev->pers->quiesce(mddev, 0);
6479 } else {
6480 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006481 if (!mddev->bitmap) {
6482 rv = -ENOENT;
6483 goto err;
6484 }
6485 if (mddev->bitmap->storage.file) {
6486 rv = -EINVAL;
6487 goto err;
6488 }
NeilBrown36fa3062005-09-09 16:23:45 -07006489 mddev->pers->quiesce(mddev, 1);
6490 bitmap_destroy(mddev);
6491 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006492 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006493 }
6494 }
NeilBrown850b2b422006-10-03 01:15:46 -07006495 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006496 if (mddev_is_clustered(mddev))
6497 md_cluster_ops->metadata_update_finish(mddev);
6498 return rv;
6499err:
6500 if (mddev_is_clustered(mddev))
6501 md_cluster_ops->metadata_update_cancel(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 return rv;
6503}
6504
NeilBrownfd01b882011-10-11 16:47:53 +11006505static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506{
NeilBrown3cb03002011-10-11 16:45:26 +11006507 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006508 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509
6510 if (mddev->pers == NULL)
6511 return -ENODEV;
6512
NeilBrown1ca69c42012-10-11 13:37:33 +11006513 rcu_read_lock();
6514 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006516 err = -ENODEV;
6517 else {
6518 md_error(mddev, rdev);
6519 if (!test_bit(Faulty, &rdev->flags))
6520 err = -EBUSY;
6521 }
6522 rcu_read_unlock();
6523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524}
6525
Andre Noll2f9618c2008-04-25 18:57:58 +02006526/*
6527 * We have a problem here : there is no easy way to give a CHS
6528 * virtual geometry. We currently pretend that we have a 2 heads
6529 * 4 sectors (with a BIG number of cylinders...). This drives
6530 * dosfs just mad... ;-)
6531 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006532static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6533{
NeilBrownfd01b882011-10-11 16:47:53 +11006534 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006535
6536 geo->heads = 2;
6537 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006538 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006539 return 0;
6540}
6541
Nicolas Schichancb335f82014-01-15 16:58:52 +01006542static inline bool md_ioctl_valid(unsigned int cmd)
6543{
6544 switch (cmd) {
6545 case ADD_NEW_DISK:
6546 case BLKROSET:
6547 case GET_ARRAY_INFO:
6548 case GET_BITMAP_FILE:
6549 case GET_DISK_INFO:
6550 case HOT_ADD_DISK:
6551 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006552 case RAID_AUTORUN:
6553 case RAID_VERSION:
6554 case RESTART_ARRAY_RW:
6555 case RUN_ARRAY:
6556 case SET_ARRAY_INFO:
6557 case SET_BITMAP_FILE:
6558 case SET_DISK_FAULTY:
6559 case STOP_ARRAY:
6560 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006561 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006562 return true;
6563 default:
6564 return false;
6565 }
6566}
6567
Al Viroa39907f2008-03-02 10:31:15 -05006568static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 unsigned int cmd, unsigned long arg)
6570{
6571 int err = 0;
6572 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006573 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006574 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575
Nicolas Schichancb335f82014-01-15 16:58:52 +01006576 if (!md_ioctl_valid(cmd))
6577 return -ENOTTY;
6578
NeilBrown506c9e42011-12-23 10:17:26 +11006579 switch (cmd) {
6580 case RAID_VERSION:
6581 case GET_ARRAY_INFO:
6582 case GET_DISK_INFO:
6583 break;
6584 default:
6585 if (!capable(CAP_SYS_ADMIN))
6586 return -EACCES;
6587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588
6589 /*
6590 * Commands dealing with the RAID driver but not any
6591 * particular array:
6592 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006593 switch (cmd) {
6594 case RAID_VERSION:
6595 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006596 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006599 case RAID_AUTORUN:
6600 err = 0;
6601 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006604 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 }
6606
6607 /*
6608 * Commands creating/starting a new array:
6609 */
6610
Al Viroa39907f2008-03-02 10:31:15 -05006611 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
6613 if (!mddev) {
6614 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 }
6617
NeilBrown1ca69c42012-10-11 13:37:33 +11006618 /* Some actions do not requires the mutex */
6619 switch (cmd) {
6620 case GET_ARRAY_INFO:
6621 if (!mddev->raid_disks && !mddev->external)
6622 err = -ENODEV;
6623 else
6624 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006625 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006626
6627 case GET_DISK_INFO:
6628 if (!mddev->raid_disks && !mddev->external)
6629 err = -ENODEV;
6630 else
6631 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006632 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006633
6634 case SET_DISK_FAULTY:
6635 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006636 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006637
6638 case GET_BITMAP_FILE:
6639 err = get_bitmap_file(mddev, argp);
6640 goto out;
6641
NeilBrown1ca69c42012-10-11 13:37:33 +11006642 }
6643
NeilBrowna7a3f082012-12-11 13:35:54 +11006644 if (cmd == ADD_NEW_DISK)
6645 /* need to ensure md_delayed_delete() has completed */
6646 flush_workqueue(md_misc_wq);
6647
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006648 if (cmd == HOT_REMOVE_DISK)
6649 /* need to ensure recovery thread has run */
6650 wait_event_interruptible_timeout(mddev->sb_wait,
6651 !test_bit(MD_RECOVERY_NEEDED,
6652 &mddev->flags),
6653 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006654 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6655 /* Need to flush page cache, and ensure no-one else opens
6656 * and writes
6657 */
6658 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006659 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006660 mutex_unlock(&mddev->open_mutex);
6661 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006662 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006663 }
6664 set_bit(MD_STILL_CLOSED, &mddev->flags);
6665 mutex_unlock(&mddev->open_mutex);
6666 sync_blockdev(bdev);
6667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 err = mddev_lock(mddev);
6669 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006670 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6672 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006673 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 }
6675
NeilBrownc02c0ae2012-12-11 13:39:21 +11006676 if (cmd == SET_ARRAY_INFO) {
6677 mdu_array_info_t info;
6678 if (!arg)
6679 memset(&info, 0, sizeof(info));
6680 else if (copy_from_user(&info, argp, sizeof(info))) {
6681 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006682 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006683 }
6684 if (mddev->pers) {
6685 err = update_array_info(mddev, &info);
6686 if (err) {
6687 printk(KERN_WARNING "md: couldn't update"
6688 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006689 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006691 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006692 }
6693 if (!list_empty(&mddev->disks)) {
6694 printk(KERN_WARNING
6695 "md: array %s already has disks!\n",
6696 mdname(mddev));
6697 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006698 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006699 }
6700 if (mddev->raid_disks) {
6701 printk(KERN_WARNING
6702 "md: array %s already initialised!\n",
6703 mdname(mddev));
6704 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006705 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006706 }
6707 err = set_array_info(mddev, &info);
6708 if (err) {
6709 printk(KERN_WARNING "md: couldn't set"
6710 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006711 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006712 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006713 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 }
6715
6716 /*
6717 * Commands querying/configuring an existing array:
6718 */
NeilBrown32a76272005-06-21 17:17:14 -07006719 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006720 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006721 if ((!mddev->raid_disks && !mddev->external)
6722 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6723 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6724 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006726 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 }
6728
6729 /*
6730 * Commands even a read-only array can execute:
6731 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006732 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006733 case RESTART_ARRAY_RW:
6734 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006735 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006736
6737 case STOP_ARRAY:
6738 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006739 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006740
6741 case STOP_ARRAY_RO:
6742 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006743 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006744
NeilBrown3ea8929d2013-04-24 11:42:41 +10006745 case HOT_REMOVE_DISK:
6746 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006747 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006748
NeilBrown7ceb17e2013-04-24 11:42:42 +10006749 case ADD_NEW_DISK:
6750 /* We can support ADD_NEW_DISK on read-only arrays
6751 * on if we are re-adding a preexisting device.
6752 * So require mddev->pers and MD_DISK_SYNC.
6753 */
6754 if (mddev->pers) {
6755 mdu_disk_info_t info;
6756 if (copy_from_user(&info, argp, sizeof(info)))
6757 err = -EFAULT;
6758 else if (!(info.state & (1<<MD_DISK_SYNC)))
6759 /* Need to clear read-only for this */
6760 break;
6761 else
6762 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006763 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006764 }
6765 break;
6766
NeilBrownc02c0ae2012-12-11 13:39:21 +11006767 case BLKROSET:
6768 if (get_user(ro, (int __user *)(arg))) {
6769 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006770 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006771 }
6772 err = -EINVAL;
6773
6774 /* if the bdev is going readonly the value of mddev->ro
6775 * does not matter, no writes are coming
6776 */
6777 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006778 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006779
NeilBrownc02c0ae2012-12-11 13:39:21 +11006780 /* are we are already prepared for writes? */
6781 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006782 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006783
6784 /* transitioning to readauto need only happen for
6785 * arrays that call md_write_start
6786 */
6787 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006789 if (err == 0) {
6790 mddev->ro = 2;
6791 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006792 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006793 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006794 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 }
6796
6797 /*
6798 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006799 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 */
NeilBrown326eb172014-09-30 15:36:28 +10006801 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006802 if (mddev->ro == 2) {
6803 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006804 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006805 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006806 /* mddev_unlock will wake thread */
6807 /* If a device failed while we were read-only, we
6808 * need to make sure the metadata is updated now.
6809 */
6810 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6811 mddev_unlock(mddev);
6812 wait_event(mddev->sb_wait,
6813 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6814 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006815 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006816 }
NeilBrownf91de922005-11-08 21:39:36 -08006817 } else {
6818 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006819 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 }
6822
NeilBrownc02c0ae2012-12-11 13:39:21 +11006823 switch (cmd) {
6824 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006826 mdu_disk_info_t info;
6827 if (copy_from_user(&info, argp, sizeof(info)))
6828 err = -EFAULT;
6829 else
6830 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006831 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006834 case CLUSTERED_DISK_NACK:
6835 if (mddev_is_clustered(mddev))
6836 md_cluster_ops->new_disk_ack(mddev, false);
6837 else
6838 err = -EINVAL;
6839 goto unlock;
6840
NeilBrownc02c0ae2012-12-11 13:39:21 +11006841 case HOT_ADD_DISK:
6842 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006843 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844
NeilBrownc02c0ae2012-12-11 13:39:21 +11006845 case RUN_ARRAY:
6846 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006847 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
NeilBrownc02c0ae2012-12-11 13:39:21 +11006849 case SET_BITMAP_FILE:
6850 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006851 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006852
NeilBrownc02c0ae2012-12-11 13:39:21 +11006853 default:
6854 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006855 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 }
6857
NeilBrown3adc28d2014-09-30 15:46:41 +10006858unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006859 if (mddev->hold_active == UNTIL_IOCTL &&
6860 err != -EINVAL)
6861 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006863out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 return err;
6865}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006866#ifdef CONFIG_COMPAT
6867static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6868 unsigned int cmd, unsigned long arg)
6869{
6870 switch (cmd) {
6871 case HOT_REMOVE_DISK:
6872 case HOT_ADD_DISK:
6873 case SET_DISK_FAULTY:
6874 case SET_BITMAP_FILE:
6875 /* These take in integer arg, do not convert */
6876 break;
6877 default:
6878 arg = (unsigned long)compat_ptr(arg);
6879 break;
6880 }
6881
6882 return md_ioctl(bdev, mode, cmd, arg);
6883}
6884#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
Al Viroa39907f2008-03-02 10:31:15 -05006886static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887{
6888 /*
6889 * Succeed if we can lock the mddev, which confirms that
6890 * it isn't being stopped right now.
6891 */
NeilBrownfd01b882011-10-11 16:47:53 +11006892 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 int err;
6894
Yuanhan Liu0c098222012-05-22 13:55:32 +10006895 if (!mddev)
6896 return -ENODEV;
6897
NeilBrownd3374822009-01-09 08:31:10 +11006898 if (mddev->gendisk != bdev->bd_disk) {
6899 /* we are racing with mddev_put which is discarding this
6900 * bd_disk.
6901 */
6902 mddev_put(mddev);
6903 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006904 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006905 /* Then retry the open from the top */
6906 return -ERESTARTSYS;
6907 }
6908 BUG_ON(mddev != bdev->bd_disk->private_data);
6909
NeilBrownc8c00a62009-08-10 12:50:52 +10006910 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 goto out;
6912
6913 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006914 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006915 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006916 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006918 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 out:
6920 return err;
6921}
6922
Al Virodb2a1442013-05-05 21:52:57 -04006923static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006925 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006927 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006928 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006931
6932static int md_media_changed(struct gendisk *disk)
6933{
NeilBrownfd01b882011-10-11 16:47:53 +11006934 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006935
6936 return mddev->changed;
6937}
6938
6939static int md_revalidate(struct gendisk *disk)
6940{
NeilBrownfd01b882011-10-11 16:47:53 +11006941 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006942
6943 mddev->changed = 0;
6944 return 0;
6945}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006946static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947{
6948 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006949 .open = md_open,
6950 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006951 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006952#ifdef CONFIG_COMPAT
6953 .compat_ioctl = md_compat_ioctl,
6954#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006955 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006956 .media_changed = md_media_changed,
6957 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958};
6959
NeilBrownf72ffdd2014-09-30 14:23:59 +10006960static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961{
NeilBrown2b8bf342011-10-11 16:48:23 +11006962 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 /*
6965 * md_thread is a 'system-thread', it's priority should be very
6966 * high. We avoid resource deadlocks individually in each
6967 * raid personality. (RAID5 does preallocation) We also use RR and
6968 * the very same RT priority as kswapd, thus we will never get
6969 * into a priority inversion deadlock.
6970 *
6971 * we definitely have to have equal or higher priority than
6972 * bdflush, otherwise bdflush will deadlock if there are too
6973 * many dirty RAID5 blocks.
6974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975
NeilBrown6985c432005-10-19 21:23:47 -07006976 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006977 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
NeilBrown93588e22005-11-15 00:09:12 -08006979 /* We need to wait INTERRUPTIBLE so that
6980 * we don't add to the load-average.
6981 * That means we need to be sure no signals are
6982 * pending
6983 */
6984 if (signal_pending(current))
6985 flush_signals(current);
6986
6987 wait_event_interruptible_timeout
6988 (thread->wqueue,
6989 test_bit(THREAD_WAKEUP, &thread->flags)
6990 || kthread_should_stop(),
6991 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
NeilBrown6c987912011-01-14 09:13:53 +11006993 clear_bit(THREAD_WAKEUP, &thread->flags);
6994 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006995 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006997
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 return 0;
6999}
7000
NeilBrown2b8bf342011-10-11 16:48:23 +11007001void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002{
7003 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007004 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 set_bit(THREAD_WAKEUP, &thread->flags);
7006 wake_up(&thread->wqueue);
7007 }
7008}
NeilBrown6c144d32014-09-30 16:15:38 +10007009EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010
Shaohua Li4ed87312012-10-11 13:34:00 +11007011struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7012 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013{
NeilBrown2b8bf342011-10-11 16:48:23 +11007014 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015
NeilBrown2b8bf342011-10-11 16:48:23 +11007016 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 if (!thread)
7018 return NULL;
7019
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 init_waitqueue_head(&thread->wqueue);
7021
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 thread->run = run;
7023 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007024 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007025 thread->tsk = kthread_run(md_thread, thread,
7026 "%s_%s",
7027 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007028 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007029 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 kfree(thread);
7031 return NULL;
7032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 return thread;
7034}
NeilBrown6c144d32014-09-30 16:15:38 +10007035EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
NeilBrown2b8bf342011-10-11 16:48:23 +11007037void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038{
NeilBrown2b8bf342011-10-11 16:48:23 +11007039 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007040 if (!thread)
7041 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007042 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007043 /* Locking ensures that mddev_unlock does not wake_up a
7044 * non-existent thread
7045 */
7046 spin_lock(&pers_lock);
7047 *threadp = NULL;
7048 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007049
7050 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 kfree(thread);
7052}
NeilBrown6c144d32014-09-30 16:15:38 +10007053EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054
NeilBrownfd01b882011-10-11 16:47:53 +11007055void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056{
NeilBrownb2d444d2005-11-08 21:39:31 -08007057 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007059
NeilBrownde393cd2011-07-28 11:31:48 +10007060 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 return;
7062 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007063 if (mddev->degraded)
7064 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007065 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7067 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7068 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007069 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007070 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07007071 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072}
NeilBrown6c144d32014-09-30 16:15:38 +10007073EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074
7075/* seq_file implementation /proc/mdstat */
7076
7077static void status_unused(struct seq_file *seq)
7078{
7079 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007080 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081
7082 seq_printf(seq, "unused devices: ");
7083
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007084 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 char b[BDEVNAME_SIZE];
7086 i++;
7087 seq_printf(seq, "%s ",
7088 bdevname(rdev->bdev,b));
7089 }
7090 if (!i)
7091 seq_printf(seq, "<none>");
7092
7093 seq_printf(seq, "\n");
7094}
7095
NeilBrownf72ffdd2014-09-30 14:23:59 +10007096static void status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097{
NeilBrowndd71cf62009-05-07 12:49:35 +10007098 sector_t max_sectors, resync, res;
7099 unsigned long dt, db;
7100 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08007101 int scale;
7102 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103
NeilBrown72f36d52012-10-11 14:25:57 +11007104 if (mddev->curr_resync <= 3)
7105 resync = 0;
7106 else
7107 resync = mddev->curr_resync
7108 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109
NeilBrownc804cde2012-05-21 09:28:33 +10007110 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7111 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007112 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007114 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
NeilBrown403df472014-09-30 15:52:29 +10007116 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007117 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007118 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007119 * u32, as those are the requirements for sector_div.
7120 * Thus 'scale' must be at least 10
7121 */
7122 scale = 10;
7123 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007124 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007125 scale++;
7126 }
7127 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007128 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007129
7130 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 {
NeilBrown4588b422006-03-27 01:18:04 -08007132 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 seq_printf(seq, "[");
7134 for (i = 0; i < x; i++)
7135 seq_printf(seq, "=");
7136 seq_printf(seq, ">");
7137 for (i = 0; i < y; i++)
7138 seq_printf(seq, ".");
7139 seq_printf(seq, "] ");
7140 }
NeilBrown4588b422006-03-27 01:18:04 -08007141 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007142 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7143 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007144 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7145 "check" :
7146 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7147 "resync" : "recovery"))),
7148 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007149 (unsigned long long) resync/2,
7150 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
7152 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 * dt: time from mark until now
7154 * db: blocks written from mark until now
7155 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007156 *
7157 * rt is a sector_t, so could be 32bit or 64bit.
7158 * So we divide before multiply in case it is 32bit and close
7159 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007160 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10007161 * near the end of resync when the number of remaining sectors
7162 * is close to 'db'.
7163 * We then divide rt by 32 after multiplying by db to compensate.
7164 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 */
7166 dt = ((jiffies - mddev->resync_mark) / HZ);
7167 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07007168 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
7169 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
NeilBrowndd71cf62009-05-07 12:49:35 +10007171 rt = max_sectors - resync; /* number of remaining sectors */
7172 sector_div(rt, db/32+1);
7173 rt *= dt;
7174 rt >>= 5;
7175
7176 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7177 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178
NeilBrownff4e8d92006-07-10 04:44:16 -07007179 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180}
7181
7182static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7183{
7184 struct list_head *tmp;
7185 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007186 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
7188 if (l >= 0x10000)
7189 return NULL;
7190 if (!l--)
7191 /* header */
7192 return (void*)1;
7193
7194 spin_lock(&all_mddevs_lock);
7195 list_for_each(tmp,&all_mddevs)
7196 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007197 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 mddev_get(mddev);
7199 spin_unlock(&all_mddevs_lock);
7200 return mddev;
7201 }
7202 spin_unlock(&all_mddevs_lock);
7203 if (!l--)
7204 return (void*)2;/* tail */
7205 return NULL;
7206}
7207
7208static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7209{
7210 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007211 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007212
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 ++*pos;
7214 if (v == (void*)2)
7215 return NULL;
7216
7217 spin_lock(&all_mddevs_lock);
7218 if (v == (void*)1)
7219 tmp = all_mddevs.next;
7220 else
7221 tmp = mddev->all_mddevs.next;
7222 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007223 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 else {
7225 next_mddev = (void*)2;
7226 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 spin_unlock(&all_mddevs_lock);
7229
7230 if (v != (void*)1)
7231 mddev_put(mddev);
7232 return next_mddev;
7233
7234}
7235
7236static void md_seq_stop(struct seq_file *seq, void *v)
7237{
NeilBrownfd01b882011-10-11 16:47:53 +11007238 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
7240 if (mddev && v != (void*)1 && v != (void*)2)
7241 mddev_put(mddev);
7242}
7243
7244static int md_seq_show(struct seq_file *seq, void *v)
7245{
NeilBrownfd01b882011-10-11 16:47:53 +11007246 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007247 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007248 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
7250 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007251 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 seq_printf(seq, "Personalities : ");
7253 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007254 list_for_each_entry(pers, &pers_list, list)
7255 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256
7257 spin_unlock(&pers_lock);
7258 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007259 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 return 0;
7261 }
7262 if (v == (void*)2) {
7263 status_unused(seq);
7264 return 0;
7265 }
7266
NeilBrown36d091f2014-12-15 12:56:58 +11007267 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7269 seq_printf(seq, "%s : %sactive", mdname(mddev),
7270 mddev->pers ? "" : "in");
7271 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007272 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007274 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007275 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 seq_printf(seq, " %s", mddev->pers->name);
7277 }
7278
Andre Nolldd8ac332009-03-31 14:33:13 +11007279 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007280 rcu_read_lock();
7281 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 char b[BDEVNAME_SIZE];
7283 seq_printf(seq, " %s[%d]",
7284 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007285 if (test_bit(WriteMostly, &rdev->flags))
7286 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007287 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 seq_printf(seq, "(F)");
7289 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007290 }
7291 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007292 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007293 if (test_bit(Replacement, &rdev->flags))
7294 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007295 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007297 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298
7299 if (!list_empty(&mddev->disks)) {
7300 if (mddev->pers)
7301 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007302 (unsigned long long)
7303 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 else
7305 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007306 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007308 if (mddev->persistent) {
7309 if (mddev->major_version != 0 ||
7310 mddev->minor_version != 90) {
7311 seq_printf(seq," super %d.%d",
7312 mddev->major_version,
7313 mddev->minor_version);
7314 }
NeilBrowne6910632008-02-06 01:39:51 -08007315 } else if (mddev->external)
7316 seq_printf(seq, " super external:%s",
7317 mddev->metadata_type);
7318 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007319 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320
7321 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007322 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007323 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007324 if (mddev->pers->sync_request) {
7325 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007326 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007327 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007328 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007329 seq_printf(seq, "\tresync=DELAYED\n ");
7330 else if (mddev->recovery_cp < MaxSector)
7331 seq_printf(seq, "\tresync=PENDING\n ");
7332 }
NeilBrown32a76272005-06-21 17:17:14 -07007333 } else
7334 seq_printf(seq, "\n ");
7335
NeilBrown57148962012-03-19 12:46:40 +11007336 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337
7338 seq_printf(seq, "\n");
7339 }
NeilBrown36d091f2014-12-15 12:56:58 +11007340 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007341
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 return 0;
7343}
7344
Jan Engelhardt110518b2009-05-07 12:49:37 +10007345static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 .start = md_seq_start,
7347 .next = md_seq_next,
7348 .stop = md_seq_stop,
7349 .show = md_seq_show,
7350};
7351
7352static int md_seq_open(struct inode *inode, struct file *file)
7353{
Kay Sieversf1514632011-07-12 20:48:39 +02007354 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 int error;
7356
7357 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007358 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007359 return error;
7360
7361 seq = file->private_data;
7362 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363 return error;
7364}
7365
NeilBrowne2f23b62014-04-09 14:33:51 +10007366static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007367static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7368{
Kay Sieversf1514632011-07-12 20:48:39 +02007369 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007370 int mask;
7371
NeilBrowne2f23b62014-04-09 14:33:51 +10007372 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007373 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007374 poll_wait(filp, &md_event_waiters, wait);
7375
7376 /* always allow read */
7377 mask = POLLIN | POLLRDNORM;
7378
Kay Sieversf1514632011-07-12 20:48:39 +02007379 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007380 mask |= POLLERR | POLLPRI;
7381 return mask;
7382}
7383
Arjan van de Venfa027c22007-02-12 00:55:33 -08007384static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007385 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 .open = md_seq_open,
7387 .read = seq_read,
7388 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007389 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007390 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391};
7392
NeilBrown84fc4b52011-10-11 16:49:58 +11007393int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394{
Chao Yu50bd3772014-09-25 15:28:34 +08007395 printk(KERN_INFO "md: %s personality registered for level %d\n",
7396 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007398 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 spin_unlock(&pers_lock);
7400 return 0;
7401}
NeilBrown6c144d32014-09-30 16:15:38 +10007402EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403
NeilBrown84fc4b52011-10-11 16:49:58 +11007404int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405{
NeilBrown2604b702006-01-06 00:20:36 -08007406 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007408 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 spin_unlock(&pers_lock);
7410 return 0;
7411}
NeilBrown6c144d32014-09-30 16:15:38 +10007412EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007414int register_md_cluster_operations(struct md_cluster_operations *ops, struct module *module)
7415{
7416 if (md_cluster_ops != NULL)
7417 return -EALREADY;
7418 spin_lock(&pers_lock);
7419 md_cluster_ops = ops;
7420 md_cluster_mod = module;
7421 spin_unlock(&pers_lock);
7422 return 0;
7423}
7424EXPORT_SYMBOL(register_md_cluster_operations);
7425
7426int unregister_md_cluster_operations(void)
7427{
7428 spin_lock(&pers_lock);
7429 md_cluster_ops = NULL;
7430 spin_unlock(&pers_lock);
7431 return 0;
7432}
7433EXPORT_SYMBOL(unregister_md_cluster_operations);
7434
7435int md_setup_cluster(struct mddev *mddev, int nodes)
7436{
7437 int err;
7438
7439 err = request_module("md-cluster");
7440 if (err) {
7441 pr_err("md-cluster module not found.\n");
Goldwyn Rodriguesb0c26a72015-07-22 12:09:15 -05007442 return -ENOENT;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007443 }
7444
7445 spin_lock(&pers_lock);
7446 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7447 spin_unlock(&pers_lock);
7448 return -ENOENT;
7449 }
7450 spin_unlock(&pers_lock);
7451
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05007452 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007453}
7454
7455void md_cluster_stop(struct mddev *mddev)
7456{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007457 if (!md_cluster_ops)
7458 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007459 md_cluster_ops->leave(mddev);
7460 module_put(md_cluster_mod);
7461}
7462
NeilBrownfd01b882011-10-11 16:47:53 +11007463static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007465 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007467 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468
7469 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007470 rcu_read_lock();
7471 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007473 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7474 (int)part_stat_read(&disk->part0, sectors[1]) -
7475 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007476 /* sync IO will cause sync_io to increase before the disk_stats
7477 * as sync_io is counted when a request starts, and
7478 * disk_stats is counted when it completes.
7479 * So resync activity will cause curr_events to be smaller than
7480 * when there was no such activity.
7481 * non-sync IO will cause disk_stat to increase without
7482 * increasing sync_io so curr_events will (eventually)
7483 * be larger than it was before. Once it becomes
7484 * substantially larger, the test below will cause
7485 * the array to appear non-idle, and resync will slow
7486 * down.
7487 * If there is a lot of outstanding resync activity when
7488 * we set last_event to curr_events, then all that activity
7489 * completing might cause the array to appear non-idle
7490 * and resync will be slowed down even though there might
7491 * not have been non-resync activity. This will only
7492 * happen once though. 'last_events' will soon reflect
7493 * the state where there is little or no outstanding
7494 * resync requests, and further resync activity will
7495 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007496 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007498 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 rdev->last_events = curr_events;
7500 idle = 0;
7501 }
7502 }
NeilBrown4b809912008-07-21 17:05:25 +10007503 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 return idle;
7505}
7506
NeilBrownfd01b882011-10-11 16:47:53 +11007507void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508{
7509 /* another "blocks" (512byte) blocks have been synced */
7510 atomic_sub(blocks, &mddev->recovery_active);
7511 wake_up(&mddev->recovery_wait);
7512 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007513 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007514 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 md_wakeup_thread(mddev->thread);
7516 // stop recovery, signal do_sync ....
7517 }
7518}
NeilBrown6c144d32014-09-30 16:15:38 +10007519EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520
NeilBrown06d91a52005-06-21 17:17:12 -07007521/* md_write_start(mddev, bi)
7522 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007523 * in superblock) before writing, schedule a superblock update
7524 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007525 */
NeilBrownfd01b882011-10-11 16:47:53 +11007526void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527{
Neil Brown0fd62b82008-06-28 08:31:36 +10007528 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007529 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007530 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007531
NeilBrownf91de922005-11-08 21:39:36 -08007532 BUG_ON(mddev->ro == 1);
7533 if (mddev->ro == 2) {
7534 /* need to switch to read/write */
7535 mddev->ro = 0;
7536 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7537 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007538 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007539 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007540 }
NeilBrown06d91a52005-06-21 17:17:12 -07007541 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007542 if (mddev->safemode == 1)
7543 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007544 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007545 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007546 if (mddev->in_sync) {
7547 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007548 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007549 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007550 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007551 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007552 }
NeilBrown85572d72014-12-15 12:56:56 +11007553 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007554 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007555 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007556 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007557 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007558 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559}
NeilBrown6c144d32014-09-30 16:15:38 +10007560EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561
NeilBrownfd01b882011-10-11 16:47:53 +11007562void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563{
7564 if (atomic_dec_and_test(&mddev->writes_pending)) {
7565 if (mddev->safemode == 2)
7566 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007567 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7569 }
7570}
NeilBrown6c144d32014-09-30 16:15:38 +10007571EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572
NeilBrown2a2275d2007-01-26 00:57:11 -08007573/* md_allow_write(mddev)
7574 * Calling this ensures that the array is marked 'active' so that writes
7575 * may proceed without blocking. It is important to call this before
7576 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7577 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007578 *
7579 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7580 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007581 */
NeilBrownfd01b882011-10-11 16:47:53 +11007582int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007583{
7584 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007585 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007586 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007587 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007588 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007589 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007590
NeilBrown85572d72014-12-15 12:56:56 +11007591 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007592 if (mddev->in_sync) {
7593 mddev->in_sync = 0;
7594 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007595 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007596 if (mddev->safemode_delay &&
7597 mddev->safemode == 0)
7598 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007599 spin_unlock(&mddev->lock);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007600 if (mddev_is_clustered(mddev))
7601 md_cluster_ops->metadata_update_start(mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08007602 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007603 if (mddev_is_clustered(mddev))
7604 md_cluster_ops->metadata_update_finish(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007605 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007606 } else
NeilBrown85572d72014-12-15 12:56:56 +11007607 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007608
NeilBrown070dc6d2010-08-30 17:33:34 +10007609 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007610 return -EAGAIN;
7611 else
7612 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007613}
7614EXPORT_SYMBOL_GPL(md_allow_write);
7615
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616#define SYNC_MARKS 10
7617#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007618#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007619void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620{
Shaohua Li4ed87312012-10-11 13:34:00 +11007621 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007622 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 unsigned int currspeed = 0,
7624 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007625 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007627 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 sector_t mark_cnt[SYNC_MARKS];
7629 int last_mark,m;
7630 struct list_head *tmp;
7631 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007632 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007633 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007634 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007635 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636
7637 /* just incase thread restarts... */
7638 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7639 return;
NeilBrown3991b312014-05-28 13:39:23 +10007640 if (mddev->ro) {/* never try to sync a read-only array */
7641 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007642 return;
NeilBrown3991b312014-05-28 13:39:23 +10007643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644
NeilBrown61df9d92006-10-03 01:15:57 -07007645 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007646 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007647 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007648 action = "check";
7649 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007650 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007651 action = "repair";
7652 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007653 desc = "resync";
7654 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7655 desc = "reshape";
7656 else
7657 desc = "recovery";
7658
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007659 mddev->last_sync_action = action ?: desc;
7660
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 /* we overload curr_resync somewhat here.
7662 * 0 == not engaged in resync at all
7663 * 2 == checking that there is no conflict with another sync
7664 * 1 == like 2, but have yielded to allow conflicting resync to
7665 * commense
7666 * other == active in resync - this many blocks
7667 *
7668 * Before starting a resync we must have set curr_resync to
7669 * 2, and then checked that every "conflicting" array has curr_resync
7670 * less than ours. When we find one that is the same or higher
7671 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7672 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7673 * This will mean we have to start checking from the beginning again.
7674 *
7675 */
7676
7677 do {
7678 mddev->curr_resync = 2;
7679
7680 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007681 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007683 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684 if (mddev2 == mddev)
7685 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007686 if (!mddev->parallel_resync
7687 && mddev2->curr_resync
7688 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 DEFINE_WAIT(wq);
7690 if (mddev < mddev2 && mddev->curr_resync == 2) {
7691 /* arbitrarily yield */
7692 mddev->curr_resync = 1;
7693 wake_up(&resync_wait);
7694 }
7695 if (mddev > mddev2 && mddev->curr_resync == 1)
7696 /* no need to wait here, we can wait the next
7697 * time 'round when curr_resync == 2
7698 */
7699 continue;
NeilBrown97441972008-09-19 11:49:54 +10007700 /* We need to wait 'interruptible' so as not to
7701 * contribute to the load average, and not to
7702 * be caught by 'softlockup'
7703 */
7704 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007705 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007706 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007707 printk(KERN_INFO "md: delaying %s of %s"
7708 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007710 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007712 if (signal_pending(current))
7713 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714 schedule();
7715 finish_wait(&resync_wait, &wq);
7716 goto try_again;
7717 }
7718 finish_wait(&resync_wait, &wq);
7719 }
7720 }
7721 } while (mddev->curr_resync < 2);
7722
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007723 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007724 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007726 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 */
7728 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007729 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007730 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007731 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7732 j = mddev->resync_min;
7733 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007734 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007735
NeilBrownccfcc3c2006-03-27 01:18:09 -08007736 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007737 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007738 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007740 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007741 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007742 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007743 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007744 if (rdev->raid_disk >= 0 &&
7745 !test_bit(Faulty, &rdev->flags) &&
7746 !test_bit(In_sync, &rdev->flags) &&
7747 rdev->recovery_offset < j)
7748 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007749 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007750
7751 /* If there is a bitmap, we need to make sure all
7752 * writes that started before we added a spare
7753 * complete before we start doing a recovery.
7754 * Otherwise the write might complete and (via
7755 * bitmap_endwrite) set a bit in the bitmap after the
7756 * recovery has checked that bit and skipped that
7757 * region.
7758 */
7759 if (mddev->bitmap) {
7760 mddev->pers->quiesce(mddev, 1);
7761 mddev->pers->quiesce(mddev, 0);
7762 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764
NeilBrown61df9d92006-10-03 01:15:57 -07007765 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7766 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7767 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007768 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007769 "(but not more than %d KB/sec) for %s.\n",
7770 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771
NeilBrowneea1bf32009-03-31 14:27:02 +11007772 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007773
NeilBrown57afd892005-06-21 17:17:13 -07007774 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 for (m = 0; m < SYNC_MARKS; m++) {
7776 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007777 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007778 }
7779 last_mark = 0;
7780 mddev->resync_mark = mark[last_mark];
7781 mddev->resync_mark_cnt = mark_cnt[last_mark];
7782
7783 /*
7784 * Tune reconstruction:
7785 */
7786 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007787 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7788 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789
7790 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 last_check = 0;
7792
7793 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007794 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007795 "md: resuming %s of %s from checkpoint.\n",
7796 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007798 } else
7799 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007800 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007801 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7802 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007803 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007805 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007806 md_cluster_ops->resync_start(mddev, j, max_sectors);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007807
majianpeng7c2c57c2012-07-03 12:12:26 +10007808 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007810 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811
NeilBrown57afd892005-06-21 17:17:13 -07007812 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007813
NeilBrown7a91ee12009-05-26 12:57:21 +10007814 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7815 ((mddev->curr_resync > mddev->curr_resync_completed &&
7816 (mddev->curr_resync - mddev->curr_resync_completed)
7817 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007818 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007819 (j - mddev->curr_resync_completed)*2
7820 >= mddev->resync_max - mddev->curr_resync_completed
7821 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007822 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007823 wait_event(mddev->recovery_wait,
7824 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007825 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007826 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7827 j > mddev->recovery_cp)
7828 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007829 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007830 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007831 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007832 }
NeilBrownacb180b2009-04-14 16:28:34 +10007833
NeilBrownc91abf52013-11-19 12:02:01 +11007834 while (j >= mddev->resync_max &&
7835 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007836 /* As this condition is controlled by user-space,
7837 * we can block indefinitely, so use '_interruptible'
7838 * to avoid triggering warnings.
7839 */
7840 flush_signals(current); /* just in case */
7841 wait_event_interruptible(mddev->recovery_wait,
7842 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007843 || test_bit(MD_RECOVERY_INTR,
7844 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007845 }
NeilBrownacb180b2009-04-14 16:28:34 +10007846
NeilBrownc91abf52013-11-19 12:02:01 +11007847 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7848 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007849
NeilBrown09314792015-02-19 16:04:40 +11007850 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07007851 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007852 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007853 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 }
NeilBrown57afd892005-06-21 17:17:13 -07007855
7856 if (!skipped) { /* actual IO requested */
7857 io_sectors += sectors;
7858 atomic_add(sectors, &mddev->recovery_active);
7859 }
7860
NeilBrowne875ece2011-07-28 11:39:24 +10007861 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7862 break;
7863
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007865 if (j > 2)
7866 mddev->curr_resync = j;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007867 if (mddev_is_clustered(mddev))
7868 md_cluster_ops->resync_info_update(mddev, j, max_sectors);
NeilBrownff4e8d92006-07-10 04:44:16 -07007869 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007870 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007871 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007872 * visible in /proc/mdstat
7873 */
7874 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007875
7876 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 continue;
7878
NeilBrown57afd892005-06-21 17:17:13 -07007879 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880 repeat:
7881 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7882 /* step marks */
7883 int next = (last_mark+1) % SYNC_MARKS;
7884
7885 mddev->resync_mark = mark[next];
7886 mddev->resync_mark_cnt = mark_cnt[next];
7887 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007888 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 last_mark = next;
7890 }
7891
NeilBrownc91abf52013-11-19 12:02:01 +11007892 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
7895 /*
7896 * this loop exits only if either when we are slower than
7897 * the 'hard' speed limit, or the system was IO-idle for
7898 * a jiffy.
7899 * the system might be non-idle CPU-wise, but we only care
7900 * about not overloading the IO subsystem. (things like an
7901 * e2fsck being done on the RAID array should execute fast)
7902 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 cond_resched();
7904
Xiao Niac7e50a2014-08-07 09:37:41 -04007905 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7906 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007907 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908
NeilBrown88202a02006-01-06 00:21:36 -08007909 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11007910 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007911 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 goto repeat;
7913 }
NeilBrownac8fa412015-02-19 16:55:00 +11007914 if (!is_mddev_idle(mddev, 0)) {
7915 /*
7916 * Give other IO more of a chance.
7917 * The faster the devices, the less we wait.
7918 */
7919 wait_event(mddev->recovery_wait,
7920 !atomic_read(&mddev->recovery_active));
7921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 }
7923 }
NeilBrownc91abf52013-11-19 12:02:01 +11007924 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7925 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7926 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 /*
7928 * this also signals 'finished resyncing' to md_stop
7929 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007930 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7932
7933 /* tell personality that we are finished */
NeilBrown09314792015-02-19 16:04:40 +11007934 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007936 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -05007937 md_cluster_ops->resync_finish(mddev);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05007938
NeilBrowndfc70642008-05-23 13:04:39 -07007939 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007940 mddev->curr_resync > 2) {
7941 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7942 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7943 if (mddev->curr_resync >= mddev->recovery_cp) {
7944 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007945 "md: checkpointing %s of %s.\n",
7946 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007947 if (test_bit(MD_RECOVERY_ERROR,
7948 &mddev->recovery))
7949 mddev->recovery_cp =
7950 mddev->curr_resync_completed;
7951 else
7952 mddev->recovery_cp =
7953 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007954 }
7955 } else
7956 mddev->recovery_cp = MaxSector;
7957 } else {
7958 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7959 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007960 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007961 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007962 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007963 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007964 !test_bit(Faulty, &rdev->flags) &&
7965 !test_bit(In_sync, &rdev->flags) &&
7966 rdev->recovery_offset < mddev->curr_resync)
7967 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007968 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007971 skip:
NeilBrown17571282006-12-10 02:20:52 -08007972 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973
NeilBrown23da4222014-12-15 12:57:01 +11007974 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11007975 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7976 /* We completed so min/max setting can be forgotten if used. */
7977 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7978 mddev->resync_min = 0;
7979 mddev->resync_max = MaxSector;
7980 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7981 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007983 spin_unlock(&mddev->lock);
7984
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 wake_up(&resync_wait);
7986 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7987 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007988 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989}
NeilBrown29269552006-03-27 01:18:10 -08007990EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991
NeilBrown746d3202013-04-24 11:42:41 +10007992static int remove_and_add_spares(struct mddev *mddev,
7993 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007994{
NeilBrown3cb03002011-10-11 16:45:26 +11007995 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007996 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007997 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007998
NeilBrowndafb20f2012-03-19 12:46:39 +11007999 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10008000 if ((this == NULL || rdev == this) &&
8001 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008002 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008003 (test_bit(Faulty, &rdev->flags) ||
8004 ! test_bit(In_sync, &rdev->flags)) &&
8005 atomic_read(&rdev->nr_pending)==0) {
8006 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008007 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008008 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008009 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008010 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008011 }
8012 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008013 if (removed && mddev->kobj.sd)
8014 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008015
NeilBrown746d3202013-04-24 11:42:41 +10008016 if (this)
8017 goto no_add;
8018
NeilBrowndafb20f2012-03-19 12:46:39 +11008019 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11008020 if (rdev->raid_disk >= 0 &&
8021 !test_bit(In_sync, &rdev->flags) &&
8022 !test_bit(Faulty, &rdev->flags))
8023 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008024 if (rdev->raid_disk >= 0)
8025 continue;
8026 if (test_bit(Faulty, &rdev->flags))
8027 continue;
8028 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11008029 ! (rdev->saved_raid_disk >= 0 &&
8030 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10008031 continue;
8032
NeilBrown7eb41882014-01-14 15:55:14 +11008033 if (rdev->saved_raid_disk < 0)
8034 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008035 if (mddev->pers->
8036 hot_add_disk(mddev, rdev) == 0) {
8037 if (sysfs_link_rdev(mddev, rdev))
8038 /* failure here is OK */;
8039 spares++;
8040 md_new_event(mddev);
8041 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008042 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008043 }
NeilBrown746d3202013-04-24 11:42:41 +10008044no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008045 if (removed)
8046 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008047 return spares;
8048}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008049
NeilBrownac05f252014-09-30 08:10:42 +10008050static void md_start_sync(struct work_struct *ws)
8051{
8052 struct mddev *mddev = container_of(ws, struct mddev, del_work);
8053
8054 mddev->sync_thread = md_register_thread(md_do_sync,
8055 mddev,
8056 "resync");
8057 if (!mddev->sync_thread) {
8058 printk(KERN_ERR "%s: could not start resync"
8059 " thread...\n",
8060 mdname(mddev));
8061 /* leave the spares where they are, it shouldn't hurt */
8062 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8063 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8064 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8065 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8066 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008067 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008068 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8069 &mddev->recovery))
8070 if (mddev->sysfs_action)
8071 sysfs_notify_dirent_safe(mddev->sysfs_action);
8072 } else
8073 md_wakeup_thread(mddev->sync_thread);
8074 sysfs_notify_dirent_safe(mddev->sysfs_action);
8075 md_new_event(mddev);
8076}
8077
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078/*
8079 * This routine is regularly called by all per-raid-array threads to
8080 * deal with generic issues like resync and super-block update.
8081 * Raid personalities that don't have a thread (linear/raid0) do not
8082 * need this as they never do any recovery or update the superblock.
8083 *
8084 * It does not do any resync itself, but rather "forks" off other threads
8085 * to do that as needed.
8086 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8087 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008088 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089 * and wakeups up this thread which will reap the thread and finish up.
8090 * This thread also removes any faulty devices (with nr_pending == 0).
8091 *
8092 * The overall approach is:
8093 * 1/ if the superblock needs updating, update it.
8094 * 2/ If a recovery thread is running, don't do anything else.
8095 * 3/ If recovery has finished, clean up, possibly marking spares active.
8096 * 4/ If there are any faulty devices, remove them.
8097 * 5/ If array is degraded, try to add spares devices
8098 * 6/ If array has spares or is not in-sync, start a resync thread.
8099 */
NeilBrownfd01b882011-10-11 16:47:53 +11008100void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101{
Jonathan Brassow68866e42011-06-08 15:10:08 +10008102 if (mddev->suspended)
8103 return;
8104
NeilBrown5f404022005-06-21 17:17:16 -07008105 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11008106 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107
NeilBrownfca4d842005-06-21 17:17:11 -07008108 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008109 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07008110 printk(KERN_INFO "md: %s in immediate safe mode\n",
8111 mdname(mddev));
8112 mddev->safemode = 2;
8113 }
8114 flush_signals(current);
8115 }
8116
NeilBrownc89a8ee2008-08-05 15:54:13 +10008117 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11008120 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008122 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008123 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008124 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
8125 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126 ))
8127 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008128
NeilBrowndf5b89b2006-03-27 01:18:20 -08008129 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008130 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008131
NeilBrownc89a8ee2008-08-05 15:54:13 +10008132 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008133 struct md_rdev *rdev;
8134 if (!mddev->external && mddev->in_sync)
8135 /* 'Blocked' flag not needed as failed devices
8136 * will be recorded if array switched to read/write.
8137 * Leaving it set will prevent the device
8138 * from being removed.
8139 */
8140 rdev_for_each(rdev, mddev)
8141 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008142 /* On a read-only array we can:
8143 * - remove failed devices
8144 * - add already-in_sync devices if the array itself
8145 * is in-sync.
8146 * As we only add devices that are already in-sync,
8147 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008148 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008149 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008150 /* There is no thread, but we need to call
8151 * ->spare_active and clear saved_raid_disk
8152 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008153 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008154 md_reap_sync_thread(mddev);
8155 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008156 goto unlock;
8157 }
8158
NeilBrown31a59e32008-04-30 00:52:30 -07008159 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10008160 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008161 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07008162 if (mddev->safemode &&
8163 !atomic_read(&mddev->writes_pending) &&
8164 !mddev->in_sync &&
8165 mddev->recovery_cp == MaxSector) {
8166 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10008167 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10008168 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07008169 }
8170 if (mddev->safemode == 1)
8171 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11008172 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10008173 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008174 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07008175 }
NeilBrownfca4d842005-06-21 17:17:11 -07008176
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008177 if (mddev->flags & MD_UPDATE_SB_FLAGS) {
8178 if (mddev_is_clustered(mddev))
8179 md_cluster_ops->metadata_update_start(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07008180 md_update_sb(mddev, 0);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008181 if (mddev_is_clustered(mddev))
8182 md_cluster_ops->metadata_update_finish(mddev);
8183 }
NeilBrown06d91a52005-06-21 17:17:12 -07008184
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8186 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8187 /* resync/recovery still happening */
8188 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8189 goto unlock;
8190 }
8191 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008192 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193 goto unlock;
8194 }
Neil Brown72a23c22008-06-28 08:31:41 +10008195 /* Set RUNNING before clearing NEEDED to avoid
8196 * any transients in the value of "sync_action".
8197 */
NeilBrown72f36d52012-10-11 14:25:57 +11008198 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008199 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10008200 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11008201 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08008202 /* Clear some bits that don't mean anything, but
8203 * might be left set
8204 */
NeilBrown24dd4692005-11-08 21:39:26 -08008205 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8206 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207
NeilBrowned209582012-04-24 10:23:14 +10008208 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8209 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008210 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008211 /* no recovery is running.
8212 * remove any failed drives, then
8213 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008214 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 * the personality to fail the re-add.
8216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008218 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008219 if (mddev->pers->check_reshape == NULL ||
8220 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008221 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008222 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008223 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008224 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008225 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008226 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8227 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008228 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008229 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008230 } else if (mddev->recovery_cp < MaxSector) {
8231 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008232 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008233 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8234 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008235 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008236
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008238 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008239 /* We are adding a device or devices to an array
8240 * which has the bitmap stored on all devices.
8241 * So make sure all bitmap pages get written
8242 */
8243 bitmap_write_all(mddev->bitmap);
8244 }
NeilBrownac05f252014-09-30 08:10:42 +10008245 INIT_WORK(&mddev->del_work, md_start_sync);
8246 queue_work(md_misc_wq, &mddev->del_work);
8247 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 }
NeilBrownac05f252014-09-30 08:10:42 +10008249 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008250 if (!mddev->sync_thread) {
8251 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008252 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008253 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8254 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008255 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008256 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008257 }
NeilBrownac05f252014-09-30 08:10:42 +10008258 unlock:
8259 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260 mddev_unlock(mddev);
8261 }
8262}
NeilBrown6c144d32014-09-30 16:15:38 +10008263EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008265void md_reap_sync_thread(struct mddev *mddev)
8266{
8267 struct md_rdev *rdev;
8268
8269 /* resync has finished, collect result */
8270 md_unregister_thread(&mddev->sync_thread);
8271 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8272 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8273 /* success...*/
8274 /* activate any spares */
8275 if (mddev->pers->spare_active(mddev)) {
8276 sysfs_notify(&mddev->kobj, NULL,
8277 "degraded");
8278 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8279 }
8280 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008281 if (mddev_is_clustered(mddev))
8282 md_cluster_ops->metadata_update_start(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008283 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8284 mddev->pers->finish_reshape)
8285 mddev->pers->finish_reshape(mddev);
8286
8287 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008288 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008289 */
NeilBrownf4667222013-12-09 12:04:56 +11008290 if (!mddev->degraded)
8291 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008292 rdev->saved_raid_disk = -1;
8293
8294 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05008295 if (mddev_is_clustered(mddev))
8296 md_cluster_ops->metadata_update_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008297 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008298 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008299 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8300 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8301 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8302 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008303 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008304 /* flag recovery needed just to double check */
8305 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8306 sysfs_notify_dirent_safe(mddev->sysfs_action);
8307 md_new_event(mddev);
8308 if (mddev->event_work.func)
8309 queue_work(md_misc_wq, &mddev->event_work);
8310}
NeilBrown6c144d32014-09-30 16:15:38 +10008311EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008312
NeilBrownfd01b882011-10-11 16:47:53 +11008313void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008314{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008315 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008316 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008317 !test_bit(Blocked, &rdev->flags) &&
8318 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008319 msecs_to_jiffies(5000));
8320 rdev_dec_pending(rdev, mddev);
8321}
8322EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8323
NeilBrownc6563a82012-05-21 09:27:00 +10008324void md_finish_reshape(struct mddev *mddev)
8325{
8326 /* called be personality module when reshape completes. */
8327 struct md_rdev *rdev;
8328
8329 rdev_for_each(rdev, mddev) {
8330 if (rdev->data_offset > rdev->new_data_offset)
8331 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8332 else
8333 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8334 rdev->data_offset = rdev->new_data_offset;
8335 }
8336}
8337EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008338
8339/* Bad block management.
8340 * We can record which blocks on each device are 'bad' and so just
8341 * fail those blocks, or that stripe, rather than the whole device.
8342 * Entries in the bad-block table are 64bits wide. This comprises:
8343 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8344 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8345 * A 'shift' can be set so that larger blocks are tracked and
8346 * consequently larger devices can be covered.
8347 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8348 *
8349 * Locking of the bad-block table uses a seqlock so md_is_badblock
8350 * might need to retry if it is very unlucky.
8351 * We will sometimes want to check for bad blocks in a bi_end_io function,
8352 * so we use the write_seqlock_irq variant.
8353 *
8354 * When looking for a bad block we specify a range and want to
8355 * know if any block in the range is bad. So we binary-search
8356 * to the last range that starts at-or-before the given endpoint,
8357 * (or "before the sector after the target range")
8358 * then see if it ends after the given start.
8359 * We return
8360 * 0 if there are no known bad blocks in the range
8361 * 1 if there are known bad block which are all acknowledged
8362 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8363 * plus the start/length of the first bad section we overlap.
8364 */
8365int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8366 sector_t *first_bad, int *bad_sectors)
8367{
8368 int hi;
majianpengab056132012-11-06 17:13:44 +08008369 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008370 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008371 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008372 sector_t target = s + sectors;
8373 unsigned seq;
8374
8375 if (bb->shift > 0) {
8376 /* round the start down, and the end up */
8377 s >>= bb->shift;
8378 target += (1<<bb->shift) - 1;
8379 target >>= bb->shift;
8380 sectors = target - s;
8381 }
8382 /* 'target' is now the first block after the bad range */
8383
8384retry:
8385 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008386 lo = 0;
8387 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008388 hi = bb->count;
8389
8390 /* Binary search between lo and hi for 'target'
8391 * i.e. for the last range that starts before 'target'
8392 */
8393 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8394 * are known not to be the last range before target.
8395 * VARIANT: hi-lo is the number of possible
8396 * ranges, and decreases until it reaches 1
8397 */
8398 while (hi - lo > 1) {
8399 int mid = (lo + hi) / 2;
8400 sector_t a = BB_OFFSET(p[mid]);
8401 if (a < target)
8402 /* This could still be the one, earlier ranges
8403 * could not. */
8404 lo = mid;
8405 else
8406 /* This and later ranges are definitely out. */
8407 hi = mid;
8408 }
8409 /* 'lo' might be the last that started before target, but 'hi' isn't */
8410 if (hi > lo) {
8411 /* need to check all range that end after 's' to see if
8412 * any are unacknowledged.
8413 */
8414 while (lo >= 0 &&
8415 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8416 if (BB_OFFSET(p[lo]) < target) {
8417 /* starts before the end, and finishes after
8418 * the start, so they must overlap
8419 */
8420 if (rv != -1 && BB_ACK(p[lo]))
8421 rv = 1;
8422 else
8423 rv = -1;
8424 *first_bad = BB_OFFSET(p[lo]);
8425 *bad_sectors = BB_LEN(p[lo]);
8426 }
8427 lo--;
8428 }
8429 }
8430
8431 if (read_seqretry(&bb->lock, seq))
8432 goto retry;
8433
8434 return rv;
8435}
8436EXPORT_SYMBOL_GPL(md_is_badblock);
8437
8438/*
8439 * Add a range of bad blocks to the table.
8440 * This might extend the table, or might contract it
8441 * if two adjacent ranges can be merged.
8442 * We binary-search to find the 'insertion' point, then
8443 * decide how best to handle it.
8444 */
8445static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8446 int acknowledged)
8447{
8448 u64 *p;
8449 int lo, hi;
8450 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008451 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008452
8453 if (bb->shift < 0)
8454 /* badblocks are disabled */
8455 return 0;
8456
8457 if (bb->shift) {
8458 /* round the start down, and the end up */
8459 sector_t next = s + sectors;
8460 s >>= bb->shift;
8461 next += (1<<bb->shift) - 1;
8462 next >>= bb->shift;
8463 sectors = next - s;
8464 }
8465
Bian Yu905b0292013-10-12 01:10:03 -04008466 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008467
8468 p = bb->page;
8469 lo = 0;
8470 hi = bb->count;
8471 /* Find the last range that starts at-or-before 's' */
8472 while (hi - lo > 1) {
8473 int mid = (lo + hi) / 2;
8474 sector_t a = BB_OFFSET(p[mid]);
8475 if (a <= s)
8476 lo = mid;
8477 else
8478 hi = mid;
8479 }
8480 if (hi > lo && BB_OFFSET(p[lo]) > s)
8481 hi = lo;
8482
8483 if (hi > lo) {
8484 /* we found a range that might merge with the start
8485 * of our new range
8486 */
8487 sector_t a = BB_OFFSET(p[lo]);
8488 sector_t e = a + BB_LEN(p[lo]);
8489 int ack = BB_ACK(p[lo]);
8490 if (e >= s) {
8491 /* Yes, we can merge with a previous range */
8492 if (s == a && s + sectors >= e)
8493 /* new range covers old */
8494 ack = acknowledged;
8495 else
8496 ack = ack && acknowledged;
8497
8498 if (e < s + sectors)
8499 e = s + sectors;
8500 if (e - a <= BB_MAX_LEN) {
8501 p[lo] = BB_MAKE(a, e-a, ack);
8502 s = e;
8503 } else {
8504 /* does not all fit in one range,
8505 * make p[lo] maximal
8506 */
8507 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8508 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8509 s = a + BB_MAX_LEN;
8510 }
8511 sectors = e - s;
8512 }
8513 }
8514 if (sectors && hi < bb->count) {
8515 /* 'hi' points to the first range that starts after 's'.
8516 * Maybe we can merge with the start of that range */
8517 sector_t a = BB_OFFSET(p[hi]);
8518 sector_t e = a + BB_LEN(p[hi]);
8519 int ack = BB_ACK(p[hi]);
8520 if (a <= s + sectors) {
8521 /* merging is possible */
8522 if (e <= s + sectors) {
8523 /* full overlap */
8524 e = s + sectors;
8525 ack = acknowledged;
8526 } else
8527 ack = ack && acknowledged;
8528
8529 a = s;
8530 if (e - a <= BB_MAX_LEN) {
8531 p[hi] = BB_MAKE(a, e-a, ack);
8532 s = e;
8533 } else {
8534 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8535 s = a + BB_MAX_LEN;
8536 }
8537 sectors = e - s;
8538 lo = hi;
8539 hi++;
8540 }
8541 }
8542 if (sectors == 0 && hi < bb->count) {
8543 /* we might be able to combine lo and hi */
8544 /* Note: 's' is at the end of 'lo' */
8545 sector_t a = BB_OFFSET(p[hi]);
8546 int lolen = BB_LEN(p[lo]);
8547 int hilen = BB_LEN(p[hi]);
8548 int newlen = lolen + hilen - (s - a);
8549 if (s >= a && newlen < BB_MAX_LEN) {
8550 /* yes, we can combine them */
8551 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8552 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8553 memmove(p + hi, p + hi + 1,
8554 (bb->count - hi - 1) * 8);
8555 bb->count--;
8556 }
8557 }
8558 while (sectors) {
8559 /* didn't merge (it all).
8560 * Need to add a range just before 'hi' */
8561 if (bb->count >= MD_MAX_BADBLOCKS) {
8562 /* No room for more */
8563 rv = 0;
8564 break;
8565 } else {
8566 int this_sectors = sectors;
8567 memmove(p + hi + 1, p + hi,
8568 (bb->count - hi) * 8);
8569 bb->count++;
8570
8571 if (this_sectors > BB_MAX_LEN)
8572 this_sectors = BB_MAX_LEN;
8573 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8574 sectors -= this_sectors;
8575 s += this_sectors;
8576 }
8577 }
8578
8579 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008580 if (!acknowledged)
8581 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008582 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008583
8584 return rv;
8585}
8586
NeilBrown3cb03002011-10-11 16:45:26 +11008587int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008588 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008589{
NeilBrownc6563a82012-05-21 09:27:00 +10008590 int rv;
8591 if (is_new)
8592 s += rdev->new_data_offset;
8593 else
8594 s += rdev->data_offset;
8595 rv = md_set_badblocks(&rdev->badblocks,
8596 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008597 if (rv) {
8598 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008599 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008600 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8601 md_wakeup_thread(rdev->mddev->thread);
8602 }
8603 return rv;
8604}
8605EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8606
8607/*
8608 * Remove a range of bad blocks from the table.
8609 * This may involve extending the table if we spilt a region,
8610 * but it must not fail. So if the table becomes full, we just
8611 * drop the remove request.
8612 */
8613static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8614{
8615 u64 *p;
8616 int lo, hi;
8617 sector_t target = s + sectors;
8618 int rv = 0;
8619
8620 if (bb->shift > 0) {
8621 /* When clearing we round the start up and the end down.
8622 * This should not matter as the shift should align with
8623 * the block size and no rounding should ever be needed.
8624 * However it is better the think a block is bad when it
8625 * isn't than to think a block is not bad when it is.
8626 */
8627 s += (1<<bb->shift) - 1;
8628 s >>= bb->shift;
8629 target >>= bb->shift;
8630 sectors = target - s;
8631 }
8632
8633 write_seqlock_irq(&bb->lock);
8634
8635 p = bb->page;
8636 lo = 0;
8637 hi = bb->count;
8638 /* Find the last range that starts before 'target' */
8639 while (hi - lo > 1) {
8640 int mid = (lo + hi) / 2;
8641 sector_t a = BB_OFFSET(p[mid]);
8642 if (a < target)
8643 lo = mid;
8644 else
8645 hi = mid;
8646 }
8647 if (hi > lo) {
8648 /* p[lo] is the last range that could overlap the
8649 * current range. Earlier ranges could also overlap,
8650 * but only this one can overlap the end of the range.
8651 */
8652 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8653 /* Partial overlap, leave the tail of this range */
8654 int ack = BB_ACK(p[lo]);
8655 sector_t a = BB_OFFSET(p[lo]);
8656 sector_t end = a + BB_LEN(p[lo]);
8657
8658 if (a < s) {
8659 /* we need to split this range */
8660 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008661 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008662 goto out;
8663 }
8664 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8665 bb->count++;
8666 p[lo] = BB_MAKE(a, s-a, ack);
8667 lo++;
8668 }
8669 p[lo] = BB_MAKE(target, end - target, ack);
8670 /* there is no longer an overlap */
8671 hi = lo;
8672 lo--;
8673 }
8674 while (lo >= 0 &&
8675 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8676 /* This range does overlap */
8677 if (BB_OFFSET(p[lo]) < s) {
8678 /* Keep the early parts of this range. */
8679 int ack = BB_ACK(p[lo]);
8680 sector_t start = BB_OFFSET(p[lo]);
8681 p[lo] = BB_MAKE(start, s - start, ack);
8682 /* now low doesn't overlap, so.. */
8683 break;
8684 }
8685 lo--;
8686 }
8687 /* 'lo' is strictly before, 'hi' is strictly after,
8688 * anything between needs to be discarded
8689 */
8690 if (hi - lo > 1) {
8691 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8692 bb->count -= (hi - lo - 1);
8693 }
8694 }
8695
8696 bb->changed = 1;
8697out:
8698 write_sequnlock_irq(&bb->lock);
8699 return rv;
8700}
8701
NeilBrownc6563a82012-05-21 09:27:00 +10008702int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8703 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008704{
NeilBrownc6563a82012-05-21 09:27:00 +10008705 if (is_new)
8706 s += rdev->new_data_offset;
8707 else
8708 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008709 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008710 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008711}
8712EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8713
8714/*
8715 * Acknowledge all bad blocks in a list.
8716 * This only succeeds if ->changed is clear. It is used by
8717 * in-kernel metadata updates
8718 */
8719void md_ack_all_badblocks(struct badblocks *bb)
8720{
8721 if (bb->page == NULL || bb->changed)
8722 /* no point even trying */
8723 return;
8724 write_seqlock_irq(&bb->lock);
8725
majianpengecb178b2012-03-19 12:46:42 +11008726 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008727 u64 *p = bb->page;
8728 int i;
8729 for (i = 0; i < bb->count ; i++) {
8730 if (!BB_ACK(p[i])) {
8731 sector_t start = BB_OFFSET(p[i]);
8732 int len = BB_LEN(p[i]);
8733 p[i] = BB_MAKE(start, len, 1);
8734 }
8735 }
NeilBrownde393cd2011-07-28 11:31:48 +10008736 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008737 }
8738 write_sequnlock_irq(&bb->lock);
8739}
8740EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8741
NeilBrown16c791a2011-07-28 11:31:47 +10008742/* sysfs access to bad-blocks list.
8743 * We present two files.
8744 * 'bad-blocks' lists sector numbers and lengths of ranges that
8745 * are recorded as bad. The list is truncated to fit within
8746 * the one-page limit of sysfs.
8747 * Writing "sector length" to this file adds an acknowledged
8748 * bad block list.
8749 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8750 * been acknowledged. Writing to this file adds bad blocks
8751 * without acknowledging them. This is largely for testing.
8752 */
8753
8754static ssize_t
8755badblocks_show(struct badblocks *bb, char *page, int unack)
8756{
8757 size_t len;
8758 int i;
8759 u64 *p = bb->page;
8760 unsigned seq;
8761
8762 if (bb->shift < 0)
8763 return 0;
8764
8765retry:
8766 seq = read_seqbegin(&bb->lock);
8767
8768 len = 0;
8769 i = 0;
8770
8771 while (len < PAGE_SIZE && i < bb->count) {
8772 sector_t s = BB_OFFSET(p[i]);
8773 unsigned int length = BB_LEN(p[i]);
8774 int ack = BB_ACK(p[i]);
8775 i++;
8776
8777 if (unack && ack)
8778 continue;
8779
8780 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8781 (unsigned long long)s << bb->shift,
8782 length << bb->shift);
8783 }
NeilBrownde393cd2011-07-28 11:31:48 +10008784 if (unack && len == 0)
8785 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008786
8787 if (read_seqretry(&bb->lock, seq))
8788 goto retry;
8789
8790 return len;
8791}
8792
8793#define DO_DEBUG 1
8794
8795static ssize_t
8796badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8797{
8798 unsigned long long sector;
8799 int length;
8800 char newline;
8801#ifdef DO_DEBUG
8802 /* Allow clearing via sysfs *only* for testing/debugging.
8803 * Normally only a successful write may clear a badblock
8804 */
8805 int clear = 0;
8806 if (page[0] == '-') {
8807 clear = 1;
8808 page++;
8809 }
8810#endif /* DO_DEBUG */
8811
8812 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8813 case 3:
8814 if (newline != '\n')
8815 return -EINVAL;
8816 case 2:
8817 if (length <= 0)
8818 return -EINVAL;
8819 break;
8820 default:
8821 return -EINVAL;
8822 }
8823
8824#ifdef DO_DEBUG
8825 if (clear) {
8826 md_clear_badblocks(bb, sector, length);
8827 return len;
8828 }
8829#endif /* DO_DEBUG */
8830 if (md_set_badblocks(bb, sector, length, !unack))
8831 return len;
8832 else
8833 return -ENOSPC;
8834}
8835
Adrian Bunk75c96f82005-05-05 16:16:09 -07008836static int md_notify_reboot(struct notifier_block *this,
8837 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838{
8839 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008840 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008841 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842
NeilBrownc744a652012-03-19 12:46:37 +11008843 for_each_mddev(mddev, tmp) {
8844 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008845 if (mddev->pers)
8846 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008847 if (mddev->persistent)
8848 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008849 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008850 }
NeilBrownc744a652012-03-19 12:46:37 +11008851 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852 }
NeilBrownc744a652012-03-19 12:46:37 +11008853 /*
8854 * certain more exotic SCSI devices are known to be
8855 * volatile wrt too early system reboots. While the
8856 * right place to handle this issue is the given
8857 * driver, we do want to have a safe RAID driver ...
8858 */
8859 if (need_delay)
8860 mdelay(1000*1);
8861
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862 return NOTIFY_DONE;
8863}
8864
Adrian Bunk75c96f82005-05-05 16:16:09 -07008865static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866 .notifier_call = md_notify_reboot,
8867 .next = NULL,
8868 .priority = INT_MAX, /* before any real devices */
8869};
8870
8871static void md_geninit(void)
8872{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008873 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008875 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876}
8877
Adrian Bunk75c96f82005-05-05 16:16:09 -07008878static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879{
Tejun Heoe804ac72010-10-15 15:36:08 +02008880 int ret = -ENOMEM;
8881
Tejun Heoada609e2011-01-25 14:35:54 +01008882 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008883 if (!md_wq)
8884 goto err_wq;
8885
8886 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8887 if (!md_misc_wq)
8888 goto err_misc_wq;
8889
8890 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8891 goto err_md;
8892
8893 if ((ret = register_blkdev(0, "mdp")) < 0)
8894 goto err_mdp;
8895 mdp_major = ret;
8896
NeilBrownaf5628f2014-07-31 13:54:54 +10008897 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008898 md_probe, NULL, NULL);
8899 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 md_probe, NULL, NULL);
8901
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008903 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904
8905 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
Tejun Heoe804ac72010-10-15 15:36:08 +02008908err_mdp:
8909 unregister_blkdev(MD_MAJOR, "md");
8910err_md:
8911 destroy_workqueue(md_misc_wq);
8912err_misc_wq:
8913 destroy_workqueue(md_wq);
8914err_wq:
8915 return ret;
8916}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05008918void md_reload_sb(struct mddev *mddev)
8919{
8920 struct md_rdev *rdev, *tmp;
8921
8922 rdev_for_each_safe(rdev, tmp, mddev) {
8923 rdev->sb_loaded = 0;
8924 ClearPageUptodate(rdev->sb_page);
8925 }
8926 mddev->raid_disks = 0;
8927 analyze_sbs(mddev);
8928 rdev_for_each_safe(rdev, tmp, mddev) {
8929 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
8930 /* since we don't write to faulty devices, we figure out if the
8931 * disk is faulty by comparing events
8932 */
8933 if (mddev->events > sb->events)
8934 set_bit(Faulty, &rdev->flags);
8935 }
8936
8937}
8938EXPORT_SYMBOL(md_reload_sb);
8939
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940#ifndef MODULE
8941
8942/*
8943 * Searches all registered partitions for autorun RAID arrays
8944 * at boot time.
8945 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008946
8947static LIST_HEAD(all_detected_devices);
8948struct detected_devices_node {
8949 struct list_head list;
8950 dev_t dev;
8951};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952
8953void md_autodetect_dev(dev_t dev)
8954{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008955 struct detected_devices_node *node_detected_dev;
8956
8957 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8958 if (node_detected_dev) {
8959 node_detected_dev->dev = dev;
8960 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8961 } else {
8962 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8963 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965}
8966
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967static void autostart_arrays(int part)
8968{
NeilBrown3cb03002011-10-11 16:45:26 +11008969 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008970 struct detected_devices_node *node_detected_dev;
8971 dev_t dev;
8972 int i_scanned, i_passed;
8973
8974 i_scanned = 0;
8975 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008976
8977 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8978
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008979 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8980 i_scanned++;
8981 node_detected_dev = list_entry(all_detected_devices.next,
8982 struct detected_devices_node, list);
8983 list_del(&node_detected_dev->list);
8984 dev = node_detected_dev->dev;
8985 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008986 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008987 if (IS_ERR(rdev))
8988 continue;
8989
NeilBrown403df472014-09-30 15:52:29 +10008990 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008991 continue;
NeilBrown403df472014-09-30 15:52:29 +10008992
NeilBrownd0fae182008-03-04 14:29:31 -08008993 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008995 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008997
8998 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8999 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000
9001 autorun_devices(part);
9002}
9003
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009004#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005
9006static __exit void md_exit(void)
9007{
NeilBrownfd01b882011-10-11 16:47:53 +11009008 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009010 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009011
NeilBrownaf5628f2014-07-31 13:54:54 +10009012 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009013 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009015 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009016 unregister_blkdev(mdp_major, "mdp");
9017 unregister_reboot_notifier(&md_notifier);
9018 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009019
9020 /* We cannot unload the modules while some process is
9021 * waiting for us in select() or poll() - wake them up
9022 */
9023 md_unloading = 1;
9024 while (waitqueue_active(&md_event_waiters)) {
9025 /* not safe to leave yet */
9026 wake_up(&md_event_waiters);
9027 msleep(delay);
9028 delay += delay;
9029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009031
NeilBrown29ac4aa2008-02-06 01:39:58 -08009032 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009033 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11009034 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009035 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009036 destroy_workqueue(md_misc_wq);
9037 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038}
9039
Dan Williams685784a2007-07-09 11:56:42 -07009040subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041module_exit(md_exit)
9042
NeilBrownf91de922005-11-08 21:39:36 -08009043static int get_ro(char *buffer, struct kernel_param *kp)
9044{
9045 return sprintf(buffer, "%d", start_readonly);
9046}
9047static int set_ro(const char *val, struct kernel_param *kp)
9048{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009049 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009050}
9051
NeilBrown80ca3a42006-07-10 04:44:18 -07009052module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9053module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009054module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009055
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009057MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009058MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009059MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);