blob: 5ed57688e5c5109d32f607618194c1dd0e9937f3 [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;
71struct module *md_cluster_mod;
72EXPORT_SYMBOL(md_cluster_mod);
73
Bernd Schubert90b08712008-05-23 13:04:38 -070074static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020075static struct workqueue_struct *md_wq;
76static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070077
NeilBrown746d3202013-04-24 11:42:41 +100078static int remove_and_add_spares(struct mddev *mddev,
79 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110080static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
Robert Becker1e509152009-12-14 12:49:58 +110083 * Default number of read corrections we'll attempt on an rdev
84 * before ejecting it from the array. We divide the read error
85 * count by 2 for every hour elapsed between read errors.
86 */
87#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
88/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
90 * is 1000 KB/sec, so the extra system load does not show up that much.
91 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070092 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * subsystem is idle. There is also an 'absolute maximum' reconstruction
94 * speed limit - in case reconstruction slows down your system despite
95 * idle IO detection.
96 *
97 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080098 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
100
101static int sysctl_speed_limit_min = 1000;
102static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100103static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800104{
105 return mddev->sync_speed_min ?
106 mddev->sync_speed_min : sysctl_speed_limit_min;
107}
108
NeilBrownfd01b882011-10-11 16:47:53 +1100109static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800110{
111 return mddev->sync_speed_max ?
112 mddev->sync_speed_max : sysctl_speed_limit_max;
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static struct ctl_table_header *raid_table_header;
116
Joe Perches82592c32013-11-14 15:16:18 +1100117static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "speed_limit_min",
120 .data = &sysctl_speed_limit_min,
121 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700122 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800123 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 },
125 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .procname = "speed_limit_max",
127 .data = &sysctl_speed_limit_max,
128 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700129 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800130 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800132 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
Joe Perches82592c32013-11-14 15:16:18 +1100135static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .procname = "raid",
138 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700139 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .child = raid_table,
141 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800142 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Joe Perches82592c32013-11-14 15:16:18 +1100145static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .procname = "dev",
148 .maxlen = 0,
149 .mode = 0555,
150 .child = raid_dir_table,
151 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800152 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700155static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
NeilBrownf91de922005-11-08 21:39:36 -0800157static int start_readonly;
158
NeilBrowna167f662010-10-26 18:31:13 +1100159/* bio_clone_mddev
160 * like bio_clone, but with a local bio set
161 */
162
NeilBrowna167f662010-10-26 18:31:13 +1100163struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100164 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100165{
166 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100167
168 if (!mddev || !mddev->bio_set)
169 return bio_alloc(gfp_mask, nr_iovecs);
170
Kent Overstreet395c72a2012-09-06 15:34:55 -0700171 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100172 if (!b)
173 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100174 return b;
175}
176EXPORT_SYMBOL_GPL(bio_alloc_mddev);
177
178struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100179 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100180{
NeilBrowna167f662010-10-26 18:31:13 +1100181 if (!mddev || !mddev->bio_set)
182 return bio_clone(bio, gfp_mask);
183
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700184 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100185}
186EXPORT_SYMBOL_GPL(bio_clone_mddev);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
NeilBrownd7603b72006-01-06 00:20:30 -0800189 * We have a system wide 'event count' that is incremented
190 * on any 'interesting' event, and readers of /proc/mdstat
191 * can use 'poll' or 'select' to find out when the event
192 * count increases.
193 *
194 * Events are:
195 * start array, stop array, error, add device, remove device,
196 * start build, activate spare
197 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800198static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800199static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100200void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800201{
202 atomic_inc(&md_event_count);
203 wake_up(&md_event_waiters);
204}
NeilBrown29269552006-03-27 01:18:10 -0800205EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800206
NeilBrownc331eb02006-05-30 21:27:13 -0700207/* Alternate version that can be called from interrupts
208 * when calling sysfs_notify isn't needed.
209 */
NeilBrownfd01b882011-10-11 16:47:53 +1100210static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700211{
212 atomic_inc(&md_event_count);
213 wake_up(&md_event_waiters);
214}
215
NeilBrownd7603b72006-01-06 00:20:30 -0800216/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * Enables to iterate over all existing md arrays
218 * all_mddevs_lock protects this list.
219 */
220static LIST_HEAD(all_mddevs);
221static DEFINE_SPINLOCK(all_mddevs_lock);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/*
224 * iterates through all used mddevs in the system.
225 * We take care to grab the all_mddevs_lock whenever navigating
226 * the list, and to always hold a refcount when unlocked.
227 * Any code which breaks out of this loop while own
228 * a reference to the current mddev and must mddev_put it.
229 */
NeilBrownfd01b882011-10-11 16:47:53 +1100230#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000232 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100233 _tmp = all_mddevs.next; \
234 _mddev = NULL;}); \
235 ({ if (_tmp != &all_mddevs) \
236 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100238 if (_mddev) mddev_put(_mddev); \
239 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
240 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100242 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 )
244
NeilBrown409c57f2009-03-31 14:39:39 +1100245/* Rather than calling directly into the personality make_request function,
246 * IO requests come here first so that we can check if the device is
247 * being suspended pending a reconfiguration.
248 * We hold a refcount over the call to ->make_request. By the time that
249 * call has finished, the bio has been linked into some internal structure
250 * and so is visible to ->quiesce(), so we don't need the refcount any more.
251 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200252static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
NeilBrown49077322010-03-25 16:20:56 +1100254 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100255 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500256 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100257
NeilBrown0ca69882011-01-14 09:14:33 +1100258 if (mddev == NULL || mddev->pers == NULL
259 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100260 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200261 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100262 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100263 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
264 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
265 return;
266 }
NeilBrown0ca69882011-01-14 09:14:33 +1100267 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100268 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200269 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100270 DEFINE_WAIT(__wait);
271 for (;;) {
272 prepare_to_wait(&mddev->sb_wait, &__wait,
273 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200274 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100275 break;
276 rcu_read_unlock();
277 schedule();
278 rcu_read_lock();
279 }
280 finish_wait(&mddev->sb_wait, &__wait);
281 }
282 atomic_inc(&mddev->active_io);
283 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100284
Chris Masone91ece52011-02-07 19:21:48 -0500285 /*
286 * save the sectors now since our bio can
287 * go away inside make_request
288 */
289 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200290 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100291
Gu Zheng18c0b222014-11-24 11:05:26 +0800292 generic_start_io_acct(rw, sectors, &mddev->gendisk->part0);
NeilBrown49077322010-03-25 16:20:56 +1100293
NeilBrown409c57f2009-03-31 14:39:39 +1100294 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
295 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
NeilBrown9e35b992010-04-06 14:23:02 +1000298/* mddev_suspend makes sure no new requests are submitted
299 * to the device, and that any requests that have been submitted
300 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100301 * Once mddev_detach() is called and completes, the module will be
302 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000303 */
NeilBrownfd01b882011-10-11 16:47:53 +1100304void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100305{
306 BUG_ON(mddev->suspended);
307 mddev->suspended = 1;
308 synchronize_rcu();
309 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
310 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500311
312 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100313}
NeilBrown390ee602010-06-01 19:37:27 +1000314EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100315
NeilBrownfd01b882011-10-11 16:47:53 +1100316void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100317{
318 mddev->suspended = 0;
319 wake_up(&mddev->sb_wait);
320 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500321
Jonathan Brassow47525e52012-05-22 13:55:29 +1000322 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500323 md_wakeup_thread(mddev->thread);
324 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100325}
NeilBrown390ee602010-06-01 19:37:27 +1000326EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100327
NeilBrownfd01b882011-10-11 16:47:53 +1100328int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000329{
NeilBrown5c675f82014-12-15 12:56:56 +1100330 struct md_personality *pers = mddev->pers;
331 int ret = 0;
332
333 rcu_read_lock();
334 if (mddev->suspended)
335 ret = 1;
336 else if (pers && pers->congested)
337 ret = pers->congested(mddev, bits);
338 rcu_read_unlock();
339 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000340}
NeilBrown5c675f82014-12-15 12:56:56 +1100341EXPORT_SYMBOL_GPL(mddev_congested);
342static int md_congested(void *data, int bits)
343{
344 struct mddev *mddev = data;
345 return mddev_congested(mddev, bits);
346}
NeilBrown3fa841d2009-09-23 18:10:29 +1000347
NeilBrown64590f42014-12-15 12:56:57 +1100348static int md_mergeable_bvec(struct request_queue *q,
349 struct bvec_merge_data *bvm,
350 struct bio_vec *biovec)
351{
352 struct mddev *mddev = q->queuedata;
353 int ret;
354 rcu_read_lock();
355 if (mddev->suspended) {
356 /* Must always allow one vec */
357 if (bvm->bi_size == 0)
358 ret = biovec->bv_len;
359 else
360 ret = 0;
361 } else {
362 struct md_personality *pers = mddev->pers;
363 if (pers && pers->mergeable_bvec)
364 ret = pers->mergeable_bvec(mddev, bvm, biovec);
365 else
366 ret = biovec->bv_len;
367 }
368 rcu_read_unlock();
369 return ret;
370}
NeilBrowna2826aa2009-12-14 12:49:49 +1100371/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200372 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100373 */
374
Tejun Heoe9c74692010-09-03 11:56:18 +0200375static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100376{
NeilBrown3cb03002011-10-11 16:45:26 +1100377 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100378 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100379
380 rdev_dec_pending(rdev, mddev);
381
382 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200383 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200384 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100385 }
386 bio_put(bio);
387}
388
NeilBrowna7a07e62010-12-09 16:04:25 +1100389static void md_submit_flush_data(struct work_struct *ws);
390
NeilBrowna035fc32010-12-09 16:17:51 +1100391static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100392{
NeilBrownfd01b882011-10-11 16:47:53 +1100393 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100394 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100395
NeilBrowna7a07e62010-12-09 16:04:25 +1100396 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
397 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100398 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100399 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100400 if (rdev->raid_disk >= 0 &&
401 !test_bit(Faulty, &rdev->flags)) {
402 /* Take two references, one is dropped
403 * when request finishes, one after
404 * we reclaim rcu_read_lock
405 */
406 struct bio *bi;
407 atomic_inc(&rdev->nr_pending);
408 atomic_inc(&rdev->nr_pending);
409 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000410 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200411 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100412 bi->bi_private = rdev;
413 bi->bi_bdev = rdev->bdev;
414 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200415 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100416 rcu_read_lock();
417 rdev_dec_pending(rdev, mddev);
418 }
419 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100420 if (atomic_dec_and_test(&mddev->flush_pending))
421 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100422}
423
Tejun Heoe9c74692010-09-03 11:56:18 +0200424static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100425{
NeilBrownfd01b882011-10-11 16:47:53 +1100426 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200427 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100428
Kent Overstreet4f024f32013-10-11 15:44:27 -0700429 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100430 /* an empty barrier - all done */
431 bio_endio(bio, 0);
432 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200433 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200434 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100435 }
NeilBrown2b74e122010-12-09 15:59:01 +1100436
437 mddev->flush_bio = NULL;
438 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100439}
440
NeilBrownfd01b882011-10-11 16:47:53 +1100441void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100442{
NeilBrown85572d72014-12-15 12:56:56 +1100443 spin_lock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200445 !mddev->flush_bio,
NeilBrown85572d72014-12-15 12:56:56 +1100446 mddev->lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200447 mddev->flush_bio = bio;
NeilBrown85572d72014-12-15 12:56:56 +1100448 spin_unlock_irq(&mddev->lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100449
NeilBrowna035fc32010-12-09 16:17:51 +1100450 INIT_WORK(&mddev->flush_work, submit_flushes);
451 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100452}
Tejun Heoe9c74692010-09-03 11:56:18 +0200453EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100454
NeilBrown74018dc2012-07-31 09:08:15 +0200455void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000456{
NeilBrown9cbb1752012-07-31 09:08:14 +0200457 struct mddev *mddev = cb->data;
458 md_wakeup_thread(mddev->thread);
459 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000460}
NeilBrown9cbb1752012-07-31 09:08:14 +0200461EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000462
NeilBrownfd01b882011-10-11 16:47:53 +1100463static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 atomic_inc(&mddev->active);
466 return mddev;
467}
468
Dan Williams5fd3a172009-03-04 00:57:25 -0700469static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100470
NeilBrownfd01b882011-10-11 16:47:53 +1100471static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
NeilBrowna167f662010-10-26 18:31:13 +1100473 struct bio_set *bs = NULL;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
476 return;
NeilBrownd3374822009-01-09 08:31:10 +1100477 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100478 mddev->ctime == 0 && !mddev->hold_active) {
479 /* Array is not configured at all, and not held active,
480 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100481 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100482 bs = mddev->bio_set;
483 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100484 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200485 /* We did a probe so need to clean up. Call
486 * queue_work inside the spinlock so that
487 * flush_workqueue() after mddev_find will
488 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100489 */
490 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200491 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100492 } else
493 kfree(mddev);
494 }
495 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100496 if (bs)
497 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
NeilBrownfd01b882011-10-11 16:47:53 +1100500void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100501{
502 mutex_init(&mddev->open_mutex);
503 mutex_init(&mddev->reconfig_mutex);
504 mutex_init(&mddev->bitmap_info.mutex);
505 INIT_LIST_HEAD(&mddev->disks);
506 INIT_LIST_HEAD(&mddev->all_mddevs);
507 init_timer(&mddev->safemode_timer);
508 atomic_set(&mddev->active, 1);
509 atomic_set(&mddev->openers, 0);
510 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100511 spin_lock_init(&mddev->lock);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100512 atomic_set(&mddev->flush_pending, 0);
513 init_waitqueue_head(&mddev->sb_wait);
514 init_waitqueue_head(&mddev->recovery_wait);
515 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000516 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500517 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100518 mddev->resync_min = 0;
519 mddev->resync_max = MaxSector;
520 mddev->level = LEVEL_NONE;
521}
NeilBrown390ee602010-06-01 19:37:27 +1000522EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100523
NeilBrownf72ffdd2014-09-30 14:23:59 +1000524static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
NeilBrownfd01b882011-10-11 16:47:53 +1100526 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
NeilBrown8f5f02c2011-02-16 13:58:51 +1100528 if (unit && MAJOR(unit) != MD_MAJOR)
529 unit &= ~((1<<MdpMinorShift)-1);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 retry:
532 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
NeilBrownefeb53c2009-01-09 08:31:10 +1100534 if (unit) {
535 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
536 if (mddev->unit == unit) {
537 mddev_get(mddev);
538 spin_unlock(&all_mddevs_lock);
539 kfree(new);
540 return mddev;
541 }
542
543 if (new) {
544 list_add(&new->all_mddevs, &all_mddevs);
545 spin_unlock(&all_mddevs_lock);
546 new->hold_active = UNTIL_IOCTL;
547 return new;
548 }
549 } else if (new) {
550 /* find an unused unit number */
551 static int next_minor = 512;
552 int start = next_minor;
553 int is_free = 0;
554 int dev = 0;
555 while (!is_free) {
556 dev = MKDEV(MD_MAJOR, next_minor);
557 next_minor++;
558 if (next_minor > MINORMASK)
559 next_minor = 0;
560 if (next_minor == start) {
561 /* Oh dear, all in use. */
562 spin_unlock(&all_mddevs_lock);
563 kfree(new);
564 return NULL;
565 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000566
NeilBrownefeb53c2009-01-09 08:31:10 +1100567 is_free = 1;
568 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
569 if (mddev->unit == dev) {
570 is_free = 0;
571 break;
572 }
573 }
574 new->unit = dev;
575 new->md_minor = MINOR(dev);
576 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 list_add(&new->all_mddevs, &all_mddevs);
578 spin_unlock(&all_mddevs_lock);
579 return new;
580 }
581 spin_unlock(&all_mddevs_lock);
582
NeilBrown9ffae0c2006-01-06 00:20:32 -0800583 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (!new)
585 return NULL;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 new->unit = unit;
588 if (MAJOR(unit) == MD_MAJOR)
589 new->md_minor = MINOR(unit);
590 else
591 new->md_minor = MINOR(unit) >> MdpMinorShift;
592
NeilBrownfafd7fb2010-04-01 15:55:30 +1100593 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto retry;
596}
597
NeilBrownb6eb1272010-04-15 10:13:47 +1000598static struct attribute_group md_redundancy_group;
599
NeilBrown5c47daf2014-12-15 12:57:01 +1100600void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
NeilBrowna64c8762010-04-14 17:15:37 +1000602 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000603 /* These cannot be removed under reconfig_mutex as
604 * an access to the files will try to take reconfig_mutex
605 * while holding the file unremovable, which leads to
606 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000607 * So hold set sysfs_active while the remove in happeing,
608 * and anything else which might set ->to_remove or my
609 * otherwise change the sysfs namespace will fail with
610 * -EBUSY if sysfs_active is still set.
611 * We set sysfs_active under reconfig_mutex and elsewhere
612 * test it under the same mutex to ensure its correct value
613 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000614 */
NeilBrowna64c8762010-04-14 17:15:37 +1000615 struct attribute_group *to_remove = mddev->to_remove;
616 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000617 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000618 mutex_unlock(&mddev->reconfig_mutex);
619
NeilBrown00bcb4a2010-06-01 19:37:23 +1000620 if (mddev->kobj.sd) {
621 if (to_remove != &md_redundancy_group)
622 sysfs_remove_group(&mddev->kobj, to_remove);
623 if (mddev->pers == NULL ||
624 mddev->pers->sync_request == NULL) {
625 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
626 if (mddev->sysfs_action)
627 sysfs_put(mddev->sysfs_action);
628 mddev->sysfs_action = NULL;
629 }
NeilBrowna64c8762010-04-14 17:15:37 +1000630 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000631 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000632 } else
633 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Chris Dunlop751e67c2011-10-19 16:48:26 +1100635 /* As we've dropped the mutex we need a spinlock to
636 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000637 */
638 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700639 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000640 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
NeilBrown5c47daf2014-12-15 12:57:01 +1100642EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
NeilBrown1ca69c42012-10-11 13:37:33 +1100644static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
645{
646 struct md_rdev *rdev;
647
648 rdev_for_each_rcu(rdev, mddev)
649 if (rdev->desc_nr == nr)
650 return rdev;
651
652 return NULL;
653}
654
655static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
NeilBrown3cb03002011-10-11 16:45:26 +1100657 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
NeilBrowndafb20f2012-03-19 12:46:39 +1100659 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (rdev->bdev->bd_dev == dev)
661 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return NULL;
664}
665
NeilBrown1ca69c42012-10-11 13:37:33 +1100666static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
667{
668 struct md_rdev *rdev;
669
670 rdev_for_each_rcu(rdev, mddev)
671 if (rdev->bdev->bd_dev == dev)
672 return rdev;
673
674 return NULL;
675}
676
NeilBrown84fc4b52011-10-11 16:49:58 +1100677static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800678{
NeilBrown84fc4b52011-10-11 16:49:58 +1100679 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800680 list_for_each_entry(pers, &pers_list, list) {
681 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800682 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800683 if (strcmp(pers->name, clevel)==0)
684 return pers;
685 }
NeilBrown2604b702006-01-06 00:20:36 -0800686 return NULL;
687}
688
Andre Nollb73df2d2008-07-11 22:02:23 +1000689/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100690static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100692 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000693 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
NeilBrownf72ffdd2014-09-30 14:23:59 +1000696static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 rdev->sb_page = alloc_page(GFP_KERNEL);
699 if (!rdev->sb_page) {
700 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000701 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703
704 return 0;
705}
706
NeilBrown545c8792012-05-22 13:54:30 +1000707void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800710 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 rdev->sb_loaded = 0;
712 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000713 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100714 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
NeilBrown2699b672011-07-28 11:31:47 +1000716 if (rdev->bb_page) {
717 put_page(rdev->bb_page);
718 rdev->bb_page = NULL;
719 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000720 kfree(rdev->badblocks.page);
721 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
NeilBrown545c8792012-05-22 13:54:30 +1000723EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
NeilBrown6712ecf2007-09-27 12:47:43 +0200725static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700726{
NeilBrown3cb03002011-10-11 16:45:26 +1100727 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100728 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700729
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700730 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
731 printk("md: super_written gets error=%d, uptodate=%d\n",
732 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
733 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800734 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700735 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700736
NeilBrowna9701a32005-11-08 21:39:34 -0800737 if (atomic_dec_and_test(&mddev->pending_writes))
738 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700739 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700740}
741
NeilBrownfd01b882011-10-11 16:47:53 +1100742void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743 sector_t sector, int size, struct page *page)
744{
745 /* write first size bytes of page to sector of rdev
746 * Increment mddev->pending_writes before returning
747 * and decrement it on completion, waking up sb_wait
748 * if zero is reached.
749 * If an error occurred, call md_error
750 */
NeilBrowna167f662010-10-26 18:31:13 +1100751 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700752
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100753 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700754 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700755 bio_add_page(bio, page, size, 0);
756 bio->bi_private = rdev;
757 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800758
NeilBrown7bfa19f2005-06-21 17:17:28 -0700759 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000760 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800761}
762
NeilBrownfd01b882011-10-11 16:47:53 +1100763void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800764{
Tejun Heoe9c74692010-09-03 11:56:18 +0200765 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000766 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700767}
768
NeilBrown3cb03002011-10-11 16:45:26 +1100769int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100770 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
NeilBrowna167f662010-10-26 18:31:13 +1100772 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int ret;
774
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100775 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
776 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100777 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700778 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000779 else if (rdev->mddev->reshape_position != MaxSector &&
780 (rdev->mddev->reshape_backwards ==
781 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700782 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100783 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700784 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700786 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
789 bio_put(bio);
790 return ret;
791}
NeilBrowna8745db2006-01-06 00:20:34 -0800792EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
NeilBrownf72ffdd2014-09-30 14:23:59 +1000794static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (rdev->sb_loaded)
799 return 0;
800
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100801 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto fail;
803 rdev->sb_loaded = 1;
804 return 0;
805
806fail:
807 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
808 bdevname(rdev->bdev,b));
809 return -EINVAL;
810}
811
812static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
813{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000814 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000815 sb1->set_uuid1 == sb2->set_uuid1 &&
816 sb1->set_uuid2 == sb2->set_uuid2 &&
817 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
821{
822 int ret;
823 mdp_super_t *tmp1, *tmp2;
824
825 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
826 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
827
828 if (!tmp1 || !tmp2) {
829 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100830 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto abort;
832 }
833
834 *tmp1 = *sb1;
835 *tmp2 = *sb2;
836
837 /*
838 * nr_disks is not constant
839 */
840 tmp1->nr_disks = 0;
841 tmp2->nr_disks = 0;
842
Andre Nollce0c8e02008-07-11 22:02:20 +1000843 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700845 kfree(tmp1);
846 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return ret;
848}
849
NeilBrown4d167f02007-05-09 02:35:37 -0700850static u32 md_csum_fold(u32 csum)
851{
852 csum = (csum & 0xffff) + (csum >> 16);
853 return (csum & 0xffff) + (csum >> 16);
854}
855
NeilBrownf72ffdd2014-09-30 14:23:59 +1000856static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
NeilBrown4d167f02007-05-09 02:35:37 -0700858 u64 newcsum = 0;
859 u32 *sb32 = (u32*)sb;
860 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned int disk_csum, csum;
862
863 disk_csum = sb->sb_csum;
864 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700865
866 for (i = 0; i < MD_SB_BYTES/4 ; i++)
867 newcsum += sb32[i];
868 csum = (newcsum & 0xffffffff) + (newcsum>>32);
869
NeilBrown4d167f02007-05-09 02:35:37 -0700870#ifdef CONFIG_ALPHA
871 /* This used to use csum_partial, which was wrong for several
872 * reasons including that different results are returned on
873 * different architectures. It isn't critical that we get exactly
874 * the same return value as before (we always csum_fold before
875 * testing, and that removes any differences). However as we
876 * know that csum_partial always returned a 16bit value on
877 * alphas, do a fold to maximise conformity to previous behaviour.
878 */
879 sb->sb_csum = md_csum_fold(disk_csum);
880#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700882#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return csum;
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/*
887 * Handle superblock details.
888 * We want to be able to handle multiple superblock formats
889 * so we have a common interface to them all, and an array of
890 * different handlers.
891 * We rely on user-space to write the initial superblock, and support
892 * reading and updating of superblocks.
893 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100894 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * loads and validates a superblock on dev.
896 * if refdev != NULL, compare superblocks on both devices
897 * Return:
898 * 0 - dev has a superblock that is compatible with refdev
899 * 1 - dev has a superblock that is compatible and newer than refdev
900 * so dev should be used as the refdev in future
901 * -EINVAL superblock incompatible or invalid
902 * -othererror e.g. -EIO
903 *
NeilBrownfd01b882011-10-11 16:47:53 +1100904 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * Verify that dev is acceptable into mddev.
906 * The first time, mddev->raid_disks will be 0, and data from
907 * dev should be merged in. Subsequent calls check that dev
908 * is new enough. Return 0 or -EINVAL
909 *
NeilBrownfd01b882011-10-11 16:47:53 +1100910 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * Update the superblock for rdev with data in mddev
912 * This does not write to disc.
913 *
914 */
915
916struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000917 char *name;
918 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000919 int (*load_super)(struct md_rdev *rdev,
920 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000921 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000922 int (*validate_super)(struct mddev *mddev,
923 struct md_rdev *rdev);
924 void (*sync_super)(struct mddev *mddev,
925 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100926 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000927 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000928 int (*allow_new_offset)(struct md_rdev *rdev,
929 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930};
931
932/*
Andre Noll0894cc32009-06-18 08:49:23 +1000933 * Check that the given mddev has no bitmap.
934 *
935 * This function is called from the run method of all personalities that do not
936 * support bitmaps. It prints an error message and returns non-zero if mddev
937 * has a bitmap. Otherwise, it returns 0.
938 *
939 */
NeilBrownfd01b882011-10-11 16:47:53 +1100940int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000941{
NeilBrownc3d97142009-12-14 12:49:52 +1100942 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000943 return 0;
944 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
945 mdname(mddev), mddev->pers->name);
946 return 1;
947}
948EXPORT_SYMBOL(md_check_no_bitmap);
949
950/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000951 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
NeilBrown3cb03002011-10-11 16:45:26 +1100953static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
956 mdp_super_t *sb;
957 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /*
Andre Noll0f420352008-07-11 22:02:23 +1000960 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * it's at the end of the disk.
962 *
963 * It also happens to be a multiple of 4Kb.
964 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100965 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
NeilBrown0002b272005-09-09 16:23:53 -0700967 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (ret) return ret;
969
970 ret = -EINVAL;
971
972 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000973 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (sb->md_magic != MD_SB_MAGIC) {
976 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
977 b);
978 goto abort;
979 }
980
981 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800982 sb->minor_version < 90 ||
983 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
985 sb->major_version, sb->minor_version,
986 b);
987 goto abort;
988 }
989
990 if (sb->raid_disks <= 0)
991 goto abort;
992
NeilBrown4d167f02007-05-09 02:35:37 -0700993 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
995 b);
996 goto abort;
997 }
998
999 rdev->preferred_minor = sb->md_minor;
1000 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001001 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001002 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001003 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 if (sb->level == LEVEL_MULTIPATH)
1006 rdev->desc_nr = -1;
1007 else
1008 rdev->desc_nr = sb->this_disk.number;
1009
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001010 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001012 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001014 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!uuid_equal(refsb, sb)) {
1016 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1017 b, bdevname(refdev->bdev,b2));
1018 goto abort;
1019 }
1020 if (!sb_equal(refsb, sb)) {
1021 printk(KERN_WARNING "md: %s has same UUID"
1022 " but different superblock to %s\n",
1023 b, bdevname(refdev->bdev, b2));
1024 goto abort;
1025 }
1026 ev1 = md_event(sb);
1027 ev2 = md_event(refsb);
1028 if (ev1 > ev2)
1029 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001030 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 ret = 0;
1032 }
NeilBrown8190e752009-06-18 08:48:58 +10001033 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001034 /* Limit to 4TB as metadata cannot record more than that.
1035 * (not needed for Linear and RAID0 as metadata doesn't
1036 * record this size)
1037 */
1038 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001039 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
NeilBrown27a7b262011-09-10 17:21:28 +10001041 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001042 /* "this cannot possibly happen" ... */
1043 ret = -EINVAL;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 abort:
1046 return ret;
1047}
1048
1049/*
1050 * validate_super for 0.90.0
1051 */
NeilBrownfd01b882011-10-11 16:47:53 +11001052static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001055 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001056 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
NeilBrown41158c72005-06-21 17:17:25 -07001058 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001059 clear_bit(Faulty, &rdev->flags);
1060 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001061 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001062 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (mddev->raid_disks == 0) {
1065 mddev->major_version = 0;
1066 mddev->minor_version = sb->minor_version;
1067 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001068 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001069 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 mddev->ctime = sb->ctime;
1071 mddev->utime = sb->utime;
1072 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001073 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 mddev->layout = sb->layout;
1075 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001076 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001077 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001078 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001079 mddev->bitmap_info.space = 0;
1080 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001081 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001082 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001083 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
NeilBrownf6705572006-03-27 01:18:11 -08001085 if (mddev->minor_version >= 91) {
1086 mddev->reshape_position = sb->reshape_position;
1087 mddev->delta_disks = sb->delta_disks;
1088 mddev->new_level = sb->new_level;
1089 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001090 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001091 if (mddev->delta_disks < 0)
1092 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001093 } else {
1094 mddev->reshape_position = MaxSector;
1095 mddev->delta_disks = 0;
1096 mddev->new_level = mddev->level;
1097 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001098 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001099 }
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (sb->state & (1<<MD_SB_CLEAN))
1102 mddev->recovery_cp = MaxSector;
1103 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001104 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 sb->events_lo == sb->cp_events_lo) {
1106 mddev->recovery_cp = sb->recovery_cp;
1107 } else
1108 mddev->recovery_cp = 0;
1109 }
1110
1111 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1112 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1113 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1114 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1115
1116 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001117
1118 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001119 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001120 mddev->bitmap_info.offset =
1121 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001122 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001123 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001124 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001125
NeilBrown41158c72005-06-21 17:17:25 -07001126 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001127 /* Insist on good event counter while assembling, except
1128 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001130 if (sb->disks[rdev->desc_nr].state & (
1131 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001132 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001133 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001134 } else if (mddev->bitmap) {
1135 /* if adding to array with a bitmap, then we can accept an
1136 * older device ... but not too old.
1137 */
NeilBrown41158c72005-06-21 17:17:25 -07001138 if (ev1 < mddev->bitmap->events_cleared)
1139 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001140 if (ev1 < mddev->events)
1141 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001142 } else {
1143 if (ev1 < mddev->events)
1144 /* just a hot-add of a new device, leave raid_disk at -1 */
1145 return 0;
1146 }
NeilBrown41158c72005-06-21 17:17:25 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 desc = sb->disks + rdev->desc_nr;
1150
1151 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001152 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001153 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1154 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001155 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001157 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001158 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1159 /* active but not in sync implies recovery up to
1160 * reshape position. We don't know exactly where
1161 * that is, so set to zero for now */
1162 if (mddev->minor_version >= 91) {
1163 rdev->recovery_offset = 0;
1164 rdev->raid_disk = desc->raid_disk;
1165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001167 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1168 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001169 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001170 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 0;
1172}
1173
1174/*
1175 * sync_super for 0.90.0
1176 */
NeilBrownfd01b882011-10-11 16:47:53 +11001177static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001180 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* make rdev->sb match mddev data..
1184 *
1185 * 1/ zero out disks
1186 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1187 * 3/ any empty disks < next_spare become removed
1188 *
1189 * disks[0] gets initialised to REMOVED because
1190 * we cannot be sure from other fields if it has
1191 * been initialised or not.
1192 */
1193 int i;
1194 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1195
NeilBrown61181562005-09-09 16:24:02 -07001196 rdev->sb_size = MD_SB_BYTES;
1197
Namhyung Kim65a06f062011-07-27 11:00:36 +10001198 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 memset(sb, 0, sizeof(*sb));
1201
1202 sb->md_magic = MD_SB_MAGIC;
1203 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 sb->patch_version = mddev->patch_version;
1205 sb->gvalid_words = 0; /* ignored */
1206 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1207 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1208 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1209 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1210
1211 sb->ctime = mddev->ctime;
1212 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001213 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 sb->raid_disks = mddev->raid_disks;
1215 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001216 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 sb->utime = mddev->utime;
1218 sb->state = 0;
1219 sb->events_hi = (mddev->events>>32);
1220 sb->events_lo = (u32)mddev->events;
1221
NeilBrownf6705572006-03-27 01:18:11 -08001222 if (mddev->reshape_position == MaxSector)
1223 sb->minor_version = 90;
1224 else {
1225 sb->minor_version = 91;
1226 sb->reshape_position = mddev->reshape_position;
1227 sb->new_level = mddev->new_level;
1228 sb->delta_disks = mddev->delta_disks;
1229 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001230 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001231 }
1232 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (mddev->in_sync)
1234 {
1235 sb->recovery_cp = mddev->recovery_cp;
1236 sb->cp_events_hi = (mddev->events>>32);
1237 sb->cp_events_lo = (u32)mddev->events;
1238 if (mddev->recovery_cp == MaxSector)
1239 sb->state = (1<< MD_SB_CLEAN);
1240 } else
1241 sb->recovery_cp = 0;
1242
1243 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001244 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
NeilBrownc3d97142009-12-14 12:49:52 +11001246 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001247 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001250 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001252 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001253 int is_active = test_bit(In_sync, &rdev2->flags);
1254
1255 if (rdev2->raid_disk >= 0 &&
1256 sb->minor_version >= 91)
1257 /* we have nowhere to store the recovery_offset,
1258 * but if it is not below the reshape_position,
1259 * we can piggy-back on that.
1260 */
1261 is_active = 1;
1262 if (rdev2->raid_disk < 0 ||
1263 test_bit(Faulty, &rdev2->flags))
1264 is_active = 0;
1265 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001266 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001268 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001269 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 d = &sb->disks[rdev2->desc_nr];
1271 nr_disks++;
1272 d->number = rdev2->desc_nr;
1273 d->major = MAJOR(rdev2->bdev->bd_dev);
1274 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001275 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 d->raid_disk = rdev2->raid_disk;
1277 else
1278 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001279 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001281 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001283 if (test_bit(In_sync, &rdev2->flags))
1284 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 active++;
1286 working++;
1287 } else {
1288 d->state = 0;
1289 spare++;
1290 working++;
1291 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001292 if (test_bit(WriteMostly, &rdev2->flags))
1293 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* now set the "removed" and "faulty" bits on any missing devices */
1296 for (i=0 ; i < mddev->raid_disks ; i++) {
1297 mdp_disk_t *d = &sb->disks[i];
1298 if (d->state == 0 && d->number == 0) {
1299 d->number = i;
1300 d->raid_disk = i;
1301 d->state = (1<<MD_DISK_REMOVED);
1302 d->state |= (1<<MD_DISK_FAULTY);
1303 failed++;
1304 }
1305 }
1306 sb->nr_disks = nr_disks;
1307 sb->active_disks = active;
1308 sb->working_disks = working;
1309 sb->failed_disks = failed;
1310 sb->spare_disks = spare;
1311
1312 sb->this_disk = sb->disks[rdev->desc_nr];
1313 sb->sb_csum = calc_sb_csum(sb);
1314}
1315
1316/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001317 * rdev_size_change for 0.90.0
1318 */
1319static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001320super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001321{
Andre Noll58c0fed2009-03-31 14:33:13 +11001322 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001323 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001324 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001326 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001327 if (!num_sectors || num_sectors > rdev->sb_start)
1328 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001329 /* Limit to 4TB as metadata cannot record more than that.
1330 * 4TB == 2^32 KB, or 2*2^32 sectors.
1331 */
NeilBrown667a5312012-08-16 16:46:12 +10001332 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001333 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001334 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001335 rdev->sb_page);
1336 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001337 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338}
1339
NeilBrownc6563a82012-05-21 09:27:00 +10001340static int
1341super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1342{
1343 /* non-zero offset changes not possible with v0.90 */
1344 return new_offset == 0;
1345}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001346
1347/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * version 1 superblock
1349 */
1350
NeilBrownf72ffdd2014-09-30 14:23:59 +10001351static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001353 __le32 disk_csum;
1354 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 unsigned long long newcsum;
1356 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001357 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 disk_csum = sb->sb_csum;
1360 sb->sb_csum = 0;
1361 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001362 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 newcsum += le32_to_cpu(*isuper++);
1364
1365 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001366 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1369 sb->sb_csum = disk_csum;
1370 return cpu_to_le32(csum);
1371}
1372
NeilBrown2699b672011-07-28 11:31:47 +10001373static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1374 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001375static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct mdp_superblock_1 *sb;
1378 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001379 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001380 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001382 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /*
Andre Noll0f420352008-07-11 22:02:23 +10001385 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * It is always aligned to a 4K boundary and
1387 * depeding on minor_version, it can be:
1388 * 0: At least 8K, but less than 12K, from end of device
1389 * 1: At start of device
1390 * 2: 4K from start of device.
1391 */
1392 switch(minor_version) {
1393 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001394 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001395 sb_start -= 8*2;
1396 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 break;
1398 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001399 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 break;
1401 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001402 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 break;
1404 default:
1405 return -EINVAL;
1406 }
Andre Noll0f420352008-07-11 22:02:23 +10001407 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
NeilBrown0002b272005-09-09 16:23:53 -07001409 /* superblock is rarely larger than 1K, but it can be larger,
1410 * and it is safe to read 4k, so we do that
1411 */
1412 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (ret) return ret;
1414
Namhyung Kim65a06f062011-07-27 11:00:36 +10001415 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1418 sb->major_version != cpu_to_le32(1) ||
1419 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001420 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001421 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return -EINVAL;
1423
1424 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1425 printk("md: invalid superblock checksum on %s\n",
1426 bdevname(rdev->bdev,b));
1427 return -EINVAL;
1428 }
1429 if (le64_to_cpu(sb->data_size) < 10) {
1430 printk("md: data_size too small on %s\n",
1431 bdevname(rdev->bdev,b));
1432 return -EINVAL;
1433 }
NeilBrownc6563a82012-05-21 09:27:00 +10001434 if (sb->pad0 ||
1435 sb->pad3[0] ||
1436 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1437 /* Some padding is non-zero, might be a new feature */
1438 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 rdev->preferred_minor = 0xffff;
1441 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001442 rdev->new_data_offset = rdev->data_offset;
1443 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1444 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1445 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001446 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
NeilBrown0002b272005-09-09 16:23:53 -07001448 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001449 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001450 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001451 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1452
1453 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001454 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001455 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001456 if (minor_version
1457 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1458 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001459
NeilBrown31b65a02006-07-10 04:44:14 -07001460 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1461 rdev->desc_nr = -1;
1462 else
1463 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1464
NeilBrown2699b672011-07-28 11:31:47 +10001465 if (!rdev->bb_page) {
1466 rdev->bb_page = alloc_page(GFP_KERNEL);
1467 if (!rdev->bb_page)
1468 return -ENOMEM;
1469 }
1470 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1471 rdev->badblocks.count == 0) {
1472 /* need to load the bad block list.
1473 * Currently we limit it to one page.
1474 */
1475 s32 offset;
1476 sector_t bb_sector;
1477 u64 *bbp;
1478 int i;
1479 int sectors = le16_to_cpu(sb->bblog_size);
1480 if (sectors > (PAGE_SIZE / 512))
1481 return -EINVAL;
1482 offset = le32_to_cpu(sb->bblog_offset);
1483 if (offset == 0)
1484 return -EINVAL;
1485 bb_sector = (long long)offset;
1486 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1487 rdev->bb_page, READ, true))
1488 return -EIO;
1489 bbp = (u64 *)page_address(rdev->bb_page);
1490 rdev->badblocks.shift = sb->bblog_shift;
1491 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1492 u64 bb = le64_to_cpu(*bbp);
1493 int count = bb & (0x3ff);
1494 u64 sector = bb >> 10;
1495 sector <<= sb->bblog_shift;
1496 count <<= sb->bblog_shift;
1497 if (bb + 1 == 0)
1498 break;
1499 if (md_set_badblocks(&rdev->badblocks,
1500 sector, count, 1) == 0)
1501 return -EINVAL;
1502 }
NeilBrown486adf72013-04-24 11:42:44 +10001503 } else if (sb->bblog_offset != 0)
1504 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001505
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001506 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001507 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001508 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001510 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1513 sb->level != refsb->level ||
1514 sb->layout != refsb->layout ||
1515 sb->chunksize != refsb->chunksize) {
1516 printk(KERN_WARNING "md: %s has strangely different"
1517 " superblock to %s\n",
1518 bdevname(rdev->bdev,b),
1519 bdevname(refdev->bdev,b2));
1520 return -EINVAL;
1521 }
1522 ev1 = le64_to_cpu(sb->events);
1523 ev2 = le64_to_cpu(refsb->events);
1524
1525 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001526 ret = 1;
1527 else
1528 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
NeilBrownc6563a82012-05-21 09:27:00 +10001530 if (minor_version) {
1531 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1532 sectors -= rdev->data_offset;
1533 } else
1534 sectors = rdev->sb_start;
1535 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001537 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
NeilBrownfd01b882011-10-11 16:47:53 +11001541static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001543 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001544 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
NeilBrown41158c72005-06-21 17:17:25 -07001546 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001547 clear_bit(Faulty, &rdev->flags);
1548 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001549 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001550 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (mddev->raid_disks == 0) {
1553 mddev->major_version = 1;
1554 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001555 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001556 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1558 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1559 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001560 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 mddev->layout = le32_to_cpu(sb->layout);
1562 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001563 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001564 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001565 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001566 mddev->bitmap_info.space = 0;
1567 /* Default location for bitmap is 1K after superblock
1568 * using 3K - total of 4K
1569 */
NeilBrownc3d97142009-12-14 12:49:52 +11001570 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001571 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001572 mddev->reshape_backwards = 0;
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1575 memcpy(mddev->uuid, sb->set_uuid, 16);
1576
1577 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001578
NeilBrown71c08052005-09-09 16:23:51 -07001579 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001580 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001581 mddev->bitmap_info.offset =
1582 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001583 /* Metadata doesn't record how much space is available.
1584 * For 1.0, we assume we can use up to the superblock
1585 * if before, else to 4K beyond superblock.
1586 * For others, assume no change is possible.
1587 */
1588 if (mddev->minor_version > 0)
1589 mddev->bitmap_info.space = 0;
1590 else if (mddev->bitmap_info.offset > 0)
1591 mddev->bitmap_info.space =
1592 8 - mddev->bitmap_info.offset;
1593 else
1594 mddev->bitmap_info.space =
1595 -mddev->bitmap_info.offset;
1596 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001597
NeilBrownf6705572006-03-27 01:18:11 -08001598 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1599 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1600 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1601 mddev->new_level = le32_to_cpu(sb->new_level);
1602 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001603 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001604 if (mddev->delta_disks < 0 ||
1605 (mddev->delta_disks == 0 &&
1606 (le32_to_cpu(sb->feature_map)
1607 & MD_FEATURE_RESHAPE_BACKWARDS)))
1608 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001609 } else {
1610 mddev->reshape_position = MaxSector;
1611 mddev->delta_disks = 0;
1612 mddev->new_level = mddev->level;
1613 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001614 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001615 }
1616
NeilBrown41158c72005-06-21 17:17:25 -07001617 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001618 /* Insist of good event counter while assembling, except for
1619 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001621 if (rdev->desc_nr >= 0 &&
1622 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1623 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1624 if (ev1 < mddev->events)
1625 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001626 } else if (mddev->bitmap) {
1627 /* If adding to array with a bitmap, then we can accept an
1628 * older device, but not too old.
1629 */
NeilBrown41158c72005-06-21 17:17:25 -07001630 if (ev1 < mddev->bitmap->events_cleared)
1631 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001632 if (ev1 < mddev->events)
1633 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001634 } else {
1635 if (ev1 < mddev->events)
1636 /* just a hot-add of a new device, leave raid_disk at -1 */
1637 return 0;
1638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (mddev->level != LEVEL_MULTIPATH) {
1640 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001641 if (rdev->desc_nr < 0 ||
1642 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1643 role = 0xffff;
1644 rdev->desc_nr = -1;
1645 } else
1646 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 switch(role) {
1648 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 break;
1650 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001651 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 break;
1653 default:
NeilBrownf4667222013-12-09 12:04:56 +11001654 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001655 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001656 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001657 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001658 if (!(le32_to_cpu(sb->feature_map) &
1659 MD_FEATURE_RECOVERY_BITMAP))
1660 rdev->saved_raid_disk = -1;
1661 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001662 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 rdev->raid_disk = role;
1664 break;
1665 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001666 if (sb->devflags & WriteMostly1)
1667 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001668 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1669 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001670 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001671 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return 0;
1674}
1675
NeilBrownfd01b882011-10-11 16:47:53 +11001676static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001679 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 int max_dev, i;
1681 /* make rdev->sb match mddev and rdev data. */
1682
Namhyung Kim65a06f062011-07-27 11:00:36 +10001683 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 sb->feature_map = 0;
1686 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001687 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 memset(sb->pad3, 0, sizeof(sb->pad3));
1689
1690 sb->utime = cpu_to_le64((__u64)mddev->utime);
1691 sb->events = cpu_to_le64(mddev->events);
1692 if (mddev->in_sync)
1693 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1694 else
1695 sb->resync_offset = cpu_to_le64(0);
1696
NeilBrown1c05b4b2006-10-21 10:24:08 -07001697 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001698
NeilBrownf0ca3402006-02-02 14:28:04 -08001699 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001700 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001701 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001702 sb->level = cpu_to_le32(mddev->level);
1703 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001704
NeilBrownaeb9b2112011-08-25 14:43:08 +10001705 if (test_bit(WriteMostly, &rdev->flags))
1706 sb->devflags |= WriteMostly1;
1707 else
1708 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001709 sb->data_offset = cpu_to_le64(rdev->data_offset);
1710 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001711
NeilBrownc3d97142009-12-14 12:49:52 +11001712 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1713 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001714 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001715 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001716
1717 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001718 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001719 sb->feature_map |=
1720 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1721 sb->recovery_offset =
1722 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001723 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1724 sb->feature_map |=
1725 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001726 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001727 if (test_bit(Replacement, &rdev->flags))
1728 sb->feature_map |=
1729 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001730
NeilBrownf6705572006-03-27 01:18:11 -08001731 if (mddev->reshape_position != MaxSector) {
1732 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1733 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1734 sb->new_layout = cpu_to_le32(mddev->new_layout);
1735 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1736 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001737 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001738 if (mddev->delta_disks == 0 &&
1739 mddev->reshape_backwards)
1740 sb->feature_map
1741 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001742 if (rdev->new_data_offset != rdev->data_offset) {
1743 sb->feature_map
1744 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1745 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1746 - rdev->data_offset));
1747 }
NeilBrownf6705572006-03-27 01:18:11 -08001748 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001749
NeilBrown2699b672011-07-28 11:31:47 +10001750 if (rdev->badblocks.count == 0)
1751 /* Nothing to do for bad blocks*/ ;
1752 else if (sb->bblog_offset == 0)
1753 /* Cannot record bad blocks on this device */
1754 md_error(mddev, rdev);
1755 else {
1756 struct badblocks *bb = &rdev->badblocks;
1757 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1758 u64 *p = bb->page;
1759 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1760 if (bb->changed) {
1761 unsigned seq;
1762
1763retry:
1764 seq = read_seqbegin(&bb->lock);
1765
1766 memset(bbp, 0xff, PAGE_SIZE);
1767
1768 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001769 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001770 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1771 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001772 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001773 }
NeilBrownd0962932012-03-19 12:46:41 +11001774 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001775 if (read_seqretry(&bb->lock, seq))
1776 goto retry;
1777
1778 bb->sector = (rdev->sb_start +
1779 (int)le32_to_cpu(sb->bblog_offset));
1780 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001781 }
1782 }
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001785 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (rdev2->desc_nr+1 > max_dev)
1787 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001788
NeilBrown70471da2009-08-03 10:59:57 +10001789 if (max_dev > le32_to_cpu(sb->max_dev)) {
1790 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001791 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001792 rdev->sb_size = max_dev * 2 + 256;
1793 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1794 if (rdev->sb_size & bmask)
1795 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001796 } else
1797 max_dev = le32_to_cpu(sb->max_dev);
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 for (i=0; i<max_dev;i++)
1800 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001801
NeilBrowndafb20f2012-03-19 12:46:39 +11001802 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001804 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001806 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001808 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001809 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 else
1811 sb->dev_roles[i] = cpu_to_le16(0xffff);
1812 }
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 sb->sb_csum = calc_sb_1_csum(sb);
1815}
1816
Chris Webb0cd17fe2008-06-28 08:31:46 +10001817static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001818super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001819{
1820 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001821 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001822 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001823 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001824 if (rdev->data_offset != rdev->new_data_offset)
1825 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001826 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001827 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001828 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001829 max_sectors -= rdev->data_offset;
1830 if (!num_sectors || num_sectors > max_sectors)
1831 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001832 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001833 /* minor version 0 with bitmap we can't move */
1834 return 0;
1835 } else {
1836 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001837 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001838 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001839 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001840 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001841 if (!num_sectors || num_sectors > max_sectors)
1842 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001843 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001844 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001845 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001846 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001847 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001848 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001849 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001850 rdev->sb_page);
1851 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001852 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001853
1854}
1855
1856static int
1857super_1_allow_new_offset(struct md_rdev *rdev,
1858 unsigned long long new_offset)
1859{
1860 /* All necessary checks on new >= old have been done */
1861 struct bitmap *bitmap;
1862 if (new_offset >= rdev->data_offset)
1863 return 1;
1864
1865 /* with 1.0 metadata, there is no metadata to tread on
1866 * so we can always move back */
1867 if (rdev->mddev->minor_version == 0)
1868 return 1;
1869
1870 /* otherwise we must be sure not to step on
1871 * any metadata, so stay:
1872 * 36K beyond start of superblock
1873 * beyond end of badblocks
1874 * beyond write-intent bitmap
1875 */
1876 if (rdev->sb_start + (32+4)*2 > new_offset)
1877 return 0;
1878 bitmap = rdev->mddev->bitmap;
1879 if (bitmap && !rdev->mddev->bitmap_info.file &&
1880 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001881 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001882 return 0;
1883 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1884 return 0;
1885
1886 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Adrian Bunk75c96f82005-05-05 16:16:09 -07001889static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 [0] = {
1891 .name = "0.90.0",
1892 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001893 .load_super = super_90_load,
1894 .validate_super = super_90_validate,
1895 .sync_super = super_90_sync,
1896 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001897 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 },
1899 [1] = {
1900 .name = "md-1",
1901 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001902 .load_super = super_1_load,
1903 .validate_super = super_1_validate,
1904 .sync_super = super_1_sync,
1905 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001906 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 },
1908};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
NeilBrownfd01b882011-10-11 16:47:53 +11001910static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001911{
1912 if (mddev->sync_super) {
1913 mddev->sync_super(mddev, rdev);
1914 return;
1915 }
1916
1917 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1918
1919 super_types[mddev->major_version].sync_super(mddev, rdev);
1920}
1921
NeilBrownfd01b882011-10-11 16:47:53 +11001922static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
NeilBrown3cb03002011-10-11 16:45:26 +11001924 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
NeilBrown4b809912008-07-21 17:05:25 +10001926 rcu_read_lock();
1927 rdev_for_each_rcu(rdev, mddev1)
1928 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001929 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001930 rdev2->bdev->bd_contains) {
1931 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001932 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001933 }
1934 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return 0;
1936}
1937
1938static LIST_HEAD(pending_raid_disks);
1939
Andre Nollac5e7112009-08-03 10:59:47 +10001940/*
1941 * Try to register data integrity profile for an mddev
1942 *
1943 * This is called when an array is started and after a disk has been kicked
1944 * from the array. It only succeeds if all working and active component devices
1945 * are integrity capable with matching profiles.
1946 */
NeilBrownfd01b882011-10-11 16:47:53 +11001947int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001948{
NeilBrown3cb03002011-10-11 16:45:26 +11001949 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001950
Andre Nollac5e7112009-08-03 10:59:47 +10001951 if (list_empty(&mddev->disks))
1952 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001953 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1954 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001955 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001956 /* skip spares and non-functional disks */
1957 if (test_bit(Faulty, &rdev->flags))
1958 continue;
1959 if (rdev->raid_disk < 0)
1960 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001961 if (!reference) {
1962 /* Use the first rdev as the reference */
1963 reference = rdev;
1964 continue;
1965 }
1966 /* does this rdev's profile match the reference profile? */
1967 if (blk_integrity_compare(reference->bdev->bd_disk,
1968 rdev->bdev->bd_disk) < 0)
1969 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001970 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001971 if (!reference || !bdev_get_integrity(reference->bdev))
1972 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001973 /*
1974 * All component devices are integrity capable and have matching
1975 * profiles, register the common profile for the md device.
1976 */
1977 if (blk_integrity_register(mddev->gendisk,
1978 bdev_get_integrity(reference->bdev)) != 0) {
1979 printk(KERN_ERR "md: failed to register integrity for %s\n",
1980 mdname(mddev));
1981 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001982 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001983 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1984 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1985 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1986 mdname(mddev));
1987 return -EINVAL;
1988 }
Andre Nollac5e7112009-08-03 10:59:47 +10001989 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001990}
Andre Nollac5e7112009-08-03 10:59:47 +10001991EXPORT_SYMBOL(md_integrity_register);
1992
1993/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001994void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001995{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001996 struct blk_integrity *bi_rdev;
1997 struct blk_integrity *bi_mddev;
1998
1999 if (!mddev->gendisk)
2000 return;
2001
2002 bi_rdev = bdev_get_integrity(rdev->bdev);
2003 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002004
2005 if (!bi_mddev) /* nothing to do */
2006 return;
2007 if (rdev->raid_disk < 0) /* skip spares */
2008 return;
2009 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2010 rdev->bdev->bd_disk) >= 0)
2011 return;
2012 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2013 blk_integrity_unregister(mddev->gendisk);
2014}
2015EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002016
NeilBrownf72ffdd2014-09-30 14:23:59 +10002017static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002019 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002020 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002021 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002022 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Dan Williams11e2ede2008-04-30 00:52:32 -07002024 /* prevent duplicates */
2025 if (find_rdev(mddev, rdev->bdev->bd_dev))
2026 return -EEXIST;
2027
Andre Nolldd8ac332009-03-31 14:33:13 +11002028 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2029 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2030 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002031 if (mddev->pers) {
2032 /* Cannot change size, so fail
2033 * If mddev->level <= 0, then we don't care
2034 * about aligning sizes (e.g. linear)
2035 */
2036 if (mddev->level > 0)
2037 return -ENOSPC;
2038 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002039 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /* Verify rdev->desc_nr is unique.
2043 * If it is -1, assign a free number, else
2044 * check number is not in use
2045 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002046 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (rdev->desc_nr < 0) {
2048 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002049 if (mddev->pers)
2050 choice = mddev->raid_disks;
2051 while (find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 choice++;
2053 rdev->desc_nr = choice;
2054 } else {
NeilBrown4878e9e2014-09-25 17:00:11 +10002055 if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2056 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002060 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002061 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2062 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2063 mdname(mddev), mddev->max_disks);
2064 return -EBUSY;
2065 }
NeilBrown19133a42005-11-08 21:39:35 -08002066 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002067 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002068 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002071 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002072
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002073 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002074 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002075
Tejun Heo0762b8b2008-08-25 19:56:12 +09002076 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002077 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2078 /* failure here is OK */;
2079 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002080
NeilBrown4b809912008-07-21 17:05:25 +10002081 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002082 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002083
2084 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002085 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002088
2089 fail:
2090 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2091 b, mdname(mddev));
2092 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
NeilBrown177a99b2008-02-06 01:39:56 -08002095static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002096{
NeilBrown3cb03002011-10-11 16:45:26 +11002097 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002098 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002099 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002100}
2101
NeilBrownf72ffdd2014-09-30 14:23:59 +10002102static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002105
Tejun Heo49731ba2011-01-14 18:43:57 +01002106 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002107 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2109 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002110 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002111 sysfs_put(rdev->sysfs_state);
2112 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002113 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002114 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002115 * writing to 'remove' to "dev/state". We also need
2116 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002117 */
NeilBrown4b809912008-07-21 17:05:25 +10002118 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002119 INIT_WORK(&rdev->del_work, md_delayed_delete);
2120 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002121 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
2124/*
2125 * prevent the device from being mounted, repartitioned or
2126 * otherwise reused by a RAID array (or any other kernel
2127 * subsystem), by bd_claiming the device.
2128 */
NeilBrown3cb03002011-10-11 16:45:26 +11002129static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 int err = 0;
2132 struct block_device *bdev;
2133 char b[BDEVNAME_SIZE];
2134
Tejun Heod4d77622010-11-13 11:55:18 +01002135 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002136 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (IS_ERR(bdev)) {
2138 printk(KERN_ERR "md: could not open %s.\n",
2139 __bdevname(dev, b));
2140 return PTR_ERR(bdev);
2141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 rdev->bdev = bdev;
2143 return err;
2144}
2145
NeilBrown3cb03002011-10-11 16:45:26 +11002146static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
2148 struct block_device *bdev = rdev->bdev;
2149 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002150 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
2153void md_autodetect_dev(dev_t dev);
2154
NeilBrownf72ffdd2014-09-30 14:23:59 +10002155static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 printk(KERN_INFO "md: export_rdev(%s)\n",
2160 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002161 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002163 if (test_bit(AutoDetected, &rdev->flags))
2164 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165#endif
2166 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002167 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
NeilBrownf72ffdd2014-09-30 14:23:59 +10002170static void kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
2172 unbind_rdev_from_array(rdev);
2173 export_rdev(rdev);
2174}
2175
NeilBrownfd01b882011-10-11 16:47:53 +11002176static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
NeilBrown0638bb02014-09-25 17:43:47 +10002178 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
NeilBrown0638bb02014-09-25 17:43:47 +10002180 while (!list_empty(&mddev->disks)) {
2181 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2182 same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 kick_rdev_from_array(rdev);
2184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 mddev->raid_disks = 0;
2186 mddev->major_version = 0;
2187}
2188
NeilBrownf72ffdd2014-09-30 14:23:59 +10002189static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
NeilBrown42543762006-06-26 00:27:57 -07002191 /* Update each superblock (in-memory image), but
2192 * if we are allowed to, skip spares which already
2193 * have the right event counter, or have one earlier
2194 * (which would mean they aren't being marked as dirty
2195 * with the rest of the array)
2196 */
NeilBrown3cb03002011-10-11 16:45:26 +11002197 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002198 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002199 if (rdev->sb_events == mddev->events ||
2200 (nospares &&
2201 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002202 rdev->sb_events+1 == mddev->events)) {
2203 /* Don't update this superblock */
2204 rdev->sb_loaded = 2;
2205 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002206 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002207 rdev->sb_loaded = 1;
2208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210}
2211
NeilBrownf72ffdd2014-09-30 14:23:59 +10002212static void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
NeilBrown3cb03002011-10-11 16:45:26 +11002214 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002215 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002216 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002217 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
NeilBrownd87f0642013-04-24 11:42:40 +10002219 if (mddev->ro) {
2220 if (force_change)
2221 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2222 return;
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002225 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002226 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002227 if (rdev->raid_disk >= 0 &&
2228 mddev->delta_disks >= 0 &&
2229 !test_bit(In_sync, &rdev->flags) &&
2230 mddev->curr_resync_completed > rdev->recovery_offset)
2231 rdev->recovery_offset = mddev->curr_resync_completed;
2232
NeilBrownf72ffdd2014-09-30 14:23:59 +10002233 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002234 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002235 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002236 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002237 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002238 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002239 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002240 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002241 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002242 md_ack_all_badblocks(&rdev->badblocks);
2243 md_error(mddev, rdev);
2244 }
2245 clear_bit(Blocked, &rdev->flags);
2246 clear_bit(BlockedBadBlocks, &rdev->flags);
2247 wake_up(&rdev->blocked_wait);
2248 }
2249 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002250 wake_up(&mddev->sb_wait);
2251 return;
2252 }
2253
NeilBrown85572d72014-12-15 12:56:56 +11002254 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002255
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002256 mddev->utime = get_seconds();
2257
NeilBrown850b2b422006-10-03 01:15:46 -07002258 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2259 force_change = 1;
2260 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2261 /* just a clean<-> dirty transition, possibly leave spares alone,
2262 * though if events isn't the right even/odd, we will have to do
2263 * spares after all
2264 */
2265 nospares = 1;
2266 if (force_change)
2267 nospares = 0;
2268 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002269 /* If the array is degraded, then skipping spares is both
2270 * dangerous and fairly pointless.
2271 * Dangerous because a device that was removed from the array
2272 * might have a event_count that still looks up-to-date,
2273 * so it can be re-added without a resync.
2274 * Pointless because if there are any spares to skip,
2275 * then a recovery will happen and soon that array won't
2276 * be degraded any more and the spare can go back to sleep then.
2277 */
NeilBrown850b2b422006-10-03 01:15:46 -07002278 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002279
NeilBrown06d91a52005-06-21 17:17:12 -07002280 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002281
2282 /* If this is just a dirty<->clean transition, and the array is clean
2283 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002284 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002285 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002286 && mddev->can_decrease_events
2287 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002288 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002289 mddev->can_decrease_events = 0;
2290 } else {
NeilBrown42543762006-06-26 00:27:57 -07002291 /* otherwise we have to go forward and ... */
2292 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002293 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
NeilBrown403df472014-09-30 15:52:29 +10002296 /*
2297 * This 64-bit counter should never wrap.
2298 * Either we are in around ~1 trillion A.C., assuming
2299 * 1 reboot per second, or we have a bug...
2300 */
2301 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002302
NeilBrowndafb20f2012-03-19 12:46:39 +11002303 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002304 if (rdev->badblocks.changed)
2305 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002306 if (test_bit(Faulty, &rdev->flags))
2307 set_bit(FaultRecorded, &rdev->flags);
2308 }
NeilBrown2699b672011-07-28 11:31:47 +10002309
NeilBrowne6910632008-02-06 01:39:51 -08002310 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002311 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
NeilBrown36a4e1f2011-10-07 14:23:17 +11002313 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2314 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
NeilBrown4ad13662007-07-17 04:06:13 -07002316 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002317 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002319
NeilBrown42543762006-06-26 00:27:57 -07002320 if (rdev->sb_loaded != 1)
2321 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
NeilBrownf4667222013-12-09 12:04:56 +11002323 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002324 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002325 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002326 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002327 pr_debug("md: (write) %s's sb offset: %llu\n",
2328 bdevname(rdev->bdev, b),
2329 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002330 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002331 if (rdev->badblocks.size) {
2332 md_super_write(mddev, rdev,
2333 rdev->badblocks.sector,
2334 rdev->badblocks.size << 9,
2335 rdev->bb_page);
2336 rdev->badblocks.size = 0;
2337 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002338
NeilBrownf4667222013-12-09 12:04:56 +11002339 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002340 pr_debug("md: %s (skipping faulty)\n",
2341 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002342
NeilBrown7bfa19f2005-06-21 17:17:28 -07002343 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 /* only need to write one superblock... */
2345 break;
2346 }
NeilBrowna9701a32005-11-08 21:39:34 -08002347 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002348 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002349
NeilBrown85572d72014-12-15 12:56:56 +11002350 spin_lock(&mddev->lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002351 if (mddev->in_sync != sync_req ||
2352 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002353 /* have to write it out again */
NeilBrown85572d72014-12-15 12:56:56 +11002354 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002355 goto repeat;
2356 }
NeilBrown850b2b422006-10-03 01:15:46 -07002357 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown85572d72014-12-15 12:56:56 +11002358 spin_unlock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002359 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002360 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2361 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002362
NeilBrowndafb20f2012-03-19 12:46:39 +11002363 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002364 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2365 clear_bit(Blocked, &rdev->flags);
2366
2367 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002368 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002369 clear_bit(BlockedBadBlocks, &rdev->flags);
2370 wake_up(&rdev->blocked_wait);
2371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
Andre Noll7f6ce762008-03-23 18:34:54 +01002374/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002375 * We want to accept with case. For this we use cmd_match.
2376 */
2377static int cmd_match(const char *cmd, const char *str)
2378{
2379 /* See if cmd, written into a sysfs file, matches
2380 * str. They must either be the same, or cmd can
2381 * have a trailing newline
2382 */
2383 while (*cmd && *str && *cmd == *str) {
2384 cmd++;
2385 str++;
2386 }
2387 if (*cmd == '\n')
2388 cmd++;
2389 if (*str || *cmd)
2390 return 0;
2391 return 1;
2392}
2393
NeilBrown86e6ffd2005-11-08 21:39:24 -08002394struct rdev_sysfs_entry {
2395 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002396 ssize_t (*show)(struct md_rdev *, char *);
2397 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002398};
2399
2400static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002401state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002402{
2403 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002404 size_t len = 0;
NeilBrown758bfc82014-12-15 12:56:59 +11002405 unsigned long flags = ACCESS_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002406
NeilBrown758bfc82014-12-15 12:56:59 +11002407 if (test_bit(Faulty, &flags) ||
NeilBrownde393cd2011-07-28 11:31:48 +10002408 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002409 len+= sprintf(page+len, "%sfaulty",sep);
2410 sep = ",";
2411 }
NeilBrown758bfc82014-12-15 12:56:59 +11002412 if (test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002413 len += sprintf(page+len, "%sin_sync",sep);
2414 sep = ",";
2415 }
NeilBrown758bfc82014-12-15 12:56:59 +11002416 if (test_bit(WriteMostly, &flags)) {
NeilBrownf6556752006-06-26 00:28:01 -07002417 len += sprintf(page+len, "%swrite_mostly",sep);
2418 sep = ",";
2419 }
NeilBrown758bfc82014-12-15 12:56:59 +11002420 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002421 (rdev->badblocks.unacked_exist
NeilBrown758bfc82014-12-15 12:56:59 +11002422 && !test_bit(Faulty, &flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002423 len += sprintf(page+len, "%sblocked", sep);
2424 sep = ",";
2425 }
NeilBrown758bfc82014-12-15 12:56:59 +11002426 if (!test_bit(Faulty, &flags) &&
2427 !test_bit(In_sync, &flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002428 len += sprintf(page+len, "%sspare", sep);
2429 sep = ",";
2430 }
NeilBrown758bfc82014-12-15 12:56:59 +11002431 if (test_bit(WriteErrorSeen, &flags)) {
NeilBrownd7a9d442011-07-28 11:31:48 +10002432 len += sprintf(page+len, "%swrite_error", sep);
2433 sep = ",";
2434 }
NeilBrown758bfc82014-12-15 12:56:59 +11002435 if (test_bit(WantReplacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002436 len += sprintf(page+len, "%swant_replacement", sep);
2437 sep = ",";
2438 }
NeilBrown758bfc82014-12-15 12:56:59 +11002439 if (test_bit(Replacement, &flags)) {
NeilBrown2d78f8c2011-12-23 10:17:51 +11002440 len += sprintf(page+len, "%sreplacement", sep);
2441 sep = ",";
2442 }
2443
NeilBrown86e6ffd2005-11-08 21:39:24 -08002444 return len+sprintf(page+len, "\n");
2445}
2446
NeilBrown45dc2de2006-06-26 00:27:58 -07002447static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002448state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002449{
2450 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002451 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002452 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002453 * writemostly - sets write_mostly
2454 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002455 * blocked - sets the Blocked flags
2456 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002457 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002458 * -insync - clear Insync for a device with a slot assigned,
2459 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002460 * write_error - sets WriteErrorSeen
2461 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002462 */
2463 int err = -EINVAL;
2464 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2465 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002466 if (test_bit(Faulty, &rdev->flags))
2467 err = 0;
2468 else
2469 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002470 } else if (cmd_match(buf, "remove")) {
2471 if (rdev->raid_disk >= 0)
2472 err = -EBUSY;
2473 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002474 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002475 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002476 if (mddev->pers)
2477 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002478 md_new_event(mddev);
2479 err = 0;
2480 }
NeilBrownf6556752006-06-26 00:28:01 -07002481 } else if (cmd_match(buf, "writemostly")) {
2482 set_bit(WriteMostly, &rdev->flags);
2483 err = 0;
2484 } else if (cmd_match(buf, "-writemostly")) {
2485 clear_bit(WriteMostly, &rdev->flags);
2486 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002487 } else if (cmd_match(buf, "blocked")) {
2488 set_bit(Blocked, &rdev->flags);
2489 err = 0;
2490 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002491 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002492 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002493 /* metadata handler doesn't understand badblocks,
2494 * so we need to fail the device
2495 */
2496 md_error(rdev->mddev, rdev);
2497 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002498 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002499 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002500 wake_up(&rdev->blocked_wait);
2501 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2502 md_wakeup_thread(rdev->mddev->thread);
2503
2504 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002505 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2506 set_bit(In_sync, &rdev->flags);
2507 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002508 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002509 if (rdev->mddev->pers == NULL) {
2510 clear_bit(In_sync, &rdev->flags);
2511 rdev->saved_raid_disk = rdev->raid_disk;
2512 rdev->raid_disk = -1;
2513 err = 0;
2514 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002515 } else if (cmd_match(buf, "write_error")) {
2516 set_bit(WriteErrorSeen, &rdev->flags);
2517 err = 0;
2518 } else if (cmd_match(buf, "-write_error")) {
2519 clear_bit(WriteErrorSeen, &rdev->flags);
2520 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002521 } else if (cmd_match(buf, "want_replacement")) {
2522 /* Any non-spare device that is not a replacement can
2523 * become want_replacement at any time, but we then need to
2524 * check if recovery is needed.
2525 */
2526 if (rdev->raid_disk >= 0 &&
2527 !test_bit(Replacement, &rdev->flags))
2528 set_bit(WantReplacement, &rdev->flags);
2529 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2530 md_wakeup_thread(rdev->mddev->thread);
2531 err = 0;
2532 } else if (cmd_match(buf, "-want_replacement")) {
2533 /* Clearing 'want_replacement' is always allowed.
2534 * Once replacements starts it is too late though.
2535 */
2536 err = 0;
2537 clear_bit(WantReplacement, &rdev->flags);
2538 } else if (cmd_match(buf, "replacement")) {
2539 /* Can only set a device as a replacement when array has not
2540 * yet been started. Once running, replacement is automatic
2541 * from spares, or by assigning 'slot'.
2542 */
2543 if (rdev->mddev->pers)
2544 err = -EBUSY;
2545 else {
2546 set_bit(Replacement, &rdev->flags);
2547 err = 0;
2548 }
2549 } else if (cmd_match(buf, "-replacement")) {
2550 /* Similarly, can only clear Replacement before start */
2551 if (rdev->mddev->pers)
2552 err = -EBUSY;
2553 else {
2554 clear_bit(Replacement, &rdev->flags);
2555 err = 0;
2556 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002557 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002558 if (!err)
2559 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002560 return err ? err : len;
2561}
NeilBrown80ca3a42006-07-10 04:44:18 -07002562static struct rdev_sysfs_entry rdev_state =
2563__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002564
2565static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002566errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002567{
2568 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2569}
2570
2571static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002572errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002573{
2574 char *e;
2575 unsigned long n = simple_strtoul(buf, &e, 10);
2576 if (*buf && (*e == 0 || *e == '\n')) {
2577 atomic_set(&rdev->corrected_errors, n);
2578 return len;
2579 }
2580 return -EINVAL;
2581}
2582static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002583__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002584
NeilBrown014236d2006-01-06 00:20:55 -08002585static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002586slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002587{
2588 if (rdev->raid_disk < 0)
2589 return sprintf(page, "none\n");
2590 else
2591 return sprintf(page, "%d\n", rdev->raid_disk);
2592}
2593
2594static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002595slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002596{
2597 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002598 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002599 int slot = simple_strtoul(buf, &e, 10);
2600 if (strncmp(buf, "none", 4)==0)
2601 slot = -1;
2602 else if (e==buf || (*e && *e!= '\n'))
2603 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002604 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002605 /* Setting 'slot' on an active array requires also
2606 * updating the 'rd%d' link, and communicating
2607 * with the personality with ->hot_*_disk.
2608 * For now we only support removing
2609 * failed/spare devices. This normally happens automatically,
2610 * but not when the metadata is externally managed.
2611 */
NeilBrownc303da62008-02-06 01:39:51 -08002612 if (rdev->raid_disk == -1)
2613 return -EEXIST;
2614 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002615 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002616 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002617 clear_bit(Blocked, &rdev->flags);
2618 remove_and_add_spares(rdev->mddev, rdev);
2619 if (rdev->raid_disk >= 0)
2620 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002621 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2622 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002623 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002624 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002625 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002626 */
2627
2628 if (rdev->raid_disk != -1)
2629 return -EBUSY;
2630
NeilBrownc6751b22011-02-02 11:57:13 +11002631 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2632 return -EBUSY;
2633
Neil Brown6c2fce22008-06-28 08:31:31 +10002634 if (rdev->mddev->pers->hot_add_disk == NULL)
2635 return -EINVAL;
2636
NeilBrownba1b41b2011-01-14 09:14:34 +11002637 if (slot >= rdev->mddev->raid_disks &&
2638 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2639 return -ENOSPC;
2640
Neil Brown6c2fce22008-06-28 08:31:31 +10002641 rdev->raid_disk = slot;
2642 if (test_bit(In_sync, &rdev->flags))
2643 rdev->saved_raid_disk = slot;
2644 else
2645 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002646 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002647 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002648 err = rdev->mddev->pers->
2649 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002650 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002651 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002652 return err;
Neil Brown52664732008-06-28 08:31:44 +10002653 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002654 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002655 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002656 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002657 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002658 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002659 if (slot >= rdev->mddev->raid_disks &&
2660 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002661 return -ENOSPC;
2662 rdev->raid_disk = slot;
2663 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002664 clear_bit(Faulty, &rdev->flags);
2665 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002666 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002667 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002668 }
NeilBrown014236d2006-01-06 00:20:55 -08002669 return len;
2670}
2671
NeilBrown014236d2006-01-06 00:20:55 -08002672static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002673__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002674
NeilBrown93c8cad2006-01-06 00:20:56 -08002675static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002676offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002677{
Andrew Morton6961ece2006-01-06 00:20:59 -08002678 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002679}
2680
2681static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002682offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002683{
NeilBrownc6563a82012-05-21 09:27:00 +10002684 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002685 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002686 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002687 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002688 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002689 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002690 /* Must set offset before size, so overlap checks
2691 * can be sane */
2692 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002693 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002694 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002695 return len;
2696}
2697
2698static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002699__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002700
NeilBrownc6563a82012-05-21 09:27:00 +10002701static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2702{
2703 return sprintf(page, "%llu\n",
2704 (unsigned long long)rdev->new_data_offset);
2705}
2706
2707static ssize_t new_offset_store(struct md_rdev *rdev,
2708 const char *buf, size_t len)
2709{
2710 unsigned long long new_offset;
2711 struct mddev *mddev = rdev->mddev;
2712
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002713 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002714 return -EINVAL;
2715
NeilBrownf851b602014-12-11 10:02:10 +11002716 if (mddev->sync_thread ||
2717 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002718 return -EBUSY;
2719 if (new_offset == rdev->data_offset)
2720 /* reset is always permitted */
2721 ;
2722 else if (new_offset > rdev->data_offset) {
2723 /* must not push array size beyond rdev_sectors */
2724 if (new_offset - rdev->data_offset
2725 + mddev->dev_sectors > rdev->sectors)
2726 return -E2BIG;
2727 }
2728 /* Metadata worries about other space details. */
2729
2730 /* decreasing the offset is inconsistent with a backwards
2731 * reshape.
2732 */
2733 if (new_offset < rdev->data_offset &&
2734 mddev->reshape_backwards)
2735 return -EINVAL;
2736 /* Increasing offset is inconsistent with forwards
2737 * reshape. reshape_direction should be set to
2738 * 'backwards' first.
2739 */
2740 if (new_offset > rdev->data_offset &&
2741 !mddev->reshape_backwards)
2742 return -EINVAL;
2743
2744 if (mddev->pers && mddev->persistent &&
2745 !super_types[mddev->major_version]
2746 .allow_new_offset(rdev, new_offset))
2747 return -E2BIG;
2748 rdev->new_data_offset = new_offset;
2749 if (new_offset > rdev->data_offset)
2750 mddev->reshape_backwards = 1;
2751 else if (new_offset < rdev->data_offset)
2752 mddev->reshape_backwards = 0;
2753
2754 return len;
2755}
2756static struct rdev_sysfs_entry rdev_new_offset =
2757__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2758
NeilBrown83303b62006-01-06 00:21:06 -08002759static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002760rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002761{
Andre Nolldd8ac332009-03-31 14:33:13 +11002762 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002763}
2764
NeilBrownc5d79ad2008-02-06 01:39:54 -08002765static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2766{
2767 /* check if two start/length pairs overlap */
2768 if (s1+l1 <= s2)
2769 return 0;
2770 if (s2+l2 <= s1)
2771 return 0;
2772 return 1;
2773}
2774
Dan Williamsb522adc2009-03-31 15:00:31 +11002775static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2776{
2777 unsigned long long blocks;
2778 sector_t new;
2779
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002780 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002781 return -EINVAL;
2782
2783 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2784 return -EINVAL; /* sector conversion overflow */
2785
2786 new = blocks * 2;
2787 if (new != blocks * 2)
2788 return -EINVAL; /* unsigned long long to sector_t overflow */
2789
2790 *sectors = new;
2791 return 0;
2792}
2793
NeilBrown83303b62006-01-06 00:21:06 -08002794static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002795rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002796{
NeilBrownfd01b882011-10-11 16:47:53 +11002797 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002798 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002799 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002800
Dan Williamsb522adc2009-03-31 15:00:31 +11002801 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002802 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002803 if (rdev->data_offset != rdev->new_data_offset)
2804 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002805 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002806 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002807 sectors = super_types[my_mddev->major_version].
2808 rdev_size_change(rdev, sectors);
2809 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002810 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002811 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002812 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002813 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002814 if (!my_mddev->pers->resize)
2815 /* Cannot change size for RAID0 or Linear etc */
2816 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002817 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002818 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002819 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002820
Andre Nolldd8ac332009-03-31 14:33:13 +11002821 rdev->sectors = sectors;
2822 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002823 /* Need to check that all other rdevs with the same
2824 * ->bdev do not overlap. 'rcu' is sufficient to walk
2825 * the rdev lists safely.
2826 * This check does not provide a hard guarantee, it
2827 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002828 */
NeilBrownfd01b882011-10-11 16:47:53 +11002829 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002830 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002831 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002832
NeilBrown8b1afc32014-09-29 15:33:20 +10002833 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002834 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002835 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002836
NeilBrowndafb20f2012-03-19 12:46:39 +11002837 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002838 if (rdev->bdev == rdev2->bdev &&
2839 rdev != rdev2 &&
2840 overlaps(rdev->data_offset, rdev->sectors,
2841 rdev2->data_offset,
2842 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002843 overlap = 1;
2844 break;
2845 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002846 if (overlap) {
2847 mddev_put(mddev);
2848 break;
2849 }
2850 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002851 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002852 if (overlap) {
2853 /* Someone else could have slipped in a size
2854 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002855 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002856 * safe, and trust userspace not to race with
2857 * itself
2858 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002859 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002860 return -EBUSY;
2861 }
2862 }
NeilBrown83303b62006-01-06 00:21:06 -08002863 return len;
2864}
2865
2866static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002867__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002868
NeilBrown3cb03002011-10-11 16:45:26 +11002869static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002870{
2871 unsigned long long recovery_start = rdev->recovery_offset;
2872
2873 if (test_bit(In_sync, &rdev->flags) ||
2874 recovery_start == MaxSector)
2875 return sprintf(page, "none\n");
2876
2877 return sprintf(page, "%llu\n", recovery_start);
2878}
2879
NeilBrown3cb03002011-10-11 16:45:26 +11002880static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002881{
2882 unsigned long long recovery_start;
2883
2884 if (cmd_match(buf, "none"))
2885 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002886 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002887 return -EINVAL;
2888
2889 if (rdev->mddev->pers &&
2890 rdev->raid_disk >= 0)
2891 return -EBUSY;
2892
2893 rdev->recovery_offset = recovery_start;
2894 if (recovery_start == MaxSector)
2895 set_bit(In_sync, &rdev->flags);
2896 else
2897 clear_bit(In_sync, &rdev->flags);
2898 return len;
2899}
2900
2901static struct rdev_sysfs_entry rdev_recovery_start =
2902__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2903
NeilBrown16c791a2011-07-28 11:31:47 +10002904static ssize_t
2905badblocks_show(struct badblocks *bb, char *page, int unack);
2906static ssize_t
2907badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2908
NeilBrown3cb03002011-10-11 16:45:26 +11002909static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002910{
2911 return badblocks_show(&rdev->badblocks, page, 0);
2912}
NeilBrown3cb03002011-10-11 16:45:26 +11002913static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002914{
NeilBrownde393cd2011-07-28 11:31:48 +10002915 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2916 /* Maybe that ack was all we needed */
2917 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2918 wake_up(&rdev->blocked_wait);
2919 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002920}
2921static struct rdev_sysfs_entry rdev_bad_blocks =
2922__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2923
NeilBrown3cb03002011-10-11 16:45:26 +11002924static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002925{
2926 return badblocks_show(&rdev->badblocks, page, 1);
2927}
NeilBrown3cb03002011-10-11 16:45:26 +11002928static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002929{
2930 return badblocks_store(&rdev->badblocks, page, len, 1);
2931}
2932static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2933__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2934
NeilBrown86e6ffd2005-11-08 21:39:24 -08002935static struct attribute *rdev_default_attrs[] = {
2936 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002937 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002938 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002939 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10002940 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002941 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002942 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002943 &rdev_bad_blocks.attr,
2944 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002945 NULL,
2946};
2947static ssize_t
2948rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2949{
2950 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002951 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002952
2953 if (!entry->show)
2954 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11002955 if (!rdev->mddev)
2956 return -EBUSY;
2957 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002958}
2959
2960static ssize_t
2961rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2962 const char *page, size_t length)
2963{
2964 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002965 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002966 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11002967 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002968
2969 if (!entry->store)
2970 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002971 if (!capable(CAP_SYS_ADMIN))
2972 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002973 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002974 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002975 if (rdev->mddev == NULL)
2976 rv = -EBUSY;
2977 else
2978 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002979 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002980 }
2981 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002982}
2983
2984static void rdev_free(struct kobject *ko)
2985{
NeilBrown3cb03002011-10-11 16:45:26 +11002986 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002987 kfree(rdev);
2988}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002989static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002990 .show = rdev_attr_show,
2991 .store = rdev_attr_store,
2992};
2993static struct kobj_type rdev_ktype = {
2994 .release = rdev_free,
2995 .sysfs_ops = &rdev_sysfs_ops,
2996 .default_attrs = rdev_default_attrs,
2997};
2998
NeilBrown3cb03002011-10-11 16:45:26 +11002999int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003000{
3001 rdev->desc_nr = -1;
3002 rdev->saved_raid_disk = -1;
3003 rdev->raid_disk = -1;
3004 rdev->flags = 0;
3005 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003006 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003007 rdev->sb_events = 0;
3008 rdev->last_read_error.tv_sec = 0;
3009 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003010 rdev->sb_loaded = 0;
3011 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003012 atomic_set(&rdev->nr_pending, 0);
3013 atomic_set(&rdev->read_errors, 0);
3014 atomic_set(&rdev->corrected_errors, 0);
3015
3016 INIT_LIST_HEAD(&rdev->same_set);
3017 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003018
3019 /* Add space to store bad block list.
3020 * This reserves the space even on arrays where it cannot
3021 * be used - I wonder if that matters
3022 */
3023 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003024 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003025 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3026 seqlock_init(&rdev->badblocks.lock);
3027 if (rdev->badblocks.page == NULL)
3028 return -ENOMEM;
3029
3030 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003031}
3032EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033/*
3034 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3035 *
3036 * mark the device faulty if:
3037 *
3038 * - the device is nonexistent (zero size)
3039 * - the device has no valid superblock
3040 *
3041 * a faulty rdev _never_ has rdev->sb set.
3042 */
NeilBrown3cb03002011-10-11 16:45:26 +11003043static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
3045 char b[BDEVNAME_SIZE];
3046 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003047 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 sector_t size;
3049
NeilBrown9ffae0c2006-01-06 00:20:32 -08003050 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (!rdev) {
3052 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3053 return ERR_PTR(-ENOMEM);
3054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
NeilBrown2230dfe2011-07-28 11:31:46 +10003056 err = md_rdev_init(rdev);
3057 if (err)
3058 goto abort_free;
3059 err = alloc_disk_sb(rdev);
3060 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 goto abort_free;
3062
NeilBrownc5d79ad2008-02-06 01:39:54 -08003063 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 if (err)
3065 goto abort_free;
3066
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003067 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003068
Mike Snitzer77304d22010-11-08 14:39:12 +01003069 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003071 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 "md: %s has zero or unknown size, marking faulty!\n",
3073 bdevname(rdev->bdev,b));
3074 err = -EINVAL;
3075 goto abort_free;
3076 }
3077
3078 if (super_format >= 0) {
3079 err = super_types[super_format].
3080 load_super(rdev, NULL, super_minor);
3081 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003082 printk(KERN_WARNING
3083 "md: %s does not have a valid v%d.%d "
3084 "superblock, not importing!\n",
3085 bdevname(rdev->bdev,b),
3086 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 goto abort_free;
3088 }
3089 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003090 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 "md: could not read %s's sb, not importing!\n",
3092 bdevname(rdev->bdev,b));
3093 goto abort_free;
3094 }
3095 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return rdev;
3098
3099abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003100 if (rdev->bdev)
3101 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003102 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 kfree(rdev);
3104 return ERR_PTR(err);
3105}
3106
3107/*
3108 * Check a full RAID array for plausibility
3109 */
3110
NeilBrownf72ffdd2014-09-30 14:23:59 +10003111static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
3113 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003114 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 char b[BDEVNAME_SIZE];
3116
3117 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003118 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 switch (super_types[mddev->major_version].
3120 load_super(rdev, freshest, mddev->minor_version)) {
3121 case 1:
3122 freshest = rdev;
3123 break;
3124 case 0:
3125 break;
3126 default:
3127 printk( KERN_ERR \
3128 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003129 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 bdevname(rdev->bdev,b));
3131 kick_rdev_from_array(rdev);
3132 }
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 super_types[mddev->major_version].
3135 validate_super(mddev, freshest);
3136
3137 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003138 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003139 if (mddev->max_disks &&
3140 (rdev->desc_nr >= mddev->max_disks ||
3141 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003142 printk(KERN_WARNING
3143 "md: %s: %s: only %d devices permitted\n",
3144 mdname(mddev), bdevname(rdev->bdev, b),
3145 mddev->max_disks);
3146 kick_rdev_from_array(rdev);
3147 continue;
3148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 if (rdev != freshest)
3150 if (super_types[mddev->major_version].
3151 validate_super(mddev, rdev)) {
3152 printk(KERN_WARNING "md: kicking non-fresh %s"
3153 " from array!\n",
3154 bdevname(rdev->bdev,b));
3155 kick_rdev_from_array(rdev);
3156 continue;
3157 }
3158 if (mddev->level == LEVEL_MULTIPATH) {
3159 rdev->desc_nr = i++;
3160 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003161 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003162 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003163 rdev->raid_disk = -1;
3164 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 }
3166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
NeilBrown72e02072009-12-14 12:49:55 +11003169/* Read a fixed-point number.
3170 * Numbers in sysfs attributes should be in "standard" units where
3171 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003172 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003173 * milliseconds or jiffies.
3174 * This function takes a decimal number with a possible fractional
3175 * component, and produces an integer which is the result of
3176 * multiplying that number by 10^'scale'.
3177 * all without any floating-point arithmetic.
3178 */
3179int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3180{
3181 unsigned long result = 0;
3182 long decimals = -1;
3183 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3184 if (*cp == '.')
3185 decimals = 0;
3186 else if (decimals < scale) {
3187 unsigned int value;
3188 value = *cp - '0';
3189 result = result * 10 + value;
3190 if (decimals >= 0)
3191 decimals++;
3192 }
3193 cp++;
3194 }
3195 if (*cp == '\n')
3196 cp++;
3197 if (*cp)
3198 return -EINVAL;
3199 if (decimals < 0)
3200 decimals = 0;
3201 while (decimals < scale) {
3202 result *= 10;
3203 decimals ++;
3204 }
3205 *res = result;
3206 return 0;
3207}
3208
NeilBrown19052c02008-08-05 15:54:13 +10003209static void md_safemode_timeout(unsigned long data);
3210
NeilBrowneae17012005-11-08 21:39:23 -08003211static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003212safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003213{
3214 int msec = (mddev->safemode_delay*1000)/HZ;
3215 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3216}
3217static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003218safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003219{
NeilBrown16f17b32006-06-26 00:27:37 -07003220 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003221
NeilBrown72e02072009-12-14 12:49:55 +11003222 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003223 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003224 if (msec == 0)
3225 mddev->safemode_delay = 0;
3226 else {
NeilBrown19052c02008-08-05 15:54:13 +10003227 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003228 unsigned long new_delay = (msec*HZ)/1000;
3229
3230 if (new_delay == 0)
3231 new_delay = 1;
3232 mddev->safemode_delay = new_delay;
3233 if (new_delay < old_delay || old_delay == 0)
3234 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003235 }
3236 return len;
3237}
3238static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003239__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003240
3241static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003242level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003243{
NeilBrown36d091f2014-12-15 12:56:58 +11003244 struct md_personality *p;
3245 int ret;
3246 spin_lock(&mddev->lock);
3247 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003248 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003249 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003250 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003251 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003252 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003253 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003254 else
NeilBrown36d091f2014-12-15 12:56:58 +11003255 ret = 0;
3256 spin_unlock(&mddev->lock);
3257 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003258}
3259
NeilBrownd9d166c2006-01-06 00:20:51 -08003260static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003261level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003262{
Dan Williamsf2859af2010-05-02 10:04:16 -07003263 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003264 ssize_t rv;
3265 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003266 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003267 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003268 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003269 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003270
NeilBrown67918752014-12-15 12:57:01 +11003271 if (slen == 0 || slen >= sizeof(clevel))
3272 return -EINVAL;
3273
3274 rv = mddev_lock(mddev);
3275 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003276 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003277
3278 if (mddev->pers == NULL) {
3279 strncpy(mddev->clevel, buf, slen);
3280 if (mddev->clevel[slen-1] == '\n')
3281 slen--;
3282 mddev->clevel[slen] = 0;
3283 mddev->level = LEVEL_NONE;
3284 rv = len;
3285 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003286 }
NeilBrown67918752014-12-15 12:57:01 +11003287 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003288 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003289 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003290
3291 /* request to change the personality. Need to ensure:
3292 * - array is not engaged in resync/recovery/reshape
3293 * - old personality can be suspended
3294 * - new personality will access other array.
3295 */
3296
NeilBrown67918752014-12-15 12:57:01 +11003297 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003298 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003299 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003300 mddev->reshape_position != MaxSector ||
3301 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003302 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003303
NeilBrown67918752014-12-15 12:57:01 +11003304 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003305 if (!mddev->pers->quiesce) {
3306 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3307 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003308 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003309 }
3310
3311 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003312 strncpy(clevel, buf, slen);
3313 if (clevel[slen-1] == '\n')
3314 slen--;
3315 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003316 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003317 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003318
Dan Williamsf2859af2010-05-02 10:04:16 -07003319 if (request_module("md-%s", clevel) != 0)
3320 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003321 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003322 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003323 if (!pers || !try_module_get(pers->owner)) {
3324 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003325 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003326 rv = -EINVAL;
3327 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003328 }
3329 spin_unlock(&pers_lock);
3330
3331 if (pers == mddev->pers) {
3332 /* Nothing to do! */
3333 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003334 rv = len;
3335 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003336 }
3337 if (!pers->takeover) {
3338 module_put(pers->owner);
3339 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003340 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003341 rv = -EINVAL;
3342 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003343 }
3344
NeilBrowndafb20f2012-03-19 12:46:39 +11003345 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003346 rdev->new_raid_disk = rdev->raid_disk;
3347
NeilBrown245f46c2009-03-31 14:39:39 +11003348 /* ->takeover must set new_* and/or delta_disks
3349 * if it succeeds, and may set them when it fails.
3350 */
3351 priv = pers->takeover(mddev);
3352 if (IS_ERR(priv)) {
3353 mddev->new_level = mddev->level;
3354 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003355 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003356 mddev->raid_disks -= mddev->delta_disks;
3357 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003358 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003359 module_put(pers->owner);
3360 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003361 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003362 rv = PTR_ERR(priv);
3363 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003364 }
3365
3366 /* Looks like we have a winner */
3367 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003368 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003369
3370 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003371 oldpers = mddev->pers;
3372 oldpriv = mddev->private;
3373 mddev->pers = pers;
3374 mddev->private = priv;
3375 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3376 mddev->level = mddev->new_level;
3377 mddev->layout = mddev->new_layout;
3378 mddev->chunk_sectors = mddev->new_chunk_sectors;
3379 mddev->delta_disks = 0;
3380 mddev->reshape_backwards = 0;
3381 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003382 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003383
NeilBrowndb721d32014-12-15 12:56:58 +11003384 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003385 mddev->external) {
3386 /* We are converting from a no-redundancy array
3387 * to a redundancy array and metadata is managed
3388 * externally so we need to be sure that writes
3389 * won't block due to a need to transition
3390 * clean->dirty
3391 * until external management is started.
3392 */
3393 mddev->in_sync = 0;
3394 mddev->safemode_delay = 0;
3395 mddev->safemode = 0;
3396 }
3397
NeilBrowndb721d32014-12-15 12:56:58 +11003398 oldpers->free(mddev, oldpriv);
3399
3400 if (oldpers->sync_request == NULL &&
3401 pers->sync_request != NULL) {
3402 /* need to add the md_redundancy_group */
3403 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3404 printk(KERN_WARNING
3405 "md: cannot register extra attributes for %s\n",
3406 mdname(mddev));
3407 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3408 }
3409 if (oldpers->sync_request != NULL &&
3410 pers->sync_request == NULL) {
3411 /* need to remove the md_redundancy_group */
3412 if (mddev->to_remove == NULL)
3413 mddev->to_remove = &md_redundancy_group;
3414 }
3415
NeilBrowndafb20f2012-03-19 12:46:39 +11003416 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003417 if (rdev->raid_disk < 0)
3418 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003419 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003420 rdev->new_raid_disk = -1;
3421 if (rdev->new_raid_disk == rdev->raid_disk)
3422 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003423 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003424 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003425 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003426 if (rdev->raid_disk < 0)
3427 continue;
3428 if (rdev->new_raid_disk == rdev->raid_disk)
3429 continue;
3430 rdev->raid_disk = rdev->new_raid_disk;
3431 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003432 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003433 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003434 if (sysfs_link_rdev(mddev, rdev))
3435 printk(KERN_WARNING "md: cannot register rd%d"
3436 " for %s after level change\n",
3437 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003438 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003439 }
3440
NeilBrowndb721d32014-12-15 12:56:58 +11003441 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003442 /* this is now an array without redundancy, so
3443 * it must always be in_sync
3444 */
3445 mddev->in_sync = 1;
3446 del_timer_sync(&mddev->safemode_timer);
3447 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003448 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003449 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003450 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003451 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003452 if (!mddev->thread)
3453 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003454 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003455 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003456 rv = len;
3457out_unlock:
3458 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003459 return rv;
3460}
3461
3462static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003463__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003464
NeilBrownd4dbd022006-06-26 00:27:59 -07003465static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003466layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003467{
3468 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003469 if (mddev->reshape_position != MaxSector &&
3470 mddev->layout != mddev->new_layout)
3471 return sprintf(page, "%d (%d)\n",
3472 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003473 return sprintf(page, "%d\n", mddev->layout);
3474}
3475
3476static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003477layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003478{
3479 char *e;
3480 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11003481 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003482
3483 if (!*buf || (*e && *e != '\n'))
3484 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11003485 err = mddev_lock(mddev);
3486 if (err)
3487 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003488
NeilBrownb3546032009-03-31 14:56:41 +11003489 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003490 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003491 err = -EBUSY;
3492 else if (mddev->ro)
3493 err = -EROFS;
3494 else {
3495 mddev->new_layout = n;
3496 err = mddev->pers->check_reshape(mddev);
3497 if (err)
3498 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003499 }
NeilBrownb3546032009-03-31 14:56:41 +11003500 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003501 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003502 if (mddev->reshape_position == MaxSector)
3503 mddev->layout = n;
3504 }
NeilBrown67918752014-12-15 12:57:01 +11003505 mddev_unlock(mddev);
3506 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003507}
3508static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003509__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003510
NeilBrowneae17012005-11-08 21:39:23 -08003511static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003512raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003513{
NeilBrownbb636542005-11-08 21:39:45 -08003514 if (mddev->raid_disks == 0)
3515 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003516 if (mddev->reshape_position != MaxSector &&
3517 mddev->delta_disks != 0)
3518 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3519 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003520 return sprintf(page, "%d\n", mddev->raid_disks);
3521}
3522
NeilBrownfd01b882011-10-11 16:47:53 +11003523static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003524
3525static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003526raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003527{
NeilBrownda943b992006-01-06 00:20:54 -08003528 char *e;
NeilBrown67918752014-12-15 12:57:01 +11003529 int err;
NeilBrownda943b992006-01-06 00:20:54 -08003530 unsigned long n = simple_strtoul(buf, &e, 10);
3531
3532 if (!*buf || (*e && *e != '\n'))
3533 return -EINVAL;
3534
NeilBrown67918752014-12-15 12:57:01 +11003535 err = mddev_lock(mddev);
3536 if (err)
3537 return err;
NeilBrownda943b992006-01-06 00:20:54 -08003538 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11003539 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003540 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003541 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003542 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003543
NeilBrown67918752014-12-15 12:57:01 +11003544 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003545 rdev_for_each(rdev, mddev) {
3546 if (olddisks < n &&
3547 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003548 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003549 if (olddisks > n &&
3550 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11003551 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10003552 }
NeilBrown67918752014-12-15 12:57:01 +11003553 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003554 mddev->delta_disks = n - olddisks;
3555 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003556 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003557 } else
NeilBrownda943b992006-01-06 00:20:54 -08003558 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11003559out_unlock:
3560 mddev_unlock(mddev);
3561 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08003562}
3563static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003564__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003565
NeilBrown24dd4692005-11-08 21:39:26 -08003566static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003567chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003568{
NeilBrown08a02ec2007-05-09 02:35:38 -07003569 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003570 mddev->chunk_sectors != mddev->new_chunk_sectors)
3571 return sprintf(page, "%d (%d)\n",
3572 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003573 mddev->chunk_sectors << 9);
3574 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003575}
3576
3577static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003578chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003579{
NeilBrown67918752014-12-15 12:57:01 +11003580 int err;
NeilBrown3b343802006-01-06 00:20:47 -08003581 char *e;
3582 unsigned long n = simple_strtoul(buf, &e, 10);
3583
NeilBrown3b343802006-01-06 00:20:47 -08003584 if (!*buf || (*e && *e != '\n'))
3585 return -EINVAL;
3586
NeilBrown67918752014-12-15 12:57:01 +11003587 err = mddev_lock(mddev);
3588 if (err)
3589 return err;
NeilBrownb3546032009-03-31 14:56:41 +11003590 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003591 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003592 err = -EBUSY;
3593 else if (mddev->ro)
3594 err = -EROFS;
3595 else {
3596 mddev->new_chunk_sectors = n >> 9;
3597 err = mddev->pers->check_reshape(mddev);
3598 if (err)
3599 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10003600 }
NeilBrownb3546032009-03-31 14:56:41 +11003601 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003602 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003603 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003604 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003605 }
NeilBrown67918752014-12-15 12:57:01 +11003606 mddev_unlock(mddev);
3607 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08003608}
3609static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003610__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003611
NeilBrowna94213b2006-06-26 00:28:00 -07003612static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003613resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003614{
NeilBrownd1a7c502009-03-31 15:24:32 +11003615 if (mddev->recovery_cp == MaxSector)
3616 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003617 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3618}
3619
3620static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003621resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003622{
NeilBrown67918752014-12-15 12:57:01 +11003623 int err;
NeilBrowna94213b2006-06-26 00:28:00 -07003624 char *e;
3625 unsigned long long n = simple_strtoull(buf, &e, 10);
3626
NeilBrown67918752014-12-15 12:57:01 +11003627 err = mddev_lock(mddev);
3628 if (err)
3629 return err;
NeilBrownb0986362011-05-11 15:52:21 +10003630 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11003631 err = -EBUSY;
3632 else if (cmd_match(buf, "none"))
Dan Williams06e3c812009-12-12 21:17:12 -07003633 n = MaxSector;
3634 else if (!*buf || (*e && *e != '\n'))
NeilBrown67918752014-12-15 12:57:01 +11003635 err = -EINVAL;
NeilBrowna94213b2006-06-26 00:28:00 -07003636
NeilBrown67918752014-12-15 12:57:01 +11003637 if (!err) {
3638 mddev->recovery_cp = n;
3639 if (mddev->pers)
3640 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3641 }
3642 mddev_unlock(mddev);
3643 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07003644}
3645static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003646__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003647
NeilBrown9e653b62006-06-26 00:27:58 -07003648/*
3649 * The array state can be:
3650 *
3651 * clear
3652 * No devices, no size, no level
3653 * Equivalent to STOP_ARRAY ioctl
3654 * inactive
3655 * May have some settings, but array is not active
3656 * all IO results in error
3657 * When written, doesn't tear down array, but just stops it
3658 * suspended (not supported yet)
3659 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003660 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003661 * readonly
3662 * no resync can happen. no superblocks get written.
3663 * write requests fail
3664 * read-auto
3665 * like readonly, but behaves like 'clean' on a write request.
3666 *
3667 * clean - no pending writes, but otherwise active.
3668 * When written to inactive array, starts without resync
3669 * If a write request arrives then
3670 * if metadata is known, mark 'dirty' and switch to 'active'.
3671 * if not known, block and switch to write-pending
3672 * If written to an active array that has pending writes, then fails.
3673 * active
3674 * fully active: IO and resync can be happening.
3675 * When written to inactive array, starts with resync
3676 *
3677 * write-pending
3678 * clean, but writes are blocked waiting for 'active' to be written.
3679 *
3680 * active-idle
3681 * like active, but no writes have been seen for a while (100msec).
3682 *
3683 */
3684enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3685 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003686static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003687 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3688 "write-pending", "active-idle", NULL };
3689
3690static int match_word(const char *word, char **list)
3691{
3692 int n;
3693 for (n=0; list[n]; n++)
3694 if (cmd_match(word, list[n]))
3695 break;
3696 return n;
3697}
3698
3699static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003700array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003701{
3702 enum array_state st = inactive;
3703
3704 if (mddev->pers)
3705 switch(mddev->ro) {
3706 case 1:
3707 st = readonly;
3708 break;
3709 case 2:
3710 st = read_auto;
3711 break;
3712 case 0:
3713 if (mddev->in_sync)
3714 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003715 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003716 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003717 else if (mddev->safemode)
3718 st = active_idle;
3719 else
3720 st = active;
3721 }
3722 else {
3723 if (list_empty(&mddev->disks) &&
3724 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003725 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003726 st = clear;
3727 else
3728 st = inactive;
3729 }
3730 return sprintf(page, "%s\n", array_states[st]);
3731}
3732
NeilBrownf72ffdd2014-09-30 14:23:59 +10003733static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3734static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3735static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003736static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003737
3738static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003739array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003740{
NeilBrown67918752014-12-15 12:57:01 +11003741 int err;
NeilBrown9e653b62006-06-26 00:27:58 -07003742 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11003743
3744 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
3745 /* don't take reconfig_mutex when toggling between
3746 * clean and active
3747 */
3748 spin_lock(&mddev->lock);
3749 if (st == active) {
3750 restart_array(mddev);
3751 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3752 wake_up(&mddev->sb_wait);
3753 err = 0;
3754 } else /* st == clean */ {
3755 restart_array(mddev);
3756 if (atomic_read(&mddev->writes_pending) == 0) {
3757 if (mddev->in_sync == 0) {
3758 mddev->in_sync = 1;
3759 if (mddev->safemode == 1)
3760 mddev->safemode = 0;
3761 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3762 }
3763 err = 0;
3764 } else
3765 err = -EBUSY;
3766 }
3767 spin_unlock(&mddev->lock);
3768 return err;
3769 }
3770 err = mddev_lock(mddev);
3771 if (err)
3772 return err;
3773 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003774 switch(st) {
3775 case bad_word:
3776 break;
3777 case clear:
3778 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003779 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003780 break;
3781 case inactive:
3782 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003783 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003784 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003785 else
NeilBrowne6910632008-02-06 01:39:51 -08003786 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003787 break;
3788 case suspended:
3789 break; /* not supported yet */
3790 case readonly:
3791 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003792 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003793 else {
3794 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003795 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003796 err = do_md_run(mddev);
3797 }
3798 break;
3799 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003800 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003801 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003802 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003803 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003804 err = restart_array(mddev);
3805 if (err == 0) {
3806 mddev->ro = 2;
3807 set_disk_ro(mddev->gendisk, 0);
3808 }
NeilBrown9e653b62006-06-26 00:27:58 -07003809 } else {
3810 mddev->ro = 2;
3811 err = do_md_run(mddev);
3812 }
3813 break;
3814 case clean:
3815 if (mddev->pers) {
3816 restart_array(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11003817 spin_lock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07003818 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003819 if (mddev->in_sync == 0) {
3820 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003821 if (mddev->safemode == 1)
3822 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003823 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003824 }
3825 err = 0;
3826 } else
3827 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11003828 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003829 } else
3830 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003831 break;
3832 case active:
3833 if (mddev->pers) {
3834 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003835 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003836 wake_up(&mddev->sb_wait);
3837 err = 0;
3838 } else {
3839 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003840 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003841 err = do_md_run(mddev);
3842 }
3843 break;
3844 case write_pending:
3845 case active_idle:
3846 /* these cannot be set */
3847 break;
3848 }
NeilBrown67918752014-12-15 12:57:01 +11003849
3850 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11003851 if (mddev->hold_active == UNTIL_IOCTL)
3852 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003853 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003854 }
NeilBrown67918752014-12-15 12:57:01 +11003855 mddev_unlock(mddev);
3856 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07003857}
NeilBrown80ca3a42006-07-10 04:44:18 -07003858static struct md_sysfs_entry md_array_state =
3859__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003860
NeilBrown6d7ff7382006-01-06 00:21:16 -08003861static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003862max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003863 return sprintf(page, "%d\n",
3864 atomic_read(&mddev->max_corr_read_errors));
3865}
3866
3867static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003868max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003869{
3870 char *e;
3871 unsigned long n = simple_strtoul(buf, &e, 10);
3872
3873 if (*buf && (*e == 0 || *e == '\n')) {
3874 atomic_set(&mddev->max_corr_read_errors, n);
3875 return len;
3876 }
3877 return -EINVAL;
3878}
3879
3880static struct md_sysfs_entry max_corr_read_errors =
3881__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3882 max_corrected_read_errors_store);
3883
3884static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003885null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003886{
3887 return -EINVAL;
3888}
3889
3890static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003891new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003892{
3893 /* buf must be %d:%d\n? giving major and minor numbers */
3894 /* The new device is added to the array.
3895 * If the array has a persistent superblock, we read the
3896 * superblock to initialise info and check validity.
3897 * Otherwise, only checking done is that in bind_rdev_to_array,
3898 * which mainly checks size.
3899 */
3900 char *e;
3901 int major = simple_strtoul(buf, &e, 10);
3902 int minor;
3903 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003904 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003905 int err;
3906
3907 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3908 return -EINVAL;
3909 minor = simple_strtoul(e+1, &e, 10);
3910 if (*e && *e != '\n')
3911 return -EINVAL;
3912 dev = MKDEV(major, minor);
3913 if (major != MAJOR(dev) ||
3914 minor != MINOR(dev))
3915 return -EOVERFLOW;
3916
NeilBrown67918752014-12-15 12:57:01 +11003917 flush_workqueue(md_misc_wq);
3918
3919 err = mddev_lock(mddev);
3920 if (err)
3921 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003922 if (mddev->persistent) {
3923 rdev = md_import_device(dev, mddev->major_version,
3924 mddev->minor_version);
3925 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003926 struct md_rdev *rdev0
3927 = list_entry(mddev->disks.next,
3928 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003929 err = super_types[mddev->major_version]
3930 .load_super(rdev, rdev0, mddev->minor_version);
3931 if (err < 0)
3932 goto out;
3933 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003934 } else if (mddev->external)
3935 rdev = md_import_device(dev, -2, -1);
3936 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003937 rdev = md_import_device(dev, -1, -1);
3938
3939 if (IS_ERR(rdev))
3940 return PTR_ERR(rdev);
3941 err = bind_rdev_to_array(rdev, mddev);
3942 out:
3943 if (err)
3944 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11003945 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003946 return err ? err : len;
3947}
3948
3949static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003950__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003951
3952static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003953bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003954{
3955 char *end;
3956 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11003957 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07003958
NeilBrown67918752014-12-15 12:57:01 +11003959 err = mddev_lock(mddev);
3960 if (err)
3961 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07003962 if (!mddev->bitmap)
3963 goto out;
3964 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3965 while (*buf) {
3966 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3967 if (buf == end) break;
3968 if (*end == '-') { /* range */
3969 buf = end + 1;
3970 end_chunk = simple_strtoul(buf, &end, 0);
3971 if (buf == end) break;
3972 }
3973 if (*end && !isspace(*end)) break;
3974 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003975 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003976 }
3977 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3978out:
NeilBrown67918752014-12-15 12:57:01 +11003979 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003980 return len;
3981}
3982
3983static struct md_sysfs_entry md_bitmap =
3984__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3985
3986static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003987size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003988{
Andre Noll58c0fed2009-03-31 14:33:13 +11003989 return sprintf(page, "%llu\n",
3990 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003991}
3992
NeilBrownfd01b882011-10-11 16:47:53 +11003993static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003994
3995static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003996size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003997{
3998 /* If array is inactive, we can reduce the component size, but
3999 * not increase it (except from 0).
4000 * If array is active, we can try an on-line resize
4001 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004002 sector_t sectors;
4003 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004004
Andre Noll58c0fed2009-03-31 14:33:13 +11004005 if (err < 0)
4006 return err;
NeilBrown67918752014-12-15 12:57:01 +11004007 err = mddev_lock(mddev);
4008 if (err)
4009 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004010 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004011 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004012 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004013 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004014 if (mddev->dev_sectors == 0 ||
4015 mddev->dev_sectors > sectors)
4016 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004017 else
4018 err = -ENOSPC;
4019 }
NeilBrown67918752014-12-15 12:57:01 +11004020 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004021 return err ? err : len;
4022}
4023
4024static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004025__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004026
Masanari Iida83f0d772012-10-30 00:18:08 +09004027/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004028 * This is one of
4029 * 'none' for arrays with no metadata (good luck...)
4030 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004031 * or N.M for internally known formats
4032 */
4033static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004034metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004035{
4036 if (mddev->persistent)
4037 return sprintf(page, "%d.%d\n",
4038 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004039 else if (mddev->external)
4040 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004041 else
4042 return sprintf(page, "none\n");
4043}
4044
4045static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004046metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004047{
4048 int major, minor;
4049 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004050 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004051 /* Changing the details of 'external' metadata is
4052 * always permitted. Otherwise there must be
4053 * no devices attached to the array.
4054 */
NeilBrown67918752014-12-15 12:57:01 +11004055
4056 err = mddev_lock(mddev);
4057 if (err)
4058 return err;
4059 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004060 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4061 ;
4062 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004063 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004064
NeilBrown67918752014-12-15 12:57:01 +11004065 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004066 if (cmd_match(buf, "none")) {
4067 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004068 mddev->external = 0;
4069 mddev->major_version = 0;
4070 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004071 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004072 }
4073 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004074 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004075 if (namelen >= sizeof(mddev->metadata_type))
4076 namelen = sizeof(mddev->metadata_type)-1;
4077 strncpy(mddev->metadata_type, buf+9, namelen);
4078 mddev->metadata_type[namelen] = 0;
4079 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4080 mddev->metadata_type[--namelen] = 0;
4081 mddev->persistent = 0;
4082 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004083 mddev->major_version = 0;
4084 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004085 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004086 }
4087 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004088 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004089 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004090 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004091 buf = e+1;
4092 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004093 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004094 goto out_unlock;
4095 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004096 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004097 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004098 mddev->major_version = major;
4099 mddev->minor_version = minor;
4100 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004101 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004102 err = 0;
4103out_unlock:
4104 mddev_unlock(mddev);
4105 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004106}
4107
4108static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004109__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004110
NeilBrowna35b0d62006-01-06 00:20:49 -08004111static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004112action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004113{
NeilBrown7eec3142005-11-08 21:39:44 -08004114 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004115 unsigned long recovery = mddev->recovery;
4116 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004117 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004118 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4119 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4120 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004121 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004122 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4123 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004124 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004125 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004126 type = "check";
4127 else
4128 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004129 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004130 type = "recover";
4131 }
4132 return sprintf(page, "%s\n", type);
4133}
4134
4135static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004136action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004137{
NeilBrown7eec3142005-11-08 21:39:44 -08004138 if (!mddev->pers || !mddev->pers->sync_request)
4139 return -EINVAL;
4140
NeilBrownb6a9ce62009-05-26 09:41:17 +10004141 if (cmd_match(page, "frozen"))
4142 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4143 else
4144 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4145
4146 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrownf851b602014-12-11 10:02:10 +11004147 flush_workqueue(md_misc_wq);
NeilBrown7eec3142005-11-08 21:39:44 -08004148 if (mddev->sync_thread) {
4149 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004150 if (mddev_lock(mddev) == 0) {
4151 md_reap_sync_thread(mddev);
4152 mddev_unlock(mddev);
4153 }
NeilBrown7eec3142005-11-08 21:39:44 -08004154 }
NeilBrown03c902e2006-01-06 00:20:46 -08004155 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4156 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004157 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004158 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004159 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004160 else if (cmd_match(page, "recover")) {
4161 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4162 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4163 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004164 int err;
4165 if (mddev->pers->start_reshape == NULL)
4166 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004167 err = mddev_lock(mddev);
4168 if (!err) {
4169 err = mddev->pers->start_reshape(mddev);
4170 mddev_unlock(mddev);
4171 }
NeilBrown16484bf2006-03-27 01:18:13 -08004172 if (err)
4173 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004174 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004175 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004176 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004177 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004178 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004179 return -EINVAL;
4180 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4181 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004182 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004183 if (mddev->ro == 2) {
4184 /* A write to sync_action is enough to justify
4185 * canceling read-auto mode
4186 */
4187 mddev->ro = 0;
4188 md_wakeup_thread(mddev->sync_thread);
4189 }
NeilBrown03c902e2006-01-06 00:20:46 -08004190 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004191 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004192 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004193 return len;
4194}
4195
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004196static struct md_sysfs_entry md_scan_mode =
4197__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4198
4199static ssize_t
4200last_sync_action_show(struct mddev *mddev, char *page)
4201{
4202 return sprintf(page, "%s\n", mddev->last_sync_action);
4203}
4204
4205static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4206
NeilBrown9d888832005-11-08 21:39:26 -08004207static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004208mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004209{
4210 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004211 (unsigned long long)
4212 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004213}
4214
NeilBrown80ca3a42006-07-10 04:44:18 -07004215static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004216
NeilBrown88202a02006-01-06 00:21:36 -08004217static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004218sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004219{
4220 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4221 mddev->sync_speed_min ? "local": "system");
4222}
4223
4224static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004225sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004226{
4227 int min;
4228 char *e;
4229 if (strncmp(buf, "system", 6)==0) {
4230 mddev->sync_speed_min = 0;
4231 return len;
4232 }
4233 min = simple_strtoul(buf, &e, 10);
4234 if (buf == e || (*e && *e != '\n') || min <= 0)
4235 return -EINVAL;
4236 mddev->sync_speed_min = min;
4237 return len;
4238}
4239
4240static struct md_sysfs_entry md_sync_min =
4241__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4242
4243static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004244sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004245{
4246 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4247 mddev->sync_speed_max ? "local": "system");
4248}
4249
4250static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004251sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004252{
4253 int max;
4254 char *e;
4255 if (strncmp(buf, "system", 6)==0) {
4256 mddev->sync_speed_max = 0;
4257 return len;
4258 }
4259 max = simple_strtoul(buf, &e, 10);
4260 if (buf == e || (*e && *e != '\n') || max <= 0)
4261 return -EINVAL;
4262 mddev->sync_speed_max = max;
4263 return len;
4264}
4265
4266static struct md_sysfs_entry md_sync_max =
4267__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4268
Iustin Popd7f3d292007-10-16 23:30:54 -07004269static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004270degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004271{
4272 return sprintf(page, "%d\n", mddev->degraded);
4273}
4274static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004275
4276static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004277sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004278{
4279 return sprintf(page, "%d\n", mddev->parallel_resync);
4280}
4281
4282static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004283sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004284{
4285 long n;
4286
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004287 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004288 return -EINVAL;
4289
4290 if (n != 0 && n != 1)
4291 return -EINVAL;
4292
4293 mddev->parallel_resync = n;
4294
4295 if (mddev->sync_thread)
4296 wake_up(&resync_wait);
4297
4298 return len;
4299}
4300
4301/* force parallel resync, even with shared block devices */
4302static struct md_sysfs_entry md_sync_force_parallel =
4303__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4304 sync_force_parallel_show, sync_force_parallel_store);
4305
4306static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004307sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004308{
4309 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004310 if (mddev->curr_resync == 0)
4311 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004312 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4313 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004314 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004315 db = resync - mddev->resync_mark_cnt;
4316 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004317}
4318
NeilBrown80ca3a42006-07-10 04:44:18 -07004319static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004320
4321static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004322sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004323{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004324 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004325
NeilBrownacb180b2009-04-14 16:28:34 +10004326 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4327 return sprintf(page, "none\n");
4328
NeilBrown72f36d52012-10-11 14:25:57 +11004329 if (mddev->curr_resync == 1 ||
4330 mddev->curr_resync == 2)
4331 return sprintf(page, "delayed\n");
4332
NeilBrownc804cde2012-05-21 09:28:33 +10004333 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4334 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004335 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004336 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004337 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004338
NeilBrownacb180b2009-04-14 16:28:34 +10004339 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004340 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004341}
4342
NeilBrown80ca3a42006-07-10 04:44:18 -07004343static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004344
NeilBrowne464eaf2006-03-27 01:18:14 -08004345static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004346min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004347{
4348 return sprintf(page, "%llu\n",
4349 (unsigned long long)mddev->resync_min);
4350}
4351static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004352min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004353{
4354 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004355 int err;
4356 int chunk;
4357
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004358 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004359 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004360
4361 spin_lock(&mddev->lock);
4362 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004363 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004364 goto out_unlock;
4365
4366 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004367 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004368 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004369
4370 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004371 chunk = mddev->chunk_sectors;
4372 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004373 sector_t temp = min;
NeilBrown23da4222014-12-15 12:57:01 +11004374
4375 err = -EINVAL;
4376 if (sector_div(temp, chunk))
4377 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004378 }
4379 mddev->resync_min = min;
NeilBrown23da4222014-12-15 12:57:01 +11004380 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004381
NeilBrown23da4222014-12-15 12:57:01 +11004382out_unlock:
4383 spin_unlock(&mddev->lock);
4384 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004385}
4386
4387static struct md_sysfs_entry md_min_sync =
4388__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4389
4390static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004391max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004392{
4393 if (mddev->resync_max == MaxSector)
4394 return sprintf(page, "max\n");
4395 else
4396 return sprintf(page, "%llu\n",
4397 (unsigned long long)mddev->resync_max);
4398}
4399static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004400max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004401{
NeilBrown23da4222014-12-15 12:57:01 +11004402 int err;
4403 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004404 if (strncmp(buf, "max", 3) == 0)
4405 mddev->resync_max = MaxSector;
4406 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004407 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004408 int chunk;
4409
4410 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004411 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004412 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004413 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004414 goto out_unlock;
4415
4416 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004417 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004418 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004419 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004420 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004421
4422 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004423 chunk = mddev->chunk_sectors;
4424 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004425 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004426
4427 err = -EINVAL;
4428 if (sector_div(temp, chunk))
4429 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004430 }
4431 mddev->resync_max = max;
4432 }
4433 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004434 err = 0;
4435out_unlock:
4436 spin_unlock(&mddev->lock);
4437 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004438}
4439
4440static struct md_sysfs_entry md_max_sync =
4441__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4442
4443static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004444suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004445{
4446 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4447}
4448
4449static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004450suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004451{
4452 char *e;
4453 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004454 unsigned long long old;
4455 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004456
NeilBrowne464eaf2006-03-27 01:18:14 -08004457 if (buf == e || (*e && *e != '\n'))
4458 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004459
NeilBrown67918752014-12-15 12:57:01 +11004460 err = mddev_lock(mddev);
4461 if (err)
4462 return err;
4463 err = -EINVAL;
4464 if (mddev->pers == NULL ||
4465 mddev->pers->quiesce == NULL)
4466 goto unlock;
4467 old = mddev->suspend_lo;
NeilBrown23ddff32011-01-14 09:14:34 +11004468 mddev->suspend_lo = new;
4469 if (new >= old)
4470 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004471 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004472 else {
4473 /* Expanding suspended region - need to wait */
4474 mddev->pers->quiesce(mddev, 1);
4475 mddev->pers->quiesce(mddev, 0);
4476 }
NeilBrown67918752014-12-15 12:57:01 +11004477 err = 0;
4478unlock:
4479 mddev_unlock(mddev);
4480 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004481}
4482static struct md_sysfs_entry md_suspend_lo =
4483__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4484
NeilBrowne464eaf2006-03-27 01:18:14 -08004485static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004486suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004487{
4488 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4489}
4490
4491static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004492suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004493{
4494 char *e;
4495 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004496 unsigned long long old;
4497 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004498
NeilBrowne464eaf2006-03-27 01:18:14 -08004499 if (buf == e || (*e && *e != '\n'))
4500 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004501
NeilBrown67918752014-12-15 12:57:01 +11004502 err = mddev_lock(mddev);
4503 if (err)
4504 return err;
4505 err = -EINVAL;
4506 if (mddev->pers == NULL ||
4507 mddev->pers->quiesce == NULL)
4508 goto unlock;
4509 old = mddev->suspend_hi;
NeilBrown23ddff32011-01-14 09:14:34 +11004510 mddev->suspend_hi = new;
4511 if (new <= old)
4512 /* Shrinking suspended region */
4513 mddev->pers->quiesce(mddev, 2);
4514 else {
4515 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004516 mddev->pers->quiesce(mddev, 1);
4517 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004518 }
NeilBrown67918752014-12-15 12:57:01 +11004519 err = 0;
4520unlock:
4521 mddev_unlock(mddev);
4522 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004523}
4524static struct md_sysfs_entry md_suspend_hi =
4525__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4526
NeilBrown08a02ec2007-05-09 02:35:38 -07004527static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004528reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004529{
4530 if (mddev->reshape_position != MaxSector)
4531 return sprintf(page, "%llu\n",
4532 (unsigned long long)mddev->reshape_position);
4533 strcpy(page, "none\n");
4534 return 5;
4535}
4536
4537static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004538reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004539{
NeilBrownc6563a82012-05-21 09:27:00 +10004540 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004541 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004542 int err;
NeilBrown08a02ec2007-05-09 02:35:38 -07004543 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004544
NeilBrown08a02ec2007-05-09 02:35:38 -07004545 if (buf == e || (*e && *e != '\n'))
4546 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004547 err = mddev_lock(mddev);
4548 if (err)
4549 return err;
4550 err = -EBUSY;
4551 if (mddev->pers)
4552 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07004553 mddev->reshape_position = new;
4554 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004555 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004556 mddev->new_level = mddev->level;
4557 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004558 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004559 rdev_for_each(rdev, mddev)
4560 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11004561 err = 0;
4562unlock:
4563 mddev_unlock(mddev);
4564 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07004565}
4566
4567static struct md_sysfs_entry md_reshape_position =
4568__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4569 reshape_position_store);
4570
Dan Williamsb522adc2009-03-31 15:00:31 +11004571static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004572reshape_direction_show(struct mddev *mddev, char *page)
4573{
4574 return sprintf(page, "%s\n",
4575 mddev->reshape_backwards ? "backwards" : "forwards");
4576}
4577
4578static ssize_t
4579reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4580{
4581 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11004582 int err;
4583
NeilBrown2c810cd2012-05-21 09:27:00 +10004584 if (cmd_match(buf, "forwards"))
4585 backwards = 0;
4586 else if (cmd_match(buf, "backwards"))
4587 backwards = 1;
4588 else
4589 return -EINVAL;
4590 if (mddev->reshape_backwards == backwards)
4591 return len;
4592
NeilBrown67918752014-12-15 12:57:01 +11004593 err = mddev_lock(mddev);
4594 if (err)
4595 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10004596 /* check if we are allowed to change */
4597 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11004598 err = -EBUSY;
4599 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10004600 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11004601 err = -EINVAL;
4602 else
4603 mddev->reshape_backwards = backwards;
4604 mddev_unlock(mddev);
4605 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10004606}
4607
4608static struct md_sysfs_entry md_reshape_direction =
4609__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4610 reshape_direction_store);
4611
4612static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004613array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004614{
4615 if (mddev->external_size)
4616 return sprintf(page, "%llu\n",
4617 (unsigned long long)mddev->array_sectors/2);
4618 else
4619 return sprintf(page, "default\n");
4620}
4621
4622static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004623array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004624{
4625 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11004626 int err;
4627
4628 err = mddev_lock(mddev);
4629 if (err)
4630 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11004631
4632 if (strncmp(buf, "default", 7) == 0) {
4633 if (mddev->pers)
4634 sectors = mddev->pers->size(mddev, 0, 0);
4635 else
4636 sectors = mddev->array_sectors;
4637
4638 mddev->external_size = 0;
4639 } else {
4640 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11004641 err = -EINVAL;
4642 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4643 err = -E2BIG;
4644 else
4645 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11004646 }
4647
NeilBrown67918752014-12-15 12:57:01 +11004648 if (!err) {
4649 mddev->array_sectors = sectors;
4650 if (mddev->pers) {
4651 set_capacity(mddev->gendisk, mddev->array_sectors);
4652 revalidate_disk(mddev->gendisk);
4653 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11004654 }
NeilBrown67918752014-12-15 12:57:01 +11004655 mddev_unlock(mddev);
4656 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11004657}
4658
4659static struct md_sysfs_entry md_array_size =
4660__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4661 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004662
NeilBrowneae17012005-11-08 21:39:23 -08004663static struct attribute *md_default_attrs[] = {
4664 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004665 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004666 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004667 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004668 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004669 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004670 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004671 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004672 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004673 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004674 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004675 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004676 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004677 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004678 NULL,
4679};
4680
4681static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004682 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004683 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004684 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004685 &md_sync_min.attr,
4686 &md_sync_max.attr,
4687 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004688 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004689 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004690 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004691 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004692 &md_suspend_lo.attr,
4693 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004694 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004695 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004696 NULL,
4697};
NeilBrown411036f2005-11-08 21:39:40 -08004698static struct attribute_group md_redundancy_group = {
4699 .name = NULL,
4700 .attrs = md_redundancy_attrs,
4701};
4702
NeilBrowneae17012005-11-08 21:39:23 -08004703static ssize_t
4704md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4705{
4706 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004707 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004708 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004709
4710 if (!entry->show)
4711 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004712 spin_lock(&all_mddevs_lock);
4713 if (list_empty(&mddev->all_mddevs)) {
4714 spin_unlock(&all_mddevs_lock);
4715 return -EBUSY;
4716 }
4717 mddev_get(mddev);
4718 spin_unlock(&all_mddevs_lock);
4719
NeilBrownb7b17c92014-12-15 12:56:59 +11004720 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11004721 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004722 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004723}
4724
4725static ssize_t
4726md_attr_store(struct kobject *kobj, struct attribute *attr,
4727 const char *page, size_t length)
4728{
4729 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004730 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004731 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004732
4733 if (!entry->store)
4734 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004735 if (!capable(CAP_SYS_ADMIN))
4736 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004737 spin_lock(&all_mddevs_lock);
4738 if (list_empty(&mddev->all_mddevs)) {
4739 spin_unlock(&all_mddevs_lock);
4740 return -EBUSY;
4741 }
4742 mddev_get(mddev);
4743 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11004744 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11004745 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004746 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004747}
4748
4749static void md_free(struct kobject *ko)
4750{
NeilBrownfd01b882011-10-11 16:47:53 +11004751 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004752
4753 if (mddev->sysfs_state)
4754 sysfs_put(mddev->sysfs_state);
4755
4756 if (mddev->gendisk) {
4757 del_gendisk(mddev->gendisk);
4758 put_disk(mddev->gendisk);
4759 }
4760 if (mddev->queue)
4761 blk_cleanup_queue(mddev->queue);
4762
NeilBrowneae17012005-11-08 21:39:23 -08004763 kfree(mddev);
4764}
4765
Emese Revfy52cf25d2010-01-19 02:58:23 +01004766static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004767 .show = md_attr_show,
4768 .store = md_attr_store,
4769};
4770static struct kobj_type md_ktype = {
4771 .release = md_free,
4772 .sysfs_ops = &md_sysfs_ops,
4773 .default_attrs = md_default_attrs,
4774};
4775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776int mdp_major = 0;
4777
Dan Williams5fd3a172009-03-04 00:57:25 -07004778static void mddev_delayed_delete(struct work_struct *ws)
4779{
NeilBrownfd01b882011-10-11 16:47:53 +11004780 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004781
NeilBrown43a70502009-12-14 12:49:55 +11004782 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004783 kobject_del(&mddev->kobj);
4784 kobject_put(&mddev->kobj);
4785}
4786
NeilBrownefeb53c2009-01-09 08:31:10 +11004787static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004789 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004790 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004792 int partitioned;
4793 int shift;
4794 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004795 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
4797 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004798 return -ENODEV;
4799
4800 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4801 shift = partitioned ? MdpMinorShift : 0;
4802 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Tejun Heoe804ac72010-10-15 15:36:08 +02004804 /* wait for any previous instance of this device to be
4805 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004806 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004807 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004808
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004809 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004810 error = -EEXIST;
4811 if (mddev->gendisk)
4812 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004813
4814 if (name) {
4815 /* Need to ensure that 'name' is not a duplicate.
4816 */
NeilBrownfd01b882011-10-11 16:47:53 +11004817 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004818 spin_lock(&all_mddevs_lock);
4819
4820 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4821 if (mddev2->gendisk &&
4822 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4823 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004824 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004825 }
4826 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 }
NeilBrown8b765392009-01-09 08:31:08 +11004828
NeilBrown0909dc42009-07-01 12:27:21 +10004829 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004830 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004831 if (!mddev->queue)
4832 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004833 mddev->queue->queuedata = mddev;
4834
NeilBrown409c57f2009-03-31 14:39:39 +11004835 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004836 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 disk = alloc_disk(1 << shift);
4839 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004840 blk_cleanup_queue(mddev->queue);
4841 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004842 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004844 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004846 if (name)
4847 strcpy(disk->disk_name, name);
4848 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004850 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 disk->fops = &md_fops;
4853 disk->private_data = mddev;
4854 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004855 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004856 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004857 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004858 * remove it now.
4859 */
4860 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004862 /* As soon as we call add_disk(), another thread could get
4863 * through to md_open, so make sure it doesn't get too far
4864 */
4865 mutex_lock(&mddev->open_mutex);
4866 add_disk(disk);
4867
Tejun Heoed9e1982008-08-25 19:56:05 +09004868 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4869 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004870 if (error) {
4871 /* This isn't possible, but as kobject_init_and_add is marked
4872 * __must_check, we must do something with the result
4873 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004874 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4875 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004876 error = 0;
4877 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004878 if (mddev->kobj.sd &&
4879 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004880 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004881 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004882 abort:
4883 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004884 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004885 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004886 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004887 }
NeilBrownd3374822009-01-09 08:31:10 +11004888 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004889 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004890}
4891
4892static struct kobject *md_probe(dev_t dev, int *part, void *data)
4893{
4894 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 return NULL;
4896}
4897
NeilBrownefeb53c2009-01-09 08:31:10 +11004898static int add_named_array(const char *val, struct kernel_param *kp)
4899{
4900 /* val must be "md_*" where * is not all digits.
4901 * We allocate an array with a large free minor number, and
4902 * set the name to val. val must not already be an active name.
4903 */
4904 int len = strlen(val);
4905 char buf[DISK_NAME_LEN];
4906
4907 while (len && val[len-1] == '\n')
4908 len--;
4909 if (len >= DISK_NAME_LEN)
4910 return -E2BIG;
4911 strlcpy(buf, val, len+1);
4912 if (strncmp(buf, "md_", 3) != 0)
4913 return -EINVAL;
4914 return md_alloc(0, buf);
4915}
4916
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917static void md_safemode_timeout(unsigned long data)
4918{
NeilBrownfd01b882011-10-11 16:47:53 +11004919 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
Neil Brown0fd62b82008-06-28 08:31:36 +10004921 if (!atomic_read(&mddev->writes_pending)) {
4922 mddev->safemode = 1;
4923 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004924 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 md_wakeup_thread(mddev->thread);
4927}
4928
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004929static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
NeilBrownfd01b882011-10-11 16:47:53 +11004931int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
NeilBrown2604b702006-01-06 00:20:36 -08004933 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004934 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004935 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
NeilBrowna757e642005-04-16 15:26:42 -07004937 if (list_empty(&mddev->disks))
4938 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
4941 if (mddev->pers)
4942 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004943 /* Cannot run until previous stop completes properly */
4944 if (mddev->sysfs_active)
4945 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 /*
4948 * Analyze all RAID superblock(s)
4949 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004950 if (!mddev->raid_disks) {
4951 if (!mddev->persistent)
4952 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004953 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
NeilBrownd9d166c2006-01-06 00:20:51 -08004956 if (mddev->level != LEVEL_NONE)
4957 request_module("md-level-%d", mddev->level);
4958 else if (mddev->clevel[0])
4959 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 /*
4962 * Drop all container device buffers, from now on
4963 * the only valid external interface is through the md
4964 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004966 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004967 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 continue;
4969 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004970 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004971
4972 /* perform some consistency tests on the device.
4973 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004974 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004975 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004976 if (rdev->meta_bdev) {
4977 /* Nothing to check */;
4978 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004979 if (mddev->dev_sectors &&
4980 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004981 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004982 printk("md: %s: data overlaps metadata\n",
4983 mdname(mddev));
4984 return -EINVAL;
4985 }
4986 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004987 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004988 > rdev->data_offset) {
4989 printk("md: %s: metadata overlaps data\n",
4990 mdname(mddev));
4991 return -EINVAL;
4992 }
4993 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004994 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 }
4996
NeilBrowna167f662010-10-26 18:31:13 +11004997 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07004998 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11004999
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005001 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005002 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005004 if (mddev->level != LEVEL_NONE)
5005 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5006 mddev->level);
5007 else
5008 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5009 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 return -EINVAL;
5011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005013 if (mddev->level != pers->level) {
5014 mddev->level = pers->level;
5015 mddev->new_level = pers->level;
5016 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005017 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
NeilBrownf6705572006-03-27 01:18:11 -08005019 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005020 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005021 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005022 module_put(pers->owner);
5023 return -EINVAL;
5024 }
5025
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005026 if (pers->sync_request) {
5027 /* Warn if this is a potentially silly
5028 * configuration.
5029 */
5030 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005031 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005032 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005033
NeilBrowndafb20f2012-03-19 12:46:39 +11005034 rdev_for_each(rdev, mddev)
5035 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005036 if (rdev < rdev2 &&
5037 rdev->bdev->bd_contains ==
5038 rdev2->bdev->bd_contains) {
5039 printk(KERN_WARNING
5040 "%s: WARNING: %s appears to be"
5041 " on the same physical disk as"
5042 " %s.\n",
5043 mdname(mddev),
5044 bdevname(rdev->bdev,b),
5045 bdevname(rdev2->bdev,b2));
5046 warned = 1;
5047 }
5048 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005049
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005050 if (warned)
5051 printk(KERN_WARNING
5052 "True protection against single-disk"
5053 " failure might be compromised.\n");
5054 }
5055
NeilBrown657390d2005-08-26 18:34:16 -07005056 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005057 /* may be over-ridden by personality */
5058 mddev->resync_max_sectors = mddev->dev_sectors;
5059
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005060 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
NeilBrown0f9552b52009-12-30 12:08:50 +11005062 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005063 mddev->ro = 2; /* read-only, but switch on first write */
5064
NeilBrown36d091f2014-12-15 12:56:58 +11005065 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005066 if (err)
5067 printk(KERN_ERR "md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005068 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
Dan Williamsb522adc2009-03-31 15:00:31 +11005069 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5070 " but 'external_size' not in effect?\n", __func__);
5071 printk(KERN_ERR
5072 "md: invalid array_size %llu > default size %llu\n",
5073 (unsigned long long)mddev->array_sectors / 2,
NeilBrown36d091f2014-12-15 12:56:58 +11005074 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005075 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005076 }
NeilBrown36d091f2014-12-15 12:56:58 +11005077 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005078 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005079 err = bitmap_create(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005080 if (err)
NeilBrownb15c2e52006-01-06 00:20:16 -08005081 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5082 mdname(mddev), err);
NeilBrownb15c2e52006-01-06 00:20:16 -08005083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 if (err) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005085 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005086 pers->free(mddev, mddev->private);
5087 module_put(pers->owner);
NeilBrown32a76272005-06-21 17:17:14 -07005088 bitmap_destroy(mddev);
5089 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 }
NeilBrown5c675f82014-12-15 12:56:56 +11005091 if (mddev->queue) {
5092 mddev->queue->backing_dev_info.congested_data = mddev;
5093 mddev->queue->backing_dev_info.congested_fn = md_congested;
NeilBrown64590f42014-12-15 12:56:57 +11005094 blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec);
NeilBrown5c675f82014-12-15 12:56:56 +11005095 }
NeilBrown36d091f2014-12-15 12:56:58 +11005096 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005097 if (mddev->kobj.sd &&
5098 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005099 printk(KERN_WARNING
5100 "md: cannot register extra attributes for %s\n",
5101 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005102 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005103 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005104 mddev->ro = 0;
5105
NeilBrownf72ffdd2014-09-30 14:23:59 +10005106 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005107 atomic_set(&mddev->max_corr_read_errors,
5108 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 mddev->safemode = 0;
5110 mddev->safemode_timer.function = md_safemode_timeout;
5111 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005112 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005114 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005115 spin_lock(&mddev->lock);
5116 mddev->pers = pers;
NeilBrown0ca69882011-01-14 09:14:33 +11005117 mddev->ready = 1;
NeilBrown36d091f2014-12-15 12:56:58 +11005118 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005119 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005120 if (rdev->raid_disk >= 0)
5121 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005122 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005123
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005125
NeilBrown7a0a5352013-08-27 16:28:23 +10005126 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005127 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
NeilBrownd7603b72006-01-06 00:20:30 -08005129 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005130 sysfs_notify_dirent_safe(mddev->sysfs_state);
5131 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005132 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 return 0;
5134}
NeilBrown390ee602010-06-01 19:37:27 +10005135EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
NeilBrownfd01b882011-10-11 16:47:53 +11005137static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005138{
5139 int err;
5140
5141 err = md_run(mddev);
5142 if (err)
5143 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005144 err = bitmap_load(mddev);
5145 if (err) {
5146 bitmap_destroy(mddev);
5147 goto out;
5148 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005149
5150 md_wakeup_thread(mddev->thread);
5151 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5152
NeilBrownfe60b012010-03-29 11:10:42 +11005153 set_capacity(mddev->gendisk, mddev->array_sectors);
5154 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005155 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005156 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5157out:
5158 return err;
5159}
5160
NeilBrownfd01b882011-10-11 16:47:53 +11005161static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162{
5163 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
Andre Noll80fab1d2008-07-11 22:02:21 +10005165 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005167 return -ENXIO;
5168 if (!mddev->pers)
5169 return -EINVAL;
5170 if (!mddev->ro)
5171 return -EBUSY;
5172 mddev->safemode = 0;
5173 mddev->ro = 0;
5174 set_disk_ro(disk, 0);
5175 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5176 mdname(mddev));
5177 /* Kick recovery or resync if necessary */
5178 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5179 md_wakeup_thread(mddev->thread);
5180 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005181 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183}
5184
NeilBrownfd01b882011-10-11 16:47:53 +11005185static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005186{
5187 mddev->array_sectors = 0;
5188 mddev->external_size = 0;
5189 mddev->dev_sectors = 0;
5190 mddev->raid_disks = 0;
5191 mddev->recovery_cp = 0;
5192 mddev->resync_min = 0;
5193 mddev->resync_max = MaxSector;
5194 mddev->reshape_position = MaxSector;
5195 mddev->external = 0;
5196 mddev->persistent = 0;
5197 mddev->level = LEVEL_NONE;
5198 mddev->clevel[0] = 0;
5199 mddev->flags = 0;
5200 mddev->ro = 0;
5201 mddev->metadata_type[0] = 0;
5202 mddev->chunk_sectors = 0;
5203 mddev->ctime = mddev->utime = 0;
5204 mddev->layout = 0;
5205 mddev->max_disks = 0;
5206 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005207 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005208 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005209 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005210 mddev->new_level = LEVEL_NONE;
5211 mddev->new_layout = 0;
5212 mddev->new_chunk_sectors = 0;
5213 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005214 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005215 mddev->suspend_lo = mddev->suspend_hi = 0;
5216 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5217 mddev->recovery = 0;
5218 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005219 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005220 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005221 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005222 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005223 mddev->bitmap_info.offset = 0;
5224 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005225 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005226 mddev->bitmap_info.chunksize = 0;
5227 mddev->bitmap_info.daemon_sleep = 0;
5228 mddev->bitmap_info.max_write_behind = 0;
5229}
5230
NeilBrownfd01b882011-10-11 16:47:53 +11005231static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005232{
NeilBrown6b6204e2013-05-09 09:48:30 +10005233 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005234 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005235 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005236 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005237 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005238 }
5239
5240 del_timer_sync(&mddev->safemode_timer);
5241
5242 bitmap_flush(mddev);
5243 md_super_wait(mddev);
5244
NeilBrownb6d428c2013-04-24 11:42:42 +10005245 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005246 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005247 /* mark array as shutdown cleanly */
5248 mddev->in_sync = 1;
5249 md_update_sb(mddev, 1);
5250 }
5251}
NeilBrowndefad612011-01-14 09:14:33 +11005252
NeilBrownfd01b882011-10-11 16:47:53 +11005253void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005254{
NeilBrown29f097c2013-11-14 17:54:51 +11005255 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005256 __md_stop_writes(mddev);
5257 mddev_unlock(mddev);
5258}
NeilBrown390ee602010-06-01 19:37:27 +10005259EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005260
NeilBrown5aa61f42014-12-15 12:56:57 +11005261static void mddev_detach(struct mddev *mddev)
5262{
5263 struct bitmap *bitmap = mddev->bitmap;
5264 /* wait for behind writes to complete */
5265 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
5266 printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n",
5267 mdname(mddev));
5268 /* need to kick something here to make sure I/O goes? */
5269 wait_event(bitmap->behind_wait,
5270 atomic_read(&bitmap->behind_writes) == 0);
5271 }
NeilBrown36d091f2014-12-15 12:56:58 +11005272 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005273 mddev->pers->quiesce(mddev, 1);
5274 mddev->pers->quiesce(mddev, 0);
5275 }
5276 md_unregister_thread(&mddev->thread);
5277 if (mddev->queue)
5278 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5279}
5280
NeilBrown5eff3c42012-11-19 10:47:48 +11005281static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005282{
NeilBrown36d091f2014-12-15 12:56:58 +11005283 struct md_personality *pers = mddev->pers;
NeilBrown5aa61f42014-12-15 12:56:57 +11005284 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005285 spin_lock(&mddev->lock);
5286 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005287 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005288 spin_unlock(&mddev->lock);
5289 pers->free(mddev, mddev->private);
5290 if (pers->sync_request && mddev->to_remove == NULL)
5291 mddev->to_remove = &md_redundancy_group;
5292 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005293 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005294}
NeilBrown5eff3c42012-11-19 10:47:48 +11005295
5296void md_stop(struct mddev *mddev)
5297{
5298 /* stop the array and free an attached data structures.
5299 * This is called from dm-raid
5300 */
5301 __md_stop(mddev);
5302 bitmap_destroy(mddev);
5303 if (mddev->bio_set)
5304 bioset_free(mddev->bio_set);
5305}
5306
NeilBrown390ee602010-06-01 19:37:27 +10005307EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005308
NeilBrowna05b7ea2012-07-19 15:59:18 +10005309static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005310{
5311 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005312 int did_freeze = 0;
5313
5314 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5315 did_freeze = 1;
5316 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5317 md_wakeup_thread(mddev->thread);
5318 }
NeilBrownf851b602014-12-11 10:02:10 +11005319 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005320 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005321 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005322 /* Thread might be blocked waiting for metadata update
5323 * which will now never happen */
5324 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005325
NeilBrown30b8feb2013-11-14 15:16:17 +11005326 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005327 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5328 &mddev->recovery));
NeilBrown30b8feb2013-11-14 15:16:17 +11005329 mddev_lock_nointr(mddev);
5330
NeilBrowna4bd82d2010-03-29 13:23:10 +11005331 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005332 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005333 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005334 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005335 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005336 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005337 if (did_freeze) {
5338 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005339 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005340 md_wakeup_thread(mddev->thread);
5341 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005342 err = -EBUSY;
5343 goto out;
5344 }
5345 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005346 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005347
5348 err = -ENXIO;
5349 if (mddev->ro==1)
5350 goto out;
5351 mddev->ro = 1;
5352 set_disk_ro(mddev->gendisk, 1);
5353 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005354 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5355 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005356 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005357 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005358 }
5359out:
5360 mutex_unlock(&mddev->open_mutex);
5361 return err;
5362}
5363
NeilBrown9e653b62006-06-26 00:27:58 -07005364/* mode:
5365 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005366 * 2 - stop but do not disassemble array
5367 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005368static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005369 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005372 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005373 int did_freeze = 0;
5374
5375 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5376 did_freeze = 1;
5377 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5378 md_wakeup_thread(mddev->thread);
5379 }
NeilBrownf851b602014-12-11 10:02:10 +11005380 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005381 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005382 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005383 /* Thread might be blocked waiting for metadata update
5384 * which will now never happen */
5385 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005386
NeilBrown30b8feb2013-11-14 15:16:17 +11005387 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005388 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5389 !test_bit(MD_RECOVERY_RUNNING,
5390 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005391 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
NeilBrownc8c00a62009-08-10 12:50:52 +10005393 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005394 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005395 mddev->sysfs_active ||
5396 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005397 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005398 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005399 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005400 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005401 if (did_freeze) {
5402 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005403 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005404 md_wakeup_thread(mddev->thread);
5405 }
NeilBrown260fa032013-08-27 16:44:13 +10005406 return -EBUSY;
5407 }
NeilBrown6e17b022010-08-07 21:41:19 +10005408 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005409 if (mddev->ro)
5410 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005411
NeilBrowndefad612011-01-14 09:14:33 +11005412 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005413 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005414 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005415 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005416
NeilBrowna4bd82d2010-03-29 13:23:10 +11005417 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005418 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005419
NeilBrowndafb20f2012-03-19 12:46:39 +11005420 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005421 if (rdev->raid_disk >= 0)
5422 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005423
NeilBrowna4bd82d2010-03-29 13:23:10 +11005424 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005425 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005426 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005427 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005428
NeilBrowna4bd82d2010-03-29 13:23:10 +11005429 if (mddev->ro)
5430 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005431 } else
5432 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 /*
5434 * Free resources if final stop
5435 */
NeilBrown9e653b62006-06-26 00:27:58 -07005436 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5438
NeilBrown978f9462006-02-02 14:28:05 -08005439 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005440 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11005441 struct file *f = mddev->bitmap_info.file;
5442 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11005443 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11005444 spin_unlock(&mddev->lock);
5445 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08005446 }
NeilBrownc3d97142009-12-14 12:49:52 +11005447 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005448
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 export_array(mddev);
5450
NeilBrown6177b472010-03-29 11:37:13 +11005451 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005452 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005453 if (mddev->hold_active == UNTIL_STOP)
5454 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005455 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005456 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005457 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005458 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460}
5461
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005462#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005463static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464{
NeilBrown3cb03002011-10-11 16:45:26 +11005465 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 int err;
5467
NeilBrowna757e642005-04-16 15:26:42 -07005468 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
5471 printk(KERN_INFO "md: running: ");
5472
NeilBrowndafb20f2012-03-19 12:46:39 +11005473 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 char b[BDEVNAME_SIZE];
5475 printk("<%s>", bdevname(rdev->bdev,b));
5476 }
5477 printk("\n");
5478
NeilBrownd710e132008-10-13 11:55:12 +11005479 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 if (err) {
5481 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005482 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 }
5484}
5485
5486/*
5487 * lets try to run arrays based on all disks that have arrived
5488 * until now. (those are in pending_raid_disks)
5489 *
5490 * the method: pick the first pending disk, collect all disks with
5491 * the same UUID, remove all from the pending list and put them into
5492 * the 'same_array' list. Then order this list based on superblock
5493 * update time (freshest comes first), kick out 'old' disks and
5494 * compare superblocks. If everything's fine then run it.
5495 *
5496 * If "unit" is allocated, then bump its reference count
5497 */
5498static void autorun_devices(int part)
5499{
NeilBrown3cb03002011-10-11 16:45:26 +11005500 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005501 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 char b[BDEVNAME_SIZE];
5503
5504 printk(KERN_INFO "md: autorun ...\n");
5505 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005506 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005508 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005510 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
5512 printk(KERN_INFO "md: considering %s ...\n",
5513 bdevname(rdev0->bdev,b));
5514 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005515 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 if (super_90_load(rdev, rdev0, 0) >= 0) {
5517 printk(KERN_INFO "md: adding %s ...\n",
5518 bdevname(rdev->bdev,b));
5519 list_move(&rdev->same_set, &candidates);
5520 }
5521 /*
5522 * now we have a set of devices, with all of them having
5523 * mostly sane superblocks. It's time to allocate the
5524 * mddev.
5525 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005526 if (part) {
5527 dev = MKDEV(mdp_major,
5528 rdev0->preferred_minor << MdpMinorShift);
5529 unit = MINOR(dev) >> MdpMinorShift;
5530 } else {
5531 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5532 unit = MINOR(dev);
5533 }
5534 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5536 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5537 break;
5538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
5540 md_probe(dev, NULL, NULL);
5541 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005542 if (!mddev || !mddev->gendisk) {
5543 if (mddev)
5544 mddev_put(mddev);
5545 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 "md: cannot allocate memory for md drive.\n");
5547 break;
5548 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005549 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 printk(KERN_WARNING "md: %s locked, cannot run\n",
5551 mdname(mddev));
5552 else if (mddev->raid_disks || mddev->major_version
5553 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005554 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 "md: %s already running, cannot run %s\n",
5556 mdname(mddev), bdevname(rdev0->bdev,b));
5557 mddev_unlock(mddev);
5558 } else {
5559 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005560 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005561 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 list_del_init(&rdev->same_set);
5563 if (bind_rdev_to_array(rdev, mddev))
5564 export_rdev(rdev);
5565 }
5566 autorun_array(mddev);
5567 mddev_unlock(mddev);
5568 }
5569 /* on success, candidates will be empty, on error
5570 * it won't...
5571 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005572 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005573 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 mddev_put(mddev);
5577 }
5578 printk(KERN_INFO "md: ... autorun DONE.\n");
5579}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005580#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
NeilBrownf72ffdd2014-09-30 14:23:59 +10005582static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583{
5584 mdu_version_t ver;
5585
5586 ver.major = MD_MAJOR_VERSION;
5587 ver.minor = MD_MINOR_VERSION;
5588 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5589
5590 if (copy_to_user(arg, &ver, sizeof(ver)))
5591 return -EFAULT;
5592
5593 return 0;
5594}
5595
NeilBrownf72ffdd2014-09-30 14:23:59 +10005596static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597{
5598 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005599 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005600 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
NeilBrown1ca69c42012-10-11 13:37:33 +11005602 nr = working = insync = failed = spare = 0;
5603 rcu_read_lock();
5604 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005606 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 failed++;
5608 else {
5609 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005610 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005611 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 else
5613 spare++;
5614 }
5615 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005616 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
5618 info.major_version = mddev->major_version;
5619 info.minor_version = mddev->minor_version;
5620 info.patch_version = MD_PATCHLEVEL_VERSION;
5621 info.ctime = mddev->ctime;
5622 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005623 info.size = mddev->dev_sectors / 2;
5624 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005625 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 info.nr_disks = nr;
5627 info.raid_disks = mddev->raid_disks;
5628 info.md_minor = mddev->md_minor;
5629 info.not_persistent= !mddev->persistent;
5630
5631 info.utime = mddev->utime;
5632 info.state = 0;
5633 if (mddev->in_sync)
5634 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005635 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005636 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06005637 if (mddev_is_clustered(mddev))
5638 info.state |= (1<<MD_SB_CLUSTERED);
5639
NeilBrowna9f326e2009-09-23 18:06:41 +10005640 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 info.working_disks = working;
5642 info.failed_disks = failed;
5643 info.spare_disks = spare;
5644
5645 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005646 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
5648 if (copy_to_user(arg, &info, sizeof(info)))
5649 return -EFAULT;
5650
5651 return 0;
5652}
5653
NeilBrownf72ffdd2014-09-30 14:23:59 +10005654static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005655{
5656 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11005657 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11005658 int err;
NeilBrown32a76272005-06-21 17:17:14 -07005659
NeilBrown60559da2013-07-16 16:44:29 +10005660 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07005661 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11005662 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07005663
NeilBrown32a76272005-06-21 17:17:14 -07005664 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11005665 spin_lock(&mddev->lock);
5666 /* bitmap disabled, zero the first byte and copy out */
5667 if (!mddev->bitmap_info.file)
5668 file->pathname[0] = '\0';
5669 else if ((ptr = d_path(&mddev->bitmap_info.file->f_path,
5670 file->pathname, sizeof(file->pathname))),
5671 IS_ERR(ptr))
5672 err = PTR_ERR(ptr);
5673 else
5674 memmove(file->pathname, ptr,
5675 sizeof(file->pathname)-(ptr-file->pathname));
5676 spin_unlock(&mddev->lock);
5677
5678 if (err == 0 &&
5679 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07005680 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11005681
NeilBrown32a76272005-06-21 17:17:14 -07005682 kfree(file);
5683 return err;
5684}
5685
NeilBrownf72ffdd2014-09-30 14:23:59 +10005686static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687{
5688 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005689 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
5691 if (copy_from_user(&info, arg, sizeof(info)))
5692 return -EFAULT;
5693
NeilBrown1ca69c42012-10-11 13:37:33 +11005694 rcu_read_lock();
5695 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 if (rdev) {
5697 info.major = MAJOR(rdev->bdev->bd_dev);
5698 info.minor = MINOR(rdev->bdev->bd_dev);
5699 info.raid_disk = rdev->raid_disk;
5700 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005701 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005703 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 info.state |= (1<<MD_DISK_ACTIVE);
5705 info.state |= (1<<MD_DISK_SYNC);
5706 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005707 if (test_bit(WriteMostly, &rdev->flags))
5708 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 } else {
5710 info.major = info.minor = 0;
5711 info.raid_disk = -1;
5712 info.state = (1<<MD_DISK_REMOVED);
5713 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005714 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
5716 if (copy_to_user(arg, &info, sizeof(info)))
5717 return -EFAULT;
5718
5719 return 0;
5720}
5721
NeilBrownf72ffdd2014-09-30 14:23:59 +10005722static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723{
5724 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005725 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 dev_t dev = MKDEV(info->major,info->minor);
5727
5728 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5729 return -EOVERFLOW;
5730
5731 if (!mddev->raid_disks) {
5732 int err;
5733 /* expecting a device which has a superblock */
5734 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5735 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005736 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 "md: md_import_device returned %ld\n",
5738 PTR_ERR(rdev));
5739 return PTR_ERR(rdev);
5740 }
5741 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005742 struct md_rdev *rdev0
5743 = list_entry(mddev->disks.next,
5744 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005745 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 .load_super(rdev, rdev0, mddev->minor_version);
5747 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005748 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005750 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 bdevname(rdev0->bdev,b2));
5752 export_rdev(rdev);
5753 return -EINVAL;
5754 }
5755 }
5756 err = bind_rdev_to_array(rdev, mddev);
5757 if (err)
5758 export_rdev(rdev);
5759 return err;
5760 }
5761
5762 /*
5763 * add_new_disk can be used once the array is assembled
5764 * to add "hot spares". They must already have a superblock
5765 * written
5766 */
5767 if (mddev->pers) {
5768 int err;
5769 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005770 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 "%s: personality does not support diskops!\n",
5772 mdname(mddev));
5773 return -EINVAL;
5774 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005775 if (mddev->persistent)
5776 rdev = md_import_device(dev, mddev->major_version,
5777 mddev->minor_version);
5778 else
5779 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005781 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 "md: md_import_device returned %ld\n",
5783 PTR_ERR(rdev));
5784 return PTR_ERR(rdev);
5785 }
NeilBrown1a855a02010-12-09 16:36:28 +11005786 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005787 if (!mddev->persistent) {
5788 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005789 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005790 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005791 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005792 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005793 } else
NeilBrown41158c72005-06-21 17:17:25 -07005794 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005795 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005796 } else
5797 super_types[mddev->major_version].
5798 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005799 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005800 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005801 /* This was a hot-add request, but events doesn't
5802 * match, so reject it.
5803 */
5804 export_rdev(rdev);
5805 return -EINVAL;
5806 }
5807
NeilBrownb2d444d2005-11-08 21:39:31 -08005808 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005809 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5810 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005811 else
5812 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005813
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 rdev->raid_disk = -1;
5815 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005816 if (!err && !mddev->pers->hot_remove_disk) {
5817 /* If there is hot_add_disk but no hot_remove_disk
5818 * then added disks for geometry changes,
5819 * and should be added immediately.
5820 */
5821 super_types[mddev->major_version].
5822 validate_super(mddev, rdev);
5823 err = mddev->pers->hot_add_disk(mddev, rdev);
5824 if (err)
5825 unbind_rdev_from_array(rdev);
5826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 if (err)
5828 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005829 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005830 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005831
NeilBrown7ceb17e2013-04-24 11:42:42 +10005832 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005833 if (mddev->degraded)
5834 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005835 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005836 if (!err)
5837 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005838 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 return err;
5840 }
5841
5842 /* otherwise, add_new_disk is only allowed
5843 * for major_version==0 superblocks
5844 */
5845 if (mddev->major_version != 0) {
5846 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5847 mdname(mddev));
5848 return -EINVAL;
5849 }
5850
5851 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5852 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005853 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005855 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 "md: error, md_import_device() returned %ld\n",
5857 PTR_ERR(rdev));
5858 return PTR_ERR(rdev);
5859 }
5860 rdev->desc_nr = info->number;
5861 if (info->raid_disk < mddev->raid_disks)
5862 rdev->raid_disk = info->raid_disk;
5863 else
5864 rdev->raid_disk = -1;
5865
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005867 if (info->state & (1<<MD_DISK_SYNC))
5868 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005870 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5871 set_bit(WriteMostly, &rdev->flags);
5872
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 if (!mddev->persistent) {
5874 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005875 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5876 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005877 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005878 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
NeilBrown2bf071b2006-01-06 00:20:55 -08005880 err = bind_rdev_to_array(rdev, mddev);
5881 if (err) {
5882 export_rdev(rdev);
5883 return err;
5884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 }
5886
5887 return 0;
5888}
5889
NeilBrownf72ffdd2014-09-30 14:23:59 +10005890static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891{
5892 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005893 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 rdev = find_rdev(mddev, dev);
5896 if (!rdev)
5897 return -ENXIO;
5898
NeilBrown3ea8929d2013-04-24 11:42:41 +10005899 clear_bit(Blocked, &rdev->flags);
5900 remove_and_add_spares(mddev, rdev);
5901
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 if (rdev->raid_disk >= 0)
5903 goto busy;
5904
5905 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005906 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005907 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909 return 0;
5910busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005911 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 bdevname(rdev->bdev,b), mdname(mddev));
5913 return -EBUSY;
5914}
5915
NeilBrownf72ffdd2014-09-30 14:23:59 +10005916static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917{
5918 char b[BDEVNAME_SIZE];
5919 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005920 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
5922 if (!mddev->pers)
5923 return -ENODEV;
5924
5925 if (mddev->major_version != 0) {
5926 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5927 " version-0 superblocks.\n",
5928 mdname(mddev));
5929 return -EINVAL;
5930 }
5931 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005932 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 "%s: personality does not support diskops!\n",
5934 mdname(mddev));
5935 return -EINVAL;
5936 }
5937
NeilBrownd710e132008-10-13 11:55:12 +11005938 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005940 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 "md: error, md_import_device() returned %ld\n",
5942 PTR_ERR(rdev));
5943 return -EINVAL;
5944 }
5945
5946 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005947 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005949 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
NeilBrown8190e752009-06-18 08:48:58 +10005951 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
NeilBrownb2d444d2005-11-08 21:39:31 -08005953 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005954 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 "md: can not hot-add faulty %s disk to %s!\n",
5956 bdevname(rdev->bdev,b), mdname(mddev));
5957 err = -EINVAL;
5958 goto abort_export;
5959 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005960 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005962 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005963 err = bind_rdev_to_array(rdev, mddev);
5964 if (err)
5965 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
5967 /*
5968 * The rest should better be atomic, we can have disk failures
5969 * noticed in interrupt contexts ...
5970 */
5971
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 rdev->raid_disk = -1;
5973
NeilBrown850b2b422006-10-03 01:15:46 -07005974 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
5976 /*
5977 * Kick recovery, maybe this spare has to be added to the
5978 * array immediately.
5979 */
5980 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5981 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005982 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 return 0;
5984
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985abort_export:
5986 export_rdev(rdev);
5987 return err;
5988}
5989
NeilBrownfd01b882011-10-11 16:47:53 +11005990static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005991{
NeilBrown035328c2014-04-09 12:25:40 +10005992 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07005993
NeilBrown36fa3062005-09-09 16:23:45 -07005994 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10005995 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07005996 return -EBUSY;
5997 if (mddev->recovery || mddev->sync_thread)
5998 return -EBUSY;
5999 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006000 }
6001
NeilBrown36fa3062005-09-09 16:23:45 -07006002 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006003 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006004 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006005
NeilBrown1e594bb2014-12-15 12:57:00 +11006006 if (mddev->bitmap || mddev->bitmap_info.file)
6007 return -EEXIST; /* cannot add when bitmap is present */
6008 f = fget(fd);
6009
6010 if (f == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006011 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6012 mdname(mddev));
6013 return -EBADF;
6014 }
6015
NeilBrown1e594bb2014-12-15 12:57:00 +11006016 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006017 if (!S_ISREG(inode->i_mode)) {
6018 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
6019 mdname(mddev));
6020 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006021 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown035328c2014-04-09 12:25:40 +10006022 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
6023 mdname(mddev));
6024 err = -EBADF;
6025 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07006026 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6027 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006028 err = -EBUSY;
6029 }
6030 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006031 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006032 return err;
6033 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006034 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006035 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006036 } else if (mddev->bitmap == NULL)
6037 return -ENOENT; /* cannot remove what isn't there */
6038 err = 0;
6039 if (mddev->pers) {
6040 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006041 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006042 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006043 if (!err)
6044 err = bitmap_load(mddev);
6045 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006046 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006047 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006048 fd = -1; /* make sure to put the file */
6049 }
NeilBrown36fa3062005-09-09 16:23:45 -07006050 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006051 }
6052 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006053 struct file *f = mddev->bitmap_info.file;
6054 if (f) {
6055 spin_lock(&mddev->lock);
6056 mddev->bitmap_info.file = NULL;
6057 spin_unlock(&mddev->lock);
6058 fput(f);
6059 }
NeilBrown36fa3062005-09-09 16:23:45 -07006060 }
6061
NeilBrown32a76272005-06-21 17:17:14 -07006062 return err;
6063}
6064
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065/*
6066 * set_array_info is used two different ways
6067 * The original usage is when creating a new array.
6068 * In this usage, raid_disks is > 0 and it together with
6069 * level, size, not_persistent,layout,chunksize determine the
6070 * shape of the array.
6071 * This will always create an array with a type-0.90.0 superblock.
6072 * The newer usage is when assembling an array.
6073 * In this case raid_disks will be 0, and the major_version field is
6074 * use to determine which style super-blocks are to be found on the devices.
6075 * The minor and patch _version numbers are also kept incase the
6076 * super_block handler wishes to interpret them.
6077 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006078static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079{
6080
6081 if (info->raid_disks == 0) {
6082 /* just setting version number for superblock loading */
6083 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006084 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 super_types[info->major_version].name == NULL) {
6086 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006087 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 "md: superblock version %d not known\n",
6089 info->major_version);
6090 return -EINVAL;
6091 }
6092 mddev->major_version = info->major_version;
6093 mddev->minor_version = info->minor_version;
6094 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006095 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006096 /* ensure mddev_put doesn't delete this now that there
6097 * is some minimal configuration.
6098 */
6099 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 return 0;
6101 }
6102 mddev->major_version = MD_MAJOR_VERSION;
6103 mddev->minor_version = MD_MINOR_VERSION;
6104 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6105 mddev->ctime = get_seconds();
6106
6107 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006108 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006109 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 mddev->raid_disks = info->raid_disks;
6111 /* don't set md_minor, it is determined by which /dev/md* was
6112 * openned
6113 */
6114 if (info->state & (1<<MD_SB_CLEAN))
6115 mddev->recovery_cp = MaxSector;
6116 else
6117 mddev->recovery_cp = 0;
6118 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006119 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
6121 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006122 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
6124 mddev->max_disks = MD_SB_DISKS;
6125
NeilBrowne6910632008-02-06 01:39:51 -08006126 if (mddev->persistent)
6127 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006128 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129
NeilBrownc3d97142009-12-14 12:49:52 +11006130 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006131 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006132 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006133
NeilBrownf6705572006-03-27 01:18:11 -08006134 mddev->reshape_position = MaxSector;
6135
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 /*
6137 * Generate a 128 bit UUID
6138 */
6139 get_random_bytes(mddev->uuid, 16);
6140
NeilBrownf6705572006-03-27 01:18:11 -08006141 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006142 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006143 mddev->new_layout = mddev->layout;
6144 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006145 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006146
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 return 0;
6148}
6149
NeilBrownfd01b882011-10-11 16:47:53 +11006150void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006151{
Dan Williamsb522adc2009-03-31 15:00:31 +11006152 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6153
6154 if (mddev->external_size)
6155 return;
6156
Dan Williams1f403622009-03-31 14:59:03 +11006157 mddev->array_sectors = array_sectors;
6158}
6159EXPORT_SYMBOL(md_set_array_sectors);
6160
NeilBrownfd01b882011-10-11 16:47:53 +11006161static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006162{
NeilBrown3cb03002011-10-11 16:45:26 +11006163 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006164 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006165 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006166
6167 if (mddev->pers->resize == NULL)
6168 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006169 /* The "num_sectors" is the number of sectors of each device that
6170 * is used. This can only make sense for arrays with redundancy.
6171 * linear and raid0 always use whatever space is available. We can only
6172 * consider changing this number if no resync or reconstruction is
6173 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006174 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006175 * of each device. If num_sectors is zero, we find the largest size
6176 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006177 */
NeilBrownf851b602014-12-11 10:02:10 +11006178 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6179 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006180 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006181 if (mddev->ro)
6182 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006183
NeilBrowndafb20f2012-03-19 12:46:39 +11006184 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006185 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006186
Andre Nolld71f9f82008-07-11 22:02:22 +10006187 if (fit && (num_sectors == 0 || num_sectors > avail))
6188 num_sectors = avail;
6189 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006190 return -ENOSPC;
6191 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006192 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006193 if (!rv)
6194 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006195 return rv;
6196}
6197
NeilBrownfd01b882011-10-11 16:47:53 +11006198static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006199{
6200 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006201 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006202 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006203 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006204 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006205 if (mddev->ro)
6206 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006207 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006208 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006209 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006210 if (mddev->sync_thread ||
6211 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6212 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006213 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006214
6215 rdev_for_each(rdev, mddev) {
6216 if (mddev->raid_disks < raid_disks &&
6217 rdev->data_offset < rdev->new_data_offset)
6218 return -EINVAL;
6219 if (mddev->raid_disks > raid_disks &&
6220 rdev->data_offset > rdev->new_data_offset)
6221 return -EINVAL;
6222 }
6223
NeilBrown63c70c42006-03-27 01:18:13 -08006224 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006225 if (mddev->delta_disks < 0)
6226 mddev->reshape_backwards = 1;
6227 else if (mddev->delta_disks > 0)
6228 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006229
6230 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006231 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006232 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006233 mddev->reshape_backwards = 0;
6234 }
NeilBrownda943b992006-01-06 00:20:54 -08006235 return rv;
6236}
6237
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238/*
6239 * update_array_info is used to change the configuration of an
6240 * on-line array.
6241 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6242 * fields in the info are checked against the array.
6243 * Any differences that cannot be handled will cause an error.
6244 * Normally, only one change can be managed at a time.
6245 */
NeilBrownfd01b882011-10-11 16:47:53 +11006246static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247{
6248 int rv = 0;
6249 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006250 int state = 0;
6251
6252 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006253 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006254 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
6256 if (mddev->major_version != info->major_version ||
6257 mddev->minor_version != info->minor_version ||
6258/* mddev->patch_version != info->patch_version || */
6259 mddev->ctime != info->ctime ||
6260 mddev->level != info->level ||
6261/* mddev->layout != info->layout || */
6262 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006263 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006264 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6265 ((state^info->state) & 0xfffffe00)
6266 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 return -EINVAL;
6268 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006269 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6270 cnt++;
6271 if (mddev->raid_disks != info->raid_disks)
6272 cnt++;
6273 if (mddev->layout != info->layout)
6274 cnt++;
6275 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6276 cnt++;
6277 if (cnt == 0)
6278 return 0;
6279 if (cnt > 1)
6280 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
6282 if (mddev->layout != info->layout) {
6283 /* Change layout
6284 * we don't need to do anything at the md level, the
6285 * personality will take care of it all.
6286 */
NeilBrown50ac1682009-06-18 08:47:55 +10006287 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006289 else {
6290 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006291 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006292 if (rv)
6293 mddev->new_layout = mddev->layout;
6294 return rv;
6295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006297 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006298 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
NeilBrownda943b992006-01-06 00:20:54 -08006300 if (mddev->raid_disks != info->raid_disks)
6301 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302
NeilBrown36fa3062005-09-09 16:23:45 -07006303 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006304 if (mddev->pers->quiesce == NULL || mddev->thread == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -07006305 return -EINVAL;
6306 if (mddev->recovery || mddev->sync_thread)
6307 return -EBUSY;
6308 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6309 /* add the bitmap */
6310 if (mddev->bitmap)
6311 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006312 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006313 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006314 mddev->bitmap_info.offset =
6315 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006316 mddev->bitmap_info.space =
6317 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006318 mddev->pers->quiesce(mddev, 1);
6319 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006320 if (!rv)
6321 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006322 if (rv)
6323 bitmap_destroy(mddev);
6324 mddev->pers->quiesce(mddev, 0);
6325 } else {
6326 /* remove the bitmap */
6327 if (!mddev->bitmap)
6328 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006329 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006330 return -EINVAL;
6331 mddev->pers->quiesce(mddev, 1);
6332 bitmap_destroy(mddev);
6333 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006334 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006335 }
6336 }
NeilBrown850b2b422006-10-03 01:15:46 -07006337 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 return rv;
6339}
6340
NeilBrownfd01b882011-10-11 16:47:53 +11006341static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342{
NeilBrown3cb03002011-10-11 16:45:26 +11006343 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006344 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345
6346 if (mddev->pers == NULL)
6347 return -ENODEV;
6348
NeilBrown1ca69c42012-10-11 13:37:33 +11006349 rcu_read_lock();
6350 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006352 err = -ENODEV;
6353 else {
6354 md_error(mddev, rdev);
6355 if (!test_bit(Faulty, &rdev->flags))
6356 err = -EBUSY;
6357 }
6358 rcu_read_unlock();
6359 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360}
6361
Andre Noll2f9618c2008-04-25 18:57:58 +02006362/*
6363 * We have a problem here : there is no easy way to give a CHS
6364 * virtual geometry. We currently pretend that we have a 2 heads
6365 * 4 sectors (with a BIG number of cylinders...). This drives
6366 * dosfs just mad... ;-)
6367 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006368static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6369{
NeilBrownfd01b882011-10-11 16:47:53 +11006370 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006371
6372 geo->heads = 2;
6373 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006374 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006375 return 0;
6376}
6377
Nicolas Schichancb335f82014-01-15 16:58:52 +01006378static inline bool md_ioctl_valid(unsigned int cmd)
6379{
6380 switch (cmd) {
6381 case ADD_NEW_DISK:
6382 case BLKROSET:
6383 case GET_ARRAY_INFO:
6384 case GET_BITMAP_FILE:
6385 case GET_DISK_INFO:
6386 case HOT_ADD_DISK:
6387 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006388 case RAID_AUTORUN:
6389 case RAID_VERSION:
6390 case RESTART_ARRAY_RW:
6391 case RUN_ARRAY:
6392 case SET_ARRAY_INFO:
6393 case SET_BITMAP_FILE:
6394 case SET_DISK_FAULTY:
6395 case STOP_ARRAY:
6396 case STOP_ARRAY_RO:
6397 return true;
6398 default:
6399 return false;
6400 }
6401}
6402
Al Viroa39907f2008-03-02 10:31:15 -05006403static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 unsigned int cmd, unsigned long arg)
6405{
6406 int err = 0;
6407 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006408 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006409 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410
Nicolas Schichancb335f82014-01-15 16:58:52 +01006411 if (!md_ioctl_valid(cmd))
6412 return -ENOTTY;
6413
NeilBrown506c9e42011-12-23 10:17:26 +11006414 switch (cmd) {
6415 case RAID_VERSION:
6416 case GET_ARRAY_INFO:
6417 case GET_DISK_INFO:
6418 break;
6419 default:
6420 if (!capable(CAP_SYS_ADMIN))
6421 return -EACCES;
6422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
6424 /*
6425 * Commands dealing with the RAID driver but not any
6426 * particular array:
6427 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006428 switch (cmd) {
6429 case RAID_VERSION:
6430 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006431 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006434 case RAID_AUTORUN:
6435 err = 0;
6436 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006437 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006439 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 }
6441
6442 /*
6443 * Commands creating/starting a new array:
6444 */
6445
Al Viroa39907f2008-03-02 10:31:15 -05006446 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
6448 if (!mddev) {
6449 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006450 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 }
6452
NeilBrown1ca69c42012-10-11 13:37:33 +11006453 /* Some actions do not requires the mutex */
6454 switch (cmd) {
6455 case GET_ARRAY_INFO:
6456 if (!mddev->raid_disks && !mddev->external)
6457 err = -ENODEV;
6458 else
6459 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006460 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006461
6462 case GET_DISK_INFO:
6463 if (!mddev->raid_disks && !mddev->external)
6464 err = -ENODEV;
6465 else
6466 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006467 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006468
6469 case SET_DISK_FAULTY:
6470 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006471 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11006472
6473 case GET_BITMAP_FILE:
6474 err = get_bitmap_file(mddev, argp);
6475 goto out;
6476
NeilBrown1ca69c42012-10-11 13:37:33 +11006477 }
6478
NeilBrowna7a3f082012-12-11 13:35:54 +11006479 if (cmd == ADD_NEW_DISK)
6480 /* need to ensure md_delayed_delete() has completed */
6481 flush_workqueue(md_misc_wq);
6482
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006483 if (cmd == HOT_REMOVE_DISK)
6484 /* need to ensure recovery thread has run */
6485 wait_event_interruptible_timeout(mddev->sb_wait,
6486 !test_bit(MD_RECOVERY_NEEDED,
6487 &mddev->flags),
6488 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006489 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6490 /* Need to flush page cache, and ensure no-one else opens
6491 * and writes
6492 */
6493 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006494 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006495 mutex_unlock(&mddev->open_mutex);
6496 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006497 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006498 }
6499 set_bit(MD_STILL_CLOSED, &mddev->flags);
6500 mutex_unlock(&mddev->open_mutex);
6501 sync_blockdev(bdev);
6502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 err = mddev_lock(mddev);
6504 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006505 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6507 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006508 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 }
6510
NeilBrownc02c0ae2012-12-11 13:39:21 +11006511 if (cmd == SET_ARRAY_INFO) {
6512 mdu_array_info_t info;
6513 if (!arg)
6514 memset(&info, 0, sizeof(info));
6515 else if (copy_from_user(&info, argp, sizeof(info))) {
6516 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006517 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006518 }
6519 if (mddev->pers) {
6520 err = update_array_info(mddev, &info);
6521 if (err) {
6522 printk(KERN_WARNING "md: couldn't update"
6523 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006524 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006526 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006527 }
6528 if (!list_empty(&mddev->disks)) {
6529 printk(KERN_WARNING
6530 "md: array %s already has disks!\n",
6531 mdname(mddev));
6532 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006533 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006534 }
6535 if (mddev->raid_disks) {
6536 printk(KERN_WARNING
6537 "md: array %s already initialised!\n",
6538 mdname(mddev));
6539 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006540 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006541 }
6542 err = set_array_info(mddev, &info);
6543 if (err) {
6544 printk(KERN_WARNING "md: couldn't set"
6545 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006546 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006547 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006548 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549 }
6550
6551 /*
6552 * Commands querying/configuring an existing array:
6553 */
NeilBrown32a76272005-06-21 17:17:14 -07006554 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006555 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006556 if ((!mddev->raid_disks && !mddev->external)
6557 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6558 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6559 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006561 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 }
6563
6564 /*
6565 * Commands even a read-only array can execute:
6566 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006567 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006568 case RESTART_ARRAY_RW:
6569 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006570 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006571
6572 case STOP_ARRAY:
6573 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006574 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006575
6576 case STOP_ARRAY_RO:
6577 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006578 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006579
NeilBrown3ea8929d2013-04-24 11:42:41 +10006580 case HOT_REMOVE_DISK:
6581 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006582 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006583
NeilBrown7ceb17e2013-04-24 11:42:42 +10006584 case ADD_NEW_DISK:
6585 /* We can support ADD_NEW_DISK on read-only arrays
6586 * on if we are re-adding a preexisting device.
6587 * So require mddev->pers and MD_DISK_SYNC.
6588 */
6589 if (mddev->pers) {
6590 mdu_disk_info_t info;
6591 if (copy_from_user(&info, argp, sizeof(info)))
6592 err = -EFAULT;
6593 else if (!(info.state & (1<<MD_DISK_SYNC)))
6594 /* Need to clear read-only for this */
6595 break;
6596 else
6597 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006598 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006599 }
6600 break;
6601
NeilBrownc02c0ae2012-12-11 13:39:21 +11006602 case BLKROSET:
6603 if (get_user(ro, (int __user *)(arg))) {
6604 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006605 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006606 }
6607 err = -EINVAL;
6608
6609 /* if the bdev is going readonly the value of mddev->ro
6610 * does not matter, no writes are coming
6611 */
6612 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006613 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006614
NeilBrownc02c0ae2012-12-11 13:39:21 +11006615 /* are we are already prepared for writes? */
6616 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006617 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006618
6619 /* transitioning to readauto need only happen for
6620 * arrays that call md_write_start
6621 */
6622 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006624 if (err == 0) {
6625 mddev->ro = 2;
6626 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006627 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006628 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006629 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 }
6631
6632 /*
6633 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006634 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 */
NeilBrown326eb172014-09-30 15:36:28 +10006636 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006637 if (mddev->ro == 2) {
6638 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006639 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006640 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006641 /* mddev_unlock will wake thread */
6642 /* If a device failed while we were read-only, we
6643 * need to make sure the metadata is updated now.
6644 */
6645 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6646 mddev_unlock(mddev);
6647 wait_event(mddev->sb_wait,
6648 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6649 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006650 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006651 }
NeilBrownf91de922005-11-08 21:39:36 -08006652 } else {
6653 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006654 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 }
6657
NeilBrownc02c0ae2012-12-11 13:39:21 +11006658 switch (cmd) {
6659 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006661 mdu_disk_info_t info;
6662 if (copy_from_user(&info, argp, sizeof(info)))
6663 err = -EFAULT;
6664 else
6665 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006666 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668
NeilBrownc02c0ae2012-12-11 13:39:21 +11006669 case HOT_ADD_DISK:
6670 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006671 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
NeilBrownc02c0ae2012-12-11 13:39:21 +11006673 case RUN_ARRAY:
6674 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006675 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
NeilBrownc02c0ae2012-12-11 13:39:21 +11006677 case SET_BITMAP_FILE:
6678 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006679 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006680
NeilBrownc02c0ae2012-12-11 13:39:21 +11006681 default:
6682 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006683 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 }
6685
NeilBrown3adc28d2014-09-30 15:46:41 +10006686unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006687 if (mddev->hold_active == UNTIL_IOCTL &&
6688 err != -EINVAL)
6689 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006691out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 return err;
6693}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006694#ifdef CONFIG_COMPAT
6695static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6696 unsigned int cmd, unsigned long arg)
6697{
6698 switch (cmd) {
6699 case HOT_REMOVE_DISK:
6700 case HOT_ADD_DISK:
6701 case SET_DISK_FAULTY:
6702 case SET_BITMAP_FILE:
6703 /* These take in integer arg, do not convert */
6704 break;
6705 default:
6706 arg = (unsigned long)compat_ptr(arg);
6707 break;
6708 }
6709
6710 return md_ioctl(bdev, mode, cmd, arg);
6711}
6712#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
Al Viroa39907f2008-03-02 10:31:15 -05006714static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715{
6716 /*
6717 * Succeed if we can lock the mddev, which confirms that
6718 * it isn't being stopped right now.
6719 */
NeilBrownfd01b882011-10-11 16:47:53 +11006720 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 int err;
6722
Yuanhan Liu0c098222012-05-22 13:55:32 +10006723 if (!mddev)
6724 return -ENODEV;
6725
NeilBrownd3374822009-01-09 08:31:10 +11006726 if (mddev->gendisk != bdev->bd_disk) {
6727 /* we are racing with mddev_put which is discarding this
6728 * bd_disk.
6729 */
6730 mddev_put(mddev);
6731 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006732 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006733 /* Then retry the open from the top */
6734 return -ERESTARTSYS;
6735 }
6736 BUG_ON(mddev != bdev->bd_disk->private_data);
6737
NeilBrownc8c00a62009-08-10 12:50:52 +10006738 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 goto out;
6740
6741 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006742 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006743 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006744 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006746 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 out:
6748 return err;
6749}
6750
Al Virodb2a1442013-05-05 21:52:57 -04006751static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006753 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006755 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006756 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006759
6760static int md_media_changed(struct gendisk *disk)
6761{
NeilBrownfd01b882011-10-11 16:47:53 +11006762 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006763
6764 return mddev->changed;
6765}
6766
6767static int md_revalidate(struct gendisk *disk)
6768{
NeilBrownfd01b882011-10-11 16:47:53 +11006769 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006770
6771 mddev->changed = 0;
6772 return 0;
6773}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006774static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775{
6776 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006777 .open = md_open,
6778 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006779 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006780#ifdef CONFIG_COMPAT
6781 .compat_ioctl = md_compat_ioctl,
6782#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006783 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006784 .media_changed = md_media_changed,
6785 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786};
6787
NeilBrownf72ffdd2014-09-30 14:23:59 +10006788static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789{
NeilBrown2b8bf342011-10-11 16:48:23 +11006790 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 /*
6793 * md_thread is a 'system-thread', it's priority should be very
6794 * high. We avoid resource deadlocks individually in each
6795 * raid personality. (RAID5 does preallocation) We also use RR and
6796 * the very same RT priority as kswapd, thus we will never get
6797 * into a priority inversion deadlock.
6798 *
6799 * we definitely have to have equal or higher priority than
6800 * bdflush, otherwise bdflush will deadlock if there are too
6801 * many dirty RAID5 blocks.
6802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
NeilBrown6985c432005-10-19 21:23:47 -07006804 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006805 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
NeilBrown93588e22005-11-15 00:09:12 -08006807 /* We need to wait INTERRUPTIBLE so that
6808 * we don't add to the load-average.
6809 * That means we need to be sure no signals are
6810 * pending
6811 */
6812 if (signal_pending(current))
6813 flush_signals(current);
6814
6815 wait_event_interruptible_timeout
6816 (thread->wqueue,
6817 test_bit(THREAD_WAKEUP, &thread->flags)
6818 || kthread_should_stop(),
6819 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
NeilBrown6c987912011-01-14 09:13:53 +11006821 clear_bit(THREAD_WAKEUP, &thread->flags);
6822 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006823 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006825
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 return 0;
6827}
6828
NeilBrown2b8bf342011-10-11 16:48:23 +11006829void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830{
6831 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006832 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 set_bit(THREAD_WAKEUP, &thread->flags);
6834 wake_up(&thread->wqueue);
6835 }
6836}
NeilBrown6c144d32014-09-30 16:15:38 +10006837EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
Shaohua Li4ed87312012-10-11 13:34:00 +11006839struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6840 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841{
NeilBrown2b8bf342011-10-11 16:48:23 +11006842 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
NeilBrown2b8bf342011-10-11 16:48:23 +11006844 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 if (!thread)
6846 return NULL;
6847
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 init_waitqueue_head(&thread->wqueue);
6849
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 thread->run = run;
6851 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006852 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006853 thread->tsk = kthread_run(md_thread, thread,
6854 "%s_%s",
6855 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006856 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006857 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858 kfree(thread);
6859 return NULL;
6860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 return thread;
6862}
NeilBrown6c144d32014-09-30 16:15:38 +10006863EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864
NeilBrown2b8bf342011-10-11 16:48:23 +11006865void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866{
NeilBrown2b8bf342011-10-11 16:48:23 +11006867 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006868 if (!thread)
6869 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006870 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006871 /* Locking ensures that mddev_unlock does not wake_up a
6872 * non-existent thread
6873 */
6874 spin_lock(&pers_lock);
6875 *threadp = NULL;
6876 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006877
6878 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 kfree(thread);
6880}
NeilBrown6c144d32014-09-30 16:15:38 +10006881EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882
NeilBrownfd01b882011-10-11 16:47:53 +11006883void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884{
NeilBrownb2d444d2005-11-08 21:39:31 -08006885 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006887
NeilBrownde393cd2011-07-28 11:31:48 +10006888 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 return;
6890 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006891 if (mddev->degraded)
6892 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006893 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6895 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6896 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006897 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006898 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006899 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900}
NeilBrown6c144d32014-09-30 16:15:38 +10006901EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
6903/* seq_file implementation /proc/mdstat */
6904
6905static void status_unused(struct seq_file *seq)
6906{
6907 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006908 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
6910 seq_printf(seq, "unused devices: ");
6911
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006912 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 char b[BDEVNAME_SIZE];
6914 i++;
6915 seq_printf(seq, "%s ",
6916 bdevname(rdev->bdev,b));
6917 }
6918 if (!i)
6919 seq_printf(seq, "<none>");
6920
6921 seq_printf(seq, "\n");
6922}
6923
NeilBrownf72ffdd2014-09-30 14:23:59 +10006924static void status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925{
NeilBrowndd71cf62009-05-07 12:49:35 +10006926 sector_t max_sectors, resync, res;
6927 unsigned long dt, db;
6928 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006929 int scale;
6930 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931
NeilBrown72f36d52012-10-11 14:25:57 +11006932 if (mddev->curr_resync <= 3)
6933 resync = 0;
6934 else
6935 resync = mddev->curr_resync
6936 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
NeilBrownc804cde2012-05-21 09:28:33 +10006938 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6939 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006940 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006942 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943
NeilBrown403df472014-09-30 15:52:29 +10006944 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08006945 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006946 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006947 * u32, as those are the requirements for sector_div.
6948 * Thus 'scale' must be at least 10
6949 */
6950 scale = 10;
6951 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006952 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006953 scale++;
6954 }
6955 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006956 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006957
6958 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 {
NeilBrown4588b422006-03-27 01:18:04 -08006960 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 seq_printf(seq, "[");
6962 for (i = 0; i < x; i++)
6963 seq_printf(seq, "=");
6964 seq_printf(seq, ">");
6965 for (i = 0; i < y; i++)
6966 seq_printf(seq, ".");
6967 seq_printf(seq, "] ");
6968 }
NeilBrown4588b422006-03-27 01:18:04 -08006969 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006970 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6971 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006972 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6973 "check" :
6974 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6975 "resync" : "recovery"))),
6976 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006977 (unsigned long long) resync/2,
6978 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
6980 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 * dt: time from mark until now
6982 * db: blocks written from mark until now
6983 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006984 *
6985 * rt is a sector_t, so could be 32bit or 64bit.
6986 * So we divide before multiply in case it is 32bit and close
6987 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006988 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006989 * near the end of resync when the number of remaining sectors
6990 * is close to 'db'.
6991 * We then divide rt by 32 after multiplying by db to compensate.
6992 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 */
6994 dt = ((jiffies - mddev->resync_mark) / HZ);
6995 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006996 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6997 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998
NeilBrowndd71cf62009-05-07 12:49:35 +10006999 rt = max_sectors - resync; /* number of remaining sectors */
7000 sector_div(rt, db/32+1);
7001 rt *= dt;
7002 rt >>= 5;
7003
7004 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7005 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006
NeilBrownff4e8d92006-07-10 04:44:16 -07007007 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008}
7009
7010static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7011{
7012 struct list_head *tmp;
7013 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007014 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015
7016 if (l >= 0x10000)
7017 return NULL;
7018 if (!l--)
7019 /* header */
7020 return (void*)1;
7021
7022 spin_lock(&all_mddevs_lock);
7023 list_for_each(tmp,&all_mddevs)
7024 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007025 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 mddev_get(mddev);
7027 spin_unlock(&all_mddevs_lock);
7028 return mddev;
7029 }
7030 spin_unlock(&all_mddevs_lock);
7031 if (!l--)
7032 return (void*)2;/* tail */
7033 return NULL;
7034}
7035
7036static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7037{
7038 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007039 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007040
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 ++*pos;
7042 if (v == (void*)2)
7043 return NULL;
7044
7045 spin_lock(&all_mddevs_lock);
7046 if (v == (void*)1)
7047 tmp = all_mddevs.next;
7048 else
7049 tmp = mddev->all_mddevs.next;
7050 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007051 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 else {
7053 next_mddev = (void*)2;
7054 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 spin_unlock(&all_mddevs_lock);
7057
7058 if (v != (void*)1)
7059 mddev_put(mddev);
7060 return next_mddev;
7061
7062}
7063
7064static void md_seq_stop(struct seq_file *seq, void *v)
7065{
NeilBrownfd01b882011-10-11 16:47:53 +11007066 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067
7068 if (mddev && v != (void*)1 && v != (void*)2)
7069 mddev_put(mddev);
7070}
7071
7072static int md_seq_show(struct seq_file *seq, void *v)
7073{
NeilBrownfd01b882011-10-11 16:47:53 +11007074 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007075 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007076 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077
7078 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007079 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 seq_printf(seq, "Personalities : ");
7081 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007082 list_for_each_entry(pers, &pers_list, list)
7083 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084
7085 spin_unlock(&pers_lock);
7086 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007087 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 return 0;
7089 }
7090 if (v == (void*)2) {
7091 status_unused(seq);
7092 return 0;
7093 }
7094
NeilBrown36d091f2014-12-15 12:56:58 +11007095 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7097 seq_printf(seq, "%s : %sactive", mdname(mddev),
7098 mddev->pers ? "" : "in");
7099 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007100 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007102 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007103 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 seq_printf(seq, " %s", mddev->pers->name);
7105 }
7106
Andre Nolldd8ac332009-03-31 14:33:13 +11007107 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007108 rcu_read_lock();
7109 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 char b[BDEVNAME_SIZE];
7111 seq_printf(seq, " %s[%d]",
7112 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007113 if (test_bit(WriteMostly, &rdev->flags))
7114 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007115 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 seq_printf(seq, "(F)");
7117 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007118 }
7119 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007120 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007121 if (test_bit(Replacement, &rdev->flags))
7122 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007123 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007125 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126
7127 if (!list_empty(&mddev->disks)) {
7128 if (mddev->pers)
7129 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007130 (unsigned long long)
7131 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 else
7133 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007134 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007136 if (mddev->persistent) {
7137 if (mddev->major_version != 0 ||
7138 mddev->minor_version != 90) {
7139 seq_printf(seq," super %d.%d",
7140 mddev->major_version,
7141 mddev->minor_version);
7142 }
NeilBrowne6910632008-02-06 01:39:51 -08007143 } else if (mddev->external)
7144 seq_printf(seq, " super external:%s",
7145 mddev->metadata_type);
7146 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007147 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148
7149 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007150 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007151 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007152 if (mddev->pers->sync_request) {
7153 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007154 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007155 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007156 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007157 seq_printf(seq, "\tresync=DELAYED\n ");
7158 else if (mddev->recovery_cp < MaxSector)
7159 seq_printf(seq, "\tresync=PENDING\n ");
7160 }
NeilBrown32a76272005-06-21 17:17:14 -07007161 } else
7162 seq_printf(seq, "\n ");
7163
NeilBrown57148962012-03-19 12:46:40 +11007164 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165
7166 seq_printf(seq, "\n");
7167 }
NeilBrown36d091f2014-12-15 12:56:58 +11007168 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007169
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 return 0;
7171}
7172
Jan Engelhardt110518b2009-05-07 12:49:37 +10007173static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 .start = md_seq_start,
7175 .next = md_seq_next,
7176 .stop = md_seq_stop,
7177 .show = md_seq_show,
7178};
7179
7180static int md_seq_open(struct inode *inode, struct file *file)
7181{
Kay Sieversf1514632011-07-12 20:48:39 +02007182 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 int error;
7184
7185 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007186 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007187 return error;
7188
7189 seq = file->private_data;
7190 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 return error;
7192}
7193
NeilBrowne2f23b62014-04-09 14:33:51 +10007194static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007195static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7196{
Kay Sieversf1514632011-07-12 20:48:39 +02007197 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007198 int mask;
7199
NeilBrowne2f23b62014-04-09 14:33:51 +10007200 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11007201 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08007202 poll_wait(filp, &md_event_waiters, wait);
7203
7204 /* always allow read */
7205 mask = POLLIN | POLLRDNORM;
7206
Kay Sieversf1514632011-07-12 20:48:39 +02007207 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007208 mask |= POLLERR | POLLPRI;
7209 return mask;
7210}
7211
Arjan van de Venfa027c22007-02-12 00:55:33 -08007212static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007213 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 .open = md_seq_open,
7215 .read = seq_read,
7216 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007217 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007218 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219};
7220
NeilBrown84fc4b52011-10-11 16:49:58 +11007221int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222{
Chao Yu50bd3772014-09-25 15:28:34 +08007223 printk(KERN_INFO "md: %s personality registered for level %d\n",
7224 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007226 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 spin_unlock(&pers_lock);
7228 return 0;
7229}
NeilBrown6c144d32014-09-30 16:15:38 +10007230EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231
NeilBrown84fc4b52011-10-11 16:49:58 +11007232int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233{
NeilBrown2604b702006-01-06 00:20:36 -08007234 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007236 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 spin_unlock(&pers_lock);
7238 return 0;
7239}
NeilBrown6c144d32014-09-30 16:15:38 +10007240EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007242int register_md_cluster_operations(struct md_cluster_operations *ops, struct module *module)
7243{
7244 if (md_cluster_ops != NULL)
7245 return -EALREADY;
7246 spin_lock(&pers_lock);
7247 md_cluster_ops = ops;
7248 md_cluster_mod = module;
7249 spin_unlock(&pers_lock);
7250 return 0;
7251}
7252EXPORT_SYMBOL(register_md_cluster_operations);
7253
7254int unregister_md_cluster_operations(void)
7255{
7256 spin_lock(&pers_lock);
7257 md_cluster_ops = NULL;
7258 spin_unlock(&pers_lock);
7259 return 0;
7260}
7261EXPORT_SYMBOL(unregister_md_cluster_operations);
7262
7263int md_setup_cluster(struct mddev *mddev, int nodes)
7264{
7265 int err;
7266
7267 err = request_module("md-cluster");
7268 if (err) {
7269 pr_err("md-cluster module not found.\n");
7270 return err;
7271 }
7272
7273 spin_lock(&pers_lock);
7274 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
7275 spin_unlock(&pers_lock);
7276 return -ENOENT;
7277 }
7278 spin_unlock(&pers_lock);
7279
7280 return md_cluster_ops->join(mddev);
7281}
7282
7283void md_cluster_stop(struct mddev *mddev)
7284{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05007285 if (!md_cluster_ops)
7286 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05007287 md_cluster_ops->leave(mddev);
7288 module_put(md_cluster_mod);
7289}
7290
NeilBrownfd01b882011-10-11 16:47:53 +11007291static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007293 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007295 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296
7297 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007298 rcu_read_lock();
7299 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007301 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7302 (int)part_stat_read(&disk->part0, sectors[1]) -
7303 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007304 /* sync IO will cause sync_io to increase before the disk_stats
7305 * as sync_io is counted when a request starts, and
7306 * disk_stats is counted when it completes.
7307 * So resync activity will cause curr_events to be smaller than
7308 * when there was no such activity.
7309 * non-sync IO will cause disk_stat to increase without
7310 * increasing sync_io so curr_events will (eventually)
7311 * be larger than it was before. Once it becomes
7312 * substantially larger, the test below will cause
7313 * the array to appear non-idle, and resync will slow
7314 * down.
7315 * If there is a lot of outstanding resync activity when
7316 * we set last_event to curr_events, then all that activity
7317 * completing might cause the array to appear non-idle
7318 * and resync will be slowed down even though there might
7319 * not have been non-resync activity. This will only
7320 * happen once though. 'last_events' will soon reflect
7321 * the state where there is little or no outstanding
7322 * resync requests, and further resync activity will
7323 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007324 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007326 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 rdev->last_events = curr_events;
7328 idle = 0;
7329 }
7330 }
NeilBrown4b809912008-07-21 17:05:25 +10007331 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 return idle;
7333}
7334
NeilBrownfd01b882011-10-11 16:47:53 +11007335void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336{
7337 /* another "blocks" (512byte) blocks have been synced */
7338 atomic_sub(blocks, &mddev->recovery_active);
7339 wake_up(&mddev->recovery_wait);
7340 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007341 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007342 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 md_wakeup_thread(mddev->thread);
7344 // stop recovery, signal do_sync ....
7345 }
7346}
NeilBrown6c144d32014-09-30 16:15:38 +10007347EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348
NeilBrown06d91a52005-06-21 17:17:12 -07007349/* md_write_start(mddev, bi)
7350 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007351 * in superblock) before writing, schedule a superblock update
7352 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007353 */
NeilBrownfd01b882011-10-11 16:47:53 +11007354void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355{
Neil Brown0fd62b82008-06-28 08:31:36 +10007356 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007357 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007358 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007359
NeilBrownf91de922005-11-08 21:39:36 -08007360 BUG_ON(mddev->ro == 1);
7361 if (mddev->ro == 2) {
7362 /* need to switch to read/write */
7363 mddev->ro = 0;
7364 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7365 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007366 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007367 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007368 }
NeilBrown06d91a52005-06-21 17:17:12 -07007369 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007370 if (mddev->safemode == 1)
7371 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007372 if (mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11007373 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007374 if (mddev->in_sync) {
7375 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007376 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007377 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007378 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007379 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007380 }
NeilBrown85572d72014-12-15 12:56:56 +11007381 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007382 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007383 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007384 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007385 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007386 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387}
NeilBrown6c144d32014-09-30 16:15:38 +10007388EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
NeilBrownfd01b882011-10-11 16:47:53 +11007390void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391{
7392 if (atomic_dec_and_test(&mddev->writes_pending)) {
7393 if (mddev->safemode == 2)
7394 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007395 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7397 }
7398}
NeilBrown6c144d32014-09-30 16:15:38 +10007399EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400
NeilBrown2a2275d2007-01-26 00:57:11 -08007401/* md_allow_write(mddev)
7402 * Calling this ensures that the array is marked 'active' so that writes
7403 * may proceed without blocking. It is important to call this before
7404 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7405 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007406 *
7407 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7408 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007409 */
NeilBrownfd01b882011-10-11 16:47:53 +11007410int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007411{
7412 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007413 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007414 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007415 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007416 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007417 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007418
NeilBrown85572d72014-12-15 12:56:56 +11007419 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007420 if (mddev->in_sync) {
7421 mddev->in_sync = 0;
7422 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007423 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007424 if (mddev->safemode_delay &&
7425 mddev->safemode == 0)
7426 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11007427 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08007428 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007429 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007430 } else
NeilBrown85572d72014-12-15 12:56:56 +11007431 spin_unlock(&mddev->lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007432
NeilBrown070dc6d2010-08-30 17:33:34 +10007433 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007434 return -EAGAIN;
7435 else
7436 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007437}
7438EXPORT_SYMBOL_GPL(md_allow_write);
7439
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440#define SYNC_MARKS 10
7441#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007442#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007443void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444{
Shaohua Li4ed87312012-10-11 13:34:00 +11007445 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007446 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 unsigned int currspeed = 0,
7448 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007449 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007451 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 sector_t mark_cnt[SYNC_MARKS];
7453 int last_mark,m;
7454 struct list_head *tmp;
7455 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007456 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007457 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007458 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007459 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460
7461 /* just incase thread restarts... */
7462 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7463 return;
NeilBrown3991b312014-05-28 13:39:23 +10007464 if (mddev->ro) {/* never try to sync a read-only array */
7465 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007466 return;
NeilBrown3991b312014-05-28 13:39:23 +10007467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468
NeilBrown61df9d92006-10-03 01:15:57 -07007469 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007470 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007471 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007472 action = "check";
7473 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007474 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007475 action = "repair";
7476 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007477 desc = "resync";
7478 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7479 desc = "reshape";
7480 else
7481 desc = "recovery";
7482
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007483 mddev->last_sync_action = action ?: desc;
7484
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 /* we overload curr_resync somewhat here.
7486 * 0 == not engaged in resync at all
7487 * 2 == checking that there is no conflict with another sync
7488 * 1 == like 2, but have yielded to allow conflicting resync to
7489 * commense
7490 * other == active in resync - this many blocks
7491 *
7492 * Before starting a resync we must have set curr_resync to
7493 * 2, and then checked that every "conflicting" array has curr_resync
7494 * less than ours. When we find one that is the same or higher
7495 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7496 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7497 * This will mean we have to start checking from the beginning again.
7498 *
7499 */
7500
7501 do {
7502 mddev->curr_resync = 2;
7503
7504 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007505 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007507 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 if (mddev2 == mddev)
7509 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007510 if (!mddev->parallel_resync
7511 && mddev2->curr_resync
7512 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 DEFINE_WAIT(wq);
7514 if (mddev < mddev2 && mddev->curr_resync == 2) {
7515 /* arbitrarily yield */
7516 mddev->curr_resync = 1;
7517 wake_up(&resync_wait);
7518 }
7519 if (mddev > mddev2 && mddev->curr_resync == 1)
7520 /* no need to wait here, we can wait the next
7521 * time 'round when curr_resync == 2
7522 */
7523 continue;
NeilBrown97441972008-09-19 11:49:54 +10007524 /* We need to wait 'interruptible' so as not to
7525 * contribute to the load average, and not to
7526 * be caught by 'softlockup'
7527 */
7528 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007529 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007530 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007531 printk(KERN_INFO "md: delaying %s of %s"
7532 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007534 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007536 if (signal_pending(current))
7537 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 schedule();
7539 finish_wait(&resync_wait, &wq);
7540 goto try_again;
7541 }
7542 finish_wait(&resync_wait, &wq);
7543 }
7544 }
7545 } while (mddev->curr_resync < 2);
7546
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007547 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007548 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007550 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 */
7552 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007553 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007554 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007555 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7556 j = mddev->resync_min;
7557 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007558 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007559
NeilBrownccfcc3c2006-03-27 01:18:09 -08007560 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007561 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007562 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007564 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007565 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007566 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007567 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007568 if (rdev->raid_disk >= 0 &&
7569 !test_bit(Faulty, &rdev->flags) &&
7570 !test_bit(In_sync, &rdev->flags) &&
7571 rdev->recovery_offset < j)
7572 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007573 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007574
7575 /* If there is a bitmap, we need to make sure all
7576 * writes that started before we added a spare
7577 * complete before we start doing a recovery.
7578 * Otherwise the write might complete and (via
7579 * bitmap_endwrite) set a bit in the bitmap after the
7580 * recovery has checked that bit and skipped that
7581 * region.
7582 */
7583 if (mddev->bitmap) {
7584 mddev->pers->quiesce(mddev, 1);
7585 mddev->pers->quiesce(mddev, 0);
7586 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588
NeilBrown61df9d92006-10-03 01:15:57 -07007589 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7590 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7591 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007592 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007593 "(but not more than %d KB/sec) for %s.\n",
7594 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595
NeilBrowneea1bf32009-03-31 14:27:02 +11007596 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007597
NeilBrown57afd892005-06-21 17:17:13 -07007598 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 for (m = 0; m < SYNC_MARKS; m++) {
7600 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007601 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602 }
7603 last_mark = 0;
7604 mddev->resync_mark = mark[last_mark];
7605 mddev->resync_mark_cnt = mark_cnt[last_mark];
7606
7607 /*
7608 * Tune reconstruction:
7609 */
7610 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007611 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7612 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613
7614 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 last_check = 0;
7616
7617 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007618 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007619 "md: resuming %s of %s from checkpoint.\n",
7620 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007622 } else
7623 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007624 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007625 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7626 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007627 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628
majianpeng7c2c57c2012-07-03 12:12:26 +10007629 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007631 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632
NeilBrown57afd892005-06-21 17:17:13 -07007633 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007634
NeilBrown7a91ee12009-05-26 12:57:21 +10007635 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7636 ((mddev->curr_resync > mddev->curr_resync_completed &&
7637 (mddev->curr_resync - mddev->curr_resync_completed)
7638 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007639 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007640 (j - mddev->curr_resync_completed)*2
7641 >= mddev->resync_max - mddev->curr_resync_completed
7642 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007643 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007644 wait_event(mddev->recovery_wait,
7645 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007646 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007647 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7648 j > mddev->recovery_cp)
7649 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007650 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007651 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007652 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007653 }
NeilBrownacb180b2009-04-14 16:28:34 +10007654
NeilBrownc91abf52013-11-19 12:02:01 +11007655 while (j >= mddev->resync_max &&
7656 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007657 /* As this condition is controlled by user-space,
7658 * we can block indefinitely, so use '_interruptible'
7659 * to avoid triggering warnings.
7660 */
7661 flush_signals(current); /* just in case */
7662 wait_event_interruptible(mddev->recovery_wait,
7663 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007664 || test_bit(MD_RECOVERY_INTR,
7665 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007666 }
NeilBrownacb180b2009-04-14 16:28:34 +10007667
NeilBrownc91abf52013-11-19 12:02:01 +11007668 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7669 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007670
NeilBrown57afd892005-06-21 17:17:13 -07007671 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007672 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007673 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007674 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007675 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 }
NeilBrown57afd892005-06-21 17:17:13 -07007677
7678 if (!skipped) { /* actual IO requested */
7679 io_sectors += sectors;
7680 atomic_add(sectors, &mddev->recovery_active);
7681 }
7682
NeilBrowne875ece2011-07-28 11:39:24 +10007683 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7684 break;
7685
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007687 if (j > 2)
7688 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007689 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007690 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007691 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007692 * visible in /proc/mdstat
7693 */
7694 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007695
7696 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 continue;
7698
NeilBrown57afd892005-06-21 17:17:13 -07007699 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700 repeat:
7701 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7702 /* step marks */
7703 int next = (last_mark+1) % SYNC_MARKS;
7704
7705 mddev->resync_mark = mark[next];
7706 mddev->resync_mark_cnt = mark_cnt[next];
7707 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007708 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 last_mark = next;
7710 }
7711
NeilBrownc91abf52013-11-19 12:02:01 +11007712 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7713 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714
7715 /*
7716 * this loop exits only if either when we are slower than
7717 * the 'hard' speed limit, or the system was IO-idle for
7718 * a jiffy.
7719 * the system might be non-idle CPU-wise, but we only care
7720 * about not overloading the IO subsystem. (things like an
7721 * e2fsck being done on the RAID array should execute fast)
7722 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 cond_resched();
7724
Xiao Niac7e50a2014-08-07 09:37:41 -04007725 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7726 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007727 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728
NeilBrown88202a02006-01-06 00:21:36 -08007729 if (currspeed > speed_min(mddev)) {
7730 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007731 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007732 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 goto repeat;
7734 }
7735 }
7736 }
NeilBrownc91abf52013-11-19 12:02:01 +11007737 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7738 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7739 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 /*
7741 * this also signals 'finished resyncing' to md_stop
7742 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007743 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7745
7746 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007747 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748
NeilBrowndfc70642008-05-23 13:04:39 -07007749 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007750 mddev->curr_resync > 2) {
7751 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7752 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7753 if (mddev->curr_resync >= mddev->recovery_cp) {
7754 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007755 "md: checkpointing %s of %s.\n",
7756 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007757 if (test_bit(MD_RECOVERY_ERROR,
7758 &mddev->recovery))
7759 mddev->recovery_cp =
7760 mddev->curr_resync_completed;
7761 else
7762 mddev->recovery_cp =
7763 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007764 }
7765 } else
7766 mddev->recovery_cp = MaxSector;
7767 } else {
7768 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7769 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007770 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007771 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007772 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007773 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007774 !test_bit(Faulty, &rdev->flags) &&
7775 !test_bit(In_sync, &rdev->flags) &&
7776 rdev->recovery_offset < mddev->curr_resync)
7777 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007778 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007781 skip:
NeilBrown17571282006-12-10 02:20:52 -08007782 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783
NeilBrown23da4222014-12-15 12:57:01 +11007784 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11007785 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7786 /* We completed so min/max setting can be forgotten if used. */
7787 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7788 mddev->resync_min = 0;
7789 mddev->resync_max = MaxSector;
7790 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7791 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007793 spin_unlock(&mddev->lock);
7794
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 wake_up(&resync_wait);
7796 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7797 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007798 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799}
NeilBrown29269552006-03-27 01:18:10 -08007800EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801
NeilBrown746d3202013-04-24 11:42:41 +10007802static int remove_and_add_spares(struct mddev *mddev,
7803 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007804{
NeilBrown3cb03002011-10-11 16:45:26 +11007805 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007806 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007807 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007808
NeilBrowndafb20f2012-03-19 12:46:39 +11007809 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007810 if ((this == NULL || rdev == this) &&
7811 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007812 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007813 (test_bit(Faulty, &rdev->flags) ||
7814 ! test_bit(In_sync, &rdev->flags)) &&
7815 atomic_read(&rdev->nr_pending)==0) {
7816 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007817 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007818 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007819 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007820 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007821 }
7822 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007823 if (removed && mddev->kobj.sd)
7824 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007825
NeilBrown746d3202013-04-24 11:42:41 +10007826 if (this)
7827 goto no_add;
7828
NeilBrowndafb20f2012-03-19 12:46:39 +11007829 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007830 if (rdev->raid_disk >= 0 &&
7831 !test_bit(In_sync, &rdev->flags) &&
7832 !test_bit(Faulty, &rdev->flags))
7833 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007834 if (rdev->raid_disk >= 0)
7835 continue;
7836 if (test_bit(Faulty, &rdev->flags))
7837 continue;
7838 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007839 ! (rdev->saved_raid_disk >= 0 &&
7840 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007841 continue;
7842
NeilBrown7eb41882014-01-14 15:55:14 +11007843 if (rdev->saved_raid_disk < 0)
7844 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007845 if (mddev->pers->
7846 hot_add_disk(mddev, rdev) == 0) {
7847 if (sysfs_link_rdev(mddev, rdev))
7848 /* failure here is OK */;
7849 spares++;
7850 md_new_event(mddev);
7851 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007852 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007853 }
NeilBrown746d3202013-04-24 11:42:41 +10007854no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007855 if (removed)
7856 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007857 return spares;
7858}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007859
NeilBrownac05f252014-09-30 08:10:42 +10007860static void md_start_sync(struct work_struct *ws)
7861{
7862 struct mddev *mddev = container_of(ws, struct mddev, del_work);
7863
7864 mddev->sync_thread = md_register_thread(md_do_sync,
7865 mddev,
7866 "resync");
7867 if (!mddev->sync_thread) {
7868 printk(KERN_ERR "%s: could not start resync"
7869 " thread...\n",
7870 mdname(mddev));
7871 /* leave the spares where they are, it shouldn't hurt */
7872 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7873 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7874 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7875 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7876 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007877 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10007878 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7879 &mddev->recovery))
7880 if (mddev->sysfs_action)
7881 sysfs_notify_dirent_safe(mddev->sysfs_action);
7882 } else
7883 md_wakeup_thread(mddev->sync_thread);
7884 sysfs_notify_dirent_safe(mddev->sysfs_action);
7885 md_new_event(mddev);
7886}
7887
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888/*
7889 * This routine is regularly called by all per-raid-array threads to
7890 * deal with generic issues like resync and super-block update.
7891 * Raid personalities that don't have a thread (linear/raid0) do not
7892 * need this as they never do any recovery or update the superblock.
7893 *
7894 * It does not do any resync itself, but rather "forks" off other threads
7895 * to do that as needed.
7896 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7897 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007898 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 * and wakeups up this thread which will reap the thread and finish up.
7900 * This thread also removes any faulty devices (with nr_pending == 0).
7901 *
7902 * The overall approach is:
7903 * 1/ if the superblock needs updating, update it.
7904 * 2/ If a recovery thread is running, don't do anything else.
7905 * 3/ If recovery has finished, clean up, possibly marking spares active.
7906 * 4/ If there are any faulty devices, remove them.
7907 * 5/ If array is degraded, try to add spares devices
7908 * 6/ If array has spares or is not in-sync, start a resync thread.
7909 */
NeilBrownfd01b882011-10-11 16:47:53 +11007910void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007912 if (mddev->suspended)
7913 return;
7914
NeilBrown5f404022005-06-21 17:17:16 -07007915 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007916 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917
NeilBrownfca4d842005-06-21 17:17:11 -07007918 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007919 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007920 printk(KERN_INFO "md: %s in immediate safe mode\n",
7921 mdname(mddev));
7922 mddev->safemode = 2;
7923 }
7924 flush_signals(current);
7925 }
7926
NeilBrownc89a8ee2008-08-05 15:54:13 +10007927 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7928 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007930 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007932 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007933 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007934 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7935 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 ))
7937 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007938
NeilBrowndf5b89b2006-03-27 01:18:20 -08007939 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007940 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007941
NeilBrownc89a8ee2008-08-05 15:54:13 +10007942 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007943 /* On a read-only array we can:
7944 * - remove failed devices
7945 * - add already-in_sync devices if the array itself
7946 * is in-sync.
7947 * As we only add devices that are already in-sync,
7948 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007949 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10007950 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11007951 /* There is no thread, but we need to call
7952 * ->spare_active and clear saved_raid_disk
7953 */
NeilBrown2ac295a2014-05-29 11:40:03 +10007954 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11007955 md_reap_sync_thread(mddev);
7956 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007957 goto unlock;
7958 }
7959
NeilBrown31a59e32008-04-30 00:52:30 -07007960 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007961 int did_change = 0;
NeilBrown85572d72014-12-15 12:56:56 +11007962 spin_lock(&mddev->lock);
NeilBrown31a59e32008-04-30 00:52:30 -07007963 if (mddev->safemode &&
7964 !atomic_read(&mddev->writes_pending) &&
7965 !mddev->in_sync &&
7966 mddev->recovery_cp == MaxSector) {
7967 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007968 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007969 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007970 }
7971 if (mddev->safemode == 1)
7972 mddev->safemode = 0;
NeilBrown85572d72014-12-15 12:56:56 +11007973 spin_unlock(&mddev->lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007974 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007975 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007976 }
NeilBrownfca4d842005-06-21 17:17:11 -07007977
NeilBrown7a0a5352013-08-27 16:28:23 +10007978 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07007979 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007980
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7982 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7983 /* resync/recovery still happening */
7984 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7985 goto unlock;
7986 }
7987 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007988 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 goto unlock;
7990 }
Neil Brown72a23c22008-06-28 08:31:41 +10007991 /* Set RUNNING before clearing NEEDED to avoid
7992 * any transients in the value of "sync_action".
7993 */
NeilBrown72f36d52012-10-11 14:25:57 +11007994 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11007995 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10007996 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11007997 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08007998 /* Clear some bits that don't mean anything, but
7999 * might be left set
8000 */
NeilBrown24dd4692005-11-08 21:39:26 -08008001 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
8002 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003
NeilBrowned209582012-04-24 10:23:14 +10008004 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
8005 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10008006 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 /* no recovery is running.
8008 * remove any failed drives, then
8009 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11008010 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008011 * the personality to fail the re-add.
8012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008014 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10008015 if (mddev->pers->check_reshape == NULL ||
8016 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008017 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10008018 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008019 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008020 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10008021 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08008022 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8023 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07008024 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008025 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008026 } else if (mddev->recovery_cp < MaxSector) {
8027 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10008028 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08008029 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
8030 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10008031 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08008032
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10008034 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07008035 /* We are adding a device or devices to an array
8036 * which has the bitmap stored on all devices.
8037 * So make sure all bitmap pages get written
8038 */
8039 bitmap_write_all(mddev->bitmap);
8040 }
NeilBrownac05f252014-09-30 08:10:42 +10008041 INIT_WORK(&mddev->del_work, md_start_sync);
8042 queue_work(md_misc_wq, &mddev->del_work);
8043 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044 }
NeilBrownac05f252014-09-30 08:10:42 +10008045 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10008046 if (!mddev->sync_thread) {
8047 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008048 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10008049 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8050 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11008051 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008052 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10008053 }
NeilBrownac05f252014-09-30 08:10:42 +10008054 unlock:
8055 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056 mddev_unlock(mddev);
8057 }
8058}
NeilBrown6c144d32014-09-30 16:15:38 +10008059EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008061void md_reap_sync_thread(struct mddev *mddev)
8062{
8063 struct md_rdev *rdev;
8064
8065 /* resync has finished, collect result */
8066 md_unregister_thread(&mddev->sync_thread);
8067 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8068 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
8069 /* success...*/
8070 /* activate any spares */
8071 if (mddev->pers->spare_active(mddev)) {
8072 sysfs_notify(&mddev->kobj, NULL,
8073 "degraded");
8074 set_bit(MD_CHANGE_DEVS, &mddev->flags);
8075 }
8076 }
8077 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8078 mddev->pers->finish_reshape)
8079 mddev->pers->finish_reshape(mddev);
8080
8081 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11008082 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008083 */
NeilBrownf4667222013-12-09 12:04:56 +11008084 if (!mddev->degraded)
8085 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008086 rdev->saved_raid_disk = -1;
8087
8088 md_update_sb(mddev, 1);
8089 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8090 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8091 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8092 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8093 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008094 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008095 /* flag recovery needed just to double check */
8096 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8097 sysfs_notify_dirent_safe(mddev->sysfs_action);
8098 md_new_event(mddev);
8099 if (mddev->event_work.func)
8100 queue_work(md_misc_wq, &mddev->event_work);
8101}
NeilBrown6c144d32014-09-30 16:15:38 +10008102EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008103
NeilBrownfd01b882011-10-11 16:47:53 +11008104void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008105{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008106 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008107 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008108 !test_bit(Blocked, &rdev->flags) &&
8109 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008110 msecs_to_jiffies(5000));
8111 rdev_dec_pending(rdev, mddev);
8112}
8113EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8114
NeilBrownc6563a82012-05-21 09:27:00 +10008115void md_finish_reshape(struct mddev *mddev)
8116{
8117 /* called be personality module when reshape completes. */
8118 struct md_rdev *rdev;
8119
8120 rdev_for_each(rdev, mddev) {
8121 if (rdev->data_offset > rdev->new_data_offset)
8122 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8123 else
8124 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8125 rdev->data_offset = rdev->new_data_offset;
8126 }
8127}
8128EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008129
8130/* Bad block management.
8131 * We can record which blocks on each device are 'bad' and so just
8132 * fail those blocks, or that stripe, rather than the whole device.
8133 * Entries in the bad-block table are 64bits wide. This comprises:
8134 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8135 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8136 * A 'shift' can be set so that larger blocks are tracked and
8137 * consequently larger devices can be covered.
8138 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8139 *
8140 * Locking of the bad-block table uses a seqlock so md_is_badblock
8141 * might need to retry if it is very unlucky.
8142 * We will sometimes want to check for bad blocks in a bi_end_io function,
8143 * so we use the write_seqlock_irq variant.
8144 *
8145 * When looking for a bad block we specify a range and want to
8146 * know if any block in the range is bad. So we binary-search
8147 * to the last range that starts at-or-before the given endpoint,
8148 * (or "before the sector after the target range")
8149 * then see if it ends after the given start.
8150 * We return
8151 * 0 if there are no known bad blocks in the range
8152 * 1 if there are known bad block which are all acknowledged
8153 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8154 * plus the start/length of the first bad section we overlap.
8155 */
8156int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8157 sector_t *first_bad, int *bad_sectors)
8158{
8159 int hi;
majianpengab056132012-11-06 17:13:44 +08008160 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008161 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008162 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008163 sector_t target = s + sectors;
8164 unsigned seq;
8165
8166 if (bb->shift > 0) {
8167 /* round the start down, and the end up */
8168 s >>= bb->shift;
8169 target += (1<<bb->shift) - 1;
8170 target >>= bb->shift;
8171 sectors = target - s;
8172 }
8173 /* 'target' is now the first block after the bad range */
8174
8175retry:
8176 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008177 lo = 0;
8178 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008179 hi = bb->count;
8180
8181 /* Binary search between lo and hi for 'target'
8182 * i.e. for the last range that starts before 'target'
8183 */
8184 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8185 * are known not to be the last range before target.
8186 * VARIANT: hi-lo is the number of possible
8187 * ranges, and decreases until it reaches 1
8188 */
8189 while (hi - lo > 1) {
8190 int mid = (lo + hi) / 2;
8191 sector_t a = BB_OFFSET(p[mid]);
8192 if (a < target)
8193 /* This could still be the one, earlier ranges
8194 * could not. */
8195 lo = mid;
8196 else
8197 /* This and later ranges are definitely out. */
8198 hi = mid;
8199 }
8200 /* 'lo' might be the last that started before target, but 'hi' isn't */
8201 if (hi > lo) {
8202 /* need to check all range that end after 's' to see if
8203 * any are unacknowledged.
8204 */
8205 while (lo >= 0 &&
8206 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8207 if (BB_OFFSET(p[lo]) < target) {
8208 /* starts before the end, and finishes after
8209 * the start, so they must overlap
8210 */
8211 if (rv != -1 && BB_ACK(p[lo]))
8212 rv = 1;
8213 else
8214 rv = -1;
8215 *first_bad = BB_OFFSET(p[lo]);
8216 *bad_sectors = BB_LEN(p[lo]);
8217 }
8218 lo--;
8219 }
8220 }
8221
8222 if (read_seqretry(&bb->lock, seq))
8223 goto retry;
8224
8225 return rv;
8226}
8227EXPORT_SYMBOL_GPL(md_is_badblock);
8228
8229/*
8230 * Add a range of bad blocks to the table.
8231 * This might extend the table, or might contract it
8232 * if two adjacent ranges can be merged.
8233 * We binary-search to find the 'insertion' point, then
8234 * decide how best to handle it.
8235 */
8236static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8237 int acknowledged)
8238{
8239 u64 *p;
8240 int lo, hi;
8241 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008242 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008243
8244 if (bb->shift < 0)
8245 /* badblocks are disabled */
8246 return 0;
8247
8248 if (bb->shift) {
8249 /* round the start down, and the end up */
8250 sector_t next = s + sectors;
8251 s >>= bb->shift;
8252 next += (1<<bb->shift) - 1;
8253 next >>= bb->shift;
8254 sectors = next - s;
8255 }
8256
Bian Yu905b0292013-10-12 01:10:03 -04008257 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008258
8259 p = bb->page;
8260 lo = 0;
8261 hi = bb->count;
8262 /* Find the last range that starts at-or-before 's' */
8263 while (hi - lo > 1) {
8264 int mid = (lo + hi) / 2;
8265 sector_t a = BB_OFFSET(p[mid]);
8266 if (a <= s)
8267 lo = mid;
8268 else
8269 hi = mid;
8270 }
8271 if (hi > lo && BB_OFFSET(p[lo]) > s)
8272 hi = lo;
8273
8274 if (hi > lo) {
8275 /* we found a range that might merge with the start
8276 * of our new range
8277 */
8278 sector_t a = BB_OFFSET(p[lo]);
8279 sector_t e = a + BB_LEN(p[lo]);
8280 int ack = BB_ACK(p[lo]);
8281 if (e >= s) {
8282 /* Yes, we can merge with a previous range */
8283 if (s == a && s + sectors >= e)
8284 /* new range covers old */
8285 ack = acknowledged;
8286 else
8287 ack = ack && acknowledged;
8288
8289 if (e < s + sectors)
8290 e = s + sectors;
8291 if (e - a <= BB_MAX_LEN) {
8292 p[lo] = BB_MAKE(a, e-a, ack);
8293 s = e;
8294 } else {
8295 /* does not all fit in one range,
8296 * make p[lo] maximal
8297 */
8298 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8299 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8300 s = a + BB_MAX_LEN;
8301 }
8302 sectors = e - s;
8303 }
8304 }
8305 if (sectors && hi < bb->count) {
8306 /* 'hi' points to the first range that starts after 's'.
8307 * Maybe we can merge with the start of that range */
8308 sector_t a = BB_OFFSET(p[hi]);
8309 sector_t e = a + BB_LEN(p[hi]);
8310 int ack = BB_ACK(p[hi]);
8311 if (a <= s + sectors) {
8312 /* merging is possible */
8313 if (e <= s + sectors) {
8314 /* full overlap */
8315 e = s + sectors;
8316 ack = acknowledged;
8317 } else
8318 ack = ack && acknowledged;
8319
8320 a = s;
8321 if (e - a <= BB_MAX_LEN) {
8322 p[hi] = BB_MAKE(a, e-a, ack);
8323 s = e;
8324 } else {
8325 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8326 s = a + BB_MAX_LEN;
8327 }
8328 sectors = e - s;
8329 lo = hi;
8330 hi++;
8331 }
8332 }
8333 if (sectors == 0 && hi < bb->count) {
8334 /* we might be able to combine lo and hi */
8335 /* Note: 's' is at the end of 'lo' */
8336 sector_t a = BB_OFFSET(p[hi]);
8337 int lolen = BB_LEN(p[lo]);
8338 int hilen = BB_LEN(p[hi]);
8339 int newlen = lolen + hilen - (s - a);
8340 if (s >= a && newlen < BB_MAX_LEN) {
8341 /* yes, we can combine them */
8342 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8343 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8344 memmove(p + hi, p + hi + 1,
8345 (bb->count - hi - 1) * 8);
8346 bb->count--;
8347 }
8348 }
8349 while (sectors) {
8350 /* didn't merge (it all).
8351 * Need to add a range just before 'hi' */
8352 if (bb->count >= MD_MAX_BADBLOCKS) {
8353 /* No room for more */
8354 rv = 0;
8355 break;
8356 } else {
8357 int this_sectors = sectors;
8358 memmove(p + hi + 1, p + hi,
8359 (bb->count - hi) * 8);
8360 bb->count++;
8361
8362 if (this_sectors > BB_MAX_LEN)
8363 this_sectors = BB_MAX_LEN;
8364 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8365 sectors -= this_sectors;
8366 s += this_sectors;
8367 }
8368 }
8369
8370 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008371 if (!acknowledged)
8372 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008373 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008374
8375 return rv;
8376}
8377
NeilBrown3cb03002011-10-11 16:45:26 +11008378int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008379 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008380{
NeilBrownc6563a82012-05-21 09:27:00 +10008381 int rv;
8382 if (is_new)
8383 s += rdev->new_data_offset;
8384 else
8385 s += rdev->data_offset;
8386 rv = md_set_badblocks(&rdev->badblocks,
8387 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008388 if (rv) {
8389 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008390 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008391 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8392 md_wakeup_thread(rdev->mddev->thread);
8393 }
8394 return rv;
8395}
8396EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8397
8398/*
8399 * Remove a range of bad blocks from the table.
8400 * This may involve extending the table if we spilt a region,
8401 * but it must not fail. So if the table becomes full, we just
8402 * drop the remove request.
8403 */
8404static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8405{
8406 u64 *p;
8407 int lo, hi;
8408 sector_t target = s + sectors;
8409 int rv = 0;
8410
8411 if (bb->shift > 0) {
8412 /* When clearing we round the start up and the end down.
8413 * This should not matter as the shift should align with
8414 * the block size and no rounding should ever be needed.
8415 * However it is better the think a block is bad when it
8416 * isn't than to think a block is not bad when it is.
8417 */
8418 s += (1<<bb->shift) - 1;
8419 s >>= bb->shift;
8420 target >>= bb->shift;
8421 sectors = target - s;
8422 }
8423
8424 write_seqlock_irq(&bb->lock);
8425
8426 p = bb->page;
8427 lo = 0;
8428 hi = bb->count;
8429 /* Find the last range that starts before 'target' */
8430 while (hi - lo > 1) {
8431 int mid = (lo + hi) / 2;
8432 sector_t a = BB_OFFSET(p[mid]);
8433 if (a < target)
8434 lo = mid;
8435 else
8436 hi = mid;
8437 }
8438 if (hi > lo) {
8439 /* p[lo] is the last range that could overlap the
8440 * current range. Earlier ranges could also overlap,
8441 * but only this one can overlap the end of the range.
8442 */
8443 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8444 /* Partial overlap, leave the tail of this range */
8445 int ack = BB_ACK(p[lo]);
8446 sector_t a = BB_OFFSET(p[lo]);
8447 sector_t end = a + BB_LEN(p[lo]);
8448
8449 if (a < s) {
8450 /* we need to split this range */
8451 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008452 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008453 goto out;
8454 }
8455 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8456 bb->count++;
8457 p[lo] = BB_MAKE(a, s-a, ack);
8458 lo++;
8459 }
8460 p[lo] = BB_MAKE(target, end - target, ack);
8461 /* there is no longer an overlap */
8462 hi = lo;
8463 lo--;
8464 }
8465 while (lo >= 0 &&
8466 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8467 /* This range does overlap */
8468 if (BB_OFFSET(p[lo]) < s) {
8469 /* Keep the early parts of this range. */
8470 int ack = BB_ACK(p[lo]);
8471 sector_t start = BB_OFFSET(p[lo]);
8472 p[lo] = BB_MAKE(start, s - start, ack);
8473 /* now low doesn't overlap, so.. */
8474 break;
8475 }
8476 lo--;
8477 }
8478 /* 'lo' is strictly before, 'hi' is strictly after,
8479 * anything between needs to be discarded
8480 */
8481 if (hi - lo > 1) {
8482 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8483 bb->count -= (hi - lo - 1);
8484 }
8485 }
8486
8487 bb->changed = 1;
8488out:
8489 write_sequnlock_irq(&bb->lock);
8490 return rv;
8491}
8492
NeilBrownc6563a82012-05-21 09:27:00 +10008493int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8494 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008495{
NeilBrownc6563a82012-05-21 09:27:00 +10008496 if (is_new)
8497 s += rdev->new_data_offset;
8498 else
8499 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008500 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008501 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008502}
8503EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8504
8505/*
8506 * Acknowledge all bad blocks in a list.
8507 * This only succeeds if ->changed is clear. It is used by
8508 * in-kernel metadata updates
8509 */
8510void md_ack_all_badblocks(struct badblocks *bb)
8511{
8512 if (bb->page == NULL || bb->changed)
8513 /* no point even trying */
8514 return;
8515 write_seqlock_irq(&bb->lock);
8516
majianpengecb178b2012-03-19 12:46:42 +11008517 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008518 u64 *p = bb->page;
8519 int i;
8520 for (i = 0; i < bb->count ; i++) {
8521 if (!BB_ACK(p[i])) {
8522 sector_t start = BB_OFFSET(p[i]);
8523 int len = BB_LEN(p[i]);
8524 p[i] = BB_MAKE(start, len, 1);
8525 }
8526 }
NeilBrownde393cd2011-07-28 11:31:48 +10008527 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008528 }
8529 write_sequnlock_irq(&bb->lock);
8530}
8531EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8532
NeilBrown16c791a2011-07-28 11:31:47 +10008533/* sysfs access to bad-blocks list.
8534 * We present two files.
8535 * 'bad-blocks' lists sector numbers and lengths of ranges that
8536 * are recorded as bad. The list is truncated to fit within
8537 * the one-page limit of sysfs.
8538 * Writing "sector length" to this file adds an acknowledged
8539 * bad block list.
8540 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8541 * been acknowledged. Writing to this file adds bad blocks
8542 * without acknowledging them. This is largely for testing.
8543 */
8544
8545static ssize_t
8546badblocks_show(struct badblocks *bb, char *page, int unack)
8547{
8548 size_t len;
8549 int i;
8550 u64 *p = bb->page;
8551 unsigned seq;
8552
8553 if (bb->shift < 0)
8554 return 0;
8555
8556retry:
8557 seq = read_seqbegin(&bb->lock);
8558
8559 len = 0;
8560 i = 0;
8561
8562 while (len < PAGE_SIZE && i < bb->count) {
8563 sector_t s = BB_OFFSET(p[i]);
8564 unsigned int length = BB_LEN(p[i]);
8565 int ack = BB_ACK(p[i]);
8566 i++;
8567
8568 if (unack && ack)
8569 continue;
8570
8571 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8572 (unsigned long long)s << bb->shift,
8573 length << bb->shift);
8574 }
NeilBrownde393cd2011-07-28 11:31:48 +10008575 if (unack && len == 0)
8576 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008577
8578 if (read_seqretry(&bb->lock, seq))
8579 goto retry;
8580
8581 return len;
8582}
8583
8584#define DO_DEBUG 1
8585
8586static ssize_t
8587badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8588{
8589 unsigned long long sector;
8590 int length;
8591 char newline;
8592#ifdef DO_DEBUG
8593 /* Allow clearing via sysfs *only* for testing/debugging.
8594 * Normally only a successful write may clear a badblock
8595 */
8596 int clear = 0;
8597 if (page[0] == '-') {
8598 clear = 1;
8599 page++;
8600 }
8601#endif /* DO_DEBUG */
8602
8603 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8604 case 3:
8605 if (newline != '\n')
8606 return -EINVAL;
8607 case 2:
8608 if (length <= 0)
8609 return -EINVAL;
8610 break;
8611 default:
8612 return -EINVAL;
8613 }
8614
8615#ifdef DO_DEBUG
8616 if (clear) {
8617 md_clear_badblocks(bb, sector, length);
8618 return len;
8619 }
8620#endif /* DO_DEBUG */
8621 if (md_set_badblocks(bb, sector, length, !unack))
8622 return len;
8623 else
8624 return -ENOSPC;
8625}
8626
Adrian Bunk75c96f82005-05-05 16:16:09 -07008627static int md_notify_reboot(struct notifier_block *this,
8628 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629{
8630 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008631 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008632 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008633
NeilBrownc744a652012-03-19 12:46:37 +11008634 for_each_mddev(mddev, tmp) {
8635 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008636 if (mddev->pers)
8637 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008638 if (mddev->persistent)
8639 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008640 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008641 }
NeilBrownc744a652012-03-19 12:46:37 +11008642 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643 }
NeilBrownc744a652012-03-19 12:46:37 +11008644 /*
8645 * certain more exotic SCSI devices are known to be
8646 * volatile wrt too early system reboots. While the
8647 * right place to handle this issue is the given
8648 * driver, we do want to have a safe RAID driver ...
8649 */
8650 if (need_delay)
8651 mdelay(1000*1);
8652
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653 return NOTIFY_DONE;
8654}
8655
Adrian Bunk75c96f82005-05-05 16:16:09 -07008656static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657 .notifier_call = md_notify_reboot,
8658 .next = NULL,
8659 .priority = INT_MAX, /* before any real devices */
8660};
8661
8662static void md_geninit(void)
8663{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008664 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008666 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667}
8668
Adrian Bunk75c96f82005-05-05 16:16:09 -07008669static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670{
Tejun Heoe804ac72010-10-15 15:36:08 +02008671 int ret = -ENOMEM;
8672
Tejun Heoada609e2011-01-25 14:35:54 +01008673 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008674 if (!md_wq)
8675 goto err_wq;
8676
8677 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8678 if (!md_misc_wq)
8679 goto err_misc_wq;
8680
8681 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8682 goto err_md;
8683
8684 if ((ret = register_blkdev(0, "mdp")) < 0)
8685 goto err_mdp;
8686 mdp_major = ret;
8687
NeilBrownaf5628f2014-07-31 13:54:54 +10008688 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008689 md_probe, NULL, NULL);
8690 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691 md_probe, NULL, NULL);
8692
Linus Torvalds1da177e2005-04-16 15:20:36 -07008693 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008694 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695
8696 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698
Tejun Heoe804ac72010-10-15 15:36:08 +02008699err_mdp:
8700 unregister_blkdev(MD_MAJOR, "md");
8701err_md:
8702 destroy_workqueue(md_misc_wq);
8703err_misc_wq:
8704 destroy_workqueue(md_wq);
8705err_wq:
8706 return ret;
8707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008708
8709#ifndef MODULE
8710
8711/*
8712 * Searches all registered partitions for autorun RAID arrays
8713 * at boot time.
8714 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008715
8716static LIST_HEAD(all_detected_devices);
8717struct detected_devices_node {
8718 struct list_head list;
8719 dev_t dev;
8720};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721
8722void md_autodetect_dev(dev_t dev)
8723{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008724 struct detected_devices_node *node_detected_dev;
8725
8726 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8727 if (node_detected_dev) {
8728 node_detected_dev->dev = dev;
8729 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8730 } else {
8731 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8732 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734}
8735
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736static void autostart_arrays(int part)
8737{
NeilBrown3cb03002011-10-11 16:45:26 +11008738 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008739 struct detected_devices_node *node_detected_dev;
8740 dev_t dev;
8741 int i_scanned, i_passed;
8742
8743 i_scanned = 0;
8744 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745
8746 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8747
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008748 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8749 i_scanned++;
8750 node_detected_dev = list_entry(all_detected_devices.next,
8751 struct detected_devices_node, list);
8752 list_del(&node_detected_dev->list);
8753 dev = node_detected_dev->dev;
8754 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008755 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008756 if (IS_ERR(rdev))
8757 continue;
8758
NeilBrown403df472014-09-30 15:52:29 +10008759 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008760 continue;
NeilBrown403df472014-09-30 15:52:29 +10008761
NeilBrownd0fae182008-03-04 14:29:31 -08008762 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008764 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008765 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008766
8767 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8768 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769
8770 autorun_devices(part);
8771}
8772
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008773#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008774
8775static __exit void md_exit(void)
8776{
NeilBrownfd01b882011-10-11 16:47:53 +11008777 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008778 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008779 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008780
NeilBrownaf5628f2014-07-31 13:54:54 +10008781 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008782 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008783
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008784 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008785 unregister_blkdev(mdp_major, "mdp");
8786 unregister_reboot_notifier(&md_notifier);
8787 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008788
8789 /* We cannot unload the modules while some process is
8790 * waiting for us in select() or poll() - wake them up
8791 */
8792 md_unloading = 1;
8793 while (waitqueue_active(&md_event_waiters)) {
8794 /* not safe to leave yet */
8795 wake_up(&md_event_waiters);
8796 msleep(delay);
8797 delay += delay;
8798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008800
NeilBrown29ac4aa2008-02-06 01:39:58 -08008801 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008802 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008803 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008804 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008805 destroy_workqueue(md_misc_wq);
8806 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008807}
8808
Dan Williams685784a2007-07-09 11:56:42 -07008809subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008810module_exit(md_exit)
8811
NeilBrownf91de922005-11-08 21:39:36 -08008812static int get_ro(char *buffer, struct kernel_param *kp)
8813{
8814 return sprintf(buffer, "%d", start_readonly);
8815}
8816static int set_ro(const char *val, struct kernel_param *kp)
8817{
8818 char *e;
8819 int num = simple_strtoul(val, &e, 10);
8820 if (*val && (*e == '\0' || *e == '\n')) {
8821 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008822 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008823 }
8824 return -EINVAL;
8825}
8826
NeilBrown80ca3a42006-07-10 04:44:18 -07008827module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8828module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008829module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008830
Linus Torvalds1da177e2005-04-16 15:20:36 -07008831MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008832MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008833MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008834MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);